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
,
342 sizeof(struct iio_chan_spec
), GFP_KERNEL
);
343 if (chan_array
== NULL
)
347 for (i
= 0; i
< channels
; i
++, chan
++) {
349 chan
->type
= IIO_VOLTAGE
;
351 chan
->channel
= adc_dev
->channel_line
[i
];
352 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
353 chan
->datasheet_name
= chan_name_ain
[chan
->channel
];
354 chan
->scan_index
= i
;
355 chan
->scan_type
.sign
= 'u';
356 chan
->scan_type
.realbits
= 12;
357 chan
->scan_type
.storagebits
= 16;
360 indio_dev
->channels
= chan_array
;
365 static void tiadc_channels_remove(struct iio_dev
*indio_dev
)
367 kfree(indio_dev
->channels
);
370 static int tiadc_read_raw(struct iio_dev
*indio_dev
,
371 struct iio_chan_spec
const *chan
,
372 int *val
, int *val2
, long mask
)
374 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
375 int ret
= IIO_VAL_INT
;
377 unsigned int fifo1count
, read
, stepid
;
380 unsigned long timeout
;
382 if (iio_buffer_enabled(indio_dev
))
385 step_en
= get_adc_chan_step_mask(adc_dev
, chan
);
389 mutex_lock(&adc_dev
->fifo1_lock
);
390 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
392 tiadc_readl(adc_dev
, REG_FIFO1
);
394 am335x_tsc_se_set_once(adc_dev
->mfd_tscadc
, step_en
);
396 timeout
= jiffies
+ msecs_to_jiffies
397 (IDLE_TIMEOUT
* adc_dev
->channels
);
398 /* Wait for Fifo threshold interrupt */
400 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
404 if (time_after(jiffies
, timeout
)) {
405 am335x_tsc_se_adc_done(adc_dev
->mfd_tscadc
);
410 map_val
= adc_dev
->channel_step
[chan
->scan_index
];
413 * We check the complete FIFO. We programmed just one entry but in case
414 * something went wrong we left empty handed (-EAGAIN previously) and
415 * then the value apeared somehow in the FIFO we would have two entries.
416 * Therefore we read every item and keep only the latest version of the
419 for (i
= 0; i
< fifo1count
; i
++) {
420 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
421 stepid
= read
& FIFOREAD_CHNLID_MASK
;
422 stepid
= stepid
>> 0x10;
424 if (stepid
== map_val
) {
425 read
= read
& FIFOREAD_DATA_MASK
;
430 am335x_tsc_se_adc_done(adc_dev
->mfd_tscadc
);
436 mutex_unlock(&adc_dev
->fifo1_lock
);
440 static const struct iio_info tiadc_info
= {
441 .read_raw
= &tiadc_read_raw
,
442 .driver_module
= THIS_MODULE
,
445 static int tiadc_parse_dt(struct platform_device
*pdev
,
446 struct tiadc_device
*adc_dev
)
448 struct device_node
*node
= pdev
->dev
.of_node
;
449 struct property
*prop
;
454 of_property_for_each_u32(node
, "ti,adc-channels", prop
, cur
, val
) {
455 adc_dev
->channel_line
[channels
] = val
;
457 /* Set Default values for optional DT parameters */
458 adc_dev
->open_delay
[channels
] = STEPCONFIG_OPENDLY
;
459 adc_dev
->sample_delay
[channels
] = STEPCONFIG_SAMPLEDLY
;
460 adc_dev
->step_avg
[channels
] = 16;
465 of_property_read_u32_array(node
, "ti,chan-step-avg",
466 adc_dev
->step_avg
, channels
);
467 of_property_read_u32_array(node
, "ti,chan-step-opendelay",
468 adc_dev
->open_delay
, channels
);
469 of_property_read_u32_array(node
, "ti,chan-step-sampledelay",
470 adc_dev
->sample_delay
, channels
);
472 adc_dev
->channels
= channels
;
476 static int tiadc_probe(struct platform_device
*pdev
)
478 struct iio_dev
*indio_dev
;
479 struct tiadc_device
*adc_dev
;
480 struct device_node
*node
= pdev
->dev
.of_node
;
484 dev_err(&pdev
->dev
, "Could not find valid DT data.\n");
488 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
489 sizeof(struct tiadc_device
));
490 if (indio_dev
== NULL
) {
491 dev_err(&pdev
->dev
, "failed to allocate iio device\n");
494 adc_dev
= iio_priv(indio_dev
);
496 adc_dev
->mfd_tscadc
= ti_tscadc_dev_get(pdev
);
497 tiadc_parse_dt(pdev
, adc_dev
);
499 indio_dev
->dev
.parent
= &pdev
->dev
;
500 indio_dev
->name
= dev_name(&pdev
->dev
);
501 indio_dev
->modes
= INDIO_DIRECT_MODE
;
502 indio_dev
->info
= &tiadc_info
;
504 tiadc_step_config(indio_dev
);
505 tiadc_writel(adc_dev
, REG_FIFO1THR
, FIFO1_THRESHOLD
);
506 mutex_init(&adc_dev
->fifo1_lock
);
508 err
= tiadc_channel_init(indio_dev
, adc_dev
->channels
);
512 err
= tiadc_iio_buffered_hardware_setup(indio_dev
,
515 adc_dev
->mfd_tscadc
->irq
,
517 &tiadc_buffer_setup_ops
);
520 goto err_free_channels
;
522 err
= iio_device_register(indio_dev
);
524 goto err_buffer_unregister
;
526 platform_set_drvdata(pdev
, indio_dev
);
530 err_buffer_unregister
:
531 tiadc_iio_buffered_hardware_remove(indio_dev
);
533 tiadc_channels_remove(indio_dev
);
537 static int tiadc_remove(struct platform_device
*pdev
)
539 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
540 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
543 iio_device_unregister(indio_dev
);
544 tiadc_iio_buffered_hardware_remove(indio_dev
);
545 tiadc_channels_remove(indio_dev
);
547 step_en
= get_adc_step_mask(adc_dev
);
548 am335x_tsc_se_clr(adc_dev
->mfd_tscadc
, step_en
);
554 static int tiadc_suspend(struct device
*dev
)
556 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
557 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
558 struct ti_tscadc_dev
*tscadc_dev
;
561 tscadc_dev
= ti_tscadc_dev_get(to_platform_device(dev
));
562 if (!device_may_wakeup(tscadc_dev
->dev
)) {
563 idle
= tiadc_readl(adc_dev
, REG_CTRL
);
564 idle
&= ~(CNTRLREG_TSCSSENB
);
565 tiadc_writel(adc_dev
, REG_CTRL
, (idle
|
566 CNTRLREG_POWERDOWN
));
572 static int tiadc_resume(struct device
*dev
)
574 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
575 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
576 unsigned int restore
;
578 /* Make sure ADC is powered up */
579 restore
= tiadc_readl(adc_dev
, REG_CTRL
);
580 restore
&= ~(CNTRLREG_POWERDOWN
);
581 tiadc_writel(adc_dev
, REG_CTRL
, restore
);
583 tiadc_step_config(indio_dev
);
584 am335x_tsc_se_set_cache(adc_dev
->mfd_tscadc
,
585 adc_dev
->buffer_en_ch_steps
);
589 static const struct dev_pm_ops tiadc_pm_ops
= {
590 .suspend
= tiadc_suspend
,
591 .resume
= tiadc_resume
,
593 #define TIADC_PM_OPS (&tiadc_pm_ops)
595 #define TIADC_PM_OPS NULL
598 static const struct of_device_id ti_adc_dt_ids
[] = {
599 { .compatible
= "ti,am3359-adc", },
602 MODULE_DEVICE_TABLE(of
, ti_adc_dt_ids
);
604 static struct platform_driver tiadc_driver
= {
606 .name
= "TI-am335x-adc",
608 .of_match_table
= ti_adc_dt_ids
,
610 .probe
= tiadc_probe
,
611 .remove
= tiadc_remove
,
613 module_platform_driver(tiadc_driver
);
615 MODULE_DESCRIPTION("TI ADC controller driver");
616 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
617 MODULE_LICENSE("GPL");