1 // SPDX-License-Identifier: GPL-2.0-only
3 * ccs811.c - Support for AMS CCS811 VOC Sensor
5 * Copyright (C) 2017 Narcisa Vasile <narcisaanamaria12@gmail.com>
7 * Datasheet: ams.com/content/download/951091/2269479/CCS811_DS000459_3-00.pdf
9 * IIO driver for AMS CCS811 (I2C address 0x5A/0x5B set by ADDR Low/High)
12 * 1. Make the drive mode selectable form userspace
13 * 2. Add support for interrupts
14 * 3. Adjust time to wait for data to be ready based on selected operation mode
15 * 4. Read error register and put the information in logs
18 #include <linux/delay.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/i2c.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/triggered_buffer.h>
25 #include <linux/iio/trigger_consumer.h>
26 #include <linux/module.h>
28 #define CCS811_STATUS 0x00
29 #define CCS811_MEAS_MODE 0x01
30 #define CCS811_ALG_RESULT_DATA 0x02
31 #define CCS811_RAW_DATA 0x03
32 #define CCS811_HW_ID 0x20
33 #define CCS811_HW_ID_VALUE 0x81
34 #define CCS811_HW_VERSION 0x21
35 #define CCS811_HW_VERSION_VALUE 0x10
36 #define CCS811_HW_VERSION_MASK 0xF0
37 #define CCS811_ERR 0xE0
38 /* Used to transition from boot to application mode */
39 #define CCS811_APP_START 0xF4
40 #define CCS811_SW_RESET 0xFF
42 /* Status register flags */
43 #define CCS811_STATUS_ERROR BIT(0)
44 #define CCS811_STATUS_DATA_READY BIT(3)
45 #define CCS811_STATUS_APP_VALID_MASK BIT(4)
46 #define CCS811_STATUS_APP_VALID_LOADED BIT(4)
48 * Value of FW_MODE bit of STATUS register describes the sensor's state:
49 * 0: Firmware is in boot mode, this allows new firmware to be loaded
50 * 1: Firmware is in application mode. CCS811 is ready to take ADC measurements
52 #define CCS811_STATUS_FW_MODE_MASK BIT(7)
53 #define CCS811_STATUS_FW_MODE_APPLICATION BIT(7)
55 /* Measurement modes */
56 #define CCS811_MODE_IDLE 0x00
57 #define CCS811_MODE_IAQ_1SEC 0x10
58 #define CCS811_MODE_IAQ_10SEC 0x20
59 #define CCS811_MODE_IAQ_60SEC 0x30
60 #define CCS811_MODE_RAW_DATA 0x40
62 #define CCS811_MEAS_MODE_INTERRUPT BIT(3)
64 #define CCS811_VOLTAGE_MASK 0x3FF
66 struct ccs811_reading
{
72 } __attribute__((__packed__
));
75 struct i2c_client
*client
;
76 struct mutex lock
; /* Protect readings */
77 struct ccs811_reading buffer
;
78 struct iio_trigger
*drdy_trig
;
79 struct gpio_desc
*wakeup_gpio
;
81 /* Ensures correct alignment of timestamp if present */
88 static const struct iio_chan_spec ccs811_channels
[] = {
91 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
92 BIT(IIO_CHAN_INFO_SCALE
),
96 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
97 BIT(IIO_CHAN_INFO_SCALE
),
100 .type
= IIO_CONCENTRATION
,
101 .channel2
= IIO_MOD_CO2
,
103 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
104 BIT(IIO_CHAN_INFO_SCALE
),
110 .endianness
= IIO_BE
,
113 .type
= IIO_CONCENTRATION
,
114 .channel2
= IIO_MOD_VOC
,
116 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
117 BIT(IIO_CHAN_INFO_SCALE
),
123 .endianness
= IIO_BE
,
126 IIO_CHAN_SOFT_TIMESTAMP(2),
130 * The CCS811 powers-up in boot mode. A setup write to CCS811_APP_START will
131 * transition the sensor to application mode.
133 static int ccs811_start_sensor_application(struct i2c_client
*client
)
137 ret
= i2c_smbus_read_byte_data(client
, CCS811_STATUS
);
141 if ((ret
& CCS811_STATUS_FW_MODE_APPLICATION
))
144 if ((ret
& CCS811_STATUS_APP_VALID_MASK
) !=
145 CCS811_STATUS_APP_VALID_LOADED
)
148 ret
= i2c_smbus_write_byte(client
, CCS811_APP_START
);
152 ret
= i2c_smbus_read_byte_data(client
, CCS811_STATUS
);
156 if ((ret
& CCS811_STATUS_FW_MODE_MASK
) !=
157 CCS811_STATUS_FW_MODE_APPLICATION
) {
158 dev_err(&client
->dev
, "Application failed to start. Sensor is still in boot mode.\n");
165 static int ccs811_setup(struct i2c_client
*client
)
169 ret
= ccs811_start_sensor_application(client
);
173 return i2c_smbus_write_byte_data(client
, CCS811_MEAS_MODE
,
174 CCS811_MODE_IAQ_1SEC
);
177 static void ccs811_set_wakeup(struct ccs811_data
*data
, bool enable
)
179 if (!data
->wakeup_gpio
)
182 gpiod_set_value(data
->wakeup_gpio
, enable
);
185 usleep_range(50, 60);
187 usleep_range(20, 30);
190 static int ccs811_get_measurement(struct ccs811_data
*data
)
194 ccs811_set_wakeup(data
, true);
196 /* Maximum waiting time: 1s, as measurements are made every second */
197 while (tries
-- > 0) {
198 ret
= i2c_smbus_read_byte_data(data
->client
, CCS811_STATUS
);
202 if ((ret
& CCS811_STATUS_DATA_READY
) || tries
== 0)
206 if (!(ret
& CCS811_STATUS_DATA_READY
))
209 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
210 CCS811_ALG_RESULT_DATA
, 8,
211 (char *)&data
->buffer
);
212 ccs811_set_wakeup(data
, false);
217 static int ccs811_read_raw(struct iio_dev
*indio_dev
,
218 struct iio_chan_spec
const *chan
,
219 int *val
, int *val2
, long mask
)
221 struct ccs811_data
*data
= iio_priv(indio_dev
);
225 case IIO_CHAN_INFO_RAW
:
226 ret
= iio_device_claim_direct_mode(indio_dev
);
229 mutex_lock(&data
->lock
);
230 ret
= ccs811_get_measurement(data
);
232 mutex_unlock(&data
->lock
);
233 iio_device_release_direct_mode(indio_dev
);
237 switch (chan
->type
) {
239 *val
= be16_to_cpu(data
->buffer
.raw_data
) &
244 *val
= be16_to_cpu(data
->buffer
.raw_data
) >> 10;
247 case IIO_CONCENTRATION
:
248 switch (chan
->channel2
) {
250 *val
= be16_to_cpu(data
->buffer
.co2
);
254 *val
= be16_to_cpu(data
->buffer
.voc
);
264 mutex_unlock(&data
->lock
);
265 iio_device_release_direct_mode(indio_dev
);
269 case IIO_CHAN_INFO_SCALE
:
270 switch (chan
->type
) {
274 return IIO_VAL_INT_PLUS_MICRO
;
278 return IIO_VAL_INT_PLUS_MICRO
;
279 case IIO_CONCENTRATION
:
280 switch (chan
->channel2
) {
284 return IIO_VAL_INT_PLUS_MICRO
;
288 return IIO_VAL_INT_PLUS_NANO
;
300 static const struct iio_info ccs811_info
= {
301 .read_raw
= ccs811_read_raw
,
304 static int ccs811_set_trigger_state(struct iio_trigger
*trig
,
307 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
308 struct ccs811_data
*data
= iio_priv(indio_dev
);
311 ret
= i2c_smbus_read_byte_data(data
->client
, CCS811_MEAS_MODE
);
316 ret
|= CCS811_MEAS_MODE_INTERRUPT
;
318 ret
&= ~CCS811_MEAS_MODE_INTERRUPT
;
320 data
->drdy_trig_on
= state
;
322 return i2c_smbus_write_byte_data(data
->client
, CCS811_MEAS_MODE
, ret
);
325 static const struct iio_trigger_ops ccs811_trigger_ops
= {
326 .set_trigger_state
= ccs811_set_trigger_state
,
329 static irqreturn_t
ccs811_trigger_handler(int irq
, void *p
)
331 struct iio_poll_func
*pf
= p
;
332 struct iio_dev
*indio_dev
= pf
->indio_dev
;
333 struct ccs811_data
*data
= iio_priv(indio_dev
);
334 struct i2c_client
*client
= data
->client
;
337 ret
= i2c_smbus_read_i2c_block_data(client
, CCS811_ALG_RESULT_DATA
,
338 sizeof(data
->scan
.channels
),
339 (u8
*)data
->scan
.channels
);
341 dev_err(&client
->dev
, "cannot read sensor data\n");
345 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
346 iio_get_time_ns(indio_dev
));
349 iio_trigger_notify_done(indio_dev
->trig
);
354 static irqreturn_t
ccs811_data_rdy_trigger_poll(int irq
, void *private)
356 struct iio_dev
*indio_dev
= private;
357 struct ccs811_data
*data
= iio_priv(indio_dev
);
359 if (data
->drdy_trig_on
)
360 iio_trigger_poll(data
->drdy_trig
);
365 static int ccs811_reset(struct i2c_client
*client
)
367 struct gpio_desc
*reset_gpio
;
370 reset_gpio
= devm_gpiod_get_optional(&client
->dev
, "reset",
372 if (IS_ERR(reset_gpio
))
373 return PTR_ERR(reset_gpio
);
375 /* Try to reset using nRESET pin if available else do SW reset */
377 gpiod_set_value(reset_gpio
, 1);
378 usleep_range(20, 30);
379 gpiod_set_value(reset_gpio
, 0);
382 * As per the datasheet, this sequence of values needs to be
383 * written to the SW_RESET register for triggering the soft
384 * reset in the device and placing it in boot mode.
386 static const u8 reset_seq
[] = {
387 0x11, 0xE5, 0x72, 0x8A,
390 ret
= i2c_smbus_write_i2c_block_data(client
, CCS811_SW_RESET
,
391 sizeof(reset_seq
), reset_seq
);
393 dev_err(&client
->dev
, "Failed to reset sensor\n");
398 /* tSTART delay required after reset */
399 usleep_range(1000, 2000);
404 static int ccs811_probe(struct i2c_client
*client
,
405 const struct i2c_device_id
*id
)
407 struct iio_dev
*indio_dev
;
408 struct ccs811_data
*data
;
411 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WRITE_BYTE
412 | I2C_FUNC_SMBUS_BYTE_DATA
413 | I2C_FUNC_SMBUS_READ_I2C_BLOCK
))
416 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
420 data
= iio_priv(indio_dev
);
421 i2c_set_clientdata(client
, indio_dev
);
422 data
->client
= client
;
424 data
->wakeup_gpio
= devm_gpiod_get_optional(&client
->dev
, "wakeup",
426 if (IS_ERR(data
->wakeup_gpio
))
427 return PTR_ERR(data
->wakeup_gpio
);
429 ccs811_set_wakeup(data
, true);
431 ret
= ccs811_reset(client
);
433 ccs811_set_wakeup(data
, false);
437 /* Check hardware id (should be 0x81 for this family of devices) */
438 ret
= i2c_smbus_read_byte_data(client
, CCS811_HW_ID
);
440 ccs811_set_wakeup(data
, false);
444 if (ret
!= CCS811_HW_ID_VALUE
) {
445 dev_err(&client
->dev
, "hardware id doesn't match CCS81x\n");
446 ccs811_set_wakeup(data
, false);
450 ret
= i2c_smbus_read_byte_data(client
, CCS811_HW_VERSION
);
452 ccs811_set_wakeup(data
, false);
456 if ((ret
& CCS811_HW_VERSION_MASK
) != CCS811_HW_VERSION_VALUE
) {
457 dev_err(&client
->dev
, "no CCS811 sensor\n");
458 ccs811_set_wakeup(data
, false);
462 ret
= ccs811_setup(client
);
464 ccs811_set_wakeup(data
, false);
468 ccs811_set_wakeup(data
, false);
470 mutex_init(&data
->lock
);
472 indio_dev
->name
= id
->name
;
473 indio_dev
->info
= &ccs811_info
;
474 indio_dev
->modes
= INDIO_DIRECT_MODE
;
476 indio_dev
->channels
= ccs811_channels
;
477 indio_dev
->num_channels
= ARRAY_SIZE(ccs811_channels
);
479 if (client
->irq
> 0) {
480 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
481 ccs811_data_rdy_trigger_poll
,
483 IRQF_TRIGGER_FALLING
|
485 "ccs811_irq", indio_dev
);
487 dev_err(&client
->dev
, "irq request error %d\n", -ret
);
491 data
->drdy_trig
= devm_iio_trigger_alloc(&client
->dev
,
495 if (!data
->drdy_trig
) {
500 data
->drdy_trig
->dev
.parent
= &client
->dev
;
501 data
->drdy_trig
->ops
= &ccs811_trigger_ops
;
502 iio_trigger_set_drvdata(data
->drdy_trig
, indio_dev
);
503 indio_dev
->trig
= data
->drdy_trig
;
504 iio_trigger_get(indio_dev
->trig
);
505 ret
= iio_trigger_register(data
->drdy_trig
);
510 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
511 ccs811_trigger_handler
, NULL
);
514 dev_err(&client
->dev
, "triggered buffer setup failed\n");
515 goto err_trigger_unregister
;
518 ret
= iio_device_register(indio_dev
);
520 dev_err(&client
->dev
, "unable to register iio device\n");
521 goto err_buffer_cleanup
;
526 iio_triggered_buffer_cleanup(indio_dev
);
527 err_trigger_unregister
:
529 iio_trigger_unregister(data
->drdy_trig
);
531 i2c_smbus_write_byte_data(client
, CCS811_MEAS_MODE
, CCS811_MODE_IDLE
);
536 static int ccs811_remove(struct i2c_client
*client
)
538 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
539 struct ccs811_data
*data
= iio_priv(indio_dev
);
541 iio_device_unregister(indio_dev
);
542 iio_triggered_buffer_cleanup(indio_dev
);
544 iio_trigger_unregister(data
->drdy_trig
);
546 return i2c_smbus_write_byte_data(client
, CCS811_MEAS_MODE
,
550 static const struct i2c_device_id ccs811_id
[] = {
554 MODULE_DEVICE_TABLE(i2c
, ccs811_id
);
556 static const struct of_device_id ccs811_dt_ids
[] = {
557 { .compatible
= "ams,ccs811" },
560 MODULE_DEVICE_TABLE(of
, ccs811_dt_ids
);
562 static struct i2c_driver ccs811_driver
= {
565 .of_match_table
= ccs811_dt_ids
,
567 .probe
= ccs811_probe
,
568 .remove
= ccs811_remove
,
569 .id_table
= ccs811_id
,
571 module_i2c_driver(ccs811_driver
);
573 MODULE_AUTHOR("Narcisa Vasile <narcisaanamaria12@gmail.com>");
574 MODULE_DESCRIPTION("CCS811 volatile organic compounds sensor");
575 MODULE_LICENSE("GPL v2");