PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / iio / adc / ti_am335x_adc.c
blob31e786e3999b128ac8e53629db48290086162f5c
1 /*
2 * TI ADC MFD driver
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>
23 #include <linux/io.h>
24 #include <linux/iio/iio.h>
25 #include <linux/of.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>
34 struct tiadc_device {
35 struct ti_tscadc_dev *mfd_tscadc;
36 int channels;
37 u8 channel_line[8];
38 u8 channel_step[8];
39 int buffer_en_ch_steps;
40 u16 data[8];
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,
49 unsigned int val)
51 writel(val, adc->mfd_tscadc->tscadc_base + reg);
54 static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
56 u32 step_en;
58 step_en = ((1 << adc_dev->channels) - 1);
59 step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
60 return step_en;
63 static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev,
64 struct iio_chan_spec const *chan)
66 int i;
68 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
69 if (chan->channel == adc_dev->channel_line[i]) {
70 u32 step;
72 step = adc_dev->channel_step[i];
73 /* +1 for the charger */
74 return 1 << (step + 1);
77 WARN_ON(1);
78 return 0;
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;
90 int i, steps;
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;
106 else
107 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
109 for (i = 0; i < adc_dev->channels; i++) {
110 int chan;
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),
116 STEPCONFIG_OPENDLY);
117 adc_dev->channel_step[i] = steps;
118 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));
141 return IRQ_HANDLED;
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;
148 return IRQ_NONE;
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);
170 return IRQ_HANDLED;
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 |
179 IRQENB_FIFO1OVRRUN |
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);
187 return 0;
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;
195 u8 bit;
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);
209 return 0;
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);
227 return 0;
230 static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
232 tiadc_step_config(indio_dev);
234 return 0;
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),
247 int irq,
248 unsigned long flags,
249 const struct iio_buffer_setup_ops *setup_ops)
251 struct iio_buffer *buffer;
252 int ret;
254 buffer = iio_kfifo_allocate(indio_dev);
255 if (!buffer)
256 return -ENOMEM;
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);
262 if (ret)
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,
269 indio_dev->channels,
270 indio_dev->num_channels);
271 if (ret)
272 goto error_free_irq;
274 return 0;
276 error_free_irq:
277 free_irq(irq, indio_dev);
278 error_kfifo_free:
279 iio_kfifo_free(indio_dev->buffer);
280 return ret;
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[] = {
294 "AIN0",
295 "AIN1",
296 "AIN2",
297 "AIN3",
298 "AIN4",
299 "AIN5",
300 "AIN6",
301 "AIN7",
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;
309 int i;
311 indio_dev->num_channels = channels;
312 chan_array = kcalloc(channels,
313 sizeof(struct iio_chan_spec), GFP_KERNEL);
314 if (chan_array == NULL)
315 return -ENOMEM;
317 chan = chan_array;
318 for (i = 0; i < channels; i++, chan++) {
320 chan->type = IIO_VOLTAGE;
321 chan->indexed = 1;
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;
333 return 0;
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);
346 int i, map_val;
347 unsigned int fifo1count, read, stepid;
348 bool found = false;
349 u32 step_en;
350 unsigned long timeout;
352 if (iio_buffer_enabled(indio_dev))
353 return -EBUSY;
355 step_en = get_adc_chan_step_mask(adc_dev, chan);
356 if (!step_en)
357 return -EINVAL;
359 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
360 while (fifo1count--)
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 */
368 while (1) {
369 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
370 if (fifo1count)
371 break;
373 if (time_after(jiffies, timeout)) {
374 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
375 return -EAGAIN;
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
385 * requested channel.
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;
394 found = true;
395 *val = (u16) read;
398 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
400 if (found == false)
401 return -EBUSY;
402 return IIO_VAL_INT;
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;
416 const __be32 *cur;
417 int err;
418 u32 val;
419 int channels = 0;
421 if (!node) {
422 dev_err(&pdev->dev, "Could not find valid DT data.\n");
423 return -EINVAL;
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");
430 return -ENOMEM;
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;
438 channels++;
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);
451 if (err < 0)
452 return err;
454 err = tiadc_iio_buffered_hardware_setup(indio_dev,
455 &tiadc_worker_h,
456 &tiadc_irq_h,
457 adc_dev->mfd_tscadc->irq,
458 IRQF_SHARED,
459 &tiadc_buffer_setup_ops);
461 if (err)
462 goto err_free_channels;
464 err = iio_device_register(indio_dev);
465 if (err)
466 goto err_buffer_unregister;
468 platform_set_drvdata(pdev, indio_dev);
470 return 0;
472 err_buffer_unregister:
473 tiadc_iio_buffered_hardware_remove(indio_dev);
474 err_free_channels:
475 tiadc_channels_remove(indio_dev);
476 return err;
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);
483 u32 step_en;
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);
492 return 0;
495 #ifdef CONFIG_PM
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;
501 unsigned int idle;
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));
511 return 0;
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);
528 return 0;
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)
536 #else
537 #define TIADC_PM_OPS NULL
538 #endif
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 = {
547 .driver = {
548 .name = "TI-am335x-adc",
549 .owner = THIS_MODULE,
550 .pm = TIADC_PM_OPS,
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");