4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
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.
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.
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>
23 #include <linux/iio/iio.h>
25 #include <linux/of_device.h>
26 #include <linux/iio/machine.h>
27 #include <linux/iio/driver.h>
29 #include <linux/mfd/ti_am335x_tscadc.h>
30 #include <linux/iio/buffer.h>
31 #include <linux/iio/kfifo_buf.h>
34 struct ti_tscadc_dev *mfd_tscadc;
35 struct mutex fifo1_lock; /* to protect fifo access */
39 int buffer_en_ch_steps;
41 u32 open_delay[8], sample_delay[8], step_avg[8];
44 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
46 return readl(adc->mfd_tscadc->tscadc_base + reg);
49 static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
52 writel(val, adc->mfd_tscadc->tscadc_base + reg);
55 static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
59 step_en = ((1 << adc_dev->channels) - 1);
60 step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
64 static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev,
65 struct iio_chan_spec const *chan)
69 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
70 if (chan->channel == adc_dev->channel_line[i]) {
73 step = adc_dev->channel_step[i];
74 /* +1 for the charger */
75 return 1 << (step + 1);
82 static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
84 return 1 << adc_dev->channel_step[chan];
87 static void tiadc_step_config(struct iio_dev *indio_dev)
89 struct tiadc_device *adc_dev = iio_priv(indio_dev);
90 struct device *dev = adc_dev->mfd_tscadc->dev;
91 unsigned int stepconfig;
95 * There are 16 configurable steps and 8 analog input
96 * lines available which are shared between Touchscreen and ADC.
98 * Steps forwards i.e. from 0 towards 16 are used by ADC
99 * depending on number of input lines needed.
100 * Channel would represent which analog input
101 * needs to be given to ADC to digitalize data.
105 for (i = 0; i < adc_dev->channels; i++) {
108 chan = adc_dev->channel_line[i];
110 if (adc_dev->step_avg[i] > STEPCONFIG_AVG_16) {
111 dev_warn(dev, "chan %d step_avg truncating to %d\n",
112 chan, STEPCONFIG_AVG_16);
113 adc_dev->step_avg[i] = STEPCONFIG_AVG_16;
116 if (adc_dev->step_avg[i])
118 STEPCONFIG_AVG(ffs(adc_dev->step_avg[i]) - 1) |
121 stepconfig = STEPCONFIG_FIFO1;
123 if (iio_buffer_enabled(indio_dev))
124 stepconfig |= STEPCONFIG_MODE_SWCNT;
126 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
127 stepconfig | STEPCONFIG_INP(chan));
129 if (adc_dev->open_delay[i] > STEPDELAY_OPEN_MASK) {
130 dev_warn(dev, "chan %d open delay truncating to 0x3FFFF\n",
132 adc_dev->open_delay[i] = STEPDELAY_OPEN_MASK;
135 if (adc_dev->sample_delay[i] > 0xFF) {
136 dev_warn(dev, "chan %d sample delay truncating to 0xFF\n",
138 adc_dev->sample_delay[i] = 0xFF;
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_TSCSSENB);
167 tiadc_writel(adc_dev, REG_CTRL, config);
168 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
169 | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
171 /* wait for idle state.
172 * ADC needs to finish the current conversion
173 * before disabling the module
176 adc_fsm = tiadc_readl(adc_dev, REG_ADCFSM);
177 } while (adc_fsm != 0x10 && count++ < 100);
179 tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
181 } else if (status & IRQENB_FIFO1THRES) {
182 /* Disable irq and wake worker thread */
183 tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
184 return IRQ_WAKE_THREAD;
190 static irqreturn_t tiadc_worker_h(int irq, void *private)
192 struct iio_dev *indio_dev = private;
193 struct tiadc_device *adc_dev = iio_priv(indio_dev);
194 int i, k, fifo1count, read;
195 u16 *data = adc_dev->data;
197 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
198 for (k = 0; k < fifo1count; k = k + i) {
199 for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
200 read = tiadc_readl(adc_dev, REG_FIFO1);
201 data[i] = read & FIFOREAD_DATA_MASK;
203 iio_push_to_buffers(indio_dev, (u8 *) data);
206 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
207 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
212 static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
214 struct tiadc_device *adc_dev = iio_priv(indio_dev);
215 int i, fifo1count, read;
217 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
219 IRQENB_FIFO1UNDRFLW));
221 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
222 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
223 for (i = 0; i < fifo1count; i++)
224 read = tiadc_readl(adc_dev, REG_FIFO1);
229 static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
231 struct tiadc_device *adc_dev = iio_priv(indio_dev);
232 unsigned int enb = 0;
235 tiadc_step_config(indio_dev);
236 for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels)
237 enb |= (get_adc_step_bit(adc_dev, bit) << 1);
238 adc_dev->buffer_en_ch_steps = enb;
240 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
242 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES
243 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
244 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES
245 | IRQENB_FIFO1OVRRUN);
250 static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
252 struct tiadc_device *adc_dev = iio_priv(indio_dev);
253 int fifo1count, i, read;
255 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
256 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
257 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
258 adc_dev->buffer_en_ch_steps = 0;
260 /* Flush FIFO of leftover data in the time it takes to disable adc */
261 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
262 for (i = 0; i < fifo1count; i++)
263 read = tiadc_readl(adc_dev, REG_FIFO1);
268 static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
270 tiadc_step_config(indio_dev);
275 static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
276 .preenable = &tiadc_buffer_preenable,
277 .postenable = &tiadc_buffer_postenable,
278 .predisable = &tiadc_buffer_predisable,
279 .postdisable = &tiadc_buffer_postdisable,
282 static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
283 irqreturn_t (*pollfunc_bh)(int irq, void *p),
284 irqreturn_t (*pollfunc_th)(int irq, void *p),
287 const struct iio_buffer_setup_ops *setup_ops)
289 struct iio_buffer *buffer;
292 buffer = iio_kfifo_allocate();
296 iio_device_attach_buffer(indio_dev, buffer);
298 ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
299 flags, indio_dev->name, indio_dev);
301 goto error_kfifo_free;
303 indio_dev->setup_ops = setup_ops;
304 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
309 iio_kfifo_free(indio_dev->buffer);
313 static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
315 struct tiadc_device *adc_dev = iio_priv(indio_dev);
317 free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
318 iio_kfifo_free(indio_dev->buffer);
322 static const char * const chan_name_ain[] = {
333 static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
335 struct tiadc_device *adc_dev = iio_priv(indio_dev);
336 struct iio_chan_spec *chan_array;
337 struct iio_chan_spec *chan;
340 indio_dev->num_channels = channels;
341 chan_array = kcalloc(channels, sizeof(*chan_array), GFP_KERNEL);
342 if (chan_array == NULL)
346 for (i = 0; i < channels; i++, chan++) {
348 chan->type = IIO_VOLTAGE;
350 chan->channel = adc_dev->channel_line[i];
351 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
352 chan->datasheet_name = chan_name_ain[chan->channel];
353 chan->scan_index = i;
354 chan->scan_type.sign = 'u';
355 chan->scan_type.realbits = 12;
356 chan->scan_type.storagebits = 16;
359 indio_dev->channels = chan_array;
364 static void tiadc_channels_remove(struct iio_dev *indio_dev)
366 kfree(indio_dev->channels);
369 static int tiadc_read_raw(struct iio_dev *indio_dev,
370 struct iio_chan_spec const *chan,
371 int *val, int *val2, long mask)
373 struct tiadc_device *adc_dev = iio_priv(indio_dev);
374 int ret = IIO_VAL_INT;
376 unsigned int fifo1count, read, stepid;
379 unsigned long timeout;
381 if (iio_buffer_enabled(indio_dev))
384 step_en = get_adc_chan_step_mask(adc_dev, chan);
388 mutex_lock(&adc_dev->fifo1_lock);
389 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
391 tiadc_readl(adc_dev, REG_FIFO1);
393 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
395 timeout = jiffies + msecs_to_jiffies
396 (IDLE_TIMEOUT * adc_dev->channels);
397 /* Wait for Fifo threshold interrupt */
399 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
403 if (time_after(jiffies, timeout)) {
404 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
409 map_val = adc_dev->channel_step[chan->scan_index];
412 * We check the complete FIFO. We programmed just one entry but in case
413 * something went wrong we left empty handed (-EAGAIN previously) and
414 * then the value apeared somehow in the FIFO we would have two entries.
415 * Therefore we read every item and keep only the latest version of the
418 for (i = 0; i < fifo1count; i++) {
419 read = tiadc_readl(adc_dev, REG_FIFO1);
420 stepid = read & FIFOREAD_CHNLID_MASK;
421 stepid = stepid >> 0x10;
423 if (stepid == map_val) {
424 read = read & FIFOREAD_DATA_MASK;
429 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
435 mutex_unlock(&adc_dev->fifo1_lock);
439 static const struct iio_info tiadc_info = {
440 .read_raw = &tiadc_read_raw,
441 .driver_module = THIS_MODULE,
444 static int tiadc_parse_dt(struct platform_device *pdev,
445 struct tiadc_device *adc_dev)
447 struct device_node *node = pdev->dev.of_node;
448 struct property *prop;
453 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
454 adc_dev->channel_line[channels] = val;
456 /* Set Default values for optional DT parameters */
457 adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY;
458 adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY;
459 adc_dev->step_avg[channels] = 16;
464 of_property_read_u32_array(node, "ti,chan-step-avg",
465 adc_dev->step_avg, channels);
466 of_property_read_u32_array(node, "ti,chan-step-opendelay",
467 adc_dev->open_delay, channels);
468 of_property_read_u32_array(node, "ti,chan-step-sampledelay",
469 adc_dev->sample_delay, channels);
471 adc_dev->channels = channels;
475 static int tiadc_probe(struct platform_device *pdev)
477 struct iio_dev *indio_dev;
478 struct tiadc_device *adc_dev;
479 struct device_node *node = pdev->dev.of_node;
483 dev_err(&pdev->dev, "Could not find valid DT data.\n");
487 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc_dev));
488 if (indio_dev == NULL) {
489 dev_err(&pdev->dev, "failed to allocate iio device\n");
492 adc_dev = iio_priv(indio_dev);
494 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
495 tiadc_parse_dt(pdev, adc_dev);
497 indio_dev->dev.parent = &pdev->dev;
498 indio_dev->name = dev_name(&pdev->dev);
499 indio_dev->modes = INDIO_DIRECT_MODE;
500 indio_dev->info = &tiadc_info;
502 tiadc_step_config(indio_dev);
503 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
504 mutex_init(&adc_dev->fifo1_lock);
506 err = tiadc_channel_init(indio_dev, adc_dev->channels);
510 err = tiadc_iio_buffered_hardware_setup(indio_dev,
513 adc_dev->mfd_tscadc->irq,
515 &tiadc_buffer_setup_ops);
518 goto err_free_channels;
520 err = iio_device_register(indio_dev);
522 goto err_buffer_unregister;
524 platform_set_drvdata(pdev, indio_dev);
528 err_buffer_unregister:
529 tiadc_iio_buffered_hardware_remove(indio_dev);
531 tiadc_channels_remove(indio_dev);
535 static int tiadc_remove(struct platform_device *pdev)
537 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
538 struct tiadc_device *adc_dev = iio_priv(indio_dev);
541 iio_device_unregister(indio_dev);
542 tiadc_iio_buffered_hardware_remove(indio_dev);
543 tiadc_channels_remove(indio_dev);
545 step_en = get_adc_step_mask(adc_dev);
546 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
551 static int __maybe_unused tiadc_suspend(struct device *dev)
553 struct iio_dev *indio_dev = dev_get_drvdata(dev);
554 struct tiadc_device *adc_dev = iio_priv(indio_dev);
555 struct ti_tscadc_dev *tscadc_dev;
558 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
559 if (!device_may_wakeup(tscadc_dev->dev)) {
560 idle = tiadc_readl(adc_dev, REG_CTRL);
561 idle &= ~(CNTRLREG_TSCSSENB);
562 tiadc_writel(adc_dev, REG_CTRL, (idle |
563 CNTRLREG_POWERDOWN));
569 static int __maybe_unused tiadc_resume(struct device *dev)
571 struct iio_dev *indio_dev = dev_get_drvdata(dev);
572 struct tiadc_device *adc_dev = iio_priv(indio_dev);
573 unsigned int restore;
575 /* Make sure ADC is powered up */
576 restore = tiadc_readl(adc_dev, REG_CTRL);
577 restore &= ~(CNTRLREG_POWERDOWN);
578 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);
586 static SIMPLE_DEV_PM_OPS(tiadc_pm_ops, tiadc_suspend, tiadc_resume);
588 static const struct of_device_id ti_adc_dt_ids[] = {
589 { .compatible = "ti,am3359-adc", },
592 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
594 static struct platform_driver tiadc_driver = {
596 .name = "TI-am335x-adc",
598 .of_match_table = ti_adc_dt_ids,
600 .probe = tiadc_probe,
601 .remove = tiadc_remove,
603 module_platform_driver(tiadc_driver);
605 MODULE_DESCRIPTION("TI ADC controller driver");
606 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
607 MODULE_LICENSE("GPL");