input: ti_am33x_tsc: Step enable bits made configurable
[firefly-linux-kernel-4.4.55.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/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
26 #include <linux/mfd/ti_am335x_tscadc.h>
27 #include <linux/platform_data/ti_am335x_adc.h>
28
29 struct tiadc_device {
30         struct ti_tscadc_dev *mfd_tscadc;
31         int channels;
32 };
33
34 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
35 {
36         return readl(adc->mfd_tscadc->tscadc_base + reg);
37 }
38
39 static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
40                                         unsigned int val)
41 {
42         writel(val, adc->mfd_tscadc->tscadc_base + reg);
43 }
44
45 static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
46 {
47         u32 step_en;
48
49         step_en = ((1 << adc_dev->channels) - 1);
50         step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
51         return step_en;
52 }
53
54 static void tiadc_step_config(struct tiadc_device *adc_dev)
55 {
56         unsigned int stepconfig;
57         int i, channels = 0, steps;
58         u32 step_en;
59
60         /*
61          * There are 16 configurable steps and 8 analog input
62          * lines available which are shared between Touchscreen and ADC.
63          *
64          * Steps backwards i.e. from 16 towards 0 are used by ADC
65          * depending on number of input lines needed.
66          * Channel would represent which analog input
67          * needs to be given to ADC to digitalize data.
68          */
69
70         steps = TOTAL_STEPS - adc_dev->channels;
71         channels = TOTAL_CHANNELS - adc_dev->channels;
72
73         stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
74
75         for (i = (steps + 1); i <= TOTAL_STEPS; i++) {
76                 tiadc_writel(adc_dev, REG_STEPCONFIG(i),
77                                 stepconfig | STEPCONFIG_INP(channels));
78                 tiadc_writel(adc_dev, REG_STEPDELAY(i),
79                                 STEPCONFIG_OPENDLY);
80                 channels++;
81         }
82         step_en = get_adc_step_mask(adc_dev);
83         am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
84 }
85
86 static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
87 {
88         struct iio_chan_spec *chan_array;
89         int i;
90
91         indio_dev->num_channels = channels;
92         chan_array = kcalloc(indio_dev->num_channels,
93                         sizeof(struct iio_chan_spec), GFP_KERNEL);
94
95         if (chan_array == NULL)
96                 return -ENOMEM;
97
98         for (i = 0; i < (indio_dev->num_channels); i++) {
99                 struct iio_chan_spec *chan = chan_array + i;
100                 chan->type = IIO_VOLTAGE;
101                 chan->indexed = 1;
102                 chan->channel = i;
103                 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
104         }
105
106         indio_dev->channels = chan_array;
107
108         return indio_dev->num_channels;
109 }
110
111 static void tiadc_channels_remove(struct iio_dev *indio_dev)
112 {
113         kfree(indio_dev->channels);
114 }
115
116 static int tiadc_read_raw(struct iio_dev *indio_dev,
117                 struct iio_chan_spec const *chan,
118                 int *val, int *val2, long mask)
119 {
120         struct tiadc_device *adc_dev = iio_priv(indio_dev);
121         int i;
122         unsigned int fifo1count, readx1;
123
124         /*
125          * When the sub-system is first enabled,
126          * the sequencer will always start with the
127          * lowest step (1) and continue until step (16).
128          * For ex: If we have enabled 4 ADC channels and
129          * currently use only 1 out of them, the
130          * sequencer still configures all the 4 steps,
131          * leading to 3 unwanted data.
132          * Hence we need to flush out this data.
133          */
134
135         fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
136         for (i = 0; i < fifo1count; i++) {
137                 readx1 = tiadc_readl(adc_dev, REG_FIFO1);
138                 if (i == chan->channel)
139                         *val = readx1 & 0xfff;
140         }
141         am335x_tsc_se_update(adc_dev->mfd_tscadc);
142
143         return IIO_VAL_INT;
144 }
145
146 static const struct iio_info tiadc_info = {
147         .read_raw = &tiadc_read_raw,
148 };
149
150 static int tiadc_probe(struct platform_device *pdev)
151 {
152         struct iio_dev          *indio_dev;
153         struct tiadc_device     *adc_dev;
154         struct ti_tscadc_dev    *tscadc_dev = ti_tscadc_dev_get(pdev);
155         struct mfd_tscadc_board *pdata;
156         int                     err;
157
158         pdata = tscadc_dev->dev->platform_data;
159         if (!pdata || !pdata->adc_init) {
160                 dev_err(&pdev->dev, "Could not find platform data\n");
161                 return -EINVAL;
162         }
163
164         indio_dev = iio_device_alloc(sizeof(struct tiadc_device));
165         if (indio_dev == NULL) {
166                 dev_err(&pdev->dev, "failed to allocate iio device\n");
167                 err = -ENOMEM;
168                 goto err_ret;
169         }
170         adc_dev = iio_priv(indio_dev);
171
172         adc_dev->mfd_tscadc = tscadc_dev;
173         adc_dev->channels = pdata->adc_init->adc_channels;
174
175         indio_dev->dev.parent = &pdev->dev;
176         indio_dev->name = dev_name(&pdev->dev);
177         indio_dev->modes = INDIO_DIRECT_MODE;
178         indio_dev->info = &tiadc_info;
179
180         tiadc_step_config(adc_dev);
181
182         err = tiadc_channel_init(indio_dev, adc_dev->channels);
183         if (err < 0)
184                 goto err_free_device;
185
186         err = iio_device_register(indio_dev);
187         if (err)
188                 goto err_free_channels;
189
190         platform_set_drvdata(pdev, indio_dev);
191
192         return 0;
193
194 err_free_channels:
195         tiadc_channels_remove(indio_dev);
196 err_free_device:
197         iio_device_free(indio_dev);
198 err_ret:
199         return err;
200 }
201
202 static int tiadc_remove(struct platform_device *pdev)
203 {
204         struct iio_dev *indio_dev = platform_get_drvdata(pdev);
205         struct tiadc_device *adc_dev = iio_priv(indio_dev);
206         u32 step_en;
207
208         iio_device_unregister(indio_dev);
209         tiadc_channels_remove(indio_dev);
210
211         step_en = get_adc_step_mask(adc_dev);
212         am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
213
214         iio_device_free(indio_dev);
215
216         return 0;
217 }
218
219 #ifdef CONFIG_PM
220 static int tiadc_suspend(struct device *dev)
221 {
222         struct iio_dev *indio_dev = dev_get_drvdata(dev);
223         struct tiadc_device *adc_dev = iio_priv(indio_dev);
224         struct ti_tscadc_dev *tscadc_dev;
225         unsigned int idle;
226
227         tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
228         if (!device_may_wakeup(tscadc_dev->dev)) {
229                 idle = tiadc_readl(adc_dev, REG_CTRL);
230                 idle &= ~(CNTRLREG_TSCSSENB);
231                 tiadc_writel(adc_dev, REG_CTRL, (idle |
232                                 CNTRLREG_POWERDOWN));
233         }
234
235         return 0;
236 }
237
238 static int tiadc_resume(struct device *dev)
239 {
240         struct iio_dev *indio_dev = dev_get_drvdata(dev);
241         struct tiadc_device *adc_dev = iio_priv(indio_dev);
242         unsigned int restore;
243
244         /* Make sure ADC is powered up */
245         restore = tiadc_readl(adc_dev, REG_CTRL);
246         restore &= ~(CNTRLREG_POWERDOWN);
247         tiadc_writel(adc_dev, REG_CTRL, restore);
248
249         tiadc_step_config(adc_dev);
250
251         return 0;
252 }
253
254 static const struct dev_pm_ops tiadc_pm_ops = {
255         .suspend = tiadc_suspend,
256         .resume = tiadc_resume,
257 };
258 #define TIADC_PM_OPS (&tiadc_pm_ops)
259 #else
260 #define TIADC_PM_OPS NULL
261 #endif
262
263 static struct platform_driver tiadc_driver = {
264         .driver = {
265                 .name   = "tiadc",
266                 .owner  = THIS_MODULE,
267                 .pm     = TIADC_PM_OPS,
268         },
269         .probe  = tiadc_probe,
270         .remove = tiadc_remove,
271 };
272
273 module_platform_driver(tiadc_driver);
274
275 MODULE_DESCRIPTION("TI ADC controller driver");
276 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
277 MODULE_LICENSE("GPL");