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