2 * exynos_adc.c - Support for ADC in EXYNOS SoCs
4 * 8 ~ 10 channel, 10/12-bit ADC
6 * Copyright (C) 2013 Naveen Krishna Chatradhi <ch.naveen@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
30 #include <linux/clk.h>
31 #include <linux/completion.h>
33 #include <linux/of_irq.h>
34 #include <linux/regulator/consumer.h>
35 #include <linux/of_platform.h>
36 #include <linux/err.h>
38 #include <linux/iio/iio.h>
39 #include <linux/iio/machine.h>
40 #include <linux/iio/driver.h>
47 /* EXYNOS4412/5250 ADC_V1 registers definitions */
48 #define ADC_V1_CON(x) ((x) + 0x00)
49 #define ADC_V1_DLY(x) ((x) + 0x08)
50 #define ADC_V1_DATX(x) ((x) + 0x0C)
51 #define ADC_V1_INTCLR(x) ((x) + 0x18)
52 #define ADC_V1_MUX(x) ((x) + 0x1c)
54 /* Future ADC_V2 registers definitions */
55 #define ADC_V2_CON1(x) ((x) + 0x00)
56 #define ADC_V2_CON2(x) ((x) + 0x04)
57 #define ADC_V2_STAT(x) ((x) + 0x08)
58 #define ADC_V2_INT_EN(x) ((x) + 0x10)
59 #define ADC_V2_INT_ST(x) ((x) + 0x14)
60 #define ADC_V2_VER(x) ((x) + 0x20)
62 /* Bit definitions for ADC_V1 */
63 #define ADC_V1_CON_RES (1u << 16)
64 #define ADC_V1_CON_PRSCEN (1u << 14)
65 #define ADC_V1_CON_PRSCLV(x) (((x) & 0xFF) << 6)
66 #define ADC_V1_CON_STANDBY (1u << 2)
68 /* Bit definitions for ADC_V2 */
69 #define ADC_V2_CON1_SOFT_RESET (1u << 2)
71 #define ADC_V2_CON2_OSEL (1u << 10)
72 #define ADC_V2_CON2_ESEL (1u << 9)
73 #define ADC_V2_CON2_HIGHF (1u << 8)
74 #define ADC_V2_CON2_C_TIME(x) (((x) & 7) << 4)
75 #define ADC_V2_CON2_ACH_SEL(x) (((x) & 0xF) << 0)
76 #define ADC_V2_CON2_ACH_MASK 0xF
78 #define MAX_ADC_V2_CHANNELS 10
79 #define MAX_ADC_V1_CHANNELS 8
81 /* Bit definitions common for ADC_V1 and ADC_V2 */
82 #define ADC_CON_EN_START (1u << 0)
83 #define ADC_DATX_MASK 0xFFF
85 #define EXYNOS_ADC_TIMEOUT (msecs_to_jiffies(100))
89 void __iomem
*enable_reg
;
92 struct regulator
*vdd
;
94 struct completion completion
;
100 static const struct of_device_id exynos_adc_match
[] = {
101 { .compatible
= "samsung,exynos-adc-v1", .data
= (void *)ADC_V1
},
102 { .compatible
= "samsung,exynos-adc-v2", .data
= (void *)ADC_V2
},
105 MODULE_DEVICE_TABLE(of
, exynos_adc_match
);
107 static inline unsigned int exynos_adc_get_version(struct platform_device
*pdev
)
109 const struct of_device_id
*match
;
111 match
= of_match_node(exynos_adc_match
, pdev
->dev
.of_node
);
112 return (unsigned int)match
->data
;
115 static void exynos_adc_hw_init(struct exynos_adc
*info
)
119 if (info
->version
== ADC_V2
) {
120 con1
= ADC_V2_CON1_SOFT_RESET
;
121 writel(con1
, ADC_V2_CON1(info
->regs
));
123 con2
= ADC_V2_CON2_OSEL
| ADC_V2_CON2_ESEL
|
124 ADC_V2_CON2_HIGHF
| ADC_V2_CON2_C_TIME(0);
125 writel(con2
, ADC_V2_CON2(info
->regs
));
127 /* Enable interrupts */
128 writel(1, ADC_V2_INT_EN(info
->regs
));
130 /* set default prescaler values and Enable prescaler */
131 con1
= ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN
;
133 /* Enable 12-bit ADC resolution */
134 con1
|= ADC_V1_CON_RES
;
135 writel(con1
, ADC_V1_CON(info
->regs
));
139 static int exynos_read_raw(struct iio_dev
*indio_dev
,
140 struct iio_chan_spec
const *chan
,
145 struct exynos_adc
*info
= iio_priv(indio_dev
);
146 unsigned long timeout
;
150 if (mask
!= IIO_CHAN_INFO_RAW
)
153 mutex_lock(&indio_dev
->mlock
);
155 /* Select the channel to be used and Trigger conversion */
156 if (info
->version
== ADC_V2
) {
157 con2
= readl(ADC_V2_CON2(info
->regs
));
158 con2
&= ~ADC_V2_CON2_ACH_MASK
;
159 con2
|= ADC_V2_CON2_ACH_SEL(chan
->address
);
160 writel(con2
, ADC_V2_CON2(info
->regs
));
162 con1
= readl(ADC_V2_CON1(info
->regs
));
163 writel(con1
| ADC_CON_EN_START
,
164 ADC_V2_CON1(info
->regs
));
166 writel(chan
->address
, ADC_V1_MUX(info
->regs
));
168 con1
= readl(ADC_V1_CON(info
->regs
));
169 writel(con1
| ADC_CON_EN_START
,
170 ADC_V1_CON(info
->regs
));
173 timeout
= wait_for_completion_timeout
174 (&info
->completion
, EXYNOS_ADC_TIMEOUT
);
176 dev_warn(&indio_dev
->dev
, "Conversion timed out! Resetting\n");
177 exynos_adc_hw_init(info
);
185 mutex_unlock(&indio_dev
->mlock
);
190 static irqreturn_t
exynos_adc_isr(int irq
, void *dev_id
)
192 struct exynos_adc
*info
= (struct exynos_adc
*)dev_id
;
195 info
->value
= readl(ADC_V1_DATX(info
->regs
)) &
198 if (info
->version
== ADC_V2
)
199 writel(1, ADC_V2_INT_ST(info
->regs
));
201 writel(1, ADC_V1_INTCLR(info
->regs
));
203 complete(&info
->completion
);
208 static int exynos_adc_reg_access(struct iio_dev
*indio_dev
,
209 unsigned reg
, unsigned writeval
,
212 struct exynos_adc
*info
= iio_priv(indio_dev
);
217 *readval
= readl(info
->regs
+ reg
);
222 static const struct iio_info exynos_adc_iio_info
= {
223 .read_raw
= &exynos_read_raw
,
224 .debugfs_reg_access
= &exynos_adc_reg_access
,
225 .driver_module
= THIS_MODULE
,
228 #define ADC_CHANNEL(_index, _id) { \
229 .type = IIO_VOLTAGE, \
233 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
234 .datasheet_name = _id, \
237 static const struct iio_chan_spec exynos_adc_iio_channels
[] = {
238 ADC_CHANNEL(0, "adc0"),
239 ADC_CHANNEL(1, "adc1"),
240 ADC_CHANNEL(2, "adc2"),
241 ADC_CHANNEL(3, "adc3"),
242 ADC_CHANNEL(4, "adc4"),
243 ADC_CHANNEL(5, "adc5"),
244 ADC_CHANNEL(6, "adc6"),
245 ADC_CHANNEL(7, "adc7"),
246 ADC_CHANNEL(8, "adc8"),
247 ADC_CHANNEL(9, "adc9"),
250 static int exynos_adc_remove_devices(struct device
*dev
, void *c
)
252 struct platform_device
*pdev
= to_platform_device(dev
);
254 platform_device_unregister(pdev
);
259 static int exynos_adc_probe(struct platform_device
*pdev
)
261 struct exynos_adc
*info
= NULL
;
262 struct device_node
*np
= pdev
->dev
.of_node
;
263 struct iio_dev
*indio_dev
= NULL
;
264 struct resource
*mem
;
271 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(struct exynos_adc
));
273 dev_err(&pdev
->dev
, "failed allocating iio device\n");
277 info
= iio_priv(indio_dev
);
279 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
280 info
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
281 if (IS_ERR(info
->regs
))
282 return PTR_ERR(info
->regs
);
284 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
285 info
->enable_reg
= devm_ioremap_resource(&pdev
->dev
, mem
);
286 if (IS_ERR(info
->enable_reg
))
287 return PTR_ERR(info
->enable_reg
);
289 irq
= platform_get_irq(pdev
, 0);
291 dev_err(&pdev
->dev
, "no irq resource?\n");
297 init_completion(&info
->completion
);
299 info
->clk
= devm_clk_get(&pdev
->dev
, "adc");
300 if (IS_ERR(info
->clk
)) {
301 dev_err(&pdev
->dev
, "failed getting clock, err = %ld\n",
303 return PTR_ERR(info
->clk
);
306 info
->vdd
= devm_regulator_get(&pdev
->dev
, "vdd");
307 if (IS_ERR(info
->vdd
)) {
308 dev_err(&pdev
->dev
, "failed getting regulator, err = %ld\n",
310 return PTR_ERR(info
->vdd
);
313 ret
= regulator_enable(info
->vdd
);
317 ret
= clk_prepare_enable(info
->clk
);
319 goto err_disable_reg
;
321 writel(1, info
->enable_reg
);
323 info
->version
= exynos_adc_get_version(pdev
);
325 platform_set_drvdata(pdev
, indio_dev
);
327 indio_dev
->name
= dev_name(&pdev
->dev
);
328 indio_dev
->dev
.parent
= &pdev
->dev
;
329 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
330 indio_dev
->info
= &exynos_adc_iio_info
;
331 indio_dev
->modes
= INDIO_DIRECT_MODE
;
332 indio_dev
->channels
= exynos_adc_iio_channels
;
334 if (info
->version
== ADC_V1
)
335 indio_dev
->num_channels
= MAX_ADC_V1_CHANNELS
;
337 indio_dev
->num_channels
= MAX_ADC_V2_CHANNELS
;
339 ret
= request_irq(info
->irq
, exynos_adc_isr
,
340 0, dev_name(&pdev
->dev
), info
);
342 dev_err(&pdev
->dev
, "failed requesting irq, irq = %d\n",
344 goto err_disable_clk
;
347 ret
= iio_device_register(indio_dev
);
351 exynos_adc_hw_init(info
);
353 ret
= of_platform_populate(np
, exynos_adc_match
, NULL
, &pdev
->dev
);
355 dev_err(&pdev
->dev
, "failed adding child nodes\n");
356 goto err_of_populate
;
362 device_for_each_child(&pdev
->dev
, NULL
,
363 exynos_adc_remove_devices
);
364 iio_device_unregister(indio_dev
);
366 free_irq(info
->irq
, info
);
368 writel(0, info
->enable_reg
);
369 clk_disable_unprepare(info
->clk
);
371 regulator_disable(info
->vdd
);
375 static int exynos_adc_remove(struct platform_device
*pdev
)
377 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
378 struct exynos_adc
*info
= iio_priv(indio_dev
);
380 device_for_each_child(&pdev
->dev
, NULL
,
381 exynos_adc_remove_devices
);
382 iio_device_unregister(indio_dev
);
383 free_irq(info
->irq
, info
);
384 writel(0, info
->enable_reg
);
385 clk_disable_unprepare(info
->clk
);
386 regulator_disable(info
->vdd
);
391 #ifdef CONFIG_PM_SLEEP
392 static int exynos_adc_suspend(struct device
*dev
)
394 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
395 struct exynos_adc
*info
= iio_priv(indio_dev
);
398 if (info
->version
== ADC_V2
) {
399 con
= readl(ADC_V2_CON1(info
->regs
));
400 con
&= ~ADC_CON_EN_START
;
401 writel(con
, ADC_V2_CON1(info
->regs
));
403 con
= readl(ADC_V1_CON(info
->regs
));
404 con
|= ADC_V1_CON_STANDBY
;
405 writel(con
, ADC_V1_CON(info
->regs
));
408 writel(0, info
->enable_reg
);
409 clk_disable_unprepare(info
->clk
);
410 regulator_disable(info
->vdd
);
415 static int exynos_adc_resume(struct device
*dev
)
417 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
418 struct exynos_adc
*info
= iio_priv(indio_dev
);
421 ret
= regulator_enable(info
->vdd
);
425 ret
= clk_prepare_enable(info
->clk
);
429 writel(1, info
->enable_reg
);
430 exynos_adc_hw_init(info
);
436 static SIMPLE_DEV_PM_OPS(exynos_adc_pm_ops
,
440 static struct platform_driver exynos_adc_driver
= {
441 .probe
= exynos_adc_probe
,
442 .remove
= exynos_adc_remove
,
444 .name
= "exynos-adc",
445 .owner
= THIS_MODULE
,
446 .of_match_table
= exynos_adc_match
,
447 .pm
= &exynos_adc_pm_ops
,
451 module_platform_driver(exynos_adc_driver
);
453 MODULE_AUTHOR("Naveen Krishna Chatradhi <ch.naveen@samsung.com>");
454 MODULE_DESCRIPTION("Samsung EXYNOS5 ADC driver");
455 MODULE_LICENSE("GPL v2");