2019-06-30 01:03:00 +00:00
|
|
|
/*
|
2020-03-13 15:36:44 +00:00
|
|
|
* Touch driver for Nintendo Switch's STM FingerTip S (4cd60d) touch controller
|
2019-06-30 01:03:00 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 2018 langerhans
|
2020-03-13 15:36:44 +00:00
|
|
|
* Copyright (c) 2018-2020 CTCaer
|
2019-06-30 01:03:00 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "../soc/clock.h"
|
|
|
|
#include "../soc/i2c.h"
|
|
|
|
#include "../soc/pinmux.h"
|
|
|
|
#include "../power/max7762x.h"
|
|
|
|
#include "../power/max77620.h"
|
|
|
|
#include "../soc/gpio.h"
|
|
|
|
#include "../soc/t210.h"
|
|
|
|
#include "../utils/util.h"
|
|
|
|
#include "touch.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "../gfx/gfx.h"
|
2019-08-27 22:08:57 +00:00
|
|
|
#define DPRINTF(...) gfx_printf(__VA_ARGS__)
|
2019-06-30 01:03:00 +00:00
|
|
|
|
2020-03-13 06:39:38 +00:00
|
|
|
static int touch_command(u8 cmd, u8 *buf, u8 size)
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
2020-03-13 06:39:38 +00:00
|
|
|
int res = i2c_send_buf_small(I2C_3, STMFTS_I2C_ADDR, cmd, buf, size);
|
|
|
|
if (!res)
|
2019-06-30 01:03:00 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-13 06:48:20 +00:00
|
|
|
static int touch_read_reg(u8 *cmd, u32 csize, u8 *buf, u32 size)
|
|
|
|
{
|
|
|
|
int res = i2c_send_buf_small(I2C_3, STMFTS_I2C_ADDR, cmd[0], &cmd[1], csize - 1);
|
|
|
|
if (res)
|
|
|
|
res = i2c_recv_buf(buf, size, I2C_3, STMFTS_I2C_ADDR);
|
|
|
|
if (!res)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-13 15:36:44 +00:00
|
|
|
static void touch_wait_controller_ready()
|
|
|
|
{
|
|
|
|
u32 timeout = get_tmr_ms() + 100;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
u8 tmp = 0;
|
|
|
|
i2c_recv_buf_small(&tmp, 1, I2C_3, STMFTS_I2C_ADDR, STMFTS_READ_ONE_EVENT);
|
|
|
|
if (tmp == STMFTS_EV_CONTROLLER_READY || get_tmr_ms() > timeout)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 02:28:12 +00:00
|
|
|
#define X_REAL_MAX 1264
|
|
|
|
#define Y_REAL_MAX 704
|
|
|
|
#define EDGE_OFFSET 15
|
|
|
|
|
|
|
|
static void _touch_compensate_limits(touch_event *event, bool touching)
|
|
|
|
{
|
|
|
|
event->x = MAX(event->x, EDGE_OFFSET);
|
|
|
|
event->x = MIN(event->x, X_REAL_MAX);
|
|
|
|
event->x -= EDGE_OFFSET;
|
|
|
|
u32 x_adj = (1280 * 1000) / (X_REAL_MAX - EDGE_OFFSET);
|
|
|
|
event->x = ((u32)event->x * x_adj) / 1000;
|
|
|
|
|
|
|
|
if (touching)
|
|
|
|
{
|
|
|
|
event->y = MAX(event->y, EDGE_OFFSET);
|
|
|
|
event->y = MIN(event->y, Y_REAL_MAX);
|
|
|
|
event->y -= EDGE_OFFSET;
|
|
|
|
u32 y_adj = (720 * 1000) / (Y_REAL_MAX - EDGE_OFFSET);
|
|
|
|
event->y = ((u32)event->y * y_adj) / 1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
static void _touch_process_contact_event(touch_event *event, bool touching)
|
|
|
|
{
|
|
|
|
event->x = (event->raw[2] << 4) | ((event->raw[4] & STMFTS_MASK_Y_LSB) >> 4);
|
|
|
|
|
|
|
|
// Normally, GUI elements have bigger horizontal estate.
|
|
|
|
// Avoid parsing y axis when finger is removed to minimize touch noise.
|
|
|
|
if (touching)
|
|
|
|
{
|
|
|
|
event->y = (event->raw[3] << 4) | (event->raw[4] & STMFTS_MASK_X_MSB);
|
2020-03-13 06:34:16 +00:00
|
|
|
|
|
|
|
event->z = event->raw[5] | (event->raw[6] << 8);
|
|
|
|
event->z = event->z << 6;
|
|
|
|
u16 tmp = 0x40;
|
|
|
|
if ((event->raw[7] & 0x3F) != 1 && (event->raw[7] & 0x3F) != 0x3F)
|
|
|
|
tmp = event->raw[7] & 0x3F;
|
|
|
|
event->z /= tmp + 0x40;
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
event->fingers = ((event->raw[1] & STMFTS_MASK_TOUCH_ID) >> 4) + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
event->fingers = 0;
|
2020-03-03 02:28:12 +00:00
|
|
|
|
|
|
|
_touch_compensate_limits(event, touching);
|
2019-06-30 01:03:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 15:02:06 +00:00
|
|
|
static void _touch_parse_event(touch_event *event)
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
|
|
|
event->type = event->raw[1] & STMFTS_MASK_EVENT_ID;
|
|
|
|
|
2019-10-18 15:02:06 +00:00
|
|
|
switch (event->type)
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
|
|
|
case STMFTS_EV_MULTI_TOUCH_ENTER:
|
|
|
|
case STMFTS_EV_MULTI_TOUCH_MOTION:
|
|
|
|
_touch_process_contact_event(event, true);
|
2020-03-13 06:39:38 +00:00
|
|
|
if (event->z < 255) // Reject palm rest.
|
2019-06-30 01:03:00 +00:00
|
|
|
event->touch = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
event->touch = false;
|
|
|
|
event->type = STMFTS_EV_MULTI_TOUCH_LEAVE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STMFTS_EV_MULTI_TOUCH_LEAVE:
|
|
|
|
event->touch = false;
|
|
|
|
_touch_process_contact_event(event, false);
|
|
|
|
break;
|
|
|
|
case STMFTS_EV_NO_EVENT:
|
|
|
|
if (event->touch)
|
|
|
|
event->type = STMFTS_EV_MULTI_TOUCH_MOTION;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (event->touch && event->raw[0] == STMFTS_EV_MULTI_TOUCH_MOTION)
|
|
|
|
event->type = STMFTS_EV_MULTI_TOUCH_MOTION;
|
|
|
|
else
|
|
|
|
event->type = STMFTS_EV_MULTI_TOUCH_LEAVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// gfx_con_setpos(&gfx_con, 0, 300);
|
2019-09-09 13:56:37 +00:00
|
|
|
// DPRINTF("x = %d \ny = %d \nz = %d \n", event->x, event->y, event->z);
|
2019-06-30 01:03:00 +00:00
|
|
|
// DPRINTF("0 = %02X\n1 = %02x\n2 = %02x\n3 = %02x\n", event->raw[0], event->raw[1], event->raw[2], event->raw[3]);
|
|
|
|
// DPRINTF("4 = %02X\n5 = %02x\n6 = %02x\n7 = %02x\n", event->raw[4], event->raw[5], event->raw[6], event->raw[7]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void touch_poll(touch_event *event)
|
|
|
|
{
|
|
|
|
i2c_recv_buf_small(event->raw, 8, I2C_3, STMFTS_I2C_ADDR, STMFTS_LATEST_EVENT);
|
|
|
|
|
|
|
|
_touch_parse_event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
touch_event touch_poll_wait()
|
|
|
|
{
|
|
|
|
touch_event event;
|
2019-10-18 15:02:06 +00:00
|
|
|
do
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
|
|
|
touch_poll(&event);
|
|
|
|
} while (event.type != STMFTS_EV_MULTI_TOUCH_LEAVE);
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
touch_info touch_get_info()
|
|
|
|
{
|
|
|
|
touch_info info;
|
|
|
|
u8 buf[8];
|
|
|
|
memset(&buf, 0, 8);
|
|
|
|
i2c_recv_buf_small(buf, 8, I2C_3, STMFTS_I2C_ADDR, STMFTS_READ_INFO);
|
|
|
|
|
|
|
|
info.chip_id = buf[0] << 8 | buf[1];
|
|
|
|
info.fw_ver = buf[2] << 8 | buf[3];
|
|
|
|
info.config_id = buf[4];
|
|
|
|
info.config_ver = buf[5];
|
|
|
|
|
|
|
|
//DPRINTF("ID: %04X, FW Ver: %d.%02d\nCfg ID: %02x, Cfg Ver: %d\n",
|
|
|
|
// info.chip_id, info.fw_ver >> 8, info.fw_ver & 0xFF, info.config_id, info.config_ver);
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2020-03-13 06:48:20 +00:00
|
|
|
int touch_get_fw_info(touch_fw_info_t *fw)
|
|
|
|
{
|
|
|
|
u8 buf[8] = {0};
|
|
|
|
|
|
|
|
// Get fw address info.
|
|
|
|
u8 cmd[3] = { STMFTS_RW_FRAMEBUFFER_REG, 0, 0x60 };
|
|
|
|
int res = touch_read_reg(cmd, 3, buf, 3);
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
// Get fw info.
|
|
|
|
cmd[1] = buf[2]; cmd[2] = buf[1];
|
|
|
|
res = touch_read_reg(cmd, 3, buf, 8);
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
fw->fw_id = (buf[1] << 24) | (buf[2] << 16) | (buf[3] << 8) | buf[4];
|
|
|
|
fw->ftb_ver = (buf[6] << 8) | buf[5];
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd[2]++;
|
|
|
|
res = touch_read_reg(cmd, 3, buf, 8);
|
|
|
|
if (!res)
|
|
|
|
fw->fw_rev = (buf[7] << 8) | buf[6];
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
int touch_power_on()
|
|
|
|
{
|
|
|
|
// Configure touchscreen GPIO.
|
2019-09-12 20:20:38 +00:00
|
|
|
PINMUX_AUX(PINMUX_AUX_DAP4_SCLK) = PINMUX_PULL_DOWN | 1;
|
2019-06-30 01:03:00 +00:00
|
|
|
gpio_config(GPIO_PORT_J, GPIO_PIN_7, GPIO_MODE_GPIO);
|
|
|
|
gpio_output_enable(GPIO_PORT_J, GPIO_PIN_7, GPIO_OUTPUT_ENABLE);
|
|
|
|
gpio_write(GPIO_PORT_J, GPIO_PIN_7, GPIO_HIGH);
|
|
|
|
|
|
|
|
// IRQ and more.
|
2019-07-06 19:24:03 +00:00
|
|
|
// PINMUX_AUX(PINMUX_AUX_TOUCH_INT) = PINMUX_INPUT_ENABLE | PINMUX_TRISTATE | PINMUX_PULL_UP | 3;
|
2019-06-30 01:03:00 +00:00
|
|
|
// gpio_config(GPIO_PORT_X, GPIO_PIN_1, GPIO_MODE_GPIO);
|
2019-07-06 19:24:03 +00:00
|
|
|
// gpio_write(GPIO_PORT_X, GPIO_PIN_1, GPIO_LOW);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
// Configure Touscreen and GCAsic shared GPIO.
|
2019-12-07 21:23:01 +00:00
|
|
|
PINMUX_AUX(PINMUX_AUX_CAM_I2C_SDA) = PINMUX_LPDR | PINMUX_INPUT_ENABLE | PINMUX_TRISTATE | PINMUX_PULL_UP | 2;
|
|
|
|
PINMUX_AUX(PINMUX_AUX_CAM_I2C_SCL) = PINMUX_IO_HV | PINMUX_LPDR | PINMUX_TRISTATE | PINMUX_PULL_DOWN | 2;
|
2019-06-30 01:03:00 +00:00
|
|
|
gpio_config(GPIO_PORT_S, GPIO_PIN_3, GPIO_MODE_GPIO);
|
|
|
|
|
|
|
|
// Initialize I2C3.
|
|
|
|
pinmux_config_i2c(I2C_3);
|
|
|
|
clock_enable_i2c(I2C_3);
|
|
|
|
i2c_init(I2C_3);
|
|
|
|
|
|
|
|
// Enables LDO6 for touchscreen VDD/AVDD supply
|
2019-07-06 19:24:03 +00:00
|
|
|
max77620_regulator_set_volt_and_flags(REGULATOR_LDO6, 2900000, MAX77620_POWER_MODE_NORMAL);
|
|
|
|
i2c_send_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_LDO6_CFG2,
|
2019-10-18 15:02:06 +00:00
|
|
|
MAX77620_LDO_CFG2_ADE_ENABLE | (3 << 3) | (MAX77620_POWER_MODE_NORMAL << MAX77620_LDO_POWER_MODE_SHIFT));
|
2019-06-30 01:03:00 +00:00
|
|
|
|
2020-03-13 15:36:44 +00:00
|
|
|
touch_wait_controller_ready();
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
// Initialize touchscreen module.
|
2020-03-13 06:39:38 +00:00
|
|
|
u8 cmd[3] = { 0, 0x28, 0x80 }; // System reset cmd.
|
|
|
|
if (touch_command(STMFTS_WRITE_REG, cmd, 3))
|
2019-06-30 01:03:00 +00:00
|
|
|
return 0;
|
2020-03-13 06:39:38 +00:00
|
|
|
msleep(10);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
2020-03-13 15:36:44 +00:00
|
|
|
touch_wait_controller_ready();
|
2019-06-30 01:03:00 +00:00
|
|
|
|
2020-03-13 06:39:38 +00:00
|
|
|
cmd[0] = 1;
|
|
|
|
if (touch_command(STMFTS_AUTO_CALIBRATION, cmd, 1))
|
2019-06-30 01:03:00 +00:00
|
|
|
return 0;
|
|
|
|
|
2020-03-13 06:39:38 +00:00
|
|
|
if (touch_command(STMFTS_MS_MT_SENSE_ON, NULL, 0))
|
2019-06-30 01:03:00 +00:00
|
|
|
return 0;
|
|
|
|
|
2020-03-13 06:39:38 +00:00
|
|
|
if (touch_command(STMFTS_CLEAR_EVENT_STACK, NULL, 0))
|
2020-03-03 02:28:12 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:02:06 +00:00
|
|
|
void touch_power_off()
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
|
|
|
// Disable touchscreen power.
|
|
|
|
gpio_write(GPIO_PORT_J, GPIO_PIN_7, GPIO_LOW);
|
|
|
|
|
2019-07-06 19:24:03 +00:00
|
|
|
// Disables LDO6 for touchscreen VDD, AVDD supply
|
|
|
|
max77620_regulator_enable(REGULATOR_LDO6, 0);
|
|
|
|
i2c_send_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_LDO6_CFG2,
|
|
|
|
MAX77620_LDO_CFG2_ADE_ENABLE | (2 << 3) | (MAX77620_POWER_MODE_NORMAL << MAX77620_LDO_POWER_MODE_SHIFT));
|
|
|
|
|
|
|
|
clock_disable_i2c(I2C_3);
|
2019-06-30 01:03:00 +00:00
|
|
|
}
|