ti_am335x_adc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * TI ADC MFD driver
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/err.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/io.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/iio/machine.h>
  27. #include <linux/iio/driver.h>
  28. #include <linux/mfd/ti_am335x_tscadc.h>
  29. #include <linux/iio/buffer.h>
  30. #include <linux/iio/kfifo_buf.h>
  31. struct tiadc_device {
  32. struct ti_tscadc_dev *mfd_tscadc;
  33. int channels;
  34. u8 channel_line[8];
  35. u8 channel_step[8];
  36. int buffer_en_ch_steps;
  37. u16 data[8];
  38. };
  39. static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
  40. {
  41. return readl(adc->mfd_tscadc->tscadc_base + reg);
  42. }
  43. static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
  44. unsigned int val)
  45. {
  46. writel(val, adc->mfd_tscadc->tscadc_base + reg);
  47. }
  48. static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
  49. {
  50. u32 step_en;
  51. step_en = ((1 << adc_dev->channels) - 1);
  52. step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
  53. return step_en;
  54. }
  55. static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
  56. {
  57. return 1 << adc_dev->channel_step[chan];
  58. }
  59. static void tiadc_step_config(struct iio_dev *indio_dev)
  60. {
  61. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  62. unsigned int stepconfig;
  63. int i, steps;
  64. /*
  65. * There are 16 configurable steps and 8 analog input
  66. * lines available which are shared between Touchscreen and ADC.
  67. *
  68. * Steps backwards i.e. from 16 towards 0 are used by ADC
  69. * depending on number of input lines needed.
  70. * Channel would represent which analog input
  71. * needs to be given to ADC to digitalize data.
  72. */
  73. steps = TOTAL_STEPS - adc_dev->channels;
  74. if (iio_buffer_enabled(indio_dev))
  75. stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1
  76. | STEPCONFIG_MODE_SWCNT;
  77. else
  78. stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
  79. for (i = 0; i < adc_dev->channels; i++) {
  80. int chan;
  81. chan = adc_dev->channel_line[i];
  82. tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
  83. stepconfig | STEPCONFIG_INP(chan));
  84. tiadc_writel(adc_dev, REG_STEPDELAY(steps),
  85. STEPCONFIG_OPENDLY);
  86. adc_dev->channel_step[i] = steps;
  87. steps++;
  88. }
  89. }
  90. static irqreturn_t tiadc_irq_h(int irq, void *private)
  91. {
  92. struct iio_dev *indio_dev = private;
  93. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  94. unsigned int status, config;
  95. status = tiadc_readl(adc_dev, REG_IRQSTATUS);
  96. /*
  97. * ADC and touchscreen share the IRQ line.
  98. * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
  99. */
  100. if (status & IRQENB_FIFO1OVRRUN) {
  101. /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
  102. config = tiadc_readl(adc_dev, REG_CTRL);
  103. config &= ~(CNTRLREG_TSCSSENB);
  104. tiadc_writel(adc_dev, REG_CTRL, config);
  105. tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
  106. | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
  107. tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
  108. return IRQ_HANDLED;
  109. } else if (status & IRQENB_FIFO1THRES) {
  110. /* Disable irq and wake worker thread */
  111. tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
  112. return IRQ_WAKE_THREAD;
  113. }
  114. return IRQ_NONE;
  115. }
  116. static irqreturn_t tiadc_worker_h(int irq, void *private)
  117. {
  118. struct iio_dev *indio_dev = private;
  119. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  120. int i, k, fifo1count, read;
  121. u16 *data = adc_dev->data;
  122. fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
  123. for (k = 0; k < fifo1count; k = k + i) {
  124. for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
  125. read = tiadc_readl(adc_dev, REG_FIFO1);
  126. data[i] = read & FIFOREAD_DATA_MASK;
  127. }
  128. iio_push_to_buffers(indio_dev, (u8 *) data);
  129. }
  130. tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
  131. tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
  132. return IRQ_HANDLED;
  133. }
  134. static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
  135. {
  136. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  137. int i, fifo1count, read;
  138. tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
  139. IRQENB_FIFO1OVRRUN |
  140. IRQENB_FIFO1UNDRFLW));
  141. /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
  142. fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
  143. for (i = 0; i < fifo1count; i++)
  144. read = tiadc_readl(adc_dev, REG_FIFO1);
  145. return 0;
  146. }
  147. static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
  148. {
  149. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  150. struct iio_buffer *buffer = indio_dev->buffer;
  151. unsigned int enb = 0;
  152. u8 bit;
  153. tiadc_step_config(indio_dev);
  154. for_each_set_bit(bit, buffer->scan_mask, adc_dev->channels)
  155. enb |= (get_adc_step_bit(adc_dev, bit) << 1);
  156. adc_dev->buffer_en_ch_steps = enb;
  157. am335x_tsc_se_set(adc_dev->mfd_tscadc, enb);
  158. tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES
  159. | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
  160. tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES
  161. | IRQENB_FIFO1OVRRUN);
  162. return 0;
  163. }
  164. static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
  165. {
  166. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  167. int fifo1count, i, read;
  168. tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
  169. IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
  170. am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
  171. /* Flush FIFO of leftover data in the time it takes to disable adc */
  172. fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
  173. for (i = 0; i < fifo1count; i++)
  174. read = tiadc_readl(adc_dev, REG_FIFO1);
  175. return 0;
  176. }
  177. static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
  178. {
  179. tiadc_step_config(indio_dev);
  180. return 0;
  181. }
  182. static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
  183. .preenable = &tiadc_buffer_preenable,
  184. .postenable = &tiadc_buffer_postenable,
  185. .predisable = &tiadc_buffer_predisable,
  186. .postdisable = &tiadc_buffer_postdisable,
  187. };
  188. static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
  189. irqreturn_t (*pollfunc_bh)(int irq, void *p),
  190. irqreturn_t (*pollfunc_th)(int irq, void *p),
  191. int irq,
  192. unsigned long flags,
  193. const struct iio_buffer_setup_ops *setup_ops)
  194. {
  195. int ret;
  196. indio_dev->buffer = iio_kfifo_allocate(indio_dev);
  197. if (!indio_dev->buffer)
  198. return -ENOMEM;
  199. ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
  200. flags, indio_dev->name, indio_dev);
  201. if (ret)
  202. goto error_kfifo_free;
  203. indio_dev->setup_ops = setup_ops;
  204. indio_dev->modes |= INDIO_BUFFER_HARDWARE;
  205. ret = iio_buffer_register(indio_dev,
  206. indio_dev->channels,
  207. indio_dev->num_channels);
  208. if (ret)
  209. goto error_free_irq;
  210. return 0;
  211. error_free_irq:
  212. free_irq(irq, indio_dev);
  213. error_kfifo_free:
  214. iio_kfifo_free(indio_dev->buffer);
  215. return ret;
  216. }
  217. static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
  218. {
  219. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  220. free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
  221. iio_kfifo_free(indio_dev->buffer);
  222. iio_buffer_unregister(indio_dev);
  223. }
  224. static const char * const chan_name_ain[] = {
  225. "AIN0",
  226. "AIN1",
  227. "AIN2",
  228. "AIN3",
  229. "AIN4",
  230. "AIN5",
  231. "AIN6",
  232. "AIN7",
  233. };
  234. static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
  235. {
  236. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  237. struct iio_chan_spec *chan_array;
  238. struct iio_chan_spec *chan;
  239. int i;
  240. indio_dev->num_channels = channels;
  241. chan_array = kcalloc(channels,
  242. sizeof(struct iio_chan_spec), GFP_KERNEL);
  243. if (chan_array == NULL)
  244. return -ENOMEM;
  245. chan = chan_array;
  246. for (i = 0; i < channels; i++, chan++) {
  247. chan->type = IIO_VOLTAGE;
  248. chan->indexed = 1;
  249. chan->channel = adc_dev->channel_line[i];
  250. chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  251. chan->datasheet_name = chan_name_ain[chan->channel];
  252. chan->scan_index = i;
  253. chan->scan_type.sign = 'u';
  254. chan->scan_type.realbits = 12;
  255. chan->scan_type.storagebits = 16;
  256. }
  257. indio_dev->channels = chan_array;
  258. return 0;
  259. }
  260. static void tiadc_channels_remove(struct iio_dev *indio_dev)
  261. {
  262. kfree(indio_dev->channels);
  263. }
  264. static int tiadc_read_raw(struct iio_dev *indio_dev,
  265. struct iio_chan_spec const *chan,
  266. int *val, int *val2, long mask)
  267. {
  268. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  269. int i, map_val;
  270. unsigned int fifo1count, read, stepid;
  271. bool found = false;
  272. u32 step_en;
  273. unsigned long timeout = jiffies + usecs_to_jiffies
  274. (IDLE_TIMEOUT * adc_dev->channels);
  275. if (iio_buffer_enabled(indio_dev))
  276. return -EBUSY;
  277. step_en = get_adc_step_mask(adc_dev);
  278. am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
  279. /* Wait for ADC sequencer to complete sampling */
  280. while (tiadc_readl(adc_dev, REG_ADCFSM) & SEQ_STATUS) {
  281. if (time_after(jiffies, timeout))
  282. return -EAGAIN;
  283. }
  284. map_val = chan->channel + TOTAL_CHANNELS;
  285. /*
  286. * When the sub-system is first enabled,
  287. * the sequencer will always start with the
  288. * lowest step (1) and continue until step (16).
  289. * For ex: If we have enabled 4 ADC channels and
  290. * currently use only 1 out of them, the
  291. * sequencer still configures all the 4 steps,
  292. * leading to 3 unwanted data.
  293. * Hence we need to flush out this data.
  294. */
  295. fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
  296. for (i = 0; i < fifo1count; i++) {
  297. read = tiadc_readl(adc_dev, REG_FIFO1);
  298. stepid = read & FIFOREAD_CHNLID_MASK;
  299. stepid = stepid >> 0x10;
  300. if (stepid == map_val) {
  301. read = read & FIFOREAD_DATA_MASK;
  302. found = true;
  303. *val = (u16) read;
  304. }
  305. }
  306. if (found == false)
  307. return -EBUSY;
  308. return IIO_VAL_INT;
  309. }
  310. static const struct iio_info tiadc_info = {
  311. .read_raw = &tiadc_read_raw,
  312. .driver_module = THIS_MODULE,
  313. };
  314. static int tiadc_probe(struct platform_device *pdev)
  315. {
  316. struct iio_dev *indio_dev;
  317. struct tiadc_device *adc_dev;
  318. struct device_node *node = pdev->dev.of_node;
  319. struct property *prop;
  320. const __be32 *cur;
  321. int err;
  322. u32 val;
  323. int channels = 0;
  324. if (!node) {
  325. dev_err(&pdev->dev, "Could not find valid DT data.\n");
  326. return -EINVAL;
  327. }
  328. indio_dev = devm_iio_device_alloc(&pdev->dev,
  329. sizeof(struct tiadc_device));
  330. if (indio_dev == NULL) {
  331. dev_err(&pdev->dev, "failed to allocate iio device\n");
  332. return -ENOMEM;
  333. }
  334. adc_dev = iio_priv(indio_dev);
  335. adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
  336. of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
  337. adc_dev->channel_line[channels] = val;
  338. channels++;
  339. }
  340. adc_dev->channels = channels;
  341. indio_dev->dev.parent = &pdev->dev;
  342. indio_dev->name = dev_name(&pdev->dev);
  343. indio_dev->modes = INDIO_DIRECT_MODE;
  344. indio_dev->info = &tiadc_info;
  345. tiadc_step_config(indio_dev);
  346. tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
  347. err = tiadc_channel_init(indio_dev, adc_dev->channels);
  348. if (err < 0)
  349. return err;
  350. err = tiadc_iio_buffered_hardware_setup(indio_dev,
  351. &tiadc_worker_h,
  352. &tiadc_irq_h,
  353. adc_dev->mfd_tscadc->irq,
  354. IRQF_SHARED,
  355. &tiadc_buffer_setup_ops);
  356. if (err)
  357. goto err_free_channels;
  358. err = iio_device_register(indio_dev);
  359. if (err)
  360. goto err_buffer_unregister;
  361. platform_set_drvdata(pdev, indio_dev);
  362. return 0;
  363. err_buffer_unregister:
  364. tiadc_iio_buffered_hardware_remove(indio_dev);
  365. err_free_channels:
  366. tiadc_channels_remove(indio_dev);
  367. return err;
  368. }
  369. static int tiadc_remove(struct platform_device *pdev)
  370. {
  371. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  372. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  373. u32 step_en;
  374. iio_device_unregister(indio_dev);
  375. tiadc_iio_buffered_hardware_remove(indio_dev);
  376. tiadc_channels_remove(indio_dev);
  377. step_en = get_adc_step_mask(adc_dev);
  378. am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
  379. return 0;
  380. }
  381. #ifdef CONFIG_PM
  382. static int tiadc_suspend(struct device *dev)
  383. {
  384. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  385. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  386. struct ti_tscadc_dev *tscadc_dev;
  387. unsigned int idle;
  388. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  389. if (!device_may_wakeup(tscadc_dev->dev)) {
  390. idle = tiadc_readl(adc_dev, REG_CTRL);
  391. idle &= ~(CNTRLREG_TSCSSENB);
  392. tiadc_writel(adc_dev, REG_CTRL, (idle |
  393. CNTRLREG_POWERDOWN));
  394. }
  395. return 0;
  396. }
  397. static int tiadc_resume(struct device *dev)
  398. {
  399. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  400. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  401. unsigned int restore;
  402. /* Make sure ADC is powered up */
  403. restore = tiadc_readl(adc_dev, REG_CTRL);
  404. restore &= ~(CNTRLREG_POWERDOWN);
  405. tiadc_writel(adc_dev, REG_CTRL, restore);
  406. tiadc_step_config(indio_dev);
  407. return 0;
  408. }
  409. static const struct dev_pm_ops tiadc_pm_ops = {
  410. .suspend = tiadc_suspend,
  411. .resume = tiadc_resume,
  412. };
  413. #define TIADC_PM_OPS (&tiadc_pm_ops)
  414. #else
  415. #define TIADC_PM_OPS NULL
  416. #endif
  417. static const struct of_device_id ti_adc_dt_ids[] = {
  418. { .compatible = "ti,am3359-adc", },
  419. { }
  420. };
  421. MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
  422. static struct platform_driver tiadc_driver = {
  423. .driver = {
  424. .name = "TI-am335x-adc",
  425. .owner = THIS_MODULE,
  426. .pm = TIADC_PM_OPS,
  427. .of_match_table = ti_adc_dt_ids,
  428. },
  429. .probe = tiadc_probe,
  430. .remove = tiadc_remove,
  431. };
  432. module_platform_driver(tiadc_driver);
  433. MODULE_DESCRIPTION("TI ADC controller driver");
  434. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  435. MODULE_LICENSE("GPL");