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 struct iio_buffer
*buffer
= indio_dev
->buffer
;
192 unsigned int enb
= 0;
195 tiadc_step_config(indio_dev
);
196 for_each_set_bit(bit
, buffer
->scan_mask
, adc_dev
->channels
)
197 enb
|= (get_adc_step_bit(adc_dev
, bit
) << 1);
198 adc_dev
->buffer_en_ch_steps
= enb
;
200 am335x_tsc_se_set_cache(adc_dev
->mfd_tscadc
, enb
);
202 tiadc_writel(adc_dev
, REG_IRQSTATUS
, IRQENB_FIFO1THRES
203 | IRQENB_FIFO1OVRRUN
| IRQENB_FIFO1UNDRFLW
);
204 tiadc_writel(adc_dev
, REG_IRQENABLE
, IRQENB_FIFO1THRES
205 | IRQENB_FIFO1OVRRUN
);
210 static int tiadc_buffer_predisable(struct iio_dev
*indio_dev
)
212 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
213 int fifo1count
, i
, read
;
215 tiadc_writel(adc_dev
, REG_IRQCLR
, (IRQENB_FIFO1THRES
|
216 IRQENB_FIFO1OVRRUN
| IRQENB_FIFO1UNDRFLW
));
217 am335x_tsc_se_clr(adc_dev
->mfd_tscadc
, adc_dev
->buffer_en_ch_steps
);
218 adc_dev
->buffer_en_ch_steps
= 0;
220 /* Flush FIFO of leftover data in the time it takes to disable adc */
221 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
222 for (i
= 0; i
< fifo1count
; i
++)
223 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
228 static int tiadc_buffer_postdisable(struct iio_dev
*indio_dev
)
230 tiadc_step_config(indio_dev
);
235 static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops
= {
236 .preenable
= &tiadc_buffer_preenable
,
237 .postenable
= &tiadc_buffer_postenable
,
238 .predisable
= &tiadc_buffer_predisable
,
239 .postdisable
= &tiadc_buffer_postdisable
,
242 static int tiadc_iio_buffered_hardware_setup(struct iio_dev
*indio_dev
,
243 irqreturn_t (*pollfunc_bh
)(int irq
, void *p
),
244 irqreturn_t (*pollfunc_th
)(int irq
, void *p
),
247 const struct iio_buffer_setup_ops
*setup_ops
)
249 struct iio_buffer
*buffer
;
252 buffer
= iio_kfifo_allocate();
256 iio_device_attach_buffer(indio_dev
, buffer
);
258 ret
= request_threaded_irq(irq
, pollfunc_th
, pollfunc_bh
,
259 flags
, indio_dev
->name
, indio_dev
);
261 goto error_kfifo_free
;
263 indio_dev
->setup_ops
= setup_ops
;
264 indio_dev
->modes
|= INDIO_BUFFER_HARDWARE
;
269 iio_kfifo_free(indio_dev
->buffer
);
273 static void tiadc_iio_buffered_hardware_remove(struct iio_dev
*indio_dev
)
275 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
277 free_irq(adc_dev
->mfd_tscadc
->irq
, indio_dev
);
278 iio_kfifo_free(indio_dev
->buffer
);
282 static const char * const chan_name_ain
[] = {
293 static int tiadc_channel_init(struct iio_dev
*indio_dev
, int channels
)
295 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
296 struct iio_chan_spec
*chan_array
;
297 struct iio_chan_spec
*chan
;
300 indio_dev
->num_channels
= channels
;
301 chan_array
= kcalloc(channels
,
302 sizeof(struct iio_chan_spec
), GFP_KERNEL
);
303 if (chan_array
== NULL
)
307 for (i
= 0; i
< channels
; i
++, chan
++) {
309 chan
->type
= IIO_VOLTAGE
;
311 chan
->channel
= adc_dev
->channel_line
[i
];
312 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
313 chan
->datasheet_name
= chan_name_ain
[chan
->channel
];
314 chan
->scan_index
= i
;
315 chan
->scan_type
.sign
= 'u';
316 chan
->scan_type
.realbits
= 12;
317 chan
->scan_type
.storagebits
= 16;
320 indio_dev
->channels
= chan_array
;
325 static void tiadc_channels_remove(struct iio_dev
*indio_dev
)
327 kfree(indio_dev
->channels
);
330 static int tiadc_read_raw(struct iio_dev
*indio_dev
,
331 struct iio_chan_spec
const *chan
,
332 int *val
, int *val2
, long mask
)
334 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
336 unsigned int fifo1count
, read
, stepid
;
339 unsigned long timeout
;
341 if (iio_buffer_enabled(indio_dev
))
344 step_en
= get_adc_chan_step_mask(adc_dev
, chan
);
348 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
350 tiadc_readl(adc_dev
, REG_FIFO1
);
352 am335x_tsc_se_set_once(adc_dev
->mfd_tscadc
, step_en
);
354 timeout
= jiffies
+ usecs_to_jiffies
355 (IDLE_TIMEOUT
* adc_dev
->channels
);
356 /* Wait for Fifo threshold interrupt */
358 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
362 if (time_after(jiffies
, timeout
)) {
363 am335x_tsc_se_adc_done(adc_dev
->mfd_tscadc
);
367 map_val
= adc_dev
->channel_step
[chan
->scan_index
];
370 * We check the complete FIFO. We programmed just one entry but in case
371 * something went wrong we left empty handed (-EAGAIN previously) and
372 * then the value apeared somehow in the FIFO we would have two entries.
373 * Therefore we read every item and keep only the latest version of the
376 for (i
= 0; i
< fifo1count
; i
++) {
377 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
378 stepid
= read
& FIFOREAD_CHNLID_MASK
;
379 stepid
= stepid
>> 0x10;
381 if (stepid
== map_val
) {
382 read
= read
& FIFOREAD_DATA_MASK
;
387 am335x_tsc_se_adc_done(adc_dev
->mfd_tscadc
);
394 static const struct iio_info tiadc_info
= {
395 .read_raw
= &tiadc_read_raw
,
396 .driver_module
= THIS_MODULE
,
399 static int tiadc_probe(struct platform_device
*pdev
)
401 struct iio_dev
*indio_dev
;
402 struct tiadc_device
*adc_dev
;
403 struct device_node
*node
= pdev
->dev
.of_node
;
404 struct property
*prop
;
411 dev_err(&pdev
->dev
, "Could not find valid DT data.\n");
415 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
416 sizeof(struct tiadc_device
));
417 if (indio_dev
== NULL
) {
418 dev_err(&pdev
->dev
, "failed to allocate iio device\n");
421 adc_dev
= iio_priv(indio_dev
);
423 adc_dev
->mfd_tscadc
= ti_tscadc_dev_get(pdev
);
425 of_property_for_each_u32(node
, "ti,adc-channels", prop
, cur
, val
) {
426 adc_dev
->channel_line
[channels
] = val
;
429 adc_dev
->channels
= channels
;
431 indio_dev
->dev
.parent
= &pdev
->dev
;
432 indio_dev
->name
= dev_name(&pdev
->dev
);
433 indio_dev
->modes
= INDIO_DIRECT_MODE
;
434 indio_dev
->info
= &tiadc_info
;
436 tiadc_step_config(indio_dev
);
437 tiadc_writel(adc_dev
, REG_FIFO1THR
, FIFO1_THRESHOLD
);
439 err
= tiadc_channel_init(indio_dev
, adc_dev
->channels
);
443 err
= tiadc_iio_buffered_hardware_setup(indio_dev
,
446 adc_dev
->mfd_tscadc
->irq
,
448 &tiadc_buffer_setup_ops
);
451 goto err_free_channels
;
453 err
= iio_device_register(indio_dev
);
455 goto err_buffer_unregister
;
457 platform_set_drvdata(pdev
, indio_dev
);
461 err_buffer_unregister
:
462 tiadc_iio_buffered_hardware_remove(indio_dev
);
464 tiadc_channels_remove(indio_dev
);
468 static int tiadc_remove(struct platform_device
*pdev
)
470 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
471 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
474 iio_device_unregister(indio_dev
);
475 tiadc_iio_buffered_hardware_remove(indio_dev
);
476 tiadc_channels_remove(indio_dev
);
478 step_en
= get_adc_step_mask(adc_dev
);
479 am335x_tsc_se_clr(adc_dev
->mfd_tscadc
, step_en
);
485 static int tiadc_suspend(struct device
*dev
)
487 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
488 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
489 struct ti_tscadc_dev
*tscadc_dev
;
492 tscadc_dev
= ti_tscadc_dev_get(to_platform_device(dev
));
493 if (!device_may_wakeup(tscadc_dev
->dev
)) {
494 idle
= tiadc_readl(adc_dev
, REG_CTRL
);
495 idle
&= ~(CNTRLREG_TSCSSENB
);
496 tiadc_writel(adc_dev
, REG_CTRL
, (idle
|
497 CNTRLREG_POWERDOWN
));
503 static int tiadc_resume(struct device
*dev
)
505 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
506 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
507 unsigned int restore
;
509 /* Make sure ADC is powered up */
510 restore
= tiadc_readl(adc_dev
, REG_CTRL
);
511 restore
&= ~(CNTRLREG_POWERDOWN
);
512 tiadc_writel(adc_dev
, REG_CTRL
, restore
);
514 tiadc_step_config(indio_dev
);
515 am335x_tsc_se_set_cache(adc_dev
->mfd_tscadc
,
516 adc_dev
->buffer_en_ch_steps
);
520 static const struct dev_pm_ops tiadc_pm_ops
= {
521 .suspend
= tiadc_suspend
,
522 .resume
= tiadc_resume
,
524 #define TIADC_PM_OPS (&tiadc_pm_ops)
526 #define TIADC_PM_OPS NULL
529 static const struct of_device_id ti_adc_dt_ids
[] = {
530 { .compatible
= "ti,am3359-adc", },
533 MODULE_DEVICE_TABLE(of
, ti_adc_dt_ids
);
535 static struct platform_driver tiadc_driver
= {
537 .name
= "TI-am335x-adc",
539 .of_match_table
= ti_adc_dt_ids
,
541 .probe
= tiadc_probe
,
542 .remove
= tiadc_remove
,
544 module_platform_driver(tiadc_driver
);
546 MODULE_DESCRIPTION("TI ADC controller driver");
547 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
548 MODULE_LICENSE("GPL");