blob: 2868c0c1b7e02b9c1ec8b139ae3505b73af76382 [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>
Patil, Rachna5e53a692012-10-16 12:55:45 +053029
30struct tiadc_device {
31 struct ti_tscadc_dev *mfd_tscadc;
32 int channels;
33};
34
35static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
36{
37 return readl(adc->mfd_tscadc->tscadc_base + reg);
38}
39
40static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
41 unsigned int val)
42{
43 writel(val, adc->mfd_tscadc->tscadc_base + reg);
44}
45
Patil, Rachnaabeccee2013-01-24 03:45:05 +000046static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
47{
48 u32 step_en;
49
50 step_en = ((1 << adc_dev->channels) - 1);
51 step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
52 return step_en;
53}
54
Patil, Rachna5e53a692012-10-16 12:55:45 +053055static void tiadc_step_config(struct tiadc_device *adc_dev)
56{
57 unsigned int stepconfig;
58 int i, channels = 0, steps;
Patil, Rachnaabeccee2013-01-24 03:45:05 +000059 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +053060
61 /*
62 * There are 16 configurable steps and 8 analog input
63 * lines available which are shared between Touchscreen and ADC.
64 *
65 * Steps backwards i.e. from 16 towards 0 are used by ADC
66 * depending on number of input lines needed.
67 * Channel would represent which analog input
68 * needs to be given to ADC to digitalize data.
69 */
70
71 steps = TOTAL_STEPS - adc_dev->channels;
72 channels = TOTAL_CHANNELS - adc_dev->channels;
73
74 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
75
76 for (i = (steps + 1); i <= TOTAL_STEPS; i++) {
77 tiadc_writel(adc_dev, REG_STEPCONFIG(i),
78 stepconfig | STEPCONFIG_INP(channels));
79 tiadc_writel(adc_dev, REG_STEPDELAY(i),
80 STEPCONFIG_OPENDLY);
81 channels++;
82 }
Patil, Rachnaabeccee2013-01-24 03:45:05 +000083 step_en = get_adc_step_mask(adc_dev);
84 am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
Patil, Rachna5e53a692012-10-16 12:55:45 +053085}
86
87static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
88{
89 struct iio_chan_spec *chan_array;
90 int i;
91
92 indio_dev->num_channels = channels;
93 chan_array = kcalloc(indio_dev->num_channels,
94 sizeof(struct iio_chan_spec), GFP_KERNEL);
95
96 if (chan_array == NULL)
97 return -ENOMEM;
98
99 for (i = 0; i < (indio_dev->num_channels); i++) {
100 struct iio_chan_spec *chan = chan_array + i;
101 chan->type = IIO_VOLTAGE;
102 chan->indexed = 1;
103 chan->channel = i;
Jonathan Cameron6c572522013-02-27 19:07:18 +0000104 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530105 }
106
107 indio_dev->channels = chan_array;
108
109 return indio_dev->num_channels;
110}
111
112static void tiadc_channels_remove(struct iio_dev *indio_dev)
113{
114 kfree(indio_dev->channels);
115}
116
117static int tiadc_read_raw(struct iio_dev *indio_dev,
118 struct iio_chan_spec const *chan,
119 int *val, int *val2, long mask)
120{
121 struct tiadc_device *adc_dev = iio_priv(indio_dev);
122 int i;
123 unsigned int fifo1count, readx1;
124
125 /*
126 * When the sub-system is first enabled,
127 * the sequencer will always start with the
128 * lowest step (1) and continue until step (16).
129 * For ex: If we have enabled 4 ADC channels and
130 * currently use only 1 out of them, the
131 * sequencer still configures all the 4 steps,
132 * leading to 3 unwanted data.
133 * Hence we need to flush out this data.
134 */
135
136 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
137 for (i = 0; i < fifo1count; i++) {
138 readx1 = tiadc_readl(adc_dev, REG_FIFO1);
139 if (i == chan->channel)
140 *val = readx1 & 0xfff;
141 }
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000142 am335x_tsc_se_update(adc_dev->mfd_tscadc);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530143
144 return IIO_VAL_INT;
145}
146
147static const struct iio_info tiadc_info = {
148 .read_raw = &tiadc_read_raw,
149};
150
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800151static int tiadc_probe(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530152{
153 struct iio_dev *indio_dev;
154 struct tiadc_device *adc_dev;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000155 struct device_node *node = pdev->dev.of_node;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530156 int err;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000157 u32 val32;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530158
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200159 if (!node) {
160 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna5e53a692012-10-16 12:55:45 +0530161 return -EINVAL;
162 }
163
164 indio_dev = iio_device_alloc(sizeof(struct tiadc_device));
165 if (indio_dev == NULL) {
166 dev_err(&pdev->dev, "failed to allocate iio device\n");
167 err = -ENOMEM;
168 goto err_ret;
169 }
170 adc_dev = iio_priv(indio_dev);
171
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000172 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
173
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200174 err = of_property_read_u32(node,
175 "ti,adc-channels", &val32);
176 if (err < 0)
177 goto err_free_device;
178 adc_dev->channels = val32;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530179
180 indio_dev->dev.parent = &pdev->dev;
181 indio_dev->name = dev_name(&pdev->dev);
182 indio_dev->modes = INDIO_DIRECT_MODE;
183 indio_dev->info = &tiadc_info;
184
185 tiadc_step_config(adc_dev);
186
187 err = tiadc_channel_init(indio_dev, adc_dev->channels);
188 if (err < 0)
189 goto err_free_device;
190
191 err = iio_device_register(indio_dev);
192 if (err)
193 goto err_free_channels;
194
195 platform_set_drvdata(pdev, indio_dev);
196
197 return 0;
198
199err_free_channels:
200 tiadc_channels_remove(indio_dev);
201err_free_device:
202 iio_device_free(indio_dev);
203err_ret:
204 return err;
205}
206
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800207static int tiadc_remove(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530208{
209 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000210 struct tiadc_device *adc_dev = iio_priv(indio_dev);
211 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530212
213 iio_device_unregister(indio_dev);
214 tiadc_channels_remove(indio_dev);
215
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000216 step_en = get_adc_step_mask(adc_dev);
217 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
218
Patil, Rachna5e53a692012-10-16 12:55:45 +0530219 iio_device_free(indio_dev);
220
221 return 0;
222}
223
224#ifdef CONFIG_PM
225static int tiadc_suspend(struct device *dev)
226{
227 struct iio_dev *indio_dev = dev_get_drvdata(dev);
228 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200229 struct ti_tscadc_dev *tscadc_dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530230 unsigned int idle;
231
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200232 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530233 if (!device_may_wakeup(tscadc_dev->dev)) {
234 idle = tiadc_readl(adc_dev, REG_CTRL);
235 idle &= ~(CNTRLREG_TSCSSENB);
236 tiadc_writel(adc_dev, REG_CTRL, (idle |
237 CNTRLREG_POWERDOWN));
238 }
239
240 return 0;
241}
242
243static int tiadc_resume(struct device *dev)
244{
245 struct iio_dev *indio_dev = dev_get_drvdata(dev);
246 struct tiadc_device *adc_dev = iio_priv(indio_dev);
247 unsigned int restore;
248
249 /* Make sure ADC is powered up */
250 restore = tiadc_readl(adc_dev, REG_CTRL);
251 restore &= ~(CNTRLREG_POWERDOWN);
252 tiadc_writel(adc_dev, REG_CTRL, restore);
253
254 tiadc_step_config(adc_dev);
255
256 return 0;
257}
258
259static const struct dev_pm_ops tiadc_pm_ops = {
260 .suspend = tiadc_suspend,
261 .resume = tiadc_resume,
262};
263#define TIADC_PM_OPS (&tiadc_pm_ops)
264#else
265#define TIADC_PM_OPS NULL
266#endif
267
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000268static const struct of_device_id ti_adc_dt_ids[] = {
269 { .compatible = "ti,am3359-adc", },
270 { }
271};
272MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
273
Patil, Rachna5e53a692012-10-16 12:55:45 +0530274static struct platform_driver tiadc_driver = {
275 .driver = {
276 .name = "tiadc",
277 .owner = THIS_MODULE,
278 .pm = TIADC_PM_OPS,
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000279 .of_match_table = of_match_ptr(ti_adc_dt_ids),
Patil, Rachna5e53a692012-10-16 12:55:45 +0530280 },
281 .probe = tiadc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800282 .remove = tiadc_remove,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530283};
284
285module_platform_driver(tiadc_driver);
286
287MODULE_DESCRIPTION("TI ADC controller driver");
288MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
289MODULE_LICENSE("GPL");