ti_am335x_adc.c 19 KB

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