Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Azoteq IQS550/572/525 Trackpad/Touchscreen Controller
0004  *
0005  * Copyright (C) 2018 Jeff LaBundy <jeff@labundy.com>
0006  *
0007  * These devices require firmware exported from a PC-based configuration tool
0008  * made available by the vendor. Firmware files may be pushed to the device's
0009  * nonvolatile memory by writing the filename to the 'fw_file' sysfs control.
0010  *
0011  * Link to PC-based configuration tool and datasheet: https://www.azoteq.com/
0012  */
0013 
0014 #include <linux/bits.h>
0015 #include <linux/delay.h>
0016 #include <linux/device.h>
0017 #include <linux/err.h>
0018 #include <linux/firmware.h>
0019 #include <linux/gpio/consumer.h>
0020 #include <linux/i2c.h>
0021 #include <linux/input.h>
0022 #include <linux/input/mt.h>
0023 #include <linux/input/touchscreen.h>
0024 #include <linux/interrupt.h>
0025 #include <linux/kernel.h>
0026 #include <linux/module.h>
0027 #include <linux/of_device.h>
0028 #include <linux/slab.h>
0029 #include <asm/unaligned.h>
0030 
0031 #define IQS5XX_FW_FILE_LEN  64
0032 #define IQS5XX_NUM_RETRIES  10
0033 #define IQS5XX_NUM_CONTACTS 5
0034 #define IQS5XX_WR_BYTES_MAX 2
0035 
0036 #define IQS5XX_PROD_NUM_IQS550  40
0037 #define IQS5XX_PROD_NUM_IQS572  58
0038 #define IQS5XX_PROD_NUM_IQS525  52
0039 
0040 #define IQS5XX_SHOW_RESET   BIT(7)
0041 #define IQS5XX_ACK_RESET    BIT(7)
0042 
0043 #define IQS5XX_SUSPEND      BIT(0)
0044 #define IQS5XX_RESUME       0
0045 
0046 #define IQS5XX_SETUP_COMPLETE   BIT(6)
0047 #define IQS5XX_WDT      BIT(5)
0048 #define IQS5XX_ALP_REATI    BIT(3)
0049 #define IQS5XX_REATI        BIT(2)
0050 
0051 #define IQS5XX_TP_EVENT     BIT(2)
0052 #define IQS5XX_EVENT_MODE   BIT(0)
0053 
0054 #define IQS5XX_PROD_NUM     0x0000
0055 #define IQS5XX_SYS_INFO0    0x000F
0056 #define IQS5XX_SYS_INFO1    0x0010
0057 #define IQS5XX_SYS_CTRL0    0x0431
0058 #define IQS5XX_SYS_CTRL1    0x0432
0059 #define IQS5XX_SYS_CFG0     0x058E
0060 #define IQS5XX_SYS_CFG1     0x058F
0061 #define IQS5XX_X_RES        0x066E
0062 #define IQS5XX_Y_RES        0x0670
0063 #define IQS5XX_EXP_FILE     0x0677
0064 #define IQS5XX_CHKSM        0x83C0
0065 #define IQS5XX_APP      0x8400
0066 #define IQS5XX_CSTM     0xBE00
0067 #define IQS5XX_PMAP_END     0xBFFF
0068 #define IQS5XX_END_COMM     0xEEEE
0069 
0070 #define IQS5XX_CHKSM_LEN    (IQS5XX_APP - IQS5XX_CHKSM)
0071 #define IQS5XX_APP_LEN      (IQS5XX_CSTM - IQS5XX_APP)
0072 #define IQS5XX_CSTM_LEN     (IQS5XX_PMAP_END + 1 - IQS5XX_CSTM)
0073 #define IQS5XX_PMAP_LEN     (IQS5XX_PMAP_END + 1 - IQS5XX_CHKSM)
0074 
0075 #define IQS5XX_REC_HDR_LEN  4
0076 #define IQS5XX_REC_LEN_MAX  255
0077 #define IQS5XX_REC_TYPE_DATA    0x00
0078 #define IQS5XX_REC_TYPE_EOF 0x01
0079 
0080 #define IQS5XX_BL_ADDR_MASK 0x40
0081 #define IQS5XX_BL_CMD_VER   0x00
0082 #define IQS5XX_BL_CMD_READ  0x01
0083 #define IQS5XX_BL_CMD_EXEC  0x02
0084 #define IQS5XX_BL_CMD_CRC   0x03
0085 #define IQS5XX_BL_BLK_LEN_MAX   64
0086 #define IQS5XX_BL_ID        0x0200
0087 #define IQS5XX_BL_STATUS_NONE   0xEE
0088 #define IQS5XX_BL_CRC_PASS  0x00
0089 #define IQS5XX_BL_CRC_FAIL  0x01
0090 #define IQS5XX_BL_ATTEMPTS  3
0091 
0092 struct iqs5xx_dev_id_info {
0093     __be16 prod_num;
0094     __be16 proj_num;
0095     u8 major_ver;
0096     u8 minor_ver;
0097     u8 bl_status;
0098 } __packed;
0099 
0100 struct iqs5xx_ihex_rec {
0101     char start;
0102     char len[2];
0103     char addr[4];
0104     char type[2];
0105     char data[2];
0106 } __packed;
0107 
0108 struct iqs5xx_touch_data {
0109     __be16 abs_x;
0110     __be16 abs_y;
0111     __be16 strength;
0112     u8 area;
0113 } __packed;
0114 
0115 struct iqs5xx_status {
0116     u8 sys_info[2];
0117     u8 num_active;
0118     __be16 rel_x;
0119     __be16 rel_y;
0120     struct iqs5xx_touch_data touch_data[IQS5XX_NUM_CONTACTS];
0121 } __packed;
0122 
0123 struct iqs5xx_private {
0124     struct i2c_client *client;
0125     struct input_dev *input;
0126     struct gpio_desc *reset_gpio;
0127     struct touchscreen_properties prop;
0128     struct mutex lock;
0129     struct iqs5xx_dev_id_info dev_id_info;
0130     u8 exp_file[2];
0131 };
0132 
0133 static int iqs5xx_read_burst(struct i2c_client *client,
0134                  u16 reg, void *val, u16 len)
0135 {
0136     __be16 reg_buf = cpu_to_be16(reg);
0137     int ret, i;
0138     struct i2c_msg msg[] = {
0139         {
0140             .addr = client->addr,
0141             .flags = 0,
0142             .len = sizeof(reg_buf),
0143             .buf = (u8 *)&reg_buf,
0144         },
0145         {
0146             .addr = client->addr,
0147             .flags = I2C_M_RD,
0148             .len = len,
0149             .buf = (u8 *)val,
0150         },
0151     };
0152 
0153     /*
0154      * The first addressing attempt outside of a communication window fails
0155      * and must be retried, after which the device clock stretches until it
0156      * is available.
0157      */
0158     for (i = 0; i < IQS5XX_NUM_RETRIES; i++) {
0159         ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
0160         if (ret == ARRAY_SIZE(msg))
0161             return 0;
0162 
0163         usleep_range(200, 300);
0164     }
0165 
0166     if (ret >= 0)
0167         ret = -EIO;
0168 
0169     dev_err(&client->dev, "Failed to read from address 0x%04X: %d\n",
0170         reg, ret);
0171 
0172     return ret;
0173 }
0174 
0175 static int iqs5xx_read_word(struct i2c_client *client, u16 reg, u16 *val)
0176 {
0177     __be16 val_buf;
0178     int error;
0179 
0180     error = iqs5xx_read_burst(client, reg, &val_buf, sizeof(val_buf));
0181     if (error)
0182         return error;
0183 
0184     *val = be16_to_cpu(val_buf);
0185 
0186     return 0;
0187 }
0188 
0189 static int iqs5xx_write_burst(struct i2c_client *client,
0190                   u16 reg, const void *val, u16 len)
0191 {
0192     int ret, i;
0193     u16 mlen = sizeof(reg) + len;
0194     u8 mbuf[sizeof(reg) + IQS5XX_WR_BYTES_MAX];
0195 
0196     if (len > IQS5XX_WR_BYTES_MAX)
0197         return -EINVAL;
0198 
0199     put_unaligned_be16(reg, mbuf);
0200     memcpy(mbuf + sizeof(reg), val, len);
0201 
0202     /*
0203      * The first addressing attempt outside of a communication window fails
0204      * and must be retried, after which the device clock stretches until it
0205      * is available.
0206      */
0207     for (i = 0; i < IQS5XX_NUM_RETRIES; i++) {
0208         ret = i2c_master_send(client, mbuf, mlen);
0209         if (ret == mlen)
0210             return 0;
0211 
0212         usleep_range(200, 300);
0213     }
0214 
0215     if (ret >= 0)
0216         ret = -EIO;
0217 
0218     dev_err(&client->dev, "Failed to write to address 0x%04X: %d\n",
0219         reg, ret);
0220 
0221     return ret;
0222 }
0223 
0224 static int iqs5xx_write_word(struct i2c_client *client, u16 reg, u16 val)
0225 {
0226     __be16 val_buf = cpu_to_be16(val);
0227 
0228     return iqs5xx_write_burst(client, reg, &val_buf, sizeof(val_buf));
0229 }
0230 
0231 static int iqs5xx_write_byte(struct i2c_client *client, u16 reg, u8 val)
0232 {
0233     return iqs5xx_write_burst(client, reg, &val, sizeof(val));
0234 }
0235 
0236 static void iqs5xx_reset(struct i2c_client *client)
0237 {
0238     struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
0239 
0240     gpiod_set_value_cansleep(iqs5xx->reset_gpio, 1);
0241     usleep_range(200, 300);
0242 
0243     gpiod_set_value_cansleep(iqs5xx->reset_gpio, 0);
0244 }
0245 
0246 static int iqs5xx_bl_cmd(struct i2c_client *client, u8 bl_cmd, u16 bl_addr)
0247 {
0248     struct i2c_msg msg;
0249     int ret;
0250     u8 mbuf[sizeof(bl_cmd) + sizeof(bl_addr)];
0251 
0252     msg.addr = client->addr ^ IQS5XX_BL_ADDR_MASK;
0253     msg.flags = 0;
0254     msg.len = sizeof(bl_cmd);
0255     msg.buf = mbuf;
0256 
0257     *mbuf = bl_cmd;
0258 
0259     switch (bl_cmd) {
0260     case IQS5XX_BL_CMD_VER:
0261     case IQS5XX_BL_CMD_CRC:
0262     case IQS5XX_BL_CMD_EXEC:
0263         break;
0264     case IQS5XX_BL_CMD_READ:
0265         msg.len += sizeof(bl_addr);
0266         put_unaligned_be16(bl_addr, mbuf + sizeof(bl_cmd));
0267         break;
0268     default:
0269         return -EINVAL;
0270     }
0271 
0272     ret = i2c_transfer(client->adapter, &msg, 1);
0273     if (ret != 1)
0274         goto msg_fail;
0275 
0276     switch (bl_cmd) {
0277     case IQS5XX_BL_CMD_VER:
0278         msg.len = sizeof(u16);
0279         break;
0280     case IQS5XX_BL_CMD_CRC:
0281         msg.len = sizeof(u8);
0282         /*
0283          * This delay saves the bus controller the trouble of having to
0284          * tolerate a relatively long clock-stretching period while the
0285          * CRC is calculated.
0286          */
0287         msleep(50);
0288         break;
0289     case IQS5XX_BL_CMD_EXEC:
0290         usleep_range(10000, 10100);
0291         fallthrough;
0292     default:
0293         return 0;
0294     }
0295 
0296     msg.flags = I2C_M_RD;
0297 
0298     ret = i2c_transfer(client->adapter, &msg, 1);
0299     if (ret != 1)
0300         goto msg_fail;
0301 
0302     if (bl_cmd == IQS5XX_BL_CMD_VER &&
0303         get_unaligned_be16(mbuf) != IQS5XX_BL_ID) {
0304         dev_err(&client->dev, "Unrecognized bootloader ID: 0x%04X\n",
0305             get_unaligned_be16(mbuf));
0306         return -EINVAL;
0307     }
0308 
0309     if (bl_cmd == IQS5XX_BL_CMD_CRC && *mbuf != IQS5XX_BL_CRC_PASS) {
0310         dev_err(&client->dev, "Bootloader CRC failed\n");
0311         return -EIO;
0312     }
0313 
0314     return 0;
0315 
0316 msg_fail:
0317     if (ret >= 0)
0318         ret = -EIO;
0319 
0320     if (bl_cmd != IQS5XX_BL_CMD_VER)
0321         dev_err(&client->dev,
0322             "Unsuccessful bootloader command 0x%02X: %d\n",
0323             bl_cmd, ret);
0324 
0325     return ret;
0326 }
0327 
0328 static int iqs5xx_bl_open(struct i2c_client *client)
0329 {
0330     int error, i, j;
0331 
0332     /*
0333      * The device opens a bootloader polling window for 2 ms following the
0334      * release of reset. If the host cannot establish communication during
0335      * this time frame, it must cycle reset again.
0336      */
0337     for (i = 0; i < IQS5XX_BL_ATTEMPTS; i++) {
0338         iqs5xx_reset(client);
0339         usleep_range(350, 400);
0340 
0341         for (j = 0; j < IQS5XX_NUM_RETRIES; j++) {
0342             error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_VER, 0);
0343             if (!error)
0344                 usleep_range(10000, 10100);
0345             else if (error != -EINVAL)
0346                 continue;
0347 
0348             return error;
0349         }
0350     }
0351 
0352     dev_err(&client->dev, "Failed to open bootloader: %d\n", error);
0353 
0354     return error;
0355 }
0356 
0357 static int iqs5xx_bl_write(struct i2c_client *client,
0358                u16 bl_addr, u8 *pmap_data, u16 pmap_len)
0359 {
0360     struct i2c_msg msg;
0361     int ret, i;
0362     u8 mbuf[sizeof(bl_addr) + IQS5XX_BL_BLK_LEN_MAX];
0363 
0364     if (pmap_len % IQS5XX_BL_BLK_LEN_MAX)
0365         return -EINVAL;
0366 
0367     msg.addr = client->addr ^ IQS5XX_BL_ADDR_MASK;
0368     msg.flags = 0;
0369     msg.len = sizeof(mbuf);
0370     msg.buf = mbuf;
0371 
0372     for (i = 0; i < pmap_len; i += IQS5XX_BL_BLK_LEN_MAX) {
0373         put_unaligned_be16(bl_addr + i, mbuf);
0374         memcpy(mbuf + sizeof(bl_addr), pmap_data + i,
0375                sizeof(mbuf) - sizeof(bl_addr));
0376 
0377         ret = i2c_transfer(client->adapter, &msg, 1);
0378         if (ret != 1)
0379             goto msg_fail;
0380 
0381         usleep_range(10000, 10100);
0382     }
0383 
0384     return 0;
0385 
0386 msg_fail:
0387     if (ret >= 0)
0388         ret = -EIO;
0389 
0390     dev_err(&client->dev, "Failed to write block at address 0x%04X: %d\n",
0391         bl_addr + i, ret);
0392 
0393     return ret;
0394 }
0395 
0396 static int iqs5xx_bl_verify(struct i2c_client *client,
0397                 u16 bl_addr, u8 *pmap_data, u16 pmap_len)
0398 {
0399     struct i2c_msg msg;
0400     int ret, i;
0401     u8 bl_data[IQS5XX_BL_BLK_LEN_MAX];
0402 
0403     if (pmap_len % IQS5XX_BL_BLK_LEN_MAX)
0404         return -EINVAL;
0405 
0406     msg.addr = client->addr ^ IQS5XX_BL_ADDR_MASK;
0407     msg.flags = I2C_M_RD;
0408     msg.len = sizeof(bl_data);
0409     msg.buf = bl_data;
0410 
0411     for (i = 0; i < pmap_len; i += IQS5XX_BL_BLK_LEN_MAX) {
0412         ret = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_READ, bl_addr + i);
0413         if (ret)
0414             return ret;
0415 
0416         ret = i2c_transfer(client->adapter, &msg, 1);
0417         if (ret != 1)
0418             goto msg_fail;
0419 
0420         if (memcmp(bl_data, pmap_data + i, sizeof(bl_data))) {
0421             dev_err(&client->dev,
0422                 "Failed to verify block at address 0x%04X\n",
0423                 bl_addr + i);
0424             return -EIO;
0425         }
0426     }
0427 
0428     return 0;
0429 
0430 msg_fail:
0431     if (ret >= 0)
0432         ret = -EIO;
0433 
0434     dev_err(&client->dev, "Failed to read block at address 0x%04X: %d\n",
0435         bl_addr + i, ret);
0436 
0437     return ret;
0438 }
0439 
0440 static int iqs5xx_set_state(struct i2c_client *client, u8 state)
0441 {
0442     struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
0443     int error1, error2;
0444 
0445     if (!iqs5xx->dev_id_info.bl_status)
0446         return 0;
0447 
0448     mutex_lock(&iqs5xx->lock);
0449 
0450     /*
0451      * Addressing the device outside of a communication window prompts it
0452      * to assert the RDY output, so disable the interrupt line to prevent
0453      * the handler from servicing a false interrupt.
0454      */
0455     disable_irq(client->irq);
0456 
0457     error1 = iqs5xx_write_byte(client, IQS5XX_SYS_CTRL1, state);
0458     error2 = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0);
0459 
0460     usleep_range(50, 100);
0461     enable_irq(client->irq);
0462 
0463     mutex_unlock(&iqs5xx->lock);
0464 
0465     if (error1)
0466         return error1;
0467 
0468     return error2;
0469 }
0470 
0471 static int iqs5xx_open(struct input_dev *input)
0472 {
0473     struct iqs5xx_private *iqs5xx = input_get_drvdata(input);
0474 
0475     return iqs5xx_set_state(iqs5xx->client, IQS5XX_RESUME);
0476 }
0477 
0478 static void iqs5xx_close(struct input_dev *input)
0479 {
0480     struct iqs5xx_private *iqs5xx = input_get_drvdata(input);
0481 
0482     iqs5xx_set_state(iqs5xx->client, IQS5XX_SUSPEND);
0483 }
0484 
0485 static int iqs5xx_axis_init(struct i2c_client *client)
0486 {
0487     struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
0488     struct touchscreen_properties *prop = &iqs5xx->prop;
0489     struct input_dev *input = iqs5xx->input;
0490     u16 max_x, max_y;
0491     int error;
0492 
0493     if (!input) {
0494         input = devm_input_allocate_device(&client->dev);
0495         if (!input)
0496             return -ENOMEM;
0497 
0498         input->name = client->name;
0499         input->id.bustype = BUS_I2C;
0500         input->open = iqs5xx_open;
0501         input->close = iqs5xx_close;
0502 
0503         input_set_drvdata(input, iqs5xx);
0504         iqs5xx->input = input;
0505     }
0506 
0507     error = iqs5xx_read_word(client, IQS5XX_X_RES, &max_x);
0508     if (error)
0509         return error;
0510 
0511     error = iqs5xx_read_word(client, IQS5XX_Y_RES, &max_y);
0512     if (error)
0513         return error;
0514 
0515     input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_x, 0, 0);
0516     input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_y, 0, 0);
0517     input_set_abs_params(input, ABS_MT_PRESSURE, 0, U16_MAX, 0, 0);
0518 
0519     touchscreen_parse_properties(input, true, prop);
0520 
0521     /*
0522      * The device reserves 0xFFFF for coordinates that correspond to slots
0523      * which are not in a state of touch.
0524      */
0525     if (prop->max_x >= U16_MAX || prop->max_y >= U16_MAX) {
0526         dev_err(&client->dev, "Invalid touchscreen size: %u*%u\n",
0527             prop->max_x, prop->max_y);
0528         return -EINVAL;
0529     }
0530 
0531     if (prop->max_x != max_x) {
0532         error = iqs5xx_write_word(client, IQS5XX_X_RES, prop->max_x);
0533         if (error)
0534             return error;
0535     }
0536 
0537     if (prop->max_y != max_y) {
0538         error = iqs5xx_write_word(client, IQS5XX_Y_RES, prop->max_y);
0539         if (error)
0540             return error;
0541     }
0542 
0543     error = input_mt_init_slots(input, IQS5XX_NUM_CONTACTS,
0544                     INPUT_MT_DIRECT);
0545     if (error)
0546         dev_err(&client->dev, "Failed to initialize slots: %d\n",
0547             error);
0548 
0549     return error;
0550 }
0551 
0552 static int iqs5xx_dev_init(struct i2c_client *client)
0553 {
0554     struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
0555     struct iqs5xx_dev_id_info *dev_id_info;
0556     int error;
0557     u8 buf[sizeof(*dev_id_info) + 1];
0558 
0559     error = iqs5xx_read_burst(client, IQS5XX_PROD_NUM,
0560                   &buf[1], sizeof(*dev_id_info));
0561     if (error)
0562         return iqs5xx_bl_open(client);
0563 
0564     /*
0565      * A000 and B000 devices use 8-bit and 16-bit addressing, respectively.
0566      * Querying an A000 device's version information with 16-bit addressing
0567      * gives the appearance that the data is shifted by one byte; a nonzero
0568      * leading array element suggests this could be the case (in which case
0569      * the missing zero is prepended).
0570      */
0571     buf[0] = 0;
0572     dev_id_info = (struct iqs5xx_dev_id_info *)&buf[buf[1] ? 0 : 1];
0573 
0574     switch (be16_to_cpu(dev_id_info->prod_num)) {
0575     case IQS5XX_PROD_NUM_IQS550:
0576     case IQS5XX_PROD_NUM_IQS572:
0577     case IQS5XX_PROD_NUM_IQS525:
0578         break;
0579     default:
0580         dev_err(&client->dev, "Unrecognized product number: %u\n",
0581             be16_to_cpu(dev_id_info->prod_num));
0582         return -EINVAL;
0583     }
0584 
0585     /*
0586      * With the product number recognized yet shifted by one byte, open the
0587      * bootloader and wait for user space to convert the A000 device into a
0588      * B000 device via new firmware.
0589      */
0590     if (buf[1]) {
0591         dev_err(&client->dev, "Opening bootloader for A000 device\n");
0592         return iqs5xx_bl_open(client);
0593     }
0594 
0595     error = iqs5xx_read_burst(client, IQS5XX_EXP_FILE,
0596                   iqs5xx->exp_file, sizeof(iqs5xx->exp_file));
0597     if (error)
0598         return error;
0599 
0600     error = iqs5xx_axis_init(client);
0601     if (error)
0602         return error;
0603 
0604     error = iqs5xx_write_byte(client, IQS5XX_SYS_CTRL0, IQS5XX_ACK_RESET);
0605     if (error)
0606         return error;
0607 
0608     error = iqs5xx_write_byte(client, IQS5XX_SYS_CFG0,
0609                   IQS5XX_SETUP_COMPLETE | IQS5XX_WDT |
0610                   IQS5XX_ALP_REATI | IQS5XX_REATI);
0611     if (error)
0612         return error;
0613 
0614     error = iqs5xx_write_byte(client, IQS5XX_SYS_CFG1,
0615                   IQS5XX_TP_EVENT | IQS5XX_EVENT_MODE);
0616     if (error)
0617         return error;
0618 
0619     error = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0);
0620     if (error)
0621         return error;
0622 
0623     iqs5xx->dev_id_info = *dev_id_info;
0624 
0625     /*
0626      * The following delay allows ATI to complete before the open and close
0627      * callbacks are free to elicit I2C communication. Any attempts to read
0628      * from or write to the device during this time may face extended clock
0629      * stretching and prompt the I2C controller to report an error.
0630      */
0631     msleep(250);
0632 
0633     return 0;
0634 }
0635 
0636 static irqreturn_t iqs5xx_irq(int irq, void *data)
0637 {
0638     struct iqs5xx_private *iqs5xx = data;
0639     struct iqs5xx_status status;
0640     struct i2c_client *client = iqs5xx->client;
0641     struct input_dev *input = iqs5xx->input;
0642     int error, i;
0643 
0644     /*
0645      * This check is purely a precaution, as the device does not assert the
0646      * RDY output during bootloader mode. If the device operates outside of
0647      * bootloader mode, the input device is guaranteed to be allocated.
0648      */
0649     if (!iqs5xx->dev_id_info.bl_status)
0650         return IRQ_NONE;
0651 
0652     error = iqs5xx_read_burst(client, IQS5XX_SYS_INFO0,
0653                   &status, sizeof(status));
0654     if (error)
0655         return IRQ_NONE;
0656 
0657     if (status.sys_info[0] & IQS5XX_SHOW_RESET) {
0658         dev_err(&client->dev, "Unexpected device reset\n");
0659 
0660         error = iqs5xx_dev_init(client);
0661         if (error) {
0662             dev_err(&client->dev,
0663                 "Failed to re-initialize device: %d\n", error);
0664             return IRQ_NONE;
0665         }
0666 
0667         return IRQ_HANDLED;
0668     }
0669 
0670     for (i = 0; i < ARRAY_SIZE(status.touch_data); i++) {
0671         struct iqs5xx_touch_data *touch_data = &status.touch_data[i];
0672         u16 pressure = be16_to_cpu(touch_data->strength);
0673 
0674         input_mt_slot(input, i);
0675         if (input_mt_report_slot_state(input, MT_TOOL_FINGER,
0676                            pressure != 0)) {
0677             touchscreen_report_pos(input, &iqs5xx->prop,
0678                            be16_to_cpu(touch_data->abs_x),
0679                            be16_to_cpu(touch_data->abs_y),
0680                            true);
0681             input_report_abs(input, ABS_MT_PRESSURE, pressure);
0682         }
0683     }
0684 
0685     input_mt_sync_frame(input);
0686     input_sync(input);
0687 
0688     error = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0);
0689     if (error)
0690         return IRQ_NONE;
0691 
0692     /*
0693      * Once the communication window is closed, a small delay is added to
0694      * ensure the device's RDY output has been deasserted by the time the
0695      * interrupt handler returns.
0696      */
0697     usleep_range(50, 100);
0698 
0699     return IRQ_HANDLED;
0700 }
0701 
0702 static int iqs5xx_fw_file_parse(struct i2c_client *client,
0703                 const char *fw_file, u8 *pmap)
0704 {
0705     const struct firmware *fw;
0706     struct iqs5xx_ihex_rec *rec;
0707     size_t pos = 0;
0708     int error, i;
0709     u16 rec_num = 1;
0710     u16 rec_addr;
0711     u8 rec_len, rec_type, rec_chksm, chksm;
0712     u8 rec_hdr[IQS5XX_REC_HDR_LEN];
0713     u8 rec_data[IQS5XX_REC_LEN_MAX];
0714 
0715     /*
0716      * Firmware exported from the vendor's configuration tool deviates from
0717      * standard ihex as follows: (1) the checksum for records corresponding
0718      * to user-exported settings is not recalculated, and (2) an address of
0719      * 0xFFFF is used for the EOF record.
0720      *
0721      * Because the ihex2fw tool tolerates neither (1) nor (2), the slightly
0722      * nonstandard ihex firmware is parsed directly by the driver.
0723      */
0724     error = request_firmware(&fw, fw_file, &client->dev);
0725     if (error) {
0726         dev_err(&client->dev, "Failed to request firmware %s: %d\n",
0727             fw_file, error);
0728         return error;
0729     }
0730 
0731     do {
0732         if (pos + sizeof(*rec) > fw->size) {
0733             dev_err(&client->dev, "Insufficient firmware size\n");
0734             error = -EINVAL;
0735             break;
0736         }
0737         rec = (struct iqs5xx_ihex_rec *)(fw->data + pos);
0738         pos += sizeof(*rec);
0739 
0740         if (rec->start != ':') {
0741             dev_err(&client->dev, "Invalid start at record %u\n",
0742                 rec_num);
0743             error = -EINVAL;
0744             break;
0745         }
0746 
0747         error = hex2bin(rec_hdr, rec->len, sizeof(rec_hdr));
0748         if (error) {
0749             dev_err(&client->dev, "Invalid header at record %u\n",
0750                 rec_num);
0751             break;
0752         }
0753 
0754         rec_len = *rec_hdr;
0755         rec_addr = get_unaligned_be16(rec_hdr + sizeof(rec_len));
0756         rec_type = *(rec_hdr + sizeof(rec_len) + sizeof(rec_addr));
0757 
0758         if (pos + rec_len * 2 > fw->size) {
0759             dev_err(&client->dev, "Insufficient firmware size\n");
0760             error = -EINVAL;
0761             break;
0762         }
0763         pos += (rec_len * 2);
0764 
0765         error = hex2bin(rec_data, rec->data, rec_len);
0766         if (error) {
0767             dev_err(&client->dev, "Invalid data at record %u\n",
0768                 rec_num);
0769             break;
0770         }
0771 
0772         error = hex2bin(&rec_chksm,
0773                 rec->data + rec_len * 2, sizeof(rec_chksm));
0774         if (error) {
0775             dev_err(&client->dev, "Invalid checksum at record %u\n",
0776                 rec_num);
0777             break;
0778         }
0779 
0780         chksm = 0;
0781         for (i = 0; i < sizeof(rec_hdr); i++)
0782             chksm += rec_hdr[i];
0783         for (i = 0; i < rec_len; i++)
0784             chksm += rec_data[i];
0785         chksm = ~chksm + 1;
0786 
0787         if (chksm != rec_chksm && rec_addr < IQS5XX_CSTM) {
0788             dev_err(&client->dev,
0789                 "Incorrect checksum at record %u\n",
0790                 rec_num);
0791             error = -EINVAL;
0792             break;
0793         }
0794 
0795         switch (rec_type) {
0796         case IQS5XX_REC_TYPE_DATA:
0797             if (rec_addr < IQS5XX_CHKSM ||
0798                 rec_addr > IQS5XX_PMAP_END) {
0799                 dev_err(&client->dev,
0800                     "Invalid address at record %u\n",
0801                     rec_num);
0802                 error = -EINVAL;
0803             } else {
0804                 memcpy(pmap + rec_addr - IQS5XX_CHKSM,
0805                        rec_data, rec_len);
0806             }
0807             break;
0808         case IQS5XX_REC_TYPE_EOF:
0809             break;
0810         default:
0811             dev_err(&client->dev, "Invalid type at record %u\n",
0812                 rec_num);
0813             error = -EINVAL;
0814         }
0815 
0816         if (error)
0817             break;
0818 
0819         rec_num++;
0820         while (pos < fw->size) {
0821             if (*(fw->data + pos) == ':')
0822                 break;
0823             pos++;
0824         }
0825     } while (rec_type != IQS5XX_REC_TYPE_EOF);
0826 
0827     release_firmware(fw);
0828 
0829     return error;
0830 }
0831 
0832 static int iqs5xx_fw_file_write(struct i2c_client *client, const char *fw_file)
0833 {
0834     struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
0835     int error, error_init = 0;
0836     u8 *pmap;
0837 
0838     pmap = kzalloc(IQS5XX_PMAP_LEN, GFP_KERNEL);
0839     if (!pmap)
0840         return -ENOMEM;
0841 
0842     error = iqs5xx_fw_file_parse(client, fw_file, pmap);
0843     if (error)
0844         goto err_kfree;
0845 
0846     mutex_lock(&iqs5xx->lock);
0847 
0848     /*
0849      * Disable the interrupt line in case the first attempt(s) to enter the
0850      * bootloader don't happen quickly enough, in which case the device may
0851      * assert the RDY output until the next attempt.
0852      */
0853     disable_irq(client->irq);
0854 
0855     iqs5xx->dev_id_info.bl_status = 0;
0856 
0857     error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_VER, 0);
0858     if (error) {
0859         error = iqs5xx_bl_open(client);
0860         if (error)
0861             goto err_reset;
0862     }
0863 
0864     error = iqs5xx_bl_write(client, IQS5XX_CHKSM, pmap, IQS5XX_PMAP_LEN);
0865     if (error)
0866         goto err_reset;
0867 
0868     error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_CRC, 0);
0869     if (error)
0870         goto err_reset;
0871 
0872     error = iqs5xx_bl_verify(client, IQS5XX_CSTM,
0873                  pmap + IQS5XX_CHKSM_LEN + IQS5XX_APP_LEN,
0874                  IQS5XX_CSTM_LEN);
0875 
0876 err_reset:
0877     iqs5xx_reset(client);
0878     usleep_range(15000, 15100);
0879 
0880     error_init = iqs5xx_dev_init(client);
0881     if (!iqs5xx->dev_id_info.bl_status)
0882         error_init = error_init ? : -EINVAL;
0883 
0884     enable_irq(client->irq);
0885 
0886     mutex_unlock(&iqs5xx->lock);
0887 
0888 err_kfree:
0889     kfree(pmap);
0890 
0891     return error ? : error_init;
0892 }
0893 
0894 static ssize_t fw_file_store(struct device *dev,
0895                  struct device_attribute *attr, const char *buf,
0896                  size_t count)
0897 {
0898     struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
0899     struct i2c_client *client = iqs5xx->client;
0900     size_t len = count;
0901     bool input_reg = !iqs5xx->input;
0902     char fw_file[IQS5XX_FW_FILE_LEN + 1];
0903     int error;
0904 
0905     if (!len)
0906         return -EINVAL;
0907 
0908     if (buf[len - 1] == '\n')
0909         len--;
0910 
0911     if (len > IQS5XX_FW_FILE_LEN)
0912         return -ENAMETOOLONG;
0913 
0914     memcpy(fw_file, buf, len);
0915     fw_file[len] = '\0';
0916 
0917     error = iqs5xx_fw_file_write(client, fw_file);
0918     if (error)
0919         return error;
0920 
0921     /*
0922      * If the input device was not allocated already, it is guaranteed to
0923      * be allocated by this point and can finally be registered.
0924      */
0925     if (input_reg) {
0926         error = input_register_device(iqs5xx->input);
0927         if (error) {
0928             dev_err(&client->dev,
0929                 "Failed to register device: %d\n",
0930                 error);
0931             return error;
0932         }
0933     }
0934 
0935     return count;
0936 }
0937 
0938 static ssize_t fw_info_show(struct device *dev,
0939                 struct device_attribute *attr, char *buf)
0940 {
0941     struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
0942 
0943     if (!iqs5xx->dev_id_info.bl_status)
0944         return -ENODATA;
0945 
0946     return scnprintf(buf, PAGE_SIZE, "%u.%u.%u.%u:%u.%u\n",
0947              be16_to_cpu(iqs5xx->dev_id_info.prod_num),
0948              be16_to_cpu(iqs5xx->dev_id_info.proj_num),
0949              iqs5xx->dev_id_info.major_ver,
0950              iqs5xx->dev_id_info.minor_ver,
0951              iqs5xx->exp_file[0], iqs5xx->exp_file[1]);
0952 }
0953 
0954 static DEVICE_ATTR_WO(fw_file);
0955 static DEVICE_ATTR_RO(fw_info);
0956 
0957 static struct attribute *iqs5xx_attrs[] = {
0958     &dev_attr_fw_file.attr,
0959     &dev_attr_fw_info.attr,
0960     NULL,
0961 };
0962 
0963 static umode_t iqs5xx_attr_is_visible(struct kobject *kobj,
0964                       struct attribute *attr, int i)
0965 {
0966     struct device *dev = kobj_to_dev(kobj);
0967     struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
0968 
0969     if (attr == &dev_attr_fw_file.attr &&
0970         (iqs5xx->dev_id_info.bl_status == IQS5XX_BL_STATUS_NONE ||
0971         !iqs5xx->reset_gpio))
0972         return 0;
0973 
0974     return attr->mode;
0975 }
0976 
0977 static const struct attribute_group iqs5xx_attr_group = {
0978     .is_visible = iqs5xx_attr_is_visible,
0979     .attrs = iqs5xx_attrs,
0980 };
0981 
0982 static int __maybe_unused iqs5xx_suspend(struct device *dev)
0983 {
0984     struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
0985     struct input_dev *input = iqs5xx->input;
0986     int error = 0;
0987 
0988     if (!input || device_may_wakeup(dev))
0989         return error;
0990 
0991     mutex_lock(&input->mutex);
0992 
0993     if (input_device_enabled(input))
0994         error = iqs5xx_set_state(iqs5xx->client, IQS5XX_SUSPEND);
0995 
0996     mutex_unlock(&input->mutex);
0997 
0998     return error;
0999 }
1000 
1001 static int __maybe_unused iqs5xx_resume(struct device *dev)
1002 {
1003     struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
1004     struct input_dev *input = iqs5xx->input;
1005     int error = 0;
1006 
1007     if (!input || device_may_wakeup(dev))
1008         return error;
1009 
1010     mutex_lock(&input->mutex);
1011 
1012     if (input_device_enabled(input))
1013         error = iqs5xx_set_state(iqs5xx->client, IQS5XX_RESUME);
1014 
1015     mutex_unlock(&input->mutex);
1016 
1017     return error;
1018 }
1019 
1020 static SIMPLE_DEV_PM_OPS(iqs5xx_pm, iqs5xx_suspend, iqs5xx_resume);
1021 
1022 static int iqs5xx_probe(struct i2c_client *client,
1023             const struct i2c_device_id *id)
1024 {
1025     struct iqs5xx_private *iqs5xx;
1026     int error;
1027 
1028     iqs5xx = devm_kzalloc(&client->dev, sizeof(*iqs5xx), GFP_KERNEL);
1029     if (!iqs5xx)
1030         return -ENOMEM;
1031 
1032     i2c_set_clientdata(client, iqs5xx);
1033     iqs5xx->client = client;
1034 
1035     iqs5xx->reset_gpio = devm_gpiod_get_optional(&client->dev,
1036                              "reset", GPIOD_OUT_LOW);
1037     if (IS_ERR(iqs5xx->reset_gpio)) {
1038         error = PTR_ERR(iqs5xx->reset_gpio);
1039         dev_err(&client->dev, "Failed to request GPIO: %d\n", error);
1040         return error;
1041     }
1042 
1043     mutex_init(&iqs5xx->lock);
1044 
1045     error = iqs5xx_dev_init(client);
1046     if (error)
1047         return error;
1048 
1049     error = devm_request_threaded_irq(&client->dev, client->irq,
1050                       NULL, iqs5xx_irq, IRQF_ONESHOT,
1051                       client->name, iqs5xx);
1052     if (error) {
1053         dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
1054         return error;
1055     }
1056 
1057     error = devm_device_add_group(&client->dev, &iqs5xx_attr_group);
1058     if (error) {
1059         dev_err(&client->dev, "Failed to add attributes: %d\n", error);
1060         return error;
1061     }
1062 
1063     if (iqs5xx->input) {
1064         error = input_register_device(iqs5xx->input);
1065         if (error)
1066             dev_err(&client->dev,
1067                 "Failed to register device: %d\n",
1068                 error);
1069     }
1070 
1071     return error;
1072 }
1073 
1074 static const struct i2c_device_id iqs5xx_id[] = {
1075     { "iqs550", 0 },
1076     { "iqs572", 1 },
1077     { "iqs525", 2 },
1078     { }
1079 };
1080 MODULE_DEVICE_TABLE(i2c, iqs5xx_id);
1081 
1082 static const struct of_device_id iqs5xx_of_match[] = {
1083     { .compatible = "azoteq,iqs550" },
1084     { .compatible = "azoteq,iqs572" },
1085     { .compatible = "azoteq,iqs525" },
1086     { }
1087 };
1088 MODULE_DEVICE_TABLE(of, iqs5xx_of_match);
1089 
1090 static struct i2c_driver iqs5xx_i2c_driver = {
1091     .driver = {
1092         .name       = "iqs5xx",
1093         .of_match_table = iqs5xx_of_match,
1094         .pm     = &iqs5xx_pm,
1095     },
1096     .id_table   = iqs5xx_id,
1097     .probe      = iqs5xx_probe,
1098 };
1099 module_i2c_driver(iqs5xx_i2c_driver);
1100 
1101 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
1102 MODULE_DESCRIPTION("Azoteq IQS550/572/525 Trackpad/Touchscreen Controller");
1103 MODULE_LICENSE("GPL");