ccs811.c 12 KB

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