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 	/* Ensures correct alignment of timestamp if present */
82 	struct {
83 		s16 channels[2];
84 		s64 ts __aligned(8);
85 	} scan;
86 };
87 
88 static const struct iio_chan_spec ccs811_channels[] = {
89 	{
90 		.type = IIO_CURRENT,
91 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
92 				      BIT(IIO_CHAN_INFO_SCALE),
93 		.scan_index = -1,
94 	}, {
95 		.type = IIO_VOLTAGE,
96 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
97 				      BIT(IIO_CHAN_INFO_SCALE),
98 		.scan_index = -1,
99 	}, {
100 		.type = IIO_CONCENTRATION,
101 		.channel2 = IIO_MOD_CO2,
102 		.modified = 1,
103 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
104 				      BIT(IIO_CHAN_INFO_SCALE),
105 		.scan_index = 0,
106 		.scan_type = {
107 			.sign = 'u',
108 			.realbits = 16,
109 			.storagebits = 16,
110 			.endianness = IIO_BE,
111 		},
112 	}, {
113 		.type = IIO_CONCENTRATION,
114 		.channel2 = IIO_MOD_VOC,
115 		.modified = 1,
116 		.info_mask_separate =  BIT(IIO_CHAN_INFO_RAW) |
117 				       BIT(IIO_CHAN_INFO_SCALE),
118 		.scan_index = 1,
119 		.scan_type = {
120 			.sign = 'u',
121 			.realbits = 16,
122 			.storagebits = 16,
123 			.endianness = IIO_BE,
124 		},
125 	},
126 	IIO_CHAN_SOFT_TIMESTAMP(2),
127 };
128 
129 /*
130  * The CCS811 powers-up in boot mode. A setup write to CCS811_APP_START will
131  * transition the sensor to application mode.
132  */
ccs811_start_sensor_application(struct i2c_client * client)133 static int ccs811_start_sensor_application(struct i2c_client *client)
134 {
135 	int ret;
136 
137 	ret = i2c_smbus_read_byte_data(client, CCS811_STATUS);
138 	if (ret < 0)
139 		return ret;
140 
141 	if ((ret & CCS811_STATUS_FW_MODE_APPLICATION))
142 		return 0;
143 
144 	if ((ret & CCS811_STATUS_APP_VALID_MASK) !=
145 	    CCS811_STATUS_APP_VALID_LOADED)
146 		return -EIO;
147 
148 	ret = i2c_smbus_write_byte(client, CCS811_APP_START);
149 	if (ret < 0)
150 		return ret;
151 
152 	ret = i2c_smbus_read_byte_data(client, CCS811_STATUS);
153 	if (ret < 0)
154 		return ret;
155 
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");
159 		return -EIO;
160 	}
161 
162 	return 0;
163 }
164 
ccs811_setup(struct i2c_client * client)165 static int ccs811_setup(struct i2c_client *client)
166 {
167 	int ret;
168 
169 	ret = ccs811_start_sensor_application(client);
170 	if (ret < 0)
171 		return ret;
172 
173 	return i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE,
174 					 CCS811_MODE_IAQ_1SEC);
175 }
176 
ccs811_get_measurement(struct ccs811_data * data)177 static int ccs811_get_measurement(struct ccs811_data *data)
178 {
179 	int ret, tries = 11;
180 
181 	/* Maximum waiting time: 1s, as measurements are made every second */
182 	while (tries-- > 0) {
183 		ret = i2c_smbus_read_byte_data(data->client, CCS811_STATUS);
184 		if (ret < 0)
185 			return ret;
186 
187 		if ((ret & CCS811_STATUS_DATA_READY) || tries == 0)
188 			break;
189 		msleep(100);
190 	}
191 	if (!(ret & CCS811_STATUS_DATA_READY))
192 		return -EIO;
193 
194 	return i2c_smbus_read_i2c_block_data(data->client,
195 					    CCS811_ALG_RESULT_DATA, 8,
196 					    (char *)&data->buffer);
197 }
198 
ccs811_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)199 static int ccs811_read_raw(struct iio_dev *indio_dev,
200 			   struct iio_chan_spec const *chan,
201 			   int *val, int *val2, long mask)
202 {
203 	struct ccs811_data *data = iio_priv(indio_dev);
204 	int ret;
205 
206 	switch (mask) {
207 	case IIO_CHAN_INFO_RAW:
208 		ret = iio_device_claim_direct_mode(indio_dev);
209 		if (ret)
210 			return ret;
211 		mutex_lock(&data->lock);
212 		ret = ccs811_get_measurement(data);
213 		if (ret < 0) {
214 			mutex_unlock(&data->lock);
215 			iio_device_release_direct_mode(indio_dev);
216 			return ret;
217 		}
218 
219 		switch (chan->type) {
220 		case IIO_VOLTAGE:
221 			*val = be16_to_cpu(data->buffer.raw_data) &
222 					   CCS811_VOLTAGE_MASK;
223 			ret = IIO_VAL_INT;
224 			break;
225 		case IIO_CURRENT:
226 			*val = be16_to_cpu(data->buffer.raw_data) >> 10;
227 			ret = IIO_VAL_INT;
228 			break;
229 		case IIO_CONCENTRATION:
230 			switch (chan->channel2) {
231 			case IIO_MOD_CO2:
232 				*val = be16_to_cpu(data->buffer.co2);
233 				ret =  IIO_VAL_INT;
234 				break;
235 			case IIO_MOD_VOC:
236 				*val = be16_to_cpu(data->buffer.voc);
237 				ret = IIO_VAL_INT;
238 				break;
239 			default:
240 				ret = -EINVAL;
241 			}
242 			break;
243 		default:
244 			ret = -EINVAL;
245 		}
246 		mutex_unlock(&data->lock);
247 		iio_device_release_direct_mode(indio_dev);
248 
249 		return ret;
250 
251 	case IIO_CHAN_INFO_SCALE:
252 		switch (chan->type) {
253 		case IIO_VOLTAGE:
254 			*val = 1;
255 			*val2 = 612903;
256 			return IIO_VAL_INT_PLUS_MICRO;
257 		case IIO_CURRENT:
258 			*val = 0;
259 			*val2 = 1000;
260 			return IIO_VAL_INT_PLUS_MICRO;
261 		case IIO_CONCENTRATION:
262 			switch (chan->channel2) {
263 			case IIO_MOD_CO2:
264 				*val = 0;
265 				*val2 = 100;
266 				return IIO_VAL_INT_PLUS_MICRO;
267 			case IIO_MOD_VOC:
268 				*val = 0;
269 				*val2 = 100;
270 				return IIO_VAL_INT_PLUS_NANO;
271 			default:
272 				return -EINVAL;
273 			}
274 		default:
275 			return -EINVAL;
276 		}
277 	default:
278 		return -EINVAL;
279 	}
280 }
281 
282 static const struct iio_info ccs811_info = {
283 	.read_raw = ccs811_read_raw,
284 };
285 
ccs811_set_trigger_state(struct iio_trigger * trig,bool state)286 static int ccs811_set_trigger_state(struct iio_trigger *trig,
287 				    bool state)
288 {
289 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
290 	struct ccs811_data *data = iio_priv(indio_dev);
291 	int ret;
292 
293 	ret = i2c_smbus_read_byte_data(data->client, CCS811_MEAS_MODE);
294 	if (ret < 0)
295 		return ret;
296 
297 	if (state)
298 		ret |= CCS811_MEAS_MODE_INTERRUPT;
299 	else
300 		ret &= ~CCS811_MEAS_MODE_INTERRUPT;
301 
302 	data->drdy_trig_on = state;
303 
304 	return i2c_smbus_write_byte_data(data->client, CCS811_MEAS_MODE, ret);
305 }
306 
307 static const struct iio_trigger_ops ccs811_trigger_ops = {
308 	.set_trigger_state = ccs811_set_trigger_state,
309 };
310 
ccs811_trigger_handler(int irq,void * p)311 static irqreturn_t ccs811_trigger_handler(int irq, void *p)
312 {
313 	struct iio_poll_func *pf = p;
314 	struct iio_dev *indio_dev = pf->indio_dev;
315 	struct ccs811_data *data = iio_priv(indio_dev);
316 	struct i2c_client *client = data->client;
317 	int ret;
318 
319 	ret = i2c_smbus_read_i2c_block_data(client, CCS811_ALG_RESULT_DATA,
320 					    sizeof(data->scan.channels),
321 					    (u8 *)data->scan.channels);
322 	if (ret != 4) {
323 		dev_err(&client->dev, "cannot read sensor data\n");
324 		goto err;
325 	}
326 
327 	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
328 					   iio_get_time_ns(indio_dev));
329 
330 err:
331 	iio_trigger_notify_done(indio_dev->trig);
332 
333 	return IRQ_HANDLED;
334 }
335 
ccs811_data_rdy_trigger_poll(int irq,void * private)336 static irqreturn_t ccs811_data_rdy_trigger_poll(int irq, void *private)
337 {
338 	struct iio_dev *indio_dev = private;
339 	struct ccs811_data *data = iio_priv(indio_dev);
340 
341 	if (data->drdy_trig_on)
342 		iio_trigger_poll(data->drdy_trig);
343 
344 	return IRQ_HANDLED;
345 }
346 
ccs811_probe(struct i2c_client * client,const struct i2c_device_id * id)347 static int ccs811_probe(struct i2c_client *client,
348 			const struct i2c_device_id *id)
349 {
350 	struct iio_dev *indio_dev;
351 	struct ccs811_data *data;
352 	int ret;
353 
354 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
355 				     | I2C_FUNC_SMBUS_BYTE_DATA
356 				     | I2C_FUNC_SMBUS_READ_I2C_BLOCK))
357 		return -EOPNOTSUPP;
358 
359 	/* Check hardware id (should be 0x81 for this family of devices) */
360 	ret = i2c_smbus_read_byte_data(client, CCS811_HW_ID);
361 	if (ret < 0)
362 		return ret;
363 
364 	if (ret != CCS811_HW_ID_VALUE) {
365 		dev_err(&client->dev, "hardware id doesn't match CCS81x\n");
366 		return -ENODEV;
367 	}
368 
369 	ret = i2c_smbus_read_byte_data(client, CCS811_HW_VERSION);
370 	if (ret < 0)
371 		return ret;
372 
373 	if ((ret & CCS811_HW_VERSION_MASK) != CCS811_HW_VERSION_VALUE) {
374 		dev_err(&client->dev, "no CCS811 sensor\n");
375 		return -ENODEV;
376 	}
377 
378 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
379 	if (!indio_dev)
380 		return -ENOMEM;
381 
382 	ret = ccs811_setup(client);
383 	if (ret < 0)
384 		return ret;
385 
386 	data = iio_priv(indio_dev);
387 	i2c_set_clientdata(client, indio_dev);
388 	data->client = client;
389 
390 	mutex_init(&data->lock);
391 
392 	indio_dev->dev.parent = &client->dev;
393 	indio_dev->name = id->name;
394 	indio_dev->info = &ccs811_info;
395 	indio_dev->modes = INDIO_DIRECT_MODE;
396 
397 	indio_dev->channels = ccs811_channels;
398 	indio_dev->num_channels = ARRAY_SIZE(ccs811_channels);
399 
400 	if (client->irq > 0) {
401 		ret = devm_request_threaded_irq(&client->dev, client->irq,
402 						ccs811_data_rdy_trigger_poll,
403 						NULL,
404 						IRQF_TRIGGER_FALLING |
405 						IRQF_ONESHOT,
406 						"ccs811_irq", indio_dev);
407 		if (ret) {
408 			dev_err(&client->dev, "irq request error %d\n", -ret);
409 			goto err_poweroff;
410 		}
411 
412 		data->drdy_trig = devm_iio_trigger_alloc(&client->dev,
413 							 "%s-dev%d",
414 							 indio_dev->name,
415 							 indio_dev->id);
416 		if (!data->drdy_trig) {
417 			ret = -ENOMEM;
418 			goto err_poweroff;
419 		}
420 
421 		data->drdy_trig->dev.parent = &client->dev;
422 		data->drdy_trig->ops = &ccs811_trigger_ops;
423 		iio_trigger_set_drvdata(data->drdy_trig, indio_dev);
424 		ret = iio_trigger_register(data->drdy_trig);
425 		if (ret)
426 			goto err_poweroff;
427 
428 		indio_dev->trig = iio_trigger_get(data->drdy_trig);
429 	}
430 
431 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
432 					 ccs811_trigger_handler, NULL);
433 
434 	if (ret < 0) {
435 		dev_err(&client->dev, "triggered buffer setup failed\n");
436 		goto err_trigger_unregister;
437 	}
438 
439 	ret = iio_device_register(indio_dev);
440 	if (ret < 0) {
441 		dev_err(&client->dev, "unable to register iio device\n");
442 		goto err_buffer_cleanup;
443 	}
444 	return 0;
445 
446 err_buffer_cleanup:
447 	iio_triggered_buffer_cleanup(indio_dev);
448 err_trigger_unregister:
449 	if (data->drdy_trig)
450 		iio_trigger_unregister(data->drdy_trig);
451 err_poweroff:
452 	i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE, CCS811_MODE_IDLE);
453 
454 	return ret;
455 }
456 
ccs811_remove(struct i2c_client * client)457 static int ccs811_remove(struct i2c_client *client)
458 {
459 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
460 	struct ccs811_data *data = iio_priv(indio_dev);
461 
462 	iio_device_unregister(indio_dev);
463 	iio_triggered_buffer_cleanup(indio_dev);
464 	if (data->drdy_trig)
465 		iio_trigger_unregister(data->drdy_trig);
466 
467 	return i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE,
468 					 CCS811_MODE_IDLE);
469 }
470 
471 static const struct i2c_device_id ccs811_id[] = {
472 	{"ccs811", 0},
473 	{	}
474 };
475 MODULE_DEVICE_TABLE(i2c, ccs811_id);
476 
477 static struct i2c_driver ccs811_driver = {
478 	.driver = {
479 		.name = "ccs811",
480 	},
481 	.probe = ccs811_probe,
482 	.remove = ccs811_remove,
483 	.id_table = ccs811_id,
484 };
485 module_i2c_driver(ccs811_driver);
486 
487 MODULE_AUTHOR("Narcisa Vasile <narcisaanamaria12@gmail.com>");
488 MODULE_DESCRIPTION("CCS811 volatile organic compounds sensor");
489 MODULE_LICENSE("GPL v2");
490