ccs811.c 9.6 KB

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