blob: b53867cb9cdc5e3148c5135aa2a4f0d2b4addd58 [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, Rachnaabeccee2013-01-24 03:45:05 +000063 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +053064
65 /*
66 * There are 16 configurable steps and 8 analog input
67 * lines available which are shared between Touchscreen and ADC.
68 *
69 * Steps backwards i.e. from 16 towards 0 are used by ADC
70 * depending on number of input lines needed.
71 * Channel would represent which analog input
72 * needs to be given to ADC to digitalize data.
73 */
74
75 steps = TOTAL_STEPS - adc_dev->channels;
Patil, Rachna5e53a692012-10-16 12:55:45 +053076 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
77
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020078 for (i = 0; i < adc_dev->channels; i++) {
79 int chan;
80
81 chan = adc_dev->channel_line[i];
82 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
83 stepconfig | STEPCONFIG_INP(chan));
84 tiadc_writel(adc_dev, REG_STEPDELAY(steps),
Patil, Rachna5e53a692012-10-16 12:55:45 +053085 STEPCONFIG_OPENDLY);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020086 adc_dev->channel_step[i] = steps;
87 steps++;
Patil, Rachna5e53a692012-10-16 12:55:45 +053088 }
Patil, Rachnaabeccee2013-01-24 03:45:05 +000089 step_en = get_adc_step_mask(adc_dev);
90 am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
Patil, Rachna5e53a692012-10-16 12:55:45 +053091}
92
Pantelis Antoniouc80df482012-10-13 16:37:24 +030093static const char * const chan_name_ain[] = {
94 "AIN0",
95 "AIN1",
96 "AIN2",
97 "AIN3",
98 "AIN4",
99 "AIN5",
100 "AIN6",
101 "AIN7",
102};
103
Patil, Rachna5e53a692012-10-16 12:55:45 +0530104static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
105{
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300106 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530107 struct iio_chan_spec *chan_array;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300108 struct iio_chan_spec *chan;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530109 int i;
110
111 indio_dev->num_channels = channels;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300112 chan_array = kcalloc(channels,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530113 sizeof(struct iio_chan_spec), GFP_KERNEL);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530114 if (chan_array == NULL)
115 return -ENOMEM;
116
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300117 chan = chan_array;
118 for (i = 0; i < channels; i++, chan++) {
119
Patil, Rachna5e53a692012-10-16 12:55:45 +0530120 chan->type = IIO_VOLTAGE;
121 chan->indexed = 1;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200122 chan->channel = adc_dev->channel_line[i];
Jonathan Cameron6c572522013-02-27 19:07:18 +0000123 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200124 chan->datasheet_name = chan_name_ain[chan->channel];
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300125 chan->scan_type.sign = 'u';
126 chan->scan_type.realbits = 12;
127 chan->scan_type.storagebits = 32;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530128 }
129
130 indio_dev->channels = chan_array;
131
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300132 return 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530133}
134
135static void tiadc_channels_remove(struct iio_dev *indio_dev)
136{
137 kfree(indio_dev->channels);
138}
139
140static int tiadc_read_raw(struct iio_dev *indio_dev,
141 struct iio_chan_spec const *chan,
142 int *val, int *val2, long mask)
143{
144 struct tiadc_device *adc_dev = iio_priv(indio_dev);
145 int i;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200146 unsigned int fifo1count, read;
147 u32 step = UINT_MAX;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200148 bool found = false;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530149
150 /*
151 * When the sub-system is first enabled,
152 * the sequencer will always start with the
153 * lowest step (1) and continue until step (16).
154 * For ex: If we have enabled 4 ADC channels and
155 * currently use only 1 out of them, the
156 * sequencer still configures all the 4 steps,
157 * leading to 3 unwanted data.
158 * Hence we need to flush out this data.
159 */
160
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200161 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
162 if (chan->channel == adc_dev->channel_line[i]) {
163 step = adc_dev->channel_step[i];
164 break;
165 }
166 }
167 if (WARN_ON_ONCE(step == UINT_MAX))
168 return -EINVAL;
169
Patil, Rachna5e53a692012-10-16 12:55:45 +0530170 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
171 for (i = 0; i < fifo1count; i++) {
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200172 read = tiadc_readl(adc_dev, REG_FIFO1);
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200173 if (read >> 16 == step) {
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200174 *val = read & 0xfff;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200175 found = true;
176 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530177 }
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000178 am335x_tsc_se_update(adc_dev->mfd_tscadc);
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200179 if (found == false)
180 return -EBUSY;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530181 return IIO_VAL_INT;
182}
183
184static const struct iio_info tiadc_info = {
185 .read_raw = &tiadc_read_raw,
Wei Yongjunbc93aa72013-07-06 05:20:00 +0100186 .driver_module = THIS_MODULE,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530187};
188
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800189static int tiadc_probe(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530190{
191 struct iio_dev *indio_dev;
192 struct tiadc_device *adc_dev;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000193 struct device_node *node = pdev->dev.of_node;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200194 struct property *prop;
195 const __be32 *cur;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530196 int err;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200197 u32 val;
198 int channels = 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530199
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200200 if (!node) {
201 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna5e53a692012-10-16 12:55:45 +0530202 return -EINVAL;
203 }
204
Sachin Kamata0648132013-07-23 09:46:00 +0100205 indio_dev = devm_iio_device_alloc(&pdev->dev,
206 sizeof(struct tiadc_device));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530207 if (indio_dev == NULL) {
208 dev_err(&pdev->dev, "failed to allocate iio device\n");
Sachin Kamata0648132013-07-23 09:46:00 +0100209 return -ENOMEM;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530210 }
211 adc_dev = iio_priv(indio_dev);
212
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000213 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
214
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200215 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
216 adc_dev->channel_line[channels] = val;
217 channels++;
218 }
219 adc_dev->channels = channels;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530220
221 indio_dev->dev.parent = &pdev->dev;
222 indio_dev->name = dev_name(&pdev->dev);
223 indio_dev->modes = INDIO_DIRECT_MODE;
224 indio_dev->info = &tiadc_info;
225
226 tiadc_step_config(adc_dev);
227
228 err = tiadc_channel_init(indio_dev, adc_dev->channels);
229 if (err < 0)
Sachin Kamata0648132013-07-23 09:46:00 +0100230 return err;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530231
232 err = iio_device_register(indio_dev);
233 if (err)
234 goto err_free_channels;
235
236 platform_set_drvdata(pdev, indio_dev);
237
238 return 0;
239
240err_free_channels:
241 tiadc_channels_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530242 return err;
243}
244
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800245static int tiadc_remove(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530246{
247 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000248 struct tiadc_device *adc_dev = iio_priv(indio_dev);
249 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530250
251 iio_device_unregister(indio_dev);
252 tiadc_channels_remove(indio_dev);
253
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000254 step_en = get_adc_step_mask(adc_dev);
255 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
256
Patil, Rachna5e53a692012-10-16 12:55:45 +0530257 return 0;
258}
259
260#ifdef CONFIG_PM
261static int tiadc_suspend(struct device *dev)
262{
263 struct iio_dev *indio_dev = dev_get_drvdata(dev);
264 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200265 struct ti_tscadc_dev *tscadc_dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530266 unsigned int idle;
267
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200268 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530269 if (!device_may_wakeup(tscadc_dev->dev)) {
270 idle = tiadc_readl(adc_dev, REG_CTRL);
271 idle &= ~(CNTRLREG_TSCSSENB);
272 tiadc_writel(adc_dev, REG_CTRL, (idle |
273 CNTRLREG_POWERDOWN));
274 }
275
276 return 0;
277}
278
279static int tiadc_resume(struct device *dev)
280{
281 struct iio_dev *indio_dev = dev_get_drvdata(dev);
282 struct tiadc_device *adc_dev = iio_priv(indio_dev);
283 unsigned int restore;
284
285 /* Make sure ADC is powered up */
286 restore = tiadc_readl(adc_dev, REG_CTRL);
287 restore &= ~(CNTRLREG_POWERDOWN);
288 tiadc_writel(adc_dev, REG_CTRL, restore);
289
290 tiadc_step_config(adc_dev);
291
292 return 0;
293}
294
295static const struct dev_pm_ops tiadc_pm_ops = {
296 .suspend = tiadc_suspend,
297 .resume = tiadc_resume,
298};
299#define TIADC_PM_OPS (&tiadc_pm_ops)
300#else
301#define TIADC_PM_OPS NULL
302#endif
303
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000304static const struct of_device_id ti_adc_dt_ids[] = {
305 { .compatible = "ti,am3359-adc", },
306 { }
307};
308MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
309
Patil, Rachna5e53a692012-10-16 12:55:45 +0530310static struct platform_driver tiadc_driver = {
311 .driver = {
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200312 .name = "TI-am335x-adc",
Patil, Rachna5e53a692012-10-16 12:55:45 +0530313 .owner = THIS_MODULE,
314 .pm = TIADC_PM_OPS,
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000315 .of_match_table = of_match_ptr(ti_adc_dt_ids),
Patil, Rachna5e53a692012-10-16 12:55:45 +0530316 },
317 .probe = tiadc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800318 .remove = tiadc_remove,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530319};
Patil, Rachna5e53a692012-10-16 12:55:45 +0530320module_platform_driver(tiadc_driver);
321
322MODULE_DESCRIPTION("TI ADC controller driver");
323MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
324MODULE_LICENSE("GPL");