ti-ads8688.c 12 KB

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