1 /*
2 * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved.
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 /**
17 * @file wm_touchsensor.c
18 *
19 * @brief touchsensor Driver Module
20 *
21 * @author
22 *
23 * Copyright (c) 2021 Winner Microelectronics Co., Ltd.
24 */
25 #include "wm_debug.h"
26 #include "wm_regs.h"
27 #include "wm_irq.h"
28 #include "wm_cpu.h"
29 #include "wm_gpio.h"
30
31 typedef void (*touchsensor_cb)(u32 status);
32 touchsensor_cb tc_callback = NULL;
33 /**
34 * @brief This function is used to initialize touch sensor.
35 *
36 * @param[in] sensorno is the touch sensor number from 1-15
37 * @param[in] scan_period is scan period for per touch sensor ,unit:16ms, >0
38 * @param[in] window is count window, window must be greater than 2.Real count window is window - 2.
39 * @param[in] enable is touch sensor enable bit.
40 *
41 * @retval 0:success
42 *
43 * @note if use touch sensor, user must configure the IO multiplex by API wm_touch_sensor_config.
44 */
tls_touchsensor_init_config(u32 sensorno,u8 scan_period,u8 window,u32 enable)45 int tls_touchsensor_init_config(u32 sensorno, u8 scan_period, u8 window, u32 enable)
46 {
47 u32 regval = 0;
48
49 regval = tls_reg_read32(HR_TC_CONFIG);
50
51 /* firstly, disable scan function */
52 tls_reg_write32(HR_TC_CONFIG, regval&(~(1<<TOUCH_SENSOR_EN_BIT)));
53
54 if (scan_period >=0x3F) {
55 regval &= ~(0x3F<<SCAN_PERID_SHIFT_BIT);
56 regval |= (scan_period<<SCAN_PERID_SHIFT_BIT);
57 }
58
59 if (window) {
60 regval &= ~(0x3F<<CAPDET_CNT_SHIFT_BIT);
61 regval |= (window<<CAPDET_CNT_SHIFT_BIT);
62 }
63
64 if (sensorno && (sensorno <= 15)) {
65 regval |= (1<<(sensorno-1+TOUCH_SENSOR_SEL_SHIFT_BIT));
66 }
67
68 if (enable) {
69 regval |= (1<<TOUCH_SENSOR_EN_BIT);
70 }
71
72 tls_reg_write32(HR_TC_CONFIG, regval);
73
74 return 0;
75 }
76
77 /**
78 * @brief This function is used to deinit touch sensor's selection and disable touch.
79 *
80 * @param[in] sensorno is the touch sensor number from 1-15
81 *
82 * @retval 0:success
83 *
84 * @note if do not use touch sensor, user can deinit by this interface and configure this touch sensor as GPIO.
85 */
tls_touchsensor_deinit(u32 sensorno)86 int tls_touchsensor_deinit(u32 sensorno)
87 {
88 u32 regval = 0;
89
90 regval = tls_reg_read32(HR_TC_CONFIG);
91 if (sensorno && (sensorno <= 15)) {
92 regval &= ~(1<<(sensorno-1+TOUCH_SENSOR_SEL_SHIFT_BIT));
93 }
94 regval &= ~(1<<TOUCH_SENSOR_EN_BIT);
95 tls_reg_write32(HR_TC_CONFIG, regval);
96
97 return 0;
98 }
99
100 /**
101 * @brief This function is used to set threshold per touch sensor.
102 *
103 * @param[in] sensorno is the touch sensor number from 1-15
104 * @param[in] threshold is the sensorno's touch sensor threshold,max value is 127.
105 *
106 * @retval 0:success. minus value: parameter wrong.
107 *
108 * @note None
109 */
tls_touchsensor_threshold_config(u32 sensorno,u8 threshold)110 int tls_touchsensor_threshold_config(u32 sensorno, u8 threshold)
111 {
112 u32 regvalue = 0;
113 if ((sensorno == 0) || (sensorno > 15)) {
114 return -1;
115 }
116
117 if (threshold > 0x7F) {
118 return -2;
119 }
120
121 regvalue = tls_reg_read32(HR_TC_CONFIG+sensorno*4);
122 regvalue &= ~(0x7F);
123 regvalue |= threshold;
124 tls_reg_write32(HR_TC_CONFIG + sensorno*4, regvalue);
125 return 0;
126 }
127
128 /**
129 * @brief This function is used to get touch sensor's count number.
130 *
131 * @param[in] sensorno is the touch sensor number from 1 to 15.
132 *
133 * @retval sensorno's count number .
134 *
135 * @note None
136 */
tls_touchsensor_countnum_get(u32 sensorno)137 int tls_touchsensor_countnum_get(u32 sensorno)
138 {
139 if ((sensorno == 0) || (sensorno > 15)) {
140 return -1;
141 }
142
143 return ((tls_reg_read32(HR_TC_CONFIG+sensorno*4)>>8)&0x3FFF);
144 }
145
146 /**
147 * @brief This function is used to enable touch sensor's irq.
148 *
149 * @param[in] sensorno is the touch sensor number from 1 to 15.
150 *
151 * @retval 0:successfully enable irq, -1:parameter wrong.
152 *
153 * @note None
154 */
tls_touchsensor_irq_enable(u32 sensorno)155 int tls_touchsensor_irq_enable(u32 sensorno)
156 {
157 u32 value = 0;
158 if (sensorno && (sensorno <= 15)) {
159 value = tls_reg_read32(HR_TC_INT_EN);
160 value |= (1<<(sensorno+15));
161 tls_reg_write32(HR_TC_INT_EN, value);
162 tls_irq_enable(TOUCH_IRQn);
163 return 0;
164 }
165
166 return -1;
167 }
168
169 /**
170 * @brief This function is used to disable touch sensor's irq.
171 *
172 * @param[in] sensorno is the touch sensor number from 1 to 15.
173 *
174 * @retval 0:successfully disable irq, -1:parameter wrong.
175 *
176 * @note None
177 */
tls_touchsensor_irq_disable(u32 sensorno)178 int tls_touchsensor_irq_disable(u32 sensorno)
179 {
180 u32 value = 0;
181 if (sensorno && (sensorno <= 15)) {
182 value = tls_reg_read32(HR_TC_INT_EN);
183 value &= ~(1<<(sensorno+15));
184 tls_reg_write32(HR_TC_INT_EN, value);
185 if ((value & 0xFFFF0000) == 0) {
186 tls_irq_disable(TOUCH_IRQn);
187 }
188 return 0;
189 }
190
191 return -1;
192 }
193
194 /**
195 * @brief This function is used to register touch sensor's irq callback.
196 *
197 * @param[in] callback is call back for user's application.
198 *
199 * @retval None.
200 *
201 * @note None
202 */
tls_touchsensor_irq_register(void (* callback)(u32 status))203 void tls_touchsensor_irq_register(void (*callback)(u32 status))
204 {
205 tc_callback = callback;
206 }
207
208 /**
209 * @brief This function is touch sensor's irq handler.
210 *
211 * @param[in] None
212 *
213 * @retval None
214 *
215 * @note None
216 */
tls_touchsensor_irq_handler(void)217 ATTRIBUTE_ISR void tls_touchsensor_irq_handler(void)
218 {
219 u32 value = 0;
220 csi_kernel_intrpt_enter();
221 value = tls_reg_read32(HR_TC_INT_EN);
222 if (tc_callback) {
223 tc_callback(value&0xFFFF);
224 }
225 tls_reg_write32(HR_TC_INT_EN, value);
226 csi_kernel_intrpt_exit();
227 }
228
229 /**
230 * @brief This function is used to get touch sensor's irq status.
231 *
232 * @param[in] sensorno is the touch sensor number from 1 to 15.
233 *
234 * @retval >=0:irq status, -1:parameter wrong.
235 *
236 * @note None
237 */
tls_touchsensor_irq_status_get(u32 sensorno)238 int tls_touchsensor_irq_status_get(u32 sensorno)
239 {
240 u32 value = 0;
241
242 if (sensorno && (sensorno <= 15)) {
243 value = tls_reg_read32(HR_TC_INT_EN);
244 return (value&(1<<(sensorno-1)))?1:0;
245 }
246
247 return -1;
248 }
249
250