blob: 3ceac3e91dde4cd30339bc3c61feb4ad570a03c7 [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;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020035 u8 channel_line[8];
36 u8 channel_step[8];
Patil, Rachna5e53a692012-10-16 12:55:45 +053037};
38
39static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
40{
41 return readl(adc->mfd_tscadc->tscadc_base + reg);
42}
43
44static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
45 unsigned int val)
46{
47 writel(val, adc->mfd_tscadc->tscadc_base + reg);
48}
49
Patil, Rachnaabeccee2013-01-24 03:45:05 +000050static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
51{
52 u32 step_en;
53
54 step_en = ((1 << adc_dev->channels) - 1);
55 step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
56 return step_en;
57}
58
Patil, Rachna5e53a692012-10-16 12:55:45 +053059static void tiadc_step_config(struct tiadc_device *adc_dev)
60{
61 unsigned int stepconfig;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020062 int i, steps;
Patil, Rachna5e53a692012-10-16 12:55:45 +053063
64 /*
65 * There are 16 configurable steps and 8 analog input
66 * lines available which are shared between Touchscreen and ADC.
67 *
68 * Steps backwards i.e. from 16 towards 0 are used by ADC
69 * depending on number of input lines needed.
70 * Channel would represent which analog input
71 * needs to be given to ADC to digitalize data.
72 */
73
74 steps = TOTAL_STEPS - adc_dev->channels;
Patil, Rachna5e53a692012-10-16 12:55:45 +053075 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
76
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020077 for (i = 0; i < adc_dev->channels; i++) {
78 int chan;
79
80 chan = adc_dev->channel_line[i];
81 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
82 stepconfig | STEPCONFIG_INP(chan));
83 tiadc_writel(adc_dev, REG_STEPDELAY(steps),
Patil, Rachna5e53a692012-10-16 12:55:45 +053084 STEPCONFIG_OPENDLY);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020085 adc_dev->channel_step[i] = steps;
86 steps++;
Patil, Rachna5e53a692012-10-16 12:55:45 +053087 }
Patil, Rachnab1451e52013-07-20 17:27:00 +010088
Patil, Rachna5e53a692012-10-16 12:55:45 +053089}
90
Pantelis Antoniouc80df482012-10-13 16:37:24 +030091static const char * const chan_name_ain[] = {
92 "AIN0",
93 "AIN1",
94 "AIN2",
95 "AIN3",
96 "AIN4",
97 "AIN5",
98 "AIN6",
99 "AIN7",
100};
101
Patil, Rachna5e53a692012-10-16 12:55:45 +0530102static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
103{
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300104 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530105 struct iio_chan_spec *chan_array;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300106 struct iio_chan_spec *chan;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530107 int i;
108
109 indio_dev->num_channels = channels;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300110 chan_array = kcalloc(channels,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530111 sizeof(struct iio_chan_spec), GFP_KERNEL);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530112 if (chan_array == NULL)
113 return -ENOMEM;
114
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300115 chan = chan_array;
116 for (i = 0; i < channels; i++, chan++) {
117
Patil, Rachna5e53a692012-10-16 12:55:45 +0530118 chan->type = IIO_VOLTAGE;
119 chan->indexed = 1;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200120 chan->channel = adc_dev->channel_line[i];
Jonathan Cameron6c572522013-02-27 19:07:18 +0000121 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200122 chan->datasheet_name = chan_name_ain[chan->channel];
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300123 chan->scan_type.sign = 'u';
124 chan->scan_type.realbits = 12;
125 chan->scan_type.storagebits = 32;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530126 }
127
128 indio_dev->channels = chan_array;
129
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300130 return 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530131}
132
133static void tiadc_channels_remove(struct iio_dev *indio_dev)
134{
135 kfree(indio_dev->channels);
136}
137
138static int tiadc_read_raw(struct iio_dev *indio_dev,
139 struct iio_chan_spec const *chan,
140 int *val, int *val2, long mask)
141{
142 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100143 int i, map_val;
144 unsigned int fifo1count, read, stepid;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200145 u32 step = UINT_MAX;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200146 bool found = false;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100147 u32 step_en;
148 unsigned long timeout = jiffies + usecs_to_jiffies
149 (IDLE_TIMEOUT * adc_dev->channels);
150 step_en = get_adc_step_mask(adc_dev);
151 am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
152
153 /* Wait for ADC sequencer to complete sampling */
154 while (tiadc_readl(adc_dev, REG_ADCFSM) & SEQ_STATUS) {
155 if (time_after(jiffies, timeout))
156 return -EAGAIN;
157 }
158 map_val = chan->channel + TOTAL_CHANNELS;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530159
160 /*
161 * When the sub-system is first enabled,
162 * the sequencer will always start with the
163 * lowest step (1) and continue until step (16).
164 * For ex: If we have enabled 4 ADC channels and
165 * currently use only 1 out of them, the
166 * sequencer still configures all the 4 steps,
167 * leading to 3 unwanted data.
168 * Hence we need to flush out this data.
169 */
170
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200171 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
172 if (chan->channel == adc_dev->channel_line[i]) {
173 step = adc_dev->channel_step[i];
174 break;
175 }
176 }
177 if (WARN_ON_ONCE(step == UINT_MAX))
178 return -EINVAL;
179
Patil, Rachna5e53a692012-10-16 12:55:45 +0530180 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
181 for (i = 0; i < fifo1count; i++) {
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200182 read = tiadc_readl(adc_dev, REG_FIFO1);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100183 stepid = read & FIFOREAD_CHNLID_MASK;
184 stepid = stepid >> 0x10;
185
186 if (stepid == map_val) {
187 read = read & FIFOREAD_DATA_MASK;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200188 found = true;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100189 *val = read;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200190 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530191 }
Patil, Rachnab1451e52013-07-20 17:27:00 +0100192
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200193 if (found == false)
194 return -EBUSY;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530195 return IIO_VAL_INT;
196}
197
198static const struct iio_info tiadc_info = {
199 .read_raw = &tiadc_read_raw,
Wei Yongjunbc93aa72013-07-06 05:20:00 +0100200 .driver_module = THIS_MODULE,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530201};
202
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800203static int tiadc_probe(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530204{
205 struct iio_dev *indio_dev;
206 struct tiadc_device *adc_dev;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000207 struct device_node *node = pdev->dev.of_node;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200208 struct property *prop;
209 const __be32 *cur;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530210 int err;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200211 u32 val;
212 int channels = 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530213
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200214 if (!node) {
215 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna5e53a692012-10-16 12:55:45 +0530216 return -EINVAL;
217 }
218
219 indio_dev = iio_device_alloc(sizeof(struct tiadc_device));
220 if (indio_dev == NULL) {
221 dev_err(&pdev->dev, "failed to allocate iio device\n");
222 err = -ENOMEM;
223 goto err_ret;
224 }
225 adc_dev = iio_priv(indio_dev);
226
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000227 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
228
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200229 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
230 adc_dev->channel_line[channels] = val;
231 channels++;
232 }
233 adc_dev->channels = channels;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530234
235 indio_dev->dev.parent = &pdev->dev;
236 indio_dev->name = dev_name(&pdev->dev);
237 indio_dev->modes = INDIO_DIRECT_MODE;
238 indio_dev->info = &tiadc_info;
239
240 tiadc_step_config(adc_dev);
241
242 err = tiadc_channel_init(indio_dev, adc_dev->channels);
243 if (err < 0)
244 goto err_free_device;
245
246 err = iio_device_register(indio_dev);
247 if (err)
248 goto err_free_channels;
249
250 platform_set_drvdata(pdev, indio_dev);
251
252 return 0;
253
254err_free_channels:
255 tiadc_channels_remove(indio_dev);
256err_free_device:
257 iio_device_free(indio_dev);
258err_ret:
259 return err;
260}
261
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800262static int tiadc_remove(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530263{
264 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000265 struct tiadc_device *adc_dev = iio_priv(indio_dev);
266 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530267
268 iio_device_unregister(indio_dev);
269 tiadc_channels_remove(indio_dev);
270
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000271 step_en = get_adc_step_mask(adc_dev);
272 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
273
Patil, Rachna5e53a692012-10-16 12:55:45 +0530274 iio_device_free(indio_dev);
275
276 return 0;
277}
278
279#ifdef CONFIG_PM
280static int tiadc_suspend(struct device *dev)
281{
282 struct iio_dev *indio_dev = dev_get_drvdata(dev);
283 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200284 struct ti_tscadc_dev *tscadc_dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530285 unsigned int idle;
286
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200287 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530288 if (!device_may_wakeup(tscadc_dev->dev)) {
289 idle = tiadc_readl(adc_dev, REG_CTRL);
290 idle &= ~(CNTRLREG_TSCSSENB);
291 tiadc_writel(adc_dev, REG_CTRL, (idle |
292 CNTRLREG_POWERDOWN));
293 }
294
295 return 0;
296}
297
298static int tiadc_resume(struct device *dev)
299{
300 struct iio_dev *indio_dev = dev_get_drvdata(dev);
301 struct tiadc_device *adc_dev = iio_priv(indio_dev);
302 unsigned int restore;
303
304 /* Make sure ADC is powered up */
305 restore = tiadc_readl(adc_dev, REG_CTRL);
306 restore &= ~(CNTRLREG_POWERDOWN);
307 tiadc_writel(adc_dev, REG_CTRL, restore);
308
309 tiadc_step_config(adc_dev);
310
311 return 0;
312}
313
314static const struct dev_pm_ops tiadc_pm_ops = {
315 .suspend = tiadc_suspend,
316 .resume = tiadc_resume,
317};
318#define TIADC_PM_OPS (&tiadc_pm_ops)
319#else
320#define TIADC_PM_OPS NULL
321#endif
322
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000323static const struct of_device_id ti_adc_dt_ids[] = {
324 { .compatible = "ti,am3359-adc", },
325 { }
326};
327MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
328
Patil, Rachna5e53a692012-10-16 12:55:45 +0530329static struct platform_driver tiadc_driver = {
330 .driver = {
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200331 .name = "TI-am335x-adc",
Patil, Rachna5e53a692012-10-16 12:55:45 +0530332 .owner = THIS_MODULE,
333 .pm = TIADC_PM_OPS,
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000334 .of_match_table = of_match_ptr(ti_adc_dt_ids),
Patil, Rachna5e53a692012-10-16 12:55:45 +0530335 },
336 .probe = tiadc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800337 .remove = tiadc_remove,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530338};
Patil, Rachna5e53a692012-10-16 12:55:45 +0530339module_platform_driver(tiadc_driver);
340
341MODULE_DESCRIPTION("TI ADC controller driver");
342MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
343MODULE_LICENSE("GPL");