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/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
31 #include <linux/clk.h>
32 #include <linux/completion.h>
34 #include <linux/of_irq.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/of_platform.h>
37 #include <linux/err.h>
39 #include <linux/iio/iio.h>
40 #include <linux/iio/machine.h>
41 #include <linux/iio/driver.h>
43 /* S3C/EXYNOS4412/5250 ADC_V1 registers definitions */
44 #define ADC_V1_CON(x) ((x) + 0x00)
45 #define ADC_V1_DLY(x) ((x) + 0x08)
46 #define ADC_V1_DATX(x) ((x) + 0x0C)
47 #define ADC_V1_INTCLR(x) ((x) + 0x18)
48 #define ADC_V1_MUX(x) ((x) + 0x1c)
50 /* S3C2410 ADC registers definitions */
51 #define ADC_S3C2410_MUX(x) ((x) + 0x18)
53 /* Future ADC_V2 registers definitions */
54 #define ADC_V2_CON1(x) ((x) + 0x00)
55 #define ADC_V2_CON2(x) ((x) + 0x04)
56 #define ADC_V2_STAT(x) ((x) + 0x08)
57 #define ADC_V2_INT_EN(x) ((x) + 0x10)
58 #define ADC_V2_INT_ST(x) ((x) + 0x14)
59 #define ADC_V2_VER(x) ((x) + 0x20)
61 /* Bit definitions for ADC_V1 */
62 #define ADC_V1_CON_RES (1u << 16)
63 #define ADC_V1_CON_PRSCEN (1u << 14)
64 #define ADC_V1_CON_PRSCLV(x) (((x) & 0xFF) << 6)
65 #define ADC_V1_CON_STANDBY (1u << 2)
67 /* Bit definitions for S3C2410 ADC */
68 #define ADC_S3C2410_CON_SELMUX(x) (((x) & 7) << 3)
69 #define ADC_S3C2410_DATX_MASK 0x3FF
70 #define ADC_S3C2416_CON_RES_SEL (1u << 3)
72 /* Bit definitions for ADC_V2 */
73 #define ADC_V2_CON1_SOFT_RESET (1u << 2)
75 #define ADC_V2_CON2_OSEL (1u << 10)
76 #define ADC_V2_CON2_ESEL (1u << 9)
77 #define ADC_V2_CON2_HIGHF (1u << 8)
78 #define ADC_V2_CON2_C_TIME(x) (((x) & 7) << 4)
79 #define ADC_V2_CON2_ACH_SEL(x) (((x) & 0xF) << 0)
80 #define ADC_V2_CON2_ACH_MASK 0xF
82 #define MAX_ADC_V2_CHANNELS 10
83 #define MAX_ADC_V1_CHANNELS 8
84 #define MAX_EXYNOS3250_ADC_CHANNELS 2
86 /* Bit definitions common for ADC_V1 and ADC_V2 */
87 #define ADC_CON_EN_START (1u << 0)
88 #define ADC_CON_EN_START_MASK (0x3 << 0)
89 #define ADC_DATX_MASK 0xFFF
91 #define EXYNOS_ADC_TIMEOUT (msecs_to_jiffies(100))
94 struct exynos_adc_data *data;
97 void __iomem *enable_reg;
101 struct regulator *vdd;
103 struct completion completion;
106 unsigned int version;
109 struct exynos_adc_data {
115 void (*init_hw)(struct exynos_adc *info);
116 void (*exit_hw)(struct exynos_adc *info);
117 void (*clear_irq)(struct exynos_adc *info);
118 void (*start_conv)(struct exynos_adc *info, unsigned long addr);
121 static void exynos_adc_unprepare_clk(struct exynos_adc *info)
123 if (info->data->needs_sclk)
124 clk_unprepare(info->sclk);
125 clk_unprepare(info->clk);
128 static int exynos_adc_prepare_clk(struct exynos_adc *info)
132 ret = clk_prepare(info->clk);
134 dev_err(info->dev, "failed preparing adc clock: %d\n", ret);
138 if (info->data->needs_sclk) {
139 ret = clk_prepare(info->sclk);
141 clk_unprepare(info->clk);
143 "failed preparing sclk_adc clock: %d\n", ret);
151 static void exynos_adc_disable_clk(struct exynos_adc *info)
153 if (info->data->needs_sclk)
154 clk_disable(info->sclk);
155 clk_disable(info->clk);
158 static int exynos_adc_enable_clk(struct exynos_adc *info)
162 ret = clk_enable(info->clk);
164 dev_err(info->dev, "failed enabling adc clock: %d\n", ret);
168 if (info->data->needs_sclk) {
169 ret = clk_enable(info->sclk);
171 clk_disable(info->clk);
173 "failed enabling sclk_adc clock: %d\n", ret);
181 static void exynos_adc_v1_init_hw(struct exynos_adc *info)
185 if (info->data->needs_adc_phy)
186 writel(1, info->enable_reg);
188 /* set default prescaler values and Enable prescaler */
189 con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN;
191 /* Enable 12-bit ADC resolution */
192 con1 |= ADC_V1_CON_RES;
193 writel(con1, ADC_V1_CON(info->regs));
196 static void exynos_adc_v1_exit_hw(struct exynos_adc *info)
200 if (info->data->needs_adc_phy)
201 writel(0, info->enable_reg);
203 con = readl(ADC_V1_CON(info->regs));
204 con |= ADC_V1_CON_STANDBY;
205 writel(con, ADC_V1_CON(info->regs));
208 static void exynos_adc_v1_clear_irq(struct exynos_adc *info)
210 writel(1, ADC_V1_INTCLR(info->regs));
213 static void exynos_adc_v1_start_conv(struct exynos_adc *info,
218 writel(addr, ADC_V1_MUX(info->regs));
220 con1 = readl(ADC_V1_CON(info->regs));
221 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
224 static const struct exynos_adc_data exynos_adc_v1_data = {
225 .num_channels = MAX_ADC_V1_CHANNELS,
226 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
227 .needs_adc_phy = true,
229 .init_hw = exynos_adc_v1_init_hw,
230 .exit_hw = exynos_adc_v1_exit_hw,
231 .clear_irq = exynos_adc_v1_clear_irq,
232 .start_conv = exynos_adc_v1_start_conv,
235 static void exynos_adc_s3c2416_start_conv(struct exynos_adc *info,
240 /* Enable 12 bit ADC resolution */
241 con1 = readl(ADC_V1_CON(info->regs));
242 con1 |= ADC_S3C2416_CON_RES_SEL;
243 writel(con1, ADC_V1_CON(info->regs));
245 /* Select channel for S3C2416 */
246 writel(addr, ADC_S3C2410_MUX(info->regs));
248 con1 = readl(ADC_V1_CON(info->regs));
249 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
252 static struct exynos_adc_data const exynos_adc_s3c2416_data = {
253 .num_channels = MAX_ADC_V1_CHANNELS,
254 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
256 .init_hw = exynos_adc_v1_init_hw,
257 .exit_hw = exynos_adc_v1_exit_hw,
258 .start_conv = exynos_adc_s3c2416_start_conv,
261 static void exynos_adc_s3c2443_start_conv(struct exynos_adc *info,
266 /* Select channel for S3C2433 */
267 writel(addr, ADC_S3C2410_MUX(info->regs));
269 con1 = readl(ADC_V1_CON(info->regs));
270 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
273 static struct exynos_adc_data const exynos_adc_s3c2443_data = {
274 .num_channels = MAX_ADC_V1_CHANNELS,
275 .mask = ADC_S3C2410_DATX_MASK, /* 10 bit ADC resolution */
277 .init_hw = exynos_adc_v1_init_hw,
278 .exit_hw = exynos_adc_v1_exit_hw,
279 .start_conv = exynos_adc_s3c2443_start_conv,
282 static void exynos_adc_s3c64xx_start_conv(struct exynos_adc *info,
287 con1 = readl(ADC_V1_CON(info->regs));
288 con1 &= ~ADC_S3C2410_CON_SELMUX(0x7);
289 con1 |= ADC_S3C2410_CON_SELMUX(addr);
290 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
293 static struct exynos_adc_data const exynos_adc_s3c24xx_data = {
294 .num_channels = MAX_ADC_V1_CHANNELS,
295 .mask = ADC_S3C2410_DATX_MASK, /* 10 bit ADC resolution */
297 .init_hw = exynos_adc_v1_init_hw,
298 .exit_hw = exynos_adc_v1_exit_hw,
299 .start_conv = exynos_adc_s3c64xx_start_conv,
302 static struct exynos_adc_data const exynos_adc_s3c64xx_data = {
303 .num_channels = MAX_ADC_V1_CHANNELS,
304 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
306 .init_hw = exynos_adc_v1_init_hw,
307 .exit_hw = exynos_adc_v1_exit_hw,
308 .clear_irq = exynos_adc_v1_clear_irq,
309 .start_conv = exynos_adc_s3c64xx_start_conv,
312 static void exynos_adc_v2_init_hw(struct exynos_adc *info)
316 if (info->data->needs_adc_phy)
317 writel(1, info->enable_reg);
319 con1 = ADC_V2_CON1_SOFT_RESET;
320 writel(con1, ADC_V2_CON1(info->regs));
322 con2 = ADC_V2_CON2_OSEL | ADC_V2_CON2_ESEL |
323 ADC_V2_CON2_HIGHF | ADC_V2_CON2_C_TIME(0);
324 writel(con2, ADC_V2_CON2(info->regs));
326 /* Enable interrupts */
327 writel(1, ADC_V2_INT_EN(info->regs));
330 static void exynos_adc_v2_exit_hw(struct exynos_adc *info)
334 if (info->data->needs_adc_phy)
335 writel(0, info->enable_reg);
337 con = readl(ADC_V2_CON1(info->regs));
338 con &= ~ADC_CON_EN_START;
339 writel(con, ADC_V2_CON1(info->regs));
342 static void exynos_adc_v2_clear_irq(struct exynos_adc *info)
344 writel(1, ADC_V2_INT_ST(info->regs));
347 static void exynos_adc_v2_start_conv(struct exynos_adc *info,
352 con2 = readl(ADC_V2_CON2(info->regs));
353 con2 &= ~ADC_V2_CON2_ACH_MASK;
354 con2 |= ADC_V2_CON2_ACH_SEL(addr);
355 writel(con2, ADC_V2_CON2(info->regs));
357 con1 = readl(ADC_V2_CON1(info->regs));
358 writel(con1 | ADC_CON_EN_START, ADC_V2_CON1(info->regs));
361 static const struct exynos_adc_data exynos_adc_v2_data = {
362 .num_channels = MAX_ADC_V2_CHANNELS,
363 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
364 .needs_adc_phy = true,
366 .init_hw = exynos_adc_v2_init_hw,
367 .exit_hw = exynos_adc_v2_exit_hw,
368 .clear_irq = exynos_adc_v2_clear_irq,
369 .start_conv = exynos_adc_v2_start_conv,
372 static const struct exynos_adc_data exynos3250_adc_data = {
373 .num_channels = MAX_EXYNOS3250_ADC_CHANNELS,
374 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
376 .needs_adc_phy = true,
378 .init_hw = exynos_adc_v2_init_hw,
379 .exit_hw = exynos_adc_v2_exit_hw,
380 .clear_irq = exynos_adc_v2_clear_irq,
381 .start_conv = exynos_adc_v2_start_conv,
384 static const struct of_device_id exynos_adc_match[] = {
386 .compatible = "samsung,s3c2410-adc",
387 .data = &exynos_adc_s3c24xx_data,
389 .compatible = "samsung,s3c2416-adc",
390 .data = &exynos_adc_s3c2416_data,
392 .compatible = "samsung,s3c2440-adc",
393 .data = &exynos_adc_s3c24xx_data,
395 .compatible = "samsung,s3c2443-adc",
396 .data = &exynos_adc_s3c2443_data,
398 .compatible = "samsung,s3c6410-adc",
399 .data = &exynos_adc_s3c64xx_data,
401 .compatible = "samsung,exynos-adc-v1",
402 .data = &exynos_adc_v1_data,
404 .compatible = "samsung,exynos-adc-v2",
405 .data = &exynos_adc_v2_data,
407 .compatible = "samsung,exynos3250-adc",
408 .data = &exynos3250_adc_data,
412 MODULE_DEVICE_TABLE(of, exynos_adc_match);
414 static struct exynos_adc_data *exynos_adc_get_data(struct platform_device *pdev)
416 const struct of_device_id *match;
418 match = of_match_node(exynos_adc_match, pdev->dev.of_node);
419 return (struct exynos_adc_data *)match->data;
422 static int exynos_read_raw(struct iio_dev *indio_dev,
423 struct iio_chan_spec const *chan,
428 struct exynos_adc *info = iio_priv(indio_dev);
429 unsigned long timeout;
432 if (mask != IIO_CHAN_INFO_RAW)
435 mutex_lock(&indio_dev->mlock);
436 reinit_completion(&info->completion);
438 /* Select the channel to be used and Trigger conversion */
439 if (info->data->start_conv)
440 info->data->start_conv(info, chan->address);
442 timeout = wait_for_completion_timeout
443 (&info->completion, EXYNOS_ADC_TIMEOUT);
445 dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n");
446 if (info->data->init_hw)
447 info->data->init_hw(info);
455 mutex_unlock(&indio_dev->mlock);
460 static irqreturn_t exynos_adc_isr(int irq, void *dev_id)
462 struct exynos_adc *info = (struct exynos_adc *)dev_id;
463 u32 mask = info->data->mask;
466 info->value = readl(ADC_V1_DATX(info->regs)) & mask;
469 if (info->data->clear_irq)
470 info->data->clear_irq(info);
472 complete(&info->completion);
477 static int exynos_adc_reg_access(struct iio_dev *indio_dev,
478 unsigned reg, unsigned writeval,
481 struct exynos_adc *info = iio_priv(indio_dev);
486 *readval = readl(info->regs + reg);
491 static const struct iio_info exynos_adc_iio_info = {
492 .read_raw = &exynos_read_raw,
493 .debugfs_reg_access = &exynos_adc_reg_access,
494 .driver_module = THIS_MODULE,
497 #define ADC_CHANNEL(_index, _id) { \
498 .type = IIO_VOLTAGE, \
502 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
503 .datasheet_name = _id, \
506 static const struct iio_chan_spec exynos_adc_iio_channels[] = {
507 ADC_CHANNEL(0, "adc0"),
508 ADC_CHANNEL(1, "adc1"),
509 ADC_CHANNEL(2, "adc2"),
510 ADC_CHANNEL(3, "adc3"),
511 ADC_CHANNEL(4, "adc4"),
512 ADC_CHANNEL(5, "adc5"),
513 ADC_CHANNEL(6, "adc6"),
514 ADC_CHANNEL(7, "adc7"),
515 ADC_CHANNEL(8, "adc8"),
516 ADC_CHANNEL(9, "adc9"),
519 static int exynos_adc_remove_devices(struct device *dev, void *c)
521 struct platform_device *pdev = to_platform_device(dev);
523 platform_device_unregister(pdev);
528 static int exynos_adc_probe(struct platform_device *pdev)
530 struct exynos_adc *info = NULL;
531 struct device_node *np = pdev->dev.of_node;
532 struct iio_dev *indio_dev = NULL;
533 struct resource *mem;
540 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct exynos_adc));
542 dev_err(&pdev->dev, "failed allocating iio device\n");
546 info = iio_priv(indio_dev);
548 info->data = exynos_adc_get_data(pdev);
550 dev_err(&pdev->dev, "failed getting exynos_adc_data\n");
554 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
555 info->regs = devm_ioremap_resource(&pdev->dev, mem);
556 if (IS_ERR(info->regs))
557 return PTR_ERR(info->regs);
560 if (info->data->needs_adc_phy) {
561 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
562 info->enable_reg = devm_ioremap_resource(&pdev->dev, mem);
563 if (IS_ERR(info->enable_reg))
564 return PTR_ERR(info->enable_reg);
567 irq = platform_get_irq(pdev, 0);
569 dev_err(&pdev->dev, "no irq resource?\n");
574 info->dev = &pdev->dev;
576 init_completion(&info->completion);
578 info->clk = devm_clk_get(&pdev->dev, "adc");
579 if (IS_ERR(info->clk)) {
580 dev_err(&pdev->dev, "failed getting clock, err = %ld\n",
582 return PTR_ERR(info->clk);
585 if (info->data->needs_sclk) {
586 info->sclk = devm_clk_get(&pdev->dev, "sclk");
587 if (IS_ERR(info->sclk)) {
589 "failed getting sclk clock, err = %ld\n",
590 PTR_ERR(info->sclk));
591 return PTR_ERR(info->sclk);
595 info->vdd = devm_regulator_get(&pdev->dev, "vdd");
596 if (IS_ERR(info->vdd)) {
597 dev_err(&pdev->dev, "failed getting regulator, err = %ld\n",
599 return PTR_ERR(info->vdd);
602 ret = regulator_enable(info->vdd);
606 ret = exynos_adc_prepare_clk(info);
608 goto err_disable_reg;
610 ret = exynos_adc_enable_clk(info);
612 goto err_unprepare_clk;
614 platform_set_drvdata(pdev, indio_dev);
616 indio_dev->name = dev_name(&pdev->dev);
617 indio_dev->dev.parent = &pdev->dev;
618 indio_dev->dev.of_node = pdev->dev.of_node;
619 indio_dev->info = &exynos_adc_iio_info;
620 indio_dev->modes = INDIO_DIRECT_MODE;
621 indio_dev->channels = exynos_adc_iio_channels;
622 indio_dev->num_channels = info->data->num_channels;
624 ret = request_irq(info->irq, exynos_adc_isr,
625 0, dev_name(&pdev->dev), info);
627 dev_err(&pdev->dev, "failed requesting irq, irq = %d\n",
629 goto err_disable_clk;
632 ret = iio_device_register(indio_dev);
636 if (info->data->init_hw)
637 info->data->init_hw(info);
639 ret = of_platform_populate(np, exynos_adc_match, NULL, &indio_dev->dev);
641 dev_err(&pdev->dev, "failed adding child nodes\n");
642 goto err_of_populate;
648 device_for_each_child(&indio_dev->dev, NULL,
649 exynos_adc_remove_devices);
650 iio_device_unregister(indio_dev);
652 free_irq(info->irq, info);
654 if (info->data->exit_hw)
655 info->data->exit_hw(info);
656 exynos_adc_disable_clk(info);
658 exynos_adc_unprepare_clk(info);
660 regulator_disable(info->vdd);
664 static int exynos_adc_remove(struct platform_device *pdev)
666 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
667 struct exynos_adc *info = iio_priv(indio_dev);
669 device_for_each_child(&indio_dev->dev, NULL,
670 exynos_adc_remove_devices);
671 iio_device_unregister(indio_dev);
672 free_irq(info->irq, info);
673 if (info->data->exit_hw)
674 info->data->exit_hw(info);
675 exynos_adc_disable_clk(info);
676 exynos_adc_unprepare_clk(info);
677 regulator_disable(info->vdd);
682 #ifdef CONFIG_PM_SLEEP
683 static int exynos_adc_suspend(struct device *dev)
685 struct iio_dev *indio_dev = dev_get_drvdata(dev);
686 struct exynos_adc *info = iio_priv(indio_dev);
688 if (info->data->exit_hw)
689 info->data->exit_hw(info);
690 exynos_adc_disable_clk(info);
691 regulator_disable(info->vdd);
696 static int exynos_adc_resume(struct device *dev)
698 struct iio_dev *indio_dev = dev_get_drvdata(dev);
699 struct exynos_adc *info = iio_priv(indio_dev);
702 ret = regulator_enable(info->vdd);
706 ret = exynos_adc_enable_clk(info);
710 if (info->data->init_hw)
711 info->data->init_hw(info);
717 static SIMPLE_DEV_PM_OPS(exynos_adc_pm_ops,
721 static struct platform_driver exynos_adc_driver = {
722 .probe = exynos_adc_probe,
723 .remove = exynos_adc_remove,
725 .name = "exynos-adc",
726 .owner = THIS_MODULE,
727 .of_match_table = exynos_adc_match,
728 .pm = &exynos_adc_pm_ops,
732 module_platform_driver(exynos_adc_driver);
734 MODULE_AUTHOR("Naveen Krishna Chatradhi <ch.naveen@samsung.com>");
735 MODULE_DESCRIPTION("Samsung EXYNOS5 ADC driver");
736 MODULE_LICENSE("GPL v2");