0001
0002 #include <linux/crc-ccitt.h>
0003 #include <linux/delay.h>
0004 #include <linux/gpio/consumer.h>
0005 #include <linux/i2c.h>
0006 #include <linux/ihex.h>
0007 #include <linux/input.h>
0008 #include <linux/input/mt.h>
0009 #include <linux/input/touchscreen.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/module.h>
0012 #include <linux/of_device.h>
0013 #include <linux/sizes.h>
0014 #include <linux/slab.h>
0015 #include <asm/unaligned.h>
0016
0017 #define ILI2XXX_POLL_PERIOD 15
0018
0019 #define ILI210X_DATA_SIZE 64
0020 #define ILI211X_DATA_SIZE 43
0021 #define ILI251X_DATA_SIZE1 31
0022 #define ILI251X_DATA_SIZE2 20
0023
0024
0025 #define REG_TOUCHDATA 0x10
0026 #define REG_PANEL_INFO 0x20
0027 #define REG_FIRMWARE_VERSION 0x40
0028 #define REG_PROTOCOL_VERSION 0x42
0029 #define REG_KERNEL_VERSION 0x61
0030 #define REG_IC_BUSY 0x80
0031 #define REG_IC_BUSY_NOT_BUSY 0x50
0032 #define REG_GET_MODE 0xc0
0033 #define REG_GET_MODE_AP 0x5a
0034 #define REG_GET_MODE_BL 0x55
0035 #define REG_SET_MODE_AP 0xc1
0036 #define REG_SET_MODE_BL 0xc2
0037 #define REG_WRITE_DATA 0xc3
0038 #define REG_WRITE_ENABLE 0xc4
0039 #define REG_READ_DATA_CRC 0xc7
0040 #define REG_CALIBRATE 0xcc
0041
0042 #define ILI251X_FW_FILENAME "ilitek/ili251x.bin"
0043
0044 struct ili2xxx_chip {
0045 int (*read_reg)(struct i2c_client *client, u8 reg,
0046 void *buf, size_t len);
0047 int (*get_touch_data)(struct i2c_client *client, u8 *data);
0048 bool (*parse_touch_data)(const u8 *data, unsigned int finger,
0049 unsigned int *x, unsigned int *y,
0050 unsigned int *z);
0051 bool (*continue_polling)(const u8 *data, bool touch);
0052 unsigned int max_touches;
0053 unsigned int resolution;
0054 bool has_calibrate_reg;
0055 bool has_firmware_proto;
0056 bool has_pressure_reg;
0057 };
0058
0059 struct ili210x {
0060 struct i2c_client *client;
0061 struct input_dev *input;
0062 struct gpio_desc *reset_gpio;
0063 struct touchscreen_properties prop;
0064 const struct ili2xxx_chip *chip;
0065 u8 version_firmware[8];
0066 u8 version_kernel[5];
0067 u8 version_proto[2];
0068 u8 ic_mode[2];
0069 bool stop;
0070 };
0071
0072 static int ili210x_read_reg(struct i2c_client *client,
0073 u8 reg, void *buf, size_t len)
0074 {
0075 struct i2c_msg msg[] = {
0076 {
0077 .addr = client->addr,
0078 .flags = 0,
0079 .len = 1,
0080 .buf = ®,
0081 },
0082 {
0083 .addr = client->addr,
0084 .flags = I2C_M_RD,
0085 .len = len,
0086 .buf = buf,
0087 }
0088 };
0089 int error, ret;
0090
0091 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
0092 if (ret != ARRAY_SIZE(msg)) {
0093 error = ret < 0 ? ret : -EIO;
0094 dev_err(&client->dev, "%s failed: %d\n", __func__, error);
0095 return error;
0096 }
0097
0098 return 0;
0099 }
0100
0101 static int ili210x_read_touch_data(struct i2c_client *client, u8 *data)
0102 {
0103 return ili210x_read_reg(client, REG_TOUCHDATA,
0104 data, ILI210X_DATA_SIZE);
0105 }
0106
0107 static bool ili210x_touchdata_to_coords(const u8 *touchdata,
0108 unsigned int finger,
0109 unsigned int *x, unsigned int *y,
0110 unsigned int *z)
0111 {
0112 if (!(touchdata[0] & BIT(finger)))
0113 return false;
0114
0115 *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0);
0116 *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2);
0117
0118 return true;
0119 }
0120
0121 static bool ili210x_check_continue_polling(const u8 *data, bool touch)
0122 {
0123 return data[0] & 0xf3;
0124 }
0125
0126 static const struct ili2xxx_chip ili210x_chip = {
0127 .read_reg = ili210x_read_reg,
0128 .get_touch_data = ili210x_read_touch_data,
0129 .parse_touch_data = ili210x_touchdata_to_coords,
0130 .continue_polling = ili210x_check_continue_polling,
0131 .max_touches = 2,
0132 .has_calibrate_reg = true,
0133 };
0134
0135 static int ili211x_read_touch_data(struct i2c_client *client, u8 *data)
0136 {
0137 s16 sum = 0;
0138 int error;
0139 int ret;
0140 int i;
0141
0142 ret = i2c_master_recv(client, data, ILI211X_DATA_SIZE);
0143 if (ret != ILI211X_DATA_SIZE) {
0144 error = ret < 0 ? ret : -EIO;
0145 dev_err(&client->dev, "%s failed: %d\n", __func__, error);
0146 return error;
0147 }
0148
0149
0150 for (i = 0; i < ILI211X_DATA_SIZE - 1; i++)
0151 sum = (sum + data[i]) & 0xff;
0152
0153 if ((-sum & 0xff) != data[ILI211X_DATA_SIZE - 1]) {
0154 dev_err(&client->dev,
0155 "CRC error (crc=0x%02x expected=0x%02x)\n",
0156 sum, data[ILI211X_DATA_SIZE - 1]);
0157 return -EIO;
0158 }
0159
0160 return 0;
0161 }
0162
0163 static bool ili211x_touchdata_to_coords(const u8 *touchdata,
0164 unsigned int finger,
0165 unsigned int *x, unsigned int *y,
0166 unsigned int *z)
0167 {
0168 u32 data;
0169
0170 data = get_unaligned_be32(touchdata + 1 + (finger * 4) + 0);
0171 if (data == 0xffffffff)
0172 return false;
0173
0174 *x = ((touchdata[1 + (finger * 4) + 0] & 0xf0) << 4) |
0175 touchdata[1 + (finger * 4) + 1];
0176 *y = ((touchdata[1 + (finger * 4) + 0] & 0x0f) << 8) |
0177 touchdata[1 + (finger * 4) + 2];
0178
0179 return true;
0180 }
0181
0182 static bool ili211x_decline_polling(const u8 *data, bool touch)
0183 {
0184 return false;
0185 }
0186
0187 static const struct ili2xxx_chip ili211x_chip = {
0188 .read_reg = ili210x_read_reg,
0189 .get_touch_data = ili211x_read_touch_data,
0190 .parse_touch_data = ili211x_touchdata_to_coords,
0191 .continue_polling = ili211x_decline_polling,
0192 .max_touches = 10,
0193 .resolution = 2048,
0194 };
0195
0196 static bool ili212x_touchdata_to_coords(const u8 *touchdata,
0197 unsigned int finger,
0198 unsigned int *x, unsigned int *y,
0199 unsigned int *z)
0200 {
0201 u16 val;
0202
0203 val = get_unaligned_be16(touchdata + 3 + (finger * 5) + 0);
0204 if (!(val & BIT(15)))
0205 return false;
0206
0207 *x = val & 0x3fff;
0208 *y = get_unaligned_be16(touchdata + 3 + (finger * 5) + 2);
0209
0210 return true;
0211 }
0212
0213 static bool ili212x_check_continue_polling(const u8 *data, bool touch)
0214 {
0215 return touch;
0216 }
0217
0218 static const struct ili2xxx_chip ili212x_chip = {
0219 .read_reg = ili210x_read_reg,
0220 .get_touch_data = ili210x_read_touch_data,
0221 .parse_touch_data = ili212x_touchdata_to_coords,
0222 .continue_polling = ili212x_check_continue_polling,
0223 .max_touches = 10,
0224 .has_calibrate_reg = true,
0225 };
0226
0227 static int ili251x_read_reg_common(struct i2c_client *client,
0228 u8 reg, void *buf, size_t len,
0229 unsigned int delay)
0230 {
0231 int error;
0232 int ret;
0233
0234 ret = i2c_master_send(client, ®, 1);
0235 if (ret == 1) {
0236 if (delay)
0237 usleep_range(delay, delay + 500);
0238
0239 ret = i2c_master_recv(client, buf, len);
0240 if (ret == len)
0241 return 0;
0242 }
0243
0244 error = ret < 0 ? ret : -EIO;
0245 dev_err(&client->dev, "%s failed: %d\n", __func__, error);
0246 return ret;
0247 }
0248
0249 static int ili251x_read_reg(struct i2c_client *client,
0250 u8 reg, void *buf, size_t len)
0251 {
0252 return ili251x_read_reg_common(client, reg, buf, len, 5000);
0253 }
0254
0255 static int ili251x_read_touch_data(struct i2c_client *client, u8 *data)
0256 {
0257 int error;
0258
0259 error = ili251x_read_reg_common(client, REG_TOUCHDATA,
0260 data, ILI251X_DATA_SIZE1, 0);
0261 if (!error && data[0] == 2) {
0262 error = i2c_master_recv(client, data + ILI251X_DATA_SIZE1,
0263 ILI251X_DATA_SIZE2);
0264 if (error >= 0 && error != ILI251X_DATA_SIZE2)
0265 error = -EIO;
0266 }
0267
0268 return error;
0269 }
0270
0271 static bool ili251x_touchdata_to_coords(const u8 *touchdata,
0272 unsigned int finger,
0273 unsigned int *x, unsigned int *y,
0274 unsigned int *z)
0275 {
0276 u16 val;
0277
0278 val = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0);
0279 if (!(val & BIT(15)))
0280 return false;
0281
0282 *x = val & 0x3fff;
0283 *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2);
0284 *z = touchdata[1 + (finger * 5) + 4];
0285
0286 return true;
0287 }
0288
0289 static bool ili251x_check_continue_polling(const u8 *data, bool touch)
0290 {
0291 return touch;
0292 }
0293
0294 static const struct ili2xxx_chip ili251x_chip = {
0295 .read_reg = ili251x_read_reg,
0296 .get_touch_data = ili251x_read_touch_data,
0297 .parse_touch_data = ili251x_touchdata_to_coords,
0298 .continue_polling = ili251x_check_continue_polling,
0299 .max_touches = 10,
0300 .has_calibrate_reg = true,
0301 .has_firmware_proto = true,
0302 .has_pressure_reg = true,
0303 };
0304
0305 static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata)
0306 {
0307 struct input_dev *input = priv->input;
0308 int i;
0309 bool contact = false, touch;
0310 unsigned int x = 0, y = 0, z = 0;
0311
0312 for (i = 0; i < priv->chip->max_touches; i++) {
0313 touch = priv->chip->parse_touch_data(touchdata, i, &x, &y, &z);
0314
0315 input_mt_slot(input, i);
0316 if (input_mt_report_slot_state(input, MT_TOOL_FINGER, touch)) {
0317 touchscreen_report_pos(input, &priv->prop, x, y, true);
0318 if (priv->chip->has_pressure_reg)
0319 input_report_abs(input, ABS_MT_PRESSURE, z);
0320 contact = true;
0321 }
0322 }
0323
0324 input_mt_report_pointer_emulation(input, false);
0325 input_sync(input);
0326
0327 return contact;
0328 }
0329
0330 static irqreturn_t ili210x_irq(int irq, void *irq_data)
0331 {
0332 struct ili210x *priv = irq_data;
0333 struct i2c_client *client = priv->client;
0334 const struct ili2xxx_chip *chip = priv->chip;
0335 u8 touchdata[ILI210X_DATA_SIZE] = { 0 };
0336 bool keep_polling;
0337 ktime_t time_next;
0338 s64 time_delta;
0339 bool touch;
0340 int error;
0341
0342 do {
0343 time_next = ktime_add_ms(ktime_get(), ILI2XXX_POLL_PERIOD);
0344 error = chip->get_touch_data(client, touchdata);
0345 if (error) {
0346 dev_err(&client->dev,
0347 "Unable to get touch data: %d\n", error);
0348 break;
0349 }
0350
0351 touch = ili210x_report_events(priv, touchdata);
0352 keep_polling = chip->continue_polling(touchdata, touch);
0353 if (keep_polling) {
0354 time_delta = ktime_us_delta(time_next, ktime_get());
0355 if (time_delta > 0)
0356 usleep_range(time_delta, time_delta + 1000);
0357 }
0358 } while (!priv->stop && keep_polling);
0359
0360 return IRQ_HANDLED;
0361 }
0362
0363 static int ili251x_firmware_update_resolution(struct device *dev)
0364 {
0365 struct i2c_client *client = to_i2c_client(dev);
0366 struct ili210x *priv = i2c_get_clientdata(client);
0367 u16 resx, resy;
0368 u8 rs[10];
0369 int error;
0370
0371
0372 error = priv->chip->read_reg(client, REG_PANEL_INFO, &rs, sizeof(rs));
0373 if (error)
0374 return error;
0375
0376 resx = le16_to_cpup((__le16 *)rs);
0377 resy = le16_to_cpup((__le16 *)(rs + 2));
0378
0379
0380 if (!resx || resx == 0xffff || !resy || resy == 0xffff)
0381 return -EINVAL;
0382
0383 input_abs_set_max(priv->input, ABS_X, resx - 1);
0384 input_abs_set_max(priv->input, ABS_Y, resy - 1);
0385 input_abs_set_max(priv->input, ABS_MT_POSITION_X, resx - 1);
0386 input_abs_set_max(priv->input, ABS_MT_POSITION_Y, resy - 1);
0387
0388 return 0;
0389 }
0390
0391 static ssize_t ili251x_firmware_update_firmware_version(struct device *dev)
0392 {
0393 struct i2c_client *client = to_i2c_client(dev);
0394 struct ili210x *priv = i2c_get_clientdata(client);
0395 int error;
0396 u8 fw[8];
0397
0398
0399 error = priv->chip->read_reg(client, REG_FIRMWARE_VERSION,
0400 &fw, sizeof(fw));
0401 if (!error)
0402 memcpy(priv->version_firmware, fw, sizeof(fw));
0403
0404 return error;
0405 }
0406
0407 static ssize_t ili251x_firmware_update_kernel_version(struct device *dev)
0408 {
0409 struct i2c_client *client = to_i2c_client(dev);
0410 struct ili210x *priv = i2c_get_clientdata(client);
0411 int error;
0412 u8 kv[5];
0413
0414
0415 error = priv->chip->read_reg(client, REG_KERNEL_VERSION,
0416 &kv, sizeof(kv));
0417 if (!error)
0418 memcpy(priv->version_kernel, kv, sizeof(kv));
0419
0420 return error;
0421 }
0422
0423 static ssize_t ili251x_firmware_update_protocol_version(struct device *dev)
0424 {
0425 struct i2c_client *client = to_i2c_client(dev);
0426 struct ili210x *priv = i2c_get_clientdata(client);
0427 int error;
0428 u8 pv[2];
0429
0430
0431 error = priv->chip->read_reg(client, REG_PROTOCOL_VERSION,
0432 &pv, sizeof(pv));
0433 if (!error)
0434 memcpy(priv->version_proto, pv, sizeof(pv));
0435
0436 return error;
0437 }
0438
0439 static ssize_t ili251x_firmware_update_ic_mode(struct device *dev)
0440 {
0441 struct i2c_client *client = to_i2c_client(dev);
0442 struct ili210x *priv = i2c_get_clientdata(client);
0443 int error;
0444 u8 md[2];
0445
0446
0447 error = priv->chip->read_reg(client, REG_GET_MODE, &md, sizeof(md));
0448 if (!error)
0449 memcpy(priv->ic_mode, md, sizeof(md));
0450
0451 return error;
0452 }
0453
0454 static int ili251x_firmware_update_cached_state(struct device *dev)
0455 {
0456 struct i2c_client *client = to_i2c_client(dev);
0457 struct ili210x *priv = i2c_get_clientdata(client);
0458 int error;
0459
0460 if (!priv->chip->has_firmware_proto)
0461 return 0;
0462
0463
0464 msleep(200);
0465
0466
0467 error = ili251x_firmware_update_resolution(dev);
0468 if (error)
0469 return error;
0470
0471 error = ili251x_firmware_update_firmware_version(dev);
0472 if (error)
0473 return error;
0474
0475 error = ili251x_firmware_update_kernel_version(dev);
0476 if (error)
0477 return error;
0478
0479 error = ili251x_firmware_update_protocol_version(dev);
0480 if (error)
0481 return error;
0482
0483 error = ili251x_firmware_update_ic_mode(dev);
0484 if (error)
0485 return error;
0486
0487 return 0;
0488 }
0489
0490 static ssize_t ili251x_firmware_version_show(struct device *dev,
0491 struct device_attribute *attr,
0492 char *buf)
0493 {
0494 struct i2c_client *client = to_i2c_client(dev);
0495 struct ili210x *priv = i2c_get_clientdata(client);
0496 u8 *fw = priv->version_firmware;
0497
0498 return sysfs_emit(buf, "%02x%02x.%02x%02x.%02x%02x.%02x%02x\n",
0499 fw[0], fw[1], fw[2], fw[3],
0500 fw[4], fw[5], fw[6], fw[7]);
0501 }
0502 static DEVICE_ATTR(firmware_version, 0444, ili251x_firmware_version_show, NULL);
0503
0504 static ssize_t ili251x_kernel_version_show(struct device *dev,
0505 struct device_attribute *attr,
0506 char *buf)
0507 {
0508 struct i2c_client *client = to_i2c_client(dev);
0509 struct ili210x *priv = i2c_get_clientdata(client);
0510 u8 *kv = priv->version_kernel;
0511
0512 return sysfs_emit(buf, "%02x.%02x.%02x.%02x.%02x\n",
0513 kv[0], kv[1], kv[2], kv[3], kv[4]);
0514 }
0515 static DEVICE_ATTR(kernel_version, 0444, ili251x_kernel_version_show, NULL);
0516
0517 static ssize_t ili251x_protocol_version_show(struct device *dev,
0518 struct device_attribute *attr,
0519 char *buf)
0520 {
0521 struct i2c_client *client = to_i2c_client(dev);
0522 struct ili210x *priv = i2c_get_clientdata(client);
0523 u8 *pv = priv->version_proto;
0524
0525 return sysfs_emit(buf, "%02x.%02x\n", pv[0], pv[1]);
0526 }
0527 static DEVICE_ATTR(protocol_version, 0444, ili251x_protocol_version_show, NULL);
0528
0529 static ssize_t ili251x_mode_show(struct device *dev,
0530 struct device_attribute *attr, char *buf)
0531 {
0532 struct i2c_client *client = to_i2c_client(dev);
0533 struct ili210x *priv = i2c_get_clientdata(client);
0534 u8 *md = priv->ic_mode;
0535 char *mode = "AP";
0536
0537 if (md[0] == REG_GET_MODE_AP)
0538 mode = "AP";
0539 else if (md[0] == REG_GET_MODE_BL)
0540 mode = "BL";
0541 else
0542 mode = "??";
0543
0544 return sysfs_emit(buf, "%02x.%02x:%s\n", md[0], md[1], mode);
0545 }
0546 static DEVICE_ATTR(mode, 0444, ili251x_mode_show, NULL);
0547
0548 static ssize_t ili210x_calibrate(struct device *dev,
0549 struct device_attribute *attr,
0550 const char *buf, size_t count)
0551 {
0552 struct i2c_client *client = to_i2c_client(dev);
0553 struct ili210x *priv = i2c_get_clientdata(client);
0554 unsigned long calibrate;
0555 int rc;
0556 u8 cmd = REG_CALIBRATE;
0557
0558 if (kstrtoul(buf, 10, &calibrate))
0559 return -EINVAL;
0560
0561 if (calibrate > 1)
0562 return -EINVAL;
0563
0564 if (calibrate) {
0565 rc = i2c_master_send(priv->client, &cmd, sizeof(cmd));
0566 if (rc != sizeof(cmd))
0567 return -EIO;
0568 }
0569
0570 return count;
0571 }
0572 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate);
0573
0574 static int ili251x_firmware_to_buffer(const struct firmware *fw,
0575 u8 **buf, u16 *ac_end, u16 *df_end)
0576 {
0577 const struct ihex_binrec *rec;
0578 u32 fw_addr, fw_last_addr = 0;
0579 u16 fw_len;
0580 u8 *fw_buf;
0581 int error;
0582
0583
0584
0585
0586
0587
0588
0589 fw_buf = kzalloc(SZ_64K, GFP_KERNEL);
0590 if (!fw_buf)
0591 return -ENOMEM;
0592
0593 rec = (const struct ihex_binrec *)fw->data;
0594 while (rec) {
0595 fw_addr = be32_to_cpu(rec->addr);
0596 fw_len = be16_to_cpu(rec->len);
0597
0598
0599 if (fw_addr + fw_len > SZ_64K || fw_addr > SZ_64K - 32) {
0600 error = -EFBIG;
0601 goto err_big;
0602 }
0603
0604
0605 if (fw_addr == 0xf000)
0606 *ac_end = fw_last_addr;
0607 fw_last_addr = fw_addr + fw_len;
0608
0609 memcpy(fw_buf + fw_addr, rec->data, fw_len);
0610 rec = ihex_next_binrec(rec);
0611 }
0612
0613
0614 *df_end = fw_addr + fw_len;
0615 *buf = fw_buf;
0616 return 0;
0617
0618 err_big:
0619 kfree(fw_buf);
0620 return error;
0621 }
0622
0623
0624 static int ili251x_switch_ic_mode(struct i2c_client *client, u8 cmd_mode)
0625 {
0626 struct ili210x *priv = i2c_get_clientdata(client);
0627 u8 cmd_wren[3] = { REG_WRITE_ENABLE, 0x5a, 0xa5 };
0628 u8 md[2];
0629 int error;
0630
0631 error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md));
0632 if (error)
0633 return error;
0634
0635 if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) ||
0636 (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL))
0637 return 0;
0638
0639
0640 error = i2c_master_send(client, cmd_wren, sizeof(cmd_wren));
0641 if (error != sizeof(cmd_wren))
0642 return -EINVAL;
0643
0644 mdelay(20);
0645
0646
0647 error = i2c_master_send(client, &cmd_mode, 1);
0648 if (error != 1)
0649 return -EINVAL;
0650
0651 mdelay(200);
0652
0653
0654 error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md));
0655 if (error)
0656 return error;
0657
0658 if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) ||
0659 (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL))
0660 return 0;
0661
0662 return -EINVAL;
0663 }
0664
0665 static int ili251x_firmware_busy(struct i2c_client *client)
0666 {
0667 struct ili210x *priv = i2c_get_clientdata(client);
0668 int error, i = 0;
0669 u8 data;
0670
0671 do {
0672
0673 error = priv->chip->read_reg(client, REG_IC_BUSY, &data, 1);
0674 if (error)
0675 return error;
0676 if (i++ == 100000)
0677 return -ETIMEDOUT;
0678 } while (data != REG_IC_BUSY_NOT_BUSY);
0679
0680 return 0;
0681 }
0682
0683 static int ili251x_firmware_write_to_ic(struct device *dev, u8 *fwbuf,
0684 u16 start, u16 end, u8 dataflash)
0685 {
0686 struct i2c_client *client = to_i2c_client(dev);
0687 struct ili210x *priv = i2c_get_clientdata(client);
0688 u8 cmd_crc = REG_READ_DATA_CRC;
0689 u8 crcrb[4] = { 0 };
0690 u8 fw_data[33];
0691 u16 fw_addr;
0692 int error;
0693
0694
0695
0696
0697
0698 u16 crc = crc_ccitt(0, fwbuf + start + (dataflash ? 2 : 0),
0699 end - start - 2);
0700
0701
0702 u8 cmd_wr[10] = {
0703 REG_WRITE_ENABLE, 0x5a, 0xa5, dataflash,
0704 (end >> 16) & 0xff, (end >> 8) & 0xff, end & 0xff,
0705 (crc >> 16) & 0xff, (crc >> 8) & 0xff, crc & 0xff
0706 };
0707
0708 error = i2c_master_send(client, cmd_wr, sizeof(cmd_wr));
0709 if (error != sizeof(cmd_wr))
0710 return -EINVAL;
0711
0712 error = ili251x_firmware_busy(client);
0713 if (error)
0714 return error;
0715
0716 for (fw_addr = start; fw_addr < end; fw_addr += 32) {
0717 fw_data[0] = REG_WRITE_DATA;
0718 memcpy(&(fw_data[1]), fwbuf + fw_addr, 32);
0719 error = i2c_master_send(client, fw_data, 33);
0720 if (error != sizeof(fw_data))
0721 return error;
0722 error = ili251x_firmware_busy(client);
0723 if (error)
0724 return error;
0725 }
0726
0727 error = i2c_master_send(client, &cmd_crc, 1);
0728 if (error != 1)
0729 return -EINVAL;
0730
0731 error = ili251x_firmware_busy(client);
0732 if (error)
0733 return error;
0734
0735 error = priv->chip->read_reg(client, REG_READ_DATA_CRC,
0736 &crcrb, sizeof(crcrb));
0737 if (error)
0738 return error;
0739
0740
0741 if ((crcrb[0] != (crc & 0xff)) || crcrb[1] != ((crc >> 8) & 0xff))
0742 return -EINVAL;
0743
0744 return 0;
0745 }
0746
0747 static int ili251x_firmware_reset(struct i2c_client *client)
0748 {
0749 u8 cmd_reset[2] = { 0xf2, 0x01 };
0750 int error;
0751
0752 error = i2c_master_send(client, cmd_reset, sizeof(cmd_reset));
0753 if (error != sizeof(cmd_reset))
0754 return -EINVAL;
0755
0756 return ili251x_firmware_busy(client);
0757 }
0758
0759 static void ili210x_hardware_reset(struct gpio_desc *reset_gpio)
0760 {
0761
0762 gpiod_set_value_cansleep(reset_gpio, 1);
0763 usleep_range(12000, 15000);
0764 gpiod_set_value_cansleep(reset_gpio, 0);
0765 msleep(300);
0766 }
0767
0768 static ssize_t ili210x_firmware_update_store(struct device *dev,
0769 struct device_attribute *attr,
0770 const char *buf, size_t count)
0771 {
0772 struct i2c_client *client = to_i2c_client(dev);
0773 struct ili210x *priv = i2c_get_clientdata(client);
0774 const char *fwname = ILI251X_FW_FILENAME;
0775 const struct firmware *fw;
0776 u16 ac_end, df_end;
0777 u8 *fwbuf;
0778 int error;
0779 int i;
0780
0781 error = request_ihex_firmware(&fw, fwname, dev);
0782 if (error) {
0783 dev_err(dev, "Failed to request firmware %s, error=%d\n",
0784 fwname, error);
0785 return error;
0786 }
0787
0788 error = ili251x_firmware_to_buffer(fw, &fwbuf, &ac_end, &df_end);
0789 release_firmware(fw);
0790 if (error)
0791 return error;
0792
0793
0794
0795
0796
0797
0798
0799
0800 disable_irq(client->irq);
0801
0802 dev_dbg(dev, "Firmware update started, firmware=%s\n", fwname);
0803
0804 ili210x_hardware_reset(priv->reset_gpio);
0805
0806 error = ili251x_firmware_reset(client);
0807 if (error)
0808 goto exit;
0809
0810
0811 for (i = 0; i < 5; i++) {
0812 error = ili251x_switch_ic_mode(client, REG_SET_MODE_BL);
0813 if (!error)
0814 break;
0815 }
0816
0817 if (error)
0818 goto exit;
0819
0820 dev_dbg(dev, "IC is now in BootLoader mode\n");
0821
0822 msleep(200);
0823
0824 error = ili251x_firmware_write_to_ic(dev, fwbuf, 0xf000, df_end, 1);
0825 if (error) {
0826 dev_err(dev, "DF firmware update failed, error=%d\n", error);
0827 goto exit;
0828 }
0829
0830 dev_dbg(dev, "DataFlash firmware written\n");
0831
0832 error = ili251x_firmware_write_to_ic(dev, fwbuf, 0x2000, ac_end, 0);
0833 if (error) {
0834 dev_err(dev, "AC firmware update failed, error=%d\n", error);
0835 goto exit;
0836 }
0837
0838 dev_dbg(dev, "Application firmware written\n");
0839
0840
0841 for (i = 0; i < 5; i++) {
0842 error = ili251x_switch_ic_mode(client, REG_SET_MODE_AP);
0843 if (!error)
0844 break;
0845 }
0846
0847 if (error)
0848 goto exit;
0849
0850 dev_dbg(dev, "IC is now in Application mode\n");
0851
0852 error = ili251x_firmware_update_cached_state(dev);
0853 if (error)
0854 goto exit;
0855
0856 error = count;
0857
0858 exit:
0859 ili210x_hardware_reset(priv->reset_gpio);
0860 dev_dbg(dev, "Firmware update ended, error=%i\n", error);
0861 enable_irq(client->irq);
0862 kfree(fwbuf);
0863 return error;
0864 }
0865
0866 static DEVICE_ATTR(firmware_update, 0200, NULL, ili210x_firmware_update_store);
0867
0868 static struct attribute *ili210x_attributes[] = {
0869 &dev_attr_calibrate.attr,
0870 &dev_attr_firmware_update.attr,
0871 &dev_attr_firmware_version.attr,
0872 &dev_attr_kernel_version.attr,
0873 &dev_attr_protocol_version.attr,
0874 &dev_attr_mode.attr,
0875 NULL,
0876 };
0877
0878 static umode_t ili210x_attributes_visible(struct kobject *kobj,
0879 struct attribute *attr, int index)
0880 {
0881 struct device *dev = kobj_to_dev(kobj);
0882 struct i2c_client *client = to_i2c_client(dev);
0883 struct ili210x *priv = i2c_get_clientdata(client);
0884
0885
0886 if (attr == &dev_attr_calibrate.attr)
0887 return priv->chip->has_calibrate_reg ? attr->mode : 0;
0888
0889
0890 if (!priv->chip->has_firmware_proto)
0891 return 0;
0892
0893 return attr->mode;
0894 }
0895
0896 static const struct attribute_group ili210x_attr_group = {
0897 .attrs = ili210x_attributes,
0898 .is_visible = ili210x_attributes_visible,
0899 };
0900
0901 static void ili210x_power_down(void *data)
0902 {
0903 struct gpio_desc *reset_gpio = data;
0904
0905 gpiod_set_value_cansleep(reset_gpio, 1);
0906 }
0907
0908 static void ili210x_stop(void *data)
0909 {
0910 struct ili210x *priv = data;
0911
0912
0913 priv->stop = true;
0914 }
0915
0916 static int ili210x_i2c_probe(struct i2c_client *client,
0917 const struct i2c_device_id *id)
0918 {
0919 struct device *dev = &client->dev;
0920 const struct ili2xxx_chip *chip;
0921 struct ili210x *priv;
0922 struct gpio_desc *reset_gpio;
0923 struct input_dev *input;
0924 int error;
0925 unsigned int max_xy;
0926
0927 dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
0928
0929 chip = device_get_match_data(dev);
0930 if (!chip && id)
0931 chip = (const struct ili2xxx_chip *)id->driver_data;
0932 if (!chip) {
0933 dev_err(&client->dev, "unknown device model\n");
0934 return -ENODEV;
0935 }
0936
0937 if (client->irq <= 0) {
0938 dev_err(dev, "No IRQ!\n");
0939 return -EINVAL;
0940 }
0941
0942 reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
0943 if (IS_ERR(reset_gpio))
0944 return PTR_ERR(reset_gpio);
0945
0946 if (reset_gpio) {
0947 error = devm_add_action_or_reset(dev, ili210x_power_down,
0948 reset_gpio);
0949 if (error)
0950 return error;
0951
0952 ili210x_hardware_reset(reset_gpio);
0953 }
0954
0955 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0956 if (!priv)
0957 return -ENOMEM;
0958
0959 input = devm_input_allocate_device(dev);
0960 if (!input)
0961 return -ENOMEM;
0962
0963 priv->client = client;
0964 priv->input = input;
0965 priv->reset_gpio = reset_gpio;
0966 priv->chip = chip;
0967 i2c_set_clientdata(client, priv);
0968
0969
0970 input->name = "ILI210x Touchscreen";
0971 input->id.bustype = BUS_I2C;
0972
0973
0974 max_xy = (chip->resolution ?: SZ_64K) - 1;
0975 input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_xy, 0, 0);
0976 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_xy, 0, 0);
0977 if (priv->chip->has_pressure_reg)
0978 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xa, 0, 0);
0979 error = ili251x_firmware_update_cached_state(dev);
0980 if (error) {
0981 dev_err(dev, "Unable to cache firmware information, err: %d\n",
0982 error);
0983 return error;
0984 }
0985 touchscreen_parse_properties(input, true, &priv->prop);
0986
0987 error = input_mt_init_slots(input, priv->chip->max_touches,
0988 INPUT_MT_DIRECT);
0989 if (error) {
0990 dev_err(dev, "Unable to set up slots, err: %d\n", error);
0991 return error;
0992 }
0993
0994 error = devm_request_threaded_irq(dev, client->irq, NULL, ili210x_irq,
0995 IRQF_ONESHOT, client->name, priv);
0996 if (error) {
0997 dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n",
0998 error);
0999 return error;
1000 }
1001
1002 error = devm_add_action_or_reset(dev, ili210x_stop, priv);
1003 if (error)
1004 return error;
1005
1006 error = devm_device_add_group(dev, &ili210x_attr_group);
1007 if (error) {
1008 dev_err(dev, "Unable to create sysfs attributes, err: %d\n",
1009 error);
1010 return error;
1011 }
1012
1013 error = input_register_device(priv->input);
1014 if (error) {
1015 dev_err(dev, "Cannot register input device, err: %d\n", error);
1016 return error;
1017 }
1018
1019 return 0;
1020 }
1021
1022 static const struct i2c_device_id ili210x_i2c_id[] = {
1023 { "ili210x", (long)&ili210x_chip },
1024 { "ili2117", (long)&ili211x_chip },
1025 { "ili2120", (long)&ili212x_chip },
1026 { "ili251x", (long)&ili251x_chip },
1027 { }
1028 };
1029 MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
1030
1031 static const struct of_device_id ili210x_dt_ids[] = {
1032 { .compatible = "ilitek,ili210x", .data = &ili210x_chip },
1033 { .compatible = "ilitek,ili2117", .data = &ili211x_chip },
1034 { .compatible = "ilitek,ili2120", .data = &ili212x_chip },
1035 { .compatible = "ilitek,ili251x", .data = &ili251x_chip },
1036 { }
1037 };
1038 MODULE_DEVICE_TABLE(of, ili210x_dt_ids);
1039
1040 static struct i2c_driver ili210x_ts_driver = {
1041 .driver = {
1042 .name = "ili210x_i2c",
1043 .of_match_table = ili210x_dt_ids,
1044 },
1045 .id_table = ili210x_i2c_id,
1046 .probe = ili210x_i2c_probe,
1047 };
1048
1049 module_i2c_driver(ili210x_ts_driver);
1050
1051 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1052 MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver");
1053 MODULE_LICENSE("GPL");