xref: /openbmc/linux/drivers/iio/chemical/ccs811.c (revision 0c874100)
1 /*
2  * ccs811.c - Support for AMS CCS811 VOC Sensor
3  *
4  * Copyright (C) 2017 Narcisa Vasile <narcisaanamaria12@gmail.com>
5  *
6  * Datasheet: ams.com/content/download/951091/2269479/CCS811_DS000459_3-00.pdf
7  *
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.
11  *
12  * IIO driver for AMS CCS811 (I2C address 0x5A/0x5B set by ADDR Low/High)
13  *
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
19  */
20 
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>
29 
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
42 
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)
48 /*
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
52  */
53 #define CCS811_STATUS_FW_MODE_MASK	BIT(7)
54 #define CCS811_STATUS_FW_MODE_APPLICATION	BIT(7)
55 
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
62 
63 #define CCS811_MEAS_MODE_INTERRUPT	BIT(3)
64 
65 #define CCS811_VOLTAGE_MASK	0x3FF
66 
67 struct ccs811_reading {
68 	__be16 co2;
69 	__be16 voc;
70 	u8 status;
71 	u8 error;
72 	__be16 raw_data;
73 } __attribute__((__packed__));
74 
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;
81 };
82 
83 static const struct iio_chan_spec ccs811_channels[] = {
84 	{
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,
106 		},
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,
119 		},
120 	},
121 	IIO_CHAN_SOFT_TIMESTAMP(2),
122 };
123 
124 /*
125  * The CCS811 powers-up in boot mode. A setup write to CCS811_APP_START will
126  * transition the sensor to application mode.
127  */
128 static int ccs811_start_sensor_application(struct i2c_client *client)
129 {
130 	int ret;
131 
132 	ret = i2c_smbus_read_byte_data(client, CCS811_STATUS);
133 	if (ret < 0)
134 		return ret;
135 
136 	if ((ret & CCS811_STATUS_FW_MODE_APPLICATION))
137 		return 0;
138 
139 	if ((ret & CCS811_STATUS_APP_VALID_MASK) !=
140 	    CCS811_STATUS_APP_VALID_LOADED)
141 		return -EIO;
142 
143 	ret = i2c_smbus_write_byte(client, CCS811_APP_START);
144 	if (ret < 0)
145 		return ret;
146 
147 	ret = i2c_smbus_read_byte_data(client, CCS811_STATUS);
148 	if (ret < 0)
149 		return ret;
150 
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;
155 	}
156 
157 	return 0;
158 }
159 
160 static int ccs811_setup(struct i2c_client *client)
161 {
162 	int ret;
163 
164 	ret = ccs811_start_sensor_application(client);
165 	if (ret < 0)
166 		return ret;
167 
168 	return i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE,
169 					 CCS811_MODE_IAQ_1SEC);
170 }
171 
172 static int ccs811_get_measurement(struct ccs811_data *data)
173 {
174 	int ret, tries = 11;
175 
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;
181 
182 		if ((ret & CCS811_STATUS_DATA_READY) || tries == 0)
183 			break;
184 		msleep(100);
185 	}
186 	if (!(ret & CCS811_STATUS_DATA_READY))
187 		return -EIO;
188 
189 	return i2c_smbus_read_i2c_block_data(data->client,
190 					    CCS811_ALG_RESULT_DATA, 8,
191 					    (char *)&data->buffer);
192 }
193 
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)
197 {
198 	struct ccs811_data *data = iio_priv(indio_dev);
199 	int ret;
200 
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;
212 		}
213 
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;
236 			}
237 			break;
238 		default:
239 			ret = -EINVAL;
240 		}
241 		mutex_unlock(&data->lock);
242 		iio_device_release_direct_mode(indio_dev);
243 
244 		return ret;
245 
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;
268 			}
269 		default:
270 			return -EINVAL;
271 		}
272 	default:
273 		return -EINVAL;
274 	}
275 }
276 
277 static const struct iio_info ccs811_info = {
278 	.read_raw = ccs811_read_raw,
279 };
280 
281 static int ccs811_set_trigger_state(struct iio_trigger *trig,
282 				    bool state)
283 {
284 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
285 	struct ccs811_data *data = iio_priv(indio_dev);
286 	int ret;
287 
288 	ret = i2c_smbus_read_byte_data(data->client, CCS811_MEAS_MODE);
289 	if (ret < 0)
290 		return ret;
291 
292 	if (state)
293 		ret |= CCS811_MEAS_MODE_INTERRUPT;
294 	else
295 		ret &= ~CCS811_MEAS_MODE_INTERRUPT;
296 
297 	data->drdy_trig_on = state;
298 
299 	return i2c_smbus_write_byte_data(data->client, CCS811_MEAS_MODE, ret);
300 }
301 
302 static const struct iio_trigger_ops ccs811_trigger_ops = {
303 	.set_trigger_state = ccs811_set_trigger_state,
304 };
305 
306 static irqreturn_t ccs811_trigger_handler(int irq, void *p)
307 {
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;
314 
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;
320 	}
321 
322 	iio_push_to_buffers_with_timestamp(indio_dev, buf,
323 					   iio_get_time_ns(indio_dev));
324 
325 err:
326 	iio_trigger_notify_done(indio_dev->trig);
327 
328 	return IRQ_HANDLED;
329 }
330 
331 static irqreturn_t ccs811_data_rdy_trigger_poll(int irq, void *private)
332 {
333 	struct iio_dev *indio_dev = private;
334 	struct ccs811_data *data = iio_priv(indio_dev);
335 
336 	if (data->drdy_trig_on)
337 		iio_trigger_poll(data->drdy_trig);
338 
339 	return IRQ_HANDLED;
340 }
341 
342 static int ccs811_probe(struct i2c_client *client,
343 			const struct i2c_device_id *id)
344 {
345 	struct iio_dev *indio_dev;
346 	struct ccs811_data *data;
347 	int ret;
348 
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;
353 
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;
358 
359 	if (ret != CCS811_HW_ID_VALUE) {
360 		dev_err(&client->dev, "hardware id doesn't match CCS81x\n");
361 		return -ENODEV;
362 	}
363 
364 	ret = i2c_smbus_read_byte_data(client, CCS811_HW_VERSION);
365 	if (ret < 0)
366 		return ret;
367 
368 	if ((ret & CCS811_HW_VERSION_MASK) != CCS811_HW_VERSION_VALUE) {
369 		dev_err(&client->dev, "no CCS811 sensor\n");
370 		return -ENODEV;
371 	}
372 
373 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
374 	if (!indio_dev)
375 		return -ENOMEM;
376 
377 	ret = ccs811_setup(client);
378 	if (ret < 0)
379 		return ret;
380 
381 	data = iio_priv(indio_dev);
382 	i2c_set_clientdata(client, indio_dev);
383 	data->client = client;
384 
385 	mutex_init(&data->lock);
386 
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;
391 
392 	indio_dev->channels = ccs811_channels;
393 	indio_dev->num_channels = ARRAY_SIZE(ccs811_channels);
394 
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;
405 		}
406 
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;
414 		}
415 
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;
424 	}
425 
426 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
427 					 ccs811_trigger_handler, NULL);
428 
429 	if (ret < 0) {
430 		dev_err(&client->dev, "triggered buffer setup failed\n");
431 		goto err_trigger_unregister;
432 	}
433 
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;
438 	}
439 	return 0;
440 
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);
448 
449 	return ret;
450 }
451 
452 static int ccs811_remove(struct i2c_client *client)
453 {
454 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
455 	struct ccs811_data *data = iio_priv(indio_dev);
456 
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);
461 
462 	return i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE,
463 					 CCS811_MODE_IDLE);
464 }
465 
466 static const struct i2c_device_id ccs811_id[] = {
467 	{"ccs811", 0},
468 	{	}
469 };
470 MODULE_DEVICE_TABLE(i2c, ccs811_id);
471 
472 static struct i2c_driver ccs811_driver = {
473 	.driver = {
474 		.name = "ccs811",
475 	},
476 	.probe = ccs811_probe,
477 	.remove = ccs811_remove,
478 	.id_table = ccs811_id,
479 };
480 module_i2c_driver(ccs811_driver);
481 
482 MODULE_AUTHOR("Narcisa Vasile <narcisaanamaria12@gmail.com>");
483 MODULE_DESCRIPTION("CCS811 volatile organic compounds sensor");
484 MODULE_LICENSE("GPL v2");
485