iio: light: veml6070: add integration time and minor cleanups.
Javier Carrasco
maintainercover iio: light: veml6070: add integration time and minor cleanups.
This series adds a missing feature in the veml6070 driver to select the integration time, which also depends on an external restistor that has been added to the corresponding bindings. Its name corresponds to the one provided in the datasheet (Rset) and its units (kilo Ohms), but I am open to more descriptive names if required.
The datasheet provides a Refresh time vs Rset graph (figure 7), which does not clearly specify the minimum and maximum values for Rset. The manufacuter has confirmed that no values under 75 kohms should be used to keep linearity, and the graph does not go beyond 1200 kohms, which is also the biggest Rset used in the application note. The default value of 300 kohms has been obtained from the datasheet, where this value is given as an example (table 4).
When at it, two minor cleanups have been carried out:
- Unsigned int instead of plain unsigned.
- Use of a field for the integration time, which eases the handling of the available integration times turning them into a simple [0, 3] index.
---
Javier Carrasco (4):
iio: light: veml6070: use unsigned int instead of unsigned
iio: light: veml6070: use field to set integration time
dt-bindings: iio: light: veml6075: document rset-kohms
iio: light: veml6070: add support for integration time
.../bindings/iio/light/vishay,veml6075.yaml | 16 +++ drivers/iio/light/veml6070.c | 118 +++++++++++++++++++-- 2 files changed, 128 insertions(+), 6 deletions(-)
---
base-commit: 57573ace0c1b142433dfe3d63ebf375269c80fc1
change-id: 20241014-veml6070-integration-time-78daf4eaad2f
Best regards,
Signature · 1 line
Javier Carrasco <[email protected]>
Javier Carrasco
maintainerPATCH 1/4 iio: light: veml6070: use unsigned int instead of unsigned
Trivial modification to use the recommended keyword 'int' after 'unsigned' for unsigned integers.
--- drivers/iio/light/veml6070.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/light/veml6070.c +2 -2
@@ -87,14 +87,14 @@ static const struct iio_chan_spec veml6070_channels[] = { } }; -static int veml6070_to_uv_index (unsigned val) +static int veml6070_to_uv_index (unsigned int val) {/* * conversion of raw UV intensity values to UV index depends on * integration time (IT) and value of the resistor connected to * the RSET pin (default: 270 KOhm) */ -unsigned uvi[11 ] = { +unsigned int uvi[11 ] = {187 ,373 ,560 ,/* low */ 746 ,933 ,1120 ,/* moderate */ 1308 ,1494 ,/* high */
Signature · 1 line
2.43.0
Re: [PATCH 1/4] iio: light: veml6070: use unsigned int instead of unsigned
Quoted 4 lines · On Thu, 17 Oct 2024 23:39:25 +0200 Javier Carrasco…
Trivial modification to use the recommended keyword 'int' after 'unsigned' for unsigned integers.
Signed-off-by Javier Carrasco <[email protected]>
Applied.
Quoted 26 lines
--- drivers/iio/light/veml6070.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/iio/light/veml6070.c +2
-2@@ -87,14 +87,14 @@ static const struct iio_chan_spec veml6070_channels[] = { } }; -static int veml6070_to_uv_index (unsigned val) +static int veml6070_to_uv_index (unsigned int val) {/* * conversion of raw UV intensity values to UV index depends on * integration time (IT) and value of the resistor connected to * the RSET pin (default: 270 KOhm) */ -unsigned uvi[11 ] = { +unsigned int uvi[11 ] = {187 ,373 ,560 ,/* low */ 746 ,933 ,1120 ,/* moderate */ 1308 ,1494 ,/* high */
Javier Carrasco
maintainerPATCH 2/4 iio: light: veml6070: use field to set integration time
In reply to Javier CarrascoDefine the integration time within the configuration register as a field to easy its handling as an index, preparing the driver to support configurable integration times.
--- drivers/iio/light/veml6070.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/light/veml6070.c +4 -3
@@ -9,6 +9,7 @@* TODO: integration time, ACK signal */ +#include <linux/bitfield.h> #include <linux/module.h> #include <linux/i2c.h> #include <linux/mutex.h>
@@ -28,7 +29,7 @@#define VEML6070_COMMAND_RSRVD BIT(1)/* reserved, set to 1 */ #define VEML6070_COMMAND_SD BIT(0)/* shutdown mode when set */ -#define VEML6070_IT_10 0x04/* integration time 1x */ +#define VEML6070_IT_10 0x01/* integration time 1x */ struct veml6070_data {struct i2c_client *client1;
@@ -172,8 +173,8 @@ static int veml6070_probe(struct i2c_client *client)return dev_err_probe (&client->dev,PTR_ERR (data->client2),"i2c device for second chip address failed\n" ); - data->config =VEML6070_IT_10 |VEML6070_COMMAND_RSRVD | -VEML6070_COMMAND_SD ; + data->config =FIELD_PREP (VEML6070_COMMAND_IT ,VEML6070_IT_10 ) | +VEML6070_COMMAND_RSRVD |VEML6070_COMMAND_SD ; ret =i2c_smbus_write_byte (data->client1, data->config);if (ret <0 )return ret;
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerRe: [PATCH 2/4] iio: light: veml6070: use field to set integration time
Quoted 5 lines · On Thu, 17 Oct 2024 23:39:26 +0200 Javier Carrasco…
Define the integration time within the configuration register as a field to easy its handling as an index, preparing the driver to support configurable integration times.
Signed-off-by Javier Carrasco <[email protected]>
Applied this patch.
Thanks,
Jonathan
Javier Carrasco
maintainerPATCH 3/4 dt-bindings: iio: light: veml6075: document rset-kohms
In reply to Javier CarrascoThe veml6070 provides a configurable integration time by means of an extertnal resistor (Rset in the datasheet) with values between 75 and 1200 kohms.
Document rset-kohms to select the integration time.
--- .../devicetree/bindings/iio/light/vishay,veml6075.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
Documentation/devicetree/bindings/iio/light/vishay,veml6075.yaml +16 -0
@@ -29,6 +29,22 @@ required: - reg - vdd-supply +allOf: + - if: + properties: + compatible: + enum: + - vishay,veml6070 + then: + properties: + rset-kohms: + $ref: /schemas/types.yaml#/definitions/uint32 + description: | + Value in kilo Ohms of the Rset resistor used to select + the integration time. + minimum: 75 + maximum: 1200 + additionalProperties: false examples:
Signature · 1 line
2.43.0
Rob Herring
maintainerRe: [PATCH 3/4] dt-bindings: iio: light: veml6075: document rset-kohms
Quoted 18 lines · Javier Carrasco
The veml6070 provides a configurable integration time by means of an extertnal resistor (Rset in the datasheet) with values between 75 and 1200 kohms.
Document rset-kohms to select the integration time.
Signed-off-by Javier Carrasco <[email protected]>--- .../devicetree/bindings/iio/light/vishay,veml6075.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)Documentation/devicetree/bindings/iio/light/vishay,veml6075.yaml +0
-0@@ -29,6 +29,22 @@ required: - reg - vdd-supply
Documentation/devicetree/bindings/iio/light/vishay,veml6075.yaml required: · line 40
Quoted 10 lines
Documentation/devicetree/bindings/iio/light/vishay,veml6075.yaml +9
-0+allOf: + - if: + properties: + compatible: + enum: + - vishay,veml6070 + then: + properties: + rset-kohms:
Use the documented '-ohms' suffix.
Properties should be defined at the top-level and then restricted here.
Quoted 14 lines
Documentation/devicetree/bindings/iio/light/vishay,veml6075.yaml +7
-0+ $ref: /schemas/types.yaml#/definitions/uint32 + description: | + Value in kilo Ohms of the Rset resistor used to select + the integration time. + minimum: 75 + maximum: 1200 + additionalProperties: false examples:Signature · 2 lines
-- 2.43.0
Jonathan Cameron
maintainerRe: [PATCH 3/4] dt-bindings: iio: light: veml6075: document rset-kohms
In reply to Javier CarrascoQuoted 2 lines · On Thu, 17 Oct 2024 23:39:27 +0200 Javier Carrasco…
The veml6070 provides a configurable integration time by means of an extertnal resistor (Rset in the datasheet) with values between 75 and
Spell check descriptions. external
Quoted 37 lines
1200 kohms.
Document rset-kohms to select the integration time.
Signed-off-by Javier Carrasco <[email protected]>--- .../devicetree/bindings/iio/light/vishay,veml6075.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)Documentation/devicetree/bindings/iio/light/vishay,veml6075.yaml +16
-0@@ -29,6 +29,22 @@ required: - reg - vdd-supply +allOf: + - if: + properties: + compatible: + enum: + - vishay,veml6070 + then: + properties: + rset-kohms: + $ref: /schemas/types.yaml#/definitions/uint32 + description: | + Value in kilo Ohms of the Rset resistor used to select + the integration time. + minimum: 75 + maximum: 1200 + additionalProperties: false examples:
Javier Carrasco
maintainerPATCH 4/4 iio: light: veml6070: add support for integration time
In reply to Javier CarrascoThe integration time of the veml6070 depends on an external resistor (called Rset in the datasheet) and the value configured in the IT field of the command register.
--- drivers/iio/light/veml6070.c | 109 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 2 deletions(-)
drivers/iio/light/veml6070.c +107 -2
@@ -6,7 +6,7 @@* * IIO driver for VEML6070 (7-bit I2C slave addresses 0x38 and 0x39) * -* TODO: integration time, ACK signal +* TODO: ACK signal */ #include <linux/bitfield.h>
@@ -29,15 +29,78 @@#define VEML6070_COMMAND_RSRVD BIT(1)/* reserved, set to 1 */ #define VEML6070_COMMAND_SD BIT(0)/* shutdown mode when set */ -#define VEML6070_IT_10 0x01/* integration time 1x */ +#define VEML6070_IT_05 0x00 +#define VEML6070_IT_10 0x01 +#define VEML6070_IT_20 0x02 +#define VEML6070_IT_40 0x03 + +#define VEML6070_MIN_RSET_KOHM 75 +#define VEML6070_MIN_IT_US 15625/* Rset = 75 kohm, IT = 1/2 */ struct veml6070_data {struct i2c_client *client1;struct i2c_client *client2;u8 config;struct mutex lock; +u32 it[4 ][2 ]; }; +static void veml6070_calc_it (struct device *dev,struct veml6070_data *data) +{ +u32 rset, tmp_it; +int i, ret; + + ret =device_property_read_u32 (dev,"rset-kohms" , &rset); +if (ret) { +dev_warn (dev,"no Rset specified, using default 300 kohms\n" ); + rset =300 ; + } + +if (rset <75 ) { +dev_warn (dev,"Rset too low, using minimum = 75 kohms\n" ); + rset =75 ; + } + +if (rset >1200 ) { +dev_warn (dev,"Rset too high, using maximum = 1200 kohms\n" ); + rset =1200 ; + } + + tmp_it =VEML6070_MIN_IT_US * (rset /VEML6070_MIN_RSET_KOHM ); +for (i =0 ; i <ARRAY_SIZE (data->it); i++) { + data->it[i][0 ] = (tmp_it << i) /1000000 ; + data->it[i][1 ] = (tmp_it << i) %1000000 ; + } +} + +static int veml6070_get_it (struct veml6070_data *data,int *val,int *val2) +{ +int it_idx =FIELD_GET (VEML6070_COMMAND_IT , data->config); + + *val = data->it[it_idx][0 ]; + *val2 = data->it[it_idx][1 ]; + +return IIO_VAL_INT_PLUS_MICRO ; +} + +static int veml6070_set_it (struct veml6070_data *data,int val,int val2) +{ +int it_idx; + +for (it_idx =0 ; it_idx <ARRAY_SIZE (data->it); it_idx++) { +if (data->it[it_idx][0 ] == val && data->it[it_idx][1 ] == val2) +break ; + } + +if (it_idx >=ARRAY_SIZE (data->it)) +return -EINVAL ; + + data->config = (data->config & ~VEML6070_COMMAND_IT ) | +FIELD_PREP (VEML6070_COMMAND_IT , it_idx); + +return i2c_smbus_write_byte (data->client1, data->config); +} +static int veml6070_read (struct veml6070_data *data) {int ret;
@@ -81,10 +144,14 @@ static const struct iio_chan_spec veml6070_channels[] = { .modified = 1, .channel2 = IIO_MOD_LIGHT_UV, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME), }, { .type = IIO_UVINDEX, .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME), } };
@@ -127,6 +194,40 @@ static int veml6070_read_raw(struct iio_dev *indio_dev,else *val = ret;return IIO_VAL_INT ; +case IIO_CHAN_INFO_INT_TIME : +return veml6070_get_it (data, val, val2); +default : +return -EINVAL ; + } +} + +static int veml6070_read_avail (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +const int **vals,int *type,int *length, +long mask) +{ +struct veml6070_data *data =iio_priv (indio_dev); + +switch (mask) { +case IIO_CHAN_INFO_INT_TIME : + *vals = (int *)data->it; + *length =2 *ARRAY_SIZE (data->it); + *type =IIO_VAL_INT_PLUS_MICRO ; +return IIO_AVAIL_LIST ; +default : +return -EINVAL ; + } +} + +static int veml6070_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int val,int val2,long mask) +{ +struct veml6070_data *data =iio_priv (indio_dev); + +switch (mask) { +case IIO_CHAN_INFO_INT_TIME : +return veml6070_set_it (data, val, val2);default :return -EINVAL ; }
@@ -134,6 +235,8 @@ static int veml6070_read_raw(struct iio_dev *indio_dev,static const struct iio_info veml6070_info = { .read_raw = veml6070_read_raw, + .read_avail = veml6070_read_avail, + .write_raw = veml6070_write_raw, };static void veml6070_i2c_unreg (void *p)
@@ -164,6 +267,8 @@ static int veml6070_probe(struct i2c_client *client) indio_dev->name =VEML6070_DRV_NAME ; indio_dev->modes =INDIO_DIRECT_MODE ; +veml6070_calc_it (&client->dev, data); + ret =devm_regulator_get_enable (&client->dev,"vdd" );if (ret <0 )return ret;
Signature · 1 line
2.43.0
Javier Carrasco
maintainerRe: [PATCH 4/4] iio: light: veml6070: add support for integration time
Quoted 159 lines · Javier Carrasco
The integration time of the veml6070 depends on an external resistor (called Rset in the datasheet) and the value configured in the IT field of the command register.
Signed-off-by Javier Carrasco <[email protected]>--- drivers/iio/light/veml6070.c | 109 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 2 deletions(-)drivers/iio/light/veml6070.c +103
-2@@ -6,7 +6,7 @@* * IIO driver for VEML6070 (7-bit I2C slave addresses 0x38 and 0x39) * -* TODO: integration time, ACK signal +* TODO: ACK signal */ #include <linux/bitfield.h> @@ -29,15 +29,78 @@#define VEML6070_COMMAND_RSRVD BIT(1)/* reserved, set to 1 */ #define VEML6070_COMMAND_SD BIT(0)/* shutdown mode when set */ -#define VEML6070_IT_10 0x01/* integration time 1x */ +#define VEML6070_IT_05 0x00 +#define VEML6070_IT_10 0x01 +#define VEML6070_IT_20 0x02 +#define VEML6070_IT_40 0x03 + +#define VEML6070_MIN_RSET_KOHM 75 +#define VEML6070_MIN_IT_US 15625/* Rset = 75 kohm, IT = 1/2 */ struct veml6070_data {struct i2c_client *client1;struct i2c_client *client2;u8 config;struct mutex lock; +u32 it[4 ][2 ]; }; +static void veml6070_calc_it (struct device *dev,struct veml6070_data *data) +{ +u32 rset, tmp_it; +int i, ret; + + ret =device_property_read_u32 (dev,"rset-kohms" , &rset); +if (ret) { +dev_warn (dev,"no Rset specified, using default 300 kohms\n" ); + rset =300 ; + } + +if (rset <75 ) { +dev_warn (dev,"Rset too low, using minimum = 75 kohms\n" ); + rset =75 ; + } + +if (rset >1200 ) { +dev_warn (dev,"Rset too high, using maximum = 1200 kohms\n" ); + rset =1200 ; + } + + tmp_it =VEML6070_MIN_IT_US * (rset /VEML6070_MIN_RSET_KOHM ); +for (i =0 ; i <ARRAY_SIZE (data->it); i++) { + data->it[i][0 ] = (tmp_it << i) /1000000 ; + data->it[i][1 ] = (tmp_it << i) %1000000 ; + } +} + +static int veml6070_get_it (struct veml6070_data *data,int *val,int *val2) +{ +int it_idx =FIELD_GET (VEML6070_COMMAND_IT , data->config); + + *val = data->it[it_idx][0 ]; + *val2 = data->it[it_idx][1 ]; + +return IIO_VAL_INT_PLUS_MICRO ; +} + +static int veml6070_set_it (struct veml6070_data *data,int val,int val2) +{ +int it_idx; + +for (it_idx =0 ; it_idx <ARRAY_SIZE (data->it); it_idx++) { +if (data->it[it_idx][0 ] == val && data->it[it_idx][1 ] == val2) +break ; + } + +if (it_idx >=ARRAY_SIZE (data->it)) +return -EINVAL ; + + data->config = (data->config & ~VEML6070_COMMAND_IT ) | +FIELD_PREP (VEML6070_COMMAND_IT , it_idx); + +return i2c_smbus_write_byte (data->client1, data->config); +} +static int veml6070_read (struct veml6070_data *data) {int ret;@@ -81,10 +144,14 @@ static const struct iio_chan_spec veml6070_channels[] = { .modified = 1, .channel2 = IIO_MOD_LIGHT_UV, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME), }, { .type = IIO_UVINDEX, .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME), } };@@ -127,6 +194,40 @@ static int veml6070_read_raw(struct iio_dev *indio_dev,else *val = ret;return IIO_VAL_INT ; +case IIO_CHAN_INFO_INT_TIME : +return veml6070_get_it (data, val, val2); +default : +return -EINVAL ; + } +} + +static int veml6070_read_avail (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +const int **vals,int *type,int *length, +long mask) +{ +struct veml6070_data *data =iio_priv (indio_dev); + +switch (mask) { +case IIO_CHAN_INFO_INT_TIME : + *vals = (int *)data->it; + *length =2 *ARRAY_SIZE (data->it); + *type =IIO_VAL_INT_PLUS_MICRO ; +return IIO_AVAIL_LIST ; +default : +return -EINVAL ; + } +} + +static int veml6070_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int val,int val2,long mask) +{ +struct veml6070_data *data =iio_priv (indio_dev); + +switch (mask) { +case IIO_CHAN_INFO_INT_TIME : +return veml6070_set_it (data, val, val2);default :return -EINVAL ; }
drivers/iio/light/veml6070.c static int veml6070_read_raw(struct iio_dev *indio_dev, · line 235
Quoted 1 line
drivers/iio/light/veml6070.c +0
-0@@ -134,6 +235,8 @@ static int veml6070_read_raw(struct iio_dev *indio_dev,
I just noticed that the processed data needs to be updated to take into account different integration times because in its current form it provides fixed values for Rset = 270kohm, IT=1T. I will add it to v2, which is required anyway to address changes in the dt-bindings.
Jonathan Cameron
maintainerRe: [PATCH 4/4] iio: light: veml6070: add support for integration time
In reply to Javier CarrascoQuoted 5 lines · On Thu, 17 Oct 2024 23:39:28 +0200 Javier Carrasco…
The integration time of the veml6070 depends on an external resistor (called Rset in the datasheet) and the value configured in the IT field of the command register.
Signed-off-by Javier Carrasco <[email protected]>
Other than your self review (which I would probably have failed to notice!), this looks fine to me.
Jonathan