1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15
16 #include <common/bk_include.h>
17 #include "sys_driver.h"
18 #include "gpio_map.h"
19 #include "gpio_driver.h"
20 #include "touch_driver.h"
21 #include "aon_pmu_driver.h"
22 #include <driver/gpio.h>
23 #include <driver/int.h>
24 #include <driver/timer.h>
25 #include <driver/touch_types.h>
26 #include <os/os.h>
27
28
29 extern void delay(int num);
30
31 typedef struct {
32 touch_isr_t callback;
33 void *param;
34 } touch_callback_t;
35
36
37 #define TOUCH_RETURN_ON_INVALID_ID(touch_id) do {\
38 if ((touch_id) >= SOC_TOUCH_ID_NUM) {\
39 return BK_ERR_TOUCH_ID;\
40 }\
41 } while(0)
42
43 static uint32_t s_touch_channel = 0;
44
45 static touch_callback_t s_touch_isr[SOC_TOUCH_ID_NUM] = {NULL};
46
bk_touch_channel_transfer(touch_channel_t touch_id)47 static uint32_t bk_touch_channel_transfer(touch_channel_t touch_id)
48 {
49 uint32_t touch_channel = 0;
50 for (touch_channel = 0; touch_channel < 16; touch_channel++)
51 {
52 touch_id = touch_id / 2;
53 if (touch_id == 0 ) {
54 break;
55 }
56 }
57 return touch_channel;
58 }
59
bk_touch_gpio_init(touch_channel_t touch_id)60 bk_err_t bk_touch_gpio_init(touch_channel_t touch_id)
61 {
62 uint32_t touch_select = 0;
63 touch_select = bk_touch_channel_transfer(touch_id);
64 TOUCH_RETURN_ON_INVALID_ID(touch_select);
65 switch(touch_id)
66 {
67 case BK_TOUCH_0:
68 gpio_dev_unmap(GPIO_12);
69 gpio_dev_map(GPIO_12, GPIO_DEV_TOUCH0);
70 bk_gpio_disable_output(GPIO_12);
71 break;
72
73 case BK_TOUCH_1:
74 gpio_dev_unmap(GPIO_13);
75 gpio_dev_map(GPIO_13, GPIO_DEV_TOUCH1);
76 bk_gpio_disable_output(GPIO_13);
77 break;
78
79 case BK_TOUCH_2:
80 gpio_dev_unmap(GPIO_28);
81 gpio_dev_map(GPIO_28, GPIO_DEV_TOUCH2);
82 bk_gpio_disable_output(GPIO_28);
83 break;
84
85 case BK_TOUCH_3:
86 gpio_dev_unmap(GPIO_29);
87 gpio_dev_map(GPIO_29, GPIO_DEV_TOUCH3);
88 bk_gpio_disable_output(GPIO_29);
89 break;
90
91 case BK_TOUCH_4:
92 gpio_dev_unmap(GPIO_30);
93 gpio_dev_map(GPIO_30, GPIO_DEV_TOUCH4);
94 bk_gpio_disable_output(GPIO_30);
95 break;
96
97 case BK_TOUCH_5:
98 gpio_dev_unmap(GPIO_31);
99 gpio_dev_map(GPIO_31, GPIO_DEV_TOUCH5);
100 bk_gpio_disable_output(GPIO_31);
101 break;
102
103 case BK_TOUCH_6:
104 gpio_dev_unmap(GPIO_32);
105 gpio_dev_map(GPIO_32, GPIO_DEV_TOUCH6);
106 bk_gpio_disable_output(GPIO_32);
107 break;
108
109 case BK_TOUCH_7:
110 gpio_dev_unmap(GPIO_33);
111 gpio_dev_map(GPIO_33, GPIO_DEV_TOUCH7);
112 bk_gpio_disable_output(GPIO_33);
113 break;
114
115 case BK_TOUCH_8:
116 gpio_dev_unmap(GPIO_34);
117 gpio_dev_map(GPIO_34, GPIO_DEV_TOUCH8);
118 bk_gpio_disable_output(GPIO_34);
119 break;
120
121 case BK_TOUCH_9:
122 gpio_dev_unmap(GPIO_35);
123 gpio_dev_map(GPIO_35, GPIO_DEV_TOUCH9);
124 bk_gpio_disable_output(GPIO_35);
125 break;
126
127 case BK_TOUCH_10:
128 gpio_dev_unmap(GPIO_36);
129 gpio_dev_map(GPIO_36, GPIO_DEV_TOUCH10);
130 bk_gpio_disable_output(GPIO_36);
131 break;
132
133 case BK_TOUCH_11:
134 gpio_dev_unmap(GPIO_37);
135 gpio_dev_map(GPIO_37, GPIO_DEV_TOUCH11);
136 bk_gpio_disable_output(GPIO_37);
137 break;
138
139 case BK_TOUCH_12:
140 gpio_dev_unmap(GPIO_38);
141 gpio_dev_map(GPIO_38, GPIO_DEV_TOUCH12);
142 bk_gpio_disable_output(GPIO_38);
143 break;
144
145 case BK_TOUCH_13:
146 gpio_dev_unmap(GPIO_39);
147 gpio_dev_map(GPIO_39, GPIO_DEV_TOUCH13);
148 bk_gpio_disable_output(GPIO_39);
149 break;
150
151 case BK_TOUCH_14:
152 gpio_dev_unmap(GPIO_46);
153 gpio_dev_map(GPIO_46, GPIO_DEV_TOUCH14);
154 bk_gpio_disable_output(GPIO_46);
155 break;
156
157 case BK_TOUCH_15:
158 gpio_dev_unmap(GPIO_47);
159 gpio_dev_map(GPIO_47, GPIO_DEV_TOUCH15);
160 bk_gpio_disable_output(GPIO_47);
161 break;
162
163 default:
164 TOUCH_LOGI("unsupported touch id\r\n");
165 break;
166 }
167
168 return BK_OK;
169 }
170
bk_touch_enable(touch_channel_t touch_id)171 bk_err_t bk_touch_enable(touch_channel_t touch_id)
172 {
173 uint32_t touch_select = 0;
174 touch_select = bk_touch_channel_transfer(touch_id);
175 TOUCH_RETURN_ON_INVALID_ID(touch_select);
176 bk_int_isr_register(INT_SRC_TOUCH, touch_isr, NULL);
177 aon_pmu_drv_touch_select(touch_select);
178
179 sys_drv_touch_power_down(0);
180
181 return BK_OK;
182 }
183
bk_touch_disable(void)184 bk_err_t bk_touch_disable(void)
185 {
186 sys_drv_touch_power_down(1);
187
188 return BK_OK;
189 }
190
bk_touch_config(const touch_config_t * touch_config)191 bk_err_t bk_touch_config(const touch_config_t *touch_config)
192 {
193 sys_drv_touch_sensitivity_level_set(touch_config->sensitivity_level);
194 sys_drv_touch_detect_threshold_set(touch_config->detect_threshold);
195 sys_drv_touch_detect_range_set(touch_config->detect_range);
196
197 return BK_OK;
198 }
199
bk_touch_calib_enable(uint32_t enable)200 bk_err_t bk_touch_calib_enable(uint32_t enable)
201 {
202 if(enable) {
203 sys_drv_touch_calib_enable(1);
204 } else {
205 sys_drv_touch_calib_enable(0);
206 }
207
208 return BK_OK;
209 }
210
bk_touch_calibration_start(void)211 bk_err_t bk_touch_calibration_start(void)
212 {
213 bk_touch_calib_enable(0);
214 delay(100);
215 bk_touch_calib_enable(1);
216 delay(200);
217
218 return BK_OK;
219 }
220
bk_touch_scan_mode_enable(uint32_t enable)221 bk_err_t bk_touch_scan_mode_enable(uint32_t enable)
222 {
223 if(enable) {
224 sys_drv_touch_scan_mode_enable(1);
225 } else {
226 sys_drv_touch_scan_mode_enable(0);
227 }
228
229 return BK_OK;
230 }
231
bk_touch_manul_mode_enable(uint32_t calib_value)232 bk_err_t bk_touch_manul_mode_enable(uint32_t calib_value)
233 {
234 sys_drv_touch_manul_mode_calib_value_set(calib_value);
235 sys_drv_touch_manul_mode_enable(1);
236 delay(100);
237
238 return BK_OK;
239 }
240
bk_touch_manul_mode_disable(void)241 bk_err_t bk_touch_manul_mode_disable(void)
242 {
243 sys_drv_touch_manul_mode_enable(0);
244
245 return BK_OK;
246 }
247
bk_touch_scan_mode_multi_channl_set(touch_channel_t touch_id)248 bk_err_t bk_touch_scan_mode_multi_channl_set(touch_channel_t touch_id)
249 {
250 sys_drv_touch_scan_mode_chann_set(touch_id);
251
252 return BK_OK;
253 }
254
bk_touch_int_enable(touch_channel_t touch_id,uint32_t enable)255 bk_err_t bk_touch_int_enable(touch_channel_t touch_id, uint32_t enable)
256 {
257 if(enable) {
258 sys_drv_touch_int_enable(1);
259 aon_pmu_drv_touch_int_enable(touch_id);
260 } else {
261 aon_pmu_drv_touch_int_disable(touch_id);
262 sys_drv_touch_int_enable(0);
263 }
264
265 return BK_OK;
266 }
267
bk_touch_get_int_status(void)268 bk_err_t bk_touch_get_int_status(void)
269 {
270 uint32_t int_status = 0;
271 int_status = aon_pmu_drv_get_touch_int_status();
272
273 return int_status;
274 }
275
bk_touch_clear_int(touch_channel_t touch_id)276 bk_err_t bk_touch_clear_int(touch_channel_t touch_id)
277 {
278 uint32_t touch_select = 0;
279 touch_select = bk_touch_channel_transfer(touch_id);
280 TOUCH_RETURN_ON_INVALID_ID(touch_select);
281 aon_pmu_drv_clear_touch_int(touch_id);
282
283 return BK_OK;
284 }
285
bk_touch_get_calib_value(void)286 uint32_t bk_touch_get_calib_value(void)
287 {
288 uint32_t calib_value = 0;
289 calib_value = aon_pmu_drv_get_cap_cal();
290
291 return calib_value;
292 }
293
bk_touch_get_touch_status(void)294 uint32_t bk_touch_get_touch_status(void)
295 {
296 uint32_t touch_status = 0;
297 touch_status = aon_pmu_drv_get_touch_state();
298
299 return touch_status;
300 }
301
bk_touch_register_touch_isr(touch_channel_t touch_id,touch_isr_t isr,void * param)302 bk_err_t bk_touch_register_touch_isr(touch_channel_t touch_id, touch_isr_t isr, void *param)
303 {
304 uint32_t touch_channel = 0;
305 touch_channel = bk_touch_channel_transfer(touch_id);
306 TOUCH_RETURN_ON_INVALID_ID(touch_channel);
307 GLOBAL_INT_DECLARATION();
308 GLOBAL_INT_DISABLE();
309 s_touch_isr[touch_channel].callback = isr;
310 s_touch_isr[touch_channel].param = param;
311 GLOBAL_INT_RESTORE();
312
313 return BK_OK;
314 }
315
touch_timer_isr(timer_id_t chan)316 static void touch_timer_isr(timer_id_t chan)
317 {
318 uint32_t touch_status = 0;
319 touch_status = bk_touch_get_touch_status();
320 if (!(touch_status & (1 << s_touch_channel))) {
321 bk_touch_int_enable(1 << s_touch_channel, 1);
322 bk_timer_stop(chan);
323 }
324 }
325
touch_isr(void)326 void touch_isr(void)
327 {
328 int ret = 0;
329 uint32_t int_status = 0;
330 uint32_t touch_id = 0;
331 int_status = bk_touch_get_int_status();
332
333 for (touch_id = 0; touch_id < SOC_TOUCH_ID_NUM; touch_id++)
334 {
335 if (int_status & (1 << touch_id)) {
336 TOUCH_LOGI("Touch[%d] has been selected!\r\n", touch_id);
337 s_touch_channel = touch_id;
338 bk_touch_clear_int(1 << touch_id);
339 bk_touch_int_enable(1 << touch_id, 0);
340 if (s_touch_isr[touch_id].callback) {
341 s_touch_isr[touch_id].callback(s_touch_isr[touch_id].param);
342 }
343
344 ret = bk_timer_start(TIMER_ID0, 100, touch_timer_isr);
345 if (ret != BK_OK) {
346 os_printf("Timer start failed\r\n");
347 }
348 break;
349 }
350 }
351 }
352
353
354
355