xref: /rk3399_rockchip-uboot/drivers/input/adc_key.c (revision c8a22fb85e7897da3a0a3ede4705e54e9f18eba3)
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 #ifdef CONFIG_SARADC_ROCKCHIP_V2
17 	int range = 4096;
18 #else
19 	int range = 1024;
20 #endif
21 
22 	uc_key = dev_get_uclass_platdata(dev);
23 	if (!uc_key)
24 		return -ENXIO;
25 
26 	uc_key->type = ADC_KEY;
27 	uc_key->name = dev_read_string(dev, "label");
28 	ret = dev_read_u32_array(dev_get_parent(dev),
29 				 "io-channels", chn, ARRAY_SIZE(chn));
30 	if (ret) {
31 		printf("%s: read 'io-channels' failed, ret=%d\n",
32 		       uc_key->name, ret);
33 		return -EINVAL;
34 	}
35 
36 	vref = dev_read_u32_default(dev_get_parent(dev),
37 				    "keyup-threshold-microvolt", -ENODATA);
38 	if (vref < 0) {
39 		printf("%s: read 'keyup-threshold-microvolt' failed, ret=%d\n",
40 		       uc_key->name, vref);
41 		return -EINVAL;
42 	}
43 
44 	uc_key->code = dev_read_u32_default(dev, "linux,code", -ENODATA);
45 	if (uc_key->code < 0) {
46 		printf("%s: read 'linux,code' failed\n", uc_key->name);
47 		return -EINVAL;
48 	}
49 
50 	mV = dev_read_u32_default(dev, "press-threshold-microvolt", -ENODATA);
51 	if (mV < 0) {
52 		printf("%s: read 'press-threshold-microvolt' failed\n",
53 		       uc_key->name);
54 		return -EINVAL;
55 	}
56 
57 	uc_key->channel = chn[1];
58 	uc_key->adcval = mV / (vref / range);
59 
60 	return 0;
61 }
62 
63 U_BOOT_DRIVER(adc_key) = {
64 	.name   = "adc_key",
65 	.id     = UCLASS_KEY,
66 	.ofdata_to_platdata = adc_key_ofdata_to_platdata,
67 };
68 
69 /* Key Bus */
70 static int adc_key_bus_bind(struct udevice *dev)
71 {
72 	return key_bind_children(dev, "adc_key");
73 }
74 
75 static const struct udevice_id adc_key_bus_match[] = {
76 	{ .compatible = "adc-keys" },
77 	{ },
78 };
79 
80 U_BOOT_DRIVER(adc_key_bus) = {
81 	.name	   = "adc_key_bus",
82 	.id	   = UCLASS_SIMPLE_BUS,
83 	.of_match  = adc_key_bus_match,
84 	.bind	   = adc_key_bus_bind,
85 };
86