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