staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / iio / adc / exynos_adc.c
blobf10443f92e4ce8758a519d67ad95acf227881562
1 /*
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>
30 #include <linux/io.h>
31 #include <linux/clk.h>
32 #include <linux/completion.h>
33 #include <linux/of.h>
34 #include <linux/of_irq.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/of_platform.h>
37 #include <linux/err.h>
38 #include <linux/input.h>
40 #include <linux/iio/iio.h>
41 #include <linux/iio/machine.h>
42 #include <linux/iio/driver.h>
43 #include <linux/mfd/syscon.h>
44 #include <linux/regmap.h>
46 #include <linux/platform_data/touchscreen-s3c2410.h>
48 /* S3C/EXYNOS4412/5250 ADC_V1 registers definitions */
49 #define ADC_V1_CON(x) ((x) + 0x00)
50 #define ADC_V1_TSC(x) ((x) + 0x04)
51 #define ADC_V1_DLY(x) ((x) + 0x08)
52 #define ADC_V1_DATX(x) ((x) + 0x0C)
53 #define ADC_V1_DATY(x) ((x) + 0x10)
54 #define ADC_V1_UPDN(x) ((x) + 0x14)
55 #define ADC_V1_INTCLR(x) ((x) + 0x18)
56 #define ADC_V1_MUX(x) ((x) + 0x1c)
57 #define ADC_V1_CLRINTPNDNUP(x) ((x) + 0x20)
59 /* S3C2410 ADC registers definitions */
60 #define ADC_S3C2410_MUX(x) ((x) + 0x18)
62 /* Future ADC_V2 registers definitions */
63 #define ADC_V2_CON1(x) ((x) + 0x00)
64 #define ADC_V2_CON2(x) ((x) + 0x04)
65 #define ADC_V2_STAT(x) ((x) + 0x08)
66 #define ADC_V2_INT_EN(x) ((x) + 0x10)
67 #define ADC_V2_INT_ST(x) ((x) + 0x14)
68 #define ADC_V2_VER(x) ((x) + 0x20)
70 /* Bit definitions for ADC_V1 */
71 #define ADC_V1_CON_RES (1u << 16)
72 #define ADC_V1_CON_PRSCEN (1u << 14)
73 #define ADC_V1_CON_PRSCLV(x) (((x) & 0xFF) << 6)
74 #define ADC_V1_CON_STANDBY (1u << 2)
76 /* Bit definitions for S3C2410 ADC */
77 #define ADC_S3C2410_CON_SELMUX(x) (((x) & 7) << 3)
78 #define ADC_S3C2410_DATX_MASK 0x3FF
79 #define ADC_S3C2416_CON_RES_SEL (1u << 3)
81 /* touch screen always uses channel 0 */
82 #define ADC_S3C2410_MUX_TS 0
84 /* ADCTSC Register Bits */
85 #define ADC_S3C2443_TSC_UD_SEN (1u << 8)
86 #define ADC_S3C2410_TSC_YM_SEN (1u << 7)
87 #define ADC_S3C2410_TSC_YP_SEN (1u << 6)
88 #define ADC_S3C2410_TSC_XM_SEN (1u << 5)
89 #define ADC_S3C2410_TSC_XP_SEN (1u << 4)
90 #define ADC_S3C2410_TSC_PULL_UP_DISABLE (1u << 3)
91 #define ADC_S3C2410_TSC_AUTO_PST (1u << 2)
92 #define ADC_S3C2410_TSC_XY_PST(x) (((x) & 0x3) << 0)
94 #define ADC_TSC_WAIT4INT (ADC_S3C2410_TSC_YM_SEN | \
95 ADC_S3C2410_TSC_YP_SEN | \
96 ADC_S3C2410_TSC_XP_SEN | \
97 ADC_S3C2410_TSC_XY_PST(3))
99 #define ADC_TSC_AUTOPST (ADC_S3C2410_TSC_YM_SEN | \
100 ADC_S3C2410_TSC_YP_SEN | \
101 ADC_S3C2410_TSC_XP_SEN | \
102 ADC_S3C2410_TSC_AUTO_PST | \
103 ADC_S3C2410_TSC_XY_PST(0))
105 /* Bit definitions for ADC_V2 */
106 #define ADC_V2_CON1_SOFT_RESET (1u << 2)
108 #define ADC_V2_CON2_OSEL (1u << 10)
109 #define ADC_V2_CON2_ESEL (1u << 9)
110 #define ADC_V2_CON2_HIGHF (1u << 8)
111 #define ADC_V2_CON2_C_TIME(x) (((x) & 7) << 4)
112 #define ADC_V2_CON2_ACH_SEL(x) (((x) & 0xF) << 0)
113 #define ADC_V2_CON2_ACH_MASK 0xF
115 #define MAX_ADC_V2_CHANNELS 10
116 #define MAX_ADC_V1_CHANNELS 8
117 #define MAX_EXYNOS3250_ADC_CHANNELS 2
119 /* Bit definitions common for ADC_V1 and ADC_V2 */
120 #define ADC_CON_EN_START (1u << 0)
121 #define ADC_CON_EN_START_MASK (0x3 << 0)
122 #define ADC_DATX_PRESSED (1u << 15)
123 #define ADC_DATX_MASK 0xFFF
124 #define ADC_DATY_MASK 0xFFF
126 #define EXYNOS_ADC_TIMEOUT (msecs_to_jiffies(100))
128 #define EXYNOS_ADCV1_PHY_OFFSET 0x0718
129 #define EXYNOS_ADCV2_PHY_OFFSET 0x0720
131 struct exynos_adc {
132 struct exynos_adc_data *data;
133 struct device *dev;
134 struct input_dev *input;
135 void __iomem *regs;
136 struct regmap *pmu_map;
137 struct clk *clk;
138 struct clk *sclk;
139 unsigned int irq;
140 unsigned int tsirq;
141 unsigned int delay;
142 struct regulator *vdd;
144 struct completion completion;
146 u32 value;
147 unsigned int version;
149 bool read_ts;
150 u32 ts_x;
151 u32 ts_y;
154 struct exynos_adc_data {
155 int num_channels;
156 bool needs_sclk;
157 bool needs_adc_phy;
158 int phy_offset;
159 u32 mask;
161 void (*init_hw)(struct exynos_adc *info);
162 void (*exit_hw)(struct exynos_adc *info);
163 void (*clear_irq)(struct exynos_adc *info);
164 void (*start_conv)(struct exynos_adc *info, unsigned long addr);
167 static void exynos_adc_unprepare_clk(struct exynos_adc *info)
169 if (info->data->needs_sclk)
170 clk_unprepare(info->sclk);
171 clk_unprepare(info->clk);
174 static int exynos_adc_prepare_clk(struct exynos_adc *info)
176 int ret;
178 ret = clk_prepare(info->clk);
179 if (ret) {
180 dev_err(info->dev, "failed preparing adc clock: %d\n", ret);
181 return ret;
184 if (info->data->needs_sclk) {
185 ret = clk_prepare(info->sclk);
186 if (ret) {
187 clk_unprepare(info->clk);
188 dev_err(info->dev,
189 "failed preparing sclk_adc clock: %d\n", ret);
190 return ret;
194 return 0;
197 static void exynos_adc_disable_clk(struct exynos_adc *info)
199 if (info->data->needs_sclk)
200 clk_disable(info->sclk);
201 clk_disable(info->clk);
204 static int exynos_adc_enable_clk(struct exynos_adc *info)
206 int ret;
208 ret = clk_enable(info->clk);
209 if (ret) {
210 dev_err(info->dev, "failed enabling adc clock: %d\n", ret);
211 return ret;
214 if (info->data->needs_sclk) {
215 ret = clk_enable(info->sclk);
216 if (ret) {
217 clk_disable(info->clk);
218 dev_err(info->dev,
219 "failed enabling sclk_adc clock: %d\n", ret);
220 return ret;
224 return 0;
227 static void exynos_adc_v1_init_hw(struct exynos_adc *info)
229 u32 con1;
231 if (info->data->needs_adc_phy)
232 regmap_write(info->pmu_map, info->data->phy_offset, 1);
234 /* set default prescaler values and Enable prescaler */
235 con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN;
237 /* Enable 12-bit ADC resolution */
238 con1 |= ADC_V1_CON_RES;
239 writel(con1, ADC_V1_CON(info->regs));
241 /* set touchscreen delay */
242 writel(info->delay, ADC_V1_DLY(info->regs));
245 static void exynos_adc_v1_exit_hw(struct exynos_adc *info)
247 u32 con;
249 if (info->data->needs_adc_phy)
250 regmap_write(info->pmu_map, info->data->phy_offset, 0);
252 con = readl(ADC_V1_CON(info->regs));
253 con |= ADC_V1_CON_STANDBY;
254 writel(con, ADC_V1_CON(info->regs));
257 static void exynos_adc_v1_clear_irq(struct exynos_adc *info)
259 writel(1, ADC_V1_INTCLR(info->regs));
262 static void exynos_adc_v1_start_conv(struct exynos_adc *info,
263 unsigned long addr)
265 u32 con1;
267 writel(addr, ADC_V1_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 const struct exynos_adc_data exynos_adc_v1_data = {
274 .num_channels = MAX_ADC_V1_CHANNELS,
275 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
276 .needs_adc_phy = true,
277 .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
279 .init_hw = exynos_adc_v1_init_hw,
280 .exit_hw = exynos_adc_v1_exit_hw,
281 .clear_irq = exynos_adc_v1_clear_irq,
282 .start_conv = exynos_adc_v1_start_conv,
285 static void exynos_adc_s3c2416_start_conv(struct exynos_adc *info,
286 unsigned long addr)
288 u32 con1;
290 /* Enable 12 bit ADC resolution */
291 con1 = readl(ADC_V1_CON(info->regs));
292 con1 |= ADC_S3C2416_CON_RES_SEL;
293 writel(con1, ADC_V1_CON(info->regs));
295 /* Select channel for S3C2416 */
296 writel(addr, ADC_S3C2410_MUX(info->regs));
298 con1 = readl(ADC_V1_CON(info->regs));
299 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
302 static struct exynos_adc_data const exynos_adc_s3c2416_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 .start_conv = exynos_adc_s3c2416_start_conv,
311 static void exynos_adc_s3c2443_start_conv(struct exynos_adc *info,
312 unsigned long addr)
314 u32 con1;
316 /* Select channel for S3C2433 */
317 writel(addr, ADC_S3C2410_MUX(info->regs));
319 con1 = readl(ADC_V1_CON(info->regs));
320 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
323 static struct exynos_adc_data const exynos_adc_s3c2443_data = {
324 .num_channels = MAX_ADC_V1_CHANNELS,
325 .mask = ADC_S3C2410_DATX_MASK, /* 10 bit ADC resolution */
327 .init_hw = exynos_adc_v1_init_hw,
328 .exit_hw = exynos_adc_v1_exit_hw,
329 .start_conv = exynos_adc_s3c2443_start_conv,
332 static void exynos_adc_s3c64xx_start_conv(struct exynos_adc *info,
333 unsigned long addr)
335 u32 con1;
337 con1 = readl(ADC_V1_CON(info->regs));
338 con1 &= ~ADC_S3C2410_CON_SELMUX(0x7);
339 con1 |= ADC_S3C2410_CON_SELMUX(addr);
340 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
343 static struct exynos_adc_data const exynos_adc_s3c24xx_data = {
344 .num_channels = MAX_ADC_V1_CHANNELS,
345 .mask = ADC_S3C2410_DATX_MASK, /* 10 bit ADC resolution */
347 .init_hw = exynos_adc_v1_init_hw,
348 .exit_hw = exynos_adc_v1_exit_hw,
349 .start_conv = exynos_adc_s3c64xx_start_conv,
352 static struct exynos_adc_data const exynos_adc_s3c64xx_data = {
353 .num_channels = MAX_ADC_V1_CHANNELS,
354 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
356 .init_hw = exynos_adc_v1_init_hw,
357 .exit_hw = exynos_adc_v1_exit_hw,
358 .clear_irq = exynos_adc_v1_clear_irq,
359 .start_conv = exynos_adc_s3c64xx_start_conv,
362 static void exynos_adc_v2_init_hw(struct exynos_adc *info)
364 u32 con1, con2;
366 if (info->data->needs_adc_phy)
367 regmap_write(info->pmu_map, info->data->phy_offset, 1);
369 con1 = ADC_V2_CON1_SOFT_RESET;
370 writel(con1, ADC_V2_CON1(info->regs));
372 con2 = ADC_V2_CON2_OSEL | ADC_V2_CON2_ESEL |
373 ADC_V2_CON2_HIGHF | ADC_V2_CON2_C_TIME(0);
374 writel(con2, ADC_V2_CON2(info->regs));
376 /* Enable interrupts */
377 writel(1, ADC_V2_INT_EN(info->regs));
380 static void exynos_adc_v2_exit_hw(struct exynos_adc *info)
382 u32 con;
384 if (info->data->needs_adc_phy)
385 regmap_write(info->pmu_map, info->data->phy_offset, 0);
387 con = readl(ADC_V2_CON1(info->regs));
388 con &= ~ADC_CON_EN_START;
389 writel(con, ADC_V2_CON1(info->regs));
392 static void exynos_adc_v2_clear_irq(struct exynos_adc *info)
394 writel(1, ADC_V2_INT_ST(info->regs));
397 static void exynos_adc_v2_start_conv(struct exynos_adc *info,
398 unsigned long addr)
400 u32 con1, con2;
402 con2 = readl(ADC_V2_CON2(info->regs));
403 con2 &= ~ADC_V2_CON2_ACH_MASK;
404 con2 |= ADC_V2_CON2_ACH_SEL(addr);
405 writel(con2, ADC_V2_CON2(info->regs));
407 con1 = readl(ADC_V2_CON1(info->regs));
408 writel(con1 | ADC_CON_EN_START, ADC_V2_CON1(info->regs));
411 static const struct exynos_adc_data exynos_adc_v2_data = {
412 .num_channels = MAX_ADC_V2_CHANNELS,
413 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
414 .needs_adc_phy = true,
415 .phy_offset = EXYNOS_ADCV2_PHY_OFFSET,
417 .init_hw = exynos_adc_v2_init_hw,
418 .exit_hw = exynos_adc_v2_exit_hw,
419 .clear_irq = exynos_adc_v2_clear_irq,
420 .start_conv = exynos_adc_v2_start_conv,
423 static const struct exynos_adc_data exynos3250_adc_data = {
424 .num_channels = MAX_EXYNOS3250_ADC_CHANNELS,
425 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
426 .needs_sclk = true,
427 .needs_adc_phy = true,
428 .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
430 .init_hw = exynos_adc_v2_init_hw,
431 .exit_hw = exynos_adc_v2_exit_hw,
432 .clear_irq = exynos_adc_v2_clear_irq,
433 .start_conv = exynos_adc_v2_start_conv,
436 static void exynos_adc_exynos7_init_hw(struct exynos_adc *info)
438 u32 con1, con2;
440 if (info->data->needs_adc_phy)
441 regmap_write(info->pmu_map, info->data->phy_offset, 1);
443 con1 = ADC_V2_CON1_SOFT_RESET;
444 writel(con1, ADC_V2_CON1(info->regs));
446 con2 = readl(ADC_V2_CON2(info->regs));
447 con2 &= ~ADC_V2_CON2_C_TIME(7);
448 con2 |= ADC_V2_CON2_C_TIME(0);
449 writel(con2, ADC_V2_CON2(info->regs));
451 /* Enable interrupts */
452 writel(1, ADC_V2_INT_EN(info->regs));
455 static const struct exynos_adc_data exynos7_adc_data = {
456 .num_channels = MAX_ADC_V1_CHANNELS,
457 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
459 .init_hw = exynos_adc_exynos7_init_hw,
460 .exit_hw = exynos_adc_v2_exit_hw,
461 .clear_irq = exynos_adc_v2_clear_irq,
462 .start_conv = exynos_adc_v2_start_conv,
465 static const struct of_device_id exynos_adc_match[] = {
467 .compatible = "samsung,s3c2410-adc",
468 .data = &exynos_adc_s3c24xx_data,
469 }, {
470 .compatible = "samsung,s3c2416-adc",
471 .data = &exynos_adc_s3c2416_data,
472 }, {
473 .compatible = "samsung,s3c2440-adc",
474 .data = &exynos_adc_s3c24xx_data,
475 }, {
476 .compatible = "samsung,s3c2443-adc",
477 .data = &exynos_adc_s3c2443_data,
478 }, {
479 .compatible = "samsung,s3c6410-adc",
480 .data = &exynos_adc_s3c64xx_data,
481 }, {
482 .compatible = "samsung,exynos-adc-v1",
483 .data = &exynos_adc_v1_data,
484 }, {
485 .compatible = "samsung,exynos-adc-v2",
486 .data = &exynos_adc_v2_data,
487 }, {
488 .compatible = "samsung,exynos3250-adc",
489 .data = &exynos3250_adc_data,
490 }, {
491 .compatible = "samsung,exynos7-adc",
492 .data = &exynos7_adc_data,
496 MODULE_DEVICE_TABLE(of, exynos_adc_match);
498 static struct exynos_adc_data *exynos_adc_get_data(struct platform_device *pdev)
500 const struct of_device_id *match;
502 match = of_match_node(exynos_adc_match, pdev->dev.of_node);
503 return (struct exynos_adc_data *)match->data;
506 static int exynos_read_raw(struct iio_dev *indio_dev,
507 struct iio_chan_spec const *chan,
508 int *val,
509 int *val2,
510 long mask)
512 struct exynos_adc *info = iio_priv(indio_dev);
513 unsigned long timeout;
514 int ret;
516 if (mask != IIO_CHAN_INFO_RAW)
517 return -EINVAL;
519 mutex_lock(&indio_dev->mlock);
520 reinit_completion(&info->completion);
522 /* Select the channel to be used and Trigger conversion */
523 if (info->data->start_conv)
524 info->data->start_conv(info, chan->address);
526 timeout = wait_for_completion_timeout(&info->completion,
527 EXYNOS_ADC_TIMEOUT);
528 if (timeout == 0) {
529 dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n");
530 if (info->data->init_hw)
531 info->data->init_hw(info);
532 ret = -ETIMEDOUT;
533 } else {
534 *val = info->value;
535 *val2 = 0;
536 ret = IIO_VAL_INT;
539 mutex_unlock(&indio_dev->mlock);
541 return ret;
544 static int exynos_read_s3c64xx_ts(struct iio_dev *indio_dev, int *x, int *y)
546 struct exynos_adc *info = iio_priv(indio_dev);
547 unsigned long timeout;
548 int ret;
550 mutex_lock(&indio_dev->mlock);
551 info->read_ts = true;
553 reinit_completion(&info->completion);
555 writel(ADC_S3C2410_TSC_PULL_UP_DISABLE | ADC_TSC_AUTOPST,
556 ADC_V1_TSC(info->regs));
558 /* Select the ts channel to be used and Trigger conversion */
559 info->data->start_conv(info, ADC_S3C2410_MUX_TS);
561 timeout = wait_for_completion_timeout(&info->completion,
562 EXYNOS_ADC_TIMEOUT);
563 if (timeout == 0) {
564 dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n");
565 if (info->data->init_hw)
566 info->data->init_hw(info);
567 ret = -ETIMEDOUT;
568 } else {
569 *x = info->ts_x;
570 *y = info->ts_y;
571 ret = 0;
574 info->read_ts = false;
575 mutex_unlock(&indio_dev->mlock);
577 return ret;
580 static irqreturn_t exynos_adc_isr(int irq, void *dev_id)
582 struct exynos_adc *info = dev_id;
583 u32 mask = info->data->mask;
585 /* Read value */
586 if (info->read_ts) {
587 info->ts_x = readl(ADC_V1_DATX(info->regs));
588 info->ts_y = readl(ADC_V1_DATY(info->regs));
589 writel(ADC_TSC_WAIT4INT | ADC_S3C2443_TSC_UD_SEN, ADC_V1_TSC(info->regs));
590 } else {
591 info->value = readl(ADC_V1_DATX(info->regs)) & mask;
594 /* clear irq */
595 if (info->data->clear_irq)
596 info->data->clear_irq(info);
598 complete(&info->completion);
600 return IRQ_HANDLED;
604 * Here we (ab)use a threaded interrupt handler to stay running
605 * for as long as the touchscreen remains pressed, we report
606 * a new event with the latest data and then sleep until the
607 * next timer tick. This mirrors the behavior of the old
608 * driver, with much less code.
610 static irqreturn_t exynos_ts_isr(int irq, void *dev_id)
612 struct exynos_adc *info = dev_id;
613 struct iio_dev *dev = dev_get_drvdata(info->dev);
614 u32 x, y;
615 bool pressed;
616 int ret;
618 while (info->input->users) {
619 ret = exynos_read_s3c64xx_ts(dev, &x, &y);
620 if (ret == -ETIMEDOUT)
621 break;
623 pressed = x & y & ADC_DATX_PRESSED;
624 if (!pressed) {
625 input_report_key(info->input, BTN_TOUCH, 0);
626 input_sync(info->input);
627 break;
630 input_report_abs(info->input, ABS_X, x & ADC_DATX_MASK);
631 input_report_abs(info->input, ABS_Y, y & ADC_DATY_MASK);
632 input_report_key(info->input, BTN_TOUCH, 1);
633 input_sync(info->input);
635 usleep_range(1000, 1100);
638 writel(0, ADC_V1_CLRINTPNDNUP(info->regs));
640 return IRQ_HANDLED;
643 static int exynos_adc_reg_access(struct iio_dev *indio_dev,
644 unsigned reg, unsigned writeval,
645 unsigned *readval)
647 struct exynos_adc *info = iio_priv(indio_dev);
649 if (readval == NULL)
650 return -EINVAL;
652 *readval = readl(info->regs + reg);
654 return 0;
657 static const struct iio_info exynos_adc_iio_info = {
658 .read_raw = &exynos_read_raw,
659 .debugfs_reg_access = &exynos_adc_reg_access,
662 #define ADC_CHANNEL(_index, _id) { \
663 .type = IIO_VOLTAGE, \
664 .indexed = 1, \
665 .channel = _index, \
666 .address = _index, \
667 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
668 .datasheet_name = _id, \
671 static const struct iio_chan_spec exynos_adc_iio_channels[] = {
672 ADC_CHANNEL(0, "adc0"),
673 ADC_CHANNEL(1, "adc1"),
674 ADC_CHANNEL(2, "adc2"),
675 ADC_CHANNEL(3, "adc3"),
676 ADC_CHANNEL(4, "adc4"),
677 ADC_CHANNEL(5, "adc5"),
678 ADC_CHANNEL(6, "adc6"),
679 ADC_CHANNEL(7, "adc7"),
680 ADC_CHANNEL(8, "adc8"),
681 ADC_CHANNEL(9, "adc9"),
684 static int exynos_adc_remove_devices(struct device *dev, void *c)
686 struct platform_device *pdev = to_platform_device(dev);
688 platform_device_unregister(pdev);
690 return 0;
693 static int exynos_adc_ts_open(struct input_dev *dev)
695 struct exynos_adc *info = input_get_drvdata(dev);
697 enable_irq(info->tsirq);
699 return 0;
702 static void exynos_adc_ts_close(struct input_dev *dev)
704 struct exynos_adc *info = input_get_drvdata(dev);
706 disable_irq(info->tsirq);
709 static int exynos_adc_ts_init(struct exynos_adc *info)
711 int ret;
713 if (info->tsirq <= 0)
714 return -ENODEV;
716 info->input = input_allocate_device();
717 if (!info->input)
718 return -ENOMEM;
720 info->input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
721 info->input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
723 input_set_abs_params(info->input, ABS_X, 0, 0x3FF, 0, 0);
724 input_set_abs_params(info->input, ABS_Y, 0, 0x3FF, 0, 0);
726 info->input->name = "S3C24xx TouchScreen";
727 info->input->id.bustype = BUS_HOST;
728 info->input->open = exynos_adc_ts_open;
729 info->input->close = exynos_adc_ts_close;
731 input_set_drvdata(info->input, info);
733 ret = input_register_device(info->input);
734 if (ret) {
735 input_free_device(info->input);
736 return ret;
739 disable_irq(info->tsirq);
740 ret = request_threaded_irq(info->tsirq, NULL, exynos_ts_isr,
741 IRQF_ONESHOT, "touchscreen", info);
742 if (ret)
743 input_unregister_device(info->input);
745 return ret;
748 static int exynos_adc_probe(struct platform_device *pdev)
750 struct exynos_adc *info = NULL;
751 struct device_node *np = pdev->dev.of_node;
752 struct s3c2410_ts_mach_info *pdata = dev_get_platdata(&pdev->dev);
753 struct iio_dev *indio_dev = NULL;
754 struct resource *mem;
755 bool has_ts = false;
756 int ret = -ENODEV;
757 int irq;
759 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct exynos_adc));
760 if (!indio_dev) {
761 dev_err(&pdev->dev, "failed allocating iio device\n");
762 return -ENOMEM;
765 info = iio_priv(indio_dev);
767 info->data = exynos_adc_get_data(pdev);
768 if (!info->data) {
769 dev_err(&pdev->dev, "failed getting exynos_adc_data\n");
770 return -EINVAL;
773 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
774 info->regs = devm_ioremap_resource(&pdev->dev, mem);
775 if (IS_ERR(info->regs))
776 return PTR_ERR(info->regs);
779 if (info->data->needs_adc_phy) {
780 info->pmu_map = syscon_regmap_lookup_by_phandle(
781 pdev->dev.of_node,
782 "samsung,syscon-phandle");
783 if (IS_ERR(info->pmu_map)) {
784 dev_err(&pdev->dev, "syscon regmap lookup failed.\n");
785 return PTR_ERR(info->pmu_map);
789 irq = platform_get_irq(pdev, 0);
790 if (irq < 0) {
791 dev_err(&pdev->dev, "no irq resource?\n");
792 return irq;
794 info->irq = irq;
796 irq = platform_get_irq(pdev, 1);
797 if (irq == -EPROBE_DEFER)
798 return irq;
800 info->tsirq = irq;
802 info->dev = &pdev->dev;
804 init_completion(&info->completion);
806 info->clk = devm_clk_get(&pdev->dev, "adc");
807 if (IS_ERR(info->clk)) {
808 dev_err(&pdev->dev, "failed getting clock, err = %ld\n",
809 PTR_ERR(info->clk));
810 return PTR_ERR(info->clk);
813 if (info->data->needs_sclk) {
814 info->sclk = devm_clk_get(&pdev->dev, "sclk");
815 if (IS_ERR(info->sclk)) {
816 dev_err(&pdev->dev,
817 "failed getting sclk clock, err = %ld\n",
818 PTR_ERR(info->sclk));
819 return PTR_ERR(info->sclk);
823 info->vdd = devm_regulator_get(&pdev->dev, "vdd");
824 if (IS_ERR(info->vdd)) {
825 dev_err(&pdev->dev, "failed getting regulator, err = %ld\n",
826 PTR_ERR(info->vdd));
827 return PTR_ERR(info->vdd);
830 ret = regulator_enable(info->vdd);
831 if (ret)
832 return ret;
834 ret = exynos_adc_prepare_clk(info);
835 if (ret)
836 goto err_disable_reg;
838 ret = exynos_adc_enable_clk(info);
839 if (ret)
840 goto err_unprepare_clk;
842 platform_set_drvdata(pdev, indio_dev);
844 indio_dev->name = dev_name(&pdev->dev);
845 indio_dev->dev.parent = &pdev->dev;
846 indio_dev->dev.of_node = pdev->dev.of_node;
847 indio_dev->info = &exynos_adc_iio_info;
848 indio_dev->modes = INDIO_DIRECT_MODE;
849 indio_dev->channels = exynos_adc_iio_channels;
850 indio_dev->num_channels = info->data->num_channels;
852 ret = request_irq(info->irq, exynos_adc_isr,
853 0, dev_name(&pdev->dev), info);
854 if (ret < 0) {
855 dev_err(&pdev->dev, "failed requesting irq, irq = %d\n",
856 info->irq);
857 goto err_disable_clk;
860 ret = iio_device_register(indio_dev);
861 if (ret)
862 goto err_irq;
864 if (info->data->init_hw)
865 info->data->init_hw(info);
867 /* leave out any TS related code if unreachable */
868 if (IS_REACHABLE(CONFIG_INPUT)) {
869 has_ts = of_property_read_bool(pdev->dev.of_node,
870 "has-touchscreen") || pdata;
873 if (pdata)
874 info->delay = pdata->delay;
875 else
876 info->delay = 10000;
878 if (has_ts)
879 ret = exynos_adc_ts_init(info);
880 if (ret)
881 goto err_iio;
883 ret = of_platform_populate(np, exynos_adc_match, NULL, &indio_dev->dev);
884 if (ret < 0) {
885 dev_err(&pdev->dev, "failed adding child nodes\n");
886 goto err_of_populate;
889 return 0;
891 err_of_populate:
892 device_for_each_child(&indio_dev->dev, NULL,
893 exynos_adc_remove_devices);
894 if (has_ts) {
895 input_unregister_device(info->input);
896 free_irq(info->tsirq, info);
898 err_iio:
899 iio_device_unregister(indio_dev);
900 err_irq:
901 free_irq(info->irq, info);
902 err_disable_clk:
903 if (info->data->exit_hw)
904 info->data->exit_hw(info);
905 exynos_adc_disable_clk(info);
906 err_unprepare_clk:
907 exynos_adc_unprepare_clk(info);
908 err_disable_reg:
909 regulator_disable(info->vdd);
910 return ret;
913 static int exynos_adc_remove(struct platform_device *pdev)
915 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
916 struct exynos_adc *info = iio_priv(indio_dev);
918 if (IS_REACHABLE(CONFIG_INPUT)) {
919 free_irq(info->tsirq, info);
920 input_unregister_device(info->input);
922 device_for_each_child(&indio_dev->dev, NULL,
923 exynos_adc_remove_devices);
924 iio_device_unregister(indio_dev);
925 free_irq(info->irq, info);
926 if (info->data->exit_hw)
927 info->data->exit_hw(info);
928 exynos_adc_disable_clk(info);
929 exynos_adc_unprepare_clk(info);
930 regulator_disable(info->vdd);
932 return 0;
935 #ifdef CONFIG_PM_SLEEP
936 static int exynos_adc_suspend(struct device *dev)
938 struct iio_dev *indio_dev = dev_get_drvdata(dev);
939 struct exynos_adc *info = iio_priv(indio_dev);
941 if (info->data->exit_hw)
942 info->data->exit_hw(info);
943 exynos_adc_disable_clk(info);
944 regulator_disable(info->vdd);
946 return 0;
949 static int exynos_adc_resume(struct device *dev)
951 struct iio_dev *indio_dev = dev_get_drvdata(dev);
952 struct exynos_adc *info = iio_priv(indio_dev);
953 int ret;
955 ret = regulator_enable(info->vdd);
956 if (ret)
957 return ret;
959 ret = exynos_adc_enable_clk(info);
960 if (ret)
961 return ret;
963 if (info->data->init_hw)
964 info->data->init_hw(info);
966 return 0;
968 #endif
970 static SIMPLE_DEV_PM_OPS(exynos_adc_pm_ops,
971 exynos_adc_suspend,
972 exynos_adc_resume);
974 static struct platform_driver exynos_adc_driver = {
975 .probe = exynos_adc_probe,
976 .remove = exynos_adc_remove,
977 .driver = {
978 .name = "exynos-adc",
979 .of_match_table = exynos_adc_match,
980 .pm = &exynos_adc_pm_ops,
984 module_platform_driver(exynos_adc_driver);
986 MODULE_AUTHOR("Naveen Krishna Chatradhi <ch.naveen@samsung.com>");
987 MODULE_DESCRIPTION("Samsung EXYNOS5 ADC driver");
988 MODULE_LICENSE("GPL v2");