blob: 9939810954f1d35e73e26707dd222f032f8ffe53 [file] [log] [blame]
Patil, Rachna5e53a692012-10-16 12:55:45 +05301/*
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>
Patil, Rachna6f39ac42013-01-24 03:45:11 +000025#include <linux/of.h>
26#include <linux/of_device.h>
Pantelis Antoniouc80df482012-10-13 16:37:24 +030027#include <linux/iio/machine.h>
28#include <linux/iio/driver.h>
Patil, Rachna5e53a692012-10-16 12:55:45 +053029
30#include <linux/mfd/ti_am335x_tscadc.h>
Patil, Rachna5e53a692012-10-16 12:55:45 +053031
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
Patil, Rachnaabeccee2013-01-24 03:45:05 +000048static 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
Patil, Rachna5e53a692012-10-16 12:55:45 +053057static void tiadc_step_config(struct tiadc_device *adc_dev)
58{
59 unsigned int stepconfig;
60 int i, channels = 0, steps;
Patil, Rachnaabeccee2013-01-24 03:45:05 +000061 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +053062
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 }
Patil, Rachnaabeccee2013-01-24 03:45:05 +000085 step_en = get_adc_step_mask(adc_dev);
86 am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
Patil, Rachna5e53a692012-10-16 12:55:45 +053087}
88
Pantelis Antoniouc80df482012-10-13 16:37:24 +030089static const char * const chan_name_ain[] = {
90 "AIN0",
91 "AIN1",
92 "AIN2",
93 "AIN3",
94 "AIN4",
95 "AIN5",
96 "AIN6",
97 "AIN7",
98};
99
Patil, Rachna5e53a692012-10-16 12:55:45 +0530100static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
101{
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300102 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530103 struct iio_chan_spec *chan_array;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300104 struct iio_chan_spec *chan;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530105 int i;
106
107 indio_dev->num_channels = channels;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300108 chan_array = kcalloc(channels,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530109 sizeof(struct iio_chan_spec), GFP_KERNEL);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530110 if (chan_array == NULL)
111 return -ENOMEM;
112
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300113 chan = chan_array;
114 for (i = 0; i < channels; i++, chan++) {
115
Patil, Rachna5e53a692012-10-16 12:55:45 +0530116 chan->type = IIO_VOLTAGE;
117 chan->indexed = 1;
118 chan->channel = i;
Jonathan Cameron6c572522013-02-27 19:07:18 +0000119 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300120 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;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530124 }
125
126 indio_dev->channels = chan_array;
127
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300128 return 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530129}
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 }
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000161 am335x_tsc_se_update(adc_dev->mfd_tscadc);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530162
163 return IIO_VAL_INT;
164}
165
166static const struct iio_info tiadc_info = {
167 .read_raw = &tiadc_read_raw,
168};
169
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800170static int tiadc_probe(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530171{
172 struct iio_dev *indio_dev;
173 struct tiadc_device *adc_dev;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000174 struct device_node *node = pdev->dev.of_node;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530175 int err;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000176 u32 val32;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530177
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200178 if (!node) {
179 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna5e53a692012-10-16 12:55:45 +0530180 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
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000191 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
192
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200193 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;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530198
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
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800226static int tiadc_remove(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530227{
228 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000229 struct tiadc_device *adc_dev = iio_priv(indio_dev);
230 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530231
232 iio_device_unregister(indio_dev);
233 tiadc_channels_remove(indio_dev);
234
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000235 step_en = get_adc_step_mask(adc_dev);
236 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
237
Patil, Rachna5e53a692012-10-16 12:55:45 +0530238 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);
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200248 struct ti_tscadc_dev *tscadc_dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530249 unsigned int idle;
250
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200251 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530252 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
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000287static 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
Patil, Rachna5e53a692012-10-16 12:55:45 +0530293static struct platform_driver tiadc_driver = {
294 .driver = {
295 .name = "tiadc",
296 .owner = THIS_MODULE,
297 .pm = TIADC_PM_OPS,
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000298 .of_match_table = of_match_ptr(ti_adc_dt_ids),
Patil, Rachna5e53a692012-10-16 12:55:45 +0530299 },
300 .probe = tiadc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800301 .remove = tiadc_remove,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530302};
303
304module_platform_driver(tiadc_driver);
305
306MODULE_DESCRIPTION("TI ADC controller driver");
307MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
308MODULE_LICENSE("GPL");