perf python: Do not force closing original perf descriptor in evlist.get_pollfd()
[linux/fpc-iii.git] / drivers / iio / chemical / ccs811.c
blobb4a46eb45789379d97d4c5abac562738db0e8897
1 /*
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)
14 * TODO:
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 {
68 __be16 co2;
69 __be16 voc;
70 u8 status;
71 u8 error;
72 __be16 raw_data;
73 } __attribute__((__packed__));
75 struct ccs811_data {
76 struct i2c_client *client;
77 struct mutex lock; /* Protect readings */
78 struct ccs811_reading buffer;
79 struct iio_trigger *drdy_trig;
80 bool drdy_trig_on;
83 static const struct iio_chan_spec ccs811_channels[] = {
85 .type = IIO_CURRENT,
86 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
87 BIT(IIO_CHAN_INFO_SCALE),
88 .scan_index = -1,
89 }, {
90 .type = IIO_VOLTAGE,
91 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
92 BIT(IIO_CHAN_INFO_SCALE),
93 .scan_index = -1,
94 }, {
95 .type = IIO_CONCENTRATION,
96 .channel2 = IIO_MOD_CO2,
97 .modified = 1,
98 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
99 BIT(IIO_CHAN_INFO_SCALE),
100 .scan_index = 0,
101 .scan_type = {
102 .sign = 'u',
103 .realbits = 16,
104 .storagebits = 16,
105 .endianness = IIO_BE,
107 }, {
108 .type = IIO_CONCENTRATION,
109 .channel2 = IIO_MOD_VOC,
110 .modified = 1,
111 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
112 BIT(IIO_CHAN_INFO_SCALE),
113 .scan_index = 1,
114 .scan_type = {
115 .sign = 'u',
116 .realbits = 16,
117 .storagebits = 16,
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)
130 int ret;
132 ret = i2c_smbus_read_byte_data(client, CCS811_STATUS);
133 if (ret < 0)
134 return ret;
136 if ((ret & CCS811_STATUS_FW_MODE_APPLICATION))
137 return 0;
139 if ((ret & CCS811_STATUS_APP_VALID_MASK) !=
140 CCS811_STATUS_APP_VALID_LOADED)
141 return -EIO;
143 ret = i2c_smbus_write_byte(client, CCS811_APP_START);
144 if (ret < 0)
145 return ret;
147 ret = i2c_smbus_read_byte_data(client, CCS811_STATUS);
148 if (ret < 0)
149 return ret;
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");
154 return -EIO;
157 return 0;
160 static int ccs811_setup(struct i2c_client *client)
162 int ret;
164 ret = ccs811_start_sensor_application(client);
165 if (ret < 0)
166 return ret;
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)
174 int ret, tries = 11;
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);
179 if (ret < 0)
180 return ret;
182 if ((ret & CCS811_STATUS_DATA_READY) || tries == 0)
183 break;
184 msleep(100);
186 if (!(ret & CCS811_STATUS_DATA_READY))
187 return -EIO;
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);
199 int ret;
201 switch (mask) {
202 case IIO_CHAN_INFO_RAW:
203 ret = iio_device_claim_direct_mode(indio_dev);
204 if (ret)
205 return ret;
206 mutex_lock(&data->lock);
207 ret = ccs811_get_measurement(data);
208 if (ret < 0) {
209 mutex_unlock(&data->lock);
210 iio_device_release_direct_mode(indio_dev);
211 return ret;
214 switch (chan->type) {
215 case IIO_VOLTAGE:
216 *val = be16_to_cpu(data->buffer.raw_data) &
217 CCS811_VOLTAGE_MASK;
218 ret = IIO_VAL_INT;
219 break;
220 case IIO_CURRENT:
221 *val = be16_to_cpu(data->buffer.raw_data) >> 10;
222 ret = IIO_VAL_INT;
223 break;
224 case IIO_CONCENTRATION:
225 switch (chan->channel2) {
226 case IIO_MOD_CO2:
227 *val = be16_to_cpu(data->buffer.co2);
228 ret = IIO_VAL_INT;
229 break;
230 case IIO_MOD_VOC:
231 *val = be16_to_cpu(data->buffer.voc);
232 ret = IIO_VAL_INT;
233 break;
234 default:
235 ret = -EINVAL;
237 break;
238 default:
239 ret = -EINVAL;
241 mutex_unlock(&data->lock);
242 iio_device_release_direct_mode(indio_dev);
244 return ret;
246 case IIO_CHAN_INFO_SCALE:
247 switch (chan->type) {
248 case IIO_VOLTAGE:
249 *val = 1;
250 *val2 = 612903;
251 return IIO_VAL_INT_PLUS_MICRO;
252 case IIO_CURRENT:
253 *val = 0;
254 *val2 = 1000;
255 return IIO_VAL_INT_PLUS_MICRO;
256 case IIO_CONCENTRATION:
257 switch (chan->channel2) {
258 case IIO_MOD_CO2:
259 *val = 0;
260 *val2 = 100;
261 return IIO_VAL_INT_PLUS_MICRO;
262 case IIO_MOD_VOC:
263 *val = 0;
264 *val2 = 100;
265 return IIO_VAL_INT_PLUS_NANO;
266 default:
267 return -EINVAL;
269 default:
270 return -EINVAL;
272 default:
273 return -EINVAL;
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,
282 bool state)
284 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
285 struct ccs811_data *data = iio_priv(indio_dev);
286 int ret;
288 ret = i2c_smbus_read_byte_data(data->client, CCS811_MEAS_MODE);
289 if (ret < 0)
290 return ret;
292 if (state)
293 ret |= CCS811_MEAS_MODE_INTERRUPT;
294 else
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 */
313 int ret;
315 ret = i2c_smbus_read_i2c_block_data(client, CCS811_ALG_RESULT_DATA, 4,
316 (u8 *)&buf);
317 if (ret != 4) {
318 dev_err(&client->dev, "cannot read sensor data\n");
319 goto err;
322 iio_push_to_buffers_with_timestamp(indio_dev, buf,
323 iio_get_time_ns(indio_dev));
325 err:
326 iio_trigger_notify_done(indio_dev->trig);
328 return IRQ_HANDLED;
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);
339 return IRQ_HANDLED;
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;
347 int ret;
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))
352 return -EOPNOTSUPP;
354 /* Check hardware id (should be 0x81 for this family of devices) */
355 ret = i2c_smbus_read_byte_data(client, CCS811_HW_ID);
356 if (ret < 0)
357 return ret;
359 if (ret != CCS811_HW_ID_VALUE) {
360 dev_err(&client->dev, "hardware id doesn't match CCS81x\n");
361 return -ENODEV;
364 ret = i2c_smbus_read_byte_data(client, CCS811_HW_VERSION);
365 if (ret < 0)
366 return ret;
368 if ((ret & CCS811_HW_VERSION_MASK) != CCS811_HW_VERSION_VALUE) {
369 dev_err(&client->dev, "no CCS811 sensor\n");
370 return -ENODEV;
373 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
374 if (!indio_dev)
375 return -ENOMEM;
377 ret = ccs811_setup(client);
378 if (ret < 0)
379 return ret;
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,
398 NULL,
399 IRQF_TRIGGER_FALLING |
400 IRQF_ONESHOT,
401 "ccs811_irq", indio_dev);
402 if (ret) {
403 dev_err(&client->dev, "irq request error %d\n", -ret);
404 goto err_poweroff;
407 data->drdy_trig = devm_iio_trigger_alloc(&client->dev,
408 "%s-dev%d",
409 indio_dev->name,
410 indio_dev->id);
411 if (!data->drdy_trig) {
412 ret = -ENOMEM;
413 goto err_poweroff;
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);
422 if (ret)
423 goto err_poweroff;
426 ret = iio_triggered_buffer_setup(indio_dev, NULL,
427 ccs811_trigger_handler, NULL);
429 if (ret < 0) {
430 dev_err(&client->dev, "triggered buffer setup failed\n");
431 goto err_trigger_unregister;
434 ret = iio_device_register(indio_dev);
435 if (ret < 0) {
436 dev_err(&client->dev, "unable to register iio device\n");
437 goto err_buffer_cleanup;
439 return 0;
441 err_buffer_cleanup:
442 iio_triggered_buffer_cleanup(indio_dev);
443 err_trigger_unregister:
444 if (data->drdy_trig)
445 iio_trigger_unregister(data->drdy_trig);
446 err_poweroff:
447 i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE, CCS811_MODE_IDLE);
449 return ret;
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);
459 if (data->drdy_trig)
460 iio_trigger_unregister(data->drdy_trig);
462 return i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE,
463 CCS811_MODE_IDLE);
466 static const struct i2c_device_id ccs811_id[] = {
467 {"ccs811", 0},
470 MODULE_DEVICE_TABLE(i2c, ccs811_id);
472 static struct i2c_driver ccs811_driver = {
473 .driver = {
474 .name = "ccs811",
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");