4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
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.
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.
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>
24 #include <linux/iio/iio.h>
26 #include <linux/of_device.h>
28 #include <linux/mfd/ti_am335x_tscadc.h>
29 #include <linux/platform_data/ti_am335x_adc.h>
32 struct ti_tscadc_dev *mfd_tscadc;
36 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
38 return readl(adc->mfd_tscadc->tscadc_base + reg);
41 static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
44 writel(val, adc->mfd_tscadc->tscadc_base + reg);
47 static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
51 step_en = ((1 << adc_dev->channels) - 1);
52 step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
56 static void tiadc_step_config(struct tiadc_device *adc_dev)
58 unsigned int stepconfig;
59 int i, channels = 0, steps;
63 * There are 16 configurable steps and 8 analog input
64 * lines available which are shared between Touchscreen and ADC.
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.
72 steps = TOTAL_STEPS - adc_dev->channels;
73 channels = TOTAL_CHANNELS - adc_dev->channels;
75 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
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),
84 step_en = get_adc_step_mask(adc_dev);
85 am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
88 static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
90 struct iio_chan_spec *chan_array;
93 indio_dev->num_channels = channels;
94 chan_array = kcalloc(indio_dev->num_channels,
95 sizeof(struct iio_chan_spec), GFP_KERNEL);
97 if (chan_array == NULL)
100 for (i = 0; i < (indio_dev->num_channels); i++) {
101 struct iio_chan_spec *chan = chan_array + i;
102 chan->type = IIO_VOLTAGE;
105 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
108 indio_dev->channels = chan_array;
110 return indio_dev->num_channels;
113 static void tiadc_channels_remove(struct iio_dev *indio_dev)
115 kfree(indio_dev->channels);
118 static int tiadc_read_raw(struct iio_dev *indio_dev,
119 struct iio_chan_spec const *chan,
120 int *val, int *val2, long mask)
122 struct tiadc_device *adc_dev = iio_priv(indio_dev);
124 unsigned int fifo1count, readx1;
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.
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;
143 am335x_tsc_se_update(adc_dev->mfd_tscadc);
148 static const struct iio_info tiadc_info = {
149 .read_raw = &tiadc_read_raw,
152 static int tiadc_probe(struct platform_device *pdev)
154 struct iio_dev *indio_dev;
155 struct tiadc_device *adc_dev;
156 struct ti_tscadc_dev *tscadc_dev = ti_tscadc_dev_get(pdev);
157 struct mfd_tscadc_board *pdata = tscadc_dev->dev->platform_data;
158 struct device_node *node = pdev->dev.of_node;
162 if (!pdata && !node) {
163 dev_err(&pdev->dev, "Could not find platform data\n");
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");
173 adc_dev = iio_priv(indio_dev);
175 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
178 adc_dev->channels = pdata->adc_init->adc_channels;
180 err = of_property_read_u32(node,
181 "ti,adc-channels", &val32);
183 goto err_free_device;
184 adc_dev->channels = val32;
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;
192 tiadc_step_config(adc_dev);
194 err = tiadc_channel_init(indio_dev, adc_dev->channels);
196 goto err_free_device;
198 err = iio_device_register(indio_dev);
200 goto err_free_channels;
202 platform_set_drvdata(pdev, indio_dev);
207 tiadc_channels_remove(indio_dev);
209 iio_device_free(indio_dev);
214 static int tiadc_remove(struct platform_device *pdev)
216 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
217 struct tiadc_device *adc_dev = iio_priv(indio_dev);
220 iio_device_unregister(indio_dev);
221 tiadc_channels_remove(indio_dev);
223 step_en = get_adc_step_mask(adc_dev);
224 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
226 iio_device_free(indio_dev);
232 static int tiadc_suspend(struct device *dev)
234 struct iio_dev *indio_dev = dev_get_drvdata(dev);
235 struct tiadc_device *adc_dev = iio_priv(indio_dev);
236 struct ti_tscadc_dev *tscadc_dev;
239 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
240 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));
250 static int tiadc_resume(struct device *dev)
252 struct iio_dev *indio_dev = dev_get_drvdata(dev);
253 struct tiadc_device *adc_dev = iio_priv(indio_dev);
254 unsigned int restore;
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);
261 tiadc_step_config(adc_dev);
266 static const struct dev_pm_ops tiadc_pm_ops = {
267 .suspend = tiadc_suspend,
268 .resume = tiadc_resume,
270 #define TIADC_PM_OPS (&tiadc_pm_ops)
272 #define TIADC_PM_OPS NULL
275 static const struct of_device_id ti_adc_dt_ids[] = {
276 { .compatible = "ti,am3359-adc", },
279 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
281 static struct platform_driver tiadc_driver = {
284 .owner = THIS_MODULE,
286 .of_match_table = of_match_ptr(ti_adc_dt_ids),
288 .probe = tiadc_probe,
289 .remove = tiadc_remove,
292 module_platform_driver(tiadc_driver);
294 MODULE_DESCRIPTION("TI ADC controller driver");
295 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
296 MODULE_LICENSE("GPL");