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>
20 #include <linux/platform_device.h>
22 #define JZ_ADC_REG_ENABLE 0x00
23 #define JZ_ADC_REG_CFG 0x04
24 #define JZ_ADC_REG_CTRL 0x08
25 #define JZ_ADC_REG_STATUS 0x0c
26 #define JZ_ADC_REG_ADSAME 0x10
27 #define JZ_ADC_REG_ADWAIT 0x14
28 #define JZ_ADC_REG_ADTCH 0x18
29 #define JZ_ADC_REG_ADBDAT 0x1c
30 #define JZ_ADC_REG_ADSDAT 0x20
31 #define JZ_ADC_REG_ADCMD 0x24
32 #define JZ_ADC_REG_ADCLK 0x28
34 #define JZ_ADC_REG_ENABLE_PD BIT(7)
35 #define JZ_ADC_REG_CFG_AUX_MD (BIT(0) | BIT(1))
36 #define JZ_ADC_REG_CFG_BAT_MD BIT(4)
37 #define JZ_ADC_REG_CFG_SAMPLE_NUM(n) ((n) << 10)
38 #define JZ_ADC_REG_CFG_PULL_UP(n) ((n) << 16)
39 #define JZ_ADC_REG_CFG_CMD_SEL BIT(22)
40 #define JZ_ADC_REG_CFG_TOUCH_OPS_MASK (BIT(31) | GENMASK(23, 10))
41 #define JZ_ADC_REG_ADCLK_CLKDIV_LSB 0
42 #define JZ4725B_ADC_REG_ADCLK_CLKDIV10US_LSB 16
43 #define JZ4770_ADC_REG_ADCLK_CLKDIV10US_LSB 8
44 #define JZ4770_ADC_REG_ADCLK_CLKDIVMS_LSB 16
46 #define JZ_ADC_REG_ADCMD_YNADC BIT(7)
47 #define JZ_ADC_REG_ADCMD_YPADC BIT(8)
48 #define JZ_ADC_REG_ADCMD_XNADC BIT(9)
49 #define JZ_ADC_REG_ADCMD_XPADC BIT(10)
50 #define JZ_ADC_REG_ADCMD_VREFPYP BIT(11)
51 #define JZ_ADC_REG_ADCMD_VREFPXP BIT(12)
52 #define JZ_ADC_REG_ADCMD_VREFPXN BIT(13)
53 #define JZ_ADC_REG_ADCMD_VREFPAUX BIT(14)
54 #define JZ_ADC_REG_ADCMD_VREFPVDD33 BIT(15)
55 #define JZ_ADC_REG_ADCMD_VREFNYN BIT(16)
56 #define JZ_ADC_REG_ADCMD_VREFNXP BIT(17)
57 #define JZ_ADC_REG_ADCMD_VREFNXN BIT(18)
58 #define JZ_ADC_REG_ADCMD_VREFAUX BIT(19)
59 #define JZ_ADC_REG_ADCMD_YNGRU BIT(20)
60 #define JZ_ADC_REG_ADCMD_XNGRU BIT(21)
61 #define JZ_ADC_REG_ADCMD_XPGRU BIT(22)
62 #define JZ_ADC_REG_ADCMD_YPSUP BIT(23)
63 #define JZ_ADC_REG_ADCMD_XNSUP BIT(24)
64 #define JZ_ADC_REG_ADCMD_XPSUP BIT(25)
66 #define JZ_ADC_AUX_VREF 3300
67 #define JZ_ADC_AUX_VREF_BITS 12
68 #define JZ_ADC_BATTERY_LOW_VREF 2500
69 #define JZ_ADC_BATTERY_LOW_VREF_BITS 12
70 #define JZ4725B_ADC_BATTERY_HIGH_VREF 7500
71 #define JZ4725B_ADC_BATTERY_HIGH_VREF_BITS 10
72 #define JZ4740_ADC_BATTERY_HIGH_VREF (7500 * 0.986)
73 #define JZ4740_ADC_BATTERY_HIGH_VREF_BITS 12
74 #define JZ4770_ADC_BATTERY_VREF 1200
75 #define JZ4770_ADC_BATTERY_VREF_BITS 12
77 #define JZ_ADC_IRQ_AUX BIT(0)
78 #define JZ_ADC_IRQ_BATTERY BIT(1)
79 #define JZ_ADC_IRQ_TOUCH BIT(2)
80 #define JZ_ADC_IRQ_PEN_DOWN BIT(3)
81 #define JZ_ADC_IRQ_PEN_UP BIT(4)
82 #define JZ_ADC_IRQ_PEN_DOWN_SLEEP BIT(5)
83 #define JZ_ADC_IRQ_SLEEP BIT(7)
87 struct ingenic_adc_soc_data
{
88 unsigned int battery_high_vref
;
89 unsigned int battery_high_vref_bits
;
90 const int *battery_raw_avail
;
91 size_t battery_raw_avail_size
;
92 const int *battery_scale_avail
;
93 size_t battery_scale_avail_size
;
94 unsigned int battery_vref_mode
: 1;
95 unsigned int has_aux2
: 1;
96 const struct iio_chan_spec
*channels
;
97 unsigned int num_channels
;
98 int (*init_clk_div
)(struct device
*dev
, struct ingenic_adc
*adc
);
105 struct mutex aux_lock
;
106 const struct ingenic_adc_soc_data
*soc_data
;
110 static void ingenic_adc_set_adcmd(struct iio_dev
*iio_dev
, unsigned long mask
)
112 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
114 mutex_lock(&adc
->lock
);
117 readl(adc
->base
+ JZ_ADC_REG_ADCMD
);
120 /* Second channel (INGENIC_ADC_TOUCH_YP): sample YP vs. GND */
121 writel(JZ_ADC_REG_ADCMD_XNGRU
122 | JZ_ADC_REG_ADCMD_VREFNXN
| JZ_ADC_REG_ADCMD_VREFPVDD33
123 | JZ_ADC_REG_ADCMD_YPADC
,
124 adc
->base
+ JZ_ADC_REG_ADCMD
);
126 /* First channel (INGENIC_ADC_TOUCH_XP): sample XP vs. GND */
127 writel(JZ_ADC_REG_ADCMD_YNGRU
128 | JZ_ADC_REG_ADCMD_VREFNYN
| JZ_ADC_REG_ADCMD_VREFPVDD33
129 | JZ_ADC_REG_ADCMD_XPADC
,
130 adc
->base
+ JZ_ADC_REG_ADCMD
);
134 /* Fourth channel (INGENIC_ADC_TOUCH_YN): sample YN vs. GND */
135 writel(JZ_ADC_REG_ADCMD_XNGRU
136 | JZ_ADC_REG_ADCMD_VREFNXN
| JZ_ADC_REG_ADCMD_VREFPVDD33
137 | JZ_ADC_REG_ADCMD_YNADC
,
138 adc
->base
+ JZ_ADC_REG_ADCMD
);
140 /* Third channel (INGENIC_ADC_TOUCH_XN): sample XN vs. GND */
141 writel(JZ_ADC_REG_ADCMD_YNGRU
142 | JZ_ADC_REG_ADCMD_VREFNYN
| JZ_ADC_REG_ADCMD_VREFPVDD33
143 | JZ_ADC_REG_ADCMD_XNADC
,
144 adc
->base
+ JZ_ADC_REG_ADCMD
);
148 /* Sixth channel (INGENIC_ADC_TOUCH_YD): sample YP vs. YN */
149 writel(JZ_ADC_REG_ADCMD_VREFNYN
| JZ_ADC_REG_ADCMD_VREFPVDD33
150 | JZ_ADC_REG_ADCMD_YPADC
,
151 adc
->base
+ JZ_ADC_REG_ADCMD
);
153 /* Fifth channel (INGENIC_ADC_TOUCH_XD): sample XP vs. XN */
154 writel(JZ_ADC_REG_ADCMD_VREFNXN
| JZ_ADC_REG_ADCMD_VREFPVDD33
155 | JZ_ADC_REG_ADCMD_XPADC
,
156 adc
->base
+ JZ_ADC_REG_ADCMD
);
160 writel(0, adc
->base
+ JZ_ADC_REG_ADCMD
);
162 mutex_unlock(&adc
->lock
);
165 static void ingenic_adc_set_config(struct ingenic_adc
*adc
,
171 mutex_lock(&adc
->lock
);
173 cfg
= readl(adc
->base
+ JZ_ADC_REG_CFG
) & ~mask
;
175 writel(cfg
, adc
->base
+ JZ_ADC_REG_CFG
);
177 mutex_unlock(&adc
->lock
);
180 static void ingenic_adc_enable_unlocked(struct ingenic_adc
*adc
,
186 val
= readb(adc
->base
+ JZ_ADC_REG_ENABLE
);
193 writeb(val
, adc
->base
+ JZ_ADC_REG_ENABLE
);
196 static void ingenic_adc_enable(struct ingenic_adc
*adc
,
200 mutex_lock(&adc
->lock
);
201 ingenic_adc_enable_unlocked(adc
, engine
, enabled
);
202 mutex_unlock(&adc
->lock
);
205 static int ingenic_adc_capture(struct ingenic_adc
*adc
,
213 * Disable CMD_SEL temporarily, because it causes wrong VBAT readings,
214 * probably due to the switch of VREF. We must keep the lock here to
215 * avoid races with the buffer enable/disable functions.
217 mutex_lock(&adc
->lock
);
218 cfg
= readl(adc
->base
+ JZ_ADC_REG_CFG
);
219 writel(cfg
& ~JZ_ADC_REG_CFG_CMD_SEL
, adc
->base
+ JZ_ADC_REG_CFG
);
221 ingenic_adc_enable_unlocked(adc
, engine
, true);
222 ret
= readb_poll_timeout(adc
->base
+ JZ_ADC_REG_ENABLE
, val
,
223 !(val
& BIT(engine
)), 250, 1000);
225 ingenic_adc_enable_unlocked(adc
, engine
, false);
227 writel(cfg
, adc
->base
+ JZ_ADC_REG_CFG
);
228 mutex_unlock(&adc
->lock
);
233 static int ingenic_adc_write_raw(struct iio_dev
*iio_dev
,
234 struct iio_chan_spec
const *chan
,
239 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
240 struct device
*dev
= iio_dev
->dev
.parent
;
244 case IIO_CHAN_INFO_SCALE
:
245 switch (chan
->channel
) {
246 case INGENIC_ADC_BATTERY
:
247 if (!adc
->soc_data
->battery_vref_mode
)
250 ret
= clk_enable(adc
->clk
);
252 dev_err(dev
, "Failed to enable clock: %d\n",
257 if (val
> JZ_ADC_BATTERY_LOW_VREF
) {
258 ingenic_adc_set_config(adc
,
259 JZ_ADC_REG_CFG_BAT_MD
,
261 adc
->low_vref_mode
= false;
263 ingenic_adc_set_config(adc
,
264 JZ_ADC_REG_CFG_BAT_MD
,
265 JZ_ADC_REG_CFG_BAT_MD
);
266 adc
->low_vref_mode
= true;
269 clk_disable(adc
->clk
);
280 static const int jz4725b_adc_battery_raw_avail
[] = {
281 0, 1, (1 << JZ_ADC_BATTERY_LOW_VREF_BITS
) - 1,
284 static const int jz4725b_adc_battery_scale_avail
[] = {
285 JZ4725B_ADC_BATTERY_HIGH_VREF
, JZ4725B_ADC_BATTERY_HIGH_VREF_BITS
,
286 JZ_ADC_BATTERY_LOW_VREF
, JZ_ADC_BATTERY_LOW_VREF_BITS
,
289 static const int jz4740_adc_battery_raw_avail
[] = {
290 0, 1, (1 << JZ_ADC_BATTERY_LOW_VREF_BITS
) - 1,
293 static const int jz4740_adc_battery_scale_avail
[] = {
294 JZ4740_ADC_BATTERY_HIGH_VREF
, JZ4740_ADC_BATTERY_HIGH_VREF_BITS
,
295 JZ_ADC_BATTERY_LOW_VREF
, JZ_ADC_BATTERY_LOW_VREF_BITS
,
298 static const int jz4770_adc_battery_raw_avail
[] = {
299 0, 1, (1 << JZ4770_ADC_BATTERY_VREF_BITS
) - 1,
302 static const int jz4770_adc_battery_scale_avail
[] = {
303 JZ4770_ADC_BATTERY_VREF
, JZ4770_ADC_BATTERY_VREF_BITS
,
306 static int jz4725b_adc_init_clk_div(struct device
*dev
, struct ingenic_adc
*adc
)
308 struct clk
*parent_clk
;
309 unsigned long parent_rate
, rate
;
310 unsigned int div_main
, div_10us
;
312 parent_clk
= clk_get_parent(adc
->clk
);
314 dev_err(dev
, "ADC clock has no parent\n");
317 parent_rate
= clk_get_rate(parent_clk
);
320 * The JZ4725B ADC works at 500 kHz to 8 MHz.
321 * We pick the highest rate possible.
322 * In practice we typically get 6 MHz, half of the 12 MHz EXT clock.
324 div_main
= DIV_ROUND_UP(parent_rate
, 8000000);
325 div_main
= clamp(div_main
, 1u, 64u);
326 rate
= parent_rate
/ div_main
;
327 if (rate
< 500000 || rate
> 8000000) {
328 dev_err(dev
, "No valid divider for ADC main clock\n");
332 /* We also need a divider that produces a 10us clock. */
333 div_10us
= DIV_ROUND_UP(rate
, 100000);
335 writel(((div_10us
- 1) << JZ4725B_ADC_REG_ADCLK_CLKDIV10US_LSB
) |
336 (div_main
- 1) << JZ_ADC_REG_ADCLK_CLKDIV_LSB
,
337 adc
->base
+ JZ_ADC_REG_ADCLK
);
342 static int jz4770_adc_init_clk_div(struct device
*dev
, struct ingenic_adc
*adc
)
344 struct clk
*parent_clk
;
345 unsigned long parent_rate
, rate
;
346 unsigned int div_main
, div_ms
, div_10us
;
348 parent_clk
= clk_get_parent(adc
->clk
);
350 dev_err(dev
, "ADC clock has no parent\n");
353 parent_rate
= clk_get_rate(parent_clk
);
356 * The JZ4770 ADC works at 20 kHz to 200 kHz.
357 * We pick the highest rate possible.
359 div_main
= DIV_ROUND_UP(parent_rate
, 200000);
360 div_main
= clamp(div_main
, 1u, 256u);
361 rate
= parent_rate
/ div_main
;
362 if (rate
< 20000 || rate
> 200000) {
363 dev_err(dev
, "No valid divider for ADC main clock\n");
367 /* We also need a divider that produces a 10us clock. */
368 div_10us
= DIV_ROUND_UP(rate
, 10000);
369 /* And another, which produces a 1ms clock. */
370 div_ms
= DIV_ROUND_UP(rate
, 1000);
372 writel(((div_ms
- 1) << JZ4770_ADC_REG_ADCLK_CLKDIVMS_LSB
) |
373 ((div_10us
- 1) << JZ4770_ADC_REG_ADCLK_CLKDIV10US_LSB
) |
374 (div_main
- 1) << JZ_ADC_REG_ADCLK_CLKDIV_LSB
,
375 adc
->base
+ JZ_ADC_REG_ADCLK
);
380 static const struct iio_chan_spec jz4740_channels
[] = {
382 .extend_name
= "aux",
384 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
385 BIT(IIO_CHAN_INFO_SCALE
),
387 .channel
= INGENIC_ADC_AUX
,
391 .extend_name
= "battery",
393 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
394 BIT(IIO_CHAN_INFO_SCALE
),
395 .info_mask_separate_available
= BIT(IIO_CHAN_INFO_RAW
) |
396 BIT(IIO_CHAN_INFO_SCALE
),
398 .channel
= INGENIC_ADC_BATTERY
,
403 static const struct iio_chan_spec jz4770_channels
[] = {
407 .channel
= INGENIC_ADC_TOUCH_XP
,
418 .channel
= INGENIC_ADC_TOUCH_YP
,
429 .channel
= INGENIC_ADC_TOUCH_XN
,
440 .channel
= INGENIC_ADC_TOUCH_YN
,
451 .channel
= INGENIC_ADC_TOUCH_XD
,
462 .channel
= INGENIC_ADC_TOUCH_YD
,
471 .extend_name
= "aux",
473 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
474 BIT(IIO_CHAN_INFO_SCALE
),
476 .channel
= INGENIC_ADC_AUX
,
480 .extend_name
= "battery",
482 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
483 BIT(IIO_CHAN_INFO_SCALE
),
484 .info_mask_separate_available
= BIT(IIO_CHAN_INFO_RAW
) |
485 BIT(IIO_CHAN_INFO_SCALE
),
487 .channel
= INGENIC_ADC_BATTERY
,
491 .extend_name
= "aux2",
493 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
494 BIT(IIO_CHAN_INFO_SCALE
),
496 .channel
= INGENIC_ADC_AUX2
,
501 static const struct ingenic_adc_soc_data jz4725b_adc_soc_data
= {
502 .battery_high_vref
= JZ4725B_ADC_BATTERY_HIGH_VREF
,
503 .battery_high_vref_bits
= JZ4725B_ADC_BATTERY_HIGH_VREF_BITS
,
504 .battery_raw_avail
= jz4725b_adc_battery_raw_avail
,
505 .battery_raw_avail_size
= ARRAY_SIZE(jz4725b_adc_battery_raw_avail
),
506 .battery_scale_avail
= jz4725b_adc_battery_scale_avail
,
507 .battery_scale_avail_size
= ARRAY_SIZE(jz4725b_adc_battery_scale_avail
),
508 .battery_vref_mode
= true,
510 .channels
= jz4740_channels
,
511 .num_channels
= ARRAY_SIZE(jz4740_channels
),
512 .init_clk_div
= jz4725b_adc_init_clk_div
,
515 static const struct ingenic_adc_soc_data jz4740_adc_soc_data
= {
516 .battery_high_vref
= JZ4740_ADC_BATTERY_HIGH_VREF
,
517 .battery_high_vref_bits
= JZ4740_ADC_BATTERY_HIGH_VREF_BITS
,
518 .battery_raw_avail
= jz4740_adc_battery_raw_avail
,
519 .battery_raw_avail_size
= ARRAY_SIZE(jz4740_adc_battery_raw_avail
),
520 .battery_scale_avail
= jz4740_adc_battery_scale_avail
,
521 .battery_scale_avail_size
= ARRAY_SIZE(jz4740_adc_battery_scale_avail
),
522 .battery_vref_mode
= true,
524 .channels
= jz4740_channels
,
525 .num_channels
= ARRAY_SIZE(jz4740_channels
),
526 .init_clk_div
= NULL
, /* no ADCLK register on JZ4740 */
529 static const struct ingenic_adc_soc_data jz4770_adc_soc_data
= {
530 .battery_high_vref
= JZ4770_ADC_BATTERY_VREF
,
531 .battery_high_vref_bits
= JZ4770_ADC_BATTERY_VREF_BITS
,
532 .battery_raw_avail
= jz4770_adc_battery_raw_avail
,
533 .battery_raw_avail_size
= ARRAY_SIZE(jz4770_adc_battery_raw_avail
),
534 .battery_scale_avail
= jz4770_adc_battery_scale_avail
,
535 .battery_scale_avail_size
= ARRAY_SIZE(jz4770_adc_battery_scale_avail
),
536 .battery_vref_mode
= false,
538 .channels
= jz4770_channels
,
539 .num_channels
= ARRAY_SIZE(jz4770_channels
),
540 .init_clk_div
= jz4770_adc_init_clk_div
,
543 static int ingenic_adc_read_avail(struct iio_dev
*iio_dev
,
544 struct iio_chan_spec
const *chan
,
550 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
553 case IIO_CHAN_INFO_RAW
:
555 *length
= adc
->soc_data
->battery_raw_avail_size
;
556 *vals
= adc
->soc_data
->battery_raw_avail
;
557 return IIO_AVAIL_RANGE
;
558 case IIO_CHAN_INFO_SCALE
:
559 *type
= IIO_VAL_FRACTIONAL_LOG2
;
560 *length
= adc
->soc_data
->battery_scale_avail_size
;
561 *vals
= adc
->soc_data
->battery_scale_avail
;
562 return IIO_AVAIL_LIST
;
568 static int ingenic_adc_read_chan_info_raw(struct iio_dev
*iio_dev
,
569 struct iio_chan_spec
const *chan
,
572 int bit
, ret
, engine
= (chan
->channel
== INGENIC_ADC_BATTERY
);
573 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
575 ret
= clk_enable(adc
->clk
);
577 dev_err(iio_dev
->dev
.parent
, "Failed to enable clock: %d\n",
582 /* We cannot sample AUX/AUX2 in parallel. */
583 mutex_lock(&adc
->aux_lock
);
584 if (adc
->soc_data
->has_aux2
&& engine
== 0) {
585 bit
= BIT(chan
->channel
== INGENIC_ADC_AUX2
);
586 ingenic_adc_set_config(adc
, JZ_ADC_REG_CFG_AUX_MD
, bit
);
589 ret
= ingenic_adc_capture(adc
, engine
);
593 switch (chan
->channel
) {
594 case INGENIC_ADC_AUX
:
595 case INGENIC_ADC_AUX2
:
596 *val
= readw(adc
->base
+ JZ_ADC_REG_ADSDAT
);
598 case INGENIC_ADC_BATTERY
:
599 *val
= readw(adc
->base
+ JZ_ADC_REG_ADBDAT
);
605 mutex_unlock(&adc
->aux_lock
);
606 clk_disable(adc
->clk
);
611 static int ingenic_adc_read_raw(struct iio_dev
*iio_dev
,
612 struct iio_chan_spec
const *chan
,
617 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
620 case IIO_CHAN_INFO_RAW
:
621 return ingenic_adc_read_chan_info_raw(iio_dev
, chan
, val
);
622 case IIO_CHAN_INFO_SCALE
:
623 switch (chan
->channel
) {
624 case INGENIC_ADC_AUX
:
625 case INGENIC_ADC_AUX2
:
626 *val
= JZ_ADC_AUX_VREF
;
627 *val2
= JZ_ADC_AUX_VREF_BITS
;
629 case INGENIC_ADC_BATTERY
:
630 if (adc
->low_vref_mode
) {
631 *val
= JZ_ADC_BATTERY_LOW_VREF
;
632 *val2
= JZ_ADC_BATTERY_LOW_VREF_BITS
;
634 *val
= adc
->soc_data
->battery_high_vref
;
635 *val2
= adc
->soc_data
->battery_high_vref_bits
;
640 return IIO_VAL_FRACTIONAL_LOG2
;
646 static int ingenic_adc_of_xlate(struct iio_dev
*iio_dev
,
647 const struct of_phandle_args
*iiospec
)
651 if (!iiospec
->args_count
)
654 for (i
= 0; i
< iio_dev
->num_channels
; ++i
)
655 if (iio_dev
->channels
[i
].channel
== iiospec
->args
[0])
661 static void ingenic_adc_clk_cleanup(void *data
)
666 static const struct iio_info ingenic_adc_info
= {
667 .write_raw
= ingenic_adc_write_raw
,
668 .read_raw
= ingenic_adc_read_raw
,
669 .read_avail
= ingenic_adc_read_avail
,
670 .of_xlate
= ingenic_adc_of_xlate
,
673 static int ingenic_adc_buffer_enable(struct iio_dev
*iio_dev
)
675 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
678 ret
= clk_enable(adc
->clk
);
680 dev_err(iio_dev
->dev
.parent
, "Failed to enable clock: %d\n",
685 /* It takes significant time for the touchscreen hw to stabilize. */
687 ingenic_adc_set_config(adc
, JZ_ADC_REG_CFG_TOUCH_OPS_MASK
,
688 JZ_ADC_REG_CFG_SAMPLE_NUM(4) |
689 JZ_ADC_REG_CFG_PULL_UP(4));
691 writew(80, adc
->base
+ JZ_ADC_REG_ADWAIT
);
692 writew(2, adc
->base
+ JZ_ADC_REG_ADSAME
);
693 writeb((u8
)~JZ_ADC_IRQ_TOUCH
, adc
->base
+ JZ_ADC_REG_CTRL
);
694 writel(0, adc
->base
+ JZ_ADC_REG_ADTCH
);
696 ingenic_adc_set_config(adc
, JZ_ADC_REG_CFG_CMD_SEL
,
697 JZ_ADC_REG_CFG_CMD_SEL
);
698 ingenic_adc_set_adcmd(iio_dev
, iio_dev
->active_scan_mask
[0]);
700 ingenic_adc_enable(adc
, 2, true);
705 static int ingenic_adc_buffer_disable(struct iio_dev
*iio_dev
)
707 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
709 ingenic_adc_enable(adc
, 2, false);
711 ingenic_adc_set_config(adc
, JZ_ADC_REG_CFG_CMD_SEL
, 0);
713 writeb(0xff, adc
->base
+ JZ_ADC_REG_CTRL
);
714 writeb(0xff, adc
->base
+ JZ_ADC_REG_STATUS
);
715 ingenic_adc_set_config(adc
, JZ_ADC_REG_CFG_TOUCH_OPS_MASK
, 0);
716 writew(0, adc
->base
+ JZ_ADC_REG_ADSAME
);
717 writew(0, adc
->base
+ JZ_ADC_REG_ADWAIT
);
718 clk_disable(adc
->clk
);
723 static const struct iio_buffer_setup_ops ingenic_buffer_setup_ops
= {
724 .postenable
= &ingenic_adc_buffer_enable
,
725 .predisable
= &ingenic_adc_buffer_disable
728 static irqreturn_t
ingenic_adc_irq(int irq
, void *data
)
730 struct iio_dev
*iio_dev
= data
;
731 struct ingenic_adc
*adc
= iio_priv(iio_dev
);
732 unsigned long mask
= iio_dev
->active_scan_mask
[0];
736 for (i
= 0; i
< ARRAY_SIZE(tdat
); mask
>>= 2, i
++) {
738 tdat
[i
] = readl(adc
->base
+ JZ_ADC_REG_ADTCH
);
743 iio_push_to_buffers(iio_dev
, tdat
);
744 writeb(JZ_ADC_IRQ_TOUCH
, adc
->base
+ JZ_ADC_REG_STATUS
);
749 static int ingenic_adc_probe(struct platform_device
*pdev
)
751 struct device
*dev
= &pdev
->dev
;
752 struct iio_dev
*iio_dev
;
753 struct ingenic_adc
*adc
;
754 const struct ingenic_adc_soc_data
*soc_data
;
757 soc_data
= device_get_match_data(dev
);
761 iio_dev
= devm_iio_device_alloc(dev
, sizeof(*adc
));
765 adc
= iio_priv(iio_dev
);
766 mutex_init(&adc
->lock
);
767 mutex_init(&adc
->aux_lock
);
768 adc
->soc_data
= soc_data
;
770 irq
= platform_get_irq(pdev
, 0);
774 ret
= devm_request_irq(dev
, irq
, ingenic_adc_irq
, 0,
775 dev_name(dev
), iio_dev
);
777 dev_err(dev
, "Failed to request irq: %d\n", ret
);
781 adc
->base
= devm_platform_ioremap_resource(pdev
, 0);
782 if (IS_ERR(adc
->base
))
783 return PTR_ERR(adc
->base
);
785 adc
->clk
= devm_clk_get(dev
, "adc");
786 if (IS_ERR(adc
->clk
)) {
787 dev_err(dev
, "Unable to get clock\n");
788 return PTR_ERR(adc
->clk
);
791 ret
= clk_prepare_enable(adc
->clk
);
793 dev_err(dev
, "Failed to enable clock\n");
797 /* Set clock dividers. */
798 if (soc_data
->init_clk_div
) {
799 ret
= soc_data
->init_clk_div(dev
, adc
);
801 clk_disable_unprepare(adc
->clk
);
806 /* Put hardware in a known passive state. */
807 writeb(0x00, adc
->base
+ JZ_ADC_REG_ENABLE
);
808 writeb(0xff, adc
->base
+ JZ_ADC_REG_CTRL
);
809 usleep_range(2000, 3000); /* Must wait at least 2ms. */
810 clk_disable(adc
->clk
);
812 ret
= devm_add_action_or_reset(dev
, ingenic_adc_clk_cleanup
, adc
->clk
);
814 dev_err(dev
, "Unable to add action\n");
818 iio_dev
->name
= "jz-adc";
819 iio_dev
->modes
= INDIO_DIRECT_MODE
| INDIO_BUFFER_SOFTWARE
;
820 iio_dev
->setup_ops
= &ingenic_buffer_setup_ops
;
821 iio_dev
->channels
= soc_data
->channels
;
822 iio_dev
->num_channels
= soc_data
->num_channels
;
823 iio_dev
->info
= &ingenic_adc_info
;
825 ret
= devm_iio_device_register(dev
, iio_dev
);
827 dev_err(dev
, "Unable to register IIO device\n");
832 static const struct of_device_id ingenic_adc_of_match
[] = {
833 { .compatible
= "ingenic,jz4725b-adc", .data
= &jz4725b_adc_soc_data
, },
834 { .compatible
= "ingenic,jz4740-adc", .data
= &jz4740_adc_soc_data
, },
835 { .compatible
= "ingenic,jz4770-adc", .data
= &jz4770_adc_soc_data
, },
838 MODULE_DEVICE_TABLE(of
, ingenic_adc_of_match
);
840 static struct platform_driver ingenic_adc_driver
= {
842 .name
= "ingenic-adc",
843 .of_match_table
= ingenic_adc_of_match
,
845 .probe
= ingenic_adc_probe
,
847 module_platform_driver(ingenic_adc_driver
);
848 MODULE_LICENSE("GPL v2");