ti-adc12138.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ADC12130/ADC12132/ADC12138 12-bit plus sign ADC driver
  4. *
  5. * Copyright (c) 2016 Akinobu Mita <[email protected]>
  6. *
  7. * Datasheet: http://www.ti.com/lit/ds/symlink/adc12138.pdf
  8. */
  9. #include <linux/module.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/completion.h>
  12. #include <linux/clk.h>
  13. #include <linux/property.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/buffer.h>
  17. #include <linux/iio/trigger.h>
  18. #include <linux/iio/triggered_buffer.h>
  19. #include <linux/iio/trigger_consumer.h>
  20. #include <linux/regulator/consumer.h>
  21. #define ADC12138_MODE_AUTO_CAL 0x08
  22. #define ADC12138_MODE_READ_STATUS 0x0c
  23. #define ADC12138_MODE_ACQUISITION_TIME_6 0x0e
  24. #define ADC12138_MODE_ACQUISITION_TIME_10 0x4e
  25. #define ADC12138_MODE_ACQUISITION_TIME_18 0x8e
  26. #define ADC12138_MODE_ACQUISITION_TIME_34 0xce
  27. #define ADC12138_STATUS_CAL BIT(6)
  28. enum {
  29. adc12130,
  30. adc12132,
  31. adc12138,
  32. };
  33. struct adc12138 {
  34. struct spi_device *spi;
  35. unsigned int id;
  36. /* conversion clock */
  37. struct clk *cclk;
  38. /* positive analog voltage reference */
  39. struct regulator *vref_p;
  40. /* negative analog voltage reference */
  41. struct regulator *vref_n;
  42. struct mutex lock;
  43. struct completion complete;
  44. /* The number of cclk periods for the S/H's acquisition time */
  45. unsigned int acquisition_time;
  46. /*
  47. * Maximum size needed: 16x 2 bytes ADC data + 8 bytes timestamp.
  48. * Less may be need if not all channels are enabled, as long as
  49. * the 8 byte alignment of the timestamp is maintained.
  50. */
  51. __be16 data[20] __aligned(8);
  52. u8 tx_buf[2] __aligned(IIO_DMA_MINALIGN);
  53. u8 rx_buf[2];
  54. };
  55. #define ADC12138_VOLTAGE_CHANNEL(chan) \
  56. { \
  57. .type = IIO_VOLTAGE, \
  58. .indexed = 1, \
  59. .channel = chan, \
  60. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  61. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
  62. | BIT(IIO_CHAN_INFO_OFFSET), \
  63. .scan_index = chan, \
  64. .scan_type = { \
  65. .sign = 's', \
  66. .realbits = 13, \
  67. .storagebits = 16, \
  68. .shift = 3, \
  69. .endianness = IIO_BE, \
  70. }, \
  71. }
  72. #define ADC12138_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si) \
  73. { \
  74. .type = IIO_VOLTAGE, \
  75. .indexed = 1, \
  76. .channel = (chan1), \
  77. .channel2 = (chan2), \
  78. .differential = 1, \
  79. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  80. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
  81. | BIT(IIO_CHAN_INFO_OFFSET), \
  82. .scan_index = si, \
  83. .scan_type = { \
  84. .sign = 's', \
  85. .realbits = 13, \
  86. .storagebits = 16, \
  87. .shift = 3, \
  88. .endianness = IIO_BE, \
  89. }, \
  90. }
  91. static const struct iio_chan_spec adc12132_channels[] = {
  92. ADC12138_VOLTAGE_CHANNEL(0),
  93. ADC12138_VOLTAGE_CHANNEL(1),
  94. ADC12138_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
  95. ADC12138_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
  96. IIO_CHAN_SOFT_TIMESTAMP(4),
  97. };
  98. static const struct iio_chan_spec adc12138_channels[] = {
  99. ADC12138_VOLTAGE_CHANNEL(0),
  100. ADC12138_VOLTAGE_CHANNEL(1),
  101. ADC12138_VOLTAGE_CHANNEL(2),
  102. ADC12138_VOLTAGE_CHANNEL(3),
  103. ADC12138_VOLTAGE_CHANNEL(4),
  104. ADC12138_VOLTAGE_CHANNEL(5),
  105. ADC12138_VOLTAGE_CHANNEL(6),
  106. ADC12138_VOLTAGE_CHANNEL(7),
  107. ADC12138_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
  108. ADC12138_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
  109. ADC12138_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
  110. ADC12138_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
  111. ADC12138_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
  112. ADC12138_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
  113. ADC12138_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
  114. ADC12138_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
  115. IIO_CHAN_SOFT_TIMESTAMP(16),
  116. };
  117. static int adc12138_mode_programming(struct adc12138 *adc, u8 mode,
  118. void *rx_buf, int len)
  119. {
  120. struct spi_transfer xfer = {
  121. .tx_buf = adc->tx_buf,
  122. .rx_buf = adc->rx_buf,
  123. .len = len,
  124. };
  125. int ret;
  126. /* Skip unused bits for ADC12130 and ADC12132 */
  127. if (adc->id != adc12138)
  128. mode = (mode & 0xc0) | ((mode & 0x0f) << 2);
  129. adc->tx_buf[0] = mode;
  130. ret = spi_sync_transfer(adc->spi, &xfer, 1);
  131. if (ret)
  132. return ret;
  133. memcpy(rx_buf, adc->rx_buf, len);
  134. return 0;
  135. }
  136. static int adc12138_read_status(struct adc12138 *adc)
  137. {
  138. u8 rx_buf[2];
  139. int ret;
  140. ret = adc12138_mode_programming(adc, ADC12138_MODE_READ_STATUS,
  141. rx_buf, 2);
  142. if (ret)
  143. return ret;
  144. return (rx_buf[0] << 1) | (rx_buf[1] >> 7);
  145. }
  146. static int __adc12138_start_conv(struct adc12138 *adc,
  147. struct iio_chan_spec const *channel,
  148. void *data, int len)
  149. {
  150. static const u8 ch_to_mux[] = { 0, 4, 1, 5, 2, 6, 3, 7 };
  151. u8 mode = (ch_to_mux[channel->channel] << 4) |
  152. (channel->differential ? 0 : 0x80);
  153. return adc12138_mode_programming(adc, mode, data, len);
  154. }
  155. static int adc12138_start_conv(struct adc12138 *adc,
  156. struct iio_chan_spec const *channel)
  157. {
  158. u8 trash;
  159. return __adc12138_start_conv(adc, channel, &trash, 1);
  160. }
  161. static int adc12138_start_and_read_conv(struct adc12138 *adc,
  162. struct iio_chan_spec const *channel,
  163. __be16 *data)
  164. {
  165. return __adc12138_start_conv(adc, channel, data, 2);
  166. }
  167. static int adc12138_read_conv_data(struct adc12138 *adc, __be16 *value)
  168. {
  169. /* Issue a read status instruction and read previous conversion data */
  170. return adc12138_mode_programming(adc, ADC12138_MODE_READ_STATUS,
  171. value, sizeof(*value));
  172. }
  173. static int adc12138_wait_eoc(struct adc12138 *adc, unsigned long timeout)
  174. {
  175. if (!wait_for_completion_timeout(&adc->complete, timeout))
  176. return -ETIMEDOUT;
  177. return 0;
  178. }
  179. static int adc12138_adc_conversion(struct adc12138 *adc,
  180. struct iio_chan_spec const *channel,
  181. __be16 *value)
  182. {
  183. int ret;
  184. reinit_completion(&adc->complete);
  185. ret = adc12138_start_conv(adc, channel);
  186. if (ret)
  187. return ret;
  188. ret = adc12138_wait_eoc(adc, msecs_to_jiffies(100));
  189. if (ret)
  190. return ret;
  191. return adc12138_read_conv_data(adc, value);
  192. }
  193. static int adc12138_read_raw(struct iio_dev *iio,
  194. struct iio_chan_spec const *channel, int *value,
  195. int *shift, long mask)
  196. {
  197. struct adc12138 *adc = iio_priv(iio);
  198. int ret;
  199. __be16 data;
  200. switch (mask) {
  201. case IIO_CHAN_INFO_RAW:
  202. mutex_lock(&adc->lock);
  203. ret = adc12138_adc_conversion(adc, channel, &data);
  204. mutex_unlock(&adc->lock);
  205. if (ret)
  206. return ret;
  207. *value = sign_extend32(be16_to_cpu(data) >> channel->scan_type.shift,
  208. channel->scan_type.realbits - 1);
  209. return IIO_VAL_INT;
  210. case IIO_CHAN_INFO_SCALE:
  211. ret = regulator_get_voltage(adc->vref_p);
  212. if (ret < 0)
  213. return ret;
  214. *value = ret;
  215. if (!IS_ERR(adc->vref_n)) {
  216. ret = regulator_get_voltage(adc->vref_n);
  217. if (ret < 0)
  218. return ret;
  219. *value -= ret;
  220. }
  221. /* convert regulator output voltage to mV */
  222. *value /= 1000;
  223. *shift = channel->scan_type.realbits - 1;
  224. return IIO_VAL_FRACTIONAL_LOG2;
  225. case IIO_CHAN_INFO_OFFSET:
  226. if (!IS_ERR(adc->vref_n)) {
  227. *value = regulator_get_voltage(adc->vref_n);
  228. if (*value < 0)
  229. return *value;
  230. } else {
  231. *value = 0;
  232. }
  233. /* convert regulator output voltage to mV */
  234. *value /= 1000;
  235. return IIO_VAL_INT;
  236. }
  237. return -EINVAL;
  238. }
  239. static const struct iio_info adc12138_info = {
  240. .read_raw = adc12138_read_raw,
  241. };
  242. static int adc12138_init(struct adc12138 *adc)
  243. {
  244. int ret;
  245. int status;
  246. u8 mode;
  247. u8 trash;
  248. reinit_completion(&adc->complete);
  249. ret = adc12138_mode_programming(adc, ADC12138_MODE_AUTO_CAL, &trash, 1);
  250. if (ret)
  251. return ret;
  252. /* data output at this time has no significance */
  253. status = adc12138_read_status(adc);
  254. if (status < 0)
  255. return status;
  256. adc12138_wait_eoc(adc, msecs_to_jiffies(100));
  257. status = adc12138_read_status(adc);
  258. if (status & ADC12138_STATUS_CAL) {
  259. dev_warn(&adc->spi->dev,
  260. "Auto Cal sequence is still in progress: %#x\n",
  261. status);
  262. return -EIO;
  263. }
  264. switch (adc->acquisition_time) {
  265. case 6:
  266. mode = ADC12138_MODE_ACQUISITION_TIME_6;
  267. break;
  268. case 10:
  269. mode = ADC12138_MODE_ACQUISITION_TIME_10;
  270. break;
  271. case 18:
  272. mode = ADC12138_MODE_ACQUISITION_TIME_18;
  273. break;
  274. case 34:
  275. mode = ADC12138_MODE_ACQUISITION_TIME_34;
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. return adc12138_mode_programming(adc, mode, &trash, 1);
  281. }
  282. static irqreturn_t adc12138_trigger_handler(int irq, void *p)
  283. {
  284. struct iio_poll_func *pf = p;
  285. struct iio_dev *indio_dev = pf->indio_dev;
  286. struct adc12138 *adc = iio_priv(indio_dev);
  287. __be16 trash;
  288. int ret;
  289. int scan_index;
  290. int i = 0;
  291. mutex_lock(&adc->lock);
  292. for_each_set_bit(scan_index, indio_dev->active_scan_mask,
  293. indio_dev->masklength) {
  294. const struct iio_chan_spec *scan_chan =
  295. &indio_dev->channels[scan_index];
  296. reinit_completion(&adc->complete);
  297. ret = adc12138_start_and_read_conv(adc, scan_chan,
  298. i ? &adc->data[i - 1] : &trash);
  299. if (ret) {
  300. dev_warn(&adc->spi->dev,
  301. "failed to start conversion\n");
  302. goto out;
  303. }
  304. ret = adc12138_wait_eoc(adc, msecs_to_jiffies(100));
  305. if (ret) {
  306. dev_warn(&adc->spi->dev, "wait eoc timeout\n");
  307. goto out;
  308. }
  309. i++;
  310. }
  311. if (i) {
  312. ret = adc12138_read_conv_data(adc, &adc->data[i - 1]);
  313. if (ret) {
  314. dev_warn(&adc->spi->dev,
  315. "failed to get conversion data\n");
  316. goto out;
  317. }
  318. }
  319. iio_push_to_buffers_with_timestamp(indio_dev, adc->data,
  320. iio_get_time_ns(indio_dev));
  321. out:
  322. mutex_unlock(&adc->lock);
  323. iio_trigger_notify_done(indio_dev->trig);
  324. return IRQ_HANDLED;
  325. }
  326. static irqreturn_t adc12138_eoc_handler(int irq, void *p)
  327. {
  328. struct iio_dev *indio_dev = p;
  329. struct adc12138 *adc = iio_priv(indio_dev);
  330. complete(&adc->complete);
  331. return IRQ_HANDLED;
  332. }
  333. static int adc12138_probe(struct spi_device *spi)
  334. {
  335. struct iio_dev *indio_dev;
  336. struct adc12138 *adc;
  337. int ret;
  338. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
  339. if (!indio_dev)
  340. return -ENOMEM;
  341. adc = iio_priv(indio_dev);
  342. adc->spi = spi;
  343. adc->id = spi_get_device_id(spi)->driver_data;
  344. mutex_init(&adc->lock);
  345. init_completion(&adc->complete);
  346. indio_dev->name = spi_get_device_id(spi)->name;
  347. indio_dev->info = &adc12138_info;
  348. indio_dev->modes = INDIO_DIRECT_MODE;
  349. switch (adc->id) {
  350. case adc12130:
  351. case adc12132:
  352. indio_dev->channels = adc12132_channels;
  353. indio_dev->num_channels = ARRAY_SIZE(adc12132_channels);
  354. break;
  355. case adc12138:
  356. indio_dev->channels = adc12138_channels;
  357. indio_dev->num_channels = ARRAY_SIZE(adc12138_channels);
  358. break;
  359. default:
  360. return -EINVAL;
  361. }
  362. ret = device_property_read_u32(&spi->dev, "ti,acquisition-time",
  363. &adc->acquisition_time);
  364. if (ret)
  365. adc->acquisition_time = 10;
  366. adc->cclk = devm_clk_get(&spi->dev, NULL);
  367. if (IS_ERR(adc->cclk))
  368. return PTR_ERR(adc->cclk);
  369. adc->vref_p = devm_regulator_get(&spi->dev, "vref-p");
  370. if (IS_ERR(adc->vref_p))
  371. return PTR_ERR(adc->vref_p);
  372. adc->vref_n = devm_regulator_get_optional(&spi->dev, "vref-n");
  373. if (IS_ERR(adc->vref_n)) {
  374. /*
  375. * Assume vref_n is 0V if an optional regulator is not
  376. * specified, otherwise return the error code.
  377. */
  378. ret = PTR_ERR(adc->vref_n);
  379. if (ret != -ENODEV)
  380. return ret;
  381. }
  382. ret = devm_request_irq(&spi->dev, spi->irq, adc12138_eoc_handler,
  383. IRQF_TRIGGER_RISING, indio_dev->name, indio_dev);
  384. if (ret)
  385. return ret;
  386. ret = clk_prepare_enable(adc->cclk);
  387. if (ret)
  388. return ret;
  389. ret = regulator_enable(adc->vref_p);
  390. if (ret)
  391. goto err_clk_disable;
  392. if (!IS_ERR(adc->vref_n)) {
  393. ret = regulator_enable(adc->vref_n);
  394. if (ret)
  395. goto err_vref_p_disable;
  396. }
  397. ret = adc12138_init(adc);
  398. if (ret)
  399. goto err_vref_n_disable;
  400. spi_set_drvdata(spi, indio_dev);
  401. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  402. adc12138_trigger_handler, NULL);
  403. if (ret)
  404. goto err_vref_n_disable;
  405. ret = iio_device_register(indio_dev);
  406. if (ret)
  407. goto err_buffer_cleanup;
  408. return 0;
  409. err_buffer_cleanup:
  410. iio_triggered_buffer_cleanup(indio_dev);
  411. err_vref_n_disable:
  412. if (!IS_ERR(adc->vref_n))
  413. regulator_disable(adc->vref_n);
  414. err_vref_p_disable:
  415. regulator_disable(adc->vref_p);
  416. err_clk_disable:
  417. clk_disable_unprepare(adc->cclk);
  418. return ret;
  419. }
  420. static void adc12138_remove(struct spi_device *spi)
  421. {
  422. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  423. struct adc12138 *adc = iio_priv(indio_dev);
  424. iio_device_unregister(indio_dev);
  425. iio_triggered_buffer_cleanup(indio_dev);
  426. if (!IS_ERR(adc->vref_n))
  427. regulator_disable(adc->vref_n);
  428. regulator_disable(adc->vref_p);
  429. clk_disable_unprepare(adc->cclk);
  430. }
  431. static const struct of_device_id adc12138_dt_ids[] = {
  432. { .compatible = "ti,adc12130", },
  433. { .compatible = "ti,adc12132", },
  434. { .compatible = "ti,adc12138", },
  435. {}
  436. };
  437. MODULE_DEVICE_TABLE(of, adc12138_dt_ids);
  438. static const struct spi_device_id adc12138_id[] = {
  439. { "adc12130", adc12130 },
  440. { "adc12132", adc12132 },
  441. { "adc12138", adc12138 },
  442. {}
  443. };
  444. MODULE_DEVICE_TABLE(spi, adc12138_id);
  445. static struct spi_driver adc12138_driver = {
  446. .driver = {
  447. .name = "adc12138",
  448. .of_match_table = adc12138_dt_ids,
  449. },
  450. .probe = adc12138_probe,
  451. .remove = adc12138_remove,
  452. .id_table = adc12138_id,
  453. };
  454. module_spi_driver(adc12138_driver);
  455. MODULE_AUTHOR("Akinobu Mita <[email protected]>");
  456. MODULE_DESCRIPTION("ADC12130/ADC12132/ADC12138 driver");
  457. MODULE_LICENSE("GPL v2");