ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
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
/*
 *  * Copyright 2000-2009
 *   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *    *
 *     * SPDX-License-Identifier:    GPL-2.0+
 *     */
#include <common.h>
#include <asm/io.h>
#include <physical_key.h>
#include <sys_config.h>
#include <fdt_support.h>
#include <console.h>
 
 
__attribute__((section(".data")))
static uint32_t keyen_flag = 1;
 
__weak int sunxi_key_clock_open(void)
{
   return 0;
}
 
__weak int sunxi_key_clock_close(void)
{
   return 0;
}
 
__weak int axp_probe_key(void)
{
   return 0;
}
 
int sunxi_key_init(void)
{
   struct sunxi_lradc *sunxi_key_base = (struct sunxi_lradc *)SUNXI_KEYADC_BASE;
   uint reg_val = 0;
   int nodeoffset;
 
   sunxi_key_clock_open();
 
   reg_val = sunxi_key_base->ctrl;
   reg_val &= ~((7<<1) | (0xffU << 24));
   reg_val |=  LRADC_HOLD_EN;
   reg_val |=  LRADC_EN;
   sunxi_key_base->ctrl = reg_val;
 
   /* disable all key irq */
   sunxi_key_base->intc = 0;
   sunxi_key_base->ints = 0x1f1f;
 
   nodeoffset = fdt_path_offset(working_fdt, FDT_PATH_KEY_DETECT);
   if (nodeoffset > 0) {
       fdt_getprop_u32(working_fdt, nodeoffset, "keyen_flag", &keyen_flag);
   }
 
   return 0;
}
 
int sunxi_key_exit(void)
{
   struct sunxi_lradc *sunxi_key_base = (struct sunxi_lradc *)SUNXI_KEYADC_BASE;
 
   sunxi_key_base->ctrl = 0;
   /* disable all key irq */
   sunxi_key_base->intc = 0;
   sunxi_key_base->ints = 0x1f1f;
 
   sunxi_key_clock_close();
 
   return 0;
}
 
 
int sunxi_key_read(void)
{
   u32 ints;
   int key = -1;
   struct sunxi_lradc *sunxi_key_base = (struct sunxi_lradc *)SUNXI_KEYADC_BASE;
 
   if (!keyen_flag) {
       return -1;
   }
   ints = sunxi_key_base->ints;
   /* clear the pending data */
   sunxi_key_base->ints |= (ints & 0x1f);
   /* if there is already data pending,
    read it */
   if (ints & ADC0_KEYDOWN_PENDING) {
       if (ints & ADC0_DATA_PENDING) {
           key = sunxi_key_base->data0 & 0x3f;
           if (!key) {
               key = -1;
           }
       }
   } else if (ints & ADC0_DATA_PENDING) {
       key = sunxi_key_base->data0 & 0x3f;
       if (!key) {
           key = -1;
       }
   }
 
   if (key > 0) {
       printf("key pressed value=0x%x\n", key);
   }
 
 
   return key;
}
 
int do_key_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
   struct sunxi_lradc *sunxi_key_base = (struct sunxi_lradc *)SUNXI_KEYADC_BASE;
   u32 power_key = 0;
   int nodeoffset;
 
   nodeoffset = fdt_path_offset(working_fdt, FDT_PATH_KEY_DETECT);
   if (nodeoffset > 0) {
       fdt_getprop_u32(working_fdt, nodeoffset, "keyen_flag", &keyen_flag);
   }
   if (!keyen_flag) {
       puts("warnning: not support,please set keyen_flag=1 in sys_config.fex\n");
       return -1;
   }
   sunxi_key_init();
   puts(" press a key:\n");
   sunxi_key_base->ints = 0x1f1f;
   while (!ctrlc()) {
       sunxi_key_read();
       power_key = axp_probe_key();
       if (power_key > 0) {
           break;
       }
   }
 
   return 0;
 
}
 
U_BOOT_CMD(
   key_test, 1, 0,    do_key_test,
   "Test the key value\n",
   ""
);