• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only
2  * Based on drivers/input/sensor/sunxi_gpadc.c
3  *
4  * Copyright (C) 2016 Allwinner.
5  * fuzhaoke <fuzhaoke@allwinnertech.com>
6  *
7  * SUNXI GPADC Controller Driver
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  */
14 
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/input.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/interrupt.h>
21 #include <linux/ioport.h>
22 #include <asm/irq.h>
23 #include <asm/io.h>
24 #include <linux/timer.h>
25 #include <linux/clk.h>
26 #include <linux/irq.h>
27 #include <linux/of_platform.h>
28 #include <linux/of_irq.h>
29 #include <linux/of_address.h>
30 #include <linux/pm.h>
31 #include <linux/pm_wakeirq.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/workqueue.h>
34 #include "sunxi_gpadc.h"
35 #include <linux/sched.h>
36 #include <linux/clkdev.h>
37 #include <linux/clk-provider.h>
38 #include <linux/reset.h>
39 
40 #if IS_ENABLED(CONFIG_IIO)
41 #include <linux/iio/iio.h>
42 #include <linux/iio/machine.h>
43 #include <linux/iio/driver.h>
44 #include <linux/regmap.h>
45 #endif
46 
47 static u32 debug_mask = 1;
48 #define dprintk(level_mask, fmt, ...)				\
49 do {								\
50 	if (unlikely(debug_mask & level_mask))			\
51 		pr_info(fmt, ##__VA_ARGS__);	\
52 } while (0)
53 
54 static struct sunxi_gpadc *sunxi_gpadc;
55 static struct status_reg  status_para;
56 static struct vol_reg     vol_para;
57 static struct sr_reg      sr_para;
58 static struct filter_reg  filter_para;
59 static struct channel_reg  channel_para;
60 u32 key_vol[VOL_NUM];
61 u8 filter_cnt = 5;
62 u8 channel;
63 void __iomem *vaddr;
64 
65 static unsigned char keypad_mapindex[128] = {
66 	0, 0, 0, 0, 0, 0, 0, 0, 0,		/* key 1, 0-8 */
67 	1, 1, 1, 1, 1,				/* key 2, 9-13 */
68 	2, 2, 2, 2, 2, 2,			/* key 3, 14-19 */
69 	3, 3, 3, 3, 3, 3,			/* key 4, 20-25 */
70 	4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,	/* key 5, 26-36 */
71 	5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,	/* key 6, 37-39 */
72 	6, 6, 6, 6, 6, 6, 6, 6, 6,		/* key 7, 40-49 */
73 	7, 7, 7, 7, 7, 7, 7			/* key 8, 50-63 */
74 };
75 
sunxi_gpadc_save_regs(struct sunxi_gpadc * sunxi_gpadc)76 static inline void sunxi_gpadc_save_regs(struct sunxi_gpadc *sunxi_gpadc)
77 {
78 	int i;
79 
80 	for (i = 0; i < ARRAY_SIZE(sunxi_gpadc_regs_offset); i++)
81 		sunxi_gpadc->regs_backup[i] = readl(sunxi_gpadc->reg_base + sunxi_gpadc_regs_offset[i]);
82 }
83 
sunxi_gpadc_restore_regs(struct sunxi_gpadc * sunxi_gpadc)84 static inline void sunxi_gpadc_restore_regs(struct sunxi_gpadc *sunxi_gpadc)
85 {
86 	int i;
87 
88 	for (i = 0; i < ARRAY_SIZE(sunxi_gpadc_regs_offset); i++)
89 		writel(sunxi_gpadc->regs_backup[i], sunxi_gpadc->reg_base + sunxi_gpadc_regs_offset[i]);
90 }
91 
92 #ifdef CONFIG_ARCH_SUN8IW18
sunxi_gpadc_check_vin(void)93 static u32 sunxi_gpadc_check_vin(void)
94 {
95 	u32 reg_val = 0, ldoa;
96 
97 	reg_val = readl(vaddr);
98 
99 	if (reg_val > 0) {
100 		if ((reg_val & 0x80) == 0x80)
101 			ldoa = (reg_val & 0x7f) + 1800;
102 		else
103 			ldoa = (reg_val & 0x7f) + 1700;
104 	} else
105 		return 0;
106 	return ldoa;
107 }
108 #endif
109 
sunxi_gpadc_sample_rate_read(void __iomem * reg_base,u32 clk_in)110 u32 sunxi_gpadc_sample_rate_read(void __iomem *reg_base, u32 clk_in)
111 {
112 	u32 div, round_clk;
113 
114 	div = readl(reg_base + GP_SR_REG);
115 	div = div >> 16;
116 	round_clk = clk_in / (div + 1);
117 //	round_clk = (div + 1) / clk_in;
118 
119 	return round_clk;
120 }
121 
122 
123 /* clk_in: source clock, round_clk: sample rate */
sunxi_gpadc_sample_rate_set(void __iomem * reg_base,u32 clk_in,u32 round_clk)124 void sunxi_gpadc_sample_rate_set(void __iomem *reg_base, u32 clk_in,
125 		u32 round_clk)
126 {
127 	u32 div, reg_val;
128 
129 	if (round_clk > clk_in)
130 		pr_err("%s, invalid round clk!\n", __func__);
131 	div = clk_in / round_clk - 1;
132 	reg_val = readl(reg_base + GP_SR_REG);
133 	reg_val &= ~GP_SR_CON;
134 	reg_val |= (div << 16);
135 	writel(reg_val, reg_base + GP_SR_REG);
136 }
137 
sunxi_gpadc_ctrl_set(void __iomem * reg_base,u32 ctrl_para)138 void sunxi_gpadc_ctrl_set(void __iomem *reg_base, u32 ctrl_para)
139 {
140 	u32 reg_val;
141 
142 	reg_val = readl(reg_base + GP_CTRL_REG);
143 	reg_val |= ctrl_para;
144 	writel(reg_val, reg_base + GP_CTRL_REG);
145 }
146 
sunxi_gpadc_ch_select(void __iomem * reg_base,enum gp_channel_id id)147 static u32 sunxi_gpadc_ch_select(void __iomem *reg_base, enum gp_channel_id id)
148 {
149 	u32 reg_val;
150 
151 	reg_val = readl(reg_base + GP_CS_EN_REG);
152 	switch (id) {
153 	case GP_CH_0:
154 		reg_val |= GP_CH0_SELECT;
155 		break;
156 	case GP_CH_1:
157 		reg_val |= GP_CH1_SELECT;
158 		break;
159 	case GP_CH_2:
160 		reg_val |= GP_CH2_SELECT;
161 		break;
162 	case GP_CH_3:
163 		reg_val |= GP_CH3_SELECT;
164 		break;
165 	case GP_CH_4:
166 		reg_val |= GP_CH4_SELECT;
167 		break;
168 	case GP_CH_5:
169 		reg_val |= GP_CH5_SELECT;
170 		break;
171 	case GP_CH_6:
172 		reg_val |= GP_CH6_SELECT;
173 		break;
174 	case GP_CH_7:
175 		reg_val |= GP_CH7_SELECT;
176 		break;
177 	default:
178 		pr_err("%s, invalid channel id!", __func__);
179 		return -EINVAL;
180 	}
181 	writel(reg_val, reg_base + GP_CS_EN_REG);
182 
183 	return 0;
184 }
185 
sunxi_gpadc_ch_deselect(void __iomem * reg_base,enum gp_channel_id id)186 static u32 sunxi_gpadc_ch_deselect(void __iomem *reg_base,
187 		enum gp_channel_id id)
188 {
189 	u32 reg_val;
190 
191 	reg_val = readl(reg_base + GP_CS_EN_REG);
192 	switch (id) {
193 	case GP_CH_0:
194 		reg_val &= ~GP_CH0_SELECT;
195 		break;
196 	case GP_CH_1:
197 		reg_val &= ~GP_CH1_SELECT;
198 		break;
199 	case GP_CH_2:
200 		reg_val &= ~GP_CH2_SELECT;
201 		break;
202 	case GP_CH_3:
203 		reg_val &= ~GP_CH3_SELECT;
204 		break;
205 	case GP_CH_4:
206 		reg_val &= ~GP_CH4_SELECT;
207 		break;
208 	case GP_CH_5:
209 		reg_val &= ~GP_CH5_SELECT;
210 		break;
211 	case GP_CH_6:
212 		reg_val &= ~GP_CH6_SELECT;
213 		break;
214 	case GP_CH_7:
215 		reg_val &= ~GP_CH7_SELECT;
216 		break;
217 	default:
218 		pr_err("%s, invalid channel id!", __func__);
219 		return -EINVAL;
220 	}
221 	writel(reg_val, reg_base + GP_CS_EN_REG);
222 
223 	return 0;
224 }
225 
sunxi_gpadc_ch_cmp_low(void __iomem * reg_base,enum gp_channel_id id,u32 low_uv)226 static u32 sunxi_gpadc_ch_cmp_low(void __iomem *reg_base, enum gp_channel_id id,
227 							u32 low_uv)
228 {
229 	u32 reg_val = 0, low = 0, unit = 0;
230 
231 	/* analog voltage range 0~1.8v, 12bits sample rate, unit=1.8v/(2^12) */
232 	unit = VOL_RANGE / 4096; /* 12bits sample rate */
233 	low = low_uv / unit;
234 	if (low > VOL_VALUE_MASK)
235 		low = VOL_VALUE_MASK;
236 
237 	switch (id) {
238 	case GP_CH_0:
239 		reg_val = readl(reg_base + GP_CH0_CMP_DATA_REG);
240 		reg_val &= ~VOL_VALUE_MASK;
241 		reg_val |= (low & VOL_VALUE_MASK);
242 		writel(reg_val, reg_base + GP_CH0_CMP_DATA_REG);
243 		break;
244 	case GP_CH_1:
245 		reg_val = readl(reg_base + GP_CH1_CMP_DATA_REG);
246 		reg_val &= ~VOL_VALUE_MASK;
247 		reg_val |= (low & VOL_VALUE_MASK);
248 		writel(reg_val, reg_base + GP_CH1_CMP_DATA_REG);
249 		break;
250 	case GP_CH_2:
251 		reg_val = readl(reg_base + GP_CH2_CMP_DATA_REG);
252 		reg_val &= ~VOL_VALUE_MASK;
253 		reg_val |= (low & VOL_VALUE_MASK);
254 		writel(reg_val, reg_base + GP_CH2_CMP_DATA_REG);
255 		break;
256 	case GP_CH_3:
257 		reg_val = readl(reg_base + GP_CH3_CMP_DATA_REG);
258 		reg_val &= ~VOL_VALUE_MASK;
259 		reg_val |= (low & VOL_VALUE_MASK);
260 		writel(reg_val, reg_base + GP_CH3_CMP_DATA_REG);
261 		break;
262 	case GP_CH_4:
263 		reg_val = readl(reg_base + GP_CH4_CMP_DATA_REG);
264 		reg_val &= ~VOL_VALUE_MASK;
265 		reg_val |= (low & VOL_VALUE_MASK);
266 		writel(reg_val, reg_base + GP_CH4_CMP_DATA_REG);
267 		break;
268 	case GP_CH_5:
269 		reg_val = readl(reg_base + GP_CH5_CMP_DATA_REG);
270 		reg_val &= ~VOL_VALUE_MASK;
271 		reg_val |= (low & VOL_VALUE_MASK);
272 		writel(reg_val, reg_base + GP_CH5_CMP_DATA_REG);
273 		break;
274 	case GP_CH_6:
275 		reg_val = readl(reg_base + GP_CH6_CMP_DATA_REG);
276 		reg_val &= ~VOL_VALUE_MASK;
277 		reg_val |= (low & VOL_VALUE_MASK);
278 		writel(reg_val, reg_base + GP_CH6_CMP_DATA_REG);
279 		break;
280 	case GP_CH_7:
281 		reg_val = readl(reg_base + GP_CH7_CMP_DATA_REG);
282 		reg_val &= ~VOL_VALUE_MASK;
283 		reg_val |= (low & VOL_VALUE_MASK);
284 		writel(reg_val, reg_base + GP_CH7_CMP_DATA_REG);
285 		break;
286 
287 	default:
288 		pr_err("%s, invalid channel id!", __func__);
289 		return -EINVAL;
290 	}
291 
292 	return 0;
293 }
sunxi_gpadc_ch_cmp_hig(void __iomem * reg_base,enum gp_channel_id id,u32 hig_uv)294 static u32 sunxi_gpadc_ch_cmp_hig(void __iomem *reg_base, enum gp_channel_id id,
295 								u32 hig_uv)
296 {
297 	u32 reg_val = 0, hig = 0, unit = 0;
298 
299 	/* anolog voltage range 0~1.8v, 12bits sample rate, unit=1.8v/(2^12) */
300 	unit = VOL_RANGE / 4096; /* 12bits sample rate */
301 	hig = hig_uv / unit;
302 	if (hig > VOL_VALUE_MASK)
303 		hig = VOL_VALUE_MASK;
304 
305 	switch (id) {
306 	case GP_CH_0:
307 		reg_val = readl(reg_base + GP_CH0_CMP_DATA_REG);
308 		reg_val &= ~(VOL_VALUE_MASK << 16);
309 		reg_val |= (hig & VOL_VALUE_MASK) << 16;
310 		writel(reg_val, reg_base + GP_CH0_CMP_DATA_REG);
311 		break;
312 	case GP_CH_1:
313 		reg_val = readl(reg_base + GP_CH1_CMP_DATA_REG);
314 		reg_val &= ~(VOL_VALUE_MASK << 16);
315 		reg_val |= (hig & VOL_VALUE_MASK) << 16;
316 		writel(reg_val, reg_base + GP_CH1_CMP_DATA_REG);
317 		break;
318 	case GP_CH_2:
319 		reg_val = readl(reg_base + GP_CH2_CMP_DATA_REG);
320 		reg_val &= ~(VOL_VALUE_MASK << 16);
321 		reg_val |= (hig & VOL_VALUE_MASK) << 16;
322 		writel(reg_val, reg_base + GP_CH2_CMP_DATA_REG);
323 		break;
324 	case GP_CH_3:
325 		reg_val = readl(reg_base + GP_CH3_CMP_DATA_REG);
326 		reg_val &= ~(VOL_VALUE_MASK << 16);
327 		reg_val |= (hig & VOL_VALUE_MASK) << 16;
328 		writel(reg_val, reg_base + GP_CH3_CMP_DATA_REG);
329 		break;
330 	case GP_CH_4:
331 		reg_val = readl(reg_base + GP_CH4_CMP_DATA_REG);
332 		reg_val &= ~(VOL_VALUE_MASK << 16);
333 		reg_val |= (hig & VOL_VALUE_MASK) << 16;
334 		writel(reg_val, reg_base + GP_CH4_CMP_DATA_REG);
335 		break;
336 	case GP_CH_5:
337 		reg_val = readl(reg_base + GP_CH5_CMP_DATA_REG);
338 		reg_val &= ~(VOL_VALUE_MASK << 16);
339 		reg_val |= (hig & VOL_VALUE_MASK) << 16;
340 		writel(reg_val, reg_base + GP_CH5_CMP_DATA_REG);
341 		break;
342 	case GP_CH_6:
343 		reg_val = readl(reg_base + GP_CH6_CMP_DATA_REG);
344 		reg_val &= ~(VOL_VALUE_MASK << 16);
345 		reg_val |= (hig & VOL_VALUE_MASK) << 16;
346 		writel(reg_val, reg_base + GP_CH6_CMP_DATA_REG);
347 		break;
348 	case GP_CH_7:
349 		reg_val = readl(reg_base + GP_CH7_CMP_DATA_REG);
350 		reg_val &= ~(VOL_VALUE_MASK << 16);
351 		reg_val |= (hig & VOL_VALUE_MASK) << 16;
352 		writel(reg_val, reg_base + GP_CH7_CMP_DATA_REG);
353 		break;
354 
355 	default:
356 		pr_err("%s, invalid channel id!", __func__);
357 		return -EINVAL;
358 	}
359 
360 	return 0;
361 }
362 
sunxi_gpadc_cmp_select(void __iomem * reg_base,enum gp_channel_id id)363 static u32 sunxi_gpadc_cmp_select(void __iomem *reg_base, enum gp_channel_id id)
364 {
365 	u32 reg_val;
366 
367 	reg_val = readl(reg_base + GP_CS_EN_REG);
368 	switch (id) {
369 	case GP_CH_0:
370 		reg_val |= GP_CH0_CMP_EN;
371 		break;
372 	case GP_CH_1:
373 		reg_val |= GP_CH1_CMP_EN;
374 		break;
375 	case GP_CH_2:
376 		reg_val |= GP_CH2_CMP_EN;
377 		break;
378 	case GP_CH_3:
379 		reg_val |= GP_CH3_CMP_EN;
380 		break;
381 	case GP_CH_4:
382 		reg_val |= GP_CH4_CMP_EN;
383 		break;
384 	case GP_CH_5:
385 		reg_val |= GP_CH5_CMP_EN;
386 		break;
387 	case GP_CH_6:
388 		reg_val |= GP_CH6_CMP_EN;
389 		break;
390 	case GP_CH_7:
391 		reg_val |= GP_CH7_CMP_EN;
392 		break;
393 	default:
394 		pr_err("%s, invalid value!", __func__);
395 		return -EINVAL;
396 	}
397 	writel(reg_val, reg_base + GP_CS_EN_REG);
398 
399 	return 0;
400 }
401 
sunxi_gpadc_cmp_deselect(void __iomem * reg_base,enum gp_channel_id id)402 static u32 sunxi_gpadc_cmp_deselect(void __iomem *reg_base,
403 		enum gp_channel_id id)
404 {
405 	u32 reg_val;
406 
407 	reg_val = readl(reg_base + GP_CS_EN_REG);
408 	switch (id) {
409 	case GP_CH_0:
410 		reg_val &= ~GP_CH0_CMP_EN;
411 		break;
412 	case GP_CH_1:
413 		reg_val &= ~GP_CH1_CMP_EN;
414 		break;
415 	case GP_CH_2:
416 		reg_val &= ~GP_CH2_CMP_EN;
417 		break;
418 	case GP_CH_3:
419 		reg_val &= ~GP_CH3_CMP_EN;
420 		break;
421 	case GP_CH_4:
422 		reg_val &= ~GP_CH4_CMP_EN;
423 		break;
424 	case GP_CH_5:
425 		reg_val &= ~GP_CH5_CMP_EN;
426 		break;
427 	case GP_CH_6:
428 		reg_val &= ~GP_CH6_CMP_EN;
429 		break;
430 	case GP_CH_7:
431 		reg_val &= ~GP_CH7_CMP_EN;
432 		break;
433 
434 	default:
435 		pr_err("%s, invalid value!", __func__);
436 		return -EINVAL;
437 	}
438 	writel(reg_val, reg_base + GP_CS_EN_REG);
439 
440 	return 0;
441 }
442 
sunxi_gpadc_mode_select(void __iomem * reg_base,enum gp_select_mode mode)443 static u32 sunxi_gpadc_mode_select(void __iomem *reg_base,
444 		enum gp_select_mode mode)
445 {
446 	u32 reg_val;
447 
448 	reg_val = readl(reg_base + GP_CTRL_REG);
449 	reg_val &= ~GP_MODE_SELECT;
450 	reg_val |= (mode << 18);
451 	writel(reg_val, reg_base + GP_CTRL_REG);
452 
453 	return 0;
454 }
455 
456 /* enable gpadc function, true:enable, false:disable */
sunxi_gpadc_enable(void __iomem * reg_base,bool onoff)457 static void sunxi_gpadc_enable(void __iomem *reg_base, bool onoff)
458 {
459 	u32 reg_val = 0;
460 
461 	reg_val = readl(reg_base + GP_CTRL_REG);
462 	if (true == onoff)
463 		reg_val |= GP_ADC_EN;
464 	else
465 		reg_val &= ~GP_ADC_EN;
466 	writel(reg_val, reg_base + GP_CTRL_REG);
467 }
468 
sunxi_gpadc_calibration_enable(void __iomem * reg_base)469 static void sunxi_gpadc_calibration_enable(void __iomem *reg_base)
470 {
471 	u32 reg_val;
472 
473 	reg_val = readl(reg_base + GP_CTRL_REG);
474 	reg_val |= GP_CALIBRATION_ENABLE;
475 	writel(reg_val, reg_base + GP_CTRL_REG);
476 
477 }
478 
sunxi_enable_lowirq_ch_select(void __iomem * reg_base,enum gp_channel_id id)479 static u32 sunxi_enable_lowirq_ch_select(void __iomem *reg_base,
480 		enum gp_channel_id id)
481 {
482 	u32 reg_val;
483 
484 	reg_val = readl(reg_base + GP_DATAL_INTC_REG);
485 	switch (id) {
486 	case GP_CH_0:
487 		reg_val |= GP_CH0_SELECT;
488 		break;
489 	case GP_CH_1:
490 		reg_val |= GP_CH1_SELECT;
491 		break;
492 	case GP_CH_2:
493 		reg_val |= GP_CH2_SELECT;
494 		break;
495 	case GP_CH_3:
496 		reg_val |= GP_CH3_SELECT;
497 		break;
498 	case GP_CH_4:
499 		reg_val |= GP_CH4_SELECT;
500 		break;
501 	case GP_CH_5:
502 		reg_val |= GP_CH5_SELECT;
503 		break;
504 	case GP_CH_6:
505 		reg_val |= GP_CH6_SELECT;
506 		break;
507 	case GP_CH_7:
508 		reg_val |= GP_CH7_SELECT;
509 		break;
510 	default:
511 		pr_err("%s, invalid channel id!", __func__);
512 		return -EINVAL;
513 	}
514 	writel(reg_val, reg_base + GP_DATAL_INTC_REG);
515 
516 	return 0;
517 }
518 
sunxi_disable_lowirq_ch_select(void __iomem * reg_base,enum gp_channel_id id)519 static u32 sunxi_disable_lowirq_ch_select(void __iomem *reg_base,
520 		enum gp_channel_id id)
521 {
522 	u32 reg_val;
523 
524 	reg_val = readl(reg_base + GP_DATAL_INTC_REG);
525 	switch (id) {
526 	case GP_CH_0:
527 		reg_val &= ~GP_CH0_SELECT;
528 		break;
529 	case GP_CH_1:
530 		reg_val &= ~GP_CH1_SELECT;
531 		break;
532 	case GP_CH_2:
533 		reg_val &= ~GP_CH2_SELECT;
534 		break;
535 	case GP_CH_3:
536 		reg_val &= ~GP_CH3_SELECT;
537 		break;
538 	case GP_CH_4:
539 		reg_val &= ~GP_CH4_SELECT;
540 		break;
541 	case GP_CH_5:
542 		reg_val &= ~GP_CH5_SELECT;
543 		break;
544 	case GP_CH_6:
545 		reg_val &= ~GP_CH6_SELECT;
546 		break;
547 	case GP_CH_7:
548 		reg_val &= ~GP_CH7_SELECT;
549 		break;
550 	default:
551 		pr_err("%s, invalid channel id!", __func__);
552 		return -EINVAL;
553 	}
554 	writel(reg_val, reg_base + GP_DATAL_INTC_REG);
555 
556 	return 0;
557 }
558 
sunxi_enable_higirq_ch_select(void __iomem * reg_base,enum gp_channel_id id)559 static u32 sunxi_enable_higirq_ch_select(void __iomem *reg_base,
560 		enum gp_channel_id id)
561 {
562 	u32 reg_val = 0;
563 
564 
565 	reg_val = readl(reg_base + GP_DATAH_INTC_REG);
566 	switch (id) {
567 	case GP_CH_0:
568 		reg_val |= GP_CH0_SELECT;
569 		break;
570 	case GP_CH_1:
571 		reg_val |= GP_CH1_SELECT;
572 		break;
573 	case GP_CH_2:
574 		reg_val |= GP_CH2_SELECT;
575 		break;
576 	case GP_CH_3:
577 		reg_val |= GP_CH3_SELECT;
578 		break;
579 	case GP_CH_4:
580 		reg_val |= GP_CH4_SELECT;
581 		break;
582 	case GP_CH_5:
583 		reg_val |= GP_CH5_SELECT;
584 		break;
585 	case GP_CH_6:
586 		reg_val |= GP_CH6_SELECT;
587 		break;
588 	case GP_CH_7:
589 		reg_val |= GP_CH7_SELECT;
590 		break;
591 	default:
592 		pr_err("%s, invalid channel id!", __func__);
593 		return -EINVAL;
594 	}
595 	writel(reg_val, reg_base + GP_DATAH_INTC_REG);
596 
597 	return 0;
598 }
599 
sunxi_disable_higirq_ch_select(void __iomem * reg_base,enum gp_channel_id id)600 static u32 sunxi_disable_higirq_ch_select(void __iomem *reg_base,
601 		enum gp_channel_id id)
602 {
603 	u32 reg_val = 0;
604 
605 	reg_val = readl(reg_base + GP_DATAH_INTC_REG);
606 	switch (id) {
607 	case GP_CH_0:
608 		reg_val &= ~GP_CH0_SELECT;
609 		break;
610 	case GP_CH_1:
611 		reg_val &= ~GP_CH1_SELECT;
612 		break;
613 	case GP_CH_2:
614 		reg_val &= ~GP_CH2_SELECT;
615 		break;
616 	case GP_CH_3:
617 		reg_val &= ~GP_CH3_SELECT;
618 		break;
619 	case GP_CH_4:
620 		reg_val &= ~GP_CH4_SELECT;
621 		break;
622 	case GP_CH_5:
623 		reg_val &= ~GP_CH5_SELECT;
624 		break;
625 	case GP_CH_6:
626 		reg_val &= ~GP_CH6_SELECT;
627 		break;
628 	case GP_CH_7:
629 		reg_val &= ~GP_CH7_SELECT;
630 		break;
631 	default:
632 		pr_err("%s, invalid channel id!", __func__);
633 		return -EINVAL;
634 	}
635 	writel(reg_val, reg_base + GP_DATAH_INTC_REG);
636 
637 	return 0;
638 }
639 
sunxi_gpadc_channel_id(enum gp_channel_id id)640 static u32 sunxi_gpadc_channel_id(enum gp_channel_id id)
641 {
642 	u32 channel_id;
643 
644 	switch (id) {
645 	case GP_CH_0:
646 		channel_id = CHANNEL_0_SELECT;
647 		break;
648 	case GP_CH_1:
649 		channel_id = CHANNEL_1_SELECT;
650 		break;
651 	case GP_CH_2:
652 		channel_id = CHANNEL_2_SELECT;
653 		break;
654 	case GP_CH_3:
655 		channel_id = CHANNEL_3_SELECT;
656 		break;
657 	case GP_CH_4:
658 		channel_id = CHANNEL_4_SELECT;
659 		break;
660 	case GP_CH_5:
661 		channel_id = CHANNEL_5_SELECT;
662 		break;
663 	case GP_CH_6:
664 		channel_id = CHANNEL_6_SELECT;
665 		break;
666 	case GP_CH_7:
667 		channel_id = CHANNEL_7_SELECT;
668 		break;
669 	default:
670 		pr_err("%s,channel id select fail", __func__);
671 		return -EINVAL;
672 	}
673 
674 	return channel_id;
675 }
676 
sunxi_gpadc_enable_irq(void __iomem * reg_base)677 static void sunxi_gpadc_enable_irq(void __iomem *reg_base)
678 {
679 	u32 reg_val;
680 
681 	reg_val = readl(reg_base + GP_FIFO_INTC_REG);
682 	reg_val |= FIFO_DATA_IRQ_EN;
683 	writel(reg_val, reg_base + GP_FIFO_INTC_REG);
684 }
685 #if 0
686 static u32 sunxi_enable_irq_ch_select(void __iomem *reg_base,
687 		enum gp_channel_id id)
688 {
689 	u32 reg_val;
690 
691 	reg_val = readl(reg_base + GP_DATA_INTC_REG);
692 	switch (id) {
693 	case GP_CH_0:
694 		reg_val |= GP_CH0_SELECT;
695 		break;
696 	case GP_CH_1:
697 		reg_val |= GP_CH1_SELECT;
698 		break;
699 	case GP_CH_2:
700 		reg_val |= GP_CH2_SELECT;
701 		break;
702 	case GP_CH_3:
703 		reg_val |= GP_CH3_SELECT;
704 		break;
705 	case GP_CH_4:
706 		reg_val |= GP_CH4_SELECT;
707 		break;
708 	case GP_CH_5:
709 		reg_val |= GP_CH5_SELECT;
710 		break;
711 	case GP_CH_6:
712 		reg_val |= GP_CH6_SELECT;
713 		break;
714 	case GP_CH_7:
715 		reg_val |= GP_CH7_SELECT;
716 		break;
717 	default:
718 		pr_err("%s, invalid channel id!", __func__);
719 		return -EINVAL;
720 	}
721 	writel(reg_val, reg_base + GP_DATA_INTC_REG);
722 
723 	return 0;
724 }
725 
726 static u32 sunxi_disable_irq_ch_select(void __iomem *reg_base,
727 		enum gp_channel_id id)
728 {
729 	u32 reg_val;
730 
731 	reg_val = readl(reg_base + GP_DATA_INTC_REG);
732 	switch (id) {
733 	case GP_CH_0:
734 		reg_val &= ~GP_CH0_SELECT;
735 		break;
736 	case GP_CH_1:
737 		reg_val &= ~GP_CH1_SELECT;
738 		break;
739 	case GP_CH_2:
740 		reg_val &= ~GP_CH2_SELECT;
741 		break;
742 	case GP_CH_3:
743 		reg_val &= ~GP_CH3_SELECT;
744 		break;
745 	case GP_CH_4:
746 		reg_val &= ~GP_CH4_SELECT;
747 		break;
748 	case GP_CH_5:
749 		reg_val &= ~GP_CH5_SELECT;
750 		break;
751 	case GP_CH_6:
752 		reg_val &= ~GP_CH6_SELECT;
753 		break;
754 	case GP_CH_7:
755 		reg_val &= ~GP_CH7_SELECT;
756 		break;
757 	default:
758 		pr_err("%s, invalid channel id!", __func__);
759 		return -EINVAL;
760 	}
761 	writel(reg_val, reg_base + GP_DATA_INTC_REG);
762 
763 	return 0;
764 }
765 #endif
766 
sunxi_ch_lowirq_status(void __iomem * reg_base)767 static u32 sunxi_ch_lowirq_status(void __iomem *reg_base)
768 {
769 	return readl(reg_base + GP_DATAL_INTS_REG);
770 }
771 
sunxi_ch_lowirq_clear_flags(void __iomem * reg_base,u32 flags)772 static void sunxi_ch_lowirq_clear_flags(void __iomem *reg_base, u32 flags)
773 {
774 	writel(flags, reg_base + GP_DATAL_INTS_REG);
775 }
776 
sunxi_ch_higirq_status(void __iomem * reg_base)777 static u32 sunxi_ch_higirq_status(void __iomem *reg_base)
778 {
779 	return readl(reg_base + GP_DATAH_INTS_REG);
780 }
781 
sunxi_ch_higirq_clear_flags(void __iomem * reg_base,u32 flags)782 static void sunxi_ch_higirq_clear_flags(void __iomem *reg_base, u32 flags)
783 {
784 	writel(flags, reg_base + GP_DATAH_INTS_REG);
785 }
786 
sunxi_ch_irq_status(void __iomem * reg_base)787 static u32 sunxi_ch_irq_status(void __iomem *reg_base)
788 {
789 	return readl(reg_base + GP_DATA_INTS_REG);
790 }
791 
sunxi_ch_irq_clear_flags(void __iomem * reg_base,u32 flags)792 static void sunxi_ch_irq_clear_flags(void __iomem *reg_base, u32 flags)
793 {
794 	writel(flags, reg_base + GP_DATA_INTS_REG);
795 }
796 
sunxi_gpadc_read_ch_lowirq_enable(void __iomem * reg_base)797 static u32 sunxi_gpadc_read_ch_lowirq_enable(void __iomem *reg_base)
798 {
799 	return readl(reg_base + GP_DATAL_INTC_REG);
800 }
801 
sunxi_gpadc_read_ch_higirq_enable(void __iomem * reg_base)802 static u32 sunxi_gpadc_read_ch_higirq_enable(void __iomem *reg_base)
803 {
804 	return readl(reg_base + GP_DATAH_INTC_REG);
805 }
806 
sunxi_gpadc_read_ch_irq_enable(void __iomem * reg_base)807 static u32 sunxi_gpadc_read_ch_irq_enable(void __iomem *reg_base)
808 {
809 	return readl(reg_base + GP_DATA_INTC_REG);
810 }
811 
sunxi_gpadc_read_data(void __iomem * reg_base,enum gp_channel_id id)812 static u32 sunxi_gpadc_read_data(void __iomem *reg_base, enum gp_channel_id id)
813 {
814 	switch (id) {
815 	case GP_CH_0:
816 		return readl(reg_base + GP_CH0_DATA_REG) & GP_CH0_DATA_MASK;
817 	case GP_CH_1:
818 		return readl(reg_base + GP_CH1_DATA_REG) & GP_CH1_DATA_MASK;
819 	case GP_CH_2:
820 		return readl(reg_base + GP_CH2_DATA_REG) & GP_CH2_DATA_MASK;
821 	case GP_CH_3:
822 		return readl(reg_base + GP_CH3_DATA_REG) & GP_CH3_DATA_MASK;
823 	case GP_CH_4:
824 		return readl(reg_base + GP_CH4_DATA_REG) & GP_CH4_DATA_MASK;
825 	case GP_CH_5:
826 		return readl(reg_base + GP_CH5_DATA_REG) & GP_CH5_DATA_MASK;
827 	case GP_CH_6:
828 		return readl(reg_base + GP_CH6_DATA_REG) & GP_CH6_DATA_MASK;
829 	case GP_CH_7:
830 		return readl(reg_base + GP_CH7_DATA_REG) & GP_CH7_DATA_MASK;
831 	default:
832 		pr_err("%s, invalid channel id!", __func__);
833 		return -EINVAL;
834 	}
835 }
836 
sunxi_gpadc_input_event_set(struct input_dev * input_dev,enum gp_channel_id id)837 static int sunxi_gpadc_input_event_set(struct input_dev *input_dev,
838 		enum gp_channel_id id)
839 {
840 	int i;
841 
842 	if (!input_dev) {
843 		pr_err("%s:input_dev: not enough memory for input device\n",
844 				__func__);
845 		return -ENOMEM;
846 	}
847 
848 	switch (id) {
849 	case GP_CH_0:
850 #ifdef CONFIG_ARCH_SUN8IW18
851 	case GP_CH_3:
852 		input_dev->evbit[0] = BIT_MASK(EV_KEY);
853 #else
854 		input_dev->evbit[0] = BIT_MASK(EV_KEY)|BIT_MASK(EV_REP);
855 #endif
856 		for (i = 0; i < KEY_MAX_CNT; i++)
857 			set_bit(sunxi_gpadc->scankeycodes[i],
858 					input_dev->keybit);
859 		break;
860 
861 	case GP_CH_1:
862 	case GP_CH_2:
863 #ifndef CONFIG_ARCH_SUN8IW18
864 	case GP_CH_3:
865 #endif
866 	case GP_CH_4:
867 	case GP_CH_5:
868 	case GP_CH_6:
869 	case GP_CH_7:
870 		input_dev->evbit[0] = BIT_MASK(EV_MSC);
871 		set_bit(EV_MSC, input_dev->evbit);
872 		set_bit(MSC_SCAN, input_dev->mscbit);
873 		break;
874 	default:
875 		pr_err("%s, invalid channel id!", __func__);
876 		return -ENOMEM;
877 	}
878 
879 	return 0;
880 }
881 
sunxi_gpadc_input_register(struct sunxi_gpadc * sunxi_gpadc,int id)882 static int sunxi_gpadc_input_register(struct sunxi_gpadc *sunxi_gpadc, int id)
883 {
884 	static struct input_dev *input_dev;
885 	int ret;
886 
887 	input_dev = input_allocate_device();
888 	if (!input_dev) {
889 		pr_err("input_dev: not enough memory for input device\n");
890 		return -ENOMEM;
891 	}
892 
893 	switch (id) {
894 	case GP_CH_0:
895 		input_dev->name = "sunxi-gpadc0";
896 		input_dev->phys = "sunxigpadc0/input0";
897 		break;
898 	case GP_CH_1:
899 		input_dev->name = "sunxi-gpadc1";
900 		input_dev->phys = "sunxigpadc1/input0";
901 		break;
902 	case GP_CH_2:
903 		input_dev->name = "sunxi-gpadc2";
904 		input_dev->phys = "sunxigpadc2/input0";
905 		break;
906 	case GP_CH_3:
907 		input_dev->name = "sunxi-gpadc3";
908 		input_dev->phys = "sunxigpadc3/input0";
909 		break;
910 	case GP_CH_4:
911 		input_dev->name = "sunxi-gpadc4";
912 		input_dev->phys = "sunxigpadc4/input0";
913 		break;
914 	case GP_CH_5:
915 		input_dev->name = "sunxi-gpadc5";
916 		input_dev->phys = "sunxigpadc5/input0";
917 		break;
918 	case GP_CH_6:
919 		input_dev->name = "sunxi-gpadc6";
920 		input_dev->phys = "sunxigpadc6/input0";
921 		break;
922 	case GP_CH_7:
923 		input_dev->name = "sunxi-gpadc7";
924 		input_dev->phys = "sunxigpadc7/input0";
925 		break;
926 	default:
927 		pr_err("%s, invalid channel id!", __func__);
928 		goto fail;
929 	}
930 
931 	input_dev->id.bustype = BUS_HOST;
932 	input_dev->id.vendor = 0x0001;
933 	input_dev->id.product = 0x0001;
934 	input_dev->id.version = 0x0100;
935 
936 	sunxi_gpadc_input_event_set(input_dev, id);
937 	sunxi_gpadc->input_gpadc[id] = input_dev;
938 	ret = input_register_device(sunxi_gpadc->input_gpadc[id]);
939 	if (ret) {
940 		pr_err("input register fail\n");
941 		goto fail1;
942 	}
943 
944 	return 0;
945 
946 fail1:
947 	input_unregister_device(sunxi_gpadc->input_gpadc[id]);
948 fail:
949 	input_free_device(sunxi_gpadc->input_gpadc[id]);
950 	return ret;
951 }
952 
sunxi_key_init(struct sunxi_gpadc * sunxi_gpadc,struct platform_device * pdev)953 static int sunxi_key_init(struct sunxi_gpadc *sunxi_gpadc,
954 		struct platform_device *pdev)
955 {
956 	struct device_node *np = NULL;
957 	unsigned char i, j = 0;
958 	u32 vol, val;
959 	u32 set_vol[VOL_NUM];
960 
961 	np = pdev->dev.of_node;
962 	if (of_property_read_u32(np, "key_cnt", &sunxi_gpadc->key_num)) {
963 		pr_err("%s: get key count failed", __func__);
964 		return -EBUSY;
965 	}
966 	pr_debug("%s key number = %d.\n", __func__, sunxi_gpadc->key_num);
967 	if (sunxi_gpadc->key_num < 1 || sunxi_gpadc->key_num > VOL_NUM) {
968 		pr_err("incorrect key number.\n");
969 		return -1;
970 	}
971 	for (i = 0; i < sunxi_gpadc->key_num; i++) {
972 		sprintf(sunxi_gpadc->key_name, "key%d_vol", i);
973 		if (of_property_read_u32(np, sunxi_gpadc->key_name, &vol)) {
974 			pr_err("%s:get%s err!\n", __func__,
975 					sunxi_gpadc->key_name);
976 			return -EBUSY;
977 		}
978 		key_vol[i] = vol;
979 
980 		sprintf(sunxi_gpadc->key_name, "key%d_val", i);
981 		if (of_property_read_u32(np, sunxi_gpadc->key_name, &val)) {
982 			pr_err("%s:get%s err!\n", __func__,
983 					sunxi_gpadc->key_name);
984 			return -EBUSY;
985 		}
986 		sunxi_gpadc->scankeycodes[i] = val;
987 		pr_debug("%s: key%d vol= %d code= %d\n", __func__, i,
988 				key_vol[i], sunxi_gpadc->scankeycodes[i]);
989 	}
990 	key_vol[sunxi_gpadc->key_num] = MAXIMUM_INPUT_VOLTAGE;
991 
992 	for (i = 0; i < sunxi_gpadc->key_num; i++)
993 		set_vol[i] = key_vol[i] + (key_vol[i+1] - key_vol[i])/2;
994 
995 	for (i = 0; i < 128; i++) {
996 		if (i * SCALE_UNIT > set_vol[j])
997 			j++;
998 		keypad_mapindex[i] = j;
999 	}
1000 
1001 	return 0;
1002 }
1003 
sunxi_gpadc_input_register_setup(struct sunxi_gpadc * sunxi_gpadc)1004 static int sunxi_gpadc_input_register_setup(struct sunxi_gpadc *sunxi_gpadc)
1005 {
1006 	struct sunxi_config *config = NULL;
1007 	int i;
1008 
1009 	config = &sunxi_gpadc->gpadc_config;
1010 	for (i = 0; i < sunxi_gpadc->channel_num; i++) {
1011 		if (config->channel_select & sunxi_gpadc_channel_id(i))
1012 			sunxi_gpadc_input_register(sunxi_gpadc, i);
1013 	}
1014 
1015 	return 0;
1016 }
1017 
sunxi_gpadc_setup(struct platform_device * pdev,struct sunxi_gpadc * sunxi_gpadc)1018 static int sunxi_gpadc_setup(struct platform_device *pdev,
1019 					struct sunxi_gpadc *sunxi_gpadc)
1020 {
1021 	struct device_node *np = NULL;
1022 	struct sunxi_config *gpadc_config = &sunxi_gpadc->gpadc_config;
1023 	u32 val;
1024 	int i;
1025 	unsigned char name[30];
1026 
1027 	np = pdev->dev.of_node;
1028 	if (of_property_read_u32(np, "gpadc_sample_rate",
1029 				&sunxi_gpadc->gpadc_sample_rate)) {
1030 		pr_debug("%s: get sample rate failed\n", __func__);
1031 		sunxi_gpadc->gpadc_sample_rate = DEFAULT_SR;
1032 	} else {
1033 		if (sunxi_gpadc->gpadc_sample_rate < MIN_SR ||
1034 				sunxi_gpadc->gpadc_sample_rate > MAX_SR)
1035 			sunxi_gpadc->gpadc_sample_rate = DEFAULT_SR;
1036 	}
1037 
1038 	if (of_property_read_u32(np, "channel_num", &sunxi_gpadc->channel_num)) {
1039 		pr_err("%s: get channel num failed\n", __func__);
1040 		return -EBUSY;
1041 	}
1042 
1043 	if (of_property_read_u32(np, "channel_select",
1044 				&gpadc_config->channel_select)) {
1045 		pr_err("%s: get channel set select failed\n", __func__);
1046 		gpadc_config->channel_select = 0;
1047 	}
1048 
1049 	if (of_property_read_u32(np, "channel_data_select",
1050 				&gpadc_config->channel_data_select)) {
1051 		pr_err("%s: get channel data setlect failed\n", __func__);
1052 		gpadc_config->channel_data_select = 0;
1053 	}
1054 
1055 	if (of_property_read_u32(np, "channel_compare_select",
1056 				&gpadc_config->channel_compare_select)) {
1057 		pr_err("%s: get channel compare select failed\n", __func__);
1058 		gpadc_config->channel_compare_select = 0;
1059 	}
1060 
1061 	if (of_property_read_u32(np, "channel_cld_select",
1062 				&gpadc_config->channel_cld_select)) {
1063 		pr_err("%s: get channel compare low data select failed\n",
1064 				__func__);
1065 		gpadc_config->channel_cld_select = 0;
1066 	}
1067 
1068 	if (of_property_read_u32(np, "channel_chd_select",
1069 				&gpadc_config->channel_chd_select)) {
1070 		pr_err("%s: get channel compare hig data select failed\n",
1071 				__func__);
1072 		gpadc_config->channel_chd_select = 0;
1073 	}
1074 
1075 	if (of_property_read_u32(np, "channel_scan_data",
1076 				&gpadc_config->channel_scan_data)) {
1077 		pr_err("%s: get channel scan data failed\n",
1078 				__func__);
1079 		gpadc_config->channel_scan_data = 0;
1080 	}
1081 
1082 	for (i = 0; i < sunxi_gpadc->channel_num; i++) {
1083 		sprintf(name, "channel%d_compare_lowdata", i);
1084 		if (of_property_read_u32(np, name, &val)) {
1085 			pr_err("%s:get %s err!\n", __func__, name);
1086 			val = 0;
1087 		}
1088 		gpadc_config->channel_compare_lowdata[i] = val;
1089 
1090 		sprintf(name, "channel%d_compare_higdata", i);
1091 		if (of_property_read_u32(np, name, &val)) {
1092 			pr_err("%s:get %s err!\n", __func__, name);
1093 			val = 0;
1094 		}
1095 		gpadc_config->channel_compare_higdata[i] = val;
1096 	}
1097 
1098 	sunxi_gpadc->wakeup_en = 0;
1099 	if (of_property_read_bool(np, "wakeup-source")) {
1100 		device_init_wakeup(&pdev->dev, true);
1101 		dev_pm_set_wake_irq(&pdev->dev, sunxi_gpadc->irq_num);
1102 		sunxi_gpadc->wakeup_en = 1;
1103 	}
1104 
1105 	return 0;
1106 }
1107 
sunxi_gpadc_request_clk(struct sunxi_gpadc * sunxi_gpadc)1108 static int sunxi_gpadc_request_clk(struct sunxi_gpadc *sunxi_gpadc)
1109 {
1110 	sunxi_gpadc->bus_clk = devm_clk_get(sunxi_gpadc->dev, "bus");
1111 	if (IS_ERR_OR_NULL(sunxi_gpadc->bus_clk)) {
1112 		pr_err("[gpadc%d] request GPADC clock failed\n", sunxi_gpadc->bus_num);
1113 		return -1;
1114 	}
1115 
1116 	sunxi_gpadc->reset = devm_reset_control_get(sunxi_gpadc->dev, NULL);
1117 	if (IS_ERR_OR_NULL(sunxi_gpadc->reset)) {
1118 		pr_err("[gpadc%d] request GPADC reset failed\n", sunxi_gpadc->bus_num);
1119 		return -1;
1120 	}
1121 
1122 	return 0;
1123 }
1124 
sunxi_gpadc_clk_init(struct sunxi_gpadc * sunxi_gpadc)1125 static int sunxi_gpadc_clk_init(struct sunxi_gpadc *sunxi_gpadc)
1126 {
1127 	if (reset_control_deassert(sunxi_gpadc->reset)) {
1128 		pr_err("[gpadc%d] reset control deassert failed!\n", sunxi_gpadc->bus_num);
1129 		return -1;
1130 	}
1131 
1132 	if (clk_prepare_enable(sunxi_gpadc->bus_clk)) {
1133 		pr_err("[gpadc%d] enable clock failed!\n", sunxi_gpadc->bus_num);
1134 		return -1;
1135 	}
1136 
1137 	return 0;
1138 }
1139 
sunxi_gpadc_clk_exit(struct sunxi_gpadc * sunxi_gpadc)1140 static void sunxi_gpadc_clk_exit(struct sunxi_gpadc *sunxi_gpadc)
1141 {
1142 	if (!IS_ERR_OR_NULL(sunxi_gpadc->bus_clk) && __clk_is_enabled(sunxi_gpadc->bus_clk))
1143 		clk_disable_unprepare(sunxi_gpadc->bus_clk);
1144 }
1145 
sunxi_gpadc_hw_init(struct sunxi_gpadc * sunxi_gpadc)1146 static int sunxi_gpadc_hw_init(struct sunxi_gpadc *sunxi_gpadc)
1147 {
1148 	struct sunxi_config *gpadc_config = &sunxi_gpadc->gpadc_config;
1149 	int i;
1150 
1151 	if (sunxi_gpadc_request_clk(sunxi_gpadc)) {
1152 		pr_err("[gpadc%d] request gpadc clk failed\n", sunxi_gpadc->bus_num);
1153 		return -EPROBE_DEFER;
1154 	}
1155 
1156 	if (sunxi_gpadc_clk_init(sunxi_gpadc)) {
1157 		pr_err("[gpadc%d] init gpadc clk failed\n", sunxi_gpadc->bus_num);
1158 		return -EPROBE_DEFER;
1159 	}
1160 
1161 	sunxi_gpadc_sample_rate_set(sunxi_gpadc->reg_base, OSC_24MHZ,
1162 			sunxi_gpadc->gpadc_sample_rate);
1163 	for (i = 0; i < sunxi_gpadc->channel_num; i++) {
1164 		if (gpadc_config->channel_select & sunxi_gpadc_channel_id(i)) {
1165 			sunxi_gpadc_ch_select(sunxi_gpadc->reg_base, i);
1166 //			if (gpadc_config->channel_data_select & sunxi_gpadc_channel_id(i))
1167 //				sunxi_enable_irq_ch_select(sunxi_gpadc->reg_base, i);
1168 //			if (gpadc_config->channel_compare_select & sunxi_gpadc_channel_id(i)) {
1169 //				sunxi_gpadc_cmp_select(sunxi_gpadc->reg_base, i);
1170 				if (gpadc_config->channel_cld_select & sunxi_gpadc_channel_id(i)) {
1171 					sunxi_gpadc_cmp_select(sunxi_gpadc->reg_base, i);
1172 					sunxi_enable_lowirq_ch_select(sunxi_gpadc->reg_base, i);
1173 					sunxi_gpadc_ch_cmp_low(sunxi_gpadc->reg_base, i,
1174 							gpadc_config->channel_compare_lowdata[i]);
1175 				}
1176 				if (gpadc_config->channel_chd_select & sunxi_gpadc_channel_id(i)) {
1177 					sunxi_gpadc_cmp_select(sunxi_gpadc->reg_base, i);
1178 					sunxi_enable_higirq_ch_select(sunxi_gpadc->reg_base, i);
1179 					sunxi_gpadc_ch_cmp_hig(sunxi_gpadc->reg_base, i,
1180 							gpadc_config->channel_compare_higdata[i]);
1181 				}
1182 //			}
1183 		}
1184 	}
1185 	sunxi_gpadc_calibration_enable(sunxi_gpadc->reg_base);
1186 	sunxi_gpadc_mode_select(sunxi_gpadc->reg_base, GP_CONTINUOUS_MODE);
1187 	sunxi_gpadc_enable(sunxi_gpadc->reg_base, true);
1188 	sunxi_gpadc_enable_irq(sunxi_gpadc->reg_base);
1189 
1190 	return 0;
1191 }
1192 
sunxi_gpadc_hw_exit(struct sunxi_gpadc * sunxi_gpadc)1193 static int sunxi_gpadc_hw_exit(struct sunxi_gpadc *sunxi_gpadc)
1194 {
1195 	struct sunxi_config *gpadc_config = &sunxi_gpadc->gpadc_config;
1196 	int i;
1197 
1198 	for (i = 0; i < sunxi_gpadc->channel_num; i++) {
1199 		if (gpadc_config->channel_select & sunxi_gpadc_channel_id(i)) {
1200 			sunxi_gpadc_ch_deselect(sunxi_gpadc->reg_base, i);
1201 //			if (gpadc_config->channel_data_select & sunxi_gpadc_channel_id(i))
1202 //				sunxi_disable_irq_ch_select(sunxi_gpadc->reg_base, i);
1203 //			if (gpadc_config->channel_compare_select & sunxi_gpadc_channel_id(i)) {
1204 //				sunxi_gpadc_cmp_deselect(sunxi_gpadc->reg_base, i);
1205 				if (gpadc_config->channel_cld_select & sunxi_gpadc_channel_id(i))
1206 					sunxi_disable_lowirq_ch_select(sunxi_gpadc->reg_base, i);
1207 				if (gpadc_config->channel_chd_select & sunxi_gpadc_channel_id(i))
1208 					sunxi_disable_higirq_ch_select(sunxi_gpadc->reg_base, i);
1209 				if ((gpadc_config->channel_chd_select |
1210 				gpadc_config->channel_cld_select) & sunxi_gpadc_channel_id(i))
1211 					sunxi_gpadc_cmp_deselect(sunxi_gpadc->reg_base, i);
1212 //					sunxi_disable_irq_ch_select(sunxi_gpadc->reg_base, i);
1213 //			}
1214 		}
1215 	}
1216 	sunxi_gpadc_enable(sunxi_gpadc->reg_base, false);
1217 	sunxi_gpadc_clk_exit(sunxi_gpadc);
1218 
1219 	return 0;
1220 }
1221 
sunxi_gpadc_key_xfer(struct sunxi_gpadc * sunxi_gpadc,u32 data,u32 irq_low_set)1222 static u32 sunxi_gpadc_key_xfer(struct sunxi_gpadc *sunxi_gpadc, u32 data, u32 irq_low_set)
1223 {
1224 	u32 vol_data;
1225 	u8 ch_num;
1226 
1227 #ifdef CONFIG_ARCH_SUN8IW18
1228 	u32 vin;
1229 
1230 	vin = sunxi_gpadc_check_vin();
1231 	if (vin == 0)
1232 		return 0;
1233 
1234 	data = ((vin - 6)*data) / 4096;
1235 	vol_data = data;
1236 	data = data * 1000;
1237 #else
1238 	data = ((VOL_RANGE / 4096)*data);	/* 12bits sample rate */
1239 	vol_data = data / 1000;
1240 #endif
1241 	if (vol_data < SUNXIKEY_DOWN) {
1242 		/* MAX compare_before = 128 */
1243 		sunxi_gpadc->compare_before = ((data / SCALE_UNIT) / 1000)&0xff;
1244 		dprintk(DEBUG_RUN, "bf=%3d lt=%3d\n",
1245 				sunxi_gpadc->compare_before,
1246 				sunxi_gpadc->compare_later);
1247 
1248 		if (sunxi_gpadc->compare_before >= sunxi_gpadc->compare_later - 1
1249 			&& sunxi_gpadc->compare_before <= sunxi_gpadc->compare_later + 1)
1250 			sunxi_gpadc->key_cnt++;
1251 		else
1252 			sunxi_gpadc->key_cnt = 0;
1253 		sunxi_gpadc->compare_later = sunxi_gpadc->compare_before;
1254 		if (sunxi_gpadc->key_cnt >= filter_cnt) {
1255 			sunxi_gpadc->compare_later = sunxi_gpadc->compare_before;
1256 			sunxi_gpadc->key_code = keypad_mapindex[sunxi_gpadc->compare_before];
1257 
1258 			switch (irq_low_set) {
1259 			case GP_CH0_LOW:
1260 				ch_num = GP_CH_0;
1261 				break;
1262 			case GP_CH1_LOW:
1263 				ch_num = GP_CH_1;
1264 				break;
1265 			case GP_CH2_LOW:
1266 				ch_num = GP_CH_2;
1267 				break;
1268 			case GP_CH3_LOW:
1269 				ch_num = GP_CH_3;
1270 				break;
1271 			default:
1272 				pr_err("%s, invalid channel id!", __func__);
1273 				return -EINVAL;
1274 			}
1275 			if (sunxi_gpadc->key_code != sunxi_gpadc->key_num) {
1276 				input_report_key(sunxi_gpadc->input_gpadc[ch_num],
1277 					sunxi_gpadc->scankeycodes[sunxi_gpadc->key_code], 1);
1278 				input_sync(sunxi_gpadc->input_gpadc[ch_num]);
1279 			}
1280 			sunxi_gpadc->compare_later = 0;
1281 			sunxi_gpadc->key_cnt = 0;
1282 		}
1283 
1284 	}
1285 
1286 	return 0;
1287 }
1288 
sunxi_gpadc_interrupt(int irqno,void * dev_id)1289 static irqreturn_t sunxi_gpadc_interrupt(int irqno, void *dev_id)
1290 {
1291 	struct sunxi_gpadc *sunxi_gpadc = (struct sunxi_gpadc *)dev_id;
1292 	u32  irq_data_set, irq_low_set, irq_hig_set;
1293 	u32  irq_data_val, irq_low_en, irq_hig_en;
1294 	u32 data;
1295 #ifndef CONFIG_ARCH_SUN8IW18
1296 	u32 i;
1297 #endif
1298 	irq_data_val = sunxi_gpadc_read_ch_irq_enable(sunxi_gpadc->reg_base);
1299 
1300 	irq_low_en = sunxi_gpadc_read_ch_lowirq_enable(
1301 			sunxi_gpadc->reg_base);
1302 	irq_hig_en = sunxi_gpadc_read_ch_higirq_enable(
1303 			sunxi_gpadc->reg_base);
1304 
1305 	irq_data_set = sunxi_ch_irq_status(sunxi_gpadc->reg_base);
1306 	sunxi_ch_irq_clear_flags(sunxi_gpadc->reg_base, irq_data_set);
1307 	irq_low_set = sunxi_ch_lowirq_status(sunxi_gpadc->reg_base);
1308 	sunxi_ch_lowirq_clear_flags(sunxi_gpadc->reg_base, irq_low_set);
1309 	irq_hig_set = sunxi_ch_higirq_status(sunxi_gpadc->reg_base);
1310 	sunxi_ch_higirq_clear_flags(sunxi_gpadc->reg_base, irq_hig_set);
1311 
1312 	if (irq_data_set & GP_CH0_DATA)
1313 		data = sunxi_gpadc_read_data(sunxi_gpadc->reg_base, GP_CH_0);
1314 
1315 	if (irq_low_set & GP_CH0_LOW & irq_low_en) {
1316 		data = sunxi_gpadc_read_data(sunxi_gpadc->reg_base, GP_CH_0);
1317 		sunxi_gpadc_key_xfer(sunxi_gpadc, data, irq_low_set);
1318 		sunxi_enable_higirq_ch_select(sunxi_gpadc->reg_base, GP_CH_0);
1319 	}
1320 
1321 	if (irq_hig_set & GP_CH0_HIG & irq_hig_en) {
1322 		input_report_key(sunxi_gpadc->input_gpadc[GP_CH_0],
1323 			sunxi_gpadc->scankeycodes[sunxi_gpadc->key_code], 0);
1324 		input_sync(sunxi_gpadc->input_gpadc[GP_CH_0]);
1325 		sunxi_gpadc->compare_later = 0;
1326 		sunxi_gpadc->key_cnt = 0;
1327 		sunxi_disable_higirq_ch_select(sunxi_gpadc->reg_base, GP_CH_0);
1328 	}
1329 
1330 #ifdef CONFIG_ARCH_SUN8IW18
1331 	if (irq_low_set & GP_CH3_LOW & irq_low_en) {
1332 		data = sunxi_gpadc_read_data(sunxi_gpadc->reg_base, GP_CH_3);
1333 		sunxi_gpadc_key_xfer(sunxi_gpadc, data, irq_low_set);
1334 		sunxi_enable_higirq_ch_select(sunxi_gpadc->reg_base, GP_CH_3);
1335 	}
1336 
1337 	if (irq_hig_set & GP_CH3_HIG & irq_hig_en) {
1338 		input_report_key(sunxi_gpadc->input_gpadc[GP_CH_3],
1339 			sunxi_gpadc->scankeycodes[sunxi_gpadc->key_code], 0);
1340 		input_sync(sunxi_gpadc->input_gpadc[GP_CH_3]);
1341 		sunxi_gpadc->compare_later = 0;
1342 		sunxi_gpadc->key_cnt = 0;
1343 		sunxi_disable_higirq_ch_select(sunxi_gpadc->reg_base, GP_CH_3);
1344 	}
1345 #else
1346 	for (i = 1; i < sunxi_gpadc->channel_num; i++) {
1347 		if (irq_data_set & irq_data_val & (1 << i)) {
1348 			data = sunxi_gpadc_read_data(sunxi_gpadc->reg_base, i);
1349 			input_event(sunxi_gpadc->input_gpadc[i],
1350 					EV_MSC, MSC_SCAN, data);
1351 			input_sync(sunxi_gpadc->input_gpadc[i]);
1352 			pr_debug("channel %d data pend\n", i);
1353 		}
1354 
1355 		if (irq_low_en & irq_hig_en & (1 << i)) {
1356 			if (irq_low_set & irq_hig_set & (1 << i)) {
1357 				data = sunxi_gpadc_read_data(
1358 						sunxi_gpadc->reg_base, i);
1359 				input_event(sunxi_gpadc->input_gpadc[i],
1360 						EV_MSC, MSC_SCAN, data);
1361 				input_sync(sunxi_gpadc->input_gpadc[i]);
1362 				pr_debug("channel %d low and hig pend\n", i);
1363 			} else
1364 				pr_debug("invalid range\n");
1365 		} else {
1366 			if (irq_low_set & irq_low_en & ~irq_hig_en
1367 					& (1 << i)) {
1368 				data = sunxi_gpadc_read_data(
1369 						sunxi_gpadc->reg_base, i);
1370 				input_event(sunxi_gpadc->input_gpadc[i],
1371 						EV_MSC, MSC_SCAN, data);
1372 				input_sync(sunxi_gpadc->input_gpadc[i]);
1373 				pr_debug("channel %d low pend\n", i);
1374 			} else {
1375 				if (irq_hig_set & irq_hig_en & ~irq_low_en
1376 					& (1 << i)) {
1377 					data = sunxi_gpadc_read_data(
1378 							sunxi_gpadc->reg_base,
1379 							i);
1380 					input_event(sunxi_gpadc->input_gpadc[i],
1381 							EV_MSC, MSC_SCAN, data);
1382 					input_sync(sunxi_gpadc->input_gpadc[i]);
1383 					pr_debug("channel %d hig pend\n", i);
1384 				}
1385 			}
1386 
1387 		}
1388 	}
1389 #endif
1390 	return IRQ_HANDLED;
1391 }
1392 
1393 #ifdef USE_DATA_SCAN
push_event_status(struct work_struct * work)1394 static void push_event_status(struct work_struct *work)
1395 {
1396 	unsigned int data, i;
1397 	unsigned int scan_channel = sunxi_gpadc->gpadc_config.channel_scan_data;
1398 
1399 	for (i = 1; i < sunxi_gpadc->channel_num; i++) {
1400 		if (scan_channel & (1 << i)) {
1401 			data = sunxi_gpadc_read_data(sunxi_gpadc->reg_base, i);
1402 			input_event(sunxi_gpadc->input_gpadc[i],
1403 					EV_MSC, MSC_SCAN, data);
1404 			input_sync(sunxi_gpadc->input_gpadc[i]);
1405 		}
1406 	}
1407 	schedule_delayed_work(&sunxi_gpadc->gpadc_work, sunxi_gpadc->interval);
1408 }
1409 #endif
1410 
__status_regs_ex(void __iomem * reg_base)1411 void __status_regs_ex(void __iomem *reg_base)
1412 {
1413 	unsigned char i;
1414 	u32 reg_val;
1415 
1416 	reg_val = readl(reg_base + GP_CS_EN_REG);
1417 
1418 	for (i = 0; i < sunxi_gpadc->channel_num; i++) {
1419 		if (reg_val & (1 << i))
1420 			pr_warn("gpadc%d: okay\n", i);
1421 		else
1422 			pr_warn("gpadc%d: disable\n", i);
1423 	}
1424 }
1425 
__vol_regs_ex(struct sunxi_gpadc * sunxi_gpadc)1426 void __vol_regs_ex(struct sunxi_gpadc *sunxi_gpadc)
1427 {
1428 	unsigned char i;
1429 	char tmp = -1;
1430 
1431 	pr_info("key_cnt = %d\n", sunxi_gpadc->key_num);
1432 	for (i = 0; i < sunxi_gpadc->key_num; i++)
1433 		pr_info("key%d_vol = %d\n", i, key_vol[i]);
1434 
1435 	dprintk(DEBUG_INFO, "keypad_mapindex[%d] = {", MAXIMUM_SCALE);
1436 	for (i = 0; i < MAXIMUM_SCALE; i++) {
1437 		if (keypad_mapindex[i] != tmp)
1438 			dprintk(DEBUG_INFO, "\n\t");
1439 		dprintk(DEBUG_INFO, "%d, ", keypad_mapindex[i]);
1440 		tmp = keypad_mapindex[i];
1441 	}
1442 	dprintk(DEBUG_INFO, "\n}\n");
1443 }
1444 
__parse_status_str(const char * buf,size_t size)1445 static struct status_reg __parse_status_str(const char *buf, size_t size)
1446 {
1447 	char *ptr = NULL;
1448 	char *str = "gpadc";
1449 	struct status_reg reg_tmp;
1450 
1451 	if (!buf)
1452 		goto err;
1453 
1454 	reg_tmp.pst = (char *)buf;
1455 	reg_tmp.ped = reg_tmp.pst;
1456 	reg_tmp.ped = strnchr(reg_tmp.pst, size, ',');
1457 	if (!reg_tmp.ped)
1458 		goto err;
1459 
1460 	*reg_tmp.ped = '\0';
1461 	reg_tmp.ped++;
1462 	reg_tmp.pst = strim(reg_tmp.pst);
1463 	reg_tmp.ped = strim(reg_tmp.ped);
1464 
1465 	ptr = strstr(reg_tmp.pst, str);
1466 	if (!ptr) {
1467 		ptr = reg_tmp.pst;
1468 		while (*ptr != 0) {
1469 			if (*ptr >= 'A' && *ptr <= 'Z')
1470 				*ptr += 32;
1471 			ptr++;
1472 		}
1473 		ptr = reg_tmp.pst;
1474 		ptr = strstr(ptr, str);
1475 		if (!ptr)
1476 			goto err;
1477 	}
1478 
1479 	reg_tmp.channel = *(reg_tmp.pst + strlen(str)) - 48;
1480 	if (reg_tmp.channel < 0
1481 			|| reg_tmp.channel > (sunxi_gpadc->channel_num - 1))
1482 		goto err;
1483 
1484 	if (!(strlen(reg_tmp.ped) == 1))
1485 		goto err;
1486 
1487 	reg_tmp.val = *reg_tmp.ped - 48;
1488 
1489 	return reg_tmp;
1490 
1491 err:
1492 	reg_tmp.pst = NULL;
1493 	reg_tmp.ped = NULL;
1494 	return reg_tmp;
1495 }
1496 
__parse_vol_str(const char * buf,size_t size)1497 static struct vol_reg __parse_vol_str(const char *buf, size_t size)
1498 {
1499 	int ret = 0;
1500 	char *ptr = NULL;
1501 	char *str = "vol";
1502 	struct vol_reg reg_tmp;
1503 
1504 	if (!buf)
1505 		goto err;
1506 
1507 	reg_tmp.pst = (char *)buf;
1508 	reg_tmp.ped = reg_tmp.pst;
1509 	reg_tmp.ped = strnchr(reg_tmp.pst, size, ',');
1510 	if (!reg_tmp.ped)
1511 		goto err;
1512 
1513 	*reg_tmp.ped = '\0';
1514 	reg_tmp.ped++;
1515 	reg_tmp.pst = strim(reg_tmp.pst);
1516 	reg_tmp.ped = strim(reg_tmp.ped);
1517 
1518 	ptr = strstr(reg_tmp.pst, str);
1519 	if (!ptr) {
1520 		ptr = reg_tmp.pst;
1521 		while (*ptr != 0) {
1522 			if (*ptr >= 'A' && *ptr <= 'Z')
1523 				*ptr += 32;
1524 			ptr++;
1525 		}
1526 		ptr = reg_tmp.pst;
1527 		ptr = strstr(ptr, str);
1528 		if (!ptr)
1529 			goto err;
1530 	}
1531 
1532 	reg_tmp.index = *(reg_tmp.pst + strlen(str)) - 48;
1533 	if (reg_tmp.index < 0
1534 			|| reg_tmp.index > (sunxi_gpadc->key_num - 1))
1535 		goto err;
1536 
1537 	ret = kstrtoul(reg_tmp.ped, 10, &reg_tmp.vol);
1538 	if (ret)
1539 		goto err;
1540 
1541 	return reg_tmp;
1542 
1543 err:
1544 	reg_tmp.pst = NULL;
1545 	reg_tmp.ped = NULL;
1546 	return reg_tmp;
1547 }
1548 
__parse_sr_str(const char * buf,size_t size)1549 static struct sr_reg __parse_sr_str(const char *buf, size_t size)
1550 {
1551 	int ret = 0;
1552 	struct sr_reg reg_tmp;
1553 
1554 	if (!buf)
1555 		reg_tmp.pst = NULL;
1556 
1557 	reg_tmp.pst = (char *)buf;
1558 	reg_tmp.pst = strim(reg_tmp.pst);
1559 
1560 	ret = kstrtoul(reg_tmp.pst, 10, &reg_tmp.val);
1561 	if (ret)
1562 		goto err;
1563 	if (reg_tmp.val < MIN_SR || reg_tmp.val > MAX_SR) {
1564 		pr_err("%s,%d err. sampling frequency: [%lu,%lu]\n",
1565 				__func__, __LINE__, MIN_SR, MAX_SR);
1566 		reg_tmp.pst = NULL;
1567 	}
1568 
1569 	return reg_tmp;
1570 
1571 err:
1572 	reg_tmp.pst = NULL;
1573 	return reg_tmp;
1574 }
1575 
__parse_filter_str(const char * buf,size_t size)1576 static struct filter_reg __parse_filter_str(const char *buf, size_t size)
1577 {
1578 	int ret = 0;
1579 	struct filter_reg reg_tmp;
1580 
1581 	if (!buf)
1582 		reg_tmp.pst = NULL;
1583 
1584 	reg_tmp.pst = (char *)buf;
1585 	reg_tmp.pst = strim(reg_tmp.pst);
1586 
1587 	ret = kstrtoul(reg_tmp.pst, 10, &reg_tmp.val);
1588 	if (ret)
1589 		goto err;
1590 
1591 	return reg_tmp;
1592 
1593 err:
1594 	reg_tmp.pst = NULL;
1595 	return reg_tmp;
1596 }
1597 
__parse_channel_str(const char * buf,size_t size)1598 static struct channel_reg __parse_channel_str(const char *buf, size_t size)
1599 {
1600 	int ret = 0;
1601 	struct channel_reg reg_tmp;
1602 
1603 	if (!buf)
1604 		reg_tmp.pst = NULL;
1605 
1606 	reg_tmp.pst = (char *)buf;
1607 	reg_tmp.pst = strim(reg_tmp.pst);
1608 
1609 	ret = kstrtoul(reg_tmp.pst, 10, &reg_tmp.val);
1610 	if (ret)
1611 		goto err;
1612 
1613 	return reg_tmp;
1614 
1615 err:
1616 	reg_tmp.pst = NULL;
1617 	return reg_tmp;
1618 }
1619 
1620 static ssize_t
status_show(struct class * class,struct class_attribute * attr,char * buf)1621 status_show(struct class *class, struct class_attribute *attr, char *buf)
1622 {
1623 	__status_regs_ex(sunxi_gpadc->reg_base);
1624 
1625 	return 0;
1626 }
1627 
1628 static ssize_t
status_store(struct class * class,struct class_attribute * attr,const char * buf,size_t count)1629 status_store(struct class *class, struct class_attribute *attr,
1630 		const char *buf, size_t count)
1631 {
1632 	status_para = __parse_status_str(buf, count);
1633 	if (!status_para.pst || !status_para.ped)
1634 		goto err;
1635 
1636 	if (status_para.val) {
1637 		sunxi_gpadc_ch_select(sunxi_gpadc->reg_base,
1638 				status_para.channel);
1639 		pr_warn("Enable gpadc%u\n", status_para.channel);
1640 	} else {
1641 		sunxi_gpadc_ch_deselect(sunxi_gpadc->reg_base,
1642 				status_para.channel);
1643 		pr_warn("Disable gpadc%u\n", status_para.channel);
1644 	}
1645 
1646 	return count;
1647 
1648 err:
1649 	pr_err("%s,%d err, invalid para!\n", __func__, __LINE__);
1650 	status_para.pst = NULL;
1651 	status_para.ped = NULL;
1652 	status_para.channel = -1;
1653 	status_para.val = -1;
1654 	return -EINVAL;
1655 }
1656 
1657 static ssize_t
vol_show(struct class * class,struct class_attribute * attr,char * buf)1658 vol_show(struct class *class, struct class_attribute *attr, char *buf)
1659 {
1660 	__vol_regs_ex(sunxi_gpadc);
1661 	return 0;
1662 }
1663 
1664 static ssize_t
vol_store(struct class * class,struct class_attribute * attr,const char * buf,size_t count)1665 vol_store(struct class *class, struct class_attribute *attr,
1666 		const char *buf, size_t count)
1667 {
1668 	unsigned char i, j = 0;
1669 	u32 gap = 0, half_gap = 0, cnt = 0;
1670 	u32 set_vol[VOL_NUM];
1671 
1672 	vol_para = __parse_vol_str(buf, count);
1673 	if (!vol_para.pst || !vol_para.ped)
1674 		goto err;
1675 
1676 	key_vol[vol_para.index] = vol_para.vol;
1677 
1678 	for (i = sunxi_gpadc->key_num - 1; i > 0; i--) {
1679 		gap = gap + (key_vol[i] - key_vol[i-1]);
1680 		cnt++;
1681 	}
1682 	half_gap = gap/cnt/2;
1683 	for (i = 0; i < sunxi_gpadc->key_num; i++)
1684 		set_vol[i] = key_vol[i] + half_gap;
1685 
1686 	for (i = 0; i < 128; i++) {
1687 		if (i * SCALE_UNIT > set_vol[j])
1688 			j++;
1689 		keypad_mapindex[i] = j;
1690 	}
1691 
1692 	return count;
1693 
1694 err:
1695 	pr_err("%s,%d err, invalid para!\n", __func__, __LINE__);
1696 	vol_para.pst = NULL;
1697 	vol_para.ped = NULL;
1698 	vol_para.index = -1;
1699 	vol_para.vol = -1;
1700 	return -EINVAL;
1701 }
1702 
sunxi_gpadc_read_channel_data(u8 channel)1703 u32 sunxi_gpadc_read_channel_data(u8 channel)
1704 {
1705 	u32 data, vol_data;
1706 
1707 	data = readl(sunxi_gpadc->reg_base + GP_CS_EN_REG);
1708 
1709 	if ((data & (0x01 << channel)) == 0)
1710 		return VOL_RANGE + 1;
1711 
1712 	data = sunxi_gpadc_read_data(sunxi_gpadc->reg_base, channel);
1713 
1714 	data = ((VOL_RANGE / 4096)*data);	/* 12bits sample rate */
1715 	vol_data = data / 1000;			//data to val_data
1716 
1717 	printk("vol_data: %d\n", vol_data);
1718 
1719 	return vol_data;
1720 }
1721 EXPORT_SYMBOL_GPL(sunxi_gpadc_read_channel_data);
1722 
1723 static ssize_t
sr_show(struct class * class,struct class_attribute * attr,char * buf)1724 sr_show(struct class *class, struct class_attribute *attr, char *buf)
1725 {
1726 	u32 sr;
1727 
1728 	sr = sunxi_gpadc_sample_rate_read(sunxi_gpadc->reg_base, OSC_24MHZ);
1729 	pr_warn("gpadc sampling frequency: %u\n", sr);
1730 
1731 	return 0;
1732 }
1733 
1734 static ssize_t
sr_store(struct class * class,struct class_attribute * attr,const char * buf,size_t count)1735 sr_store(struct class *class, struct class_attribute *attr,
1736 		const char *buf, size_t count)
1737 {
1738 	sr_para = __parse_sr_str(buf, count);
1739 	if (!sr_para.pst)
1740 		goto err;
1741 
1742 	sunxi_gpadc_sample_rate_set(sunxi_gpadc->reg_base, OSC_24MHZ,
1743 			sr_para.val);
1744 
1745 	return count;
1746 
1747 err:
1748 	pr_err("%s,%d err, invalid para!\n", __func__, __LINE__);
1749 	return -EINVAL;
1750 }
1751 
1752 static ssize_t
filter_show(struct class * class,struct class_attribute * attr,char * buf)1753 filter_show(struct class *class, struct class_attribute *attr, char *buf)
1754 {
1755 	dprintk(DEBUG_INFO, "filter_cnt = %u\n", filter_cnt);
1756 
1757 	return 0;
1758 }
1759 
1760 static ssize_t
filter_store(struct class * class,struct class_attribute * attr,const char * buf,size_t count)1761 filter_store(struct class *class, struct class_attribute *attr,
1762 		const char *buf, size_t count)
1763 {
1764 	filter_para = __parse_filter_str(buf, count);
1765 	if (!filter_para.pst)
1766 		goto err;
1767 
1768 	filter_cnt = filter_para.val;
1769 
1770 	return count;
1771 
1772 err:
1773 	pr_err("%s,%d err, invalid para!\n", __func__, __LINE__);
1774 	return -EINVAL;
1775 }
1776 
1777 static ssize_t
data_store(struct class * class,struct class_attribute * attr,const char * buf,size_t count)1778 data_store(struct class *class, struct class_attribute *attr,
1779 		const char *buf, size_t count)
1780 {
1781 	channel_para = __parse_channel_str(buf, count);
1782 	if (!channel_para.pst)
1783 		goto err;
1784 
1785 	channel = channel_para.val;
1786 
1787 	return count;
1788 
1789 err:
1790 	pr_err("%s,%d err, invalid para!\n", __func__, __LINE__);
1791 	return -EINVAL;
1792 }
1793 
1794 static ssize_t
data_show(struct class * class,struct class_attribute * attr,char * buf)1795 data_show(struct class *class, struct class_attribute *attr, char *buf)
1796 {
1797 	unsigned int data;
1798 
1799 	data = sunxi_gpadc_read_channel_data(channel);
1800 
1801 	return sprintf(buf, "%d\n", data);
1802 }
1803 
1804 static struct class_attribute gpadc_class_attrs[] = {
1805 	__ATTR(status, 0644, status_show, status_store),
1806 	__ATTR(vol,    0644, vol_show,    vol_store),
1807 	__ATTR(sr,     0644, sr_show,     sr_store),
1808 	__ATTR(filter, 0644, filter_show, filter_store),
1809 	__ATTR(data,   0644, data_show,   data_store),
1810 };
1811 
1812 static struct class gpadc_class = {
1813 	.name		= "gpadc",
1814 	.owner		= THIS_MODULE,
1815 };
1816 
1817 #if IS_ENABLED(CONFIG_IIO)
1818 struct sunxi_gpadc_iio {
1819 	struct sunxi_gpadc *sunxi_gpadc;
1820 };
1821 
sunxi_gpadc_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)1822 static int sunxi_gpadc_read_raw(struct iio_dev *indio_dev,
1823 			struct iio_chan_spec const *chan,
1824 			int *val, int *val2, long mask)
1825 {
1826 	int ret = 0;
1827 	int key_val, id_vol;
1828 	struct sunxi_gpadc_iio *info = iio_priv(indio_dev);
1829 	struct sunxi_gpadc *sunxi_gpadc = info->sunxi_gpadc;
1830 	u32 data, vol_data;
1831 
1832 	mutex_lock(&indio_dev->mlock);
1833 	switch (mask) {
1834 	case IIO_CHAN_INFO_RAW:
1835 		/*
1836 		* This is an example,so if there are new requirements,
1837 		* you can replace it with your interface to upload data.
1838 		* *val and *val2 are used as needed.
1839 		* In the shell console you can use the "cat in_voltage0_raw" command to view the data.
1840 		*/
1841 		data = sunxi_gpadc_read_data(sunxi_gpadc->reg_base, chan->channel);
1842 
1843 		data = ((VOL_RANGE / 4096)*data);		/* 12bits sample rate */
1844 		vol_data = data / 1000;					/* data to val_data */
1845 		*val  = vol_data;
1846 
1847 		ret = IIO_VAL_INT;
1848 		break;
1849 
1850 	case IIO_CHAN_INFO_SCALE:
1851 		break;
1852 
1853 	default:
1854 		ret = -EINVAL;
1855 	}
1856 	mutex_unlock(&indio_dev->mlock);
1857 
1858 	return ret;
1859 }
1860 
1861 /*
1862 * If necessary, you can fill other callback functions
1863 * in this data structure, for example:
1864 * write_raw/read_event_value/write_event_value and so on...
1865 */
1866 static const struct iio_info sunxi_gpadc_iio_info = {
1867 	.read_raw = &sunxi_gpadc_read_raw,
1868 };
1869 
sunxi_gpadc_remove_iio(void * data)1870 static void sunxi_gpadc_remove_iio(void *data)
1871 {
1872 	struct iio_dev *indio_dev = data;
1873 
1874 	if (!indio_dev)
1875 		pr_err("indio_dev is null\n");
1876 	else
1877 		iio_device_unregister(indio_dev);
1878 
1879 }
1880 
1881 #define ADC_CHANNEL(_index, _id) {	\
1882 	.type = IIO_VOLTAGE,			\
1883 	.indexed = 1,					\
1884 	.channel = _index,				\
1885 	.address = _index,				\
1886 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),	\
1887 	.datasheet_name = _id,							\
1888 }
1889 
1890 /*
1891 * According to the GPADC spec,
1892 * the current GPADC only supports 4 channels.
1893 */
1894 static const struct iio_chan_spec gpadc_adc_iio_channels[] = {
1895 	ADC_CHANNEL(0, "adc_chan0"),
1896 	ADC_CHANNEL(1, "adc_chan1"),
1897 	ADC_CHANNEL(2, "adc_chan2"),
1898 	ADC_CHANNEL(3, "adc_chan3"),
1899 	//ADC_CHANNEL(4, "adc_chan4"),
1900 	//ADC_CHANNEL(5, "adc_chan5"),
1901 	//ADC_CHANNEL(6, "adc_chan6"),
1902 	//ADC_CHANNEL(7, "adc_chan7"),
1903 };
1904 
1905 /*
1906 * Register the IIO data structure,
1907 * the basic data has been initialized in the function: sunxi_gpadc_probe,
1908 * you can use it directly here: platform_get_drvdata.
1909 */
sunxi_gpadc_iio_init(struct platform_device * pdev)1910 static int sunxi_gpadc_iio_init(struct platform_device *pdev)
1911 {
1912 	int ret;
1913 	struct iio_dev *indio_dev;
1914 	struct sunxi_gpadc_iio *info;
1915 	struct sunxi_gpadc *sunxi_gpadc = platform_get_drvdata(pdev);
1916 
1917 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct sunxi_gpadc));
1918 	if (!indio_dev)
1919 		return -ENOMEM;
1920 
1921 	info = iio_priv(indio_dev);
1922 	info->sunxi_gpadc = sunxi_gpadc;
1923 
1924 	indio_dev->dev.parent = &pdev->dev;
1925 	indio_dev->name = pdev->name;
1926 	indio_dev->channels = gpadc_adc_iio_channels;
1927 	indio_dev->num_channels = ARRAY_SIZE(gpadc_adc_iio_channels);
1928 	indio_dev->info = &sunxi_gpadc_iio_info;
1929 	indio_dev->modes = INDIO_DIRECT_MODE;
1930 
1931 	ret = iio_device_register(indio_dev);
1932 	if (ret < 0) {
1933 		dev_err(&pdev->dev, "unable to register iio device\n");
1934 		ret = -EINVAL;
1935 	}
1936 /*
1937  * Destroy IIO:the kernel will call the callback to execute sunxi_gpadc_remove_iio.
1938  */
1939 	ret = devm_add_action(&pdev->dev, sunxi_gpadc_remove_iio, indio_dev);
1940 
1941 	if (ret) {
1942 		dev_err(&pdev->dev, "unable to add iio cleanup action\n");
1943 		goto err_iio_unregister;
1944 	}
1945 
1946 	return 0;
1947 
1948 err_iio_unregister:
1949 	iio_device_unregister(indio_dev);
1950 
1951 	return ret;
1952 }
1953 #endif
1954 
1955 
sunxi_gpadc_probe(struct platform_device * pdev)1956 int sunxi_gpadc_probe(struct platform_device *pdev)
1957 {
1958 	struct device_node *np = pdev->dev.of_node;
1959 	int ret = 0;
1960 
1961 	if (!of_device_is_available(np)) {
1962 		pr_err("%s: sunxi gpadc is disable\n", __func__);
1963 		return -EPERM;
1964 	}
1965 
1966 	sunxi_gpadc = kzalloc(sizeof(struct sunxi_gpadc), GFP_KERNEL);
1967 	if (IS_ERR_OR_NULL(sunxi_gpadc)) {
1968 		pr_err("not enough memory for sunxi_gpadc\n");
1969 		return -ENOMEM;
1970 	}
1971 
1972 	sunxi_gpadc->reg_base = of_iomap(np, 0);
1973 	if (NULL == sunxi_gpadc->reg_base) {
1974 		pr_err("sunxi_gpadc of_iomap fail\n");
1975 		ret = -EBUSY;
1976 		goto fail1;
1977 	}
1978 
1979 	vaddr = ioremap(LDOA_EFUSE_REG, 1);
1980 	if (!vaddr) {
1981 		pr_err("sunxi_gpadc iomap fail\n");
1982 		ret = -EBUSY;
1983 		goto fail1;
1984 	}
1985 
1986 	sunxi_gpadc->irq_num = irq_of_parse_and_map(np, 0);
1987 	if (0 == sunxi_gpadc->irq_num) {
1988 		pr_err("sunxi_gpadc fail to map irq\n");
1989 		ret = -EBUSY;
1990 		goto fail2;
1991 	}
1992 
1993 #if 0
1994 	sunxi_gpadc->mclk = of_clk_get(np, 0);
1995 	if (IS_ERR_OR_NULL(sunxi_gpadc->mclk)) {
1996 		pr_err("sunxi_gpadc has no clk\n");
1997 		ret = -EINVAL;
1998 		goto fail3;
1999 	} else {
2000 		if (clk_prepare_enable(sunxi_gpadc->mclk)) {
2001 			pr_err("enable sunxi_gpadc clock failed!\n");
2002 			ret = -EINVAL;
2003 			goto fail3;
2004 		}
2005 	}
2006 #endif
2007 	sunxi_gpadc->dev = &pdev->dev;
2008 	sunxi_key_init(sunxi_gpadc, pdev);
2009 	sunxi_gpadc_setup(pdev, sunxi_gpadc);
2010 	sunxi_gpadc_hw_init(sunxi_gpadc);
2011 	sunxi_gpadc_input_register_setup(sunxi_gpadc);
2012 
2013 #ifdef USE_DATA_SCAN
2014 	sunxi_gpadc->interval = msecs_to_jiffies(1 * 1000);
2015 	INIT_DELAYED_WORK(&sunxi_gpadc->gpadc_work, push_event_status);
2016 	schedule_delayed_work(&sunxi_gpadc->gpadc_work, sunxi_gpadc->interval);
2017 #endif
2018 
2019 	platform_set_drvdata(pdev, sunxi_gpadc);
2020 
2021 	if (request_irq(sunxi_gpadc->irq_num, sunxi_gpadc_interrupt,
2022 			IRQF_TRIGGER_NONE, "sunxi-gpadc", sunxi_gpadc)) {
2023 		pr_err("sunxi_gpadc request irq failure\n");
2024 		return -1;
2025 	}
2026 
2027 /*
2028 * This function will only be used when IIO is enabled !!!
2029 */
2030 #if IS_ENABLED(CONFIG_IIO)
2031 	sunxi_gpadc_iio_init(pdev);
2032 #endif
2033 
2034 	return 0;
2035 #if 0
2036 fail3:
2037 	free_irq(sunxi_gpadc->irq_num, sunxi_gpadc);
2038 #endif
2039 fail2:
2040 	iounmap(vaddr);
2041 	iounmap(sunxi_gpadc->reg_base);
2042 fail1:
2043 	kfree(sunxi_gpadc);
2044 
2045 	return ret;
2046 }
2047 
sunxi_gpadc_remove(struct platform_device * pdev)2048 int sunxi_gpadc_remove(struct platform_device *pdev)
2049 
2050 {
2051 	struct sunxi_gpadc *sunxi_gpadc = platform_get_drvdata(pdev);
2052 
2053 	cancel_delayed_work_sync(&sunxi_gpadc->gpadc_work);
2054 
2055 	sunxi_gpadc_hw_exit(sunxi_gpadc);
2056 	free_irq(sunxi_gpadc->irq_num, sunxi_gpadc);
2057 	iounmap(sunxi_gpadc->reg_base);
2058 	kfree(sunxi_gpadc);
2059 
2060 	return 0;
2061 }
2062 
2063 #ifdef CONFIG_PM
sunxi_gpadc_suspend(struct device * dev)2064 static int sunxi_gpadc_suspend(struct device *dev)
2065 {
2066 	struct platform_device *pdev = to_platform_device(dev);
2067 	struct sunxi_gpadc *sunxi_gpadc = platform_get_drvdata(pdev);
2068 
2069 #ifdef USE_DATA_SCAN
2070 	schedule_delayed_work(&sunxi_gpadc->gpadc_work, 0);
2071 	flush_delayed_work(&sunxi_gpadc->gpadc_work);
2072 	cancel_delayed_work_sync(&sunxi_gpadc->gpadc_work);
2073 #endif
2074 
2075 	if (sunxi_gpadc->wakeup_en)
2076 		sunxi_gpadc_save_regs(sunxi_gpadc);		/* standby need irq enable */
2077 	else {
2078 		disable_irq_nosync(sunxi_gpadc->irq_num);
2079 		sunxi_gpadc_save_regs(sunxi_gpadc);
2080 		clk_disable_unprepare(sunxi_gpadc->bus_clk);
2081 		reset_control_assert(sunxi_gpadc->reset);
2082 	}
2083 
2084 	return 0;
2085 }
2086 
sunxi_gpadc_resume(struct device * dev)2087 static int sunxi_gpadc_resume(struct device *dev)
2088 {
2089 	struct platform_device *pdev = to_platform_device(dev);
2090 	struct sunxi_gpadc *sunxi_gpadc = platform_get_drvdata(pdev);
2091 
2092 	if (sunxi_gpadc->wakeup_en)
2093 		sunxi_gpadc_restore_regs(sunxi_gpadc);
2094 	else {
2095 		reset_control_deassert(sunxi_gpadc->reset);
2096 
2097 		if (clk_prepare_enable(sunxi_gpadc->bus_clk)) {
2098 			pr_err("[gpadc%d] enable clock failed!\n", sunxi_gpadc->bus_num);
2099 			return 0;
2100 		}
2101 
2102 		sunxi_gpadc_restore_regs(sunxi_gpadc);
2103 		enable_irq(sunxi_gpadc->irq_num);
2104 	}
2105 #ifdef USE_DATA_SCAN
2106 	schedule_delayed_work(&sunxi_gpadc->gpadc_work, sunxi_gpadc->interval);
2107 #endif
2108 
2109 	return 0;
2110 }
2111 
2112 static const struct dev_pm_ops sunxi_gpadc_dev_pm_ops = {
2113 	.suspend = sunxi_gpadc_suspend,
2114 	.resume = sunxi_gpadc_resume,
2115 };
2116 
2117 #define SUNXI_GPADC_DEV_PM_OPS (&sunxi_gpadc_dev_pm_ops)
2118 #else
2119 #define SUNXI_GPADC_DEV_PM_OPS NULL
2120 #endif
2121 
2122 static const struct of_device_id sunxi_gpadc_of_match[] = {
2123 	{ .compatible = "allwinner,sunxi-gpadc"},
2124 	{},
2125 };
2126 MODULE_DEVICE_TABLE(of, sunxi_gpadc_of_match);
2127 
2128 static struct platform_driver sunxi_gpadc_driver = {
2129 	.probe  = sunxi_gpadc_probe,
2130 	.remove = sunxi_gpadc_remove,
2131 	.driver = {
2132 		.name   = "sunxi-gpadc",
2133 		.owner  = THIS_MODULE,
2134 		.pm = SUNXI_GPADC_DEV_PM_OPS,
2135 		.of_match_table = of_match_ptr(sunxi_gpadc_of_match),
2136 	},
2137 };
2138 module_param_named(debug_mask, debug_mask, int, 0644);
2139 
sunxi_gpadc_init(void)2140 static int __init sunxi_gpadc_init(void)
2141 {
2142 	int ret;
2143 	int i;
2144 	int err;
2145 
2146 	ret = class_register(&gpadc_class);
2147 	if (ret < 0)
2148 		pr_err("%s,%d err, ret:%d\n", __func__, __LINE__, ret);
2149 	else
2150 		pr_info("%s,%d, success\n", __func__, __LINE__);
2151 
2152 	for (i = 0; i < ARRAY_SIZE(gpadc_class_attrs); i++) {
2153 		err = class_create_file(&gpadc_class, &gpadc_class_attrs[i]);
2154 		if (err) {
2155 			pr_err("%s(): class_create_file() failed. err=%d\n", __func__, err);
2156 			while (i--)
2157 				class_remove_file(&gpadc_class, &gpadc_class_attrs[i]);
2158 			class_unregister(&gpadc_class);
2159 			return err;
2160 		}
2161 	}
2162 
2163 	ret = platform_driver_register(&sunxi_gpadc_driver);
2164 	if (ret != 0)
2165 		class_unregister(&gpadc_class);
2166 
2167 	return ret;
2168 }
2169 module_init(sunxi_gpadc_init);
2170 
sunxi_gpadc_exit(void)2171 static void __exit sunxi_gpadc_exit(void)
2172 {
2173 	platform_driver_unregister(&sunxi_gpadc_driver);
2174 	class_unregister(&gpadc_class);
2175 }
2176 module_exit(sunxi_gpadc_exit);
2177 
2178 MODULE_AUTHOR("Fuzhaoke");
2179 MODULE_DESCRIPTION("sunxi-gpadc driver");
2180 MODULE_LICENSE("GPL v2");
2181 MODULE_VERSION("1.1.0");
2182