blob: b24402cb5432b83dbbc4dd2c8db52166f93093b5 [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>
Patil, Rachna5e53a692012-10-16 12:55:45 +053027
28#include <linux/mfd/ti_am335x_tscadc.h>
29#include <linux/platform_data/ti_am335x_adc.h>
30
31struct tiadc_device {
32 struct ti_tscadc_dev *mfd_tscadc;
33 int channels;
34};
35
36static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
37{
38 return readl(adc->mfd_tscadc->tscadc_base + reg);
39}
40
41static 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
Patil, Rachnaabeccee2013-01-24 03:45:05 +000047static 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
Patil, Rachna5e53a692012-10-16 12:55:45 +053056static void tiadc_step_config(struct tiadc_device *adc_dev)
57{
58 unsigned int stepconfig;
59 int i, channels = 0, steps;
Patil, Rachnaabeccee2013-01-24 03:45:05 +000060 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +053061
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 }
Patil, Rachnaabeccee2013-01-24 03:45:05 +000084 step_en = get_adc_step_mask(adc_dev);
85 am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
Patil, Rachna5e53a692012-10-16 12:55:45 +053086}
87
88static 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;
Jonathan Cameron6c572522013-02-27 19:07:18 +0000105 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530106 }
107
108 indio_dev->channels = chan_array;
109
110 return indio_dev->num_channels;
111}
112
113static void tiadc_channels_remove(struct iio_dev *indio_dev)
114{
115 kfree(indio_dev->channels);
116}
117
118static 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 }
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000143 am335x_tsc_se_update(adc_dev->mfd_tscadc);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530144
145 return IIO_VAL_INT;
146}
147
148static const struct iio_info tiadc_info = {
149 .read_raw = &tiadc_read_raw,
150};
151
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800152static int tiadc_probe(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530153{
154 struct iio_dev *indio_dev;
155 struct tiadc_device *adc_dev;
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200156 struct ti_tscadc_dev *tscadc_dev = ti_tscadc_dev_get(pdev);
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000157 struct mfd_tscadc_board *pdata = tscadc_dev->dev->platform_data;
158 struct device_node *node = pdev->dev.of_node;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530159 int err;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000160 u32 val32;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530161
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000162 if (!pdata && !node) {
Patil, Rachna5e53a692012-10-16 12:55:45 +0530163 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
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000175 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 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530186
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
206err_free_channels:
207 tiadc_channels_remove(indio_dev);
208err_free_device:
209 iio_device_free(indio_dev);
210err_ret:
211 return err;
212}
213
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800214static int tiadc_remove(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530215{
216 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000217 struct tiadc_device *adc_dev = iio_priv(indio_dev);
218 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530219
220 iio_device_unregister(indio_dev);
221 tiadc_channels_remove(indio_dev);
222
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000223 step_en = get_adc_step_mask(adc_dev);
224 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
225
Patil, Rachna5e53a692012-10-16 12:55:45 +0530226 iio_device_free(indio_dev);
227
228 return 0;
229}
230
231#ifdef CONFIG_PM
232static 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);
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200236 struct ti_tscadc_dev *tscadc_dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530237 unsigned int idle;
238
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200239 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530240 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
250static 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
266static 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
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000275static const struct of_device_id ti_adc_dt_ids[] = {
276 { .compatible = "ti,am3359-adc", },
277 { }
278};
279MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
280
Patil, Rachna5e53a692012-10-16 12:55:45 +0530281static struct platform_driver tiadc_driver = {
282 .driver = {
283 .name = "tiadc",
284 .owner = THIS_MODULE,
285 .pm = TIADC_PM_OPS,
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000286 .of_match_table = of_match_ptr(ti_adc_dt_ids),
Patil, Rachna5e53a692012-10-16 12:55:45 +0530287 },
288 .probe = tiadc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800289 .remove = tiadc_remove,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530290};
291
292module_platform_driver(tiadc_driver);
293
294MODULE_DESCRIPTION("TI ADC controller driver");
295MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
296MODULE_LICENSE("GPL");