[processor-sdk/performance-audio-sr.git] / psdk_cust / pdk_k2g_1_0_1_0_eng / packages / ti / board / diag / lcd / src / evmk2g_touch.c
1 /*
2 * Copyright (c) 2016, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
34 /**
35 *
36 * \file evmk2g_touch.c
37 *
38 * \brief This file contains functions for touch driver
39 *
40 ******************************************************************************/
42 #include "platform_internal.h"
44 #include <ti/drv/i2c/I2C.h>
45 #include <ti/drv/i2c/soc/I2C_soc.h>
47 #include <ti/drv/gpio/GPIO.h>
48 #include <ti/csl/soc.h>
49 #include <ti/drv/gpio/soc/GPIO_soc.h>
51 /* Port and pin number mask for touch input.
52 Bits 7-0: Pin number and Bits 15-8: Port number */
53 #define GPIO_TOUCH_INPUT (TOUCH_GPIO_PIN_NUM | (TOUCH_GPIO_INSTANCE << 8))
55 /* GPIO Driver board specific pin configuration structure */
56 GPIO_PinConfig gpioPinConfigs[] = {
57 GPIO_TOUCH_INPUT | GPIO_CFG_INPUT
58 };
60 /* GPIO Driver call back functions */
61 GPIO_CallbackFxn gpioCallbackFunctions[] = {
62 NULL
63 };
65 /* GPIO Driver configuration structure */
66 GPIO_v0_Config GPIO_v0_config = {
67 gpioPinConfigs,
68 gpioCallbackFunctions,
69 sizeof(gpioPinConfigs) / sizeof(GPIO_PinConfig),
70 sizeof(gpioCallbackFunctions) / sizeof(GPIO_CallbackFxn),
71 0,
72 };
74 static I2C_Params touchI2cParams;
75 static I2C_Handle touchI2cHandle = NULL;
77 extern const I2C_config_list I2C_config;
79 /******************************************************************************
80 ** INTERNAL FUNCTION DEFINITIONS
81 ******************************************************************************/
83 /**
84 *
85 * \brief Function to configure the TOUCH GPIO.
86 *
87 * \return NONE.
88 *
89 */
90 static void gpioTouchIntConfigure(void)
91 {
92 GPIO_init();
93 }
95 /**
96 *
97 * \brief Function to read the status of TOUCH GPIO.
98 *
99 * \return 0 : if GPIO pin is HIGH.
100 * 1 : if GPIO pin is LOW.
101 *
102 */
103 uint8_t touchGpioPinRead(void)
104 {
105 uint8_t ret;
107 /* Assuming it as active low. Pin goes LOW when there is touch signal */
108 ret = GPIO_read(0);
110 return (!ret);
111 }
113 /**
114 *
115 * \brief Function to write the data into touch controller register.
116 *
117 * \param reg [IN] : Register to be written.
118 * \param data [IN] : Data to write into the register.
119 *
120 * \return TOUCH_SUCCESS : On Success.
121 * TOUCH_ERR : On Failure.
122 *
123 */
124 int8_t touchWrite(uint8_t reg, uint8_t data)
125 {
126 I2C_Transaction i2cTransaction;
127 uint8_t slaveData[2];
128 bool ret = 0;
130 slaveData[0] = reg;
131 slaveData[1] = data;
133 i2cTransaction.slaveAddress = TOUCH_SLAVE_ADDR;
134 i2cTransaction.writeBuf = (uint8_t *)&slaveData[0];
135 i2cTransaction.writeCount = 2;
136 i2cTransaction.readCount = 0;
138 ret = I2C_transfer(touchI2cHandle, &i2cTransaction);
139 if (!ret) {
140 IFPRINT (UART_printf("touchWrite: i2c Write error - %d\n", ret));
141 ret = TOUCH_ERR;
142 }
144 return (TOUCH_SUCCESS);
145 }
147 /**
148 *
149 * \brief Function to read data from the touch controller reg.
150 *
151 * \param reg [IN] : Register to be read.
152 *
153 * \return Data read from register : On Success.
154 * TOUCH_ERR : On Failure.
155 *
156 */
157 int8_t touchRead(uint32_t reg, uint8_t *data)
158 {
159 I2C_Transaction i2cTransaction;
160 uint8_t txData[2];
161 bool ret = 0;
163 txData[0] = reg;
165 i2cTransaction.slaveAddress = TOUCH_SLAVE_ADDR;
166 i2cTransaction.writeBuf = &txData[0];
167 i2cTransaction.writeCount = 1;
168 i2cTransaction.readBuf = data;
169 i2cTransaction.readCount = 1;
171 ret = I2C_transfer(touchI2cHandle, &i2cTransaction);
172 if (!ret) {
173 IFPRINT (UART_printf("touchRead - i2c Read error - %d\n", ret));
174 return TOUCH_ERR;
175 }
177 return (TOUCH_SUCCESS);
178 }
180 /**
181 *
182 * \brief Function to get all the value of Touch Panel registers
183 * from [00h to 1Fh] and save in the array.
184 *
185 * \param data [IN] : Buffer to store the touch data read
186 * touches : No: of touch points
187 *
188 * \return TOUCH_SUCCESS : On Success.
189 * TOUCH_ERR : On Failure.
190 *
191 */
192 int8_t touchGetAllData(uint16_t data[][DIMENSIONS], uint8_t touches)
193 {
194 int8_t ret = 0;
195 uint8_t regVal = 0;
196 uint8_t i;
197 uint8_t x = 0;
198 uint8_t y = 1;
200 memset(data, 0x0, sizeof(data));
202 for (i = 0; i < touches; i++)
203 {
204 ret = touchRead(TOUCH1_XH + (i * 0x6), ®Val);
205 if (ret == TOUCH_ERR) {
206 IFPRINT (UART_printf("touchRead failed with %d\n", ret));
207 UART_printf("touchRead failed with %d\n", ret);
208 return TOUCH_ERR;
209 }
210 if (regVal & 0x80) {
211 UART_printf("pen-down for Touch%d\n", i+1);
212 data[i][x] = (regVal & 0x0F) << 8;
214 ret = touchRead(TOUCH1_XL + (i * 0x6), ®Val);
215 if (ret == TOUCH_ERR) {
216 IFPRINT (UART_printf("touchRead failed with %d\n", ret));
217 return TOUCH_ERR;
218 }
219 else
220 data[i][x] |= regVal;
222 ret = touchRead(TOUCH1_YH + (i * 0x6), ®Val);
223 if (ret == TOUCH_ERR) {
224 IFPRINT (UART_printf("touchRead failed with %d\n", ret));
225 return TOUCH_ERR;
226 }
227 else
228 data[i][y] = (regVal & 0x0F) << 8;
230 ret = touchRead(TOUCH1_YL + (i * 0x6), ®Val);
231 if (ret == TOUCH_ERR) {
232 IFPRINT (UART_printf("touchRead failed with %d\n", ret));
233 return TOUCH_ERR;
234 }
235 else
236 data[i][y] |= regVal;
238 }
239 }
241 return (TOUCH_SUCCESS);
242 }
244 /**
245 *
246 * \brief Function to configure the touch controller and clear
247 * the status registers if the touch already exists.
248 *
249 * \return TOUCH_SUCCESS : On Success.
250 * TOUCH_ERR : On Failure.
251 *
252 */
253 int8_t touchSetup(void)
254 {
255 int8_t ret = 0;
256 int32_t timeOut = 15 * DELAY/2; /* 15 seconds */
257 // uint16_t TouchDump[5][2];
258 uint8_t regVal = 0;
259 uint8_t touch, numTouches;
261 /* ID reads */
262 UART_printf("\n=====================================\n");
263 /* Chip Vendor ID */
264 ret = touchRead(ID_G_CIPHER, ®Val);
265 if (ret) {
266 goto err;
267 }
268 UART_printf("Chip Vendor ID:---------------->0x%x\n", regVal);
270 /* CTPM Vendor's Chip ID */
271 ret = touchRead(ID_G_FT5201ID, ®Val);
272 if (ret) {
273 goto err;
274 }
275 UART_printf("CTPM Vendor's Chip ID:--------->0x%x\n", regVal);
277 /* Firmware ID */
278 ret = touchRead(ID_G_FIRMID, ®Val);
279 if (ret) {
280 goto err;
281 }
282 UART_printf("Firmware ID:------------------->0x%x\n", regVal);
284 /* Firmware version High */
285 ret = touchRead(ID_G_LIB_VERSION_H, ®Val);
286 if (ret) {
287 goto err;
288 }
289 UART_printf("Firmware version High byte:---->0x%x\n", regVal);
291 /* Firmware version Low */
292 ret = touchRead(ID_G_LIB_VERSION_L, ®Val);
293 if (ret) {
294 goto err;
295 }
296 UART_printf("Firmware version Low byte:----->0x%x\n", regVal);
297 UART_printf("=====================================\n\n");
299 UART_printf("Touch Setup in Progress.. Please wait...\n");
301 /* Calibration */
302 ret = touchWrite(ID_G_AUTO_CLB_MODE, 0x00);
303 if (ret) {
304 goto err;
305 }
307 /* Delay of 10ms */
308 platform_delay(10000);
310 while (1)
311 {
312 ret = touchRead(ID_G_AUTO_CLB_MODE, ®Val);
313 if (ret) {
314 goto err;
315 }
316 if (!regVal)
317 break;
318 }
320 ret = touchWrite(DEVICE_MODE, NORMAL_OPMODE);
321 if (ret) {
322 goto err;
323 }
325 ret = touchWrite(ID_G_THGROUP, 0x10);
326 if (ret) {
327 goto err;
328 }
330 ret = touchWrite(ID_G_THPEAK, 0x3c);
331 if (ret) {
332 goto err;
333 }
335 ret = touchWrite(ID_G_THCAL, 0x1d);
336 if (ret) {
337 goto err;
338 }
340 ret = touchWrite(ID_G_THWATER, 0xd3);
341 if (ret) {
342 goto err;
343 }
345 ret = touchWrite(ID_G_TEMP, 0xeb);
346 if (ret) {
347 goto err;
348 }
350 ret = touchWrite(ID_G_THDIFF, 0xa0);
351 if (ret)
352 goto err;
354 ret = touchWrite(ID_G_TIME_ENTER_MONITOR, 0xc8);
355 if (ret) {
356 goto err;
357 }
359 ret = touchWrite(ID_G_PERIODACTIVE, 0x06);
360 if (ret) {
361 goto err;
362 }
364 ret = touchWrite(ID_G_PERIODMONITOR, 0x28);
365 if (ret) {
366 goto err;
367 }
368 ret = touchWrite(ID_G_MODE, 0x01);
369 if (ret) {
370 goto err;
371 }
373 /* Sometimes when initializing a touch already exists, we're going to
374 clear it. Timeout is set for 15 seconds */
375 do
376 {
377 touch = touchGpioPinRead();
378 ret = touchRead(TD_STATUS, ®Val);
379 if (ret < 0)
380 {
381 IFPRINT (UART_printf("Error in getting touch data\n"));
382 goto err;
383 }
385 touch |= numTouches;
386 platform_delay(DELAY);
387 timeOut -= DELAY;
388 } while (touch && (timeOut <= 0));
391 err :
392 if (ret != 0) {
393 IFPRINT (UART_printf("Error in doing touch setup\n"));
394 }
396 return (ret);
397 }
399 /**
400 *
401 * \brief Function to initialize the touch controller.
402 *
403 * No input parameters.
404 *
405 * \return NONE.
406 *
407 */
408 int8_t touchInit(void)
409 {
410 uint32_t index;
412 /* Configure GPIO pin */
413 gpioTouchIntConfigure();
415 for (index = 0; I2C_config[index].fxnTablePtr != NULL; index++)
416 {
417 ((I2C_HwAttrs *)I2C_config[index].hwAttrs)->enableIntr = false;
418 }
420 /* Initialize I2C module */
421 I2C_init();
422 I2C_Params_init(&touchI2cParams);
424 touchI2cHandle = I2C_open(TOUCH_I2C_INSTANCE, &touchI2cParams);
425 if(touchI2cHandle == NULL)
426 {
427 return (-1);
428 }
430 return (0);
431 }
433 /* Nothing past this point */