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
;
38 int buffer_en_ch_steps
;
42 static unsigned int tiadc_readl(struct tiadc_device
*adc
, unsigned int reg
)
44 return readl(adc
->mfd_tscadc
->tscadc_base
+ reg
);
47 static void tiadc_writel(struct tiadc_device
*adc
, unsigned int reg
,
50 writel(val
, adc
->mfd_tscadc
->tscadc_base
+ reg
);
53 static u32
get_adc_step_mask(struct tiadc_device
*adc_dev
)
57 step_en
= ((1 << adc_dev
->channels
) - 1);
58 step_en
<<= TOTAL_STEPS
- adc_dev
->channels
+ 1;
62 static u32
get_adc_chan_step_mask(struct tiadc_device
*adc_dev
,
63 struct iio_chan_spec
const *chan
)
67 for (i
= 0; i
< ARRAY_SIZE(adc_dev
->channel_step
); i
++) {
68 if (chan
->channel
== adc_dev
->channel_line
[i
]) {
71 step
= adc_dev
->channel_step
[i
];
72 /* +1 for the charger */
73 return 1 << (step
+ 1);
80 static u32
get_adc_step_bit(struct tiadc_device
*adc_dev
, int chan
)
82 return 1 << adc_dev
->channel_step
[chan
];
85 static void tiadc_step_config(struct iio_dev
*indio_dev
)
87 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
88 unsigned int stepconfig
;
92 * There are 16 configurable steps and 8 analog input
93 * lines available which are shared between Touchscreen and ADC.
95 * Steps forwards i.e. from 0 towards 16 are used by ADC
96 * depending on number of input lines needed.
97 * Channel would represent which analog input
98 * needs to be given to ADC to digitalize data.
101 if (iio_buffer_enabled(indio_dev
))
102 stepconfig
= STEPCONFIG_AVG_16
| STEPCONFIG_FIFO1
103 | STEPCONFIG_MODE_SWCNT
;
105 stepconfig
= STEPCONFIG_AVG_16
| STEPCONFIG_FIFO1
;
107 for (i
= 0; i
< adc_dev
->channels
; i
++) {
110 chan
= adc_dev
->channel_line
[i
];
111 tiadc_writel(adc_dev
, REG_STEPCONFIG(steps
),
112 stepconfig
| STEPCONFIG_INP(chan
));
113 tiadc_writel(adc_dev
, REG_STEPDELAY(steps
),
115 adc_dev
->channel_step
[i
] = steps
;
120 static irqreturn_t
tiadc_irq_h(int irq
, void *private)
122 struct iio_dev
*indio_dev
= private;
123 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
124 unsigned int status
, config
;
125 status
= tiadc_readl(adc_dev
, REG_IRQSTATUS
);
128 * ADC and touchscreen share the IRQ line.
129 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
131 if (status
& IRQENB_FIFO1OVRRUN
) {
132 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
133 config
= tiadc_readl(adc_dev
, REG_CTRL
);
134 config
&= ~(CNTRLREG_TSCSSENB
);
135 tiadc_writel(adc_dev
, REG_CTRL
, config
);
136 tiadc_writel(adc_dev
, REG_IRQSTATUS
, IRQENB_FIFO1OVRRUN
137 | IRQENB_FIFO1UNDRFLW
| IRQENB_FIFO1THRES
);
138 tiadc_writel(adc_dev
, REG_CTRL
, (config
| CNTRLREG_TSCSSENB
));
140 } else if (status
& IRQENB_FIFO1THRES
) {
141 /* Disable irq and wake worker thread */
142 tiadc_writel(adc_dev
, REG_IRQCLR
, IRQENB_FIFO1THRES
);
143 return IRQ_WAKE_THREAD
;
149 static irqreturn_t
tiadc_worker_h(int irq
, void *private)
151 struct iio_dev
*indio_dev
= private;
152 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
153 int i
, k
, fifo1count
, read
;
154 u16
*data
= adc_dev
->data
;
156 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
157 for (k
= 0; k
< fifo1count
; k
= k
+ i
) {
158 for (i
= 0; i
< (indio_dev
->scan_bytes
)/2; i
++) {
159 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
160 data
[i
] = read
& FIFOREAD_DATA_MASK
;
162 iio_push_to_buffers(indio_dev
, (u8
*) data
);
165 tiadc_writel(adc_dev
, REG_IRQSTATUS
, IRQENB_FIFO1THRES
);
166 tiadc_writel(adc_dev
, REG_IRQENABLE
, IRQENB_FIFO1THRES
);
171 static int tiadc_buffer_preenable(struct iio_dev
*indio_dev
)
173 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
174 int i
, fifo1count
, read
;
176 tiadc_writel(adc_dev
, REG_IRQCLR
, (IRQENB_FIFO1THRES
|
178 IRQENB_FIFO1UNDRFLW
));
180 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
181 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
182 for (i
= 0; i
< fifo1count
; i
++)
183 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
188 static int tiadc_buffer_postenable(struct iio_dev
*indio_dev
)
190 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
191 unsigned int enb
= 0;
194 tiadc_step_config(indio_dev
);
195 for_each_set_bit(bit
, indio_dev
->active_scan_mask
, adc_dev
->channels
)
196 enb
|= (get_adc_step_bit(adc_dev
, bit
) << 1);
197 adc_dev
->buffer_en_ch_steps
= enb
;
199 am335x_tsc_se_set_cache(adc_dev
->mfd_tscadc
, enb
);
201 tiadc_writel(adc_dev
, REG_IRQSTATUS
, IRQENB_FIFO1THRES
202 | IRQENB_FIFO1OVRRUN
| IRQENB_FIFO1UNDRFLW
);
203 tiadc_writel(adc_dev
, REG_IRQENABLE
, IRQENB_FIFO1THRES
204 | IRQENB_FIFO1OVRRUN
);
209 static int tiadc_buffer_predisable(struct iio_dev
*indio_dev
)
211 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
212 int fifo1count
, i
, read
;
214 tiadc_writel(adc_dev
, REG_IRQCLR
, (IRQENB_FIFO1THRES
|
215 IRQENB_FIFO1OVRRUN
| IRQENB_FIFO1UNDRFLW
));
216 am335x_tsc_se_clr(adc_dev
->mfd_tscadc
, adc_dev
->buffer_en_ch_steps
);
217 adc_dev
->buffer_en_ch_steps
= 0;
219 /* Flush FIFO of leftover data in the time it takes to disable adc */
220 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
221 for (i
= 0; i
< fifo1count
; i
++)
222 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
227 static int tiadc_buffer_postdisable(struct iio_dev
*indio_dev
)
229 tiadc_step_config(indio_dev
);
234 static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops
= {
235 .preenable
= &tiadc_buffer_preenable
,
236 .postenable
= &tiadc_buffer_postenable
,
237 .predisable
= &tiadc_buffer_predisable
,
238 .postdisable
= &tiadc_buffer_postdisable
,
241 static int tiadc_iio_buffered_hardware_setup(struct iio_dev
*indio_dev
,
242 irqreturn_t (*pollfunc_bh
)(int irq
, void *p
),
243 irqreturn_t (*pollfunc_th
)(int irq
, void *p
),
246 const struct iio_buffer_setup_ops
*setup_ops
)
248 struct iio_buffer
*buffer
;
251 buffer
= iio_kfifo_allocate();
255 iio_device_attach_buffer(indio_dev
, buffer
);
257 ret
= request_threaded_irq(irq
, pollfunc_th
, pollfunc_bh
,
258 flags
, indio_dev
->name
, indio_dev
);
260 goto error_kfifo_free
;
262 indio_dev
->setup_ops
= setup_ops
;
263 indio_dev
->modes
|= INDIO_BUFFER_HARDWARE
;
268 iio_kfifo_free(indio_dev
->buffer
);
272 static void tiadc_iio_buffered_hardware_remove(struct iio_dev
*indio_dev
)
274 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
276 free_irq(adc_dev
->mfd_tscadc
->irq
, indio_dev
);
277 iio_kfifo_free(indio_dev
->buffer
);
281 static const char * const chan_name_ain
[] = {
292 static int tiadc_channel_init(struct iio_dev
*indio_dev
, int channels
)
294 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
295 struct iio_chan_spec
*chan_array
;
296 struct iio_chan_spec
*chan
;
299 indio_dev
->num_channels
= channels
;
300 chan_array
= kcalloc(channels
,
301 sizeof(struct iio_chan_spec
), GFP_KERNEL
);
302 if (chan_array
== NULL
)
306 for (i
= 0; i
< channels
; i
++, chan
++) {
308 chan
->type
= IIO_VOLTAGE
;
310 chan
->channel
= adc_dev
->channel_line
[i
];
311 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
312 chan
->datasheet_name
= chan_name_ain
[chan
->channel
];
313 chan
->scan_index
= i
;
314 chan
->scan_type
.sign
= 'u';
315 chan
->scan_type
.realbits
= 12;
316 chan
->scan_type
.storagebits
= 16;
319 indio_dev
->channels
= chan_array
;
324 static void tiadc_channels_remove(struct iio_dev
*indio_dev
)
326 kfree(indio_dev
->channels
);
329 static int tiadc_read_raw(struct iio_dev
*indio_dev
,
330 struct iio_chan_spec
const *chan
,
331 int *val
, int *val2
, long mask
)
333 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
335 unsigned int fifo1count
, read
, stepid
;
338 unsigned long timeout
;
340 if (iio_buffer_enabled(indio_dev
))
343 step_en
= get_adc_chan_step_mask(adc_dev
, chan
);
347 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
349 tiadc_readl(adc_dev
, REG_FIFO1
);
351 am335x_tsc_se_set_once(adc_dev
->mfd_tscadc
, step_en
);
353 timeout
= jiffies
+ usecs_to_jiffies
354 (IDLE_TIMEOUT
* adc_dev
->channels
);
355 /* Wait for Fifo threshold interrupt */
357 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
361 if (time_after(jiffies
, timeout
)) {
362 am335x_tsc_se_adc_done(adc_dev
->mfd_tscadc
);
366 map_val
= adc_dev
->channel_step
[chan
->scan_index
];
369 * We check the complete FIFO. We programmed just one entry but in case
370 * something went wrong we left empty handed (-EAGAIN previously) and
371 * then the value apeared somehow in the FIFO we would have two entries.
372 * Therefore we read every item and keep only the latest version of the
375 for (i
= 0; i
< fifo1count
; i
++) {
376 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
377 stepid
= read
& FIFOREAD_CHNLID_MASK
;
378 stepid
= stepid
>> 0x10;
380 if (stepid
== map_val
) {
381 read
= read
& FIFOREAD_DATA_MASK
;
386 am335x_tsc_se_adc_done(adc_dev
->mfd_tscadc
);
393 static const struct iio_info tiadc_info
= {
394 .read_raw
= &tiadc_read_raw
,
395 .driver_module
= THIS_MODULE
,
398 static int tiadc_probe(struct platform_device
*pdev
)
400 struct iio_dev
*indio_dev
;
401 struct tiadc_device
*adc_dev
;
402 struct device_node
*node
= pdev
->dev
.of_node
;
403 struct property
*prop
;
410 dev_err(&pdev
->dev
, "Could not find valid DT data.\n");
414 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
415 sizeof(struct tiadc_device
));
416 if (indio_dev
== NULL
) {
417 dev_err(&pdev
->dev
, "failed to allocate iio device\n");
420 adc_dev
= iio_priv(indio_dev
);
422 adc_dev
->mfd_tscadc
= ti_tscadc_dev_get(pdev
);
424 of_property_for_each_u32(node
, "ti,adc-channels", prop
, cur
, val
) {
425 adc_dev
->channel_line
[channels
] = val
;
428 adc_dev
->channels
= channels
;
430 indio_dev
->dev
.parent
= &pdev
->dev
;
431 indio_dev
->name
= dev_name(&pdev
->dev
);
432 indio_dev
->modes
= INDIO_DIRECT_MODE
;
433 indio_dev
->info
= &tiadc_info
;
435 tiadc_step_config(indio_dev
);
436 tiadc_writel(adc_dev
, REG_FIFO1THR
, FIFO1_THRESHOLD
);
438 err
= tiadc_channel_init(indio_dev
, adc_dev
->channels
);
442 err
= tiadc_iio_buffered_hardware_setup(indio_dev
,
445 adc_dev
->mfd_tscadc
->irq
,
447 &tiadc_buffer_setup_ops
);
450 goto err_free_channels
;
452 err
= iio_device_register(indio_dev
);
454 goto err_buffer_unregister
;
456 platform_set_drvdata(pdev
, indio_dev
);
460 err_buffer_unregister
:
461 tiadc_iio_buffered_hardware_remove(indio_dev
);
463 tiadc_channels_remove(indio_dev
);
467 static int tiadc_remove(struct platform_device
*pdev
)
469 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
470 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
473 iio_device_unregister(indio_dev
);
474 tiadc_iio_buffered_hardware_remove(indio_dev
);
475 tiadc_channels_remove(indio_dev
);
477 step_en
= get_adc_step_mask(adc_dev
);
478 am335x_tsc_se_clr(adc_dev
->mfd_tscadc
, step_en
);
484 static int tiadc_suspend(struct device
*dev
)
486 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
487 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
488 struct ti_tscadc_dev
*tscadc_dev
;
491 tscadc_dev
= ti_tscadc_dev_get(to_platform_device(dev
));
492 if (!device_may_wakeup(tscadc_dev
->dev
)) {
493 idle
= tiadc_readl(adc_dev
, REG_CTRL
);
494 idle
&= ~(CNTRLREG_TSCSSENB
);
495 tiadc_writel(adc_dev
, REG_CTRL
, (idle
|
496 CNTRLREG_POWERDOWN
));
502 static int tiadc_resume(struct device
*dev
)
504 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
505 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
506 unsigned int restore
;
508 /* Make sure ADC is powered up */
509 restore
= tiadc_readl(adc_dev
, REG_CTRL
);
510 restore
&= ~(CNTRLREG_POWERDOWN
);
511 tiadc_writel(adc_dev
, REG_CTRL
, restore
);
513 tiadc_step_config(indio_dev
);
514 am335x_tsc_se_set_cache(adc_dev
->mfd_tscadc
,
515 adc_dev
->buffer_en_ch_steps
);
519 static const struct dev_pm_ops tiadc_pm_ops
= {
520 .suspend
= tiadc_suspend
,
521 .resume
= tiadc_resume
,
523 #define TIADC_PM_OPS (&tiadc_pm_ops)
525 #define TIADC_PM_OPS NULL
528 static const struct of_device_id ti_adc_dt_ids
[] = {
529 { .compatible
= "ti,am3359-adc", },
532 MODULE_DEVICE_TABLE(of
, ti_adc_dt_ids
);
534 static struct platform_driver tiadc_driver
= {
536 .name
= "TI-am335x-adc",
538 .of_match_table
= ti_adc_dt_ids
,
540 .probe
= tiadc_probe
,
541 .remove
= tiadc_remove
,
543 module_platform_driver(tiadc_driver
);
545 MODULE_DESCRIPTION("TI ADC controller driver");
546 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
547 MODULE_LICENSE("GPL");