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