1 // SPDX-License-Identifier: GPL-2.0+
3 * Azoteq IQS269A Capacitive Touch Controller
5 * Copyright (C) 2020 Jeff LaBundy <jeff@labundy.com>
7 * This driver registers up to 3 input devices: one representing capacitive or
8 * inductive keys as well as Hall-effect switches, and one for each of the two
9 * axial sliders presented by the device.
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/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/mod_devicetable.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/property.h>
25 #include <linux/regmap.h>
26 #include <linux/slab.h>
28 #define IQS269_VER_INFO 0x00
29 #define IQS269_VER_INFO_PROD_NUM 0x4F
30 #define IQS269_VER_INFO_FW_NUM_2 0x03
31 #define IQS269_VER_INFO_FW_NUM_3 0x10
33 #define IQS269_SYS_FLAGS 0x02
34 #define IQS269_SYS_FLAGS_SHOW_RESET BIT(15)
35 #define IQS269_SYS_FLAGS_PWR_MODE_MASK GENMASK(12, 11)
36 #define IQS269_SYS_FLAGS_PWR_MODE_SHIFT 11
37 #define IQS269_SYS_FLAGS_IN_ATI BIT(10)
39 #define IQS269_CHx_COUNTS 0x08
41 #define IQS269_SLIDER_X 0x30
43 #define IQS269_CAL_DATA_A 0x35
44 #define IQS269_CAL_DATA_A_HALL_BIN_L_MASK GENMASK(15, 12)
45 #define IQS269_CAL_DATA_A_HALL_BIN_L_SHIFT 12
46 #define IQS269_CAL_DATA_A_HALL_BIN_R_MASK GENMASK(11, 8)
47 #define IQS269_CAL_DATA_A_HALL_BIN_R_SHIFT 8
49 #define IQS269_SYS_SETTINGS 0x80
50 #define IQS269_SYS_SETTINGS_CLK_DIV BIT(15)
51 #define IQS269_SYS_SETTINGS_ULP_AUTO BIT(14)
52 #define IQS269_SYS_SETTINGS_DIS_AUTO BIT(13)
53 #define IQS269_SYS_SETTINGS_PWR_MODE_MASK GENMASK(12, 11)
54 #define IQS269_SYS_SETTINGS_PWR_MODE_SHIFT 11
55 #define IQS269_SYS_SETTINGS_PWR_MODE_MAX 3
56 #define IQS269_SYS_SETTINGS_ULP_UPDATE_MASK GENMASK(10, 8)
57 #define IQS269_SYS_SETTINGS_ULP_UPDATE_SHIFT 8
58 #define IQS269_SYS_SETTINGS_ULP_UPDATE_MAX 7
59 #define IQS269_SYS_SETTINGS_SLIDER_SWIPE BIT(7)
60 #define IQS269_SYS_SETTINGS_RESEED_OFFSET BIT(6)
61 #define IQS269_SYS_SETTINGS_EVENT_MODE BIT(5)
62 #define IQS269_SYS_SETTINGS_EVENT_MODE_LP BIT(4)
63 #define IQS269_SYS_SETTINGS_REDO_ATI BIT(2)
64 #define IQS269_SYS_SETTINGS_ACK_RESET BIT(0)
66 #define IQS269_FILT_STR_LP_LTA_MASK GENMASK(7, 6)
67 #define IQS269_FILT_STR_LP_LTA_SHIFT 6
68 #define IQS269_FILT_STR_LP_CNT_MASK GENMASK(5, 4)
69 #define IQS269_FILT_STR_LP_CNT_SHIFT 4
70 #define IQS269_FILT_STR_NP_LTA_MASK GENMASK(3, 2)
71 #define IQS269_FILT_STR_NP_LTA_SHIFT 2
72 #define IQS269_FILT_STR_NP_CNT_MASK GENMASK(1, 0)
73 #define IQS269_FILT_STR_MAX 3
75 #define IQS269_EVENT_MASK_SYS BIT(6)
76 #define IQS269_EVENT_MASK_GESTURE BIT(3)
77 #define IQS269_EVENT_MASK_DEEP BIT(2)
78 #define IQS269_EVENT_MASK_TOUCH BIT(1)
79 #define IQS269_EVENT_MASK_PROX BIT(0)
81 #define IQS269_RATE_NP_MS_MAX 255
82 #define IQS269_RATE_LP_MS_MAX 255
83 #define IQS269_RATE_ULP_MS_MAX 4080
84 #define IQS269_TIMEOUT_PWR_MS_MAX 130560
85 #define IQS269_TIMEOUT_LTA_MS_MAX 130560
87 #define IQS269_MISC_A_ATI_BAND_DISABLE BIT(15)
88 #define IQS269_MISC_A_ATI_LP_ONLY BIT(14)
89 #define IQS269_MISC_A_ATI_BAND_TIGHTEN BIT(13)
90 #define IQS269_MISC_A_FILT_DISABLE BIT(12)
91 #define IQS269_MISC_A_GPIO3_SELECT_MASK GENMASK(10, 8)
92 #define IQS269_MISC_A_GPIO3_SELECT_SHIFT 8
93 #define IQS269_MISC_A_DUAL_DIR BIT(6)
94 #define IQS269_MISC_A_TX_FREQ_MASK GENMASK(5, 4)
95 #define IQS269_MISC_A_TX_FREQ_SHIFT 4
96 #define IQS269_MISC_A_TX_FREQ_MAX 3
97 #define IQS269_MISC_A_GLOBAL_CAP_SIZE BIT(0)
99 #define IQS269_MISC_B_RESEED_UI_SEL_MASK GENMASK(7, 6)
100 #define IQS269_MISC_B_RESEED_UI_SEL_SHIFT 6
101 #define IQS269_MISC_B_RESEED_UI_SEL_MAX 3
102 #define IQS269_MISC_B_TRACKING_UI_ENABLE BIT(4)
103 #define IQS269_MISC_B_FILT_STR_SLIDER GENMASK(1, 0)
105 #define IQS269_TOUCH_HOLD_SLIDER_SEL 0x89
106 #define IQS269_TOUCH_HOLD_DEFAULT 0x14
107 #define IQS269_TOUCH_HOLD_MS_MIN 256
108 #define IQS269_TOUCH_HOLD_MS_MAX 65280
110 #define IQS269_TIMEOUT_TAP_MS_MAX 4080
111 #define IQS269_TIMEOUT_SWIPE_MS_MAX 4080
112 #define IQS269_THRESH_SWIPE_MAX 255
114 #define IQS269_CHx_ENG_A_MEAS_CAP_SIZE BIT(15)
115 #define IQS269_CHx_ENG_A_RX_GND_INACTIVE BIT(13)
116 #define IQS269_CHx_ENG_A_LOCAL_CAP_SIZE BIT(12)
117 #define IQS269_CHx_ENG_A_ATI_MODE_MASK GENMASK(9, 8)
118 #define IQS269_CHx_ENG_A_ATI_MODE_SHIFT 8
119 #define IQS269_CHx_ENG_A_ATI_MODE_MAX 3
120 #define IQS269_CHx_ENG_A_INV_LOGIC BIT(7)
121 #define IQS269_CHx_ENG_A_PROJ_BIAS_MASK GENMASK(6, 5)
122 #define IQS269_CHx_ENG_A_PROJ_BIAS_SHIFT 5
123 #define IQS269_CHx_ENG_A_PROJ_BIAS_MAX 3
124 #define IQS269_CHx_ENG_A_SENSE_MODE_MASK GENMASK(3, 0)
125 #define IQS269_CHx_ENG_A_SENSE_MODE_MAX 15
127 #define IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE BIT(13)
128 #define IQS269_CHx_ENG_B_SENSE_FREQ_MASK GENMASK(10, 9)
129 #define IQS269_CHx_ENG_B_SENSE_FREQ_SHIFT 9
130 #define IQS269_CHx_ENG_B_SENSE_FREQ_MAX 3
131 #define IQS269_CHx_ENG_B_STATIC_ENABLE BIT(8)
132 #define IQS269_CHx_ENG_B_ATI_BASE_MASK GENMASK(7, 6)
133 #define IQS269_CHx_ENG_B_ATI_BASE_75 0x00
134 #define IQS269_CHx_ENG_B_ATI_BASE_100 0x40
135 #define IQS269_CHx_ENG_B_ATI_BASE_150 0x80
136 #define IQS269_CHx_ENG_B_ATI_BASE_200 0xC0
137 #define IQS269_CHx_ENG_B_ATI_TARGET_MASK GENMASK(5, 0)
138 #define IQS269_CHx_ENG_B_ATI_TARGET_MAX 2016
140 #define IQS269_CHx_WEIGHT_MAX 255
141 #define IQS269_CHx_THRESH_MAX 255
142 #define IQS269_CHx_HYST_DEEP_MASK GENMASK(7, 4)
143 #define IQS269_CHx_HYST_DEEP_SHIFT 4
144 #define IQS269_CHx_HYST_TOUCH_MASK GENMASK(3, 0)
145 #define IQS269_CHx_HYST_MAX 15
147 #define IQS269_CHx_HALL_INACTIVE 6
148 #define IQS269_CHx_HALL_ACTIVE 7
150 #define IQS269_HALL_PAD_R BIT(0)
151 #define IQS269_HALL_PAD_L BIT(1)
152 #define IQS269_HALL_PAD_INV BIT(6)
154 #define IQS269_HALL_UI 0xF5
155 #define IQS269_HALL_UI_ENABLE BIT(15)
157 #define IQS269_MAX_REG 0xFF
159 #define IQS269_OTP_OPTION_DEFAULT 0x00
160 #define IQS269_OTP_OPTION_TWS 0xD0
161 #define IQS269_OTP_OPTION_HOLD BIT(7)
163 #define IQS269_NUM_CH 8
164 #define IQS269_NUM_SL 2
166 #define iqs269_irq_wait() usleep_range(200, 250)
168 enum iqs269_local_cap_size {
169 IQS269_LOCAL_CAP_SIZE_0,
170 IQS269_LOCAL_CAP_SIZE_GLOBAL_ONLY,
171 IQS269_LOCAL_CAP_SIZE_GLOBAL_0pF5,
174 enum iqs269_st_offs {
177 IQS269_ST_OFFS_TOUCH,
181 enum iqs269_th_offs {
183 IQS269_TH_OFFS_TOUCH,
187 enum iqs269_event_id {
188 IQS269_EVENT_PROX_DN,
189 IQS269_EVENT_PROX_UP,
190 IQS269_EVENT_TOUCH_DN,
191 IQS269_EVENT_TOUCH_UP,
192 IQS269_EVENT_DEEP_DN,
193 IQS269_EVENT_DEEP_UP,
196 enum iqs269_slider_id {
202 enum iqs269_gesture_id {
205 IQS269_GESTURE_FLICK_POS,
206 IQS269_GESTURE_FLICK_NEG,
210 struct iqs269_switch_desc {
215 struct iqs269_event_desc {
217 enum iqs269_st_offs st_offs;
218 enum iqs269_th_offs th_offs;
223 static const struct iqs269_event_desc iqs269_events[] = {
224 [IQS269_EVENT_PROX_DN] = {
225 .name = "event-prox",
226 .st_offs = IQS269_ST_OFFS_PROX,
227 .th_offs = IQS269_TH_OFFS_PROX,
228 .mask = IQS269_EVENT_MASK_PROX,
230 [IQS269_EVENT_PROX_UP] = {
231 .name = "event-prox-alt",
232 .st_offs = IQS269_ST_OFFS_PROX,
233 .th_offs = IQS269_TH_OFFS_PROX,
235 .mask = IQS269_EVENT_MASK_PROX,
237 [IQS269_EVENT_TOUCH_DN] = {
238 .name = "event-touch",
239 .st_offs = IQS269_ST_OFFS_TOUCH,
240 .th_offs = IQS269_TH_OFFS_TOUCH,
241 .mask = IQS269_EVENT_MASK_TOUCH,
243 [IQS269_EVENT_TOUCH_UP] = {
244 .name = "event-touch-alt",
245 .st_offs = IQS269_ST_OFFS_TOUCH,
246 .th_offs = IQS269_TH_OFFS_TOUCH,
248 .mask = IQS269_EVENT_MASK_TOUCH,
250 [IQS269_EVENT_DEEP_DN] = {
251 .name = "event-deep",
252 .st_offs = IQS269_ST_OFFS_DEEP,
253 .th_offs = IQS269_TH_OFFS_DEEP,
254 .mask = IQS269_EVENT_MASK_DEEP,
256 [IQS269_EVENT_DEEP_UP] = {
257 .name = "event-deep-alt",
258 .st_offs = IQS269_ST_OFFS_DEEP,
259 .th_offs = IQS269_TH_OFFS_DEEP,
261 .mask = IQS269_EVENT_MASK_DEEP,
265 struct iqs269_ver_info {
272 struct iqs269_ch_reg {
284 struct iqs269_sys_reg {
300 u8 slider_select[IQS269_NUM_SL];
305 struct iqs269_ch_reg ch_reg[IQS269_NUM_CH];
308 struct iqs269_flags {
315 struct iqs269_private {
316 struct i2c_client *client;
317 struct regmap *regmap;
319 struct iqs269_switch_desc switches[ARRAY_SIZE(iqs269_events)];
320 struct iqs269_ver_info ver_info;
321 struct iqs269_sys_reg sys_reg;
322 struct completion ati_done;
323 struct input_dev *keypad;
324 struct input_dev *slider[IQS269_NUM_SL];
325 unsigned int keycode[ARRAY_SIZE(iqs269_events) * IQS269_NUM_CH];
326 unsigned int sl_code[IQS269_NUM_SL][IQS269_NUM_GESTURES];
327 unsigned int otp_option;
333 static enum iqs269_slider_id iqs269_slider_type(struct iqs269_private *iqs269,
339 * Slider 1 is unavailable if the touch-and-hold option is enabled via
340 * OTP. In that case, the channel selection register is repurposed for
341 * the touch-and-hold timer ceiling.
343 if (slider_num && (iqs269->otp_option & IQS269_OTP_OPTION_HOLD))
344 return IQS269_SLIDER_NONE;
346 if (!iqs269->sys_reg.slider_select[slider_num])
347 return IQS269_SLIDER_NONE;
349 for (i = 0; i < IQS269_NUM_GESTURES; i++)
350 if (iqs269->sl_code[slider_num][i] != KEY_RESERVED)
351 return IQS269_SLIDER_KEY;
353 return IQS269_SLIDER_RAW;
356 static int iqs269_ati_mode_set(struct iqs269_private *iqs269,
357 unsigned int ch_num, unsigned int mode)
359 struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
362 if (ch_num >= IQS269_NUM_CH)
365 if (mode > IQS269_CHx_ENG_A_ATI_MODE_MAX)
368 mutex_lock(&iqs269->lock);
370 engine_a = be16_to_cpu(ch_reg[ch_num].engine_a);
372 engine_a &= ~IQS269_CHx_ENG_A_ATI_MODE_MASK;
373 engine_a |= (mode << IQS269_CHx_ENG_A_ATI_MODE_SHIFT);
375 ch_reg[ch_num].engine_a = cpu_to_be16(engine_a);
376 iqs269->ati_current = false;
378 mutex_unlock(&iqs269->lock);
383 static int iqs269_ati_mode_get(struct iqs269_private *iqs269,
384 unsigned int ch_num, unsigned int *mode)
386 struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
389 if (ch_num >= IQS269_NUM_CH)
392 mutex_lock(&iqs269->lock);
393 engine_a = be16_to_cpu(ch_reg[ch_num].engine_a);
394 mutex_unlock(&iqs269->lock);
396 engine_a &= IQS269_CHx_ENG_A_ATI_MODE_MASK;
397 *mode = (engine_a >> IQS269_CHx_ENG_A_ATI_MODE_SHIFT);
402 static int iqs269_ati_base_set(struct iqs269_private *iqs269,
403 unsigned int ch_num, unsigned int base)
405 struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
408 if (ch_num >= IQS269_NUM_CH)
413 base = IQS269_CHx_ENG_B_ATI_BASE_75;
417 base = IQS269_CHx_ENG_B_ATI_BASE_100;
421 base = IQS269_CHx_ENG_B_ATI_BASE_150;
425 base = IQS269_CHx_ENG_B_ATI_BASE_200;
432 mutex_lock(&iqs269->lock);
434 engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
436 engine_b &= ~IQS269_CHx_ENG_B_ATI_BASE_MASK;
439 ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
440 iqs269->ati_current = false;
442 mutex_unlock(&iqs269->lock);
447 static int iqs269_ati_base_get(struct iqs269_private *iqs269,
448 unsigned int ch_num, unsigned int *base)
450 struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
453 if (ch_num >= IQS269_NUM_CH)
456 mutex_lock(&iqs269->lock);
457 engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
458 mutex_unlock(&iqs269->lock);
460 switch (engine_b & IQS269_CHx_ENG_B_ATI_BASE_MASK) {
461 case IQS269_CHx_ENG_B_ATI_BASE_75:
465 case IQS269_CHx_ENG_B_ATI_BASE_100:
469 case IQS269_CHx_ENG_B_ATI_BASE_150:
473 case IQS269_CHx_ENG_B_ATI_BASE_200:
482 static int iqs269_ati_target_set(struct iqs269_private *iqs269,
483 unsigned int ch_num, unsigned int target)
485 struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
488 if (ch_num >= IQS269_NUM_CH)
491 if (target > IQS269_CHx_ENG_B_ATI_TARGET_MAX)
494 mutex_lock(&iqs269->lock);
496 engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
498 engine_b &= ~IQS269_CHx_ENG_B_ATI_TARGET_MASK;
499 engine_b |= target / 32;
501 ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
502 iqs269->ati_current = false;
504 mutex_unlock(&iqs269->lock);
509 static int iqs269_ati_target_get(struct iqs269_private *iqs269,
510 unsigned int ch_num, unsigned int *target)
512 struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
515 if (ch_num >= IQS269_NUM_CH)
518 mutex_lock(&iqs269->lock);
519 engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
520 mutex_unlock(&iqs269->lock);
522 *target = (engine_b & IQS269_CHx_ENG_B_ATI_TARGET_MASK) * 32;
527 static int iqs269_parse_mask(const struct fwnode_handle *fwnode,
528 const char *propname, u8 *mask)
530 unsigned int val[IQS269_NUM_CH];
533 count = fwnode_property_count_u32(fwnode, propname);
537 if (count > IQS269_NUM_CH)
540 error = fwnode_property_read_u32_array(fwnode, propname, val, count);
546 for (i = 0; i < count; i++) {
547 if (val[i] >= IQS269_NUM_CH)
550 *mask |= BIT(val[i]);
556 static int iqs269_parse_chan(struct iqs269_private *iqs269,
557 const struct fwnode_handle *ch_node)
559 struct i2c_client *client = iqs269->client;
560 struct fwnode_handle *ev_node;
561 struct iqs269_ch_reg *ch_reg;
562 u16 engine_a, engine_b;
563 unsigned int reg, val;
566 error = fwnode_property_read_u32(ch_node, "reg", ®);
568 dev_err(&client->dev, "Failed to read channel number: %d\n",
571 } else if (reg >= IQS269_NUM_CH) {
572 dev_err(&client->dev, "Invalid channel number: %u\n", reg);
576 iqs269->sys_reg.active |= BIT(reg);
577 if (!fwnode_property_present(ch_node, "azoteq,reseed-disable"))
578 iqs269->sys_reg.reseed |= BIT(reg);
580 if (fwnode_property_present(ch_node, "azoteq,blocking-enable"))
581 iqs269->sys_reg.blocking |= BIT(reg);
583 if (fwnode_property_present(ch_node, "azoteq,slider0-select"))
584 iqs269->sys_reg.slider_select[0] |= BIT(reg);
586 if (fwnode_property_present(ch_node, "azoteq,slider1-select") &&
587 !(iqs269->otp_option & IQS269_OTP_OPTION_HOLD))
588 iqs269->sys_reg.slider_select[1] |= BIT(reg);
590 ch_reg = &iqs269->sys_reg.ch_reg[reg];
592 error = iqs269_parse_mask(ch_node, "azoteq,rx-enable",
595 dev_err(&client->dev, "Invalid channel %u RX enable mask: %d\n",
600 error = iqs269_parse_mask(ch_node, "azoteq,tx-enable",
603 dev_err(&client->dev, "Invalid channel %u TX enable mask: %d\n",
608 engine_a = be16_to_cpu(ch_reg->engine_a);
609 engine_b = be16_to_cpu(ch_reg->engine_b);
611 engine_a |= IQS269_CHx_ENG_A_MEAS_CAP_SIZE;
612 if (fwnode_property_present(ch_node, "azoteq,meas-cap-decrease"))
613 engine_a &= ~IQS269_CHx_ENG_A_MEAS_CAP_SIZE;
615 engine_a |= IQS269_CHx_ENG_A_RX_GND_INACTIVE;
616 if (fwnode_property_present(ch_node, "azoteq,rx-float-inactive"))
617 engine_a &= ~IQS269_CHx_ENG_A_RX_GND_INACTIVE;
619 engine_a &= ~IQS269_CHx_ENG_A_LOCAL_CAP_SIZE;
620 engine_b &= ~IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE;
621 if (!fwnode_property_read_u32(ch_node, "azoteq,local-cap-size", &val)) {
623 case IQS269_LOCAL_CAP_SIZE_0:
626 case IQS269_LOCAL_CAP_SIZE_GLOBAL_0pF5:
627 engine_a |= IQS269_CHx_ENG_A_LOCAL_CAP_SIZE;
630 case IQS269_LOCAL_CAP_SIZE_GLOBAL_ONLY:
631 engine_b |= IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE;
635 dev_err(&client->dev,
636 "Invalid channel %u local cap. size: %u\n", reg,
642 engine_a &= ~IQS269_CHx_ENG_A_INV_LOGIC;
643 if (fwnode_property_present(ch_node, "azoteq,invert-enable"))
644 engine_a |= IQS269_CHx_ENG_A_INV_LOGIC;
646 if (!fwnode_property_read_u32(ch_node, "azoteq,proj-bias", &val)) {
647 if (val > IQS269_CHx_ENG_A_PROJ_BIAS_MAX) {
648 dev_err(&client->dev,
649 "Invalid channel %u bias current: %u\n", reg,
654 engine_a &= ~IQS269_CHx_ENG_A_PROJ_BIAS_MASK;
655 engine_a |= (val << IQS269_CHx_ENG_A_PROJ_BIAS_SHIFT);
658 if (!fwnode_property_read_u32(ch_node, "azoteq,sense-mode", &val)) {
659 if (val > IQS269_CHx_ENG_A_SENSE_MODE_MAX) {
660 dev_err(&client->dev,
661 "Invalid channel %u sensing mode: %u\n", reg,
666 engine_a &= ~IQS269_CHx_ENG_A_SENSE_MODE_MASK;
670 if (!fwnode_property_read_u32(ch_node, "azoteq,sense-freq", &val)) {
671 if (val > IQS269_CHx_ENG_B_SENSE_FREQ_MAX) {
672 dev_err(&client->dev,
673 "Invalid channel %u sensing frequency: %u\n",
678 engine_b &= ~IQS269_CHx_ENG_B_SENSE_FREQ_MASK;
679 engine_b |= (val << IQS269_CHx_ENG_B_SENSE_FREQ_SHIFT);
682 engine_b &= ~IQS269_CHx_ENG_B_STATIC_ENABLE;
683 if (fwnode_property_present(ch_node, "azoteq,static-enable"))
684 engine_b |= IQS269_CHx_ENG_B_STATIC_ENABLE;
686 ch_reg->engine_a = cpu_to_be16(engine_a);
687 ch_reg->engine_b = cpu_to_be16(engine_b);
689 if (!fwnode_property_read_u32(ch_node, "azoteq,ati-mode", &val)) {
690 error = iqs269_ati_mode_set(iqs269, reg, val);
692 dev_err(&client->dev,
693 "Invalid channel %u ATI mode: %u\n", reg, val);
698 if (!fwnode_property_read_u32(ch_node, "azoteq,ati-base", &val)) {
699 error = iqs269_ati_base_set(iqs269, reg, val);
701 dev_err(&client->dev,
702 "Invalid channel %u ATI base: %u\n", reg, val);
707 if (!fwnode_property_read_u32(ch_node, "azoteq,ati-target", &val)) {
708 error = iqs269_ati_target_set(iqs269, reg, val);
710 dev_err(&client->dev,
711 "Invalid channel %u ATI target: %u\n", reg,
717 error = iqs269_parse_mask(ch_node, "azoteq,assoc-select",
718 &ch_reg->assoc_select);
720 dev_err(&client->dev, "Invalid channel %u association: %d\n",
725 if (!fwnode_property_read_u32(ch_node, "azoteq,assoc-weight", &val)) {
726 if (val > IQS269_CHx_WEIGHT_MAX) {
727 dev_err(&client->dev,
728 "Invalid channel %u associated weight: %u\n",
733 ch_reg->assoc_weight = val;
736 for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
737 ev_node = fwnode_get_named_child_node(ch_node,
738 iqs269_events[i].name);
742 if (!fwnode_property_read_u32(ev_node, "azoteq,thresh", &val)) {
743 if (val > IQS269_CHx_THRESH_MAX) {
744 dev_err(&client->dev,
745 "Invalid channel %u threshold: %u\n",
747 fwnode_handle_put(ev_node);
751 ch_reg->thresh[iqs269_events[i].th_offs] = val;
754 if (!fwnode_property_read_u32(ev_node, "azoteq,hyst", &val)) {
755 u8 *hyst = &ch_reg->hyst;
757 if (val > IQS269_CHx_HYST_MAX) {
758 dev_err(&client->dev,
759 "Invalid channel %u hysteresis: %u\n",
761 fwnode_handle_put(ev_node);
765 if (i == IQS269_EVENT_DEEP_DN ||
766 i == IQS269_EVENT_DEEP_UP) {
767 *hyst &= ~IQS269_CHx_HYST_DEEP_MASK;
768 *hyst |= (val << IQS269_CHx_HYST_DEEP_SHIFT);
769 } else if (i == IQS269_EVENT_TOUCH_DN ||
770 i == IQS269_EVENT_TOUCH_UP) {
771 *hyst &= ~IQS269_CHx_HYST_TOUCH_MASK;
776 error = fwnode_property_read_u32(ev_node, "linux,code", &val);
777 fwnode_handle_put(ev_node);
778 if (error == -EINVAL) {
781 dev_err(&client->dev,
782 "Failed to read channel %u code: %d\n", reg,
788 case IQS269_CHx_HALL_ACTIVE:
789 if (iqs269->hall_enable) {
790 iqs269->switches[i].code = val;
791 iqs269->switches[i].enabled = true;
795 case IQS269_CHx_HALL_INACTIVE:
796 if (iqs269->hall_enable)
801 iqs269->keycode[i * IQS269_NUM_CH + reg] = val;
804 iqs269->sys_reg.event_mask &= ~iqs269_events[i].mask;
810 static int iqs269_parse_prop(struct iqs269_private *iqs269)
812 struct iqs269_sys_reg *sys_reg = &iqs269->sys_reg;
813 struct i2c_client *client = iqs269->client;
814 struct fwnode_handle *ch_node;
815 u16 general, misc_a, misc_b;
819 iqs269->hall_enable = device_property_present(&client->dev,
820 "azoteq,hall-enable");
822 error = regmap_raw_read(iqs269->regmap, IQS269_SYS_SETTINGS, sys_reg,
827 if (!device_property_read_u32(&client->dev, "azoteq,filt-str-lp-lta",
829 if (val > IQS269_FILT_STR_MAX) {
830 dev_err(&client->dev, "Invalid filter strength: %u\n",
835 sys_reg->filter &= ~IQS269_FILT_STR_LP_LTA_MASK;
836 sys_reg->filter |= (val << IQS269_FILT_STR_LP_LTA_SHIFT);
839 if (!device_property_read_u32(&client->dev, "azoteq,filt-str-lp-cnt",
841 if (val > IQS269_FILT_STR_MAX) {
842 dev_err(&client->dev, "Invalid filter strength: %u\n",
847 sys_reg->filter &= ~IQS269_FILT_STR_LP_CNT_MASK;
848 sys_reg->filter |= (val << IQS269_FILT_STR_LP_CNT_SHIFT);
851 if (!device_property_read_u32(&client->dev, "azoteq,filt-str-np-lta",
853 if (val > IQS269_FILT_STR_MAX) {
854 dev_err(&client->dev, "Invalid filter strength: %u\n",
859 sys_reg->filter &= ~IQS269_FILT_STR_NP_LTA_MASK;
860 sys_reg->filter |= (val << IQS269_FILT_STR_NP_LTA_SHIFT);
863 if (!device_property_read_u32(&client->dev, "azoteq,filt-str-np-cnt",
865 if (val > IQS269_FILT_STR_MAX) {
866 dev_err(&client->dev, "Invalid filter strength: %u\n",
871 sys_reg->filter &= ~IQS269_FILT_STR_NP_CNT_MASK;
872 sys_reg->filter |= val;
875 if (!device_property_read_u32(&client->dev, "azoteq,rate-np-ms",
877 if (val > IQS269_RATE_NP_MS_MAX) {
878 dev_err(&client->dev, "Invalid report rate: %u\n", val);
882 sys_reg->rate_np = val;
885 if (!device_property_read_u32(&client->dev, "azoteq,rate-lp-ms",
887 if (val > IQS269_RATE_LP_MS_MAX) {
888 dev_err(&client->dev, "Invalid report rate: %u\n", val);
892 sys_reg->rate_lp = val;
895 if (!device_property_read_u32(&client->dev, "azoteq,rate-ulp-ms",
897 if (val > IQS269_RATE_ULP_MS_MAX) {
898 dev_err(&client->dev, "Invalid report rate: %u\n", val);
902 sys_reg->rate_ulp = val / 16;
905 if (!device_property_read_u32(&client->dev, "azoteq,timeout-pwr-ms",
907 if (val > IQS269_TIMEOUT_PWR_MS_MAX) {
908 dev_err(&client->dev, "Invalid timeout: %u\n", val);
912 sys_reg->timeout_pwr = val / 512;
915 if (!device_property_read_u32(&client->dev, "azoteq,timeout-lta-ms",
917 if (val > IQS269_TIMEOUT_LTA_MS_MAX) {
918 dev_err(&client->dev, "Invalid timeout: %u\n", val);
922 sys_reg->timeout_lta = val / 512;
925 misc_a = be16_to_cpu(sys_reg->misc_a);
926 misc_b = be16_to_cpu(sys_reg->misc_b);
928 misc_a &= ~IQS269_MISC_A_ATI_BAND_DISABLE;
929 if (device_property_present(&client->dev, "azoteq,ati-band-disable"))
930 misc_a |= IQS269_MISC_A_ATI_BAND_DISABLE;
932 misc_a &= ~IQS269_MISC_A_ATI_LP_ONLY;
933 if (device_property_present(&client->dev, "azoteq,ati-lp-only"))
934 misc_a |= IQS269_MISC_A_ATI_LP_ONLY;
936 misc_a &= ~IQS269_MISC_A_ATI_BAND_TIGHTEN;
937 if (device_property_present(&client->dev, "azoteq,ati-band-tighten"))
938 misc_a |= IQS269_MISC_A_ATI_BAND_TIGHTEN;
940 misc_a &= ~IQS269_MISC_A_FILT_DISABLE;
941 if (device_property_present(&client->dev, "azoteq,filt-disable"))
942 misc_a |= IQS269_MISC_A_FILT_DISABLE;
944 if (!device_property_read_u32(&client->dev, "azoteq,gpio3-select",
946 if (val >= IQS269_NUM_CH) {
947 dev_err(&client->dev, "Invalid GPIO3 selection: %u\n",
952 misc_a &= ~IQS269_MISC_A_GPIO3_SELECT_MASK;
953 misc_a |= (val << IQS269_MISC_A_GPIO3_SELECT_SHIFT);
956 misc_a &= ~IQS269_MISC_A_DUAL_DIR;
957 if (device_property_present(&client->dev, "azoteq,dual-direction"))
958 misc_a |= IQS269_MISC_A_DUAL_DIR;
960 if (!device_property_read_u32(&client->dev, "azoteq,tx-freq", &val)) {
961 if (val > IQS269_MISC_A_TX_FREQ_MAX) {
962 dev_err(&client->dev,
963 "Invalid excitation frequency: %u\n", val);
967 misc_a &= ~IQS269_MISC_A_TX_FREQ_MASK;
968 misc_a |= (val << IQS269_MISC_A_TX_FREQ_SHIFT);
971 misc_a &= ~IQS269_MISC_A_GLOBAL_CAP_SIZE;
972 if (device_property_present(&client->dev, "azoteq,global-cap-increase"))
973 misc_a |= IQS269_MISC_A_GLOBAL_CAP_SIZE;
975 if (!device_property_read_u32(&client->dev, "azoteq,reseed-select",
977 if (val > IQS269_MISC_B_RESEED_UI_SEL_MAX) {
978 dev_err(&client->dev, "Invalid reseed selection: %u\n",
983 misc_b &= ~IQS269_MISC_B_RESEED_UI_SEL_MASK;
984 misc_b |= (val << IQS269_MISC_B_RESEED_UI_SEL_SHIFT);
987 misc_b &= ~IQS269_MISC_B_TRACKING_UI_ENABLE;
988 if (device_property_present(&client->dev, "azoteq,tracking-enable"))
989 misc_b |= IQS269_MISC_B_TRACKING_UI_ENABLE;
991 if (!device_property_read_u32(&client->dev, "azoteq,filt-str-slider",
993 if (val > IQS269_FILT_STR_MAX) {
994 dev_err(&client->dev, "Invalid filter strength: %u\n",
999 misc_b &= ~IQS269_MISC_B_FILT_STR_SLIDER;
1003 sys_reg->misc_a = cpu_to_be16(misc_a);
1004 sys_reg->misc_b = cpu_to_be16(misc_b);
1006 sys_reg->active = 0;
1007 sys_reg->reseed = 0;
1009 sys_reg->blocking = 0;
1011 sys_reg->slider_select[0] = 0;
1014 * If configured via OTP to do so, the device asserts a pulse on the
1015 * GPIO4 pin for approximately 60 ms once a selected channel is held
1016 * in a state of touch for a configurable length of time.
1018 * In that case, the register used for slider 1 channel selection is
1019 * repurposed for the touch-and-hold timer ceiling.
1021 if (iqs269->otp_option & IQS269_OTP_OPTION_HOLD) {
1022 if (!device_property_read_u32(&client->dev,
1023 "azoteq,touch-hold-ms", &val)) {
1024 if (val < IQS269_TOUCH_HOLD_MS_MIN ||
1025 val > IQS269_TOUCH_HOLD_MS_MAX) {
1026 dev_err(&client->dev,
1027 "Invalid touch-and-hold ceiling: %u\n",
1032 sys_reg->slider_select[1] = val / 256;
1033 } else if (iqs269->ver_info.fw_num < IQS269_VER_INFO_FW_NUM_3) {
1035 * The default touch-and-hold timer ceiling initially
1036 * read from early revisions of silicon is invalid if
1037 * the device experienced a soft reset between power-
1038 * on and the read operation.
1040 * To protect against this case, explicitly cache the
1041 * default value so that it is restored each time the
1042 * device is re-initialized.
1044 sys_reg->slider_select[1] = IQS269_TOUCH_HOLD_DEFAULT;
1047 sys_reg->slider_select[1] = 0;
1050 sys_reg->event_mask = ~((u8)IQS269_EVENT_MASK_SYS);
1052 device_for_each_child_node(&client->dev, ch_node) {
1053 error = iqs269_parse_chan(iqs269, ch_node);
1055 fwnode_handle_put(ch_node);
1061 * Volunteer all active channels to participate in ATI when REDO-ATI is
1062 * manually triggered.
1064 sys_reg->redo_ati = sys_reg->active;
1066 general = be16_to_cpu(sys_reg->general);
1068 if (device_property_present(&client->dev, "azoteq,clk-div"))
1069 general |= IQS269_SYS_SETTINGS_CLK_DIV;
1072 * Configure the device to automatically switch between normal and low-
1073 * power modes as a function of sensing activity. Ultra-low-power mode,
1074 * if enabled, is reserved for suspend.
1076 general &= ~IQS269_SYS_SETTINGS_ULP_AUTO;
1077 general &= ~IQS269_SYS_SETTINGS_DIS_AUTO;
1078 general &= ~IQS269_SYS_SETTINGS_PWR_MODE_MASK;
1080 if (!device_property_read_u32(&client->dev, "azoteq,suspend-mode",
1082 if (val > IQS269_SYS_SETTINGS_PWR_MODE_MAX) {
1083 dev_err(&client->dev, "Invalid suspend mode: %u\n",
1088 general |= (val << IQS269_SYS_SETTINGS_PWR_MODE_SHIFT);
1091 if (!device_property_read_u32(&client->dev, "azoteq,ulp-update",
1093 if (val > IQS269_SYS_SETTINGS_ULP_UPDATE_MAX) {
1094 dev_err(&client->dev, "Invalid update rate: %u\n", val);
1098 general &= ~IQS269_SYS_SETTINGS_ULP_UPDATE_MASK;
1099 general |= (val << IQS269_SYS_SETTINGS_ULP_UPDATE_SHIFT);
1102 if (device_property_present(&client->dev, "linux,keycodes")) {
1104 int count = device_property_count_u32(&client->dev,
1106 if (count > IQS269_NUM_GESTURES * IQS269_NUM_SL) {
1107 dev_err(&client->dev, "Too many keycodes present\n");
1109 } else if (count < 0) {
1110 dev_err(&client->dev, "Failed to count keycodes: %d\n",
1115 error = device_property_read_u32_array(&client->dev,
1117 *iqs269->sl_code, count);
1119 dev_err(&client->dev, "Failed to read keycodes: %d\n",
1124 if (device_property_present(&client->dev,
1125 "azoteq,gesture-swipe"))
1126 general |= IQS269_SYS_SETTINGS_SLIDER_SWIPE;
1129 * Early revisions of silicon use a more granular step size for
1130 * tap and swipe gesture timeouts; scale them appropriately.
1132 if (iqs269->ver_info.fw_num < IQS269_VER_INFO_FW_NUM_3)
1135 if (!device_property_read_u32(&client->dev,
1136 "azoteq,timeout-tap-ms", &val)) {
1137 if (val > IQS269_TIMEOUT_TAP_MS_MAX / scale) {
1138 dev_err(&client->dev, "Invalid timeout: %u\n",
1143 sys_reg->timeout_tap = val / (16 / scale);
1146 if (!device_property_read_u32(&client->dev,
1147 "azoteq,timeout-swipe-ms",
1149 if (val > IQS269_TIMEOUT_SWIPE_MS_MAX / scale) {
1150 dev_err(&client->dev, "Invalid timeout: %u\n",
1155 sys_reg->timeout_swipe = val / (16 / scale);
1158 if (!device_property_read_u32(&client->dev,
1159 "azoteq,thresh-swipe", &val)) {
1160 if (val > IQS269_THRESH_SWIPE_MAX) {
1161 dev_err(&client->dev, "Invalid threshold: %u\n",
1166 sys_reg->thresh_swipe = val;
1169 sys_reg->event_mask &= ~IQS269_EVENT_MASK_GESTURE;
1172 general &= ~IQS269_SYS_SETTINGS_RESEED_OFFSET;
1173 if (device_property_present(&client->dev, "azoteq,reseed-offset"))
1174 general |= IQS269_SYS_SETTINGS_RESEED_OFFSET;
1176 general |= IQS269_SYS_SETTINGS_EVENT_MODE;
1179 * As per the datasheet, enable streaming during normal-power mode if
1180 * raw coordinates will be read from either slider. In that case, the
1181 * device returns to event mode during low-power mode.
1183 if (iqs269_slider_type(iqs269, 0) == IQS269_SLIDER_RAW ||
1184 iqs269_slider_type(iqs269, 1) == IQS269_SLIDER_RAW)
1185 general |= IQS269_SYS_SETTINGS_EVENT_MODE_LP;
1187 general |= IQS269_SYS_SETTINGS_REDO_ATI;
1188 general |= IQS269_SYS_SETTINGS_ACK_RESET;
1190 sys_reg->general = cpu_to_be16(general);
1195 static const struct reg_sequence iqs269_tws_init[] = {
1196 { IQS269_TOUCH_HOLD_SLIDER_SEL, IQS269_TOUCH_HOLD_DEFAULT },
1201 static int iqs269_dev_init(struct iqs269_private *iqs269)
1205 mutex_lock(&iqs269->lock);
1208 * Early revisions of silicon require the following workaround in order
1209 * to restore any OTP-enabled functionality after a soft reset.
1211 if (iqs269->otp_option == IQS269_OTP_OPTION_TWS &&
1212 iqs269->ver_info.fw_num < IQS269_VER_INFO_FW_NUM_3) {
1213 error = regmap_multi_reg_write(iqs269->regmap, iqs269_tws_init,
1214 ARRAY_SIZE(iqs269_tws_init));
1219 error = regmap_update_bits(iqs269->regmap, IQS269_HALL_UI,
1220 IQS269_HALL_UI_ENABLE,
1221 iqs269->hall_enable ? ~0 : 0);
1225 error = regmap_raw_write(iqs269->regmap, IQS269_SYS_SETTINGS,
1226 &iqs269->sys_reg, sizeof(iqs269->sys_reg));
1231 * The following delay gives the device time to deassert its RDY output
1232 * so as to prevent an interrupt from being serviced prematurely.
1234 usleep_range(2000, 2100);
1236 iqs269->ati_current = true;
1239 mutex_unlock(&iqs269->lock);
1244 static int iqs269_input_init(struct iqs269_private *iqs269)
1246 struct i2c_client *client = iqs269->client;
1247 unsigned int sw_code, keycode;
1250 iqs269->keypad = devm_input_allocate_device(&client->dev);
1251 if (!iqs269->keypad)
1254 iqs269->keypad->keycodemax = ARRAY_SIZE(iqs269->keycode);
1255 iqs269->keypad->keycode = iqs269->keycode;
1256 iqs269->keypad->keycodesize = sizeof(*iqs269->keycode);
1258 iqs269->keypad->name = "iqs269a_keypad";
1259 iqs269->keypad->id.bustype = BUS_I2C;
1261 for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
1262 sw_code = iqs269->switches[i].code;
1264 for (j = 0; j < IQS269_NUM_CH; j++) {
1265 keycode = iqs269->keycode[i * IQS269_NUM_CH + j];
1268 * Hall-effect sensing repurposes a pair of dedicated
1269 * channels, only one of which reports events.
1272 case IQS269_CHx_HALL_ACTIVE:
1273 if (iqs269->hall_enable &&
1274 iqs269->switches[i].enabled)
1275 input_set_capability(iqs269->keypad,
1279 case IQS269_CHx_HALL_INACTIVE:
1280 if (iqs269->hall_enable)
1285 if (keycode != KEY_RESERVED)
1286 input_set_capability(iqs269->keypad,
1292 for (i = 0; i < IQS269_NUM_SL; i++) {
1293 if (iqs269_slider_type(iqs269, i) == IQS269_SLIDER_NONE)
1296 iqs269->slider[i] = devm_input_allocate_device(&client->dev);
1297 if (!iqs269->slider[i])
1300 iqs269->slider[i]->keycodemax = ARRAY_SIZE(iqs269->sl_code[i]);
1301 iqs269->slider[i]->keycode = iqs269->sl_code[i];
1302 iqs269->slider[i]->keycodesize = sizeof(**iqs269->sl_code);
1304 iqs269->slider[i]->name = i ? "iqs269a_slider_1"
1305 : "iqs269a_slider_0";
1306 iqs269->slider[i]->id.bustype = BUS_I2C;
1308 for (j = 0; j < IQS269_NUM_GESTURES; j++)
1309 if (iqs269->sl_code[i][j] != KEY_RESERVED)
1310 input_set_capability(iqs269->slider[i], EV_KEY,
1311 iqs269->sl_code[i][j]);
1314 * Present the slider as a narrow trackpad if one or more chan-
1315 * nels have been selected to participate, but no gestures have
1316 * been mapped to a keycode.
1318 if (iqs269_slider_type(iqs269, i) == IQS269_SLIDER_RAW) {
1319 input_set_capability(iqs269->slider[i],
1321 input_set_abs_params(iqs269->slider[i],
1322 ABS_X, 0, 255, 0, 0);
1325 error = input_register_device(iqs269->slider[i]);
1327 dev_err(&client->dev,
1328 "Failed to register slider %d: %d\n", i, error);
1336 static int iqs269_report(struct iqs269_private *iqs269)
1338 struct i2c_client *client = iqs269->client;
1339 struct iqs269_flags flags;
1340 unsigned int sw_code, keycode;
1342 u8 slider_x[IQS269_NUM_SL];
1345 error = regmap_raw_read(iqs269->regmap, IQS269_SYS_FLAGS, &flags,
1348 dev_err(&client->dev, "Failed to read device status: %d\n",
1354 * The device resets itself if its own watchdog bites, which can happen
1355 * in the event of an I2C communication error. In this case, the device
1356 * asserts a SHOW_RESET interrupt and all registers must be restored.
1358 if (be16_to_cpu(flags.system) & IQS269_SYS_FLAGS_SHOW_RESET) {
1359 dev_err(&client->dev, "Unexpected device reset\n");
1361 error = iqs269_dev_init(iqs269);
1363 dev_err(&client->dev,
1364 "Failed to re-initialize device: %d\n", error);
1369 if (be16_to_cpu(flags.system) & IQS269_SYS_FLAGS_IN_ATI)
1372 if (iqs269_slider_type(iqs269, 0) == IQS269_SLIDER_RAW ||
1373 iqs269_slider_type(iqs269, 1) == IQS269_SLIDER_RAW) {
1374 error = regmap_raw_read(iqs269->regmap, IQS269_SLIDER_X,
1375 slider_x, sizeof(slider_x));
1377 dev_err(&client->dev,
1378 "Failed to read slider position: %d\n", error);
1383 for (i = 0; i < IQS269_NUM_SL; i++) {
1384 flags.gesture >>= (i * IQS269_NUM_GESTURES);
1386 switch (iqs269_slider_type(iqs269, i)) {
1387 case IQS269_SLIDER_NONE:
1390 case IQS269_SLIDER_KEY:
1391 for (j = 0; j < IQS269_NUM_GESTURES; j++)
1392 input_report_key(iqs269->slider[i],
1393 iqs269->sl_code[i][j],
1394 flags.gesture & BIT(j));
1396 if (!(flags.gesture & (BIT(IQS269_GESTURE_FLICK_NEG) |
1397 BIT(IQS269_GESTURE_FLICK_POS) |
1398 BIT(IQS269_GESTURE_TAP))))
1401 input_sync(iqs269->slider[i]);
1404 * Momentary gestures are followed by a complementary
1405 * release cycle so as to emulate a full keystroke.
1407 for (j = 0; j < IQS269_NUM_GESTURES; j++)
1408 if (j != IQS269_GESTURE_HOLD)
1409 input_report_key(iqs269->slider[i],
1410 iqs269->sl_code[i][j],
1414 case IQS269_SLIDER_RAW:
1416 * The slider is considered to be in a state of touch
1417 * if any selected channels are in a state of touch.
1419 state = flags.states[IQS269_ST_OFFS_TOUCH];
1420 state &= iqs269->sys_reg.slider_select[i];
1422 input_report_key(iqs269->slider[i], BTN_TOUCH, state);
1425 input_report_abs(iqs269->slider[i],
1426 ABS_X, slider_x[i]);
1430 input_sync(iqs269->slider[i]);
1433 for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
1434 dir_mask = flags.states[IQS269_ST_OFFS_DIR];
1435 if (!iqs269_events[i].dir_up)
1436 dir_mask = ~dir_mask;
1438 state = flags.states[iqs269_events[i].st_offs] & dir_mask;
1440 sw_code = iqs269->switches[i].code;
1442 for (j = 0; j < IQS269_NUM_CH; j++) {
1443 keycode = iqs269->keycode[i * IQS269_NUM_CH + j];
1446 case IQS269_CHx_HALL_ACTIVE:
1447 if (iqs269->hall_enable &&
1448 iqs269->switches[i].enabled)
1449 input_report_switch(iqs269->keypad,
1454 case IQS269_CHx_HALL_INACTIVE:
1455 if (iqs269->hall_enable)
1460 input_report_key(iqs269->keypad, keycode,
1466 input_sync(iqs269->keypad);
1469 * The following completion signals that ATI has finished, any initial
1470 * switch states have been reported and the keypad can be registered.
1472 complete_all(&iqs269->ati_done);
1477 static irqreturn_t iqs269_irq(int irq, void *context)
1479 struct iqs269_private *iqs269 = context;
1481 if (iqs269_report(iqs269))
1485 * The device does not deassert its interrupt (RDY) pin until shortly
1486 * after receiving an I2C stop condition; the following delay ensures
1487 * the interrupt handler does not return before this time.
1494 static ssize_t counts_show(struct device *dev,
1495 struct device_attribute *attr, char *buf)
1497 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1498 struct i2c_client *client = iqs269->client;
1502 if (!iqs269->ati_current || iqs269->hall_enable)
1505 if (!completion_done(&iqs269->ati_done))
1509 * Unsolicited I2C communication prompts the device to assert its RDY
1510 * pin, so disable the interrupt line until the operation is finished
1511 * and RDY has been deasserted.
1513 disable_irq(client->irq);
1515 error = regmap_raw_read(iqs269->regmap,
1516 IQS269_CHx_COUNTS + iqs269->ch_num * 2,
1517 &counts, sizeof(counts));
1520 enable_irq(client->irq);
1525 return sysfs_emit(buf, "%u\n", le16_to_cpu(counts));
1528 static ssize_t hall_bin_show(struct device *dev,
1529 struct device_attribute *attr, char *buf)
1531 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1532 struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
1533 struct i2c_client *client = iqs269->client;
1537 disable_irq(client->irq);
1539 error = regmap_read(iqs269->regmap, IQS269_CAL_DATA_A, &val);
1542 enable_irq(client->irq);
1547 switch (ch_reg[IQS269_CHx_HALL_ACTIVE].rx_enable &
1548 ch_reg[IQS269_CHx_HALL_INACTIVE].rx_enable) {
1549 case IQS269_HALL_PAD_R:
1550 val &= IQS269_CAL_DATA_A_HALL_BIN_R_MASK;
1551 val >>= IQS269_CAL_DATA_A_HALL_BIN_R_SHIFT;
1554 case IQS269_HALL_PAD_L:
1555 val &= IQS269_CAL_DATA_A_HALL_BIN_L_MASK;
1556 val >>= IQS269_CAL_DATA_A_HALL_BIN_L_SHIFT;
1563 return sysfs_emit(buf, "%u\n", val);
1566 static ssize_t hall_enable_show(struct device *dev,
1567 struct device_attribute *attr, char *buf)
1569 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1571 return sysfs_emit(buf, "%u\n", iqs269->hall_enable);
1574 static ssize_t hall_enable_store(struct device *dev,
1575 struct device_attribute *attr, const char *buf,
1578 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1582 error = kstrtouint(buf, 10, &val);
1586 mutex_lock(&iqs269->lock);
1588 iqs269->hall_enable = val;
1589 iqs269->ati_current = false;
1591 mutex_unlock(&iqs269->lock);
1596 static ssize_t ch_number_show(struct device *dev,
1597 struct device_attribute *attr, char *buf)
1599 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1601 return sysfs_emit(buf, "%u\n", iqs269->ch_num);
1604 static ssize_t ch_number_store(struct device *dev,
1605 struct device_attribute *attr, const char *buf,
1608 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1612 error = kstrtouint(buf, 10, &val);
1616 if (val >= IQS269_NUM_CH)
1619 iqs269->ch_num = val;
1624 static ssize_t rx_enable_show(struct device *dev,
1625 struct device_attribute *attr, char *buf)
1627 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1628 struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
1630 return sysfs_emit(buf, "%u\n", ch_reg[iqs269->ch_num].rx_enable);
1633 static ssize_t rx_enable_store(struct device *dev,
1634 struct device_attribute *attr, const char *buf,
1637 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1638 struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
1642 error = kstrtouint(buf, 10, &val);
1649 mutex_lock(&iqs269->lock);
1651 ch_reg[iqs269->ch_num].rx_enable = val;
1652 iqs269->ati_current = false;
1654 mutex_unlock(&iqs269->lock);
1659 static ssize_t ati_mode_show(struct device *dev,
1660 struct device_attribute *attr, char *buf)
1662 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1666 error = iqs269_ati_mode_get(iqs269, iqs269->ch_num, &val);
1670 return sysfs_emit(buf, "%u\n", val);
1673 static ssize_t ati_mode_store(struct device *dev,
1674 struct device_attribute *attr, const char *buf,
1677 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1681 error = kstrtouint(buf, 10, &val);
1685 error = iqs269_ati_mode_set(iqs269, iqs269->ch_num, val);
1692 static ssize_t ati_base_show(struct device *dev,
1693 struct device_attribute *attr, char *buf)
1695 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1699 error = iqs269_ati_base_get(iqs269, iqs269->ch_num, &val);
1703 return sysfs_emit(buf, "%u\n", val);
1706 static ssize_t ati_base_store(struct device *dev,
1707 struct device_attribute *attr, const char *buf,
1710 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1714 error = kstrtouint(buf, 10, &val);
1718 error = iqs269_ati_base_set(iqs269, iqs269->ch_num, val);
1725 static ssize_t ati_target_show(struct device *dev,
1726 struct device_attribute *attr, char *buf)
1728 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1732 error = iqs269_ati_target_get(iqs269, iqs269->ch_num, &val);
1736 return sysfs_emit(buf, "%u\n", val);
1739 static ssize_t ati_target_store(struct device *dev,
1740 struct device_attribute *attr, const char *buf,
1743 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1747 error = kstrtouint(buf, 10, &val);
1751 error = iqs269_ati_target_set(iqs269, iqs269->ch_num, val);
1758 static ssize_t ati_trigger_show(struct device *dev,
1759 struct device_attribute *attr, char *buf)
1761 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1763 return sysfs_emit(buf, "%u\n",
1764 iqs269->ati_current &&
1765 completion_done(&iqs269->ati_done));
1768 static ssize_t ati_trigger_store(struct device *dev,
1769 struct device_attribute *attr, const char *buf,
1772 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1773 struct i2c_client *client = iqs269->client;
1777 error = kstrtouint(buf, 10, &val);
1784 disable_irq(client->irq);
1785 reinit_completion(&iqs269->ati_done);
1787 error = iqs269_dev_init(iqs269);
1790 enable_irq(client->irq);
1795 if (!wait_for_completion_timeout(&iqs269->ati_done,
1796 msecs_to_jiffies(2000)))
1802 static DEVICE_ATTR_RO(counts);
1803 static DEVICE_ATTR_RO(hall_bin);
1804 static DEVICE_ATTR_RW(hall_enable);
1805 static DEVICE_ATTR_RW(ch_number);
1806 static DEVICE_ATTR_RW(rx_enable);
1807 static DEVICE_ATTR_RW(ati_mode);
1808 static DEVICE_ATTR_RW(ati_base);
1809 static DEVICE_ATTR_RW(ati_target);
1810 static DEVICE_ATTR_RW(ati_trigger);
1812 static struct attribute *iqs269_attrs[] = {
1813 &dev_attr_counts.attr,
1814 &dev_attr_hall_bin.attr,
1815 &dev_attr_hall_enable.attr,
1816 &dev_attr_ch_number.attr,
1817 &dev_attr_rx_enable.attr,
1818 &dev_attr_ati_mode.attr,
1819 &dev_attr_ati_base.attr,
1820 &dev_attr_ati_target.attr,
1821 &dev_attr_ati_trigger.attr,
1824 ATTRIBUTE_GROUPS(iqs269);
1826 static const struct regmap_config iqs269_regmap_config = {
1829 .max_register = IQS269_MAX_REG,
1832 static int iqs269_probe(struct i2c_client *client)
1834 struct iqs269_private *iqs269;
1837 iqs269 = devm_kzalloc(&client->dev, sizeof(*iqs269), GFP_KERNEL);
1841 i2c_set_clientdata(client, iqs269);
1842 iqs269->client = client;
1844 iqs269->regmap = devm_regmap_init_i2c(client, &iqs269_regmap_config);
1845 if (IS_ERR(iqs269->regmap)) {
1846 error = PTR_ERR(iqs269->regmap);
1847 dev_err(&client->dev, "Failed to initialize register map: %d\n",
1852 mutex_init(&iqs269->lock);
1853 init_completion(&iqs269->ati_done);
1855 iqs269->otp_option = (uintptr_t)device_get_match_data(&client->dev);
1857 error = regmap_raw_read(iqs269->regmap, IQS269_VER_INFO,
1858 &iqs269->ver_info, sizeof(iqs269->ver_info));
1862 if (iqs269->ver_info.prod_num != IQS269_VER_INFO_PROD_NUM) {
1863 dev_err(&client->dev, "Unrecognized product number: 0x%02X\n",
1864 iqs269->ver_info.prod_num);
1868 error = iqs269_parse_prop(iqs269);
1872 error = iqs269_dev_init(iqs269);
1874 dev_err(&client->dev, "Failed to initialize device: %d\n",
1879 error = iqs269_input_init(iqs269);
1883 error = devm_request_threaded_irq(&client->dev, client->irq,
1884 NULL, iqs269_irq, IRQF_ONESHOT,
1885 client->name, iqs269);
1887 dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
1891 if (!wait_for_completion_timeout(&iqs269->ati_done,
1892 msecs_to_jiffies(2000))) {
1893 dev_err(&client->dev, "Failed to complete ATI\n");
1898 * The keypad may include one or more switches and is not registered
1899 * until ATI is complete and the initial switch states are read.
1901 error = input_register_device(iqs269->keypad);
1903 dev_err(&client->dev, "Failed to register keypad: %d\n", error);
1910 static u16 iqs269_general_get(struct iqs269_private *iqs269)
1912 u16 general = be16_to_cpu(iqs269->sys_reg.general);
1914 general &= ~IQS269_SYS_SETTINGS_REDO_ATI;
1915 general &= ~IQS269_SYS_SETTINGS_ACK_RESET;
1917 return general | IQS269_SYS_SETTINGS_DIS_AUTO;
1920 static int iqs269_suspend(struct device *dev)
1922 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1923 struct i2c_client *client = iqs269->client;
1925 u16 general = iqs269_general_get(iqs269);
1927 if (!(general & IQS269_SYS_SETTINGS_PWR_MODE_MASK))
1930 disable_irq(client->irq);
1932 error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS, general);
1935 enable_irq(client->irq);
1940 static int iqs269_resume(struct device *dev)
1942 struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1943 struct i2c_client *client = iqs269->client;
1945 u16 general = iqs269_general_get(iqs269);
1947 if (!(general & IQS269_SYS_SETTINGS_PWR_MODE_MASK))
1950 disable_irq(client->irq);
1952 error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS,
1953 general & ~IQS269_SYS_SETTINGS_PWR_MODE_MASK);
1955 error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS,
1956 general & ~IQS269_SYS_SETTINGS_DIS_AUTO);
1959 enable_irq(client->irq);
1964 static DEFINE_SIMPLE_DEV_PM_OPS(iqs269_pm, iqs269_suspend, iqs269_resume);
1966 static const struct of_device_id iqs269_of_match[] = {
1968 .compatible = "azoteq,iqs269a",
1969 .data = (void *)IQS269_OTP_OPTION_DEFAULT,
1972 .compatible = "azoteq,iqs269a-00",
1973 .data = (void *)IQS269_OTP_OPTION_DEFAULT,
1976 .compatible = "azoteq,iqs269a-d0",
1977 .data = (void *)IQS269_OTP_OPTION_TWS,
1981 MODULE_DEVICE_TABLE(of, iqs269_of_match);
1983 static struct i2c_driver iqs269_i2c_driver = {
1986 .dev_groups = iqs269_groups,
1987 .of_match_table = iqs269_of_match,
1988 .pm = pm_sleep_ptr(&iqs269_pm),
1990 .probe = iqs269_probe,
1992 module_i2c_driver(iqs269_i2c_driver);
1994 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
1995 MODULE_DESCRIPTION("Azoteq IQS269A Capacitive Touch Controller");
1996 MODULE_LICENSE("GPL");