source: src/linux/universal/linux-3.18/drivers/iio/adc/ti_am335x_adc.c @ 31885

Last change on this file since 31885 was 31885, checked in by brainslayer, 3 months ago

update

File size: 14.1 KB
Line 
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
33struct 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
42static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
43{
44        return readl(adc->mfd_tscadc->tscadc_base + reg);
45}
46
47static 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
53static 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
62static 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
80static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
81{
82        return 1 << adc_dev->channel_step[chan];
83}
84
85static 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;
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 backwards i.e. from 16 towards 0 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        steps = TOTAL_STEPS - adc_dev->channels;
102        if (iio_buffer_enabled(indio_dev))
103                stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1
104                                        | STEPCONFIG_MODE_SWCNT;
105        else
106                stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
107
108        for (i = 0; i < adc_dev->channels; i++) {
109                int chan;
110
111                chan = adc_dev->channel_line[i];
112                tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
113                                stepconfig | STEPCONFIG_INP(chan));
114                tiadc_writel(adc_dev, REG_STEPDELAY(steps),
115                                STEPCONFIG_OPENDLY);
116                adc_dev->channel_step[i] = steps;
117                steps++;
118        }
119}
120
121static irqreturn_t tiadc_irq_h(int irq, void *private)
122{
123        struct iio_dev *indio_dev = private;
124        struct tiadc_device *adc_dev = iio_priv(indio_dev);
125        unsigned int status, config, adc_fsm;
126        unsigned short count = 0;
127
128        status = tiadc_readl(adc_dev, REG_IRQSTATUS);
129
130        /*
131         * ADC and touchscreen share the IRQ line.
132         * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
133         */
134        if (status & IRQENB_FIFO1OVRRUN) {
135                /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
136                config = tiadc_readl(adc_dev, REG_CTRL);
137                config &= ~(CNTRLREG_TSCSSENB);
138                tiadc_writel(adc_dev, REG_CTRL, config);
139                tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
140                                | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
141
142                /* wait for idle state.
143                 * ADC needs to finish the current conversion
144                 * before disabling the module
145                 */
146                do {
147                        adc_fsm = tiadc_readl(adc_dev, REG_ADCFSM);
148                } while (adc_fsm != 0x10 && count++ < 100);
149
150                tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
151                return IRQ_HANDLED;
152        } else if (status & IRQENB_FIFO1THRES) {
153                /* Disable irq and wake worker thread */
154                tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
155                return IRQ_WAKE_THREAD;
156        }
157
158        return IRQ_NONE;
159}
160
161static irqreturn_t tiadc_worker_h(int irq, void *private)
162{
163        struct iio_dev *indio_dev = private;
164        struct tiadc_device *adc_dev = iio_priv(indio_dev);
165        int i, k, fifo1count, read;
166        u16 *data = adc_dev->data;
167
168        fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
169        for (k = 0; k < fifo1count; k = k + i) {
170                for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
171                        read = tiadc_readl(adc_dev, REG_FIFO1);
172                        data[i] = read & FIFOREAD_DATA_MASK;
173                }
174                iio_push_to_buffers(indio_dev, (u8 *) data);
175        }
176
177        tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
178        tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
179
180        return IRQ_HANDLED;
181}
182
183static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
184{
185        struct tiadc_device *adc_dev = iio_priv(indio_dev);
186        int i, fifo1count, read;
187
188        tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
189                                IRQENB_FIFO1OVRRUN |
190                                IRQENB_FIFO1UNDRFLW));
191
192        /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
193        fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
194        for (i = 0; i < fifo1count; i++)
195                read = tiadc_readl(adc_dev, REG_FIFO1);
196
197        return 0;
198}
199
200static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
201{
202        struct tiadc_device *adc_dev = iio_priv(indio_dev);
203        struct iio_buffer *buffer = indio_dev->buffer;
204        unsigned int enb = 0;
205        u8 bit;
206
207        tiadc_step_config(indio_dev);
208        for_each_set_bit(bit, buffer->scan_mask, adc_dev->channels)
209                enb |= (get_adc_step_bit(adc_dev, bit) << 1);
210        adc_dev->buffer_en_ch_steps = enb;
211
212        am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
213
214        tiadc_writel(adc_dev,  REG_IRQSTATUS, IRQENB_FIFO1THRES
215                                | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
216        tiadc_writel(adc_dev,  REG_IRQENABLE, IRQENB_FIFO1THRES
217                                | IRQENB_FIFO1OVRRUN);
218
219        return 0;
220}
221
222static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
223{
224        struct tiadc_device *adc_dev = iio_priv(indio_dev);
225        int fifo1count, i, read;
226
227        tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
228                                IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
229        am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
230        adc_dev->buffer_en_ch_steps = 0;
231
232        /* Flush FIFO of leftover data in the time it takes to disable adc */
233        fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
234        for (i = 0; i < fifo1count; i++)
235                read = tiadc_readl(adc_dev, REG_FIFO1);
236
237        return 0;
238}
239
240static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
241{
242        tiadc_step_config(indio_dev);
243
244        return 0;
245}
246
247static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
248        .preenable = &tiadc_buffer_preenable,
249        .postenable = &tiadc_buffer_postenable,
250        .predisable = &tiadc_buffer_predisable,
251        .postdisable = &tiadc_buffer_postdisable,
252};
253
254static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
255        irqreturn_t (*pollfunc_bh)(int irq, void *p),
256        irqreturn_t (*pollfunc_th)(int irq, void *p),
257        int irq,
258        unsigned long flags,
259        const struct iio_buffer_setup_ops *setup_ops)
260{
261        struct iio_buffer *buffer;
262        int ret;
263
264        buffer = iio_kfifo_allocate(indio_dev);
265        if (!buffer)
266                return -ENOMEM;
267
268        iio_device_attach_buffer(indio_dev, buffer);
269
270        ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
271                                flags, indio_dev->name, indio_dev);
272        if (ret)
273                goto error_kfifo_free;
274
275        indio_dev->setup_ops = setup_ops;
276        indio_dev->modes |= INDIO_BUFFER_HARDWARE;
277
278        ret = iio_buffer_register(indio_dev,
279                                  indio_dev->channels,
280                                  indio_dev->num_channels);
281        if (ret)
282                goto error_free_irq;
283
284        return 0;
285
286error_free_irq:
287        free_irq(irq, indio_dev);
288error_kfifo_free:
289        iio_kfifo_free(indio_dev->buffer);
290        return ret;
291}
292
293static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
294{
295        struct tiadc_device *adc_dev = iio_priv(indio_dev);
296
297        free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
298        iio_kfifo_free(indio_dev->buffer);
299        iio_buffer_unregister(indio_dev);
300}
301
302
303static const char * const chan_name_ain[] = {
304        "AIN0",
305        "AIN1",
306        "AIN2",
307        "AIN3",
308        "AIN4",
309        "AIN5",
310        "AIN6",
311        "AIN7",
312};
313
314static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
315{
316        struct tiadc_device *adc_dev = iio_priv(indio_dev);
317        struct iio_chan_spec *chan_array;
318        struct iio_chan_spec *chan;
319        int i;
320
321        indio_dev->num_channels = channels;
322        chan_array = kcalloc(channels,
323                        sizeof(struct iio_chan_spec), GFP_KERNEL);
324        if (chan_array == NULL)
325                return -ENOMEM;
326
327        chan = chan_array;
328        for (i = 0; i < channels; i++, chan++) {
329
330                chan->type = IIO_VOLTAGE;
331                chan->indexed = 1;
332                chan->channel = adc_dev->channel_line[i];
333                chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
334                chan->datasheet_name = chan_name_ain[chan->channel];
335                chan->scan_index = i;
336                chan->scan_type.sign = 'u';
337                chan->scan_type.realbits = 12;
338                chan->scan_type.storagebits = 16;
339        }
340
341        indio_dev->channels = chan_array;
342
343        return 0;
344}
345
346static void tiadc_channels_remove(struct iio_dev *indio_dev)
347{
348        kfree(indio_dev->channels);
349}
350
351static int tiadc_read_raw(struct iio_dev *indio_dev,
352                struct iio_chan_spec const *chan,
353                int *val, int *val2, long mask)
354{
355        struct tiadc_device *adc_dev = iio_priv(indio_dev);
356        int i, map_val;
357        unsigned int fifo1count, read, stepid;
358        bool found = false;
359        u32 step_en;
360        unsigned long timeout;
361
362        if (iio_buffer_enabled(indio_dev))
363                return -EBUSY;
364
365        step_en = get_adc_chan_step_mask(adc_dev, chan);
366        if (!step_en)
367                return -EINVAL;
368
369        fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
370        while (fifo1count--)
371                tiadc_readl(adc_dev, REG_FIFO1);
372
373        am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
374
375        timeout = jiffies + usecs_to_jiffies
376                                (IDLE_TIMEOUT * adc_dev->channels);
377        /* Wait for Fifo threshold interrupt */
378        while (1) {
379                fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
380                if (fifo1count)
381                        break;
382
383                if (time_after(jiffies, timeout)) {
384                        am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
385                        return -EAGAIN;
386                }
387        }
388        map_val = adc_dev->channel_step[chan->scan_index];
389
390        /*
391         * We check the complete FIFO. We programmed just one entry but in case
392         * something went wrong we left empty handed (-EAGAIN previously) and
393         * then the value apeared somehow in the FIFO we would have two entries.
394         * Therefore we read every item and keep only the latest version of the
395         * requested channel.
396         */
397        for (i = 0; i < fifo1count; i++) {
398                read = tiadc_readl(adc_dev, REG_FIFO1);
399                stepid = read & FIFOREAD_CHNLID_MASK;
400                stepid = stepid >> 0x10;
401
402                if (stepid == map_val) {
403                        read = read & FIFOREAD_DATA_MASK;
404                        found = true;
405                        *val = (u16) read;
406                }
407        }
408        am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
409
410        if (found == false)
411                return -EBUSY;
412        return IIO_VAL_INT;
413}
414
415static const struct iio_info tiadc_info = {
416        .read_raw = &tiadc_read_raw,
417        .driver_module = THIS_MODULE,
418};
419
420static int tiadc_probe(struct platform_device *pdev)
421{
422        struct iio_dev          *indio_dev;
423        struct tiadc_device     *adc_dev;
424        struct device_node      *node = pdev->dev.of_node;
425        struct property         *prop;
426        const __be32            *cur;
427        int                     err;
428        u32                     val;
429        int                     channels = 0;
430
431        if (!node) {
432                dev_err(&pdev->dev, "Could not find valid DT data.\n");
433                return -EINVAL;
434        }
435
436        indio_dev = devm_iio_device_alloc(&pdev->dev,
437                                          sizeof(struct tiadc_device));
438        if (indio_dev == NULL) {
439                dev_err(&pdev->dev, "failed to allocate iio device\n");
440                return -ENOMEM;
441        }
442        adc_dev = iio_priv(indio_dev);
443
444        adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
445
446        of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
447                adc_dev->channel_line[channels] = val;
448                channels++;
449        }
450        adc_dev->channels = channels;
451
452        indio_dev->dev.parent = &pdev->dev;
453        indio_dev->name = dev_name(&pdev->dev);
454        indio_dev->modes = INDIO_DIRECT_MODE;
455        indio_dev->info = &tiadc_info;
456
457        tiadc_step_config(indio_dev);
458        tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
459
460        err = tiadc_channel_init(indio_dev, adc_dev->channels);
461        if (err < 0)
462                return err;
463
464        err = tiadc_iio_buffered_hardware_setup(indio_dev,
465                &tiadc_worker_h,
466                &tiadc_irq_h,
467                adc_dev->mfd_tscadc->irq,
468                IRQF_SHARED,
469                &tiadc_buffer_setup_ops);
470
471        if (err)
472                goto err_free_channels;
473
474        err = iio_device_register(indio_dev);
475        if (err)
476                goto err_buffer_unregister;
477
478        platform_set_drvdata(pdev, indio_dev);
479
480        return 0;
481
482err_buffer_unregister:
483        tiadc_iio_buffered_hardware_remove(indio_dev);
484err_free_channels:
485        tiadc_channels_remove(indio_dev);
486        return err;
487}
488
489static int tiadc_remove(struct platform_device *pdev)
490{
491        struct iio_dev *indio_dev = platform_get_drvdata(pdev);
492        struct tiadc_device *adc_dev = iio_priv(indio_dev);
493        u32 step_en;
494
495        iio_device_unregister(indio_dev);
496        tiadc_iio_buffered_hardware_remove(indio_dev);
497        tiadc_channels_remove(indio_dev);
498
499        step_en = get_adc_step_mask(adc_dev);
500        am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
501
502        return 0;
503}
504
505#ifdef CONFIG_PM
506static int tiadc_suspend(struct device *dev)
507{
508        struct iio_dev *indio_dev = dev_get_drvdata(dev);
509        struct tiadc_device *adc_dev = iio_priv(indio_dev);
510        struct ti_tscadc_dev *tscadc_dev;
511        unsigned int idle;
512
513        tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
514        if (!device_may_wakeup(tscadc_dev->dev)) {
515                idle = tiadc_readl(adc_dev, REG_CTRL);
516                idle &= ~(CNTRLREG_TSCSSENB);
517                tiadc_writel(adc_dev, REG_CTRL, (idle |
518                                CNTRLREG_POWERDOWN));
519        }
520
521        return 0;
522}
523
524static int tiadc_resume(struct device *dev)
525{
526        struct iio_dev *indio_dev = dev_get_drvdata(dev);
527        struct tiadc_device *adc_dev = iio_priv(indio_dev);
528        unsigned int restore;
529
530        /* Make sure ADC is powered up */
531        restore = tiadc_readl(adc_dev, REG_CTRL);
532        restore &= ~(CNTRLREG_POWERDOWN);
533        tiadc_writel(adc_dev, REG_CTRL, restore);
534
535        tiadc_step_config(indio_dev);
536        am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
537                        adc_dev->buffer_en_ch_steps);
538        return 0;
539}
540
541static const struct dev_pm_ops tiadc_pm_ops = {
542        .suspend = tiadc_suspend,
543        .resume = tiadc_resume,
544};
545#define TIADC_PM_OPS (&tiadc_pm_ops)
546#else
547#define TIADC_PM_OPS NULL
548#endif
549
550static const struct of_device_id ti_adc_dt_ids[] = {
551        { .compatible = "ti,am3359-adc", },
552        { }
553};
554MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
555
556static struct platform_driver tiadc_driver = {
557        .driver = {
558                .name   = "TI-am335x-adc",
559                .pm     = TIADC_PM_OPS,
560                .of_match_table = ti_adc_dt_ids,
561        },
562        .probe  = tiadc_probe,
563        .remove = tiadc_remove,
564};
565module_platform_driver(tiadc_driver);
566
567MODULE_DESCRIPTION("TI ADC controller driver");
568MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
569MODULE_LICENSE("GPL");
Note: See TracBrowser for help on using the repository browser.