1 // SPDX-License-Identifier: GPL-2.0
3 * ADC driver for the Ingenic JZ47xx SoCs
4 * Copyright (c) 2019 Artur Rojek <contact@artur-rojek.eu>
6 * based on drivers/mfd/jz4740-adc.c
9 #include <dt-bindings/iio/adc/ingenic,adc.h>
10 #include <linux/clk.h>
11 #include <linux/iio/buffer.h>
12 #include <linux/iio/iio.h>
13 #include <linux/interrupt.h>
15 #include <linux/iopoll.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/mutex.h>
21 #include <linux/platform_device.h>
22 #include <linux/property.h>
24 #define JZ_ADC_REG_ENABLE 0x00
25 #define JZ_ADC_REG_CFG 0x04
26 #define JZ_ADC_REG_CTRL 0x08
27 #define JZ_ADC_REG_STATUS 0x0c
28 #define JZ_ADC_REG_ADSAME 0x10
29 #define JZ_ADC_REG_ADWAIT 0x14
30 #define JZ_ADC_REG_ADTCH 0x18
31 #define JZ_ADC_REG_ADBDAT 0x1c
32 #define JZ_ADC_REG_ADSDAT 0x20
33 #define JZ_ADC_REG_ADCMD 0x24
34 #define JZ_ADC_REG_ADCLK 0x28
36 #define JZ_ADC_REG_ENABLE_PD BIT(7)
37 #define JZ_ADC_REG_CFG_AUX_MD (BIT(0) | BIT(1))
38 #define JZ_ADC_REG_CFG_BAT_MD BIT(4)
39 #define JZ_ADC_REG_CFG_SAMPLE_NUM(n) ((n) << 10)
40 #define JZ_ADC_REG_CFG_PULL_UP(n) ((n) << 16)
41 #define JZ_ADC_REG_CFG_CMD_SEL BIT(22)
42 #define JZ_ADC_REG_CFG_VBAT_SEL BIT(30)
43 #define JZ_ADC_REG_CFG_TOUCH_OPS_MASK (BIT(31) | GENMASK(23, 10))
44 #define JZ_ADC_REG_ADCLK_CLKDIV_LSB 0
45 #define JZ4725B_ADC_REG_ADCLK_CLKDIV10US_LSB 16
46 #define JZ4770_ADC_REG_ADCLK_CLKDIV10US_LSB 8
47 #define JZ4770_ADC_REG_ADCLK_CLKDIVMS_LSB 16
49 #define JZ_ADC_REG_ADCMD_YNADC BIT(7)
50 #define JZ_ADC_REG_ADCMD_YPADC BIT(8)
51 #define JZ_ADC_REG_ADCMD_XNADC BIT(9)
52 #define JZ_ADC_REG_ADCMD_XPADC BIT(10)
53 #define JZ_ADC_REG_ADCMD_VREFPYP BIT(11)
54 #define JZ_ADC_REG_ADCMD_VREFPXP BIT(12)
55 #define JZ_ADC_REG_ADCMD_VREFPXN BIT(13)
56 #define JZ_ADC_REG_ADCMD_VREFPAUX BIT(14)
57 #define JZ_ADC_REG_ADCMD_VREFPVDD33 BIT(15)
58 #define JZ_ADC_REG_ADCMD_VREFNYN BIT(16)
59 #define JZ_ADC_REG_ADCMD_VREFNXP BIT(17)
60 #define JZ_ADC_REG_ADCMD_VREFNXN BIT(18)
61 #define JZ_ADC_REG_ADCMD_VREFAUX BIT(19)
62 #define JZ_ADC_REG_ADCMD_YNGRU BIT(20)
63 #define JZ_ADC_REG_ADCMD_XNGRU BIT(21)
64 #define JZ_ADC_REG_ADCMD_XPGRU BIT(22)
65 #define JZ_ADC_REG_ADCMD_YPSUP BIT(23)
66 #define JZ_ADC_REG_ADCMD_XNSUP BIT(24)
67 #define JZ_ADC_REG_ADCMD_XPSUP BIT(25)
69 #define JZ_ADC_AUX_VREF 3300
70 #define JZ_ADC_AUX_VREF_BITS 12
71 #define JZ_ADC_BATTERY_LOW_VREF 2500
72 #define JZ_ADC_BATTERY_LOW_VREF_BITS 12
73 #define JZ4725B_ADC_BATTERY_HIGH_VREF 7500
74 #define JZ4725B_ADC_BATTERY_HIGH_VREF_BITS 10
75 #define JZ4740_ADC_BATTERY_HIGH_VREF (7500 * 0.986)
76 #define JZ4740_ADC_BATTERY_HIGH_VREF_BITS 12
77 #define JZ4760_ADC_BATTERY_VREF 2500
78 #define JZ4770_ADC_BATTERY_VREF 1200
79 #define JZ4770_ADC_BATTERY_VREF_BITS 12
81 #define JZ_ADC_IRQ_AUX BIT(0)
82 #define JZ_ADC_IRQ_BATTERY BIT(1)
83 #define JZ_ADC_IRQ_TOUCH BIT(2)
84 #define JZ_ADC_IRQ_PEN_DOWN BIT(3)
85 #define JZ_ADC_IRQ_PEN_UP BIT(4)
86 #define JZ_ADC_IRQ_PEN_DOWN_SLEEP BIT(5)
87 #define JZ_ADC_IRQ_SLEEP BIT(7)
91 struct ingenic_adc_soc_data
{
92 unsigned int battery_high_vref
;
93 unsigned int battery_high_vref_bits
;
94 const int *battery_raw_avail
;
95 size_t battery_raw_avail_size
;
96 const int *battery_scale_avail
;
97 size_t battery_scale_avail_size
;
98 unsigned int battery_vref_mode
: 1;
99 unsigned int has_aux_md
: 1;
100 const struct iio_chan_spec
*channels
;
101 unsigned int num_channels
;
102 int (*init_clk_div
)(struct device
*dev
, struct ingenic_adc
*adc
);
109 struct mutex aux_lock
;
110 const struct ingenic_adc_soc_data
*soc_data
;
114 static void ingenic_adc_set_adcmd(struct iio_dev
*iio_dev
, unsigned long mask
)
116 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
118 mutex_lock(&adc
->lock
);
121 readl(adc
->base
+ JZ_ADC_REG_ADCMD
);
124 /* Second channel (INGENIC_ADC_TOUCH_YP): sample YP vs. GND */
125 writel(JZ_ADC_REG_ADCMD_XNGRU
126 | JZ_ADC_REG_ADCMD_VREFNXN
| JZ_ADC_REG_ADCMD_VREFPVDD33
127 | JZ_ADC_REG_ADCMD_YPADC
,
128 adc
->base
+ JZ_ADC_REG_ADCMD
);
130 /* First channel (INGENIC_ADC_TOUCH_XP): sample XP vs. GND */
131 writel(JZ_ADC_REG_ADCMD_YNGRU
132 | JZ_ADC_REG_ADCMD_VREFNYN
| JZ_ADC_REG_ADCMD_VREFPVDD33
133 | JZ_ADC_REG_ADCMD_XPADC
,
134 adc
->base
+ JZ_ADC_REG_ADCMD
);
138 /* Fourth channel (INGENIC_ADC_TOUCH_YN): sample YN vs. GND */
139 writel(JZ_ADC_REG_ADCMD_XNGRU
140 | JZ_ADC_REG_ADCMD_VREFNXN
| JZ_ADC_REG_ADCMD_VREFPVDD33
141 | JZ_ADC_REG_ADCMD_YNADC
,
142 adc
->base
+ JZ_ADC_REG_ADCMD
);
144 /* Third channel (INGENIC_ADC_TOUCH_XN): sample XN vs. GND */
145 writel(JZ_ADC_REG_ADCMD_YNGRU
146 | JZ_ADC_REG_ADCMD_VREFNYN
| JZ_ADC_REG_ADCMD_VREFPVDD33
147 | JZ_ADC_REG_ADCMD_XNADC
,
148 adc
->base
+ JZ_ADC_REG_ADCMD
);
152 /* Sixth channel (INGENIC_ADC_TOUCH_YD): sample YP vs. YN */
153 writel(JZ_ADC_REG_ADCMD_VREFNYN
| JZ_ADC_REG_ADCMD_VREFPVDD33
154 | JZ_ADC_REG_ADCMD_YPADC
,
155 adc
->base
+ JZ_ADC_REG_ADCMD
);
157 /* Fifth channel (INGENIC_ADC_TOUCH_XD): sample XP vs. XN */
158 writel(JZ_ADC_REG_ADCMD_VREFNXN
| JZ_ADC_REG_ADCMD_VREFPVDD33
159 | JZ_ADC_REG_ADCMD_XPADC
,
160 adc
->base
+ JZ_ADC_REG_ADCMD
);
164 writel(0, adc
->base
+ JZ_ADC_REG_ADCMD
);
166 mutex_unlock(&adc
->lock
);
169 static void ingenic_adc_set_config(struct ingenic_adc
*adc
,
175 mutex_lock(&adc
->lock
);
177 cfg
= readl(adc
->base
+ JZ_ADC_REG_CFG
) & ~mask
;
179 writel(cfg
, adc
->base
+ JZ_ADC_REG_CFG
);
181 mutex_unlock(&adc
->lock
);
184 static void ingenic_adc_enable_unlocked(struct ingenic_adc
*adc
,
190 val
= readb(adc
->base
+ JZ_ADC_REG_ENABLE
);
197 writeb(val
, adc
->base
+ JZ_ADC_REG_ENABLE
);
200 static void ingenic_adc_enable(struct ingenic_adc
*adc
,
204 mutex_lock(&adc
->lock
);
205 ingenic_adc_enable_unlocked(adc
, engine
, enabled
);
206 mutex_unlock(&adc
->lock
);
209 static int ingenic_adc_capture(struct ingenic_adc
*adc
,
217 * Disable CMD_SEL temporarily, because it causes wrong VBAT readings,
218 * probably due to the switch of VREF. We must keep the lock here to
219 * avoid races with the buffer enable/disable functions.
221 mutex_lock(&adc
->lock
);
222 cfg
= readl(adc
->base
+ JZ_ADC_REG_CFG
);
223 writel(cfg
& ~JZ_ADC_REG_CFG_CMD_SEL
, adc
->base
+ JZ_ADC_REG_CFG
);
225 ingenic_adc_enable_unlocked(adc
, engine
, true);
226 ret
= readb_poll_timeout(adc
->base
+ JZ_ADC_REG_ENABLE
, val
,
227 !(val
& BIT(engine
)), 250, 1000);
229 ingenic_adc_enable_unlocked(adc
, engine
, false);
231 writel(cfg
, adc
->base
+ JZ_ADC_REG_CFG
);
232 mutex_unlock(&adc
->lock
);
237 static int ingenic_adc_write_raw(struct iio_dev
*iio_dev
,
238 struct iio_chan_spec
const *chan
,
243 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
244 struct device
*dev
= iio_dev
->dev
.parent
;
248 case IIO_CHAN_INFO_SCALE
:
249 switch (chan
->channel
) {
250 case INGENIC_ADC_BATTERY
:
251 if (!adc
->soc_data
->battery_vref_mode
)
254 ret
= clk_enable(adc
->clk
);
256 dev_err(dev
, "Failed to enable clock: %d\n",
261 if (val
> JZ_ADC_BATTERY_LOW_VREF
) {
262 ingenic_adc_set_config(adc
,
263 JZ_ADC_REG_CFG_BAT_MD
,
265 adc
->low_vref_mode
= false;
267 ingenic_adc_set_config(adc
,
268 JZ_ADC_REG_CFG_BAT_MD
,
269 JZ_ADC_REG_CFG_BAT_MD
);
270 adc
->low_vref_mode
= true;
273 clk_disable(adc
->clk
);
284 static const int jz4725b_adc_battery_raw_avail
[] = {
285 0, 1, (1 << JZ_ADC_BATTERY_LOW_VREF_BITS
) - 1,
288 static const int jz4725b_adc_battery_scale_avail
[] = {
289 JZ4725B_ADC_BATTERY_HIGH_VREF
, JZ4725B_ADC_BATTERY_HIGH_VREF_BITS
,
290 JZ_ADC_BATTERY_LOW_VREF
, JZ_ADC_BATTERY_LOW_VREF_BITS
,
293 static const int jz4740_adc_battery_raw_avail
[] = {
294 0, 1, (1 << JZ_ADC_BATTERY_LOW_VREF_BITS
) - 1,
297 static const int jz4740_adc_battery_scale_avail
[] = {
298 JZ4740_ADC_BATTERY_HIGH_VREF
, JZ4740_ADC_BATTERY_HIGH_VREF_BITS
,
299 JZ_ADC_BATTERY_LOW_VREF
, JZ_ADC_BATTERY_LOW_VREF_BITS
,
302 static const int jz4760_adc_battery_scale_avail
[] = {
303 JZ4760_ADC_BATTERY_VREF
, JZ4770_ADC_BATTERY_VREF_BITS
,
306 static const int jz4770_adc_battery_raw_avail
[] = {
307 0, 1, (1 << JZ4770_ADC_BATTERY_VREF_BITS
) - 1,
310 static const int jz4770_adc_battery_scale_avail
[] = {
311 JZ4770_ADC_BATTERY_VREF
, JZ4770_ADC_BATTERY_VREF_BITS
,
314 static int jz4725b_adc_init_clk_div(struct device
*dev
, struct ingenic_adc
*adc
)
316 struct clk
*parent_clk
;
317 unsigned long parent_rate
, rate
;
318 unsigned int div_main
, div_10us
;
320 parent_clk
= clk_get_parent(adc
->clk
);
322 dev_err(dev
, "ADC clock has no parent\n");
325 parent_rate
= clk_get_rate(parent_clk
);
328 * The JZ4725B ADC works at 500 kHz to 8 MHz.
329 * We pick the highest rate possible.
330 * In practice we typically get 6 MHz, half of the 12 MHz EXT clock.
332 div_main
= DIV_ROUND_UP(parent_rate
, 8000000);
333 div_main
= clamp(div_main
, 1u, 64u);
334 rate
= parent_rate
/ div_main
;
335 if (rate
< 500000 || rate
> 8000000) {
336 dev_err(dev
, "No valid divider for ADC main clock\n");
340 /* We also need a divider that produces a 10us clock. */
341 div_10us
= DIV_ROUND_UP(rate
, 100000);
343 writel(((div_10us
- 1) << JZ4725B_ADC_REG_ADCLK_CLKDIV10US_LSB
) |
344 (div_main
- 1) << JZ_ADC_REG_ADCLK_CLKDIV_LSB
,
345 adc
->base
+ JZ_ADC_REG_ADCLK
);
350 static int jz4770_adc_init_clk_div(struct device
*dev
, struct ingenic_adc
*adc
)
352 struct clk
*parent_clk
;
353 unsigned long parent_rate
, rate
;
354 unsigned int div_main
, div_ms
, div_10us
;
356 parent_clk
= clk_get_parent(adc
->clk
);
358 dev_err(dev
, "ADC clock has no parent\n");
361 parent_rate
= clk_get_rate(parent_clk
);
364 * The JZ4770 ADC works at 20 kHz to 200 kHz.
365 * We pick the highest rate possible.
367 div_main
= DIV_ROUND_UP(parent_rate
, 200000);
368 div_main
= clamp(div_main
, 1u, 256u);
369 rate
= parent_rate
/ div_main
;
370 if (rate
< 20000 || rate
> 200000) {
371 dev_err(dev
, "No valid divider for ADC main clock\n");
375 /* We also need a divider that produces a 10us clock. */
376 div_10us
= DIV_ROUND_UP(rate
, 10000);
377 /* And another, which produces a 1ms clock. */
378 div_ms
= DIV_ROUND_UP(rate
, 1000);
380 writel(((div_ms
- 1) << JZ4770_ADC_REG_ADCLK_CLKDIVMS_LSB
) |
381 ((div_10us
- 1) << JZ4770_ADC_REG_ADCLK_CLKDIV10US_LSB
) |
382 (div_main
- 1) << JZ_ADC_REG_ADCLK_CLKDIV_LSB
,
383 adc
->base
+ JZ_ADC_REG_ADCLK
);
388 static const struct iio_chan_spec jz4740_channels
[] = {
390 .extend_name
= "aux",
392 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
393 BIT(IIO_CHAN_INFO_SCALE
),
395 .channel
= INGENIC_ADC_AUX
,
399 .extend_name
= "battery",
401 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
402 BIT(IIO_CHAN_INFO_SCALE
),
403 .info_mask_separate_available
= BIT(IIO_CHAN_INFO_RAW
) |
404 BIT(IIO_CHAN_INFO_SCALE
),
406 .channel
= INGENIC_ADC_BATTERY
,
411 static const struct iio_chan_spec jz4760_channels
[] = {
413 .extend_name
= "aux",
415 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
416 BIT(IIO_CHAN_INFO_SCALE
),
418 .channel
= INGENIC_ADC_AUX0
,
422 .extend_name
= "aux1",
424 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
425 BIT(IIO_CHAN_INFO_SCALE
),
427 .channel
= INGENIC_ADC_AUX
,
431 .extend_name
= "aux2",
433 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
434 BIT(IIO_CHAN_INFO_SCALE
),
436 .channel
= INGENIC_ADC_AUX2
,
440 .extend_name
= "battery",
442 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
443 BIT(IIO_CHAN_INFO_SCALE
),
444 .info_mask_separate_available
= BIT(IIO_CHAN_INFO_RAW
) |
445 BIT(IIO_CHAN_INFO_SCALE
),
447 .channel
= INGENIC_ADC_BATTERY
,
452 static const struct iio_chan_spec jz4770_channels
[] = {
456 .channel
= INGENIC_ADC_TOUCH_XP
,
467 .channel
= INGENIC_ADC_TOUCH_YP
,
478 .channel
= INGENIC_ADC_TOUCH_XN
,
489 .channel
= INGENIC_ADC_TOUCH_YN
,
500 .channel
= INGENIC_ADC_TOUCH_XD
,
511 .channel
= INGENIC_ADC_TOUCH_YD
,
520 .extend_name
= "aux",
522 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
523 BIT(IIO_CHAN_INFO_SCALE
),
525 .channel
= INGENIC_ADC_AUX
,
529 .extend_name
= "battery",
531 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
532 BIT(IIO_CHAN_INFO_SCALE
),
533 .info_mask_separate_available
= BIT(IIO_CHAN_INFO_RAW
) |
534 BIT(IIO_CHAN_INFO_SCALE
),
536 .channel
= INGENIC_ADC_BATTERY
,
540 .extend_name
= "aux2",
542 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
543 BIT(IIO_CHAN_INFO_SCALE
),
545 .channel
= INGENIC_ADC_AUX2
,
550 static const struct ingenic_adc_soc_data jz4725b_adc_soc_data
= {
551 .battery_high_vref
= JZ4725B_ADC_BATTERY_HIGH_VREF
,
552 .battery_high_vref_bits
= JZ4725B_ADC_BATTERY_HIGH_VREF_BITS
,
553 .battery_raw_avail
= jz4725b_adc_battery_raw_avail
,
554 .battery_raw_avail_size
= ARRAY_SIZE(jz4725b_adc_battery_raw_avail
),
555 .battery_scale_avail
= jz4725b_adc_battery_scale_avail
,
556 .battery_scale_avail_size
= ARRAY_SIZE(jz4725b_adc_battery_scale_avail
),
557 .battery_vref_mode
= true,
559 .channels
= jz4740_channels
,
560 .num_channels
= ARRAY_SIZE(jz4740_channels
),
561 .init_clk_div
= jz4725b_adc_init_clk_div
,
564 static const struct ingenic_adc_soc_data jz4740_adc_soc_data
= {
565 .battery_high_vref
= JZ4740_ADC_BATTERY_HIGH_VREF
,
566 .battery_high_vref_bits
= JZ4740_ADC_BATTERY_HIGH_VREF_BITS
,
567 .battery_raw_avail
= jz4740_adc_battery_raw_avail
,
568 .battery_raw_avail_size
= ARRAY_SIZE(jz4740_adc_battery_raw_avail
),
569 .battery_scale_avail
= jz4740_adc_battery_scale_avail
,
570 .battery_scale_avail_size
= ARRAY_SIZE(jz4740_adc_battery_scale_avail
),
571 .battery_vref_mode
= true,
573 .channels
= jz4740_channels
,
574 .num_channels
= ARRAY_SIZE(jz4740_channels
),
575 .init_clk_div
= NULL
, /* no ADCLK register on JZ4740 */
578 static const struct ingenic_adc_soc_data jz4760_adc_soc_data
= {
579 .battery_high_vref
= JZ4760_ADC_BATTERY_VREF
,
580 .battery_high_vref_bits
= JZ4770_ADC_BATTERY_VREF_BITS
,
581 .battery_raw_avail
= jz4770_adc_battery_raw_avail
,
582 .battery_raw_avail_size
= ARRAY_SIZE(jz4770_adc_battery_raw_avail
),
583 .battery_scale_avail
= jz4760_adc_battery_scale_avail
,
584 .battery_scale_avail_size
= ARRAY_SIZE(jz4760_adc_battery_scale_avail
),
585 .battery_vref_mode
= false,
587 .channels
= jz4760_channels
,
588 .num_channels
= ARRAY_SIZE(jz4760_channels
),
589 .init_clk_div
= jz4770_adc_init_clk_div
,
592 static const struct ingenic_adc_soc_data jz4770_adc_soc_data
= {
593 .battery_high_vref
= JZ4770_ADC_BATTERY_VREF
,
594 .battery_high_vref_bits
= JZ4770_ADC_BATTERY_VREF_BITS
,
595 .battery_raw_avail
= jz4770_adc_battery_raw_avail
,
596 .battery_raw_avail_size
= ARRAY_SIZE(jz4770_adc_battery_raw_avail
),
597 .battery_scale_avail
= jz4770_adc_battery_scale_avail
,
598 .battery_scale_avail_size
= ARRAY_SIZE(jz4770_adc_battery_scale_avail
),
599 .battery_vref_mode
= false,
601 .channels
= jz4770_channels
,
602 .num_channels
= ARRAY_SIZE(jz4770_channels
),
603 .init_clk_div
= jz4770_adc_init_clk_div
,
606 static int ingenic_adc_read_avail(struct iio_dev
*iio_dev
,
607 struct iio_chan_spec
const *chan
,
613 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
616 case IIO_CHAN_INFO_RAW
:
618 *length
= adc
->soc_data
->battery_raw_avail_size
;
619 *vals
= adc
->soc_data
->battery_raw_avail
;
620 return IIO_AVAIL_RANGE
;
621 case IIO_CHAN_INFO_SCALE
:
622 *type
= IIO_VAL_FRACTIONAL_LOG2
;
623 *length
= adc
->soc_data
->battery_scale_avail_size
;
624 *vals
= adc
->soc_data
->battery_scale_avail
;
625 return IIO_AVAIL_LIST
;
631 static int ingenic_adc_read_chan_info_raw(struct iio_dev
*iio_dev
,
632 struct iio_chan_spec
const *chan
,
635 int cmd
, ret
, engine
= (chan
->channel
== INGENIC_ADC_BATTERY
);
636 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
638 ret
= clk_enable(adc
->clk
);
640 dev_err(iio_dev
->dev
.parent
, "Failed to enable clock: %d\n",
645 /* We cannot sample the aux channels in parallel. */
646 mutex_lock(&adc
->aux_lock
);
647 if (adc
->soc_data
->has_aux_md
&& engine
== 0) {
648 switch (chan
->channel
) {
649 case INGENIC_ADC_AUX0
:
652 case INGENIC_ADC_AUX
:
655 case INGENIC_ADC_AUX2
:
660 ingenic_adc_set_config(adc
, JZ_ADC_REG_CFG_AUX_MD
, cmd
);
663 ret
= ingenic_adc_capture(adc
, engine
);
667 switch (chan
->channel
) {
668 case INGENIC_ADC_AUX0
:
669 case INGENIC_ADC_AUX
:
670 case INGENIC_ADC_AUX2
:
671 *val
= readw(adc
->base
+ JZ_ADC_REG_ADSDAT
);
673 case INGENIC_ADC_BATTERY
:
674 *val
= readw(adc
->base
+ JZ_ADC_REG_ADBDAT
);
680 mutex_unlock(&adc
->aux_lock
);
681 clk_disable(adc
->clk
);
686 static int ingenic_adc_read_raw(struct iio_dev
*iio_dev
,
687 struct iio_chan_spec
const *chan
,
692 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
695 case IIO_CHAN_INFO_RAW
:
696 return ingenic_adc_read_chan_info_raw(iio_dev
, chan
, val
);
697 case IIO_CHAN_INFO_SCALE
:
698 switch (chan
->channel
) {
699 case INGENIC_ADC_AUX0
:
700 case INGENIC_ADC_AUX
:
701 case INGENIC_ADC_AUX2
:
702 *val
= JZ_ADC_AUX_VREF
;
703 *val2
= JZ_ADC_AUX_VREF_BITS
;
705 case INGENIC_ADC_BATTERY
:
706 if (adc
->low_vref_mode
) {
707 *val
= JZ_ADC_BATTERY_LOW_VREF
;
708 *val2
= JZ_ADC_BATTERY_LOW_VREF_BITS
;
710 *val
= adc
->soc_data
->battery_high_vref
;
711 *val2
= adc
->soc_data
->battery_high_vref_bits
;
716 return IIO_VAL_FRACTIONAL_LOG2
;
722 static int ingenic_adc_fwnode_xlate(struct iio_dev
*iio_dev
,
723 const struct fwnode_reference_args
*iiospec
)
730 for (i
= 0; i
< iio_dev
->num_channels
; ++i
)
731 if (iio_dev
->channels
[i
].channel
== iiospec
->args
[0])
737 static const struct iio_info ingenic_adc_info
= {
738 .write_raw
= ingenic_adc_write_raw
,
739 .read_raw
= ingenic_adc_read_raw
,
740 .read_avail
= ingenic_adc_read_avail
,
741 .fwnode_xlate
= ingenic_adc_fwnode_xlate
,
744 static int ingenic_adc_buffer_enable(struct iio_dev
*iio_dev
)
746 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
749 ret
= clk_enable(adc
->clk
);
751 dev_err(iio_dev
->dev
.parent
, "Failed to enable clock: %d\n",
756 /* It takes significant time for the touchscreen hw to stabilize. */
758 ingenic_adc_set_config(adc
, JZ_ADC_REG_CFG_TOUCH_OPS_MASK
,
759 JZ_ADC_REG_CFG_SAMPLE_NUM(4) |
760 JZ_ADC_REG_CFG_PULL_UP(4));
762 writew(80, adc
->base
+ JZ_ADC_REG_ADWAIT
);
763 writew(2, adc
->base
+ JZ_ADC_REG_ADSAME
);
764 writeb((u8
)~JZ_ADC_IRQ_TOUCH
, adc
->base
+ JZ_ADC_REG_CTRL
);
765 writel(0, adc
->base
+ JZ_ADC_REG_ADTCH
);
767 ingenic_adc_set_config(adc
, JZ_ADC_REG_CFG_CMD_SEL
,
768 JZ_ADC_REG_CFG_CMD_SEL
);
769 ingenic_adc_set_adcmd(iio_dev
, iio_dev
->active_scan_mask
[0]);
771 ingenic_adc_enable(adc
, 2, true);
776 static int ingenic_adc_buffer_disable(struct iio_dev
*iio_dev
)
778 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
780 ingenic_adc_enable(adc
, 2, false);
782 ingenic_adc_set_config(adc
, JZ_ADC_REG_CFG_CMD_SEL
, 0);
784 writeb(0xff, adc
->base
+ JZ_ADC_REG_CTRL
);
785 writeb(0xff, adc
->base
+ JZ_ADC_REG_STATUS
);
786 ingenic_adc_set_config(adc
, JZ_ADC_REG_CFG_TOUCH_OPS_MASK
, 0);
787 writew(0, adc
->base
+ JZ_ADC_REG_ADSAME
);
788 writew(0, adc
->base
+ JZ_ADC_REG_ADWAIT
);
789 clk_disable(adc
->clk
);
794 static const struct iio_buffer_setup_ops ingenic_buffer_setup_ops
= {
795 .postenable
= &ingenic_adc_buffer_enable
,
796 .predisable
= &ingenic_adc_buffer_disable
799 static irqreturn_t
ingenic_adc_irq(int irq
, void *data
)
801 struct iio_dev
*iio_dev
= data
;
802 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
803 unsigned long mask
= iio_dev
->active_scan_mask
[0];
807 for (i
= 0; i
< ARRAY_SIZE(tdat
); mask
>>= 2, i
++) {
809 tdat
[i
] = readl(adc
->base
+ JZ_ADC_REG_ADTCH
);
814 iio_push_to_buffers(iio_dev
, tdat
);
815 writeb(JZ_ADC_IRQ_TOUCH
, adc
->base
+ JZ_ADC_REG_STATUS
);
820 static int ingenic_adc_probe(struct platform_device
*pdev
)
822 struct device
*dev
= &pdev
->dev
;
823 struct iio_dev
*iio_dev
;
824 struct ingenic_adc
*adc
;
825 const struct ingenic_adc_soc_data
*soc_data
;
828 soc_data
= device_get_match_data(dev
);
832 iio_dev
= devm_iio_device_alloc(dev
, sizeof(*adc
));
836 adc
= iio_priv(iio_dev
);
837 mutex_init(&adc
->lock
);
838 mutex_init(&adc
->aux_lock
);
839 adc
->soc_data
= soc_data
;
841 irq
= platform_get_irq(pdev
, 0);
845 ret
= devm_request_irq(dev
, irq
, ingenic_adc_irq
, 0,
846 dev_name(dev
), iio_dev
);
848 dev_err(dev
, "Failed to request irq: %d\n", ret
);
852 adc
->base
= devm_platform_ioremap_resource(pdev
, 0);
853 if (IS_ERR(adc
->base
))
854 return PTR_ERR(adc
->base
);
856 adc
->clk
= devm_clk_get_prepared(dev
, "adc");
857 if (IS_ERR(adc
->clk
)) {
858 dev_err(dev
, "Unable to get clock\n");
859 return PTR_ERR(adc
->clk
);
862 ret
= clk_enable(adc
->clk
);
864 dev_err(dev
, "Failed to enable clock\n");
868 /* Set clock dividers. */
869 if (soc_data
->init_clk_div
) {
870 ret
= soc_data
->init_clk_div(dev
, adc
);
872 clk_disable_unprepare(adc
->clk
);
877 /* Put hardware in a known passive state. */
878 writeb(0x00, adc
->base
+ JZ_ADC_REG_ENABLE
);
879 writeb(0xff, adc
->base
+ JZ_ADC_REG_CTRL
);
881 /* JZ4760B specific */
882 if (device_property_present(dev
, "ingenic,use-internal-divider"))
883 ingenic_adc_set_config(adc
, JZ_ADC_REG_CFG_VBAT_SEL
,
884 JZ_ADC_REG_CFG_VBAT_SEL
);
886 ingenic_adc_set_config(adc
, JZ_ADC_REG_CFG_VBAT_SEL
, 0);
888 usleep_range(2000, 3000); /* Must wait at least 2ms. */
889 clk_disable(adc
->clk
);
891 iio_dev
->name
= "jz-adc";
892 iio_dev
->modes
= INDIO_DIRECT_MODE
| INDIO_BUFFER_SOFTWARE
;
893 iio_dev
->setup_ops
= &ingenic_buffer_setup_ops
;
894 iio_dev
->channels
= soc_data
->channels
;
895 iio_dev
->num_channels
= soc_data
->num_channels
;
896 iio_dev
->info
= &ingenic_adc_info
;
898 ret
= devm_iio_device_register(dev
, iio_dev
);
900 dev_err(dev
, "Unable to register IIO device\n");
905 static const struct of_device_id ingenic_adc_of_match
[] = {
906 { .compatible
= "ingenic,jz4725b-adc", .data
= &jz4725b_adc_soc_data
, },
907 { .compatible
= "ingenic,jz4740-adc", .data
= &jz4740_adc_soc_data
, },
908 { .compatible
= "ingenic,jz4760-adc", .data
= &jz4760_adc_soc_data
, },
909 { .compatible
= "ingenic,jz4760b-adc", .data
= &jz4760_adc_soc_data
, },
910 { .compatible
= "ingenic,jz4770-adc", .data
= &jz4770_adc_soc_data
, },
913 MODULE_DEVICE_TABLE(of
, ingenic_adc_of_match
);
915 static struct platform_driver ingenic_adc_driver
= {
917 .name
= "ingenic-adc",
918 .of_match_table
= ingenic_adc_of_match
,
920 .probe
= ingenic_adc_probe
,
922 module_platform_driver(ingenic_adc_driver
);
923 MODULE_DESCRIPTION("ADC driver for the Ingenic JZ47xx SoCs");
924 MODULE_LICENSE("GPL v2");