ti-ads8688.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * Copyright (C) 2015 Prevas A/S
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/buffer.h>
  19. #include <linux/iio/trigger_consumer.h>
  20. #include <linux/iio/triggered_buffer.h>
  21. #include <linux/iio/sysfs.h>
  22. #define ADS8688_CMD_REG(x) (x << 8)
  23. #define ADS8688_CMD_REG_NOOP 0x00
  24. #define ADS8688_CMD_REG_RST 0x85
  25. #define ADS8688_CMD_REG_MAN_CH(chan) (0xC0 | (4 * chan))
  26. #define ADS8688_CMD_DONT_CARE_BITS 16
  27. #define ADS8688_PROG_REG(x) (x << 9)
  28. #define ADS8688_PROG_REG_RANGE_CH(chan) (0x05 + chan)
  29. #define ADS8688_PROG_WR_BIT BIT(8)
  30. #define ADS8688_PROG_DONT_CARE_BITS 8
  31. #define ADS8688_REG_PLUSMINUS25VREF 0
  32. #define ADS8688_REG_PLUSMINUS125VREF 1
  33. #define ADS8688_REG_PLUSMINUS0625VREF 2
  34. #define ADS8688_REG_PLUS25VREF 5
  35. #define ADS8688_REG_PLUS125VREF 6
  36. #define ADS8688_VREF_MV 4096
  37. #define ADS8688_REALBITS 16
  38. /*
  39. * enum ads8688_range - ADS8688 reference voltage range
  40. * @ADS8688_PLUSMINUS25VREF: Device is configured for input range ±2.5 * VREF
  41. * @ADS8688_PLUSMINUS125VREF: Device is configured for input range ±1.25 * VREF
  42. * @ADS8688_PLUSMINUS0625VREF: Device is configured for input range ±0.625 * VREF
  43. * @ADS8688_PLUS25VREF: Device is configured for input range 0 - 2.5 * VREF
  44. * @ADS8688_PLUS125VREF: Device is configured for input range 0 - 1.25 * VREF
  45. */
  46. enum ads8688_range {
  47. ADS8688_PLUSMINUS25VREF,
  48. ADS8688_PLUSMINUS125VREF,
  49. ADS8688_PLUSMINUS0625VREF,
  50. ADS8688_PLUS25VREF,
  51. ADS8688_PLUS125VREF,
  52. };
  53. struct ads8688_chip_info {
  54. const struct iio_chan_spec *channels;
  55. unsigned int num_channels;
  56. };
  57. struct ads8688_state {
  58. struct mutex lock;
  59. const struct ads8688_chip_info *chip_info;
  60. struct spi_device *spi;
  61. struct regulator *reg;
  62. unsigned int vref_mv;
  63. enum ads8688_range range[8];
  64. union {
  65. __be32 d32;
  66. u8 d8[4];
  67. } data[2] ____cacheline_aligned;
  68. };
  69. enum ads8688_id {
  70. ID_ADS8684,
  71. ID_ADS8688,
  72. };
  73. struct ads8688_ranges {
  74. enum ads8688_range range;
  75. unsigned int scale;
  76. int offset;
  77. u8 reg;
  78. };
  79. static const struct ads8688_ranges ads8688_range_def[5] = {
  80. {
  81. .range = ADS8688_PLUSMINUS25VREF,
  82. .scale = 76295,
  83. .offset = -(1 << (ADS8688_REALBITS - 1)),
  84. .reg = ADS8688_REG_PLUSMINUS25VREF,
  85. }, {
  86. .range = ADS8688_PLUSMINUS125VREF,
  87. .scale = 38148,
  88. .offset = -(1 << (ADS8688_REALBITS - 1)),
  89. .reg = ADS8688_REG_PLUSMINUS125VREF,
  90. }, {
  91. .range = ADS8688_PLUSMINUS0625VREF,
  92. .scale = 19074,
  93. .offset = -(1 << (ADS8688_REALBITS - 1)),
  94. .reg = ADS8688_REG_PLUSMINUS0625VREF,
  95. }, {
  96. .range = ADS8688_PLUS25VREF,
  97. .scale = 38148,
  98. .offset = 0,
  99. .reg = ADS8688_REG_PLUS25VREF,
  100. }, {
  101. .range = ADS8688_PLUS125VREF,
  102. .scale = 19074,
  103. .offset = 0,
  104. .reg = ADS8688_REG_PLUS125VREF,
  105. }
  106. };
  107. static ssize_t ads8688_show_scales(struct device *dev,
  108. struct device_attribute *attr, char *buf)
  109. {
  110. struct ads8688_state *st = iio_priv(dev_to_iio_dev(dev));
  111. return sprintf(buf, "0.%09u 0.%09u 0.%09u\n",
  112. ads8688_range_def[0].scale * st->vref_mv,
  113. ads8688_range_def[1].scale * st->vref_mv,
  114. ads8688_range_def[2].scale * st->vref_mv);
  115. }
  116. static ssize_t ads8688_show_offsets(struct device *dev,
  117. struct device_attribute *attr, char *buf)
  118. {
  119. return sprintf(buf, "%d %d\n", ads8688_range_def[0].offset,
  120. ads8688_range_def[3].offset);
  121. }
  122. static IIO_DEVICE_ATTR(in_voltage_scale_available, S_IRUGO,
  123. ads8688_show_scales, NULL, 0);
  124. static IIO_DEVICE_ATTR(in_voltage_offset_available, S_IRUGO,
  125. ads8688_show_offsets, NULL, 0);
  126. static struct attribute *ads8688_attributes[] = {
  127. &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
  128. &iio_dev_attr_in_voltage_offset_available.dev_attr.attr,
  129. NULL,
  130. };
  131. static const struct attribute_group ads8688_attribute_group = {
  132. .attrs = ads8688_attributes,
  133. };
  134. #define ADS8688_CHAN(index) \
  135. { \
  136. .type = IIO_VOLTAGE, \
  137. .indexed = 1, \
  138. .channel = index, \
  139. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
  140. | BIT(IIO_CHAN_INFO_SCALE) \
  141. | BIT(IIO_CHAN_INFO_OFFSET), \
  142. .scan_index = index, \
  143. .scan_type = { \
  144. .sign = 'u', \
  145. .realbits = 16, \
  146. .storagebits = 16, \
  147. .endianness = IIO_BE, \
  148. }, \
  149. }
  150. static const struct iio_chan_spec ads8684_channels[] = {
  151. ADS8688_CHAN(0),
  152. ADS8688_CHAN(1),
  153. ADS8688_CHAN(2),
  154. ADS8688_CHAN(3),
  155. };
  156. static const struct iio_chan_spec ads8688_channels[] = {
  157. ADS8688_CHAN(0),
  158. ADS8688_CHAN(1),
  159. ADS8688_CHAN(2),
  160. ADS8688_CHAN(3),
  161. ADS8688_CHAN(4),
  162. ADS8688_CHAN(5),
  163. ADS8688_CHAN(6),
  164. ADS8688_CHAN(7),
  165. };
  166. static int ads8688_prog_write(struct iio_dev *indio_dev, unsigned int addr,
  167. unsigned int val)
  168. {
  169. struct ads8688_state *st = iio_priv(indio_dev);
  170. u32 tmp;
  171. tmp = ADS8688_PROG_REG(addr) | ADS8688_PROG_WR_BIT | val;
  172. tmp <<= ADS8688_PROG_DONT_CARE_BITS;
  173. st->data[0].d32 = cpu_to_be32(tmp);
  174. return spi_write(st->spi, &st->data[0].d8[1], 3);
  175. }
  176. static int ads8688_reset(struct iio_dev *indio_dev)
  177. {
  178. struct ads8688_state *st = iio_priv(indio_dev);
  179. u32 tmp;
  180. tmp = ADS8688_CMD_REG(ADS8688_CMD_REG_RST);
  181. tmp <<= ADS8688_CMD_DONT_CARE_BITS;
  182. st->data[0].d32 = cpu_to_be32(tmp);
  183. return spi_write(st->spi, &st->data[0].d8[0], 4);
  184. }
  185. static int ads8688_read(struct iio_dev *indio_dev, unsigned int chan)
  186. {
  187. struct ads8688_state *st = iio_priv(indio_dev);
  188. int ret;
  189. u32 tmp;
  190. struct spi_transfer t[] = {
  191. {
  192. .tx_buf = &st->data[0].d8[0],
  193. .len = 4,
  194. .cs_change = 1,
  195. }, {
  196. .tx_buf = &st->data[1].d8[0],
  197. .rx_buf = &st->data[1].d8[0],
  198. .len = 4,
  199. },
  200. };
  201. tmp = ADS8688_CMD_REG(ADS8688_CMD_REG_MAN_CH(chan));
  202. tmp <<= ADS8688_CMD_DONT_CARE_BITS;
  203. st->data[0].d32 = cpu_to_be32(tmp);
  204. tmp = ADS8688_CMD_REG(ADS8688_CMD_REG_NOOP);
  205. tmp <<= ADS8688_CMD_DONT_CARE_BITS;
  206. st->data[1].d32 = cpu_to_be32(tmp);
  207. ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
  208. if (ret < 0)
  209. return ret;
  210. return be32_to_cpu(st->data[1].d32) & 0xffff;
  211. }
  212. static int ads8688_read_raw(struct iio_dev *indio_dev,
  213. struct iio_chan_spec const *chan,
  214. int *val, int *val2, long m)
  215. {
  216. int ret, offset;
  217. unsigned long scale_mv;
  218. struct ads8688_state *st = iio_priv(indio_dev);
  219. mutex_lock(&st->lock);
  220. switch (m) {
  221. case IIO_CHAN_INFO_RAW:
  222. ret = ads8688_read(indio_dev, chan->channel);
  223. mutex_unlock(&st->lock);
  224. if (ret < 0)
  225. return ret;
  226. *val = ret;
  227. return IIO_VAL_INT;
  228. case IIO_CHAN_INFO_SCALE:
  229. scale_mv = st->vref_mv;
  230. scale_mv *= ads8688_range_def[st->range[chan->channel]].scale;
  231. *val = 0;
  232. *val2 = scale_mv;
  233. mutex_unlock(&st->lock);
  234. return IIO_VAL_INT_PLUS_NANO;
  235. case IIO_CHAN_INFO_OFFSET:
  236. offset = ads8688_range_def[st->range[chan->channel]].offset;
  237. *val = offset;
  238. mutex_unlock(&st->lock);
  239. return IIO_VAL_INT;
  240. }
  241. mutex_unlock(&st->lock);
  242. return -EINVAL;
  243. }
  244. static int ads8688_write_reg_range(struct iio_dev *indio_dev,
  245. struct iio_chan_spec const *chan,
  246. enum ads8688_range range)
  247. {
  248. unsigned int tmp;
  249. int ret;
  250. tmp = ADS8688_PROG_REG_RANGE_CH(chan->channel);
  251. ret = ads8688_prog_write(indio_dev, tmp, range);
  252. return ret;
  253. }
  254. static int ads8688_write_raw(struct iio_dev *indio_dev,
  255. struct iio_chan_spec const *chan,
  256. int val, int val2, long mask)
  257. {
  258. struct ads8688_state *st = iio_priv(indio_dev);
  259. unsigned int scale = 0;
  260. int ret = -EINVAL, i, offset = 0;
  261. mutex_lock(&st->lock);
  262. switch (mask) {
  263. case IIO_CHAN_INFO_SCALE:
  264. /* If the offset is 0 the ±2.5 * VREF mode is not available */
  265. offset = ads8688_range_def[st->range[chan->channel]].offset;
  266. if (offset == 0 && val2 == ads8688_range_def[0].scale * st->vref_mv) {
  267. mutex_unlock(&st->lock);
  268. return -EINVAL;
  269. }
  270. /* Lookup new mode */
  271. for (i = 0; i < ARRAY_SIZE(ads8688_range_def); i++)
  272. if (val2 == ads8688_range_def[i].scale * st->vref_mv &&
  273. offset == ads8688_range_def[i].offset) {
  274. ret = ads8688_write_reg_range(indio_dev, chan,
  275. ads8688_range_def[i].reg);
  276. break;
  277. }
  278. break;
  279. case IIO_CHAN_INFO_OFFSET:
  280. /*
  281. * There are only two available offsets:
  282. * 0 and -(1 << (ADS8688_REALBITS - 1))
  283. */
  284. if (!(ads8688_range_def[0].offset == val ||
  285. ads8688_range_def[3].offset == val)) {
  286. mutex_unlock(&st->lock);
  287. return -EINVAL;
  288. }
  289. /*
  290. * If the device are in ±2.5 * VREF mode, it's not allowed to
  291. * switch to a mode where the offset is 0
  292. */
  293. if (val == 0 &&
  294. st->range[chan->channel] == ADS8688_PLUSMINUS25VREF) {
  295. mutex_unlock(&st->lock);
  296. return -EINVAL;
  297. }
  298. scale = ads8688_range_def[st->range[chan->channel]].scale;
  299. /* Lookup new mode */
  300. for (i = 0; i < ARRAY_SIZE(ads8688_range_def); i++)
  301. if (val == ads8688_range_def[i].offset &&
  302. scale == ads8688_range_def[i].scale) {
  303. ret = ads8688_write_reg_range(indio_dev, chan,
  304. ads8688_range_def[i].reg);
  305. break;
  306. }
  307. break;
  308. }
  309. if (!ret)
  310. st->range[chan->channel] = ads8688_range_def[i].range;
  311. mutex_unlock(&st->lock);
  312. return ret;
  313. }
  314. static int ads8688_write_raw_get_fmt(struct iio_dev *indio_dev,
  315. struct iio_chan_spec const *chan,
  316. long mask)
  317. {
  318. switch (mask) {
  319. case IIO_CHAN_INFO_SCALE:
  320. return IIO_VAL_INT_PLUS_NANO;
  321. case IIO_CHAN_INFO_OFFSET:
  322. return IIO_VAL_INT;
  323. }
  324. return -EINVAL;
  325. }
  326. static const struct iio_info ads8688_info = {
  327. .read_raw = &ads8688_read_raw,
  328. .write_raw = &ads8688_write_raw,
  329. .write_raw_get_fmt = &ads8688_write_raw_get_fmt,
  330. .attrs = &ads8688_attribute_group,
  331. };
  332. static irqreturn_t ads8688_trigger_handler(int irq, void *p)
  333. {
  334. struct iio_poll_func *pf = p;
  335. struct iio_dev *indio_dev = pf->indio_dev;
  336. u16 buffer[8];
  337. int i, j = 0;
  338. for (i = 0; i < indio_dev->masklength; i++) {
  339. if (!test_bit(i, indio_dev->active_scan_mask))
  340. continue;
  341. buffer[j] = ads8688_read(indio_dev, i);
  342. j++;
  343. }
  344. iio_push_to_buffers_with_timestamp(indio_dev, buffer,
  345. pf->timestamp);
  346. iio_trigger_notify_done(indio_dev->trig);
  347. return IRQ_HANDLED;
  348. }
  349. static const struct ads8688_chip_info ads8688_chip_info_tbl[] = {
  350. [ID_ADS8684] = {
  351. .channels = ads8684_channels,
  352. .num_channels = ARRAY_SIZE(ads8684_channels),
  353. },
  354. [ID_ADS8688] = {
  355. .channels = ads8688_channels,
  356. .num_channels = ARRAY_SIZE(ads8688_channels),
  357. },
  358. };
  359. static int ads8688_probe(struct spi_device *spi)
  360. {
  361. struct ads8688_state *st;
  362. struct iio_dev *indio_dev;
  363. int ret;
  364. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  365. if (indio_dev == NULL)
  366. return -ENOMEM;
  367. st = iio_priv(indio_dev);
  368. st->reg = devm_regulator_get_optional(&spi->dev, "vref");
  369. if (!IS_ERR(st->reg)) {
  370. ret = regulator_enable(st->reg);
  371. if (ret)
  372. return ret;
  373. ret = regulator_get_voltage(st->reg);
  374. if (ret < 0)
  375. goto err_regulator_disable;
  376. st->vref_mv = ret / 1000;
  377. } else {
  378. /* Use internal reference */
  379. st->vref_mv = ADS8688_VREF_MV;
  380. }
  381. st->chip_info = &ads8688_chip_info_tbl[spi_get_device_id(spi)->driver_data];
  382. spi->mode = SPI_MODE_1;
  383. spi_set_drvdata(spi, indio_dev);
  384. st->spi = spi;
  385. indio_dev->name = spi_get_device_id(spi)->name;
  386. indio_dev->dev.parent = &spi->dev;
  387. indio_dev->dev.of_node = spi->dev.of_node;
  388. indio_dev->modes = INDIO_DIRECT_MODE;
  389. indio_dev->channels = st->chip_info->channels;
  390. indio_dev->num_channels = st->chip_info->num_channels;
  391. indio_dev->info = &ads8688_info;
  392. ads8688_reset(indio_dev);
  393. mutex_init(&st->lock);
  394. ret = iio_triggered_buffer_setup(indio_dev, NULL, ads8688_trigger_handler, NULL);
  395. if (ret < 0) {
  396. dev_err(&spi->dev, "iio triggered buffer setup failed\n");
  397. goto err_regulator_disable;
  398. }
  399. ret = iio_device_register(indio_dev);
  400. if (ret)
  401. goto err_buffer_cleanup;
  402. return 0;
  403. err_buffer_cleanup:
  404. iio_triggered_buffer_cleanup(indio_dev);
  405. err_regulator_disable:
  406. if (!IS_ERR(st->reg))
  407. regulator_disable(st->reg);
  408. return ret;
  409. }
  410. static int ads8688_remove(struct spi_device *spi)
  411. {
  412. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  413. struct ads8688_state *st = iio_priv(indio_dev);
  414. iio_device_unregister(indio_dev);
  415. iio_triggered_buffer_cleanup(indio_dev);
  416. if (!IS_ERR(st->reg))
  417. regulator_disable(st->reg);
  418. return 0;
  419. }
  420. static const struct spi_device_id ads8688_id[] = {
  421. {"ads8684", ID_ADS8684},
  422. {"ads8688", ID_ADS8688},
  423. {}
  424. };
  425. MODULE_DEVICE_TABLE(spi, ads8688_id);
  426. static const struct of_device_id ads8688_of_match[] = {
  427. { .compatible = "ti,ads8684" },
  428. { .compatible = "ti,ads8688" },
  429. { }
  430. };
  431. MODULE_DEVICE_TABLE(of, ads8688_of_match);
  432. static struct spi_driver ads8688_driver = {
  433. .driver = {
  434. .name = "ads8688",
  435. },
  436. .probe = ads8688_probe,
  437. .remove = ads8688_remove,
  438. .id_table = ads8688_id,
  439. };
  440. module_spi_driver(ads8688_driver);
  441. MODULE_AUTHOR("Sean Nyekjaer <sean.nyekjaer@prevas.dk>");
  442. MODULE_DESCRIPTION("Texas Instruments ADS8688 driver");
  443. MODULE_LICENSE("GPL v2");