xref: /OK3568_Linux_fs/u-boot/drivers/input/rk_key.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 
rk_key_ofdata_to_platdata(struct udevice * dev)11 static int rk_key_ofdata_to_platdata(struct udevice *dev)
12 {
13 	struct dm_key_uclass_platdata *uc_key;
14 	u32 chn[2];
15 
16 	uc_key = dev_get_uclass_platdata(dev);
17 	if (!uc_key)
18 		return -ENXIO;
19 
20 	uc_key->name = dev_read_string(dev, "label");
21 	uc_key->code = dev_read_u32_default(dev, "linux,code", -ENODATA);
22 
23 	if (dev_read_u32_array(dev_get_parent(dev), "io-channels", chn, 2)) {
24 		printf("%s: read 'io-channels' failed\n", uc_key->name);
25 		return -EINVAL;
26 	}
27 
28 	if (dev_read_bool(dev, "rockchip,adc_value")) {
29 		uc_key->type = ADC_KEY;
30 		uc_key->in_volt = 0;
31 		uc_key->channel = chn[1];
32 		uc_key->center = dev_read_u32_default(dev, "rockchip,adc_value", 0);
33 		uc_key->min = uc_key->center - 30;
34 		if (uc_key->min < 0)
35 			uc_key->min = 0;
36 		uc_key->max = uc_key->center + 30;
37 	} else {
38 		uc_key->type = GPIO_KEY;
39 		if (dev_read_u32_array(dev, "gpios",
40 				       uc_key->gpios,
41 				       ARRAY_SIZE(uc_key->gpios))) {
42 			printf("%s: read 'gpios' failed\n", uc_key->name);
43 			return -EINVAL;
44 		}
45 	}
46 
47 	return 0;
48 }
49 
50 U_BOOT_DRIVER(rk_key) = {
51 	.name     = "rk_key",
52 	.id       = UCLASS_KEY,
53 	.ofdata_to_platdata = rk_key_ofdata_to_platdata,
54 };
55 
56 /* Key Bus */
rk_key_bus_bind(struct udevice * dev)57 static int rk_key_bus_bind(struct udevice *dev)
58 {
59 	return key_bind_children(dev, "rk_key");
60 }
61 
62 static const struct udevice_id rk_key_bus_match[] = {
63 	{ .compatible = "rockchip,key" },
64 	{ },
65 };
66 
67 U_BOOT_DRIVER(rk_key_bus) = {
68 	.name	   = "rk_key_bus",
69 	.id	   = UCLASS_SIMPLE_BUS,
70 	.of_match  = rk_key_bus_match,
71 	.bind	   = rk_key_bus_bind,
72 };
73