xref: /openbmc/linux/drivers/input/misc/iqs7222.c (revision 95215d3d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Azoteq IQS7222A/B/C Capacitive Touch Controller
4  *
5  * Copyright (C) 2022 Jeff LaBundy <jeff@labundy.com>
6  */
7 
8 #include <linux/bits.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/input.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/ktime.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/property.h>
21 #include <linux/slab.h>
22 #include <asm/unaligned.h>
23 
24 #define IQS7222_PROD_NUM			0x00
25 #define IQS7222_PROD_NUM_A			840
26 #define IQS7222_PROD_NUM_B			698
27 #define IQS7222_PROD_NUM_C			863
28 
29 #define IQS7222_SYS_STATUS			0x10
30 #define IQS7222_SYS_STATUS_RESET		BIT(3)
31 #define IQS7222_SYS_STATUS_ATI_ERROR		BIT(1)
32 #define IQS7222_SYS_STATUS_ATI_ACTIVE		BIT(0)
33 
34 #define IQS7222_CHAN_SETUP_0_REF_MODE_MASK	GENMASK(15, 14)
35 #define IQS7222_CHAN_SETUP_0_REF_MODE_FOLLOW	BIT(15)
36 #define IQS7222_CHAN_SETUP_0_REF_MODE_REF	BIT(14)
37 #define IQS7222_CHAN_SETUP_0_CHAN_EN		BIT(8)
38 
39 #define IQS7222_SLDR_SETUP_0_CHAN_CNT_MASK	GENMASK(2, 0)
40 #define IQS7222_SLDR_SETUP_2_RES_MASK		GENMASK(15, 8)
41 #define IQS7222_SLDR_SETUP_2_RES_SHIFT		8
42 #define IQS7222_SLDR_SETUP_2_TOP_SPEED_MASK	GENMASK(7, 0)
43 
44 #define IQS7222_GPIO_SETUP_0_GPIO_EN		BIT(0)
45 
46 #define IQS7222_SYS_SETUP			0xD0
47 #define IQS7222_SYS_SETUP_INTF_MODE_MASK	GENMASK(7, 6)
48 #define IQS7222_SYS_SETUP_INTF_MODE_TOUCH	BIT(7)
49 #define IQS7222_SYS_SETUP_INTF_MODE_EVENT	BIT(6)
50 #define IQS7222_SYS_SETUP_PWR_MODE_MASK		GENMASK(5, 4)
51 #define IQS7222_SYS_SETUP_PWR_MODE_AUTO		IQS7222_SYS_SETUP_PWR_MODE_MASK
52 #define IQS7222_SYS_SETUP_REDO_ATI		BIT(2)
53 #define IQS7222_SYS_SETUP_ACK_RESET		BIT(0)
54 
55 #define IQS7222_EVENT_MASK_ATI			BIT(12)
56 #define IQS7222_EVENT_MASK_SLDR			BIT(10)
57 #define IQS7222_EVENT_MASK_TOUCH		BIT(1)
58 #define IQS7222_EVENT_MASK_PROX			BIT(0)
59 
60 #define IQS7222_COMMS_HOLD			BIT(0)
61 #define IQS7222_COMMS_ERROR			0xEEEE
62 #define IQS7222_COMMS_RETRY_MS			50
63 #define IQS7222_COMMS_TIMEOUT_MS		100
64 #define IQS7222_RESET_TIMEOUT_MS		250
65 #define IQS7222_ATI_TIMEOUT_MS			2000
66 
67 #define IQS7222_MAX_COLS_STAT			8
68 #define IQS7222_MAX_COLS_CYCLE			3
69 #define IQS7222_MAX_COLS_GLBL			3
70 #define IQS7222_MAX_COLS_BTN			3
71 #define IQS7222_MAX_COLS_CHAN			6
72 #define IQS7222_MAX_COLS_FILT			2
73 #define IQS7222_MAX_COLS_SLDR			11
74 #define IQS7222_MAX_COLS_GPIO			3
75 #define IQS7222_MAX_COLS_SYS			13
76 
77 #define IQS7222_MAX_CHAN			20
78 #define IQS7222_MAX_SLDR			2
79 
80 #define IQS7222_NUM_RETRIES			5
81 #define IQS7222_REG_OFFSET			0x100
82 
83 enum iqs7222_reg_key_id {
84 	IQS7222_REG_KEY_NONE,
85 	IQS7222_REG_KEY_PROX,
86 	IQS7222_REG_KEY_TOUCH,
87 	IQS7222_REG_KEY_DEBOUNCE,
88 	IQS7222_REG_KEY_TAP,
89 	IQS7222_REG_KEY_AXIAL,
90 	IQS7222_REG_KEY_WHEEL,
91 	IQS7222_REG_KEY_NO_WHEEL,
92 	IQS7222_REG_KEY_RESERVED
93 };
94 
95 enum iqs7222_reg_grp_id {
96 	IQS7222_REG_GRP_STAT,
97 	IQS7222_REG_GRP_CYCLE,
98 	IQS7222_REG_GRP_GLBL,
99 	IQS7222_REG_GRP_BTN,
100 	IQS7222_REG_GRP_CHAN,
101 	IQS7222_REG_GRP_FILT,
102 	IQS7222_REG_GRP_SLDR,
103 	IQS7222_REG_GRP_GPIO,
104 	IQS7222_REG_GRP_SYS,
105 	IQS7222_NUM_REG_GRPS
106 };
107 
108 static const char * const iqs7222_reg_grp_names[] = {
109 	[IQS7222_REG_GRP_CYCLE] = "cycle",
110 	[IQS7222_REG_GRP_CHAN] = "channel",
111 	[IQS7222_REG_GRP_SLDR] = "slider",
112 	[IQS7222_REG_GRP_GPIO] = "gpio",
113 };
114 
115 static const unsigned int iqs7222_max_cols[] = {
116 	[IQS7222_REG_GRP_STAT] = IQS7222_MAX_COLS_STAT,
117 	[IQS7222_REG_GRP_CYCLE] = IQS7222_MAX_COLS_CYCLE,
118 	[IQS7222_REG_GRP_GLBL] = IQS7222_MAX_COLS_GLBL,
119 	[IQS7222_REG_GRP_BTN] = IQS7222_MAX_COLS_BTN,
120 	[IQS7222_REG_GRP_CHAN] = IQS7222_MAX_COLS_CHAN,
121 	[IQS7222_REG_GRP_FILT] = IQS7222_MAX_COLS_FILT,
122 	[IQS7222_REG_GRP_SLDR] = IQS7222_MAX_COLS_SLDR,
123 	[IQS7222_REG_GRP_GPIO] = IQS7222_MAX_COLS_GPIO,
124 	[IQS7222_REG_GRP_SYS] = IQS7222_MAX_COLS_SYS,
125 };
126 
127 static const unsigned int iqs7222_gpio_links[] = { 2, 5, 6, };
128 
129 struct iqs7222_event_desc {
130 	const char *name;
131 	u16 mask;
132 	u16 val;
133 	u16 enable;
134 	enum iqs7222_reg_key_id reg_key;
135 };
136 
137 static const struct iqs7222_event_desc iqs7222_kp_events[] = {
138 	{
139 		.name = "event-prox",
140 		.enable = IQS7222_EVENT_MASK_PROX,
141 		.reg_key = IQS7222_REG_KEY_PROX,
142 	},
143 	{
144 		.name = "event-touch",
145 		.enable = IQS7222_EVENT_MASK_TOUCH,
146 		.reg_key = IQS7222_REG_KEY_TOUCH,
147 	},
148 };
149 
150 static const struct iqs7222_event_desc iqs7222_sl_events[] = {
151 	{ .name = "event-press", },
152 	{
153 		.name = "event-tap",
154 		.mask = BIT(0),
155 		.val = BIT(0),
156 		.enable = BIT(0),
157 		.reg_key = IQS7222_REG_KEY_TAP,
158 	},
159 	{
160 		.name = "event-swipe-pos",
161 		.mask = BIT(5) | BIT(1),
162 		.val = BIT(1),
163 		.enable = BIT(1),
164 		.reg_key = IQS7222_REG_KEY_AXIAL,
165 	},
166 	{
167 		.name = "event-swipe-neg",
168 		.mask = BIT(5) | BIT(1),
169 		.val = BIT(5) | BIT(1),
170 		.enable = BIT(1),
171 		.reg_key = IQS7222_REG_KEY_AXIAL,
172 	},
173 	{
174 		.name = "event-flick-pos",
175 		.mask = BIT(5) | BIT(2),
176 		.val = BIT(2),
177 		.enable = BIT(2),
178 		.reg_key = IQS7222_REG_KEY_AXIAL,
179 	},
180 	{
181 		.name = "event-flick-neg",
182 		.mask = BIT(5) | BIT(2),
183 		.val = BIT(5) | BIT(2),
184 		.enable = BIT(2),
185 		.reg_key = IQS7222_REG_KEY_AXIAL,
186 	},
187 };
188 
189 struct iqs7222_reg_grp_desc {
190 	u16 base;
191 	int num_row;
192 	int num_col;
193 };
194 
195 struct iqs7222_dev_desc {
196 	u16 prod_num;
197 	u16 fw_major;
198 	u16 fw_minor;
199 	u16 sldr_res;
200 	u16 touch_link;
201 	u16 wheel_enable;
202 	int allow_offset;
203 	int event_offset;
204 	int comms_offset;
205 	struct iqs7222_reg_grp_desc reg_grps[IQS7222_NUM_REG_GRPS];
206 };
207 
208 static const struct iqs7222_dev_desc iqs7222_devs[] = {
209 	{
210 		.prod_num = IQS7222_PROD_NUM_A,
211 		.fw_major = 1,
212 		.fw_minor = 12,
213 		.sldr_res = U8_MAX * 16,
214 		.touch_link = 1768,
215 		.allow_offset = 9,
216 		.event_offset = 10,
217 		.comms_offset = 12,
218 		.reg_grps = {
219 			[IQS7222_REG_GRP_STAT] = {
220 				.base = IQS7222_SYS_STATUS,
221 				.num_row = 1,
222 				.num_col = 8,
223 			},
224 			[IQS7222_REG_GRP_CYCLE] = {
225 				.base = 0x8000,
226 				.num_row = 7,
227 				.num_col = 3,
228 			},
229 			[IQS7222_REG_GRP_GLBL] = {
230 				.base = 0x8700,
231 				.num_row = 1,
232 				.num_col = 3,
233 			},
234 			[IQS7222_REG_GRP_BTN] = {
235 				.base = 0x9000,
236 				.num_row = 12,
237 				.num_col = 3,
238 			},
239 			[IQS7222_REG_GRP_CHAN] = {
240 				.base = 0xA000,
241 				.num_row = 12,
242 				.num_col = 6,
243 			},
244 			[IQS7222_REG_GRP_FILT] = {
245 				.base = 0xAC00,
246 				.num_row = 1,
247 				.num_col = 2,
248 			},
249 			[IQS7222_REG_GRP_SLDR] = {
250 				.base = 0xB000,
251 				.num_row = 2,
252 				.num_col = 11,
253 			},
254 			[IQS7222_REG_GRP_GPIO] = {
255 				.base = 0xC000,
256 				.num_row = 1,
257 				.num_col = 3,
258 			},
259 			[IQS7222_REG_GRP_SYS] = {
260 				.base = IQS7222_SYS_SETUP,
261 				.num_row = 1,
262 				.num_col = 13,
263 			},
264 		},
265 	},
266 	{
267 		.prod_num = IQS7222_PROD_NUM_B,
268 		.fw_major = 1,
269 		.fw_minor = 43,
270 		.event_offset = 10,
271 		.comms_offset = 11,
272 		.reg_grps = {
273 			[IQS7222_REG_GRP_STAT] = {
274 				.base = IQS7222_SYS_STATUS,
275 				.num_row = 1,
276 				.num_col = 6,
277 			},
278 			[IQS7222_REG_GRP_CYCLE] = {
279 				.base = 0x8000,
280 				.num_row = 10,
281 				.num_col = 2,
282 			},
283 			[IQS7222_REG_GRP_GLBL] = {
284 				.base = 0x8A00,
285 				.num_row = 1,
286 				.num_col = 3,
287 			},
288 			[IQS7222_REG_GRP_BTN] = {
289 				.base = 0x9000,
290 				.num_row = 20,
291 				.num_col = 2,
292 			},
293 			[IQS7222_REG_GRP_CHAN] = {
294 				.base = 0xB000,
295 				.num_row = 20,
296 				.num_col = 4,
297 			},
298 			[IQS7222_REG_GRP_FILT] = {
299 				.base = 0xC400,
300 				.num_row = 1,
301 				.num_col = 2,
302 			},
303 			[IQS7222_REG_GRP_SYS] = {
304 				.base = IQS7222_SYS_SETUP,
305 				.num_row = 1,
306 				.num_col = 13,
307 			},
308 		},
309 	},
310 	{
311 		.prod_num = IQS7222_PROD_NUM_B,
312 		.fw_major = 1,
313 		.fw_minor = 27,
314 		.reg_grps = {
315 			[IQS7222_REG_GRP_STAT] = {
316 				.base = IQS7222_SYS_STATUS,
317 				.num_row = 1,
318 				.num_col = 6,
319 			},
320 			[IQS7222_REG_GRP_CYCLE] = {
321 				.base = 0x8000,
322 				.num_row = 10,
323 				.num_col = 2,
324 			},
325 			[IQS7222_REG_GRP_GLBL] = {
326 				.base = 0x8A00,
327 				.num_row = 1,
328 				.num_col = 3,
329 			},
330 			[IQS7222_REG_GRP_BTN] = {
331 				.base = 0x9000,
332 				.num_row = 20,
333 				.num_col = 2,
334 			},
335 			[IQS7222_REG_GRP_CHAN] = {
336 				.base = 0xB000,
337 				.num_row = 20,
338 				.num_col = 4,
339 			},
340 			[IQS7222_REG_GRP_FILT] = {
341 				.base = 0xC400,
342 				.num_row = 1,
343 				.num_col = 2,
344 			},
345 			[IQS7222_REG_GRP_SYS] = {
346 				.base = IQS7222_SYS_SETUP,
347 				.num_row = 1,
348 				.num_col = 10,
349 			},
350 		},
351 	},
352 	{
353 		.prod_num = IQS7222_PROD_NUM_C,
354 		.fw_major = 2,
355 		.fw_minor = 6,
356 		.sldr_res = U16_MAX,
357 		.touch_link = 1686,
358 		.wheel_enable = BIT(3),
359 		.event_offset = 9,
360 		.comms_offset = 10,
361 		.reg_grps = {
362 			[IQS7222_REG_GRP_STAT] = {
363 				.base = IQS7222_SYS_STATUS,
364 				.num_row = 1,
365 				.num_col = 6,
366 			},
367 			[IQS7222_REG_GRP_CYCLE] = {
368 				.base = 0x8000,
369 				.num_row = 5,
370 				.num_col = 3,
371 			},
372 			[IQS7222_REG_GRP_GLBL] = {
373 				.base = 0x8500,
374 				.num_row = 1,
375 				.num_col = 3,
376 			},
377 			[IQS7222_REG_GRP_BTN] = {
378 				.base = 0x9000,
379 				.num_row = 10,
380 				.num_col = 3,
381 			},
382 			[IQS7222_REG_GRP_CHAN] = {
383 				.base = 0xA000,
384 				.num_row = 10,
385 				.num_col = 6,
386 			},
387 			[IQS7222_REG_GRP_FILT] = {
388 				.base = 0xAA00,
389 				.num_row = 1,
390 				.num_col = 2,
391 			},
392 			[IQS7222_REG_GRP_SLDR] = {
393 				.base = 0xB000,
394 				.num_row = 2,
395 				.num_col = 10,
396 			},
397 			[IQS7222_REG_GRP_GPIO] = {
398 				.base = 0xC000,
399 				.num_row = 3,
400 				.num_col = 3,
401 			},
402 			[IQS7222_REG_GRP_SYS] = {
403 				.base = IQS7222_SYS_SETUP,
404 				.num_row = 1,
405 				.num_col = 12,
406 			},
407 		},
408 	},
409 	{
410 		.prod_num = IQS7222_PROD_NUM_C,
411 		.fw_major = 1,
412 		.fw_minor = 13,
413 		.sldr_res = U16_MAX,
414 		.touch_link = 1674,
415 		.wheel_enable = BIT(3),
416 		.event_offset = 9,
417 		.comms_offset = 10,
418 		.reg_grps = {
419 			[IQS7222_REG_GRP_STAT] = {
420 				.base = IQS7222_SYS_STATUS,
421 				.num_row = 1,
422 				.num_col = 6,
423 			},
424 			[IQS7222_REG_GRP_CYCLE] = {
425 				.base = 0x8000,
426 				.num_row = 5,
427 				.num_col = 3,
428 			},
429 			[IQS7222_REG_GRP_GLBL] = {
430 				.base = 0x8500,
431 				.num_row = 1,
432 				.num_col = 3,
433 			},
434 			[IQS7222_REG_GRP_BTN] = {
435 				.base = 0x9000,
436 				.num_row = 10,
437 				.num_col = 3,
438 			},
439 			[IQS7222_REG_GRP_CHAN] = {
440 				.base = 0xA000,
441 				.num_row = 10,
442 				.num_col = 6,
443 			},
444 			[IQS7222_REG_GRP_FILT] = {
445 				.base = 0xAA00,
446 				.num_row = 1,
447 				.num_col = 2,
448 			},
449 			[IQS7222_REG_GRP_SLDR] = {
450 				.base = 0xB000,
451 				.num_row = 2,
452 				.num_col = 10,
453 			},
454 			[IQS7222_REG_GRP_GPIO] = {
455 				.base = 0xC000,
456 				.num_row = 1,
457 				.num_col = 3,
458 			},
459 			[IQS7222_REG_GRP_SYS] = {
460 				.base = IQS7222_SYS_SETUP,
461 				.num_row = 1,
462 				.num_col = 11,
463 			},
464 		},
465 	},
466 };
467 
468 struct iqs7222_prop_desc {
469 	const char *name;
470 	enum iqs7222_reg_grp_id reg_grp;
471 	enum iqs7222_reg_key_id reg_key;
472 	int reg_offset;
473 	int reg_shift;
474 	int reg_width;
475 	int val_pitch;
476 	int val_min;
477 	int val_max;
478 	bool invert;
479 	const char *label;
480 };
481 
482 static const struct iqs7222_prop_desc iqs7222_props[] = {
483 	{
484 		.name = "azoteq,conv-period",
485 		.reg_grp = IQS7222_REG_GRP_CYCLE,
486 		.reg_offset = 0,
487 		.reg_shift = 8,
488 		.reg_width = 8,
489 		.label = "conversion period",
490 	},
491 	{
492 		.name = "azoteq,conv-frac",
493 		.reg_grp = IQS7222_REG_GRP_CYCLE,
494 		.reg_offset = 0,
495 		.reg_shift = 0,
496 		.reg_width = 8,
497 		.label = "conversion frequency fractional divider",
498 	},
499 	{
500 		.name = "azoteq,rx-float-inactive",
501 		.reg_grp = IQS7222_REG_GRP_CYCLE,
502 		.reg_offset = 1,
503 		.reg_shift = 6,
504 		.reg_width = 1,
505 		.invert = true,
506 	},
507 	{
508 		.name = "azoteq,dead-time-enable",
509 		.reg_grp = IQS7222_REG_GRP_CYCLE,
510 		.reg_offset = 1,
511 		.reg_shift = 5,
512 		.reg_width = 1,
513 	},
514 	{
515 		.name = "azoteq,tx-freq-fosc",
516 		.reg_grp = IQS7222_REG_GRP_CYCLE,
517 		.reg_offset = 1,
518 		.reg_shift = 4,
519 		.reg_width = 1,
520 	},
521 	{
522 		.name = "azoteq,vbias-enable",
523 		.reg_grp = IQS7222_REG_GRP_CYCLE,
524 		.reg_offset = 1,
525 		.reg_shift = 3,
526 		.reg_width = 1,
527 	},
528 	{
529 		.name = "azoteq,sense-mode",
530 		.reg_grp = IQS7222_REG_GRP_CYCLE,
531 		.reg_offset = 1,
532 		.reg_shift = 0,
533 		.reg_width = 3,
534 		.val_max = 3,
535 		.label = "sensing mode",
536 	},
537 	{
538 		.name = "azoteq,iref-enable",
539 		.reg_grp = IQS7222_REG_GRP_CYCLE,
540 		.reg_offset = 2,
541 		.reg_shift = 10,
542 		.reg_width = 1,
543 	},
544 	{
545 		.name = "azoteq,iref-level",
546 		.reg_grp = IQS7222_REG_GRP_CYCLE,
547 		.reg_offset = 2,
548 		.reg_shift = 4,
549 		.reg_width = 4,
550 		.label = "current reference level",
551 	},
552 	{
553 		.name = "azoteq,iref-trim",
554 		.reg_grp = IQS7222_REG_GRP_CYCLE,
555 		.reg_offset = 2,
556 		.reg_shift = 0,
557 		.reg_width = 4,
558 		.label = "current reference trim",
559 	},
560 	{
561 		.name = "azoteq,rf-filt-enable",
562 		.reg_grp = IQS7222_REG_GRP_GLBL,
563 		.reg_offset = 0,
564 		.reg_shift = 15,
565 		.reg_width = 1,
566 	},
567 	{
568 		.name = "azoteq,max-counts",
569 		.reg_grp = IQS7222_REG_GRP_GLBL,
570 		.reg_offset = 0,
571 		.reg_shift = 13,
572 		.reg_width = 2,
573 		.label = "maximum counts",
574 	},
575 	{
576 		.name = "azoteq,auto-mode",
577 		.reg_grp = IQS7222_REG_GRP_GLBL,
578 		.reg_offset = 0,
579 		.reg_shift = 2,
580 		.reg_width = 2,
581 		.label = "number of conversions",
582 	},
583 	{
584 		.name = "azoteq,ati-frac-div-fine",
585 		.reg_grp = IQS7222_REG_GRP_GLBL,
586 		.reg_offset = 1,
587 		.reg_shift = 9,
588 		.reg_width = 5,
589 		.label = "ATI fine fractional divider",
590 	},
591 	{
592 		.name = "azoteq,ati-frac-div-coarse",
593 		.reg_grp = IQS7222_REG_GRP_GLBL,
594 		.reg_offset = 1,
595 		.reg_shift = 0,
596 		.reg_width = 5,
597 		.label = "ATI coarse fractional divider",
598 	},
599 	{
600 		.name = "azoteq,ati-comp-select",
601 		.reg_grp = IQS7222_REG_GRP_GLBL,
602 		.reg_offset = 2,
603 		.reg_shift = 0,
604 		.reg_width = 10,
605 		.label = "ATI compensation selection",
606 	},
607 	{
608 		.name = "azoteq,ati-band",
609 		.reg_grp = IQS7222_REG_GRP_CHAN,
610 		.reg_offset = 0,
611 		.reg_shift = 12,
612 		.reg_width = 2,
613 		.label = "ATI band",
614 	},
615 	{
616 		.name = "azoteq,global-halt",
617 		.reg_grp = IQS7222_REG_GRP_CHAN,
618 		.reg_offset = 0,
619 		.reg_shift = 11,
620 		.reg_width = 1,
621 	},
622 	{
623 		.name = "azoteq,invert-enable",
624 		.reg_grp = IQS7222_REG_GRP_CHAN,
625 		.reg_offset = 0,
626 		.reg_shift = 10,
627 		.reg_width = 1,
628 	},
629 	{
630 		.name = "azoteq,dual-direction",
631 		.reg_grp = IQS7222_REG_GRP_CHAN,
632 		.reg_offset = 0,
633 		.reg_shift = 9,
634 		.reg_width = 1,
635 	},
636 	{
637 		.name = "azoteq,samp-cap-double",
638 		.reg_grp = IQS7222_REG_GRP_CHAN,
639 		.reg_offset = 0,
640 		.reg_shift = 3,
641 		.reg_width = 1,
642 	},
643 	{
644 		.name = "azoteq,vref-half",
645 		.reg_grp = IQS7222_REG_GRP_CHAN,
646 		.reg_offset = 0,
647 		.reg_shift = 2,
648 		.reg_width = 1,
649 	},
650 	{
651 		.name = "azoteq,proj-bias",
652 		.reg_grp = IQS7222_REG_GRP_CHAN,
653 		.reg_offset = 0,
654 		.reg_shift = 0,
655 		.reg_width = 2,
656 		.label = "projected bias current",
657 	},
658 	{
659 		.name = "azoteq,ati-target",
660 		.reg_grp = IQS7222_REG_GRP_CHAN,
661 		.reg_offset = 1,
662 		.reg_shift = 8,
663 		.reg_width = 8,
664 		.val_pitch = 8,
665 		.label = "ATI target",
666 	},
667 	{
668 		.name = "azoteq,ati-base",
669 		.reg_grp = IQS7222_REG_GRP_CHAN,
670 		.reg_offset = 1,
671 		.reg_shift = 3,
672 		.reg_width = 5,
673 		.val_pitch = 16,
674 		.label = "ATI base",
675 	},
676 	{
677 		.name = "azoteq,ati-mode",
678 		.reg_grp = IQS7222_REG_GRP_CHAN,
679 		.reg_offset = 1,
680 		.reg_shift = 0,
681 		.reg_width = 3,
682 		.val_max = 5,
683 		.label = "ATI mode",
684 	},
685 	{
686 		.name = "azoteq,ati-frac-div-fine",
687 		.reg_grp = IQS7222_REG_GRP_CHAN,
688 		.reg_offset = 2,
689 		.reg_shift = 9,
690 		.reg_width = 5,
691 		.label = "ATI fine fractional divider",
692 	},
693 	{
694 		.name = "azoteq,ati-frac-mult-coarse",
695 		.reg_grp = IQS7222_REG_GRP_CHAN,
696 		.reg_offset = 2,
697 		.reg_shift = 5,
698 		.reg_width = 4,
699 		.label = "ATI coarse fractional multiplier",
700 	},
701 	{
702 		.name = "azoteq,ati-frac-div-coarse",
703 		.reg_grp = IQS7222_REG_GRP_CHAN,
704 		.reg_offset = 2,
705 		.reg_shift = 0,
706 		.reg_width = 5,
707 		.label = "ATI coarse fractional divider",
708 	},
709 	{
710 		.name = "azoteq,ati-comp-div",
711 		.reg_grp = IQS7222_REG_GRP_CHAN,
712 		.reg_offset = 3,
713 		.reg_shift = 11,
714 		.reg_width = 5,
715 		.label = "ATI compensation divider",
716 	},
717 	{
718 		.name = "azoteq,ati-comp-select",
719 		.reg_grp = IQS7222_REG_GRP_CHAN,
720 		.reg_offset = 3,
721 		.reg_shift = 0,
722 		.reg_width = 10,
723 		.label = "ATI compensation selection",
724 	},
725 	{
726 		.name = "azoteq,debounce-exit",
727 		.reg_grp = IQS7222_REG_GRP_BTN,
728 		.reg_key = IQS7222_REG_KEY_DEBOUNCE,
729 		.reg_offset = 0,
730 		.reg_shift = 12,
731 		.reg_width = 4,
732 		.label = "debounce exit factor",
733 	},
734 	{
735 		.name = "azoteq,debounce-enter",
736 		.reg_grp = IQS7222_REG_GRP_BTN,
737 		.reg_key = IQS7222_REG_KEY_DEBOUNCE,
738 		.reg_offset = 0,
739 		.reg_shift = 8,
740 		.reg_width = 4,
741 		.label = "debounce entrance factor",
742 	},
743 	{
744 		.name = "azoteq,thresh",
745 		.reg_grp = IQS7222_REG_GRP_BTN,
746 		.reg_key = IQS7222_REG_KEY_PROX,
747 		.reg_offset = 0,
748 		.reg_shift = 0,
749 		.reg_width = 8,
750 		.val_max = 127,
751 		.label = "threshold",
752 	},
753 	{
754 		.name = "azoteq,thresh",
755 		.reg_grp = IQS7222_REG_GRP_BTN,
756 		.reg_key = IQS7222_REG_KEY_TOUCH,
757 		.reg_offset = 1,
758 		.reg_shift = 0,
759 		.reg_width = 8,
760 		.label = "threshold",
761 	},
762 	{
763 		.name = "azoteq,hyst",
764 		.reg_grp = IQS7222_REG_GRP_BTN,
765 		.reg_key = IQS7222_REG_KEY_TOUCH,
766 		.reg_offset = 1,
767 		.reg_shift = 8,
768 		.reg_width = 8,
769 		.label = "hysteresis",
770 	},
771 	{
772 		.name = "azoteq,lta-beta-lp",
773 		.reg_grp = IQS7222_REG_GRP_FILT,
774 		.reg_offset = 0,
775 		.reg_shift = 12,
776 		.reg_width = 4,
777 		.label = "low-power mode long-term average beta",
778 	},
779 	{
780 		.name = "azoteq,lta-beta-np",
781 		.reg_grp = IQS7222_REG_GRP_FILT,
782 		.reg_offset = 0,
783 		.reg_shift = 8,
784 		.reg_width = 4,
785 		.label = "normal-power mode long-term average beta",
786 	},
787 	{
788 		.name = "azoteq,counts-beta-lp",
789 		.reg_grp = IQS7222_REG_GRP_FILT,
790 		.reg_offset = 0,
791 		.reg_shift = 4,
792 		.reg_width = 4,
793 		.label = "low-power mode counts beta",
794 	},
795 	{
796 		.name = "azoteq,counts-beta-np",
797 		.reg_grp = IQS7222_REG_GRP_FILT,
798 		.reg_offset = 0,
799 		.reg_shift = 0,
800 		.reg_width = 4,
801 		.label = "normal-power mode counts beta",
802 	},
803 	{
804 		.name = "azoteq,lta-fast-beta-lp",
805 		.reg_grp = IQS7222_REG_GRP_FILT,
806 		.reg_offset = 1,
807 		.reg_shift = 4,
808 		.reg_width = 4,
809 		.label = "low-power mode long-term average fast beta",
810 	},
811 	{
812 		.name = "azoteq,lta-fast-beta-np",
813 		.reg_grp = IQS7222_REG_GRP_FILT,
814 		.reg_offset = 1,
815 		.reg_shift = 0,
816 		.reg_width = 4,
817 		.label = "normal-power mode long-term average fast beta",
818 	},
819 	{
820 		.name = "azoteq,lower-cal",
821 		.reg_grp = IQS7222_REG_GRP_SLDR,
822 		.reg_offset = 0,
823 		.reg_shift = 8,
824 		.reg_width = 8,
825 		.label = "lower calibration",
826 	},
827 	{
828 		.name = "azoteq,static-beta",
829 		.reg_grp = IQS7222_REG_GRP_SLDR,
830 		.reg_key = IQS7222_REG_KEY_NO_WHEEL,
831 		.reg_offset = 0,
832 		.reg_shift = 6,
833 		.reg_width = 1,
834 	},
835 	{
836 		.name = "azoteq,bottom-beta",
837 		.reg_grp = IQS7222_REG_GRP_SLDR,
838 		.reg_key = IQS7222_REG_KEY_NO_WHEEL,
839 		.reg_offset = 0,
840 		.reg_shift = 3,
841 		.reg_width = 3,
842 		.label = "bottom beta",
843 	},
844 	{
845 		.name = "azoteq,static-beta",
846 		.reg_grp = IQS7222_REG_GRP_SLDR,
847 		.reg_key = IQS7222_REG_KEY_WHEEL,
848 		.reg_offset = 0,
849 		.reg_shift = 7,
850 		.reg_width = 1,
851 	},
852 	{
853 		.name = "azoteq,bottom-beta",
854 		.reg_grp = IQS7222_REG_GRP_SLDR,
855 		.reg_key = IQS7222_REG_KEY_WHEEL,
856 		.reg_offset = 0,
857 		.reg_shift = 4,
858 		.reg_width = 3,
859 		.label = "bottom beta",
860 	},
861 	{
862 		.name = "azoteq,bottom-speed",
863 		.reg_grp = IQS7222_REG_GRP_SLDR,
864 		.reg_offset = 1,
865 		.reg_shift = 8,
866 		.reg_width = 8,
867 		.label = "bottom speed",
868 	},
869 	{
870 		.name = "azoteq,upper-cal",
871 		.reg_grp = IQS7222_REG_GRP_SLDR,
872 		.reg_offset = 1,
873 		.reg_shift = 0,
874 		.reg_width = 8,
875 		.label = "upper calibration",
876 	},
877 	{
878 		.name = "azoteq,gesture-max-ms",
879 		.reg_grp = IQS7222_REG_GRP_SLDR,
880 		.reg_key = IQS7222_REG_KEY_TAP,
881 		.reg_offset = 9,
882 		.reg_shift = 8,
883 		.reg_width = 8,
884 		.val_pitch = 4,
885 		.label = "maximum gesture time",
886 	},
887 	{
888 		.name = "azoteq,gesture-min-ms",
889 		.reg_grp = IQS7222_REG_GRP_SLDR,
890 		.reg_key = IQS7222_REG_KEY_TAP,
891 		.reg_offset = 9,
892 		.reg_shift = 3,
893 		.reg_width = 5,
894 		.val_pitch = 4,
895 		.label = "minimum gesture time",
896 	},
897 	{
898 		.name = "azoteq,gesture-dist",
899 		.reg_grp = IQS7222_REG_GRP_SLDR,
900 		.reg_key = IQS7222_REG_KEY_AXIAL,
901 		.reg_offset = 10,
902 		.reg_shift = 8,
903 		.reg_width = 8,
904 		.val_pitch = 16,
905 		.label = "gesture distance",
906 	},
907 	{
908 		.name = "azoteq,gesture-max-ms",
909 		.reg_grp = IQS7222_REG_GRP_SLDR,
910 		.reg_key = IQS7222_REG_KEY_AXIAL,
911 		.reg_offset = 10,
912 		.reg_shift = 0,
913 		.reg_width = 8,
914 		.val_pitch = 4,
915 		.label = "maximum gesture time",
916 	},
917 	{
918 		.name = "drive-open-drain",
919 		.reg_grp = IQS7222_REG_GRP_GPIO,
920 		.reg_offset = 0,
921 		.reg_shift = 1,
922 		.reg_width = 1,
923 	},
924 	{
925 		.name = "azoteq,timeout-ati-ms",
926 		.reg_grp = IQS7222_REG_GRP_SYS,
927 		.reg_offset = 1,
928 		.reg_shift = 0,
929 		.reg_width = 16,
930 		.val_pitch = 500,
931 		.label = "ATI error timeout",
932 	},
933 	{
934 		.name = "azoteq,rate-ati-ms",
935 		.reg_grp = IQS7222_REG_GRP_SYS,
936 		.reg_offset = 2,
937 		.reg_shift = 0,
938 		.reg_width = 16,
939 		.label = "ATI report rate",
940 	},
941 	{
942 		.name = "azoteq,timeout-np-ms",
943 		.reg_grp = IQS7222_REG_GRP_SYS,
944 		.reg_offset = 3,
945 		.reg_shift = 0,
946 		.reg_width = 16,
947 		.label = "normal-power mode timeout",
948 	},
949 	{
950 		.name = "azoteq,rate-np-ms",
951 		.reg_grp = IQS7222_REG_GRP_SYS,
952 		.reg_offset = 4,
953 		.reg_shift = 0,
954 		.reg_width = 16,
955 		.val_max = 3000,
956 		.label = "normal-power mode report rate",
957 	},
958 	{
959 		.name = "azoteq,timeout-lp-ms",
960 		.reg_grp = IQS7222_REG_GRP_SYS,
961 		.reg_offset = 5,
962 		.reg_shift = 0,
963 		.reg_width = 16,
964 		.label = "low-power mode timeout",
965 	},
966 	{
967 		.name = "azoteq,rate-lp-ms",
968 		.reg_grp = IQS7222_REG_GRP_SYS,
969 		.reg_offset = 6,
970 		.reg_shift = 0,
971 		.reg_width = 16,
972 		.val_max = 3000,
973 		.label = "low-power mode report rate",
974 	},
975 	{
976 		.name = "azoteq,timeout-ulp-ms",
977 		.reg_grp = IQS7222_REG_GRP_SYS,
978 		.reg_offset = 7,
979 		.reg_shift = 0,
980 		.reg_width = 16,
981 		.label = "ultra-low-power mode timeout",
982 	},
983 	{
984 		.name = "azoteq,rate-ulp-ms",
985 		.reg_grp = IQS7222_REG_GRP_SYS,
986 		.reg_offset = 8,
987 		.reg_shift = 0,
988 		.reg_width = 16,
989 		.val_max = 3000,
990 		.label = "ultra-low-power mode report rate",
991 	},
992 };
993 
994 struct iqs7222_private {
995 	const struct iqs7222_dev_desc *dev_desc;
996 	struct gpio_desc *reset_gpio;
997 	struct gpio_desc *irq_gpio;
998 	struct i2c_client *client;
999 	struct input_dev *keypad;
1000 	unsigned int kp_type[IQS7222_MAX_CHAN][ARRAY_SIZE(iqs7222_kp_events)];
1001 	unsigned int kp_code[IQS7222_MAX_CHAN][ARRAY_SIZE(iqs7222_kp_events)];
1002 	unsigned int sl_code[IQS7222_MAX_SLDR][ARRAY_SIZE(iqs7222_sl_events)];
1003 	unsigned int sl_axis[IQS7222_MAX_SLDR];
1004 	u16 cycle_setup[IQS7222_MAX_CHAN / 2][IQS7222_MAX_COLS_CYCLE];
1005 	u16 glbl_setup[IQS7222_MAX_COLS_GLBL];
1006 	u16 btn_setup[IQS7222_MAX_CHAN][IQS7222_MAX_COLS_BTN];
1007 	u16 chan_setup[IQS7222_MAX_CHAN][IQS7222_MAX_COLS_CHAN];
1008 	u16 filt_setup[IQS7222_MAX_COLS_FILT];
1009 	u16 sldr_setup[IQS7222_MAX_SLDR][IQS7222_MAX_COLS_SLDR];
1010 	u16 gpio_setup[ARRAY_SIZE(iqs7222_gpio_links)][IQS7222_MAX_COLS_GPIO];
1011 	u16 sys_setup[IQS7222_MAX_COLS_SYS];
1012 };
1013 
1014 static u16 *iqs7222_setup(struct iqs7222_private *iqs7222,
1015 			  enum iqs7222_reg_grp_id reg_grp, int row)
1016 {
1017 	switch (reg_grp) {
1018 	case IQS7222_REG_GRP_CYCLE:
1019 		return iqs7222->cycle_setup[row];
1020 
1021 	case IQS7222_REG_GRP_GLBL:
1022 		return iqs7222->glbl_setup;
1023 
1024 	case IQS7222_REG_GRP_BTN:
1025 		return iqs7222->btn_setup[row];
1026 
1027 	case IQS7222_REG_GRP_CHAN:
1028 		return iqs7222->chan_setup[row];
1029 
1030 	case IQS7222_REG_GRP_FILT:
1031 		return iqs7222->filt_setup;
1032 
1033 	case IQS7222_REG_GRP_SLDR:
1034 		return iqs7222->sldr_setup[row];
1035 
1036 	case IQS7222_REG_GRP_GPIO:
1037 		return iqs7222->gpio_setup[row];
1038 
1039 	case IQS7222_REG_GRP_SYS:
1040 		return iqs7222->sys_setup;
1041 
1042 	default:
1043 		return NULL;
1044 	}
1045 }
1046 
1047 static int iqs7222_irq_poll(struct iqs7222_private *iqs7222, u16 timeout_ms)
1048 {
1049 	ktime_t irq_timeout = ktime_add_ms(ktime_get(), timeout_ms);
1050 	int ret;
1051 
1052 	do {
1053 		usleep_range(1000, 1100);
1054 
1055 		ret = gpiod_get_value_cansleep(iqs7222->irq_gpio);
1056 		if (ret < 0)
1057 			return ret;
1058 		else if (ret > 0)
1059 			return 0;
1060 	} while (ktime_compare(ktime_get(), irq_timeout) < 0);
1061 
1062 	return -EBUSY;
1063 }
1064 
1065 static int iqs7222_hard_reset(struct iqs7222_private *iqs7222)
1066 {
1067 	struct i2c_client *client = iqs7222->client;
1068 	int error;
1069 
1070 	if (!iqs7222->reset_gpio)
1071 		return 0;
1072 
1073 	gpiod_set_value_cansleep(iqs7222->reset_gpio, 1);
1074 	usleep_range(1000, 1100);
1075 
1076 	gpiod_set_value_cansleep(iqs7222->reset_gpio, 0);
1077 
1078 	error = iqs7222_irq_poll(iqs7222, IQS7222_RESET_TIMEOUT_MS);
1079 	if (error)
1080 		dev_err(&client->dev, "Failed to reset device: %d\n", error);
1081 
1082 	return error;
1083 }
1084 
1085 static int iqs7222_force_comms(struct iqs7222_private *iqs7222)
1086 {
1087 	u8 msg_buf[] = { 0xFF, 0x00, };
1088 	int ret;
1089 
1090 	/*
1091 	 * The device cannot communicate until it asserts its interrupt (RDY)
1092 	 * pin. Attempts to do so while RDY is deasserted return an ACK; how-
1093 	 * ever all write data is ignored, and all read data returns 0xEE.
1094 	 *
1095 	 * Unsolicited communication must be preceded by a special force com-
1096 	 * munication command, after which the device eventually asserts its
1097 	 * RDY pin and agrees to communicate.
1098 	 *
1099 	 * Regardless of whether communication is forced or the result of an
1100 	 * interrupt, the device automatically deasserts its RDY pin once it
1101 	 * detects an I2C stop condition, or a timeout expires.
1102 	 */
1103 	ret = gpiod_get_value_cansleep(iqs7222->irq_gpio);
1104 	if (ret < 0)
1105 		return ret;
1106 	else if (ret > 0)
1107 		return 0;
1108 
1109 	ret = i2c_master_send(iqs7222->client, msg_buf, sizeof(msg_buf));
1110 	if (ret < (int)sizeof(msg_buf)) {
1111 		if (ret >= 0)
1112 			ret = -EIO;
1113 
1114 		/*
1115 		 * The datasheet states that the host must wait to retry any
1116 		 * failed attempt to communicate over I2C.
1117 		 */
1118 		msleep(IQS7222_COMMS_RETRY_MS);
1119 		return ret;
1120 	}
1121 
1122 	return iqs7222_irq_poll(iqs7222, IQS7222_COMMS_TIMEOUT_MS);
1123 }
1124 
1125 static int iqs7222_read_burst(struct iqs7222_private *iqs7222,
1126 			      u16 reg, void *val, u16 num_val)
1127 {
1128 	u8 reg_buf[sizeof(__be16)];
1129 	int ret, i;
1130 	struct i2c_client *client = iqs7222->client;
1131 	struct i2c_msg msg[] = {
1132 		{
1133 			.addr = client->addr,
1134 			.flags = 0,
1135 			.len = reg > U8_MAX ? sizeof(reg) : sizeof(u8),
1136 			.buf = reg_buf,
1137 		},
1138 		{
1139 			.addr = client->addr,
1140 			.flags = I2C_M_RD,
1141 			.len = num_val * sizeof(__le16),
1142 			.buf = (u8 *)val,
1143 		},
1144 	};
1145 
1146 	if (reg > U8_MAX)
1147 		put_unaligned_be16(reg, reg_buf);
1148 	else
1149 		*reg_buf = (u8)reg;
1150 
1151 	/*
1152 	 * The following loop protects against an edge case in which the RDY
1153 	 * pin is automatically deasserted just as the read is initiated. In
1154 	 * that case, the read must be retried using forced communication.
1155 	 */
1156 	for (i = 0; i < IQS7222_NUM_RETRIES; i++) {
1157 		ret = iqs7222_force_comms(iqs7222);
1158 		if (ret < 0)
1159 			continue;
1160 
1161 		ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
1162 		if (ret < (int)ARRAY_SIZE(msg)) {
1163 			if (ret >= 0)
1164 				ret = -EIO;
1165 
1166 			msleep(IQS7222_COMMS_RETRY_MS);
1167 			continue;
1168 		}
1169 
1170 		if (get_unaligned_le16(msg[1].buf) == IQS7222_COMMS_ERROR) {
1171 			ret = -ENODATA;
1172 			continue;
1173 		}
1174 
1175 		ret = 0;
1176 		break;
1177 	}
1178 
1179 	/*
1180 	 * The following delay ensures the device has deasserted the RDY pin
1181 	 * following the I2C stop condition.
1182 	 */
1183 	usleep_range(50, 100);
1184 
1185 	if (ret < 0)
1186 		dev_err(&client->dev,
1187 			"Failed to read from address 0x%04X: %d\n", reg, ret);
1188 
1189 	return ret;
1190 }
1191 
1192 static int iqs7222_read_word(struct iqs7222_private *iqs7222, u16 reg, u16 *val)
1193 {
1194 	__le16 val_buf;
1195 	int error;
1196 
1197 	error = iqs7222_read_burst(iqs7222, reg, &val_buf, 1);
1198 	if (error)
1199 		return error;
1200 
1201 	*val = le16_to_cpu(val_buf);
1202 
1203 	return 0;
1204 }
1205 
1206 static int iqs7222_write_burst(struct iqs7222_private *iqs7222,
1207 			       u16 reg, const void *val, u16 num_val)
1208 {
1209 	int reg_len = reg > U8_MAX ? sizeof(reg) : sizeof(u8);
1210 	int val_len = num_val * sizeof(__le16);
1211 	int msg_len = reg_len + val_len;
1212 	int ret, i;
1213 	struct i2c_client *client = iqs7222->client;
1214 	u8 *msg_buf;
1215 
1216 	msg_buf = kzalloc(msg_len, GFP_KERNEL);
1217 	if (!msg_buf)
1218 		return -ENOMEM;
1219 
1220 	if (reg > U8_MAX)
1221 		put_unaligned_be16(reg, msg_buf);
1222 	else
1223 		*msg_buf = (u8)reg;
1224 
1225 	memcpy(msg_buf + reg_len, val, val_len);
1226 
1227 	/*
1228 	 * The following loop protects against an edge case in which the RDY
1229 	 * pin is automatically asserted just before the force communication
1230 	 * command is sent.
1231 	 *
1232 	 * In that case, the subsequent I2C stop condition tricks the device
1233 	 * into preemptively deasserting the RDY pin and the command must be
1234 	 * sent again.
1235 	 */
1236 	for (i = 0; i < IQS7222_NUM_RETRIES; i++) {
1237 		ret = iqs7222_force_comms(iqs7222);
1238 		if (ret < 0)
1239 			continue;
1240 
1241 		ret = i2c_master_send(client, msg_buf, msg_len);
1242 		if (ret < msg_len) {
1243 			if (ret >= 0)
1244 				ret = -EIO;
1245 
1246 			msleep(IQS7222_COMMS_RETRY_MS);
1247 			continue;
1248 		}
1249 
1250 		ret = 0;
1251 		break;
1252 	}
1253 
1254 	kfree(msg_buf);
1255 
1256 	usleep_range(50, 100);
1257 
1258 	if (ret < 0)
1259 		dev_err(&client->dev,
1260 			"Failed to write to address 0x%04X: %d\n", reg, ret);
1261 
1262 	return ret;
1263 }
1264 
1265 static int iqs7222_write_word(struct iqs7222_private *iqs7222, u16 reg, u16 val)
1266 {
1267 	__le16 val_buf = cpu_to_le16(val);
1268 
1269 	return iqs7222_write_burst(iqs7222, reg, &val_buf, 1);
1270 }
1271 
1272 static int iqs7222_ati_trigger(struct iqs7222_private *iqs7222)
1273 {
1274 	struct i2c_client *client = iqs7222->client;
1275 	ktime_t ati_timeout;
1276 	u16 sys_status = 0;
1277 	u16 sys_setup = iqs7222->sys_setup[0] & ~IQS7222_SYS_SETUP_ACK_RESET;
1278 	int error, i;
1279 
1280 	for (i = 0; i < IQS7222_NUM_RETRIES; i++) {
1281 		/*
1282 		 * Trigger ATI from streaming and normal-power modes so that
1283 		 * the RDY pin continues to be asserted during ATI.
1284 		 */
1285 		error = iqs7222_write_word(iqs7222, IQS7222_SYS_SETUP,
1286 					   sys_setup |
1287 					   IQS7222_SYS_SETUP_REDO_ATI);
1288 		if (error)
1289 			return error;
1290 
1291 		ati_timeout = ktime_add_ms(ktime_get(), IQS7222_ATI_TIMEOUT_MS);
1292 
1293 		do {
1294 			error = iqs7222_irq_poll(iqs7222,
1295 						 IQS7222_COMMS_TIMEOUT_MS);
1296 			if (error)
1297 				continue;
1298 
1299 			error = iqs7222_read_word(iqs7222, IQS7222_SYS_STATUS,
1300 						  &sys_status);
1301 			if (error)
1302 				return error;
1303 
1304 			if (sys_status & IQS7222_SYS_STATUS_ATI_ACTIVE)
1305 				continue;
1306 
1307 			if (sys_status & IQS7222_SYS_STATUS_ATI_ERROR)
1308 				break;
1309 
1310 			/*
1311 			 * Use stream-in-touch mode if either slider reports
1312 			 * absolute position.
1313 			 */
1314 			sys_setup |= test_bit(EV_ABS, iqs7222->keypad->evbit)
1315 				   ? IQS7222_SYS_SETUP_INTF_MODE_TOUCH
1316 				   : IQS7222_SYS_SETUP_INTF_MODE_EVENT;
1317 			sys_setup |= IQS7222_SYS_SETUP_PWR_MODE_AUTO;
1318 
1319 			return iqs7222_write_word(iqs7222, IQS7222_SYS_SETUP,
1320 						  sys_setup);
1321 		} while (ktime_compare(ktime_get(), ati_timeout) < 0);
1322 
1323 		dev_err(&client->dev,
1324 			"ATI attempt %d of %d failed with status 0x%02X, %s\n",
1325 			i + 1, IQS7222_NUM_RETRIES, (u8)sys_status,
1326 			i < IQS7222_NUM_RETRIES ? "retrying..." : "stopping");
1327 	}
1328 
1329 	return -ETIMEDOUT;
1330 }
1331 
1332 static int iqs7222_dev_init(struct iqs7222_private *iqs7222, int dir)
1333 {
1334 	const struct iqs7222_dev_desc *dev_desc = iqs7222->dev_desc;
1335 	int comms_offset = dev_desc->comms_offset;
1336 	int error, i, j, k;
1337 
1338 	/*
1339 	 * Take advantage of the stop-bit disable function, if available, to
1340 	 * save the trouble of having to reopen a communication window after
1341 	 * each burst read or write.
1342 	 */
1343 	if (comms_offset) {
1344 		u16 comms_setup;
1345 
1346 		error = iqs7222_read_word(iqs7222,
1347 					  IQS7222_SYS_SETUP + comms_offset,
1348 					  &comms_setup);
1349 		if (error)
1350 			return error;
1351 
1352 		error = iqs7222_write_word(iqs7222,
1353 					   IQS7222_SYS_SETUP + comms_offset,
1354 					   comms_setup | IQS7222_COMMS_HOLD);
1355 		if (error)
1356 			return error;
1357 	}
1358 
1359 	for (i = 0; i < IQS7222_NUM_REG_GRPS; i++) {
1360 		int num_row = dev_desc->reg_grps[i].num_row;
1361 		int num_col = dev_desc->reg_grps[i].num_col;
1362 		u16 reg = dev_desc->reg_grps[i].base;
1363 		__le16 *val_buf;
1364 		u16 *val;
1365 
1366 		if (!num_col)
1367 			continue;
1368 
1369 		val = iqs7222_setup(iqs7222, i, 0);
1370 		if (!val)
1371 			continue;
1372 
1373 		val_buf = kcalloc(num_col, sizeof(__le16), GFP_KERNEL);
1374 		if (!val_buf)
1375 			return -ENOMEM;
1376 
1377 		for (j = 0; j < num_row; j++) {
1378 			switch (dir) {
1379 			case READ:
1380 				error = iqs7222_read_burst(iqs7222, reg,
1381 							   val_buf, num_col);
1382 				for (k = 0; k < num_col; k++)
1383 					val[k] = le16_to_cpu(val_buf[k]);
1384 				break;
1385 
1386 			case WRITE:
1387 				for (k = 0; k < num_col; k++)
1388 					val_buf[k] = cpu_to_le16(val[k]);
1389 				error = iqs7222_write_burst(iqs7222, reg,
1390 							    val_buf, num_col);
1391 				break;
1392 
1393 			default:
1394 				error = -EINVAL;
1395 			}
1396 
1397 			if (error)
1398 				break;
1399 
1400 			reg += IQS7222_REG_OFFSET;
1401 			val += iqs7222_max_cols[i];
1402 		}
1403 
1404 		kfree(val_buf);
1405 
1406 		if (error)
1407 			return error;
1408 	}
1409 
1410 	if (comms_offset) {
1411 		u16 comms_setup;
1412 
1413 		error = iqs7222_read_word(iqs7222,
1414 					  IQS7222_SYS_SETUP + comms_offset,
1415 					  &comms_setup);
1416 		if (error)
1417 			return error;
1418 
1419 		error = iqs7222_write_word(iqs7222,
1420 					   IQS7222_SYS_SETUP + comms_offset,
1421 					   comms_setup & ~IQS7222_COMMS_HOLD);
1422 		if (error)
1423 			return error;
1424 	}
1425 
1426 	if (dir == READ)
1427 		return 0;
1428 
1429 	return iqs7222_ati_trigger(iqs7222);
1430 }
1431 
1432 static int iqs7222_dev_info(struct iqs7222_private *iqs7222)
1433 {
1434 	struct i2c_client *client = iqs7222->client;
1435 	bool prod_num_valid = false;
1436 	__le16 dev_id[3];
1437 	int error, i;
1438 
1439 	error = iqs7222_read_burst(iqs7222, IQS7222_PROD_NUM, dev_id,
1440 				   ARRAY_SIZE(dev_id));
1441 	if (error)
1442 		return error;
1443 
1444 	for (i = 0; i < ARRAY_SIZE(iqs7222_devs); i++) {
1445 		if (le16_to_cpu(dev_id[0]) != iqs7222_devs[i].prod_num)
1446 			continue;
1447 
1448 		prod_num_valid = true;
1449 
1450 		if (le16_to_cpu(dev_id[1]) < iqs7222_devs[i].fw_major)
1451 			continue;
1452 
1453 		if (le16_to_cpu(dev_id[2]) < iqs7222_devs[i].fw_minor)
1454 			continue;
1455 
1456 		iqs7222->dev_desc = &iqs7222_devs[i];
1457 		return 0;
1458 	}
1459 
1460 	if (prod_num_valid)
1461 		dev_err(&client->dev, "Unsupported firmware revision: %u.%u\n",
1462 			le16_to_cpu(dev_id[1]), le16_to_cpu(dev_id[2]));
1463 	else
1464 		dev_err(&client->dev, "Unrecognized product number: %u\n",
1465 			le16_to_cpu(dev_id[0]));
1466 
1467 	return -EINVAL;
1468 }
1469 
1470 static int iqs7222_gpio_select(struct iqs7222_private *iqs7222,
1471 			       struct fwnode_handle *child_node,
1472 			       int child_enable, u16 child_link)
1473 {
1474 	const struct iqs7222_dev_desc *dev_desc = iqs7222->dev_desc;
1475 	struct i2c_client *client = iqs7222->client;
1476 	int num_gpio = dev_desc->reg_grps[IQS7222_REG_GRP_GPIO].num_row;
1477 	int error, count, i;
1478 	unsigned int gpio_sel[ARRAY_SIZE(iqs7222_gpio_links)];
1479 
1480 	if (!num_gpio)
1481 		return 0;
1482 
1483 	if (!fwnode_property_present(child_node, "azoteq,gpio-select"))
1484 		return 0;
1485 
1486 	count = fwnode_property_count_u32(child_node, "azoteq,gpio-select");
1487 	if (count > num_gpio) {
1488 		dev_err(&client->dev, "Invalid number of %s GPIOs\n",
1489 			fwnode_get_name(child_node));
1490 		return -EINVAL;
1491 	} else if (count < 0) {
1492 		dev_err(&client->dev, "Failed to count %s GPIOs: %d\n",
1493 			fwnode_get_name(child_node), count);
1494 		return count;
1495 	}
1496 
1497 	error = fwnode_property_read_u32_array(child_node,
1498 					       "azoteq,gpio-select",
1499 					       gpio_sel, count);
1500 	if (error) {
1501 		dev_err(&client->dev, "Failed to read %s GPIOs: %d\n",
1502 			fwnode_get_name(child_node), error);
1503 		return error;
1504 	}
1505 
1506 	for (i = 0; i < count; i++) {
1507 		u16 *gpio_setup;
1508 
1509 		if (gpio_sel[i] >= num_gpio) {
1510 			dev_err(&client->dev, "Invalid %s GPIO: %u\n",
1511 				fwnode_get_name(child_node), gpio_sel[i]);
1512 			return -EINVAL;
1513 		}
1514 
1515 		gpio_setup = iqs7222->gpio_setup[gpio_sel[i]];
1516 
1517 		if (gpio_setup[2] && child_link != gpio_setup[2]) {
1518 			dev_err(&client->dev,
1519 				"Conflicting GPIO %u event types\n",
1520 				gpio_sel[i]);
1521 			return -EINVAL;
1522 		}
1523 
1524 		gpio_setup[0] |= IQS7222_GPIO_SETUP_0_GPIO_EN;
1525 		gpio_setup[1] |= child_enable;
1526 		gpio_setup[2] = child_link;
1527 	}
1528 
1529 	return 0;
1530 }
1531 
1532 static int iqs7222_parse_props(struct iqs7222_private *iqs7222,
1533 			       struct fwnode_handle **child_node,
1534 			       int child_index,
1535 			       enum iqs7222_reg_grp_id reg_grp,
1536 			       enum iqs7222_reg_key_id reg_key)
1537 {
1538 	u16 *setup = iqs7222_setup(iqs7222, reg_grp, child_index);
1539 	struct i2c_client *client = iqs7222->client;
1540 	struct fwnode_handle *reg_grp_node;
1541 	char reg_grp_name[16];
1542 	int i;
1543 
1544 	switch (reg_grp) {
1545 	case IQS7222_REG_GRP_CYCLE:
1546 	case IQS7222_REG_GRP_CHAN:
1547 	case IQS7222_REG_GRP_SLDR:
1548 	case IQS7222_REG_GRP_GPIO:
1549 	case IQS7222_REG_GRP_BTN:
1550 		/*
1551 		 * These groups derive a child node and return it to the caller
1552 		 * for additional group-specific processing. In some cases, the
1553 		 * child node may have already been derived.
1554 		 */
1555 		reg_grp_node = *child_node;
1556 		if (reg_grp_node)
1557 			break;
1558 
1559 		snprintf(reg_grp_name, sizeof(reg_grp_name), "%s-%d",
1560 			 iqs7222_reg_grp_names[reg_grp], child_index);
1561 
1562 		reg_grp_node = device_get_named_child_node(&client->dev,
1563 							   reg_grp_name);
1564 		if (!reg_grp_node)
1565 			return 0;
1566 
1567 		*child_node = reg_grp_node;
1568 		break;
1569 
1570 	case IQS7222_REG_GRP_GLBL:
1571 	case IQS7222_REG_GRP_FILT:
1572 	case IQS7222_REG_GRP_SYS:
1573 		/*
1574 		 * These groups are not organized beneath a child node, nor are
1575 		 * they subject to any additional processing by the caller.
1576 		 */
1577 		reg_grp_node = dev_fwnode(&client->dev);
1578 		break;
1579 
1580 	default:
1581 		return -EINVAL;
1582 	}
1583 
1584 	for (i = 0; i < ARRAY_SIZE(iqs7222_props); i++) {
1585 		const char *name = iqs7222_props[i].name;
1586 		int reg_offset = iqs7222_props[i].reg_offset;
1587 		int reg_shift = iqs7222_props[i].reg_shift;
1588 		int reg_width = iqs7222_props[i].reg_width;
1589 		int val_pitch = iqs7222_props[i].val_pitch ? : 1;
1590 		int val_min = iqs7222_props[i].val_min;
1591 		int val_max = iqs7222_props[i].val_max;
1592 		bool invert = iqs7222_props[i].invert;
1593 		const char *label = iqs7222_props[i].label ? : name;
1594 		unsigned int val;
1595 		int error;
1596 
1597 		if (iqs7222_props[i].reg_grp != reg_grp ||
1598 		    iqs7222_props[i].reg_key != reg_key)
1599 			continue;
1600 
1601 		/*
1602 		 * Boolean register fields are one bit wide; they are forcibly
1603 		 * reset to provide a means to undo changes by a bootloader if
1604 		 * necessary.
1605 		 *
1606 		 * Scalar fields, on the other hand, are left untouched unless
1607 		 * their corresponding properties are present.
1608 		 */
1609 		if (reg_width == 1) {
1610 			if (invert)
1611 				setup[reg_offset] |= BIT(reg_shift);
1612 			else
1613 				setup[reg_offset] &= ~BIT(reg_shift);
1614 		}
1615 
1616 		if (!fwnode_property_present(reg_grp_node, name))
1617 			continue;
1618 
1619 		if (reg_width == 1) {
1620 			if (invert)
1621 				setup[reg_offset] &= ~BIT(reg_shift);
1622 			else
1623 				setup[reg_offset] |= BIT(reg_shift);
1624 
1625 			continue;
1626 		}
1627 
1628 		error = fwnode_property_read_u32(reg_grp_node, name, &val);
1629 		if (error) {
1630 			dev_err(&client->dev, "Failed to read %s %s: %d\n",
1631 				fwnode_get_name(reg_grp_node), label, error);
1632 			return error;
1633 		}
1634 
1635 		if (!val_max)
1636 			val_max = GENMASK(reg_width - 1, 0) * val_pitch;
1637 
1638 		if (val < val_min || val > val_max) {
1639 			dev_err(&client->dev, "Invalid %s %s: %u\n",
1640 				fwnode_get_name(reg_grp_node), label, val);
1641 			return -EINVAL;
1642 		}
1643 
1644 		setup[reg_offset] &= ~GENMASK(reg_shift + reg_width - 1,
1645 					      reg_shift);
1646 		setup[reg_offset] |= (val / val_pitch << reg_shift);
1647 	}
1648 
1649 	return 0;
1650 }
1651 
1652 static int iqs7222_parse_cycle(struct iqs7222_private *iqs7222, int cycle_index)
1653 {
1654 	u16 *cycle_setup = iqs7222->cycle_setup[cycle_index];
1655 	struct i2c_client *client = iqs7222->client;
1656 	struct fwnode_handle *cycle_node = NULL;
1657 	unsigned int pins[9];
1658 	int error, count, i;
1659 
1660 	/*
1661 	 * Each channel shares a cycle with one other channel; the mapping of
1662 	 * channels to cycles is fixed. Properties defined for a cycle impact
1663 	 * both channels tied to the cycle.
1664 	 */
1665 	error = iqs7222_parse_props(iqs7222, &cycle_node, cycle_index,
1666 				    IQS7222_REG_GRP_CYCLE,
1667 				    IQS7222_REG_KEY_NONE);
1668 	if (error)
1669 		return error;
1670 
1671 	if (!cycle_node)
1672 		return 0;
1673 
1674 	/*
1675 	 * Unlike channels which are restricted to a select range of CRx pins
1676 	 * based on channel number, any cycle can claim any of the device's 9
1677 	 * CTx pins (CTx0-8).
1678 	 */
1679 	if (!fwnode_property_present(cycle_node, "azoteq,tx-enable"))
1680 		return 0;
1681 
1682 	count = fwnode_property_count_u32(cycle_node, "azoteq,tx-enable");
1683 	if (count < 0) {
1684 		dev_err(&client->dev, "Failed to count %s CTx pins: %d\n",
1685 			fwnode_get_name(cycle_node), count);
1686 		return count;
1687 	} else if (count > ARRAY_SIZE(pins)) {
1688 		dev_err(&client->dev, "Invalid number of %s CTx pins\n",
1689 			fwnode_get_name(cycle_node));
1690 		return -EINVAL;
1691 	}
1692 
1693 	error = fwnode_property_read_u32_array(cycle_node, "azoteq,tx-enable",
1694 					       pins, count);
1695 	if (error) {
1696 		dev_err(&client->dev, "Failed to read %s CTx pins: %d\n",
1697 			fwnode_get_name(cycle_node), error);
1698 		return error;
1699 	}
1700 
1701 	cycle_setup[1] &= ~GENMASK(7 + ARRAY_SIZE(pins) - 1, 7);
1702 
1703 	for (i = 0; i < count; i++) {
1704 		if (pins[i] > 8) {
1705 			dev_err(&client->dev, "Invalid %s CTx pin: %u\n",
1706 				fwnode_get_name(cycle_node), pins[i]);
1707 			return -EINVAL;
1708 		}
1709 
1710 		cycle_setup[1] |= BIT(pins[i] + 7);
1711 	}
1712 
1713 	return 0;
1714 }
1715 
1716 static int iqs7222_parse_chan(struct iqs7222_private *iqs7222, int chan_index)
1717 {
1718 	const struct iqs7222_dev_desc *dev_desc = iqs7222->dev_desc;
1719 	struct i2c_client *client = iqs7222->client;
1720 	struct fwnode_handle *chan_node = NULL;
1721 	int num_chan = dev_desc->reg_grps[IQS7222_REG_GRP_CHAN].num_row;
1722 	int ext_chan = rounddown(num_chan, 10);
1723 	int error, i;
1724 	u16 *chan_setup = iqs7222->chan_setup[chan_index];
1725 	u16 *sys_setup = iqs7222->sys_setup;
1726 	unsigned int val;
1727 
1728 	error = iqs7222_parse_props(iqs7222, &chan_node, chan_index,
1729 				    IQS7222_REG_GRP_CHAN,
1730 				    IQS7222_REG_KEY_NONE);
1731 	if (error)
1732 		return error;
1733 
1734 	if (!chan_node)
1735 		return 0;
1736 
1737 	if (dev_desc->allow_offset) {
1738 		sys_setup[dev_desc->allow_offset] |= BIT(chan_index);
1739 		if (fwnode_property_present(chan_node, "azoteq,ulp-allow"))
1740 			sys_setup[dev_desc->allow_offset] &= ~BIT(chan_index);
1741 	}
1742 
1743 	chan_setup[0] |= IQS7222_CHAN_SETUP_0_CHAN_EN;
1744 
1745 	/*
1746 	 * The reference channel function allows for differential measurements
1747 	 * and is only available in the case of IQS7222A or IQS7222C.
1748 	 */
1749 	if (dev_desc->reg_grps[IQS7222_REG_GRP_CHAN].num_col > 4 &&
1750 	    fwnode_property_present(chan_node, "azoteq,ref-select")) {
1751 		u16 *ref_setup;
1752 
1753 		error = fwnode_property_read_u32(chan_node, "azoteq,ref-select",
1754 						 &val);
1755 		if (error) {
1756 			dev_err(&client->dev,
1757 				"Failed to read %s reference channel: %d\n",
1758 				fwnode_get_name(chan_node), error);
1759 			return error;
1760 		}
1761 
1762 		if (val >= ext_chan) {
1763 			dev_err(&client->dev,
1764 				"Invalid %s reference channel: %u\n",
1765 				fwnode_get_name(chan_node), val);
1766 			return -EINVAL;
1767 		}
1768 
1769 		ref_setup = iqs7222->chan_setup[val];
1770 
1771 		/*
1772 		 * Configure the current channel as a follower of the selected
1773 		 * reference channel.
1774 		 */
1775 		chan_setup[0] |= IQS7222_CHAN_SETUP_0_REF_MODE_FOLLOW;
1776 		chan_setup[4] = val * 42 + 1048;
1777 
1778 		if (!fwnode_property_read_u32(chan_node, "azoteq,ref-weight",
1779 					      &val)) {
1780 			if (val > U16_MAX) {
1781 				dev_err(&client->dev,
1782 					"Invalid %s reference weight: %u\n",
1783 					fwnode_get_name(chan_node), val);
1784 				return -EINVAL;
1785 			}
1786 
1787 			chan_setup[5] = val;
1788 		}
1789 
1790 		/*
1791 		 * Configure the selected channel as a reference channel which
1792 		 * serves the current channel.
1793 		 */
1794 		ref_setup[0] |= IQS7222_CHAN_SETUP_0_REF_MODE_REF;
1795 		ref_setup[5] |= BIT(chan_index);
1796 
1797 		ref_setup[4] = dev_desc->touch_link;
1798 		if (fwnode_property_present(chan_node, "azoteq,use-prox"))
1799 			ref_setup[4] -= 2;
1800 	}
1801 
1802 	if (fwnode_property_present(chan_node, "azoteq,rx-enable")) {
1803 		/*
1804 		 * Each channel can claim up to 4 CRx pins. The first half of
1805 		 * the channels can use CRx0-3, while the second half can use
1806 		 * CRx4-7.
1807 		 */
1808 		unsigned int pins[4];
1809 		int count;
1810 
1811 		count = fwnode_property_count_u32(chan_node,
1812 						  "azoteq,rx-enable");
1813 		if (count < 0) {
1814 			dev_err(&client->dev,
1815 				"Failed to count %s CRx pins: %d\n",
1816 				fwnode_get_name(chan_node), count);
1817 			return count;
1818 		} else if (count > ARRAY_SIZE(pins)) {
1819 			dev_err(&client->dev,
1820 				"Invalid number of %s CRx pins\n",
1821 				fwnode_get_name(chan_node));
1822 			return -EINVAL;
1823 		}
1824 
1825 		error = fwnode_property_read_u32_array(chan_node,
1826 						       "azoteq,rx-enable",
1827 						       pins, count);
1828 		if (error) {
1829 			dev_err(&client->dev,
1830 				"Failed to read %s CRx pins: %d\n",
1831 				fwnode_get_name(chan_node), error);
1832 			return error;
1833 		}
1834 
1835 		chan_setup[0] &= ~GENMASK(4 + ARRAY_SIZE(pins) - 1, 4);
1836 
1837 		for (i = 0; i < count; i++) {
1838 			int min_crx = chan_index < ext_chan / 2 ? 0 : 4;
1839 
1840 			if (pins[i] < min_crx || pins[i] > min_crx + 3) {
1841 				dev_err(&client->dev,
1842 					"Invalid %s CRx pin: %u\n",
1843 					fwnode_get_name(chan_node), pins[i]);
1844 				return -EINVAL;
1845 			}
1846 
1847 			chan_setup[0] |= BIT(pins[i] + 4 - min_crx);
1848 		}
1849 	}
1850 
1851 	for (i = 0; i < ARRAY_SIZE(iqs7222_kp_events); i++) {
1852 		const char *event_name = iqs7222_kp_events[i].name;
1853 		u16 event_enable = iqs7222_kp_events[i].enable;
1854 		struct fwnode_handle *event_node;
1855 
1856 		event_node = fwnode_get_named_child_node(chan_node, event_name);
1857 		if (!event_node)
1858 			continue;
1859 
1860 		error = iqs7222_parse_props(iqs7222, &event_node, chan_index,
1861 					    IQS7222_REG_GRP_BTN,
1862 					    iqs7222_kp_events[i].reg_key);
1863 		if (error)
1864 			return error;
1865 
1866 		error = iqs7222_gpio_select(iqs7222, event_node,
1867 					    BIT(chan_index),
1868 					    dev_desc->touch_link - (i ? 0 : 2));
1869 		if (error)
1870 			return error;
1871 
1872 		if (!fwnode_property_read_u32(event_node,
1873 					      "azoteq,timeout-press-ms",
1874 					      &val)) {
1875 			/*
1876 			 * The IQS7222B employs a global pair of press timeout
1877 			 * registers as opposed to channel-specific registers.
1878 			 */
1879 			u16 *setup = dev_desc->reg_grps
1880 				     [IQS7222_REG_GRP_BTN].num_col > 2 ?
1881 				     &iqs7222->btn_setup[chan_index][2] :
1882 				     &sys_setup[9];
1883 
1884 			if (val > U8_MAX * 500) {
1885 				dev_err(&client->dev,
1886 					"Invalid %s press timeout: %u\n",
1887 					fwnode_get_name(chan_node), val);
1888 				return -EINVAL;
1889 			}
1890 
1891 			*setup &= ~(U8_MAX << i * 8);
1892 			*setup |= (val / 500 << i * 8);
1893 		}
1894 
1895 		error = fwnode_property_read_u32(event_node, "linux,code",
1896 						 &val);
1897 		if (error) {
1898 			dev_err(&client->dev, "Failed to read %s code: %d\n",
1899 				fwnode_get_name(chan_node), error);
1900 			return error;
1901 		}
1902 
1903 		iqs7222->kp_code[chan_index][i] = val;
1904 		iqs7222->kp_type[chan_index][i] = EV_KEY;
1905 
1906 		if (fwnode_property_present(event_node, "linux,input-type")) {
1907 			error = fwnode_property_read_u32(event_node,
1908 							 "linux,input-type",
1909 							 &val);
1910 			if (error) {
1911 				dev_err(&client->dev,
1912 					"Failed to read %s input type: %d\n",
1913 					fwnode_get_name(chan_node), error);
1914 				return error;
1915 			}
1916 
1917 			if (val != EV_KEY && val != EV_SW) {
1918 				dev_err(&client->dev,
1919 					"Invalid %s input type: %u\n",
1920 					fwnode_get_name(chan_node), val);
1921 				return -EINVAL;
1922 			}
1923 
1924 			iqs7222->kp_type[chan_index][i] = val;
1925 		}
1926 
1927 		/*
1928 		 * Reference channels can opt out of event reporting by using
1929 		 * KEY_RESERVED in place of a true key or switch code.
1930 		 */
1931 		if (iqs7222->kp_type[chan_index][i] == EV_KEY &&
1932 		    iqs7222->kp_code[chan_index][i] == KEY_RESERVED)
1933 			continue;
1934 
1935 		input_set_capability(iqs7222->keypad,
1936 				     iqs7222->kp_type[chan_index][i],
1937 				     iqs7222->kp_code[chan_index][i]);
1938 
1939 		if (!dev_desc->event_offset)
1940 			continue;
1941 
1942 		sys_setup[dev_desc->event_offset] |= event_enable;
1943 	}
1944 
1945 	/*
1946 	 * The following call handles a special pair of properties that apply
1947 	 * to a channel node, but reside within the button (event) group.
1948 	 */
1949 	return iqs7222_parse_props(iqs7222, &chan_node, chan_index,
1950 				   IQS7222_REG_GRP_BTN,
1951 				   IQS7222_REG_KEY_DEBOUNCE);
1952 }
1953 
1954 static int iqs7222_parse_sldr(struct iqs7222_private *iqs7222, int sldr_index)
1955 {
1956 	const struct iqs7222_dev_desc *dev_desc = iqs7222->dev_desc;
1957 	struct i2c_client *client = iqs7222->client;
1958 	struct fwnode_handle *sldr_node = NULL;
1959 	int num_chan = dev_desc->reg_grps[IQS7222_REG_GRP_CHAN].num_row;
1960 	int ext_chan = rounddown(num_chan, 10);
1961 	int count, error, reg_offset, i;
1962 	u16 *event_mask = &iqs7222->sys_setup[dev_desc->event_offset];
1963 	u16 *sldr_setup = iqs7222->sldr_setup[sldr_index];
1964 	unsigned int chan_sel[4], val;
1965 
1966 	error = iqs7222_parse_props(iqs7222, &sldr_node, sldr_index,
1967 				    IQS7222_REG_GRP_SLDR,
1968 				    IQS7222_REG_KEY_NONE);
1969 	if (error)
1970 		return error;
1971 
1972 	if (!sldr_node)
1973 		return 0;
1974 
1975 	/*
1976 	 * Each slider can be spread across 3 to 4 channels. It is possible to
1977 	 * select only 2 channels, but doing so prevents the slider from using
1978 	 * the specified resolution.
1979 	 */
1980 	count = fwnode_property_count_u32(sldr_node, "azoteq,channel-select");
1981 	if (count < 0) {
1982 		dev_err(&client->dev, "Failed to count %s channels: %d\n",
1983 			fwnode_get_name(sldr_node), count);
1984 		return count;
1985 	} else if (count < 3 || count > ARRAY_SIZE(chan_sel)) {
1986 		dev_err(&client->dev, "Invalid number of %s channels\n",
1987 			fwnode_get_name(sldr_node));
1988 		return -EINVAL;
1989 	}
1990 
1991 	error = fwnode_property_read_u32_array(sldr_node,
1992 					       "azoteq,channel-select",
1993 					       chan_sel, count);
1994 	if (error) {
1995 		dev_err(&client->dev, "Failed to read %s channels: %d\n",
1996 			fwnode_get_name(sldr_node), error);
1997 		return error;
1998 	}
1999 
2000 	/*
2001 	 * Resolution and top speed, if small enough, are packed into a single
2002 	 * register. Otherwise, each occupies its own register and the rest of
2003 	 * the slider-related register addresses are offset by one.
2004 	 */
2005 	reg_offset = dev_desc->sldr_res < U16_MAX ? 0 : 1;
2006 
2007 	sldr_setup[0] |= count;
2008 	sldr_setup[3 + reg_offset] &= ~GENMASK(ext_chan - 1, 0);
2009 
2010 	for (i = 0; i < ARRAY_SIZE(chan_sel); i++) {
2011 		sldr_setup[5 + reg_offset + i] = 0;
2012 		if (i >= count)
2013 			continue;
2014 
2015 		if (chan_sel[i] >= ext_chan) {
2016 			dev_err(&client->dev, "Invalid %s channel: %u\n",
2017 				fwnode_get_name(sldr_node), chan_sel[i]);
2018 			return -EINVAL;
2019 		}
2020 
2021 		/*
2022 		 * The following fields indicate which channels participate in
2023 		 * the slider, as well as each channel's relative placement.
2024 		 */
2025 		sldr_setup[3 + reg_offset] |= BIT(chan_sel[i]);
2026 		sldr_setup[5 + reg_offset + i] = chan_sel[i] * 42 + 1080;
2027 	}
2028 
2029 	sldr_setup[4 + reg_offset] = dev_desc->touch_link;
2030 	if (fwnode_property_present(sldr_node, "azoteq,use-prox"))
2031 		sldr_setup[4 + reg_offset] -= 2;
2032 
2033 	if (!fwnode_property_read_u32(sldr_node, "azoteq,slider-size", &val)) {
2034 		if (!val || val > dev_desc->sldr_res) {
2035 			dev_err(&client->dev, "Invalid %s size: %u\n",
2036 				fwnode_get_name(sldr_node), val);
2037 			return -EINVAL;
2038 		}
2039 
2040 		if (reg_offset) {
2041 			sldr_setup[3] = val;
2042 		} else {
2043 			sldr_setup[2] &= ~IQS7222_SLDR_SETUP_2_RES_MASK;
2044 			sldr_setup[2] |= (val / 16 <<
2045 					  IQS7222_SLDR_SETUP_2_RES_SHIFT);
2046 		}
2047 	}
2048 
2049 	if (!fwnode_property_read_u32(sldr_node, "azoteq,top-speed", &val)) {
2050 		if (val > (reg_offset ? U16_MAX : U8_MAX * 4)) {
2051 			dev_err(&client->dev, "Invalid %s top speed: %u\n",
2052 				fwnode_get_name(sldr_node), val);
2053 			return -EINVAL;
2054 		}
2055 
2056 		if (reg_offset) {
2057 			sldr_setup[2] = val;
2058 		} else {
2059 			sldr_setup[2] &= ~IQS7222_SLDR_SETUP_2_TOP_SPEED_MASK;
2060 			sldr_setup[2] |= (val / 4);
2061 		}
2062 	}
2063 
2064 	if (!fwnode_property_read_u32(sldr_node, "linux,axis", &val)) {
2065 		u16 sldr_max = sldr_setup[3] - 1;
2066 
2067 		if (!reg_offset) {
2068 			sldr_max = sldr_setup[2];
2069 
2070 			sldr_max &= IQS7222_SLDR_SETUP_2_RES_MASK;
2071 			sldr_max >>= IQS7222_SLDR_SETUP_2_RES_SHIFT;
2072 
2073 			sldr_max = sldr_max * 16 - 1;
2074 		}
2075 
2076 		input_set_abs_params(iqs7222->keypad, val, 0, sldr_max, 0, 0);
2077 		iqs7222->sl_axis[sldr_index] = val;
2078 	}
2079 
2080 	if (dev_desc->wheel_enable) {
2081 		sldr_setup[0] &= ~dev_desc->wheel_enable;
2082 		if (iqs7222->sl_axis[sldr_index] == ABS_WHEEL)
2083 			sldr_setup[0] |= dev_desc->wheel_enable;
2084 	}
2085 
2086 	/*
2087 	 * The absence of a register offset makes it safe to assume the device
2088 	 * supports gestures, each of which is first disabled until explicitly
2089 	 * enabled.
2090 	 */
2091 	if (!reg_offset)
2092 		for (i = 0; i < ARRAY_SIZE(iqs7222_sl_events); i++)
2093 			sldr_setup[9] &= ~iqs7222_sl_events[i].enable;
2094 
2095 	for (i = 0; i < ARRAY_SIZE(iqs7222_sl_events); i++) {
2096 		const char *event_name = iqs7222_sl_events[i].name;
2097 		struct fwnode_handle *event_node;
2098 
2099 		event_node = fwnode_get_named_child_node(sldr_node, event_name);
2100 		if (!event_node)
2101 			continue;
2102 
2103 		error = iqs7222_parse_props(iqs7222, &event_node, sldr_index,
2104 					    IQS7222_REG_GRP_SLDR,
2105 					    reg_offset ?
2106 					    IQS7222_REG_KEY_RESERVED :
2107 					    iqs7222_sl_events[i].reg_key);
2108 		if (error)
2109 			return error;
2110 
2111 		/*
2112 		 * The press/release event does not expose a direct GPIO link,
2113 		 * but one can be emulated by tying each of the participating
2114 		 * channels to the same GPIO.
2115 		 */
2116 		error = iqs7222_gpio_select(iqs7222, event_node,
2117 					    i ? iqs7222_sl_events[i].enable
2118 					      : sldr_setup[3 + reg_offset],
2119 					    i ? 1568 + sldr_index * 30
2120 					      : sldr_setup[4 + reg_offset]);
2121 		if (error)
2122 			return error;
2123 
2124 		if (!reg_offset)
2125 			sldr_setup[9] |= iqs7222_sl_events[i].enable;
2126 
2127 		error = fwnode_property_read_u32(event_node, "linux,code",
2128 						 &val);
2129 		if (error) {
2130 			dev_err(&client->dev, "Failed to read %s code: %d\n",
2131 				fwnode_get_name(sldr_node), error);
2132 			return error;
2133 		}
2134 
2135 		iqs7222->sl_code[sldr_index][i] = val;
2136 		input_set_capability(iqs7222->keypad, EV_KEY, val);
2137 
2138 		if (!dev_desc->event_offset)
2139 			continue;
2140 
2141 		/*
2142 		 * The press/release event is determined based on whether the
2143 		 * coordinate field reports 0xFFFF and solely relies on touch
2144 		 * or proximity interrupts to be unmasked.
2145 		 */
2146 		if (i && !reg_offset)
2147 			*event_mask |= (IQS7222_EVENT_MASK_SLDR << sldr_index);
2148 		else if (sldr_setup[4 + reg_offset] == dev_desc->touch_link)
2149 			*event_mask |= IQS7222_EVENT_MASK_TOUCH;
2150 		else
2151 			*event_mask |= IQS7222_EVENT_MASK_PROX;
2152 	}
2153 
2154 	/*
2155 	 * The following call handles a special pair of properties that shift
2156 	 * to make room for a wheel enable control in the case of IQS7222C.
2157 	 */
2158 	return iqs7222_parse_props(iqs7222, &sldr_node, sldr_index,
2159 				   IQS7222_REG_GRP_SLDR,
2160 				   dev_desc->wheel_enable ?
2161 				   IQS7222_REG_KEY_WHEEL :
2162 				   IQS7222_REG_KEY_NO_WHEEL);
2163 }
2164 
2165 static int iqs7222_parse_all(struct iqs7222_private *iqs7222)
2166 {
2167 	const struct iqs7222_dev_desc *dev_desc = iqs7222->dev_desc;
2168 	const struct iqs7222_reg_grp_desc *reg_grps = dev_desc->reg_grps;
2169 	u16 *sys_setup = iqs7222->sys_setup;
2170 	int error, i;
2171 
2172 	if (dev_desc->event_offset)
2173 		sys_setup[dev_desc->event_offset] = IQS7222_EVENT_MASK_ATI;
2174 
2175 	for (i = 0; i < reg_grps[IQS7222_REG_GRP_CYCLE].num_row; i++) {
2176 		error = iqs7222_parse_cycle(iqs7222, i);
2177 		if (error)
2178 			return error;
2179 	}
2180 
2181 	error = iqs7222_parse_props(iqs7222, NULL, 0, IQS7222_REG_GRP_GLBL,
2182 				    IQS7222_REG_KEY_NONE);
2183 	if (error)
2184 		return error;
2185 
2186 	for (i = 0; i < reg_grps[IQS7222_REG_GRP_GPIO].num_row; i++) {
2187 		struct fwnode_handle *gpio_node = NULL;
2188 		u16 *gpio_setup = iqs7222->gpio_setup[i];
2189 		int j;
2190 
2191 		gpio_setup[0] &= ~IQS7222_GPIO_SETUP_0_GPIO_EN;
2192 		gpio_setup[1] = 0;
2193 		gpio_setup[2] = 0;
2194 
2195 		error = iqs7222_parse_props(iqs7222, &gpio_node, i,
2196 					    IQS7222_REG_GRP_GPIO,
2197 					    IQS7222_REG_KEY_NONE);
2198 		if (error)
2199 			return error;
2200 
2201 		if (reg_grps[IQS7222_REG_GRP_GPIO].num_row == 1)
2202 			continue;
2203 
2204 		/*
2205 		 * The IQS7222C exposes multiple GPIO and must be informed
2206 		 * as to which GPIO this group represents.
2207 		 */
2208 		for (j = 0; j < ARRAY_SIZE(iqs7222_gpio_links); j++)
2209 			gpio_setup[0] &= ~BIT(iqs7222_gpio_links[j]);
2210 
2211 		gpio_setup[0] |= BIT(iqs7222_gpio_links[i]);
2212 	}
2213 
2214 	for (i = 0; i < reg_grps[IQS7222_REG_GRP_CHAN].num_row; i++) {
2215 		u16 *chan_setup = iqs7222->chan_setup[i];
2216 
2217 		chan_setup[0] &= ~IQS7222_CHAN_SETUP_0_REF_MODE_MASK;
2218 		chan_setup[0] &= ~IQS7222_CHAN_SETUP_0_CHAN_EN;
2219 
2220 		chan_setup[5] = 0;
2221 	}
2222 
2223 	for (i = 0; i < reg_grps[IQS7222_REG_GRP_CHAN].num_row; i++) {
2224 		error = iqs7222_parse_chan(iqs7222, i);
2225 		if (error)
2226 			return error;
2227 	}
2228 
2229 	error = iqs7222_parse_props(iqs7222, NULL, 0, IQS7222_REG_GRP_FILT,
2230 				    IQS7222_REG_KEY_NONE);
2231 	if (error)
2232 		return error;
2233 
2234 	for (i = 0; i < reg_grps[IQS7222_REG_GRP_SLDR].num_row; i++) {
2235 		u16 *sldr_setup = iqs7222->sldr_setup[i];
2236 
2237 		sldr_setup[0] &= ~IQS7222_SLDR_SETUP_0_CHAN_CNT_MASK;
2238 
2239 		error = iqs7222_parse_sldr(iqs7222, i);
2240 		if (error)
2241 			return error;
2242 	}
2243 
2244 	sys_setup[0] &= ~IQS7222_SYS_SETUP_INTF_MODE_MASK;
2245 	sys_setup[0] &= ~IQS7222_SYS_SETUP_PWR_MODE_MASK;
2246 
2247 	sys_setup[0] |= IQS7222_SYS_SETUP_ACK_RESET;
2248 
2249 	return iqs7222_parse_props(iqs7222, NULL, 0, IQS7222_REG_GRP_SYS,
2250 				   IQS7222_REG_KEY_NONE);
2251 }
2252 
2253 static int iqs7222_report(struct iqs7222_private *iqs7222)
2254 {
2255 	const struct iqs7222_dev_desc *dev_desc = iqs7222->dev_desc;
2256 	struct i2c_client *client = iqs7222->client;
2257 	int num_chan = dev_desc->reg_grps[IQS7222_REG_GRP_CHAN].num_row;
2258 	int num_stat = dev_desc->reg_grps[IQS7222_REG_GRP_STAT].num_col;
2259 	int error, i, j;
2260 	__le16 status[IQS7222_MAX_COLS_STAT];
2261 
2262 	error = iqs7222_read_burst(iqs7222, IQS7222_SYS_STATUS, status,
2263 				   num_stat);
2264 	if (error)
2265 		return error;
2266 
2267 	if (le16_to_cpu(status[0]) & IQS7222_SYS_STATUS_RESET) {
2268 		dev_err(&client->dev, "Unexpected device reset\n");
2269 		return iqs7222_dev_init(iqs7222, WRITE);
2270 	}
2271 
2272 	if (le16_to_cpu(status[0]) & IQS7222_SYS_STATUS_ATI_ERROR) {
2273 		dev_err(&client->dev, "Unexpected ATI error\n");
2274 		return iqs7222_ati_trigger(iqs7222);
2275 	}
2276 
2277 	if (le16_to_cpu(status[0]) & IQS7222_SYS_STATUS_ATI_ACTIVE)
2278 		return 0;
2279 
2280 	for (i = 0; i < num_chan; i++) {
2281 		u16 *chan_setup = iqs7222->chan_setup[i];
2282 
2283 		if (!(chan_setup[0] & IQS7222_CHAN_SETUP_0_CHAN_EN))
2284 			continue;
2285 
2286 		for (j = 0; j < ARRAY_SIZE(iqs7222_kp_events); j++) {
2287 			/*
2288 			 * Proximity state begins at offset 2 and spills into
2289 			 * offset 3 for devices with more than 16 channels.
2290 			 *
2291 			 * Touch state begins at the first offset immediately
2292 			 * following proximity state.
2293 			 */
2294 			int k = 2 + j * (num_chan > 16 ? 2 : 1);
2295 			u16 state = le16_to_cpu(status[k + i / 16]);
2296 
2297 			input_event(iqs7222->keypad,
2298 				    iqs7222->kp_type[i][j],
2299 				    iqs7222->kp_code[i][j],
2300 				    !!(state & BIT(i % 16)));
2301 		}
2302 	}
2303 
2304 	for (i = 0; i < dev_desc->reg_grps[IQS7222_REG_GRP_SLDR].num_row; i++) {
2305 		u16 *sldr_setup = iqs7222->sldr_setup[i];
2306 		u16 sldr_pos = le16_to_cpu(status[4 + i]);
2307 		u16 state = le16_to_cpu(status[6 + i]);
2308 
2309 		if (!(sldr_setup[0] & IQS7222_SLDR_SETUP_0_CHAN_CNT_MASK))
2310 			continue;
2311 
2312 		if (sldr_pos < dev_desc->sldr_res)
2313 			input_report_abs(iqs7222->keypad, iqs7222->sl_axis[i],
2314 					 sldr_pos);
2315 
2316 		input_report_key(iqs7222->keypad, iqs7222->sl_code[i][0],
2317 				 sldr_pos < dev_desc->sldr_res);
2318 
2319 		/*
2320 		 * A maximum resolution indicates the device does not support
2321 		 * gestures, in which case the remaining fields are ignored.
2322 		 */
2323 		if (dev_desc->sldr_res == U16_MAX)
2324 			continue;
2325 
2326 		if (!(le16_to_cpu(status[1]) & IQS7222_EVENT_MASK_SLDR << i))
2327 			continue;
2328 
2329 		/*
2330 		 * Skip the press/release event, as it does not have separate
2331 		 * status fields and is handled separately.
2332 		 */
2333 		for (j = 1; j < ARRAY_SIZE(iqs7222_sl_events); j++) {
2334 			u16 mask = iqs7222_sl_events[j].mask;
2335 			u16 val = iqs7222_sl_events[j].val;
2336 
2337 			input_report_key(iqs7222->keypad,
2338 					 iqs7222->sl_code[i][j],
2339 					 (state & mask) == val);
2340 		}
2341 
2342 		input_sync(iqs7222->keypad);
2343 
2344 		for (j = 1; j < ARRAY_SIZE(iqs7222_sl_events); j++)
2345 			input_report_key(iqs7222->keypad,
2346 					 iqs7222->sl_code[i][j], 0);
2347 	}
2348 
2349 	input_sync(iqs7222->keypad);
2350 
2351 	return 0;
2352 }
2353 
2354 static irqreturn_t iqs7222_irq(int irq, void *context)
2355 {
2356 	struct iqs7222_private *iqs7222 = context;
2357 
2358 	return iqs7222_report(iqs7222) ? IRQ_NONE : IRQ_HANDLED;
2359 }
2360 
2361 static int iqs7222_probe(struct i2c_client *client)
2362 {
2363 	struct iqs7222_private *iqs7222;
2364 	unsigned long irq_flags;
2365 	int error, irq;
2366 
2367 	iqs7222 = devm_kzalloc(&client->dev, sizeof(*iqs7222), GFP_KERNEL);
2368 	if (!iqs7222)
2369 		return -ENOMEM;
2370 
2371 	i2c_set_clientdata(client, iqs7222);
2372 	iqs7222->client = client;
2373 
2374 	iqs7222->keypad = devm_input_allocate_device(&client->dev);
2375 	if (!iqs7222->keypad)
2376 		return -ENOMEM;
2377 
2378 	iqs7222->keypad->name = client->name;
2379 	iqs7222->keypad->id.bustype = BUS_I2C;
2380 
2381 	/*
2382 	 * The RDY pin behaves as an interrupt, but must also be polled ahead
2383 	 * of unsolicited I2C communication. As such, it is first opened as a
2384 	 * GPIO and then passed to gpiod_to_irq() to register the interrupt.
2385 	 */
2386 	iqs7222->irq_gpio = devm_gpiod_get(&client->dev, "irq", GPIOD_IN);
2387 	if (IS_ERR(iqs7222->irq_gpio)) {
2388 		error = PTR_ERR(iqs7222->irq_gpio);
2389 		dev_err(&client->dev, "Failed to request IRQ GPIO: %d\n",
2390 			error);
2391 		return error;
2392 	}
2393 
2394 	iqs7222->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
2395 						      GPIOD_OUT_HIGH);
2396 	if (IS_ERR(iqs7222->reset_gpio)) {
2397 		error = PTR_ERR(iqs7222->reset_gpio);
2398 		dev_err(&client->dev, "Failed to request reset GPIO: %d\n",
2399 			error);
2400 		return error;
2401 	}
2402 
2403 	error = iqs7222_hard_reset(iqs7222);
2404 	if (error)
2405 		return error;
2406 
2407 	error = iqs7222_dev_info(iqs7222);
2408 	if (error)
2409 		return error;
2410 
2411 	error = iqs7222_dev_init(iqs7222, READ);
2412 	if (error)
2413 		return error;
2414 
2415 	error = iqs7222_parse_all(iqs7222);
2416 	if (error)
2417 		return error;
2418 
2419 	error = iqs7222_dev_init(iqs7222, WRITE);
2420 	if (error)
2421 		return error;
2422 
2423 	error = iqs7222_report(iqs7222);
2424 	if (error)
2425 		return error;
2426 
2427 	error = input_register_device(iqs7222->keypad);
2428 	if (error) {
2429 		dev_err(&client->dev, "Failed to register device: %d\n", error);
2430 		return error;
2431 	}
2432 
2433 	irq = gpiod_to_irq(iqs7222->irq_gpio);
2434 	if (irq < 0)
2435 		return irq;
2436 
2437 	irq_flags = gpiod_is_active_low(iqs7222->irq_gpio) ? IRQF_TRIGGER_LOW
2438 							   : IRQF_TRIGGER_HIGH;
2439 	irq_flags |= IRQF_ONESHOT;
2440 
2441 	error = devm_request_threaded_irq(&client->dev, irq, NULL, iqs7222_irq,
2442 					  irq_flags, client->name, iqs7222);
2443 	if (error)
2444 		dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
2445 
2446 	return error;
2447 }
2448 
2449 static const struct of_device_id iqs7222_of_match[] = {
2450 	{ .compatible = "azoteq,iqs7222a" },
2451 	{ .compatible = "azoteq,iqs7222b" },
2452 	{ .compatible = "azoteq,iqs7222c" },
2453 	{ }
2454 };
2455 MODULE_DEVICE_TABLE(of, iqs7222_of_match);
2456 
2457 static struct i2c_driver iqs7222_i2c_driver = {
2458 	.driver = {
2459 		.name = "iqs7222",
2460 		.of_match_table = iqs7222_of_match,
2461 	},
2462 	.probe_new = iqs7222_probe,
2463 };
2464 module_i2c_driver(iqs7222_i2c_driver);
2465 
2466 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
2467 MODULE_DESCRIPTION("Azoteq IQS7222A/B/C Capacitive Touch Controller");
2468 MODULE_LICENSE("GPL");
2469