xref: /rk3399_rockchip-uboot/drivers/input/adc_key.c (revision 01b8c4d110abb0dcbe36dc5b6b10d93b2b8e2667)
1 /*
2  * (C) Copyright 2017 Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <key.h>
10 
11 static int adc_key_ofdata_to_platdata(struct udevice *dev)
12 {
13 	struct dm_key_uclass_platdata *uc_key;
14 	u32 chn[2], mV;
15 	int vref, ret;
16 
17 	uc_key = dev_get_uclass_platdata(dev);
18 	if (!uc_key)
19 		return -ENXIO;
20 
21 	uc_key->type = ADC_KEY;
22 	uc_key->name = dev_read_string(dev, "label");
23 	ret = dev_read_u32_array(dev_get_parent(dev),
24 				 "io-channels", chn, ARRAY_SIZE(chn));
25 	if (ret) {
26 		printf("%s: read 'io-channels' failed, ret=%d\n",
27 		       uc_key->name, ret);
28 		return -EINVAL;
29 	}
30 
31 	vref = dev_read_u32_default(dev_get_parent(dev),
32 				    "keyup-threshold-microvolt", -ENODATA);
33 	if (vref < 0) {
34 		printf("%s: read 'keyup-threshold-microvolt' failed, ret=%d\n",
35 		       uc_key->name, vref);
36 		return -EINVAL;
37 	}
38 
39 	uc_key->code = dev_read_u32_default(dev, "linux,code", -ENODATA);
40 	if (uc_key->code < 0) {
41 		printf("%s: read 'linux,code' failed\n", uc_key->name);
42 		return -EINVAL;
43 	}
44 
45 	mV = dev_read_u32_default(dev, "press-threshold-microvolt", -ENODATA);
46 	if (mV < 0) {
47 		printf("%s: read 'press-threshold-microvolt' failed\n",
48 		       uc_key->name);
49 		return -EINVAL;
50 	}
51 
52 	uc_key->channel = chn[1];
53 	uc_key->adcval = mV / (vref / 1024);
54 
55 	return 0;
56 }
57 
58 U_BOOT_DRIVER(adc_key) = {
59 	.name   = "adc_key",
60 	.id     = UCLASS_KEY,
61 	.ofdata_to_platdata = adc_key_ofdata_to_platdata,
62 };
63 
64 /* Key Bus */
65 static int adc_key_bus_bind(struct udevice *dev)
66 {
67 	return key_bind_children(dev, "adc_key");
68 }
69 
70 static const struct udevice_id adc_key_bus_match[] = {
71 	{ .compatible = "adc-keys" },
72 	{ },
73 };
74 
75 U_BOOT_DRIVER(adc_key_bus) = {
76 	.name	   = "adc_key_bus",
77 	.id	   = UCLASS_SIMPLE_BUS,
78 	.of_match  = adc_key_bus_match,
79 	.bind	   = adc_key_bus_bind,
80 };
81