blob: fe0317745436e3b93955d486a86998f1da99ed80 [file] [log] [blame]
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +00001/*
2 * exynos_adc.c - Support for ADC in EXYNOS SoCs
3 *
4 * 8 ~ 10 channel, 10/12-bit ADC
5 *
6 * Copyright (C) 2013 Naveen Krishna Chatradhi <ch.naveen@samsung.com>
7 *
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.
12 *
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.
17 *
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.
21 */
22
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/interrupt.h>
26#include <linux/delay.h>
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +010027#include <linux/errno.h>
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +000028#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>
Sachin Kamatebeb0212013-07-22 12:02:00 +010037#include <linux/err.h>
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +000038
39#include <linux/iio/iio.h>
40#include <linux/iio/machine.h>
41#include <linux/iio/driver.h>
Naveen Krishna Chatradhifafb37c2014-09-16 09:58:00 +010042#include <linux/mfd/syscon.h>
43#include <linux/regmap.h>
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +000044
Arnd Bergmann249535d2014-07-28 13:44:00 +010045/* S3C/EXYNOS4412/5250 ADC_V1 registers definitions */
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +000046#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)
51
Chanwoo Choi145b0a52014-07-28 13:44:00 +010052/* S3C2410 ADC registers definitions */
53#define ADC_S3C2410_MUX(x) ((x) + 0x18)
54
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +000055/* 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)
62
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)
68
Arnd Bergmann249535d2014-07-28 13:44:00 +010069/* Bit definitions for S3C2410 ADC */
70#define ADC_S3C2410_CON_SELMUX(x) (((x) & 7) << 3)
Chanwoo Choi145b0a52014-07-28 13:44:00 +010071#define ADC_S3C2410_DATX_MASK 0x3FF
72#define ADC_S3C2416_CON_RES_SEL (1u << 3)
Arnd Bergmann249535d2014-07-28 13:44:00 +010073
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +000074/* Bit definitions for ADC_V2 */
75#define ADC_V2_CON1_SOFT_RESET (1u << 2)
76
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
83
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +010084#define MAX_ADC_V2_CHANNELS 10
85#define MAX_ADC_V1_CHANNELS 8
86#define MAX_EXYNOS3250_ADC_CHANNELS 2
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +000087
88/* Bit definitions common for ADC_V1 and ADC_V2 */
89#define ADC_CON_EN_START (1u << 0)
Chanwoo Choi145b0a52014-07-28 13:44:00 +010090#define ADC_CON_EN_START_MASK (0x3 << 0)
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +000091#define ADC_DATX_MASK 0xFFF
92
Naveen Krishna Chatradhic780a8c2014-04-30 10:26:00 +010093#define EXYNOS_ADC_TIMEOUT (msecs_to_jiffies(100))
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +000094
Naveen Krishna Chatradhifafb37c2014-09-16 09:58:00 +010095#define EXYNOS_ADCV1_PHY_OFFSET 0x0718
96#define EXYNOS_ADCV2_PHY_OFFSET 0x0720
97
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +000098struct exynos_adc {
Chanwoo Choie49d99e2014-07-22 03:04:00 +010099 struct exynos_adc_data *data;
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100100 struct device *dev;
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000101 void __iomem *regs;
Naveen Krishna Chatradhifafb37c2014-09-16 09:58:00 +0100102 struct regmap *pmu_map;
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000103 struct clk *clk;
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100104 struct clk *sclk;
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000105 unsigned int irq;
106 struct regulator *vdd;
107
108 struct completion completion;
109
110 u32 value;
111 unsigned int version;
112};
113
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100114struct exynos_adc_data {
115 int num_channels;
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100116 bool needs_sclk;
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100117 bool needs_adc_phy;
Naveen Krishna Chatradhifafb37c2014-09-16 09:58:00 +0100118 int phy_offset;
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100119 u32 mask;
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100120
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);
125};
126
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100127static void exynos_adc_unprepare_clk(struct exynos_adc *info)
128{
129 if (info->data->needs_sclk)
130 clk_unprepare(info->sclk);
131 clk_unprepare(info->clk);
132}
133
134static int exynos_adc_prepare_clk(struct exynos_adc *info)
135{
136 int ret;
137
138 ret = clk_prepare(info->clk);
139 if (ret) {
140 dev_err(info->dev, "failed preparing adc clock: %d\n", ret);
141 return ret;
142 }
143
144 if (info->data->needs_sclk) {
145 ret = clk_prepare(info->sclk);
146 if (ret) {
147 clk_unprepare(info->clk);
148 dev_err(info->dev,
149 "failed preparing sclk_adc clock: %d\n", ret);
150 return ret;
151 }
152 }
153
154 return 0;
155}
156
157static void exynos_adc_disable_clk(struct exynos_adc *info)
158{
159 if (info->data->needs_sclk)
160 clk_disable(info->sclk);
161 clk_disable(info->clk);
162}
163
164static int exynos_adc_enable_clk(struct exynos_adc *info)
165{
166 int ret;
167
168 ret = clk_enable(info->clk);
169 if (ret) {
170 dev_err(info->dev, "failed enabling adc clock: %d\n", ret);
171 return ret;
172 }
173
174 if (info->data->needs_sclk) {
175 ret = clk_enable(info->sclk);
176 if (ret) {
177 clk_disable(info->clk);
178 dev_err(info->dev,
179 "failed enabling sclk_adc clock: %d\n", ret);
180 return ret;
181 }
182 }
183
184 return 0;
185}
186
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100187static void exynos_adc_v1_init_hw(struct exynos_adc *info)
188{
189 u32 con1;
190
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100191 if (info->data->needs_adc_phy)
Naveen Krishna Chatradhifafb37c2014-09-16 09:58:00 +0100192 regmap_write(info->pmu_map, info->data->phy_offset, 1);
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100193
194 /* set default prescaler values and Enable prescaler */
195 con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN;
196
197 /* Enable 12-bit ADC resolution */
198 con1 |= ADC_V1_CON_RES;
199 writel(con1, ADC_V1_CON(info->regs));
200}
201
202static void exynos_adc_v1_exit_hw(struct exynos_adc *info)
203{
204 u32 con;
205
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100206 if (info->data->needs_adc_phy)
Naveen Krishna Chatradhifafb37c2014-09-16 09:58:00 +0100207 regmap_write(info->pmu_map, info->data->phy_offset, 0);
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100208
209 con = readl(ADC_V1_CON(info->regs));
210 con |= ADC_V1_CON_STANDBY;
211 writel(con, ADC_V1_CON(info->regs));
212}
213
214static void exynos_adc_v1_clear_irq(struct exynos_adc *info)
215{
216 writel(1, ADC_V1_INTCLR(info->regs));
217}
218
219static void exynos_adc_v1_start_conv(struct exynos_adc *info,
220 unsigned long addr)
221{
222 u32 con1;
223
224 writel(addr, ADC_V1_MUX(info->regs));
225
226 con1 = readl(ADC_V1_CON(info->regs));
227 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
228}
229
230static const struct exynos_adc_data exynos_adc_v1_data = {
231 .num_channels = MAX_ADC_V1_CHANNELS,
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100232 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
233 .needs_adc_phy = true,
Naveen Krishna Chatradhifafb37c2014-09-16 09:58:00 +0100234 .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100235
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,
240};
241
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100242static void exynos_adc_s3c2416_start_conv(struct exynos_adc *info,
243 unsigned long addr)
244{
245 u32 con1;
246
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));
251
252 /* Select channel for S3C2416 */
253 writel(addr, ADC_S3C2410_MUX(info->regs));
254
255 con1 = readl(ADC_V1_CON(info->regs));
256 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
257}
258
259static 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 */
262
263 .init_hw = exynos_adc_v1_init_hw,
264 .exit_hw = exynos_adc_v1_exit_hw,
265 .start_conv = exynos_adc_s3c2416_start_conv,
266};
267
268static void exynos_adc_s3c2443_start_conv(struct exynos_adc *info,
269 unsigned long addr)
270{
271 u32 con1;
272
273 /* Select channel for S3C2433 */
274 writel(addr, ADC_S3C2410_MUX(info->regs));
275
276 con1 = readl(ADC_V1_CON(info->regs));
277 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
278}
279
280static 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 */
283
284 .init_hw = exynos_adc_v1_init_hw,
285 .exit_hw = exynos_adc_v1_exit_hw,
286 .start_conv = exynos_adc_s3c2443_start_conv,
287};
288
Arnd Bergmann249535d2014-07-28 13:44:00 +0100289static void exynos_adc_s3c64xx_start_conv(struct exynos_adc *info,
290 unsigned long addr)
291{
292 u32 con1;
293
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));
298}
299
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100300static 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 */
303
304 .init_hw = exynos_adc_v1_init_hw,
305 .exit_hw = exynos_adc_v1_exit_hw,
306 .start_conv = exynos_adc_s3c64xx_start_conv,
307};
308
Arnd Bergmann249535d2014-07-28 13:44:00 +0100309static struct exynos_adc_data const exynos_adc_s3c64xx_data = {
310 .num_channels = MAX_ADC_V1_CHANNELS,
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100311 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
Arnd Bergmann249535d2014-07-28 13:44:00 +0100312
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,
317};
318
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100319static void exynos_adc_v2_init_hw(struct exynos_adc *info)
320{
321 u32 con1, con2;
322
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100323 if (info->data->needs_adc_phy)
Naveen Krishna Chatradhifafb37c2014-09-16 09:58:00 +0100324 regmap_write(info->pmu_map, info->data->phy_offset, 1);
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100325
326 con1 = ADC_V2_CON1_SOFT_RESET;
327 writel(con1, ADC_V2_CON1(info->regs));
328
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));
332
333 /* Enable interrupts */
334 writel(1, ADC_V2_INT_EN(info->regs));
335}
336
337static void exynos_adc_v2_exit_hw(struct exynos_adc *info)
338{
339 u32 con;
340
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100341 if (info->data->needs_adc_phy)
Naveen Krishna Chatradhifafb37c2014-09-16 09:58:00 +0100342 regmap_write(info->pmu_map, info->data->phy_offset, 0);
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100343
344 con = readl(ADC_V2_CON1(info->regs));
345 con &= ~ADC_CON_EN_START;
346 writel(con, ADC_V2_CON1(info->regs));
347}
348
349static void exynos_adc_v2_clear_irq(struct exynos_adc *info)
350{
351 writel(1, ADC_V2_INT_ST(info->regs));
352}
353
354static void exynos_adc_v2_start_conv(struct exynos_adc *info,
355 unsigned long addr)
356{
357 u32 con1, con2;
358
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));
363
364 con1 = readl(ADC_V2_CON1(info->regs));
365 writel(con1 | ADC_CON_EN_START, ADC_V2_CON1(info->regs));
366}
367
368static const struct exynos_adc_data exynos_adc_v2_data = {
369 .num_channels = MAX_ADC_V2_CHANNELS,
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100370 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
371 .needs_adc_phy = true,
Naveen Krishna Chatradhifafb37c2014-09-16 09:58:00 +0100372 .phy_offset = EXYNOS_ADCV2_PHY_OFFSET,
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100373
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,
378};
379
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100380static const struct exynos_adc_data exynos3250_adc_data = {
381 .num_channels = MAX_EXYNOS3250_ADC_CHANNELS,
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100382 .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100383 .needs_sclk = true,
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100384 .needs_adc_phy = true,
Naveen Krishna Chatradhifafb37c2014-09-16 09:58:00 +0100385 .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100386
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,
391};
392
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000393static const struct of_device_id exynos_adc_match[] = {
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100394 {
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100395 .compatible = "samsung,s3c2410-adc",
396 .data = &exynos_adc_s3c24xx_data,
397 }, {
398 .compatible = "samsung,s3c2416-adc",
399 .data = &exynos_adc_s3c2416_data,
400 }, {
401 .compatible = "samsung,s3c2440-adc",
402 .data = &exynos_adc_s3c24xx_data,
403 }, {
404 .compatible = "samsung,s3c2443-adc",
405 .data = &exynos_adc_s3c2443_data,
406 }, {
Arnd Bergmann249535d2014-07-28 13:44:00 +0100407 .compatible = "samsung,s3c6410-adc",
408 .data = &exynos_adc_s3c64xx_data,
409 }, {
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100410 .compatible = "samsung,exynos-adc-v1",
411 .data = &exynos_adc_v1_data,
412 }, {
413 .compatible = "samsung,exynos-adc-v2",
414 .data = &exynos_adc_v2_data,
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100415 }, {
416 .compatible = "samsung,exynos3250-adc",
417 .data = &exynos3250_adc_data,
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100418 },
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000419 {},
420};
421MODULE_DEVICE_TABLE(of, exynos_adc_match);
422
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100423static struct exynos_adc_data *exynos_adc_get_data(struct platform_device *pdev)
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000424{
425 const struct of_device_id *match;
426
427 match = of_match_node(exynos_adc_match, pdev->dev.of_node);
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100428 return (struct exynos_adc_data *)match->data;
Naveen Krishna Chatradhidd2723f2014-04-30 10:26:00 +0100429}
430
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000431static int exynos_read_raw(struct iio_dev *indio_dev,
432 struct iio_chan_spec const *chan,
433 int *val,
434 int *val2,
435 long mask)
436{
437 struct exynos_adc *info = iio_priv(indio_dev);
438 unsigned long timeout;
Naveen Krishna Chatradhic780a8c2014-04-30 10:26:00 +0100439 int ret;
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000440
441 if (mask != IIO_CHAN_INFO_RAW)
442 return -EINVAL;
443
444 mutex_lock(&indio_dev->mlock);
Naveen Krishna Chatradhi6442d942014-04-30 10:26:00 +0100445 reinit_completion(&info->completion);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000446
447 /* Select the channel to be used and Trigger conversion */
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100448 if (info->data->start_conv)
449 info->data->start_conv(info, chan->address);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000450
Naveen Krishna Chatradhic780a8c2014-04-30 10:26:00 +0100451 timeout = wait_for_completion_timeout
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000452 (&info->completion, EXYNOS_ADC_TIMEOUT);
Naveen Krishna Chatradhic780a8c2014-04-30 10:26:00 +0100453 if (timeout == 0) {
Naveen Krishna Chatradhidd2723f2014-04-30 10:26:00 +0100454 dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n");
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100455 if (info->data->init_hw)
456 info->data->init_hw(info);
Naveen Krishna Chatradhic780a8c2014-04-30 10:26:00 +0100457 ret = -ETIMEDOUT;
458 } else {
459 *val = info->value;
460 *val2 = 0;
461 ret = IIO_VAL_INT;
462 }
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000463
464 mutex_unlock(&indio_dev->mlock);
465
Naveen Krishna Chatradhic780a8c2014-04-30 10:26:00 +0100466 return ret;
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000467}
468
469static irqreturn_t exynos_adc_isr(int irq, void *dev_id)
470{
471 struct exynos_adc *info = (struct exynos_adc *)dev_id;
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100472 u32 mask = info->data->mask;
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000473
474 /* Read value */
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100475 info->value = readl(ADC_V1_DATX(info->regs)) & mask;
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100476
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000477 /* clear irq */
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100478 if (info->data->clear_irq)
479 info->data->clear_irq(info);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000480
481 complete(&info->completion);
482
483 return IRQ_HANDLED;
484}
485
486static int exynos_adc_reg_access(struct iio_dev *indio_dev,
487 unsigned reg, unsigned writeval,
488 unsigned *readval)
489{
490 struct exynos_adc *info = iio_priv(indio_dev);
491
492 if (readval == NULL)
493 return -EINVAL;
494
495 *readval = readl(info->regs + reg);
496
497 return 0;
498}
499
500static 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,
504};
505
506#define ADC_CHANNEL(_index, _id) { \
507 .type = IIO_VOLTAGE, \
508 .indexed = 1, \
509 .channel = _index, \
510 .address = _index, \
Jonathan Cameron0d23d322013-03-03 12:25:30 +0000511 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000512 .datasheet_name = _id, \
513}
514
515static 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"),
526};
527
528static int exynos_adc_remove_devices(struct device *dev, void *c)
529{
530 struct platform_device *pdev = to_platform_device(dev);
531
532 platform_device_unregister(pdev);
533
534 return 0;
535}
536
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000537static int exynos_adc_probe(struct platform_device *pdev)
538{
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;
543 int ret = -ENODEV;
544 int irq;
545
546 if (!np)
547 return ret;
548
Sachin Kamatebeb0212013-07-22 12:02:00 +0100549 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct exynos_adc));
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000550 if (!indio_dev) {
551 dev_err(&pdev->dev, "failed allocating iio device\n");
552 return -ENOMEM;
553 }
554
555 info = iio_priv(indio_dev);
556
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100557 info->data = exynos_adc_get_data(pdev);
558 if (!info->data) {
559 dev_err(&pdev->dev, "failed getting exynos_adc_data\n");
560 return -EINVAL;
561 }
562
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000563 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sachin Kamatc6196532013-04-03 07:23:00 +0100564 info->regs = devm_ioremap_resource(&pdev->dev, mem);
Sachin Kamatebeb0212013-07-22 12:02:00 +0100565 if (IS_ERR(info->regs))
566 return PTR_ERR(info->regs);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000567
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100568
569 if (info->data->needs_adc_phy) {
Naveen Krishna Chatradhifafb37c2014-09-16 09:58:00 +0100570 info->pmu_map = syscon_regmap_lookup_by_phandle(
571 pdev->dev.of_node,
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);
576 }
Chanwoo Choi145b0a52014-07-28 13:44:00 +0100577 }
Doug Andersonbb916eb2013-03-13 20:40:00 +0000578
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000579 irq = platform_get_irq(pdev, 0);
580 if (irq < 0) {
581 dev_err(&pdev->dev, "no irq resource?\n");
Sachin Kamatebeb0212013-07-22 12:02:00 +0100582 return irq;
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000583 }
584
585 info->irq = irq;
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100586 info->dev = &pdev->dev;
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000587
588 init_completion(&info->completion);
589
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000590 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",
593 PTR_ERR(info->clk));
Naveen Krishna Ch2bbc7242014-04-30 10:26:00 +0100594 return PTR_ERR(info->clk);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000595 }
596
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100597 if (info->data->needs_sclk) {
598 info->sclk = devm_clk_get(&pdev->dev, "sclk");
599 if (IS_ERR(info->sclk)) {
600 dev_err(&pdev->dev,
601 "failed getting sclk clock, err = %ld\n",
602 PTR_ERR(info->sclk));
603 return PTR_ERR(info->sclk);
604 }
605 }
606
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000607 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",
610 PTR_ERR(info->vdd));
Naveen Krishna Ch2bbc7242014-04-30 10:26:00 +0100611 return PTR_ERR(info->vdd);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000612 }
613
Naveen Krishna Ch2bbc7242014-04-30 10:26:00 +0100614 ret = regulator_enable(info->vdd);
615 if (ret)
616 return ret;
617
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100618 ret = exynos_adc_prepare_clk(info);
Naveen Krishna Ch2bbc7242014-04-30 10:26:00 +0100619 if (ret)
620 goto err_disable_reg;
621
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100622 ret = exynos_adc_enable_clk(info);
623 if (ret)
624 goto err_unprepare_clk;
625
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000626 platform_set_drvdata(pdev, indio_dev);
627
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;
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100634 indio_dev->num_channels = info->data->num_channels;
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000635
Naveen Krishna Ch2bbc7242014-04-30 10:26:00 +0100636 ret = request_irq(info->irq, exynos_adc_isr,
637 0, dev_name(&pdev->dev), info);
638 if (ret < 0) {
639 dev_err(&pdev->dev, "failed requesting irq, irq = %d\n",
640 info->irq);
641 goto err_disable_clk;
642 }
643
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000644 ret = iio_device_register(indio_dev);
645 if (ret)
646 goto err_irq;
647
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100648 if (info->data->init_hw)
649 info->data->init_hw(info);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000650
Naveen Krishna Ch3d821a12014-04-25 11:14:00 +0100651 ret = of_platform_populate(np, exynos_adc_match, NULL, &indio_dev->dev);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000652 if (ret < 0) {
653 dev_err(&pdev->dev, "failed adding child nodes\n");
654 goto err_of_populate;
655 }
656
657 return 0;
658
659err_of_populate:
Naveen Krishna Ch3d821a12014-04-25 11:14:00 +0100660 device_for_each_child(&indio_dev->dev, NULL,
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000661 exynos_adc_remove_devices);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000662 iio_device_unregister(indio_dev);
663err_irq:
664 free_irq(info->irq, info);
Naveen Krishna Ch2bbc7242014-04-30 10:26:00 +0100665err_disable_clk:
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100666 if (info->data->exit_hw)
667 info->data->exit_hw(info);
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100668 exynos_adc_disable_clk(info);
669err_unprepare_clk:
670 exynos_adc_unprepare_clk(info);
Naveen Krishna Ch2bbc7242014-04-30 10:26:00 +0100671err_disable_reg:
672 regulator_disable(info->vdd);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000673 return ret;
674}
675
676static int exynos_adc_remove(struct platform_device *pdev)
677{
678 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
679 struct exynos_adc *info = iio_priv(indio_dev);
680
Naveen Krishna Ch3d821a12014-04-25 11:14:00 +0100681 device_for_each_child(&indio_dev->dev, NULL,
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000682 exynos_adc_remove_devices);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000683 iio_device_unregister(indio_dev);
684 free_irq(info->irq, info);
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100685 if (info->data->exit_hw)
686 info->data->exit_hw(info);
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100687 exynos_adc_disable_clk(info);
688 exynos_adc_unprepare_clk(info);
Naveen Krishna Ch2bbc7242014-04-30 10:26:00 +0100689 regulator_disable(info->vdd);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000690
691 return 0;
692}
693
694#ifdef CONFIG_PM_SLEEP
695static int exynos_adc_suspend(struct device *dev)
696{
Naveen Krishna Chatradhi927b4dc2013-05-20 07:34:00 +0100697 struct iio_dev *indio_dev = dev_get_drvdata(dev);
698 struct exynos_adc *info = iio_priv(indio_dev);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000699
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100700 if (info->data->exit_hw)
701 info->data->exit_hw(info);
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100702 exynos_adc_disable_clk(info);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000703 regulator_disable(info->vdd);
704
705 return 0;
706}
707
708static int exynos_adc_resume(struct device *dev)
709{
Naveen Krishna Chatradhi927b4dc2013-05-20 07:34:00 +0100710 struct iio_dev *indio_dev = dev_get_drvdata(dev);
711 struct exynos_adc *info = iio_priv(indio_dev);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000712 int ret;
713
714 ret = regulator_enable(info->vdd);
715 if (ret)
716 return ret;
717
Chanwoo Choiadb4e3f2014-07-22 03:04:00 +0100718 ret = exynos_adc_enable_clk(info);
Naveen Krishna Ch2bbc7242014-04-30 10:26:00 +0100719 if (ret)
720 return ret;
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000721
Chanwoo Choie49d99e2014-07-22 03:04:00 +0100722 if (info->data->init_hw)
723 info->data->init_hw(info);
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000724
725 return 0;
726}
727#endif
728
729static SIMPLE_DEV_PM_OPS(exynos_adc_pm_ops,
730 exynos_adc_suspend,
731 exynos_adc_resume);
732
733static struct platform_driver exynos_adc_driver = {
734 .probe = exynos_adc_probe,
735 .remove = exynos_adc_remove,
736 .driver = {
737 .name = "exynos-adc",
Sachin Kamat1ba06862013-03-26 09:42:00 +0000738 .of_match_table = exynos_adc_match,
Naveen Krishna Chatradhi10f5b142013-02-15 06:56:00 +0000739 .pm = &exynos_adc_pm_ops,
740 },
741};
742
743module_platform_driver(exynos_adc_driver);
744
745MODULE_AUTHOR("Naveen Krishna Chatradhi <ch.naveen@samsung.com>");
746MODULE_DESCRIPTION("Samsung EXYNOS5 ADC driver");
747MODULE_LICENSE("GPL v2");