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/init.h>
17 #include <linux/kernel.h>
18 #include <linux/err.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/platform_device.h>
24 #include <linux/iio/iio.h>
26 #include <linux/of_device.h>
27 #include <linux/iio/machine.h>
28 #include <linux/iio/driver.h>
30 #include <linux/mfd/ti_am335x_tscadc.h>
31 #include <linux/iio/buffer.h>
32 #include <linux/iio/kfifo_buf.h>
35 struct ti_tscadc_dev
*mfd_tscadc
;
39 int buffer_en_ch_steps
;
43 static unsigned int tiadc_readl(struct tiadc_device
*adc
, unsigned int reg
)
45 return readl(adc
->mfd_tscadc
->tscadc_base
+ reg
);
48 static void tiadc_writel(struct tiadc_device
*adc
, unsigned int reg
,
51 writel(val
, adc
->mfd_tscadc
->tscadc_base
+ reg
);
54 static u32
get_adc_step_mask(struct tiadc_device
*adc_dev
)
58 step_en
= ((1 << adc_dev
->channels
) - 1);
59 step_en
<<= TOTAL_STEPS
- adc_dev
->channels
+ 1;
63 static u32
get_adc_chan_step_mask(struct tiadc_device
*adc_dev
,
64 struct iio_chan_spec
const *chan
)
68 for (i
= 0; i
< ARRAY_SIZE(adc_dev
->channel_step
); i
++) {
69 if (chan
->channel
== adc_dev
->channel_line
[i
]) {
72 step
= adc_dev
->channel_step
[i
];
73 /* +1 for the charger */
74 return 1 << (step
+ 1);
81 static u32
get_adc_step_bit(struct tiadc_device
*adc_dev
, int chan
)
83 return 1 << adc_dev
->channel_step
[chan
];
86 static void tiadc_step_config(struct iio_dev
*indio_dev
)
88 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
89 unsigned int stepconfig
;
93 * There are 16 configurable steps and 8 analog input
94 * lines available which are shared between Touchscreen and ADC.
96 * Steps backwards i.e. from 16 towards 0 are used by ADC
97 * depending on number of input lines needed.
98 * Channel would represent which analog input
99 * needs to be given to ADC to digitalize data.
102 steps
= TOTAL_STEPS
- adc_dev
->channels
;
103 if (iio_buffer_enabled(indio_dev
))
104 stepconfig
= STEPCONFIG_AVG_16
| STEPCONFIG_FIFO1
105 | STEPCONFIG_MODE_SWCNT
;
107 stepconfig
= STEPCONFIG_AVG_16
| STEPCONFIG_FIFO1
;
109 for (i
= 0; i
< adc_dev
->channels
; i
++) {
112 chan
= adc_dev
->channel_line
[i
];
113 tiadc_writel(adc_dev
, REG_STEPCONFIG(steps
),
114 stepconfig
| STEPCONFIG_INP(chan
));
115 tiadc_writel(adc_dev
, REG_STEPDELAY(steps
),
117 adc_dev
->channel_step
[i
] = steps
;
122 static irqreturn_t
tiadc_irq_h(int irq
, void *private)
124 struct iio_dev
*indio_dev
= private;
125 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
126 unsigned int status
, config
;
127 status
= tiadc_readl(adc_dev
, REG_IRQSTATUS
);
130 * ADC and touchscreen share the IRQ line.
131 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
133 if (status
& IRQENB_FIFO1OVRRUN
) {
134 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
135 config
= tiadc_readl(adc_dev
, REG_CTRL
);
136 config
&= ~(CNTRLREG_TSCSSENB
);
137 tiadc_writel(adc_dev
, REG_CTRL
, config
);
138 tiadc_writel(adc_dev
, REG_IRQSTATUS
, IRQENB_FIFO1OVRRUN
139 | IRQENB_FIFO1UNDRFLW
| IRQENB_FIFO1THRES
);
140 tiadc_writel(adc_dev
, REG_CTRL
, (config
| CNTRLREG_TSCSSENB
));
142 } else if (status
& IRQENB_FIFO1THRES
) {
143 /* Disable irq and wake worker thread */
144 tiadc_writel(adc_dev
, REG_IRQCLR
, IRQENB_FIFO1THRES
);
145 return IRQ_WAKE_THREAD
;
151 static irqreturn_t
tiadc_worker_h(int irq
, void *private)
153 struct iio_dev
*indio_dev
= private;
154 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
155 int i
, k
, fifo1count
, read
;
156 u16
*data
= adc_dev
->data
;
158 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
159 for (k
= 0; k
< fifo1count
; k
= k
+ i
) {
160 for (i
= 0; i
< (indio_dev
->scan_bytes
)/2; i
++) {
161 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
162 data
[i
] = read
& FIFOREAD_DATA_MASK
;
164 iio_push_to_buffers(indio_dev
, (u8
*) data
);
167 tiadc_writel(adc_dev
, REG_IRQSTATUS
, IRQENB_FIFO1THRES
);
168 tiadc_writel(adc_dev
, REG_IRQENABLE
, IRQENB_FIFO1THRES
);
173 static int tiadc_buffer_preenable(struct iio_dev
*indio_dev
)
175 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
176 int i
, fifo1count
, read
;
178 tiadc_writel(adc_dev
, REG_IRQCLR
, (IRQENB_FIFO1THRES
|
180 IRQENB_FIFO1UNDRFLW
));
182 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
183 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
184 for (i
= 0; i
< fifo1count
; i
++)
185 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
190 static int tiadc_buffer_postenable(struct iio_dev
*indio_dev
)
192 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
193 struct iio_buffer
*buffer
= indio_dev
->buffer
;
194 unsigned int enb
= 0;
197 tiadc_step_config(indio_dev
);
198 for_each_set_bit(bit
, buffer
->scan_mask
, adc_dev
->channels
)
199 enb
|= (get_adc_step_bit(adc_dev
, bit
) << 1);
200 adc_dev
->buffer_en_ch_steps
= enb
;
202 am335x_tsc_se_set_cache(adc_dev
->mfd_tscadc
, enb
);
204 tiadc_writel(adc_dev
, REG_IRQSTATUS
, IRQENB_FIFO1THRES
205 | IRQENB_FIFO1OVRRUN
| IRQENB_FIFO1UNDRFLW
);
206 tiadc_writel(adc_dev
, REG_IRQENABLE
, IRQENB_FIFO1THRES
207 | IRQENB_FIFO1OVRRUN
);
212 static int tiadc_buffer_predisable(struct iio_dev
*indio_dev
)
214 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
215 int fifo1count
, i
, read
;
217 tiadc_writel(adc_dev
, REG_IRQCLR
, (IRQENB_FIFO1THRES
|
218 IRQENB_FIFO1OVRRUN
| IRQENB_FIFO1UNDRFLW
));
219 am335x_tsc_se_clr(adc_dev
->mfd_tscadc
, adc_dev
->buffer_en_ch_steps
);
220 adc_dev
->buffer_en_ch_steps
= 0;
222 /* Flush FIFO of leftover data in the time it takes to disable adc */
223 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
224 for (i
= 0; i
< fifo1count
; i
++)
225 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
230 static int tiadc_buffer_postdisable(struct iio_dev
*indio_dev
)
232 tiadc_step_config(indio_dev
);
237 static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops
= {
238 .preenable
= &tiadc_buffer_preenable
,
239 .postenable
= &tiadc_buffer_postenable
,
240 .predisable
= &tiadc_buffer_predisable
,
241 .postdisable
= &tiadc_buffer_postdisable
,
244 static int tiadc_iio_buffered_hardware_setup(struct iio_dev
*indio_dev
,
245 irqreturn_t (*pollfunc_bh
)(int irq
, void *p
),
246 irqreturn_t (*pollfunc_th
)(int irq
, void *p
),
249 const struct iio_buffer_setup_ops
*setup_ops
)
251 struct iio_buffer
*buffer
;
254 buffer
= iio_kfifo_allocate(indio_dev
);
258 iio_device_attach_buffer(indio_dev
, buffer
);
260 ret
= request_threaded_irq(irq
, pollfunc_th
, pollfunc_bh
,
261 flags
, indio_dev
->name
, indio_dev
);
263 goto error_kfifo_free
;
265 indio_dev
->setup_ops
= setup_ops
;
266 indio_dev
->modes
|= INDIO_BUFFER_HARDWARE
;
268 ret
= iio_buffer_register(indio_dev
,
270 indio_dev
->num_channels
);
277 free_irq(irq
, indio_dev
);
279 iio_kfifo_free(indio_dev
->buffer
);
283 static void tiadc_iio_buffered_hardware_remove(struct iio_dev
*indio_dev
)
285 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
287 free_irq(adc_dev
->mfd_tscadc
->irq
, indio_dev
);
288 iio_kfifo_free(indio_dev
->buffer
);
289 iio_buffer_unregister(indio_dev
);
293 static const char * const chan_name_ain
[] = {
304 static int tiadc_channel_init(struct iio_dev
*indio_dev
, int channels
)
306 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
307 struct iio_chan_spec
*chan_array
;
308 struct iio_chan_spec
*chan
;
311 indio_dev
->num_channels
= channels
;
312 chan_array
= kcalloc(channels
,
313 sizeof(struct iio_chan_spec
), GFP_KERNEL
);
314 if (chan_array
== NULL
)
318 for (i
= 0; i
< channels
; i
++, chan
++) {
320 chan
->type
= IIO_VOLTAGE
;
322 chan
->channel
= adc_dev
->channel_line
[i
];
323 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
324 chan
->datasheet_name
= chan_name_ain
[chan
->channel
];
325 chan
->scan_index
= i
;
326 chan
->scan_type
.sign
= 'u';
327 chan
->scan_type
.realbits
= 12;
328 chan
->scan_type
.storagebits
= 16;
331 indio_dev
->channels
= chan_array
;
336 static void tiadc_channels_remove(struct iio_dev
*indio_dev
)
338 kfree(indio_dev
->channels
);
341 static int tiadc_read_raw(struct iio_dev
*indio_dev
,
342 struct iio_chan_spec
const *chan
,
343 int *val
, int *val2
, long mask
)
345 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
347 unsigned int fifo1count
, read
, stepid
;
350 unsigned long timeout
;
352 if (iio_buffer_enabled(indio_dev
))
355 step_en
= get_adc_chan_step_mask(adc_dev
, chan
);
359 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
361 tiadc_readl(adc_dev
, REG_FIFO1
);
363 am335x_tsc_se_set_once(adc_dev
->mfd_tscadc
, step_en
);
365 timeout
= jiffies
+ usecs_to_jiffies
366 (IDLE_TIMEOUT
* adc_dev
->channels
);
367 /* Wait for Fifo threshold interrupt */
369 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
373 if (time_after(jiffies
, timeout
)) {
374 am335x_tsc_se_adc_done(adc_dev
->mfd_tscadc
);
378 map_val
= chan
->channel
+ TOTAL_CHANNELS
;
381 * We check the complete FIFO. We programmed just one entry but in case
382 * something went wrong we left empty handed (-EAGAIN previously) and
383 * then the value apeared somehow in the FIFO we would have two entries.
384 * Therefore we read every item and keep only the latest version of the
387 for (i
= 0; i
< fifo1count
; i
++) {
388 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
389 stepid
= read
& FIFOREAD_CHNLID_MASK
;
390 stepid
= stepid
>> 0x10;
392 if (stepid
== map_val
) {
393 read
= read
& FIFOREAD_DATA_MASK
;
398 am335x_tsc_se_adc_done(adc_dev
->mfd_tscadc
);
405 static const struct iio_info tiadc_info
= {
406 .read_raw
= &tiadc_read_raw
,
407 .driver_module
= THIS_MODULE
,
410 static int tiadc_probe(struct platform_device
*pdev
)
412 struct iio_dev
*indio_dev
;
413 struct tiadc_device
*adc_dev
;
414 struct device_node
*node
= pdev
->dev
.of_node
;
415 struct property
*prop
;
422 dev_err(&pdev
->dev
, "Could not find valid DT data.\n");
426 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
427 sizeof(struct tiadc_device
));
428 if (indio_dev
== NULL
) {
429 dev_err(&pdev
->dev
, "failed to allocate iio device\n");
432 adc_dev
= iio_priv(indio_dev
);
434 adc_dev
->mfd_tscadc
= ti_tscadc_dev_get(pdev
);
436 of_property_for_each_u32(node
, "ti,adc-channels", prop
, cur
, val
) {
437 adc_dev
->channel_line
[channels
] = val
;
440 adc_dev
->channels
= channels
;
442 indio_dev
->dev
.parent
= &pdev
->dev
;
443 indio_dev
->name
= dev_name(&pdev
->dev
);
444 indio_dev
->modes
= INDIO_DIRECT_MODE
;
445 indio_dev
->info
= &tiadc_info
;
447 tiadc_step_config(indio_dev
);
448 tiadc_writel(adc_dev
, REG_FIFO1THR
, FIFO1_THRESHOLD
);
450 err
= tiadc_channel_init(indio_dev
, adc_dev
->channels
);
454 err
= tiadc_iio_buffered_hardware_setup(indio_dev
,
457 adc_dev
->mfd_tscadc
->irq
,
459 &tiadc_buffer_setup_ops
);
462 goto err_free_channels
;
464 err
= iio_device_register(indio_dev
);
466 goto err_buffer_unregister
;
468 platform_set_drvdata(pdev
, indio_dev
);
472 err_buffer_unregister
:
473 tiadc_iio_buffered_hardware_remove(indio_dev
);
475 tiadc_channels_remove(indio_dev
);
479 static int tiadc_remove(struct platform_device
*pdev
)
481 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
482 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
485 iio_device_unregister(indio_dev
);
486 tiadc_iio_buffered_hardware_remove(indio_dev
);
487 tiadc_channels_remove(indio_dev
);
489 step_en
= get_adc_step_mask(adc_dev
);
490 am335x_tsc_se_clr(adc_dev
->mfd_tscadc
, step_en
);
496 static int tiadc_suspend(struct device
*dev
)
498 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
499 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
500 struct ti_tscadc_dev
*tscadc_dev
;
503 tscadc_dev
= ti_tscadc_dev_get(to_platform_device(dev
));
504 if (!device_may_wakeup(tscadc_dev
->dev
)) {
505 idle
= tiadc_readl(adc_dev
, REG_CTRL
);
506 idle
&= ~(CNTRLREG_TSCSSENB
);
507 tiadc_writel(adc_dev
, REG_CTRL
, (idle
|
508 CNTRLREG_POWERDOWN
));
514 static int tiadc_resume(struct device
*dev
)
516 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
517 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
518 unsigned int restore
;
520 /* Make sure ADC is powered up */
521 restore
= tiadc_readl(adc_dev
, REG_CTRL
);
522 restore
&= ~(CNTRLREG_POWERDOWN
);
523 tiadc_writel(adc_dev
, REG_CTRL
, restore
);
525 tiadc_step_config(indio_dev
);
526 am335x_tsc_se_set_cache(adc_dev
->mfd_tscadc
,
527 adc_dev
->buffer_en_ch_steps
);
531 static const struct dev_pm_ops tiadc_pm_ops
= {
532 .suspend
= tiadc_suspend
,
533 .resume
= tiadc_resume
,
535 #define TIADC_PM_OPS (&tiadc_pm_ops)
537 #define TIADC_PM_OPS NULL
540 static const struct of_device_id ti_adc_dt_ids
[] = {
541 { .compatible
= "ti,am3359-adc", },
544 MODULE_DEVICE_TABLE(of
, ti_adc_dt_ids
);
546 static struct platform_driver tiadc_driver
= {
548 .name
= "TI-am335x-adc",
549 .owner
= THIS_MODULE
,
551 .of_match_table
= ti_adc_dt_ids
,
553 .probe
= tiadc_probe
,
554 .remove
= tiadc_remove
,
556 module_platform_driver(tiadc_driver
);
558 MODULE_DESCRIPTION("TI ADC controller driver");
559 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
560 MODULE_LICENSE("GPL");