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/triggered_buffer.h> 26 #include <linux/iio/trigger_consumer.h> 27 #include <linux/module.h> 28 29 #define CCS811_STATUS 0x00 30 #define CCS811_MEAS_MODE 0x01 31 #define CCS811_ALG_RESULT_DATA 0x02 32 #define CCS811_RAW_DATA 0x03 33 #define CCS811_HW_ID 0x20 34 #define CCS881_HW_ID_VALUE 0x81 35 #define CCS811_HW_VERSION 0x21 36 #define CCS811_HW_VERSION_VALUE 0x10 37 #define CCS811_HW_VERSION_MASK 0xF0 38 #define CCS811_ERR 0xE0 39 /* Used to transition from boot to application mode */ 40 #define CCS811_APP_START 0xF4 41 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) 47 /* 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 51 */ 52 #define CCS811_STATUS_FW_MODE_MASK BIT(7) 53 #define CCS811_STATUS_FW_MODE_APPLICATION BIT(7) 54 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 61 62 #define CCS811_VOLTAGE_MASK 0x3FF 63 64 struct ccs811_reading { 65 __be16 co2; 66 __be16 voc; 67 u8 status; 68 u8 error; 69 __be16 resistance; 70 } __attribute__((__packed__)); 71 72 struct ccs811_data { 73 struct i2c_client *client; 74 struct mutex lock; /* Protect readings */ 75 struct ccs811_reading buffer; 76 }; 77 78 static const struct iio_chan_spec ccs811_channels[] = { 79 { 80 .type = IIO_CURRENT, 81 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 82 BIT(IIO_CHAN_INFO_SCALE), 83 .scan_index = -1, 84 }, { 85 .type = IIO_VOLTAGE, 86 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 87 BIT(IIO_CHAN_INFO_SCALE), 88 .scan_index = -1, 89 }, { 90 .type = IIO_CONCENTRATION, 91 .channel2 = IIO_MOD_CO2, 92 .modified = 1, 93 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 94 BIT(IIO_CHAN_INFO_OFFSET) | 95 BIT(IIO_CHAN_INFO_SCALE), 96 .scan_index = 0, 97 .scan_type = { 98 .sign = 'u', 99 .realbits = 16, 100 .storagebits = 16, 101 .endianness = IIO_BE, 102 }, 103 }, { 104 .type = IIO_CONCENTRATION, 105 .channel2 = IIO_MOD_VOC, 106 .modified = 1, 107 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 108 BIT(IIO_CHAN_INFO_SCALE), 109 .scan_index = 1, 110 .scan_type = { 111 .sign = 'u', 112 .realbits = 16, 113 .storagebits = 16, 114 .endianness = IIO_BE, 115 }, 116 }, 117 IIO_CHAN_SOFT_TIMESTAMP(2), 118 }; 119 120 /* 121 * The CCS811 powers-up in boot mode. A setup write to CCS811_APP_START will 122 * transition the sensor to application mode. 123 */ 124 static int ccs811_start_sensor_application(struct i2c_client *client) 125 { 126 int ret; 127 128 ret = i2c_smbus_read_byte_data(client, CCS811_STATUS); 129 if (ret < 0) 130 return ret; 131 132 if ((ret & CCS811_STATUS_APP_VALID_MASK) != 133 CCS811_STATUS_APP_VALID_LOADED) 134 return -EIO; 135 136 ret = i2c_smbus_write_byte(client, CCS811_APP_START); 137 if (ret < 0) 138 return ret; 139 140 ret = i2c_smbus_read_byte_data(client, CCS811_STATUS); 141 if (ret < 0) 142 return ret; 143 144 if ((ret & CCS811_STATUS_FW_MODE_MASK) != 145 CCS811_STATUS_FW_MODE_APPLICATION) { 146 dev_err(&client->dev, "Application failed to start. Sensor is still in boot mode.\n"); 147 return -EIO; 148 } 149 150 return 0; 151 } 152 153 static int ccs811_setup(struct i2c_client *client) 154 { 155 int ret; 156 157 ret = ccs811_start_sensor_application(client); 158 if (ret < 0) 159 return ret; 160 161 return i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE, 162 CCS811_MODE_IAQ_1SEC); 163 } 164 165 static int ccs811_get_measurement(struct ccs811_data *data) 166 { 167 int ret, tries = 11; 168 169 /* Maximum waiting time: 1s, as measurements are made every second */ 170 while (tries-- > 0) { 171 ret = i2c_smbus_read_byte_data(data->client, CCS811_STATUS); 172 if (ret < 0) 173 return ret; 174 175 if ((ret & CCS811_STATUS_DATA_READY) || tries == 0) 176 break; 177 msleep(100); 178 } 179 if (!(ret & CCS811_STATUS_DATA_READY)) 180 return -EIO; 181 182 return i2c_smbus_read_i2c_block_data(data->client, 183 CCS811_ALG_RESULT_DATA, 8, 184 (char *)&data->buffer); 185 } 186 187 static int ccs811_read_raw(struct iio_dev *indio_dev, 188 struct iio_chan_spec const *chan, 189 int *val, int *val2, long mask) 190 { 191 struct ccs811_data *data = iio_priv(indio_dev); 192 int ret; 193 194 switch (mask) { 195 case IIO_CHAN_INFO_RAW: 196 mutex_lock(&data->lock); 197 ret = ccs811_get_measurement(data); 198 if (ret < 0) { 199 mutex_unlock(&data->lock); 200 return ret; 201 } 202 203 switch (chan->type) { 204 case IIO_VOLTAGE: 205 *val = be16_to_cpu(data->buffer.resistance) & 206 CCS811_VOLTAGE_MASK; 207 ret = IIO_VAL_INT; 208 break; 209 case IIO_CURRENT: 210 *val = be16_to_cpu(data->buffer.resistance) >> 10; 211 ret = IIO_VAL_INT; 212 break; 213 case IIO_CONCENTRATION: 214 switch (chan->channel2) { 215 case IIO_MOD_CO2: 216 *val = be16_to_cpu(data->buffer.co2); 217 ret = IIO_VAL_INT; 218 break; 219 case IIO_MOD_VOC: 220 *val = be16_to_cpu(data->buffer.voc); 221 ret = IIO_VAL_INT; 222 break; 223 default: 224 ret = -EINVAL; 225 } 226 break; 227 default: 228 ret = -EINVAL; 229 } 230 mutex_unlock(&data->lock); 231 232 return ret; 233 234 case IIO_CHAN_INFO_SCALE: 235 switch (chan->type) { 236 case IIO_VOLTAGE: 237 *val = 1; 238 *val2 = 612903; 239 return IIO_VAL_INT_PLUS_MICRO; 240 case IIO_CURRENT: 241 *val = 0; 242 *val2 = 1000; 243 return IIO_VAL_INT_PLUS_MICRO; 244 case IIO_CONCENTRATION: 245 switch (chan->channel2) { 246 case IIO_MOD_CO2: 247 *val = 0; 248 *val2 = 12834; 249 return IIO_VAL_INT_PLUS_MICRO; 250 case IIO_MOD_VOC: 251 *val = 0; 252 *val2 = 84246; 253 return IIO_VAL_INT_PLUS_MICRO; 254 default: 255 return -EINVAL; 256 } 257 default: 258 return -EINVAL; 259 } 260 case IIO_CHAN_INFO_OFFSET: 261 if (!(chan->type == IIO_CONCENTRATION && 262 chan->channel2 == IIO_MOD_CO2)) 263 return -EINVAL; 264 *val = -400; 265 return IIO_VAL_INT; 266 default: 267 return -EINVAL; 268 } 269 } 270 271 static const struct iio_info ccs811_info = { 272 .read_raw = ccs811_read_raw, 273 .driver_module = THIS_MODULE, 274 }; 275 276 static irqreturn_t ccs811_trigger_handler(int irq, void *p) 277 { 278 struct iio_poll_func *pf = p; 279 struct iio_dev *indio_dev = pf->indio_dev; 280 struct ccs811_data *data = iio_priv(indio_dev); 281 struct i2c_client *client = data->client; 282 s16 buf[8]; /* s16 eCO2 + s16 TVOC + padding + 8 byte timestamp */ 283 int ret; 284 285 ret = i2c_smbus_read_i2c_block_data(client, CCS811_ALG_RESULT_DATA, 4, 286 (u8 *)&buf); 287 if (ret != 4) { 288 dev_err(&client->dev, "cannot read sensor data\n"); 289 goto err; 290 } 291 292 iio_push_to_buffers_with_timestamp(indio_dev, buf, 293 iio_get_time_ns(indio_dev)); 294 295 err: 296 iio_trigger_notify_done(indio_dev->trig); 297 298 return IRQ_HANDLED; 299 } 300 301 static int ccs811_probe(struct i2c_client *client, 302 const struct i2c_device_id *id) 303 { 304 struct iio_dev *indio_dev; 305 struct ccs811_data *data; 306 int ret; 307 308 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE 309 | I2C_FUNC_SMBUS_BYTE_DATA 310 | I2C_FUNC_SMBUS_READ_I2C_BLOCK)) 311 return -EOPNOTSUPP; 312 313 /* Check hardware id (should be 0x81 for this family of devices) */ 314 ret = i2c_smbus_read_byte_data(client, CCS811_HW_ID); 315 if (ret < 0) 316 return ret; 317 318 if (ret != CCS881_HW_ID_VALUE) { 319 dev_err(&client->dev, "hardware id doesn't match CCS81x\n"); 320 return -ENODEV; 321 } 322 323 ret = i2c_smbus_read_byte_data(client, CCS811_HW_VERSION); 324 if (ret < 0) 325 return ret; 326 327 if ((ret & CCS811_HW_VERSION_MASK) != CCS811_HW_VERSION_VALUE) { 328 dev_err(&client->dev, "no CCS811 sensor\n"); 329 return -ENODEV; 330 } 331 332 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 333 if (!indio_dev) 334 return -ENOMEM; 335 336 ret = ccs811_setup(client); 337 if (ret < 0) 338 return ret; 339 340 data = iio_priv(indio_dev); 341 i2c_set_clientdata(client, indio_dev); 342 data->client = client; 343 344 mutex_init(&data->lock); 345 346 indio_dev->dev.parent = &client->dev; 347 indio_dev->name = id->name; 348 indio_dev->info = &ccs811_info; 349 350 indio_dev->channels = ccs811_channels; 351 indio_dev->num_channels = ARRAY_SIZE(ccs811_channels); 352 353 ret = iio_triggered_buffer_setup(indio_dev, NULL, 354 ccs811_trigger_handler, NULL); 355 356 if (ret < 0) { 357 dev_err(&client->dev, "triggered buffer setup failed\n"); 358 goto err_poweroff; 359 } 360 361 ret = iio_device_register(indio_dev); 362 if (ret < 0) { 363 dev_err(&client->dev, "unable to register iio device\n"); 364 goto err_buffer_cleanup; 365 } 366 return 0; 367 368 err_buffer_cleanup: 369 iio_triggered_buffer_cleanup(indio_dev); 370 err_poweroff: 371 i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE, CCS811_MODE_IDLE); 372 373 return ret; 374 } 375 376 static int ccs811_remove(struct i2c_client *client) 377 { 378 struct iio_dev *indio_dev = i2c_get_clientdata(client); 379 380 iio_device_unregister(indio_dev); 381 iio_triggered_buffer_cleanup(indio_dev); 382 383 return i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE, 384 CCS811_MODE_IDLE); 385 } 386 387 static const struct i2c_device_id ccs811_id[] = { 388 {"ccs811", 0}, 389 { } 390 }; 391 MODULE_DEVICE_TABLE(i2c, ccs811_id); 392 393 static struct i2c_driver ccs811_driver = { 394 .driver = { 395 .name = "ccs811", 396 }, 397 .probe = ccs811_probe, 398 .remove = ccs811_remove, 399 .id_table = ccs811_id, 400 }; 401 module_i2c_driver(ccs811_driver); 402 403 MODULE_AUTHOR("Narcisa Vasile <narcisaanamaria12@gmail.com>"); 404 MODULE_DESCRIPTION("CCS811 volatile organic compounds sensor"); 405 MODULE_LICENSE("GPL v2"); 406