Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid
[linux-2.6-microblaze.git] / drivers / input / misc / iqs269a.c
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
158 enum 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
164 enum 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
171 enum iqs269_th_offs {
172         IQS269_TH_OFFS_PROX,
173         IQS269_TH_OFFS_TOUCH,
174         IQS269_TH_OFFS_DEEP,
175 };
176
177 enum 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
186 struct iqs269_switch_desc {
187         unsigned int code;
188         bool enabled;
189 };
190
191 struct 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
199 static 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
241 struct iqs269_ver_info {
242         u8 prod_num;
243         u8 sw_num;
244         u8 hw_num;
245         u8 padding;
246 } __packed;
247
248 struct 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
271 struct iqs269_ch_reg {
272         u8 rx_enable;
273         u8 tx_enable;
274         __be16 engine_a;
275         __be16 engine_b;
276         __be16 ati_comp;
277         u8 thresh[3];
278         u8 hyst;
279         u8 assoc_select;
280         u8 assoc_weight;
281 } __packed;
282
283 struct iqs269_flags {
284         __be16 system;
285         u8 gesture;
286         u8 padding;
287         u8 states[4];
288 } __packed;
289
290 struct 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
307 static 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
333 static 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
351 static 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
395 static 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
429 static 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
455 static 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
472 static 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
501 static 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", &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
579                         /* fall through */
580
581                 case IQS269_LOCAL_CAP_SIZE_GLOBAL_ONLY:
582                         engine_b |= IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE;
583                         break;
584
585                 default:
586                         dev_err(&client->dev,
587                                 "Invalid channel %u local cap. size: %u\n", reg,
588                                 val);
589                         return -EINVAL;
590                 }
591         }
592
593         engine_a &= ~IQS269_CHx_ENG_A_INV_LOGIC;
594         if (fwnode_property_present(ch_node, "azoteq,invert-enable"))
595                 engine_a |= IQS269_CHx_ENG_A_INV_LOGIC;
596
597         if (!fwnode_property_read_u32(ch_node, "azoteq,proj-bias", &val)) {
598                 if (val > IQS269_CHx_ENG_A_PROJ_BIAS_MAX) {
599                         dev_err(&client->dev,
600                                 "Invalid channel %u bias current: %u\n", reg,
601                                 val);
602                         return -EINVAL;
603                 }
604
605                 engine_a &= ~IQS269_CHx_ENG_A_PROJ_BIAS_MASK;
606                 engine_a |= (val << IQS269_CHx_ENG_A_PROJ_BIAS_SHIFT);
607         }
608
609         if (!fwnode_property_read_u32(ch_node, "azoteq,sense-mode", &val)) {
610                 if (val > IQS269_CHx_ENG_A_SENSE_MODE_MAX) {
611                         dev_err(&client->dev,
612                                 "Invalid channel %u sensing mode: %u\n", reg,
613                                 val);
614                         return -EINVAL;
615                 }
616
617                 engine_a &= ~IQS269_CHx_ENG_A_SENSE_MODE_MASK;
618                 engine_a |= val;
619         }
620
621         if (!fwnode_property_read_u32(ch_node, "azoteq,sense-freq", &val)) {
622                 if (val > IQS269_CHx_ENG_B_SENSE_FREQ_MAX) {
623                         dev_err(&client->dev,
624                                 "Invalid channel %u sensing frequency: %u\n",
625                                 reg, val);
626                         return -EINVAL;
627                 }
628
629                 engine_b &= ~IQS269_CHx_ENG_B_SENSE_FREQ_MASK;
630                 engine_b |= (val << IQS269_CHx_ENG_B_SENSE_FREQ_SHIFT);
631         }
632
633         engine_b &= ~IQS269_CHx_ENG_B_STATIC_ENABLE;
634         if (fwnode_property_present(ch_node, "azoteq,static-enable"))
635                 engine_b |= IQS269_CHx_ENG_B_STATIC_ENABLE;
636
637         ch_reg->engine_a = cpu_to_be16(engine_a);
638         ch_reg->engine_b = cpu_to_be16(engine_b);
639
640         if (!fwnode_property_read_u32(ch_node, "azoteq,ati-mode", &val)) {
641                 error = iqs269_ati_mode_set(iqs269, reg, val);
642                 if (error) {
643                         dev_err(&client->dev,
644                                 "Invalid channel %u ATI mode: %u\n", reg, val);
645                         return error;
646                 }
647         }
648
649         if (!fwnode_property_read_u32(ch_node, "azoteq,ati-base", &val)) {
650                 error = iqs269_ati_base_set(iqs269, reg, val);
651                 if (error) {
652                         dev_err(&client->dev,
653                                 "Invalid channel %u ATI base: %u\n", reg, val);
654                         return error;
655                 }
656         }
657
658         if (!fwnode_property_read_u32(ch_node, "azoteq,ati-target", &val)) {
659                 error = iqs269_ati_target_set(iqs269, reg, val);
660                 if (error) {
661                         dev_err(&client->dev,
662                                 "Invalid channel %u ATI target: %u\n", reg,
663                                 val);
664                         return error;
665                 }
666         }
667
668         error = iqs269_parse_mask(ch_node, "azoteq,assoc-select",
669                                   &ch_reg->assoc_select);
670         if (error) {
671                 dev_err(&client->dev, "Invalid channel %u association: %d\n",
672                         reg, error);
673                 return error;
674         }
675
676         if (!fwnode_property_read_u32(ch_node, "azoteq,assoc-weight", &val)) {
677                 if (val > IQS269_CHx_WEIGHT_MAX) {
678                         dev_err(&client->dev,
679                                 "Invalid channel %u associated weight: %u\n",
680                                 reg, val);
681                         return -EINVAL;
682                 }
683
684                 ch_reg->assoc_weight = val;
685         }
686
687         for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
688                 ev_node = fwnode_get_named_child_node(ch_node,
689                                                       iqs269_events[i].name);
690                 if (!ev_node)
691                         continue;
692
693                 if (!fwnode_property_read_u32(ev_node, "azoteq,thresh", &val)) {
694                         if (val > IQS269_CHx_THRESH_MAX) {
695                                 dev_err(&client->dev,
696                                         "Invalid channel %u threshold: %u\n",
697                                         reg, val);
698                                 return -EINVAL;
699                         }
700
701                         ch_reg->thresh[iqs269_events[i].th_offs] = val;
702                 }
703
704                 if (!fwnode_property_read_u32(ev_node, "azoteq,hyst", &val)) {
705                         u8 *hyst = &ch_reg->hyst;
706
707                         if (val > IQS269_CHx_HYST_MAX) {
708                                 dev_err(&client->dev,
709                                         "Invalid channel %u hysteresis: %u\n",
710                                         reg, val);
711                                 return -EINVAL;
712                         }
713
714                         if (i == IQS269_EVENT_DEEP_DN ||
715                             i == IQS269_EVENT_DEEP_UP) {
716                                 *hyst &= ~IQS269_CHx_HYST_DEEP_MASK;
717                                 *hyst |= (val << IQS269_CHx_HYST_DEEP_SHIFT);
718                         } else if (i == IQS269_EVENT_TOUCH_DN ||
719                                    i == IQS269_EVENT_TOUCH_UP) {
720                                 *hyst &= ~IQS269_CHx_HYST_TOUCH_MASK;
721                                 *hyst |= val;
722                         }
723                 }
724
725                 if (fwnode_property_read_u32(ev_node, "linux,code", &val))
726                         continue;
727
728                 switch (reg) {
729                 case IQS269_CHx_HALL_ACTIVE:
730                         if (iqs269->hall_enable) {
731                                 iqs269->switches[i].code = val;
732                                 iqs269->switches[i].enabled = true;
733                         }
734
735                         /* fall through */
736
737                 case IQS269_CHx_HALL_INACTIVE:
738                         if (iqs269->hall_enable)
739                                 break;
740
741                         /* fall through */
742
743                 default:
744                         iqs269->keycode[i * IQS269_NUM_CH + reg] = val;
745                 }
746
747                 iqs269->sys_reg.event_mask &= ~iqs269_events[i].mask;
748         }
749
750         return 0;
751 }
752
753 static int iqs269_parse_prop(struct iqs269_private *iqs269)
754 {
755         struct iqs269_sys_reg *sys_reg = &iqs269->sys_reg;
756         struct i2c_client *client = iqs269->client;
757         struct fwnode_handle *ch_node;
758         u16 general, misc_a, misc_b;
759         unsigned int val;
760         int error;
761
762         iqs269->hall_enable = device_property_present(&client->dev,
763                                                       "azoteq,hall-enable");
764
765         if (!device_property_read_u32(&client->dev, "azoteq,suspend-mode",
766                                       &val)) {
767                 if (val > IQS269_SYS_SETTINGS_PWR_MODE_MAX) {
768                         dev_err(&client->dev, "Invalid suspend mode: %u\n",
769                                 val);
770                         return -EINVAL;
771                 }
772
773                 iqs269->suspend_mode = val;
774         }
775
776         error = regmap_raw_read(iqs269->regmap, IQS269_SYS_SETTINGS, sys_reg,
777                                 sizeof(*sys_reg));
778         if (error)
779                 return error;
780
781         if (!device_property_read_u32(&client->dev, "azoteq,filt-str-lp-lta",
782                                       &val)) {
783                 if (val > IQS269_FILT_STR_MAX) {
784                         dev_err(&client->dev, "Invalid filter strength: %u\n",
785                                 val);
786                         return -EINVAL;
787                 }
788
789                 sys_reg->filter &= ~IQS269_FILT_STR_LP_LTA_MASK;
790                 sys_reg->filter |= (val << IQS269_FILT_STR_LP_LTA_SHIFT);
791         }
792
793         if (!device_property_read_u32(&client->dev, "azoteq,filt-str-lp-cnt",
794                                       &val)) {
795                 if (val > IQS269_FILT_STR_MAX) {
796                         dev_err(&client->dev, "Invalid filter strength: %u\n",
797                                 val);
798                         return -EINVAL;
799                 }
800
801                 sys_reg->filter &= ~IQS269_FILT_STR_LP_CNT_MASK;
802                 sys_reg->filter |= (val << IQS269_FILT_STR_LP_CNT_SHIFT);
803         }
804
805         if (!device_property_read_u32(&client->dev, "azoteq,filt-str-np-lta",
806                                       &val)) {
807                 if (val > IQS269_FILT_STR_MAX) {
808                         dev_err(&client->dev, "Invalid filter strength: %u\n",
809                                 val);
810                         return -EINVAL;
811                 }
812
813                 sys_reg->filter &= ~IQS269_FILT_STR_NP_LTA_MASK;
814                 sys_reg->filter |= (val << IQS269_FILT_STR_NP_LTA_SHIFT);
815         }
816
817         if (!device_property_read_u32(&client->dev, "azoteq,filt-str-np-cnt",
818                                       &val)) {
819                 if (val > IQS269_FILT_STR_MAX) {
820                         dev_err(&client->dev, "Invalid filter strength: %u\n",
821                                 val);
822                         return -EINVAL;
823                 }
824
825                 sys_reg->filter &= ~IQS269_FILT_STR_NP_CNT_MASK;
826                 sys_reg->filter |= val;
827         }
828
829         if (!device_property_read_u32(&client->dev, "azoteq,rate-np-ms",
830                                       &val)) {
831                 if (val > IQS269_RATE_NP_MS_MAX) {
832                         dev_err(&client->dev, "Invalid report rate: %u\n", val);
833                         return -EINVAL;
834                 }
835
836                 sys_reg->rate_np = val;
837         }
838
839         if (!device_property_read_u32(&client->dev, "azoteq,rate-lp-ms",
840                                       &val)) {
841                 if (val > IQS269_RATE_LP_MS_MAX) {
842                         dev_err(&client->dev, "Invalid report rate: %u\n", val);
843                         return -EINVAL;
844                 }
845
846                 sys_reg->rate_lp = val;
847         }
848
849         if (!device_property_read_u32(&client->dev, "azoteq,rate-ulp-ms",
850                                       &val)) {
851                 if (val > IQS269_RATE_ULP_MS_MAX) {
852                         dev_err(&client->dev, "Invalid report rate: %u\n", val);
853                         return -EINVAL;
854                 }
855
856                 sys_reg->rate_ulp = val / 16;
857         }
858
859         if (!device_property_read_u32(&client->dev, "azoteq,timeout-pwr-ms",
860                                       &val)) {
861                 if (val > IQS269_TIMEOUT_PWR_MS_MAX) {
862                         dev_err(&client->dev, "Invalid timeout: %u\n", val);
863                         return -EINVAL;
864                 }
865
866                 sys_reg->timeout_pwr = val / 512;
867         }
868
869         if (!device_property_read_u32(&client->dev, "azoteq,timeout-lta-ms",
870                                       &val)) {
871                 if (val > IQS269_TIMEOUT_LTA_MS_MAX) {
872                         dev_err(&client->dev, "Invalid timeout: %u\n", val);
873                         return -EINVAL;
874                 }
875
876                 sys_reg->timeout_lta = val / 512;
877         }
878
879         misc_a = be16_to_cpu(sys_reg->misc_a);
880         misc_b = be16_to_cpu(sys_reg->misc_b);
881
882         misc_a &= ~IQS269_MISC_A_ATI_BAND_DISABLE;
883         if (device_property_present(&client->dev, "azoteq,ati-band-disable"))
884                 misc_a |= IQS269_MISC_A_ATI_BAND_DISABLE;
885
886         misc_a &= ~IQS269_MISC_A_ATI_LP_ONLY;
887         if (device_property_present(&client->dev, "azoteq,ati-lp-only"))
888                 misc_a |= IQS269_MISC_A_ATI_LP_ONLY;
889
890         misc_a &= ~IQS269_MISC_A_ATI_BAND_TIGHTEN;
891         if (device_property_present(&client->dev, "azoteq,ati-band-tighten"))
892                 misc_a |= IQS269_MISC_A_ATI_BAND_TIGHTEN;
893
894         misc_a &= ~IQS269_MISC_A_FILT_DISABLE;
895         if (device_property_present(&client->dev, "azoteq,filt-disable"))
896                 misc_a |= IQS269_MISC_A_FILT_DISABLE;
897
898         if (!device_property_read_u32(&client->dev, "azoteq,gpio3-select",
899                                       &val)) {
900                 if (val >= IQS269_NUM_CH) {
901                         dev_err(&client->dev, "Invalid GPIO3 selection: %u\n",
902                                 val);
903                         return -EINVAL;
904                 }
905
906                 misc_a &= ~IQS269_MISC_A_GPIO3_SELECT_MASK;
907                 misc_a |= (val << IQS269_MISC_A_GPIO3_SELECT_SHIFT);
908         }
909
910         misc_a &= ~IQS269_MISC_A_DUAL_DIR;
911         if (device_property_present(&client->dev, "azoteq,dual-direction"))
912                 misc_a |= IQS269_MISC_A_DUAL_DIR;
913
914         if (!device_property_read_u32(&client->dev, "azoteq,tx-freq", &val)) {
915                 if (val > IQS269_MISC_A_TX_FREQ_MAX) {
916                         dev_err(&client->dev,
917                                 "Invalid excitation frequency: %u\n", val);
918                         return -EINVAL;
919                 }
920
921                 misc_a &= ~IQS269_MISC_A_TX_FREQ_MASK;
922                 misc_a |= (val << IQS269_MISC_A_TX_FREQ_SHIFT);
923         }
924
925         misc_a &= ~IQS269_MISC_A_GLOBAL_CAP_SIZE;
926         if (device_property_present(&client->dev, "azoteq,global-cap-increase"))
927                 misc_a |= IQS269_MISC_A_GLOBAL_CAP_SIZE;
928
929         if (!device_property_read_u32(&client->dev, "azoteq,reseed-select",
930                                       &val)) {
931                 if (val > IQS269_MISC_B_RESEED_UI_SEL_MAX) {
932                         dev_err(&client->dev, "Invalid reseed selection: %u\n",
933                                 val);
934                         return -EINVAL;
935                 }
936
937                 misc_b &= ~IQS269_MISC_B_RESEED_UI_SEL_MASK;
938                 misc_b |= (val << IQS269_MISC_B_RESEED_UI_SEL_SHIFT);
939         }
940
941         misc_b &= ~IQS269_MISC_B_TRACKING_UI_ENABLE;
942         if (device_property_present(&client->dev, "azoteq,tracking-enable"))
943                 misc_b |= IQS269_MISC_B_TRACKING_UI_ENABLE;
944
945         if (!device_property_read_u32(&client->dev, "azoteq,filt-str-slider",
946                                       &val)) {
947                 if (val > IQS269_FILT_STR_MAX) {
948                         dev_err(&client->dev, "Invalid filter strength: %u\n",
949                                 val);
950                         return -EINVAL;
951                 }
952
953                 misc_b &= ~IQS269_MISC_B_FILT_STR_SLIDER;
954                 misc_b |= val;
955         }
956
957         sys_reg->misc_a = cpu_to_be16(misc_a);
958         sys_reg->misc_b = cpu_to_be16(misc_b);
959
960         sys_reg->active = 0;
961         sys_reg->reseed = 0;
962
963         sys_reg->blocking = 0;
964
965         sys_reg->slider_select[0] = 0;
966         sys_reg->slider_select[1] = 0;
967
968         sys_reg->event_mask = ~((u8)IQS269_EVENT_MASK_SYS);
969
970         device_for_each_child_node(&client->dev, ch_node) {
971                 error = iqs269_parse_chan(iqs269, ch_node);
972                 if (error) {
973                         fwnode_handle_put(ch_node);
974                         return error;
975                 }
976         }
977
978         /*
979          * Volunteer all active channels to participate in ATI when REDO-ATI is
980          * manually triggered.
981          */
982         sys_reg->redo_ati = sys_reg->active;
983
984         general = be16_to_cpu(sys_reg->general);
985
986         if (device_property_present(&client->dev, "azoteq,clk-div")) {
987                 general |= IQS269_SYS_SETTINGS_CLK_DIV;
988                 iqs269->delay_mult = 4;
989         } else {
990                 general &= ~IQS269_SYS_SETTINGS_CLK_DIV;
991                 iqs269->delay_mult = 1;
992         }
993
994         /*
995          * Configure the device to automatically switch between normal and low-
996          * power modes as a function of sensing activity. Ultra-low-power mode,
997          * if enabled, is reserved for suspend.
998          */
999         general &= ~IQS269_SYS_SETTINGS_ULP_AUTO;
1000         general &= ~IQS269_SYS_SETTINGS_DIS_AUTO;
1001         general &= ~IQS269_SYS_SETTINGS_PWR_MODE_MASK;
1002
1003         if (!device_property_read_u32(&client->dev, "azoteq,ulp-update",
1004                                       &val)) {
1005                 if (val > IQS269_SYS_SETTINGS_ULP_UPDATE_MAX) {
1006                         dev_err(&client->dev, "Invalid update rate: %u\n", val);
1007                         return -EINVAL;
1008                 }
1009
1010                 general &= ~IQS269_SYS_SETTINGS_ULP_UPDATE_MASK;
1011                 general |= (val << IQS269_SYS_SETTINGS_ULP_UPDATE_SHIFT);
1012         }
1013
1014         general &= ~IQS269_SYS_SETTINGS_RESEED_OFFSET;
1015         if (device_property_present(&client->dev, "azoteq,reseed-offset"))
1016                 general |= IQS269_SYS_SETTINGS_RESEED_OFFSET;
1017
1018         general |= IQS269_SYS_SETTINGS_EVENT_MODE;
1019
1020         /*
1021          * As per the datasheet, enable streaming during normal-power mode if
1022          * either slider is in use. In that case, the device returns to event
1023          * mode during low-power mode.
1024          */
1025         if (sys_reg->slider_select[0] || sys_reg->slider_select[1])
1026                 general |= IQS269_SYS_SETTINGS_EVENT_MODE_LP;
1027
1028         general |= IQS269_SYS_SETTINGS_REDO_ATI;
1029         general |= IQS269_SYS_SETTINGS_ACK_RESET;
1030
1031         sys_reg->general = cpu_to_be16(general);
1032
1033         return 0;
1034 }
1035
1036 static int iqs269_dev_init(struct iqs269_private *iqs269)
1037 {
1038         struct iqs269_sys_reg *sys_reg = &iqs269->sys_reg;
1039         struct iqs269_ch_reg *ch_reg;
1040         unsigned int val;
1041         int error, i;
1042
1043         mutex_lock(&iqs269->lock);
1044
1045         error = regmap_update_bits(iqs269->regmap, IQS269_HALL_UI,
1046                                    IQS269_HALL_UI_ENABLE,
1047                                    iqs269->hall_enable ? ~0 : 0);
1048         if (error)
1049                 goto err_mutex;
1050
1051         for (i = 0; i < IQS269_NUM_CH; i++) {
1052                 if (!(sys_reg->active & BIT(i)))
1053                         continue;
1054
1055                 ch_reg = &iqs269->ch_reg[i];
1056
1057                 error = regmap_raw_write(iqs269->regmap,
1058                                          IQS269_CHx_SETTINGS + i *
1059                                          sizeof(*ch_reg) / 2, ch_reg,
1060                                          sizeof(*ch_reg));
1061                 if (error)
1062                         goto err_mutex;
1063         }
1064
1065         /*
1066          * The REDO-ATI and ATI channel selection fields must be written in the
1067          * same block write, so every field between registers 0x80 through 0x8B
1068          * (inclusive) must be written as well.
1069          */
1070         error = regmap_raw_write(iqs269->regmap, IQS269_SYS_SETTINGS, sys_reg,
1071                                  sizeof(*sys_reg));
1072         if (error)
1073                 goto err_mutex;
1074
1075         error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
1076                                         !(val & IQS269_SYS_FLAGS_IN_ATI),
1077                                          IQS269_ATI_POLL_SLEEP_US,
1078                                          IQS269_ATI_POLL_TIMEOUT_US);
1079         if (error)
1080                 goto err_mutex;
1081
1082         msleep(IQS269_ATI_STABLE_DELAY_MS);
1083         iqs269->ati_current = true;
1084
1085 err_mutex:
1086         mutex_unlock(&iqs269->lock);
1087
1088         return error;
1089 }
1090
1091 static int iqs269_input_init(struct iqs269_private *iqs269)
1092 {
1093         struct i2c_client *client = iqs269->client;
1094         struct iqs269_flags flags;
1095         unsigned int sw_code, keycode;
1096         int error, i, j;
1097         u8 dir_mask, state;
1098
1099         iqs269->keypad = devm_input_allocate_device(&client->dev);
1100         if (!iqs269->keypad)
1101                 return -ENOMEM;
1102
1103         iqs269->keypad->keycodemax = ARRAY_SIZE(iqs269->keycode);
1104         iqs269->keypad->keycode = iqs269->keycode;
1105         iqs269->keypad->keycodesize = sizeof(*iqs269->keycode);
1106
1107         iqs269->keypad->name = "iqs269a_keypad";
1108         iqs269->keypad->id.bustype = BUS_I2C;
1109
1110         if (iqs269->hall_enable) {
1111                 error = regmap_raw_read(iqs269->regmap, IQS269_SYS_FLAGS,
1112                                         &flags, sizeof(flags));
1113                 if (error) {
1114                         dev_err(&client->dev,
1115                                 "Failed to read initial status: %d\n", error);
1116                         return error;
1117                 }
1118         }
1119
1120         for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
1121                 dir_mask = flags.states[IQS269_ST_OFFS_DIR];
1122                 if (!iqs269_events[i].dir_up)
1123                         dir_mask = ~dir_mask;
1124
1125                 state = flags.states[iqs269_events[i].st_offs] & dir_mask;
1126
1127                 sw_code = iqs269->switches[i].code;
1128
1129                 for (j = 0; j < IQS269_NUM_CH; j++) {
1130                         keycode = iqs269->keycode[i * IQS269_NUM_CH + j];
1131
1132                         /*
1133                          * Hall-effect sensing repurposes a pair of dedicated
1134                          * channels, only one of which reports events.
1135                          */
1136                         switch (j) {
1137                         case IQS269_CHx_HALL_ACTIVE:
1138                                 if (iqs269->hall_enable &&
1139                                     iqs269->switches[i].enabled) {
1140                                         input_set_capability(iqs269->keypad,
1141                                                              EV_SW, sw_code);
1142                                         input_report_switch(iqs269->keypad,
1143                                                             sw_code,
1144                                                             state & BIT(j));
1145                                 }
1146
1147                                 /* fall through */
1148
1149                         case IQS269_CHx_HALL_INACTIVE:
1150                                 if (iqs269->hall_enable)
1151                                         continue;
1152
1153                                 /* fall through */
1154
1155                         default:
1156                                 if (keycode != KEY_RESERVED)
1157                                         input_set_capability(iqs269->keypad,
1158                                                              EV_KEY, keycode);
1159                         }
1160                 }
1161         }
1162
1163         input_sync(iqs269->keypad);
1164
1165         error = input_register_device(iqs269->keypad);
1166         if (error) {
1167                 dev_err(&client->dev, "Failed to register keypad: %d\n", error);
1168                 return error;
1169         }
1170
1171         for (i = 0; i < IQS269_NUM_SL; i++) {
1172                 if (!iqs269->sys_reg.slider_select[i])
1173                         continue;
1174
1175                 iqs269->slider[i] = devm_input_allocate_device(&client->dev);
1176                 if (!iqs269->slider[i])
1177                         return -ENOMEM;
1178
1179                 iqs269->slider[i]->name = i ? "iqs269a_slider_1"
1180                                             : "iqs269a_slider_0";
1181                 iqs269->slider[i]->id.bustype = BUS_I2C;
1182
1183                 input_set_capability(iqs269->slider[i], EV_KEY, BTN_TOUCH);
1184                 input_set_abs_params(iqs269->slider[i], ABS_X, 0, 255, 0, 0);
1185
1186                 error = input_register_device(iqs269->slider[i]);
1187                 if (error) {
1188                         dev_err(&client->dev,
1189                                 "Failed to register slider %d: %d\n", i, error);
1190                         return error;
1191                 }
1192         }
1193
1194         return 0;
1195 }
1196
1197 static int iqs269_report(struct iqs269_private *iqs269)
1198 {
1199         struct i2c_client *client = iqs269->client;
1200         struct iqs269_flags flags;
1201         unsigned int sw_code, keycode;
1202         int error, i, j;
1203         u8 slider_x[IQS269_NUM_SL];
1204         u8 dir_mask, state;
1205
1206         error = regmap_raw_read(iqs269->regmap, IQS269_SYS_FLAGS, &flags,
1207                                 sizeof(flags));
1208         if (error) {
1209                 dev_err(&client->dev, "Failed to read device status: %d\n",
1210                         error);
1211                 return error;
1212         }
1213
1214         /*
1215          * The device resets itself if its own watchdog bites, which can happen
1216          * in the event of an I2C communication error. In this case, the device
1217          * asserts a SHOW_RESET interrupt and all registers must be restored.
1218          */
1219         if (be16_to_cpu(flags.system) & IQS269_SYS_FLAGS_SHOW_RESET) {
1220                 dev_err(&client->dev, "Unexpected device reset\n");
1221
1222                 error = iqs269_dev_init(iqs269);
1223                 if (error)
1224                         dev_err(&client->dev,
1225                                 "Failed to re-initialize device: %d\n", error);
1226
1227                 return error;
1228         }
1229
1230         error = regmap_raw_read(iqs269->regmap, IQS269_SLIDER_X, slider_x,
1231                                 sizeof(slider_x));
1232         if (error) {
1233                 dev_err(&client->dev, "Failed to read slider position: %d\n",
1234                         error);
1235                 return error;
1236         }
1237
1238         for (i = 0; i < IQS269_NUM_SL; i++) {
1239                 if (!iqs269->sys_reg.slider_select[i])
1240                         continue;
1241
1242                 /*
1243                  * Report BTN_TOUCH if any channel that participates in the
1244                  * slider is in a state of touch.
1245                  */
1246                 if (flags.states[IQS269_ST_OFFS_TOUCH] &
1247                     iqs269->sys_reg.slider_select[i]) {
1248                         input_report_key(iqs269->slider[i], BTN_TOUCH, 1);
1249                         input_report_abs(iqs269->slider[i], ABS_X, slider_x[i]);
1250                 } else {
1251                         input_report_key(iqs269->slider[i], BTN_TOUCH, 0);
1252                 }
1253
1254                 input_sync(iqs269->slider[i]);
1255         }
1256
1257         for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
1258                 dir_mask = flags.states[IQS269_ST_OFFS_DIR];
1259                 if (!iqs269_events[i].dir_up)
1260                         dir_mask = ~dir_mask;
1261
1262                 state = flags.states[iqs269_events[i].st_offs] & dir_mask;
1263
1264                 sw_code = iqs269->switches[i].code;
1265
1266                 for (j = 0; j < IQS269_NUM_CH; j++) {
1267                         keycode = iqs269->keycode[i * IQS269_NUM_CH + j];
1268
1269                         switch (j) {
1270                         case IQS269_CHx_HALL_ACTIVE:
1271                                 if (iqs269->hall_enable &&
1272                                     iqs269->switches[i].enabled)
1273                                         input_report_switch(iqs269->keypad,
1274                                                             sw_code,
1275                                                             state & BIT(j));
1276
1277                                 /* fall through */
1278
1279                         case IQS269_CHx_HALL_INACTIVE:
1280                                 if (iqs269->hall_enable)
1281                                         continue;
1282
1283                                 /* fall through */
1284
1285                         default:
1286                                 input_report_key(iqs269->keypad, keycode,
1287                                                  state & BIT(j));
1288                         }
1289                 }
1290         }
1291
1292         input_sync(iqs269->keypad);
1293
1294         return 0;
1295 }
1296
1297 static irqreturn_t iqs269_irq(int irq, void *context)
1298 {
1299         struct iqs269_private *iqs269 = context;
1300
1301         if (iqs269_report(iqs269))
1302                 return IRQ_NONE;
1303
1304         /*
1305          * The device does not deassert its interrupt (RDY) pin until shortly
1306          * after receiving an I2C stop condition; the following delay ensures
1307          * the interrupt handler does not return before this time.
1308          */
1309         iqs269_irq_wait();
1310
1311         return IRQ_HANDLED;
1312 }
1313
1314 static ssize_t counts_show(struct device *dev,
1315                            struct device_attribute *attr, char *buf)
1316 {
1317         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1318         struct i2c_client *client = iqs269->client;
1319         __le16 counts;
1320         int error;
1321
1322         if (!iqs269->ati_current || iqs269->hall_enable)
1323                 return -EPERM;
1324
1325         /*
1326          * Unsolicited I2C communication prompts the device to assert its RDY
1327          * pin, so disable the interrupt line until the operation is finished
1328          * and RDY has been deasserted.
1329          */
1330         disable_irq(client->irq);
1331
1332         error = regmap_raw_read(iqs269->regmap,
1333                                 IQS269_CHx_COUNTS + iqs269->ch_num * 2,
1334                                 &counts, sizeof(counts));
1335
1336         iqs269_irq_wait();
1337         enable_irq(client->irq);
1338
1339         if (error)
1340                 return error;
1341
1342         return scnprintf(buf, PAGE_SIZE, "%u\n", le16_to_cpu(counts));
1343 }
1344
1345 static ssize_t hall_bin_show(struct device *dev,
1346                              struct device_attribute *attr, char *buf)
1347 {
1348         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1349         struct i2c_client *client = iqs269->client;
1350         unsigned int val;
1351         int error;
1352
1353         disable_irq(client->irq);
1354
1355         error = regmap_read(iqs269->regmap, IQS269_CAL_DATA_A, &val);
1356
1357         iqs269_irq_wait();
1358         enable_irq(client->irq);
1359
1360         if (error)
1361                 return error;
1362
1363         switch (iqs269->ch_reg[IQS269_CHx_HALL_ACTIVE].rx_enable &
1364                 iqs269->ch_reg[IQS269_CHx_HALL_INACTIVE].rx_enable) {
1365         case IQS269_HALL_PAD_R:
1366                 val &= IQS269_CAL_DATA_A_HALL_BIN_R_MASK;
1367                 val >>= IQS269_CAL_DATA_A_HALL_BIN_R_SHIFT;
1368                 break;
1369
1370         case IQS269_HALL_PAD_L:
1371                 val &= IQS269_CAL_DATA_A_HALL_BIN_L_MASK;
1372                 val >>= IQS269_CAL_DATA_A_HALL_BIN_L_SHIFT;
1373                 break;
1374
1375         default:
1376                 return -EINVAL;
1377         }
1378
1379         return scnprintf(buf, PAGE_SIZE, "%u\n", val);
1380 }
1381
1382 static ssize_t hall_enable_show(struct device *dev,
1383                                 struct device_attribute *attr, char *buf)
1384 {
1385         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1386
1387         return scnprintf(buf, PAGE_SIZE, "%u\n", iqs269->hall_enable);
1388 }
1389
1390 static ssize_t hall_enable_store(struct device *dev,
1391                                  struct device_attribute *attr, const char *buf,
1392                                  size_t count)
1393 {
1394         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1395         unsigned int val;
1396         int error;
1397
1398         error = kstrtouint(buf, 10, &val);
1399         if (error)
1400                 return error;
1401
1402         mutex_lock(&iqs269->lock);
1403
1404         iqs269->hall_enable = val;
1405         iqs269->ati_current = false;
1406
1407         mutex_unlock(&iqs269->lock);
1408
1409         return count;
1410 }
1411
1412 static ssize_t ch_number_show(struct device *dev,
1413                               struct device_attribute *attr, char *buf)
1414 {
1415         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1416
1417         return scnprintf(buf, PAGE_SIZE, "%u\n", iqs269->ch_num);
1418 }
1419
1420 static ssize_t ch_number_store(struct device *dev,
1421                                struct device_attribute *attr, const char *buf,
1422                                size_t count)
1423 {
1424         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1425         unsigned int val;
1426         int error;
1427
1428         error = kstrtouint(buf, 10, &val);
1429         if (error)
1430                 return error;
1431
1432         if (val >= IQS269_NUM_CH)
1433                 return -EINVAL;
1434
1435         iqs269->ch_num = val;
1436
1437         return count;
1438 }
1439
1440 static ssize_t rx_enable_show(struct device *dev,
1441                               struct device_attribute *attr, char *buf)
1442 {
1443         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1444
1445         return scnprintf(buf, PAGE_SIZE, "%u\n",
1446                          iqs269->ch_reg[iqs269->ch_num].rx_enable);
1447 }
1448
1449 static ssize_t rx_enable_store(struct device *dev,
1450                                struct device_attribute *attr, const char *buf,
1451                                size_t count)
1452 {
1453         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1454         unsigned int val;
1455         int error;
1456
1457         error = kstrtouint(buf, 10, &val);
1458         if (error)
1459                 return error;
1460
1461         if (val > 0xFF)
1462                 return -EINVAL;
1463
1464         mutex_lock(&iqs269->lock);
1465
1466         iqs269->ch_reg[iqs269->ch_num].rx_enable = val;
1467         iqs269->ati_current = false;
1468
1469         mutex_unlock(&iqs269->lock);
1470
1471         return count;
1472 }
1473
1474 static ssize_t ati_mode_show(struct device *dev,
1475                              struct device_attribute *attr, char *buf)
1476 {
1477         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1478         unsigned int val;
1479         int error;
1480
1481         error = iqs269_ati_mode_get(iqs269, iqs269->ch_num, &val);
1482         if (error)
1483                 return error;
1484
1485         return scnprintf(buf, PAGE_SIZE, "%u\n", val);
1486 }
1487
1488 static ssize_t ati_mode_store(struct device *dev,
1489                               struct device_attribute *attr, const char *buf,
1490                               size_t count)
1491 {
1492         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1493         unsigned int val;
1494         int error;
1495
1496         error = kstrtouint(buf, 10, &val);
1497         if (error)
1498                 return error;
1499
1500         error = iqs269_ati_mode_set(iqs269, iqs269->ch_num, val);
1501         if (error)
1502                 return error;
1503
1504         return count;
1505 }
1506
1507 static ssize_t ati_base_show(struct device *dev,
1508                              struct device_attribute *attr, char *buf)
1509 {
1510         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1511         unsigned int val;
1512         int error;
1513
1514         error = iqs269_ati_base_get(iqs269, iqs269->ch_num, &val);
1515         if (error)
1516                 return error;
1517
1518         return scnprintf(buf, PAGE_SIZE, "%u\n", val);
1519 }
1520
1521 static ssize_t ati_base_store(struct device *dev,
1522                               struct device_attribute *attr, const char *buf,
1523                               size_t count)
1524 {
1525         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1526         unsigned int val;
1527         int error;
1528
1529         error = kstrtouint(buf, 10, &val);
1530         if (error)
1531                 return error;
1532
1533         error = iqs269_ati_base_set(iqs269, iqs269->ch_num, val);
1534         if (error)
1535                 return error;
1536
1537         return count;
1538 }
1539
1540 static ssize_t ati_target_show(struct device *dev,
1541                                struct device_attribute *attr, char *buf)
1542 {
1543         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1544         unsigned int val;
1545         int error;
1546
1547         error = iqs269_ati_target_get(iqs269, iqs269->ch_num, &val);
1548         if (error)
1549                 return error;
1550
1551         return scnprintf(buf, PAGE_SIZE, "%u\n", val);
1552 }
1553
1554 static ssize_t ati_target_store(struct device *dev,
1555                                 struct device_attribute *attr, const char *buf,
1556                                 size_t count)
1557 {
1558         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1559         unsigned int val;
1560         int error;
1561
1562         error = kstrtouint(buf, 10, &val);
1563         if (error)
1564                 return error;
1565
1566         error = iqs269_ati_target_set(iqs269, iqs269->ch_num, val);
1567         if (error)
1568                 return error;
1569
1570         return count;
1571 }
1572
1573 static ssize_t ati_trigger_show(struct device *dev,
1574                                 struct device_attribute *attr, char *buf)
1575 {
1576         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1577
1578         return scnprintf(buf, PAGE_SIZE, "%u\n", iqs269->ati_current);
1579 }
1580
1581 static ssize_t ati_trigger_store(struct device *dev,
1582                                  struct device_attribute *attr, const char *buf,
1583                                  size_t count)
1584 {
1585         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1586         struct i2c_client *client = iqs269->client;
1587         unsigned int val;
1588         int error;
1589
1590         error = kstrtouint(buf, 10, &val);
1591         if (error)
1592                 return error;
1593
1594         if (!val)
1595                 return count;
1596
1597         disable_irq(client->irq);
1598
1599         error = iqs269_dev_init(iqs269);
1600
1601         iqs269_irq_wait();
1602         enable_irq(client->irq);
1603
1604         if (error)
1605                 return error;
1606
1607         return count;
1608 }
1609
1610 static DEVICE_ATTR_RO(counts);
1611 static DEVICE_ATTR_RO(hall_bin);
1612 static DEVICE_ATTR_RW(hall_enable);
1613 static DEVICE_ATTR_RW(ch_number);
1614 static DEVICE_ATTR_RW(rx_enable);
1615 static DEVICE_ATTR_RW(ati_mode);
1616 static DEVICE_ATTR_RW(ati_base);
1617 static DEVICE_ATTR_RW(ati_target);
1618 static DEVICE_ATTR_RW(ati_trigger);
1619
1620 static struct attribute *iqs269_attrs[] = {
1621         &dev_attr_counts.attr,
1622         &dev_attr_hall_bin.attr,
1623         &dev_attr_hall_enable.attr,
1624         &dev_attr_ch_number.attr,
1625         &dev_attr_rx_enable.attr,
1626         &dev_attr_ati_mode.attr,
1627         &dev_attr_ati_base.attr,
1628         &dev_attr_ati_target.attr,
1629         &dev_attr_ati_trigger.attr,
1630         NULL,
1631 };
1632
1633 static const struct attribute_group iqs269_attr_group = {
1634         .attrs = iqs269_attrs,
1635 };
1636
1637 static const struct regmap_config iqs269_regmap_config = {
1638         .reg_bits = 8,
1639         .val_bits = 16,
1640         .max_register = IQS269_MAX_REG,
1641 };
1642
1643 static int iqs269_probe(struct i2c_client *client)
1644 {
1645         struct iqs269_ver_info ver_info;
1646         struct iqs269_private *iqs269;
1647         int error;
1648
1649         iqs269 = devm_kzalloc(&client->dev, sizeof(*iqs269), GFP_KERNEL);
1650         if (!iqs269)
1651                 return -ENOMEM;
1652
1653         i2c_set_clientdata(client, iqs269);
1654         iqs269->client = client;
1655
1656         iqs269->regmap = devm_regmap_init_i2c(client, &iqs269_regmap_config);
1657         if (IS_ERR(iqs269->regmap)) {
1658                 error = PTR_ERR(iqs269->regmap);
1659                 dev_err(&client->dev, "Failed to initialize register map: %d\n",
1660                         error);
1661                 return error;
1662         }
1663
1664         mutex_init(&iqs269->lock);
1665
1666         error = regmap_raw_read(iqs269->regmap, IQS269_VER_INFO, &ver_info,
1667                                 sizeof(ver_info));
1668         if (error)
1669                 return error;
1670
1671         if (ver_info.prod_num != IQS269_VER_INFO_PROD_NUM) {
1672                 dev_err(&client->dev, "Unrecognized product number: 0x%02X\n",
1673                         ver_info.prod_num);
1674                 return -EINVAL;
1675         }
1676
1677         error = iqs269_parse_prop(iqs269);
1678         if (error)
1679                 return error;
1680
1681         error = iqs269_dev_init(iqs269);
1682         if (error) {
1683                 dev_err(&client->dev, "Failed to initialize device: %d\n",
1684                         error);
1685                 return error;
1686         }
1687
1688         error = iqs269_input_init(iqs269);
1689         if (error)
1690                 return error;
1691
1692         error = devm_request_threaded_irq(&client->dev, client->irq,
1693                                           NULL, iqs269_irq, IRQF_ONESHOT,
1694                                           client->name, iqs269);
1695         if (error) {
1696                 dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
1697                 return error;
1698         }
1699
1700         error = devm_device_add_group(&client->dev, &iqs269_attr_group);
1701         if (error)
1702                 dev_err(&client->dev, "Failed to add attributes: %d\n", error);
1703
1704         return error;
1705 }
1706
1707 static int __maybe_unused iqs269_suspend(struct device *dev)
1708 {
1709         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1710         struct i2c_client *client = iqs269->client;
1711         unsigned int val;
1712         int error;
1713
1714         if (!iqs269->suspend_mode)
1715                 return 0;
1716
1717         disable_irq(client->irq);
1718
1719         /*
1720          * Automatic power mode switching must be disabled before the device is
1721          * forced into any particular power mode. In this case, the device will
1722          * transition into normal-power mode.
1723          */
1724         error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS,
1725                                    IQS269_SYS_SETTINGS_DIS_AUTO, ~0);
1726         if (error)
1727                 goto err_irq;
1728
1729         /*
1730          * The following check ensures the device has completed its transition
1731          * into normal-power mode before a manual mode switch is performed.
1732          */
1733         error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
1734                                         !(val & IQS269_SYS_FLAGS_PWR_MODE_MASK),
1735                                          IQS269_PWR_MODE_POLL_SLEEP_US,
1736                                          IQS269_PWR_MODE_POLL_TIMEOUT_US);
1737         if (error)
1738                 goto err_irq;
1739
1740         error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS,
1741                                    IQS269_SYS_SETTINGS_PWR_MODE_MASK,
1742                                    iqs269->suspend_mode <<
1743                                    IQS269_SYS_SETTINGS_PWR_MODE_SHIFT);
1744         if (error)
1745                 goto err_irq;
1746
1747         /*
1748          * This last check ensures the device has completed its transition into
1749          * the desired power mode to prevent any spurious interrupts from being
1750          * triggered after iqs269_suspend has already returned.
1751          */
1752         error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
1753                                          (val & IQS269_SYS_FLAGS_PWR_MODE_MASK)
1754                                          == (iqs269->suspend_mode <<
1755                                              IQS269_SYS_FLAGS_PWR_MODE_SHIFT),
1756                                          IQS269_PWR_MODE_POLL_SLEEP_US,
1757                                          IQS269_PWR_MODE_POLL_TIMEOUT_US);
1758
1759 err_irq:
1760         iqs269_irq_wait();
1761         enable_irq(client->irq);
1762
1763         return error;
1764 }
1765
1766 static int __maybe_unused iqs269_resume(struct device *dev)
1767 {
1768         struct iqs269_private *iqs269 = dev_get_drvdata(dev);
1769         struct i2c_client *client = iqs269->client;
1770         unsigned int val;
1771         int error;
1772
1773         if (!iqs269->suspend_mode)
1774                 return 0;
1775
1776         disable_irq(client->irq);
1777
1778         error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS,
1779                                    IQS269_SYS_SETTINGS_PWR_MODE_MASK, 0);
1780         if (error)
1781                 goto err_irq;
1782
1783         /*
1784          * This check ensures the device has returned to normal-power mode
1785          * before automatic power mode switching is re-enabled.
1786          */
1787         error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
1788                                         !(val & IQS269_SYS_FLAGS_PWR_MODE_MASK),
1789                                          IQS269_PWR_MODE_POLL_SLEEP_US,
1790                                          IQS269_PWR_MODE_POLL_TIMEOUT_US);
1791         if (error)
1792                 goto err_irq;
1793
1794         error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS,
1795                                    IQS269_SYS_SETTINGS_DIS_AUTO, 0);
1796         if (error)
1797                 goto err_irq;
1798
1799         /*
1800          * This step reports any events that may have been "swallowed" as a
1801          * result of polling PWR_MODE (which automatically acknowledges any
1802          * pending interrupts).
1803          */
1804         error = iqs269_report(iqs269);
1805
1806 err_irq:
1807         iqs269_irq_wait();
1808         enable_irq(client->irq);
1809
1810         return error;
1811 }
1812
1813 static SIMPLE_DEV_PM_OPS(iqs269_pm, iqs269_suspend, iqs269_resume);
1814
1815 static const struct of_device_id iqs269_of_match[] = {
1816         { .compatible = "azoteq,iqs269a" },
1817         { }
1818 };
1819 MODULE_DEVICE_TABLE(of, iqs269_of_match);
1820
1821 static struct i2c_driver iqs269_i2c_driver = {
1822         .driver = {
1823                 .name = "iqs269a",
1824                 .of_match_table = iqs269_of_match,
1825                 .pm = &iqs269_pm,
1826         },
1827         .probe_new = iqs269_probe,
1828 };
1829 module_i2c_driver(iqs269_i2c_driver);
1830
1831 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
1832 MODULE_DESCRIPTION("Azoteq IQS269A Capacitive Touch Controller");
1833 MODULE_LICENSE("GPL");