* [PATCH] iio: chemical: ccs811: Make sampling frequency configurable
@ 2026-10-18 17:00 Nikhil Gautam
2026-10-19 7:26 ` kernel test robot
0 siblings, 1 reply; 2+ messages in thread
From: Nikhil Gautam @ 2026-10-18 17:00 UTC (permalink / raw)
To: linux-iio; +Cc: jic23, dlechner, nuno.sa, andy, linux-kernel, Nikhil Gautam
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.
Signed-off-by: Nikhil Gautam <nikhilgtr@gmail.com>
---
drivers/iio/chemical/ccs811.c | 139 +++++++++++++++++++++++++++++++---
1 file changed, 130 insertions(+), 9 deletions(-)
diff --git a/drivers/iio/chemical/ccs811.c b/drivers/iio/chemical/ccs811.c
index d126ee12ffe5..4d157f3f5714 100644
--- a/drivers/iio/chemical/ccs811.c
+++ b/drivers/iio/chemical/ccs811.c
@@ -9,10 +9,9 @@
* IIO driver for AMS CCS811 (I2C address 0x5A/0x5B set by ADDR Low/High)
*
* TODO:
- * 1. 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
+ * 1. Add support for interrupts
+ * 2. Adjust time to wait for data to be ready based on selected operation mode
+ * 3. Read error register and put the information in logs
*/
#include <linux/cleanup.h>
@@ -54,6 +53,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 +79,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 +87,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 +110,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 +172,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 +202,44 @@ static void ccs811_set_wakeup(struct ccs811_data *data, bool enable)
usleep_range(20, 30);
}
+static int ccs811_set_sampling_freq(struct ccs811_data *data,
+ int val, int val2)
+{
+ u8 drive_mode;
+ int ret;
+
+ if (val == 1 && val2 == 0)
+ drive_mode = CCS811_MODE_IAQ_1SEC;
+ else if (val == 0 && val2 == 100000)
+ drive_mode = CCS811_MODE_IAQ_10SEC;
+ else if (val == 0 && val2 == 16667)
+ drive_mode = CCS811_MODE_IAQ_60SEC;
+ else
+ return -EINVAL;
+
+ guard(mutex)(&data->lock);
+ ccs811_set_wakeup(data, true);
+
+ ret = i2c_smbus_read_byte_data(data->client, CCS811_MEAS_MODE);
+ if (ret < 0)
+ goto out;
+
+ ret &= ~CCS811_MODE_MASK;
+ ret |= drive_mode;
+
+ ret = i2c_smbus_write_byte_data(data->client,
+ CCS811_MEAS_MODE, ret);
+ if (ret < 0)
+ goto out;
+
+ data->drive_mode = drive_mode;
+ ret = 0;
+
+out:
+ ccs811_set_wakeup(data, false);
+ return ret;
+}
+
static int ccs811_get_measurement(struct ccs811_data *data)
{
int ret, tries = 11;
@@ -249,6 +301,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 +367,60 @@ static int ccs811_read_raw(struct iio_dev *indio_dev,
default:
return -EINVAL;
}
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ guard(mutex)(&data->lock);
+ switch (data->drive_mode) {
+ case CCS811_MODE_IAQ_1SEC:
+ *val = 1;
+ *val2 = 0;
+ return IIO_VAL_INT_PLUS_MICRO;
+ case CCS811_MODE_IAQ_10SEC:
+ *val = 0;
+ *val2 = 100000;
+ return IIO_VAL_INT_PLUS_MICRO;
+ case CCS811_MODE_IAQ_60SEC:
+ *val = 0;
+ *val2 = 16667;
+ return IIO_VAL_INT_PLUS_MICRO;
+ default:
+ return -EINVAL;
+ }
+
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 +581,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;
--
2.39.5
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] iio: chemical: ccs811: Make sampling frequency configurable
2026-10-18 17:00 [PATCH] iio: chemical: ccs811: Make sampling frequency configurable Nikhil Gautam
@ 2026-10-19 7:26 ` kernel test robot
0 siblings, 0 replies; 2+ messages in thread
From: kernel test robot @ 2026-10-19 7:26 UTC (permalink / raw)
To: Nikhil Gautam, linux-iio
Cc: llvm, oe-kbuild-all, jic23, dlechner, nuno.sa, andy, linux-kernel,
Nikhil Gautam
Hi Nikhil,
kernel test robot noticed the following build errors:
[auto build test ERROR on jic23-iio/togreg]
[also build test ERROR on linus/master v7.3 next-20261016]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Nikhil-Gautam/iio-chemical-ccs811-Make-sampling-frequency-configurable/20261018-223013
base: https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
patch link: https://lore.kernel.org/r/20261018170013.79443-1-nikhilgtr%40gmail.com
patch subject: [PATCH] iio: chemical: ccs811: Make sampling frequency configurable
config: i386-buildonly-randconfig-1003-20261019 (https://download.01.org/0day-ci/archive/20261019/202610191533.IG7DYuci-lkp@intel.com/config)
compiler: clang version 22.1.8 (https://github.com/llvm/llvm-project ca7933e47d3a3451d81e72ac174dcb5aa28b59d1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261019/202610191533.IG7DYuci-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202610191533.IG7DYuci-lkp@intel.com/
All errors (new ones prefixed by >>):
>> drivers/iio/chemical/ccs811.c:389:2: error: cannot jump from switch statement to this case label
389 | default:
| ^
drivers/iio/chemical/ccs811.c:371:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
371 | guard(mutex)(&data->lock);
| ^
include/linux/cleanup.h:423:2: note: expanded from macro 'guard'
423 | CLASS(_name, __UNIQUE_ID(guard))
| ^
include/linux/cleanup.h:303:3: note: expanded from macro 'CLASS'
303 | class_##_name##_constructor
| ^
<scratch space>:136:1: note: expanded from here
136 | class_mutex_constructor
| ^
note: (skipping 3 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
include/linux/compiler_types.h:16:23: note: expanded from macro '__PASTE'
16 | #define __PASTE(a, b) ___PASTE(a, b)
| ^
include/linux/compiler_types.h:15:24: note: expanded from macro '___PASTE'
15 | #define ___PASTE(a, b) a##b
| ^
<scratch space>:142:1: note: expanded from here
142 | __UNIQUE_ID_unlock_406
| ^
drivers/iio/chemical/ccs811.c:371:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
include/linux/cleanup.h:423:15: note: expanded from macro 'guard'
423 | CLASS(_name, __UNIQUE_ID(guard))
| ^
include/linux/compiler.h:165:2: note: expanded from macro '__UNIQUE_ID'
165 | __PASTE(__UNIQUE_ID_, \
| ^
include/linux/compiler_types.h:16:23: note: expanded from macro '__PASTE'
16 | #define __PASTE(a, b) ___PASTE(a, b)
| ^
include/linux/compiler_types.h:15:24: note: expanded from macro '___PASTE'
15 | #define ___PASTE(a, b) a##b
| ^
<scratch space>:130:1: note: expanded from here
130 | __UNIQUE_ID_guard_405
| ^
1 error generated.
vim +389 drivers/iio/chemical/ccs811.c
1a2cae129fb93c Nikhil Gautam 2026-10-18 325
fef6da136ef283 Jonathan Cameron 2025-03-31 326 static int ccs811_read_raw(struct iio_dev *indio_dev,
fef6da136ef283 Jonathan Cameron 2025-03-31 327 struct iio_chan_spec const *chan,
fef6da136ef283 Jonathan Cameron 2025-03-31 328 int *val, int *val2, long mask)
fef6da136ef283 Jonathan Cameron 2025-03-31 329 {
fef6da136ef283 Jonathan Cameron 2025-03-31 330 struct ccs811_data *data = iio_priv(indio_dev);
fef6da136ef283 Jonathan Cameron 2025-03-31 331 int ret;
fef6da136ef283 Jonathan Cameron 2025-03-31 332
fef6da136ef283 Jonathan Cameron 2025-03-31 333 switch (mask) {
fef6da136ef283 Jonathan Cameron 2025-03-31 334 case IIO_CHAN_INFO_RAW:
e9786c540804c3 Jonathan Cameron 2025-03-31 335 if (!iio_device_claim_direct(indio_dev))
e9786c540804c3 Jonathan Cameron 2025-03-31 336 return -EBUSY;
fef6da136ef283 Jonathan Cameron 2025-03-31 337
fef6da136ef283 Jonathan Cameron 2025-03-31 338 ret = ccs811_read_info_raw(data, chan, val, mask);
fef6da136ef283 Jonathan Cameron 2025-03-31 339
e9786c540804c3 Jonathan Cameron 2025-03-31 340 iio_device_release_direct(indio_dev);
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 341
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 342 return ret;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 343
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 344 case IIO_CHAN_INFO_SCALE:
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 345 switch (chan->type) {
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 346 case IIO_VOLTAGE:
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 347 *val = 1;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 348 *val2 = 612903;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 349 return IIO_VAL_INT_PLUS_MICRO;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 350 case IIO_CURRENT:
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 351 *val = 0;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 352 *val2 = 1000;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 353 return IIO_VAL_INT_PLUS_MICRO;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 354 case IIO_CONCENTRATION:
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 355 switch (chan->channel2) {
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 356 case IIO_MOD_CO2:
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 357 *val = 0;
8f114acd4e1a9c Narcisa Ana Maria Vasile 2017-12-06 358 *val2 = 100;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 359 return IIO_VAL_INT_PLUS_MICRO;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 360 case IIO_MOD_VOC:
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 361 *val = 0;
8f114acd4e1a9c Narcisa Ana Maria Vasile 2017-12-06 362 *val2 = 100;
8f114acd4e1a9c Narcisa Ana Maria Vasile 2017-12-06 363 return IIO_VAL_INT_PLUS_NANO;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 364 default:
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 365 return -EINVAL;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 366 }
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 367 default:
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 368 return -EINVAL;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 369 }
1a2cae129fb93c Nikhil Gautam 2026-10-18 370 case IIO_CHAN_INFO_SAMP_FREQ:
1a2cae129fb93c Nikhil Gautam 2026-10-18 371 guard(mutex)(&data->lock);
1a2cae129fb93c Nikhil Gautam 2026-10-18 372 switch (data->drive_mode) {
1a2cae129fb93c Nikhil Gautam 2026-10-18 373 case CCS811_MODE_IAQ_1SEC:
1a2cae129fb93c Nikhil Gautam 2026-10-18 374 *val = 1;
1a2cae129fb93c Nikhil Gautam 2026-10-18 375 *val2 = 0;
1a2cae129fb93c Nikhil Gautam 2026-10-18 376 return IIO_VAL_INT_PLUS_MICRO;
1a2cae129fb93c Nikhil Gautam 2026-10-18 377 case CCS811_MODE_IAQ_10SEC:
1a2cae129fb93c Nikhil Gautam 2026-10-18 378 *val = 0;
1a2cae129fb93c Nikhil Gautam 2026-10-18 379 *val2 = 100000;
1a2cae129fb93c Nikhil Gautam 2026-10-18 380 return IIO_VAL_INT_PLUS_MICRO;
1a2cae129fb93c Nikhil Gautam 2026-10-18 381 case CCS811_MODE_IAQ_60SEC:
1a2cae129fb93c Nikhil Gautam 2026-10-18 382 *val = 0;
1a2cae129fb93c Nikhil Gautam 2026-10-18 383 *val2 = 16667;
1a2cae129fb93c Nikhil Gautam 2026-10-18 384 return IIO_VAL_INT_PLUS_MICRO;
1a2cae129fb93c Nikhil Gautam 2026-10-18 385 default:
1a2cae129fb93c Nikhil Gautam 2026-10-18 386 return -EINVAL;
1a2cae129fb93c Nikhil Gautam 2026-10-18 387 }
1a2cae129fb93c Nikhil Gautam 2026-10-18 388
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 @389 default:
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 390 return -EINVAL;
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 391 }
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 392 }
a94c24a712acb2 Narcisa Ana Maria Vasile 2017-07-17 393
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-10-19 7:26 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-18 17:00 [PATCH] iio: chemical: ccs811: Make sampling frequency configurable Nikhil Gautam
2026-10-19 7:26 ` kernel test robot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.