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