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