1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Azoteq IQS7210A/7211A/E Trackpad/Touchscreen Controller
8 #include <linux/bits.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/input.h>
15 #include <linux/input/mt.h>
16 #include <linux/input/touchscreen.h>
17 #include <linux/interrupt.h>
18 #include <linux/iopoll.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/property.h>
24 #include <linux/slab.h>
25 #include <linux/unaligned.h>
27 #define IQS7211_PROD_NUM 0x00
29 #define IQS7211_EVENT_MASK_ALL GENMASK(14, 8)
30 #define IQS7211_EVENT_MASK_ALP BIT(13)
31 #define IQS7211_EVENT_MASK_BTN BIT(12)
32 #define IQS7211_EVENT_MASK_ATI BIT(11)
33 #define IQS7211_EVENT_MASK_MOVE BIT(10)
34 #define IQS7211_EVENT_MASK_GSTR BIT(9)
35 #define IQS7211_EVENT_MODE BIT(8)
37 #define IQS7211_COMMS_ERROR 0xEEEE
38 #define IQS7211_COMMS_RETRY_MS 50
39 #define IQS7211_COMMS_SLEEP_US 100
40 #define IQS7211_COMMS_TIMEOUT_US (100 * USEC_PER_MSEC)
41 #define IQS7211_RESET_TIMEOUT_MS 150
42 #define IQS7211_START_TIMEOUT_US (1 * USEC_PER_SEC)
44 #define IQS7211_NUM_RETRIES 5
45 #define IQS7211_NUM_CRX 8
46 #define IQS7211_MAX_CTX 13
48 #define IQS7211_MAX_CONTACTS 2
49 #define IQS7211_MAX_CYCLES 21
52 * The following delay is used during instances that must wait for the open-
53 * drain RDY pin to settle. Its value is calculated as 5*R*C, where R and C
54 * represent typical datasheet values of 4.7k and 100 nF, respectively.
56 #define iqs7211_irq_wait() usleep_range(2500, 2600)
64 enum iqs7211_comms_mode {
65 IQS7211_COMMS_MODE_WAIT,
66 IQS7211_COMMS_MODE_FREE,
67 IQS7211_COMMS_MODE_FORCE,
70 struct iqs7211_reg_field_desc {
71 struct list_head list;
77 enum iqs7211_reg_key_id {
80 IQS7211_REG_KEY_TOUCH,
84 IQS7211_REG_KEY_AXIAL_X,
85 IQS7211_REG_KEY_AXIAL_Y,
86 IQS7211_REG_KEY_RESERVED
89 enum iqs7211_reg_grp_id {
97 static const char * const iqs7211_reg_grp_names[IQS7211_NUM_REG_GRPS] = {
98 [IQS7211_REG_GRP_TP] = "trackpad",
99 [IQS7211_REG_GRP_BTN] = "button",
100 [IQS7211_REG_GRP_ALP] = "alp",
103 static const u16 iqs7211_reg_grp_masks[IQS7211_NUM_REG_GRPS] = {
104 [IQS7211_REG_GRP_TP] = IQS7211_EVENT_MASK_GSTR,
105 [IQS7211_REG_GRP_BTN] = IQS7211_EVENT_MASK_BTN,
106 [IQS7211_REG_GRP_ALP] = IQS7211_EVENT_MASK_ALP,
109 struct iqs7211_event_desc {
113 enum iqs7211_reg_grp_id reg_grp;
114 enum iqs7211_reg_key_id reg_key;
117 static const struct iqs7211_event_desc iqs7210a_kp_events[] = {
120 .enable = BIT(13) | BIT(12),
121 .reg_grp = IQS7211_REG_GRP_ALP,
124 .name = "event-prox",
126 .enable = BIT(5) | BIT(4),
127 .reg_grp = IQS7211_REG_GRP_BTN,
128 .reg_key = IQS7211_REG_KEY_PROX,
131 .name = "event-touch",
133 .enable = BIT(5) | BIT(4),
134 .reg_grp = IQS7211_REG_GRP_BTN,
135 .reg_key = IQS7211_REG_KEY_TOUCH,
141 .reg_grp = IQS7211_REG_GRP_TP,
142 .reg_key = IQS7211_REG_KEY_TAP,
145 .name = "event-hold",
148 .reg_grp = IQS7211_REG_GRP_TP,
149 .reg_key = IQS7211_REG_KEY_HOLD,
152 .name = "event-swipe-x-neg",
155 .reg_grp = IQS7211_REG_GRP_TP,
156 .reg_key = IQS7211_REG_KEY_AXIAL_X,
159 .name = "event-swipe-x-pos",
162 .reg_grp = IQS7211_REG_GRP_TP,
163 .reg_key = IQS7211_REG_KEY_AXIAL_X,
166 .name = "event-swipe-y-pos",
169 .reg_grp = IQS7211_REG_GRP_TP,
170 .reg_key = IQS7211_REG_KEY_AXIAL_Y,
173 .name = "event-swipe-y-neg",
176 .reg_grp = IQS7211_REG_GRP_TP,
177 .reg_key = IQS7211_REG_KEY_AXIAL_Y,
181 static const struct iqs7211_event_desc iqs7211a_kp_events[] = {
184 .reg_grp = IQS7211_REG_GRP_ALP,
190 .reg_grp = IQS7211_REG_GRP_TP,
191 .reg_key = IQS7211_REG_KEY_TAP,
194 .name = "event-hold",
197 .reg_grp = IQS7211_REG_GRP_TP,
198 .reg_key = IQS7211_REG_KEY_HOLD,
201 .name = "event-swipe-x-neg",
204 .reg_grp = IQS7211_REG_GRP_TP,
205 .reg_key = IQS7211_REG_KEY_AXIAL_X,
208 .name = "event-swipe-x-pos",
211 .reg_grp = IQS7211_REG_GRP_TP,
212 .reg_key = IQS7211_REG_KEY_AXIAL_X,
215 .name = "event-swipe-y-pos",
218 .reg_grp = IQS7211_REG_GRP_TP,
219 .reg_key = IQS7211_REG_KEY_AXIAL_Y,
222 .name = "event-swipe-y-neg",
225 .reg_grp = IQS7211_REG_GRP_TP,
226 .reg_key = IQS7211_REG_KEY_AXIAL_Y,
230 static const struct iqs7211_event_desc iqs7211e_kp_events[] = {
233 .reg_grp = IQS7211_REG_GRP_ALP,
239 .reg_grp = IQS7211_REG_GRP_TP,
240 .reg_key = IQS7211_REG_KEY_TAP,
243 .name = "event-tap-double",
246 .reg_grp = IQS7211_REG_GRP_TP,
247 .reg_key = IQS7211_REG_KEY_TAP,
250 .name = "event-tap-triple",
253 .reg_grp = IQS7211_REG_GRP_TP,
254 .reg_key = IQS7211_REG_KEY_TAP,
257 .name = "event-hold",
260 .reg_grp = IQS7211_REG_GRP_TP,
261 .reg_key = IQS7211_REG_KEY_HOLD,
264 .name = "event-palm",
267 .reg_grp = IQS7211_REG_GRP_TP,
268 .reg_key = IQS7211_REG_KEY_PALM,
271 .name = "event-swipe-x-pos",
274 .reg_grp = IQS7211_REG_GRP_TP,
275 .reg_key = IQS7211_REG_KEY_AXIAL_X,
278 .name = "event-swipe-x-neg",
281 .reg_grp = IQS7211_REG_GRP_TP,
282 .reg_key = IQS7211_REG_KEY_AXIAL_X,
285 .name = "event-swipe-y-pos",
288 .reg_grp = IQS7211_REG_GRP_TP,
289 .reg_key = IQS7211_REG_KEY_AXIAL_Y,
292 .name = "event-swipe-y-neg",
295 .reg_grp = IQS7211_REG_GRP_TP,
296 .reg_key = IQS7211_REG_KEY_AXIAL_Y,
299 .name = "event-swipe-x-pos-hold",
302 .reg_grp = IQS7211_REG_GRP_TP,
303 .reg_key = IQS7211_REG_KEY_HOLD,
306 .name = "event-swipe-x-neg-hold",
309 .reg_grp = IQS7211_REG_GRP_TP,
310 .reg_key = IQS7211_REG_KEY_HOLD,
313 .name = "event-swipe-y-pos-hold",
316 .reg_grp = IQS7211_REG_GRP_TP,
317 .reg_key = IQS7211_REG_KEY_HOLD,
320 .name = "event-swipe-y-neg-hold",
323 .reg_grp = IQS7211_REG_GRP_TP,
324 .reg_key = IQS7211_REG_KEY_HOLD,
328 struct iqs7211_dev_desc {
333 u16 ati_error[IQS7211_NUM_REG_GRPS];
334 u16 ati_start[IQS7211_NUM_REG_GRPS];
348 u8 kp_enable[IQS7211_NUM_REG_GRPS];
353 const struct iqs7211_event_desc *kp_events;
359 static const struct iqs7211_dev_desc iqs7211_devs[] = {
361 .tp_name = "iqs7210a_trackpad",
362 .kp_name = "iqs7210a_keys",
364 .show_reset = BIT(15),
366 [IQS7211_REG_GRP_TP] = BIT(12),
367 [IQS7211_REG_GRP_BTN] = BIT(0),
368 [IQS7211_REG_GRP_ALP] = BIT(8),
371 [IQS7211_REG_GRP_TP] = BIT(13),
372 [IQS7211_REG_GRP_BTN] = BIT(1),
373 [IQS7211_REG_GRP_ALP] = BIT(9),
389 [IQS7211_REG_GRP_TP] = 0x58,
390 [IQS7211_REG_GRP_BTN] = 0x37,
391 [IQS7211_REG_GRP_ALP] = 0x37,
393 .gesture_angle = 0x5F,
395 .cycle_alloc = { 0x66, 0x75, },
396 .cycle_limit = { 10, 6, },
397 .kp_events = iqs7210a_kp_events,
398 .num_kp_events = ARRAY_SIZE(iqs7210a_kp_events),
400 .num_ctx = IQS7211_MAX_CTX - 1,
403 .tp_name = "iqs7211a_trackpad",
404 .kp_name = "iqs7211a_keys",
406 .show_reset = BIT(7),
408 [IQS7211_REG_GRP_TP] = BIT(3),
409 [IQS7211_REG_GRP_ALP] = BIT(5),
412 [IQS7211_REG_GRP_TP] = BIT(5),
413 [IQS7211_REG_GRP_ALP] = BIT(6),
427 [IQS7211_REG_GRP_TP] = 0x80,
429 .gesture_angle = 0x87,
431 .cycle_alloc = { 0xA0, 0xB0, },
432 .cycle_limit = { 10, 8, },
433 .kp_events = iqs7211a_kp_events,
434 .num_kp_events = ARRAY_SIZE(iqs7211a_kp_events),
435 .num_ctx = IQS7211_MAX_CTX - 1,
438 .tp_name = "iqs7211e_trackpad",
439 .kp_name = "iqs7211e_keys",
441 .show_reset = BIT(7),
443 [IQS7211_REG_GRP_TP] = BIT(3),
444 [IQS7211_REG_GRP_ALP] = BIT(5),
447 [IQS7211_REG_GRP_TP] = BIT(5),
448 [IQS7211_REG_GRP_ALP] = BIT(6),
464 [IQS7211_REG_GRP_TP] = 0x4B,
466 .gesture_angle = 0x55,
468 .cycle_alloc = { 0x5D, 0x6C, },
469 .cycle_limit = { 10, 11, },
470 .kp_events = iqs7211e_kp_events,
471 .num_kp_events = ARRAY_SIZE(iqs7211e_kp_events),
472 .num_ctx = IQS7211_MAX_CTX,
476 struct iqs7211_prop_desc {
478 enum iqs7211_reg_key_id reg_key;
479 u8 reg_addr[IQS7211_NUM_REG_GRPS][ARRAY_SIZE(iqs7211_devs)];
488 static const struct iqs7211_prop_desc iqs7211_props[] = {
490 .name = "azoteq,ati-frac-div-fine",
492 [IQS7211_REG_GRP_TP] = {
497 [IQS7211_REG_GRP_BTN] = {
500 [IQS7211_REG_GRP_ALP] = {
508 .label = "ATI fine fractional divider",
511 .name = "azoteq,ati-frac-mult-coarse",
513 [IQS7211_REG_GRP_TP] = {
518 [IQS7211_REG_GRP_BTN] = {
521 [IQS7211_REG_GRP_ALP] = {
529 .label = "ATI coarse fractional multiplier",
532 .name = "azoteq,ati-frac-div-coarse",
534 [IQS7211_REG_GRP_TP] = {
539 [IQS7211_REG_GRP_BTN] = {
542 [IQS7211_REG_GRP_ALP] = {
550 .label = "ATI coarse fractional divider",
553 .name = "azoteq,ati-comp-div",
555 [IQS7211_REG_GRP_TP] = {
559 [IQS7211_REG_GRP_BTN] = {
562 [IQS7211_REG_GRP_ALP] = {
569 .label = "ATI compensation divider",
572 .name = "azoteq,ati-comp-div",
574 [IQS7211_REG_GRP_ALP] = {
581 .label = "ATI compensation divider",
584 .name = "azoteq,ati-comp-div",
586 [IQS7211_REG_GRP_TP] = {
589 [IQS7211_REG_GRP_ALP] = {
594 .label = "ATI compensation divider",
597 .name = "azoteq,ati-target",
599 [IQS7211_REG_GRP_TP] = {
604 [IQS7211_REG_GRP_BTN] = {
607 [IQS7211_REG_GRP_ALP] = {
613 .label = "ATI target",
616 .name = "azoteq,ati-base",
617 .reg_addr[IQS7211_REG_GRP_ALP] = {
626 .name = "azoteq,ati-base",
627 .reg_addr[IQS7211_REG_GRP_BTN] = {
636 .name = "azoteq,rate-active-ms",
637 .reg_addr[IQS7211_REG_GRP_SYS] = {
642 .label = "active mode report rate",
645 .name = "azoteq,rate-touch-ms",
646 .reg_addr[IQS7211_REG_GRP_SYS] = {
651 .label = "idle-touch mode report rate",
654 .name = "azoteq,rate-idle-ms",
655 .reg_addr[IQS7211_REG_GRP_SYS] = {
660 .label = "idle mode report rate",
663 .name = "azoteq,rate-lp1-ms",
664 .reg_addr[IQS7211_REG_GRP_SYS] = {
669 .label = "low-power mode 1 report rate",
672 .name = "azoteq,rate-lp2-ms",
673 .reg_addr[IQS7211_REG_GRP_SYS] = {
678 .label = "low-power mode 2 report rate",
681 .name = "azoteq,timeout-active-ms",
682 .reg_addr[IQS7211_REG_GRP_SYS] = {
688 .label = "active mode timeout",
691 .name = "azoteq,timeout-touch-ms",
692 .reg_addr[IQS7211_REG_GRP_SYS] = {
698 .label = "idle-touch mode timeout",
701 .name = "azoteq,timeout-idle-ms",
702 .reg_addr[IQS7211_REG_GRP_SYS] = {
708 .label = "idle mode timeout",
711 .name = "azoteq,timeout-lp1-ms",
712 .reg_addr[IQS7211_REG_GRP_SYS] = {
718 .label = "low-power mode 1 timeout",
721 .name = "azoteq,timeout-lp2-ms",
722 .reg_addr[IQS7211_REG_GRP_SYS] = {
730 .label = "trackpad reference value update rate",
733 .name = "azoteq,timeout-lp2-ms",
734 .reg_addr[IQS7211_REG_GRP_SYS] = {
739 .label = "trackpad reference value update rate",
742 .name = "azoteq,timeout-ati-ms",
743 .reg_addr[IQS7211_REG_GRP_SYS] = {
750 .label = "ATI error timeout",
753 .name = "azoteq,timeout-ati-ms",
754 .reg_addr[IQS7211_REG_GRP_SYS] = {
759 .label = "ATI error timeout",
762 .name = "azoteq,timeout-comms-ms",
763 .reg_addr[IQS7211_REG_GRP_SYS] = {
768 .label = "communication timeout",
771 .name = "azoteq,timeout-press-ms",
772 .reg_addr[IQS7211_REG_GRP_SYS] = {
778 .label = "press timeout",
781 .name = "azoteq,ati-mode",
782 .reg_addr[IQS7211_REG_GRP_ALP] = {
790 .name = "azoteq,ati-mode",
791 .reg_addr[IQS7211_REG_GRP_BTN] = {
799 .name = "azoteq,sense-mode",
800 .reg_addr[IQS7211_REG_GRP_ALP] = {
807 .label = "sensing mode",
810 .name = "azoteq,sense-mode",
811 .reg_addr[IQS7211_REG_GRP_BTN] = {
817 .label = "sensing mode",
820 .name = "azoteq,fosc-freq",
821 .reg_addr[IQS7211_REG_GRP_SYS] = {
828 .label = "core clock frequency selection",
831 .name = "azoteq,fosc-trim",
832 .reg_addr[IQS7211_REG_GRP_SYS] = {
839 .label = "core clock frequency trim",
842 .name = "azoteq,touch-exit",
844 [IQS7211_REG_GRP_TP] = {
849 [IQS7211_REG_GRP_BTN] = {
855 .label = "touch exit factor",
858 .name = "azoteq,touch-enter",
860 [IQS7211_REG_GRP_TP] = {
865 [IQS7211_REG_GRP_BTN] = {
871 .label = "touch entrance factor",
874 .name = "azoteq,thresh",
876 [IQS7211_REG_GRP_BTN] = {
879 [IQS7211_REG_GRP_ALP] = {
885 .label = "threshold",
888 .name = "azoteq,debounce-exit",
890 [IQS7211_REG_GRP_BTN] = {
893 [IQS7211_REG_GRP_ALP] = {
901 .label = "debounce exit factor",
904 .name = "azoteq,debounce-enter",
906 [IQS7211_REG_GRP_BTN] = {
909 [IQS7211_REG_GRP_ALP] = {
917 .label = "debounce entrance factor",
920 .name = "azoteq,conv-frac",
922 [IQS7211_REG_GRP_TP] = {
927 [IQS7211_REG_GRP_BTN] = {
930 [IQS7211_REG_GRP_ALP] = {
938 .label = "conversion frequency fractional divider",
941 .name = "azoteq,conv-period",
943 [IQS7211_REG_GRP_TP] = {
948 [IQS7211_REG_GRP_BTN] = {
951 [IQS7211_REG_GRP_ALP] = {
959 .label = "conversion period",
962 .name = "azoteq,thresh",
963 .reg_addr[IQS7211_REG_GRP_TP] = {
970 .label = "threshold",
973 .name = "azoteq,contact-split",
974 .reg_addr[IQS7211_REG_GRP_SYS] = {
981 .label = "contact split factor",
984 .name = "azoteq,trim-x",
985 .reg_addr[IQS7211_REG_GRP_SYS] = {
991 .label = "horizontal trim width",
994 .name = "azoteq,trim-x",
995 .reg_addr[IQS7211_REG_GRP_SYS] = {
998 .label = "horizontal trim width",
1001 .name = "azoteq,trim-y",
1002 .reg_addr[IQS7211_REG_GRP_SYS] = {
1008 .label = "vertical trim height",
1011 .name = "azoteq,trim-y",
1012 .reg_addr[IQS7211_REG_GRP_SYS] = {
1015 .label = "vertical trim height",
1018 .name = "azoteq,gesture-max-ms",
1019 .reg_key = IQS7211_REG_KEY_TAP,
1020 .reg_addr[IQS7211_REG_GRP_TP] = {
1025 .label = "maximum gesture time",
1028 .name = "azoteq,gesture-mid-ms",
1029 .reg_key = IQS7211_REG_KEY_TAP,
1030 .reg_addr[IQS7211_REG_GRP_TP] = {
1033 .label = "repeated gesture time",
1036 .name = "azoteq,gesture-dist",
1037 .reg_key = IQS7211_REG_KEY_TAP,
1038 .reg_addr[IQS7211_REG_GRP_TP] = {
1043 .label = "gesture distance",
1046 .name = "azoteq,gesture-dist",
1047 .reg_key = IQS7211_REG_KEY_HOLD,
1048 .reg_addr[IQS7211_REG_GRP_TP] = {
1053 .label = "gesture distance",
1056 .name = "azoteq,gesture-min-ms",
1057 .reg_key = IQS7211_REG_KEY_HOLD,
1058 .reg_addr[IQS7211_REG_GRP_TP] = {
1063 .label = "minimum gesture time",
1066 .name = "azoteq,gesture-max-ms",
1067 .reg_key = IQS7211_REG_KEY_AXIAL_X,
1068 .reg_addr[IQS7211_REG_GRP_TP] = {
1073 .label = "maximum gesture time",
1076 .name = "azoteq,gesture-max-ms",
1077 .reg_key = IQS7211_REG_KEY_AXIAL_Y,
1078 .reg_addr[IQS7211_REG_GRP_TP] = {
1083 .label = "maximum gesture time",
1086 .name = "azoteq,gesture-dist",
1087 .reg_key = IQS7211_REG_KEY_AXIAL_X,
1088 .reg_addr[IQS7211_REG_GRP_TP] = {
1093 .label = "gesture distance",
1096 .name = "azoteq,gesture-dist",
1097 .reg_key = IQS7211_REG_KEY_AXIAL_Y,
1098 .reg_addr[IQS7211_REG_GRP_TP] = {
1103 .label = "gesture distance",
1106 .name = "azoteq,gesture-dist-rep",
1107 .reg_key = IQS7211_REG_KEY_AXIAL_X,
1108 .reg_addr[IQS7211_REG_GRP_TP] = {
1111 .label = "repeated gesture distance",
1114 .name = "azoteq,gesture-dist-rep",
1115 .reg_key = IQS7211_REG_KEY_AXIAL_Y,
1116 .reg_addr[IQS7211_REG_GRP_TP] = {
1119 .label = "repeated gesture distance",
1122 .name = "azoteq,thresh",
1123 .reg_key = IQS7211_REG_KEY_PALM,
1124 .reg_addr[IQS7211_REG_GRP_TP] = {
1130 .label = "threshold",
1134 static const u8 iqs7211_gesture_angle[] = {
1135 0x00, 0x01, 0x02, 0x03,
1136 0x04, 0x06, 0x07, 0x08,
1137 0x09, 0x0A, 0x0B, 0x0C,
1138 0x0E, 0x0F, 0x10, 0x11,
1139 0x12, 0x14, 0x15, 0x16,
1140 0x17, 0x19, 0x1A, 0x1B,
1141 0x1C, 0x1E, 0x1F, 0x21,
1142 0x22, 0x23, 0x25, 0x26,
1143 0x28, 0x2A, 0x2B, 0x2D,
1144 0x2E, 0x30, 0x32, 0x34,
1145 0x36, 0x38, 0x3A, 0x3C,
1146 0x3E, 0x40, 0x42, 0x45,
1147 0x47, 0x4A, 0x4C, 0x4F,
1148 0x52, 0x55, 0x58, 0x5B,
1149 0x5F, 0x63, 0x66, 0x6B,
1150 0x6F, 0x73, 0x78, 0x7E,
1151 0x83, 0x89, 0x90, 0x97,
1152 0x9E, 0xA7, 0xB0, 0xBA,
1153 0xC5, 0xD1, 0xDF, 0xEF,
1156 struct iqs7211_ver_info {
1163 struct iqs7211_touch_data {
1170 struct iqs7211_tp_config {
1179 struct iqs7211_private {
1180 const struct iqs7211_dev_desc *dev_desc;
1181 struct gpio_desc *reset_gpio;
1182 struct gpio_desc *irq_gpio;
1183 struct i2c_client *client;
1184 struct input_dev *tp_idev;
1185 struct input_dev *kp_idev;
1186 struct iqs7211_ver_info ver_info;
1187 struct iqs7211_tp_config tp_config;
1188 struct touchscreen_properties prop;
1189 struct list_head reg_field_head;
1190 enum iqs7211_comms_mode comms_init;
1191 enum iqs7211_comms_mode comms_mode;
1192 unsigned int num_contacts;
1193 unsigned int kp_code[ARRAY_SIZE(iqs7211e_kp_events)];
1194 u8 rx_tx_map[IQS7211_MAX_CTX + 1];
1195 u8 cycle_alloc[2][33];
1202 static int iqs7211_irq_poll(struct iqs7211_private *iqs7211, u64 timeout_us)
1206 error = readx_poll_timeout(gpiod_get_value_cansleep, iqs7211->irq_gpio,
1207 val, val, IQS7211_COMMS_SLEEP_US, timeout_us);
1209 return val < 0 ? val : error;
1212 static int iqs7211_hard_reset(struct iqs7211_private *iqs7211)
1214 if (!iqs7211->reset_gpio)
1217 gpiod_set_value_cansleep(iqs7211->reset_gpio, 1);
1220 * The following delay ensures the shared RDY/MCLR pin is sampled in
1221 * between periodic assertions by the device and assumes the default
1222 * communication timeout has not been overwritten in OTP memory.
1224 if (iqs7211->reset_gpio == iqs7211->irq_gpio)
1225 msleep(IQS7211_RESET_TIMEOUT_MS);
1227 usleep_range(1000, 1100);
1229 gpiod_set_value_cansleep(iqs7211->reset_gpio, 0);
1230 if (iqs7211->reset_gpio == iqs7211->irq_gpio)
1233 return iqs7211_irq_poll(iqs7211, IQS7211_START_TIMEOUT_US);
1236 static int iqs7211_force_comms(struct iqs7211_private *iqs7211)
1238 u8 msg_buf[] = { 0xFF, };
1241 switch (iqs7211->comms_mode) {
1242 case IQS7211_COMMS_MODE_WAIT:
1243 return iqs7211_irq_poll(iqs7211, IQS7211_START_TIMEOUT_US);
1245 case IQS7211_COMMS_MODE_FREE:
1248 case IQS7211_COMMS_MODE_FORCE:
1256 * The device cannot communicate until it asserts its interrupt (RDY)
1257 * pin. Attempts to do so while RDY is deasserted return an ACK; how-
1258 * ever all write data is ignored, and all read data returns 0xEE.
1260 * Unsolicited communication must be preceded by a special force com-
1261 * munication command, after which the device eventually asserts its
1262 * RDY pin and agrees to communicate.
1264 * Regardless of whether communication is forced or the result of an
1265 * interrupt, the device automatically deasserts its RDY pin once it
1266 * detects an I2C stop condition, or a timeout expires.
1268 ret = gpiod_get_value_cansleep(iqs7211->irq_gpio);
1274 ret = i2c_master_send(iqs7211->client, msg_buf, sizeof(msg_buf));
1275 if (ret < (int)sizeof(msg_buf)) {
1279 msleep(IQS7211_COMMS_RETRY_MS);
1285 return iqs7211_irq_poll(iqs7211, IQS7211_COMMS_TIMEOUT_US);
1288 static int iqs7211_read_burst(struct iqs7211_private *iqs7211,
1289 u8 reg, void *val, u16 val_len)
1292 struct i2c_client *client = iqs7211->client;
1293 struct i2c_msg msg[] = {
1295 .addr = client->addr,
1301 .addr = client->addr,
1309 * The following loop protects against an edge case in which the RDY
1310 * pin is automatically deasserted just as the read is initiated. In
1311 * that case, the read must be retried using forced communication.
1313 for (i = 0; i < IQS7211_NUM_RETRIES; i++) {
1314 ret = iqs7211_force_comms(iqs7211);
1318 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
1319 if (ret < (int)ARRAY_SIZE(msg)) {
1323 msleep(IQS7211_COMMS_RETRY_MS);
1327 if (get_unaligned_le16(msg[1].buf) == IQS7211_COMMS_ERROR) {
1339 dev_err(&client->dev,
1340 "Failed to read from address 0x%02X: %d\n", reg, ret);
1345 static int iqs7211_read_word(struct iqs7211_private *iqs7211, u8 reg, u16 *val)
1350 error = iqs7211_read_burst(iqs7211, reg, &val_buf, sizeof(val_buf));
1354 *val = le16_to_cpu(val_buf);
1359 static int iqs7211_write_burst(struct iqs7211_private *iqs7211,
1360 u8 reg, const void *val, u16 val_len)
1362 int msg_len = sizeof(reg) + val_len;
1364 struct i2c_client *client = iqs7211->client;
1367 msg_buf = kzalloc(msg_len, GFP_KERNEL);
1372 memcpy(msg_buf + sizeof(reg), val, val_len);
1375 * The following loop protects against an edge case in which the RDY
1376 * pin is automatically asserted just before the force communication
1379 * In that case, the subsequent I2C stop condition tricks the device
1380 * into preemptively deasserting the RDY pin and the command must be
1383 for (i = 0; i < IQS7211_NUM_RETRIES; i++) {
1384 ret = iqs7211_force_comms(iqs7211);
1388 ret = i2c_master_send(client, msg_buf, msg_len);
1389 if (ret < msg_len) {
1393 msleep(IQS7211_COMMS_RETRY_MS);
1406 dev_err(&client->dev,
1407 "Failed to write to address 0x%02X: %d\n", reg, ret);
1412 static int iqs7211_write_word(struct iqs7211_private *iqs7211, u8 reg, u16 val)
1414 __le16 val_buf = cpu_to_le16(val);
1416 return iqs7211_write_burst(iqs7211, reg, &val_buf, sizeof(val_buf));
1419 static int iqs7211_start_comms(struct iqs7211_private *iqs7211)
1421 const struct iqs7211_dev_desc *dev_desc = iqs7211->dev_desc;
1422 struct i2c_client *client = iqs7211->client;
1429 * Until forced communication can be enabled, the host must wait for a
1430 * communication window each time it intends to elicit a response from
1433 * Forced communication is not necessary, however, if the host adapter
1434 * can support clock stretching. In that case, the device freely clock
1435 * stretches until all pending conversions are complete.
1437 forced_comms = device_property_present(&client->dev,
1438 "azoteq,forced-comms");
1440 error = device_property_read_u32(&client->dev,
1441 "azoteq,forced-comms-default", &val);
1442 if (error == -EINVAL) {
1443 iqs7211->comms_init = IQS7211_COMMS_MODE_WAIT;
1445 dev_err(&client->dev,
1446 "Failed to read default communication mode: %d\n",
1450 iqs7211->comms_init = forced_comms ? IQS7211_COMMS_MODE_FORCE
1451 : IQS7211_COMMS_MODE_WAIT;
1453 iqs7211->comms_init = forced_comms ? IQS7211_COMMS_MODE_WAIT
1454 : IQS7211_COMMS_MODE_FREE;
1457 iqs7211->comms_mode = iqs7211->comms_init;
1459 error = iqs7211_hard_reset(iqs7211);
1461 dev_err(&client->dev, "Failed to reset device: %d\n", error);
1465 error = iqs7211_read_burst(iqs7211, IQS7211_PROD_NUM,
1467 sizeof(iqs7211->ver_info));
1471 if (le16_to_cpu(iqs7211->ver_info.prod_num) != dev_desc->prod_num) {
1472 dev_err(&client->dev, "Invalid product number: %u\n",
1473 le16_to_cpu(iqs7211->ver_info.prod_num));
1477 error = iqs7211_read_word(iqs7211, dev_desc->sys_ctrl + 1,
1483 comms_setup |= dev_desc->comms_req;
1485 comms_setup &= ~dev_desc->comms_req;
1487 error = iqs7211_write_word(iqs7211, dev_desc->sys_ctrl + 1,
1488 comms_setup | dev_desc->comms_end);
1493 iqs7211->comms_mode = IQS7211_COMMS_MODE_FORCE;
1495 iqs7211->comms_mode = IQS7211_COMMS_MODE_FREE;
1497 error = iqs7211_read_burst(iqs7211, dev_desc->exp_file,
1499 sizeof(iqs7211->exp_file));
1503 error = iqs7211_read_burst(iqs7211, dev_desc->tp_config,
1504 &iqs7211->tp_config,
1505 sizeof(iqs7211->tp_config));
1509 error = iqs7211_write_word(iqs7211, dev_desc->sys_ctrl + 1,
1514 iqs7211->event_mask = comms_setup & ~IQS7211_EVENT_MASK_ALL;
1515 iqs7211->event_mask |= (IQS7211_EVENT_MASK_ATI | IQS7211_EVENT_MODE);
1520 static int iqs7211_init_device(struct iqs7211_private *iqs7211)
1522 const struct iqs7211_dev_desc *dev_desc = iqs7211->dev_desc;
1523 struct iqs7211_reg_field_desc *reg_field;
1524 __le16 sys_ctrl[] = {
1525 cpu_to_le16(dev_desc->ack_reset),
1526 cpu_to_le16(iqs7211->event_mask),
1531 * Acknowledge reset before writing any registers in case the device
1532 * suffers a spurious reset during initialization. The communication
1533 * mode is configured at this time as well.
1535 error = iqs7211_write_burst(iqs7211, dev_desc->sys_ctrl, sys_ctrl,
1540 if (iqs7211->event_mask & dev_desc->comms_req)
1541 iqs7211->comms_mode = IQS7211_COMMS_MODE_FORCE;
1543 iqs7211->comms_mode = IQS7211_COMMS_MODE_FREE;
1546 * Take advantage of the stop-bit disable function, if available, to
1547 * save the trouble of having to reopen a communication window after
1548 * each read or write.
1550 error = iqs7211_write_word(iqs7211, dev_desc->sys_ctrl + 1,
1551 iqs7211->event_mask | dev_desc->comms_end);
1555 list_for_each_entry(reg_field, &iqs7211->reg_field_head, list) {
1556 u16 new_val = reg_field->val;
1558 if (reg_field->mask < U16_MAX) {
1561 error = iqs7211_read_word(iqs7211, reg_field->addr,
1566 new_val = old_val & ~reg_field->mask;
1567 new_val |= reg_field->val;
1569 if (new_val == old_val)
1573 error = iqs7211_write_word(iqs7211, reg_field->addr, new_val);
1578 error = iqs7211_write_burst(iqs7211, dev_desc->tp_config,
1579 &iqs7211->tp_config,
1580 sizeof(iqs7211->tp_config));
1584 if (**iqs7211->cycle_alloc) {
1585 error = iqs7211_write_burst(iqs7211, dev_desc->rx_tx_map,
1586 &iqs7211->rx_tx_map,
1591 for (i = 0; i < sizeof(dev_desc->cycle_limit); i++) {
1592 error = iqs7211_write_burst(iqs7211,
1593 dev_desc->cycle_alloc[i],
1594 iqs7211->cycle_alloc[i],
1595 dev_desc->cycle_limit[i] * 3);
1601 *sys_ctrl = cpu_to_le16(iqs7211->ati_start);
1603 return iqs7211_write_burst(iqs7211, dev_desc->sys_ctrl, sys_ctrl,
1607 static int iqs7211_add_field(struct iqs7211_private *iqs7211,
1608 struct iqs7211_reg_field_desc new_field)
1610 struct i2c_client *client = iqs7211->client;
1611 struct iqs7211_reg_field_desc *reg_field;
1613 if (!new_field.addr)
1616 list_for_each_entry(reg_field, &iqs7211->reg_field_head, list) {
1617 if (reg_field->addr != new_field.addr)
1620 reg_field->mask |= new_field.mask;
1621 reg_field->val |= new_field.val;
1625 reg_field = devm_kzalloc(&client->dev, sizeof(*reg_field), GFP_KERNEL);
1629 reg_field->addr = new_field.addr;
1630 reg_field->mask = new_field.mask;
1631 reg_field->val = new_field.val;
1633 list_add(®_field->list, &iqs7211->reg_field_head);
1638 static int iqs7211_parse_props(struct iqs7211_private *iqs7211,
1639 struct fwnode_handle *reg_grp_node,
1640 enum iqs7211_reg_grp_id reg_grp,
1641 enum iqs7211_reg_key_id reg_key)
1643 struct i2c_client *client = iqs7211->client;
1646 for (i = 0; i < ARRAY_SIZE(iqs7211_props); i++) {
1647 const char *name = iqs7211_props[i].name;
1648 u8 reg_addr = iqs7211_props[i].reg_addr[reg_grp]
1649 [iqs7211->dev_desc -
1651 int reg_shift = iqs7211_props[i].reg_shift;
1652 int reg_width = iqs7211_props[i].reg_width ? : 16;
1653 int val_pitch = iqs7211_props[i].val_pitch ? : 1;
1654 int val_min = iqs7211_props[i].val_min;
1655 int val_max = iqs7211_props[i].val_max;
1656 const char *label = iqs7211_props[i].label ? : name;
1657 struct iqs7211_reg_field_desc reg_field;
1661 if (iqs7211_props[i].reg_key != reg_key)
1667 error = fwnode_property_read_u32(reg_grp_node, name, &val);
1668 if (error == -EINVAL) {
1671 dev_err(&client->dev, "Failed to read %s %s: %d\n",
1672 fwnode_get_name(reg_grp_node), label, error);
1677 val_max = GENMASK(reg_width - 1, 0) * val_pitch;
1679 if (val < val_min || val > val_max) {
1680 dev_err(&client->dev, "Invalid %s: %u\n", label, val);
1684 reg_field.addr = reg_addr;
1685 reg_field.mask = GENMASK(reg_shift + reg_width - 1, reg_shift);
1686 reg_field.val = val / val_pitch << reg_shift;
1688 error = iqs7211_add_field(iqs7211, reg_field);
1696 static int iqs7211_parse_event(struct iqs7211_private *iqs7211,
1697 struct fwnode_handle *event_node,
1698 enum iqs7211_reg_grp_id reg_grp,
1699 enum iqs7211_reg_key_id reg_key,
1700 unsigned int *event_code)
1702 const struct iqs7211_dev_desc *dev_desc = iqs7211->dev_desc;
1703 struct i2c_client *client = iqs7211->client;
1704 struct iqs7211_reg_field_desc reg_field;
1708 error = iqs7211_parse_props(iqs7211, event_node, reg_grp, reg_key);
1712 if (reg_key == IQS7211_REG_KEY_AXIAL_X ||
1713 reg_key == IQS7211_REG_KEY_AXIAL_Y) {
1714 error = fwnode_property_read_u32(event_node,
1715 "azoteq,gesture-angle", &val);
1717 if (val >= ARRAY_SIZE(iqs7211_gesture_angle)) {
1718 dev_err(&client->dev,
1719 "Invalid %s gesture angle: %u\n",
1720 fwnode_get_name(event_node), val);
1724 reg_field.addr = dev_desc->gesture_angle;
1725 reg_field.mask = U8_MAX;
1726 reg_field.val = iqs7211_gesture_angle[val];
1728 error = iqs7211_add_field(iqs7211, reg_field);
1731 } else if (error != -EINVAL) {
1732 dev_err(&client->dev,
1733 "Failed to read %s gesture angle: %d\n",
1734 fwnode_get_name(event_node), error);
1739 error = fwnode_property_read_u32(event_node, "linux,code", event_code);
1740 if (error == -EINVAL)
1743 dev_err(&client->dev, "Failed to read %s code: %d\n",
1744 fwnode_get_name(event_node), error);
1749 static int iqs7211_parse_cycles(struct iqs7211_private *iqs7211,
1750 struct fwnode_handle *tp_node)
1752 const struct iqs7211_dev_desc *dev_desc = iqs7211->dev_desc;
1753 struct i2c_client *client = iqs7211->client;
1754 int num_cycles = dev_desc->cycle_limit[0] + dev_desc->cycle_limit[1];
1755 int error, count, i, j, k, cycle_start;
1756 unsigned int cycle_alloc[IQS7211_MAX_CYCLES][2];
1757 u8 total_rx = iqs7211->tp_config.total_rx;
1758 u8 total_tx = iqs7211->tp_config.total_tx;
1760 for (i = 0; i < IQS7211_MAX_CYCLES * 2; i++)
1761 *(cycle_alloc[0] + i) = U8_MAX;
1763 count = fwnode_property_count_u32(tp_node, "azoteq,channel-select");
1764 if (count == -EINVAL) {
1766 * Assign each sensing cycle's slots (0 and 1) to a channel,
1767 * defined as the intersection between two CRx and CTx pins.
1768 * A channel assignment of 255 means the slot is unused.
1770 for (i = 0, cycle_start = 0; i < total_tx; i++) {
1773 for (j = 0; j < total_rx; j++) {
1775 * Channels formed by CRx0-3 and CRx4-7 are
1776 * bound to slots 0 and 1, respectively.
1778 int slot = iqs7211->rx_tx_map[j] < 4 ? 0 : 1;
1779 int chan = i * total_rx + j;
1781 for (k = cycle_start; k < num_cycles; k++) {
1782 if (cycle_alloc[k][slot] < U8_MAX)
1785 cycle_alloc[k][slot] = chan;
1789 if (k < num_cycles) {
1790 cycle_stop = max(k, cycle_stop);
1794 dev_err(&client->dev,
1795 "Insufficient number of cycles\n");
1800 * Sensing cycles cannot straddle more than one CTx
1801 * pin. As such, the next row's starting cycle must
1802 * be greater than the previous row's highest cycle.
1804 cycle_start = cycle_stop + 1;
1806 } else if (count < 0) {
1807 dev_err(&client->dev, "Failed to count channels: %d\n", count);
1809 } else if (count > num_cycles * 2) {
1810 dev_err(&client->dev, "Insufficient number of cycles\n");
1812 } else if (count > 0) {
1813 error = fwnode_property_read_u32_array(tp_node,
1814 "azoteq,channel-select",
1815 cycle_alloc[0], count);
1817 dev_err(&client->dev, "Failed to read channels: %d\n",
1822 for (i = 0; i < count; i++) {
1823 int chan = *(cycle_alloc[0] + i);
1828 if (chan >= total_rx * total_tx) {
1829 dev_err(&client->dev, "Invalid channel: %d\n",
1834 for (j = 0; j < count; j++) {
1835 if (j == i || *(cycle_alloc[0] + j) != chan)
1838 dev_err(&client->dev, "Duplicate channel: %d\n",
1846 * Once the raw channel assignments have been derived, they must be
1847 * packed according to the device's register map.
1849 for (i = 0, cycle_start = 0; i < sizeof(dev_desc->cycle_limit); i++) {
1852 for (j = cycle_start;
1853 j < cycle_start + dev_desc->cycle_limit[i]; j++) {
1854 iqs7211->cycle_alloc[i][offs++] = 0x05;
1855 iqs7211->cycle_alloc[i][offs++] = cycle_alloc[j][0];
1856 iqs7211->cycle_alloc[i][offs++] = cycle_alloc[j][1];
1859 cycle_start += dev_desc->cycle_limit[i];
1865 static int iqs7211_parse_tp(struct iqs7211_private *iqs7211,
1866 struct fwnode_handle *tp_node)
1868 const struct iqs7211_dev_desc *dev_desc = iqs7211->dev_desc;
1869 struct i2c_client *client = iqs7211->client;
1870 unsigned int pins[IQS7211_MAX_CTX];
1871 int error, count, i, j;
1873 count = fwnode_property_count_u32(tp_node, "azoteq,rx-enable");
1874 if (count == -EINVAL) {
1876 } else if (count < 0) {
1877 dev_err(&client->dev, "Failed to count CRx pins: %d\n", count);
1879 } else if (count > IQS7211_NUM_CRX) {
1880 dev_err(&client->dev, "Invalid number of CRx pins\n");
1884 error = fwnode_property_read_u32_array(tp_node, "azoteq,rx-enable",
1887 dev_err(&client->dev, "Failed to read CRx pins: %d\n", error);
1891 for (i = 0; i < count; i++) {
1892 if (pins[i] >= IQS7211_NUM_CRX) {
1893 dev_err(&client->dev, "Invalid CRx pin: %u\n", pins[i]);
1897 iqs7211->rx_tx_map[i] = pins[i];
1900 iqs7211->tp_config.total_rx = count;
1902 count = fwnode_property_count_u32(tp_node, "azoteq,tx-enable");
1904 dev_err(&client->dev, "Failed to count CTx pins: %d\n", count);
1906 } else if (count > dev_desc->num_ctx) {
1907 dev_err(&client->dev, "Invalid number of CTx pins\n");
1911 error = fwnode_property_read_u32_array(tp_node, "azoteq,tx-enable",
1914 dev_err(&client->dev, "Failed to read CTx pins: %d\n", error);
1918 for (i = 0; i < count; i++) {
1919 if (pins[i] >= dev_desc->num_ctx) {
1920 dev_err(&client->dev, "Invalid CTx pin: %u\n", pins[i]);
1924 for (j = 0; j < iqs7211->tp_config.total_rx; j++) {
1925 if (iqs7211->rx_tx_map[j] != pins[i])
1928 dev_err(&client->dev, "Conflicting CTx pin: %u\n",
1933 iqs7211->rx_tx_map[iqs7211->tp_config.total_rx + i] = pins[i];
1936 iqs7211->tp_config.total_tx = count;
1938 return iqs7211_parse_cycles(iqs7211, tp_node);
1941 static int iqs7211_parse_alp(struct iqs7211_private *iqs7211,
1942 struct fwnode_handle *alp_node)
1944 const struct iqs7211_dev_desc *dev_desc = iqs7211->dev_desc;
1945 struct i2c_client *client = iqs7211->client;
1946 struct iqs7211_reg_field_desc reg_field;
1947 int error, count, i;
1949 count = fwnode_property_count_u32(alp_node, "azoteq,rx-enable");
1950 if (count < 0 && count != -EINVAL) {
1951 dev_err(&client->dev, "Failed to count CRx pins: %d\n", count);
1953 } else if (count > IQS7211_NUM_CRX) {
1954 dev_err(&client->dev, "Invalid number of CRx pins\n");
1956 } else if (count >= 0) {
1957 unsigned int pins[IQS7211_NUM_CRX];
1959 error = fwnode_property_read_u32_array(alp_node,
1963 dev_err(&client->dev, "Failed to read CRx pins: %d\n",
1968 reg_field.addr = dev_desc->alp_config;
1969 reg_field.mask = GENMASK(IQS7211_NUM_CRX - 1, 0);
1972 for (i = 0; i < count; i++) {
1973 if (pins[i] < dev_desc->min_crx_alp ||
1974 pins[i] >= IQS7211_NUM_CRX) {
1975 dev_err(&client->dev, "Invalid CRx pin: %u\n",
1980 reg_field.val |= BIT(pins[i]);
1983 error = iqs7211_add_field(iqs7211, reg_field);
1988 count = fwnode_property_count_u32(alp_node, "azoteq,tx-enable");
1989 if (count < 0 && count != -EINVAL) {
1990 dev_err(&client->dev, "Failed to count CTx pins: %d\n", count);
1992 } else if (count > dev_desc->num_ctx) {
1993 dev_err(&client->dev, "Invalid number of CTx pins\n");
1995 } else if (count >= 0) {
1996 unsigned int pins[IQS7211_MAX_CTX];
1998 error = fwnode_property_read_u32_array(alp_node,
2002 dev_err(&client->dev, "Failed to read CTx pins: %d\n",
2007 reg_field.addr = dev_desc->alp_config + 1;
2008 reg_field.mask = GENMASK(dev_desc->num_ctx - 1, 0);
2011 for (i = 0; i < count; i++) {
2012 if (pins[i] >= dev_desc->num_ctx) {
2013 dev_err(&client->dev, "Invalid CTx pin: %u\n",
2018 reg_field.val |= BIT(pins[i]);
2021 error = iqs7211_add_field(iqs7211, reg_field);
2029 static int (*iqs7211_parse_extra[IQS7211_NUM_REG_GRPS])
2030 (struct iqs7211_private *iqs7211,
2031 struct fwnode_handle *reg_grp_node) = {
2032 [IQS7211_REG_GRP_TP] = iqs7211_parse_tp,
2033 [IQS7211_REG_GRP_ALP] = iqs7211_parse_alp,
2036 static int iqs7211_parse_reg_grp(struct iqs7211_private *iqs7211,
2037 struct fwnode_handle *reg_grp_node,
2038 enum iqs7211_reg_grp_id reg_grp)
2040 const struct iqs7211_dev_desc *dev_desc = iqs7211->dev_desc;
2041 struct iqs7211_reg_field_desc reg_field;
2044 error = iqs7211_parse_props(iqs7211, reg_grp_node, reg_grp,
2045 IQS7211_REG_KEY_NONE);
2049 if (iqs7211_parse_extra[reg_grp]) {
2050 error = iqs7211_parse_extra[reg_grp](iqs7211, reg_grp_node);
2055 iqs7211->ati_start |= dev_desc->ati_start[reg_grp];
2057 reg_field.addr = dev_desc->kp_enable[reg_grp];
2061 for (i = 0; i < dev_desc->num_kp_events; i++) {
2062 const char *event_name = dev_desc->kp_events[i].name;
2063 struct fwnode_handle *event_node;
2065 if (dev_desc->kp_events[i].reg_grp != reg_grp)
2068 reg_field.mask |= dev_desc->kp_events[i].enable;
2071 event_node = fwnode_get_named_child_node(reg_grp_node,
2074 event_node = fwnode_handle_get(reg_grp_node);
2079 error = iqs7211_parse_event(iqs7211, event_node,
2080 dev_desc->kp_events[i].reg_grp,
2081 dev_desc->kp_events[i].reg_key,
2082 &iqs7211->kp_code[i]);
2083 fwnode_handle_put(event_node);
2087 reg_field.val |= dev_desc->kp_events[i].enable;
2089 iqs7211->event_mask |= iqs7211_reg_grp_masks[reg_grp];
2092 return iqs7211_add_field(iqs7211, reg_field);
2095 static int iqs7211_register_kp(struct iqs7211_private *iqs7211)
2097 const struct iqs7211_dev_desc *dev_desc = iqs7211->dev_desc;
2098 struct input_dev *kp_idev = iqs7211->kp_idev;
2099 struct i2c_client *client = iqs7211->client;
2102 for (i = 0; i < dev_desc->num_kp_events; i++)
2103 if (iqs7211->kp_code[i])
2106 if (i == dev_desc->num_kp_events)
2109 kp_idev = devm_input_allocate_device(&client->dev);
2113 iqs7211->kp_idev = kp_idev;
2115 kp_idev->name = dev_desc->kp_name;
2116 kp_idev->id.bustype = BUS_I2C;
2118 for (i = 0; i < dev_desc->num_kp_events; i++)
2119 if (iqs7211->kp_code[i])
2120 input_set_capability(iqs7211->kp_idev, EV_KEY,
2121 iqs7211->kp_code[i]);
2123 error = input_register_device(kp_idev);
2125 dev_err(&client->dev, "Failed to register %s: %d\n",
2126 kp_idev->name, error);
2131 static int iqs7211_register_tp(struct iqs7211_private *iqs7211)
2133 const struct iqs7211_dev_desc *dev_desc = iqs7211->dev_desc;
2134 struct touchscreen_properties *prop = &iqs7211->prop;
2135 struct input_dev *tp_idev = iqs7211->tp_idev;
2136 struct i2c_client *client = iqs7211->client;
2139 error = device_property_read_u32(&client->dev, "azoteq,num-contacts",
2140 &iqs7211->num_contacts);
2141 if (error == -EINVAL) {
2144 dev_err(&client->dev, "Failed to read number of contacts: %d\n",
2147 } else if (iqs7211->num_contacts > IQS7211_MAX_CONTACTS) {
2148 dev_err(&client->dev, "Invalid number of contacts: %u\n",
2149 iqs7211->num_contacts);
2153 iqs7211->tp_config.num_contacts = iqs7211->num_contacts ? : 1;
2155 if (!iqs7211->num_contacts)
2158 iqs7211->event_mask |= IQS7211_EVENT_MASK_MOVE;
2160 tp_idev = devm_input_allocate_device(&client->dev);
2164 iqs7211->tp_idev = tp_idev;
2166 tp_idev->name = dev_desc->tp_name;
2167 tp_idev->id.bustype = BUS_I2C;
2169 input_set_abs_params(tp_idev, ABS_MT_POSITION_X,
2170 0, le16_to_cpu(iqs7211->tp_config.max_x), 0, 0);
2172 input_set_abs_params(tp_idev, ABS_MT_POSITION_Y,
2173 0, le16_to_cpu(iqs7211->tp_config.max_y), 0, 0);
2175 input_set_abs_params(tp_idev, ABS_MT_PRESSURE, 0, U16_MAX, 0, 0);
2177 touchscreen_parse_properties(tp_idev, true, prop);
2180 * The device reserves 0xFFFF for coordinates that correspond to slots
2181 * which are not in a state of touch.
2183 if (prop->max_x >= U16_MAX || prop->max_y >= U16_MAX) {
2184 dev_err(&client->dev, "Invalid trackpad size: %u*%u\n",
2185 prop->max_x, prop->max_y);
2189 iqs7211->tp_config.max_x = cpu_to_le16(prop->max_x);
2190 iqs7211->tp_config.max_y = cpu_to_le16(prop->max_y);
2192 error = input_mt_init_slots(tp_idev, iqs7211->num_contacts,
2195 dev_err(&client->dev, "Failed to initialize slots: %d\n",
2200 error = input_register_device(tp_idev);
2202 dev_err(&client->dev, "Failed to register %s: %d\n",
2203 tp_idev->name, error);
2208 static int iqs7211_report(struct iqs7211_private *iqs7211)
2210 const struct iqs7211_dev_desc *dev_desc = iqs7211->dev_desc;
2211 struct i2c_client *client = iqs7211->client;
2212 struct iqs7211_touch_data *touch_data;
2213 u16 info_flags, charge_mode, gesture_flags;
2217 error = iqs7211_read_burst(iqs7211, dev_desc->sys_stat, status,
2218 dev_desc->contact_offs * sizeof(__le16) +
2219 iqs7211->num_contacts * sizeof(*touch_data));
2223 info_flags = le16_to_cpu(status[dev_desc->info_offs]);
2225 if (info_flags & dev_desc->show_reset) {
2226 dev_err(&client->dev, "Unexpected device reset\n");
2229 * The device may or may not expect forced communication after
2230 * it exits hardware reset, so the corresponding state machine
2231 * must be reset as well.
2233 iqs7211->comms_mode = iqs7211->comms_init;
2235 return iqs7211_init_device(iqs7211);
2238 for (i = 0; i < ARRAY_SIZE(dev_desc->ati_error); i++) {
2239 if (!(info_flags & dev_desc->ati_error[i]))
2242 dev_err(&client->dev, "Unexpected %s ATI error\n",
2243 iqs7211_reg_grp_names[i]);
2247 for (i = 0; i < iqs7211->num_contacts; i++) {
2250 touch_data = (struct iqs7211_touch_data *)
2251 &status[dev_desc->contact_offs] + i;
2252 pressure = le16_to_cpu(touch_data->pressure);
2254 input_mt_slot(iqs7211->tp_idev, i);
2255 if (input_mt_report_slot_state(iqs7211->tp_idev, MT_TOOL_FINGER,
2257 touchscreen_report_pos(iqs7211->tp_idev, &iqs7211->prop,
2258 le16_to_cpu(touch_data->abs_x),
2259 le16_to_cpu(touch_data->abs_y),
2261 input_report_abs(iqs7211->tp_idev, ABS_MT_PRESSURE,
2266 if (iqs7211->num_contacts) {
2267 input_mt_sync_frame(iqs7211->tp_idev);
2268 input_sync(iqs7211->tp_idev);
2271 if (!iqs7211->kp_idev)
2274 charge_mode = info_flags & GENMASK(dev_desc->charge_shift + 2,
2275 dev_desc->charge_shift);
2276 charge_mode >>= dev_desc->charge_shift;
2279 * A charging mode higher than 2 (idle mode) indicates the device last
2280 * operated in low-power mode and intends to express an ALP event.
2282 if (info_flags & dev_desc->kp_events->mask && charge_mode > 2) {
2283 input_report_key(iqs7211->kp_idev, *iqs7211->kp_code, 1);
2284 input_sync(iqs7211->kp_idev);
2286 input_report_key(iqs7211->kp_idev, *iqs7211->kp_code, 0);
2289 for (i = 0; i < dev_desc->num_kp_events; i++) {
2290 if (dev_desc->kp_events[i].reg_grp != IQS7211_REG_GRP_BTN)
2293 input_report_key(iqs7211->kp_idev, iqs7211->kp_code[i],
2294 info_flags & dev_desc->kp_events[i].mask);
2297 gesture_flags = le16_to_cpu(status[dev_desc->gesture_offs]);
2299 for (i = 0; i < dev_desc->num_kp_events; i++) {
2300 enum iqs7211_reg_key_id reg_key = dev_desc->kp_events[i].reg_key;
2301 u16 mask = dev_desc->kp_events[i].mask;
2303 if (dev_desc->kp_events[i].reg_grp != IQS7211_REG_GRP_TP)
2306 if ((gesture_flags ^ iqs7211->gesture_cache) & mask)
2307 input_report_key(iqs7211->kp_idev, iqs7211->kp_code[i],
2308 gesture_flags & mask);
2310 iqs7211->gesture_cache &= ~mask;
2313 * Hold and palm gestures persist while the contact remains in
2314 * place; all others are momentary and hence are followed by a
2315 * complementary release event.
2317 if (reg_key == IQS7211_REG_KEY_HOLD ||
2318 reg_key == IQS7211_REG_KEY_PALM) {
2319 iqs7211->gesture_cache |= gesture_flags & mask;
2320 gesture_flags &= ~mask;
2324 if (gesture_flags) {
2325 input_sync(iqs7211->kp_idev);
2327 for (i = 0; i < dev_desc->num_kp_events; i++)
2328 if (dev_desc->kp_events[i].reg_grp == IQS7211_REG_GRP_TP &&
2329 gesture_flags & dev_desc->kp_events[i].mask)
2330 input_report_key(iqs7211->kp_idev,
2331 iqs7211->kp_code[i], 0);
2334 input_sync(iqs7211->kp_idev);
2339 static irqreturn_t iqs7211_irq(int irq, void *context)
2341 struct iqs7211_private *iqs7211 = context;
2343 return iqs7211_report(iqs7211) ? IRQ_NONE : IRQ_HANDLED;
2346 static int iqs7211_suspend(struct device *dev)
2348 struct iqs7211_private *iqs7211 = dev_get_drvdata(dev);
2349 const struct iqs7211_dev_desc *dev_desc = iqs7211->dev_desc;
2352 if (!dev_desc->suspend || device_may_wakeup(dev))
2356 * I2C communication prompts the device to assert its RDY pin if it is
2357 * not already asserted. As such, the interrupt must be disabled so as
2358 * to prevent reentrant interrupts.
2360 disable_irq(gpiod_to_irq(iqs7211->irq_gpio));
2362 error = iqs7211_write_word(iqs7211, dev_desc->sys_ctrl,
2365 enable_irq(gpiod_to_irq(iqs7211->irq_gpio));
2370 static int iqs7211_resume(struct device *dev)
2372 struct iqs7211_private *iqs7211 = dev_get_drvdata(dev);
2373 const struct iqs7211_dev_desc *dev_desc = iqs7211->dev_desc;
2374 __le16 sys_ctrl[] = {
2376 cpu_to_le16(iqs7211->event_mask),
2380 if (!dev_desc->suspend || device_may_wakeup(dev))
2383 disable_irq(gpiod_to_irq(iqs7211->irq_gpio));
2386 * Forced communication, if in use, must be explicitly enabled as part
2387 * of the wake-up command.
2389 error = iqs7211_write_burst(iqs7211, dev_desc->sys_ctrl, sys_ctrl,
2392 enable_irq(gpiod_to_irq(iqs7211->irq_gpio));
2397 static DEFINE_SIMPLE_DEV_PM_OPS(iqs7211_pm, iqs7211_suspend, iqs7211_resume);
2399 static ssize_t fw_info_show(struct device *dev,
2400 struct device_attribute *attr, char *buf)
2402 struct iqs7211_private *iqs7211 = dev_get_drvdata(dev);
2404 return sysfs_emit(buf, "%u.%u.%u.%u:%u.%u\n",
2405 le16_to_cpu(iqs7211->ver_info.prod_num),
2406 le32_to_cpu(iqs7211->ver_info.patch),
2407 le16_to_cpu(iqs7211->ver_info.major),
2408 le16_to_cpu(iqs7211->ver_info.minor),
2409 iqs7211->exp_file[1], iqs7211->exp_file[0]);
2412 static DEVICE_ATTR_RO(fw_info);
2414 static struct attribute *iqs7211_attrs[] = {
2415 &dev_attr_fw_info.attr,
2418 ATTRIBUTE_GROUPS(iqs7211);
2420 static const struct of_device_id iqs7211_of_match[] = {
2422 .compatible = "azoteq,iqs7210a",
2423 .data = &iqs7211_devs[IQS7210A],
2426 .compatible = "azoteq,iqs7211a",
2427 .data = &iqs7211_devs[IQS7211A],
2430 .compatible = "azoteq,iqs7211e",
2431 .data = &iqs7211_devs[IQS7211E],
2435 MODULE_DEVICE_TABLE(of, iqs7211_of_match);
2437 static int iqs7211_probe(struct i2c_client *client)
2439 struct iqs7211_private *iqs7211;
2440 enum iqs7211_reg_grp_id reg_grp;
2441 unsigned long irq_flags;
2445 iqs7211 = devm_kzalloc(&client->dev, sizeof(*iqs7211), GFP_KERNEL);
2449 i2c_set_clientdata(client, iqs7211);
2450 iqs7211->client = client;
2452 INIT_LIST_HEAD(&iqs7211->reg_field_head);
2454 iqs7211->dev_desc = device_get_match_data(&client->dev);
2455 if (!iqs7211->dev_desc)
2458 shared_irq = iqs7211->dev_desc->num_ctx == IQS7211_MAX_CTX;
2461 * The RDY pin behaves as an interrupt, but must also be polled ahead
2462 * of unsolicited I2C communication. As such, it is first opened as a
2463 * GPIO and then passed to gpiod_to_irq() to register the interrupt.
2465 * If an extra CTx pin is present, the RDY and MCLR pins are combined
2466 * into a single bidirectional pin. In that case, the platform's GPIO
2467 * must be configured as an open-drain output.
2469 iqs7211->irq_gpio = devm_gpiod_get(&client->dev, "irq",
2470 shared_irq ? GPIOD_OUT_LOW
2472 if (IS_ERR(iqs7211->irq_gpio)) {
2473 error = PTR_ERR(iqs7211->irq_gpio);
2474 dev_err(&client->dev, "Failed to request IRQ GPIO: %d\n",
2480 iqs7211->reset_gpio = iqs7211->irq_gpio;
2482 iqs7211->reset_gpio = devm_gpiod_get_optional(&client->dev,
2485 if (IS_ERR(iqs7211->reset_gpio)) {
2486 error = PTR_ERR(iqs7211->reset_gpio);
2487 dev_err(&client->dev,
2488 "Failed to request reset GPIO: %d\n", error);
2493 error = iqs7211_start_comms(iqs7211);
2497 for (reg_grp = 0; reg_grp < IQS7211_NUM_REG_GRPS; reg_grp++) {
2498 const char *reg_grp_name = iqs7211_reg_grp_names[reg_grp];
2499 struct fwnode_handle *reg_grp_node;
2502 reg_grp_node = device_get_named_child_node(&client->dev,
2505 reg_grp_node = fwnode_handle_get(dev_fwnode(&client->dev));
2510 error = iqs7211_parse_reg_grp(iqs7211, reg_grp_node, reg_grp);
2511 fwnode_handle_put(reg_grp_node);
2516 error = iqs7211_register_kp(iqs7211);
2520 error = iqs7211_register_tp(iqs7211);
2524 error = iqs7211_init_device(iqs7211);
2528 irq = gpiod_to_irq(iqs7211->irq_gpio);
2532 irq_flags = gpiod_is_active_low(iqs7211->irq_gpio) ? IRQF_TRIGGER_LOW
2533 : IRQF_TRIGGER_HIGH;
2534 irq_flags |= IRQF_ONESHOT;
2536 error = devm_request_threaded_irq(&client->dev, irq, NULL, iqs7211_irq,
2537 irq_flags, client->name, iqs7211);
2539 dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
2544 static struct i2c_driver iqs7211_i2c_driver = {
2545 .probe = iqs7211_probe,
2548 .of_match_table = iqs7211_of_match,
2549 .dev_groups = iqs7211_groups,
2550 .pm = pm_sleep_ptr(&iqs7211_pm),
2553 module_i2c_driver(iqs7211_i2c_driver);
2556 MODULE_DESCRIPTION("Azoteq IQS7210A/7211A/E Trackpad/Touchscreen Controller");
2557 MODULE_LICENSE("GPL");