iio: Add channel for Phase
Subsystems (2)
PATCH 1/2 iio: Add channel for Phase
Add new channel type support for phase.
This channel may be used by Time-of-flight sensors to express the phase difference between emitted and received signals. Those sensor will then use the phase shift of return signals to approximate the distance to objects.
--- Documentation/ABI/testing/sysfs-bus-iio | 7 +++++++ drivers/iio/industrialio-core.c | 1 + include/uapi/linux/iio/types.h | 1 + tools/iio/iio_event_monitor.c | 2 ++ 4 files changed, 11 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +7 -0
@@ -1663,3 +1663,10 @@ KernelVersion: 4.12 Contact: [email protected] Description: Raw counter device counters direction for channel Y. + +What: /sys/bus/iio/devices/iio:deviceX/in_phaseY_raw +KernelVersion: 4.18 +Contact: [email protected] +Description: + Raw (unscaled) phase difference reading from channel Y + that can be processed to radians.
drivers/iio/industrialio-core.c +1 -0
@@ -85,6 +85,7 @@ static const char * const iio_chan_type_name_spec[] = { [IIO_COUNT] = "count", [IIO_INDEX] = "index", [IIO_GRAVITY] = "gravity", + [IIO_PHASE] = "phase", };static const char *const iio_modifier_names[] = {
include/uapi/linux/iio/types.h +1 -0
@@ -44,6 +44,7 @@ enum iio_chan_type {IIO_COUNT ,IIO_INDEX ,IIO_GRAVITY , + IIO_PHASE, }; enum iio_modifier {
tools/iio/iio_event_monitor.c +2 -0
@@ -58,6 +58,7 @@ static const char * const iio_chan_type_name_spec[] = { [IIO_PH] = "ph", [IIO_UVINDEX] = "uvindex", [IIO_GRAVITY] = "gravity", + [IIO_PHASE] = "phase", };static const char *const iio_ev_type_text[] = {
@@ -151,6 +152,7 @@ static bool event_is_known(struct iio_event_data *event)case IIO_PH :case IIO_UVINDEX :case IIO_GRAVITY : +case IIO_PHASE :break ;default :return false;
Signature · 1 line
2.7.4
PATCH 2/2 iio: light: isl29501: Add support for the ISL29501 ToF sensor.
This patch adds support for the ISL29501 Time of Flight sensor.
--- Documentation/ABI/testing/sysfs-bus-iio-isl29501 | 49 + .../devicetree/bindings/iio/light/isl29501.txt | 13 + drivers/iio/light/Kconfig | 13 + drivers/iio/light/Makefile | 1 + drivers/iio/light/isl29501.c | 1212 ++++++++++++++++++++ 5 files changed, 1288 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-isl29501 create mode 100644 Documentation/devicetree/bindings/iio/light/isl29501.txt create mode 100644 drivers/iio/light/isl29501.c
Documentation/ABI/testing/sysfs-bus-iio-isl29501 new file +49 -0
@@ -0,0 +1,49 @@ +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_agc_gain +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_agc_gain_bias +KernelVersion: 4.18 +Contact: [email protected] +Description: + This sensor has an automatic gain control (agc) loop + which sets the analog signal levels at an optimum + level by controlling programmable gain amplifiers. The + criteria for optimal gain is determined by the sensor. + + Return the actual gain value as an integer between 0 + and 65536 when read from. + + The agc gain read when measuring crosstalk shall be + written into in_proximity0_agc_gain_bias. + +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_temp_a +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_temp_b +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_amb_a +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_amb_b +KernelVersion: 4.18 +Contact: [email protected] +Description: + The sensor is able to perform correction of distance + measurements due to changing temperature and ambient + light conditions. It can be programmed to correct for + a second order error polynomial. + + Phase data has to be collected when temperature and + ambient light are modulated independently. + + Then a least squares curve fit to a second order + polynomial has to be generated from the data. The + resultant curves have the form ax^2 + bx + c. + + From those two curves, a and b coefficients shall be + stored in in_proximity0_calib_phase_temp_a and + in_proximity0_calib_phase_temp_b for temperature and + in in_proximity0_calib_phase_amb_a and + in_proximity0_calib_phase_amb_b for ambient light. + + Those values must be integer between 0 and 8355840 + inclusive. + + Finally, the c constant is set by the sensor + internally. + + Get those values from hardware and show them when read + from.
Documentation/devicetree/bindings/iio/light/isl29501.txt new file +13 -0
@@ -0,0 +1,13 @@ +* ISL29501 Time-of-flight sensor. + +Required properties: + + - compatible : should be "renesas,isl29501" + - reg : the I2C address of the sensor + +Example: + +isl29501@57 { + compatible = "renesas,isl29501"; + reg = <0x57>; +};
drivers/iio/light/Kconfig +13 -0
@@ -194,6 +194,19 @@ config ISL29125 To compile this driver as a module, choose M here: the module will be called isl29125. +config ISL29501 + tristate "Intersil ISL29501 Time Of Flight sensor" + depends on I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + select IIO_KFIFO_BUF + help + Say Y here if you want to build a driver for the Intersil ISL29501 + Time of Flight sensor. + + To compile this driver as a module, choose M here: the module will be + called isl29501. + config HID_SENSOR_ALS depends on HID_SENSOR_HUB select IIO_BUFFER
drivers/iio/light/Makefile +1 -0
@@ -23,6 +23,7 @@ obj-$(CONFIG_HID_SENSOR_PROX) += hid-sensor-prox.o obj-$(CONFIG_SENSORS_ISL29018) += isl29018.o obj-$(CONFIG_SENSORS_ISL29028) += isl29028.o obj-$(CONFIG_ISL29125) += isl29125.o +obj-$(CONFIG_ISL29501) += isl29501.o obj-$(CONFIG_JSA1212) += jsa1212.o obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o obj-$(CONFIG_LTR501) += ltr501.o
drivers/iio/light/isl29501.c new file +1212 -0
1,212 lines not on this page · raw message
Signature · 1 line
2.7.4
Quoted 8 lines · On Tue, 19 Jun 2018 12:16:48 +0200 Mathieu Othacehe <[email protected]>…
Add new channel type support for phase.
This channel may be used by Time-of-flight sensors to express the phase difference between emitted and received signals. Those sensor will then use the phase shift of return signals to approximate the distance to objects.
Signed-off-by Mathieu Othacehe <[email protected]>
Looks good. Will pick up once the user is ready.
Jonathan
Quoted 67 lines
--- Documentation/ABI/testing/sysfs-bus-iio | 7 +++++++ drivers/iio/industrialio-core.c | 1 + include/uapi/linux/iio/types.h | 1 + tools/iio/iio_event_monitor.c | 2 ++ 4 files changed, 11 insertions(+)Documentation/ABI/testing/sysfs-bus-iio +7
-0@@ -1663,3 +1663,10 @@ KernelVersion: 4.12 Contact: [email protected] Description: Raw counter device counters direction for channel Y. + +What: /sys/bus/iio/devices/iio:deviceX/in_phaseY_raw +KernelVersion: 4.18 +Contact: [email protected] +Description: + Raw (unscaled) phase difference reading from channel Y + that can be processed to radians.drivers/iio/industrialio-core.c +1
-0@@ -85,6 +85,7 @@ static const char * const iio_chan_type_name_spec[] = { [IIO_COUNT] = "count", [IIO_INDEX] = "index", [IIO_GRAVITY] = "gravity", + [IIO_PHASE] = "phase", };static const char *const iio_modifier_names[] = {include/uapi/linux/iio/types.h +1
-0@@ -44,6 +44,7 @@ enum iio_chan_type {IIO_COUNT ,IIO_INDEX ,IIO_GRAVITY , + IIO_PHASE, }; enum iio_modifier {tools/iio/iio_event_monitor.c +2
-0@@ -58,6 +58,7 @@ static const char * const iio_chan_type_name_spec[] = { [IIO_PH] = "ph", [IIO_UVINDEX] = "uvindex", [IIO_GRAVITY] = "gravity", + [IIO_PHASE] = "phase", };static const char *const iio_ev_type_text[] = {@@ -151,6 +152,7 @@ static bool event_is_known(struct iio_event_data *event)case IIO_PH :case IIO_UVINDEX :case IIO_GRAVITY : +case IIO_PHASE :break ;default :return false;
Re: [PATCH 2/2] iio: light: isl29501: Add support for the ISL29501 ToF sensor.
In reply to Mathieu OthaceheQuoted 3 lines · On Tue, 19 Jun 2018 12:16:49 +0200 Mathieu Othacehe <[email protected]>…
This patch adds support for the ISL29501 Time of Flight sensor.
Signed-off-by Mathieu Othacehe <[email protected]>
Nothing major beyond the whole figuring out the floating point question.
Even though they are very device specific I'm not that happy with the few bits of additional ABI. Don't have a better suggestion now, but will have a think about it whilst we wait for others to give comments on the floating point options.
Thanks,
Jonathan
Quoted 57 lines
--- Documentation/ABI/testing/sysfs-bus-iio-isl29501 | 49 + .../devicetree/bindings/iio/light/isl29501.txt | 13 + drivers/iio/light/Kconfig | 13 + drivers/iio/light/Makefile | 1 + drivers/iio/light/isl29501.c | 1212 ++++++++++++++++++++ 5 files changed, 1288 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-isl29501 create mode 100644 Documentation/devicetree/bindings/iio/light/isl29501.txt create mode 100644 drivers/iio/light/isl29501.cDocumentation/ABI/testing/sysfs-bus-iio-isl29501 new file +40
-0@@ -0,0 +1,49 @@ +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_agc_gain +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_agc_gain_bias +KernelVersion: 4.18 +Contact: [email protected] +Description: + This sensor has an automatic gain control (agc) loop + which sets the analog signal levels at an optimum + level by controlling programmable gain amplifiers. The + criteria for optimal gain is determined by the sensor. + + Return the actual gain value as an integer between 0 + and 65536 when read from. + + The agc gain read when measuring crosstalk shall be + written into in_proximity0_agc_gain_bias. + +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_temp_a +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_temp_b +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_amb_a +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_amb_b +KernelVersion: 4.18 +Contact: [email protected] +Description: + The sensor is able to perform correction of distance + measurements due to changing temperature and ambient + light conditions. It can be programmed to correct for + a second order error polynomial. + + Phase data has to be collected when temperature and + ambient light are modulated independently. + + Then a least squares curve fit to a second order + polynomial has to be generated from the data. The + resultant curves have the form ax^2 + bx + c. + + From those two curves, a and b coefficients shall be + stored in in_proximity0_calib_phase_temp_a and + in_proximity0_calib_phase_temp_b for temperature and + in in_proximity0_calib_phase_amb_a and + in_proximity0_calib_phase_amb_b for ambient light.
Documentation/ABI/testing/sysfs-bus-iio-isl29501 line 49
Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-iio-isl29501 new file +9
-0+ + Those values must be integer between 0 and 8355840 + inclusive. + + Finally, the c constant is set by the sensor + internally. + + Get those values from hardware and show them when read + from.
Nice to fix this.. Not important really but still nice ;)
Quoted 1,172 lines
Documentation/devicetree/bindings/iio/light/isl29501.txt new file +13
-0@@ -0,0 +1,13 @@ +* ISL29501 Time-of-flight sensor. + +Required properties: + + - compatible : should be "renesas,isl29501" + - reg : the I2C address of the sensor + +Example: + +isl29501@57 { + compatible = "renesas,isl29501"; + reg = <0x57>; +};drivers/iio/light/Kconfig +13
-0@@ -194,6 +194,19 @@ config ISL29125 To compile this driver as a module, choose M here: the module will be called isl29125. +config ISL29501 + tristate "Intersil ISL29501 Time Of Flight sensor" + depends on I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + select IIO_KFIFO_BUF + help + Say Y here if you want to build a driver for the Intersil ISL29501 + Time of Flight sensor. + + To compile this driver as a module, choose M here: the module will be + called isl29501. + config HID_SENSOR_ALS depends on HID_SENSOR_HUB select IIO_BUFFERdrivers/iio/light/Makefile +1
-0@@ -23,6 +23,7 @@ obj-$(CONFIG_HID_SENSOR_PROX) += hid-sensor-prox.o obj-$(CONFIG_SENSORS_ISL29018) += isl29018.o obj-$(CONFIG_SENSORS_ISL29028) += isl29028.o obj-$(CONFIG_ISL29125) += isl29125.o +obj-$(CONFIG_ISL29501) += isl29501.o obj-$(CONFIG_JSA1212) += jsa1212.o obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o obj-$(CONFIG_LTR501) += ltr501.odrivers/iio/light/isl29501.c new file +1111
-0@@ -0,0 +1,1212 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* isl29501.c: ISL29501 Time of Flight sensor driver. +* +* Copyright (C) 2018 +* Author: Mathieu Othacehe <[email protected]> +* +* 7-bit I2C slave address: 0x57 +*/ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/i2c.h> +#include <linux/err.h> +#include <linux/of_device.h> +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> + +#include <linux/iio/trigger_consumer.h> +#include <linux/iio/buffer.h> +#include <linux/iio/triggered_buffer.h> + +/* Control, setting and status registers */ +#define ISL29501_DEVICE_ID 0x00 +#define ISL29501_ID 0x0A + +/* Sampling control registers */ +#define ISL29501_INTEGRATION_PERIOD 0x10 +#define ISL29501_SAMPLE_PERIOD 0x11 + +/* Closed loop calibration registers */ +#define ISL29501_CROSSTALK_I_MSB 0x24 +#define ISL29501_CROSSTALK_I_LSB 0x25 +#define ISL29501_CROSSTALK_I_EXPONENT 0x26 +#define ISL29501_CROSSTALK_Q_MSB 0x27 +#define ISL29501_CROSSTALK_Q_LSB 0x28 +#define ISL29501_CROSSTALK_Q_EXPONENT 0x29 +#define ISL29501_CROSSTALK_GAIN_MSB 0x2A +#define ISL29501_CROSSTALK_GAIN_LSB 0x2B +#define ISL29501_MAGNITUDE_REF_EXP 0x2C +#define ISL29501_MAGNITUDE_REF_MSB 0x2D +#define ISL29501_MAGNITUDE_REF_LSB 0x2E +#define ISL29501_PHASE_OFFSET_MSB 0x2F +#define ISL29501_PHASE_OFFSET_LSB 0x30 + +/* Analog control registers */ +#define ISL29501_DRIVER_RANGE 0x90 +#define ISL29501_EMITTER_DAC 0x91 + +#define ISL29501_COMMAND_REGISTER 0xB0 + +/* Commands */ +#define ISL29501_EMUL_SAMPLE_START_PIN 0x49 +#define ISL29501_RESET_ALL_REGISTERS 0xD7 +#define ISL29501_RESET_INT_SM 0xD1 + +/* Ambiant light and temperature corrections */ +#define ISL29501_TEMP_REFERENCE 0x31 +#define ISL29501_PHASE_EXPONENT 0x33 +#define ISL29501_TEMP_COEFF_A 0x34 +#define ISL29501_TEMP_COEFF_B 0x39 +#define ISL29501_AMBIANT_COEFF_A 0x36 +#define ISL29501_AMBIANT_COEFF_B 0x3B + +/* Data output registers */ +#define ISL29501_DISTANCE_MSB_DATA 0xD1 +#define ISL29501_DISTANCE_LSB_DATA 0xD2 +#define ISL29501_PRECISION_MSB 0xD3 +#define ISL29501_PRECISION_LSB 0xD4 +#define ISL29501_MAGNITUDE_EXPONENT 0xD5 +#define ISL29501_MAGNITUDE_MSB 0xD6 +#define ISL29501_MAGNITUDE_LSB 0xD7 +#define ISL29501_PHASE_MSB 0xD8 +#define ISL29501_PHASE_LSB 0xD9 +#define ISL29501_I_RAW_EXPONENT 0xDA +#define ISL29501_I_RAW_MSB 0xDB +#define ISL29501_I_RAW_LSB 0xDC +#define ISL29501_Q_RAW_EXPONENT 0xDD +#define ISL29501_Q_RAW_MSB 0xDE +#define ISL29501_Q_RAW_LSB 0xDF +#define ISL29501_DIE_TEMPERATURE 0xE2 +#define ISL29501_AMBIENT_LIGHT 0xE3 +#define ISL29501_GAIN_MSB 0xE6 +#define ISL29501_GAIN_LSB 0xE7 + +#define ISL29501_MAX_EXP_VAL 15 + +#define ISL29501_INT_TIME_AVAILABLE \ + "0.00007 0.00014 0.00028 0.00057 0.00114 " \ + "0.00228 0.00455 0.00910 0.01820 0.03640 " \ + "0.07281 0.14561" + +#define ISL29501_CURRENT_SCALE_AVAILABLE \ + "0.0039 0.0078 0.0118 0.0157 0.0196 " \ + "0.0235 0.0275 0.0314 0.0352 0.0392 " \ + "0.0431 0.0471 0.0510 0.0549 0.0588" + +enum correction_coeff { +COEFF_TEMP_A , +COEFF_TEMP_B , +COEFF_LIGHT_A , +COEFF_LIGHT_B , +COEFF_MAX , +}; + +struct isl29501_private { +struct i2c_client *client; +struct mutex lock; +/* Exact representation of correction coefficients. */ +unsigned int shadow_coeffs[COEFF_MAX ]; +}; + +enum register_name { +REG_DISTANCE , +REG_AMPLITUDE , +REG_PHASE , +REG_I_RAW , +REG_Q_RAW , +REG_TEMPERATURE , +REG_AMBIENT_LIGHT , +REG_GAIN , +REG_I_BIAS , +REG_Q_BIAS , +REG_GAIN_BIAS , +REG_AMPL_BIAS , +REG_PHASE_EXP , +REG_CALIB_PHASE_TEMP_A , +REG_CALIB_PHASE_TEMP_B , +REG_CALIB_PHASE_AMB_A , +REG_CALIB_PHASE_AMB_B , +REG_DISTANCE_BIAS , +REG_TEMPERATURE_BIAS , +REG_INT_TIME , +REG_SAMPLE_TIME , +REG_DRIVER_RANGE , +REG_EMITTER_DAC , +}; + +struct isl29501_register_desc { +u8 msb; +u8 lsb; +u8 exp; +}; + +static const struct isl29501_register_desc isl29501_registers[] = { + [REG_DISTANCE ] = { + .msb =ISL29501_DISTANCE_MSB_DATA , + .lsb =ISL29501_DISTANCE_LSB_DATA , + + }, + [REG_AMPLITUDE ] = { + .msb =ISL29501_MAGNITUDE_MSB , + .lsb =ISL29501_MAGNITUDE_LSB , + .exp =ISL29501_MAGNITUDE_EXPONENT , + }, + [REG_PHASE ] = { + .msb =ISL29501_PHASE_MSB , + .lsb =ISL29501_PHASE_LSB , + }, + [REG_I_RAW ] = { + .msb =ISL29501_I_RAW_MSB , + .lsb =ISL29501_I_RAW_LSB , + .exp =ISL29501_I_RAW_EXPONENT , + }, + [REG_Q_RAW ] = { + .msb =ISL29501_Q_RAW_MSB , + .lsb =ISL29501_Q_RAW_LSB , + .exp =ISL29501_Q_RAW_EXPONENT , + }, + [REG_TEMPERATURE ] = { + .lsb =ISL29501_DIE_TEMPERATURE , + }, + [REG_AMBIENT_LIGHT ] = { + .lsb =ISL29501_AMBIENT_LIGHT , + }, + [REG_GAIN ] = { + .msb =ISL29501_GAIN_MSB , + .lsb =ISL29501_GAIN_LSB , + }, + [REG_I_BIAS ] = { + .msb =ISL29501_CROSSTALK_I_MSB , + .lsb =ISL29501_CROSSTALK_I_LSB , + .exp =ISL29501_CROSSTALK_I_EXPONENT , + }, + [REG_Q_BIAS ] = { + .msb =ISL29501_CROSSTALK_Q_MSB , + .lsb =ISL29501_CROSSTALK_Q_LSB , + .exp =ISL29501_CROSSTALK_Q_EXPONENT , + }, + [REG_GAIN_BIAS ] = { + .msb =ISL29501_CROSSTALK_GAIN_MSB , + .lsb =ISL29501_CROSSTALK_GAIN_LSB , + }, + [REG_AMPL_BIAS ] = { + .msb =ISL29501_MAGNITUDE_REF_MSB , + .lsb =ISL29501_MAGNITUDE_REF_LSB , + .exp =ISL29501_MAGNITUDE_REF_EXP , + }, + [REG_PHASE_EXP ] = { + .lsb =ISL29501_PHASE_EXPONENT , + }, + [REG_CALIB_PHASE_TEMP_A ] = { + .lsb =ISL29501_TEMP_COEFF_A , + }, + [REG_CALIB_PHASE_TEMP_B ] = { + .lsb =ISL29501_TEMP_COEFF_B , + }, + [REG_CALIB_PHASE_AMB_A ] = { + .lsb =ISL29501_AMBIANT_COEFF_A , + }, + [REG_CALIB_PHASE_AMB_B ] = { + .lsb =ISL29501_AMBIANT_COEFF_B , + }, + [REG_DISTANCE_BIAS ] = { + .msb =ISL29501_PHASE_OFFSET_MSB , + .lsb =ISL29501_PHASE_OFFSET_LSB , + }, + [REG_TEMPERATURE_BIAS ] = { + .lsb =ISL29501_TEMP_REFERENCE , + }, + [REG_INT_TIME ] = { + .lsb =ISL29501_INTEGRATION_PERIOD , + }, + [REG_SAMPLE_TIME ] = { + .lsb =ISL29501_SAMPLE_PERIOD , + }, + [REG_DRIVER_RANGE ] = { + .lsb =ISL29501_DRIVER_RANGE , + }, + [REG_EMITTER_DAC ] = { + .lsb =ISL29501_EMITTER_DAC , + }, + +}; + +static int isl29501_register_read (struct isl29501_private *isl29501, +enum register_name name, +u32 *val) +{ +const struct isl29501_register_desc *reg = &isl29501_registers[name]; +u8 msb =0 , lsb =0 , exp =0 ; +s32 ret; + +if (reg->msb) { + ret =i2c_smbus_read_byte_data (isl29501->client, reg->msb); +if (ret <0 ) +return ret; + msb = ret; + } + +if (reg->lsb) { + ret =i2c_smbus_read_byte_data (isl29501->client, reg->lsb); +if (ret <0 ) +return ret; + lsb = ret; + } + +if (reg->exp) { + ret =i2c_smbus_read_byte_data (isl29501->client, reg->exp); +if (ret <0 ) +return ret; + exp = ret; + } + + *val = ((msb <<8 ) + lsb) << exp; + +return 0 ; +} + +static u32 isl29501_register_write (struct isl29501_private *isl29501, +enum register_name name, +u32 value) +{ +const struct isl29501_register_desc *reg = &isl29501_registers[name]; +u8 msb, lsb; +int ret; + +if (!reg->msb && value >U8_MAX ) +return -ERANGE ; + +if (!reg->exp && value >U16_MAX ) +return -ERANGE ; + +if (reg->exp) { +/* TODO: Implement float write */ +return -EINVAL ; + } + +if (!reg->msb) { + msb =0 ; + lsb = value &0xFF ; + }else { + msb = (value >>8 ) &0xFF ; + lsb = value &0xFF ; + } + +if (reg->msb) { + ret =i2c_smbus_write_byte_data (isl29501->client, + reg->msb, msb); +if (ret <0 ) +return ret; + } + + ret =i2c_smbus_write_byte_data (isl29501->client, reg->lsb, lsb); +if (ret <0 ) +return ret; + +return 0 ; +} + +static ssize_t isl29501_read_ext (struct iio_dev *indio_dev, +uintptr_t private, +const struct iio_chan_spec *chan, +char *buf) +{ +struct isl29501_private *isl29501 =iio_priv (indio_dev); +enum register_name reg = private; +int ret; +u32 value, gain, coeff, exp; + +switch (reg) { +case REG_GAIN : +case REG_GAIN_BIAS : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501, reg, &gain); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + + value = gain; +break ; +case REG_CALIB_PHASE_TEMP_A : +case REG_CALIB_PHASE_TEMP_B : +case REG_CALIB_PHASE_AMB_A : +case REG_CALIB_PHASE_AMB_B : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_PHASE_EXP , &exp); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501, reg, &coeff); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + + value = coeff << exp; +break ; +default : +return -EINVAL ; + } + +return sprintf (buf,"%u\n" , value); +} + +static int isl29501_set_shadow_coeff (struct isl29501_private *isl29501, +enum register_name reg, +unsigned int val) +{ +enum correction_coeff coeff; + +switch (reg) { +case REG_CALIB_PHASE_TEMP_A : + coeff =COEFF_TEMP_A ; +break ; +case REG_CALIB_PHASE_TEMP_B : + coeff =COEFF_TEMP_B ; +break ; +case REG_CALIB_PHASE_AMB_A : + coeff =COEFF_LIGHT_A ; +break ; +case REG_CALIB_PHASE_AMB_B : + coeff =COEFF_LIGHT_B ; +break ; +default : +return -EINVAL ; + } + isl29501->shadow_coeffs[coeff] = val; + +return 0 ; +} + +static int isl29501_write_coeff (struct isl29501_private *isl29501, +enum correction_coeff coeff, +int val) +{ +enum register_name reg; +int ret; + +switch (coeff) { +case COEFF_TEMP_A : + reg =REG_CALIB_PHASE_TEMP_A ; +break ; +case COEFF_TEMP_B : + reg =REG_CALIB_PHASE_TEMP_B ; +break ; +case COEFF_LIGHT_A : + reg =REG_CALIB_PHASE_AMB_A ; +break ; +case COEFF_LIGHT_B : + reg =REG_CALIB_PHASE_AMB_B ; +break ; +default : +return -EINVAL ; + } + +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501, reg, val); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +return 0 ; +} + +static unsigned int isl29501_find_corr_exp (unsigned int val, +unsigned int max_exp, +unsigned int max_mantissa) +{ +unsigned int exp =1 ; + +/* +* Correction coefficients are represented under +* mantissa * 2^exponent form, where mantissa and exponent +* are stored in two separate registers of the sensor. +* +* Compute and return the lowest exponent such as: +* mantissa = value / 2^exponent +* +* where mantissa < max_mantissa. +*/ +if (val <= max_mantissa) +return 0 ; + +while ((val >> exp) > max_mantissa) { + exp++; + +if (exp > max_exp) +return max_exp; + } + +return exp; +} + +static ssize_t isl29501_write_ext (struct iio_dev *indio_dev, +uintptr_t private, +const struct iio_chan_spec *chan, +const char *buf,size_t len) +{ +struct isl29501_private *isl29501 =iio_priv (indio_dev); +enum register_name reg = private; +unsigned int val; +int max_exp =0 ; +int ret; +int i; + + ret =kstrtouint (buf,10 , &val); +if (ret) +return ret; + +switch (reg) { +case REG_GAIN_BIAS : +if (val >U16_MAX ) +return -ERANGE ; + +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501, reg, val); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +break ; +case REG_CALIB_PHASE_TEMP_A : +case REG_CALIB_PHASE_TEMP_B : +case REG_CALIB_PHASE_AMB_A : +case REG_CALIB_PHASE_AMB_B : + +if (val > (U8_MAX <<ISL29501_MAX_EXP_VAL )) +return -ERANGE ; + +/* Store the correction coefficient under its exact form. */ + ret =isl29501_set_shadow_coeff (isl29501, reg, val); +if (ret <0 ) +return ret; + +/* +* Find the highest exponent needed to represent +* correction coefficients. +*/ +for (i =0 ; i <COEFF_MAX ; i++) { +int corr; +int corr_exp; + + corr = isl29501->shadow_coeffs[i]; + corr_exp =isl29501_find_corr_exp (corr, +ISL29501_MAX_EXP_VAL , +U8_MAX /2 ); +dev_dbg (&isl29501->client->dev, +"found exp of corr(%d) = %d\n" , corr, corr_exp); + + max_exp =max (max_exp, corr_exp); + } + +/* +* Represent every correction coefficient under +* mantissa * 2^max_exponent form and force the +* writing of those coefficients on the sensor. +*/ +for (i =0 ; i <COEFF_MAX ; i++) { +int corr; +int mantissa; + + corr = isl29501->shadow_coeffs[i]; +if (!corr) +continue ; + + mantissa = corr >> max_exp; + + ret =isl29501_write_coeff (isl29501, i, mantissa); +if (ret <0 ) +return ret; + } + +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501,REG_PHASE_EXP , max_exp); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +break ; +default : +return -EINVAL ; + } + +return len; +} + +#define _ISL29501_EXT_INFO (_name, _ident) { \ + .name = _name, \ + .read = isl29501_read_ext, \ + .write = isl29501_write_ext, \ + .private = _ident, \ + .shared = IIO_SEPARATE, \ +} + +static const struct iio_chan_spec_ext_info isl29501_ext_info[] = { +_ISL29501_EXT_INFO ("agc_gain" ,REG_GAIN ), +_ISL29501_EXT_INFO ("agc_gain_bias" ,REG_GAIN_BIAS ), +_ISL29501_EXT_INFO ("calib_phase_temp_a" ,REG_CALIB_PHASE_TEMP_A ), +_ISL29501_EXT_INFO ("calib_phase_temp_b" ,REG_CALIB_PHASE_TEMP_B ), +_ISL29501_EXT_INFO ("calib_phase_amb_a" ,REG_CALIB_PHASE_AMB_A ), +_ISL29501_EXT_INFO ("calib_phase_amb_b" ,REG_CALIB_PHASE_AMB_B ), + { }, +}; + +#define ISL29501_DISTANCE_SCAN_INDEX 0 +#define ISL29501_TIMESTAMP_SCAN_INDEX 1 + +#define ISL29501_MAGNITUDE_CHANNEL 0 +#define ISL29501_AMBIENT_LIGHT_CHANNEL 1 + +static const struct iio_chan_spec isl29501_channels[] = { + { + .type =IIO_PROXIMITY , + .channel =0 , + .indexed = true, + .scan_index =ISL29501_DISTANCE_SCAN_INDEX , + .info_mask_separate = +BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_SCALE ) | +BIT (IIO_CHAN_INFO_CALIBBIAS ), + .scan_type = { + .sign ='u' , + .realbits =16 , + .storagebits =16 , + .endianness =IIO_CPU , + }, + .info_mask_shared_by_all =BIT (IIO_CHAN_INFO_INT_TIME ) | +BIT (IIO_CHAN_INFO_SAMP_FREQ ), + .ext_info = isl29501_ext_info, + }, + { + .type =IIO_PHASE , + .channel =0 , + .indexed = true, + .scan_index =-1 , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_SCALE ), + }, + { + .type =IIO_INTENSITY , + .channel =ISL29501_MAGNITUDE_CHANNEL , + .indexed = true, + .scan_index =-1 , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_CALIBBIAS ), + }, + { + .type =IIO_INTENSITY , + .channel =ISL29501_MAGNITUDE_CHANNEL , + .indexed = true, + .scan_index =-1 , + .modified =1 , + .channel2 =IIO_MOD_I , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_CALIBBIAS ), + }, + { + .type =IIO_INTENSITY , + .channel =ISL29501_MAGNITUDE_CHANNEL , + .indexed = true, + .scan_index =-1 , + .modified =1 , + .channel2 =IIO_MOD_Q , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_CALIBBIAS ), + }, + { + .type =IIO_CURRENT , + .channel =0 , + .indexed = true, + .scan_index =-1 , + .output =1 , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_SCALE ), + }, + { + .type =IIO_TEMP , + .channel =0 , + .indexed = true, + .scan_index =-1 , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_SCALE ) | +BIT (IIO_CHAN_INFO_CALIBBIAS ), + }, + { + .type =IIO_INTENSITY , + .channel =ISL29501_AMBIENT_LIGHT_CHANNEL , + .indexed = true, + .scan_index =-1 , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_SCALE ), + }, +IIO_CHAN_SOFT_TIMESTAMP (ISL29501_TIMESTAMP_SCAN_INDEX ), +}; + +static int isl29501_reset_registers (struct isl29501_private *isl29501) +{ +int ret; + + ret =i2c_smbus_write_byte_data (isl29501->client, +ISL29501_COMMAND_REGISTER , +ISL29501_RESET_ALL_REGISTERS ); + ret =i2c_smbus_write_byte_data (isl29501->client, +ISL29501_COMMAND_REGISTER , +ISL29501_RESET_INT_SM ); +if (ret <0 ) { +dev_err (&isl29501->client->dev, +"cannot reset registers %d\n" , ret); +return ret; + } + +return 0 ; +} + +static int isl29501_begin_acquisition (struct isl29501_private *isl29501) +{ +int ret; + + ret =i2c_smbus_write_byte_data (isl29501->client, +ISL29501_COMMAND_REGISTER , +ISL29501_EMUL_SAMPLE_START_PIN ); +if (ret <0 ) { +dev_err (&isl29501->client->dev, +"cannot begin acquisition %d\n" , ret); +return ret; + } + +return 0 ; +} + +static IIO_CONST_ATTR_INT_TIME_AVAIL (ISL29501_INT_TIME_AVAILABLE ); +static IIO_CONST_ATTR(out_current0_scale_available, + ISL29501_CURRENT_SCALE_AVAILABLE); + +static struct attribute *isl29501_attributes[] = { + &iio_const_attr_integration_time_available.dev_attr.attr, + &iio_const_attr_out_current0_scale_available.dev_attr.attr, +NULL +}; + +static const struct attribute_group isl29501_attribute_group = { + .attrs = isl29501_attributes, +}; + +static const int isl29501_current_st[][2 ] = { + {0 ,3900 }, {0 ,7800 }, {0 ,11800 }, {0 ,15700 }, + {0 ,19600 }, {0 ,23500 }, {0 ,27500 }, {0 ,31400 }, + {0 ,35200 }, {0 ,39200 }, {0 ,43100 }, {0 ,47100 }, + {0 ,51000 }, {0 ,54900 }, {0 ,58800 }, +}; + +static const int isl29501_int_time[][2 ] = { + {0 ,70 }, {0 ,140 }, {0 ,280 }, {0 ,570 }, + {0 ,1140 }, {0 ,2280 }, {0 ,4550 }, {0 ,9100 }, + {0 ,18200 }, {0 ,36400 }, {0 ,72810 }, {0 ,145610 } +}; + +static int isl29501_get_raw (struct isl29501_private *isl29501, +const struct iio_chan_spec *chan, +int *raw) +{ +int ret; + +switch (chan->type) { +case IIO_PROXIMITY : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_DISTANCE , raw); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +return IIO_VAL_INT ; +case IIO_INTENSITY : +if (chan->channel ==ISL29501_MAGNITUDE_CHANNEL ) { +switch (chan->channel2) { +case IIO_NO_MOD : +case IIO_MOD_I : +case IIO_MOD_Q : +/* TODO: implement float read */ +return -EINVAL ; +default : +return -EINVAL ; + } + }else if (chan->channel ==ISL29501_AMBIENT_LIGHT_CHANNEL ) { +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501, +REG_AMBIENT_LIGHT , + raw); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +return IIO_VAL_INT ; + }else { +return IIO_VAL_INT ; + } +case IIO_PHASE : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_PHASE , raw); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +return IIO_VAL_INT ; +case IIO_CURRENT : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_EMITTER_DAC , raw); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return -EINVAL ; + +return IIO_VAL_INT ; +case IIO_TEMP : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_TEMPERATURE , raw); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +return IIO_VAL_INT ; +default : +return -EINVAL ; + } +} + +static int isl29501_get_scale (struct isl29501_private *isl29501, +const struct iio_chan_spec *chan, +int *val,int *val2) +{ +int ret; +u32 current_scale; + +switch (chan->type) { +case IIO_PROXIMITY : +/* distance = raw_distance * 33.31 / 65536 (m) */ + *val =3331 ; + *val2 =6553600 ; + +return IIO_VAL_FRACTIONAL ; +case IIO_PHASE : +/* phase = raw_phase * 2pi / 65536 (rad) */ + *val =2 *314 ; + *val2 =6553600 ; + +return IIO_VAL_FRACTIONAL ; +case IIO_INTENSITY : +if (chan->channel !=ISL29501_AMBIENT_LIGHT_CHANNEL ) +return -EINVAL ; +/* light = raw_light * 35 / 10000 (mA) */ + *val =35 ; + *val2 =10000 ; + +return IIO_VAL_FRACTIONAL ; +case IIO_CURRENT : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501, +REG_DRIVER_RANGE , + ¤t_scale); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +if (current_scale >ARRAY_SIZE (isl29501_current_st)) +return -EINVAL ; + +if (!current_scale) { + *val =0 ; + *val2 =0 ; +return IIO_VAL_INT ; + } + + *val = isl29501_current_st[current_scale -1 ][0 ]; + *val2 = isl29501_current_st[current_scale -1 ][1 ]; + +return IIO_VAL_INT_PLUS_MICRO ; +case IIO_TEMP : +/* temperature = raw_temperature * 125 / 100000 (milli °C) */ + *val =125 ; + *val2 =100000 ; + +return IIO_VAL_FRACTIONAL ; +default : +return -EINVAL ; + } +} + +static int isl29501_get_calibbias (struct isl29501_private *isl29501, +const struct iio_chan_spec *chan, +int *bias) +{ +int ret; + +switch (chan->type) { +case IIO_PROXIMITY : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_DISTANCE_BIAS , bias); +mutex_unlock (&isl29501->lock); + +return ret; +case IIO_INTENSITY : +if (chan->channel !=ISL29501_MAGNITUDE_CHANNEL ) +return -EINVAL ; + +switch (chan->channel2) { +case IIO_NO_MOD : +case IIO_MOD_I : +case IIO_MOD_Q : +/* TODO: Implement float read */ +return -EINVAL ; +default : +return -EINVAL ; + } +case IIO_TEMP : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501, +REG_TEMPERATURE_BIAS , + bias); +mutex_unlock (&isl29501->lock); + +return ret; +default : +return -EINVAL ; + } +} + +static int isl29501_get_inttime (struct isl29501_private *isl29501, +int *val,int *val2) +{ +int ret; +u32 inttime; + +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_INT_TIME , &inttime); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +if (inttime >=ARRAY_SIZE (isl29501_int_time)) +return -EINVAL ; + + *val = isl29501_int_time[inttime][0 ]; + *val2 = isl29501_int_time[inttime][1 ]; + +return IIO_VAL_INT_PLUS_MICRO ; +} + +static int isl29501_get_freq (struct isl29501_private *isl29501, +int *val,int *val2) +{ +int ret; +int sample_time; +unsigned long long freq; + +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_SAMPLE_TIME , &sample_time); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +/* freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */ + freq =1000000ULL *1000000ULL / (450ULL * (sample_time +1ULL )); + + *val = freq /1000000 ; + *val2 = freq %1000000 ; + +return IIO_VAL_INT_PLUS_MICRO ; +} + +static int isl29501_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int *val, +int *val2,long mask) +{ +struct isl29501_private *isl29501 =iio_priv (indio_dev); + +switch (mask) { +case IIO_CHAN_INFO_RAW : +return isl29501_get_raw (isl29501, chan, val); +case IIO_CHAN_INFO_SCALE : +return isl29501_get_scale (isl29501, chan, val, val2); +case IIO_CHAN_INFO_INT_TIME : +return isl29501_get_inttime (isl29501, val, val2); +case IIO_CHAN_INFO_SAMP_FREQ : +return isl29501_get_freq (isl29501, val, val2); +case IIO_CHAN_INFO_CALIBBIAS : +return isl29501_get_calibbias (isl29501, chan, val); +default : +return -EINVAL ; + } + +return -EINVAL ; +} + +static int isl29501_set_raw (struct isl29501_private *isl29501, +const struct iio_chan_spec *chan, +int raw) +{ +int ret; + +switch (chan->type) { +case IIO_CURRENT : +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501,REG_EMITTER_DAC , raw); +mutex_unlock (&isl29501->lock); + +return ret; +default : +return -EINVAL ; + } +} + +static int isl29501_set_inttime (struct isl29501_private *isl29501, +int val,int val2) +{ +int ret; +int i; + +for (i =0 ; i <ARRAY_SIZE (isl29501_int_time); i++) { +if (isl29501_int_time[i][0 ] == val && + isl29501_int_time[i][1 ] == val2) { +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501, +REG_INT_TIME , + i); +mutex_unlock (&isl29501->lock); +return ret; + } + } + +return -EINVAL ; +} + +static int isl29501_set_scale (struct isl29501_private *isl29501, +const struct iio_chan_spec *chan, +int val,int val2) +{ +int ret; +int i; + +if (chan->type !=IIO_CURRENT ) +return -EINVAL ; + +for (i =0 ; i <ARRAY_SIZE (isl29501_current_st); i++) { +if (isl29501_current_st[i][0 ] == val && + isl29501_current_st[i][1 ] == val2) { +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501, +REG_DRIVER_RANGE , + i +1 ); +mutex_unlock (&isl29501->lock); +return ret; + } + } + +return -EINVAL ; +} + +static int isl29501_set_calibbias (struct isl29501_private *isl29501, +const struct iio_chan_spec *chan, +int bias) +{ +int ret; + +switch (chan->type) { +case IIO_PROXIMITY : +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501, +REG_DISTANCE_BIAS , + bias); +mutex_unlock (&isl29501->lock); + +return ret; +case IIO_INTENSITY : +if (chan->channel !=ISL29501_MAGNITUDE_CHANNEL ) +return -EINVAL ; + +switch (chan->channel2) { +case IIO_NO_MOD : +case IIO_MOD_I : +case IIO_MOD_Q : +/* TODO: Implement float read */ +return -EINVAL ; +default : +return -EINVAL ; + } +case IIO_TEMP : +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501, +REG_TEMPERATURE_BIAS , + bias); +mutex_unlock (&isl29501->lock); +return ret; +default : +return -EINVAL ; + } +} + +static int isl29501_set_freq (struct isl29501_private *isl29501, +int val,int val2) +{ +int freq; +unsigned long long sample_time; +int ret; + +/* sample_freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */ + freq = val *1000000 + val2 %1000000 ; + sample_time = (2222ULL *1000000ULL / freq) -1ULL ; + +if (sample_time >255 ) +return -ERANGE ; + +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501,REG_SAMPLE_TIME , sample_time); +mutex_unlock (&isl29501->lock); + +return ret; +} + +static int isl29501_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int val,int val2,long mask) +{ +struct isl29501_private *isl29501 =iio_priv (indio_dev); + +switch (mask) { +case IIO_CHAN_INFO_RAW : +return isl29501_set_raw (isl29501, chan, val); +case IIO_CHAN_INFO_INT_TIME : +return isl29501_set_inttime (isl29501, val, val2); +case IIO_CHAN_INFO_SAMP_FREQ : +return isl29501_set_freq (isl29501, val, val2); +case IIO_CHAN_INFO_SCALE : +return isl29501_set_scale (isl29501, chan, val, val2); +case IIO_CHAN_INFO_CALIBBIAS : +return isl29501_set_calibbias (isl29501, chan, val); + } + +return -EINVAL ; +} + +static const struct iio_info isl29501_info = { + .read_raw = &isl29501_read_raw, + .write_raw = &isl29501_write_raw, + .attrs = &isl29501_attribute_group, +}; + +static int isl29501_init_chip (struct isl29501_private *isl29501) +{ +int ret; + + ret =i2c_smbus_read_byte_data (isl29501->client,ISL29501_DEVICE_ID ); +if (ret <0 ) { +dev_err (&isl29501->client->dev,"Error reading device id\n" ); +return ret; + } + +if (ret !=ISL29501_ID ) { +dev_err (&isl29501->client->dev, +"Wrong chip id, got %x expected %x\n" , + ret,ISL29501_DEVICE_ID );
drivers/iio/light/isl29501.c line 1121
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+return -EINVAL ; + } + + ret =isl29501_reset_registers (isl29501); +if (ret <0 ) +return ret; + + ret =isl29501_begin_acquisition (isl29501); +if (ret <0 ) +return ret;
It's trivial but just
return isl29501_begin_acquisition given it's either less than 0 or 0.
One of the static checkers moans about this so we'll only end up fixing it later otherwise.
Quoted 91 lines
drivers/iio/light/isl29501.c new file +91
-0+ +return 0 ; +} + +static irqreturn_t isl29501_trigger_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *indio_dev = pf->indio_dev; +struct isl29501_private *isl29501 =iio_priv (indio_dev); +const unsigned long *active_mask = indio_dev->active_scan_mask; +u32 buffer[2 ];/* 1x16-bit + ts */ + +if (test_bit (ISL29501_DISTANCE_SCAN_INDEX , active_mask)) { +mutex_lock (&isl29501->lock); +isl29501_register_read (isl29501,REG_DISTANCE , buffer); +mutex_unlock (&isl29501->lock); + } + +iio_push_to_buffers_with_timestamp (indio_dev, buffer, pf->timestamp); +iio_trigger_notify_done (indio_dev->trig); + +return IRQ_HANDLED ; +} + +static int isl29501_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +struct iio_dev *indio_dev; +struct isl29501_private *isl29501; +int ret; + + indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*isl29501)); +if (!indio_dev) +return -ENOMEM ; + + isl29501 =iio_priv (indio_dev); + +i2c_set_clientdata (client, indio_dev); + isl29501->client = client; + +mutex_init (&isl29501->lock); + + ret =isl29501_init_chip (isl29501); +if (ret <0 ) +return ret; + + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->dev.parent = &client->dev; + indio_dev->channels = isl29501_channels; + indio_dev->num_channels =ARRAY_SIZE (isl29501_channels); + indio_dev->name = client->name; + indio_dev->info = &isl29501_info; + + ret =devm_iio_triggered_buffer_setup (&client->dev, indio_dev, + iio_pollfunc_store_time, + isl29501_trigger_handler, +NULL ); +if (ret <0 ) { +dev_err (&client->dev,"unable to setup iio triggered buffer\n" ); +return ret; + } +return devm_iio_device_register (&client->dev, indio_dev); +} + +static const struct i2c_device_id isl29501_id[] = { + {"isl29501" ,0 }, + {} +}; + +MODULE_DEVICE_TABLE (i2c, isl29501_id); + +#if defined(CONFIG_OF ) +static const struct of_device_id isl29501_i2c_matches[] = { + { .compatible ="renesas,isl29501" }, + { } +}; +MODULE_DEVICE_TABLE (of, isl29501_i2c_matches); +#endif + +static struct i2c_driver isl29501_driver = { + .driver = { + .name ="isl29501" , + }, + .id_table = isl29501_id, + .probe = isl29501_probe, +}; +module_i2c_driver (isl29501_driver); + +MODULE_AUTHOR ("Mathieu Othacehe <[email protected]>" ); +MODULE_DESCRIPTION ("ISL29501 Time of Flight sensor driver" ); +MODULE_LICENSE ("GPL v2" );
PATCH v2 1/2 iio: Add channel for Phase
Add new channel type support for phase.
This channel may be used by Time-of-flight sensors to express the phase difference between emitted and received signals. Those sensor will then use the phase shift of return signals to approximate the distance to objects.
--- Documentation/ABI/testing/sysfs-bus-iio | 7 +++++++ drivers/iio/industrialio-core.c | 1 + include/uapi/linux/iio/types.h | 1 + tools/iio/iio_event_monitor.c | 2 ++ 4 files changed, 11 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +7 -0
@@ -1663,3 +1663,10 @@ KernelVersion: 4.12 Contact: [email protected] Description: Raw counter device counters direction for channel Y. + +What: /sys/bus/iio/devices/iio:deviceX/in_phaseY_raw +KernelVersion: 4.18 +Contact: [email protected] +Description: + Raw (unscaled) phase difference reading from channel Y + that can be processed to radians.
drivers/iio/industrialio-core.c +1 -0
@@ -85,6 +85,7 @@ static const char * const iio_chan_type_name_spec[] = { [IIO_COUNT] = "count", [IIO_INDEX] = "index", [IIO_GRAVITY] = "gravity", + [IIO_PHASE] = "phase", };static const char *const iio_modifier_names[] = {
include/uapi/linux/iio/types.h +1 -0
@@ -44,6 +44,7 @@ enum iio_chan_type {IIO_COUNT ,IIO_INDEX ,IIO_GRAVITY , + IIO_PHASE, }; enum iio_modifier {
tools/iio/iio_event_monitor.c +2 -0
@@ -58,6 +58,7 @@ static const char * const iio_chan_type_name_spec[] = { [IIO_PH] = "ph", [IIO_UVINDEX] = "uvindex", [IIO_GRAVITY] = "gravity", + [IIO_PHASE] = "phase", };static const char *const iio_ev_type_text[] = {
@@ -151,6 +152,7 @@ static bool event_is_known(struct iio_event_data *event)case IIO_PH :case IIO_UVINDEX :case IIO_GRAVITY : +case IIO_PHASE :break ;default :return false;
Signature · 1 line
2.7.4
PATCH v2 2/2 iio: light: isl29501: Add support for the ISL29501 ToF sensor.
This patch adds support for the ISL29501 Time of Flight sensor.
---
Hi Jonathan,
Thanks for the review, here is a v2 with the following changes:
v2:
* Add missing eof new line
* Do not set .indexed to true for all channels
* Add a .extended to differenciate "magnitude" and "ambient_light" intensity channels
* Return isl29501_begin_acquisition result in isl29501_init_chip
* Fix buffer size in isl29501_trigger_handler
I agree with you about extended fields, I struggled to handle the shared exponent and all the coefficient related code is quite annoying :(
Thanks,
Mathieu
Documentation/ABI/testing/sysfs-bus-iio-isl29501 | 49 + .../devicetree/bindings/iio/light/isl29501.txt | 13 + drivers/iio/light/Kconfig | 13 + drivers/iio/light/Makefile | 1 + drivers/iio/light/isl29501.c | 1202 ++++++++++++++++++++ 5 files changed, 1278 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-isl29501 create mode 100644 Documentation/devicetree/bindings/iio/light/isl29501.txt create mode 100644 drivers/iio/light/isl29501.c
Documentation/ABI/testing/sysfs-bus-iio-isl29501 new file +49 -0
@@ -0,0 +1,49 @@ +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_agc_gain +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_agc_gain_bias +KernelVersion: 4.18 +Contact: [email protected] +Description: + This sensor has an automatic gain control (agc) loop + which sets the analog signal levels at an optimum + level by controlling programmable gain amplifiers. The + criteria for optimal gain is determined by the sensor. + + Return the actual gain value as an integer between 0 + and 65536 when read from. + + The agc gain read when measuring crosstalk shall be + written into in_proximity0_agc_gain_bias. + +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_temp_a +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_temp_b +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_amb_a +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_amb_b +KernelVersion: 4.18 +Contact: [email protected] +Description: + The sensor is able to perform correction of distance + measurements due to changing temperature and ambient + light conditions. It can be programmed to correct for + a second order error polynomial. + + Phase data has to be collected when temperature and + ambient light are modulated independently. + + Then a least squares curve fit to a second order + polynomial has to be generated from the data. The + resultant curves have the form ax^2 + bx + c. + + From those two curves, a and b coefficients shall be + stored in in_proximity0_calib_phase_temp_a and + in_proximity0_calib_phase_temp_b for temperature and + in in_proximity0_calib_phase_amb_a and + in_proximity0_calib_phase_amb_b for ambient light. + + Those values must be integer between 0 and 8355840 + inclusive. + + Finally, the c constant is set by the sensor + internally. + + Get those values from hardware and show them when read + from.
Documentation/devicetree/bindings/iio/light/isl29501.txt new file +13 -0
@@ -0,0 +1,13 @@ +* ISL29501 Time-of-flight sensor. + +Required properties: + + - compatible : should be "renesas,isl29501" + - reg : the I2C address of the sensor + +Example: + +isl29501@57 { + compatible = "renesas,isl29501"; + reg = <0x57>; +};
drivers/iio/light/Kconfig +13 -0
@@ -194,6 +194,19 @@ config ISL29125 To compile this driver as a module, choose M here: the module will be called isl29125. +config ISL29501 + tristate "Intersil ISL29501 Time Of Flight sensor" + depends on I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + select IIO_KFIFO_BUF + help + Say Y here if you want to build a driver for the Intersil ISL29501 + Time of Flight sensor. + + To compile this driver as a module, choose M here: the module will be + called isl29501. + config HID_SENSOR_ALS depends on HID_SENSOR_HUB select IIO_BUFFER
drivers/iio/light/Makefile +1 -0
@@ -23,6 +23,7 @@ obj-$(CONFIG_HID_SENSOR_PROX) += hid-sensor-prox.o obj-$(CONFIG_SENSORS_ISL29018) += isl29018.o obj-$(CONFIG_SENSORS_ISL29028) += isl29028.o obj-$(CONFIG_ISL29125) += isl29125.o +obj-$(CONFIG_ISL29501) += isl29501.o obj-$(CONFIG_JSA1212) += jsa1212.o obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o obj-$(CONFIG_LTR501) += ltr501.o
drivers/iio/light/isl29501.c new file +1202 -0
1,202 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH v2 2/2] iio: light: isl29501: Add support for the ISL29501 ToF sensor.
Quoted 2 lines
This patch adds support for the ISL29501 Time of Flight sensor.
some comments below
Quoted 40 lines
Signed-off-by Mathieu Othacehe <[email protected]>---
Hi Jonathan,Thanks for the review, here is a v2 with the following changes:
v2:
* Add missing eof new line
* Do not set .indexed to true for all channels
* Add a .extended to differenciate "magnitude" and "ambient_light" intensity channels
* Returnisl29501_begin_acquisitionresult inisl29501_init_chip
* Fix buffer size inisl29501_trigger_handlerI agree with you about extended fields, I struggled to handle the shared exponent and all the coefficient related code is quite annoying :(
Thanks,
Mathieu
Documentation/ABI/testing/sysfs-bus-iio-isl29501 | 49 + .../devicetree/bindings/iio/light/isl29501.txt | 13 + drivers/iio/light/Kconfig | 13 + drivers/iio/light/Makefile | 1 + drivers/iio/light/isl29501.c | 1202 ++++++++++++++++++++ 5 files changed, 1278 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-isl29501 create mode 100644 Documentation/devicetree/bindings/iio/light/isl29501.txt create mode 100644 drivers/iio/light/isl29501.cDocumentation/ABI/testing/sysfs-bus-iio-isl29501 new file +2
-0@@ -0,0 +1,49 @@ +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_agc_gain +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_agc_gain_bias
Documentation/ABI/testing/sysfs-bus-iio-isl29501 line 12
Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-iio-isl29501 new file +10
-0+KernelVersion: 4.18 +Contact: [email protected] +Description: + This sensor has an automatic gain control (agc) loop + which sets the analog signal levels at an optimum + level by controlling programmable gain amplifiers. The + criteria for optimal gain is determined by the sensor. + + Return the actual gain value as an integer between 0 + and 65536 when read from.
can you be more specific what 'in between' means?
0 .. 2^16 or rather 0 .. 2^16 - 1?
Quoted 4 lines
Documentation/ABI/testing/sysfs-bus-iio-isl29501 new file +4
-0+ The agc gain read when measuring crosstalk shall be + written into in_proximity0_agc_gain_bias. + +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_temp_a
Documentation/ABI/testing/sysfs-bus-iio-isl29501 line 27
Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-iio-isl29501 new file +10
-0+What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_temp_b +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_amb_a +What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calib_phase_amb_b +KernelVersion: 4.18 +Contact: [email protected] +Description: + The sensor is able to perform correction of distance + measurements due to changing temperature and ambient + light conditions. It can be programmed to correct for + a second order error polynomial.
IIO uses 'intensity' to denote light intensity, maybe better than 'amb'
enum correction_coeff uses LIGHT
Quoted 12 lines
Documentation/ABI/testing/sysfs-bus-iio-isl29501 new file +12
-0+ + Phase data has to be collected when temperature and + ambient light are modulated independently. + + Then a least squares curve fit to a second order + polynomial has to be generated from the data. The + resultant curves have the form ax^2 + bx + c. + + From those two curves, a and b coefficients shall be + stored in in_proximity0_calib_phase_temp_a and + in_proximity0_calib_phase_temp_b for temperature and + in in_proximity0_calib_phase_amb_a and
Documentation/ABI/testing/sysfs-bus-iio-isl29501 line 49
Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-iio-isl29501 new file +10
-0+ in_proximity0_calib_phase_amb_b for ambient light. + + Those values must be integer between 0 and 8355840 + inclusive. + + Finally, the c constant is set by the sensor + internally. + + Get those values from hardware and show them when read + from.
the purpose of the last sentence is not clear
Quoted 149 lines
Documentation/devicetree/bindings/iio/light/isl29501.txt new file +13
-0@@ -0,0 +1,13 @@ +* ISL29501 Time-of-flight sensor. + +Required properties: + + - compatible : should be "renesas,isl29501" + - reg : the I2C address of the sensor + +Example: + +isl29501@57 { + compatible = "renesas,isl29501"; + reg = <0x57>; +};drivers/iio/light/Kconfig +13
-0@@ -194,6 +194,19 @@ config ISL29125 To compile this driver as a module, choose M here: the module will be called isl29125. +config ISL29501 + tristate "Intersil ISL29501 Time Of Flight sensor" + depends on I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + select IIO_KFIFO_BUF + help + Say Y here if you want to build a driver for the Intersil ISL29501 + Time of Flight sensor. + + To compile this driver as a module, choose M here: the module will be + called isl29501. + config HID_SENSOR_ALS depends on HID_SENSOR_HUB select IIO_BUFFERdrivers/iio/light/Makefile +1
-0@@ -23,6 +23,7 @@ obj-$(CONFIG_HID_SENSOR_PROX) += hid-sensor-prox.o obj-$(CONFIG_SENSORS_ISL29018) += isl29018.o obj-$(CONFIG_SENSORS_ISL29028) += isl29028.o obj-$(CONFIG_ISL29125) += isl29125.o +obj-$(CONFIG_ISL29501) += isl29501.o obj-$(CONFIG_JSA1212) += jsa1212.o obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o obj-$(CONFIG_LTR501) += ltr501.odrivers/iio/light/isl29501.c new file +88
-0@@ -0,0 +1,1202 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* isl29501.c: ISL29501 Time of Flight sensor driver. +* +* Copyright (C) 2018 +* Author: Mathieu Othacehe <[email protected]> +* +* 7-bit I2C slave address: 0x57 +*/ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/i2c.h> +#include <linux/err.h> +#include <linux/of_device.h> +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> + +#include <linux/iio/trigger_consumer.h> +#include <linux/iio/buffer.h> +#include <linux/iio/triggered_buffer.h> + +/* Control, setting and status registers */ +#define ISL29501_DEVICE_ID 0x00 +#define ISL29501_ID 0x0A + +/* Sampling control registers */ +#define ISL29501_INTEGRATION_PERIOD 0x10 +#define ISL29501_SAMPLE_PERIOD 0x11 + +/* Closed loop calibration registers */ +#define ISL29501_CROSSTALK_I_MSB 0x24 +#define ISL29501_CROSSTALK_I_LSB 0x25 +#define ISL29501_CROSSTALK_I_EXPONENT 0x26 +#define ISL29501_CROSSTALK_Q_MSB 0x27 +#define ISL29501_CROSSTALK_Q_LSB 0x28 +#define ISL29501_CROSSTALK_Q_EXPONENT 0x29 +#define ISL29501_CROSSTALK_GAIN_MSB 0x2A +#define ISL29501_CROSSTALK_GAIN_LSB 0x2B +#define ISL29501_MAGNITUDE_REF_EXP 0x2C +#define ISL29501_MAGNITUDE_REF_MSB 0x2D +#define ISL29501_MAGNITUDE_REF_LSB 0x2E +#define ISL29501_PHASE_OFFSET_MSB 0x2F +#define ISL29501_PHASE_OFFSET_LSB 0x30 + +/* Analog control registers */ +#define ISL29501_DRIVER_RANGE 0x90 +#define ISL29501_EMITTER_DAC 0x91 + +#define ISL29501_COMMAND_REGISTER 0xB0 + +/* Commands */ +#define ISL29501_EMUL_SAMPLE_START_PIN 0x49 +#define ISL29501_RESET_ALL_REGISTERS 0xD7 +#define ISL29501_RESET_INT_SM 0xD1 + +/* Ambiant light and temperature corrections */ +#define ISL29501_TEMP_REFERENCE 0x31 +#define ISL29501_PHASE_EXPONENT 0x33 +#define ISL29501_TEMP_COEFF_A 0x34 +#define ISL29501_TEMP_COEFF_B 0x39 +#define ISL29501_AMBIANT_COEFF_A 0x36 +#define ISL29501_AMBIANT_COEFF_B 0x3B + +/* Data output registers */ +#define ISL29501_DISTANCE_MSB_DATA 0xD1 +#define ISL29501_DISTANCE_LSB_DATA 0xD2 +#define ISL29501_PRECISION_MSB 0xD3 +#define ISL29501_PRECISION_LSB 0xD4 +#define ISL29501_MAGNITUDE_EXPONENT 0xD5 +#define ISL29501_MAGNITUDE_MSB 0xD6 +#define ISL29501_MAGNITUDE_LSB 0xD7 +#define ISL29501_PHASE_MSB 0xD8 +#define ISL29501_PHASE_LSB 0xD9 +#define ISL29501_I_RAW_EXPONENT 0xDA +#define ISL29501_I_RAW_MSB 0xDB +#define ISL29501_I_RAW_LSB 0xDC +#define ISL29501_Q_RAW_EXPONENT 0xDD +#define ISL29501_Q_RAW_MSB 0xDE +#define ISL29501_Q_RAW_LSB 0xDF +#define ISL29501_DIE_TEMPERATURE 0xE2 +#define ISL29501_AMBIENT_LIGHT 0xE3 +#define ISL29501_GAIN_MSB 0xE6 +#define ISL29501_GAIN_LSB 0xE7 + +#define ISL29501_MAX_EXP_VAL 15 + +#define ISL29501_INT_TIME_AVAILABLE \
drivers/iio/light/isl29501.c line 98
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+"0.00007 0.00014 0.00028 0.00057 0.00114 " \ +"0.00228 0.00455 0.00910 0.01820 0.03640 " \ + "0.07281 0.14561" + +#define ISL29501_CURRENT_SCALE_AVAILABLE \ + "0.0039 0.0078 0.0118 0.0157 0.0196 " \ + "0.0235 0.0275 0.0314 0.0352 0.0392 " \ + "0.0431 0.0471 0.0510 0.0549 0.0588" + +enum correction_coeff {
isl29501_correction_coeff
Quoted 5 lines
drivers/iio/light/isl29501.c new file +5
-0+COEFF_TEMP_A , +COEFF_TEMP_B , +COEFF_LIGHT_A , +COEFF_LIGHT_B , + COEFF_MAX,
drivers/iio/light/isl29501.c line 113
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+}; + +struct isl29501_private { +struct i2c_client *client; +struct mutex lock; +/* Exact representation of correction coefficients. */ +unsigned int shadow_coeffs[COEFF_MAX ]; +}; + +enum register_name {
isl29501_register_name
Quoted 26 lines
drivers/iio/light/isl29501.c new file +26
-0+REG_DISTANCE , +REG_AMPLITUDE , +REG_PHASE , +REG_I_RAW , +REG_Q_RAW , +REG_TEMPERATURE , +REG_AMBIENT_LIGHT , +REG_GAIN , +REG_I_BIAS , +REG_Q_BIAS , +REG_GAIN_BIAS , +REG_AMPL_BIAS , +REG_PHASE_EXP , +REG_CALIB_PHASE_TEMP_A , +REG_CALIB_PHASE_TEMP_B , +REG_CALIB_PHASE_AMB_A , +REG_CALIB_PHASE_AMB_B , +REG_DISTANCE_BIAS , +REG_TEMPERATURE_BIAS , +REG_INT_TIME , +REG_SAMPLE_TIME , +REG_DRIVER_RANGE , + REG_EMITTER_DAC, +}; + +struct isl29501_register_desc {
drivers/iio/light/isl29501.c line 149
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+u8 msb; +u8 lsb; +u8 exp; +}; + +static const struct isl29501_register_desc isl29501_registers[] = { + [REG_DISTANCE ] = { + .msb =ISL29501_DISTANCE_MSB_DATA , + .lsb =ISL29501_DISTANCE_LSB_DATA , +
delete newline
Quoted 74 lines
drivers/iio/light/isl29501.c new file +74
-0+ }, + [REG_AMPLITUDE] = { + .msb = ISL29501_MAGNITUDE_MSB, + .lsb = ISL29501_MAGNITUDE_LSB, + .exp = ISL29501_MAGNITUDE_EXPONENT, + }, + [REG_PHASE] = { + .msb = ISL29501_PHASE_MSB, + .lsb = ISL29501_PHASE_LSB, + }, + [REG_I_RAW] = { + .msb = ISL29501_I_RAW_MSB, + .lsb = ISL29501_I_RAW_LSB, + .exp = ISL29501_I_RAW_EXPONENT, + }, + [REG_Q_RAW] = { + .msb = ISL29501_Q_RAW_MSB, + .lsb = ISL29501_Q_RAW_LSB, + .exp = ISL29501_Q_RAW_EXPONENT, + }, + [REG_TEMPERATURE] = { + .lsb = ISL29501_DIE_TEMPERATURE, + }, + [REG_AMBIENT_LIGHT] = { + .lsb = ISL29501_AMBIENT_LIGHT, + }, + [REG_GAIN] = { + .msb = ISL29501_GAIN_MSB, + .lsb = ISL29501_GAIN_LSB, + }, + [REG_I_BIAS] = { + .msb = ISL29501_CROSSTALK_I_MSB, + .lsb = ISL29501_CROSSTALK_I_LSB, + .exp = ISL29501_CROSSTALK_I_EXPONENT, + }, + [REG_Q_BIAS] = { + .msb = ISL29501_CROSSTALK_Q_MSB, + .lsb = ISL29501_CROSSTALK_Q_LSB, + .exp = ISL29501_CROSSTALK_Q_EXPONENT, + }, + [REG_GAIN_BIAS] = { + .msb = ISL29501_CROSSTALK_GAIN_MSB, + .lsb = ISL29501_CROSSTALK_GAIN_LSB, + }, + [REG_AMPL_BIAS] = { + .msb = ISL29501_MAGNITUDE_REF_MSB, + .lsb = ISL29501_MAGNITUDE_REF_LSB, + .exp = ISL29501_MAGNITUDE_REF_EXP, + }, + [REG_PHASE_EXP] = { + .lsb = ISL29501_PHASE_EXPONENT, + }, + [REG_CALIB_PHASE_TEMP_A] = { + .lsb = ISL29501_TEMP_COEFF_A, + }, + [REG_CALIB_PHASE_TEMP_B] = { + .lsb = ISL29501_TEMP_COEFF_B, + }, + [REG_CALIB_PHASE_AMB_A] = { + .lsb = ISL29501_AMBIANT_COEFF_A, + }, + [REG_CALIB_PHASE_AMB_B] = { + .lsb = ISL29501_AMBIANT_COEFF_B, + }, + [REG_DISTANCE_BIAS] = { + .msb = ISL29501_PHASE_OFFSET_MSB, + .lsb = ISL29501_PHASE_OFFSET_LSB, + }, + [REG_TEMPERATURE_BIAS] = { + .lsb = ISL29501_TEMP_REFERENCE, + }, + [REG_INT_TIME] = { + .lsb = ISL29501_INTEGRATION_PERIOD, + },
drivers/iio/light/isl29501.c line 233
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+ [REG_SAMPLE_TIME] = { + .lsb = ISL29501_SAMPLE_PERIOD, + }, + [REG_DRIVER_RANGE] = { + .lsb = ISL29501_DRIVER_RANGE, + }, + [REG_EMITTER_DAC] = { + .lsb = ISL29501_EMITTER_DAC, + }, +
delete newline
Quoted 47 lines
drivers/iio/light/isl29501.c new file +47
-0+}; + +static int isl29501_register_read (struct isl29501_private *isl29501, +enum register_name name, +u32 *val) +{ +const struct isl29501_register_desc *reg = &isl29501_registers[name]; +u8 msb =0 , lsb =0 , exp =0 ; +s32 ret; + +if (reg->msb) { + ret =i2c_smbus_read_byte_data (isl29501->client, reg->msb); +if (ret <0 ) +return ret; + msb = ret; + } + +if (reg->lsb) { + ret =i2c_smbus_read_byte_data (isl29501->client, reg->lsb); +if (ret <0 ) +return ret; + lsb = ret; + } + +if (reg->exp) { + ret =i2c_smbus_read_byte_data (isl29501->client, reg->exp); +if (ret <0 ) +return ret; + exp = ret; + } + + *val = ((msb <<8 ) + lsb) << exp; + +return 0 ; +} + +static u32 isl29501_register_write (struct isl29501_private *isl29501, +enum register_name name, +u32 value) +{ +const struct isl29501_register_desc *reg = &isl29501_registers[name]; +u8 msb, lsb; +int ret; + +if (!reg->msb && value >U8_MAX ) +return -ERANGE ; +
drivers/iio/light/isl29501.c line 290
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+if (!reg->exp && value >U16_MAX ) +return -ERANGE ; + +if (reg->exp) { +/* TODO: Implement float write */ +return -EINVAL ; + } + +if (!reg->msb) { + msb =0 ;
msb is not used
drivers/iio/light/isl29501.c line 297
Quoted 7 lines
drivers/iio/light/isl29501.c new file +7
-0+ lsb = value &0xFF ; + }else { + msb = (value >>8 ) &0xFF ; + lsb = value &0xFF ; + } + +if (reg->msb) {
maybe could be folded into above if statement
drivers/iio/light/isl29501.c line 304
Quoted 7 lines
drivers/iio/light/isl29501.c new file +7
-0+ ret =i2c_smbus_write_byte_data (isl29501->client, + reg->msb, msb); +if (ret <0 ) +return ret; + } + + ret =i2c_smbus_write_byte_data (isl29501->client, reg->lsb, lsb);
just
return i2c_smbus_write_byte_data(..);
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+if (ret <0 ) +return ret; + +return 0 ; +} + +static ssize_t isl29501_read_ext (struct iio_dev *indio_dev, +uintptr_t private, +const struct iio_chan_spec *chan, +char *buf)
drivers/iio/light/isl29501.c line 324
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+{ +struct isl29501_private *isl29501 =iio_priv (indio_dev); +enum register_name reg = private; +int ret; +u32 value, gain, coeff, exp; + +switch (reg) { +case REG_GAIN : +case REG_GAIN_BIAS : +mutex_lock (&isl29501->lock);
there is no call to register_read() which is unlocked, maybe move the locking into the function?
Quoted 76 lines
drivers/iio/light/isl29501.c new file +76
-0+ ret =isl29501_register_read (isl29501, reg, &gain); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + + value = gain; +break ; +case REG_CALIB_PHASE_TEMP_A : +case REG_CALIB_PHASE_TEMP_B : +case REG_CALIB_PHASE_AMB_A : +case REG_CALIB_PHASE_AMB_B : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_PHASE_EXP , &exp); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501, reg, &coeff); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + + value = coeff << exp; +break ; +default : +return -EINVAL ; + } + +return sprintf (buf,"%u\n" , value); +} + +static int isl29501_set_shadow_coeff (struct isl29501_private *isl29501, +enum register_name reg, +unsigned int val) +{ +enum correction_coeff coeff; + +switch (reg) { +case REG_CALIB_PHASE_TEMP_A : + coeff =COEFF_TEMP_A ; +break ; +case REG_CALIB_PHASE_TEMP_B : + coeff =COEFF_TEMP_B ; +break ; +case REG_CALIB_PHASE_AMB_A : + coeff =COEFF_LIGHT_A ; +break ; +case REG_CALIB_PHASE_AMB_B : + coeff =COEFF_LIGHT_B ; +break ; +default : +return -EINVAL ; + } + isl29501->shadow_coeffs[coeff] = val; + +return 0 ; +} + +static int isl29501_write_coeff (struct isl29501_private *isl29501, +enum correction_coeff coeff, +int val) +{ +enum register_name reg; +int ret; + +switch (coeff) { +case COEFF_TEMP_A : + reg =REG_CALIB_PHASE_TEMP_A ; +break ; +case COEFF_TEMP_B : + reg =REG_CALIB_PHASE_TEMP_B ; +break ; +case COEFF_LIGHT_A : + reg =REG_CALIB_PHASE_AMB_A ; +break ;
drivers/iio/light/isl29501.c line 410
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+case COEFF_LIGHT_B : + reg =REG_CALIB_PHASE_AMB_B ; +break ; +default : +return -EINVAL ; + } + +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501, reg, val); +mutex_unlock (&isl29501->lock);
just
return ret;
Quoted 146 lines
drivers/iio/light/isl29501.c new file +146
-0+if (ret <0 ) +return ret; + +return 0 ; +} + +static unsigned int isl29501_find_corr_exp (unsigned int val, +unsigned int max_exp, +unsigned int max_mantissa) +{ +unsigned int exp =1 ; + +/* +* Correction coefficients are represented under +* mantissa * 2^exponent form, where mantissa and exponent +* are stored in two separate registers of the sensor. +* +* Compute and return the lowest exponent such as: +* mantissa = value / 2^exponent +* +* where mantissa < max_mantissa. +*/ +if (val <= max_mantissa) +return 0 ; + +while ((val >> exp) > max_mantissa) { + exp++; + +if (exp > max_exp) +return max_exp; + } + +return exp; +} + +static ssize_t isl29501_write_ext (struct iio_dev *indio_dev, +uintptr_t private, +const struct iio_chan_spec *chan, +const char *buf,size_t len) +{ +struct isl29501_private *isl29501 =iio_priv (indio_dev); +enum register_name reg = private; +unsigned int val; +int max_exp =0 ; +int ret; +int i; + + ret =kstrtouint (buf,10 , &val); +if (ret) +return ret; + +switch (reg) { +case REG_GAIN_BIAS : +if (val >U16_MAX ) +return -ERANGE ; + +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501, reg, val); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +break ; +case REG_CALIB_PHASE_TEMP_A : +case REG_CALIB_PHASE_TEMP_B : +case REG_CALIB_PHASE_AMB_A : +case REG_CALIB_PHASE_AMB_B : + +if (val > (U8_MAX <<ISL29501_MAX_EXP_VAL )) +return -ERANGE ; + +/* Store the correction coefficient under its exact form. */ + ret =isl29501_set_shadow_coeff (isl29501, reg, val); +if (ret <0 ) +return ret; + +/* +* Find the highest exponent needed to represent +* correction coefficients. +*/ +for (i =0 ; i <COEFF_MAX ; i++) { +int corr; +int corr_exp; + + corr = isl29501->shadow_coeffs[i]; + corr_exp =isl29501_find_corr_exp (corr, +ISL29501_MAX_EXP_VAL , +U8_MAX /2 ); +dev_dbg (&isl29501->client->dev, +"found exp of corr(%d) = %d\n" , corr, corr_exp); + + max_exp =max (max_exp, corr_exp); + } + +/* +* Represent every correction coefficient under +* mantissa * 2^max_exponent form and force the +* writing of those coefficients on the sensor. +*/ +for (i =0 ; i <COEFF_MAX ; i++) { +int corr; +int mantissa; + + corr = isl29501->shadow_coeffs[i]; +if (!corr) +continue ; + + mantissa = corr >> max_exp; + + ret =isl29501_write_coeff (isl29501, i, mantissa); +if (ret <0 ) +return ret; + } + +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501,REG_PHASE_EXP , max_exp); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +break ; +default : +return -EINVAL ; + } + +return len; +} + +#define _ISL29501_EXT_INFO (_name, _ident) { \ + .name = _name, \ + .read = isl29501_read_ext, \ + .write = isl29501_write_ext, \ + .private = _ident, \ + .shared = IIO_SEPARATE, \ +} + +static const struct iio_chan_spec_ext_info isl29501_ext_info[] = { +_ISL29501_EXT_INFO ("agc_gain" ,REG_GAIN ), +_ISL29501_EXT_INFO ("agc_gain_bias" ,REG_GAIN_BIAS ), +_ISL29501_EXT_INFO ("calib_phase_temp_a" ,REG_CALIB_PHASE_TEMP_A ), +_ISL29501_EXT_INFO ("calib_phase_temp_b" ,REG_CALIB_PHASE_TEMP_B ), +_ISL29501_EXT_INFO ("calib_phase_amb_a" ,REG_CALIB_PHASE_AMB_A ), +_ISL29501_EXT_INFO ("calib_phase_amb_b" ,REG_CALIB_PHASE_AMB_B ), + { }, +}; +
drivers/iio/light/isl29501.c line 566
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+#define ISL29501_DISTANCE_SCAN_INDEX 0 +#define ISL29501_TIMESTAMP_SCAN_INDEX 1 + +#define ISL29501_MAGNITUDE_CHANNEL 0 +#define ISL29501_AMBIENT_LIGHT_CHANNEL 1 + +static const struct iio_chan_spec isl29501_channels[] = { + { + .type =IIO_PROXIMITY , + .channel =0 ,
.channel = 0 not needed
Quoted 8 lines
drivers/iio/light/isl29501.c new file +8
-0+ .scan_index = ISL29501_DISTANCE_SCAN_INDEX, + .info_mask_separate = +BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_SCALE ) | + BIT(IIO_CHAN_INFO_CALIBBIAS), + .scan_type = { + .sign = 'u', + .realbits = 16,
drivers/iio/light/isl29501.c line 584
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+ .storagebits = 16, + .endianness = IIO_CPU, + }, + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) | + BIT(IIO_CHAN_INFO_SAMP_FREQ), + .ext_info = isl29501_ext_info, + }, + { + .type = IIO_PHASE, + .channel = 0,
.channel = 0 not needed
drivers/iio/light/isl29501.c line 591
Quoted 7 lines
drivers/iio/light/isl29501.c new file +7
-0+ .scan_index = -1, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE), + }, + { + .type = IIO_INTENSITY, + .channel = ISL29501_MAGNITUDE_CHANNEL,
.channel not needed?
drivers/iio/light/isl29501.c line 599
Quoted 8 lines
drivers/iio/light/isl29501.c new file +8
-0+ .extend_name = "magnitude", + .scan_index = -1, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_CALIBBIAS), + }, + { + .type = IIO_INTENSITY, + .channel = ISL29501_MAGNITUDE_CHANNEL,
.channel not needed?
Quoted 8 lines
drivers/iio/light/isl29501.c new file +8
-0+ .scan_index = -1, + .modified = 1, + .channel2 = IIO_MOD_I, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_CALIBBIAS), + }, + { + .type = IIO_INTENSITY,
drivers/iio/light/isl29501.c line 617
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+ .channel = ISL29501_MAGNITUDE_CHANNEL, + .scan_index = -1, + .modified = 1, + .channel2 = IIO_MOD_Q, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_CALIBBIAS), + }, + { + .type = IIO_CURRENT, + .channel = 0,
.channel not needed
drivers/iio/light/isl29501.c line 625
Quoted 8 lines
drivers/iio/light/isl29501.c new file +8
-0+ .scan_index = -1, + .output = 1, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE), + }, + { + .type = IIO_TEMP, + .channel = 0,
.channel not needed
drivers/iio/light/isl29501.c line 633
Quoted 8 lines
drivers/iio/light/isl29501.c new file +8
-0+ .scan_index = -1, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE) | + BIT(IIO_CHAN_INFO_CALIBBIAS), + }, + { + .type = IIO_INTENSITY, + .channel = ISL29501_AMBIENT_LIGHT_CHANNEL,
.channel not needed?
Quoted 48 lines
drivers/iio/light/isl29501.c new file +48
-0+ .extend_name = "ambient_light", + .scan_index = -1, + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_SCALE ), + }, + IIO_CHAN_SOFT_TIMESTAMP(ISL29501_TIMESTAMP_SCAN_INDEX), +}; + +static int isl29501_reset_registers (struct isl29501_private *isl29501) +{ +int ret; + + ret =i2c_smbus_write_byte_data (isl29501->client, +ISL29501_COMMAND_REGISTER , +ISL29501_RESET_ALL_REGISTERS ); + ret =i2c_smbus_write_byte_data (isl29501->client, +ISL29501_COMMAND_REGISTER , +ISL29501_RESET_INT_SM ); +if (ret <0 ) { +dev_err (&isl29501->client->dev, +"cannot reset registers %d\n" , ret); +return ret; + } + +return 0 ; +} + +static int isl29501_begin_acquisition (struct isl29501_private *isl29501) +{ +int ret; + + ret =i2c_smbus_write_byte_data (isl29501->client, +ISL29501_COMMAND_REGISTER , +ISL29501_EMUL_SAMPLE_START_PIN ); +if (ret <0 ) { +dev_err (&isl29501->client->dev, +"cannot begin acquisition %d\n" , ret); +return ret; + } + +return 0 ; +} + +static IIO_CONST_ATTR_INT_TIME_AVAIL (ISL29501_INT_TIME_AVAILABLE ); +static IIO_CONST_ATTR(out_current_scale_available, + ISL29501_CURRENT_SCALE_AVAILABLE); + +static struct attribute *isl29501_attributes[] = {
drivers/iio/light/isl29501.c line 691
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+ &iio_const_attr_integration_time_available.dev_attr.attr, + &iio_const_attr_out_current0_scale_available.dev_attr.attr, +NULL +}; + +static const struct attribute_group isl29501_attribute_group = { + .attrs = isl29501_attributes, +}; + +static const int isl29501_current_st[][2] = {
what is this? st?
drivers/iio/light/isl29501.c line 698
Quoted 7 lines
drivers/iio/light/isl29501.c new file +7
-0+ {0, 3900}, {0, 7800}, {0, 11800}, {0, 15700}, + {0, 19600}, {0, 23500}, {0, 27500}, {0, 31400}, + {0, 35200}, {0, 39200}, {0, 43100}, {0, 47100}, + {0, 51000}, {0, 54900}, {0, 58800}, +}; + +static const int isl29501_int_time[][2] = {
maybe indicate time unit?
Quoted 80 lines
drivers/iio/light/isl29501.c new file +80
-0+ {0, 70}, {0, 140}, {0, 280}, {0, 570}, + {0, 1140}, {0, 2280}, {0, 4550}, {0, 9100}, + {0, 18200}, {0, 36400}, {0, 72810}, {0, 145610} +}; + +static int isl29501_get_raw (struct isl29501_private *isl29501, +const struct iio_chan_spec *chan, +int *raw) +{ +int ret; + +switch (chan->type) { +case IIO_PROXIMITY : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_DISTANCE , raw); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +return IIO_VAL_INT ; +case IIO_INTENSITY : +if (chan->channel ==ISL29501_MAGNITUDE_CHANNEL ) { +switch (chan->channel2) { +case IIO_NO_MOD : +case IIO_MOD_I : +case IIO_MOD_Q : +/* TODO: implement float read */ +return -EINVAL ; +default : +return -EINVAL ; + } + }else if (chan->channel ==ISL29501_AMBIENT_LIGHT_CHANNEL ) { +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501, +REG_AMBIENT_LIGHT , + raw); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +return IIO_VAL_INT ; + }else { +return IIO_VAL_INT ; + } +case IIO_PHASE : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_PHASE , raw); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +return IIO_VAL_INT ; +case IIO_CURRENT : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_EMITTER_DAC , raw); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +return IIO_VAL_INT ; +case IIO_TEMP : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_TEMPERATURE , raw); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +return IIO_VAL_INT ; +default : +return -EINVAL ; + } +} + +static int isl29501_get_scale (struct isl29501_private *isl29501, +const struct iio_chan_spec *chan, +int *val,int *val2) +{ +int ret; +u32 current_scale; +
drivers/iio/light/isl29501.c line 788
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+switch (chan->type) { +case IIO_PROXIMITY : +/* distance = raw_distance * 33.31 / 65536 (m) */ + *val =3331 ; + *val2 =6553600 ; + +return IIO_VAL_FRACTIONAL ; +case IIO_PHASE : +/* phase = raw_phase * 2pi / 65536 (rad) */ + *val =2 *314 ;
3.14 is a poor approximation :)
Quoted 137 lines
drivers/iio/light/isl29501.c new file +137
-0+ *val2 =6553600 ; + +return IIO_VAL_FRACTIONAL ; +case IIO_INTENSITY : +if (chan->channel !=ISL29501_AMBIENT_LIGHT_CHANNEL ) +return -EINVAL ; +/* light = raw_light * 35 / 10000 (mA) */ + *val =35 ; + *val2 =10000 ; + +return IIO_VAL_FRACTIONAL ; +case IIO_CURRENT : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501, +REG_DRIVER_RANGE , + ¤t_scale); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +if (current_scale >ARRAY_SIZE (isl29501_current_st)) +return -EINVAL ; + +if (!current_scale) { + *val =0 ; + *val2 =0 ; +return IIO_VAL_INT ; + } + + *val = isl29501_current_st[current_scale -1 ][0 ]; + *val2 = isl29501_current_st[current_scale -1 ][1 ]; + +return IIO_VAL_INT_PLUS_MICRO ; +case IIO_TEMP : +/* temperature = raw_temperature * 125 / 100000 (milli °C) */ + *val =125 ; + *val2 =100000 ; + +return IIO_VAL_FRACTIONAL ; +default : +return -EINVAL ; + } +} + +static int isl29501_get_calibbias (struct isl29501_private *isl29501, +const struct iio_chan_spec *chan, +int *bias) +{ +int ret; + +switch (chan->type) { +case IIO_PROXIMITY : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_DISTANCE_BIAS , bias); +mutex_unlock (&isl29501->lock); + +return ret; +case IIO_INTENSITY : +if (chan->channel !=ISL29501_MAGNITUDE_CHANNEL ) +return -EINVAL ; + +switch (chan->channel2) { +case IIO_NO_MOD : +case IIO_MOD_I : +case IIO_MOD_Q : +/* TODO: Implement float read */ +return -EINVAL ; +default : +return -EINVAL ; + } +case IIO_TEMP : +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501, +REG_TEMPERATURE_BIAS , + bias); +mutex_unlock (&isl29501->lock); + +return ret; +default : +return -EINVAL ; + } +} + +static int isl29501_get_inttime (struct isl29501_private *isl29501, +int *val,int *val2) +{ +int ret; +u32 inttime; + +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_INT_TIME , &inttime); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +if (inttime >=ARRAY_SIZE (isl29501_int_time)) +return -EINVAL ; + + *val = isl29501_int_time[inttime][0 ]; + *val2 = isl29501_int_time[inttime][1 ]; + +return IIO_VAL_INT_PLUS_MICRO ; +} + +static int isl29501_get_freq (struct isl29501_private *isl29501, +int *val,int *val2) +{ +int ret; +int sample_time; +unsigned long long freq; + +mutex_lock (&isl29501->lock); + ret =isl29501_register_read (isl29501,REG_SAMPLE_TIME , &sample_time); +mutex_unlock (&isl29501->lock); +if (ret <0 ) +return ret; + +/* freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */ + freq =1000000ULL *1000000ULL / (450ULL * (sample_time +1ULL )); + + *val = freq /1000000 ; + *val2 = freq %1000000 ; + +return IIO_VAL_INT_PLUS_MICRO ; +} + +static int isl29501_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int *val, +int *val2,long mask) +{ +struct isl29501_private *isl29501 =iio_priv (indio_dev); + +switch (mask) { +case IIO_CHAN_INFO_RAW : +return isl29501_get_raw (isl29501, chan, val); +case IIO_CHAN_INFO_SCALE : +return isl29501_get_scale (isl29501, chan, val, val2);
drivers/iio/light/isl29501.c line 935
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+case IIO_CHAN_INFO_INT_TIME : +return isl29501_get_inttime (isl29501, val, val2); +case IIO_CHAN_INFO_SAMP_FREQ : +return isl29501_get_freq (isl29501, val, val2); +case IIO_CHAN_INFO_CALIBBIAS : +return isl29501_get_calibbias (isl29501, chan, val); +default : +return -EINVAL ; + } +
dead code?
Quoted 138 lines
drivers/iio/light/isl29501.c new file +138
-0+return -EINVAL ; +} + +static int isl29501_set_raw (struct isl29501_private *isl29501, +const struct iio_chan_spec *chan, +int raw) +{ +int ret; + +switch (chan->type) { +case IIO_CURRENT : +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501,REG_EMITTER_DAC , raw); +mutex_unlock (&isl29501->lock); + +return ret; +default : +return -EINVAL ; + } +} + +static int isl29501_set_inttime (struct isl29501_private *isl29501, +int val,int val2) +{ +int ret; +int i; + +for (i =0 ; i <ARRAY_SIZE (isl29501_int_time); i++) { +if (isl29501_int_time[i][0 ] == val && + isl29501_int_time[i][1 ] == val2) { +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501, +REG_INT_TIME , + i); +mutex_unlock (&isl29501->lock); +return ret; + } + } + +return -EINVAL ; +} + +static int isl29501_set_scale (struct isl29501_private *isl29501, +const struct iio_chan_spec *chan, +int val,int val2) +{ +int ret; +int i; + +if (chan->type !=IIO_CURRENT ) +return -EINVAL ; + +for (i =0 ; i <ARRAY_SIZE (isl29501_current_st); i++) { +if (isl29501_current_st[i][0 ] == val && + isl29501_current_st[i][1 ] == val2) { +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501, +REG_DRIVER_RANGE , + i +1 ); +mutex_unlock (&isl29501->lock); +return ret; + } + } + +return -EINVAL ; +} + +static int isl29501_set_calibbias (struct isl29501_private *isl29501, +const struct iio_chan_spec *chan, +int bias) +{ +int ret; + +switch (chan->type) { +case IIO_PROXIMITY : +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501, +REG_DISTANCE_BIAS , + bias); +mutex_unlock (&isl29501->lock); + +return ret; +case IIO_INTENSITY : +if (chan->channel !=ISL29501_MAGNITUDE_CHANNEL ) +return -EINVAL ; + +switch (chan->channel2) { +case IIO_NO_MOD : +case IIO_MOD_I : +case IIO_MOD_Q : +/* TODO: Implement float read */ +return -EINVAL ; +default : +return -EINVAL ; + } +case IIO_TEMP : +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501, +REG_TEMPERATURE_BIAS , + bias); +mutex_unlock (&isl29501->lock); +return ret; +default : +return -EINVAL ; + } +} + +static int isl29501_set_freq (struct isl29501_private *isl29501, +int val,int val2) +{ +int freq; +unsigned long long sample_time; +int ret; + +/* sample_freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */ + freq = val *1000000 + val2 %1000000 ; + sample_time = (2222ULL *1000000ULL / freq) -1ULL ; + +if (sample_time >255 ) +return -ERANGE ; + +mutex_lock (&isl29501->lock); + ret =isl29501_register_write (isl29501,REG_SAMPLE_TIME , sample_time); +mutex_unlock (&isl29501->lock); + +return ret; +} + +static int isl29501_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int val,int val2,long mask) +{ +struct isl29501_private *isl29501 =iio_priv (indio_dev); + +switch (mask) { +case IIO_CHAN_INFO_RAW : +return isl29501_set_raw (isl29501, chan, val); +case IIO_CHAN_INFO_INT_TIME :
drivers/iio/light/isl29501.c line 1083
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+return isl29501_set_inttime (isl29501, val, val2); +case IIO_CHAN_INFO_SAMP_FREQ : +return isl29501_set_freq (isl29501, val, val2); +case IIO_CHAN_INFO_SCALE : +return isl29501_set_scale (isl29501, chan, val, val2); +case IIO_CHAN_INFO_CALIBBIAS : +return isl29501_set_calibbias (isl29501, chan, val); + } + +return -EINVAL ;
maybe move to above, add
defaut:
Quoted 13 lines
drivers/iio/light/isl29501.c new file +13
-0+} + +static const struct iio_info isl29501_info = { + .read_raw = &isl29501_read_raw, + .write_raw = &isl29501_write_raw, + .attrs = &isl29501_attribute_group, +}; + +static int isl29501_init_chip (struct isl29501_private *isl29501) +{ +int ret; + + ret =i2c_smbus_read_byte_data (isl29501->client,ISL29501_DEVICE_ID );
drivers/iio/light/isl29501.c line 1106
Quoted 10 lines
drivers/iio/light/isl29501.c new file +10
-0+if (ret <0 ) { +dev_err (&isl29501->client->dev,"Error reading device id\n" ); +return ret; + } + +if (ret !=ISL29501_ID ) { +dev_err (&isl29501->client->dev, +"Wrong chip id, got %x expected %x\n" , + ret,ISL29501_DEVICE_ID ); +return -EINVAL ;
ENODEV?
Quoted 97 lines
drivers/iio/light/isl29501.c new file +96
-0+ } + + ret =isl29501_reset_registers (isl29501); +if (ret <0 ) +return ret; + +return isl29501_begin_acquisition (isl29501); +} + +static irqreturn_t isl29501_trigger_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *indio_dev = pf->indio_dev; +struct isl29501_private *isl29501 =iio_priv (indio_dev); +const unsigned long *active_mask = indio_dev->active_scan_mask; +u32 buffer[4 ];/* 1x16-bit + ts */ + +if (test_bit (ISL29501_DISTANCE_SCAN_INDEX , active_mask)) { +mutex_lock (&isl29501->lock); +isl29501_register_read (isl29501,REG_DISTANCE , buffer); +mutex_unlock (&isl29501->lock); + } + +iio_push_to_buffers_with_timestamp (indio_dev, buffer, pf->timestamp); +iio_trigger_notify_done (indio_dev->trig); + +return IRQ_HANDLED ; +} + +static int isl29501_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +struct iio_dev *indio_dev; +struct isl29501_private *isl29501; +int ret; + + indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*isl29501)); +if (!indio_dev) +return -ENOMEM ; + + isl29501 =iio_priv (indio_dev); + +i2c_set_clientdata (client, indio_dev); + isl29501->client = client; + +mutex_init (&isl29501->lock); + + ret =isl29501_init_chip (isl29501); +if (ret <0 ) +return ret; + + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->dev.parent = &client->dev; + indio_dev->channels = isl29501_channels; + indio_dev->num_channels =ARRAY_SIZE (isl29501_channels); + indio_dev->name = client->name; + indio_dev->info = &isl29501_info; + + ret =devm_iio_triggered_buffer_setup (&client->dev, indio_dev, + iio_pollfunc_store_time, + isl29501_trigger_handler, +NULL ); +if (ret <0 ) { +dev_err (&client->dev,"unable to setup iio triggered buffer\n" ); +return ret; + } +return devm_iio_device_register (&client->dev, indio_dev); +} + +static const struct i2c_device_id isl29501_id[] = { + {"isl29501" ,0 }, + {} +}; + +MODULE_DEVICE_TABLE (i2c, isl29501_id); + +#if defined(CONFIG_OF ) +static const struct of_device_id isl29501_i2c_matches[] = { + { .compatible ="renesas,isl29501" }, + { } +}; +MODULE_DEVICE_TABLE (of, isl29501_i2c_matches); +#endif + +static struct i2c_driver isl29501_driver = { + .driver = { + .name ="isl29501" , + }, + .id_table = isl29501_id, + .probe = isl29501_probe, +}; +module_i2c_driver (isl29501_driver); + +MODULE_AUTHOR ("Mathieu Othacehe <[email protected]>" ); +MODULE_DESCRIPTION ("ISL29501 Time of Flight sensor driver" ); +MODULE_LICENSE ("GPL v2" );
Signature · 2 lines
Peter Meerwald-Stadler Mobile: +43 664 24 44 418
Re: [PATCH v2 2/2] iio: light: isl29501: Add support for the ISL29501 ToF sensor.
Hi Peter,
Thanks for your comments.
Quoted 3 lines
there is no call to
register_read()which is unlocked, maybe move the locking into the function?
Sure, I will lock/unlock mutexes in register_read and register_write functions.
Quoted 3 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ .channel = ISL29501_MAGNITUDE_CHANNEL,.channel not needed?
I use this channel field to distinguish between channels of the same type in read_raw and write_raw functions so I'd rather keep it, unless there is a better way?
Quoted 3 lines
3.14 is a poor approximation :)
Review
Quoted 1 line
(quoted diff) +1
-0+ *val2 = 6553600;
Indeed :)
I'll use IIO_VAL_INT_PLUS_NANO to express this with the best possible precision.
I'll post a v3 soon taking your remarks into account.
Mathieu