ti-adc12138.c 13 KB

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