ti-adc12138.c 13 KB

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