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>
42 #include <linux/mfd/syscon.h>
43 #include <linux/regmap.h>
45 /* S3C/EXYNOS4412/5250 ADC_V1 registers definitions */
46 #define ADC_V1_CON(x) ((x) + 0x00)
47 #define ADC_V1_DLY(x) ((x) + 0x08)
48 #define ADC_V1_DATX(x) ((x) + 0x0C)
49 #define ADC_V1_INTCLR(x) ((x) + 0x18)
50 #define ADC_V1_MUX(x) ((x) + 0x1c)
52 /* S3C2410 ADC registers definitions */
53 #define ADC_S3C2410_MUX(x) ((x) + 0x18)
55 /* Future ADC_V2 registers definitions */
56 #define ADC_V2_CON1(x) ((x) + 0x00)
57 #define ADC_V2_CON2(x) ((x) + 0x04)
58 #define ADC_V2_STAT(x) ((x) + 0x08)
59 #define ADC_V2_INT_EN(x) ((x) + 0x10)
60 #define ADC_V2_INT_ST(x) ((x) + 0x14)
61 #define ADC_V2_VER(x) ((x) + 0x20)
63 /* Bit definitions for ADC_V1 */
64 #define ADC_V1_CON_RES (1u << 16)
65 #define ADC_V1_CON_PRSCEN (1u << 14)
66 #define ADC_V1_CON_PRSCLV(x) (((x) & 0xFF) << 6)
67 #define ADC_V1_CON_STANDBY (1u << 2)
69 /* Bit definitions for S3C2410 ADC */
70 #define ADC_S3C2410_CON_SELMUX(x) (((x) & 7) << 3)
71 #define ADC_S3C2410_DATX_MASK 0x3FF
72 #define ADC_S3C2416_CON_RES_SEL (1u << 3)
74 /* Bit definitions for ADC_V2 */
75 #define ADC_V2_CON1_SOFT_RESET (1u << 2)
77 #define ADC_V2_CON2_OSEL (1u << 10)
78 #define ADC_V2_CON2_ESEL (1u << 9)
79 #define ADC_V2_CON2_HIGHF (1u << 8)
80 #define ADC_V2_CON2_C_TIME(x) (((x) & 7) << 4)
81 #define ADC_V2_CON2_ACH_SEL(x) (((x) & 0xF) << 0)
82 #define ADC_V2_CON2_ACH_MASK 0xF
84 #define MAX_ADC_V2_CHANNELS 10
85 #define MAX_ADC_V1_CHANNELS 8
86 #define MAX_EXYNOS3250_ADC_CHANNELS 2
88 /* Bit definitions common for ADC_V1 and ADC_V2 */
89 #define ADC_CON_EN_START (1u << 0)
90 #define ADC_CON_EN_START_MASK (0x3 << 0)
91 #define ADC_DATX_MASK 0xFFF
93 #define EXYNOS_ADC_TIMEOUT (msecs_to_jiffies(100))
95 #define EXYNOS_ADCV1_PHY_OFFSET 0x0718
96 #define EXYNOS_ADCV2_PHY_OFFSET 0x0720
99 struct exynos_adc_data *data;
102 struct regmap *pmu_map;
106 struct regulator *vdd;
108 struct completion completion;
111 unsigned int version;
114 struct exynos_adc_data {
121 void (*init_hw)(struct exynos_adc *info);
122 void (*exit_hw)(struct exynos_adc *info);
123 void (*clear_irq)(struct exynos_adc *info);
124 void (*start_conv)(struct exynos_adc *info, unsigned long addr);
127 static void exynos_adc_unprepare_clk(struct exynos_adc *info)
129 if (info->data->needs_sclk)
130 clk_unprepare(info->sclk);
131 clk_unprepare(info->clk);
134 static int exynos_adc_prepare_clk(struct exynos_adc *info)
138 ret = clk_prepare(info->clk);
140 dev_err(info->dev, "failed preparing adc clock: %d\n", ret);
144 if (info->data->needs_sclk) {
145 ret = clk_prepare(info->sclk);
147 clk_unprepare(info->clk);
149 "failed preparing sclk_adc clock: %d\n", ret);
157 static void exynos_adc_disable_clk(struct exynos_adc *info)
159 if (info->data->needs_sclk)
160 clk_disable(info->sclk);
161 clk_disable(info->clk);
164 static int exynos_adc_enable_clk(struct exynos_adc *info)
168 ret = clk_enable(info->clk);
170 dev_err(info->dev, "failed enabling adc clock: %d\n", ret);
174 if (info->data->needs_sclk) {
175 ret = clk_enable(info->sclk);
177 clk_disable(info->clk);
179 "failed enabling sclk_adc clock: %d\n", ret);
187 static void exynos_adc_v1_init_hw(struct exynos_adc *info)
191 if (info->data->needs_adc_phy)
192 regmap_write(info->pmu_map, info->data->phy_offset, 1);
194 /* set default prescaler values and Enable prescaler */
195 con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN;
197 /* Enable 12-bit ADC resolution */
198 con1 |= ADC_V1_CON_RES;
199 writel(con1, ADC_V1_CON(info->regs));
202 static void exynos_adc_v1_exit_hw(struct exynos_adc *info)
206 if (info->data->needs_adc_phy)
207 regmap_write(info->pmu_map, info->data->phy_offset, 0);
209 con = readl(ADC_V1_CON(info->regs));
210 con |= ADC_V1_CON_STANDBY;
211 writel(con, ADC_V1_CON(info->regs));
214 static void exynos_adc_v1_clear_irq(struct exynos_adc *info)
216 writel(1, ADC_V1_INTCLR(info->regs));
219 static void exynos_adc_v1_start_conv(struct exynos_adc *info,
224 writel(addr, ADC_V1_MUX(info->regs));
226 con1 = readl(ADC_V1_CON(info->regs));
227 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
230 static const struct exynos_adc_data exynos_adc_v1_data = {
231 .num_channels = MAX_ADC_V1_CHANNELS,
232 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
233 .needs_adc_phy = true,
234 .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
236 .init_hw = exynos_adc_v1_init_hw,
237 .exit_hw = exynos_adc_v1_exit_hw,
238 .clear_irq = exynos_adc_v1_clear_irq,
239 .start_conv = exynos_adc_v1_start_conv,
242 static void exynos_adc_s3c2416_start_conv(struct exynos_adc *info,
247 /* Enable 12 bit ADC resolution */
248 con1 = readl(ADC_V1_CON(info->regs));
249 con1 |= ADC_S3C2416_CON_RES_SEL;
250 writel(con1, ADC_V1_CON(info->regs));
252 /* Select channel for S3C2416 */
253 writel(addr, ADC_S3C2410_MUX(info->regs));
255 con1 = readl(ADC_V1_CON(info->regs));
256 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
259 static struct exynos_adc_data const exynos_adc_s3c2416_data = {
260 .num_channels = MAX_ADC_V1_CHANNELS,
261 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
263 .init_hw = exynos_adc_v1_init_hw,
264 .exit_hw = exynos_adc_v1_exit_hw,
265 .start_conv = exynos_adc_s3c2416_start_conv,
268 static void exynos_adc_s3c2443_start_conv(struct exynos_adc *info,
273 /* Select channel for S3C2433 */
274 writel(addr, ADC_S3C2410_MUX(info->regs));
276 con1 = readl(ADC_V1_CON(info->regs));
277 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
280 static struct exynos_adc_data const exynos_adc_s3c2443_data = {
281 .num_channels = MAX_ADC_V1_CHANNELS,
282 .mask = ADC_S3C2410_DATX_MASK, /* 10 bit ADC resolution */
284 .init_hw = exynos_adc_v1_init_hw,
285 .exit_hw = exynos_adc_v1_exit_hw,
286 .start_conv = exynos_adc_s3c2443_start_conv,
289 static void exynos_adc_s3c64xx_start_conv(struct exynos_adc *info,
294 con1 = readl(ADC_V1_CON(info->regs));
295 con1 &= ~ADC_S3C2410_CON_SELMUX(0x7);
296 con1 |= ADC_S3C2410_CON_SELMUX(addr);
297 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
300 static struct exynos_adc_data const exynos_adc_s3c24xx_data = {
301 .num_channels = MAX_ADC_V1_CHANNELS,
302 .mask = ADC_S3C2410_DATX_MASK, /* 10 bit ADC resolution */
304 .init_hw = exynos_adc_v1_init_hw,
305 .exit_hw = exynos_adc_v1_exit_hw,
306 .start_conv = exynos_adc_s3c64xx_start_conv,
309 static struct exynos_adc_data const exynos_adc_s3c64xx_data = {
310 .num_channels = MAX_ADC_V1_CHANNELS,
311 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
313 .init_hw = exynos_adc_v1_init_hw,
314 .exit_hw = exynos_adc_v1_exit_hw,
315 .clear_irq = exynos_adc_v1_clear_irq,
316 .start_conv = exynos_adc_s3c64xx_start_conv,
319 static void exynos_adc_v2_init_hw(struct exynos_adc *info)
323 if (info->data->needs_adc_phy)
324 regmap_write(info->pmu_map, info->data->phy_offset, 1);
326 con1 = ADC_V2_CON1_SOFT_RESET;
327 writel(con1, ADC_V2_CON1(info->regs));
329 con2 = ADC_V2_CON2_OSEL | ADC_V2_CON2_ESEL |
330 ADC_V2_CON2_HIGHF | ADC_V2_CON2_C_TIME(0);
331 writel(con2, ADC_V2_CON2(info->regs));
333 /* Enable interrupts */
334 writel(1, ADC_V2_INT_EN(info->regs));
337 static void exynos_adc_v2_exit_hw(struct exynos_adc *info)
341 if (info->data->needs_adc_phy)
342 regmap_write(info->pmu_map, info->data->phy_offset, 0);
344 con = readl(ADC_V2_CON1(info->regs));
345 con &= ~ADC_CON_EN_START;
346 writel(con, ADC_V2_CON1(info->regs));
349 static void exynos_adc_v2_clear_irq(struct exynos_adc *info)
351 writel(1, ADC_V2_INT_ST(info->regs));
354 static void exynos_adc_v2_start_conv(struct exynos_adc *info,
359 con2 = readl(ADC_V2_CON2(info->regs));
360 con2 &= ~ADC_V2_CON2_ACH_MASK;
361 con2 |= ADC_V2_CON2_ACH_SEL(addr);
362 writel(con2, ADC_V2_CON2(info->regs));
364 con1 = readl(ADC_V2_CON1(info->regs));
365 writel(con1 | ADC_CON_EN_START, ADC_V2_CON1(info->regs));
368 static const struct exynos_adc_data exynos_adc_v2_data = {
369 .num_channels = MAX_ADC_V2_CHANNELS,
370 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
371 .needs_adc_phy = true,
372 .phy_offset = EXYNOS_ADCV2_PHY_OFFSET,
374 .init_hw = exynos_adc_v2_init_hw,
375 .exit_hw = exynos_adc_v2_exit_hw,
376 .clear_irq = exynos_adc_v2_clear_irq,
377 .start_conv = exynos_adc_v2_start_conv,
380 static const struct exynos_adc_data exynos3250_adc_data = {
381 .num_channels = MAX_EXYNOS3250_ADC_CHANNELS,
382 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
384 .needs_adc_phy = true,
385 .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
387 .init_hw = exynos_adc_v2_init_hw,
388 .exit_hw = exynos_adc_v2_exit_hw,
389 .clear_irq = exynos_adc_v2_clear_irq,
390 .start_conv = exynos_adc_v2_start_conv,
393 static const struct of_device_id exynos_adc_match[] = {
395 .compatible = "samsung,s3c2410-adc",
396 .data = &exynos_adc_s3c24xx_data,
398 .compatible = "samsung,s3c2416-adc",
399 .data = &exynos_adc_s3c2416_data,
401 .compatible = "samsung,s3c2440-adc",
402 .data = &exynos_adc_s3c24xx_data,
404 .compatible = "samsung,s3c2443-adc",
405 .data = &exynos_adc_s3c2443_data,
407 .compatible = "samsung,s3c6410-adc",
408 .data = &exynos_adc_s3c64xx_data,
410 .compatible = "samsung,exynos-adc-v1",
411 .data = &exynos_adc_v1_data,
413 .compatible = "samsung,exynos-adc-v2",
414 .data = &exynos_adc_v2_data,
416 .compatible = "samsung,exynos3250-adc",
417 .data = &exynos3250_adc_data,
421 MODULE_DEVICE_TABLE(of, exynos_adc_match);
423 static struct exynos_adc_data *exynos_adc_get_data(struct platform_device *pdev)
425 const struct of_device_id *match;
427 match = of_match_node(exynos_adc_match, pdev->dev.of_node);
428 return (struct exynos_adc_data *)match->data;
431 static int exynos_read_raw(struct iio_dev *indio_dev,
432 struct iio_chan_spec const *chan,
437 struct exynos_adc *info = iio_priv(indio_dev);
438 unsigned long timeout;
441 if (mask != IIO_CHAN_INFO_RAW)
444 mutex_lock(&indio_dev->mlock);
445 reinit_completion(&info->completion);
447 /* Select the channel to be used and Trigger conversion */
448 if (info->data->start_conv)
449 info->data->start_conv(info, chan->address);
451 timeout = wait_for_completion_timeout
452 (&info->completion, EXYNOS_ADC_TIMEOUT);
454 dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n");
455 if (info->data->init_hw)
456 info->data->init_hw(info);
464 mutex_unlock(&indio_dev->mlock);
469 static irqreturn_t exynos_adc_isr(int irq, void *dev_id)
471 struct exynos_adc *info = (struct exynos_adc *)dev_id;
472 u32 mask = info->data->mask;
475 info->value = readl(ADC_V1_DATX(info->regs)) & mask;
478 if (info->data->clear_irq)
479 info->data->clear_irq(info);
481 complete(&info->completion);
486 static int exynos_adc_reg_access(struct iio_dev *indio_dev,
487 unsigned reg, unsigned writeval,
490 struct exynos_adc *info = iio_priv(indio_dev);
495 *readval = readl(info->regs + reg);
500 static const struct iio_info exynos_adc_iio_info = {
501 .read_raw = &exynos_read_raw,
502 .debugfs_reg_access = &exynos_adc_reg_access,
503 .driver_module = THIS_MODULE,
506 #define ADC_CHANNEL(_index, _id) { \
507 .type = IIO_VOLTAGE, \
511 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
512 .datasheet_name = _id, \
515 static const struct iio_chan_spec exynos_adc_iio_channels[] = {
516 ADC_CHANNEL(0, "adc0"),
517 ADC_CHANNEL(1, "adc1"),
518 ADC_CHANNEL(2, "adc2"),
519 ADC_CHANNEL(3, "adc3"),
520 ADC_CHANNEL(4, "adc4"),
521 ADC_CHANNEL(5, "adc5"),
522 ADC_CHANNEL(6, "adc6"),
523 ADC_CHANNEL(7, "adc7"),
524 ADC_CHANNEL(8, "adc8"),
525 ADC_CHANNEL(9, "adc9"),
528 static int exynos_adc_remove_devices(struct device *dev, void *c)
530 struct platform_device *pdev = to_platform_device(dev);
532 platform_device_unregister(pdev);
537 static int exynos_adc_probe(struct platform_device *pdev)
539 struct exynos_adc *info = NULL;
540 struct device_node *np = pdev->dev.of_node;
541 struct iio_dev *indio_dev = NULL;
542 struct resource *mem;
549 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct exynos_adc));
551 dev_err(&pdev->dev, "failed allocating iio device\n");
555 info = iio_priv(indio_dev);
557 info->data = exynos_adc_get_data(pdev);
559 dev_err(&pdev->dev, "failed getting exynos_adc_data\n");
563 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
564 info->regs = devm_ioremap_resource(&pdev->dev, mem);
565 if (IS_ERR(info->regs))
566 return PTR_ERR(info->regs);
569 if (info->data->needs_adc_phy) {
570 info->pmu_map = syscon_regmap_lookup_by_phandle(
572 "samsung,syscon-phandle");
573 if (IS_ERR(info->pmu_map)) {
574 dev_err(&pdev->dev, "syscon regmap lookup failed.\n");
575 return PTR_ERR(info->pmu_map);
579 irq = platform_get_irq(pdev, 0);
581 dev_err(&pdev->dev, "no irq resource?\n");
586 info->dev = &pdev->dev;
588 init_completion(&info->completion);
590 info->clk = devm_clk_get(&pdev->dev, "adc");
591 if (IS_ERR(info->clk)) {
592 dev_err(&pdev->dev, "failed getting clock, err = %ld\n",
594 return PTR_ERR(info->clk);
597 if (info->data->needs_sclk) {
598 info->sclk = devm_clk_get(&pdev->dev, "sclk");
599 if (IS_ERR(info->sclk)) {
601 "failed getting sclk clock, err = %ld\n",
602 PTR_ERR(info->sclk));
603 return PTR_ERR(info->sclk);
607 info->vdd = devm_regulator_get(&pdev->dev, "vdd");
608 if (IS_ERR(info->vdd)) {
609 dev_err(&pdev->dev, "failed getting regulator, err = %ld\n",
611 return PTR_ERR(info->vdd);
614 ret = regulator_enable(info->vdd);
618 ret = exynos_adc_prepare_clk(info);
620 goto err_disable_reg;
622 ret = exynos_adc_enable_clk(info);
624 goto err_unprepare_clk;
626 platform_set_drvdata(pdev, indio_dev);
628 indio_dev->name = dev_name(&pdev->dev);
629 indio_dev->dev.parent = &pdev->dev;
630 indio_dev->dev.of_node = pdev->dev.of_node;
631 indio_dev->info = &exynos_adc_iio_info;
632 indio_dev->modes = INDIO_DIRECT_MODE;
633 indio_dev->channels = exynos_adc_iio_channels;
634 indio_dev->num_channels = info->data->num_channels;
636 ret = request_irq(info->irq, exynos_adc_isr,
637 0, dev_name(&pdev->dev), info);
639 dev_err(&pdev->dev, "failed requesting irq, irq = %d\n",
641 goto err_disable_clk;
644 ret = iio_device_register(indio_dev);
648 if (info->data->init_hw)
649 info->data->init_hw(info);
651 ret = of_platform_populate(np, exynos_adc_match, NULL, &indio_dev->dev);
653 dev_err(&pdev->dev, "failed adding child nodes\n");
654 goto err_of_populate;
660 device_for_each_child(&indio_dev->dev, NULL,
661 exynos_adc_remove_devices);
662 iio_device_unregister(indio_dev);
664 free_irq(info->irq, info);
666 if (info->data->exit_hw)
667 info->data->exit_hw(info);
668 exynos_adc_disable_clk(info);
670 exynos_adc_unprepare_clk(info);
672 regulator_disable(info->vdd);
676 static int exynos_adc_remove(struct platform_device *pdev)
678 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
679 struct exynos_adc *info = iio_priv(indio_dev);
681 device_for_each_child(&indio_dev->dev, NULL,
682 exynos_adc_remove_devices);
683 iio_device_unregister(indio_dev);
684 free_irq(info->irq, info);
685 if (info->data->exit_hw)
686 info->data->exit_hw(info);
687 exynos_adc_disable_clk(info);
688 exynos_adc_unprepare_clk(info);
689 regulator_disable(info->vdd);
694 #ifdef CONFIG_PM_SLEEP
695 static int exynos_adc_suspend(struct device *dev)
697 struct iio_dev *indio_dev = dev_get_drvdata(dev);
698 struct exynos_adc *info = iio_priv(indio_dev);
700 if (info->data->exit_hw)
701 info->data->exit_hw(info);
702 exynos_adc_disable_clk(info);
703 regulator_disable(info->vdd);
708 static int exynos_adc_resume(struct device *dev)
710 struct iio_dev *indio_dev = dev_get_drvdata(dev);
711 struct exynos_adc *info = iio_priv(indio_dev);
714 ret = regulator_enable(info->vdd);
718 ret = exynos_adc_enable_clk(info);
722 if (info->data->init_hw)
723 info->data->init_hw(info);
729 static SIMPLE_DEV_PM_OPS(exynos_adc_pm_ops,
733 static struct platform_driver exynos_adc_driver = {
734 .probe = exynos_adc_probe,
735 .remove = exynos_adc_remove,
737 .name = "exynos-adc",
738 .of_match_table = exynos_adc_match,
739 .pm = &exynos_adc_pm_ops,
743 module_platform_driver(exynos_adc_driver);
745 MODULE_AUTHOR("Naveen Krishna Chatradhi <ch.naveen@samsung.com>");
746 MODULE_DESCRIPTION("Samsung EXYNOS5 ADC driver");
747 MODULE_LICENSE("GPL v2");