.. | .. |
---|
11 | 11 | static int adc_key_ofdata_to_platdata(struct udevice *dev) |
---|
12 | 12 | { |
---|
13 | 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; /* 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; |
---|
21 | 18 | |
---|
22 | 19 | uc_key = dev_get_uclass_platdata(dev); |
---|
23 | 20 | if (!uc_key) |
---|
.. | .. |
---|
33 | 30 | return -EINVAL; |
---|
34 | 31 | } |
---|
35 | 32 | |
---|
36 | | - vref = dev_read_u32_default(dev_get_parent(dev), |
---|
| 33 | + up_threshold = dev_read_u32_default(dev_get_parent(dev), |
---|
37 | 34 | "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; |
---|
43 | 37 | |
---|
44 | 38 | 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++; |
---|
48 | 56 | } |
---|
49 | 57 | |
---|
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; |
---|
55 | 62 | } |
---|
56 | 63 | |
---|
| 64 | + uc_key->in_volt = 1; |
---|
57 | 65 | 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; |
---|
59 | 77 | |
---|
60 | 78 | return 0; |
---|
61 | 79 | } |
---|