vz89x.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * vz89x.c - Support for SGX Sensortech MiCS VZ89X VOC sensors
  3. *
  4. * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/init.h>
  20. #include <linux/i2c.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/sysfs.h>
  25. #define VZ89X_REG_MEASUREMENT 0x09
  26. #define VZ89X_REG_MEASUREMENT_RD_SIZE 6
  27. #define VZ89X_REG_MEASUREMENT_WR_SIZE 3
  28. #define VZ89X_VOC_CO2_IDX 0
  29. #define VZ89X_VOC_SHORT_IDX 1
  30. #define VZ89X_VOC_TVOC_IDX 2
  31. #define VZ89X_VOC_RESISTANCE_IDX 3
  32. #define VZ89TE_REG_MEASUREMENT 0x0c
  33. #define VZ89TE_REG_MEASUREMENT_RD_SIZE 7
  34. #define VZ89TE_REG_MEASUREMENT_WR_SIZE 6
  35. #define VZ89TE_VOC_TVOC_IDX 0
  36. #define VZ89TE_VOC_CO2_IDX 1
  37. #define VZ89TE_VOC_RESISTANCE_IDX 2
  38. enum {
  39. VZ89X,
  40. VZ89TE,
  41. };
  42. struct vz89x_chip_data;
  43. struct vz89x_data {
  44. struct i2c_client *client;
  45. const struct vz89x_chip_data *chip;
  46. struct mutex lock;
  47. int (*xfer)(struct vz89x_data *data, u8 cmd);
  48. bool is_valid;
  49. unsigned long last_update;
  50. u8 buffer[VZ89TE_REG_MEASUREMENT_RD_SIZE];
  51. };
  52. struct vz89x_chip_data {
  53. bool (*valid)(struct vz89x_data *data);
  54. const struct iio_chan_spec *channels;
  55. u8 num_channels;
  56. u8 cmd;
  57. u8 read_size;
  58. u8 write_size;
  59. };
  60. static const struct iio_chan_spec vz89x_channels[] = {
  61. {
  62. .type = IIO_CONCENTRATION,
  63. .channel2 = IIO_MOD_CO2,
  64. .modified = 1,
  65. .info_mask_separate =
  66. BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
  67. .address = VZ89X_VOC_CO2_IDX,
  68. },
  69. {
  70. .type = IIO_CONCENTRATION,
  71. .channel2 = IIO_MOD_VOC,
  72. .modified = 1,
  73. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  74. .address = VZ89X_VOC_SHORT_IDX,
  75. .extend_name = "short",
  76. },
  77. {
  78. .type = IIO_CONCENTRATION,
  79. .channel2 = IIO_MOD_VOC,
  80. .modified = 1,
  81. .info_mask_separate =
  82. BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
  83. .address = VZ89X_VOC_TVOC_IDX,
  84. },
  85. {
  86. .type = IIO_RESISTANCE,
  87. .info_mask_separate =
  88. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  89. .address = VZ89X_VOC_RESISTANCE_IDX,
  90. .scan_index = -1,
  91. .scan_type = {
  92. .endianness = IIO_LE,
  93. },
  94. },
  95. };
  96. static const struct iio_chan_spec vz89te_channels[] = {
  97. {
  98. .type = IIO_CONCENTRATION,
  99. .channel2 = IIO_MOD_VOC,
  100. .modified = 1,
  101. .info_mask_separate =
  102. BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
  103. .address = VZ89TE_VOC_TVOC_IDX,
  104. },
  105. {
  106. .type = IIO_CONCENTRATION,
  107. .channel2 = IIO_MOD_CO2,
  108. .modified = 1,
  109. .info_mask_separate =
  110. BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
  111. .address = VZ89TE_VOC_CO2_IDX,
  112. },
  113. {
  114. .type = IIO_RESISTANCE,
  115. .info_mask_separate =
  116. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  117. .address = VZ89TE_VOC_RESISTANCE_IDX,
  118. .scan_index = -1,
  119. .scan_type = {
  120. .endianness = IIO_BE,
  121. },
  122. },
  123. };
  124. static IIO_CONST_ATTR(in_concentration_co2_scale, "0.00000698689");
  125. static IIO_CONST_ATTR(in_concentration_voc_scale, "0.00000000436681223");
  126. static struct attribute *vz89x_attributes[] = {
  127. &iio_const_attr_in_concentration_co2_scale.dev_attr.attr,
  128. &iio_const_attr_in_concentration_voc_scale.dev_attr.attr,
  129. NULL,
  130. };
  131. static const struct attribute_group vz89x_attrs_group = {
  132. .attrs = vz89x_attributes,
  133. };
  134. /*
  135. * Chipset sometime updates in the middle of a reading causing it to reset the
  136. * data pointer, and causing invalid reading of previous data.
  137. * We can check for this by reading MSB of the resistance reading that is
  138. * always zero, and by also confirming the VOC_short isn't zero.
  139. */
  140. static bool vz89x_measurement_is_valid(struct vz89x_data *data)
  141. {
  142. if (data->buffer[VZ89X_VOC_SHORT_IDX] == 0)
  143. return true;
  144. return !!(data->buffer[data->chip->read_size - 1] > 0);
  145. }
  146. /* VZ89TE device has a modified CRC-8 two complement check */
  147. static bool vz89te_measurement_is_valid(struct vz89x_data *data)
  148. {
  149. u8 crc = 0;
  150. int i, sum = 0;
  151. for (i = 0; i < (data->chip->read_size - 1); i++) {
  152. sum = crc + data->buffer[i];
  153. crc = sum;
  154. crc += sum / 256;
  155. }
  156. return !((0xff - crc) == data->buffer[data->chip->read_size - 1]);
  157. }
  158. static int vz89x_i2c_xfer(struct vz89x_data *data, u8 cmd)
  159. {
  160. const struct vz89x_chip_data *chip = data->chip;
  161. struct i2c_client *client = data->client;
  162. struct i2c_msg msg[2];
  163. int ret;
  164. u8 buf[6] = { cmd, 0, 0, 0, 0, 0xf3 };
  165. msg[0].addr = client->addr;
  166. msg[0].flags = client->flags;
  167. msg[0].len = chip->write_size;
  168. msg[0].buf = (char *) &buf;
  169. msg[1].addr = client->addr;
  170. msg[1].flags = client->flags | I2C_M_RD;
  171. msg[1].len = chip->read_size;
  172. msg[1].buf = (char *) &data->buffer;
  173. ret = i2c_transfer(client->adapter, msg, 2);
  174. return (ret == 2) ? 0 : ret;
  175. }
  176. static int vz89x_smbus_xfer(struct vz89x_data *data, u8 cmd)
  177. {
  178. struct i2c_client *client = data->client;
  179. int ret;
  180. int i;
  181. ret = i2c_smbus_write_word_data(client, cmd, 0);
  182. if (ret < 0)
  183. return ret;
  184. for (i = 0; i < data->chip->read_size; i++) {
  185. ret = i2c_smbus_read_byte(client);
  186. if (ret < 0)
  187. return ret;
  188. data->buffer[i] = ret;
  189. }
  190. return 0;
  191. }
  192. static int vz89x_get_measurement(struct vz89x_data *data)
  193. {
  194. const struct vz89x_chip_data *chip = data->chip;
  195. int ret;
  196. /* sensor can only be polled once a second max per datasheet */
  197. if (!time_after(jiffies, data->last_update + HZ))
  198. return data->is_valid ? 0 : -EAGAIN;
  199. data->is_valid = false;
  200. data->last_update = jiffies;
  201. ret = data->xfer(data, chip->cmd);
  202. if (ret < 0)
  203. return ret;
  204. ret = chip->valid(data);
  205. if (ret)
  206. return -EAGAIN;
  207. data->is_valid = true;
  208. return 0;
  209. }
  210. static int vz89x_get_resistance_reading(struct vz89x_data *data,
  211. struct iio_chan_spec const *chan,
  212. int *val)
  213. {
  214. u8 *tmp = (u8 *) &data->buffer[chan->address];
  215. switch (chan->scan_type.endianness) {
  216. case IIO_LE:
  217. *val = le32_to_cpup((__le32 *) tmp) & GENMASK(23, 0);
  218. break;
  219. case IIO_BE:
  220. *val = be32_to_cpup((__be32 *) tmp) >> 8;
  221. break;
  222. default:
  223. return -EINVAL;
  224. }
  225. return 0;
  226. }
  227. static int vz89x_read_raw(struct iio_dev *indio_dev,
  228. struct iio_chan_spec const *chan, int *val,
  229. int *val2, long mask)
  230. {
  231. struct vz89x_data *data = iio_priv(indio_dev);
  232. int ret = -EINVAL;
  233. switch (mask) {
  234. case IIO_CHAN_INFO_RAW:
  235. mutex_lock(&data->lock);
  236. ret = vz89x_get_measurement(data);
  237. mutex_unlock(&data->lock);
  238. if (ret)
  239. return ret;
  240. switch (chan->type) {
  241. case IIO_CONCENTRATION:
  242. *val = data->buffer[chan->address];
  243. return IIO_VAL_INT;
  244. case IIO_RESISTANCE:
  245. ret = vz89x_get_resistance_reading(data, chan, val);
  246. if (!ret)
  247. return IIO_VAL_INT;
  248. break;
  249. default:
  250. return -EINVAL;
  251. }
  252. break;
  253. case IIO_CHAN_INFO_SCALE:
  254. switch (chan->type) {
  255. case IIO_RESISTANCE:
  256. *val = 10;
  257. return IIO_VAL_INT;
  258. default:
  259. return -EINVAL;
  260. }
  261. break;
  262. case IIO_CHAN_INFO_OFFSET:
  263. switch (chan->channel2) {
  264. case IIO_MOD_CO2:
  265. *val = 44;
  266. *val2 = 250000;
  267. return IIO_VAL_INT_PLUS_MICRO;
  268. case IIO_MOD_VOC:
  269. *val = -13;
  270. return IIO_VAL_INT;
  271. default:
  272. return -EINVAL;
  273. }
  274. }
  275. return ret;
  276. }
  277. static const struct iio_info vz89x_info = {
  278. .attrs = &vz89x_attrs_group,
  279. .read_raw = vz89x_read_raw,
  280. .driver_module = THIS_MODULE,
  281. };
  282. static const struct vz89x_chip_data vz89x_chips[] = {
  283. {
  284. .valid = vz89x_measurement_is_valid,
  285. .cmd = VZ89X_REG_MEASUREMENT,
  286. .read_size = VZ89X_REG_MEASUREMENT_RD_SIZE,
  287. .write_size = VZ89X_REG_MEASUREMENT_WR_SIZE,
  288. .channels = vz89x_channels,
  289. .num_channels = ARRAY_SIZE(vz89x_channels),
  290. },
  291. {
  292. .valid = vz89te_measurement_is_valid,
  293. .cmd = VZ89TE_REG_MEASUREMENT,
  294. .read_size = VZ89TE_REG_MEASUREMENT_RD_SIZE,
  295. .write_size = VZ89TE_REG_MEASUREMENT_WR_SIZE,
  296. .channels = vz89te_channels,
  297. .num_channels = ARRAY_SIZE(vz89te_channels),
  298. },
  299. };
  300. static const struct of_device_id vz89x_dt_ids[] = {
  301. { .compatible = "sgx,vz89x", .data = (void *) VZ89X },
  302. { .compatible = "sgx,vz89te", .data = (void *) VZ89TE },
  303. { }
  304. };
  305. MODULE_DEVICE_TABLE(of, vz89x_dt_ids);
  306. static int vz89x_probe(struct i2c_client *client,
  307. const struct i2c_device_id *id)
  308. {
  309. struct iio_dev *indio_dev;
  310. struct vz89x_data *data;
  311. const struct of_device_id *of_id;
  312. int chip_id;
  313. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  314. if (!indio_dev)
  315. return -ENOMEM;
  316. data = iio_priv(indio_dev);
  317. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  318. data->xfer = vz89x_i2c_xfer;
  319. else if (i2c_check_functionality(client->adapter,
  320. I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE))
  321. data->xfer = vz89x_smbus_xfer;
  322. else
  323. return -EOPNOTSUPP;
  324. of_id = of_match_device(vz89x_dt_ids, &client->dev);
  325. if (!of_id)
  326. chip_id = id->driver_data;
  327. else
  328. chip_id = (unsigned long)of_id->data;
  329. i2c_set_clientdata(client, indio_dev);
  330. data->client = client;
  331. data->chip = &vz89x_chips[chip_id];
  332. data->last_update = jiffies - HZ;
  333. mutex_init(&data->lock);
  334. indio_dev->dev.parent = &client->dev;
  335. indio_dev->info = &vz89x_info,
  336. indio_dev->name = dev_name(&client->dev);
  337. indio_dev->modes = INDIO_DIRECT_MODE;
  338. indio_dev->channels = data->chip->channels;
  339. indio_dev->num_channels = data->chip->num_channels;
  340. return devm_iio_device_register(&client->dev, indio_dev);
  341. }
  342. static const struct i2c_device_id vz89x_id[] = {
  343. { "vz89x", VZ89X },
  344. { "vz89te", VZ89TE },
  345. { }
  346. };
  347. MODULE_DEVICE_TABLE(i2c, vz89x_id);
  348. static struct i2c_driver vz89x_driver = {
  349. .driver = {
  350. .name = "vz89x",
  351. .of_match_table = of_match_ptr(vz89x_dt_ids),
  352. },
  353. .probe = vz89x_probe,
  354. .id_table = vz89x_id,
  355. };
  356. module_i2c_driver(vz89x_driver);
  357. MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
  358. MODULE_DESCRIPTION("SGX Sensortech MiCS VZ89X VOC sensors");
  359. MODULE_LICENSE("GPL v2");