xref: /linux/drivers/input/misc/iqs269a.c (revision 995832b2cebe6969d1b42635db698803ee31294d)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Azoteq IQS269A Capacitive Touch Controller
4  *
5  * Copyright (C) 2020 Jeff LaBundy <jeff@labundy.com>
6  *
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.
10  */
11 
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/module.h>
22 #include <linux/mutex.h>
23 #include <linux/property.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
26 
27 #define IQS269_VER_INFO				0x00
28 #define IQS269_VER_INFO_PROD_NUM		0x4F
29 #define IQS269_VER_INFO_FW_NUM_2		0x03
30 #define IQS269_VER_INFO_FW_NUM_3		0x10
31 
32 #define IQS269_SYS_FLAGS			0x02
33 #define IQS269_SYS_FLAGS_SHOW_RESET		BIT(15)
34 #define IQS269_SYS_FLAGS_PWR_MODE_MASK		GENMASK(12, 11)
35 #define IQS269_SYS_FLAGS_PWR_MODE_SHIFT		11
36 #define IQS269_SYS_FLAGS_IN_ATI			BIT(10)
37 
38 #define IQS269_CHx_COUNTS			0x08
39 
40 #define IQS269_SLIDER_X				0x30
41 
42 #define IQS269_CAL_DATA_A			0x35
43 #define IQS269_CAL_DATA_A_HALL_BIN_L_MASK	GENMASK(15, 12)
44 #define IQS269_CAL_DATA_A_HALL_BIN_L_SHIFT	12
45 #define IQS269_CAL_DATA_A_HALL_BIN_R_MASK	GENMASK(11, 8)
46 #define IQS269_CAL_DATA_A_HALL_BIN_R_SHIFT	8
47 
48 #define IQS269_SYS_SETTINGS			0x80
49 #define IQS269_SYS_SETTINGS_CLK_DIV		BIT(15)
50 #define IQS269_SYS_SETTINGS_ULP_AUTO		BIT(14)
51 #define IQS269_SYS_SETTINGS_DIS_AUTO		BIT(13)
52 #define IQS269_SYS_SETTINGS_PWR_MODE_MASK	GENMASK(12, 11)
53 #define IQS269_SYS_SETTINGS_PWR_MODE_SHIFT	11
54 #define IQS269_SYS_SETTINGS_PWR_MODE_MAX	3
55 #define IQS269_SYS_SETTINGS_ULP_UPDATE_MASK	GENMASK(10, 8)
56 #define IQS269_SYS_SETTINGS_ULP_UPDATE_SHIFT	8
57 #define IQS269_SYS_SETTINGS_ULP_UPDATE_MAX	7
58 #define IQS269_SYS_SETTINGS_SLIDER_SWIPE	BIT(7)
59 #define IQS269_SYS_SETTINGS_RESEED_OFFSET	BIT(6)
60 #define IQS269_SYS_SETTINGS_EVENT_MODE		BIT(5)
61 #define IQS269_SYS_SETTINGS_EVENT_MODE_LP	BIT(4)
62 #define IQS269_SYS_SETTINGS_REDO_ATI		BIT(2)
63 #define IQS269_SYS_SETTINGS_ACK_RESET		BIT(0)
64 
65 #define IQS269_FILT_STR_LP_LTA_MASK		GENMASK(7, 6)
66 #define IQS269_FILT_STR_LP_LTA_SHIFT		6
67 #define IQS269_FILT_STR_LP_CNT_MASK		GENMASK(5, 4)
68 #define IQS269_FILT_STR_LP_CNT_SHIFT		4
69 #define IQS269_FILT_STR_NP_LTA_MASK		GENMASK(3, 2)
70 #define IQS269_FILT_STR_NP_LTA_SHIFT		2
71 #define IQS269_FILT_STR_NP_CNT_MASK		GENMASK(1, 0)
72 #define IQS269_FILT_STR_MAX			3
73 
74 #define IQS269_EVENT_MASK_SYS			BIT(6)
75 #define IQS269_EVENT_MASK_GESTURE		BIT(3)
76 #define IQS269_EVENT_MASK_DEEP			BIT(2)
77 #define IQS269_EVENT_MASK_TOUCH			BIT(1)
78 #define IQS269_EVENT_MASK_PROX			BIT(0)
79 
80 #define IQS269_RATE_NP_MS_MAX			255
81 #define IQS269_RATE_LP_MS_MAX			255
82 #define IQS269_RATE_ULP_MS_MAX			4080
83 #define IQS269_TIMEOUT_PWR_MS_MAX		130560
84 #define IQS269_TIMEOUT_LTA_MS_MAX		130560
85 
86 #define IQS269_MISC_A_ATI_BAND_DISABLE		BIT(15)
87 #define IQS269_MISC_A_ATI_LP_ONLY		BIT(14)
88 #define IQS269_MISC_A_ATI_BAND_TIGHTEN		BIT(13)
89 #define IQS269_MISC_A_FILT_DISABLE		BIT(12)
90 #define IQS269_MISC_A_GPIO3_SELECT_MASK		GENMASK(10, 8)
91 #define IQS269_MISC_A_GPIO3_SELECT_SHIFT	8
92 #define IQS269_MISC_A_DUAL_DIR			BIT(6)
93 #define IQS269_MISC_A_TX_FREQ_MASK		GENMASK(5, 4)
94 #define IQS269_MISC_A_TX_FREQ_SHIFT		4
95 #define IQS269_MISC_A_TX_FREQ_MAX		3
96 #define IQS269_MISC_A_GLOBAL_CAP_SIZE		BIT(0)
97 
98 #define IQS269_MISC_B_RESEED_UI_SEL_MASK	GENMASK(7, 6)
99 #define IQS269_MISC_B_RESEED_UI_SEL_SHIFT	6
100 #define IQS269_MISC_B_RESEED_UI_SEL_MAX		3
101 #define IQS269_MISC_B_TRACKING_UI_ENABLE	BIT(4)
102 #define IQS269_MISC_B_FILT_STR_SLIDER		GENMASK(1, 0)
103 
104 #define IQS269_TOUCH_HOLD_SLIDER_SEL		0x89
105 #define IQS269_TOUCH_HOLD_DEFAULT		0x14
106 #define IQS269_TOUCH_HOLD_MS_MIN		256
107 #define IQS269_TOUCH_HOLD_MS_MAX		65280
108 
109 #define IQS269_TIMEOUT_TAP_MS_MAX		4080
110 #define IQS269_TIMEOUT_SWIPE_MS_MAX		4080
111 #define IQS269_THRESH_SWIPE_MAX			255
112 
113 #define IQS269_CHx_ENG_A_MEAS_CAP_SIZE		BIT(15)
114 #define IQS269_CHx_ENG_A_RX_GND_INACTIVE	BIT(13)
115 #define IQS269_CHx_ENG_A_LOCAL_CAP_SIZE		BIT(12)
116 #define IQS269_CHx_ENG_A_ATI_MODE_MASK		GENMASK(9, 8)
117 #define IQS269_CHx_ENG_A_ATI_MODE_SHIFT		8
118 #define IQS269_CHx_ENG_A_ATI_MODE_MAX		3
119 #define IQS269_CHx_ENG_A_INV_LOGIC		BIT(7)
120 #define IQS269_CHx_ENG_A_PROJ_BIAS_MASK		GENMASK(6, 5)
121 #define IQS269_CHx_ENG_A_PROJ_BIAS_SHIFT	5
122 #define IQS269_CHx_ENG_A_PROJ_BIAS_MAX		3
123 #define IQS269_CHx_ENG_A_SENSE_MODE_MASK	GENMASK(3, 0)
124 #define IQS269_CHx_ENG_A_SENSE_MODE_MAX		15
125 
126 #define IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE	BIT(13)
127 #define IQS269_CHx_ENG_B_SENSE_FREQ_MASK	GENMASK(10, 9)
128 #define IQS269_CHx_ENG_B_SENSE_FREQ_SHIFT	9
129 #define IQS269_CHx_ENG_B_SENSE_FREQ_MAX		3
130 #define IQS269_CHx_ENG_B_STATIC_ENABLE		BIT(8)
131 #define IQS269_CHx_ENG_B_ATI_BASE_MASK		GENMASK(7, 6)
132 #define IQS269_CHx_ENG_B_ATI_BASE_75		0x00
133 #define IQS269_CHx_ENG_B_ATI_BASE_100		0x40
134 #define IQS269_CHx_ENG_B_ATI_BASE_150		0x80
135 #define IQS269_CHx_ENG_B_ATI_BASE_200		0xC0
136 #define IQS269_CHx_ENG_B_ATI_TARGET_MASK	GENMASK(5, 0)
137 #define IQS269_CHx_ENG_B_ATI_TARGET_MAX		2016
138 
139 #define IQS269_CHx_WEIGHT_MAX			255
140 #define IQS269_CHx_THRESH_MAX			255
141 #define IQS269_CHx_HYST_DEEP_MASK		GENMASK(7, 4)
142 #define IQS269_CHx_HYST_DEEP_SHIFT		4
143 #define IQS269_CHx_HYST_TOUCH_MASK		GENMASK(3, 0)
144 #define IQS269_CHx_HYST_MAX			15
145 
146 #define IQS269_CHx_HALL_INACTIVE		6
147 #define IQS269_CHx_HALL_ACTIVE			7
148 
149 #define IQS269_HALL_PAD_R			BIT(0)
150 #define IQS269_HALL_PAD_L			BIT(1)
151 #define IQS269_HALL_PAD_INV			BIT(6)
152 
153 #define IQS269_HALL_UI				0xF5
154 #define IQS269_HALL_UI_ENABLE			BIT(15)
155 
156 #define IQS269_MAX_REG				0xFF
157 
158 #define IQS269_OTP_OPTION_DEFAULT		0x00
159 #define IQS269_OTP_OPTION_TWS			0xD0
160 #define IQS269_OTP_OPTION_HOLD			BIT(7)
161 
162 #define IQS269_NUM_CH				8
163 #define IQS269_NUM_SL				2
164 
165 #define iqs269_irq_wait()			usleep_range(200, 250)
166 
167 enum iqs269_local_cap_size {
168 	IQS269_LOCAL_CAP_SIZE_0,
169 	IQS269_LOCAL_CAP_SIZE_GLOBAL_ONLY,
170 	IQS269_LOCAL_CAP_SIZE_GLOBAL_0pF5,
171 };
172 
173 enum iqs269_st_offs {
174 	IQS269_ST_OFFS_PROX,
175 	IQS269_ST_OFFS_DIR,
176 	IQS269_ST_OFFS_TOUCH,
177 	IQS269_ST_OFFS_DEEP,
178 };
179 
180 enum iqs269_th_offs {
181 	IQS269_TH_OFFS_PROX,
182 	IQS269_TH_OFFS_TOUCH,
183 	IQS269_TH_OFFS_DEEP,
184 };
185 
186 enum iqs269_event_id {
187 	IQS269_EVENT_PROX_DN,
188 	IQS269_EVENT_PROX_UP,
189 	IQS269_EVENT_TOUCH_DN,
190 	IQS269_EVENT_TOUCH_UP,
191 	IQS269_EVENT_DEEP_DN,
192 	IQS269_EVENT_DEEP_UP,
193 };
194 
195 enum iqs269_slider_id {
196 	IQS269_SLIDER_NONE,
197 	IQS269_SLIDER_KEY,
198 	IQS269_SLIDER_RAW,
199 };
200 
201 enum iqs269_gesture_id {
202 	IQS269_GESTURE_TAP,
203 	IQS269_GESTURE_HOLD,
204 	IQS269_GESTURE_FLICK_POS,
205 	IQS269_GESTURE_FLICK_NEG,
206 	IQS269_NUM_GESTURES,
207 };
208 
209 struct iqs269_switch_desc {
210 	unsigned int code;
211 	bool enabled;
212 };
213 
214 struct iqs269_event_desc {
215 	const char *name;
216 	enum iqs269_st_offs st_offs;
217 	enum iqs269_th_offs th_offs;
218 	bool dir_up;
219 	u8 mask;
220 };
221 
222 static const struct iqs269_event_desc iqs269_events[] = {
223 	[IQS269_EVENT_PROX_DN] = {
224 		.name = "event-prox",
225 		.st_offs = IQS269_ST_OFFS_PROX,
226 		.th_offs = IQS269_TH_OFFS_PROX,
227 		.mask = IQS269_EVENT_MASK_PROX,
228 	},
229 	[IQS269_EVENT_PROX_UP] = {
230 		.name = "event-prox-alt",
231 		.st_offs = IQS269_ST_OFFS_PROX,
232 		.th_offs = IQS269_TH_OFFS_PROX,
233 		.dir_up = true,
234 		.mask = IQS269_EVENT_MASK_PROX,
235 	},
236 	[IQS269_EVENT_TOUCH_DN] = {
237 		.name = "event-touch",
238 		.st_offs = IQS269_ST_OFFS_TOUCH,
239 		.th_offs = IQS269_TH_OFFS_TOUCH,
240 		.mask = IQS269_EVENT_MASK_TOUCH,
241 	},
242 	[IQS269_EVENT_TOUCH_UP] = {
243 		.name = "event-touch-alt",
244 		.st_offs = IQS269_ST_OFFS_TOUCH,
245 		.th_offs = IQS269_TH_OFFS_TOUCH,
246 		.dir_up = true,
247 		.mask = IQS269_EVENT_MASK_TOUCH,
248 	},
249 	[IQS269_EVENT_DEEP_DN] = {
250 		.name = "event-deep",
251 		.st_offs = IQS269_ST_OFFS_DEEP,
252 		.th_offs = IQS269_TH_OFFS_DEEP,
253 		.mask = IQS269_EVENT_MASK_DEEP,
254 	},
255 	[IQS269_EVENT_DEEP_UP] = {
256 		.name = "event-deep-alt",
257 		.st_offs = IQS269_ST_OFFS_DEEP,
258 		.th_offs = IQS269_TH_OFFS_DEEP,
259 		.dir_up = true,
260 		.mask = IQS269_EVENT_MASK_DEEP,
261 	},
262 };
263 
264 struct iqs269_ver_info {
265 	u8 prod_num;
266 	u8 sw_num;
267 	u8 hw_num;
268 	u8 fw_num;
269 } __packed;
270 
271 struct iqs269_ch_reg {
272 	u8 rx_enable;
273 	u8 tx_enable;
274 	__be16 engine_a;
275 	__be16 engine_b;
276 	__be16 ati_comp;
277 	u8 thresh[3];
278 	u8 hyst;
279 	u8 assoc_select;
280 	u8 assoc_weight;
281 } __packed;
282 
283 struct iqs269_sys_reg {
284 	__be16 general;
285 	u8 active;
286 	u8 filter;
287 	u8 reseed;
288 	u8 event_mask;
289 	u8 rate_np;
290 	u8 rate_lp;
291 	u8 rate_ulp;
292 	u8 timeout_pwr;
293 	u8 timeout_rdy;
294 	u8 timeout_lta;
295 	__be16 misc_a;
296 	__be16 misc_b;
297 	u8 blocking;
298 	u8 padding;
299 	u8 slider_select[IQS269_NUM_SL];
300 	u8 timeout_tap;
301 	u8 timeout_swipe;
302 	u8 thresh_swipe;
303 	u8 redo_ati;
304 	struct iqs269_ch_reg ch_reg[IQS269_NUM_CH];
305 } __packed;
306 
307 struct iqs269_flags {
308 	__be16 system;
309 	u8 gesture;
310 	u8 padding;
311 	u8 states[4];
312 } __packed;
313 
314 struct iqs269_private {
315 	struct i2c_client *client;
316 	struct regmap *regmap;
317 	struct mutex lock;
318 	struct iqs269_switch_desc switches[ARRAY_SIZE(iqs269_events)];
319 	struct iqs269_ver_info ver_info;
320 	struct iqs269_sys_reg sys_reg;
321 	struct completion ati_done;
322 	struct input_dev *keypad;
323 	struct input_dev *slider[IQS269_NUM_SL];
324 	unsigned int keycode[ARRAY_SIZE(iqs269_events) * IQS269_NUM_CH];
325 	unsigned int sl_code[IQS269_NUM_SL][IQS269_NUM_GESTURES];
326 	unsigned int otp_option;
327 	unsigned int ch_num;
328 	bool hall_enable;
329 	bool ati_current;
330 };
331 
332 static enum iqs269_slider_id iqs269_slider_type(struct iqs269_private *iqs269,
333 						int slider_num)
334 {
335 	int i;
336 
337 	/*
338 	 * Slider 1 is unavailable if the touch-and-hold option is enabled via
339 	 * OTP. In that case, the channel selection register is repurposed for
340 	 * the touch-and-hold timer ceiling.
341 	 */
342 	if (slider_num && (iqs269->otp_option & IQS269_OTP_OPTION_HOLD))
343 		return IQS269_SLIDER_NONE;
344 
345 	if (!iqs269->sys_reg.slider_select[slider_num])
346 		return IQS269_SLIDER_NONE;
347 
348 	for (i = 0; i < IQS269_NUM_GESTURES; i++)
349 		if (iqs269->sl_code[slider_num][i] != KEY_RESERVED)
350 			return IQS269_SLIDER_KEY;
351 
352 	return IQS269_SLIDER_RAW;
353 }
354 
355 static int iqs269_ati_mode_set(struct iqs269_private *iqs269,
356 			       unsigned int ch_num, unsigned int mode)
357 {
358 	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
359 	u16 engine_a;
360 
361 	if (ch_num >= IQS269_NUM_CH)
362 		return -EINVAL;
363 
364 	if (mode > IQS269_CHx_ENG_A_ATI_MODE_MAX)
365 		return -EINVAL;
366 
367 	guard(mutex)(&iqs269->lock);
368 
369 	engine_a = be16_to_cpu(ch_reg[ch_num].engine_a);
370 
371 	engine_a &= ~IQS269_CHx_ENG_A_ATI_MODE_MASK;
372 	engine_a |= (mode << IQS269_CHx_ENG_A_ATI_MODE_SHIFT);
373 
374 	ch_reg[ch_num].engine_a = cpu_to_be16(engine_a);
375 	iqs269->ati_current = false;
376 
377 	return 0;
378 }
379 
380 static int iqs269_ati_mode_get(struct iqs269_private *iqs269,
381 			       unsigned int ch_num, unsigned int *mode)
382 {
383 	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
384 	u16 engine_a;
385 
386 	if (ch_num >= IQS269_NUM_CH)
387 		return -EINVAL;
388 
389 	guard(mutex)(&iqs269->lock);
390 
391 	engine_a = be16_to_cpu(ch_reg[ch_num].engine_a);
392 
393 	engine_a &= IQS269_CHx_ENG_A_ATI_MODE_MASK;
394 	*mode = (engine_a >> IQS269_CHx_ENG_A_ATI_MODE_SHIFT);
395 
396 	return 0;
397 }
398 
399 static int iqs269_ati_base_set(struct iqs269_private *iqs269,
400 			       unsigned int ch_num, unsigned int base)
401 {
402 	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
403 	u16 engine_b;
404 
405 	if (ch_num >= IQS269_NUM_CH)
406 		return -EINVAL;
407 
408 	switch (base) {
409 	case 75:
410 		base = IQS269_CHx_ENG_B_ATI_BASE_75;
411 		break;
412 
413 	case 100:
414 		base = IQS269_CHx_ENG_B_ATI_BASE_100;
415 		break;
416 
417 	case 150:
418 		base = IQS269_CHx_ENG_B_ATI_BASE_150;
419 		break;
420 
421 	case 200:
422 		base = IQS269_CHx_ENG_B_ATI_BASE_200;
423 		break;
424 
425 	default:
426 		return -EINVAL;
427 	}
428 
429 	guard(mutex)(&iqs269->lock);
430 
431 	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
432 
433 	engine_b &= ~IQS269_CHx_ENG_B_ATI_BASE_MASK;
434 	engine_b |= base;
435 
436 	ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
437 	iqs269->ati_current = false;
438 
439 	return 0;
440 }
441 
442 static int iqs269_ati_base_get(struct iqs269_private *iqs269,
443 			       unsigned int ch_num, unsigned int *base)
444 {
445 	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
446 	u16 engine_b;
447 
448 	if (ch_num >= IQS269_NUM_CH)
449 		return -EINVAL;
450 
451 	guard(mutex)(&iqs269->lock);
452 
453 	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
454 
455 	switch (engine_b & IQS269_CHx_ENG_B_ATI_BASE_MASK) {
456 	case IQS269_CHx_ENG_B_ATI_BASE_75:
457 		*base = 75;
458 		return 0;
459 
460 	case IQS269_CHx_ENG_B_ATI_BASE_100:
461 		*base = 100;
462 		return 0;
463 
464 	case IQS269_CHx_ENG_B_ATI_BASE_150:
465 		*base = 150;
466 		return 0;
467 
468 	case IQS269_CHx_ENG_B_ATI_BASE_200:
469 		*base = 200;
470 		return 0;
471 
472 	default:
473 		return -EINVAL;
474 	}
475 }
476 
477 static int iqs269_ati_target_set(struct iqs269_private *iqs269,
478 				 unsigned int ch_num, unsigned int target)
479 {
480 	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
481 	u16 engine_b;
482 
483 	if (ch_num >= IQS269_NUM_CH)
484 		return -EINVAL;
485 
486 	if (target > IQS269_CHx_ENG_B_ATI_TARGET_MAX)
487 		return -EINVAL;
488 
489 	guard(mutex)(&iqs269->lock);
490 
491 	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
492 
493 	engine_b &= ~IQS269_CHx_ENG_B_ATI_TARGET_MASK;
494 	engine_b |= target / 32;
495 
496 	ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
497 	iqs269->ati_current = false;
498 
499 	return 0;
500 }
501 
502 static int iqs269_ati_target_get(struct iqs269_private *iqs269,
503 				 unsigned int ch_num, unsigned int *target)
504 {
505 	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
506 	u16 engine_b;
507 
508 	if (ch_num >= IQS269_NUM_CH)
509 		return -EINVAL;
510 
511 	guard(mutex)(&iqs269->lock);
512 
513 	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
514 	*target = (engine_b & IQS269_CHx_ENG_B_ATI_TARGET_MASK) * 32;
515 
516 	return 0;
517 }
518 
519 static int iqs269_parse_mask(const struct fwnode_handle *fwnode,
520 			     const char *propname, u8 *mask)
521 {
522 	unsigned int val[IQS269_NUM_CH];
523 	int count, error, i;
524 
525 	count = fwnode_property_count_u32(fwnode, propname);
526 	if (count < 0)
527 		return 0;
528 
529 	if (count > IQS269_NUM_CH)
530 		return -EINVAL;
531 
532 	error = fwnode_property_read_u32_array(fwnode, propname, val, count);
533 	if (error)
534 		return error;
535 
536 	*mask = 0;
537 
538 	for (i = 0; i < count; i++) {
539 		if (val[i] >= IQS269_NUM_CH)
540 			return -EINVAL;
541 
542 		*mask |= BIT(val[i]);
543 	}
544 
545 	return 0;
546 }
547 
548 static int iqs269_parse_chan(struct iqs269_private *iqs269,
549 			     const struct fwnode_handle *ch_node)
550 {
551 	struct i2c_client *client = iqs269->client;
552 	struct iqs269_ch_reg *ch_reg;
553 	u16 engine_a, engine_b;
554 	unsigned int reg, val;
555 	int error, i;
556 
557 	error = fwnode_property_read_u32(ch_node, "reg", &reg);
558 	if (error) {
559 		dev_err(&client->dev, "Failed to read channel number: %d\n",
560 			error);
561 		return error;
562 	} else if (reg >= IQS269_NUM_CH) {
563 		dev_err(&client->dev, "Invalid channel number: %u\n", reg);
564 		return -EINVAL;
565 	}
566 
567 	iqs269->sys_reg.active |= BIT(reg);
568 	if (!fwnode_property_present(ch_node, "azoteq,reseed-disable"))
569 		iqs269->sys_reg.reseed |= BIT(reg);
570 
571 	if (fwnode_property_present(ch_node, "azoteq,blocking-enable"))
572 		iqs269->sys_reg.blocking |= BIT(reg);
573 
574 	if (fwnode_property_present(ch_node, "azoteq,slider0-select"))
575 		iqs269->sys_reg.slider_select[0] |= BIT(reg);
576 
577 	if (fwnode_property_present(ch_node, "azoteq,slider1-select") &&
578 	    !(iqs269->otp_option & IQS269_OTP_OPTION_HOLD))
579 		iqs269->sys_reg.slider_select[1] |= BIT(reg);
580 
581 	ch_reg = &iqs269->sys_reg.ch_reg[reg];
582 
583 	error = iqs269_parse_mask(ch_node, "azoteq,rx-enable",
584 				  &ch_reg->rx_enable);
585 	if (error) {
586 		dev_err(&client->dev, "Invalid channel %u RX enable mask: %d\n",
587 			reg, error);
588 		return error;
589 	}
590 
591 	error = iqs269_parse_mask(ch_node, "azoteq,tx-enable",
592 				  &ch_reg->tx_enable);
593 	if (error) {
594 		dev_err(&client->dev, "Invalid channel %u TX enable mask: %d\n",
595 			reg, error);
596 		return error;
597 	}
598 
599 	engine_a = be16_to_cpu(ch_reg->engine_a);
600 	engine_b = be16_to_cpu(ch_reg->engine_b);
601 
602 	engine_a |= IQS269_CHx_ENG_A_MEAS_CAP_SIZE;
603 	if (fwnode_property_present(ch_node, "azoteq,meas-cap-decrease"))
604 		engine_a &= ~IQS269_CHx_ENG_A_MEAS_CAP_SIZE;
605 
606 	engine_a |= IQS269_CHx_ENG_A_RX_GND_INACTIVE;
607 	if (fwnode_property_present(ch_node, "azoteq,rx-float-inactive"))
608 		engine_a &= ~IQS269_CHx_ENG_A_RX_GND_INACTIVE;
609 
610 	engine_a &= ~IQS269_CHx_ENG_A_LOCAL_CAP_SIZE;
611 	engine_b &= ~IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE;
612 	if (!fwnode_property_read_u32(ch_node, "azoteq,local-cap-size", &val)) {
613 		switch (val) {
614 		case IQS269_LOCAL_CAP_SIZE_0:
615 			break;
616 
617 		case IQS269_LOCAL_CAP_SIZE_GLOBAL_0pF5:
618 			engine_a |= IQS269_CHx_ENG_A_LOCAL_CAP_SIZE;
619 			fallthrough;
620 
621 		case IQS269_LOCAL_CAP_SIZE_GLOBAL_ONLY:
622 			engine_b |= IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE;
623 			break;
624 
625 		default:
626 			dev_err(&client->dev,
627 				"Invalid channel %u local cap. size: %u\n", reg,
628 				val);
629 			return -EINVAL;
630 		}
631 	}
632 
633 	engine_a &= ~IQS269_CHx_ENG_A_INV_LOGIC;
634 	if (fwnode_property_present(ch_node, "azoteq,invert-enable"))
635 		engine_a |= IQS269_CHx_ENG_A_INV_LOGIC;
636 
637 	if (!fwnode_property_read_u32(ch_node, "azoteq,proj-bias", &val)) {
638 		if (val > IQS269_CHx_ENG_A_PROJ_BIAS_MAX) {
639 			dev_err(&client->dev,
640 				"Invalid channel %u bias current: %u\n", reg,
641 				val);
642 			return -EINVAL;
643 		}
644 
645 		engine_a &= ~IQS269_CHx_ENG_A_PROJ_BIAS_MASK;
646 		engine_a |= (val << IQS269_CHx_ENG_A_PROJ_BIAS_SHIFT);
647 	}
648 
649 	if (!fwnode_property_read_u32(ch_node, "azoteq,sense-mode", &val)) {
650 		if (val > IQS269_CHx_ENG_A_SENSE_MODE_MAX) {
651 			dev_err(&client->dev,
652 				"Invalid channel %u sensing mode: %u\n", reg,
653 				val);
654 			return -EINVAL;
655 		}
656 
657 		engine_a &= ~IQS269_CHx_ENG_A_SENSE_MODE_MASK;
658 		engine_a |= val;
659 	}
660 
661 	if (!fwnode_property_read_u32(ch_node, "azoteq,sense-freq", &val)) {
662 		if (val > IQS269_CHx_ENG_B_SENSE_FREQ_MAX) {
663 			dev_err(&client->dev,
664 				"Invalid channel %u sensing frequency: %u\n",
665 				reg, val);
666 			return -EINVAL;
667 		}
668 
669 		engine_b &= ~IQS269_CHx_ENG_B_SENSE_FREQ_MASK;
670 		engine_b |= (val << IQS269_CHx_ENG_B_SENSE_FREQ_SHIFT);
671 	}
672 
673 	engine_b &= ~IQS269_CHx_ENG_B_STATIC_ENABLE;
674 	if (fwnode_property_present(ch_node, "azoteq,static-enable"))
675 		engine_b |= IQS269_CHx_ENG_B_STATIC_ENABLE;
676 
677 	ch_reg->engine_a = cpu_to_be16(engine_a);
678 	ch_reg->engine_b = cpu_to_be16(engine_b);
679 
680 	if (!fwnode_property_read_u32(ch_node, "azoteq,ati-mode", &val)) {
681 		error = iqs269_ati_mode_set(iqs269, reg, val);
682 		if (error) {
683 			dev_err(&client->dev,
684 				"Invalid channel %u ATI mode: %u\n", reg, val);
685 			return error;
686 		}
687 	}
688 
689 	if (!fwnode_property_read_u32(ch_node, "azoteq,ati-base", &val)) {
690 		error = iqs269_ati_base_set(iqs269, reg, val);
691 		if (error) {
692 			dev_err(&client->dev,
693 				"Invalid channel %u ATI base: %u\n", reg, val);
694 			return error;
695 		}
696 	}
697 
698 	if (!fwnode_property_read_u32(ch_node, "azoteq,ati-target", &val)) {
699 		error = iqs269_ati_target_set(iqs269, reg, val);
700 		if (error) {
701 			dev_err(&client->dev,
702 				"Invalid channel %u ATI target: %u\n", reg,
703 				val);
704 			return error;
705 		}
706 	}
707 
708 	error = iqs269_parse_mask(ch_node, "azoteq,assoc-select",
709 				  &ch_reg->assoc_select);
710 	if (error) {
711 		dev_err(&client->dev, "Invalid channel %u association: %d\n",
712 			reg, error);
713 		return error;
714 	}
715 
716 	if (!fwnode_property_read_u32(ch_node, "azoteq,assoc-weight", &val)) {
717 		if (val > IQS269_CHx_WEIGHT_MAX) {
718 			dev_err(&client->dev,
719 				"Invalid channel %u associated weight: %u\n",
720 				reg, val);
721 			return -EINVAL;
722 		}
723 
724 		ch_reg->assoc_weight = val;
725 	}
726 
727 	for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
728 		struct fwnode_handle *ev_node __free(fwnode_handle) =
729 			fwnode_get_named_child_node(ch_node,
730 						    iqs269_events[i].name);
731 		if (!ev_node)
732 			continue;
733 
734 		if (!fwnode_property_read_u32(ev_node, "azoteq,thresh", &val)) {
735 			if (val > IQS269_CHx_THRESH_MAX) {
736 				dev_err(&client->dev,
737 					"Invalid channel %u threshold: %u\n",
738 					reg, val);
739 				return -EINVAL;
740 			}
741 
742 			ch_reg->thresh[iqs269_events[i].th_offs] = val;
743 		}
744 
745 		if (!fwnode_property_read_u32(ev_node, "azoteq,hyst", &val)) {
746 			u8 *hyst = &ch_reg->hyst;
747 
748 			if (val > IQS269_CHx_HYST_MAX) {
749 				dev_err(&client->dev,
750 					"Invalid channel %u hysteresis: %u\n",
751 					reg, val);
752 				return -EINVAL;
753 			}
754 
755 			if (i == IQS269_EVENT_DEEP_DN ||
756 			    i == IQS269_EVENT_DEEP_UP) {
757 				*hyst &= ~IQS269_CHx_HYST_DEEP_MASK;
758 				*hyst |= (val << IQS269_CHx_HYST_DEEP_SHIFT);
759 			} else if (i == IQS269_EVENT_TOUCH_DN ||
760 				   i == IQS269_EVENT_TOUCH_UP) {
761 				*hyst &= ~IQS269_CHx_HYST_TOUCH_MASK;
762 				*hyst |= val;
763 			}
764 		}
765 
766 		error = fwnode_property_read_u32(ev_node, "linux,code", &val);
767 		if (error == -EINVAL) {
768 			continue;
769 		} else if (error) {
770 			dev_err(&client->dev,
771 				"Failed to read channel %u code: %d\n", reg,
772 				error);
773 			return error;
774 		}
775 
776 		switch (reg) {
777 		case IQS269_CHx_HALL_ACTIVE:
778 			if (iqs269->hall_enable) {
779 				iqs269->switches[i].code = val;
780 				iqs269->switches[i].enabled = true;
781 			}
782 			fallthrough;
783 
784 		case IQS269_CHx_HALL_INACTIVE:
785 			if (iqs269->hall_enable)
786 				break;
787 			fallthrough;
788 
789 		default:
790 			iqs269->keycode[i * IQS269_NUM_CH + reg] = val;
791 		}
792 
793 		iqs269->sys_reg.event_mask &= ~iqs269_events[i].mask;
794 	}
795 
796 	return 0;
797 }
798 
799 static int iqs269_parse_prop(struct iqs269_private *iqs269)
800 {
801 	struct iqs269_sys_reg *sys_reg = &iqs269->sys_reg;
802 	struct i2c_client *client = iqs269->client;
803 	u16 general, misc_a, misc_b;
804 	unsigned int val;
805 	int error;
806 
807 	iqs269->hall_enable = device_property_present(&client->dev,
808 						      "azoteq,hall-enable");
809 
810 	error = regmap_raw_read(iqs269->regmap, IQS269_SYS_SETTINGS, sys_reg,
811 				sizeof(*sys_reg));
812 	if (error)
813 		return error;
814 
815 	if (!device_property_read_u32(&client->dev, "azoteq,filt-str-lp-lta",
816 				      &val)) {
817 		if (val > IQS269_FILT_STR_MAX) {
818 			dev_err(&client->dev, "Invalid filter strength: %u\n",
819 				val);
820 			return -EINVAL;
821 		}
822 
823 		sys_reg->filter &= ~IQS269_FILT_STR_LP_LTA_MASK;
824 		sys_reg->filter |= (val << IQS269_FILT_STR_LP_LTA_SHIFT);
825 	}
826 
827 	if (!device_property_read_u32(&client->dev, "azoteq,filt-str-lp-cnt",
828 				      &val)) {
829 		if (val > IQS269_FILT_STR_MAX) {
830 			dev_err(&client->dev, "Invalid filter strength: %u\n",
831 				val);
832 			return -EINVAL;
833 		}
834 
835 		sys_reg->filter &= ~IQS269_FILT_STR_LP_CNT_MASK;
836 		sys_reg->filter |= (val << IQS269_FILT_STR_LP_CNT_SHIFT);
837 	}
838 
839 	if (!device_property_read_u32(&client->dev, "azoteq,filt-str-np-lta",
840 				      &val)) {
841 		if (val > IQS269_FILT_STR_MAX) {
842 			dev_err(&client->dev, "Invalid filter strength: %u\n",
843 				val);
844 			return -EINVAL;
845 		}
846 
847 		sys_reg->filter &= ~IQS269_FILT_STR_NP_LTA_MASK;
848 		sys_reg->filter |= (val << IQS269_FILT_STR_NP_LTA_SHIFT);
849 	}
850 
851 	if (!device_property_read_u32(&client->dev, "azoteq,filt-str-np-cnt",
852 				      &val)) {
853 		if (val > IQS269_FILT_STR_MAX) {
854 			dev_err(&client->dev, "Invalid filter strength: %u\n",
855 				val);
856 			return -EINVAL;
857 		}
858 
859 		sys_reg->filter &= ~IQS269_FILT_STR_NP_CNT_MASK;
860 		sys_reg->filter |= val;
861 	}
862 
863 	if (!device_property_read_u32(&client->dev, "azoteq,rate-np-ms",
864 				      &val)) {
865 		if (val > IQS269_RATE_NP_MS_MAX) {
866 			dev_err(&client->dev, "Invalid report rate: %u\n", val);
867 			return -EINVAL;
868 		}
869 
870 		sys_reg->rate_np = val;
871 	}
872 
873 	if (!device_property_read_u32(&client->dev, "azoteq,rate-lp-ms",
874 				      &val)) {
875 		if (val > IQS269_RATE_LP_MS_MAX) {
876 			dev_err(&client->dev, "Invalid report rate: %u\n", val);
877 			return -EINVAL;
878 		}
879 
880 		sys_reg->rate_lp = val;
881 	}
882 
883 	if (!device_property_read_u32(&client->dev, "azoteq,rate-ulp-ms",
884 				      &val)) {
885 		if (val > IQS269_RATE_ULP_MS_MAX) {
886 			dev_err(&client->dev, "Invalid report rate: %u\n", val);
887 			return -EINVAL;
888 		}
889 
890 		sys_reg->rate_ulp = val / 16;
891 	}
892 
893 	if (!device_property_read_u32(&client->dev, "azoteq,timeout-pwr-ms",
894 				      &val)) {
895 		if (val > IQS269_TIMEOUT_PWR_MS_MAX) {
896 			dev_err(&client->dev, "Invalid timeout: %u\n", val);
897 			return -EINVAL;
898 		}
899 
900 		sys_reg->timeout_pwr = val / 512;
901 	}
902 
903 	if (!device_property_read_u32(&client->dev, "azoteq,timeout-lta-ms",
904 				      &val)) {
905 		if (val > IQS269_TIMEOUT_LTA_MS_MAX) {
906 			dev_err(&client->dev, "Invalid timeout: %u\n", val);
907 			return -EINVAL;
908 		}
909 
910 		sys_reg->timeout_lta = val / 512;
911 	}
912 
913 	misc_a = be16_to_cpu(sys_reg->misc_a);
914 	misc_b = be16_to_cpu(sys_reg->misc_b);
915 
916 	misc_a &= ~IQS269_MISC_A_ATI_BAND_DISABLE;
917 	if (device_property_present(&client->dev, "azoteq,ati-band-disable"))
918 		misc_a |= IQS269_MISC_A_ATI_BAND_DISABLE;
919 
920 	misc_a &= ~IQS269_MISC_A_ATI_LP_ONLY;
921 	if (device_property_present(&client->dev, "azoteq,ati-lp-only"))
922 		misc_a |= IQS269_MISC_A_ATI_LP_ONLY;
923 
924 	misc_a &= ~IQS269_MISC_A_ATI_BAND_TIGHTEN;
925 	if (device_property_present(&client->dev, "azoteq,ati-band-tighten"))
926 		misc_a |= IQS269_MISC_A_ATI_BAND_TIGHTEN;
927 
928 	misc_a &= ~IQS269_MISC_A_FILT_DISABLE;
929 	if (device_property_present(&client->dev, "azoteq,filt-disable"))
930 		misc_a |= IQS269_MISC_A_FILT_DISABLE;
931 
932 	if (!device_property_read_u32(&client->dev, "azoteq,gpio3-select",
933 				      &val)) {
934 		if (val >= IQS269_NUM_CH) {
935 			dev_err(&client->dev, "Invalid GPIO3 selection: %u\n",
936 				val);
937 			return -EINVAL;
938 		}
939 
940 		misc_a &= ~IQS269_MISC_A_GPIO3_SELECT_MASK;
941 		misc_a |= (val << IQS269_MISC_A_GPIO3_SELECT_SHIFT);
942 	}
943 
944 	misc_a &= ~IQS269_MISC_A_DUAL_DIR;
945 	if (device_property_present(&client->dev, "azoteq,dual-direction"))
946 		misc_a |= IQS269_MISC_A_DUAL_DIR;
947 
948 	if (!device_property_read_u32(&client->dev, "azoteq,tx-freq", &val)) {
949 		if (val > IQS269_MISC_A_TX_FREQ_MAX) {
950 			dev_err(&client->dev,
951 				"Invalid excitation frequency: %u\n", val);
952 			return -EINVAL;
953 		}
954 
955 		misc_a &= ~IQS269_MISC_A_TX_FREQ_MASK;
956 		misc_a |= (val << IQS269_MISC_A_TX_FREQ_SHIFT);
957 	}
958 
959 	misc_a &= ~IQS269_MISC_A_GLOBAL_CAP_SIZE;
960 	if (device_property_present(&client->dev, "azoteq,global-cap-increase"))
961 		misc_a |= IQS269_MISC_A_GLOBAL_CAP_SIZE;
962 
963 	if (!device_property_read_u32(&client->dev, "azoteq,reseed-select",
964 				      &val)) {
965 		if (val > IQS269_MISC_B_RESEED_UI_SEL_MAX) {
966 			dev_err(&client->dev, "Invalid reseed selection: %u\n",
967 				val);
968 			return -EINVAL;
969 		}
970 
971 		misc_b &= ~IQS269_MISC_B_RESEED_UI_SEL_MASK;
972 		misc_b |= (val << IQS269_MISC_B_RESEED_UI_SEL_SHIFT);
973 	}
974 
975 	misc_b &= ~IQS269_MISC_B_TRACKING_UI_ENABLE;
976 	if (device_property_present(&client->dev, "azoteq,tracking-enable"))
977 		misc_b |= IQS269_MISC_B_TRACKING_UI_ENABLE;
978 
979 	if (!device_property_read_u32(&client->dev, "azoteq,filt-str-slider",
980 				      &val)) {
981 		if (val > IQS269_FILT_STR_MAX) {
982 			dev_err(&client->dev, "Invalid filter strength: %u\n",
983 				val);
984 			return -EINVAL;
985 		}
986 
987 		misc_b &= ~IQS269_MISC_B_FILT_STR_SLIDER;
988 		misc_b |= val;
989 	}
990 
991 	sys_reg->misc_a = cpu_to_be16(misc_a);
992 	sys_reg->misc_b = cpu_to_be16(misc_b);
993 
994 	sys_reg->active = 0;
995 	sys_reg->reseed = 0;
996 
997 	sys_reg->blocking = 0;
998 
999 	sys_reg->slider_select[0] = 0;
1000 
1001 	/*
1002 	 * If configured via OTP to do so, the device asserts a pulse on the
1003 	 * GPIO4 pin for approximately 60 ms once a selected channel is held
1004 	 * in a state of touch for a configurable length of time.
1005 	 *
1006 	 * In that case, the register used for slider 1 channel selection is
1007 	 * repurposed for the touch-and-hold timer ceiling.
1008 	 */
1009 	if (iqs269->otp_option & IQS269_OTP_OPTION_HOLD) {
1010 		if (!device_property_read_u32(&client->dev,
1011 					      "azoteq,touch-hold-ms", &val)) {
1012 			if (val < IQS269_TOUCH_HOLD_MS_MIN ||
1013 			    val > IQS269_TOUCH_HOLD_MS_MAX) {
1014 				dev_err(&client->dev,
1015 					"Invalid touch-and-hold ceiling: %u\n",
1016 					val);
1017 				return -EINVAL;
1018 			}
1019 
1020 			sys_reg->slider_select[1] = val / 256;
1021 		} else if (iqs269->ver_info.fw_num < IQS269_VER_INFO_FW_NUM_3) {
1022 			/*
1023 			 * The default touch-and-hold timer ceiling initially
1024 			 * read from early revisions of silicon is invalid if
1025 			 * the device experienced a soft reset between power-
1026 			 * on and the read operation.
1027 			 *
1028 			 * To protect against this case, explicitly cache the
1029 			 * default value so that it is restored each time the
1030 			 * device is re-initialized.
1031 			 */
1032 			sys_reg->slider_select[1] = IQS269_TOUCH_HOLD_DEFAULT;
1033 		}
1034 	} else {
1035 		sys_reg->slider_select[1] = 0;
1036 	}
1037 
1038 	sys_reg->event_mask = ~((u8)IQS269_EVENT_MASK_SYS);
1039 
1040 	device_for_each_child_node_scoped(&client->dev, ch_node) {
1041 		error = iqs269_parse_chan(iqs269, ch_node);
1042 		if (error)
1043 			return error;
1044 	}
1045 
1046 	/*
1047 	 * Volunteer all active channels to participate in ATI when REDO-ATI is
1048 	 * manually triggered.
1049 	 */
1050 	sys_reg->redo_ati = sys_reg->active;
1051 
1052 	general = be16_to_cpu(sys_reg->general);
1053 
1054 	if (device_property_present(&client->dev, "azoteq,clk-div"))
1055 		general |= IQS269_SYS_SETTINGS_CLK_DIV;
1056 
1057 	/*
1058 	 * Configure the device to automatically switch between normal and low-
1059 	 * power modes as a function of sensing activity. Ultra-low-power mode,
1060 	 * if enabled, is reserved for suspend.
1061 	 */
1062 	general &= ~IQS269_SYS_SETTINGS_ULP_AUTO;
1063 	general &= ~IQS269_SYS_SETTINGS_DIS_AUTO;
1064 	general &= ~IQS269_SYS_SETTINGS_PWR_MODE_MASK;
1065 
1066 	if (!device_property_read_u32(&client->dev, "azoteq,suspend-mode",
1067 				      &val)) {
1068 		if (val > IQS269_SYS_SETTINGS_PWR_MODE_MAX) {
1069 			dev_err(&client->dev, "Invalid suspend mode: %u\n",
1070 				val);
1071 			return -EINVAL;
1072 		}
1073 
1074 		general |= (val << IQS269_SYS_SETTINGS_PWR_MODE_SHIFT);
1075 	}
1076 
1077 	if (!device_property_read_u32(&client->dev, "azoteq,ulp-update",
1078 				      &val)) {
1079 		if (val > IQS269_SYS_SETTINGS_ULP_UPDATE_MAX) {
1080 			dev_err(&client->dev, "Invalid update rate: %u\n", val);
1081 			return -EINVAL;
1082 		}
1083 
1084 		general &= ~IQS269_SYS_SETTINGS_ULP_UPDATE_MASK;
1085 		general |= (val << IQS269_SYS_SETTINGS_ULP_UPDATE_SHIFT);
1086 	}
1087 
1088 	if (device_property_present(&client->dev, "linux,keycodes")) {
1089 		int scale = 1;
1090 		int count = device_property_count_u32(&client->dev,
1091 						      "linux,keycodes");
1092 		if (count > IQS269_NUM_GESTURES * IQS269_NUM_SL) {
1093 			dev_err(&client->dev, "Too many keycodes present\n");
1094 			return -EINVAL;
1095 		} else if (count < 0) {
1096 			dev_err(&client->dev, "Failed to count keycodes: %d\n",
1097 				count);
1098 			return count;
1099 		}
1100 
1101 		error = device_property_read_u32_array(&client->dev,
1102 						       "linux,keycodes",
1103 						       *iqs269->sl_code, count);
1104 		if (error) {
1105 			dev_err(&client->dev, "Failed to read keycodes: %d\n",
1106 				error);
1107 			return error;
1108 		}
1109 
1110 		if (device_property_present(&client->dev,
1111 					    "azoteq,gesture-swipe"))
1112 			general |= IQS269_SYS_SETTINGS_SLIDER_SWIPE;
1113 
1114 		/*
1115 		 * Early revisions of silicon use a more granular step size for
1116 		 * tap and swipe gesture timeouts; scale them appropriately.
1117 		 */
1118 		if (iqs269->ver_info.fw_num < IQS269_VER_INFO_FW_NUM_3)
1119 			scale = 4;
1120 
1121 		if (!device_property_read_u32(&client->dev,
1122 					      "azoteq,timeout-tap-ms", &val)) {
1123 			if (val > IQS269_TIMEOUT_TAP_MS_MAX / scale) {
1124 				dev_err(&client->dev, "Invalid timeout: %u\n",
1125 					val);
1126 				return -EINVAL;
1127 			}
1128 
1129 			sys_reg->timeout_tap = val / (16 / scale);
1130 		}
1131 
1132 		if (!device_property_read_u32(&client->dev,
1133 					      "azoteq,timeout-swipe-ms",
1134 					      &val)) {
1135 			if (val > IQS269_TIMEOUT_SWIPE_MS_MAX / scale) {
1136 				dev_err(&client->dev, "Invalid timeout: %u\n",
1137 					val);
1138 				return -EINVAL;
1139 			}
1140 
1141 			sys_reg->timeout_swipe = val / (16 / scale);
1142 		}
1143 
1144 		if (!device_property_read_u32(&client->dev,
1145 					      "azoteq,thresh-swipe", &val)) {
1146 			if (val > IQS269_THRESH_SWIPE_MAX) {
1147 				dev_err(&client->dev, "Invalid threshold: %u\n",
1148 					val);
1149 				return -EINVAL;
1150 			}
1151 
1152 			sys_reg->thresh_swipe = val;
1153 		}
1154 
1155 		sys_reg->event_mask &= ~IQS269_EVENT_MASK_GESTURE;
1156 	}
1157 
1158 	general &= ~IQS269_SYS_SETTINGS_RESEED_OFFSET;
1159 	if (device_property_present(&client->dev, "azoteq,reseed-offset"))
1160 		general |= IQS269_SYS_SETTINGS_RESEED_OFFSET;
1161 
1162 	general |= IQS269_SYS_SETTINGS_EVENT_MODE;
1163 
1164 	/*
1165 	 * As per the datasheet, enable streaming during normal-power mode if
1166 	 * raw coordinates will be read from either slider. In that case, the
1167 	 * device returns to event mode during low-power mode.
1168 	 */
1169 	if (iqs269_slider_type(iqs269, 0) == IQS269_SLIDER_RAW ||
1170 	    iqs269_slider_type(iqs269, 1) == IQS269_SLIDER_RAW)
1171 		general |= IQS269_SYS_SETTINGS_EVENT_MODE_LP;
1172 
1173 	general |= IQS269_SYS_SETTINGS_REDO_ATI;
1174 	general |= IQS269_SYS_SETTINGS_ACK_RESET;
1175 
1176 	sys_reg->general = cpu_to_be16(general);
1177 
1178 	return 0;
1179 }
1180 
1181 static const struct reg_sequence iqs269_tws_init[] = {
1182 	{ IQS269_TOUCH_HOLD_SLIDER_SEL, IQS269_TOUCH_HOLD_DEFAULT },
1183 	{ 0xF0, 0x580F },
1184 	{ 0xF0, 0x59EF },
1185 };
1186 
1187 static int iqs269_dev_init(struct iqs269_private *iqs269)
1188 {
1189 	int error;
1190 
1191 	guard(mutex)(&iqs269->lock);
1192 
1193 	/*
1194 	 * Early revisions of silicon require the following workaround in order
1195 	 * to restore any OTP-enabled functionality after a soft reset.
1196 	 */
1197 	if (iqs269->otp_option == IQS269_OTP_OPTION_TWS &&
1198 	    iqs269->ver_info.fw_num < IQS269_VER_INFO_FW_NUM_3) {
1199 		error = regmap_multi_reg_write(iqs269->regmap, iqs269_tws_init,
1200 					       ARRAY_SIZE(iqs269_tws_init));
1201 		if (error)
1202 			return error;
1203 	}
1204 
1205 	error = regmap_update_bits(iqs269->regmap, IQS269_HALL_UI,
1206 				   IQS269_HALL_UI_ENABLE,
1207 				   iqs269->hall_enable ? ~0 : 0);
1208 	if (error)
1209 		return error;
1210 
1211 	error = regmap_raw_write(iqs269->regmap, IQS269_SYS_SETTINGS,
1212 				 &iqs269->sys_reg, sizeof(iqs269->sys_reg));
1213 	if (error)
1214 		return error;
1215 
1216 	/*
1217 	 * The following delay gives the device time to deassert its RDY output
1218 	 * so as to prevent an interrupt from being serviced prematurely.
1219 	 */
1220 	usleep_range(2000, 2100);
1221 
1222 	iqs269->ati_current = true;
1223 
1224 	return 0;
1225 }
1226 
1227 static int iqs269_input_init(struct iqs269_private *iqs269)
1228 {
1229 	struct i2c_client *client = iqs269->client;
1230 	unsigned int sw_code, keycode;
1231 	int error, i, j;
1232 
1233 	iqs269->keypad = devm_input_allocate_device(&client->dev);
1234 	if (!iqs269->keypad)
1235 		return -ENOMEM;
1236 
1237 	iqs269->keypad->keycodemax = ARRAY_SIZE(iqs269->keycode);
1238 	iqs269->keypad->keycode = iqs269->keycode;
1239 	iqs269->keypad->keycodesize = sizeof(*iqs269->keycode);
1240 
1241 	iqs269->keypad->name = "iqs269a_keypad";
1242 	iqs269->keypad->id.bustype = BUS_I2C;
1243 
1244 	for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
1245 		sw_code = iqs269->switches[i].code;
1246 
1247 		for (j = 0; j < IQS269_NUM_CH; j++) {
1248 			keycode = iqs269->keycode[i * IQS269_NUM_CH + j];
1249 
1250 			/*
1251 			 * Hall-effect sensing repurposes a pair of dedicated
1252 			 * channels, only one of which reports events.
1253 			 */
1254 			switch (j) {
1255 			case IQS269_CHx_HALL_ACTIVE:
1256 				if (iqs269->hall_enable &&
1257 				    iqs269->switches[i].enabled)
1258 					input_set_capability(iqs269->keypad,
1259 							     EV_SW, sw_code);
1260 				fallthrough;
1261 
1262 			case IQS269_CHx_HALL_INACTIVE:
1263 				if (iqs269->hall_enable)
1264 					continue;
1265 				fallthrough;
1266 
1267 			default:
1268 				if (keycode != KEY_RESERVED)
1269 					input_set_capability(iqs269->keypad,
1270 							     EV_KEY, keycode);
1271 			}
1272 		}
1273 	}
1274 
1275 	for (i = 0; i < IQS269_NUM_SL; i++) {
1276 		if (iqs269_slider_type(iqs269, i) == IQS269_SLIDER_NONE)
1277 			continue;
1278 
1279 		iqs269->slider[i] = devm_input_allocate_device(&client->dev);
1280 		if (!iqs269->slider[i])
1281 			return -ENOMEM;
1282 
1283 		iqs269->slider[i]->keycodemax = ARRAY_SIZE(iqs269->sl_code[i]);
1284 		iqs269->slider[i]->keycode = iqs269->sl_code[i];
1285 		iqs269->slider[i]->keycodesize = sizeof(**iqs269->sl_code);
1286 
1287 		iqs269->slider[i]->name = i ? "iqs269a_slider_1"
1288 					    : "iqs269a_slider_0";
1289 		iqs269->slider[i]->id.bustype = BUS_I2C;
1290 
1291 		for (j = 0; j < IQS269_NUM_GESTURES; j++)
1292 			if (iqs269->sl_code[i][j] != KEY_RESERVED)
1293 				input_set_capability(iqs269->slider[i], EV_KEY,
1294 						     iqs269->sl_code[i][j]);
1295 
1296 		/*
1297 		 * Present the slider as a narrow trackpad if one or more chan-
1298 		 * nels have been selected to participate, but no gestures have
1299 		 * been mapped to a keycode.
1300 		 */
1301 		if (iqs269_slider_type(iqs269, i) == IQS269_SLIDER_RAW) {
1302 			input_set_capability(iqs269->slider[i],
1303 					     EV_KEY, BTN_TOUCH);
1304 			input_set_abs_params(iqs269->slider[i],
1305 					     ABS_X, 0, 255, 0, 0);
1306 		}
1307 
1308 		error = input_register_device(iqs269->slider[i]);
1309 		if (error) {
1310 			dev_err(&client->dev,
1311 				"Failed to register slider %d: %d\n", i, error);
1312 			return error;
1313 		}
1314 	}
1315 
1316 	return 0;
1317 }
1318 
1319 static int iqs269_report(struct iqs269_private *iqs269)
1320 {
1321 	struct i2c_client *client = iqs269->client;
1322 	struct iqs269_flags flags;
1323 	unsigned int sw_code, keycode;
1324 	int error, i, j;
1325 	u8 slider_x[IQS269_NUM_SL];
1326 	u8 dir_mask, state;
1327 
1328 	error = regmap_raw_read(iqs269->regmap, IQS269_SYS_FLAGS, &flags,
1329 				sizeof(flags));
1330 	if (error) {
1331 		dev_err(&client->dev, "Failed to read device status: %d\n",
1332 			error);
1333 		return error;
1334 	}
1335 
1336 	/*
1337 	 * The device resets itself if its own watchdog bites, which can happen
1338 	 * in the event of an I2C communication error. In this case, the device
1339 	 * asserts a SHOW_RESET interrupt and all registers must be restored.
1340 	 */
1341 	if (be16_to_cpu(flags.system) & IQS269_SYS_FLAGS_SHOW_RESET) {
1342 		dev_err(&client->dev, "Unexpected device reset\n");
1343 
1344 		error = iqs269_dev_init(iqs269);
1345 		if (error)
1346 			dev_err(&client->dev,
1347 				"Failed to re-initialize device: %d\n", error);
1348 
1349 		return error;
1350 	}
1351 
1352 	if (be16_to_cpu(flags.system) & IQS269_SYS_FLAGS_IN_ATI)
1353 		return 0;
1354 
1355 	if (iqs269_slider_type(iqs269, 0) == IQS269_SLIDER_RAW ||
1356 	    iqs269_slider_type(iqs269, 1) == IQS269_SLIDER_RAW) {
1357 		error = regmap_raw_read(iqs269->regmap, IQS269_SLIDER_X,
1358 					slider_x, sizeof(slider_x));
1359 		if (error) {
1360 			dev_err(&client->dev,
1361 				"Failed to read slider position: %d\n", error);
1362 			return error;
1363 		}
1364 	}
1365 
1366 	for (i = 0; i < IQS269_NUM_SL; i++) {
1367 		flags.gesture >>= (i * IQS269_NUM_GESTURES);
1368 
1369 		switch (iqs269_slider_type(iqs269, i)) {
1370 		case IQS269_SLIDER_NONE:
1371 			continue;
1372 
1373 		case IQS269_SLIDER_KEY:
1374 			for (j = 0; j < IQS269_NUM_GESTURES; j++)
1375 				input_report_key(iqs269->slider[i],
1376 						 iqs269->sl_code[i][j],
1377 						 flags.gesture & BIT(j));
1378 
1379 			if (!(flags.gesture & (BIT(IQS269_GESTURE_FLICK_NEG) |
1380 					       BIT(IQS269_GESTURE_FLICK_POS) |
1381 					       BIT(IQS269_GESTURE_TAP))))
1382 				break;
1383 
1384 			input_sync(iqs269->slider[i]);
1385 
1386 			/*
1387 			 * Momentary gestures are followed by a complementary
1388 			 * release cycle so as to emulate a full keystroke.
1389 			 */
1390 			for (j = 0; j < IQS269_NUM_GESTURES; j++)
1391 				if (j != IQS269_GESTURE_HOLD)
1392 					input_report_key(iqs269->slider[i],
1393 							 iqs269->sl_code[i][j],
1394 							 0);
1395 			break;
1396 
1397 		case IQS269_SLIDER_RAW:
1398 			/*
1399 			 * The slider is considered to be in a state of touch
1400 			 * if any selected channels are in a state of touch.
1401 			 */
1402 			state = flags.states[IQS269_ST_OFFS_TOUCH];
1403 			state &= iqs269->sys_reg.slider_select[i];
1404 
1405 			input_report_key(iqs269->slider[i], BTN_TOUCH, state);
1406 
1407 			if (state)
1408 				input_report_abs(iqs269->slider[i],
1409 						 ABS_X, slider_x[i]);
1410 			break;
1411 		}
1412 
1413 		input_sync(iqs269->slider[i]);
1414 	}
1415 
1416 	for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
1417 		dir_mask = flags.states[IQS269_ST_OFFS_DIR];
1418 		if (!iqs269_events[i].dir_up)
1419 			dir_mask = ~dir_mask;
1420 
1421 		state = flags.states[iqs269_events[i].st_offs] & dir_mask;
1422 
1423 		sw_code = iqs269->switches[i].code;
1424 
1425 		for (j = 0; j < IQS269_NUM_CH; j++) {
1426 			keycode = iqs269->keycode[i * IQS269_NUM_CH + j];
1427 
1428 			switch (j) {
1429 			case IQS269_CHx_HALL_ACTIVE:
1430 				if (iqs269->hall_enable &&
1431 				    iqs269->switches[i].enabled)
1432 					input_report_switch(iqs269->keypad,
1433 							    sw_code,
1434 							    state & BIT(j));
1435 				fallthrough;
1436 
1437 			case IQS269_CHx_HALL_INACTIVE:
1438 				if (iqs269->hall_enable)
1439 					continue;
1440 				fallthrough;
1441 
1442 			default:
1443 				input_report_key(iqs269->keypad, keycode,
1444 						 state & BIT(j));
1445 			}
1446 		}
1447 	}
1448 
1449 	input_sync(iqs269->keypad);
1450 
1451 	/*
1452 	 * The following completion signals that ATI has finished, any initial
1453 	 * switch states have been reported and the keypad can be registered.
1454 	 */
1455 	complete_all(&iqs269->ati_done);
1456 
1457 	return 0;
1458 }
1459 
1460 static irqreturn_t iqs269_irq(int irq, void *context)
1461 {
1462 	struct iqs269_private *iqs269 = context;
1463 
1464 	if (iqs269_report(iqs269))
1465 		return IRQ_NONE;
1466 
1467 	/*
1468 	 * The device does not deassert its interrupt (RDY) pin until shortly
1469 	 * after receiving an I2C stop condition; the following delay ensures
1470 	 * the interrupt handler does not return before this time.
1471 	 */
1472 	iqs269_irq_wait();
1473 
1474 	return IRQ_HANDLED;
1475 }
1476 
1477 static ssize_t counts_show(struct device *dev,
1478 			   struct device_attribute *attr, char *buf)
1479 {
1480 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1481 	struct i2c_client *client = iqs269->client;
1482 	__le16 counts;
1483 	int error;
1484 
1485 	if (!iqs269->ati_current || iqs269->hall_enable)
1486 		return -EPERM;
1487 
1488 	if (!completion_done(&iqs269->ati_done))
1489 		return -EBUSY;
1490 
1491 	/*
1492 	 * Unsolicited I2C communication prompts the device to assert its RDY
1493 	 * pin, so disable the interrupt line until the operation is finished
1494 	 * and RDY has been deasserted.
1495 	 */
1496 	disable_irq(client->irq);
1497 
1498 	error = regmap_raw_read(iqs269->regmap,
1499 				IQS269_CHx_COUNTS + iqs269->ch_num * 2,
1500 				&counts, sizeof(counts));
1501 
1502 	iqs269_irq_wait();
1503 	enable_irq(client->irq);
1504 
1505 	if (error)
1506 		return error;
1507 
1508 	return sysfs_emit(buf, "%u\n", le16_to_cpu(counts));
1509 }
1510 
1511 static ssize_t hall_bin_show(struct device *dev,
1512 			     struct device_attribute *attr, char *buf)
1513 {
1514 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1515 	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
1516 	struct i2c_client *client = iqs269->client;
1517 	unsigned int val;
1518 	int error;
1519 
1520 	disable_irq(client->irq);
1521 
1522 	error = regmap_read(iqs269->regmap, IQS269_CAL_DATA_A, &val);
1523 
1524 	iqs269_irq_wait();
1525 	enable_irq(client->irq);
1526 
1527 	if (error)
1528 		return error;
1529 
1530 	switch (ch_reg[IQS269_CHx_HALL_ACTIVE].rx_enable &
1531 		ch_reg[IQS269_CHx_HALL_INACTIVE].rx_enable) {
1532 	case IQS269_HALL_PAD_R:
1533 		val &= IQS269_CAL_DATA_A_HALL_BIN_R_MASK;
1534 		val >>= IQS269_CAL_DATA_A_HALL_BIN_R_SHIFT;
1535 		break;
1536 
1537 	case IQS269_HALL_PAD_L:
1538 		val &= IQS269_CAL_DATA_A_HALL_BIN_L_MASK;
1539 		val >>= IQS269_CAL_DATA_A_HALL_BIN_L_SHIFT;
1540 		break;
1541 
1542 	default:
1543 		return -EINVAL;
1544 	}
1545 
1546 	return sysfs_emit(buf, "%u\n", val);
1547 }
1548 
1549 static ssize_t hall_enable_show(struct device *dev,
1550 				struct device_attribute *attr, char *buf)
1551 {
1552 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1553 
1554 	return sysfs_emit(buf, "%u\n", iqs269->hall_enable);
1555 }
1556 
1557 static ssize_t hall_enable_store(struct device *dev,
1558 				 struct device_attribute *attr, const char *buf,
1559 				 size_t count)
1560 {
1561 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1562 	unsigned int val;
1563 	int error;
1564 
1565 	error = kstrtouint(buf, 10, &val);
1566 	if (error)
1567 		return error;
1568 
1569 	guard(mutex)(&iqs269->lock);
1570 
1571 	iqs269->hall_enable = val;
1572 	iqs269->ati_current = false;
1573 
1574 	return count;
1575 }
1576 
1577 static ssize_t ch_number_show(struct device *dev,
1578 			      struct device_attribute *attr, char *buf)
1579 {
1580 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1581 
1582 	return sysfs_emit(buf, "%u\n", iqs269->ch_num);
1583 }
1584 
1585 static ssize_t ch_number_store(struct device *dev,
1586 			       struct device_attribute *attr, const char *buf,
1587 			       size_t count)
1588 {
1589 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1590 	unsigned int val;
1591 	int error;
1592 
1593 	error = kstrtouint(buf, 10, &val);
1594 	if (error)
1595 		return error;
1596 
1597 	if (val >= IQS269_NUM_CH)
1598 		return -EINVAL;
1599 
1600 	iqs269->ch_num = val;
1601 
1602 	return count;
1603 }
1604 
1605 static ssize_t rx_enable_show(struct device *dev,
1606 			      struct device_attribute *attr, char *buf)
1607 {
1608 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1609 	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
1610 
1611 	return sysfs_emit(buf, "%u\n", ch_reg[iqs269->ch_num].rx_enable);
1612 }
1613 
1614 static ssize_t rx_enable_store(struct device *dev,
1615 			       struct device_attribute *attr, const char *buf,
1616 			       size_t count)
1617 {
1618 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1619 	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
1620 	unsigned int val;
1621 	int error;
1622 
1623 	error = kstrtouint(buf, 10, &val);
1624 	if (error)
1625 		return error;
1626 
1627 	if (val > 0xFF)
1628 		return -EINVAL;
1629 
1630 	guard(mutex)(&iqs269->lock);
1631 
1632 	ch_reg[iqs269->ch_num].rx_enable = val;
1633 	iqs269->ati_current = false;
1634 
1635 	return count;
1636 }
1637 
1638 static ssize_t ati_mode_show(struct device *dev,
1639 			     struct device_attribute *attr, char *buf)
1640 {
1641 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1642 	unsigned int val;
1643 	int error;
1644 
1645 	error = iqs269_ati_mode_get(iqs269, iqs269->ch_num, &val);
1646 	if (error)
1647 		return error;
1648 
1649 	return sysfs_emit(buf, "%u\n", val);
1650 }
1651 
1652 static ssize_t ati_mode_store(struct device *dev,
1653 			      struct device_attribute *attr, const char *buf,
1654 			      size_t count)
1655 {
1656 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1657 	unsigned int val;
1658 	int error;
1659 
1660 	error = kstrtouint(buf, 10, &val);
1661 	if (error)
1662 		return error;
1663 
1664 	error = iqs269_ati_mode_set(iqs269, iqs269->ch_num, val);
1665 	if (error)
1666 		return error;
1667 
1668 	return count;
1669 }
1670 
1671 static ssize_t ati_base_show(struct device *dev,
1672 			     struct device_attribute *attr, char *buf)
1673 {
1674 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1675 	unsigned int val;
1676 	int error;
1677 
1678 	error = iqs269_ati_base_get(iqs269, iqs269->ch_num, &val);
1679 	if (error)
1680 		return error;
1681 
1682 	return sysfs_emit(buf, "%u\n", val);
1683 }
1684 
1685 static ssize_t ati_base_store(struct device *dev,
1686 			      struct device_attribute *attr, const char *buf,
1687 			      size_t count)
1688 {
1689 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1690 	unsigned int val;
1691 	int error;
1692 
1693 	error = kstrtouint(buf, 10, &val);
1694 	if (error)
1695 		return error;
1696 
1697 	error = iqs269_ati_base_set(iqs269, iqs269->ch_num, val);
1698 	if (error)
1699 		return error;
1700 
1701 	return count;
1702 }
1703 
1704 static ssize_t ati_target_show(struct device *dev,
1705 			       struct device_attribute *attr, char *buf)
1706 {
1707 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1708 	unsigned int val;
1709 	int error;
1710 
1711 	error = iqs269_ati_target_get(iqs269, iqs269->ch_num, &val);
1712 	if (error)
1713 		return error;
1714 
1715 	return sysfs_emit(buf, "%u\n", val);
1716 }
1717 
1718 static ssize_t ati_target_store(struct device *dev,
1719 				struct device_attribute *attr, const char *buf,
1720 				size_t count)
1721 {
1722 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1723 	unsigned int val;
1724 	int error;
1725 
1726 	error = kstrtouint(buf, 10, &val);
1727 	if (error)
1728 		return error;
1729 
1730 	error = iqs269_ati_target_set(iqs269, iqs269->ch_num, val);
1731 	if (error)
1732 		return error;
1733 
1734 	return count;
1735 }
1736 
1737 static ssize_t ati_trigger_show(struct device *dev,
1738 				struct device_attribute *attr, char *buf)
1739 {
1740 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1741 
1742 	return sysfs_emit(buf, "%u\n",
1743 			  iqs269->ati_current &&
1744 			  completion_done(&iqs269->ati_done));
1745 }
1746 
1747 static ssize_t ati_trigger_store(struct device *dev,
1748 				 struct device_attribute *attr, const char *buf,
1749 				 size_t count)
1750 {
1751 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1752 	struct i2c_client *client = iqs269->client;
1753 	unsigned int val;
1754 	int error;
1755 
1756 	error = kstrtouint(buf, 10, &val);
1757 	if (error)
1758 		return error;
1759 
1760 	if (!val)
1761 		return count;
1762 
1763 	disable_irq(client->irq);
1764 	reinit_completion(&iqs269->ati_done);
1765 
1766 	error = iqs269_dev_init(iqs269);
1767 
1768 	iqs269_irq_wait();
1769 	enable_irq(client->irq);
1770 
1771 	if (error)
1772 		return error;
1773 
1774 	if (!wait_for_completion_timeout(&iqs269->ati_done,
1775 					 msecs_to_jiffies(2000)))
1776 		return -ETIMEDOUT;
1777 
1778 	return count;
1779 }
1780 
1781 static DEVICE_ATTR_RO(counts);
1782 static DEVICE_ATTR_RO(hall_bin);
1783 static DEVICE_ATTR_RW(hall_enable);
1784 static DEVICE_ATTR_RW(ch_number);
1785 static DEVICE_ATTR_RW(rx_enable);
1786 static DEVICE_ATTR_RW(ati_mode);
1787 static DEVICE_ATTR_RW(ati_base);
1788 static DEVICE_ATTR_RW(ati_target);
1789 static DEVICE_ATTR_RW(ati_trigger);
1790 
1791 static struct attribute *iqs269_attrs[] = {
1792 	&dev_attr_counts.attr,
1793 	&dev_attr_hall_bin.attr,
1794 	&dev_attr_hall_enable.attr,
1795 	&dev_attr_ch_number.attr,
1796 	&dev_attr_rx_enable.attr,
1797 	&dev_attr_ati_mode.attr,
1798 	&dev_attr_ati_base.attr,
1799 	&dev_attr_ati_target.attr,
1800 	&dev_attr_ati_trigger.attr,
1801 	NULL,
1802 };
1803 ATTRIBUTE_GROUPS(iqs269);
1804 
1805 static const struct regmap_config iqs269_regmap_config = {
1806 	.reg_bits = 8,
1807 	.val_bits = 16,
1808 	.max_register = IQS269_MAX_REG,
1809 };
1810 
1811 static int iqs269_probe(struct i2c_client *client)
1812 {
1813 	struct iqs269_private *iqs269;
1814 	int error;
1815 
1816 	iqs269 = devm_kzalloc(&client->dev, sizeof(*iqs269), GFP_KERNEL);
1817 	if (!iqs269)
1818 		return -ENOMEM;
1819 
1820 	i2c_set_clientdata(client, iqs269);
1821 	iqs269->client = client;
1822 
1823 	iqs269->regmap = devm_regmap_init_i2c(client, &iqs269_regmap_config);
1824 	if (IS_ERR(iqs269->regmap)) {
1825 		error = PTR_ERR(iqs269->regmap);
1826 		dev_err(&client->dev, "Failed to initialize register map: %d\n",
1827 			error);
1828 		return error;
1829 	}
1830 
1831 	mutex_init(&iqs269->lock);
1832 	init_completion(&iqs269->ati_done);
1833 
1834 	iqs269->otp_option = (uintptr_t)device_get_match_data(&client->dev);
1835 
1836 	error = regmap_raw_read(iqs269->regmap, IQS269_VER_INFO,
1837 				&iqs269->ver_info, sizeof(iqs269->ver_info));
1838 	if (error)
1839 		return error;
1840 
1841 	if (iqs269->ver_info.prod_num != IQS269_VER_INFO_PROD_NUM) {
1842 		dev_err(&client->dev, "Unrecognized product number: 0x%02X\n",
1843 			iqs269->ver_info.prod_num);
1844 		return -EINVAL;
1845 	}
1846 
1847 	error = iqs269_parse_prop(iqs269);
1848 	if (error)
1849 		return error;
1850 
1851 	error = iqs269_dev_init(iqs269);
1852 	if (error) {
1853 		dev_err(&client->dev, "Failed to initialize device: %d\n",
1854 			error);
1855 		return error;
1856 	}
1857 
1858 	error = iqs269_input_init(iqs269);
1859 	if (error)
1860 		return error;
1861 
1862 	error = devm_request_threaded_irq(&client->dev, client->irq,
1863 					  NULL, iqs269_irq, IRQF_ONESHOT,
1864 					  client->name, iqs269);
1865 	if (error) {
1866 		dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
1867 		return error;
1868 	}
1869 
1870 	if (!wait_for_completion_timeout(&iqs269->ati_done,
1871 					 msecs_to_jiffies(2000))) {
1872 		dev_err(&client->dev, "Failed to complete ATI\n");
1873 		return -ETIMEDOUT;
1874 	}
1875 
1876 	/*
1877 	 * The keypad may include one or more switches and is not registered
1878 	 * until ATI is complete and the initial switch states are read.
1879 	 */
1880 	error = input_register_device(iqs269->keypad);
1881 	if (error) {
1882 		dev_err(&client->dev, "Failed to register keypad: %d\n", error);
1883 		return error;
1884 	}
1885 
1886 	return error;
1887 }
1888 
1889 static u16 iqs269_general_get(struct iqs269_private *iqs269)
1890 {
1891 	u16 general = be16_to_cpu(iqs269->sys_reg.general);
1892 
1893 	general &= ~IQS269_SYS_SETTINGS_REDO_ATI;
1894 	general &= ~IQS269_SYS_SETTINGS_ACK_RESET;
1895 
1896 	return general | IQS269_SYS_SETTINGS_DIS_AUTO;
1897 }
1898 
1899 static int iqs269_suspend(struct device *dev)
1900 {
1901 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1902 	struct i2c_client *client = iqs269->client;
1903 	int error;
1904 	u16 general = iqs269_general_get(iqs269);
1905 
1906 	if (!(general & IQS269_SYS_SETTINGS_PWR_MODE_MASK))
1907 		return 0;
1908 
1909 	disable_irq(client->irq);
1910 
1911 	error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS, general);
1912 
1913 	iqs269_irq_wait();
1914 	enable_irq(client->irq);
1915 
1916 	return error;
1917 }
1918 
1919 static int iqs269_resume(struct device *dev)
1920 {
1921 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1922 	struct i2c_client *client = iqs269->client;
1923 	int error;
1924 	u16 general = iqs269_general_get(iqs269);
1925 
1926 	if (!(general & IQS269_SYS_SETTINGS_PWR_MODE_MASK))
1927 		return 0;
1928 
1929 	disable_irq(client->irq);
1930 
1931 	error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS,
1932 			     general & ~IQS269_SYS_SETTINGS_PWR_MODE_MASK);
1933 	if (!error)
1934 		error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS,
1935 				     general & ~IQS269_SYS_SETTINGS_DIS_AUTO);
1936 
1937 	iqs269_irq_wait();
1938 	enable_irq(client->irq);
1939 
1940 	return error;
1941 }
1942 
1943 static DEFINE_SIMPLE_DEV_PM_OPS(iqs269_pm, iqs269_suspend, iqs269_resume);
1944 
1945 static const struct of_device_id iqs269_of_match[] = {
1946 	{
1947 		.compatible = "azoteq,iqs269a",
1948 		.data = (void *)IQS269_OTP_OPTION_DEFAULT,
1949 	},
1950 	{
1951 		.compatible = "azoteq,iqs269a-00",
1952 		.data = (void *)IQS269_OTP_OPTION_DEFAULT,
1953 	},
1954 	{
1955 		.compatible = "azoteq,iqs269a-d0",
1956 		.data = (void *)IQS269_OTP_OPTION_TWS,
1957 	},
1958 	{ }
1959 };
1960 MODULE_DEVICE_TABLE(of, iqs269_of_match);
1961 
1962 static struct i2c_driver iqs269_i2c_driver = {
1963 	.driver = {
1964 		.name = "iqs269a",
1965 		.dev_groups = iqs269_groups,
1966 		.of_match_table = iqs269_of_match,
1967 		.pm = pm_sleep_ptr(&iqs269_pm),
1968 	},
1969 	.probe = iqs269_probe,
1970 };
1971 module_i2c_driver(iqs269_i2c_driver);
1972 
1973 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
1974 MODULE_DESCRIPTION("Azoteq IQS269A Capacitive Touch Controller");
1975 MODULE_LICENSE("GPL");
1976