ti_am335x_adc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. #include <linux/dmaengine.h>
  31. #include <linux/dma-mapping.h>
  32. #define DMA_BUFFER_SIZE SZ_2K
  33. struct tiadc_dma {
  34. struct dma_slave_config conf;
  35. struct dma_chan *chan;
  36. dma_addr_t addr;
  37. dma_cookie_t cookie;
  38. u8 *buf;
  39. int current_period;
  40. int period_size;
  41. u8 fifo_thresh;
  42. };
  43. struct tiadc_device {
  44. struct ti_tscadc_dev *mfd_tscadc;
  45. struct tiadc_dma dma;
  46. struct mutex fifo1_lock; /* to protect fifo access */
  47. int channels;
  48. int total_ch_enabled;
  49. u8 channel_line[8];
  50. u8 channel_step[8];
  51. int buffer_en_ch_steps;
  52. u16 data[8];
  53. u32 open_delay[8], sample_delay[8], step_avg[8];
  54. };
  55. static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
  56. {
  57. return readl(adc->mfd_tscadc->tscadc_base + reg);
  58. }
  59. static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
  60. unsigned int val)
  61. {
  62. writel(val, adc->mfd_tscadc->tscadc_base + reg);
  63. }
  64. static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
  65. {
  66. u32 step_en;
  67. step_en = ((1 << adc_dev->channels) - 1);
  68. step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
  69. return step_en;
  70. }
  71. static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev,
  72. struct iio_chan_spec const *chan)
  73. {
  74. int i;
  75. for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
  76. if (chan->channel == adc_dev->channel_line[i]) {
  77. u32 step;
  78. step = adc_dev->channel_step[i];
  79. /* +1 for the charger */
  80. return 1 << (step + 1);
  81. }
  82. }
  83. WARN_ON(1);
  84. return 0;
  85. }
  86. static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
  87. {
  88. return 1 << adc_dev->channel_step[chan];
  89. }
  90. static void tiadc_step_config(struct iio_dev *indio_dev)
  91. {
  92. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  93. struct device *dev = adc_dev->mfd_tscadc->dev;
  94. unsigned int stepconfig;
  95. int i, steps = 0;
  96. /*
  97. * There are 16 configurable steps and 8 analog input
  98. * lines available which are shared between Touchscreen and ADC.
  99. *
  100. * Steps forwards i.e. from 0 towards 16 are used by ADC
  101. * depending on number of input lines needed.
  102. * Channel would represent which analog input
  103. * needs to be given to ADC to digitalize data.
  104. */
  105. for (i = 0; i < adc_dev->channels; i++) {
  106. int chan;
  107. chan = adc_dev->channel_line[i];
  108. if (adc_dev->step_avg[i] > STEPCONFIG_AVG_16) {
  109. dev_warn(dev, "chan %d step_avg truncating to %d\n",
  110. chan, STEPCONFIG_AVG_16);
  111. adc_dev->step_avg[i] = STEPCONFIG_AVG_16;
  112. }
  113. if (adc_dev->step_avg[i])
  114. stepconfig =
  115. STEPCONFIG_AVG(ffs(adc_dev->step_avg[i]) - 1) |
  116. STEPCONFIG_FIFO1;
  117. else
  118. stepconfig = STEPCONFIG_FIFO1;
  119. if (iio_buffer_enabled(indio_dev))
  120. stepconfig |= STEPCONFIG_MODE_SWCNT;
  121. tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
  122. stepconfig | STEPCONFIG_INP(chan));
  123. if (adc_dev->open_delay[i] > STEPDELAY_OPEN_MASK) {
  124. dev_warn(dev, "chan %d open delay truncating to 0x3FFFF\n",
  125. chan);
  126. adc_dev->open_delay[i] = STEPDELAY_OPEN_MASK;
  127. }
  128. if (adc_dev->sample_delay[i] > 0xFF) {
  129. dev_warn(dev, "chan %d sample delay truncating to 0xFF\n",
  130. chan);
  131. adc_dev->sample_delay[i] = 0xFF;
  132. }
  133. tiadc_writel(adc_dev, REG_STEPDELAY(steps),
  134. STEPDELAY_OPEN(adc_dev->open_delay[i]) |
  135. STEPDELAY_SAMPLE(adc_dev->sample_delay[i]));
  136. adc_dev->channel_step[i] = steps;
  137. steps++;
  138. }
  139. }
  140. static irqreturn_t tiadc_irq_h(int irq, void *private)
  141. {
  142. struct iio_dev *indio_dev = private;
  143. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  144. unsigned int status, config;
  145. status = tiadc_readl(adc_dev, REG_IRQSTATUS);
  146. /*
  147. * ADC and touchscreen share the IRQ line.
  148. * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
  149. */
  150. if (status & IRQENB_FIFO1OVRRUN) {
  151. /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
  152. config = tiadc_readl(adc_dev, REG_CTRL);
  153. config &= ~(CNTRLREG_TSCSSENB);
  154. tiadc_writel(adc_dev, REG_CTRL, config);
  155. tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
  156. | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
  157. tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
  158. return IRQ_HANDLED;
  159. } else if (status & IRQENB_FIFO1THRES) {
  160. /* Disable irq and wake worker thread */
  161. tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
  162. return IRQ_WAKE_THREAD;
  163. }
  164. return IRQ_NONE;
  165. }
  166. static irqreturn_t tiadc_worker_h(int irq, void *private)
  167. {
  168. struct iio_dev *indio_dev = private;
  169. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  170. int i, k, fifo1count, read;
  171. u16 *data = adc_dev->data;
  172. fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
  173. for (k = 0; k < fifo1count; k = k + i) {
  174. for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
  175. read = tiadc_readl(adc_dev, REG_FIFO1);
  176. data[i] = read & FIFOREAD_DATA_MASK;
  177. }
  178. iio_push_to_buffers(indio_dev, (u8 *) data);
  179. }
  180. tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
  181. tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
  182. return IRQ_HANDLED;
  183. }
  184. static void tiadc_dma_rx_complete(void *param)
  185. {
  186. struct iio_dev *indio_dev = param;
  187. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  188. struct tiadc_dma *dma = &adc_dev->dma;
  189. u8 *data;
  190. int i;
  191. data = dma->buf + dma->current_period * dma->period_size;
  192. dma->current_period = 1 - dma->current_period; /* swap the buffer ID */
  193. for (i = 0; i < dma->period_size; i += indio_dev->scan_bytes) {
  194. iio_push_to_buffers(indio_dev, data);
  195. data += indio_dev->scan_bytes;
  196. }
  197. }
  198. static int tiadc_start_dma(struct iio_dev *indio_dev)
  199. {
  200. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  201. struct tiadc_dma *dma = &adc_dev->dma;
  202. struct dma_async_tx_descriptor *desc;
  203. dma->current_period = 0; /* We start to fill period 0 */
  204. /*
  205. * Make the fifo thresh as the multiple of total number of
  206. * channels enabled, so make sure that cyclic DMA period
  207. * length is also a multiple of total number of channels
  208. * enabled. This ensures that no invalid data is reported
  209. * to the stack via iio_push_to_buffers().
  210. */
  211. dma->fifo_thresh = rounddown(FIFO1_THRESHOLD + 1,
  212. adc_dev->total_ch_enabled) - 1;
  213. /* Make sure that period length is multiple of fifo thresh level */
  214. dma->period_size = rounddown(DMA_BUFFER_SIZE / 2,
  215. (dma->fifo_thresh + 1) * sizeof(u16));
  216. dma->conf.src_maxburst = dma->fifo_thresh + 1;
  217. dmaengine_slave_config(dma->chan, &dma->conf);
  218. desc = dmaengine_prep_dma_cyclic(dma->chan, dma->addr,
  219. dma->period_size * 2,
  220. dma->period_size, DMA_DEV_TO_MEM,
  221. DMA_PREP_INTERRUPT);
  222. if (!desc)
  223. return -EBUSY;
  224. desc->callback = tiadc_dma_rx_complete;
  225. desc->callback_param = indio_dev;
  226. dma->cookie = dmaengine_submit(desc);
  227. dma_async_issue_pending(dma->chan);
  228. tiadc_writel(adc_dev, REG_FIFO1THR, dma->fifo_thresh);
  229. tiadc_writel(adc_dev, REG_DMA1REQ, dma->fifo_thresh);
  230. tiadc_writel(adc_dev, REG_DMAENABLE_SET, DMA_FIFO1);
  231. return 0;
  232. }
  233. static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
  234. {
  235. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  236. int i, fifo1count, read;
  237. tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
  238. IRQENB_FIFO1OVRRUN |
  239. IRQENB_FIFO1UNDRFLW));
  240. /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
  241. fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
  242. for (i = 0; i < fifo1count; i++)
  243. read = tiadc_readl(adc_dev, REG_FIFO1);
  244. return 0;
  245. }
  246. static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
  247. {
  248. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  249. struct tiadc_dma *dma = &adc_dev->dma;
  250. unsigned int irq_enable;
  251. unsigned int enb = 0;
  252. u8 bit;
  253. tiadc_step_config(indio_dev);
  254. for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels) {
  255. enb |= (get_adc_step_bit(adc_dev, bit) << 1);
  256. adc_dev->total_ch_enabled++;
  257. }
  258. adc_dev->buffer_en_ch_steps = enb;
  259. if (dma->chan)
  260. tiadc_start_dma(indio_dev);
  261. am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
  262. tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES
  263. | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
  264. irq_enable = IRQENB_FIFO1OVRRUN;
  265. if (!dma->chan)
  266. irq_enable |= IRQENB_FIFO1THRES;
  267. tiadc_writel(adc_dev, REG_IRQENABLE, irq_enable);
  268. return 0;
  269. }
  270. static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
  271. {
  272. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  273. struct tiadc_dma *dma = &adc_dev->dma;
  274. int fifo1count, i, read;
  275. tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
  276. IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
  277. am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
  278. adc_dev->buffer_en_ch_steps = 0;
  279. adc_dev->total_ch_enabled = 0;
  280. if (dma->chan) {
  281. tiadc_writel(adc_dev, REG_DMAENABLE_CLEAR, 0x2);
  282. dmaengine_terminate_async(dma->chan);
  283. }
  284. /* Flush FIFO of leftover data in the time it takes to disable adc */
  285. fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
  286. for (i = 0; i < fifo1count; i++)
  287. read = tiadc_readl(adc_dev, REG_FIFO1);
  288. return 0;
  289. }
  290. static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
  291. {
  292. tiadc_step_config(indio_dev);
  293. return 0;
  294. }
  295. static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
  296. .preenable = &tiadc_buffer_preenable,
  297. .postenable = &tiadc_buffer_postenable,
  298. .predisable = &tiadc_buffer_predisable,
  299. .postdisable = &tiadc_buffer_postdisable,
  300. };
  301. static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
  302. irqreturn_t (*pollfunc_bh)(int irq, void *p),
  303. irqreturn_t (*pollfunc_th)(int irq, void *p),
  304. int irq,
  305. unsigned long flags,
  306. const struct iio_buffer_setup_ops *setup_ops)
  307. {
  308. struct iio_buffer *buffer;
  309. int ret;
  310. buffer = iio_kfifo_allocate();
  311. if (!buffer)
  312. return -ENOMEM;
  313. iio_device_attach_buffer(indio_dev, buffer);
  314. ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
  315. flags, indio_dev->name, indio_dev);
  316. if (ret)
  317. goto error_kfifo_free;
  318. indio_dev->setup_ops = setup_ops;
  319. indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
  320. return 0;
  321. error_kfifo_free:
  322. iio_kfifo_free(indio_dev->buffer);
  323. return ret;
  324. }
  325. static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
  326. {
  327. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  328. free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
  329. iio_kfifo_free(indio_dev->buffer);
  330. }
  331. static const char * const chan_name_ain[] = {
  332. "AIN0",
  333. "AIN1",
  334. "AIN2",
  335. "AIN3",
  336. "AIN4",
  337. "AIN5",
  338. "AIN6",
  339. "AIN7",
  340. };
  341. static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
  342. {
  343. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  344. struct iio_chan_spec *chan_array;
  345. struct iio_chan_spec *chan;
  346. int i;
  347. indio_dev->num_channels = channels;
  348. chan_array = kcalloc(channels, sizeof(*chan_array), GFP_KERNEL);
  349. if (chan_array == NULL)
  350. return -ENOMEM;
  351. chan = chan_array;
  352. for (i = 0; i < channels; i++, chan++) {
  353. chan->type = IIO_VOLTAGE;
  354. chan->indexed = 1;
  355. chan->channel = adc_dev->channel_line[i];
  356. chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  357. chan->datasheet_name = chan_name_ain[chan->channel];
  358. chan->scan_index = i;
  359. chan->scan_type.sign = 'u';
  360. chan->scan_type.realbits = 12;
  361. chan->scan_type.storagebits = 16;
  362. }
  363. indio_dev->channels = chan_array;
  364. return 0;
  365. }
  366. static void tiadc_channels_remove(struct iio_dev *indio_dev)
  367. {
  368. kfree(indio_dev->channels);
  369. }
  370. static int tiadc_read_raw(struct iio_dev *indio_dev,
  371. struct iio_chan_spec const *chan,
  372. int *val, int *val2, long mask)
  373. {
  374. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  375. int ret = IIO_VAL_INT;
  376. int i, map_val;
  377. unsigned int fifo1count, read, stepid;
  378. bool found = false;
  379. u32 step_en;
  380. unsigned long timeout;
  381. if (iio_buffer_enabled(indio_dev))
  382. return -EBUSY;
  383. step_en = get_adc_chan_step_mask(adc_dev, chan);
  384. if (!step_en)
  385. return -EINVAL;
  386. mutex_lock(&adc_dev->fifo1_lock);
  387. fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
  388. while (fifo1count--)
  389. tiadc_readl(adc_dev, REG_FIFO1);
  390. am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
  391. timeout = jiffies + msecs_to_jiffies
  392. (IDLE_TIMEOUT * adc_dev->channels);
  393. /* Wait for Fifo threshold interrupt */
  394. while (1) {
  395. fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
  396. if (fifo1count)
  397. break;
  398. if (time_after(jiffies, timeout)) {
  399. am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
  400. ret = -EAGAIN;
  401. goto err_unlock;
  402. }
  403. }
  404. map_val = adc_dev->channel_step[chan->scan_index];
  405. /*
  406. * We check the complete FIFO. We programmed just one entry but in case
  407. * something went wrong we left empty handed (-EAGAIN previously) and
  408. * then the value apeared somehow in the FIFO we would have two entries.
  409. * Therefore we read every item and keep only the latest version of the
  410. * requested channel.
  411. */
  412. for (i = 0; i < fifo1count; i++) {
  413. read = tiadc_readl(adc_dev, REG_FIFO1);
  414. stepid = read & FIFOREAD_CHNLID_MASK;
  415. stepid = stepid >> 0x10;
  416. if (stepid == map_val) {
  417. read = read & FIFOREAD_DATA_MASK;
  418. found = true;
  419. *val = (u16) read;
  420. }
  421. }
  422. am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
  423. if (found == false)
  424. ret = -EBUSY;
  425. err_unlock:
  426. mutex_unlock(&adc_dev->fifo1_lock);
  427. return ret;
  428. }
  429. static const struct iio_info tiadc_info = {
  430. .read_raw = &tiadc_read_raw,
  431. .driver_module = THIS_MODULE,
  432. };
  433. static int tiadc_request_dma(struct platform_device *pdev,
  434. struct tiadc_device *adc_dev)
  435. {
  436. struct tiadc_dma *dma = &adc_dev->dma;
  437. dma_cap_mask_t mask;
  438. /* Default slave configuration parameters */
  439. dma->conf.direction = DMA_DEV_TO_MEM;
  440. dma->conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  441. dma->conf.src_addr = adc_dev->mfd_tscadc->tscadc_phys_base + REG_FIFO1;
  442. dma_cap_zero(mask);
  443. dma_cap_set(DMA_CYCLIC, mask);
  444. /* Get a channel for RX */
  445. dma->chan = dma_request_chan(adc_dev->mfd_tscadc->dev, "fifo1");
  446. if (IS_ERR(dma->chan)) {
  447. int ret = PTR_ERR(dma->chan);
  448. dma->chan = NULL;
  449. return ret;
  450. }
  451. /* RX buffer */
  452. dma->buf = dma_alloc_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE,
  453. &dma->addr, GFP_KERNEL);
  454. if (!dma->buf)
  455. goto err;
  456. return 0;
  457. err:
  458. dma_release_channel(dma->chan);
  459. return -ENOMEM;
  460. }
  461. static int tiadc_parse_dt(struct platform_device *pdev,
  462. struct tiadc_device *adc_dev)
  463. {
  464. struct device_node *node = pdev->dev.of_node;
  465. struct property *prop;
  466. const __be32 *cur;
  467. int channels = 0;
  468. u32 val;
  469. of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
  470. adc_dev->channel_line[channels] = val;
  471. /* Set Default values for optional DT parameters */
  472. adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY;
  473. adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY;
  474. adc_dev->step_avg[channels] = 16;
  475. channels++;
  476. }
  477. of_property_read_u32_array(node, "ti,chan-step-avg",
  478. adc_dev->step_avg, channels);
  479. of_property_read_u32_array(node, "ti,chan-step-opendelay",
  480. adc_dev->open_delay, channels);
  481. of_property_read_u32_array(node, "ti,chan-step-sampledelay",
  482. adc_dev->sample_delay, channels);
  483. adc_dev->channels = channels;
  484. return 0;
  485. }
  486. static int tiadc_probe(struct platform_device *pdev)
  487. {
  488. struct iio_dev *indio_dev;
  489. struct tiadc_device *adc_dev;
  490. struct device_node *node = pdev->dev.of_node;
  491. int err;
  492. if (!node) {
  493. dev_err(&pdev->dev, "Could not find valid DT data.\n");
  494. return -EINVAL;
  495. }
  496. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*indio_dev));
  497. if (indio_dev == NULL) {
  498. dev_err(&pdev->dev, "failed to allocate iio device\n");
  499. return -ENOMEM;
  500. }
  501. adc_dev = iio_priv(indio_dev);
  502. adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
  503. tiadc_parse_dt(pdev, adc_dev);
  504. indio_dev->dev.parent = &pdev->dev;
  505. indio_dev->name = dev_name(&pdev->dev);
  506. indio_dev->modes = INDIO_DIRECT_MODE;
  507. indio_dev->info = &tiadc_info;
  508. tiadc_step_config(indio_dev);
  509. tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
  510. mutex_init(&adc_dev->fifo1_lock);
  511. err = tiadc_channel_init(indio_dev, adc_dev->channels);
  512. if (err < 0)
  513. return err;
  514. err = tiadc_iio_buffered_hardware_setup(indio_dev,
  515. &tiadc_worker_h,
  516. &tiadc_irq_h,
  517. adc_dev->mfd_tscadc->irq,
  518. IRQF_SHARED,
  519. &tiadc_buffer_setup_ops);
  520. if (err)
  521. goto err_free_channels;
  522. err = iio_device_register(indio_dev);
  523. if (err)
  524. goto err_buffer_unregister;
  525. platform_set_drvdata(pdev, indio_dev);
  526. err = tiadc_request_dma(pdev, adc_dev);
  527. if (err && err == -EPROBE_DEFER)
  528. goto err_dma;
  529. return 0;
  530. err_dma:
  531. iio_device_unregister(indio_dev);
  532. err_buffer_unregister:
  533. tiadc_iio_buffered_hardware_remove(indio_dev);
  534. err_free_channels:
  535. tiadc_channels_remove(indio_dev);
  536. return err;
  537. }
  538. static int tiadc_remove(struct platform_device *pdev)
  539. {
  540. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  541. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  542. struct tiadc_dma *dma = &adc_dev->dma;
  543. u32 step_en;
  544. if (dma->chan) {
  545. dma_free_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE,
  546. dma->buf, dma->addr);
  547. dma_release_channel(dma->chan);
  548. }
  549. iio_device_unregister(indio_dev);
  550. tiadc_iio_buffered_hardware_remove(indio_dev);
  551. tiadc_channels_remove(indio_dev);
  552. step_en = get_adc_step_mask(adc_dev);
  553. am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
  554. return 0;
  555. }
  556. static int __maybe_unused tiadc_suspend(struct device *dev)
  557. {
  558. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  559. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  560. struct ti_tscadc_dev *tscadc_dev;
  561. unsigned int idle;
  562. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  563. if (!device_may_wakeup(tscadc_dev->dev)) {
  564. idle = tiadc_readl(adc_dev, REG_CTRL);
  565. idle &= ~(CNTRLREG_TSCSSENB);
  566. tiadc_writel(adc_dev, REG_CTRL, (idle |
  567. CNTRLREG_POWERDOWN));
  568. }
  569. return 0;
  570. }
  571. static int __maybe_unused tiadc_resume(struct device *dev)
  572. {
  573. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  574. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  575. unsigned int restore;
  576. /* Make sure ADC is powered up */
  577. restore = tiadc_readl(adc_dev, REG_CTRL);
  578. restore &= ~(CNTRLREG_POWERDOWN);
  579. tiadc_writel(adc_dev, REG_CTRL, restore);
  580. tiadc_step_config(indio_dev);
  581. am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
  582. adc_dev->buffer_en_ch_steps);
  583. return 0;
  584. }
  585. static SIMPLE_DEV_PM_OPS(tiadc_pm_ops, tiadc_suspend, tiadc_resume);
  586. static const struct of_device_id ti_adc_dt_ids[] = {
  587. { .compatible = "ti,am3359-adc", },
  588. { }
  589. };
  590. MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
  591. static struct platform_driver tiadc_driver = {
  592. .driver = {
  593. .name = "TI-am335x-adc",
  594. .pm = &tiadc_pm_ops,
  595. .of_match_table = ti_adc_dt_ids,
  596. },
  597. .probe = tiadc_probe,
  598. .remove = tiadc_remove,
  599. };
  600. module_platform_driver(tiadc_driver);
  601. MODULE_DESCRIPTION("TI ADC controller driver");
  602. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  603. MODULE_LICENSE("GPL");