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
;
155 status
= tiadc_readl(adc_dev
, REG_IRQSTATUS
);
158 * ADC and touchscreen share the IRQ line.
159 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
161 if (status
& IRQENB_FIFO1OVRRUN
) {
162 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
163 config
= tiadc_readl(adc_dev
, REG_CTRL
);
164 config
&= ~(CNTRLREG_TSCSSENB
);
165 tiadc_writel(adc_dev
, REG_CTRL
, config
);
166 tiadc_writel(adc_dev
, REG_IRQSTATUS
, IRQENB_FIFO1OVRRUN
167 | IRQENB_FIFO1UNDRFLW
| IRQENB_FIFO1THRES
);
168 tiadc_writel(adc_dev
, REG_CTRL
, (config
| CNTRLREG_TSCSSENB
));
170 } else if (status
& IRQENB_FIFO1THRES
) {
171 /* Disable irq and wake worker thread */
172 tiadc_writel(adc_dev
, REG_IRQCLR
, IRQENB_FIFO1THRES
);
173 return IRQ_WAKE_THREAD
;
179 static irqreturn_t
tiadc_worker_h(int irq
, void *private)
181 struct iio_dev
*indio_dev
= private;
182 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
183 int i
, k
, fifo1count
, read
;
184 u16
*data
= adc_dev
->data
;
186 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
187 for (k
= 0; k
< fifo1count
; k
= k
+ i
) {
188 for (i
= 0; i
< (indio_dev
->scan_bytes
)/2; i
++) {
189 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
190 data
[i
] = read
& FIFOREAD_DATA_MASK
;
192 iio_push_to_buffers(indio_dev
, (u8
*) data
);
195 tiadc_writel(adc_dev
, REG_IRQSTATUS
, IRQENB_FIFO1THRES
);
196 tiadc_writel(adc_dev
, REG_IRQENABLE
, IRQENB_FIFO1THRES
);
201 static int tiadc_buffer_preenable(struct iio_dev
*indio_dev
)
203 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
204 int i
, fifo1count
, read
;
206 tiadc_writel(adc_dev
, REG_IRQCLR
, (IRQENB_FIFO1THRES
|
208 IRQENB_FIFO1UNDRFLW
));
210 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
211 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
212 for (i
= 0; i
< fifo1count
; i
++)
213 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
218 static int tiadc_buffer_postenable(struct iio_dev
*indio_dev
)
220 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
221 unsigned int enb
= 0;
224 tiadc_step_config(indio_dev
);
225 for_each_set_bit(bit
, indio_dev
->active_scan_mask
, adc_dev
->channels
)
226 enb
|= (get_adc_step_bit(adc_dev
, bit
) << 1);
227 adc_dev
->buffer_en_ch_steps
= enb
;
229 am335x_tsc_se_set_cache(adc_dev
->mfd_tscadc
, enb
);
231 tiadc_writel(adc_dev
, REG_IRQSTATUS
, IRQENB_FIFO1THRES
232 | IRQENB_FIFO1OVRRUN
| IRQENB_FIFO1UNDRFLW
);
233 tiadc_writel(adc_dev
, REG_IRQENABLE
, IRQENB_FIFO1THRES
234 | IRQENB_FIFO1OVRRUN
);
239 static int tiadc_buffer_predisable(struct iio_dev
*indio_dev
)
241 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
242 int fifo1count
, i
, read
;
244 tiadc_writel(adc_dev
, REG_IRQCLR
, (IRQENB_FIFO1THRES
|
245 IRQENB_FIFO1OVRRUN
| IRQENB_FIFO1UNDRFLW
));
246 am335x_tsc_se_clr(adc_dev
->mfd_tscadc
, adc_dev
->buffer_en_ch_steps
);
247 adc_dev
->buffer_en_ch_steps
= 0;
249 /* Flush FIFO of leftover data in the time it takes to disable adc */
250 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
251 for (i
= 0; i
< fifo1count
; i
++)
252 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
257 static int tiadc_buffer_postdisable(struct iio_dev
*indio_dev
)
259 tiadc_step_config(indio_dev
);
264 static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops
= {
265 .preenable
= &tiadc_buffer_preenable
,
266 .postenable
= &tiadc_buffer_postenable
,
267 .predisable
= &tiadc_buffer_predisable
,
268 .postdisable
= &tiadc_buffer_postdisable
,
271 static int tiadc_iio_buffered_hardware_setup(struct iio_dev
*indio_dev
,
272 irqreturn_t (*pollfunc_bh
)(int irq
, void *p
),
273 irqreturn_t (*pollfunc_th
)(int irq
, void *p
),
276 const struct iio_buffer_setup_ops
*setup_ops
)
278 struct iio_buffer
*buffer
;
281 buffer
= iio_kfifo_allocate();
285 iio_device_attach_buffer(indio_dev
, buffer
);
287 ret
= request_threaded_irq(irq
, pollfunc_th
, pollfunc_bh
,
288 flags
, indio_dev
->name
, indio_dev
);
290 goto error_kfifo_free
;
292 indio_dev
->setup_ops
= setup_ops
;
293 indio_dev
->modes
|= INDIO_BUFFER_SOFTWARE
;
298 iio_kfifo_free(indio_dev
->buffer
);
302 static void tiadc_iio_buffered_hardware_remove(struct iio_dev
*indio_dev
)
304 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
306 free_irq(adc_dev
->mfd_tscadc
->irq
, indio_dev
);
307 iio_kfifo_free(indio_dev
->buffer
);
311 static const char * const chan_name_ain
[] = {
322 static int tiadc_channel_init(struct iio_dev
*indio_dev
, int channels
)
324 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
325 struct iio_chan_spec
*chan_array
;
326 struct iio_chan_spec
*chan
;
329 indio_dev
->num_channels
= channels
;
330 chan_array
= kcalloc(channels
, sizeof(*chan_array
), GFP_KERNEL
);
331 if (chan_array
== NULL
)
335 for (i
= 0; i
< channels
; i
++, chan
++) {
337 chan
->type
= IIO_VOLTAGE
;
339 chan
->channel
= adc_dev
->channel_line
[i
];
340 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
341 chan
->datasheet_name
= chan_name_ain
[chan
->channel
];
342 chan
->scan_index
= i
;
343 chan
->scan_type
.sign
= 'u';
344 chan
->scan_type
.realbits
= 12;
345 chan
->scan_type
.storagebits
= 16;
348 indio_dev
->channels
= chan_array
;
353 static void tiadc_channels_remove(struct iio_dev
*indio_dev
)
355 kfree(indio_dev
->channels
);
358 static int tiadc_read_raw(struct iio_dev
*indio_dev
,
359 struct iio_chan_spec
const *chan
,
360 int *val
, int *val2
, long mask
)
362 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
363 int ret
= IIO_VAL_INT
;
365 unsigned int fifo1count
, read
, stepid
;
368 unsigned long timeout
;
370 if (iio_buffer_enabled(indio_dev
))
373 step_en
= get_adc_chan_step_mask(adc_dev
, chan
);
377 mutex_lock(&adc_dev
->fifo1_lock
);
378 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
380 tiadc_readl(adc_dev
, REG_FIFO1
);
382 am335x_tsc_se_set_once(adc_dev
->mfd_tscadc
, step_en
);
384 timeout
= jiffies
+ msecs_to_jiffies
385 (IDLE_TIMEOUT
* adc_dev
->channels
);
386 /* Wait for Fifo threshold interrupt */
388 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
392 if (time_after(jiffies
, timeout
)) {
393 am335x_tsc_se_adc_done(adc_dev
->mfd_tscadc
);
398 map_val
= adc_dev
->channel_step
[chan
->scan_index
];
401 * We check the complete FIFO. We programmed just one entry but in case
402 * something went wrong we left empty handed (-EAGAIN previously) and
403 * then the value apeared somehow in the FIFO we would have two entries.
404 * Therefore we read every item and keep only the latest version of the
407 for (i
= 0; i
< fifo1count
; i
++) {
408 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
409 stepid
= read
& FIFOREAD_CHNLID_MASK
;
410 stepid
= stepid
>> 0x10;
412 if (stepid
== map_val
) {
413 read
= read
& FIFOREAD_DATA_MASK
;
418 am335x_tsc_se_adc_done(adc_dev
->mfd_tscadc
);
424 mutex_unlock(&adc_dev
->fifo1_lock
);
428 static const struct iio_info tiadc_info
= {
429 .read_raw
= &tiadc_read_raw
,
430 .driver_module
= THIS_MODULE
,
433 static int tiadc_parse_dt(struct platform_device
*pdev
,
434 struct tiadc_device
*adc_dev
)
436 struct device_node
*node
= pdev
->dev
.of_node
;
437 struct property
*prop
;
442 of_property_for_each_u32(node
, "ti,adc-channels", prop
, cur
, val
) {
443 adc_dev
->channel_line
[channels
] = val
;
445 /* Set Default values for optional DT parameters */
446 adc_dev
->open_delay
[channels
] = STEPCONFIG_OPENDLY
;
447 adc_dev
->sample_delay
[channels
] = STEPCONFIG_SAMPLEDLY
;
448 adc_dev
->step_avg
[channels
] = 16;
453 of_property_read_u32_array(node
, "ti,chan-step-avg",
454 adc_dev
->step_avg
, channels
);
455 of_property_read_u32_array(node
, "ti,chan-step-opendelay",
456 adc_dev
->open_delay
, channels
);
457 of_property_read_u32_array(node
, "ti,chan-step-sampledelay",
458 adc_dev
->sample_delay
, channels
);
460 adc_dev
->channels
= channels
;
464 static int tiadc_probe(struct platform_device
*pdev
)
466 struct iio_dev
*indio_dev
;
467 struct tiadc_device
*adc_dev
;
468 struct device_node
*node
= pdev
->dev
.of_node
;
472 dev_err(&pdev
->dev
, "Could not find valid DT data.\n");
476 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*indio_dev
));
477 if (indio_dev
== NULL
) {
478 dev_err(&pdev
->dev
, "failed to allocate iio device\n");
481 adc_dev
= iio_priv(indio_dev
);
483 adc_dev
->mfd_tscadc
= ti_tscadc_dev_get(pdev
);
484 tiadc_parse_dt(pdev
, adc_dev
);
486 indio_dev
->dev
.parent
= &pdev
->dev
;
487 indio_dev
->name
= dev_name(&pdev
->dev
);
488 indio_dev
->modes
= INDIO_DIRECT_MODE
;
489 indio_dev
->info
= &tiadc_info
;
491 tiadc_step_config(indio_dev
);
492 tiadc_writel(adc_dev
, REG_FIFO1THR
, FIFO1_THRESHOLD
);
493 mutex_init(&adc_dev
->fifo1_lock
);
495 err
= tiadc_channel_init(indio_dev
, adc_dev
->channels
);
499 err
= tiadc_iio_buffered_hardware_setup(indio_dev
,
502 adc_dev
->mfd_tscadc
->irq
,
504 &tiadc_buffer_setup_ops
);
507 goto err_free_channels
;
509 err
= iio_device_register(indio_dev
);
511 goto err_buffer_unregister
;
513 platform_set_drvdata(pdev
, indio_dev
);
517 err_buffer_unregister
:
518 tiadc_iio_buffered_hardware_remove(indio_dev
);
520 tiadc_channels_remove(indio_dev
);
524 static int tiadc_remove(struct platform_device
*pdev
)
526 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
527 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
530 iio_device_unregister(indio_dev
);
531 tiadc_iio_buffered_hardware_remove(indio_dev
);
532 tiadc_channels_remove(indio_dev
);
534 step_en
= get_adc_step_mask(adc_dev
);
535 am335x_tsc_se_clr(adc_dev
->mfd_tscadc
, step_en
);
540 static int __maybe_unused
tiadc_suspend(struct device
*dev
)
542 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
543 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
544 struct ti_tscadc_dev
*tscadc_dev
;
547 tscadc_dev
= ti_tscadc_dev_get(to_platform_device(dev
));
548 if (!device_may_wakeup(tscadc_dev
->dev
)) {
549 idle
= tiadc_readl(adc_dev
, REG_CTRL
);
550 idle
&= ~(CNTRLREG_TSCSSENB
);
551 tiadc_writel(adc_dev
, REG_CTRL
, (idle
|
552 CNTRLREG_POWERDOWN
));
558 static int __maybe_unused
tiadc_resume(struct device
*dev
)
560 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
561 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
562 unsigned int restore
;
564 /* Make sure ADC is powered up */
565 restore
= tiadc_readl(adc_dev
, REG_CTRL
);
566 restore
&= ~(CNTRLREG_POWERDOWN
);
567 tiadc_writel(adc_dev
, REG_CTRL
, restore
);
569 tiadc_step_config(indio_dev
);
570 am335x_tsc_se_set_cache(adc_dev
->mfd_tscadc
,
571 adc_dev
->buffer_en_ch_steps
);
575 static SIMPLE_DEV_PM_OPS(tiadc_pm_ops
, tiadc_suspend
, tiadc_resume
);
577 static const struct of_device_id ti_adc_dt_ids
[] = {
578 { .compatible
= "ti,am3359-adc", },
581 MODULE_DEVICE_TABLE(of
, ti_adc_dt_ids
);
583 static struct platform_driver tiadc_driver
= {
585 .name
= "TI-am335x-adc",
587 .of_match_table
= ti_adc_dt_ids
,
589 .probe
= tiadc_probe
,
590 .remove
= tiadc_remove
,
592 module_platform_driver(tiadc_driver
);
594 MODULE_DESCRIPTION("TI ADC controller driver");
595 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
596 MODULE_LICENSE("GPL");