1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2012 Texas Instruments Incorporated - https://www.ti.com/
8 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
15 #include <linux/iio/iio.h>
17 #include <linux/of_device.h>
18 #include <linux/iio/machine.h>
19 #include <linux/iio/driver.h>
20 #include <linux/iopoll.h>
22 #include <linux/mfd/ti_am335x_tscadc.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/kfifo_buf.h>
26 #include <linux/dmaengine.h>
27 #include <linux/dma-mapping.h>
29 #define DMA_BUFFER_SIZE SZ_2K
32 struct dma_slave_config conf;
33 struct dma_chan *chan;
43 struct ti_tscadc_dev *mfd_tscadc;
45 struct mutex fifo1_lock; /* to protect fifo access */
50 int buffer_en_ch_steps;
52 u32 open_delay[8], sample_delay[8], step_avg[8];
55 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
57 return readl(adc->mfd_tscadc->tscadc_base + reg);
60 static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
63 writel(val, adc->mfd_tscadc->tscadc_base + reg);
66 static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
70 step_en = ((1 << adc_dev->channels) - 1);
71 step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
75 static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev,
76 struct iio_chan_spec const *chan)
80 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
81 if (chan->channel == adc_dev->channel_line[i]) {
84 step = adc_dev->channel_step[i];
85 /* +1 for the charger */
86 return 1 << (step + 1);
93 static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
95 return 1 << adc_dev->channel_step[chan];
98 static int tiadc_wait_idle(struct tiadc_device *adc_dev)
102 return readl_poll_timeout(adc_dev->mfd_tscadc->tscadc_base + REG_ADCFSM,
103 val, !(val & SEQ_STATUS), 10,
104 IDLE_TIMEOUT_MS * 1000 * adc_dev->channels);
107 static void tiadc_step_config(struct iio_dev *indio_dev)
109 struct tiadc_device *adc_dev = iio_priv(indio_dev);
110 unsigned int stepconfig;
114 * There are 16 configurable steps and 8 analog input
115 * lines available which are shared between Touchscreen and ADC.
117 * Steps forwards i.e. from 0 towards 16 are used by ADC
118 * depending on number of input lines needed.
119 * Channel would represent which analog input
120 * needs to be given to ADC to digitalize data.
122 for (i = 0; i < adc_dev->channels; i++) {
125 chan = adc_dev->channel_line[i];
127 if (adc_dev->step_avg[i])
128 stepconfig = STEPCONFIG_AVG(ffs(adc_dev->step_avg[i]) - 1) |
131 stepconfig = STEPCONFIG_FIFO1;
133 if (iio_buffer_enabled(indio_dev))
134 stepconfig |= STEPCONFIG_MODE_SWCNT;
136 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
137 stepconfig | STEPCONFIG_INP(chan) |
138 STEPCONFIG_INM_ADCREFM | STEPCONFIG_RFP_VREFP |
139 STEPCONFIG_RFM_VREFN);
141 tiadc_writel(adc_dev, REG_STEPDELAY(steps),
142 STEPDELAY_OPEN(adc_dev->open_delay[i]) |
143 STEPDELAY_SAMPLE(adc_dev->sample_delay[i]));
145 adc_dev->channel_step[i] = steps;
150 static irqreturn_t tiadc_irq_h(int irq, void *private)
152 struct iio_dev *indio_dev = private;
153 struct tiadc_device *adc_dev = iio_priv(indio_dev);
154 unsigned int status, config, adc_fsm;
155 unsigned short count = 0;
157 status = tiadc_readl(adc_dev, REG_IRQSTATUS);
160 * ADC and touchscreen share the IRQ line.
161 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
163 if (status & IRQENB_FIFO1OVRRUN) {
164 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
165 config = tiadc_readl(adc_dev, REG_CTRL);
166 config &= ~(CNTRLREG_SSENB);
167 tiadc_writel(adc_dev, REG_CTRL, config);
168 tiadc_writel(adc_dev, REG_IRQSTATUS,
169 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW |
173 * Wait for the idle state.
174 * ADC needs to finish the current conversion
175 * before disabling the module
178 adc_fsm = tiadc_readl(adc_dev, REG_ADCFSM);
179 } while (adc_fsm != 0x10 && count++ < 100);
181 tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_SSENB));
183 } else if (status & IRQENB_FIFO1THRES) {
184 /* Disable irq and wake worker thread */
185 tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
186 return IRQ_WAKE_THREAD;
192 static irqreturn_t tiadc_worker_h(int irq, void *private)
194 struct iio_dev *indio_dev = private;
195 struct tiadc_device *adc_dev = iio_priv(indio_dev);
196 int i, k, fifo1count, read;
197 u16 *data = adc_dev->data;
199 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
200 for (k = 0; k < fifo1count; k = k + i) {
201 for (i = 0; i < indio_dev->scan_bytes / 2; i++) {
202 read = tiadc_readl(adc_dev, REG_FIFO1);
203 data[i] = read & FIFOREAD_DATA_MASK;
205 iio_push_to_buffers(indio_dev, (u8 *)data);
208 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
209 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
214 static void tiadc_dma_rx_complete(void *param)
216 struct iio_dev *indio_dev = param;
217 struct tiadc_device *adc_dev = iio_priv(indio_dev);
218 struct tiadc_dma *dma = &adc_dev->dma;
222 data = dma->buf + dma->current_period * dma->period_size;
223 dma->current_period = 1 - dma->current_period; /* swap the buffer ID */
225 for (i = 0; i < dma->period_size; i += indio_dev->scan_bytes) {
226 iio_push_to_buffers(indio_dev, data);
227 data += indio_dev->scan_bytes;
231 static int tiadc_start_dma(struct iio_dev *indio_dev)
233 struct tiadc_device *adc_dev = iio_priv(indio_dev);
234 struct tiadc_dma *dma = &adc_dev->dma;
235 struct dma_async_tx_descriptor *desc;
237 dma->current_period = 0; /* We start to fill period 0 */
240 * Make the fifo thresh as the multiple of total number of
241 * channels enabled, so make sure that cyclic DMA period
242 * length is also a multiple of total number of channels
243 * enabled. This ensures that no invalid data is reported
244 * to the stack via iio_push_to_buffers().
246 dma->fifo_thresh = rounddown(FIFO1_THRESHOLD + 1,
247 adc_dev->total_ch_enabled) - 1;
249 /* Make sure that period length is multiple of fifo thresh level */
250 dma->period_size = rounddown(DMA_BUFFER_SIZE / 2,
251 (dma->fifo_thresh + 1) * sizeof(u16));
253 dma->conf.src_maxburst = dma->fifo_thresh + 1;
254 dmaengine_slave_config(dma->chan, &dma->conf);
256 desc = dmaengine_prep_dma_cyclic(dma->chan, dma->addr,
257 dma->period_size * 2,
258 dma->period_size, DMA_DEV_TO_MEM,
263 desc->callback = tiadc_dma_rx_complete;
264 desc->callback_param = indio_dev;
266 dma->cookie = dmaengine_submit(desc);
268 dma_async_issue_pending(dma->chan);
270 tiadc_writel(adc_dev, REG_FIFO1THR, dma->fifo_thresh);
271 tiadc_writel(adc_dev, REG_DMA1REQ, dma->fifo_thresh);
272 tiadc_writel(adc_dev, REG_DMAENABLE_SET, DMA_FIFO1);
277 static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
279 struct tiadc_device *adc_dev = iio_priv(indio_dev);
283 ret = tiadc_wait_idle(adc_dev);
287 tiadc_writel(adc_dev, REG_IRQCLR,
288 IRQENB_FIFO1THRES | IRQENB_FIFO1OVRRUN |
289 IRQENB_FIFO1UNDRFLW);
291 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
292 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
293 for (i = 0; i < fifo1count; i++)
294 tiadc_readl(adc_dev, REG_FIFO1);
299 static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
301 struct tiadc_device *adc_dev = iio_priv(indio_dev);
302 struct tiadc_dma *dma = &adc_dev->dma;
303 unsigned int irq_enable;
304 unsigned int enb = 0;
307 tiadc_step_config(indio_dev);
308 for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels) {
309 enb |= (get_adc_step_bit(adc_dev, bit) << 1);
310 adc_dev->total_ch_enabled++;
312 adc_dev->buffer_en_ch_steps = enb;
315 tiadc_start_dma(indio_dev);
317 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
319 tiadc_writel(adc_dev, REG_IRQSTATUS,
320 IRQENB_FIFO1THRES | IRQENB_FIFO1OVRRUN |
321 IRQENB_FIFO1UNDRFLW);
323 irq_enable = IRQENB_FIFO1OVRRUN;
325 irq_enable |= IRQENB_FIFO1THRES;
326 tiadc_writel(adc_dev, REG_IRQENABLE, irq_enable);
331 static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
333 struct tiadc_device *adc_dev = iio_priv(indio_dev);
334 struct tiadc_dma *dma = &adc_dev->dma;
337 tiadc_writel(adc_dev, REG_IRQCLR,
338 IRQENB_FIFO1THRES | IRQENB_FIFO1OVRRUN |
339 IRQENB_FIFO1UNDRFLW);
340 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
341 adc_dev->buffer_en_ch_steps = 0;
342 adc_dev->total_ch_enabled = 0;
344 tiadc_writel(adc_dev, REG_DMAENABLE_CLEAR, 0x2);
345 dmaengine_terminate_async(dma->chan);
348 /* Flush FIFO of leftover data in the time it takes to disable adc */
349 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
350 for (i = 0; i < fifo1count; i++)
351 tiadc_readl(adc_dev, REG_FIFO1);
356 static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
358 tiadc_step_config(indio_dev);
363 static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
364 .preenable = &tiadc_buffer_preenable,
365 .postenable = &tiadc_buffer_postenable,
366 .predisable = &tiadc_buffer_predisable,
367 .postdisable = &tiadc_buffer_postdisable,
370 static int tiadc_iio_buffered_hardware_setup(struct device *dev,
371 struct iio_dev *indio_dev,
372 irqreturn_t (*pollfunc_bh)(int irq, void *p),
373 irqreturn_t (*pollfunc_th)(int irq, void *p),
374 int irq, unsigned long flags,
375 const struct iio_buffer_setup_ops *setup_ops)
379 ret = devm_iio_kfifo_buffer_setup(dev, indio_dev, setup_ops);
383 return devm_request_threaded_irq(dev, irq, pollfunc_th, pollfunc_bh,
384 flags, indio_dev->name, indio_dev);
387 static const char * const chan_name_ain[] = {
398 static int tiadc_channel_init(struct device *dev, struct iio_dev *indio_dev,
401 struct tiadc_device *adc_dev = iio_priv(indio_dev);
402 struct iio_chan_spec *chan_array;
403 struct iio_chan_spec *chan;
406 indio_dev->num_channels = channels;
407 chan_array = devm_kcalloc(dev, channels, sizeof(*chan_array),
413 for (i = 0; i < channels; i++, chan++) {
414 chan->type = IIO_VOLTAGE;
416 chan->channel = adc_dev->channel_line[i];
417 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
418 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
419 chan->datasheet_name = chan_name_ain[chan->channel];
420 chan->scan_index = i;
421 chan->scan_type.sign = 'u';
422 chan->scan_type.realbits = 12;
423 chan->scan_type.storagebits = 16;
426 indio_dev->channels = chan_array;
431 static int tiadc_read_raw(struct iio_dev *indio_dev,
432 struct iio_chan_spec const *chan, int *val, int *val2,
435 struct tiadc_device *adc_dev = iio_priv(indio_dev);
437 unsigned int fifo1count, read, stepid;
440 unsigned long timeout;
444 case IIO_CHAN_INFO_RAW:
446 case IIO_CHAN_INFO_SCALE:
447 switch (chan->type) {
450 *val2 = chan->scan_type.realbits;
451 return IIO_VAL_FRACTIONAL_LOG2;
460 if (iio_buffer_enabled(indio_dev))
463 step_en = get_adc_chan_step_mask(adc_dev, chan);
467 mutex_lock(&adc_dev->fifo1_lock);
469 ret = tiadc_wait_idle(adc_dev);
473 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
475 tiadc_readl(adc_dev, REG_FIFO1);
477 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
479 /* Wait for Fifo threshold interrupt */
480 timeout = jiffies + msecs_to_jiffies(IDLE_TIMEOUT_MS * adc_dev->channels);
482 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
486 if (time_after(jiffies, timeout)) {
487 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
493 map_val = adc_dev->channel_step[chan->scan_index];
496 * We check the complete FIFO. We programmed just one entry but in case
497 * something went wrong we left empty handed (-EAGAIN previously) and
498 * then the value apeared somehow in the FIFO we would have two entries.
499 * Therefore we read every item and keep only the latest version of the
502 for (i = 0; i < fifo1count; i++) {
503 read = tiadc_readl(adc_dev, REG_FIFO1);
504 stepid = read & FIFOREAD_CHNLID_MASK;
505 stepid = stepid >> 0x10;
507 if (stepid == map_val) {
508 read = read & FIFOREAD_DATA_MASK;
514 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
520 mutex_unlock(&adc_dev->fifo1_lock);
521 return ret ? ret : IIO_VAL_INT;
524 static const struct iio_info tiadc_info = {
525 .read_raw = &tiadc_read_raw,
528 static int tiadc_request_dma(struct platform_device *pdev,
529 struct tiadc_device *adc_dev)
531 struct tiadc_dma *dma = &adc_dev->dma;
534 /* Default slave configuration parameters */
535 dma->conf.direction = DMA_DEV_TO_MEM;
536 dma->conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
537 dma->conf.src_addr = adc_dev->mfd_tscadc->tscadc_phys_base + REG_FIFO1;
540 dma_cap_set(DMA_CYCLIC, mask);
542 /* Get a channel for RX */
543 dma->chan = dma_request_chan(adc_dev->mfd_tscadc->dev, "fifo1");
544 if (IS_ERR(dma->chan)) {
545 int ret = PTR_ERR(dma->chan);
552 dma->buf = dma_alloc_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE,
553 &dma->addr, GFP_KERNEL);
560 dma_release_channel(dma->chan);
564 static int tiadc_parse_dt(struct platform_device *pdev,
565 struct tiadc_device *adc_dev)
567 struct device_node *node = pdev->dev.of_node;
568 struct property *prop;
574 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
575 adc_dev->channel_line[channels] = val;
577 /* Set Default values for optional DT parameters */
578 adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY;
579 adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY;
580 adc_dev->step_avg[channels] = 16;
585 adc_dev->channels = channels;
587 of_property_read_u32_array(node, "ti,chan-step-avg",
588 adc_dev->step_avg, channels);
589 of_property_read_u32_array(node, "ti,chan-step-opendelay",
590 adc_dev->open_delay, channels);
591 of_property_read_u32_array(node, "ti,chan-step-sampledelay",
592 adc_dev->sample_delay, channels);
594 for (i = 0; i < adc_dev->channels; i++) {
597 chan = adc_dev->channel_line[i];
599 if (adc_dev->step_avg[i] > STEPCONFIG_AVG_16) {
601 "chan %d: wrong step avg, truncated to %ld\n",
602 chan, STEPCONFIG_AVG_16);
603 adc_dev->step_avg[i] = STEPCONFIG_AVG_16;
606 if (adc_dev->open_delay[i] > STEPCONFIG_MAX_OPENDLY) {
608 "chan %d: wrong open delay, truncated to 0x%lX\n",
609 chan, STEPCONFIG_MAX_OPENDLY);
610 adc_dev->open_delay[i] = STEPCONFIG_MAX_OPENDLY;
613 if (adc_dev->sample_delay[i] > STEPCONFIG_MAX_SAMPLE) {
615 "chan %d: wrong sample delay, truncated to 0x%lX\n",
616 chan, STEPCONFIG_MAX_SAMPLE);
617 adc_dev->sample_delay[i] = STEPCONFIG_MAX_SAMPLE;
624 static int tiadc_probe(struct platform_device *pdev)
626 struct iio_dev *indio_dev;
627 struct tiadc_device *adc_dev;
628 struct device_node *node = pdev->dev.of_node;
632 dev_err(&pdev->dev, "Could not find valid DT data.\n");
636 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc_dev));
638 dev_err(&pdev->dev, "failed to allocate iio device\n");
641 adc_dev = iio_priv(indio_dev);
643 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
644 tiadc_parse_dt(pdev, adc_dev);
646 indio_dev->name = dev_name(&pdev->dev);
647 indio_dev->modes = INDIO_DIRECT_MODE;
648 indio_dev->info = &tiadc_info;
650 tiadc_step_config(indio_dev);
651 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
652 mutex_init(&adc_dev->fifo1_lock);
654 err = tiadc_channel_init(&pdev->dev, indio_dev, adc_dev->channels);
658 err = tiadc_iio_buffered_hardware_setup(&pdev->dev, indio_dev,
661 adc_dev->mfd_tscadc->irq,
663 &tiadc_buffer_setup_ops);
667 err = iio_device_register(indio_dev);
671 platform_set_drvdata(pdev, indio_dev);
673 err = tiadc_request_dma(pdev, adc_dev);
674 if (err && err == -EPROBE_DEFER)
680 iio_device_unregister(indio_dev);
685 static int tiadc_remove(struct platform_device *pdev)
687 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
688 struct tiadc_device *adc_dev = iio_priv(indio_dev);
689 struct tiadc_dma *dma = &adc_dev->dma;
693 dma_free_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE,
694 dma->buf, dma->addr);
695 dma_release_channel(dma->chan);
697 iio_device_unregister(indio_dev);
699 step_en = get_adc_step_mask(adc_dev);
700 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
705 static int tiadc_suspend(struct device *dev)
707 struct iio_dev *indio_dev = dev_get_drvdata(dev);
708 struct tiadc_device *adc_dev = iio_priv(indio_dev);
711 idle = tiadc_readl(adc_dev, REG_CTRL);
712 idle &= ~(CNTRLREG_SSENB);
713 tiadc_writel(adc_dev, REG_CTRL, idle | CNTRLREG_POWERDOWN);
718 static int tiadc_resume(struct device *dev)
720 struct iio_dev *indio_dev = dev_get_drvdata(dev);
721 struct tiadc_device *adc_dev = iio_priv(indio_dev);
722 unsigned int restore;
724 /* Make sure ADC is powered up */
725 restore = tiadc_readl(adc_dev, REG_CTRL);
726 restore &= ~CNTRLREG_POWERDOWN;
727 tiadc_writel(adc_dev, REG_CTRL, restore);
729 tiadc_step_config(indio_dev);
730 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
731 adc_dev->buffer_en_ch_steps);
735 static DEFINE_SIMPLE_DEV_PM_OPS(tiadc_pm_ops, tiadc_suspend, tiadc_resume);
737 static const struct of_device_id ti_adc_dt_ids[] = {
738 { .compatible = "ti,am3359-adc", },
739 { .compatible = "ti,am4372-adc", },
742 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
744 static struct platform_driver tiadc_driver = {
746 .name = "TI-am335x-adc",
747 .pm = pm_sleep_ptr(&tiadc_pm_ops),
748 .of_match_table = ti_adc_dt_ids,
750 .probe = tiadc_probe,
751 .remove = tiadc_remove,
753 module_platform_driver(tiadc_driver);
755 MODULE_DESCRIPTION("TI ADC controller driver");
757 MODULE_LICENSE("GPL");