ti_am335x_adc.c 14 KB

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