2 * ccs811.c - Support for AMS CCS811 VOC Sensor
4 * Copyright (C) 2017 Narcisa Vasile <narcisaanamaria12@gmail.com>
6 * Datasheet: ams.com/content/download/951091/2269479/CCS811_DS000459_3-00.pdf
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * IIO driver for AMS CCS811 (I2C address 0x5A/0x5B set by ADDR Low/High)
15 * 1. Make the drive mode selectable form userspace
16 * 2. Add support for interrupts
17 * 3. Adjust time to wait for data to be ready based on selected operation mode
18 * 4. Read error register and put the information in logs
21 #include <linux/delay.h>
22 #include <linux/i2c.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/trigger.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/iio/trigger_consumer.h>
28 #include <linux/module.h>
30 #define CCS811_STATUS 0x00
31 #define CCS811_MEAS_MODE 0x01
32 #define CCS811_ALG_RESULT_DATA 0x02
33 #define CCS811_RAW_DATA 0x03
34 #define CCS811_HW_ID 0x20
35 #define CCS811_HW_ID_VALUE 0x81
36 #define CCS811_HW_VERSION 0x21
37 #define CCS811_HW_VERSION_VALUE 0x10
38 #define CCS811_HW_VERSION_MASK 0xF0
39 #define CCS811_ERR 0xE0
40 /* Used to transition from boot to application mode */
41 #define CCS811_APP_START 0xF4
43 /* Status register flags */
44 #define CCS811_STATUS_ERROR BIT(0)
45 #define CCS811_STATUS_DATA_READY BIT(3)
46 #define CCS811_STATUS_APP_VALID_MASK BIT(4)
47 #define CCS811_STATUS_APP_VALID_LOADED BIT(4)
49 * Value of FW_MODE bit of STATUS register describes the sensor's state:
50 * 0: Firmware is in boot mode, this allows new firmware to be loaded
51 * 1: Firmware is in application mode. CCS811 is ready to take ADC measurements
53 #define CCS811_STATUS_FW_MODE_MASK BIT(7)
54 #define CCS811_STATUS_FW_MODE_APPLICATION BIT(7)
56 /* Measurement modes */
57 #define CCS811_MODE_IDLE 0x00
58 #define CCS811_MODE_IAQ_1SEC 0x10
59 #define CCS811_MODE_IAQ_10SEC 0x20
60 #define CCS811_MODE_IAQ_60SEC 0x30
61 #define CCS811_MODE_RAW_DATA 0x40
63 #define CCS811_MEAS_MODE_INTERRUPT BIT(3)
65 #define CCS811_VOLTAGE_MASK 0x3FF
67 struct ccs811_reading
{
73 } __attribute__((__packed__
));
76 struct i2c_client
*client
;
77 struct mutex lock
; /* Protect readings */
78 struct ccs811_reading buffer
;
79 struct iio_trigger
*drdy_trig
;
83 static const struct iio_chan_spec ccs811_channels
[] = {
86 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
87 BIT(IIO_CHAN_INFO_SCALE
),
91 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
92 BIT(IIO_CHAN_INFO_SCALE
),
95 .type
= IIO_CONCENTRATION
,
96 .channel2
= IIO_MOD_CO2
,
98 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
99 BIT(IIO_CHAN_INFO_SCALE
),
105 .endianness
= IIO_BE
,
108 .type
= IIO_CONCENTRATION
,
109 .channel2
= IIO_MOD_VOC
,
111 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
112 BIT(IIO_CHAN_INFO_SCALE
),
118 .endianness
= IIO_BE
,
121 IIO_CHAN_SOFT_TIMESTAMP(2),
125 * The CCS811 powers-up in boot mode. A setup write to CCS811_APP_START will
126 * transition the sensor to application mode.
128 static int ccs811_start_sensor_application(struct i2c_client
*client
)
132 ret
= i2c_smbus_read_byte_data(client
, CCS811_STATUS
);
136 if ((ret
& CCS811_STATUS_FW_MODE_APPLICATION
))
139 if ((ret
& CCS811_STATUS_APP_VALID_MASK
) !=
140 CCS811_STATUS_APP_VALID_LOADED
)
143 ret
= i2c_smbus_write_byte(client
, CCS811_APP_START
);
147 ret
= i2c_smbus_read_byte_data(client
, CCS811_STATUS
);
151 if ((ret
& CCS811_STATUS_FW_MODE_MASK
) !=
152 CCS811_STATUS_FW_MODE_APPLICATION
) {
153 dev_err(&client
->dev
, "Application failed to start. Sensor is still in boot mode.\n");
160 static int ccs811_setup(struct i2c_client
*client
)
164 ret
= ccs811_start_sensor_application(client
);
168 return i2c_smbus_write_byte_data(client
, CCS811_MEAS_MODE
,
169 CCS811_MODE_IAQ_1SEC
);
172 static int ccs811_get_measurement(struct ccs811_data
*data
)
176 /* Maximum waiting time: 1s, as measurements are made every second */
177 while (tries
-- > 0) {
178 ret
= i2c_smbus_read_byte_data(data
->client
, CCS811_STATUS
);
182 if ((ret
& CCS811_STATUS_DATA_READY
) || tries
== 0)
186 if (!(ret
& CCS811_STATUS_DATA_READY
))
189 return i2c_smbus_read_i2c_block_data(data
->client
,
190 CCS811_ALG_RESULT_DATA
, 8,
191 (char *)&data
->buffer
);
194 static int ccs811_read_raw(struct iio_dev
*indio_dev
,
195 struct iio_chan_spec
const *chan
,
196 int *val
, int *val2
, long mask
)
198 struct ccs811_data
*data
= iio_priv(indio_dev
);
202 case IIO_CHAN_INFO_RAW
:
203 ret
= iio_device_claim_direct_mode(indio_dev
);
206 mutex_lock(&data
->lock
);
207 ret
= ccs811_get_measurement(data
);
209 mutex_unlock(&data
->lock
);
210 iio_device_release_direct_mode(indio_dev
);
214 switch (chan
->type
) {
216 *val
= be16_to_cpu(data
->buffer
.raw_data
) &
221 *val
= be16_to_cpu(data
->buffer
.raw_data
) >> 10;
224 case IIO_CONCENTRATION
:
225 switch (chan
->channel2
) {
227 *val
= be16_to_cpu(data
->buffer
.co2
);
231 *val
= be16_to_cpu(data
->buffer
.voc
);
241 mutex_unlock(&data
->lock
);
242 iio_device_release_direct_mode(indio_dev
);
246 case IIO_CHAN_INFO_SCALE
:
247 switch (chan
->type
) {
251 return IIO_VAL_INT_PLUS_MICRO
;
255 return IIO_VAL_INT_PLUS_MICRO
;
256 case IIO_CONCENTRATION
:
257 switch (chan
->channel2
) {
261 return IIO_VAL_INT_PLUS_MICRO
;
265 return IIO_VAL_INT_PLUS_NANO
;
277 static const struct iio_info ccs811_info
= {
278 .read_raw
= ccs811_read_raw
,
281 static int ccs811_set_trigger_state(struct iio_trigger
*trig
,
284 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
285 struct ccs811_data
*data
= iio_priv(indio_dev
);
288 ret
= i2c_smbus_read_byte_data(data
->client
, CCS811_MEAS_MODE
);
293 ret
|= CCS811_MEAS_MODE_INTERRUPT
;
295 ret
&= ~CCS811_MEAS_MODE_INTERRUPT
;
297 data
->drdy_trig_on
= state
;
299 return i2c_smbus_write_byte_data(data
->client
, CCS811_MEAS_MODE
, ret
);
302 static const struct iio_trigger_ops ccs811_trigger_ops
= {
303 .set_trigger_state
= ccs811_set_trigger_state
,
306 static irqreturn_t
ccs811_trigger_handler(int irq
, void *p
)
308 struct iio_poll_func
*pf
= p
;
309 struct iio_dev
*indio_dev
= pf
->indio_dev
;
310 struct ccs811_data
*data
= iio_priv(indio_dev
);
311 struct i2c_client
*client
= data
->client
;
312 s16 buf
[8]; /* s16 eCO2 + s16 TVOC + padding + 8 byte timestamp */
315 ret
= i2c_smbus_read_i2c_block_data(client
, CCS811_ALG_RESULT_DATA
, 4,
318 dev_err(&client
->dev
, "cannot read sensor data\n");
322 iio_push_to_buffers_with_timestamp(indio_dev
, buf
,
323 iio_get_time_ns(indio_dev
));
326 iio_trigger_notify_done(indio_dev
->trig
);
331 static irqreturn_t
ccs811_data_rdy_trigger_poll(int irq
, void *private)
333 struct iio_dev
*indio_dev
= private;
334 struct ccs811_data
*data
= iio_priv(indio_dev
);
336 if (data
->drdy_trig_on
)
337 iio_trigger_poll(data
->drdy_trig
);
342 static int ccs811_probe(struct i2c_client
*client
,
343 const struct i2c_device_id
*id
)
345 struct iio_dev
*indio_dev
;
346 struct ccs811_data
*data
;
349 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WRITE_BYTE
350 | I2C_FUNC_SMBUS_BYTE_DATA
351 | I2C_FUNC_SMBUS_READ_I2C_BLOCK
))
354 /* Check hardware id (should be 0x81 for this family of devices) */
355 ret
= i2c_smbus_read_byte_data(client
, CCS811_HW_ID
);
359 if (ret
!= CCS811_HW_ID_VALUE
) {
360 dev_err(&client
->dev
, "hardware id doesn't match CCS81x\n");
364 ret
= i2c_smbus_read_byte_data(client
, CCS811_HW_VERSION
);
368 if ((ret
& CCS811_HW_VERSION_MASK
) != CCS811_HW_VERSION_VALUE
) {
369 dev_err(&client
->dev
, "no CCS811 sensor\n");
373 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
377 ret
= ccs811_setup(client
);
381 data
= iio_priv(indio_dev
);
382 i2c_set_clientdata(client
, indio_dev
);
383 data
->client
= client
;
385 mutex_init(&data
->lock
);
387 indio_dev
->dev
.parent
= &client
->dev
;
388 indio_dev
->name
= id
->name
;
389 indio_dev
->info
= &ccs811_info
;
390 indio_dev
->modes
= INDIO_DIRECT_MODE
;
392 indio_dev
->channels
= ccs811_channels
;
393 indio_dev
->num_channels
= ARRAY_SIZE(ccs811_channels
);
395 if (client
->irq
> 0) {
396 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
397 ccs811_data_rdy_trigger_poll
,
399 IRQF_TRIGGER_FALLING
|
401 "ccs811_irq", indio_dev
);
403 dev_err(&client
->dev
, "irq request error %d\n", -ret
);
407 data
->drdy_trig
= devm_iio_trigger_alloc(&client
->dev
,
411 if (!data
->drdy_trig
) {
416 data
->drdy_trig
->dev
.parent
= &client
->dev
;
417 data
->drdy_trig
->ops
= &ccs811_trigger_ops
;
418 iio_trigger_set_drvdata(data
->drdy_trig
, indio_dev
);
419 indio_dev
->trig
= data
->drdy_trig
;
420 iio_trigger_get(indio_dev
->trig
);
421 ret
= iio_trigger_register(data
->drdy_trig
);
426 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
427 ccs811_trigger_handler
, NULL
);
430 dev_err(&client
->dev
, "triggered buffer setup failed\n");
431 goto err_trigger_unregister
;
434 ret
= iio_device_register(indio_dev
);
436 dev_err(&client
->dev
, "unable to register iio device\n");
437 goto err_buffer_cleanup
;
442 iio_triggered_buffer_cleanup(indio_dev
);
443 err_trigger_unregister
:
445 iio_trigger_unregister(data
->drdy_trig
);
447 i2c_smbus_write_byte_data(client
, CCS811_MEAS_MODE
, CCS811_MODE_IDLE
);
452 static int ccs811_remove(struct i2c_client
*client
)
454 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
455 struct ccs811_data
*data
= iio_priv(indio_dev
);
457 iio_device_unregister(indio_dev
);
458 iio_triggered_buffer_cleanup(indio_dev
);
460 iio_trigger_unregister(data
->drdy_trig
);
462 return i2c_smbus_write_byte_data(client
, CCS811_MEAS_MODE
,
466 static const struct i2c_device_id ccs811_id
[] = {
470 MODULE_DEVICE_TABLE(i2c
, ccs811_id
);
472 static struct i2c_driver ccs811_driver
= {
476 .probe
= ccs811_probe
,
477 .remove
= ccs811_remove
,
478 .id_table
= ccs811_id
,
480 module_i2c_driver(ccs811_driver
);
482 MODULE_AUTHOR("Narcisa Vasile <narcisaanamaria12@gmail.com>");
483 MODULE_DESCRIPTION("CCS811 volatile organic compounds sensor");
484 MODULE_LICENSE("GPL v2");