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
;
39 static unsigned int tiadc_readl(struct tiadc_device
*adc
, unsigned int reg
)
41 return readl(adc
->mfd_tscadc
->tscadc_base
+ reg
);
44 static void tiadc_writel(struct tiadc_device
*adc
, unsigned int reg
,
47 writel(val
, adc
->mfd_tscadc
->tscadc_base
+ reg
);
50 static u32
get_adc_step_mask(struct tiadc_device
*adc_dev
)
54 step_en
= ((1 << adc_dev
->channels
) - 1);
55 step_en
<<= TOTAL_STEPS
- adc_dev
->channels
+ 1;
59 static void tiadc_step_config(struct tiadc_device
*adc_dev
)
61 unsigned int stepconfig
;
66 * There are 16 configurable steps and 8 analog input
67 * lines available which are shared between Touchscreen and ADC.
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.
75 steps
= TOTAL_STEPS
- adc_dev
->channels
;
76 stepconfig
= STEPCONFIG_AVG_16
| STEPCONFIG_FIFO1
;
78 for (i
= 0; i
< adc_dev
->channels
; i
++) {
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
),
86 adc_dev
->channel_step
[i
] = steps
;
89 step_en
= get_adc_step_mask(adc_dev
);
90 am335x_tsc_se_set(adc_dev
->mfd_tscadc
, step_en
);
93 static const char * const chan_name_ain
[] = {
104 static int tiadc_channel_init(struct iio_dev
*indio_dev
, int channels
)
106 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
107 struct iio_chan_spec
*chan_array
;
108 struct iio_chan_spec
*chan
;
111 indio_dev
->num_channels
= channels
;
112 chan_array
= kcalloc(channels
,
113 sizeof(struct iio_chan_spec
), GFP_KERNEL
);
114 if (chan_array
== NULL
)
118 for (i
= 0; i
< channels
; i
++, chan
++) {
120 chan
->type
= IIO_VOLTAGE
;
122 chan
->channel
= adc_dev
->channel_line
[i
];
123 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
124 chan
->datasheet_name
= chan_name_ain
[chan
->channel
];
125 chan
->scan_type
.sign
= 'u';
126 chan
->scan_type
.realbits
= 12;
127 chan
->scan_type
.storagebits
= 32;
130 indio_dev
->channels
= chan_array
;
135 static void tiadc_channels_remove(struct iio_dev
*indio_dev
)
137 kfree(indio_dev
->channels
);
140 static int tiadc_read_raw(struct iio_dev
*indio_dev
,
141 struct iio_chan_spec
const *chan
,
142 int *val
, int *val2
, long mask
)
144 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
146 unsigned int fifo1count
, read
;
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.
161 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
];
167 if (WARN_ON_ONCE(step
== UINT_MAX
))
170 fifo1count
= tiadc_readl(adc_dev
, REG_FIFO1CNT
);
171 for (i
= 0; i
< fifo1count
; i
++) {
172 read
= tiadc_readl(adc_dev
, REG_FIFO1
);
173 if (read
>> 16 == step
) {
178 am335x_tsc_se_update(adc_dev
->mfd_tscadc
);
184 static const struct iio_info tiadc_info
= {
185 .read_raw
= &tiadc_read_raw
,
186 .driver_module
= THIS_MODULE
,
189 static int tiadc_probe(struct platform_device
*pdev
)
191 struct iio_dev
*indio_dev
;
192 struct tiadc_device
*adc_dev
;
193 struct device_node
*node
= pdev
->dev
.of_node
;
194 struct property
*prop
;
201 dev_err(&pdev
->dev
, "Could not find valid DT data.\n");
205 indio_dev
= iio_device_alloc(sizeof(struct tiadc_device
));
206 if (indio_dev
== NULL
) {
207 dev_err(&pdev
->dev
, "failed to allocate iio device\n");
211 adc_dev
= iio_priv(indio_dev
);
213 adc_dev
->mfd_tscadc
= ti_tscadc_dev_get(pdev
);
215 of_property_for_each_u32(node
, "ti,adc-channels", prop
, cur
, val
) {
216 adc_dev
->channel_line
[channels
] = val
;
219 adc_dev
->channels
= channels
;
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
;
226 tiadc_step_config(adc_dev
);
228 err
= tiadc_channel_init(indio_dev
, adc_dev
->channels
);
230 goto err_free_device
;
232 err
= iio_device_register(indio_dev
);
234 goto err_free_channels
;
236 platform_set_drvdata(pdev
, indio_dev
);
241 tiadc_channels_remove(indio_dev
);
243 iio_device_free(indio_dev
);
248 static int tiadc_remove(struct platform_device
*pdev
)
250 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
251 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
254 iio_device_unregister(indio_dev
);
255 tiadc_channels_remove(indio_dev
);
257 step_en
= get_adc_step_mask(adc_dev
);
258 am335x_tsc_se_clr(adc_dev
->mfd_tscadc
, step_en
);
260 iio_device_free(indio_dev
);
266 static int tiadc_suspend(struct device
*dev
)
268 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
269 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
270 struct ti_tscadc_dev
*tscadc_dev
;
273 tscadc_dev
= ti_tscadc_dev_get(to_platform_device(dev
));
274 if (!device_may_wakeup(tscadc_dev
->dev
)) {
275 idle
= tiadc_readl(adc_dev
, REG_CTRL
);
276 idle
&= ~(CNTRLREG_TSCSSENB
);
277 tiadc_writel(adc_dev
, REG_CTRL
, (idle
|
278 CNTRLREG_POWERDOWN
));
284 static int tiadc_resume(struct device
*dev
)
286 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
287 struct tiadc_device
*adc_dev
= iio_priv(indio_dev
);
288 unsigned int restore
;
290 /* Make sure ADC is powered up */
291 restore
= tiadc_readl(adc_dev
, REG_CTRL
);
292 restore
&= ~(CNTRLREG_POWERDOWN
);
293 tiadc_writel(adc_dev
, REG_CTRL
, restore
);
295 tiadc_step_config(adc_dev
);
300 static const struct dev_pm_ops tiadc_pm_ops
= {
301 .suspend
= tiadc_suspend
,
302 .resume
= tiadc_resume
,
304 #define TIADC_PM_OPS (&tiadc_pm_ops)
306 #define TIADC_PM_OPS NULL
309 static const struct of_device_id ti_adc_dt_ids
[] = {
310 { .compatible
= "ti,am3359-adc", },
313 MODULE_DEVICE_TABLE(of
, ti_adc_dt_ids
);
315 static struct platform_driver tiadc_driver
= {
317 .name
= "TI-am335x-adc",
318 .owner
= THIS_MODULE
,
320 .of_match_table
= of_match_ptr(ti_adc_dt_ids
),
322 .probe
= tiadc_probe
,
323 .remove
= tiadc_remove
,
325 module_platform_driver(tiadc_driver
);
327 MODULE_DESCRIPTION("TI ADC controller driver");
328 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
329 MODULE_LICENSE("GPL");