hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * (C) Copyright 2020 Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:     GPL-2.0+
 */
 
#include <common.h>
#include <adc.h>
#include <div64.h>
#include <fdtdec.h>
#include <dm/uclass.h>
 
DECLARE_GLOBAL_DATA_PTR;
 
static int adc_raw_to_mV(struct udevice *dev, unsigned int raw, int *mV)
{
   unsigned int data_mask;
   int ret, vref = 1800000;
   u64 raw64 = raw;
 
   ret = adc_data_mask(dev, &data_mask);
   if (ret)
       return ret;
 
   raw64 *= vref;
   do_div(raw64, data_mask);
   *mV = raw64;
 
   return 0;
}
 
int key_read(int code)
{
   const void *fdt_blob = gd->fdt_blob;
   struct udevice *dev;
   int adc_node, offset;
   int t, down_threshold = -1, up_threshold;
   int ret, num = 0, volt_margin = 150000;    /* will be div 2 */
   int mV, cd, voltage = -1;
   int min, max;
   u32 chn[2], adc;
 
   ret = uclass_get_device_by_name(UCLASS_ADC, "saradc", &dev);
   if (ret) {
       debug("No saradc device, ret=%d\n", ret);
       return 0;
   }
 
   adc_node = fdt_node_offset_by_compatible(fdt_blob, 0, "adc-keys");
   if (adc_node < 0) {
       debug("No 'adc-keys' node, ret=%d\n", adc_node);
       return 0;
   }
 
   ret = fdtdec_get_int_array(fdt_blob, adc_node, "io-channels",
                  chn, ARRAY_SIZE(chn));
   if (ret) {
       debug("Can't read 'io-channels', ret=%d\n", ret);
       return 0;
   }
 
   up_threshold = fdtdec_get_int(fdt_blob, adc_node,
                     "keyup-threshold-microvolt", -ENODATA);
   if (up_threshold < 0) {
       debug("Can't read 'keyup-threshold-microvolt'\n");
       return 0;
   }
 
   /* find the expected key-code */
   for (offset = fdt_first_subnode(fdt_blob, adc_node);
        offset >= 0;
        offset = fdt_next_subnode(fdt_blob, offset)) {
       cd = fdtdec_get_int(fdt_blob, offset, "linux,code", -ENODATA);
       if (cd < 0) {
           debug("Can't read 'linux,code', ret=%d\n", cd);
           return 0;
       }
 
       if (cd == code) {
           voltage = fdtdec_get_int(fdt_blob, offset,
                   "press-threshold-microvolt", -ENODATA);
           if (voltage < 0) {
               debug("Can't read 'press-threshold-microvolt'\n");
               return 0;
           }
           break;
       }
   }
 
   if (voltage < 0)
       return 0;
 
   for (offset = fdt_first_subnode(fdt_blob, adc_node);
        offset >= 0;
        offset = fdt_next_subnode(fdt_blob, offset)) {
       t = fdtdec_get_int(fdt_blob, offset,
                  "press-threshold-microvolt", -ENODATA);
       if (t < 0) {
           debug("Can't read 'press-threshold-microvolt'\n");
           return 0;
       }
 
       if (t > voltage && t < up_threshold)
           up_threshold = t;
       else if (t < voltage && t > down_threshold)
           down_threshold = t;
       num++;
   }
 
   /* although one node only, it doesn't mean only one key on hardware */
   if (num == 1) {
       down_threshold = voltage - volt_margin;
       up_threshold = voltage + volt_margin;
   }
 
   /*
    * Define the voltage range such that the button is only pressed
    * when the voltage is closest to its own press-threshold-microvolt
    */
   if (down_threshold < 0)
       min = 0;
   else
       min = down_threshold + (voltage - down_threshold) / 2;
 
   max = voltage + (up_threshold - voltage) / 2;
 
   /* now, read key status */
   ret = adc_channel_single_shot("saradc", chn[1], &adc);
   if (ret) {
       debug("Failed to read adc%d, ret=%d\n", chn[1], ret);
       return 0;
   }
 
   ret = adc_raw_to_mV(dev, adc, &mV);
   if (ret) {
       debug("Failed to convert adc to mV, ret=%d\n", ret);
       return 0;
   }
 
   debug("key[%d] <%d, %d, %d>: adc=%d -> mV=%d\n",
         code, min, voltage, max, adc, mV);
 
   return (mV <= max && mV >= min);
}