109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2*c6ac8f0bSMarek Vasut #include <linux/crc-ccitt.h> 371f8e38aSDmitry Torokhov #include <linux/delay.h> 471f8e38aSDmitry Torokhov #include <linux/gpio/consumer.h> 55c6a7a62SOlivier Sobrie #include <linux/i2c.h> 6*c6ac8f0bSMarek Vasut #include <linux/ihex.h> 75c6a7a62SOlivier Sobrie #include <linux/input.h> 85c6a7a62SOlivier Sobrie #include <linux/input/mt.h> 9f67cc3e9SMarek Vasut #include <linux/input/touchscreen.h> 1071f8e38aSDmitry Torokhov #include <linux/interrupt.h> 1171f8e38aSDmitry Torokhov #include <linux/module.h> 1249588917SMarek Vasut #include <linux/of_device.h> 13b32fbeaeSSven Van Asbroeck #include <linux/sizes.h> 1471f8e38aSDmitry Torokhov #include <linux/slab.h> 15e3559442SMarek Vasut #include <asm/unaligned.h> 165c6a7a62SOlivier Sobrie 1771f8e38aSDmitry Torokhov #define ILI2XXX_POLL_PERIOD 20 185c6a7a62SOlivier Sobrie 19ef536abdSDmitry Torokhov #define ILI210X_DATA_SIZE 64 20ef536abdSDmitry Torokhov #define ILI211X_DATA_SIZE 43 21ef536abdSDmitry Torokhov #define ILI251X_DATA_SIZE1 31 22ef536abdSDmitry Torokhov #define ILI251X_DATA_SIZE2 20 23ef536abdSDmitry Torokhov 245c6a7a62SOlivier Sobrie /* Touchscreen commands */ 255c6a7a62SOlivier Sobrie #define REG_TOUCHDATA 0x10 265c6a7a62SOlivier Sobrie #define REG_PANEL_INFO 0x20 2770a7681dSMarek Vasut #define REG_FIRMWARE_VERSION 0x40 2870a7681dSMarek Vasut #define REG_PROTOCOL_VERSION 0x42 2970a7681dSMarek Vasut #define REG_KERNEL_VERSION 0x61 30*c6ac8f0bSMarek Vasut #define REG_IC_BUSY 0x80 31*c6ac8f0bSMarek Vasut #define REG_IC_BUSY_NOT_BUSY 0x50 3270a7681dSMarek Vasut #define REG_GET_MODE 0xc0 3370a7681dSMarek Vasut #define REG_GET_MODE_AP 0x5a 3470a7681dSMarek Vasut #define REG_GET_MODE_BL 0x55 35*c6ac8f0bSMarek Vasut #define REG_SET_MODE_AP 0xc1 36*c6ac8f0bSMarek Vasut #define REG_SET_MODE_BL 0xc2 37*c6ac8f0bSMarek Vasut #define REG_WRITE_DATA 0xc3 38*c6ac8f0bSMarek Vasut #define REG_WRITE_ENABLE 0xc4 39*c6ac8f0bSMarek Vasut #define REG_READ_DATA_CRC 0xc7 405c6a7a62SOlivier Sobrie #define REG_CALIBRATE 0xcc 415c6a7a62SOlivier Sobrie 42*c6ac8f0bSMarek Vasut #define ILI251X_FW_FILENAME "ilitek/ili251x.bin" 43*c6ac8f0bSMarek Vasut 44ef536abdSDmitry Torokhov struct ili2xxx_chip { 45ef536abdSDmitry Torokhov int (*read_reg)(struct i2c_client *client, u8 reg, 46ef536abdSDmitry Torokhov void *buf, size_t len); 47ef536abdSDmitry Torokhov int (*get_touch_data)(struct i2c_client *client, u8 *data); 48ef536abdSDmitry Torokhov bool (*parse_touch_data)(const u8 *data, unsigned int finger, 4960159e9eSMarek Vasut unsigned int *x, unsigned int *y, 5060159e9eSMarek Vasut unsigned int *z); 51ef536abdSDmitry Torokhov bool (*continue_polling)(const u8 *data, bool touch); 52ef536abdSDmitry Torokhov unsigned int max_touches; 53b32fbeaeSSven Van Asbroeck unsigned int resolution; 54cc12ba18SSven Van Asbroeck bool has_calibrate_reg; 55235300edSMarek Vasut bool has_firmware_proto; 5660159e9eSMarek Vasut bool has_pressure_reg; 5749588917SMarek Vasut }; 5849588917SMarek Vasut 595c6a7a62SOlivier Sobrie struct ili210x { 605c6a7a62SOlivier Sobrie struct i2c_client *client; 615c6a7a62SOlivier Sobrie struct input_dev *input; 62201f3c80SMarek Vasut struct gpio_desc *reset_gpio; 63f67cc3e9SMarek Vasut struct touchscreen_properties prop; 64ef536abdSDmitry Torokhov const struct ili2xxx_chip *chip; 6570a7681dSMarek Vasut u8 version_firmware[8]; 6670a7681dSMarek Vasut u8 version_kernel[5]; 6770a7681dSMarek Vasut u8 version_proto[2]; 6870a7681dSMarek Vasut u8 ic_mode[2]; 6971f8e38aSDmitry Torokhov bool stop; 705c6a7a62SOlivier Sobrie }; 715c6a7a62SOlivier Sobrie 72ef536abdSDmitry Torokhov static int ili210x_read_reg(struct i2c_client *client, 73ef536abdSDmitry Torokhov u8 reg, void *buf, size_t len) 745c6a7a62SOlivier Sobrie { 75ef536abdSDmitry Torokhov struct i2c_msg msg[] = { 765c6a7a62SOlivier Sobrie { 775c6a7a62SOlivier Sobrie .addr = client->addr, 785c6a7a62SOlivier Sobrie .flags = 0, 795c6a7a62SOlivier Sobrie .len = 1, 805c6a7a62SOlivier Sobrie .buf = ®, 815c6a7a62SOlivier Sobrie }, 825c6a7a62SOlivier Sobrie { 835c6a7a62SOlivier Sobrie .addr = client->addr, 845c6a7a62SOlivier Sobrie .flags = I2C_M_RD, 855c6a7a62SOlivier Sobrie .len = len, 865c6a7a62SOlivier Sobrie .buf = buf, 875c6a7a62SOlivier Sobrie } 885c6a7a62SOlivier Sobrie }; 89ef536abdSDmitry Torokhov int error, ret; 905c6a7a62SOlivier Sobrie 91ef536abdSDmitry Torokhov ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); 92ef536abdSDmitry Torokhov if (ret != ARRAY_SIZE(msg)) { 93ef536abdSDmitry Torokhov error = ret < 0 ? ret : -EIO; 94ef536abdSDmitry Torokhov dev_err(&client->dev, "%s failed: %d\n", __func__, error); 95ef536abdSDmitry Torokhov return error; 9649588917SMarek Vasut } 9749588917SMarek Vasut 9849588917SMarek Vasut return 0; 9949588917SMarek Vasut } 10049588917SMarek Vasut 101ef536abdSDmitry Torokhov static int ili210x_read_touch_data(struct i2c_client *client, u8 *data) 10249588917SMarek Vasut { 103ef536abdSDmitry Torokhov return ili210x_read_reg(client, REG_TOUCHDATA, 104ef536abdSDmitry Torokhov data, ILI210X_DATA_SIZE); 10549588917SMarek Vasut } 1065c6a7a62SOlivier Sobrie 107ef536abdSDmitry Torokhov static bool ili210x_touchdata_to_coords(const u8 *touchdata, 108e3559442SMarek Vasut unsigned int finger, 10960159e9eSMarek Vasut unsigned int *x, unsigned int *y, 11060159e9eSMarek Vasut unsigned int *z) 1115c6a7a62SOlivier Sobrie { 112ac05a8a9SHansem Ro if (!(touchdata[0] & BIT(finger))) 113e3559442SMarek Vasut return false; 114e3559442SMarek Vasut 115e3559442SMarek Vasut *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0); 116e3559442SMarek Vasut *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2); 117e3559442SMarek Vasut 118e3559442SMarek Vasut return true; 119e3559442SMarek Vasut } 120e3559442SMarek Vasut 121ef536abdSDmitry Torokhov static bool ili210x_check_continue_polling(const u8 *data, bool touch) 122ef536abdSDmitry Torokhov { 123ef536abdSDmitry Torokhov return data[0] & 0xf3; 124ef536abdSDmitry Torokhov } 125ef536abdSDmitry Torokhov 126ef536abdSDmitry Torokhov static const struct ili2xxx_chip ili210x_chip = { 127ef536abdSDmitry Torokhov .read_reg = ili210x_read_reg, 128ef536abdSDmitry Torokhov .get_touch_data = ili210x_read_touch_data, 129ef536abdSDmitry Torokhov .parse_touch_data = ili210x_touchdata_to_coords, 130ef536abdSDmitry Torokhov .continue_polling = ili210x_check_continue_polling, 131ef536abdSDmitry Torokhov .max_touches = 2, 132cc12ba18SSven Van Asbroeck .has_calibrate_reg = true, 133ef536abdSDmitry Torokhov }; 134ef536abdSDmitry Torokhov 135ef536abdSDmitry Torokhov static int ili211x_read_touch_data(struct i2c_client *client, u8 *data) 136ef536abdSDmitry Torokhov { 137ef536abdSDmitry Torokhov s16 sum = 0; 138ef536abdSDmitry Torokhov int error; 139ef536abdSDmitry Torokhov int ret; 140ef536abdSDmitry Torokhov int i; 141ef536abdSDmitry Torokhov 142ef536abdSDmitry Torokhov ret = i2c_master_recv(client, data, ILI211X_DATA_SIZE); 143ef536abdSDmitry Torokhov if (ret != ILI211X_DATA_SIZE) { 144ef536abdSDmitry Torokhov error = ret < 0 ? ret : -EIO; 145ef536abdSDmitry Torokhov dev_err(&client->dev, "%s failed: %d\n", __func__, error); 146ef536abdSDmitry Torokhov return error; 147ef536abdSDmitry Torokhov } 148ef536abdSDmitry Torokhov 149ef536abdSDmitry Torokhov /* This chip uses custom checksum at the end of data */ 150ef536abdSDmitry Torokhov for (i = 0; i < ILI211X_DATA_SIZE - 1; i++) 151ef536abdSDmitry Torokhov sum = (sum + data[i]) & 0xff; 152ef536abdSDmitry Torokhov 153ef536abdSDmitry Torokhov if ((-sum & 0xff) != data[ILI211X_DATA_SIZE - 1]) { 154ef536abdSDmitry Torokhov dev_err(&client->dev, 155ef536abdSDmitry Torokhov "CRC error (crc=0x%02x expected=0x%02x)\n", 156ef536abdSDmitry Torokhov sum, data[ILI211X_DATA_SIZE - 1]); 157ef536abdSDmitry Torokhov return -EIO; 158ef536abdSDmitry Torokhov } 159ef536abdSDmitry Torokhov 160ef536abdSDmitry Torokhov return 0; 161ef536abdSDmitry Torokhov } 162ef536abdSDmitry Torokhov 163ef536abdSDmitry Torokhov static bool ili211x_touchdata_to_coords(const u8 *touchdata, 164eb91ecc9SMarek Vasut unsigned int finger, 16560159e9eSMarek Vasut unsigned int *x, unsigned int *y, 16660159e9eSMarek Vasut unsigned int *z) 167eb91ecc9SMarek Vasut { 168eb91ecc9SMarek Vasut u32 data; 169eb91ecc9SMarek Vasut 170eb91ecc9SMarek Vasut data = get_unaligned_be32(touchdata + 1 + (finger * 4) + 0); 171eb91ecc9SMarek Vasut if (data == 0xffffffff) /* Finger up */ 172eb91ecc9SMarek Vasut return false; 173eb91ecc9SMarek Vasut 174eb91ecc9SMarek Vasut *x = ((touchdata[1 + (finger * 4) + 0] & 0xf0) << 4) | 175eb91ecc9SMarek Vasut touchdata[1 + (finger * 4) + 1]; 176eb91ecc9SMarek Vasut *y = ((touchdata[1 + (finger * 4) + 0] & 0x0f) << 8) | 177eb91ecc9SMarek Vasut touchdata[1 + (finger * 4) + 2]; 178eb91ecc9SMarek Vasut 179eb91ecc9SMarek Vasut return true; 180eb91ecc9SMarek Vasut } 181eb91ecc9SMarek Vasut 182ef536abdSDmitry Torokhov static bool ili211x_decline_polling(const u8 *data, bool touch) 183ef536abdSDmitry Torokhov { 184ef536abdSDmitry Torokhov return false; 185ef536abdSDmitry Torokhov } 186ef536abdSDmitry Torokhov 187ef536abdSDmitry Torokhov static const struct ili2xxx_chip ili211x_chip = { 188ef536abdSDmitry Torokhov .read_reg = ili210x_read_reg, 189ef536abdSDmitry Torokhov .get_touch_data = ili211x_read_touch_data, 190ef536abdSDmitry Torokhov .parse_touch_data = ili211x_touchdata_to_coords, 191ef536abdSDmitry Torokhov .continue_polling = ili211x_decline_polling, 192ef536abdSDmitry Torokhov .max_touches = 10, 193b32fbeaeSSven Van Asbroeck .resolution = 2048, 194ef536abdSDmitry Torokhov }; 195ef536abdSDmitry Torokhov 196d0c5e7d4SLuca Weiss static bool ili212x_touchdata_to_coords(const u8 *touchdata, 197d0c5e7d4SLuca Weiss unsigned int finger, 19860159e9eSMarek Vasut unsigned int *x, unsigned int *y, 19960159e9eSMarek Vasut unsigned int *z) 200d0c5e7d4SLuca Weiss { 201d0c5e7d4SLuca Weiss u16 val; 202d0c5e7d4SLuca Weiss 203d0c5e7d4SLuca Weiss val = get_unaligned_be16(touchdata + 3 + (finger * 5) + 0); 204d0c5e7d4SLuca Weiss if (!(val & BIT(15))) /* Touch indication */ 205d0c5e7d4SLuca Weiss return false; 206d0c5e7d4SLuca Weiss 207d0c5e7d4SLuca Weiss *x = val & 0x3fff; 208d0c5e7d4SLuca Weiss *y = get_unaligned_be16(touchdata + 3 + (finger * 5) + 2); 209d0c5e7d4SLuca Weiss 210d0c5e7d4SLuca Weiss return true; 211d0c5e7d4SLuca Weiss } 212d0c5e7d4SLuca Weiss 213d0c5e7d4SLuca Weiss static bool ili212x_check_continue_polling(const u8 *data, bool touch) 214d0c5e7d4SLuca Weiss { 215d0c5e7d4SLuca Weiss return touch; 216d0c5e7d4SLuca Weiss } 217d0c5e7d4SLuca Weiss 218d0c5e7d4SLuca Weiss static const struct ili2xxx_chip ili212x_chip = { 219d0c5e7d4SLuca Weiss .read_reg = ili210x_read_reg, 220d0c5e7d4SLuca Weiss .get_touch_data = ili210x_read_touch_data, 221d0c5e7d4SLuca Weiss .parse_touch_data = ili212x_touchdata_to_coords, 222d0c5e7d4SLuca Weiss .continue_polling = ili212x_check_continue_polling, 223d0c5e7d4SLuca Weiss .max_touches = 10, 224d0c5e7d4SLuca Weiss .has_calibrate_reg = true, 225d0c5e7d4SLuca Weiss }; 226d0c5e7d4SLuca Weiss 227ef536abdSDmitry Torokhov static int ili251x_read_reg(struct i2c_client *client, 228ef536abdSDmitry Torokhov u8 reg, void *buf, size_t len) 229ef536abdSDmitry Torokhov { 230ef536abdSDmitry Torokhov int error; 231ef536abdSDmitry Torokhov int ret; 232ef536abdSDmitry Torokhov 233ef536abdSDmitry Torokhov ret = i2c_master_send(client, ®, 1); 234ef536abdSDmitry Torokhov if (ret == 1) { 235ef536abdSDmitry Torokhov usleep_range(5000, 5500); 236ef536abdSDmitry Torokhov 237ef536abdSDmitry Torokhov ret = i2c_master_recv(client, buf, len); 238ef536abdSDmitry Torokhov if (ret == len) 239ef536abdSDmitry Torokhov return 0; 240ef536abdSDmitry Torokhov } 241ef536abdSDmitry Torokhov 242ef536abdSDmitry Torokhov error = ret < 0 ? ret : -EIO; 243ef536abdSDmitry Torokhov dev_err(&client->dev, "%s failed: %d\n", __func__, error); 244ef536abdSDmitry Torokhov return ret; 245ef536abdSDmitry Torokhov } 246ef536abdSDmitry Torokhov 247ef536abdSDmitry Torokhov static int ili251x_read_touch_data(struct i2c_client *client, u8 *data) 248ef536abdSDmitry Torokhov { 249ef536abdSDmitry Torokhov int error; 250ef536abdSDmitry Torokhov 251ef536abdSDmitry Torokhov error = ili251x_read_reg(client, REG_TOUCHDATA, 252ef536abdSDmitry Torokhov data, ILI251X_DATA_SIZE1); 253ef536abdSDmitry Torokhov if (!error && data[0] == 2) { 254ef536abdSDmitry Torokhov error = i2c_master_recv(client, data + ILI251X_DATA_SIZE1, 255ef536abdSDmitry Torokhov ILI251X_DATA_SIZE2); 256ef536abdSDmitry Torokhov if (error >= 0 && error != ILI251X_DATA_SIZE2) 257ef536abdSDmitry Torokhov error = -EIO; 258ef536abdSDmitry Torokhov } 259ef536abdSDmitry Torokhov 260ef536abdSDmitry Torokhov return error; 261ef536abdSDmitry Torokhov } 262ef536abdSDmitry Torokhov 263ef536abdSDmitry Torokhov static bool ili251x_touchdata_to_coords(const u8 *touchdata, 26449588917SMarek Vasut unsigned int finger, 26560159e9eSMarek Vasut unsigned int *x, unsigned int *y, 26660159e9eSMarek Vasut unsigned int *z) 26749588917SMarek Vasut { 268ef536abdSDmitry Torokhov u16 val; 269ef536abdSDmitry Torokhov 270ef536abdSDmitry Torokhov val = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0); 271ef536abdSDmitry Torokhov if (!(val & BIT(15))) /* Touch indication */ 27249588917SMarek Vasut return false; 27349588917SMarek Vasut 274ef536abdSDmitry Torokhov *x = val & 0x3fff; 27549588917SMarek Vasut *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2); 27660159e9eSMarek Vasut *z = touchdata[1 + (finger * 5) + 4]; 27749588917SMarek Vasut 27849588917SMarek Vasut return true; 27949588917SMarek Vasut } 28049588917SMarek Vasut 281ef536abdSDmitry Torokhov static bool ili251x_check_continue_polling(const u8 *data, bool touch) 282ef536abdSDmitry Torokhov { 283ef536abdSDmitry Torokhov return touch; 284ef536abdSDmitry Torokhov } 285ef536abdSDmitry Torokhov 286ef536abdSDmitry Torokhov static const struct ili2xxx_chip ili251x_chip = { 287ef536abdSDmitry Torokhov .read_reg = ili251x_read_reg, 288ef536abdSDmitry Torokhov .get_touch_data = ili251x_read_touch_data, 289ef536abdSDmitry Torokhov .parse_touch_data = ili251x_touchdata_to_coords, 290ef536abdSDmitry Torokhov .continue_polling = ili251x_check_continue_polling, 291ef536abdSDmitry Torokhov .max_touches = 10, 292cc12ba18SSven Van Asbroeck .has_calibrate_reg = true, 293235300edSMarek Vasut .has_firmware_proto = true, 29460159e9eSMarek Vasut .has_pressure_reg = true, 295ef536abdSDmitry Torokhov }; 296ef536abdSDmitry Torokhov 297e3559442SMarek Vasut static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata) 298e3559442SMarek Vasut { 299e3559442SMarek Vasut struct input_dev *input = priv->input; 3005c6a7a62SOlivier Sobrie int i; 301ef536abdSDmitry Torokhov bool contact = false, touch; 30260159e9eSMarek Vasut unsigned int x = 0, y = 0, z = 0; 3035c6a7a62SOlivier Sobrie 304ef536abdSDmitry Torokhov for (i = 0; i < priv->chip->max_touches; i++) { 30560159e9eSMarek Vasut touch = priv->chip->parse_touch_data(touchdata, i, &x, &y, &z); 30649588917SMarek Vasut 307f67cc3e9SMarek Vasut input_mt_slot(input, i); 308ef536abdSDmitry Torokhov if (input_mt_report_slot_state(input, MT_TOOL_FINGER, touch)) { 309ef536abdSDmitry Torokhov touchscreen_report_pos(input, &priv->prop, x, y, true); 31060159e9eSMarek Vasut if (priv->chip->has_pressure_reg) 31160159e9eSMarek Vasut input_report_abs(input, ABS_MT_PRESSURE, z); 312ef536abdSDmitry Torokhov contact = true; 313ef536abdSDmitry Torokhov } 3145c6a7a62SOlivier Sobrie } 3155c6a7a62SOlivier Sobrie 3165c6a7a62SOlivier Sobrie input_mt_report_pointer_emulation(input, false); 3175c6a7a62SOlivier Sobrie input_sync(input); 318e3559442SMarek Vasut 31949588917SMarek Vasut return contact; 3205c6a7a62SOlivier Sobrie } 3215c6a7a62SOlivier Sobrie 32271f8e38aSDmitry Torokhov static irqreturn_t ili210x_irq(int irq, void *irq_data) 3235c6a7a62SOlivier Sobrie { 32471f8e38aSDmitry Torokhov struct ili210x *priv = irq_data; 3255c6a7a62SOlivier Sobrie struct i2c_client *client = priv->client; 326ef536abdSDmitry Torokhov const struct ili2xxx_chip *chip = priv->chip; 327ef536abdSDmitry Torokhov u8 touchdata[ILI210X_DATA_SIZE] = { 0 }; 328ef536abdSDmitry Torokhov bool keep_polling; 329e3559442SMarek Vasut bool touch; 33071f8e38aSDmitry Torokhov int error; 3315c6a7a62SOlivier Sobrie 33271f8e38aSDmitry Torokhov do { 333ef536abdSDmitry Torokhov error = chip->get_touch_data(client, touchdata); 3345c6a7a62SOlivier Sobrie if (error) { 3355c6a7a62SOlivier Sobrie dev_err(&client->dev, 336ef536abdSDmitry Torokhov "Unable to get touch data: %d\n", error); 33771f8e38aSDmitry Torokhov break; 3385c6a7a62SOlivier Sobrie } 3395c6a7a62SOlivier Sobrie 340e3559442SMarek Vasut touch = ili210x_report_events(priv, touchdata); 341ef536abdSDmitry Torokhov keep_polling = chip->continue_polling(touchdata, touch); 342ef536abdSDmitry Torokhov if (keep_polling) 34371f8e38aSDmitry Torokhov msleep(ILI2XXX_POLL_PERIOD); 344ef536abdSDmitry Torokhov } while (!priv->stop && keep_polling); 3455c6a7a62SOlivier Sobrie 3465c6a7a62SOlivier Sobrie return IRQ_HANDLED; 3475c6a7a62SOlivier Sobrie } 3485c6a7a62SOlivier Sobrie 349235300edSMarek Vasut static int ili251x_firmware_update_resolution(struct device *dev) 350235300edSMarek Vasut { 351235300edSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 352235300edSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 353235300edSMarek Vasut u16 resx, resy; 354235300edSMarek Vasut u8 rs[10]; 355235300edSMarek Vasut int error; 356235300edSMarek Vasut 357235300edSMarek Vasut /* The firmware update blob might have changed the resolution. */ 358235300edSMarek Vasut error = priv->chip->read_reg(client, REG_PANEL_INFO, &rs, sizeof(rs)); 359235300edSMarek Vasut if (error) 360235300edSMarek Vasut return error; 361235300edSMarek Vasut 362235300edSMarek Vasut resx = le16_to_cpup((__le16 *)rs); 363235300edSMarek Vasut resy = le16_to_cpup((__le16 *)(rs + 2)); 364235300edSMarek Vasut 365235300edSMarek Vasut /* The value reported by the firmware is invalid. */ 366235300edSMarek Vasut if (!resx || resx == 0xffff || !resy || resy == 0xffff) 367235300edSMarek Vasut return -EINVAL; 368235300edSMarek Vasut 369235300edSMarek Vasut input_abs_set_max(priv->input, ABS_X, resx - 1); 370235300edSMarek Vasut input_abs_set_max(priv->input, ABS_Y, resy - 1); 371235300edSMarek Vasut input_abs_set_max(priv->input, ABS_MT_POSITION_X, resx - 1); 372235300edSMarek Vasut input_abs_set_max(priv->input, ABS_MT_POSITION_Y, resy - 1); 373235300edSMarek Vasut 374235300edSMarek Vasut return 0; 375235300edSMarek Vasut } 376235300edSMarek Vasut 37770a7681dSMarek Vasut static ssize_t ili251x_firmware_update_firmware_version(struct device *dev) 37870a7681dSMarek Vasut { 37970a7681dSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 38070a7681dSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 38170a7681dSMarek Vasut int error; 38270a7681dSMarek Vasut u8 fw[8]; 38370a7681dSMarek Vasut 38470a7681dSMarek Vasut /* Get firmware version */ 38570a7681dSMarek Vasut error = priv->chip->read_reg(client, REG_FIRMWARE_VERSION, 38670a7681dSMarek Vasut &fw, sizeof(fw)); 38770a7681dSMarek Vasut if (!error) 38870a7681dSMarek Vasut memcpy(priv->version_firmware, fw, sizeof(fw)); 38970a7681dSMarek Vasut 39070a7681dSMarek Vasut return error; 39170a7681dSMarek Vasut } 39270a7681dSMarek Vasut 39370a7681dSMarek Vasut static ssize_t ili251x_firmware_update_kernel_version(struct device *dev) 39470a7681dSMarek Vasut { 39570a7681dSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 39670a7681dSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 39770a7681dSMarek Vasut int error; 39870a7681dSMarek Vasut u8 kv[5]; 39970a7681dSMarek Vasut 40070a7681dSMarek Vasut /* Get kernel version */ 40170a7681dSMarek Vasut error = priv->chip->read_reg(client, REG_KERNEL_VERSION, 40270a7681dSMarek Vasut &kv, sizeof(kv)); 40370a7681dSMarek Vasut if (!error) 40470a7681dSMarek Vasut memcpy(priv->version_kernel, kv, sizeof(kv)); 40570a7681dSMarek Vasut 40670a7681dSMarek Vasut return error; 40770a7681dSMarek Vasut } 40870a7681dSMarek Vasut 40970a7681dSMarek Vasut static ssize_t ili251x_firmware_update_protocol_version(struct device *dev) 41070a7681dSMarek Vasut { 41170a7681dSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 41270a7681dSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 41370a7681dSMarek Vasut int error; 41470a7681dSMarek Vasut u8 pv[2]; 41570a7681dSMarek Vasut 41670a7681dSMarek Vasut /* Get protocol version */ 41770a7681dSMarek Vasut error = priv->chip->read_reg(client, REG_PROTOCOL_VERSION, 41870a7681dSMarek Vasut &pv, sizeof(pv)); 41970a7681dSMarek Vasut if (!error) 42070a7681dSMarek Vasut memcpy(priv->version_proto, pv, sizeof(pv)); 42170a7681dSMarek Vasut 42270a7681dSMarek Vasut return error; 42370a7681dSMarek Vasut } 42470a7681dSMarek Vasut 42570a7681dSMarek Vasut static ssize_t ili251x_firmware_update_ic_mode(struct device *dev) 42670a7681dSMarek Vasut { 42770a7681dSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 42870a7681dSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 42970a7681dSMarek Vasut int error; 43070a7681dSMarek Vasut u8 md[2]; 43170a7681dSMarek Vasut 43270a7681dSMarek Vasut /* Get chip boot mode */ 43370a7681dSMarek Vasut error = priv->chip->read_reg(client, REG_GET_MODE, &md, sizeof(md)); 43470a7681dSMarek Vasut if (!error) 43570a7681dSMarek Vasut memcpy(priv->ic_mode, md, sizeof(md)); 43670a7681dSMarek Vasut 43770a7681dSMarek Vasut return error; 43870a7681dSMarek Vasut } 43970a7681dSMarek Vasut 440235300edSMarek Vasut static int ili251x_firmware_update_cached_state(struct device *dev) 441235300edSMarek Vasut { 442235300edSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 443235300edSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 444235300edSMarek Vasut int error; 445235300edSMarek Vasut 446235300edSMarek Vasut if (!priv->chip->has_firmware_proto) 447235300edSMarek Vasut return 0; 448235300edSMarek Vasut 449235300edSMarek Vasut /* Wait for firmware to boot and stabilize itself. */ 450235300edSMarek Vasut msleep(200); 451235300edSMarek Vasut 452235300edSMarek Vasut /* Firmware does report valid information. */ 453235300edSMarek Vasut error = ili251x_firmware_update_resolution(dev); 454235300edSMarek Vasut if (error) 455235300edSMarek Vasut return error; 456235300edSMarek Vasut 45770a7681dSMarek Vasut error = ili251x_firmware_update_firmware_version(dev); 45870a7681dSMarek Vasut if (error) 45970a7681dSMarek Vasut return error; 46070a7681dSMarek Vasut 46170a7681dSMarek Vasut error = ili251x_firmware_update_kernel_version(dev); 46270a7681dSMarek Vasut if (error) 46370a7681dSMarek Vasut return error; 46470a7681dSMarek Vasut 46570a7681dSMarek Vasut error = ili251x_firmware_update_protocol_version(dev); 46670a7681dSMarek Vasut if (error) 46770a7681dSMarek Vasut return error; 46870a7681dSMarek Vasut 46970a7681dSMarek Vasut error = ili251x_firmware_update_ic_mode(dev); 47070a7681dSMarek Vasut if (error) 47170a7681dSMarek Vasut return error; 47270a7681dSMarek Vasut 473235300edSMarek Vasut return 0; 474235300edSMarek Vasut } 475235300edSMarek Vasut 47670a7681dSMarek Vasut static ssize_t ili251x_firmware_version_show(struct device *dev, 47770a7681dSMarek Vasut struct device_attribute *attr, 47870a7681dSMarek Vasut char *buf) 47970a7681dSMarek Vasut { 48070a7681dSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 48170a7681dSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 48270a7681dSMarek Vasut u8 *fw = priv->version_firmware; 48370a7681dSMarek Vasut 48470a7681dSMarek Vasut return sysfs_emit(buf, "%02x%02x.%02x%02x.%02x%02x.%02x%02x\n", 48570a7681dSMarek Vasut fw[0], fw[1], fw[2], fw[3], 48670a7681dSMarek Vasut fw[4], fw[5], fw[6], fw[7]); 48770a7681dSMarek Vasut } 48870a7681dSMarek Vasut static DEVICE_ATTR(firmware_version, 0444, ili251x_firmware_version_show, NULL); 48970a7681dSMarek Vasut 49070a7681dSMarek Vasut static ssize_t ili251x_kernel_version_show(struct device *dev, 49170a7681dSMarek Vasut struct device_attribute *attr, 49270a7681dSMarek Vasut char *buf) 49370a7681dSMarek Vasut { 49470a7681dSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 49570a7681dSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 49670a7681dSMarek Vasut u8 *kv = priv->version_kernel; 49770a7681dSMarek Vasut 49870a7681dSMarek Vasut return sysfs_emit(buf, "%02x.%02x.%02x.%02x.%02x\n", 49970a7681dSMarek Vasut kv[0], kv[1], kv[2], kv[3], kv[4]); 50070a7681dSMarek Vasut } 50170a7681dSMarek Vasut static DEVICE_ATTR(kernel_version, 0444, ili251x_kernel_version_show, NULL); 50270a7681dSMarek Vasut 50370a7681dSMarek Vasut static ssize_t ili251x_protocol_version_show(struct device *dev, 50470a7681dSMarek Vasut struct device_attribute *attr, 50570a7681dSMarek Vasut char *buf) 50670a7681dSMarek Vasut { 50770a7681dSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 50870a7681dSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 50970a7681dSMarek Vasut u8 *pv = priv->version_proto; 51070a7681dSMarek Vasut 51170a7681dSMarek Vasut return sysfs_emit(buf, "%02x.%02x\n", pv[0], pv[1]); 51270a7681dSMarek Vasut } 51370a7681dSMarek Vasut static DEVICE_ATTR(protocol_version, 0444, ili251x_protocol_version_show, NULL); 51470a7681dSMarek Vasut 51570a7681dSMarek Vasut static ssize_t ili251x_mode_show(struct device *dev, 51670a7681dSMarek Vasut struct device_attribute *attr, char *buf) 51770a7681dSMarek Vasut { 51870a7681dSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 51970a7681dSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 52070a7681dSMarek Vasut u8 *md = priv->ic_mode; 52170a7681dSMarek Vasut char *mode = "AP"; 52270a7681dSMarek Vasut 52370a7681dSMarek Vasut if (md[0] == REG_GET_MODE_AP) /* Application Mode */ 52470a7681dSMarek Vasut mode = "AP"; 52570a7681dSMarek Vasut else if (md[0] == REG_GET_MODE_BL) /* BootLoader Mode */ 52670a7681dSMarek Vasut mode = "BL"; 52770a7681dSMarek Vasut else /* Unknown Mode */ 52870a7681dSMarek Vasut mode = "??"; 52970a7681dSMarek Vasut 53070a7681dSMarek Vasut return sysfs_emit(buf, "%02x.%02x:%s\n", md[0], md[1], mode); 53170a7681dSMarek Vasut } 53270a7681dSMarek Vasut static DEVICE_ATTR(mode, 0444, ili251x_mode_show, NULL); 53370a7681dSMarek Vasut 5345c6a7a62SOlivier Sobrie static ssize_t ili210x_calibrate(struct device *dev, 5355c6a7a62SOlivier Sobrie struct device_attribute *attr, 5365c6a7a62SOlivier Sobrie const char *buf, size_t count) 5375c6a7a62SOlivier Sobrie { 5385c6a7a62SOlivier Sobrie struct i2c_client *client = to_i2c_client(dev); 5395c6a7a62SOlivier Sobrie struct ili210x *priv = i2c_get_clientdata(client); 5405c6a7a62SOlivier Sobrie unsigned long calibrate; 5415c6a7a62SOlivier Sobrie int rc; 5425c6a7a62SOlivier Sobrie u8 cmd = REG_CALIBRATE; 5435c6a7a62SOlivier Sobrie 5445c6a7a62SOlivier Sobrie if (kstrtoul(buf, 10, &calibrate)) 5455c6a7a62SOlivier Sobrie return -EINVAL; 5465c6a7a62SOlivier Sobrie 5475c6a7a62SOlivier Sobrie if (calibrate > 1) 5485c6a7a62SOlivier Sobrie return -EINVAL; 5495c6a7a62SOlivier Sobrie 5505c6a7a62SOlivier Sobrie if (calibrate) { 5515c6a7a62SOlivier Sobrie rc = i2c_master_send(priv->client, &cmd, sizeof(cmd)); 5525c6a7a62SOlivier Sobrie if (rc != sizeof(cmd)) 5535c6a7a62SOlivier Sobrie return -EIO; 5545c6a7a62SOlivier Sobrie } 5555c6a7a62SOlivier Sobrie 5565c6a7a62SOlivier Sobrie return count; 5575c6a7a62SOlivier Sobrie } 558b27c0d0cSDmitry Torokhov static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate); 5595c6a7a62SOlivier Sobrie 560*c6ac8f0bSMarek Vasut static int ili251x_firmware_to_buffer(const struct firmware *fw, 561*c6ac8f0bSMarek Vasut u8 **buf, u16 *ac_end, u16 *df_end) 562*c6ac8f0bSMarek Vasut { 563*c6ac8f0bSMarek Vasut const struct ihex_binrec *rec; 564*c6ac8f0bSMarek Vasut u32 fw_addr, fw_last_addr = 0; 565*c6ac8f0bSMarek Vasut u16 fw_len; 566*c6ac8f0bSMarek Vasut u8 *fw_buf; 567*c6ac8f0bSMarek Vasut int error; 568*c6ac8f0bSMarek Vasut 569*c6ac8f0bSMarek Vasut /* 570*c6ac8f0bSMarek Vasut * The firmware ihex blob can never be bigger than 64 kiB, so make this 571*c6ac8f0bSMarek Vasut * simple -- allocate a 64 kiB buffer, iterate over the ihex blob records 572*c6ac8f0bSMarek Vasut * once, copy them all into this buffer at the right locations, and then 573*c6ac8f0bSMarek Vasut * do all operations on this linear buffer. 574*c6ac8f0bSMarek Vasut */ 575*c6ac8f0bSMarek Vasut fw_buf = kzalloc(SZ_64K, GFP_KERNEL); 576*c6ac8f0bSMarek Vasut if (!fw_buf) 577*c6ac8f0bSMarek Vasut return -ENOMEM; 578*c6ac8f0bSMarek Vasut 579*c6ac8f0bSMarek Vasut rec = (const struct ihex_binrec *)fw->data; 580*c6ac8f0bSMarek Vasut while (rec) { 581*c6ac8f0bSMarek Vasut fw_addr = be32_to_cpu(rec->addr); 582*c6ac8f0bSMarek Vasut fw_len = be16_to_cpu(rec->len); 583*c6ac8f0bSMarek Vasut 584*c6ac8f0bSMarek Vasut /* The last 32 Byte firmware block can be 0xffe0 */ 585*c6ac8f0bSMarek Vasut if (fw_addr + fw_len > SZ_64K || fw_addr > SZ_64K - 32) { 586*c6ac8f0bSMarek Vasut error = -EFBIG; 587*c6ac8f0bSMarek Vasut goto err_big; 588*c6ac8f0bSMarek Vasut } 589*c6ac8f0bSMarek Vasut 590*c6ac8f0bSMarek Vasut /* Find the last address before DF start address, that is AC end */ 591*c6ac8f0bSMarek Vasut if (fw_addr == 0xf000) 592*c6ac8f0bSMarek Vasut *ac_end = fw_last_addr; 593*c6ac8f0bSMarek Vasut fw_last_addr = fw_addr + fw_len; 594*c6ac8f0bSMarek Vasut 595*c6ac8f0bSMarek Vasut memcpy(fw_buf + fw_addr, rec->data, fw_len); 596*c6ac8f0bSMarek Vasut rec = ihex_next_binrec(rec); 597*c6ac8f0bSMarek Vasut } 598*c6ac8f0bSMarek Vasut 599*c6ac8f0bSMarek Vasut /* DF end address is the last address in the firmware blob */ 600*c6ac8f0bSMarek Vasut *df_end = fw_addr + fw_len; 601*c6ac8f0bSMarek Vasut *buf = fw_buf; 602*c6ac8f0bSMarek Vasut return 0; 603*c6ac8f0bSMarek Vasut 604*c6ac8f0bSMarek Vasut err_big: 605*c6ac8f0bSMarek Vasut kfree(fw_buf); 606*c6ac8f0bSMarek Vasut return error; 607*c6ac8f0bSMarek Vasut } 608*c6ac8f0bSMarek Vasut 609*c6ac8f0bSMarek Vasut /* Switch mode between Application and BootLoader */ 610*c6ac8f0bSMarek Vasut static int ili251x_switch_ic_mode(struct i2c_client *client, u8 cmd_mode) 611*c6ac8f0bSMarek Vasut { 612*c6ac8f0bSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 613*c6ac8f0bSMarek Vasut u8 cmd_wren[3] = { REG_WRITE_ENABLE, 0x5a, 0xa5 }; 614*c6ac8f0bSMarek Vasut u8 md[2]; 615*c6ac8f0bSMarek Vasut int error; 616*c6ac8f0bSMarek Vasut 617*c6ac8f0bSMarek Vasut error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md)); 618*c6ac8f0bSMarek Vasut if (error) 619*c6ac8f0bSMarek Vasut return error; 620*c6ac8f0bSMarek Vasut /* Mode already set */ 621*c6ac8f0bSMarek Vasut if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) || 622*c6ac8f0bSMarek Vasut (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL)) 623*c6ac8f0bSMarek Vasut return 0; 624*c6ac8f0bSMarek Vasut 625*c6ac8f0bSMarek Vasut /* Unlock writes */ 626*c6ac8f0bSMarek Vasut error = i2c_master_send(client, cmd_wren, sizeof(cmd_wren)); 627*c6ac8f0bSMarek Vasut if (error != sizeof(cmd_wren)) 628*c6ac8f0bSMarek Vasut return -EINVAL; 629*c6ac8f0bSMarek Vasut 630*c6ac8f0bSMarek Vasut mdelay(20); 631*c6ac8f0bSMarek Vasut 632*c6ac8f0bSMarek Vasut /* Select mode (BootLoader or Application) */ 633*c6ac8f0bSMarek Vasut error = i2c_master_send(client, &cmd_mode, 1); 634*c6ac8f0bSMarek Vasut if (error != 1) 635*c6ac8f0bSMarek Vasut return -EINVAL; 636*c6ac8f0bSMarek Vasut 637*c6ac8f0bSMarek Vasut mdelay(200); /* Reboot into bootloader takes a lot of time ... */ 638*c6ac8f0bSMarek Vasut 639*c6ac8f0bSMarek Vasut /* Read back mode */ 640*c6ac8f0bSMarek Vasut error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md)); 641*c6ac8f0bSMarek Vasut if (error) 642*c6ac8f0bSMarek Vasut return error; 643*c6ac8f0bSMarek Vasut /* Check if mode is correct now. */ 644*c6ac8f0bSMarek Vasut if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) || 645*c6ac8f0bSMarek Vasut (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL)) 646*c6ac8f0bSMarek Vasut return 0; 647*c6ac8f0bSMarek Vasut 648*c6ac8f0bSMarek Vasut return -EINVAL; 649*c6ac8f0bSMarek Vasut } 650*c6ac8f0bSMarek Vasut 651*c6ac8f0bSMarek Vasut static int ili251x_firmware_busy(struct i2c_client *client) 652*c6ac8f0bSMarek Vasut { 653*c6ac8f0bSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 654*c6ac8f0bSMarek Vasut int error, i = 0; 655*c6ac8f0bSMarek Vasut u8 data; 656*c6ac8f0bSMarek Vasut 657*c6ac8f0bSMarek Vasut do { 658*c6ac8f0bSMarek Vasut /* The read_reg already contains suitable delay */ 659*c6ac8f0bSMarek Vasut error = priv->chip->read_reg(client, REG_IC_BUSY, &data, 1); 660*c6ac8f0bSMarek Vasut if (error) 661*c6ac8f0bSMarek Vasut return error; 662*c6ac8f0bSMarek Vasut if (i++ == 100000) 663*c6ac8f0bSMarek Vasut return -ETIMEDOUT; 664*c6ac8f0bSMarek Vasut } while (data != REG_IC_BUSY_NOT_BUSY); 665*c6ac8f0bSMarek Vasut 666*c6ac8f0bSMarek Vasut return 0; 667*c6ac8f0bSMarek Vasut } 668*c6ac8f0bSMarek Vasut 669*c6ac8f0bSMarek Vasut static int ili251x_firmware_write_to_ic(struct device *dev, u8 *fwbuf, 670*c6ac8f0bSMarek Vasut u16 start, u16 end, u8 dataflash) 671*c6ac8f0bSMarek Vasut { 672*c6ac8f0bSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 673*c6ac8f0bSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 674*c6ac8f0bSMarek Vasut u8 cmd_crc = REG_READ_DATA_CRC; 675*c6ac8f0bSMarek Vasut u8 crcrb[4] = { 0 }; 676*c6ac8f0bSMarek Vasut u8 fw_data[33]; 677*c6ac8f0bSMarek Vasut u16 fw_addr; 678*c6ac8f0bSMarek Vasut int error; 679*c6ac8f0bSMarek Vasut 680*c6ac8f0bSMarek Vasut /* 681*c6ac8f0bSMarek Vasut * The DF (dataflash) needs 2 bytes offset for unknown reasons, 682*c6ac8f0bSMarek Vasut * the AC (application) has 2 bytes CRC16-CCITT at the end. 683*c6ac8f0bSMarek Vasut */ 684*c6ac8f0bSMarek Vasut u16 crc = crc_ccitt(0, fwbuf + start + (dataflash ? 2 : 0), 685*c6ac8f0bSMarek Vasut end - start - 2); 686*c6ac8f0bSMarek Vasut 687*c6ac8f0bSMarek Vasut /* Unlock write to either AC (application) or DF (dataflash) area */ 688*c6ac8f0bSMarek Vasut u8 cmd_wr[10] = { 689*c6ac8f0bSMarek Vasut REG_WRITE_ENABLE, 0x5a, 0xa5, dataflash, 690*c6ac8f0bSMarek Vasut (end >> 16) & 0xff, (end >> 8) & 0xff, end & 0xff, 691*c6ac8f0bSMarek Vasut (crc >> 16) & 0xff, (crc >> 8) & 0xff, crc & 0xff 692*c6ac8f0bSMarek Vasut }; 693*c6ac8f0bSMarek Vasut 694*c6ac8f0bSMarek Vasut error = i2c_master_send(client, cmd_wr, sizeof(cmd_wr)); 695*c6ac8f0bSMarek Vasut if (error != sizeof(cmd_wr)) 696*c6ac8f0bSMarek Vasut return -EINVAL; 697*c6ac8f0bSMarek Vasut 698*c6ac8f0bSMarek Vasut error = ili251x_firmware_busy(client); 699*c6ac8f0bSMarek Vasut if (error) 700*c6ac8f0bSMarek Vasut return error; 701*c6ac8f0bSMarek Vasut 702*c6ac8f0bSMarek Vasut for (fw_addr = start; fw_addr < end; fw_addr += 32) { 703*c6ac8f0bSMarek Vasut fw_data[0] = REG_WRITE_DATA; 704*c6ac8f0bSMarek Vasut memcpy(&(fw_data[1]), fwbuf + fw_addr, 32); 705*c6ac8f0bSMarek Vasut error = i2c_master_send(client, fw_data, 33); 706*c6ac8f0bSMarek Vasut if (error != sizeof(fw_data)) 707*c6ac8f0bSMarek Vasut return error; 708*c6ac8f0bSMarek Vasut error = ili251x_firmware_busy(client); 709*c6ac8f0bSMarek Vasut if (error) 710*c6ac8f0bSMarek Vasut return error; 711*c6ac8f0bSMarek Vasut } 712*c6ac8f0bSMarek Vasut 713*c6ac8f0bSMarek Vasut error = i2c_master_send(client, &cmd_crc, 1); 714*c6ac8f0bSMarek Vasut if (error != 1) 715*c6ac8f0bSMarek Vasut return -EINVAL; 716*c6ac8f0bSMarek Vasut 717*c6ac8f0bSMarek Vasut error = ili251x_firmware_busy(client); 718*c6ac8f0bSMarek Vasut if (error) 719*c6ac8f0bSMarek Vasut return error; 720*c6ac8f0bSMarek Vasut 721*c6ac8f0bSMarek Vasut error = priv->chip->read_reg(client, REG_READ_DATA_CRC, 722*c6ac8f0bSMarek Vasut &crcrb, sizeof(crcrb)); 723*c6ac8f0bSMarek Vasut if (error) 724*c6ac8f0bSMarek Vasut return error; 725*c6ac8f0bSMarek Vasut 726*c6ac8f0bSMarek Vasut /* Check CRC readback */ 727*c6ac8f0bSMarek Vasut if ((crcrb[0] != (crc & 0xff)) || crcrb[1] != ((crc >> 8) & 0xff)) 728*c6ac8f0bSMarek Vasut return -EINVAL; 729*c6ac8f0bSMarek Vasut 730*c6ac8f0bSMarek Vasut return 0; 731*c6ac8f0bSMarek Vasut } 732*c6ac8f0bSMarek Vasut 733*c6ac8f0bSMarek Vasut static int ili251x_firmware_reset(struct i2c_client *client) 734*c6ac8f0bSMarek Vasut { 735*c6ac8f0bSMarek Vasut u8 cmd_reset[2] = { 0xf2, 0x01 }; 736*c6ac8f0bSMarek Vasut int error; 737*c6ac8f0bSMarek Vasut 738*c6ac8f0bSMarek Vasut error = i2c_master_send(client, cmd_reset, sizeof(cmd_reset)); 739*c6ac8f0bSMarek Vasut if (error != sizeof(cmd_reset)) 740*c6ac8f0bSMarek Vasut return -EINVAL; 741*c6ac8f0bSMarek Vasut 742*c6ac8f0bSMarek Vasut return ili251x_firmware_busy(client); 743*c6ac8f0bSMarek Vasut } 744*c6ac8f0bSMarek Vasut 745*c6ac8f0bSMarek Vasut static void ili251x_hardware_reset(struct device *dev) 746*c6ac8f0bSMarek Vasut { 747*c6ac8f0bSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 748*c6ac8f0bSMarek Vasut struct ili210x *priv = i2c_get_clientdata(client); 749*c6ac8f0bSMarek Vasut 750*c6ac8f0bSMarek Vasut /* Reset the controller */ 751*c6ac8f0bSMarek Vasut gpiod_set_value_cansleep(priv->reset_gpio, 1); 752*c6ac8f0bSMarek Vasut usleep_range(10000, 15000); 753*c6ac8f0bSMarek Vasut gpiod_set_value_cansleep(priv->reset_gpio, 0); 754*c6ac8f0bSMarek Vasut msleep(300); 755*c6ac8f0bSMarek Vasut } 756*c6ac8f0bSMarek Vasut 757*c6ac8f0bSMarek Vasut static ssize_t ili210x_firmware_update_store(struct device *dev, 758*c6ac8f0bSMarek Vasut struct device_attribute *attr, 759*c6ac8f0bSMarek Vasut const char *buf, size_t count) 760*c6ac8f0bSMarek Vasut { 761*c6ac8f0bSMarek Vasut struct i2c_client *client = to_i2c_client(dev); 762*c6ac8f0bSMarek Vasut const char *fwname = ILI251X_FW_FILENAME; 763*c6ac8f0bSMarek Vasut const struct firmware *fw; 764*c6ac8f0bSMarek Vasut u16 ac_end, df_end; 765*c6ac8f0bSMarek Vasut u8 *fwbuf; 766*c6ac8f0bSMarek Vasut int error; 767*c6ac8f0bSMarek Vasut int i; 768*c6ac8f0bSMarek Vasut 769*c6ac8f0bSMarek Vasut error = request_ihex_firmware(&fw, fwname, dev); 770*c6ac8f0bSMarek Vasut if (error) { 771*c6ac8f0bSMarek Vasut dev_err(dev, "Failed to request firmware %s, error=%d\n", 772*c6ac8f0bSMarek Vasut fwname, error); 773*c6ac8f0bSMarek Vasut return error; 774*c6ac8f0bSMarek Vasut } 775*c6ac8f0bSMarek Vasut 776*c6ac8f0bSMarek Vasut error = ili251x_firmware_to_buffer(fw, &fwbuf, &ac_end, &df_end); 777*c6ac8f0bSMarek Vasut release_firmware(fw); 778*c6ac8f0bSMarek Vasut if (error) 779*c6ac8f0bSMarek Vasut return error; 780*c6ac8f0bSMarek Vasut 781*c6ac8f0bSMarek Vasut /* 782*c6ac8f0bSMarek Vasut * Disable touchscreen IRQ, so that we would not get spurious touch 783*c6ac8f0bSMarek Vasut * interrupt during firmware update, and so that the IRQ handler won't 784*c6ac8f0bSMarek Vasut * trigger and interfere with the firmware update. There is no bit in 785*c6ac8f0bSMarek Vasut * the touch controller to disable the IRQs during update, so we have 786*c6ac8f0bSMarek Vasut * to do it this way here. 787*c6ac8f0bSMarek Vasut */ 788*c6ac8f0bSMarek Vasut disable_irq(client->irq); 789*c6ac8f0bSMarek Vasut 790*c6ac8f0bSMarek Vasut dev_dbg(dev, "Firmware update started, firmware=%s\n", fwname); 791*c6ac8f0bSMarek Vasut 792*c6ac8f0bSMarek Vasut ili251x_hardware_reset(dev); 793*c6ac8f0bSMarek Vasut 794*c6ac8f0bSMarek Vasut error = ili251x_firmware_reset(client); 795*c6ac8f0bSMarek Vasut if (error) 796*c6ac8f0bSMarek Vasut goto exit; 797*c6ac8f0bSMarek Vasut 798*c6ac8f0bSMarek Vasut /* This may not succeed on first try, so re-try a few times. */ 799*c6ac8f0bSMarek Vasut for (i = 0; i < 5; i++) { 800*c6ac8f0bSMarek Vasut error = ili251x_switch_ic_mode(client, REG_SET_MODE_BL); 801*c6ac8f0bSMarek Vasut if (!error) 802*c6ac8f0bSMarek Vasut break; 803*c6ac8f0bSMarek Vasut } 804*c6ac8f0bSMarek Vasut 805*c6ac8f0bSMarek Vasut if (error) 806*c6ac8f0bSMarek Vasut goto exit; 807*c6ac8f0bSMarek Vasut 808*c6ac8f0bSMarek Vasut dev_dbg(dev, "IC is now in BootLoader mode\n"); 809*c6ac8f0bSMarek Vasut 810*c6ac8f0bSMarek Vasut msleep(200); /* The bootloader seems to need some time too. */ 811*c6ac8f0bSMarek Vasut 812*c6ac8f0bSMarek Vasut error = ili251x_firmware_write_to_ic(dev, fwbuf, 0xf000, df_end, 1); 813*c6ac8f0bSMarek Vasut if (error) { 814*c6ac8f0bSMarek Vasut dev_err(dev, "DF firmware update failed, error=%d\n", error); 815*c6ac8f0bSMarek Vasut goto exit; 816*c6ac8f0bSMarek Vasut } 817*c6ac8f0bSMarek Vasut 818*c6ac8f0bSMarek Vasut dev_dbg(dev, "DataFlash firmware written\n"); 819*c6ac8f0bSMarek Vasut 820*c6ac8f0bSMarek Vasut error = ili251x_firmware_write_to_ic(dev, fwbuf, 0x2000, ac_end, 0); 821*c6ac8f0bSMarek Vasut if (error) { 822*c6ac8f0bSMarek Vasut dev_err(dev, "AC firmware update failed, error=%d\n", error); 823*c6ac8f0bSMarek Vasut goto exit; 824*c6ac8f0bSMarek Vasut } 825*c6ac8f0bSMarek Vasut 826*c6ac8f0bSMarek Vasut dev_dbg(dev, "Application firmware written\n"); 827*c6ac8f0bSMarek Vasut 828*c6ac8f0bSMarek Vasut /* This may not succeed on first try, so re-try a few times. */ 829*c6ac8f0bSMarek Vasut for (i = 0; i < 5; i++) { 830*c6ac8f0bSMarek Vasut error = ili251x_switch_ic_mode(client, REG_SET_MODE_AP); 831*c6ac8f0bSMarek Vasut if (!error) 832*c6ac8f0bSMarek Vasut break; 833*c6ac8f0bSMarek Vasut } 834*c6ac8f0bSMarek Vasut 835*c6ac8f0bSMarek Vasut if (error) 836*c6ac8f0bSMarek Vasut goto exit; 837*c6ac8f0bSMarek Vasut 838*c6ac8f0bSMarek Vasut dev_dbg(dev, "IC is now in Application mode\n"); 839*c6ac8f0bSMarek Vasut 840*c6ac8f0bSMarek Vasut error = ili251x_firmware_update_cached_state(dev); 841*c6ac8f0bSMarek Vasut if (error) 842*c6ac8f0bSMarek Vasut goto exit; 843*c6ac8f0bSMarek Vasut 844*c6ac8f0bSMarek Vasut error = count; 845*c6ac8f0bSMarek Vasut 846*c6ac8f0bSMarek Vasut exit: 847*c6ac8f0bSMarek Vasut ili251x_hardware_reset(dev); 848*c6ac8f0bSMarek Vasut dev_dbg(dev, "Firmware update ended, error=%i\n", error); 849*c6ac8f0bSMarek Vasut enable_irq(client->irq); 850*c6ac8f0bSMarek Vasut kfree(fwbuf); 851*c6ac8f0bSMarek Vasut return error; 852*c6ac8f0bSMarek Vasut } 853*c6ac8f0bSMarek Vasut 854*c6ac8f0bSMarek Vasut static DEVICE_ATTR(firmware_update, 0200, NULL, ili210x_firmware_update_store); 855*c6ac8f0bSMarek Vasut 8565c6a7a62SOlivier Sobrie static struct attribute *ili210x_attributes[] = { 8575c6a7a62SOlivier Sobrie &dev_attr_calibrate.attr, 858*c6ac8f0bSMarek Vasut &dev_attr_firmware_update.attr, 85970a7681dSMarek Vasut &dev_attr_firmware_version.attr, 86070a7681dSMarek Vasut &dev_attr_kernel_version.attr, 86170a7681dSMarek Vasut &dev_attr_protocol_version.attr, 86270a7681dSMarek Vasut &dev_attr_mode.attr, 8635c6a7a62SOlivier Sobrie NULL, 8645c6a7a62SOlivier Sobrie }; 8655c6a7a62SOlivier Sobrie 86670a7681dSMarek Vasut static umode_t ili210x_attributes_visible(struct kobject *kobj, 867cc12ba18SSven Van Asbroeck struct attribute *attr, int index) 868cc12ba18SSven Van Asbroeck { 869cc12ba18SSven Van Asbroeck struct device *dev = kobj_to_dev(kobj); 870cc12ba18SSven Van Asbroeck struct i2c_client *client = to_i2c_client(dev); 871cc12ba18SSven Van Asbroeck struct ili210x *priv = i2c_get_clientdata(client); 872cc12ba18SSven Van Asbroeck 87370a7681dSMarek Vasut /* Calibrate is present on all ILI2xxx which have calibrate register */ 87470a7681dSMarek Vasut if (attr == &dev_attr_calibrate.attr) 875fbd1ec00SLuca Weiss return priv->chip->has_calibrate_reg ? attr->mode : 0; 87670a7681dSMarek Vasut 87770a7681dSMarek Vasut /* Firmware/Kernel/Protocol/BootMode is implememted only for ILI251x */ 87870a7681dSMarek Vasut if (!priv->chip->has_firmware_proto) 87970a7681dSMarek Vasut return 0; 88070a7681dSMarek Vasut 88170a7681dSMarek Vasut return attr->mode; 882cc12ba18SSven Van Asbroeck } 883cc12ba18SSven Van Asbroeck 8845c6a7a62SOlivier Sobrie static const struct attribute_group ili210x_attr_group = { 8855c6a7a62SOlivier Sobrie .attrs = ili210x_attributes, 88670a7681dSMarek Vasut .is_visible = ili210x_attributes_visible, 8875c6a7a62SOlivier Sobrie }; 8885c6a7a62SOlivier Sobrie 889201f3c80SMarek Vasut static void ili210x_power_down(void *data) 890201f3c80SMarek Vasut { 891201f3c80SMarek Vasut struct gpio_desc *reset_gpio = data; 892201f3c80SMarek Vasut 893201f3c80SMarek Vasut gpiod_set_value_cansleep(reset_gpio, 1); 894201f3c80SMarek Vasut } 895201f3c80SMarek Vasut 89671f8e38aSDmitry Torokhov static void ili210x_stop(void *data) 8971bdec5d9SMarek Vasut { 8981bdec5d9SMarek Vasut struct ili210x *priv = data; 8991bdec5d9SMarek Vasut 90071f8e38aSDmitry Torokhov /* Tell ISR to quit even if there is a contact. */ 90171f8e38aSDmitry Torokhov priv->stop = true; 9021bdec5d9SMarek Vasut } 9031bdec5d9SMarek Vasut 9045298cc4cSBill Pemberton static int ili210x_i2c_probe(struct i2c_client *client, 9055c6a7a62SOlivier Sobrie const struct i2c_device_id *id) 9065c6a7a62SOlivier Sobrie { 9075c6a7a62SOlivier Sobrie struct device *dev = &client->dev; 908ef536abdSDmitry Torokhov const struct ili2xxx_chip *chip; 9095c6a7a62SOlivier Sobrie struct ili210x *priv; 910201f3c80SMarek Vasut struct gpio_desc *reset_gpio; 9115c6a7a62SOlivier Sobrie struct input_dev *input; 9125c6a7a62SOlivier Sobrie int error; 913b32fbeaeSSven Van Asbroeck unsigned int max_xy; 9145c6a7a62SOlivier Sobrie 9155c6a7a62SOlivier Sobrie dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver"); 9165c6a7a62SOlivier Sobrie 917ef536abdSDmitry Torokhov chip = device_get_match_data(dev); 918ef536abdSDmitry Torokhov if (!chip && id) 919ef536abdSDmitry Torokhov chip = (const struct ili2xxx_chip *)id->driver_data; 920ef536abdSDmitry Torokhov if (!chip) { 921ef536abdSDmitry Torokhov dev_err(&client->dev, "unknown device model\n"); 922ef536abdSDmitry Torokhov return -ENODEV; 923ef536abdSDmitry Torokhov } 924ef536abdSDmitry Torokhov 9255c6a7a62SOlivier Sobrie if (client->irq <= 0) { 9265c6a7a62SOlivier Sobrie dev_err(dev, "No IRQ!\n"); 9275c6a7a62SOlivier Sobrie return -EINVAL; 9285c6a7a62SOlivier Sobrie } 9295c6a7a62SOlivier Sobrie 930201f3c80SMarek Vasut reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 931201f3c80SMarek Vasut if (IS_ERR(reset_gpio)) 932201f3c80SMarek Vasut return PTR_ERR(reset_gpio); 933201f3c80SMarek Vasut 934201f3c80SMarek Vasut if (reset_gpio) { 935201f3c80SMarek Vasut error = devm_add_action_or_reset(dev, ili210x_power_down, 936201f3c80SMarek Vasut reset_gpio); 937201f3c80SMarek Vasut if (error) 938201f3c80SMarek Vasut return error; 939201f3c80SMarek Vasut 940201f3c80SMarek Vasut usleep_range(50, 100); 941201f3c80SMarek Vasut gpiod_set_value_cansleep(reset_gpio, 0); 942201f3c80SMarek Vasut msleep(100); 943201f3c80SMarek Vasut } 944201f3c80SMarek Vasut 94512294577SMarek Vasut priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 94612294577SMarek Vasut if (!priv) 94712294577SMarek Vasut return -ENOMEM; 94812294577SMarek Vasut 94912294577SMarek Vasut input = devm_input_allocate_device(dev); 95012294577SMarek Vasut if (!input) 95112294577SMarek Vasut return -ENOMEM; 95212294577SMarek Vasut 95312294577SMarek Vasut priv->client = client; 95412294577SMarek Vasut priv->input = input; 95512294577SMarek Vasut priv->reset_gpio = reset_gpio; 956ef536abdSDmitry Torokhov priv->chip = chip; 95712294577SMarek Vasut i2c_set_clientdata(client, priv); 95812294577SMarek Vasut 9595c6a7a62SOlivier Sobrie /* Setup input device */ 9605c6a7a62SOlivier Sobrie input->name = "ILI210x Touchscreen"; 9615c6a7a62SOlivier Sobrie input->id.bustype = BUS_I2C; 9625c6a7a62SOlivier Sobrie 9635c6a7a62SOlivier Sobrie /* Multi touch */ 964b32fbeaeSSven Van Asbroeck max_xy = (chip->resolution ?: SZ_64K) - 1; 965b32fbeaeSSven Van Asbroeck input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_xy, 0, 0); 966b32fbeaeSSven Van Asbroeck input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_xy, 0, 0); 96760159e9eSMarek Vasut if (priv->chip->has_pressure_reg) 96860159e9eSMarek Vasut input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xa, 0, 0); 969235300edSMarek Vasut error = ili251x_firmware_update_cached_state(dev); 970235300edSMarek Vasut if (error) { 971235300edSMarek Vasut dev_err(dev, "Unable to cache firmware information, err: %d\n", 972235300edSMarek Vasut error); 973235300edSMarek Vasut return error; 974235300edSMarek Vasut } 975f67cc3e9SMarek Vasut touchscreen_parse_properties(input, true, &priv->prop); 97643f06a4cSDmitry Torokhov 977ef536abdSDmitry Torokhov error = input_mt_init_slots(input, priv->chip->max_touches, 978ef536abdSDmitry Torokhov INPUT_MT_DIRECT); 97943f06a4cSDmitry Torokhov if (error) { 98043f06a4cSDmitry Torokhov dev_err(dev, "Unable to set up slots, err: %d\n", error); 98143f06a4cSDmitry Torokhov return error; 98243f06a4cSDmitry Torokhov } 9835c6a7a62SOlivier Sobrie 98471f8e38aSDmitry Torokhov error = devm_request_threaded_irq(dev, client->irq, NULL, ili210x_irq, 98571f8e38aSDmitry Torokhov IRQF_ONESHOT, client->name, priv); 9865c6a7a62SOlivier Sobrie if (error) { 9875c6a7a62SOlivier Sobrie dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n", 9885c6a7a62SOlivier Sobrie error); 9891bdec5d9SMarek Vasut return error; 9905c6a7a62SOlivier Sobrie } 9915c6a7a62SOlivier Sobrie 99271f8e38aSDmitry Torokhov error = devm_add_action_or_reset(dev, ili210x_stop, priv); 99371f8e38aSDmitry Torokhov if (error) 99471f8e38aSDmitry Torokhov return error; 99571f8e38aSDmitry Torokhov 996576057bfSDmitry Torokhov error = devm_device_add_group(dev, &ili210x_attr_group); 9975c6a7a62SOlivier Sobrie if (error) { 9985c6a7a62SOlivier Sobrie dev_err(dev, "Unable to create sysfs attributes, err: %d\n", 9995c6a7a62SOlivier Sobrie error); 10001bdec5d9SMarek Vasut return error; 10015c6a7a62SOlivier Sobrie } 10025c6a7a62SOlivier Sobrie 10035c6a7a62SOlivier Sobrie error = input_register_device(priv->input); 10045c6a7a62SOlivier Sobrie if (error) { 1005971bd8faSMasanari Iida dev_err(dev, "Cannot register input device, err: %d\n", error); 1006576057bfSDmitry Torokhov return error; 10075c6a7a62SOlivier Sobrie } 10085c6a7a62SOlivier Sobrie 10095c6a7a62SOlivier Sobrie return 0; 10105c6a7a62SOlivier Sobrie } 10115c6a7a62SOlivier Sobrie 10125c6a7a62SOlivier Sobrie static const struct i2c_device_id ili210x_i2c_id[] = { 1013ef536abdSDmitry Torokhov { "ili210x", (long)&ili210x_chip }, 1014ef536abdSDmitry Torokhov { "ili2117", (long)&ili211x_chip }, 1015d0c5e7d4SLuca Weiss { "ili2120", (long)&ili212x_chip }, 1016ef536abdSDmitry Torokhov { "ili251x", (long)&ili251x_chip }, 10175c6a7a62SOlivier Sobrie { } 10185c6a7a62SOlivier Sobrie }; 10195c6a7a62SOlivier Sobrie MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id); 10205c6a7a62SOlivier Sobrie 1021c5d0e4b5SMarek Vasut static const struct of_device_id ili210x_dt_ids[] = { 1022ef536abdSDmitry Torokhov { .compatible = "ilitek,ili210x", .data = &ili210x_chip }, 1023ef536abdSDmitry Torokhov { .compatible = "ilitek,ili2117", .data = &ili211x_chip }, 1024d0c5e7d4SLuca Weiss { .compatible = "ilitek,ili2120", .data = &ili212x_chip }, 1025ef536abdSDmitry Torokhov { .compatible = "ilitek,ili251x", .data = &ili251x_chip }, 1026ef536abdSDmitry Torokhov { } 1027c5d0e4b5SMarek Vasut }; 1028c5d0e4b5SMarek Vasut MODULE_DEVICE_TABLE(of, ili210x_dt_ids); 1029c5d0e4b5SMarek Vasut 10305c6a7a62SOlivier Sobrie static struct i2c_driver ili210x_ts_driver = { 10315c6a7a62SOlivier Sobrie .driver = { 10325c6a7a62SOlivier Sobrie .name = "ili210x_i2c", 1033c5d0e4b5SMarek Vasut .of_match_table = ili210x_dt_ids, 10345c6a7a62SOlivier Sobrie }, 10355c6a7a62SOlivier Sobrie .id_table = ili210x_i2c_id, 10365c6a7a62SOlivier Sobrie .probe = ili210x_i2c_probe, 10375c6a7a62SOlivier Sobrie }; 10385c6a7a62SOlivier Sobrie 10395c6a7a62SOlivier Sobrie module_i2c_driver(ili210x_ts_driver); 10405c6a7a62SOlivier Sobrie 10415c6a7a62SOlivier Sobrie MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>"); 10425c6a7a62SOlivier Sobrie MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver"); 10435c6a7a62SOlivier Sobrie MODULE_LICENSE("GPL"); 1044