Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-drm-fsl-dcu.git] / drivers / iio / adc / ti_am335x_adc.c
1 /*
2  * TI ADC MFD driver
3  *
4  * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
5  *
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.
9  *
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.
14  */
15
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>
22 #include <linux/io.h>
23 #include <linux/iio/iio.h>
24 #include <linux/of.h>
25 #include <linux/of_device.h>
26 #include <linux/iio/machine.h>
27 #include <linux/iio/driver.h>
28
29 #include <linux/mfd/ti_am335x_tscadc.h>
30 #include <linux/iio/buffer.h>
31 #include <linux/iio/kfifo_buf.h>
32
33 struct tiadc_device {
34         struct ti_tscadc_dev *mfd_tscadc;
35         int channels;
36         u8 channel_line[8];
37         u8 channel_step[8];
38         int buffer_en_ch_steps;
39         u16 data[8];
40 };
41
42 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
43 {
44         return readl(adc->mfd_tscadc->tscadc_base + reg);
45 }
46
47 static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
48                                         unsigned int val)
49 {
50         writel(val, adc->mfd_tscadc->tscadc_base + reg);
51 }
52
53 static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
54 {
55         u32 step_en;
56
57         step_en = ((1 << adc_dev->channels) - 1);
58         step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
59         return step_en;
60 }
61
62 static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev,
63                 struct iio_chan_spec const *chan)
64 {
65         int i;
66
67         for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
68                 if (chan->channel == adc_dev->channel_line[i]) {
69                         u32 step;
70
71                         step = adc_dev->channel_step[i];
72                         /* +1 for the charger */
73                         return 1 << (step + 1);
74                 }
75         }
76         WARN_ON(1);
77         return 0;
78 }
79
80 static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
81 {
82         return 1 << adc_dev->channel_step[chan];
83 }
84
85 static void tiadc_step_config(struct iio_dev *indio_dev)
86 {
87         struct tiadc_device *adc_dev = iio_priv(indio_dev);
88         unsigned int stepconfig;
89         int i, steps = 0;
90
91         /*
92          * There are 16 configurable steps and 8 analog input
93          * lines available which are shared between Touchscreen and ADC.
94          *
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.
99          */
100
101         if (iio_buffer_enabled(indio_dev))
102                 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1
103                                         | STEPCONFIG_MODE_SWCNT;
104         else
105                 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
106
107         for (i = 0; i < adc_dev->channels; i++) {
108                 int chan;
109
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),
114                                 STEPCONFIG_OPENDLY);
115                 adc_dev->channel_step[i] = steps;
116                 steps++;
117         }
118 }
119
120 static irqreturn_t tiadc_irq_h(int irq, void *private)
121 {
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);
126
127         /*
128          * ADC and touchscreen share the IRQ line.
129          * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
130          */
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));
139                 return IRQ_HANDLED;
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;
144         }
145
146         return IRQ_NONE;
147 }
148
149 static irqreturn_t tiadc_worker_h(int irq, void *private)
150 {
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;
155
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;
161                 }
162                 iio_push_to_buffers(indio_dev, (u8 *) data);
163         }
164
165         tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
166         tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
167
168         return IRQ_HANDLED;
169 }
170
171 static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
172 {
173         struct tiadc_device *adc_dev = iio_priv(indio_dev);
174         int i, fifo1count, read;
175
176         tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
177                                 IRQENB_FIFO1OVRRUN |
178                                 IRQENB_FIFO1UNDRFLW));
179
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);
184
185         return 0;
186 }
187
188 static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
189 {
190         struct tiadc_device *adc_dev = iio_priv(indio_dev);
191         struct iio_buffer *buffer = indio_dev->buffer;
192         unsigned int enb = 0;
193         u8 bit;
194
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;
199
200         am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
201
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);
206
207         return 0;
208 }
209
210 static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
211 {
212         struct tiadc_device *adc_dev = iio_priv(indio_dev);
213         int fifo1count, i, read;
214
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;
219
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);
224
225         return 0;
226 }
227
228 static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
229 {
230         tiadc_step_config(indio_dev);
231
232         return 0;
233 }
234
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,
240 };
241
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),
245         int irq,
246         unsigned long flags,
247         const struct iio_buffer_setup_ops *setup_ops)
248 {
249         struct iio_buffer *buffer;
250         int ret;
251
252         buffer = iio_kfifo_allocate(indio_dev);
253         if (!buffer)
254                 return -ENOMEM;
255
256         iio_device_attach_buffer(indio_dev, buffer);
257
258         ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
259                                 flags, indio_dev->name, indio_dev);
260         if (ret)
261                 goto error_kfifo_free;
262
263         indio_dev->setup_ops = setup_ops;
264         indio_dev->modes |= INDIO_BUFFER_HARDWARE;
265
266         ret = iio_buffer_register(indio_dev,
267                                   indio_dev->channels,
268                                   indio_dev->num_channels);
269         if (ret)
270                 goto error_free_irq;
271
272         return 0;
273
274 error_free_irq:
275         free_irq(irq, indio_dev);
276 error_kfifo_free:
277         iio_kfifo_free(indio_dev->buffer);
278         return ret;
279 }
280
281 static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
282 {
283         struct tiadc_device *adc_dev = iio_priv(indio_dev);
284
285         free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
286         iio_kfifo_free(indio_dev->buffer);
287         iio_buffer_unregister(indio_dev);
288 }
289
290
291 static const char * const chan_name_ain[] = {
292         "AIN0",
293         "AIN1",
294         "AIN2",
295         "AIN3",
296         "AIN4",
297         "AIN5",
298         "AIN6",
299         "AIN7",
300 };
301
302 static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
303 {
304         struct tiadc_device *adc_dev = iio_priv(indio_dev);
305         struct iio_chan_spec *chan_array;
306         struct iio_chan_spec *chan;
307         int i;
308
309         indio_dev->num_channels = channels;
310         chan_array = kcalloc(channels,
311                         sizeof(struct iio_chan_spec), GFP_KERNEL);
312         if (chan_array == NULL)
313                 return -ENOMEM;
314
315         chan = chan_array;
316         for (i = 0; i < channels; i++, chan++) {
317
318                 chan->type = IIO_VOLTAGE;
319                 chan->indexed = 1;
320                 chan->channel = adc_dev->channel_line[i];
321                 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
322                 chan->datasheet_name = chan_name_ain[chan->channel];
323                 chan->scan_index = i;
324                 chan->scan_type.sign = 'u';
325                 chan->scan_type.realbits = 12;
326                 chan->scan_type.storagebits = 16;
327         }
328
329         indio_dev->channels = chan_array;
330
331         return 0;
332 }
333
334 static void tiadc_channels_remove(struct iio_dev *indio_dev)
335 {
336         kfree(indio_dev->channels);
337 }
338
339 static int tiadc_read_raw(struct iio_dev *indio_dev,
340                 struct iio_chan_spec const *chan,
341                 int *val, int *val2, long mask)
342 {
343         struct tiadc_device *adc_dev = iio_priv(indio_dev);
344         int i, map_val;
345         unsigned int fifo1count, read, stepid;
346         bool found = false;
347         u32 step_en;
348         unsigned long timeout;
349
350         if (iio_buffer_enabled(indio_dev))
351                 return -EBUSY;
352
353         step_en = get_adc_chan_step_mask(adc_dev, chan);
354         if (!step_en)
355                 return -EINVAL;
356
357         fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
358         while (fifo1count--)
359                 tiadc_readl(adc_dev, REG_FIFO1);
360
361         am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
362
363         timeout = jiffies + usecs_to_jiffies
364                                 (IDLE_TIMEOUT * adc_dev->channels);
365         /* Wait for Fifo threshold interrupt */
366         while (1) {
367                 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
368                 if (fifo1count)
369                         break;
370
371                 if (time_after(jiffies, timeout)) {
372                         am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
373                         return -EAGAIN;
374                 }
375         }
376         map_val = adc_dev->channel_step[chan->scan_index];
377
378         /*
379          * We check the complete FIFO. We programmed just one entry but in case
380          * something went wrong we left empty handed (-EAGAIN previously) and
381          * then the value apeared somehow in the FIFO we would have two entries.
382          * Therefore we read every item and keep only the latest version of the
383          * requested channel.
384          */
385         for (i = 0; i < fifo1count; i++) {
386                 read = tiadc_readl(adc_dev, REG_FIFO1);
387                 stepid = read & FIFOREAD_CHNLID_MASK;
388                 stepid = stepid >> 0x10;
389
390                 if (stepid == map_val) {
391                         read = read & FIFOREAD_DATA_MASK;
392                         found = true;
393                         *val = (u16) read;
394                 }
395         }
396         am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
397
398         if (found == false)
399                 return -EBUSY;
400         return IIO_VAL_INT;
401 }
402
403 static const struct iio_info tiadc_info = {
404         .read_raw = &tiadc_read_raw,
405         .driver_module = THIS_MODULE,
406 };
407
408 static int tiadc_probe(struct platform_device *pdev)
409 {
410         struct iio_dev          *indio_dev;
411         struct tiadc_device     *adc_dev;
412         struct device_node      *node = pdev->dev.of_node;
413         struct property         *prop;
414         const __be32            *cur;
415         int                     err;
416         u32                     val;
417         int                     channels = 0;
418
419         if (!node) {
420                 dev_err(&pdev->dev, "Could not find valid DT data.\n");
421                 return -EINVAL;
422         }
423
424         indio_dev = devm_iio_device_alloc(&pdev->dev,
425                                           sizeof(struct tiadc_device));
426         if (indio_dev == NULL) {
427                 dev_err(&pdev->dev, "failed to allocate iio device\n");
428                 return -ENOMEM;
429         }
430         adc_dev = iio_priv(indio_dev);
431
432         adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
433
434         of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
435                 adc_dev->channel_line[channels] = val;
436                 channels++;
437         }
438         adc_dev->channels = channels;
439
440         indio_dev->dev.parent = &pdev->dev;
441         indio_dev->name = dev_name(&pdev->dev);
442         indio_dev->modes = INDIO_DIRECT_MODE;
443         indio_dev->info = &tiadc_info;
444
445         tiadc_step_config(indio_dev);
446         tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
447
448         err = tiadc_channel_init(indio_dev, adc_dev->channels);
449         if (err < 0)
450                 return err;
451
452         err = tiadc_iio_buffered_hardware_setup(indio_dev,
453                 &tiadc_worker_h,
454                 &tiadc_irq_h,
455                 adc_dev->mfd_tscadc->irq,
456                 IRQF_SHARED,
457                 &tiadc_buffer_setup_ops);
458
459         if (err)
460                 goto err_free_channels;
461
462         err = iio_device_register(indio_dev);
463         if (err)
464                 goto err_buffer_unregister;
465
466         platform_set_drvdata(pdev, indio_dev);
467
468         return 0;
469
470 err_buffer_unregister:
471         tiadc_iio_buffered_hardware_remove(indio_dev);
472 err_free_channels:
473         tiadc_channels_remove(indio_dev);
474         return err;
475 }
476
477 static int tiadc_remove(struct platform_device *pdev)
478 {
479         struct iio_dev *indio_dev = platform_get_drvdata(pdev);
480         struct tiadc_device *adc_dev = iio_priv(indio_dev);
481         u32 step_en;
482
483         iio_device_unregister(indio_dev);
484         tiadc_iio_buffered_hardware_remove(indio_dev);
485         tiadc_channels_remove(indio_dev);
486
487         step_en = get_adc_step_mask(adc_dev);
488         am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
489
490         return 0;
491 }
492
493 #ifdef CONFIG_PM
494 static int tiadc_suspend(struct device *dev)
495 {
496         struct iio_dev *indio_dev = dev_get_drvdata(dev);
497         struct tiadc_device *adc_dev = iio_priv(indio_dev);
498         struct ti_tscadc_dev *tscadc_dev;
499         unsigned int idle;
500
501         tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
502         if (!device_may_wakeup(tscadc_dev->dev)) {
503                 idle = tiadc_readl(adc_dev, REG_CTRL);
504                 idle &= ~(CNTRLREG_TSCSSENB);
505                 tiadc_writel(adc_dev, REG_CTRL, (idle |
506                                 CNTRLREG_POWERDOWN));
507         }
508
509         return 0;
510 }
511
512 static int tiadc_resume(struct device *dev)
513 {
514         struct iio_dev *indio_dev = dev_get_drvdata(dev);
515         struct tiadc_device *adc_dev = iio_priv(indio_dev);
516         unsigned int restore;
517
518         /* Make sure ADC is powered up */
519         restore = tiadc_readl(adc_dev, REG_CTRL);
520         restore &= ~(CNTRLREG_POWERDOWN);
521         tiadc_writel(adc_dev, REG_CTRL, restore);
522
523         tiadc_step_config(indio_dev);
524         am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
525                         adc_dev->buffer_en_ch_steps);
526         return 0;
527 }
528
529 static const struct dev_pm_ops tiadc_pm_ops = {
530         .suspend = tiadc_suspend,
531         .resume = tiadc_resume,
532 };
533 #define TIADC_PM_OPS (&tiadc_pm_ops)
534 #else
535 #define TIADC_PM_OPS NULL
536 #endif
537
538 static const struct of_device_id ti_adc_dt_ids[] = {
539         { .compatible = "ti,am3359-adc", },
540         { }
541 };
542 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
543
544 static struct platform_driver tiadc_driver = {
545         .driver = {
546                 .name   = "TI-am335x-adc",
547                 .pm     = TIADC_PM_OPS,
548                 .of_match_table = ti_adc_dt_ids,
549         },
550         .probe  = tiadc_probe,
551         .remove = tiadc_remove,
552 };
553 module_platform_driver(tiadc_driver);
554
555 MODULE_DESCRIPTION("TI ADC controller driver");
556 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
557 MODULE_LICENSE("GPL");