iio: chemical: ccs811: Make sampling frequency configurable
PATCH v3 1/1 iio: chemical: ccs811: Make sampling frequency configurable
Allow userspace to configure the CCS811 IAQ measurement rate through the IIO sampling_frequency attribute.
Support the three IAQ measurement modes with sampling frequencies of 1 Hz, 0.1 Hz, and approximately 0.016667 Hz, corresponding to measurement intervals of 1, 10, and 60 seconds.
Preserve unrelated bits in the MEAS_MODE register when updating the drive mode, and cache the selected mode after a successful register write. Expose the supported frequencies through sampling_frequency_available.
Changes in v3:
- Reuse the sampling frequency array instead of duplicating frequency values.
- Separate register updates from locking and wakeup handling.
- Use a separate variable for the updated MEAS_MODE register value.
- Simplify the sampling frequency getter.
- Mark the completed drive-mode configuration TODO as done.
Changes in v2:
- Fix the Clang build error caused by guard() in the switch statement.
--- drivers/iio/chemical/ccs811.c | 148 ++++++++++++++++++++++++++++++++-- 1 file changed, 142 insertions(+), 6 deletions(-)
drivers/iio/chemical/ccs811.c +142 -6
@@ -9,7 +9,7 @@* IIO driver for AMS CCS811 (I2C address 0x5A/0x5B set by ADDR Low/High) * * TODO: -* 1. Make the drive mode selectable form userspace +* 1. DONE. Was: "Make the drive mode selectable form userspace" * 2. Add support for interrupts * 3. Adjust time to wait for data to be ready based on selected operation mode * 4. Read error register and put the information in logs
@@ -54,6 +54,7 @@#define CCS811_STATUS_FW_MODE_APPLICATION BIT(7)/* Measurement modes */ +#define CCS811_MODE_MASK GENMASK(6, 4)#define CCS811_MODE_IDLE 0x00#define CCS811_MODE_IAQ_1SEC 0x10#define CCS811_MODE_IAQ_10SEC 0x20
@@ -79,6 +80,7 @@ struct ccs811_data {struct iio_trigger *drdy_trig;struct gpio_desc *wakeup_gpio;bool drdy_trig_on; +u8 drive_mode;/* Ensures correct alignment of timestamp if present */ struct {s16 channels[2 ];
@@ -86,6 +88,12 @@ struct ccs811_data { } scan; }; +static const int ccs811_samp_freq[] = { +1 ,0 , +0 ,100000 , +0 ,16667 , +}; +static const struct iio_chan_spec ccs811_channels[] = { { .type =IIO_CURRENT ,
@@ -103,6 +111,8 @@ static const struct iio_chan_spec ccs811_channels[] = { .modified = 1, .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | BIT(IIO_CHAN_INFO_SCALE), + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), + .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), .scan_index = 0, .scan_type = { .sign = 'u',
@@ -163,16 +173,21 @@ static int ccs811_start_sensor_application(struct i2c_client *client)return 0 ; } -static int ccs811_setup (struct i2c_client *client) +static int ccs811_setup (struct ccs811_data *data) {int ret; - ret =ccs811_start_sensor_application (client); + ret =ccs811_start_sensor_application (data->client); +if (ret <0 ) +return ret; + + ret =i2c_smbus_write_byte_data (data->client,CCS811_MEAS_MODE , +CCS811_MODE_IAQ_1SEC );if (ret <0 )return ret; -return i2c_smbus_write_byte_data (client,CCS811_MEAS_MODE , -CCS811_MODE_IAQ_1SEC ); + data->drive_mode =CCS811_MODE_IAQ_1SEC ; +return 0 ; }static void ccs811_set_wakeup (struct ccs811_data *data,bool enable)
@@ -188,6 +203,52 @@ static void ccs811_set_wakeup(struct ccs811_data *data, bool enable)usleep_range (20 ,30 ); } +static int ccs811_update_sampling_freq (struct ccs811_data *data, +int val,int val2) +{ +u8 drive_mode; +u8 mode; +int ret; + +if (val == ccs811_samp_freq[0 ] && val2 == ccs811_samp_freq[1 ]) + drive_mode =CCS811_MODE_IAQ_1SEC ; +else if (val == ccs811_samp_freq[2 ] && val2 == ccs811_samp_freq[3 ]) + drive_mode =CCS811_MODE_IAQ_10SEC ; +else if (val == ccs811_samp_freq[4 ] && val2 == ccs811_samp_freq[5 ]) + drive_mode =CCS811_MODE_IAQ_60SEC ; +else +return -EINVAL ; + + ret =i2c_smbus_read_byte_data (data->client,CCS811_MEAS_MODE ); +if (ret <0 ) +return ret; + + mode = (ret & ~CCS811_MODE_MASK ) | drive_mode; + + ret =i2c_smbus_write_byte_data (data->client,CCS811_MEAS_MODE , + mode); +if (ret <0 ) +return ret; + + data->drive_mode = drive_mode; + +return 0 ; +} + +static int ccs811_set_sampling_freq (struct ccs811_data *data, +int val,int val2) +{ +int ret; + +guard (mutex)(&data->lock); + +ccs811_set_wakeup (data, true); + ret =ccs811_update_sampling_freq (data, val, val2); +ccs811_set_wakeup (data, false); + +return ret; +} +static int ccs811_get_measurement (struct ccs811_data *data) {int ret, tries =11 ;
@@ -215,6 +276,29 @@ static int ccs811_get_measurement(struct ccs811_data *data)return ret; } +static int ccs811_get_sampling_freq (struct ccs811_data *data, +int *val,int *val2) +{ +guard (mutex)(&data->lock); + +switch (data->drive_mode) { +case CCS811_MODE_IAQ_1SEC : + *val = ccs811_samp_freq[0 ]; + *val2 = ccs811_samp_freq[1 ]; +return IIO_VAL_INT_PLUS_MICRO ; +case CCS811_MODE_IAQ_10SEC : + *val = ccs811_samp_freq[2 ]; + *val2 = ccs811_samp_freq[3 ]; +return IIO_VAL_INT_PLUS_MICRO ; +case CCS811_MODE_IAQ_60SEC : + *val = ccs811_samp_freq[4 ]; + *val2 = ccs811_samp_freq[5 ]; +return IIO_VAL_INT_PLUS_MICRO ; +default : +return -EINVAL ; + } +} +static int ccs811_read_info_raw (struct ccs811_data *data,struct iio_chan_spec const *chan,int *val,int mask)
@@ -249,6 +333,28 @@ static int ccs811_read_info_raw(struct ccs811_data *data, } } +static int ccs811_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int val,int val2,long mask) +{ +struct ccs811_data *data =iio_priv (indio_dev); +int ret; + +switch (mask) { +case IIO_CHAN_INFO_SAMP_FREQ : +if (!iio_device_claim_direct (indio_dev)) +return -EBUSY ; + + ret =ccs811_set_sampling_freq (data, val, val2); + +iio_device_release_direct (indio_dev); +return ret; + +default : +return -EINVAL ; + } +} +static int ccs811_read_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan,int *val,int *val2,long mask)
@@ -293,13 +399,43 @@ static int ccs811_read_raw(struct iio_dev *indio_dev,default :return -EINVAL ; } +case IIO_CHAN_INFO_SAMP_FREQ : +return ccs811_get_sampling_freq (data, val, val2);default :return -EINVAL ; } } +static int ccs811_read_avail (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +const int **vals,int *type,int *length, +long mask) +{ +if (mask !=IIO_CHAN_INFO_SAMP_FREQ ) +return -EINVAL ; + + *vals = ccs811_samp_freq; + *type =IIO_VAL_INT_PLUS_MICRO ; + *length =ARRAY_SIZE (ccs811_samp_freq); + +return IIO_AVAIL_LIST ; +} + +static int ccs811_write_raw_get_fmt (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +long mask) +{ +if (mask ==IIO_CHAN_INFO_SAMP_FREQ ) +return IIO_VAL_INT_PLUS_MICRO ; + +return -EINVAL ; +} +static const struct iio_info ccs811_info = { .read_raw = ccs811_read_raw, + .write_raw = ccs811_write_raw, + .read_avail = ccs811_read_avail, + .write_raw_get_fmt = ccs811_write_raw_get_fmt, }; static int ccs811_set_trigger_state(struct iio_trigger *trig,
@@ -460,7 +596,7 @@ static int ccs811_probe(struct i2c_client *client)return -ENODEV ; } - ret =ccs811_setup (client); + ret =ccs811_setup (data);if (ret <0 ) {ccs811_set_wakeup (data, false);return ret;
Signature · 1 line
2.39.5
Quoted 7 lines · Nikhil Gautam
Allow userspace to configure the CCS811 IAQ measurement rate through the IIO
sampling_frequencyattribute.Support the three IAQ measurement modes with sampling frequencies of 1 Hz, 0.1 Hz, and approximately 0.016667 Hz, corresponding to measurement intervals of 1, 10, and 60 seconds.
Quoted 6 lines
Preserve unrelated bits in the
MEAS_MODEregister when updating the drive mode, and cache the selected mode after a successful register write. Expose the supported frequencies throughsampling_frequency_available.Signed-off-by Nikhil Gautam <[email protected]>
The cutter is missing here, we don't want to see changelog in the commit messages and moreover this contribution is not strictly signed-off, as the tag block is the last in the commit message. This is a show-stopper to take the patch, even if it's desired and correct.
Quoted 11 lines
Changes in v3:
- Reuse the sampling frequency array instead of duplicating frequency values.
- Separate register updates from locking and wakeup handling.
- Use a separate variable for the updatedMEAS_MODEregister value.
- Simplify the sampling frequency getter.
- Mark the completed drive-mode configuration TODO as done.Changes in v2:
- Fix the Clang build error caused byguard()in the switch statement.
---
...
drivers/iio/chemical/ccs811.c struct ccs811_data { · line 95
Quoted 5 lines
drivers/iio/chemical/ccs811.c +5
-0+static const int ccs811_samp_freq[] = { +1 ,0 , +0 ,100000 , +0 ,16667 , +};
Make it double dimension array instead. Each index can be named as CCS811_MODE_IAQ_1SEC and so on, while the shift is applied later on.
With the above being done, the code can use either a for-loop or simple indexing.
*Yes, I see that the value(s) can be used directly in the code, but try to explore an approach that can improve robustness and readability of the code.
...
drivers/iio/chemical/ccs811.c static int ccs811_read_info_raw(struct ccs811_data *data, · line 346
Quoted 8 lines
drivers/iio/chemical/ccs811.c +8
-0+{ +struct ccs811_data *data =iio_priv (indio_dev); +int ret; + +switch (mask) { +case IIO_CHAN_INFO_SAMP_FREQ : +if (!iio_device_claim_direct (indio_dev)) +return -EBUSY ;
Can we use IIO_DEV_ACQUIRE_DIRECT_MODE() here?
Quoted 9 lines
drivers/iio/chemical/ccs811.c +9
-0+ ret =ccs811_set_sampling_freq (data, val, val2); + +iio_device_release_direct (indio_dev); +return ret; + +default : +return -EINVAL ; + } +}
Signature · 2 lines
With Best Regards, Andy Shevchenko