1 // SPDX-License-Identifier: GPL-2.0+
3 * Azoteq IQS550/572/525 Trackpad/Touchscreen Controller
5 * Copyright (C) 2018 Jeff LaBundy <jeff@labundy.com>
7 * These devices require firmware exported from a PC-based configuration tool
8 * made available by the vendor. Firmware files may be pushed to the device's
9 * nonvolatile memory by writing the filename to the 'fw_file' sysfs control.
11 * Link to PC-based configuration tool and datasheet: https://www.azoteq.com/
14 #include <linux/bits.h>
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/mod_devicetable.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/unaligned.h>
31 #define IQS5XX_FW_FILE_LEN 64
32 #define IQS5XX_NUM_RETRIES 10
33 #define IQS5XX_NUM_CONTACTS 5
34 #define IQS5XX_WR_BYTES_MAX 2
36 #define IQS5XX_PROD_NUM_IQS550 40
37 #define IQS5XX_PROD_NUM_IQS572 58
38 #define IQS5XX_PROD_NUM_IQS525 52
40 #define IQS5XX_SHOW_RESET BIT(7)
41 #define IQS5XX_ACK_RESET BIT(7)
43 #define IQS5XX_SUSPEND BIT(0)
44 #define IQS5XX_RESUME 0
46 #define IQS5XX_SETUP_COMPLETE BIT(6)
47 #define IQS5XX_WDT BIT(5)
48 #define IQS5XX_ALP_REATI BIT(3)
49 #define IQS5XX_REATI BIT(2)
51 #define IQS5XX_TP_EVENT BIT(2)
52 #define IQS5XX_EVENT_MODE BIT(0)
54 #define IQS5XX_PROD_NUM 0x0000
55 #define IQS5XX_SYS_INFO0 0x000F
56 #define IQS5XX_SYS_INFO1 0x0010
57 #define IQS5XX_SYS_CTRL0 0x0431
58 #define IQS5XX_SYS_CTRL1 0x0432
59 #define IQS5XX_SYS_CFG0 0x058E
60 #define IQS5XX_SYS_CFG1 0x058F
61 #define IQS5XX_X_RES 0x066E
62 #define IQS5XX_Y_RES 0x0670
63 #define IQS5XX_EXP_FILE 0x0677
64 #define IQS5XX_CHKSM 0x83C0
65 #define IQS5XX_APP 0x8400
66 #define IQS5XX_CSTM 0xBE00
67 #define IQS5XX_PMAP_END 0xBFFF
68 #define IQS5XX_END_COMM 0xEEEE
70 #define IQS5XX_CHKSM_LEN (IQS5XX_APP - IQS5XX_CHKSM)
71 #define IQS5XX_APP_LEN (IQS5XX_CSTM - IQS5XX_APP)
72 #define IQS5XX_CSTM_LEN (IQS5XX_PMAP_END + 1 - IQS5XX_CSTM)
73 #define IQS5XX_PMAP_LEN (IQS5XX_PMAP_END + 1 - IQS5XX_CHKSM)
75 #define IQS5XX_REC_HDR_LEN 4
76 #define IQS5XX_REC_LEN_MAX 255
77 #define IQS5XX_REC_TYPE_DATA 0x00
78 #define IQS5XX_REC_TYPE_EOF 0x01
80 #define IQS5XX_BL_ADDR_MASK 0x40
81 #define IQS5XX_BL_CMD_VER 0x00
82 #define IQS5XX_BL_CMD_READ 0x01
83 #define IQS5XX_BL_CMD_EXEC 0x02
84 #define IQS5XX_BL_CMD_CRC 0x03
85 #define IQS5XX_BL_BLK_LEN_MAX 64
86 #define IQS5XX_BL_ID 0x0200
87 #define IQS5XX_BL_STATUS_NONE 0xEE
88 #define IQS5XX_BL_CRC_PASS 0x00
89 #define IQS5XX_BL_CRC_FAIL 0x01
90 #define IQS5XX_BL_ATTEMPTS 3
92 struct iqs5xx_dev_id_info
{
100 struct iqs5xx_ihex_rec
{
108 struct iqs5xx_touch_data
{
115 struct iqs5xx_status
{
120 struct iqs5xx_touch_data touch_data
[IQS5XX_NUM_CONTACTS
];
123 struct iqs5xx_private
{
124 struct i2c_client
*client
;
125 struct input_dev
*input
;
126 struct gpio_desc
*reset_gpio
;
127 struct touchscreen_properties prop
;
129 struct iqs5xx_dev_id_info dev_id_info
;
133 static int iqs5xx_read_burst(struct i2c_client
*client
,
134 u16 reg
, void *val
, u16 len
)
136 __be16 reg_buf
= cpu_to_be16(reg
);
138 struct i2c_msg msg
[] = {
140 .addr
= client
->addr
,
142 .len
= sizeof(reg_buf
),
143 .buf
= (u8
*)®_buf
,
146 .addr
= client
->addr
,
154 * The first addressing attempt outside of a communication window fails
155 * and must be retried, after which the device clock stretches until it
158 for (i
= 0; i
< IQS5XX_NUM_RETRIES
; i
++) {
159 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
160 if (ret
== ARRAY_SIZE(msg
))
163 usleep_range(200, 300);
169 dev_err(&client
->dev
, "Failed to read from address 0x%04X: %d\n",
175 static int iqs5xx_read_word(struct i2c_client
*client
, u16 reg
, u16
*val
)
180 error
= iqs5xx_read_burst(client
, reg
, &val_buf
, sizeof(val_buf
));
184 *val
= be16_to_cpu(val_buf
);
189 static int iqs5xx_write_burst(struct i2c_client
*client
,
190 u16 reg
, const void *val
, u16 len
)
193 u16 mlen
= sizeof(reg
) + len
;
194 u8 mbuf
[sizeof(reg
) + IQS5XX_WR_BYTES_MAX
];
196 if (len
> IQS5XX_WR_BYTES_MAX
)
199 put_unaligned_be16(reg
, mbuf
);
200 memcpy(mbuf
+ sizeof(reg
), val
, len
);
203 * The first addressing attempt outside of a communication window fails
204 * and must be retried, after which the device clock stretches until it
207 for (i
= 0; i
< IQS5XX_NUM_RETRIES
; i
++) {
208 ret
= i2c_master_send(client
, mbuf
, mlen
);
212 usleep_range(200, 300);
218 dev_err(&client
->dev
, "Failed to write to address 0x%04X: %d\n",
224 static int iqs5xx_write_word(struct i2c_client
*client
, u16 reg
, u16 val
)
226 __be16 val_buf
= cpu_to_be16(val
);
228 return iqs5xx_write_burst(client
, reg
, &val_buf
, sizeof(val_buf
));
231 static int iqs5xx_write_byte(struct i2c_client
*client
, u16 reg
, u8 val
)
233 return iqs5xx_write_burst(client
, reg
, &val
, sizeof(val
));
236 static void iqs5xx_reset(struct i2c_client
*client
)
238 struct iqs5xx_private
*iqs5xx
= i2c_get_clientdata(client
);
240 gpiod_set_value_cansleep(iqs5xx
->reset_gpio
, 1);
241 usleep_range(200, 300);
243 gpiod_set_value_cansleep(iqs5xx
->reset_gpio
, 0);
246 static int iqs5xx_bl_cmd(struct i2c_client
*client
, u8 bl_cmd
, u16 bl_addr
)
250 u8 mbuf
[sizeof(bl_cmd
) + sizeof(bl_addr
)];
252 msg
.addr
= client
->addr
^ IQS5XX_BL_ADDR_MASK
;
254 msg
.len
= sizeof(bl_cmd
);
260 case IQS5XX_BL_CMD_VER
:
261 case IQS5XX_BL_CMD_CRC
:
262 case IQS5XX_BL_CMD_EXEC
:
264 case IQS5XX_BL_CMD_READ
:
265 msg
.len
+= sizeof(bl_addr
);
266 put_unaligned_be16(bl_addr
, mbuf
+ sizeof(bl_cmd
));
272 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
277 case IQS5XX_BL_CMD_VER
:
278 msg
.len
= sizeof(u16
);
280 case IQS5XX_BL_CMD_CRC
:
281 msg
.len
= sizeof(u8
);
283 * This delay saves the bus controller the trouble of having to
284 * tolerate a relatively long clock-stretching period while the
289 case IQS5XX_BL_CMD_EXEC
:
290 usleep_range(10000, 10100);
296 msg
.flags
= I2C_M_RD
;
298 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
302 if (bl_cmd
== IQS5XX_BL_CMD_VER
&&
303 get_unaligned_be16(mbuf
) != IQS5XX_BL_ID
) {
304 dev_err(&client
->dev
, "Unrecognized bootloader ID: 0x%04X\n",
305 get_unaligned_be16(mbuf
));
309 if (bl_cmd
== IQS5XX_BL_CMD_CRC
&& *mbuf
!= IQS5XX_BL_CRC_PASS
) {
310 dev_err(&client
->dev
, "Bootloader CRC failed\n");
320 if (bl_cmd
!= IQS5XX_BL_CMD_VER
)
321 dev_err(&client
->dev
,
322 "Unsuccessful bootloader command 0x%02X: %d\n",
328 static int iqs5xx_bl_open(struct i2c_client
*client
)
333 * The device opens a bootloader polling window for 2 ms following the
334 * release of reset. If the host cannot establish communication during
335 * this time frame, it must cycle reset again.
337 for (i
= 0; i
< IQS5XX_BL_ATTEMPTS
; i
++) {
338 iqs5xx_reset(client
);
339 usleep_range(350, 400);
341 for (j
= 0; j
< IQS5XX_NUM_RETRIES
; j
++) {
342 error
= iqs5xx_bl_cmd(client
, IQS5XX_BL_CMD_VER
, 0);
344 usleep_range(10000, 10100);
345 else if (error
!= -EINVAL
)
352 dev_err(&client
->dev
, "Failed to open bootloader: %d\n", error
);
357 static int iqs5xx_bl_write(struct i2c_client
*client
,
358 u16 bl_addr
, u8
*pmap_data
, u16 pmap_len
)
362 u8 mbuf
[sizeof(bl_addr
) + IQS5XX_BL_BLK_LEN_MAX
];
364 if (pmap_len
% IQS5XX_BL_BLK_LEN_MAX
)
367 msg
.addr
= client
->addr
^ IQS5XX_BL_ADDR_MASK
;
369 msg
.len
= sizeof(mbuf
);
372 for (i
= 0; i
< pmap_len
; i
+= IQS5XX_BL_BLK_LEN_MAX
) {
373 put_unaligned_be16(bl_addr
+ i
, mbuf
);
374 memcpy(mbuf
+ sizeof(bl_addr
), pmap_data
+ i
,
375 sizeof(mbuf
) - sizeof(bl_addr
));
377 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
381 usleep_range(10000, 10100);
390 dev_err(&client
->dev
, "Failed to write block at address 0x%04X: %d\n",
396 static int iqs5xx_bl_verify(struct i2c_client
*client
,
397 u16 bl_addr
, u8
*pmap_data
, u16 pmap_len
)
401 u8 bl_data
[IQS5XX_BL_BLK_LEN_MAX
];
403 if (pmap_len
% IQS5XX_BL_BLK_LEN_MAX
)
406 msg
.addr
= client
->addr
^ IQS5XX_BL_ADDR_MASK
;
407 msg
.flags
= I2C_M_RD
;
408 msg
.len
= sizeof(bl_data
);
411 for (i
= 0; i
< pmap_len
; i
+= IQS5XX_BL_BLK_LEN_MAX
) {
412 ret
= iqs5xx_bl_cmd(client
, IQS5XX_BL_CMD_READ
, bl_addr
+ i
);
416 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
420 if (memcmp(bl_data
, pmap_data
+ i
, sizeof(bl_data
))) {
421 dev_err(&client
->dev
,
422 "Failed to verify block at address 0x%04X\n",
434 dev_err(&client
->dev
, "Failed to read block at address 0x%04X: %d\n",
440 static int iqs5xx_set_state(struct i2c_client
*client
, u8 state
)
442 struct iqs5xx_private
*iqs5xx
= i2c_get_clientdata(client
);
445 if (!iqs5xx
->dev_id_info
.bl_status
)
448 mutex_lock(&iqs5xx
->lock
);
451 * Addressing the device outside of a communication window prompts it
452 * to assert the RDY output, so disable the interrupt line to prevent
453 * the handler from servicing a false interrupt.
455 disable_irq(client
->irq
);
457 error1
= iqs5xx_write_byte(client
, IQS5XX_SYS_CTRL1
, state
);
458 error2
= iqs5xx_write_byte(client
, IQS5XX_END_COMM
, 0);
460 usleep_range(50, 100);
461 enable_irq(client
->irq
);
463 mutex_unlock(&iqs5xx
->lock
);
471 static int iqs5xx_open(struct input_dev
*input
)
473 struct iqs5xx_private
*iqs5xx
= input_get_drvdata(input
);
475 return iqs5xx_set_state(iqs5xx
->client
, IQS5XX_RESUME
);
478 static void iqs5xx_close(struct input_dev
*input
)
480 struct iqs5xx_private
*iqs5xx
= input_get_drvdata(input
);
482 iqs5xx_set_state(iqs5xx
->client
, IQS5XX_SUSPEND
);
485 static int iqs5xx_axis_init(struct i2c_client
*client
)
487 struct iqs5xx_private
*iqs5xx
= i2c_get_clientdata(client
);
488 struct touchscreen_properties
*prop
= &iqs5xx
->prop
;
489 struct input_dev
*input
= iqs5xx
->input
;
494 input
= devm_input_allocate_device(&client
->dev
);
498 input
->name
= client
->name
;
499 input
->id
.bustype
= BUS_I2C
;
500 input
->open
= iqs5xx_open
;
501 input
->close
= iqs5xx_close
;
503 input_set_drvdata(input
, iqs5xx
);
504 iqs5xx
->input
= input
;
507 error
= iqs5xx_read_word(client
, IQS5XX_X_RES
, &max_x
);
511 error
= iqs5xx_read_word(client
, IQS5XX_Y_RES
, &max_y
);
515 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, max_x
, 0, 0);
516 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, max_y
, 0, 0);
517 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0, U16_MAX
, 0, 0);
519 touchscreen_parse_properties(input
, true, prop
);
522 * The device reserves 0xFFFF for coordinates that correspond to slots
523 * which are not in a state of touch.
525 if (prop
->max_x
>= U16_MAX
|| prop
->max_y
>= U16_MAX
) {
526 dev_err(&client
->dev
, "Invalid touchscreen size: %u*%u\n",
527 prop
->max_x
, prop
->max_y
);
531 if (prop
->max_x
!= max_x
) {
532 error
= iqs5xx_write_word(client
, IQS5XX_X_RES
, prop
->max_x
);
537 if (prop
->max_y
!= max_y
) {
538 error
= iqs5xx_write_word(client
, IQS5XX_Y_RES
, prop
->max_y
);
543 error
= input_mt_init_slots(input
, IQS5XX_NUM_CONTACTS
,
546 dev_err(&client
->dev
, "Failed to initialize slots: %d\n",
552 static int iqs5xx_dev_init(struct i2c_client
*client
)
554 struct iqs5xx_private
*iqs5xx
= i2c_get_clientdata(client
);
555 struct iqs5xx_dev_id_info
*dev_id_info
;
557 u8 buf
[sizeof(*dev_id_info
) + 1];
559 error
= iqs5xx_read_burst(client
, IQS5XX_PROD_NUM
,
560 &buf
[1], sizeof(*dev_id_info
));
562 return iqs5xx_bl_open(client
);
565 * A000 and B000 devices use 8-bit and 16-bit addressing, respectively.
566 * Querying an A000 device's version information with 16-bit addressing
567 * gives the appearance that the data is shifted by one byte; a nonzero
568 * leading array element suggests this could be the case (in which case
569 * the missing zero is prepended).
572 dev_id_info
= (struct iqs5xx_dev_id_info
*)&buf
[buf
[1] ? 0 : 1];
574 switch (be16_to_cpu(dev_id_info
->prod_num
)) {
575 case IQS5XX_PROD_NUM_IQS550
:
576 case IQS5XX_PROD_NUM_IQS572
:
577 case IQS5XX_PROD_NUM_IQS525
:
580 dev_err(&client
->dev
, "Unrecognized product number: %u\n",
581 be16_to_cpu(dev_id_info
->prod_num
));
586 * With the product number recognized yet shifted by one byte, open the
587 * bootloader and wait for user space to convert the A000 device into a
588 * B000 device via new firmware.
591 dev_err(&client
->dev
, "Opening bootloader for A000 device\n");
592 return iqs5xx_bl_open(client
);
595 error
= iqs5xx_read_burst(client
, IQS5XX_EXP_FILE
,
596 iqs5xx
->exp_file
, sizeof(iqs5xx
->exp_file
));
600 error
= iqs5xx_axis_init(client
);
604 error
= iqs5xx_write_byte(client
, IQS5XX_SYS_CTRL0
, IQS5XX_ACK_RESET
);
608 error
= iqs5xx_write_byte(client
, IQS5XX_SYS_CFG0
,
609 IQS5XX_SETUP_COMPLETE
| IQS5XX_WDT
|
610 IQS5XX_ALP_REATI
| IQS5XX_REATI
);
614 error
= iqs5xx_write_byte(client
, IQS5XX_SYS_CFG1
,
615 IQS5XX_TP_EVENT
| IQS5XX_EVENT_MODE
);
619 error
= iqs5xx_write_byte(client
, IQS5XX_END_COMM
, 0);
623 iqs5xx
->dev_id_info
= *dev_id_info
;
626 * The following delay allows ATI to complete before the open and close
627 * callbacks are free to elicit I2C communication. Any attempts to read
628 * from or write to the device during this time may face extended clock
629 * stretching and prompt the I2C controller to report an error.
636 static irqreturn_t
iqs5xx_irq(int irq
, void *data
)
638 struct iqs5xx_private
*iqs5xx
= data
;
639 struct iqs5xx_status status
;
640 struct i2c_client
*client
= iqs5xx
->client
;
641 struct input_dev
*input
= iqs5xx
->input
;
645 * This check is purely a precaution, as the device does not assert the
646 * RDY output during bootloader mode. If the device operates outside of
647 * bootloader mode, the input device is guaranteed to be allocated.
649 if (!iqs5xx
->dev_id_info
.bl_status
)
652 error
= iqs5xx_read_burst(client
, IQS5XX_SYS_INFO0
,
653 &status
, sizeof(status
));
657 if (status
.sys_info
[0] & IQS5XX_SHOW_RESET
) {
658 dev_err(&client
->dev
, "Unexpected device reset\n");
660 error
= iqs5xx_dev_init(client
);
662 dev_err(&client
->dev
,
663 "Failed to re-initialize device: %d\n", error
);
670 for (i
= 0; i
< ARRAY_SIZE(status
.touch_data
); i
++) {
671 struct iqs5xx_touch_data
*touch_data
= &status
.touch_data
[i
];
672 u16 pressure
= be16_to_cpu(touch_data
->strength
);
674 input_mt_slot(input
, i
);
675 if (input_mt_report_slot_state(input
, MT_TOOL_FINGER
,
677 touchscreen_report_pos(input
, &iqs5xx
->prop
,
678 be16_to_cpu(touch_data
->abs_x
),
679 be16_to_cpu(touch_data
->abs_y
),
681 input_report_abs(input
, ABS_MT_PRESSURE
, pressure
);
685 input_mt_sync_frame(input
);
688 error
= iqs5xx_write_byte(client
, IQS5XX_END_COMM
, 0);
693 * Once the communication window is closed, a small delay is added to
694 * ensure the device's RDY output has been deasserted by the time the
695 * interrupt handler returns.
697 usleep_range(50, 100);
702 static int iqs5xx_fw_file_parse(struct i2c_client
*client
,
703 const char *fw_file
, u8
*pmap
)
705 const struct firmware
*fw
;
706 struct iqs5xx_ihex_rec
*rec
;
711 u8 rec_len
, rec_type
, rec_chksm
, chksm
;
712 u8 rec_hdr
[IQS5XX_REC_HDR_LEN
];
713 u8 rec_data
[IQS5XX_REC_LEN_MAX
];
716 * Firmware exported from the vendor's configuration tool deviates from
717 * standard ihex as follows: (1) the checksum for records corresponding
718 * to user-exported settings is not recalculated, and (2) an address of
719 * 0xFFFF is used for the EOF record.
721 * Because the ihex2fw tool tolerates neither (1) nor (2), the slightly
722 * nonstandard ihex firmware is parsed directly by the driver.
724 error
= request_firmware(&fw
, fw_file
, &client
->dev
);
726 dev_err(&client
->dev
, "Failed to request firmware %s: %d\n",
732 if (pos
+ sizeof(*rec
) > fw
->size
) {
733 dev_err(&client
->dev
, "Insufficient firmware size\n");
737 rec
= (struct iqs5xx_ihex_rec
*)(fw
->data
+ pos
);
740 if (rec
->start
!= ':') {
741 dev_err(&client
->dev
, "Invalid start at record %u\n",
747 error
= hex2bin(rec_hdr
, rec
->len
, sizeof(rec_hdr
));
749 dev_err(&client
->dev
, "Invalid header at record %u\n",
755 rec_addr
= get_unaligned_be16(rec_hdr
+ sizeof(rec_len
));
756 rec_type
= *(rec_hdr
+ sizeof(rec_len
) + sizeof(rec_addr
));
758 if (pos
+ rec_len
* 2 > fw
->size
) {
759 dev_err(&client
->dev
, "Insufficient firmware size\n");
763 pos
+= (rec_len
* 2);
765 error
= hex2bin(rec_data
, rec
->data
, rec_len
);
767 dev_err(&client
->dev
, "Invalid data at record %u\n",
772 error
= hex2bin(&rec_chksm
,
773 rec
->data
+ rec_len
* 2, sizeof(rec_chksm
));
775 dev_err(&client
->dev
, "Invalid checksum at record %u\n",
781 for (i
= 0; i
< sizeof(rec_hdr
); i
++)
783 for (i
= 0; i
< rec_len
; i
++)
784 chksm
+= rec_data
[i
];
787 if (chksm
!= rec_chksm
&& rec_addr
< IQS5XX_CSTM
) {
788 dev_err(&client
->dev
,
789 "Incorrect checksum at record %u\n",
796 case IQS5XX_REC_TYPE_DATA
:
797 if (rec_addr
< IQS5XX_CHKSM
||
798 rec_addr
> IQS5XX_PMAP_END
) {
799 dev_err(&client
->dev
,
800 "Invalid address at record %u\n",
804 memcpy(pmap
+ rec_addr
- IQS5XX_CHKSM
,
808 case IQS5XX_REC_TYPE_EOF
:
811 dev_err(&client
->dev
, "Invalid type at record %u\n",
820 while (pos
< fw
->size
) {
821 if (*(fw
->data
+ pos
) == ':')
825 } while (rec_type
!= IQS5XX_REC_TYPE_EOF
);
827 release_firmware(fw
);
832 static int iqs5xx_fw_file_write(struct i2c_client
*client
, const char *fw_file
)
834 struct iqs5xx_private
*iqs5xx
= i2c_get_clientdata(client
);
835 int error
, error_init
= 0;
838 pmap
= kzalloc(IQS5XX_PMAP_LEN
, GFP_KERNEL
);
842 error
= iqs5xx_fw_file_parse(client
, fw_file
, pmap
);
846 mutex_lock(&iqs5xx
->lock
);
849 * Disable the interrupt line in case the first attempt(s) to enter the
850 * bootloader don't happen quickly enough, in which case the device may
851 * assert the RDY output until the next attempt.
853 disable_irq(client
->irq
);
855 iqs5xx
->dev_id_info
.bl_status
= 0;
857 error
= iqs5xx_bl_cmd(client
, IQS5XX_BL_CMD_VER
, 0);
859 error
= iqs5xx_bl_open(client
);
864 error
= iqs5xx_bl_write(client
, IQS5XX_CHKSM
, pmap
, IQS5XX_PMAP_LEN
);
868 error
= iqs5xx_bl_cmd(client
, IQS5XX_BL_CMD_CRC
, 0);
872 error
= iqs5xx_bl_verify(client
, IQS5XX_CSTM
,
873 pmap
+ IQS5XX_CHKSM_LEN
+ IQS5XX_APP_LEN
,
877 iqs5xx_reset(client
);
878 usleep_range(15000, 15100);
880 error_init
= iqs5xx_dev_init(client
);
881 if (!iqs5xx
->dev_id_info
.bl_status
)
882 error_init
= error_init
? : -EINVAL
;
884 enable_irq(client
->irq
);
886 mutex_unlock(&iqs5xx
->lock
);
891 return error
? : error_init
;
894 static ssize_t
fw_file_store(struct device
*dev
,
895 struct device_attribute
*attr
, const char *buf
,
898 struct iqs5xx_private
*iqs5xx
= dev_get_drvdata(dev
);
899 struct i2c_client
*client
= iqs5xx
->client
;
901 bool input_reg
= !iqs5xx
->input
;
902 char fw_file
[IQS5XX_FW_FILE_LEN
+ 1];
908 if (buf
[len
- 1] == '\n')
911 if (len
> IQS5XX_FW_FILE_LEN
)
912 return -ENAMETOOLONG
;
914 memcpy(fw_file
, buf
, len
);
917 error
= iqs5xx_fw_file_write(client
, fw_file
);
922 * If the input device was not allocated already, it is guaranteed to
923 * be allocated by this point and can finally be registered.
926 error
= input_register_device(iqs5xx
->input
);
928 dev_err(&client
->dev
,
929 "Failed to register device: %d\n",
938 static ssize_t
fw_info_show(struct device
*dev
,
939 struct device_attribute
*attr
, char *buf
)
941 struct iqs5xx_private
*iqs5xx
= dev_get_drvdata(dev
);
943 if (!iqs5xx
->dev_id_info
.bl_status
)
946 return sysfs_emit(buf
, "%u.%u.%u.%u:%u.%u\n",
947 be16_to_cpu(iqs5xx
->dev_id_info
.prod_num
),
948 be16_to_cpu(iqs5xx
->dev_id_info
.proj_num
),
949 iqs5xx
->dev_id_info
.major_ver
,
950 iqs5xx
->dev_id_info
.minor_ver
,
951 iqs5xx
->exp_file
[0], iqs5xx
->exp_file
[1]);
954 static DEVICE_ATTR_WO(fw_file
);
955 static DEVICE_ATTR_RO(fw_info
);
957 static struct attribute
*iqs5xx_attrs
[] = {
958 &dev_attr_fw_file
.attr
,
959 &dev_attr_fw_info
.attr
,
963 static umode_t
iqs5xx_attr_is_visible(struct kobject
*kobj
,
964 struct attribute
*attr
, int i
)
966 struct device
*dev
= kobj_to_dev(kobj
);
967 struct iqs5xx_private
*iqs5xx
= dev_get_drvdata(dev
);
969 if (attr
== &dev_attr_fw_file
.attr
&&
970 (iqs5xx
->dev_id_info
.bl_status
== IQS5XX_BL_STATUS_NONE
||
971 !iqs5xx
->reset_gpio
))
977 static const struct attribute_group iqs5xx_group
= {
978 .is_visible
= iqs5xx_attr_is_visible
,
979 .attrs
= iqs5xx_attrs
,
981 __ATTRIBUTE_GROUPS(iqs5xx
);
983 static int iqs5xx_suspend(struct device
*dev
)
985 struct iqs5xx_private
*iqs5xx
= dev_get_drvdata(dev
);
986 struct input_dev
*input
= iqs5xx
->input
;
989 if (!input
|| device_may_wakeup(dev
))
992 mutex_lock(&input
->mutex
);
994 if (input_device_enabled(input
))
995 error
= iqs5xx_set_state(iqs5xx
->client
, IQS5XX_SUSPEND
);
997 mutex_unlock(&input
->mutex
);
1002 static int iqs5xx_resume(struct device
*dev
)
1004 struct iqs5xx_private
*iqs5xx
= dev_get_drvdata(dev
);
1005 struct input_dev
*input
= iqs5xx
->input
;
1008 if (!input
|| device_may_wakeup(dev
))
1011 mutex_lock(&input
->mutex
);
1013 if (input_device_enabled(input
))
1014 error
= iqs5xx_set_state(iqs5xx
->client
, IQS5XX_RESUME
);
1016 mutex_unlock(&input
->mutex
);
1021 static DEFINE_SIMPLE_DEV_PM_OPS(iqs5xx_pm
, iqs5xx_suspend
, iqs5xx_resume
);
1023 static int iqs5xx_probe(struct i2c_client
*client
)
1025 struct iqs5xx_private
*iqs5xx
;
1028 iqs5xx
= devm_kzalloc(&client
->dev
, sizeof(*iqs5xx
), GFP_KERNEL
);
1032 i2c_set_clientdata(client
, iqs5xx
);
1033 iqs5xx
->client
= client
;
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
);
1043 mutex_init(&iqs5xx
->lock
);
1045 error
= iqs5xx_dev_init(client
);
1049 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1050 NULL
, iqs5xx_irq
, IRQF_ONESHOT
,
1051 client
->name
, iqs5xx
);
1053 dev_err(&client
->dev
, "Failed to request IRQ: %d\n", error
);
1057 if (iqs5xx
->input
) {
1058 error
= input_register_device(iqs5xx
->input
);
1060 dev_err(&client
->dev
,
1061 "Failed to register device: %d\n",
1068 static const struct i2c_device_id iqs5xx_id
[] = {
1074 MODULE_DEVICE_TABLE(i2c
, iqs5xx_id
);
1076 static const struct of_device_id iqs5xx_of_match
[] = {
1077 { .compatible
= "azoteq,iqs550" },
1078 { .compatible
= "azoteq,iqs572" },
1079 { .compatible
= "azoteq,iqs525" },
1082 MODULE_DEVICE_TABLE(of
, iqs5xx_of_match
);
1084 static struct i2c_driver iqs5xx_i2c_driver
= {
1087 .dev_groups
= iqs5xx_groups
,
1088 .of_match_table
= iqs5xx_of_match
,
1089 .pm
= pm_sleep_ptr(&iqs5xx_pm
),
1091 .id_table
= iqs5xx_id
,
1092 .probe
= iqs5xx_probe
,
1094 module_i2c_driver(iqs5xx_i2c_driver
);
1096 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
1097 MODULE_DESCRIPTION("Azoteq IQS550/572/525 Trackpad/Touchscreen Controller");
1098 MODULE_LICENSE("GPL");