vz89x.c 9.3 KB

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