1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Allwinner sun4i low res adc attached tablet keys driver
5 * Copyright (C) 2014 Hans de Goede <hdegoede@redhat.com>
9 * Allwinnner sunxi SoCs have a lradc which is specifically designed to have
10 * various (tablet) keys (ie home, back, search, etc). attached to it using
11 * a resistor network. This driver is for the keys on such boards.
13 * There are 2 channels, currently this driver only supports channel 0 since
14 * there are no boards known to use channel 1.
17 #include <linux/clk.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/input.h>
21 #include <linux/interrupt.h>
23 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_wakeirq.h>
27 #include <linux/property.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/reset.h>
30 #include <linux/slab.h>
32 #define LRADC_CTRL 0x00
33 #define LRADC_INTC 0x04
34 #define LRADC_INTS 0x08
35 #define LRADC_DATA0 0x0c
36 #define LRADC_DATA1 0x10
39 #define FIRST_CONVERT_DLY(x) ((x) << 24) /* 8 bits */
40 #define CHAN_SELECT(x) ((x) << 22) /* 2 bits */
41 #define CONTINUE_TIME_SEL(x) ((x) << 16) /* 4 bits */
42 #define KEY_MODE_SEL(x) ((x) << 12) /* 2 bits */
43 #define LEVELA_B_CNT(x) ((x) << 8) /* 4 bits */
44 #define HOLD_KEY_EN(x) ((x) << 7)
45 #define HOLD_EN(x) ((x) << 6)
46 #define LEVELB_VOL(x) ((x) << 4) /* 2 bits */
47 #define SAMPLE_RATE(x) ((x) << 2) /* 2 bits */
48 #define ENABLE(x) ((x) << 0)
50 /* LRADC_INTC and LRADC_INTS bits */
51 #define CHAN1_KEYUP_IRQ BIT(12)
52 #define CHAN1_ALRDY_HOLD_IRQ BIT(11)
53 #define CHAN1_HOLD_IRQ BIT(10)
54 #define CHAN1_KEYDOWN_IRQ BIT(9)
55 #define CHAN1_DATA_IRQ BIT(8)
56 #define CHAN0_KEYUP_IRQ BIT(4)
57 #define CHAN0_ALRDY_HOLD_IRQ BIT(3)
58 #define CHAN0_HOLD_IRQ BIT(2)
59 #define CHAN0_KEYDOWN_IRQ BIT(1)
60 #define CHAN0_DATA_IRQ BIT(0)
62 /* struct lradc_variant - Describe sun4i-a10-lradc-keys hardware variant
63 * @divisor_numerator: The numerator of lradc Vref internally divisor
64 * @divisor_denominator: The denominator of lradc Vref internally divisor
65 * @has_clock_reset: If the binding requires a clock and reset
67 struct lradc_variant
{
69 u8 divisor_denominator
;
73 static const struct lradc_variant lradc_variant_a10
= {
74 .divisor_numerator
= 2,
75 .divisor_denominator
= 3
78 static const struct lradc_variant r_lradc_variant_a83t
= {
79 .divisor_numerator
= 3,
80 .divisor_denominator
= 4
83 static const struct lradc_variant lradc_variant_r329
= {
84 .divisor_numerator
= 3,
85 .divisor_denominator
= 4,
86 .has_clock_reset
= true,
89 struct sun4i_lradc_keymap
{
94 struct sun4i_lradc_data
{
96 struct input_dev
*input
;
99 struct reset_control
*reset
;
100 struct regulator
*vref_supply
;
101 struct sun4i_lradc_keymap
*chan0_map
;
102 const struct lradc_variant
*variant
;
108 static irqreturn_t
sun4i_lradc_irq(int irq
, void *dev_id
)
110 struct sun4i_lradc_data
*lradc
= dev_id
;
111 u32 i
, ints
, val
, voltage
, diff
, keycode
= 0, closest
= 0xffffffff;
113 ints
= readl(lradc
->base
+ LRADC_INTS
);
116 * lradc supports only one keypress at a time, release does not give
117 * any info as to which key was released, so we cache the keycode.
120 if (ints
& CHAN0_KEYUP_IRQ
) {
121 input_report_key(lradc
->input
, lradc
->chan0_keycode
, 0);
122 lradc
->chan0_keycode
= 0;
125 if ((ints
& CHAN0_KEYDOWN_IRQ
) && lradc
->chan0_keycode
== 0) {
126 val
= readl(lradc
->base
+ LRADC_DATA0
) & 0x3f;
127 voltage
= val
* lradc
->vref
/ 63;
129 for (i
= 0; i
< lradc
->chan0_map_count
; i
++) {
130 diff
= abs(lradc
->chan0_map
[i
].voltage
- voltage
);
131 if (diff
< closest
) {
133 keycode
= lradc
->chan0_map
[i
].keycode
;
137 lradc
->chan0_keycode
= keycode
;
138 input_report_key(lradc
->input
, lradc
->chan0_keycode
, 1);
141 input_sync(lradc
->input
);
143 writel(ints
, lradc
->base
+ LRADC_INTS
);
148 static int sun4i_lradc_open(struct input_dev
*dev
)
150 struct sun4i_lradc_data
*lradc
= input_get_drvdata(dev
);
153 error
= regulator_enable(lradc
->vref_supply
);
157 error
= reset_control_deassert(lradc
->reset
);
159 goto err_disable_reg
;
161 error
= clk_prepare_enable(lradc
->clk
);
163 goto err_assert_reset
;
165 lradc
->vref
= regulator_get_voltage(lradc
->vref_supply
) *
166 lradc
->variant
->divisor_numerator
/
167 lradc
->variant
->divisor_denominator
;
169 * Set sample time to 4 ms / 250 Hz. Wait 2 * 4 ms for key to
170 * stabilize on press, wait (1 + 1) * 4 ms for key release
172 writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
173 SAMPLE_RATE(0) | ENABLE(1), lradc
->base
+ LRADC_CTRL
);
175 writel(CHAN0_KEYUP_IRQ
| CHAN0_KEYDOWN_IRQ
, lradc
->base
+ LRADC_INTC
);
180 reset_control_assert(lradc
->reset
);
182 regulator_disable(lradc
->vref_supply
);
187 static void sun4i_lradc_close(struct input_dev
*dev
)
189 struct sun4i_lradc_data
*lradc
= input_get_drvdata(dev
);
191 /* Disable lradc, leave other settings unchanged */
192 writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
193 SAMPLE_RATE(2), lradc
->base
+ LRADC_CTRL
);
194 writel(0, lradc
->base
+ LRADC_INTC
);
196 clk_disable_unprepare(lradc
->clk
);
197 reset_control_assert(lradc
->reset
);
198 regulator_disable(lradc
->vref_supply
);
201 static int sun4i_lradc_load_dt_keymap(struct device
*dev
,
202 struct sun4i_lradc_data
*lradc
)
204 struct device_node
*np
;
212 lradc
->chan0_map_count
= of_get_child_count(np
);
213 if (lradc
->chan0_map_count
== 0) {
214 dev_err(dev
, "keymap is missing in device tree\n");
218 lradc
->chan0_map
= devm_kmalloc_array(dev
, lradc
->chan0_map_count
,
219 sizeof(struct sun4i_lradc_keymap
),
221 if (!lradc
->chan0_map
)
225 for_each_child_of_node_scoped(np
, pp
) {
226 struct sun4i_lradc_keymap
*map
= &lradc
->chan0_map
[i
];
229 error
= of_property_read_u32(pp
, "channel", &channel
);
230 if (error
|| channel
!= 0) {
231 dev_err(dev
, "%pOFn: Inval channel prop\n", pp
);
235 error
= of_property_read_u32(pp
, "voltage", &map
->voltage
);
237 dev_err(dev
, "%pOFn: Inval voltage prop\n", pp
);
241 error
= of_property_read_u32(pp
, "linux,code", &map
->keycode
);
243 dev_err(dev
, "%pOFn: Inval linux,code prop\n", pp
);
253 static int sun4i_lradc_probe(struct platform_device
*pdev
)
255 struct sun4i_lradc_data
*lradc
;
256 struct device
*dev
= &pdev
->dev
;
259 lradc
= devm_kzalloc(dev
, sizeof(struct sun4i_lradc_data
), GFP_KERNEL
);
263 error
= sun4i_lradc_load_dt_keymap(dev
, lradc
);
267 lradc
->variant
= of_device_get_match_data(&pdev
->dev
);
268 if (!lradc
->variant
) {
269 dev_err(&pdev
->dev
, "Missing sun4i-a10-lradc-keys variant\n");
273 if (lradc
->variant
->has_clock_reset
) {
274 lradc
->clk
= devm_clk_get(dev
, NULL
);
275 if (IS_ERR(lradc
->clk
))
276 return PTR_ERR(lradc
->clk
);
278 lradc
->reset
= devm_reset_control_get_exclusive(dev
, NULL
);
279 if (IS_ERR(lradc
->reset
))
280 return PTR_ERR(lradc
->reset
);
283 lradc
->vref_supply
= devm_regulator_get(dev
, "vref");
284 if (IS_ERR(lradc
->vref_supply
))
285 return PTR_ERR(lradc
->vref_supply
);
288 lradc
->input
= devm_input_allocate_device(dev
);
292 lradc
->input
->name
= pdev
->name
;
293 lradc
->input
->phys
= "sun4i_lradc/input0";
294 lradc
->input
->open
= sun4i_lradc_open
;
295 lradc
->input
->close
= sun4i_lradc_close
;
296 lradc
->input
->id
.bustype
= BUS_HOST
;
297 lradc
->input
->id
.vendor
= 0x0001;
298 lradc
->input
->id
.product
= 0x0001;
299 lradc
->input
->id
.version
= 0x0100;
301 __set_bit(EV_KEY
, lradc
->input
->evbit
);
302 for (i
= 0; i
< lradc
->chan0_map_count
; i
++)
303 __set_bit(lradc
->chan0_map
[i
].keycode
, lradc
->input
->keybit
);
305 input_set_drvdata(lradc
->input
, lradc
);
307 lradc
->base
= devm_platform_ioremap_resource(pdev
, 0);
308 if (IS_ERR(lradc
->base
))
309 return PTR_ERR(lradc
->base
);
311 irq
= platform_get_irq(pdev
, 0);
315 error
= devm_request_irq(dev
, irq
, sun4i_lradc_irq
, 0,
316 "sun4i-a10-lradc-keys", lradc
);
320 error
= input_register_device(lradc
->input
);
324 if (device_property_read_bool(dev
, "wakeup-source")) {
325 error
= dev_pm_set_wake_irq(dev
, irq
);
328 "Failed to set IRQ %d as a wake IRQ: %d\n",
331 device_set_wakeup_capable(dev
, true);
337 static const struct of_device_id sun4i_lradc_of_match
[] = {
338 { .compatible
= "allwinner,sun4i-a10-lradc-keys",
339 .data
= &lradc_variant_a10
},
340 { .compatible
= "allwinner,sun8i-a83t-r-lradc",
341 .data
= &r_lradc_variant_a83t
},
342 { .compatible
= "allwinner,sun50i-r329-lradc",
343 .data
= &lradc_variant_r329
},
346 MODULE_DEVICE_TABLE(of
, sun4i_lradc_of_match
);
348 static struct platform_driver sun4i_lradc_driver
= {
350 .name
= "sun4i-a10-lradc-keys",
351 .of_match_table
= of_match_ptr(sun4i_lradc_of_match
),
353 .probe
= sun4i_lradc_probe
,
356 module_platform_driver(sun4i_lradc_driver
);
358 MODULE_DESCRIPTION("Allwinner sun4i low res adc attached tablet keys driver");
359 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
360 MODULE_LICENSE("GPL");