1 // SPDX-License-Identifier: GPL-2.0+
3 * Azoteq IQS626A Capacitive Touch Controller
5 * Copyright (C) 2020 Jeff LaBundy <jeff@labundy.com>
7 * This driver registers up to 2 input devices: one representing capacitive or
8 * inductive keys as well as Hall-effect switches, and one for a trackpad that
9 * can express various gestures.
12 #include <linux/bits.h>
13 #include <linux/completion.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/i2c.h>
18 #include <linux/input.h>
19 #include <linux/input/touchscreen.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/property.h>
25 #include <linux/regmap.h>
26 #include <linux/slab.h>
28 #define IQS626_VER_INFO 0x00
29 #define IQS626_VER_INFO_PROD_NUM 0x51
31 #define IQS626_SYS_FLAGS 0x02
32 #define IQS626_SYS_FLAGS_SHOW_RESET BIT(15)
33 #define IQS626_SYS_FLAGS_IN_ATI BIT(12)
34 #define IQS626_SYS_FLAGS_PWR_MODE_MASK GENMASK(9, 8)
35 #define IQS626_SYS_FLAGS_PWR_MODE_SHIFT 8
37 #define IQS626_HALL_OUTPUT 0x23
39 #define IQS626_SYS_SETTINGS 0x80
40 #define IQS626_SYS_SETTINGS_CLK_DIV BIT(15)
41 #define IQS626_SYS_SETTINGS_ULP_AUTO BIT(14)
42 #define IQS626_SYS_SETTINGS_DIS_AUTO BIT(13)
43 #define IQS626_SYS_SETTINGS_PWR_MODE_MASK GENMASK(12, 11)
44 #define IQS626_SYS_SETTINGS_PWR_MODE_SHIFT 11
45 #define IQS626_SYS_SETTINGS_PWR_MODE_MAX 3
46 #define IQS626_SYS_SETTINGS_ULP_UPDATE_MASK GENMASK(10, 8)
47 #define IQS626_SYS_SETTINGS_ULP_UPDATE_SHIFT 8
48 #define IQS626_SYS_SETTINGS_ULP_UPDATE_MAX 7
49 #define IQS626_SYS_SETTINGS_EVENT_MODE BIT(5)
50 #define IQS626_SYS_SETTINGS_EVENT_MODE_LP BIT(4)
51 #define IQS626_SYS_SETTINGS_REDO_ATI BIT(2)
52 #define IQS626_SYS_SETTINGS_ACK_RESET BIT(0)
54 #define IQS626_MISC_A_ATI_BAND_DISABLE BIT(7)
55 #define IQS626_MISC_A_TPx_LTA_UPDATE_MASK GENMASK(6, 4)
56 #define IQS626_MISC_A_TPx_LTA_UPDATE_SHIFT 4
57 #define IQS626_MISC_A_TPx_LTA_UPDATE_MAX 7
58 #define IQS626_MISC_A_ATI_LP_ONLY BIT(3)
59 #define IQS626_MISC_A_GPIO3_SELECT_MASK GENMASK(2, 0)
60 #define IQS626_MISC_A_GPIO3_SELECT_MAX 7
62 #define IQS626_EVENT_MASK_SYS BIT(6)
63 #define IQS626_EVENT_MASK_GESTURE BIT(3)
64 #define IQS626_EVENT_MASK_DEEP BIT(2)
65 #define IQS626_EVENT_MASK_TOUCH BIT(1)
66 #define IQS626_EVENT_MASK_PROX BIT(0)
68 #define IQS626_RATE_NP_MS_MAX 255
69 #define IQS626_RATE_LP_MS_MAX 255
70 #define IQS626_RATE_ULP_MS_MAX 4080
71 #define IQS626_TIMEOUT_PWR_MS_MAX 130560
72 #define IQS626_TIMEOUT_LTA_MS_MAX 130560
74 #define IQS626_MISC_B_RESEED_UI_SEL_MASK GENMASK(7, 6)
75 #define IQS626_MISC_B_RESEED_UI_SEL_SHIFT 6
76 #define IQS626_MISC_B_RESEED_UI_SEL_MAX 3
77 #define IQS626_MISC_B_THRESH_EXTEND BIT(5)
78 #define IQS626_MISC_B_TRACKING_UI_ENABLE BIT(4)
79 #define IQS626_MISC_B_TPx_SWIPE BIT(3)
80 #define IQS626_MISC_B_RESEED_OFFSET BIT(2)
81 #define IQS626_MISC_B_FILT_STR_TPx GENMASK(1, 0)
83 #define IQS626_THRESH_SWIPE_MAX 255
84 #define IQS626_TIMEOUT_TAP_MS_MAX 4080
85 #define IQS626_TIMEOUT_SWIPE_MS_MAX 4080
87 #define IQS626_CHx_ENG_0_MEAS_CAP_SIZE BIT(7)
88 #define IQS626_CHx_ENG_0_RX_TERM_VSS BIT(5)
89 #define IQS626_CHx_ENG_0_LINEARIZE BIT(4)
90 #define IQS626_CHx_ENG_0_DUAL_DIR BIT(3)
91 #define IQS626_CHx_ENG_0_FILT_DISABLE BIT(2)
92 #define IQS626_CHx_ENG_0_ATI_MODE_MASK GENMASK(1, 0)
93 #define IQS626_CHx_ENG_0_ATI_MODE_MAX 3
95 #define IQS626_CHx_ENG_1_CCT_HIGH_1 BIT(7)
96 #define IQS626_CHx_ENG_1_CCT_HIGH_0 BIT(6)
97 #define IQS626_CHx_ENG_1_PROJ_BIAS_MASK GENMASK(5, 4)
98 #define IQS626_CHx_ENG_1_PROJ_BIAS_SHIFT 4
99 #define IQS626_CHx_ENG_1_PROJ_BIAS_MAX 3
100 #define IQS626_CHx_ENG_1_CCT_ENABLE BIT(3)
101 #define IQS626_CHx_ENG_1_SENSE_FREQ_MASK GENMASK(2, 1)
102 #define IQS626_CHx_ENG_1_SENSE_FREQ_SHIFT 1
103 #define IQS626_CHx_ENG_1_SENSE_FREQ_MAX 3
104 #define IQS626_CHx_ENG_1_ATI_BAND_TIGHTEN BIT(0)
106 #define IQS626_CHx_ENG_2_LOCAL_CAP_MASK GENMASK(7, 6)
107 #define IQS626_CHx_ENG_2_LOCAL_CAP_SHIFT 6
108 #define IQS626_CHx_ENG_2_LOCAL_CAP_MAX 3
109 #define IQS626_CHx_ENG_2_LOCAL_CAP_ENABLE BIT(5)
110 #define IQS626_CHx_ENG_2_SENSE_MODE_MASK GENMASK(3, 0)
111 #define IQS626_CHx_ENG_2_SENSE_MODE_MAX 15
113 #define IQS626_CHx_ENG_3_TX_FREQ_MASK GENMASK(5, 4)
114 #define IQS626_CHx_ENG_3_TX_FREQ_SHIFT 4
115 #define IQS626_CHx_ENG_3_TX_FREQ_MAX 3
116 #define IQS626_CHx_ENG_3_INV_LOGIC BIT(0)
118 #define IQS626_CHx_ENG_4_RX_TERM_VREG BIT(6)
119 #define IQS626_CHx_ENG_4_CCT_LOW_1 BIT(5)
120 #define IQS626_CHx_ENG_4_CCT_LOW_0 BIT(4)
121 #define IQS626_CHx_ENG_4_COMP_DISABLE BIT(1)
122 #define IQS626_CHx_ENG_4_STATIC_ENABLE BIT(0)
124 #define IQS626_TPx_ATI_BASE_MIN 45
125 #define IQS626_TPx_ATI_BASE_MAX 300
126 #define IQS626_CHx_ATI_BASE_MASK GENMASK(7, 6)
127 #define IQS626_CHx_ATI_BASE_75 0x00
128 #define IQS626_CHx_ATI_BASE_100 0x40
129 #define IQS626_CHx_ATI_BASE_150 0x80
130 #define IQS626_CHx_ATI_BASE_200 0xC0
131 #define IQS626_CHx_ATI_TARGET_MASK GENMASK(5, 0)
132 #define IQS626_CHx_ATI_TARGET_MAX 2016
134 #define IQS626_CHx_THRESH_MAX 255
135 #define IQS626_CHx_HYST_DEEP_MASK GENMASK(7, 4)
136 #define IQS626_CHx_HYST_DEEP_SHIFT 4
137 #define IQS626_CHx_HYST_TOUCH_MASK GENMASK(3, 0)
138 #define IQS626_CHx_HYST_MAX 15
140 #define IQS626_FILT_STR_NP_TPx_MASK GENMASK(7, 6)
141 #define IQS626_FILT_STR_NP_TPx_SHIFT 6
142 #define IQS626_FILT_STR_LP_TPx_MASK GENMASK(5, 4)
143 #define IQS626_FILT_STR_LP_TPx_SHIFT 4
145 #define IQS626_FILT_STR_NP_CNT_MASK GENMASK(7, 6)
146 #define IQS626_FILT_STR_NP_CNT_SHIFT 6
147 #define IQS626_FILT_STR_LP_CNT_MASK GENMASK(5, 4)
148 #define IQS626_FILT_STR_LP_CNT_SHIFT 4
149 #define IQS626_FILT_STR_NP_LTA_MASK GENMASK(3, 2)
150 #define IQS626_FILT_STR_NP_LTA_SHIFT 2
151 #define IQS626_FILT_STR_LP_LTA_MASK GENMASK(1, 0)
152 #define IQS626_FILT_STR_MAX 3
154 #define IQS626_ULP_PROJ_ENABLE BIT(4)
155 #define IQS626_GEN_WEIGHT_MAX 255
157 #define IQS626_MAX_REG 0xFF
159 #define IQS626_NUM_CH_TP_3 9
160 #define IQS626_NUM_CH_TP_2 6
161 #define IQS626_NUM_CH_GEN 3
162 #define IQS626_NUM_CRx_TX 8
164 #define IQS626_PWR_MODE_POLL_SLEEP_US 50000
165 #define IQS626_PWR_MODE_POLL_TIMEOUT_US 500000
167 #define iqs626_irq_wait() usleep_range(350, 400)
179 enum iqs626_rx_inactive {
180 IQS626_RX_INACTIVE_VSS,
181 IQS626_RX_INACTIVE_FLOAT,
182 IQS626_RX_INACTIVE_VREG,
185 enum iqs626_st_offs {
188 IQS626_ST_OFFS_TOUCH,
192 enum iqs626_th_offs {
194 IQS626_TH_OFFS_TOUCH,
198 enum iqs626_event_id {
199 IQS626_EVENT_PROX_DN,
200 IQS626_EVENT_PROX_UP,
201 IQS626_EVENT_TOUCH_DN,
202 IQS626_EVENT_TOUCH_UP,
203 IQS626_EVENT_DEEP_DN,
204 IQS626_EVENT_DEEP_UP,
207 enum iqs626_gesture_id {
208 IQS626_GESTURE_FLICK_X_POS,
209 IQS626_GESTURE_FLICK_X_NEG,
210 IQS626_GESTURE_FLICK_Y_POS,
211 IQS626_GESTURE_FLICK_Y_NEG,
217 struct iqs626_event_desc {
219 enum iqs626_st_offs st_offs;
220 enum iqs626_th_offs th_offs;
225 static const struct iqs626_event_desc iqs626_events[] = {
226 [IQS626_EVENT_PROX_DN] = {
227 .name = "event-prox",
228 .st_offs = IQS626_ST_OFFS_PROX,
229 .th_offs = IQS626_TH_OFFS_PROX,
230 .mask = IQS626_EVENT_MASK_PROX,
232 [IQS626_EVENT_PROX_UP] = {
233 .name = "event-prox-alt",
234 .st_offs = IQS626_ST_OFFS_PROX,
235 .th_offs = IQS626_TH_OFFS_PROX,
237 .mask = IQS626_EVENT_MASK_PROX,
239 [IQS626_EVENT_TOUCH_DN] = {
240 .name = "event-touch",
241 .st_offs = IQS626_ST_OFFS_TOUCH,
242 .th_offs = IQS626_TH_OFFS_TOUCH,
243 .mask = IQS626_EVENT_MASK_TOUCH,
245 [IQS626_EVENT_TOUCH_UP] = {
246 .name = "event-touch-alt",
247 .st_offs = IQS626_ST_OFFS_TOUCH,
248 .th_offs = IQS626_TH_OFFS_TOUCH,
250 .mask = IQS626_EVENT_MASK_TOUCH,
252 [IQS626_EVENT_DEEP_DN] = {
253 .name = "event-deep",
254 .st_offs = IQS626_ST_OFFS_DEEP,
255 .th_offs = IQS626_TH_OFFS_DEEP,
256 .mask = IQS626_EVENT_MASK_DEEP,
258 [IQS626_EVENT_DEEP_UP] = {
259 .name = "event-deep-alt",
260 .st_offs = IQS626_ST_OFFS_DEEP,
261 .th_offs = IQS626_TH_OFFS_DEEP,
263 .mask = IQS626_EVENT_MASK_DEEP,
267 struct iqs626_ver_info {
274 struct iqs626_flags {
287 struct iqs626_ch_reg_ulp {
299 struct iqs626_ch_reg_tp {
305 struct iqs626_tp_grp_reg {
309 struct iqs626_ch_reg_tp ch_reg_tp[IQS626_NUM_CH_TP_3];
312 struct iqs626_ch_reg_gen {
326 struct iqs626_ch_reg_hall {
334 struct iqs626_sys_reg {
352 struct iqs626_ch_reg_ulp ch_reg_ulp;
353 struct iqs626_tp_grp_reg tp_grp_reg;
354 struct iqs626_ch_reg_gen ch_reg_gen[IQS626_NUM_CH_GEN];
355 struct iqs626_ch_reg_hall ch_reg_hall;
358 struct iqs626_channel_desc {
362 bool events[ARRAY_SIZE(iqs626_events)];
365 static const struct iqs626_channel_desc iqs626_channels[] = {
366 [IQS626_CH_ULP_0] = {
371 [IQS626_EVENT_PROX_DN] = true,
372 [IQS626_EVENT_PROX_UP] = true,
373 [IQS626_EVENT_TOUCH_DN] = true,
374 [IQS626_EVENT_TOUCH_UP] = true,
378 .name = "trackpad-3x2",
379 .num_ch = IQS626_NUM_CH_TP_2,
382 [IQS626_EVENT_TOUCH_DN] = true,
386 .name = "trackpad-3x3",
387 .num_ch = IQS626_NUM_CH_TP_3,
388 .active = BIT(2) | BIT(1),
390 [IQS626_EVENT_TOUCH_DN] = true,
393 [IQS626_CH_GEN_0] = {
398 [IQS626_EVENT_PROX_DN] = true,
399 [IQS626_EVENT_PROX_UP] = true,
400 [IQS626_EVENT_TOUCH_DN] = true,
401 [IQS626_EVENT_TOUCH_UP] = true,
402 [IQS626_EVENT_DEEP_DN] = true,
403 [IQS626_EVENT_DEEP_UP] = true,
406 [IQS626_CH_GEN_1] = {
411 [IQS626_EVENT_PROX_DN] = true,
412 [IQS626_EVENT_PROX_UP] = true,
413 [IQS626_EVENT_TOUCH_DN] = true,
414 [IQS626_EVENT_TOUCH_UP] = true,
415 [IQS626_EVENT_DEEP_DN] = true,
416 [IQS626_EVENT_DEEP_UP] = true,
419 [IQS626_CH_GEN_2] = {
424 [IQS626_EVENT_PROX_DN] = true,
425 [IQS626_EVENT_PROX_UP] = true,
426 [IQS626_EVENT_TOUCH_DN] = true,
427 [IQS626_EVENT_TOUCH_UP] = true,
428 [IQS626_EVENT_DEEP_DN] = true,
429 [IQS626_EVENT_DEEP_UP] = true,
437 [IQS626_EVENT_TOUCH_DN] = true,
438 [IQS626_EVENT_TOUCH_UP] = true,
443 struct iqs626_private {
444 struct i2c_client *client;
445 struct regmap *regmap;
446 struct iqs626_sys_reg sys_reg;
447 struct completion ati_done;
448 struct input_dev *keypad;
449 struct input_dev *trackpad;
450 struct touchscreen_properties prop;
451 unsigned int kp_type[ARRAY_SIZE(iqs626_channels)]
452 [ARRAY_SIZE(iqs626_events)];
453 unsigned int kp_code[ARRAY_SIZE(iqs626_channels)]
454 [ARRAY_SIZE(iqs626_events)];
455 unsigned int tp_code[IQS626_NUM_GESTURES];
456 unsigned int suspend_mode;
459 static int iqs626_parse_events(struct iqs626_private *iqs626,
460 const struct fwnode_handle *ch_node,
461 enum iqs626_ch_id ch_id)
463 struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
464 struct i2c_client *client = iqs626->client;
465 const struct fwnode_handle *ev_node;
468 unsigned int thresh_tp[IQS626_NUM_CH_TP_3];
470 int num_ch = iqs626_channels[ch_id].num_ch;
474 case IQS626_CH_ULP_0:
475 thresh = sys_reg->ch_reg_ulp.thresh;
476 hyst = &sys_reg->ch_reg_ulp.hyst;
481 thresh = &sys_reg->tp_grp_reg.ch_reg_tp[0].thresh;
482 hyst = &sys_reg->tp_grp_reg.hyst;
485 case IQS626_CH_GEN_0:
486 case IQS626_CH_GEN_1:
487 case IQS626_CH_GEN_2:
488 i = ch_id - IQS626_CH_GEN_0;
489 thresh = sys_reg->ch_reg_gen[i].thresh;
490 hyst = &sys_reg->ch_reg_gen[i].hyst;
494 thresh = &sys_reg->ch_reg_hall.thresh;
495 hyst = &sys_reg->ch_reg_hall.hyst;
502 for (i = 0; i < ARRAY_SIZE(iqs626_events); i++) {
503 if (!iqs626_channels[ch_id].events[i])
506 if (ch_id == IQS626_CH_TP_2 || ch_id == IQS626_CH_TP_3) {
508 * Trackpad touch events are simply described under the
509 * trackpad child node.
513 ev_name = iqs626_events[i].name;
514 ev_node = fwnode_get_named_child_node(ch_node, ev_name);
518 if (!fwnode_property_read_u32(ev_node, "linux,code",
520 iqs626->kp_code[ch_id][i] = val;
522 if (fwnode_property_read_u32(ev_node,
525 if (ch_id == IQS626_CH_HALL)
531 if (val != EV_KEY && val != EV_SW) {
532 dev_err(&client->dev,
533 "Invalid input type: %u\n",
538 iqs626->kp_type[ch_id][i] = val;
540 sys_reg->event_mask &= ~iqs626_events[i].mask;
544 if (!fwnode_property_read_u32(ev_node, "azoteq,hyst", &val)) {
545 if (val > IQS626_CHx_HYST_MAX) {
546 dev_err(&client->dev,
547 "Invalid %s channel hysteresis: %u\n",
548 fwnode_get_name(ch_node), val);
552 if (i == IQS626_EVENT_DEEP_DN ||
553 i == IQS626_EVENT_DEEP_UP) {
554 *hyst &= ~IQS626_CHx_HYST_DEEP_MASK;
555 *hyst |= (val << IQS626_CHx_HYST_DEEP_SHIFT);
556 } else if (i == IQS626_EVENT_TOUCH_DN ||
557 i == IQS626_EVENT_TOUCH_UP) {
558 *hyst &= ~IQS626_CHx_HYST_TOUCH_MASK;
563 if (ch_id != IQS626_CH_TP_2 && ch_id != IQS626_CH_TP_3 &&
564 !fwnode_property_read_u32(ev_node, "azoteq,thresh", &val)) {
565 if (val > IQS626_CHx_THRESH_MAX) {
566 dev_err(&client->dev,
567 "Invalid %s channel threshold: %u\n",
568 fwnode_get_name(ch_node), val);
572 if (ch_id == IQS626_CH_HALL)
575 *(thresh + iqs626_events[i].th_offs) = val;
580 if (!fwnode_property_present(ev_node, "azoteq,thresh"))
583 error = fwnode_property_read_u32_array(ev_node, "azoteq,thresh",
586 dev_err(&client->dev,
587 "Failed to read %s channel thresholds: %d\n",
588 fwnode_get_name(ch_node), error);
592 for (j = 0; j < num_ch; j++) {
593 if (thresh_tp[j] > IQS626_CHx_THRESH_MAX) {
594 dev_err(&client->dev,
595 "Invalid %s channel threshold: %u\n",
596 fwnode_get_name(ch_node), thresh_tp[j]);
600 sys_reg->tp_grp_reg.ch_reg_tp[j].thresh = thresh_tp[j];
607 static int iqs626_parse_ati_target(struct iqs626_private *iqs626,
608 const struct fwnode_handle *ch_node,
609 enum iqs626_ch_id ch_id)
611 struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
612 struct i2c_client *client = iqs626->client;
613 unsigned int ati_base[IQS626_NUM_CH_TP_3];
616 int num_ch = iqs626_channels[ch_id].num_ch;
620 case IQS626_CH_ULP_0:
621 ati_target = &sys_reg->ch_reg_ulp.ati_target;
626 ati_target = &sys_reg->tp_grp_reg.ati_target;
629 case IQS626_CH_GEN_0:
630 case IQS626_CH_GEN_1:
631 case IQS626_CH_GEN_2:
632 i = ch_id - IQS626_CH_GEN_0;
633 ati_target = &sys_reg->ch_reg_gen[i].ati_target;
637 ati_target = &sys_reg->ch_reg_hall.ati_target;
644 if (!fwnode_property_read_u32(ch_node, "azoteq,ati-target", &val)) {
645 if (val > IQS626_CHx_ATI_TARGET_MAX) {
646 dev_err(&client->dev,
647 "Invalid %s channel ATI target: %u\n",
648 fwnode_get_name(ch_node), val);
652 *ati_target &= ~IQS626_CHx_ATI_TARGET_MASK;
653 *ati_target |= (val / 32);
656 if (ch_id != IQS626_CH_TP_2 && ch_id != IQS626_CH_TP_3 &&
657 !fwnode_property_read_u32(ch_node, "azoteq,ati-base", &val)) {
660 val = IQS626_CHx_ATI_BASE_75;
664 val = IQS626_CHx_ATI_BASE_100;
668 val = IQS626_CHx_ATI_BASE_150;
672 val = IQS626_CHx_ATI_BASE_200;
676 dev_err(&client->dev,
677 "Invalid %s channel ATI base: %u\n",
678 fwnode_get_name(ch_node), val);
682 *ati_target &= ~IQS626_CHx_ATI_BASE_MASK;
688 if (!fwnode_property_present(ch_node, "azoteq,ati-base"))
691 error = fwnode_property_read_u32_array(ch_node, "azoteq,ati-base",
694 dev_err(&client->dev,
695 "Failed to read %s channel ATI bases: %d\n",
696 fwnode_get_name(ch_node), error);
700 for (i = 0; i < num_ch; i++) {
701 if (ati_base[i] < IQS626_TPx_ATI_BASE_MIN ||
702 ati_base[i] > IQS626_TPx_ATI_BASE_MAX) {
703 dev_err(&client->dev,
704 "Invalid %s channel ATI base: %u\n",
705 fwnode_get_name(ch_node), ati_base[i]);
709 ati_base[i] -= IQS626_TPx_ATI_BASE_MIN;
710 sys_reg->tp_grp_reg.ch_reg_tp[i].ati_base = ati_base[i];
716 static int iqs626_parse_pins(struct iqs626_private *iqs626,
717 const struct fwnode_handle *ch_node,
718 const char *propname, u8 *enable)
720 struct i2c_client *client = iqs626->client;
721 unsigned int val[IQS626_NUM_CRx_TX];
724 if (!fwnode_property_present(ch_node, propname))
727 count = fwnode_property_count_u32(ch_node, propname);
728 if (count > IQS626_NUM_CRx_TX) {
729 dev_err(&client->dev,
730 "Too many %s channel CRX/TX pins present\n",
731 fwnode_get_name(ch_node));
733 } else if (count < 0) {
734 dev_err(&client->dev,
735 "Failed to count %s channel CRX/TX pins: %d\n",
736 fwnode_get_name(ch_node), count);
740 error = fwnode_property_read_u32_array(ch_node, propname, val, count);
742 dev_err(&client->dev,
743 "Failed to read %s channel CRX/TX pins: %d\n",
744 fwnode_get_name(ch_node), error);
750 for (i = 0; i < count; i++) {
751 if (val[i] >= IQS626_NUM_CRx_TX) {
752 dev_err(&client->dev,
753 "Invalid %s channel CRX/TX pin: %u\n",
754 fwnode_get_name(ch_node), val[i]);
758 *enable |= BIT(val[i]);
764 static int iqs626_parse_trackpad(struct iqs626_private *iqs626,
765 const struct fwnode_handle *ch_node)
767 struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
768 struct i2c_client *client = iqs626->client;
769 u8 *hyst = &sys_reg->tp_grp_reg.hyst;
773 if (!fwnode_property_read_u32(ch_node, "azoteq,lta-update", &val)) {
774 if (val > IQS626_MISC_A_TPx_LTA_UPDATE_MAX) {
775 dev_err(&client->dev,
776 "Invalid %s channel update rate: %u\n",
777 fwnode_get_name(ch_node), val);
781 sys_reg->misc_a &= ~IQS626_MISC_A_TPx_LTA_UPDATE_MASK;
782 sys_reg->misc_a |= (val << IQS626_MISC_A_TPx_LTA_UPDATE_SHIFT);
785 if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-trackpad",
787 if (val > IQS626_FILT_STR_MAX) {
788 dev_err(&client->dev,
789 "Invalid %s channel filter strength: %u\n",
790 fwnode_get_name(ch_node), val);
794 sys_reg->misc_b &= ~IQS626_MISC_B_FILT_STR_TPx;
795 sys_reg->misc_b |= val;
798 if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-np-cnt",
800 if (val > IQS626_FILT_STR_MAX) {
801 dev_err(&client->dev,
802 "Invalid %s channel filter strength: %u\n",
803 fwnode_get_name(ch_node), val);
807 *hyst &= ~IQS626_FILT_STR_NP_TPx_MASK;
808 *hyst |= (val << IQS626_FILT_STR_NP_TPx_SHIFT);
811 if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-lp-cnt",
813 if (val > IQS626_FILT_STR_MAX) {
814 dev_err(&client->dev,
815 "Invalid %s channel filter strength: %u\n",
816 fwnode_get_name(ch_node), val);
820 *hyst &= ~IQS626_FILT_STR_LP_TPx_MASK;
821 *hyst |= (val << IQS626_FILT_STR_LP_TPx_SHIFT);
824 if (!fwnode_property_present(ch_node, "linux,keycodes"))
827 count = fwnode_property_count_u32(ch_node, "linux,keycodes");
828 if (count > IQS626_NUM_GESTURES) {
829 dev_err(&client->dev, "Too many keycodes present\n");
831 } else if (count < 0) {
832 dev_err(&client->dev, "Failed to count keycodes: %d\n", count);
836 error = fwnode_property_read_u32_array(ch_node, "linux,keycodes",
837 iqs626->tp_code, count);
839 dev_err(&client->dev, "Failed to read keycodes: %d\n", error);
843 sys_reg->misc_b &= ~IQS626_MISC_B_TPx_SWIPE;
844 if (fwnode_property_present(ch_node, "azoteq,gesture-swipe"))
845 sys_reg->misc_b |= IQS626_MISC_B_TPx_SWIPE;
847 if (!fwnode_property_read_u32(ch_node, "azoteq,timeout-tap-ms",
849 if (val > IQS626_TIMEOUT_TAP_MS_MAX) {
850 dev_err(&client->dev,
851 "Invalid %s channel timeout: %u\n",
852 fwnode_get_name(ch_node), val);
856 sys_reg->timeout_tap = val / 16;
859 if (!fwnode_property_read_u32(ch_node, "azoteq,timeout-swipe-ms",
861 if (val > IQS626_TIMEOUT_SWIPE_MS_MAX) {
862 dev_err(&client->dev,
863 "Invalid %s channel timeout: %u\n",
864 fwnode_get_name(ch_node), val);
868 sys_reg->timeout_swipe = val / 16;
871 if (!fwnode_property_read_u32(ch_node, "azoteq,thresh-swipe",
873 if (val > IQS626_THRESH_SWIPE_MAX) {
874 dev_err(&client->dev,
875 "Invalid %s channel threshold: %u\n",
876 fwnode_get_name(ch_node), val);
880 sys_reg->thresh_swipe = val;
883 sys_reg->event_mask &= ~IQS626_EVENT_MASK_GESTURE;
888 static int iqs626_parse_channel(struct iqs626_private *iqs626,
889 const struct fwnode_handle *ch_node,
890 enum iqs626_ch_id ch_id)
892 struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
893 struct i2c_client *client = iqs626->client;
894 u8 *engine, *filter, *rx_enable, *tx_enable;
895 u8 *assoc_select, *assoc_weight;
900 case IQS626_CH_ULP_0:
901 engine = sys_reg->ch_reg_ulp.engine;
906 engine = sys_reg->tp_grp_reg.engine;
909 case IQS626_CH_GEN_0:
910 case IQS626_CH_GEN_1:
911 case IQS626_CH_GEN_2:
912 i = ch_id - IQS626_CH_GEN_0;
913 engine = sys_reg->ch_reg_gen[i].engine;
917 engine = &sys_reg->ch_reg_hall.engine;
924 *engine |= IQS626_CHx_ENG_0_MEAS_CAP_SIZE;
925 if (fwnode_property_present(ch_node, "azoteq,meas-cap-decrease"))
926 *engine &= ~IQS626_CHx_ENG_0_MEAS_CAP_SIZE;
928 *engine |= IQS626_CHx_ENG_0_RX_TERM_VSS;
929 if (!fwnode_property_read_u32(ch_node, "azoteq,rx-inactive", &val)) {
931 case IQS626_RX_INACTIVE_VSS:
934 case IQS626_RX_INACTIVE_FLOAT:
935 *engine &= ~IQS626_CHx_ENG_0_RX_TERM_VSS;
936 if (ch_id == IQS626_CH_GEN_0 ||
937 ch_id == IQS626_CH_GEN_1 ||
938 ch_id == IQS626_CH_GEN_2)
939 *(engine + 4) &= ~IQS626_CHx_ENG_4_RX_TERM_VREG;
942 case IQS626_RX_INACTIVE_VREG:
943 if (ch_id == IQS626_CH_GEN_0 ||
944 ch_id == IQS626_CH_GEN_1 ||
945 ch_id == IQS626_CH_GEN_2) {
946 *engine &= ~IQS626_CHx_ENG_0_RX_TERM_VSS;
947 *(engine + 4) |= IQS626_CHx_ENG_4_RX_TERM_VREG;
953 dev_err(&client->dev,
954 "Invalid %s channel CRX pin termination: %u\n",
955 fwnode_get_name(ch_node), val);
960 *engine &= ~IQS626_CHx_ENG_0_LINEARIZE;
961 if (fwnode_property_present(ch_node, "azoteq,linearize"))
962 *engine |= IQS626_CHx_ENG_0_LINEARIZE;
964 *engine &= ~IQS626_CHx_ENG_0_DUAL_DIR;
965 if (fwnode_property_present(ch_node, "azoteq,dual-direction"))
966 *engine |= IQS626_CHx_ENG_0_DUAL_DIR;
968 *engine &= ~IQS626_CHx_ENG_0_FILT_DISABLE;
969 if (fwnode_property_present(ch_node, "azoteq,filt-disable"))
970 *engine |= IQS626_CHx_ENG_0_FILT_DISABLE;
972 if (!fwnode_property_read_u32(ch_node, "azoteq,ati-mode", &val)) {
973 if (val > IQS626_CHx_ENG_0_ATI_MODE_MAX) {
974 dev_err(&client->dev,
975 "Invalid %s channel ATI mode: %u\n",
976 fwnode_get_name(ch_node), val);
980 *engine &= ~IQS626_CHx_ENG_0_ATI_MODE_MASK;
984 if (ch_id == IQS626_CH_HALL)
987 *(engine + 1) &= ~IQS626_CHx_ENG_1_CCT_ENABLE;
988 if (!fwnode_property_read_u32(ch_node, "azoteq,cct-increase",
990 unsigned int orig_val = val--;
993 * In the case of the generic channels, the charge cycle time
994 * field doubles in size and straddles two separate registers.
996 if (ch_id == IQS626_CH_GEN_0 ||
997 ch_id == IQS626_CH_GEN_1 ||
998 ch_id == IQS626_CH_GEN_2) {
999 *(engine + 4) &= ~IQS626_CHx_ENG_4_CCT_LOW_1;
1001 *(engine + 4) |= IQS626_CHx_ENG_4_CCT_LOW_1;
1003 *(engine + 4) &= ~IQS626_CHx_ENG_4_CCT_LOW_0;
1005 *(engine + 4) |= IQS626_CHx_ENG_4_CCT_LOW_0;
1010 if (val & ~GENMASK(1, 0)) {
1011 dev_err(&client->dev,
1012 "Invalid %s channel charge cycle time: %u\n",
1013 fwnode_get_name(ch_node), orig_val);
1017 *(engine + 1) &= ~IQS626_CHx_ENG_1_CCT_HIGH_1;
1019 *(engine + 1) |= IQS626_CHx_ENG_1_CCT_HIGH_1;
1021 *(engine + 1) &= ~IQS626_CHx_ENG_1_CCT_HIGH_0;
1023 *(engine + 1) |= IQS626_CHx_ENG_1_CCT_HIGH_0;
1025 *(engine + 1) |= IQS626_CHx_ENG_1_CCT_ENABLE;
1028 if (!fwnode_property_read_u32(ch_node, "azoteq,proj-bias", &val)) {
1029 if (val > IQS626_CHx_ENG_1_PROJ_BIAS_MAX) {
1030 dev_err(&client->dev,
1031 "Invalid %s channel bias current: %u\n",
1032 fwnode_get_name(ch_node), val);
1036 *(engine + 1) &= ~IQS626_CHx_ENG_1_PROJ_BIAS_MASK;
1037 *(engine + 1) |= (val << IQS626_CHx_ENG_1_PROJ_BIAS_SHIFT);
1040 if (!fwnode_property_read_u32(ch_node, "azoteq,sense-freq", &val)) {
1041 if (val > IQS626_CHx_ENG_1_SENSE_FREQ_MAX) {
1042 dev_err(&client->dev,
1043 "Invalid %s channel sensing frequency: %u\n",
1044 fwnode_get_name(ch_node), val);
1048 *(engine + 1) &= ~IQS626_CHx_ENG_1_SENSE_FREQ_MASK;
1049 *(engine + 1) |= (val << IQS626_CHx_ENG_1_SENSE_FREQ_SHIFT);
1052 *(engine + 1) &= ~IQS626_CHx_ENG_1_ATI_BAND_TIGHTEN;
1053 if (fwnode_property_present(ch_node, "azoteq,ati-band-tighten"))
1054 *(engine + 1) |= IQS626_CHx_ENG_1_ATI_BAND_TIGHTEN;
1056 if (ch_id == IQS626_CH_TP_2 || ch_id == IQS626_CH_TP_3)
1057 return iqs626_parse_trackpad(iqs626, ch_node);
1059 if (ch_id == IQS626_CH_ULP_0) {
1060 sys_reg->ch_reg_ulp.hyst &= ~IQS626_ULP_PROJ_ENABLE;
1061 if (fwnode_property_present(ch_node, "azoteq,proj-enable"))
1062 sys_reg->ch_reg_ulp.hyst |= IQS626_ULP_PROJ_ENABLE;
1064 filter = &sys_reg->ch_reg_ulp.filter;
1066 rx_enable = &sys_reg->ch_reg_ulp.rx_enable;
1067 tx_enable = &sys_reg->ch_reg_ulp.tx_enable;
1069 i = ch_id - IQS626_CH_GEN_0;
1070 filter = &sys_reg->ch_reg_gen[i].filter;
1072 rx_enable = &sys_reg->ch_reg_gen[i].rx_enable;
1073 tx_enable = &sys_reg->ch_reg_gen[i].tx_enable;
1076 if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-np-cnt",
1078 if (val > IQS626_FILT_STR_MAX) {
1079 dev_err(&client->dev,
1080 "Invalid %s channel filter strength: %u\n",
1081 fwnode_get_name(ch_node), val);
1085 *filter &= ~IQS626_FILT_STR_NP_CNT_MASK;
1086 *filter |= (val << IQS626_FILT_STR_NP_CNT_SHIFT);
1089 if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-lp-cnt",
1091 if (val > IQS626_FILT_STR_MAX) {
1092 dev_err(&client->dev,
1093 "Invalid %s channel filter strength: %u\n",
1094 fwnode_get_name(ch_node), val);
1098 *filter &= ~IQS626_FILT_STR_LP_CNT_MASK;
1099 *filter |= (val << IQS626_FILT_STR_LP_CNT_SHIFT);
1102 if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-np-lta",
1104 if (val > IQS626_FILT_STR_MAX) {
1105 dev_err(&client->dev,
1106 "Invalid %s channel filter strength: %u\n",
1107 fwnode_get_name(ch_node), val);
1111 *filter &= ~IQS626_FILT_STR_NP_LTA_MASK;
1112 *filter |= (val << IQS626_FILT_STR_NP_LTA_SHIFT);
1115 if (!fwnode_property_read_u32(ch_node, "azoteq,filt-str-lp-lta",
1117 if (val > IQS626_FILT_STR_MAX) {
1118 dev_err(&client->dev,
1119 "Invalid %s channel filter strength: %u\n",
1120 fwnode_get_name(ch_node), val);
1124 *filter &= ~IQS626_FILT_STR_LP_LTA_MASK;
1128 error = iqs626_parse_pins(iqs626, ch_node, "azoteq,rx-enable",
1133 error = iqs626_parse_pins(iqs626, ch_node, "azoteq,tx-enable",
1138 if (ch_id == IQS626_CH_ULP_0)
1141 *(engine + 2) &= ~IQS626_CHx_ENG_2_LOCAL_CAP_ENABLE;
1142 if (!fwnode_property_read_u32(ch_node, "azoteq,local-cap-size",
1144 unsigned int orig_val = val--;
1146 if (val > IQS626_CHx_ENG_2_LOCAL_CAP_MAX) {
1147 dev_err(&client->dev,
1148 "Invalid %s channel local cap. size: %u\n",
1149 fwnode_get_name(ch_node), orig_val);
1153 *(engine + 2) &= ~IQS626_CHx_ENG_2_LOCAL_CAP_MASK;
1154 *(engine + 2) |= (val << IQS626_CHx_ENG_2_LOCAL_CAP_SHIFT);
1156 *(engine + 2) |= IQS626_CHx_ENG_2_LOCAL_CAP_ENABLE;
1159 if (!fwnode_property_read_u32(ch_node, "azoteq,sense-mode", &val)) {
1160 if (val > IQS626_CHx_ENG_2_SENSE_MODE_MAX) {
1161 dev_err(&client->dev,
1162 "Invalid %s channel sensing mode: %u\n",
1163 fwnode_get_name(ch_node), val);
1167 *(engine + 2) &= ~IQS626_CHx_ENG_2_SENSE_MODE_MASK;
1168 *(engine + 2) |= val;
1171 if (!fwnode_property_read_u32(ch_node, "azoteq,tx-freq", &val)) {
1172 if (val > IQS626_CHx_ENG_3_TX_FREQ_MAX) {
1173 dev_err(&client->dev,
1174 "Invalid %s channel excitation frequency: %u\n",
1175 fwnode_get_name(ch_node), val);
1179 *(engine + 3) &= ~IQS626_CHx_ENG_3_TX_FREQ_MASK;
1180 *(engine + 3) |= (val << IQS626_CHx_ENG_3_TX_FREQ_SHIFT);
1183 *(engine + 3) &= ~IQS626_CHx_ENG_3_INV_LOGIC;
1184 if (fwnode_property_present(ch_node, "azoteq,invert-enable"))
1185 *(engine + 3) |= IQS626_CHx_ENG_3_INV_LOGIC;
1187 *(engine + 4) &= ~IQS626_CHx_ENG_4_COMP_DISABLE;
1188 if (fwnode_property_present(ch_node, "azoteq,comp-disable"))
1189 *(engine + 4) |= IQS626_CHx_ENG_4_COMP_DISABLE;
1191 *(engine + 4) &= ~IQS626_CHx_ENG_4_STATIC_ENABLE;
1192 if (fwnode_property_present(ch_node, "azoteq,static-enable"))
1193 *(engine + 4) |= IQS626_CHx_ENG_4_STATIC_ENABLE;
1195 i = ch_id - IQS626_CH_GEN_0;
1196 assoc_select = &sys_reg->ch_reg_gen[i].assoc_select;
1197 assoc_weight = &sys_reg->ch_reg_gen[i].assoc_weight;
1200 if (!fwnode_property_present(ch_node, "azoteq,assoc-select"))
1203 for (i = 0; i < ARRAY_SIZE(iqs626_channels); i++) {
1204 if (fwnode_property_match_string(ch_node, "azoteq,assoc-select",
1205 iqs626_channels[i].name) < 0)
1208 *assoc_select |= iqs626_channels[i].active;
1211 if (fwnode_property_read_u32(ch_node, "azoteq,assoc-weight", &val))
1214 if (val > IQS626_GEN_WEIGHT_MAX) {
1215 dev_err(&client->dev,
1216 "Invalid %s channel associated weight: %u\n",
1217 fwnode_get_name(ch_node), val);
1221 *assoc_weight = val;
1226 static int iqs626_parse_prop(struct iqs626_private *iqs626)
1228 struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
1229 struct i2c_client *client = iqs626->client;
1230 struct fwnode_handle *ch_node;
1235 if (!device_property_read_u32(&client->dev, "azoteq,suspend-mode",
1237 if (val > IQS626_SYS_SETTINGS_PWR_MODE_MAX) {
1238 dev_err(&client->dev, "Invalid suspend mode: %u\n",
1243 iqs626->suspend_mode = val;
1246 error = regmap_raw_read(iqs626->regmap, IQS626_SYS_SETTINGS, sys_reg,
1251 general = be16_to_cpu(sys_reg->general);
1252 general &= IQS626_SYS_SETTINGS_ULP_UPDATE_MASK;
1254 if (device_property_present(&client->dev, "azoteq,clk-div"))
1255 general |= IQS626_SYS_SETTINGS_CLK_DIV;
1257 if (device_property_present(&client->dev, "azoteq,ulp-enable"))
1258 general |= IQS626_SYS_SETTINGS_ULP_AUTO;
1260 if (!device_property_read_u32(&client->dev, "azoteq,ulp-update",
1262 if (val > IQS626_SYS_SETTINGS_ULP_UPDATE_MAX) {
1263 dev_err(&client->dev, "Invalid update rate: %u\n", val);
1267 general &= ~IQS626_SYS_SETTINGS_ULP_UPDATE_MASK;
1268 general |= (val << IQS626_SYS_SETTINGS_ULP_UPDATE_SHIFT);
1271 sys_reg->misc_a &= ~IQS626_MISC_A_ATI_BAND_DISABLE;
1272 if (device_property_present(&client->dev, "azoteq,ati-band-disable"))
1273 sys_reg->misc_a |= IQS626_MISC_A_ATI_BAND_DISABLE;
1275 sys_reg->misc_a &= ~IQS626_MISC_A_ATI_LP_ONLY;
1276 if (device_property_present(&client->dev, "azoteq,ati-lp-only"))
1277 sys_reg->misc_a |= IQS626_MISC_A_ATI_LP_ONLY;
1279 if (!device_property_read_u32(&client->dev, "azoteq,gpio3-select",
1281 if (val > IQS626_MISC_A_GPIO3_SELECT_MAX) {
1282 dev_err(&client->dev, "Invalid GPIO3 selection: %u\n",
1287 sys_reg->misc_a &= ~IQS626_MISC_A_GPIO3_SELECT_MASK;
1288 sys_reg->misc_a |= val;
1291 if (!device_property_read_u32(&client->dev, "azoteq,reseed-select",
1293 if (val > IQS626_MISC_B_RESEED_UI_SEL_MAX) {
1294 dev_err(&client->dev, "Invalid reseed selection: %u\n",
1299 sys_reg->misc_b &= ~IQS626_MISC_B_RESEED_UI_SEL_MASK;
1300 sys_reg->misc_b |= (val << IQS626_MISC_B_RESEED_UI_SEL_SHIFT);
1303 sys_reg->misc_b &= ~IQS626_MISC_B_THRESH_EXTEND;
1304 if (device_property_present(&client->dev, "azoteq,thresh-extend"))
1305 sys_reg->misc_b |= IQS626_MISC_B_THRESH_EXTEND;
1307 sys_reg->misc_b &= ~IQS626_MISC_B_TRACKING_UI_ENABLE;
1308 if (device_property_present(&client->dev, "azoteq,tracking-enable"))
1309 sys_reg->misc_b |= IQS626_MISC_B_TRACKING_UI_ENABLE;
1311 sys_reg->misc_b &= ~IQS626_MISC_B_RESEED_OFFSET;
1312 if (device_property_present(&client->dev, "azoteq,reseed-offset"))
1313 sys_reg->misc_b |= IQS626_MISC_B_RESEED_OFFSET;
1315 if (!device_property_read_u32(&client->dev, "azoteq,rate-np-ms",
1317 if (val > IQS626_RATE_NP_MS_MAX) {
1318 dev_err(&client->dev, "Invalid report rate: %u\n", val);
1322 sys_reg->rate_np = val;
1325 if (!device_property_read_u32(&client->dev, "azoteq,rate-lp-ms",
1327 if (val > IQS626_RATE_LP_MS_MAX) {
1328 dev_err(&client->dev, "Invalid report rate: %u\n", val);
1332 sys_reg->rate_lp = val;
1335 if (!device_property_read_u32(&client->dev, "azoteq,rate-ulp-ms",
1337 if (val > IQS626_RATE_ULP_MS_MAX) {
1338 dev_err(&client->dev, "Invalid report rate: %u\n", val);
1342 sys_reg->rate_ulp = val / 16;
1345 if (!device_property_read_u32(&client->dev, "azoteq,timeout-pwr-ms",
1347 if (val > IQS626_TIMEOUT_PWR_MS_MAX) {
1348 dev_err(&client->dev, "Invalid timeout: %u\n", val);
1352 sys_reg->timeout_pwr = val / 512;
1355 if (!device_property_read_u32(&client->dev, "azoteq,timeout-lta-ms",
1357 if (val > IQS626_TIMEOUT_LTA_MS_MAX) {
1358 dev_err(&client->dev, "Invalid timeout: %u\n", val);
1362 sys_reg->timeout_lta = val / 512;
1365 sys_reg->event_mask = ~((u8)IQS626_EVENT_MASK_SYS);
1366 sys_reg->redo_ati = 0;
1368 sys_reg->reseed = 0;
1369 sys_reg->active = 0;
1371 for (i = 0; i < ARRAY_SIZE(iqs626_channels); i++) {
1372 ch_node = device_get_named_child_node(&client->dev,
1373 iqs626_channels[i].name);
1377 error = iqs626_parse_channel(iqs626, ch_node, i);
1381 error = iqs626_parse_ati_target(iqs626, ch_node, i);
1385 error = iqs626_parse_events(iqs626, ch_node, i);
1389 if (!fwnode_property_present(ch_node, "azoteq,ati-exclude"))
1390 sys_reg->redo_ati |= iqs626_channels[i].active;
1392 if (!fwnode_property_present(ch_node, "azoteq,reseed-disable"))
1393 sys_reg->reseed |= iqs626_channels[i].active;
1395 sys_reg->active |= iqs626_channels[i].active;
1398 general |= IQS626_SYS_SETTINGS_EVENT_MODE;
1401 * Enable streaming during normal-power mode if the trackpad is used to
1402 * report raw coordinates instead of gestures. In that case, the device
1403 * returns to event mode during low-power mode.
1405 if (sys_reg->active & iqs626_channels[IQS626_CH_TP_2].active &&
1406 sys_reg->event_mask & IQS626_EVENT_MASK_GESTURE)
1407 general |= IQS626_SYS_SETTINGS_EVENT_MODE_LP;
1409 general |= IQS626_SYS_SETTINGS_REDO_ATI;
1410 general |= IQS626_SYS_SETTINGS_ACK_RESET;
1412 sys_reg->general = cpu_to_be16(general);
1414 error = regmap_raw_write(iqs626->regmap, IQS626_SYS_SETTINGS,
1415 &iqs626->sys_reg, sizeof(iqs626->sys_reg));
1424 static int iqs626_input_init(struct iqs626_private *iqs626)
1426 struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
1427 struct i2c_client *client = iqs626->client;
1430 iqs626->keypad = devm_input_allocate_device(&client->dev);
1431 if (!iqs626->keypad)
1434 iqs626->keypad->keycodemax = ARRAY_SIZE(iqs626->kp_code);
1435 iqs626->keypad->keycode = iqs626->kp_code;
1436 iqs626->keypad->keycodesize = sizeof(**iqs626->kp_code);
1438 iqs626->keypad->name = "iqs626a_keypad";
1439 iqs626->keypad->id.bustype = BUS_I2C;
1441 for (i = 0; i < ARRAY_SIZE(iqs626_channels); i++) {
1442 if (!(sys_reg->active & iqs626_channels[i].active))
1445 for (j = 0; j < ARRAY_SIZE(iqs626_events); j++) {
1446 if (!iqs626->kp_type[i][j])
1449 input_set_capability(iqs626->keypad,
1450 iqs626->kp_type[i][j],
1451 iqs626->kp_code[i][j]);
1455 if (!(sys_reg->active & iqs626_channels[IQS626_CH_TP_2].active))
1458 iqs626->trackpad = devm_input_allocate_device(&client->dev);
1459 if (!iqs626->trackpad)
1462 iqs626->trackpad->keycodemax = ARRAY_SIZE(iqs626->tp_code);
1463 iqs626->trackpad->keycode = iqs626->tp_code;
1464 iqs626->trackpad->keycodesize = sizeof(*iqs626->tp_code);
1466 iqs626->trackpad->name = "iqs626a_trackpad";
1467 iqs626->trackpad->id.bustype = BUS_I2C;
1470 * Present the trackpad as a traditional pointing device if no gestures
1471 * have been mapped to a keycode.
1473 if (sys_reg->event_mask & IQS626_EVENT_MASK_GESTURE) {
1474 u8 tp_mask = iqs626_channels[IQS626_CH_TP_3].active;
1476 input_set_capability(iqs626->trackpad, EV_KEY, BTN_TOUCH);
1477 input_set_abs_params(iqs626->trackpad, ABS_Y, 0, 255, 0, 0);
1479 if ((sys_reg->active & tp_mask) == tp_mask)
1480 input_set_abs_params(iqs626->trackpad,
1481 ABS_X, 0, 255, 0, 0);
1483 input_set_abs_params(iqs626->trackpad,
1484 ABS_X, 0, 128, 0, 0);
1486 touchscreen_parse_properties(iqs626->trackpad, false,
1489 for (i = 0; i < IQS626_NUM_GESTURES; i++)
1490 if (iqs626->tp_code[i] != KEY_RESERVED)
1491 input_set_capability(iqs626->trackpad, EV_KEY,
1492 iqs626->tp_code[i]);
1495 error = input_register_device(iqs626->trackpad);
1497 dev_err(&client->dev, "Failed to register trackpad: %d\n",
1503 static int iqs626_report(struct iqs626_private *iqs626)
1505 struct iqs626_sys_reg *sys_reg = &iqs626->sys_reg;
1506 struct i2c_client *client = iqs626->client;
1507 struct iqs626_flags flags;
1511 u8 *dir_mask = &flags.states[IQS626_ST_OFFS_DIR];
1513 error = regmap_raw_read(iqs626->regmap, IQS626_SYS_FLAGS, &flags,
1516 dev_err(&client->dev, "Failed to read device status: %d\n",
1522 * The device resets itself if its own watchdog bites, which can happen
1523 * in the event of an I2C communication error. In this case, the device
1524 * asserts a SHOW_RESET interrupt and all registers must be restored.
1526 if (be16_to_cpu(flags.system) & IQS626_SYS_FLAGS_SHOW_RESET) {
1527 dev_err(&client->dev, "Unexpected device reset\n");
1529 error = regmap_raw_write(iqs626->regmap, IQS626_SYS_SETTINGS,
1530 sys_reg, sizeof(*sys_reg));
1532 dev_err(&client->dev,
1533 "Failed to re-initialize device: %d\n", error);
1538 if (be16_to_cpu(flags.system) & IQS626_SYS_FLAGS_IN_ATI)
1542 * Unlike the ULP or generic channels, the Hall channel does not have a
1543 * direction flag. Instead, the direction (i.e. magnet polarity) can be
1544 * derived based on the sign of the 2's complement differential output.
1546 if (sys_reg->active & iqs626_channels[IQS626_CH_HALL].active) {
1547 error = regmap_raw_read(iqs626->regmap, IQS626_HALL_OUTPUT,
1548 &hall_output, sizeof(hall_output));
1550 dev_err(&client->dev,
1551 "Failed to read Hall output: %d\n", error);
1555 *dir_mask &= ~iqs626_channels[IQS626_CH_HALL].active;
1556 if (le16_to_cpu(hall_output) < 0x8000)
1557 *dir_mask |= iqs626_channels[IQS626_CH_HALL].active;
1560 for (i = 0; i < ARRAY_SIZE(iqs626_channels); i++) {
1561 if (!(sys_reg->active & iqs626_channels[i].active))
1564 for (j = 0; j < ARRAY_SIZE(iqs626_events); j++) {
1565 if (!iqs626->kp_type[i][j])
1568 state = flags.states[iqs626_events[j].st_offs];
1569 state &= iqs626_events[j].dir_up ? *dir_mask
1571 state &= iqs626_channels[i].active;
1573 input_event(iqs626->keypad, iqs626->kp_type[i][j],
1574 iqs626->kp_code[i][j], !!state);
1578 input_sync(iqs626->keypad);
1581 * The following completion signals that ATI has finished, any initial
1582 * switch states have been reported and the keypad can be registered.
1584 complete_all(&iqs626->ati_done);
1586 if (!(sys_reg->active & iqs626_channels[IQS626_CH_TP_2].active))
1589 if (sys_reg->event_mask & IQS626_EVENT_MASK_GESTURE) {
1590 state = flags.states[IQS626_ST_OFFS_TOUCH];
1591 state &= iqs626_channels[IQS626_CH_TP_2].active;
1593 input_report_key(iqs626->trackpad, BTN_TOUCH, state);
1596 touchscreen_report_pos(iqs626->trackpad, &iqs626->prop,
1598 flags.trackpad_y, false);
1600 for (i = 0; i < IQS626_NUM_GESTURES; i++)
1601 input_report_key(iqs626->trackpad, iqs626->tp_code[i],
1602 flags.gesture & BIT(i));
1604 if (flags.gesture & GENMASK(IQS626_GESTURE_TAP, 0)) {
1605 input_sync(iqs626->trackpad);
1608 * Momentary gestures are followed by a complementary
1609 * release cycle so as to emulate a full keystroke.
1611 for (i = 0; i < IQS626_GESTURE_HOLD; i++)
1612 input_report_key(iqs626->trackpad,
1613 iqs626->tp_code[i], 0);
1617 input_sync(iqs626->trackpad);
1622 static irqreturn_t iqs626_irq(int irq, void *context)
1624 struct iqs626_private *iqs626 = context;
1626 if (iqs626_report(iqs626))
1630 * The device does not deassert its interrupt (RDY) pin until shortly
1631 * after receiving an I2C stop condition; the following delay ensures
1632 * the interrupt handler does not return before this time.
1639 static const struct regmap_config iqs626_regmap_config = {
1642 .max_register = IQS626_MAX_REG,
1645 static int iqs626_probe(struct i2c_client *client)
1647 struct iqs626_ver_info ver_info;
1648 struct iqs626_private *iqs626;
1651 iqs626 = devm_kzalloc(&client->dev, sizeof(*iqs626), GFP_KERNEL);
1655 i2c_set_clientdata(client, iqs626);
1656 iqs626->client = client;
1658 iqs626->regmap = devm_regmap_init_i2c(client, &iqs626_regmap_config);
1659 if (IS_ERR(iqs626->regmap)) {
1660 error = PTR_ERR(iqs626->regmap);
1661 dev_err(&client->dev, "Failed to initialize register map: %d\n",
1666 init_completion(&iqs626->ati_done);
1668 error = regmap_raw_read(iqs626->regmap, IQS626_VER_INFO, &ver_info,
1673 if (ver_info.prod_num != IQS626_VER_INFO_PROD_NUM) {
1674 dev_err(&client->dev, "Unrecognized product number: 0x%02X\n",
1679 error = iqs626_parse_prop(iqs626);
1683 error = iqs626_input_init(iqs626);
1687 error = devm_request_threaded_irq(&client->dev, client->irq,
1688 NULL, iqs626_irq, IRQF_ONESHOT,
1689 client->name, iqs626);
1691 dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
1695 if (!wait_for_completion_timeout(&iqs626->ati_done,
1696 msecs_to_jiffies(2000))) {
1697 dev_err(&client->dev, "Failed to complete ATI\n");
1702 * The keypad may include one or more switches and is not registered
1703 * until ATI is complete and the initial switch states are read.
1705 error = input_register_device(iqs626->keypad);
1707 dev_err(&client->dev, "Failed to register keypad: %d\n", error);
1712 static int __maybe_unused iqs626_suspend(struct device *dev)
1714 struct iqs626_private *iqs626 = dev_get_drvdata(dev);
1715 struct i2c_client *client = iqs626->client;
1719 if (!iqs626->suspend_mode)
1722 disable_irq(client->irq);
1725 * Automatic power mode switching must be disabled before the device is
1726 * forced into any particular power mode. In this case, the device will
1727 * transition into normal-power mode.
1729 error = regmap_update_bits(iqs626->regmap, IQS626_SYS_SETTINGS,
1730 IQS626_SYS_SETTINGS_DIS_AUTO, ~0);
1735 * The following check ensures the device has completed its transition
1736 * into normal-power mode before a manual mode switch is performed.
1738 error = regmap_read_poll_timeout(iqs626->regmap, IQS626_SYS_FLAGS, val,
1739 !(val & IQS626_SYS_FLAGS_PWR_MODE_MASK),
1740 IQS626_PWR_MODE_POLL_SLEEP_US,
1741 IQS626_PWR_MODE_POLL_TIMEOUT_US);
1745 error = regmap_update_bits(iqs626->regmap, IQS626_SYS_SETTINGS,
1746 IQS626_SYS_SETTINGS_PWR_MODE_MASK,
1747 iqs626->suspend_mode <<
1748 IQS626_SYS_SETTINGS_PWR_MODE_SHIFT);
1753 * This last check ensures the device has completed its transition into
1754 * the desired power mode to prevent any spurious interrupts from being
1755 * triggered after iqs626_suspend has already returned.
1757 error = regmap_read_poll_timeout(iqs626->regmap, IQS626_SYS_FLAGS, val,
1758 (val & IQS626_SYS_FLAGS_PWR_MODE_MASK)
1759 == (iqs626->suspend_mode <<
1760 IQS626_SYS_FLAGS_PWR_MODE_SHIFT),
1761 IQS626_PWR_MODE_POLL_SLEEP_US,
1762 IQS626_PWR_MODE_POLL_TIMEOUT_US);
1766 enable_irq(client->irq);
1771 static int __maybe_unused iqs626_resume(struct device *dev)
1773 struct iqs626_private *iqs626 = dev_get_drvdata(dev);
1774 struct i2c_client *client = iqs626->client;
1778 if (!iqs626->suspend_mode)
1781 disable_irq(client->irq);
1783 error = regmap_update_bits(iqs626->regmap, IQS626_SYS_SETTINGS,
1784 IQS626_SYS_SETTINGS_PWR_MODE_MASK, 0);
1789 * This check ensures the device has returned to normal-power mode
1790 * before automatic power mode switching is re-enabled.
1792 error = regmap_read_poll_timeout(iqs626->regmap, IQS626_SYS_FLAGS, val,
1793 !(val & IQS626_SYS_FLAGS_PWR_MODE_MASK),
1794 IQS626_PWR_MODE_POLL_SLEEP_US,
1795 IQS626_PWR_MODE_POLL_TIMEOUT_US);
1799 error = regmap_update_bits(iqs626->regmap, IQS626_SYS_SETTINGS,
1800 IQS626_SYS_SETTINGS_DIS_AUTO, 0);
1805 * This step reports any events that may have been "swallowed" as a
1806 * result of polling PWR_MODE (which automatically acknowledges any
1807 * pending interrupts).
1809 error = iqs626_report(iqs626);
1813 enable_irq(client->irq);
1818 static SIMPLE_DEV_PM_OPS(iqs626_pm, iqs626_suspend, iqs626_resume);
1820 static const struct of_device_id iqs626_of_match[] = {
1821 { .compatible = "azoteq,iqs626a" },
1824 MODULE_DEVICE_TABLE(of, iqs626_of_match);
1826 static struct i2c_driver iqs626_i2c_driver = {
1829 .of_match_table = iqs626_of_match,
1832 .probe_new = iqs626_probe,
1834 module_i2c_driver(iqs626_i2c_driver);
1836 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
1837 MODULE_DESCRIPTION("Azoteq IQS626A Capacitive Touch Controller");
1838 MODULE_LICENSE("GPL");