1842ff286SAnthony Kim /* 2842ff286SAnthony Kim * Copyright (C) 2012-2017 Hideep, Inc. 3842ff286SAnthony Kim * 4842ff286SAnthony Kim * This program is free software; you can redistribute it and/or modify it 5842ff286SAnthony Kim * under the terms of the GNU General Public License version 2 6842ff286SAnthony Kim * as published by the Free Software Foudation. 7842ff286SAnthony Kim */ 8842ff286SAnthony Kim 9842ff286SAnthony Kim #include <linux/module.h> 10842ff286SAnthony Kim #include <linux/of.h> 11842ff286SAnthony Kim #include <linux/firmware.h> 12842ff286SAnthony Kim #include <linux/delay.h> 13842ff286SAnthony Kim #include <linux/gpio.h> 14842ff286SAnthony Kim #include <linux/gpio/machine.h> 15842ff286SAnthony Kim #include <linux/i2c.h> 16842ff286SAnthony Kim #include <linux/acpi.h> 17842ff286SAnthony Kim #include <linux/interrupt.h> 18842ff286SAnthony Kim #include <linux/regmap.h> 19842ff286SAnthony Kim #include <linux/sysfs.h> 20842ff286SAnthony Kim #include <linux/input.h> 21842ff286SAnthony Kim #include <linux/input/mt.h> 22842ff286SAnthony Kim #include <linux/input/touchscreen.h> 23842ff286SAnthony Kim #include <linux/regulator/consumer.h> 24842ff286SAnthony Kim #include <asm/unaligned.h> 25842ff286SAnthony Kim 26842ff286SAnthony Kim #define HIDEEP_TS_NAME "HiDeep Touchscreen" 27842ff286SAnthony Kim #define HIDEEP_I2C_NAME "hideep_ts" 28842ff286SAnthony Kim 29842ff286SAnthony Kim #define HIDEEP_MT_MAX 10 30842ff286SAnthony Kim #define HIDEEP_KEY_MAX 3 31842ff286SAnthony Kim 32842ff286SAnthony Kim /* count(2) + touch data(100) + key data(6) */ 33842ff286SAnthony Kim #define HIDEEP_MAX_EVENT 108UL 34842ff286SAnthony Kim 35842ff286SAnthony Kim #define HIDEEP_TOUCH_EVENT_INDEX 2 36842ff286SAnthony Kim #define HIDEEP_KEY_EVENT_INDEX 102 37842ff286SAnthony Kim 38842ff286SAnthony Kim /* Touch & key event */ 39842ff286SAnthony Kim #define HIDEEP_EVENT_ADDR 0x240 40842ff286SAnthony Kim 41842ff286SAnthony Kim /* command list */ 42842ff286SAnthony Kim #define HIDEEP_RESET_CMD 0x9800 43842ff286SAnthony Kim 44842ff286SAnthony Kim /* event bit */ 45842ff286SAnthony Kim #define HIDEEP_MT_RELEASED BIT(4) 46842ff286SAnthony Kim #define HIDEEP_KEY_PRESSED BIT(7) 47842ff286SAnthony Kim #define HIDEEP_KEY_FIRST_PRESSED BIT(8) 48842ff286SAnthony Kim #define HIDEEP_KEY_PRESSED_MASK (HIDEEP_KEY_PRESSED | \ 49842ff286SAnthony Kim HIDEEP_KEY_FIRST_PRESSED) 50842ff286SAnthony Kim 51842ff286SAnthony Kim #define HIDEEP_KEY_IDX_MASK 0x0f 52842ff286SAnthony Kim 53842ff286SAnthony Kim /* For NVM */ 54842ff286SAnthony Kim #define HIDEEP_YRAM_BASE 0x40000000 55842ff286SAnthony Kim #define HIDEEP_PERIPHERAL_BASE 0x50000000 56842ff286SAnthony Kim #define HIDEEP_ESI_BASE (HIDEEP_PERIPHERAL_BASE + 0x00000000) 57842ff286SAnthony Kim #define HIDEEP_FLASH_BASE (HIDEEP_PERIPHERAL_BASE + 0x01000000) 58842ff286SAnthony Kim #define HIDEEP_SYSCON_BASE (HIDEEP_PERIPHERAL_BASE + 0x02000000) 59842ff286SAnthony Kim 60842ff286SAnthony Kim #define HIDEEP_SYSCON_MOD_CON (HIDEEP_SYSCON_BASE + 0x0000) 61842ff286SAnthony Kim #define HIDEEP_SYSCON_SPC_CON (HIDEEP_SYSCON_BASE + 0x0004) 62842ff286SAnthony Kim #define HIDEEP_SYSCON_CLK_CON (HIDEEP_SYSCON_BASE + 0x0008) 63842ff286SAnthony Kim #define HIDEEP_SYSCON_CLK_ENA (HIDEEP_SYSCON_BASE + 0x000C) 64842ff286SAnthony Kim #define HIDEEP_SYSCON_RST_CON (HIDEEP_SYSCON_BASE + 0x0010) 65842ff286SAnthony Kim #define HIDEEP_SYSCON_WDT_CON (HIDEEP_SYSCON_BASE + 0x0014) 66842ff286SAnthony Kim #define HIDEEP_SYSCON_WDT_CNT (HIDEEP_SYSCON_BASE + 0x0018) 67842ff286SAnthony Kim #define HIDEEP_SYSCON_PWR_CON (HIDEEP_SYSCON_BASE + 0x0020) 68842ff286SAnthony Kim #define HIDEEP_SYSCON_PGM_ID (HIDEEP_SYSCON_BASE + 0x00F4) 69842ff286SAnthony Kim 70842ff286SAnthony Kim #define HIDEEP_FLASH_CON (HIDEEP_FLASH_BASE + 0x0000) 71842ff286SAnthony Kim #define HIDEEP_FLASH_STA (HIDEEP_FLASH_BASE + 0x0004) 72842ff286SAnthony Kim #define HIDEEP_FLASH_CFG (HIDEEP_FLASH_BASE + 0x0008) 73842ff286SAnthony Kim #define HIDEEP_FLASH_TIM (HIDEEP_FLASH_BASE + 0x000C) 74842ff286SAnthony Kim #define HIDEEP_FLASH_CACHE_CFG (HIDEEP_FLASH_BASE + 0x0010) 75842ff286SAnthony Kim #define HIDEEP_FLASH_PIO_SIG (HIDEEP_FLASH_BASE + 0x400000) 76842ff286SAnthony Kim 77842ff286SAnthony Kim #define HIDEEP_ESI_TX_INVALID (HIDEEP_ESI_BASE + 0x0008) 78842ff286SAnthony Kim 79842ff286SAnthony Kim #define HIDEEP_PERASE 0x00040000 80842ff286SAnthony Kim #define HIDEEP_WRONLY 0x00100000 81842ff286SAnthony Kim 82842ff286SAnthony Kim #define HIDEEP_NVM_MASK_OFS 0x0000000C 83842ff286SAnthony Kim #define HIDEEP_NVM_DEFAULT_PAGE 0 84842ff286SAnthony Kim #define HIDEEP_NVM_SFR_WPAGE 1 85842ff286SAnthony Kim #define HIDEEP_NVM_SFR_RPAGE 2 86842ff286SAnthony Kim 87842ff286SAnthony Kim #define HIDEEP_PIO_SIG 0x00400000 88842ff286SAnthony Kim #define HIDEEP_PROT_MODE 0x03400000 89842ff286SAnthony Kim 90842ff286SAnthony Kim #define HIDEEP_NVM_PAGE_SIZE 128 91842ff286SAnthony Kim 92842ff286SAnthony Kim #define HIDEEP_DWZ_INFO 0x000002C0 93842ff286SAnthony Kim 94842ff286SAnthony Kim struct hideep_event { 95842ff286SAnthony Kim __le16 x; 96842ff286SAnthony Kim __le16 y; 97842ff286SAnthony Kim __le16 z; 98842ff286SAnthony Kim u8 w; 99842ff286SAnthony Kim u8 flag; 100842ff286SAnthony Kim u8 type; 101842ff286SAnthony Kim u8 index; 102842ff286SAnthony Kim }; 103842ff286SAnthony Kim 104842ff286SAnthony Kim struct dwz_info { 105842ff286SAnthony Kim __be32 code_start; 106842ff286SAnthony Kim u8 code_crc[12]; 107842ff286SAnthony Kim 108842ff286SAnthony Kim __be32 c_code_start; 109842ff286SAnthony Kim __be16 gen_ver; 110842ff286SAnthony Kim __be16 c_code_len; 111842ff286SAnthony Kim 112842ff286SAnthony Kim __be32 vr_start; 113842ff286SAnthony Kim __be16 rsv0; 114842ff286SAnthony Kim __be16 vr_len; 115842ff286SAnthony Kim 116842ff286SAnthony Kim __be32 ft_start; 117842ff286SAnthony Kim __be16 vr_version; 118842ff286SAnthony Kim __be16 ft_len; 119842ff286SAnthony Kim 120842ff286SAnthony Kim __be16 core_ver; 121842ff286SAnthony Kim __be16 boot_ver; 122842ff286SAnthony Kim 123842ff286SAnthony Kim __be16 release_ver; 124842ff286SAnthony Kim __be16 custom_ver; 125842ff286SAnthony Kim 126842ff286SAnthony Kim u8 factory_id; 127842ff286SAnthony Kim u8 panel_type; 128842ff286SAnthony Kim u8 model_name[6]; 129842ff286SAnthony Kim 130842ff286SAnthony Kim __be16 extra_option; 131842ff286SAnthony Kim __be16 product_code; 132842ff286SAnthony Kim 133842ff286SAnthony Kim __be16 vendor_id; 134842ff286SAnthony Kim __be16 product_id; 135842ff286SAnthony Kim }; 136842ff286SAnthony Kim 137842ff286SAnthony Kim struct pgm_packet { 138842ff286SAnthony Kim struct { 139842ff286SAnthony Kim u8 unused[3]; 140842ff286SAnthony Kim u8 len; 141842ff286SAnthony Kim __be32 addr; 142842ff286SAnthony Kim } header; 143842ff286SAnthony Kim __be32 payload[HIDEEP_NVM_PAGE_SIZE / sizeof(__be32)]; 144842ff286SAnthony Kim }; 145842ff286SAnthony Kim 146842ff286SAnthony Kim #define HIDEEP_XFER_BUF_SIZE sizeof(struct pgm_packet) 147842ff286SAnthony Kim 148842ff286SAnthony Kim struct hideep_ts { 149842ff286SAnthony Kim struct i2c_client *client; 150842ff286SAnthony Kim struct input_dev *input_dev; 151842ff286SAnthony Kim struct regmap *reg; 152842ff286SAnthony Kim 153842ff286SAnthony Kim struct touchscreen_properties prop; 154842ff286SAnthony Kim 155842ff286SAnthony Kim struct gpio_desc *reset_gpio; 156842ff286SAnthony Kim 157842ff286SAnthony Kim struct regulator *vcc_vdd; 158842ff286SAnthony Kim struct regulator *vcc_vid; 159842ff286SAnthony Kim 160842ff286SAnthony Kim struct mutex dev_mutex; 161842ff286SAnthony Kim 162842ff286SAnthony Kim u32 tch_count; 163842ff286SAnthony Kim u32 lpm_count; 164842ff286SAnthony Kim 165842ff286SAnthony Kim /* 166842ff286SAnthony Kim * Data buffer to read packet from the device (contacts and key 167842ff286SAnthony Kim * states). We align it on double-word boundary to keep word-sized 168842ff286SAnthony Kim * fields in contact data and double-word-sized fields in program 169842ff286SAnthony Kim * packet aligned. 170842ff286SAnthony Kim */ 171842ff286SAnthony Kim u8 xfer_buf[HIDEEP_XFER_BUF_SIZE] __aligned(4); 172842ff286SAnthony Kim 173842ff286SAnthony Kim int key_num; 174842ff286SAnthony Kim u32 key_codes[HIDEEP_KEY_MAX]; 175842ff286SAnthony Kim 176842ff286SAnthony Kim struct dwz_info dwz_info; 177842ff286SAnthony Kim 178842ff286SAnthony Kim unsigned int fw_size; 179842ff286SAnthony Kim u32 nvm_mask; 180842ff286SAnthony Kim }; 181842ff286SAnthony Kim 182842ff286SAnthony Kim static int hideep_pgm_w_mem(struct hideep_ts *ts, u32 addr, 183842ff286SAnthony Kim const __be32 *data, size_t count) 184842ff286SAnthony Kim { 185842ff286SAnthony Kim struct pgm_packet *packet = (void *)ts->xfer_buf; 186842ff286SAnthony Kim size_t len = count * sizeof(*data); 187842ff286SAnthony Kim struct i2c_msg msg = { 188842ff286SAnthony Kim .addr = ts->client->addr, 189842ff286SAnthony Kim .len = len + sizeof(packet->header.len) + 190842ff286SAnthony Kim sizeof(packet->header.addr), 191842ff286SAnthony Kim .buf = &packet->header.len, 192842ff286SAnthony Kim }; 193842ff286SAnthony Kim int ret; 194842ff286SAnthony Kim 195842ff286SAnthony Kim if (len > HIDEEP_NVM_PAGE_SIZE) 196842ff286SAnthony Kim return -EINVAL; 197842ff286SAnthony Kim 198842ff286SAnthony Kim packet->header.len = 0x80 | (count - 1); 199842ff286SAnthony Kim packet->header.addr = cpu_to_be32(addr); 200842ff286SAnthony Kim memcpy(packet->payload, data, len); 201842ff286SAnthony Kim 202842ff286SAnthony Kim ret = i2c_transfer(ts->client->adapter, &msg, 1); 203842ff286SAnthony Kim if (ret != 1) 204842ff286SAnthony Kim return ret < 0 ? ret : -EIO; 205842ff286SAnthony Kim 206842ff286SAnthony Kim return 0; 207842ff286SAnthony Kim } 208842ff286SAnthony Kim 209842ff286SAnthony Kim static int hideep_pgm_r_mem(struct hideep_ts *ts, u32 addr, 210842ff286SAnthony Kim __be32 *data, size_t count) 211842ff286SAnthony Kim { 212842ff286SAnthony Kim struct pgm_packet *packet = (void *)ts->xfer_buf; 213842ff286SAnthony Kim size_t len = count * sizeof(*data); 214842ff286SAnthony Kim struct i2c_msg msg[] = { 215842ff286SAnthony Kim { 216842ff286SAnthony Kim .addr = ts->client->addr, 217842ff286SAnthony Kim .len = sizeof(packet->header.len) + 218842ff286SAnthony Kim sizeof(packet->header.addr), 219842ff286SAnthony Kim .buf = &packet->header.len, 220842ff286SAnthony Kim }, 221842ff286SAnthony Kim { 222842ff286SAnthony Kim .addr = ts->client->addr, 223842ff286SAnthony Kim .flags = I2C_M_RD, 224842ff286SAnthony Kim .len = len, 225842ff286SAnthony Kim .buf = (u8 *)data, 226842ff286SAnthony Kim }, 227842ff286SAnthony Kim }; 228842ff286SAnthony Kim int ret; 229842ff286SAnthony Kim 230842ff286SAnthony Kim if (len > HIDEEP_NVM_PAGE_SIZE) 231842ff286SAnthony Kim return -EINVAL; 232842ff286SAnthony Kim 233842ff286SAnthony Kim packet->header.len = count - 1; 234842ff286SAnthony Kim packet->header.addr = cpu_to_be32(addr); 235842ff286SAnthony Kim 236842ff286SAnthony Kim ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); 237842ff286SAnthony Kim if (ret != ARRAY_SIZE(msg)) 238842ff286SAnthony Kim return ret < 0 ? ret : -EIO; 239842ff286SAnthony Kim 240842ff286SAnthony Kim return 0; 241842ff286SAnthony Kim } 242842ff286SAnthony Kim 243842ff286SAnthony Kim static int hideep_pgm_r_reg(struct hideep_ts *ts, u32 addr, u32 *val) 244842ff286SAnthony Kim { 245842ff286SAnthony Kim __be32 data; 246842ff286SAnthony Kim int error; 247842ff286SAnthony Kim 248842ff286SAnthony Kim error = hideep_pgm_r_mem(ts, addr, &data, 1); 249842ff286SAnthony Kim if (error) { 250842ff286SAnthony Kim dev_err(&ts->client->dev, 251842ff286SAnthony Kim "read of register %#08x failed: %d\n", 252842ff286SAnthony Kim addr, error); 253842ff286SAnthony Kim return error; 254842ff286SAnthony Kim } 255842ff286SAnthony Kim 256842ff286SAnthony Kim *val = be32_to_cpu(data); 257842ff286SAnthony Kim return 0; 258842ff286SAnthony Kim } 259842ff286SAnthony Kim 260842ff286SAnthony Kim static int hideep_pgm_w_reg(struct hideep_ts *ts, u32 addr, u32 val) 261842ff286SAnthony Kim { 262842ff286SAnthony Kim __be32 data = cpu_to_be32(val); 263842ff286SAnthony Kim int error; 264842ff286SAnthony Kim 265842ff286SAnthony Kim error = hideep_pgm_w_mem(ts, addr, &data, 1); 266842ff286SAnthony Kim if (error) { 267842ff286SAnthony Kim dev_err(&ts->client->dev, 268842ff286SAnthony Kim "write to register %#08x (%#08x) failed: %d\n", 269842ff286SAnthony Kim addr, val, error); 270842ff286SAnthony Kim return error; 271842ff286SAnthony Kim } 272842ff286SAnthony Kim 273842ff286SAnthony Kim return 0; 274842ff286SAnthony Kim } 275842ff286SAnthony Kim 276842ff286SAnthony Kim #define SW_RESET_IN_PGM(clk) \ 277842ff286SAnthony Kim { \ 278842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_SYSCON_WDT_CNT, (clk)); \ 279842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_SYSCON_WDT_CON, 0x03); \ 280842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_SYSCON_WDT_CON, 0x01); \ 281842ff286SAnthony Kim } 282842ff286SAnthony Kim 283842ff286SAnthony Kim #define SET_FLASH_PIO(ce) \ 284842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_FLASH_CON, \ 285842ff286SAnthony Kim 0x01 | ((ce) << 1)) 286842ff286SAnthony Kim 287842ff286SAnthony Kim #define SET_PIO_SIG(x, y) \ 288842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_FLASH_PIO_SIG + (x), (y)) 289842ff286SAnthony Kim 290842ff286SAnthony Kim #define SET_FLASH_HWCONTROL() \ 291842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_FLASH_CON, 0x00) 292842ff286SAnthony Kim 293842ff286SAnthony Kim #define NVM_W_SFR(x, y) \ 294842ff286SAnthony Kim { \ 295842ff286SAnthony Kim SET_FLASH_PIO(1); \ 296842ff286SAnthony Kim SET_PIO_SIG(x, y); \ 297842ff286SAnthony Kim SET_FLASH_PIO(0); \ 298842ff286SAnthony Kim } 299842ff286SAnthony Kim 300842ff286SAnthony Kim static void hideep_pgm_set(struct hideep_ts *ts) 301842ff286SAnthony Kim { 302842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_SYSCON_WDT_CON, 0x00); 303842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_SYSCON_SPC_CON, 0x00); 304842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_SYSCON_CLK_ENA, 0xFF); 305842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_SYSCON_CLK_CON, 0x01); 306842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_SYSCON_PWR_CON, 0x01); 307842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_FLASH_TIM, 0x03); 308842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_FLASH_CACHE_CFG, 0x00); 309842ff286SAnthony Kim } 310842ff286SAnthony Kim 311842ff286SAnthony Kim static int hideep_pgm_get_pattern(struct hideep_ts *ts, u32 *pattern) 312842ff286SAnthony Kim { 313842ff286SAnthony Kim u16 p1 = 0xAF39; 314842ff286SAnthony Kim u16 p2 = 0xDF9D; 315842ff286SAnthony Kim int error; 316842ff286SAnthony Kim 317842ff286SAnthony Kim error = regmap_bulk_write(ts->reg, p1, &p2, 1); 318842ff286SAnthony Kim if (error) { 319842ff286SAnthony Kim dev_err(&ts->client->dev, 320842ff286SAnthony Kim "%s: regmap_bulk_write() failed with %d\n", 321842ff286SAnthony Kim __func__, error); 322842ff286SAnthony Kim return error; 323842ff286SAnthony Kim } 324842ff286SAnthony Kim 325842ff286SAnthony Kim usleep_range(1000, 1100); 326842ff286SAnthony Kim 327842ff286SAnthony Kim /* flush invalid Tx load register */ 328842ff286SAnthony Kim error = hideep_pgm_w_reg(ts, HIDEEP_ESI_TX_INVALID, 0x01); 329842ff286SAnthony Kim if (error) 330842ff286SAnthony Kim return error; 331842ff286SAnthony Kim 332842ff286SAnthony Kim error = hideep_pgm_r_reg(ts, HIDEEP_SYSCON_PGM_ID, pattern); 333842ff286SAnthony Kim if (error) 334842ff286SAnthony Kim return error; 335842ff286SAnthony Kim 336842ff286SAnthony Kim return 0; 337842ff286SAnthony Kim } 338842ff286SAnthony Kim 339842ff286SAnthony Kim static int hideep_enter_pgm(struct hideep_ts *ts) 340842ff286SAnthony Kim { 341842ff286SAnthony Kim int retry_count = 10; 342842ff286SAnthony Kim u32 pattern; 343842ff286SAnthony Kim int error; 344842ff286SAnthony Kim 345842ff286SAnthony Kim while (retry_count--) { 346842ff286SAnthony Kim error = hideep_pgm_get_pattern(ts, &pattern); 347842ff286SAnthony Kim if (error) { 348842ff286SAnthony Kim dev_err(&ts->client->dev, 349842ff286SAnthony Kim "hideep_pgm_get_pattern failed: %d\n", error); 350842ff286SAnthony Kim } else if (pattern != 0x39AF9DDF) { 351842ff286SAnthony Kim dev_err(&ts->client->dev, "%s: bad pattern: %#08x\n", 352842ff286SAnthony Kim __func__, pattern); 353842ff286SAnthony Kim } else { 354842ff286SAnthony Kim dev_dbg(&ts->client->dev, "found magic code"); 355842ff286SAnthony Kim 356842ff286SAnthony Kim hideep_pgm_set(ts); 357842ff286SAnthony Kim usleep_range(1000, 1100); 358842ff286SAnthony Kim 359842ff286SAnthony Kim return 0; 360842ff286SAnthony Kim } 361842ff286SAnthony Kim } 362842ff286SAnthony Kim 363842ff286SAnthony Kim dev_err(&ts->client->dev, "failed to enter pgm mode\n"); 364842ff286SAnthony Kim SW_RESET_IN_PGM(1000); 365842ff286SAnthony Kim return -EIO; 366842ff286SAnthony Kim } 367842ff286SAnthony Kim 368842ff286SAnthony Kim static void hideep_nvm_unlock(struct hideep_ts *ts) 369842ff286SAnthony Kim { 370842ff286SAnthony Kim u32 unmask_code; 371842ff286SAnthony Kim 372842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_FLASH_CFG, HIDEEP_NVM_SFR_RPAGE); 373842ff286SAnthony Kim hideep_pgm_r_reg(ts, 0x0000000C, &unmask_code); 374842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_FLASH_CFG, HIDEEP_NVM_DEFAULT_PAGE); 375842ff286SAnthony Kim 376842ff286SAnthony Kim /* make it unprotected code */ 377842ff286SAnthony Kim unmask_code &= ~HIDEEP_PROT_MODE; 378842ff286SAnthony Kim 379842ff286SAnthony Kim /* compare unmask code */ 380842ff286SAnthony Kim if (unmask_code != ts->nvm_mask) 381842ff286SAnthony Kim dev_warn(&ts->client->dev, 382842ff286SAnthony Kim "read mask code different %#08x vs %#08x", 383842ff286SAnthony Kim unmask_code, ts->nvm_mask); 384842ff286SAnthony Kim 385842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_FLASH_CFG, HIDEEP_NVM_SFR_WPAGE); 386842ff286SAnthony Kim SET_FLASH_PIO(0); 387842ff286SAnthony Kim 388842ff286SAnthony Kim NVM_W_SFR(HIDEEP_NVM_MASK_OFS, ts->nvm_mask); 389842ff286SAnthony Kim SET_FLASH_HWCONTROL(); 390842ff286SAnthony Kim hideep_pgm_w_reg(ts, HIDEEP_FLASH_CFG, HIDEEP_NVM_DEFAULT_PAGE); 391842ff286SAnthony Kim } 392842ff286SAnthony Kim 393842ff286SAnthony Kim static int hideep_check_status(struct hideep_ts *ts) 394842ff286SAnthony Kim { 395842ff286SAnthony Kim int time_out = 100; 396842ff286SAnthony Kim int status; 397842ff286SAnthony Kim int error; 398842ff286SAnthony Kim 399842ff286SAnthony Kim while (time_out--) { 400842ff286SAnthony Kim error = hideep_pgm_r_reg(ts, HIDEEP_FLASH_STA, &status); 401842ff286SAnthony Kim if (!error && status) 402842ff286SAnthony Kim return 0; 403842ff286SAnthony Kim 404842ff286SAnthony Kim usleep_range(1000, 1100); 405842ff286SAnthony Kim } 406842ff286SAnthony Kim 407842ff286SAnthony Kim return -ETIMEDOUT; 408842ff286SAnthony Kim } 409842ff286SAnthony Kim 410842ff286SAnthony Kim static int hideep_program_page(struct hideep_ts *ts, u32 addr, 411842ff286SAnthony Kim const __be32 *ucode, size_t xfer_count) 412842ff286SAnthony Kim { 413842ff286SAnthony Kim u32 val; 414842ff286SAnthony Kim int error; 415842ff286SAnthony Kim 416842ff286SAnthony Kim error = hideep_check_status(ts); 417842ff286SAnthony Kim if (error) 418842ff286SAnthony Kim return -EBUSY; 419842ff286SAnthony Kim 420842ff286SAnthony Kim addr &= ~(HIDEEP_NVM_PAGE_SIZE - 1); 421842ff286SAnthony Kim 422842ff286SAnthony Kim SET_FLASH_PIO(0); 423842ff286SAnthony Kim SET_FLASH_PIO(1); 424842ff286SAnthony Kim 425842ff286SAnthony Kim /* erase page */ 426842ff286SAnthony Kim SET_PIO_SIG(HIDEEP_PERASE | addr, 0xFFFFFFFF); 427842ff286SAnthony Kim 428842ff286SAnthony Kim SET_FLASH_PIO(0); 429842ff286SAnthony Kim 430842ff286SAnthony Kim error = hideep_check_status(ts); 431842ff286SAnthony Kim if (error) 432842ff286SAnthony Kim return -EBUSY; 433842ff286SAnthony Kim 434842ff286SAnthony Kim /* write page */ 435842ff286SAnthony Kim SET_FLASH_PIO(1); 436842ff286SAnthony Kim 437842ff286SAnthony Kim val = be32_to_cpu(ucode[0]); 438842ff286SAnthony Kim SET_PIO_SIG(HIDEEP_WRONLY | addr, val); 439842ff286SAnthony Kim 440842ff286SAnthony Kim hideep_pgm_w_mem(ts, HIDEEP_FLASH_PIO_SIG | HIDEEP_WRONLY, 441842ff286SAnthony Kim ucode, xfer_count); 442842ff286SAnthony Kim 443842ff286SAnthony Kim val = be32_to_cpu(ucode[xfer_count - 1]); 444842ff286SAnthony Kim SET_PIO_SIG(124, val); 445842ff286SAnthony Kim 446842ff286SAnthony Kim SET_FLASH_PIO(0); 447842ff286SAnthony Kim 448842ff286SAnthony Kim usleep_range(1000, 1100); 449842ff286SAnthony Kim 450842ff286SAnthony Kim error = hideep_check_status(ts); 451842ff286SAnthony Kim if (error) 452842ff286SAnthony Kim return -EBUSY; 453842ff286SAnthony Kim 454842ff286SAnthony Kim SET_FLASH_HWCONTROL(); 455842ff286SAnthony Kim 456842ff286SAnthony Kim return 0; 457842ff286SAnthony Kim } 458842ff286SAnthony Kim 459842ff286SAnthony Kim static int hideep_program_nvm(struct hideep_ts *ts, 460842ff286SAnthony Kim const __be32 *ucode, size_t ucode_len) 461842ff286SAnthony Kim { 462842ff286SAnthony Kim struct pgm_packet *packet_r = (void *)ts->xfer_buf; 463842ff286SAnthony Kim __be32 *current_ucode = packet_r->payload; 464842ff286SAnthony Kim size_t xfer_len; 465842ff286SAnthony Kim size_t xfer_count; 466842ff286SAnthony Kim u32 addr = 0; 467842ff286SAnthony Kim int error; 468842ff286SAnthony Kim 469842ff286SAnthony Kim hideep_nvm_unlock(ts); 470842ff286SAnthony Kim 471842ff286SAnthony Kim while (ucode_len > 0) { 472842ff286SAnthony Kim xfer_len = min_t(size_t, ucode_len, HIDEEP_NVM_PAGE_SIZE); 473842ff286SAnthony Kim xfer_count = xfer_len / sizeof(*ucode); 474842ff286SAnthony Kim 475842ff286SAnthony Kim error = hideep_pgm_r_mem(ts, 0x00000000 + addr, 476842ff286SAnthony Kim current_ucode, xfer_count); 477842ff286SAnthony Kim if (error) { 478842ff286SAnthony Kim dev_err(&ts->client->dev, 479842ff286SAnthony Kim "%s: failed to read page at offset %#08x: %d\n", 480842ff286SAnthony Kim __func__, addr, error); 481842ff286SAnthony Kim return error; 482842ff286SAnthony Kim } 483842ff286SAnthony Kim 484842ff286SAnthony Kim /* See if the page needs updating */ 485842ff286SAnthony Kim if (memcmp(ucode, current_ucode, xfer_len)) { 486842ff286SAnthony Kim error = hideep_program_page(ts, addr, 487842ff286SAnthony Kim ucode, xfer_count); 488842ff286SAnthony Kim if (error) { 489842ff286SAnthony Kim dev_err(&ts->client->dev, 490842ff286SAnthony Kim "%s: iwrite failure @%#08x: %d\n", 491842ff286SAnthony Kim __func__, addr, error); 492842ff286SAnthony Kim return error; 493842ff286SAnthony Kim } 494842ff286SAnthony Kim 495842ff286SAnthony Kim usleep_range(1000, 1100); 496842ff286SAnthony Kim } 497842ff286SAnthony Kim 498842ff286SAnthony Kim ucode += xfer_count; 499842ff286SAnthony Kim addr += xfer_len; 500842ff286SAnthony Kim ucode_len -= xfer_len; 501842ff286SAnthony Kim } 502842ff286SAnthony Kim 503842ff286SAnthony Kim return 0; 504842ff286SAnthony Kim } 505842ff286SAnthony Kim 506842ff286SAnthony Kim static int hideep_verify_nvm(struct hideep_ts *ts, 507842ff286SAnthony Kim const __be32 *ucode, size_t ucode_len) 508842ff286SAnthony Kim { 509842ff286SAnthony Kim struct pgm_packet *packet_r = (void *)ts->xfer_buf; 510842ff286SAnthony Kim __be32 *current_ucode = packet_r->payload; 511842ff286SAnthony Kim size_t xfer_len; 512842ff286SAnthony Kim size_t xfer_count; 513842ff286SAnthony Kim u32 addr = 0; 514842ff286SAnthony Kim int i; 515842ff286SAnthony Kim int error; 516842ff286SAnthony Kim 517842ff286SAnthony Kim while (ucode_len > 0) { 518842ff286SAnthony Kim xfer_len = min_t(size_t, ucode_len, HIDEEP_NVM_PAGE_SIZE); 519842ff286SAnthony Kim xfer_count = xfer_len / sizeof(*ucode); 520842ff286SAnthony Kim 521842ff286SAnthony Kim error = hideep_pgm_r_mem(ts, 0x00000000 + addr, 522842ff286SAnthony Kim current_ucode, xfer_count); 523842ff286SAnthony Kim if (error) { 524842ff286SAnthony Kim dev_err(&ts->client->dev, 525842ff286SAnthony Kim "%s: failed to read page at offset %#08x: %d\n", 526842ff286SAnthony Kim __func__, addr, error); 527842ff286SAnthony Kim return error; 528842ff286SAnthony Kim } 529842ff286SAnthony Kim 530842ff286SAnthony Kim if (memcmp(ucode, current_ucode, xfer_len)) { 531842ff286SAnthony Kim const u8 *ucode_bytes = (const u8 *)ucode; 532842ff286SAnthony Kim const u8 *current_bytes = (const u8 *)current_ucode; 533842ff286SAnthony Kim 534842ff286SAnthony Kim for (i = 0; i < xfer_len; i++) 535842ff286SAnthony Kim if (ucode_bytes[i] != current_bytes[i]) 536842ff286SAnthony Kim dev_err(&ts->client->dev, 537842ff286SAnthony Kim "%s: mismatch @%#08x: (%#02x vs %#02x)\n", 538842ff286SAnthony Kim __func__, addr + i, 539842ff286SAnthony Kim ucode_bytes[i], 540842ff286SAnthony Kim current_bytes[i]); 541842ff286SAnthony Kim 542842ff286SAnthony Kim return -EIO; 543842ff286SAnthony Kim } 544842ff286SAnthony Kim 545842ff286SAnthony Kim ucode += xfer_count; 546842ff286SAnthony Kim addr += xfer_len; 547842ff286SAnthony Kim ucode_len -= xfer_len; 548842ff286SAnthony Kim } 549842ff286SAnthony Kim 550842ff286SAnthony Kim return 0; 551842ff286SAnthony Kim } 552842ff286SAnthony Kim 553842ff286SAnthony Kim static int hideep_load_dwz(struct hideep_ts *ts) 554842ff286SAnthony Kim { 555842ff286SAnthony Kim u16 product_code; 556842ff286SAnthony Kim int error; 557842ff286SAnthony Kim 558842ff286SAnthony Kim error = hideep_enter_pgm(ts); 559842ff286SAnthony Kim if (error) 560842ff286SAnthony Kim return error; 561842ff286SAnthony Kim 562842ff286SAnthony Kim msleep(50); 563842ff286SAnthony Kim 564842ff286SAnthony Kim error = hideep_pgm_r_mem(ts, HIDEEP_DWZ_INFO, 565842ff286SAnthony Kim (void *)&ts->dwz_info, 566842ff286SAnthony Kim sizeof(ts->dwz_info) / sizeof(__be32)); 567842ff286SAnthony Kim 568842ff286SAnthony Kim SW_RESET_IN_PGM(10); 569842ff286SAnthony Kim msleep(50); 570842ff286SAnthony Kim 571842ff286SAnthony Kim if (error) { 572842ff286SAnthony Kim dev_err(&ts->client->dev, 573842ff286SAnthony Kim "failed to fetch DWZ data: %d\n", error); 574842ff286SAnthony Kim return error; 575842ff286SAnthony Kim } 576842ff286SAnthony Kim 577842ff286SAnthony Kim product_code = be16_to_cpu(ts->dwz_info.product_code); 578842ff286SAnthony Kim 579842ff286SAnthony Kim switch (product_code & 0xF0) { 580842ff286SAnthony Kim case 0x40: 581842ff286SAnthony Kim dev_dbg(&ts->client->dev, "used crimson IC"); 582842ff286SAnthony Kim ts->fw_size = 1024 * 48; 583842ff286SAnthony Kim ts->nvm_mask = 0x00310000; 584842ff286SAnthony Kim break; 585842ff286SAnthony Kim case 0x60: 586842ff286SAnthony Kim dev_dbg(&ts->client->dev, "used lime IC"); 587842ff286SAnthony Kim ts->fw_size = 1024 * 64; 588842ff286SAnthony Kim ts->nvm_mask = 0x0030027B; 589842ff286SAnthony Kim break; 590842ff286SAnthony Kim default: 591842ff286SAnthony Kim dev_err(&ts->client->dev, "product code is wrong: %#04x", 592842ff286SAnthony Kim product_code); 593842ff286SAnthony Kim return -EINVAL; 594842ff286SAnthony Kim } 595842ff286SAnthony Kim 596842ff286SAnthony Kim dev_dbg(&ts->client->dev, "firmware release version: %#04x", 597842ff286SAnthony Kim be16_to_cpu(ts->dwz_info.release_ver)); 598842ff286SAnthony Kim 599842ff286SAnthony Kim return 0; 600842ff286SAnthony Kim } 601842ff286SAnthony Kim 602842ff286SAnthony Kim static int hideep_flash_firmware(struct hideep_ts *ts, 603842ff286SAnthony Kim const __be32 *ucode, size_t ucode_len) 604842ff286SAnthony Kim { 605842ff286SAnthony Kim int retry_cnt = 3; 606842ff286SAnthony Kim int error; 607842ff286SAnthony Kim 608842ff286SAnthony Kim while (retry_cnt--) { 609842ff286SAnthony Kim error = hideep_program_nvm(ts, ucode, ucode_len); 610842ff286SAnthony Kim if (!error) { 611842ff286SAnthony Kim error = hideep_verify_nvm(ts, ucode, ucode_len); 612842ff286SAnthony Kim if (!error) 613842ff286SAnthony Kim return 0; 614842ff286SAnthony Kim } 615842ff286SAnthony Kim } 616842ff286SAnthony Kim 617842ff286SAnthony Kim return error; 618842ff286SAnthony Kim } 619842ff286SAnthony Kim 620842ff286SAnthony Kim static int hideep_update_firmware(struct hideep_ts *ts, 621842ff286SAnthony Kim const __be32 *ucode, size_t ucode_len) 622842ff286SAnthony Kim { 623842ff286SAnthony Kim int error, error2; 624842ff286SAnthony Kim 625842ff286SAnthony Kim dev_dbg(&ts->client->dev, "starting firmware update"); 626842ff286SAnthony Kim 627842ff286SAnthony Kim /* enter program mode */ 628842ff286SAnthony Kim error = hideep_enter_pgm(ts); 629842ff286SAnthony Kim if (error) 630842ff286SAnthony Kim return error; 631842ff286SAnthony Kim 632842ff286SAnthony Kim error = hideep_flash_firmware(ts, ucode, ucode_len); 633842ff286SAnthony Kim if (error) 634842ff286SAnthony Kim dev_err(&ts->client->dev, 635842ff286SAnthony Kim "firmware update failed: %d\n", error); 636842ff286SAnthony Kim else 637842ff286SAnthony Kim dev_dbg(&ts->client->dev, "firmware updated successfully\n"); 638842ff286SAnthony Kim 639842ff286SAnthony Kim SW_RESET_IN_PGM(1000); 640842ff286SAnthony Kim 641842ff286SAnthony Kim error2 = hideep_load_dwz(ts); 642842ff286SAnthony Kim if (error2) 643842ff286SAnthony Kim dev_err(&ts->client->dev, 644842ff286SAnthony Kim "failed to load dwz after firmware update: %d\n", 645842ff286SAnthony Kim error2); 646842ff286SAnthony Kim 647842ff286SAnthony Kim return error ?: error2; 648842ff286SAnthony Kim } 649842ff286SAnthony Kim 650842ff286SAnthony Kim static int hideep_power_on(struct hideep_ts *ts) 651842ff286SAnthony Kim { 652842ff286SAnthony Kim int error = 0; 653842ff286SAnthony Kim 654842ff286SAnthony Kim error = regulator_enable(ts->vcc_vdd); 655842ff286SAnthony Kim if (error) 656842ff286SAnthony Kim dev_err(&ts->client->dev, 657842ff286SAnthony Kim "failed to enable 'vdd' regulator: %d", error); 658842ff286SAnthony Kim 659842ff286SAnthony Kim usleep_range(999, 1000); 660842ff286SAnthony Kim 661842ff286SAnthony Kim error = regulator_enable(ts->vcc_vid); 662842ff286SAnthony Kim if (error) 663842ff286SAnthony Kim dev_err(&ts->client->dev, 664842ff286SAnthony Kim "failed to enable 'vcc_vid' regulator: %d", 665842ff286SAnthony Kim error); 666842ff286SAnthony Kim 667842ff286SAnthony Kim msleep(30); 668842ff286SAnthony Kim 669842ff286SAnthony Kim if (ts->reset_gpio) { 670842ff286SAnthony Kim gpiod_set_value_cansleep(ts->reset_gpio, 0); 671842ff286SAnthony Kim } else { 672842ff286SAnthony Kim error = regmap_write(ts->reg, HIDEEP_RESET_CMD, 0x01); 673842ff286SAnthony Kim if (error) 674842ff286SAnthony Kim dev_err(&ts->client->dev, 675842ff286SAnthony Kim "failed to send 'reset' command: %d\n", error); 676842ff286SAnthony Kim } 677842ff286SAnthony Kim 678842ff286SAnthony Kim msleep(50); 679842ff286SAnthony Kim 680842ff286SAnthony Kim return error; 681842ff286SAnthony Kim } 682842ff286SAnthony Kim 683842ff286SAnthony Kim static void hideep_power_off(void *data) 684842ff286SAnthony Kim { 685842ff286SAnthony Kim struct hideep_ts *ts = data; 686842ff286SAnthony Kim 687842ff286SAnthony Kim if (ts->reset_gpio) 688842ff286SAnthony Kim gpiod_set_value(ts->reset_gpio, 1); 689842ff286SAnthony Kim 690842ff286SAnthony Kim regulator_disable(ts->vcc_vid); 691842ff286SAnthony Kim regulator_disable(ts->vcc_vdd); 692842ff286SAnthony Kim } 693842ff286SAnthony Kim 694842ff286SAnthony Kim #define __GET_MT_TOOL_TYPE(type) ((type) == 0x01 ? MT_TOOL_FINGER : MT_TOOL_PEN) 695842ff286SAnthony Kim 696842ff286SAnthony Kim static void hideep_report_slot(struct input_dev *input, 697842ff286SAnthony Kim const struct hideep_event *event) 698842ff286SAnthony Kim { 699842ff286SAnthony Kim input_mt_slot(input, event->index & 0x0f); 700842ff286SAnthony Kim input_mt_report_slot_state(input, 701842ff286SAnthony Kim __GET_MT_TOOL_TYPE(event->type), 702842ff286SAnthony Kim !(event->flag & HIDEEP_MT_RELEASED)); 703842ff286SAnthony Kim if (!(event->flag & HIDEEP_MT_RELEASED)) { 704842ff286SAnthony Kim input_report_abs(input, ABS_MT_POSITION_X, 705842ff286SAnthony Kim le16_to_cpup(&event->x)); 706842ff286SAnthony Kim input_report_abs(input, ABS_MT_POSITION_Y, 707842ff286SAnthony Kim le16_to_cpup(&event->y)); 708842ff286SAnthony Kim input_report_abs(input, ABS_MT_PRESSURE, 709842ff286SAnthony Kim le16_to_cpup(&event->z)); 710842ff286SAnthony Kim input_report_abs(input, ABS_MT_TOUCH_MAJOR, event->w); 711842ff286SAnthony Kim } 712842ff286SAnthony Kim } 713842ff286SAnthony Kim 714842ff286SAnthony Kim static void hideep_parse_and_report(struct hideep_ts *ts) 715842ff286SAnthony Kim { 716842ff286SAnthony Kim const struct hideep_event *events = 717842ff286SAnthony Kim (void *)&ts->xfer_buf[HIDEEP_TOUCH_EVENT_INDEX]; 718842ff286SAnthony Kim const u8 *keys = &ts->xfer_buf[HIDEEP_KEY_EVENT_INDEX]; 719842ff286SAnthony Kim int touch_count = ts->xfer_buf[0]; 720842ff286SAnthony Kim int key_count = ts->xfer_buf[1] & 0x0f; 721842ff286SAnthony Kim int lpm_count = ts->xfer_buf[1] & 0xf0; 722842ff286SAnthony Kim int i; 723842ff286SAnthony Kim 724842ff286SAnthony Kim /* get touch event count */ 725842ff286SAnthony Kim dev_dbg(&ts->client->dev, "mt = %d, key = %d, lpm = %02x", 726842ff286SAnthony Kim touch_count, key_count, lpm_count); 727842ff286SAnthony Kim 728842ff286SAnthony Kim touch_count = min(touch_count, HIDEEP_MT_MAX); 729842ff286SAnthony Kim for (i = 0; i < touch_count; i++) 730842ff286SAnthony Kim hideep_report_slot(ts->input_dev, events + i); 731842ff286SAnthony Kim 732842ff286SAnthony Kim key_count = min(key_count, HIDEEP_KEY_MAX); 733842ff286SAnthony Kim for (i = 0; i < key_count; i++) { 734842ff286SAnthony Kim u8 key_data = keys[i * 2]; 735842ff286SAnthony Kim 736842ff286SAnthony Kim input_report_key(ts->input_dev, 737842ff286SAnthony Kim ts->key_codes[key_data & HIDEEP_KEY_IDX_MASK], 738842ff286SAnthony Kim key_data & HIDEEP_KEY_PRESSED_MASK); 739842ff286SAnthony Kim } 740842ff286SAnthony Kim 741842ff286SAnthony Kim input_mt_sync_frame(ts->input_dev); 742842ff286SAnthony Kim input_sync(ts->input_dev); 743842ff286SAnthony Kim } 744842ff286SAnthony Kim 745842ff286SAnthony Kim static irqreturn_t hideep_irq(int irq, void *handle) 746842ff286SAnthony Kim { 747842ff286SAnthony Kim struct hideep_ts *ts = handle; 748842ff286SAnthony Kim int error; 749842ff286SAnthony Kim 750842ff286SAnthony Kim BUILD_BUG_ON(HIDEEP_MAX_EVENT > HIDEEP_XFER_BUF_SIZE); 751842ff286SAnthony Kim 752842ff286SAnthony Kim error = regmap_bulk_read(ts->reg, HIDEEP_EVENT_ADDR, 753842ff286SAnthony Kim ts->xfer_buf, HIDEEP_MAX_EVENT / 2); 754842ff286SAnthony Kim if (error) { 755842ff286SAnthony Kim dev_err(&ts->client->dev, "failed to read events: %d\n", error); 756842ff286SAnthony Kim goto out; 757842ff286SAnthony Kim } 758842ff286SAnthony Kim 759842ff286SAnthony Kim hideep_parse_and_report(ts); 760842ff286SAnthony Kim 761842ff286SAnthony Kim out: 762842ff286SAnthony Kim return IRQ_HANDLED; 763842ff286SAnthony Kim } 764842ff286SAnthony Kim 765842ff286SAnthony Kim static int hideep_get_axis_info(struct hideep_ts *ts) 766842ff286SAnthony Kim { 767842ff286SAnthony Kim __le16 val[2]; 768842ff286SAnthony Kim int error; 769842ff286SAnthony Kim 770842ff286SAnthony Kim error = regmap_bulk_read(ts->reg, 0x28, val, ARRAY_SIZE(val)); 771842ff286SAnthony Kim if (error) 772842ff286SAnthony Kim return error; 773842ff286SAnthony Kim 774842ff286SAnthony Kim ts->prop.max_x = le16_to_cpup(val); 775842ff286SAnthony Kim ts->prop.max_y = le16_to_cpup(val + 1); 776842ff286SAnthony Kim 777842ff286SAnthony Kim dev_dbg(&ts->client->dev, "X: %d, Y: %d", 778842ff286SAnthony Kim ts->prop.max_x, ts->prop.max_y); 779842ff286SAnthony Kim 780842ff286SAnthony Kim return 0; 781842ff286SAnthony Kim } 782842ff286SAnthony Kim 783842ff286SAnthony Kim static int hideep_init_input(struct hideep_ts *ts) 784842ff286SAnthony Kim { 785842ff286SAnthony Kim struct device *dev = &ts->client->dev; 786842ff286SAnthony Kim int i; 787842ff286SAnthony Kim int error; 788842ff286SAnthony Kim 789842ff286SAnthony Kim ts->input_dev = devm_input_allocate_device(dev); 790842ff286SAnthony Kim if (!ts->input_dev) { 791842ff286SAnthony Kim dev_err(dev, "failed to allocate input device\n"); 792842ff286SAnthony Kim return -ENOMEM; 793842ff286SAnthony Kim } 794842ff286SAnthony Kim 795842ff286SAnthony Kim ts->input_dev->name = HIDEEP_TS_NAME; 796842ff286SAnthony Kim ts->input_dev->id.bustype = BUS_I2C; 797842ff286SAnthony Kim input_set_drvdata(ts->input_dev, ts); 798842ff286SAnthony Kim 799842ff286SAnthony Kim input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X); 800842ff286SAnthony Kim input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y); 801842ff286SAnthony Kim input_set_abs_params(ts->input_dev, ABS_MT_PRESSURE, 0, 65535, 0, 0); 802842ff286SAnthony Kim input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 803842ff286SAnthony Kim input_set_abs_params(ts->input_dev, ABS_MT_TOOL_TYPE, 804842ff286SAnthony Kim 0, MT_TOOL_MAX, 0, 0); 805842ff286SAnthony Kim touchscreen_parse_properties(ts->input_dev, true, &ts->prop); 806842ff286SAnthony Kim 807842ff286SAnthony Kim if (ts->prop.max_x == 0 || ts->prop.max_y == 0) { 808842ff286SAnthony Kim error = hideep_get_axis_info(ts); 809842ff286SAnthony Kim if (error) 810842ff286SAnthony Kim return error; 811842ff286SAnthony Kim } 812842ff286SAnthony Kim 813842ff286SAnthony Kim error = input_mt_init_slots(ts->input_dev, HIDEEP_MT_MAX, 814842ff286SAnthony Kim INPUT_MT_DIRECT); 815842ff286SAnthony Kim if (error) 816842ff286SAnthony Kim return error; 817842ff286SAnthony Kim 818842ff286SAnthony Kim ts->key_num = device_property_read_u32_array(dev, "linux,keycodes", 819842ff286SAnthony Kim NULL, 0); 820842ff286SAnthony Kim if (ts->key_num > HIDEEP_KEY_MAX) { 821842ff286SAnthony Kim dev_err(dev, "too many keys defined: %d\n", 822842ff286SAnthony Kim ts->key_num); 823842ff286SAnthony Kim return -EINVAL; 824842ff286SAnthony Kim } 825842ff286SAnthony Kim 826842ff286SAnthony Kim if (ts->key_num <= 0) { 827842ff286SAnthony Kim dev_dbg(dev, 828842ff286SAnthony Kim "missing or malformed 'linux,keycodes' property\n"); 829842ff286SAnthony Kim } else { 830842ff286SAnthony Kim error = device_property_read_u32_array(dev, "linux,keycodes", 831842ff286SAnthony Kim ts->key_codes, 832842ff286SAnthony Kim ts->key_num); 833842ff286SAnthony Kim if (error) { 834842ff286SAnthony Kim dev_dbg(dev, "failed to read keymap: %d", error); 835842ff286SAnthony Kim return error; 836842ff286SAnthony Kim } 837842ff286SAnthony Kim 838842ff286SAnthony Kim if (ts->key_num) { 839842ff286SAnthony Kim ts->input_dev->keycode = ts->key_codes; 840842ff286SAnthony Kim ts->input_dev->keycodesize = sizeof(ts->key_codes[0]); 841842ff286SAnthony Kim ts->input_dev->keycodemax = ts->key_num; 842842ff286SAnthony Kim 843842ff286SAnthony Kim for (i = 0; i < ts->key_num; i++) 844842ff286SAnthony Kim input_set_capability(ts->input_dev, EV_KEY, 845842ff286SAnthony Kim ts->key_codes[i]); 846842ff286SAnthony Kim } 847842ff286SAnthony Kim } 848842ff286SAnthony Kim 849842ff286SAnthony Kim error = input_register_device(ts->input_dev); 850842ff286SAnthony Kim if (error) { 851842ff286SAnthony Kim dev_err(dev, "failed to register input device: %d", error); 852842ff286SAnthony Kim return error; 853842ff286SAnthony Kim } 854842ff286SAnthony Kim 855842ff286SAnthony Kim return 0; 856842ff286SAnthony Kim } 857842ff286SAnthony Kim 858842ff286SAnthony Kim static ssize_t hideep_update_fw(struct device *dev, 859842ff286SAnthony Kim struct device_attribute *attr, 860842ff286SAnthony Kim const char *buf, size_t count) 861842ff286SAnthony Kim { 862842ff286SAnthony Kim struct i2c_client *client = to_i2c_client(dev); 863842ff286SAnthony Kim struct hideep_ts *ts = i2c_get_clientdata(client); 864842ff286SAnthony Kim const struct firmware *fw_entry; 865842ff286SAnthony Kim char *fw_name; 866842ff286SAnthony Kim int mode; 867842ff286SAnthony Kim int error; 868842ff286SAnthony Kim 869842ff286SAnthony Kim error = kstrtoint(buf, 0, &mode); 870842ff286SAnthony Kim if (error) 871842ff286SAnthony Kim return error; 872842ff286SAnthony Kim 873842ff286SAnthony Kim fw_name = kasprintf(GFP_KERNEL, "hideep_ts_%04x.bin", 874842ff286SAnthony Kim be16_to_cpu(ts->dwz_info.product_id)); 875842ff286SAnthony Kim if (!fw_name) 876842ff286SAnthony Kim return -ENOMEM; 877842ff286SAnthony Kim 878842ff286SAnthony Kim error = request_firmware(&fw_entry, fw_name, dev); 879842ff286SAnthony Kim if (error) { 880842ff286SAnthony Kim dev_err(dev, "failed to request firmware %s: %d", 881842ff286SAnthony Kim fw_name, error); 882842ff286SAnthony Kim goto out_free_fw_name; 883842ff286SAnthony Kim } 884842ff286SAnthony Kim 885842ff286SAnthony Kim if (fw_entry->size % sizeof(__be32)) { 886842ff286SAnthony Kim dev_err(dev, "invalid firmware size %zu\n", fw_entry->size); 887842ff286SAnthony Kim error = -EINVAL; 888842ff286SAnthony Kim goto out_release_fw; 889842ff286SAnthony Kim } 890842ff286SAnthony Kim 891842ff286SAnthony Kim if (fw_entry->size > ts->fw_size) { 892842ff286SAnthony Kim dev_err(dev, "fw size (%zu) is too big (memory size %d)\n", 893842ff286SAnthony Kim fw_entry->size, ts->fw_size); 894842ff286SAnthony Kim error = -EFBIG; 895842ff286SAnthony Kim goto out_release_fw; 896842ff286SAnthony Kim } 897842ff286SAnthony Kim 898842ff286SAnthony Kim mutex_lock(&ts->dev_mutex); 899842ff286SAnthony Kim disable_irq(client->irq); 900842ff286SAnthony Kim 901842ff286SAnthony Kim error = hideep_update_firmware(ts, (const __be32 *)fw_entry->data, 902842ff286SAnthony Kim fw_entry->size); 903842ff286SAnthony Kim 904842ff286SAnthony Kim enable_irq(client->irq); 905842ff286SAnthony Kim mutex_unlock(&ts->dev_mutex); 906842ff286SAnthony Kim 907842ff286SAnthony Kim out_release_fw: 908842ff286SAnthony Kim release_firmware(fw_entry); 909842ff286SAnthony Kim out_free_fw_name: 910842ff286SAnthony Kim kfree(fw_name); 911842ff286SAnthony Kim 912842ff286SAnthony Kim return error ?: count; 913842ff286SAnthony Kim } 914842ff286SAnthony Kim 915842ff286SAnthony Kim static ssize_t hideep_fw_version_show(struct device *dev, 916842ff286SAnthony Kim struct device_attribute *attr, char *buf) 917842ff286SAnthony Kim { 918842ff286SAnthony Kim struct i2c_client *client = to_i2c_client(dev); 919842ff286SAnthony Kim struct hideep_ts *ts = i2c_get_clientdata(client); 920842ff286SAnthony Kim ssize_t len; 921842ff286SAnthony Kim 922842ff286SAnthony Kim mutex_lock(&ts->dev_mutex); 923842ff286SAnthony Kim len = scnprintf(buf, PAGE_SIZE, "%04x\n", 924842ff286SAnthony Kim be16_to_cpu(ts->dwz_info.release_ver)); 925842ff286SAnthony Kim mutex_unlock(&ts->dev_mutex); 926842ff286SAnthony Kim 927842ff286SAnthony Kim return len; 928842ff286SAnthony Kim } 929842ff286SAnthony Kim 930842ff286SAnthony Kim static ssize_t hideep_product_id_show(struct device *dev, 931842ff286SAnthony Kim struct device_attribute *attr, char *buf) 932842ff286SAnthony Kim { 933842ff286SAnthony Kim struct i2c_client *client = to_i2c_client(dev); 934842ff286SAnthony Kim struct hideep_ts *ts = i2c_get_clientdata(client); 935842ff286SAnthony Kim ssize_t len; 936842ff286SAnthony Kim 937842ff286SAnthony Kim mutex_lock(&ts->dev_mutex); 938842ff286SAnthony Kim len = scnprintf(buf, PAGE_SIZE, "%04x\n", 939842ff286SAnthony Kim be16_to_cpu(ts->dwz_info.product_id)); 940842ff286SAnthony Kim mutex_unlock(&ts->dev_mutex); 941842ff286SAnthony Kim 942842ff286SAnthony Kim return len; 943842ff286SAnthony Kim } 944842ff286SAnthony Kim 945842ff286SAnthony Kim static DEVICE_ATTR(version, 0664, hideep_fw_version_show, NULL); 946842ff286SAnthony Kim static DEVICE_ATTR(product_id, 0664, hideep_product_id_show, NULL); 947842ff286SAnthony Kim static DEVICE_ATTR(update_fw, 0664, NULL, hideep_update_fw); 948842ff286SAnthony Kim 949842ff286SAnthony Kim static struct attribute *hideep_ts_sysfs_entries[] = { 950842ff286SAnthony Kim &dev_attr_version.attr, 951842ff286SAnthony Kim &dev_attr_product_id.attr, 952842ff286SAnthony Kim &dev_attr_update_fw.attr, 953842ff286SAnthony Kim NULL, 954842ff286SAnthony Kim }; 955842ff286SAnthony Kim 956842ff286SAnthony Kim static const struct attribute_group hideep_ts_attr_group = { 957842ff286SAnthony Kim .attrs = hideep_ts_sysfs_entries, 958842ff286SAnthony Kim }; 959842ff286SAnthony Kim 960842ff286SAnthony Kim static int __maybe_unused hideep_suspend(struct device *dev) 961842ff286SAnthony Kim { 962842ff286SAnthony Kim struct i2c_client *client = to_i2c_client(dev); 963842ff286SAnthony Kim struct hideep_ts *ts = i2c_get_clientdata(client); 964842ff286SAnthony Kim 965842ff286SAnthony Kim disable_irq(client->irq); 966842ff286SAnthony Kim hideep_power_off(ts); 967842ff286SAnthony Kim 968842ff286SAnthony Kim return 0; 969842ff286SAnthony Kim } 970842ff286SAnthony Kim 971842ff286SAnthony Kim static int __maybe_unused hideep_resume(struct device *dev) 972842ff286SAnthony Kim { 973842ff286SAnthony Kim struct i2c_client *client = to_i2c_client(dev); 974842ff286SAnthony Kim struct hideep_ts *ts = i2c_get_clientdata(client); 975842ff286SAnthony Kim int error; 976842ff286SAnthony Kim 977842ff286SAnthony Kim error = hideep_power_on(ts); 978842ff286SAnthony Kim if (error) { 979842ff286SAnthony Kim dev_err(&client->dev, "power on failed"); 980842ff286SAnthony Kim return error; 981842ff286SAnthony Kim } 982842ff286SAnthony Kim 983842ff286SAnthony Kim enable_irq(client->irq); 984842ff286SAnthony Kim 985842ff286SAnthony Kim return 0; 986842ff286SAnthony Kim } 987842ff286SAnthony Kim 988842ff286SAnthony Kim static SIMPLE_DEV_PM_OPS(hideep_pm_ops, hideep_suspend, hideep_resume); 989842ff286SAnthony Kim 990842ff286SAnthony Kim static const struct regmap_config hideep_regmap_config = { 991842ff286SAnthony Kim .reg_bits = 16, 992842ff286SAnthony Kim .reg_format_endian = REGMAP_ENDIAN_LITTLE, 993842ff286SAnthony Kim .val_bits = 16, 994842ff286SAnthony Kim .val_format_endian = REGMAP_ENDIAN_LITTLE, 995842ff286SAnthony Kim .max_register = 0xffff, 996842ff286SAnthony Kim }; 997842ff286SAnthony Kim 998842ff286SAnthony Kim static int hideep_probe(struct i2c_client *client, 999842ff286SAnthony Kim const struct i2c_device_id *id) 1000842ff286SAnthony Kim { 1001842ff286SAnthony Kim struct hideep_ts *ts; 1002842ff286SAnthony Kim int error; 1003842ff286SAnthony Kim 1004842ff286SAnthony Kim /* check i2c bus */ 1005842ff286SAnthony Kim if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 1006842ff286SAnthony Kim dev_err(&client->dev, "check i2c device error"); 1007842ff286SAnthony Kim return -ENODEV; 1008842ff286SAnthony Kim } 1009842ff286SAnthony Kim 1010842ff286SAnthony Kim if (client->irq <= 0) { 1011842ff286SAnthony Kim dev_err(&client->dev, "missing irq: %d\n", client->irq); 1012842ff286SAnthony Kim return -EINVAL; 1013842ff286SAnthony Kim } 1014842ff286SAnthony Kim 1015842ff286SAnthony Kim ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); 1016842ff286SAnthony Kim if (!ts) 1017842ff286SAnthony Kim return -ENOMEM; 1018842ff286SAnthony Kim 1019842ff286SAnthony Kim ts->client = client; 1020842ff286SAnthony Kim i2c_set_clientdata(client, ts); 1021842ff286SAnthony Kim mutex_init(&ts->dev_mutex); 1022842ff286SAnthony Kim 1023842ff286SAnthony Kim ts->reg = devm_regmap_init_i2c(client, &hideep_regmap_config); 1024842ff286SAnthony Kim if (IS_ERR(ts->reg)) { 1025842ff286SAnthony Kim error = PTR_ERR(ts->reg); 1026842ff286SAnthony Kim dev_err(&client->dev, 1027842ff286SAnthony Kim "failed to initialize regmap: %d\n", error); 1028842ff286SAnthony Kim return error; 1029842ff286SAnthony Kim } 1030842ff286SAnthony Kim 1031842ff286SAnthony Kim ts->vcc_vdd = devm_regulator_get(&client->dev, "vdd"); 1032842ff286SAnthony Kim if (IS_ERR(ts->vcc_vdd)) 1033842ff286SAnthony Kim return PTR_ERR(ts->vcc_vdd); 1034842ff286SAnthony Kim 1035842ff286SAnthony Kim ts->vcc_vid = devm_regulator_get(&client->dev, "vid"); 1036842ff286SAnthony Kim if (IS_ERR(ts->vcc_vid)) 1037842ff286SAnthony Kim return PTR_ERR(ts->vcc_vid); 1038842ff286SAnthony Kim 1039842ff286SAnthony Kim ts->reset_gpio = devm_gpiod_get_optional(&client->dev, 1040842ff286SAnthony Kim "reset", GPIOD_OUT_HIGH); 1041842ff286SAnthony Kim if (IS_ERR(ts->reset_gpio)) 1042842ff286SAnthony Kim return PTR_ERR(ts->reset_gpio); 1043842ff286SAnthony Kim 1044842ff286SAnthony Kim error = hideep_power_on(ts); 1045842ff286SAnthony Kim if (error) { 1046842ff286SAnthony Kim dev_err(&client->dev, "power on failed: %d\n", error); 1047842ff286SAnthony Kim return error; 1048842ff286SAnthony Kim } 1049842ff286SAnthony Kim 1050842ff286SAnthony Kim error = devm_add_action_or_reset(&client->dev, hideep_power_off, ts); 1051842ff286SAnthony Kim if (error) 1052842ff286SAnthony Kim return error; 1053842ff286SAnthony Kim 1054842ff286SAnthony Kim error = hideep_load_dwz(ts); 1055842ff286SAnthony Kim if (error) { 1056842ff286SAnthony Kim dev_err(&client->dev, "failed to load dwz: %d", error); 1057842ff286SAnthony Kim return error; 1058842ff286SAnthony Kim } 1059842ff286SAnthony Kim 1060842ff286SAnthony Kim error = hideep_init_input(ts); 1061842ff286SAnthony Kim if (error) 1062842ff286SAnthony Kim return error; 1063842ff286SAnthony Kim 1064842ff286SAnthony Kim error = devm_request_threaded_irq(&client->dev, client->irq, 1065842ff286SAnthony Kim NULL, hideep_irq, IRQF_ONESHOT, 1066842ff286SAnthony Kim client->name, ts); 1067842ff286SAnthony Kim if (error) { 1068842ff286SAnthony Kim dev_err(&client->dev, "failed to request irq %d: %d\n", 1069842ff286SAnthony Kim client->irq, error); 1070842ff286SAnthony Kim return error; 1071842ff286SAnthony Kim } 1072842ff286SAnthony Kim 1073842ff286SAnthony Kim error = devm_device_add_group(&client->dev, &hideep_ts_attr_group); 1074842ff286SAnthony Kim if (error) { 1075842ff286SAnthony Kim dev_err(&client->dev, 1076842ff286SAnthony Kim "failed to add sysfs attributes: %d\n", error); 1077842ff286SAnthony Kim return error; 1078842ff286SAnthony Kim } 1079842ff286SAnthony Kim 1080842ff286SAnthony Kim return 0; 1081842ff286SAnthony Kim } 1082842ff286SAnthony Kim 1083842ff286SAnthony Kim static const struct i2c_device_id hideep_i2c_id[] = { 1084842ff286SAnthony Kim { HIDEEP_I2C_NAME, 0 }, 1085842ff286SAnthony Kim { } 1086842ff286SAnthony Kim }; 1087842ff286SAnthony Kim MODULE_DEVICE_TABLE(i2c, hideep_i2c_id); 1088842ff286SAnthony Kim 1089842ff286SAnthony Kim #ifdef CONFIG_ACPI 1090842ff286SAnthony Kim static const struct acpi_device_id hideep_acpi_id[] = { 1091842ff286SAnthony Kim { "HIDP0001", 0 }, 1092842ff286SAnthony Kim { } 1093842ff286SAnthony Kim }; 1094842ff286SAnthony Kim MODULE_DEVICE_TABLE(acpi, hideep_acpi_id); 1095842ff286SAnthony Kim #endif 1096842ff286SAnthony Kim 1097842ff286SAnthony Kim #ifdef CONFIG_OF 1098842ff286SAnthony Kim static const struct of_device_id hideep_match_table[] = { 1099842ff286SAnthony Kim { .compatible = "hideep,hideep-ts" }, 1100842ff286SAnthony Kim { } 1101842ff286SAnthony Kim }; 1102842ff286SAnthony Kim MODULE_DEVICE_TABLE(of, hideep_match_table); 1103842ff286SAnthony Kim #endif 1104842ff286SAnthony Kim 1105842ff286SAnthony Kim static struct i2c_driver hideep_driver = { 1106842ff286SAnthony Kim .driver = { 1107842ff286SAnthony Kim .name = HIDEEP_I2C_NAME, 1108842ff286SAnthony Kim .of_match_table = of_match_ptr(hideep_match_table), 1109842ff286SAnthony Kim .acpi_match_table = ACPI_PTR(hideep_acpi_id), 1110842ff286SAnthony Kim .pm = &hideep_pm_ops, 1111842ff286SAnthony Kim }, 1112842ff286SAnthony Kim .id_table = hideep_i2c_id, 1113842ff286SAnthony Kim .probe = hideep_probe, 1114842ff286SAnthony Kim }; 1115842ff286SAnthony Kim 1116842ff286SAnthony Kim module_i2c_driver(hideep_driver); 1117842ff286SAnthony Kim 1118842ff286SAnthony Kim MODULE_DESCRIPTION("Driver for HiDeep Touchscreen Controller"); 1119842ff286SAnthony Kim MODULE_AUTHOR("anthony.kim@hideep.com"); 1120842ff286SAnthony Kim MODULE_LICENSE("GPL v2"); 1121