1 // SPDX-License-Identifier: GPL-2.0+
3 * Azoteq IQS550/572/525 Trackpad/Touchscreen Controller
6 * Author: Jeff LaBundy <jeff@labundy.com>
8 * These devices require firmware exported from a PC-based configuration tool
9 * made available by the vendor. Firmware files may be pushed to the device's
10 * nonvolatile memory by writing the filename to the 'fw_file' sysfs control.
12 * Link to PC-based configuration tool and data sheet: http://www.azoteq.com/
15 #include <linux/delay.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/firmware.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/i2c.h>
21 #include <linux/input.h>
22 #include <linux/input/mt.h>
23 #include <linux/input/touchscreen.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/of_device.h>
28 #include <linux/slab.h>
29 #include <asm/unaligned.h>
31 #define IQS5XX_FW_FILE_LEN 64
32 #define IQS5XX_NUM_RETRIES 10
33 #define IQS5XX_NUM_POINTS 256
34 #define IQS5XX_NUM_CONTACTS 5
35 #define IQS5XX_WR_BYTES_MAX 2
37 #define IQS5XX_PROD_NUM_IQS550 40
38 #define IQS5XX_PROD_NUM_IQS572 58
39 #define IQS5XX_PROD_NUM_IQS525 52
40 #define IQS5XX_PROJ_NUM_A000 0
41 #define IQS5XX_PROJ_NUM_B000 15
42 #define IQS5XX_MAJOR_VER_MIN 2
44 #define IQS5XX_RESUME 0x00
45 #define IQS5XX_SUSPEND 0x01
47 #define IQS5XX_SW_INPUT_EVENT 0x10
48 #define IQS5XX_SETUP_COMPLETE 0x40
49 #define IQS5XX_EVENT_MODE 0x01
50 #define IQS5XX_TP_EVENT 0x04
52 #define IQS5XX_FLIP_X 0x01
53 #define IQS5XX_FLIP_Y 0x02
54 #define IQS5XX_SWITCH_XY_AXIS 0x04
56 #define IQS5XX_PROD_NUM 0x0000
57 #define IQS5XX_ABS_X 0x0016
58 #define IQS5XX_ABS_Y 0x0018
59 #define IQS5XX_SYS_CTRL0 0x0431
60 #define IQS5XX_SYS_CTRL1 0x0432
61 #define IQS5XX_SYS_CFG0 0x058E
62 #define IQS5XX_SYS_CFG1 0x058F
63 #define IQS5XX_TOTAL_RX 0x063D
64 #define IQS5XX_TOTAL_TX 0x063E
65 #define IQS5XX_XY_CFG0 0x0669
66 #define IQS5XX_X_RES 0x066E
67 #define IQS5XX_Y_RES 0x0670
68 #define IQS5XX_CHKSM 0x83C0
69 #define IQS5XX_APP 0x8400
70 #define IQS5XX_CSTM 0xBE00
71 #define IQS5XX_PMAP_END 0xBFFF
72 #define IQS5XX_END_COMM 0xEEEE
74 #define IQS5XX_CHKSM_LEN (IQS5XX_APP - IQS5XX_CHKSM)
75 #define IQS5XX_APP_LEN (IQS5XX_CSTM - IQS5XX_APP)
76 #define IQS5XX_CSTM_LEN (IQS5XX_PMAP_END + 1 - IQS5XX_CSTM)
77 #define IQS5XX_PMAP_LEN (IQS5XX_PMAP_END + 1 - IQS5XX_CHKSM)
79 #define IQS5XX_REC_HDR_LEN 4
80 #define IQS5XX_REC_LEN_MAX 255
81 #define IQS5XX_REC_TYPE_DATA 0x00
82 #define IQS5XX_REC_TYPE_EOF 0x01
84 #define IQS5XX_BL_ADDR_MASK 0x40
85 #define IQS5XX_BL_CMD_VER 0x00
86 #define IQS5XX_BL_CMD_READ 0x01
87 #define IQS5XX_BL_CMD_EXEC 0x02
88 #define IQS5XX_BL_CMD_CRC 0x03
89 #define IQS5XX_BL_BLK_LEN_MAX 64
90 #define IQS5XX_BL_ID 0x0200
91 #define IQS5XX_BL_STATUS_RESET 0x00
92 #define IQS5XX_BL_STATUS_AVAIL 0xA5
93 #define IQS5XX_BL_STATUS_NONE 0xEE
94 #define IQS5XX_BL_CRC_PASS 0x00
95 #define IQS5XX_BL_CRC_FAIL 0x01
96 #define IQS5XX_BL_ATTEMPTS 3
98 struct iqs5xx_private {
99 struct i2c_client *client;
100 struct input_dev *input;
101 struct gpio_desc *reset_gpio;
106 struct iqs5xx_dev_id_info {
114 struct iqs5xx_ihex_rec {
122 struct iqs5xx_touch_data {
129 static int iqs5xx_read_burst(struct i2c_client *client,
130 u16 reg, void *val, u16 len)
132 __be16 reg_buf = cpu_to_be16(reg);
134 struct i2c_msg msg[] = {
136 .addr = client->addr,
138 .len = sizeof(reg_buf),
139 .buf = (u8 *)®_buf,
142 .addr = client->addr,
150 * The first addressing attempt outside of a communication window fails
151 * and must be retried, after which the device clock stretches until it
154 for (i = 0; i < IQS5XX_NUM_RETRIES; i++) {
155 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
156 if (ret == ARRAY_SIZE(msg))
159 usleep_range(200, 300);
165 dev_err(&client->dev, "Failed to read from address 0x%04X: %d\n",
171 static int iqs5xx_read_word(struct i2c_client *client, u16 reg, u16 *val)
176 error = iqs5xx_read_burst(client, reg, &val_buf, sizeof(val_buf));
180 *val = be16_to_cpu(val_buf);
185 static int iqs5xx_read_byte(struct i2c_client *client, u16 reg, u8 *val)
187 return iqs5xx_read_burst(client, reg, val, sizeof(*val));
190 static int iqs5xx_write_burst(struct i2c_client *client,
191 u16 reg, const void *val, u16 len)
194 u16 mlen = sizeof(reg) + len;
195 u8 mbuf[sizeof(reg) + IQS5XX_WR_BYTES_MAX];
197 if (len > IQS5XX_WR_BYTES_MAX)
200 put_unaligned_be16(reg, mbuf);
201 memcpy(mbuf + sizeof(reg), val, len);
204 * The first addressing attempt outside of a communication window fails
205 * and must be retried, after which the device clock stretches until it
208 for (i = 0; i < IQS5XX_NUM_RETRIES; i++) {
209 ret = i2c_master_send(client, mbuf, mlen);
213 usleep_range(200, 300);
219 dev_err(&client->dev, "Failed to write to address 0x%04X: %d\n",
225 static int iqs5xx_write_word(struct i2c_client *client, u16 reg, u16 val)
227 __be16 val_buf = cpu_to_be16(val);
229 return iqs5xx_write_burst(client, reg, &val_buf, sizeof(val_buf));
232 static int iqs5xx_write_byte(struct i2c_client *client, u16 reg, u8 val)
234 return iqs5xx_write_burst(client, reg, &val, sizeof(val));
237 static void iqs5xx_reset(struct i2c_client *client)
239 struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
241 gpiod_set_value_cansleep(iqs5xx->reset_gpio, 1);
242 usleep_range(200, 300);
244 gpiod_set_value_cansleep(iqs5xx->reset_gpio, 0);
247 static int iqs5xx_bl_cmd(struct i2c_client *client, u8 bl_cmd, u16 bl_addr)
251 u8 mbuf[sizeof(bl_cmd) + sizeof(bl_addr)];
253 msg.addr = client->addr ^ IQS5XX_BL_ADDR_MASK;
255 msg.len = sizeof(bl_cmd);
261 case IQS5XX_BL_CMD_VER:
262 case IQS5XX_BL_CMD_CRC:
263 case IQS5XX_BL_CMD_EXEC:
265 case IQS5XX_BL_CMD_READ:
266 msg.len += sizeof(bl_addr);
267 put_unaligned_be16(bl_addr, mbuf + sizeof(bl_cmd));
273 ret = i2c_transfer(client->adapter, &msg, 1);
278 case IQS5XX_BL_CMD_VER:
279 msg.len = sizeof(u16);
281 case IQS5XX_BL_CMD_CRC:
282 msg.len = sizeof(u8);
284 * This delay saves the bus controller the trouble of having to
285 * tolerate a relatively long clock-stretching period while the
290 case IQS5XX_BL_CMD_EXEC:
291 usleep_range(10000, 10100);
297 msg.flags = I2C_M_RD;
299 ret = i2c_transfer(client->adapter, &msg, 1);
303 if (bl_cmd == IQS5XX_BL_CMD_VER &&
304 get_unaligned_be16(mbuf) != IQS5XX_BL_ID) {
305 dev_err(&client->dev, "Unrecognized bootloader ID: 0x%04X\n",
306 get_unaligned_be16(mbuf));
310 if (bl_cmd == IQS5XX_BL_CMD_CRC && *mbuf != IQS5XX_BL_CRC_PASS) {
311 dev_err(&client->dev, "Bootloader CRC failed\n");
321 if (bl_cmd != IQS5XX_BL_CMD_VER)
322 dev_err(&client->dev,
323 "Unsuccessful bootloader command 0x%02X: %d\n",
329 static int iqs5xx_bl_open(struct i2c_client *client)
334 * The device opens a bootloader polling window for 2 ms following the
335 * release of reset. If the host cannot establish communication during
336 * this time frame, it must cycle reset again.
338 for (i = 0; i < IQS5XX_BL_ATTEMPTS; i++) {
339 iqs5xx_reset(client);
341 for (j = 0; j < IQS5XX_NUM_RETRIES; j++) {
342 error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_VER, 0);
343 if (!error || error == -EINVAL)
348 dev_err(&client->dev, "Failed to open bootloader: %d\n", error);
353 static int iqs5xx_bl_write(struct i2c_client *client,
354 u16 bl_addr, u8 *pmap_data, u16 pmap_len)
358 u8 mbuf[sizeof(bl_addr) + IQS5XX_BL_BLK_LEN_MAX];
360 if (pmap_len % IQS5XX_BL_BLK_LEN_MAX)
363 msg.addr = client->addr ^ IQS5XX_BL_ADDR_MASK;
365 msg.len = sizeof(mbuf);
368 for (i = 0; i < pmap_len; i += IQS5XX_BL_BLK_LEN_MAX) {
369 put_unaligned_be16(bl_addr + i, mbuf);
370 memcpy(mbuf + sizeof(bl_addr), pmap_data + i,
371 sizeof(mbuf) - sizeof(bl_addr));
373 ret = i2c_transfer(client->adapter, &msg, 1);
377 usleep_range(10000, 10100);
386 dev_err(&client->dev, "Failed to write block at address 0x%04X: %d\n",
392 static int iqs5xx_bl_verify(struct i2c_client *client,
393 u16 bl_addr, u8 *pmap_data, u16 pmap_len)
397 u8 bl_data[IQS5XX_BL_BLK_LEN_MAX];
399 if (pmap_len % IQS5XX_BL_BLK_LEN_MAX)
402 msg.addr = client->addr ^ IQS5XX_BL_ADDR_MASK;
403 msg.flags = I2C_M_RD;
404 msg.len = sizeof(bl_data);
407 for (i = 0; i < pmap_len; i += IQS5XX_BL_BLK_LEN_MAX) {
408 ret = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_READ, bl_addr + i);
412 ret = i2c_transfer(client->adapter, &msg, 1);
416 if (memcmp(bl_data, pmap_data + i, sizeof(bl_data))) {
417 dev_err(&client->dev,
418 "Failed to verify block at address 0x%04X\n",
430 dev_err(&client->dev, "Failed to read block at address 0x%04X: %d\n",
436 static int iqs5xx_set_state(struct i2c_client *client, u8 state)
438 struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
441 if (iqs5xx->bl_status == IQS5XX_BL_STATUS_RESET)
444 mutex_lock(&iqs5xx->lock);
447 * Addressing the device outside of a communication window prompts it
448 * to assert the RDY output, so disable the interrupt line to prevent
449 * the handler from servicing a false interrupt.
451 disable_irq(client->irq);
453 error1 = iqs5xx_write_byte(client, IQS5XX_SYS_CTRL1, state);
454 error2 = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0);
456 usleep_range(50, 100);
457 enable_irq(client->irq);
459 mutex_unlock(&iqs5xx->lock);
467 static int iqs5xx_open(struct input_dev *input)
469 struct iqs5xx_private *iqs5xx = input_get_drvdata(input);
471 return iqs5xx_set_state(iqs5xx->client, IQS5XX_RESUME);
474 static void iqs5xx_close(struct input_dev *input)
476 struct iqs5xx_private *iqs5xx = input_get_drvdata(input);
478 iqs5xx_set_state(iqs5xx->client, IQS5XX_SUSPEND);
481 static int iqs5xx_axis_init(struct i2c_client *client)
483 struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
484 struct touchscreen_properties prop;
485 struct input_dev *input;
491 if (!iqs5xx->input) {
492 input = devm_input_allocate_device(&client->dev);
496 input->name = client->name;
497 input->id.bustype = BUS_I2C;
498 input->open = iqs5xx_open;
499 input->close = iqs5xx_close;
501 input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
502 input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
503 input_set_capability(input, EV_ABS, ABS_MT_PRESSURE);
505 input_set_drvdata(input, iqs5xx);
506 iqs5xx->input = input;
509 touchscreen_parse_properties(iqs5xx->input, true, &prop);
511 error = iqs5xx_read_byte(client, IQS5XX_TOTAL_RX, &val);
514 max_x_hw = (val - 1) * IQS5XX_NUM_POINTS;
516 error = iqs5xx_read_byte(client, IQS5XX_TOTAL_TX, &val);
519 max_y_hw = (val - 1) * IQS5XX_NUM_POINTS;
521 error = iqs5xx_read_byte(client, IQS5XX_XY_CFG0, &val);
525 if (val & IQS5XX_SWITCH_XY_AXIS)
526 swap(max_x_hw, max_y_hw);
529 val ^= IQS5XX_SWITCH_XY_AXIS;
532 val ^= prop.swap_x_y ? IQS5XX_FLIP_Y : IQS5XX_FLIP_X;
535 val ^= prop.swap_x_y ? IQS5XX_FLIP_X : IQS5XX_FLIP_Y;
537 error = iqs5xx_write_byte(client, IQS5XX_XY_CFG0, val);
541 if (prop.max_x > max_x_hw) {
542 dev_err(&client->dev, "Invalid maximum x-coordinate: %u > %u\n",
543 prop.max_x, max_x_hw);
545 } else if (prop.max_x == 0) {
546 error = iqs5xx_read_word(client, IQS5XX_X_RES, &max_x);
550 input_abs_set_max(iqs5xx->input,
551 prop.swap_x_y ? ABS_MT_POSITION_Y :
555 max_x = (u16)prop.max_x;
558 if (prop.max_y > max_y_hw) {
559 dev_err(&client->dev, "Invalid maximum y-coordinate: %u > %u\n",
560 prop.max_y, max_y_hw);
562 } else if (prop.max_y == 0) {
563 error = iqs5xx_read_word(client, IQS5XX_Y_RES, &max_y);
567 input_abs_set_max(iqs5xx->input,
568 prop.swap_x_y ? ABS_MT_POSITION_X :
572 max_y = (u16)prop.max_y;
576 * Write horizontal and vertical resolution to the device in case its
577 * original defaults were overridden or swapped as per the properties
578 * specified in the device tree.
580 error = iqs5xx_write_word(client,
581 prop.swap_x_y ? IQS5XX_Y_RES : IQS5XX_X_RES,
586 error = iqs5xx_write_word(client,
587 prop.swap_x_y ? IQS5XX_X_RES : IQS5XX_Y_RES,
592 error = input_mt_init_slots(iqs5xx->input, IQS5XX_NUM_CONTACTS,
595 dev_err(&client->dev, "Failed to initialize slots: %d\n",
601 static int iqs5xx_dev_init(struct i2c_client *client)
603 struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
604 struct iqs5xx_dev_id_info *dev_id_info;
607 u8 buf[sizeof(*dev_id_info) + 1];
609 error = iqs5xx_read_burst(client, IQS5XX_PROD_NUM,
610 &buf[1], sizeof(*dev_id_info));
612 return iqs5xx_bl_open(client);
615 * A000 and B000 devices use 8-bit and 16-bit addressing, respectively.
616 * Querying an A000 device's version information with 16-bit addressing
617 * gives the appearance that the data is shifted by one byte; a nonzero
618 * leading array element suggests this could be the case (in which case
619 * the missing zero is prepended).
622 dev_id_info = (struct iqs5xx_dev_id_info *)&buf[(buf[1] > 0) ? 0 : 1];
624 switch (be16_to_cpu(dev_id_info->prod_num)) {
625 case IQS5XX_PROD_NUM_IQS550:
626 case IQS5XX_PROD_NUM_IQS572:
627 case IQS5XX_PROD_NUM_IQS525:
630 dev_err(&client->dev, "Unrecognized product number: %u\n",
631 be16_to_cpu(dev_id_info->prod_num));
635 switch (be16_to_cpu(dev_id_info->proj_num)) {
636 case IQS5XX_PROJ_NUM_A000:
637 dev_err(&client->dev, "Unsupported project number: %u\n",
638 be16_to_cpu(dev_id_info->proj_num));
639 return iqs5xx_bl_open(client);
640 case IQS5XX_PROJ_NUM_B000:
643 dev_err(&client->dev, "Unrecognized project number: %u\n",
644 be16_to_cpu(dev_id_info->proj_num));
648 if (dev_id_info->major_ver < IQS5XX_MAJOR_VER_MIN) {
649 dev_err(&client->dev, "Unsupported major version: %u\n",
650 dev_id_info->major_ver);
651 return iqs5xx_bl_open(client);
654 switch (dev_id_info->bl_status) {
655 case IQS5XX_BL_STATUS_AVAIL:
656 case IQS5XX_BL_STATUS_NONE:
659 dev_err(&client->dev,
660 "Unrecognized bootloader status: 0x%02X\n",
661 dev_id_info->bl_status);
665 error = iqs5xx_axis_init(client);
669 error = iqs5xx_read_byte(client, IQS5XX_SYS_CFG0, &val);
673 val |= IQS5XX_SETUP_COMPLETE;
674 val &= ~IQS5XX_SW_INPUT_EVENT;
675 error = iqs5xx_write_byte(client, IQS5XX_SYS_CFG0, val);
679 val = IQS5XX_TP_EVENT | IQS5XX_EVENT_MODE;
680 error = iqs5xx_write_byte(client, IQS5XX_SYS_CFG1, val);
684 error = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0);
688 iqs5xx->bl_status = dev_id_info->bl_status;
691 * Closure of the first communication window that appears following the
692 * release of reset appears to kick off an initialization period during
693 * which further communication is met with clock stretching. The return
694 * from this function is delayed so that further communication attempts
702 static irqreturn_t iqs5xx_irq(int irq, void *data)
704 struct iqs5xx_private *iqs5xx = data;
705 struct iqs5xx_touch_data touch_data[IQS5XX_NUM_CONTACTS];
706 struct i2c_client *client = iqs5xx->client;
707 struct input_dev *input = iqs5xx->input;
711 * This check is purely a precaution, as the device does not assert the
712 * RDY output during bootloader mode. If the device operates outside of
713 * bootloader mode, the input device is guaranteed to be allocated.
715 if (iqs5xx->bl_status == IQS5XX_BL_STATUS_RESET)
718 error = iqs5xx_read_burst(client, IQS5XX_ABS_X,
719 touch_data, sizeof(touch_data));
723 for (i = 0; i < ARRAY_SIZE(touch_data); i++) {
724 u16 pressure = be16_to_cpu(touch_data[i].strength);
726 input_mt_slot(input, i);
727 if (input_mt_report_slot_state(input, MT_TOOL_FINGER,
729 input_report_abs(input, ABS_MT_POSITION_X,
730 be16_to_cpu(touch_data[i].abs_x));
731 input_report_abs(input, ABS_MT_POSITION_Y,
732 be16_to_cpu(touch_data[i].abs_y));
733 input_report_abs(input, ABS_MT_PRESSURE, pressure);
737 input_mt_sync_frame(input);
740 error = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0);
745 * Once the communication window is closed, a small delay is added to
746 * ensure the device's RDY output has been deasserted by the time the
747 * interrupt handler returns.
749 usleep_range(50, 100);
754 static int iqs5xx_fw_file_parse(struct i2c_client *client,
755 const char *fw_file, u8 *pmap)
757 const struct firmware *fw;
758 struct iqs5xx_ihex_rec *rec;
763 u8 rec_len, rec_type, rec_chksm, chksm;
764 u8 rec_hdr[IQS5XX_REC_HDR_LEN];
765 u8 rec_data[IQS5XX_REC_LEN_MAX];
768 * Firmware exported from the vendor's configuration tool deviates from
769 * standard ihex as follows: (1) the checksum for records corresponding
770 * to user-exported settings is not recalculated, and (2) an address of
771 * 0xFFFF is used for the EOF record.
773 * Because the ihex2fw tool tolerates neither (1) nor (2), the slightly
774 * nonstandard ihex firmware is parsed directly by the driver.
776 error = request_firmware(&fw, fw_file, &client->dev);
778 dev_err(&client->dev, "Failed to request firmware %s: %d\n",
784 if (pos + sizeof(*rec) > fw->size) {
785 dev_err(&client->dev, "Insufficient firmware size\n");
789 rec = (struct iqs5xx_ihex_rec *)(fw->data + pos);
792 if (rec->start != ':') {
793 dev_err(&client->dev, "Invalid start at record %u\n",
799 error = hex2bin(rec_hdr, rec->len, sizeof(rec_hdr));
801 dev_err(&client->dev, "Invalid header at record %u\n",
807 rec_addr = get_unaligned_be16(rec_hdr + sizeof(rec_len));
808 rec_type = *(rec_hdr + sizeof(rec_len) + sizeof(rec_addr));
810 if (pos + rec_len * 2 > fw->size) {
811 dev_err(&client->dev, "Insufficient firmware size\n");
815 pos += (rec_len * 2);
817 error = hex2bin(rec_data, rec->data, rec_len);
819 dev_err(&client->dev, "Invalid data at record %u\n",
824 error = hex2bin(&rec_chksm,
825 rec->data + rec_len * 2, sizeof(rec_chksm));
827 dev_err(&client->dev, "Invalid checksum at record %u\n",
833 for (i = 0; i < sizeof(rec_hdr); i++)
835 for (i = 0; i < rec_len; i++)
836 chksm += rec_data[i];
839 if (chksm != rec_chksm && rec_addr < IQS5XX_CSTM) {
840 dev_err(&client->dev,
841 "Incorrect checksum at record %u\n",
848 case IQS5XX_REC_TYPE_DATA:
849 if (rec_addr < IQS5XX_CHKSM ||
850 rec_addr > IQS5XX_PMAP_END) {
851 dev_err(&client->dev,
852 "Invalid address at record %u\n",
856 memcpy(pmap + rec_addr - IQS5XX_CHKSM,
860 case IQS5XX_REC_TYPE_EOF:
863 dev_err(&client->dev, "Invalid type at record %u\n",
872 while (pos < fw->size) {
873 if (*(fw->data + pos) == ':')
877 } while (rec_type != IQS5XX_REC_TYPE_EOF);
879 release_firmware(fw);
884 static int iqs5xx_fw_file_write(struct i2c_client *client, const char *fw_file)
886 struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
890 if (iqs5xx->bl_status == IQS5XX_BL_STATUS_NONE)
893 pmap = kzalloc(IQS5XX_PMAP_LEN, GFP_KERNEL);
897 error = iqs5xx_fw_file_parse(client, fw_file, pmap);
901 mutex_lock(&iqs5xx->lock);
904 * Disable the interrupt line in case the first attempt(s) to enter the
905 * bootloader don't happen quickly enough, in which case the device may
906 * assert the RDY output until the next attempt.
908 disable_irq(client->irq);
910 iqs5xx->bl_status = IQS5XX_BL_STATUS_RESET;
912 error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_VER, 0);
914 error = iqs5xx_bl_open(client);
919 error = iqs5xx_bl_write(client, IQS5XX_CHKSM, pmap, IQS5XX_PMAP_LEN);
923 error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_CRC, 0);
927 error = iqs5xx_bl_verify(client, IQS5XX_CSTM,
928 pmap + IQS5XX_CHKSM_LEN + IQS5XX_APP_LEN,
933 error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_EXEC, 0);
937 iqs5xx_reset(client);
938 usleep_range(10000, 10100);
941 error = iqs5xx_dev_init(client);
942 if (!error && iqs5xx->bl_status == IQS5XX_BL_STATUS_RESET)
945 enable_irq(client->irq);
947 mutex_unlock(&iqs5xx->lock);
955 static ssize_t fw_file_store(struct device *dev, struct device_attribute *attr,
956 const char *buf, size_t count)
958 struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
959 struct i2c_client *client = iqs5xx->client;
961 bool input_reg = !iqs5xx->input;
962 char fw_file[IQS5XX_FW_FILE_LEN + 1];
968 if (buf[len - 1] == '\n')
971 if (len > IQS5XX_FW_FILE_LEN)
972 return -ENAMETOOLONG;
974 memcpy(fw_file, buf, len);
977 error = iqs5xx_fw_file_write(client, fw_file);
982 * If the input device was not allocated already, it is guaranteed to
983 * be allocated by this point and can finally be registered.
986 error = input_register_device(iqs5xx->input);
988 dev_err(&client->dev,
989 "Failed to register device: %d\n",
998 static DEVICE_ATTR_WO(fw_file);
1000 static struct attribute *iqs5xx_attrs[] = {
1001 &dev_attr_fw_file.attr,
1005 static const struct attribute_group iqs5xx_attr_group = {
1006 .attrs = iqs5xx_attrs,
1009 static int __maybe_unused iqs5xx_suspend(struct device *dev)
1011 struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
1012 struct input_dev *input = iqs5xx->input;
1018 mutex_lock(&input->mutex);
1021 error = iqs5xx_set_state(iqs5xx->client, IQS5XX_SUSPEND);
1023 mutex_unlock(&input->mutex);
1028 static int __maybe_unused iqs5xx_resume(struct device *dev)
1030 struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
1031 struct input_dev *input = iqs5xx->input;
1037 mutex_lock(&input->mutex);
1040 error = iqs5xx_set_state(iqs5xx->client, IQS5XX_RESUME);
1042 mutex_unlock(&input->mutex);
1047 static SIMPLE_DEV_PM_OPS(iqs5xx_pm, iqs5xx_suspend, iqs5xx_resume);
1049 static int iqs5xx_probe(struct i2c_client *client,
1050 const struct i2c_device_id *id)
1052 struct iqs5xx_private *iqs5xx;
1055 iqs5xx = devm_kzalloc(&client->dev, sizeof(*iqs5xx), GFP_KERNEL);
1059 i2c_set_clientdata(client, iqs5xx);
1060 iqs5xx->client = client;
1062 iqs5xx->reset_gpio = devm_gpiod_get(&client->dev,
1063 "reset", GPIOD_OUT_LOW);
1064 if (IS_ERR(iqs5xx->reset_gpio)) {
1065 error = PTR_ERR(iqs5xx->reset_gpio);
1066 dev_err(&client->dev, "Failed to request GPIO: %d\n", error);
1070 mutex_init(&iqs5xx->lock);
1072 iqs5xx_reset(client);
1073 usleep_range(10000, 10100);
1075 error = iqs5xx_dev_init(client);
1079 error = devm_request_threaded_irq(&client->dev, client->irq,
1080 NULL, iqs5xx_irq, IRQF_ONESHOT,
1081 client->name, iqs5xx);
1083 dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
1087 error = devm_device_add_group(&client->dev, &iqs5xx_attr_group);
1089 dev_err(&client->dev, "Failed to add attributes: %d\n", error);
1093 if (iqs5xx->input) {
1094 error = input_register_device(iqs5xx->input);
1096 dev_err(&client->dev,
1097 "Failed to register device: %d\n",
1104 static const struct i2c_device_id iqs5xx_id[] = {
1110 MODULE_DEVICE_TABLE(i2c, iqs5xx_id);
1112 static const struct of_device_id iqs5xx_of_match[] = {
1113 { .compatible = "azoteq,iqs550" },
1114 { .compatible = "azoteq,iqs572" },
1115 { .compatible = "azoteq,iqs525" },
1118 MODULE_DEVICE_TABLE(of, iqs5xx_of_match);
1120 static struct i2c_driver iqs5xx_i2c_driver = {
1123 .of_match_table = iqs5xx_of_match,
1126 .id_table = iqs5xx_id,
1127 .probe = iqs5xx_probe,
1129 module_i2c_driver(iqs5xx_i2c_driver);
1131 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
1132 MODULE_DESCRIPTION("Azoteq IQS550/572/525 Trackpad/Touchscreen Controller");
1133 MODULE_LICENSE("GPL");