lmp91000.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * lmp91000.c - Support for Texas Instruments digital potentiostats
  4. *
  5. * Copyright (C) 2016, 2018
  6. * Author: Matt Ranostay <matt.ranostay@konsulko.com>
  7. *
  8. * TODO: bias voltage + polarity control, and multiple chip support
  9. */
  10. #include <linux/module.h>
  11. #include <linux/i2c.h>
  12. #include <linux/delay.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/regmap.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/buffer.h>
  17. #include <linux/iio/consumer.h>
  18. #include <linux/iio/trigger.h>
  19. #include <linux/iio/trigger_consumer.h>
  20. #include <linux/iio/triggered_buffer.h>
  21. #define LMP91000_REG_LOCK 0x01
  22. #define LMP91000_REG_TIACN 0x10
  23. #define LMP91000_REG_TIACN_GAIN_SHIFT 2
  24. #define LMP91000_REG_REFCN 0x11
  25. #define LMP91000_REG_REFCN_EXT_REF 0x20
  26. #define LMP91000_REG_REFCN_50_ZERO 0x80
  27. #define LMP91000_REG_MODECN 0x12
  28. #define LMP91000_REG_MODECN_3LEAD 0x03
  29. #define LMP91000_REG_MODECN_TEMP 0x07
  30. #define LMP91000_DRV_NAME "lmp91000"
  31. static const int lmp91000_tia_gain[] = { 0, 2750, 3500, 7000, 14000, 35000,
  32. 120000, 350000 };
  33. static const int lmp91000_rload[] = { 10, 33, 50, 100 };
  34. #define LMP91000_TEMP_BASE -40
  35. static const u16 lmp91000_temp_lut[] = {
  36. 1875, 1867, 1860, 1852, 1844, 1836, 1828, 1821, 1813, 1805,
  37. 1797, 1789, 1782, 1774, 1766, 1758, 1750, 1742, 1734, 1727,
  38. 1719, 1711, 1703, 1695, 1687, 1679, 1671, 1663, 1656, 1648,
  39. 1640, 1632, 1624, 1616, 1608, 1600, 1592, 1584, 1576, 1568,
  40. 1560, 1552, 1544, 1536, 1528, 1520, 1512, 1504, 1496, 1488,
  41. 1480, 1472, 1464, 1456, 1448, 1440, 1432, 1424, 1415, 1407,
  42. 1399, 1391, 1383, 1375, 1367, 1359, 1351, 1342, 1334, 1326,
  43. 1318, 1310, 1302, 1293, 1285, 1277, 1269, 1261, 1253, 1244,
  44. 1236, 1228, 1220, 1212, 1203, 1195, 1187, 1179, 1170, 1162,
  45. 1154, 1146, 1137, 1129, 1121, 1112, 1104, 1096, 1087, 1079,
  46. 1071, 1063, 1054, 1046, 1038, 1029, 1021, 1012, 1004, 996,
  47. 987, 979, 971, 962, 954, 945, 937, 929, 920, 912,
  48. 903, 895, 886, 878, 870, 861 };
  49. static const struct regmap_config lmp91000_regmap_config = {
  50. .reg_bits = 8,
  51. .val_bits = 8,
  52. };
  53. struct lmp91000_data {
  54. struct regmap *regmap;
  55. struct device *dev;
  56. struct iio_trigger *trig;
  57. struct iio_cb_buffer *cb_buffer;
  58. struct iio_channel *adc_chan;
  59. struct completion completion;
  60. u8 chan_select;
  61. /* 64-bit data + 64-bit naturally aligned timestamp */
  62. u32 buffer[4] __aligned(8);
  63. };
  64. static const struct iio_chan_spec lmp91000_channels[] = {
  65. { /* chemical channel mV */
  66. .type = IIO_VOLTAGE,
  67. .channel = 0,
  68. .address = LMP91000_REG_MODECN_3LEAD,
  69. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  70. BIT(IIO_CHAN_INFO_OFFSET) |
  71. BIT(IIO_CHAN_INFO_SCALE),
  72. .scan_index = 0,
  73. .scan_type = {
  74. .sign = 's',
  75. .realbits = 32,
  76. .storagebits = 32,
  77. },
  78. },
  79. IIO_CHAN_SOFT_TIMESTAMP(1),
  80. { /* temperature channel mV */
  81. .type = IIO_TEMP,
  82. .channel = 1,
  83. .address = LMP91000_REG_MODECN_TEMP,
  84. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  85. .scan_index = -1,
  86. },
  87. };
  88. static int lmp91000_read(struct lmp91000_data *data, int channel, int *val)
  89. {
  90. int state, ret;
  91. ret = regmap_read(data->regmap, LMP91000_REG_MODECN, &state);
  92. if (ret)
  93. return -EINVAL;
  94. ret = regmap_write(data->regmap, LMP91000_REG_MODECN, channel);
  95. if (ret)
  96. return -EINVAL;
  97. /* delay till first temperature reading is complete */
  98. if (state != channel && channel == LMP91000_REG_MODECN_TEMP)
  99. usleep_range(3000, 4000);
  100. data->chan_select = channel != LMP91000_REG_MODECN_3LEAD;
  101. iio_trigger_poll_chained(data->trig);
  102. ret = wait_for_completion_timeout(&data->completion, HZ);
  103. reinit_completion(&data->completion);
  104. if (!ret)
  105. return -ETIMEDOUT;
  106. *val = data->buffer[data->chan_select];
  107. return 0;
  108. }
  109. static irqreturn_t lmp91000_buffer_handler(int irq, void *private)
  110. {
  111. struct iio_poll_func *pf = private;
  112. struct iio_dev *indio_dev = pf->indio_dev;
  113. struct lmp91000_data *data = iio_priv(indio_dev);
  114. int ret, val;
  115. memset(data->buffer, 0, sizeof(data->buffer));
  116. ret = lmp91000_read(data, LMP91000_REG_MODECN_3LEAD, &val);
  117. if (!ret) {
  118. data->buffer[0] = val;
  119. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  120. iio_get_time_ns(indio_dev));
  121. }
  122. iio_trigger_notify_done(indio_dev->trig);
  123. return IRQ_HANDLED;
  124. }
  125. static int lmp91000_read_raw(struct iio_dev *indio_dev,
  126. struct iio_chan_spec const *chan,
  127. int *val, int *val2, long mask)
  128. {
  129. struct lmp91000_data *data = iio_priv(indio_dev);
  130. switch (mask) {
  131. case IIO_CHAN_INFO_RAW:
  132. case IIO_CHAN_INFO_PROCESSED: {
  133. int ret = iio_channel_start_all_cb(data->cb_buffer);
  134. if (ret)
  135. return ret;
  136. ret = lmp91000_read(data, chan->address, val);
  137. iio_channel_stop_all_cb(data->cb_buffer);
  138. if (ret)
  139. return ret;
  140. if (mask == IIO_CHAN_INFO_PROCESSED) {
  141. int tmp, i;
  142. ret = iio_convert_raw_to_processed(data->adc_chan,
  143. *val, &tmp, 1);
  144. if (ret)
  145. return ret;
  146. for (i = 0; i < ARRAY_SIZE(lmp91000_temp_lut); i++)
  147. if (lmp91000_temp_lut[i] < tmp)
  148. break;
  149. *val = (LMP91000_TEMP_BASE + i) * 1000;
  150. }
  151. return IIO_VAL_INT;
  152. }
  153. case IIO_CHAN_INFO_OFFSET:
  154. return iio_read_channel_offset(data->adc_chan, val, val2);
  155. case IIO_CHAN_INFO_SCALE:
  156. return iio_read_channel_scale(data->adc_chan, val, val2);
  157. }
  158. return -EINVAL;
  159. }
  160. static const struct iio_info lmp91000_info = {
  161. .read_raw = lmp91000_read_raw,
  162. };
  163. static int lmp91000_read_config(struct lmp91000_data *data)
  164. {
  165. struct device *dev = data->dev;
  166. unsigned int reg, val;
  167. int i, ret;
  168. ret = device_property_read_u32(dev, "ti,tia-gain-ohm", &val);
  169. if (ret) {
  170. if (!device_property_read_bool(dev, "ti,external-tia-resistor")) {
  171. dev_err(dev, "no ti,tia-gain-ohm defined and external resistor not specified\n");
  172. return ret;
  173. }
  174. val = 0;
  175. }
  176. ret = -EINVAL;
  177. for (i = 0; i < ARRAY_SIZE(lmp91000_tia_gain); i++) {
  178. if (lmp91000_tia_gain[i] == val) {
  179. reg = i << LMP91000_REG_TIACN_GAIN_SHIFT;
  180. ret = 0;
  181. break;
  182. }
  183. }
  184. if (ret) {
  185. dev_err(dev, "invalid ti,tia-gain-ohm %d\n", val);
  186. return ret;
  187. }
  188. ret = device_property_read_u32(dev, "ti,rload-ohm", &val);
  189. if (ret) {
  190. val = 100;
  191. dev_info(dev, "no ti,rload-ohm defined, default to %d\n", val);
  192. }
  193. ret = -EINVAL;
  194. for (i = 0; i < ARRAY_SIZE(lmp91000_rload); i++) {
  195. if (lmp91000_rload[i] == val) {
  196. reg |= i;
  197. ret = 0;
  198. break;
  199. }
  200. }
  201. if (ret) {
  202. dev_err(dev, "invalid ti,rload-ohm %d\n", val);
  203. return ret;
  204. }
  205. regmap_write(data->regmap, LMP91000_REG_LOCK, 0);
  206. regmap_write(data->regmap, LMP91000_REG_TIACN, reg);
  207. regmap_write(data->regmap, LMP91000_REG_REFCN,
  208. LMP91000_REG_REFCN_EXT_REF | LMP91000_REG_REFCN_50_ZERO);
  209. regmap_write(data->regmap, LMP91000_REG_LOCK, 1);
  210. return 0;
  211. }
  212. static int lmp91000_buffer_cb(const void *val, void *private)
  213. {
  214. struct iio_dev *indio_dev = private;
  215. struct lmp91000_data *data = iio_priv(indio_dev);
  216. data->buffer[data->chan_select] = *((int *)val);
  217. complete_all(&data->completion);
  218. return 0;
  219. }
  220. static const struct iio_trigger_ops lmp91000_trigger_ops = {
  221. };
  222. static int lmp91000_buffer_postenable(struct iio_dev *indio_dev)
  223. {
  224. struct lmp91000_data *data = iio_priv(indio_dev);
  225. return iio_channel_start_all_cb(data->cb_buffer);
  226. }
  227. static int lmp91000_buffer_predisable(struct iio_dev *indio_dev)
  228. {
  229. struct lmp91000_data *data = iio_priv(indio_dev);
  230. iio_channel_stop_all_cb(data->cb_buffer);
  231. return 0;
  232. }
  233. static const struct iio_buffer_setup_ops lmp91000_buffer_setup_ops = {
  234. .postenable = lmp91000_buffer_postenable,
  235. .predisable = lmp91000_buffer_predisable,
  236. };
  237. static int lmp91000_probe(struct i2c_client *client,
  238. const struct i2c_device_id *id)
  239. {
  240. struct device *dev = &client->dev;
  241. struct lmp91000_data *data;
  242. struct iio_dev *indio_dev;
  243. int ret;
  244. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  245. if (!indio_dev)
  246. return -ENOMEM;
  247. indio_dev->info = &lmp91000_info;
  248. indio_dev->channels = lmp91000_channels;
  249. indio_dev->num_channels = ARRAY_SIZE(lmp91000_channels);
  250. indio_dev->name = LMP91000_DRV_NAME;
  251. indio_dev->modes = INDIO_DIRECT_MODE;
  252. i2c_set_clientdata(client, indio_dev);
  253. data = iio_priv(indio_dev);
  254. data->dev = dev;
  255. data->regmap = devm_regmap_init_i2c(client, &lmp91000_regmap_config);
  256. if (IS_ERR(data->regmap)) {
  257. dev_err(dev, "regmap initialization failed.\n");
  258. return PTR_ERR(data->regmap);
  259. }
  260. data->trig = devm_iio_trigger_alloc(data->dev, "%s-mux%d",
  261. indio_dev->name, indio_dev->id);
  262. if (!data->trig) {
  263. dev_err(dev, "cannot allocate iio trigger.\n");
  264. return -ENOMEM;
  265. }
  266. data->trig->ops = &lmp91000_trigger_ops;
  267. data->trig->dev.parent = dev;
  268. init_completion(&data->completion);
  269. ret = lmp91000_read_config(data);
  270. if (ret)
  271. return ret;
  272. ret = iio_trigger_set_immutable(iio_channel_cb_get_iio_dev(data->cb_buffer),
  273. data->trig);
  274. if (ret) {
  275. dev_err(dev, "cannot set immutable trigger.\n");
  276. return ret;
  277. }
  278. ret = iio_trigger_register(data->trig);
  279. if (ret) {
  280. dev_err(dev, "cannot register iio trigger.\n");
  281. return ret;
  282. }
  283. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  284. &lmp91000_buffer_handler,
  285. &lmp91000_buffer_setup_ops);
  286. if (ret)
  287. goto error_unreg_trigger;
  288. data->cb_buffer = iio_channel_get_all_cb(dev, &lmp91000_buffer_cb,
  289. indio_dev);
  290. if (IS_ERR(data->cb_buffer)) {
  291. if (PTR_ERR(data->cb_buffer) == -ENODEV)
  292. ret = -EPROBE_DEFER;
  293. else
  294. ret = PTR_ERR(data->cb_buffer);
  295. goto error_unreg_buffer;
  296. }
  297. data->adc_chan = iio_channel_cb_get_channels(data->cb_buffer);
  298. ret = iio_device_register(indio_dev);
  299. if (ret)
  300. goto error_unreg_cb_buffer;
  301. return 0;
  302. error_unreg_cb_buffer:
  303. iio_channel_release_all_cb(data->cb_buffer);
  304. error_unreg_buffer:
  305. iio_triggered_buffer_cleanup(indio_dev);
  306. error_unreg_trigger:
  307. iio_trigger_unregister(data->trig);
  308. return ret;
  309. }
  310. static int lmp91000_remove(struct i2c_client *client)
  311. {
  312. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  313. struct lmp91000_data *data = iio_priv(indio_dev);
  314. iio_device_unregister(indio_dev);
  315. iio_channel_stop_all_cb(data->cb_buffer);
  316. iio_channel_release_all_cb(data->cb_buffer);
  317. iio_triggered_buffer_cleanup(indio_dev);
  318. iio_trigger_unregister(data->trig);
  319. return 0;
  320. }
  321. static const struct of_device_id lmp91000_of_match[] = {
  322. { .compatible = "ti,lmp91000", },
  323. { .compatible = "ti,lmp91002", },
  324. { },
  325. };
  326. MODULE_DEVICE_TABLE(of, lmp91000_of_match);
  327. static const struct i2c_device_id lmp91000_id[] = {
  328. { "lmp91000", 0 },
  329. { "lmp91002", 0 },
  330. {}
  331. };
  332. MODULE_DEVICE_TABLE(i2c, lmp91000_id);
  333. static struct i2c_driver lmp91000_driver = {
  334. .driver = {
  335. .name = LMP91000_DRV_NAME,
  336. .of_match_table = lmp91000_of_match,
  337. },
  338. .probe = lmp91000_probe,
  339. .remove = lmp91000_remove,
  340. .id_table = lmp91000_id,
  341. };
  342. module_i2c_driver(lmp91000_driver);
  343. MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
  344. MODULE_DESCRIPTION("LMP91000 digital potentiostat");
  345. MODULE_LICENSE("GPL");