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>
27 #include <linux/iio/machine.h>
28 #include <linux/iio/driver.h>
30 #include <linux/mfd/ti_am335x_tscadc.h>
33 struct ti_tscadc_dev *mfd_tscadc;
37 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
39 return readl(adc->mfd_tscadc->tscadc_base + reg);
42 static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
45 writel(val, adc->mfd_tscadc->tscadc_base + reg);
48 static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
52 step_en = ((1 << adc_dev->channels) - 1);
53 step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
57 static void tiadc_step_config(struct tiadc_device *adc_dev)
59 unsigned int stepconfig;
60 int i, channels = 0, steps;
64 * There are 16 configurable steps and 8 analog input
65 * lines available which are shared between Touchscreen and ADC.
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.
73 steps = TOTAL_STEPS - adc_dev->channels;
74 channels = TOTAL_CHANNELS - adc_dev->channels;
76 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
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),
85 step_en = get_adc_step_mask(adc_dev);
86 am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
89 static const char * const chan_name_ain[] = {
100 static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
102 struct tiadc_device *adc_dev = iio_priv(indio_dev);
103 struct iio_chan_spec *chan_array;
104 struct iio_chan_spec *chan;
107 indio_dev->num_channels = channels;
108 chan_array = kcalloc(channels,
109 sizeof(struct iio_chan_spec), GFP_KERNEL);
110 if (chan_array == NULL)
114 for (i = 0; i < channels; i++, chan++) {
116 chan->type = IIO_VOLTAGE;
119 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
120 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;
126 indio_dev->channels = chan_array;
131 static void tiadc_channels_remove(struct iio_dev *indio_dev)
133 kfree(indio_dev->channels);
136 static int tiadc_read_raw(struct iio_dev *indio_dev,
137 struct iio_chan_spec const *chan,
138 int *val, int *val2, long mask)
140 struct tiadc_device *adc_dev = iio_priv(indio_dev);
142 unsigned int fifo1count, readx1;
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.
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;
161 am335x_tsc_se_update(adc_dev->mfd_tscadc);
166 static const struct iio_info tiadc_info = {
167 .read_raw = &tiadc_read_raw,
170 static int tiadc_probe(struct platform_device *pdev)
172 struct iio_dev *indio_dev;
173 struct tiadc_device *adc_dev;
174 struct device_node *node = pdev->dev.of_node;
179 dev_err(&pdev->dev, "Could not find valid DT data.\n");
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");
189 adc_dev = iio_priv(indio_dev);
191 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
193 err = of_property_read_u32(node,
194 "ti,adc-channels", &val32);
196 goto err_free_device;
197 adc_dev->channels = val32;
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;
204 tiadc_step_config(adc_dev);
206 err = tiadc_channel_init(indio_dev, adc_dev->channels);
208 goto err_free_device;
210 err = iio_device_register(indio_dev);
212 goto err_free_channels;
214 platform_set_drvdata(pdev, indio_dev);
219 tiadc_channels_remove(indio_dev);
221 iio_device_free(indio_dev);
226 static int tiadc_remove(struct platform_device *pdev)
228 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
229 struct tiadc_device *adc_dev = iio_priv(indio_dev);
232 iio_device_unregister(indio_dev);
233 tiadc_channels_remove(indio_dev);
235 step_en = get_adc_step_mask(adc_dev);
236 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
238 iio_device_free(indio_dev);
244 static int tiadc_suspend(struct device *dev)
246 struct iio_dev *indio_dev = dev_get_drvdata(dev);
247 struct tiadc_device *adc_dev = iio_priv(indio_dev);
248 struct ti_tscadc_dev *tscadc_dev;
251 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
252 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));
262 static int tiadc_resume(struct device *dev)
264 struct iio_dev *indio_dev = dev_get_drvdata(dev);
265 struct tiadc_device *adc_dev = iio_priv(indio_dev);
266 unsigned int restore;
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);
273 tiadc_step_config(adc_dev);
278 static const struct dev_pm_ops tiadc_pm_ops = {
279 .suspend = tiadc_suspend,
280 .resume = tiadc_resume,
282 #define TIADC_PM_OPS (&tiadc_pm_ops)
284 #define TIADC_PM_OPS NULL
287 static const struct of_device_id ti_adc_dt_ids[] = {
288 { .compatible = "ti,am3359-adc", },
291 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
293 static struct platform_driver tiadc_driver = {
296 .owner = THIS_MODULE,
298 .of_match_table = of_match_ptr(ti_adc_dt_ids),
300 .probe = tiadc_probe,
301 .remove = tiadc_remove,
304 module_platform_driver(tiadc_driver);
306 MODULE_DESCRIPTION("TI ADC controller driver");
307 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
308 MODULE_LICENSE("GPL");