hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
u-boot/drivers/input/adc_key.c
....@@ -11,13 +11,10 @@
1111 static int adc_key_ofdata_to_platdata(struct udevice *dev)
1212 {
1313 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; /* 12-bit adc */
18
-#else
19
- int range = 1024; /* 10-bit adc */
20
-#endif
14
+ int t, down_threshold = -1, up_threshold;
15
+ int ret, num = 0, volt_margin = 150000; /* will be div 2 */
16
+ u32 voltage, chn[2];
17
+ ofnode node;
2118
2219 uc_key = dev_get_uclass_platdata(dev);
2320 if (!uc_key)
....@@ -33,29 +30,50 @@
3330 return -EINVAL;
3431 }
3532
36
- vref = dev_read_u32_default(dev_get_parent(dev),
33
+ up_threshold = dev_read_u32_default(dev_get_parent(dev),
3734 "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
- }
35
+ if (up_threshold < 0)
36
+ return -ENODATA;
4337
4438 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;
39
+ if (uc_key->code < 0)
40
+ return -ENODATA;
41
+
42
+ voltage = dev_read_u32_default(dev, "press-threshold-microvolt", -ENODATA);
43
+ if (voltage < 0)
44
+ return -ENODATA;
45
+
46
+ dev_for_each_subnode(node, dev->parent) {
47
+ ret = ofnode_read_s32(node, "press-threshold-microvolt", &t);
48
+ if (ret)
49
+ return ret;
50
+
51
+ if (t > voltage && t < up_threshold)
52
+ up_threshold = t;
53
+ else if (t < voltage && t > down_threshold)
54
+ down_threshold = t;
55
+ num++;
4856 }
4957
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;
58
+ /* although one node only, it doesn't mean only one key on hardware */
59
+ if (num == 1) {
60
+ down_threshold = voltage - volt_margin;
61
+ up_threshold = voltage + volt_margin;
5562 }
5663
64
+ uc_key->in_volt = 1;
5765 uc_key->channel = chn[1];
58
- uc_key->adcval = mV / (vref / range);
66
+ uc_key->center = voltage;
67
+ /*
68
+ * Define the voltage range such that the button is only pressed
69
+ * when the voltage is closest to its own press-threshold-microvolt
70
+ */
71
+ if (down_threshold < 0)
72
+ uc_key->min = 0;
73
+ else
74
+ uc_key->min = down_threshold + (voltage - down_threshold) / 2;
75
+
76
+ uc_key->max = voltage + (up_threshold - voltage) / 2;
5977
6078 return 0;
6179 }