1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Description: keypad driver for ADP5589, ADP5585
4 * I2C QWERTY Keypad and IO Expander
5 * Bugs: Enter bugs at http://blackfin.uclinux.org/
6 *
7 * Copyright (C) 2010-2011 Analog Devices Inc.
8 */
9
10 #include <linux/bitops.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/workqueue.h>
15 #include <linux/errno.h>
16 #include <linux/pm.h>
17 #include <linux/pm_wakeirq.h>
18 #include <linux/platform_device.h>
19 #include <linux/input.h>
20 #include <linux/i2c.h>
21 #include <linux/gpio/driver.h>
22 #include <linux/slab.h>
23
24 #include <linux/input/adp5589.h>
25
26 /* ADP5589/ADP5585 Common Registers */
27 #define ADP5589_5_ID 0x00
28 #define ADP5589_5_INT_STATUS 0x01
29 #define ADP5589_5_STATUS 0x02
30 #define ADP5589_5_FIFO_1 0x03
31 #define ADP5589_5_FIFO_2 0x04
32 #define ADP5589_5_FIFO_3 0x05
33 #define ADP5589_5_FIFO_4 0x06
34 #define ADP5589_5_FIFO_5 0x07
35 #define ADP5589_5_FIFO_6 0x08
36 #define ADP5589_5_FIFO_7 0x09
37 #define ADP5589_5_FIFO_8 0x0A
38 #define ADP5589_5_FIFO_9 0x0B
39 #define ADP5589_5_FIFO_10 0x0C
40 #define ADP5589_5_FIFO_11 0x0D
41 #define ADP5589_5_FIFO_12 0x0E
42 #define ADP5589_5_FIFO_13 0x0F
43 #define ADP5589_5_FIFO_14 0x10
44 #define ADP5589_5_FIFO_15 0x11
45 #define ADP5589_5_FIFO_16 0x12
46 #define ADP5589_5_GPI_INT_STAT_A 0x13
47 #define ADP5589_5_GPI_INT_STAT_B 0x14
48
49 /* ADP5589 Registers */
50 #define ADP5589_GPI_INT_STAT_C 0x15
51 #define ADP5589_GPI_STATUS_A 0x16
52 #define ADP5589_GPI_STATUS_B 0x17
53 #define ADP5589_GPI_STATUS_C 0x18
54 #define ADP5589_RPULL_CONFIG_A 0x19
55 #define ADP5589_RPULL_CONFIG_B 0x1A
56 #define ADP5589_RPULL_CONFIG_C 0x1B
57 #define ADP5589_RPULL_CONFIG_D 0x1C
58 #define ADP5589_RPULL_CONFIG_E 0x1D
59 #define ADP5589_GPI_INT_LEVEL_A 0x1E
60 #define ADP5589_GPI_INT_LEVEL_B 0x1F
61 #define ADP5589_GPI_INT_LEVEL_C 0x20
62 #define ADP5589_GPI_EVENT_EN_A 0x21
63 #define ADP5589_GPI_EVENT_EN_B 0x22
64 #define ADP5589_GPI_EVENT_EN_C 0x23
65 #define ADP5589_GPI_INTERRUPT_EN_A 0x24
66 #define ADP5589_GPI_INTERRUPT_EN_B 0x25
67 #define ADP5589_GPI_INTERRUPT_EN_C 0x26
68 #define ADP5589_DEBOUNCE_DIS_A 0x27
69 #define ADP5589_DEBOUNCE_DIS_B 0x28
70 #define ADP5589_DEBOUNCE_DIS_C 0x29
71 #define ADP5589_GPO_DATA_OUT_A 0x2A
72 #define ADP5589_GPO_DATA_OUT_B 0x2B
73 #define ADP5589_GPO_DATA_OUT_C 0x2C
74 #define ADP5589_GPO_OUT_MODE_A 0x2D
75 #define ADP5589_GPO_OUT_MODE_B 0x2E
76 #define ADP5589_GPO_OUT_MODE_C 0x2F
77 #define ADP5589_GPIO_DIRECTION_A 0x30
78 #define ADP5589_GPIO_DIRECTION_B 0x31
79 #define ADP5589_GPIO_DIRECTION_C 0x32
80 #define ADP5589_UNLOCK1 0x33
81 #define ADP5589_UNLOCK2 0x34
82 #define ADP5589_EXT_LOCK_EVENT 0x35
83 #define ADP5589_UNLOCK_TIMERS 0x36
84 #define ADP5589_LOCK_CFG 0x37
85 #define ADP5589_RESET1_EVENT_A 0x38
86 #define ADP5589_RESET1_EVENT_B 0x39
87 #define ADP5589_RESET1_EVENT_C 0x3A
88 #define ADP5589_RESET2_EVENT_A 0x3B
89 #define ADP5589_RESET2_EVENT_B 0x3C
90 #define ADP5589_RESET_CFG 0x3D
91 #define ADP5589_PWM_OFFT_LOW 0x3E
92 #define ADP5589_PWM_OFFT_HIGH 0x3F
93 #define ADP5589_PWM_ONT_LOW 0x40
94 #define ADP5589_PWM_ONT_HIGH 0x41
95 #define ADP5589_PWM_CFG 0x42
96 #define ADP5589_CLOCK_DIV_CFG 0x43
97 #define ADP5589_LOGIC_1_CFG 0x44
98 #define ADP5589_LOGIC_2_CFG 0x45
99 #define ADP5589_LOGIC_FF_CFG 0x46
100 #define ADP5589_LOGIC_INT_EVENT_EN 0x47
101 #define ADP5589_POLL_PTIME_CFG 0x48
102 #define ADP5589_PIN_CONFIG_A 0x49
103 #define ADP5589_PIN_CONFIG_B 0x4A
104 #define ADP5589_PIN_CONFIG_C 0x4B
105 #define ADP5589_PIN_CONFIG_D 0x4C
106 #define ADP5589_GENERAL_CFG 0x4D
107 #define ADP5589_INT_EN 0x4E
108
109 /* ADP5585 Registers */
110 #define ADP5585_GPI_STATUS_A 0x15
111 #define ADP5585_GPI_STATUS_B 0x16
112 #define ADP5585_RPULL_CONFIG_A 0x17
113 #define ADP5585_RPULL_CONFIG_B 0x18
114 #define ADP5585_RPULL_CONFIG_C 0x19
115 #define ADP5585_RPULL_CONFIG_D 0x1A
116 #define ADP5585_GPI_INT_LEVEL_A 0x1B
117 #define ADP5585_GPI_INT_LEVEL_B 0x1C
118 #define ADP5585_GPI_EVENT_EN_A 0x1D
119 #define ADP5585_GPI_EVENT_EN_B 0x1E
120 #define ADP5585_GPI_INTERRUPT_EN_A 0x1F
121 #define ADP5585_GPI_INTERRUPT_EN_B 0x20
122 #define ADP5585_DEBOUNCE_DIS_A 0x21
123 #define ADP5585_DEBOUNCE_DIS_B 0x22
124 #define ADP5585_GPO_DATA_OUT_A 0x23
125 #define ADP5585_GPO_DATA_OUT_B 0x24
126 #define ADP5585_GPO_OUT_MODE_A 0x25
127 #define ADP5585_GPO_OUT_MODE_B 0x26
128 #define ADP5585_GPIO_DIRECTION_A 0x27
129 #define ADP5585_GPIO_DIRECTION_B 0x28
130 #define ADP5585_RESET1_EVENT_A 0x29
131 #define ADP5585_RESET1_EVENT_B 0x2A
132 #define ADP5585_RESET1_EVENT_C 0x2B
133 #define ADP5585_RESET2_EVENT_A 0x2C
134 #define ADP5585_RESET2_EVENT_B 0x2D
135 #define ADP5585_RESET_CFG 0x2E
136 #define ADP5585_PWM_OFFT_LOW 0x2F
137 #define ADP5585_PWM_OFFT_HIGH 0x30
138 #define ADP5585_PWM_ONT_LOW 0x31
139 #define ADP5585_PWM_ONT_HIGH 0x32
140 #define ADP5585_PWM_CFG 0x33
141 #define ADP5585_LOGIC_CFG 0x34
142 #define ADP5585_LOGIC_FF_CFG 0x35
143 #define ADP5585_LOGIC_INT_EVENT_EN 0x36
144 #define ADP5585_POLL_PTIME_CFG 0x37
145 #define ADP5585_PIN_CONFIG_A 0x38
146 #define ADP5585_PIN_CONFIG_B 0x39
147 #define ADP5585_PIN_CONFIG_D 0x3A
148 #define ADP5585_GENERAL_CFG 0x3B
149 #define ADP5585_INT_EN 0x3C
150
151 /* ID Register */
152 #define ADP5589_5_DEVICE_ID_MASK 0xF
153 #define ADP5589_5_MAN_ID_MASK 0xF
154 #define ADP5589_5_MAN_ID_SHIFT 4
155 #define ADP5589_5_MAN_ID 0x02
156
157 /* GENERAL_CFG Register */
158 #define OSC_EN BIT(7)
159 #define CORE_CLK(x) (((x) & 0x3) << 5)
160 #define LCK_TRK_LOGIC BIT(4) /* ADP5589 only */
161 #define LCK_TRK_GPI BIT(3) /* ADP5589 only */
162 #define INT_CFG BIT(1)
163 #define RST_CFG BIT(0)
164
165 /* INT_EN Register */
166 #define LOGIC2_IEN BIT(5) /* ADP5589 only */
167 #define LOGIC1_IEN BIT(4)
168 #define LOCK_IEN BIT(3) /* ADP5589 only */
169 #define OVRFLOW_IEN BIT(2)
170 #define GPI_IEN BIT(1)
171 #define EVENT_IEN BIT(0)
172
173 /* Interrupt Status Register */
174 #define LOGIC2_INT BIT(5) /* ADP5589 only */
175 #define LOGIC1_INT BIT(4)
176 #define LOCK_INT BIT(3) /* ADP5589 only */
177 #define OVRFLOW_INT BIT(2)
178 #define GPI_INT BIT(1)
179 #define EVENT_INT BIT(0)
180
181 /* STATUS Register */
182 #define LOGIC2_STAT BIT(7) /* ADP5589 only */
183 #define LOGIC1_STAT BIT(6)
184 #define LOCK_STAT BIT(5) /* ADP5589 only */
185 #define KEC 0x1F
186
187 /* PIN_CONFIG_D Register */
188 #define C4_EXTEND_CFG BIT(6) /* RESET2 */
189 #define R4_EXTEND_CFG BIT(5) /* RESET1 */
190
191 /* LOCK_CFG */
192 #define LOCK_EN BIT(0)
193
194 #define PTIME_MASK 0x3
195 #define LTIME_MASK 0x3 /* ADP5589 only */
196
197 /* Key Event Register xy */
198 #define KEY_EV_PRESSED BIT(7)
199 #define KEY_EV_MASK 0x7F
200
201 #define KEYP_MAX_EVENT 16
202 #define ADP5589_MAXGPIO 19
203 #define ADP5585_MAXGPIO 11 /* 10 on the ADP5585-01, 11 on ADP5585-02 */
204
205 enum {
206 ADP5589,
207 ADP5585_01,
208 ADP5585_02
209 };
210
211 struct adp_constants {
212 u8 maxgpio;
213 u8 keymapsize;
214 u8 gpi_pin_row_base;
215 u8 gpi_pin_row_end;
216 u8 gpi_pin_col_base;
217 u8 gpi_pin_base;
218 u8 gpi_pin_end;
219 u8 gpimapsize_max;
220 u8 max_row_num;
221 u8 max_col_num;
222 u8 row_mask;
223 u8 col_mask;
224 u8 col_shift;
225 u8 c4_extend_cfg;
226 u8 (*bank) (u8 offset);
227 u8 (*bit) (u8 offset);
228 u8 (*reg) (u8 reg);
229 };
230
231 struct adp5589_kpad {
232 struct i2c_client *client;
233 struct input_dev *input;
234 const struct adp_constants *var;
235 unsigned short keycode[ADP5589_KEYMAPSIZE];
236 const struct adp5589_gpi_map *gpimap;
237 unsigned short gpimapsize;
238 unsigned extend_cfg;
239 bool is_adp5585;
240 bool support_row5;
241 #ifdef CONFIG_GPIOLIB
242 unsigned char gpiomap[ADP5589_MAXGPIO];
243 struct gpio_chip gc;
244 struct mutex gpio_lock; /* Protect cached dir, dat_out */
245 u8 dat_out[3];
246 u8 dir[3];
247 #endif
248 };
249
250 /*
251 * ADP5589 / ADP5585 derivative / variant handling
252 */
253
254
255 /* ADP5589 */
256
adp5589_bank(unsigned char offset)257 static unsigned char adp5589_bank(unsigned char offset)
258 {
259 return offset >> 3;
260 }
261
adp5589_bit(unsigned char offset)262 static unsigned char adp5589_bit(unsigned char offset)
263 {
264 return 1u << (offset & 0x7);
265 }
266
adp5589_reg(unsigned char reg)267 static unsigned char adp5589_reg(unsigned char reg)
268 {
269 return reg;
270 }
271
272 static const struct adp_constants const_adp5589 = {
273 .maxgpio = ADP5589_MAXGPIO,
274 .keymapsize = ADP5589_KEYMAPSIZE,
275 .gpi_pin_row_base = ADP5589_GPI_PIN_ROW_BASE,
276 .gpi_pin_row_end = ADP5589_GPI_PIN_ROW_END,
277 .gpi_pin_col_base = ADP5589_GPI_PIN_COL_BASE,
278 .gpi_pin_base = ADP5589_GPI_PIN_BASE,
279 .gpi_pin_end = ADP5589_GPI_PIN_END,
280 .gpimapsize_max = ADP5589_GPIMAPSIZE_MAX,
281 .c4_extend_cfg = 12,
282 .max_row_num = ADP5589_MAX_ROW_NUM,
283 .max_col_num = ADP5589_MAX_COL_NUM,
284 .row_mask = ADP5589_ROW_MASK,
285 .col_mask = ADP5589_COL_MASK,
286 .col_shift = ADP5589_COL_SHIFT,
287 .bank = adp5589_bank,
288 .bit = adp5589_bit,
289 .reg = adp5589_reg,
290 };
291
292 /* ADP5585 */
293
adp5585_bank(unsigned char offset)294 static unsigned char adp5585_bank(unsigned char offset)
295 {
296 return offset > ADP5585_MAX_ROW_NUM;
297 }
298
adp5585_bit(unsigned char offset)299 static unsigned char adp5585_bit(unsigned char offset)
300 {
301 return (offset > ADP5585_MAX_ROW_NUM) ?
302 1u << (offset - ADP5585_COL_SHIFT) : 1u << offset;
303 }
304
305 static const unsigned char adp5585_reg_lut[] = {
306 [ADP5589_GPI_STATUS_A] = ADP5585_GPI_STATUS_A,
307 [ADP5589_GPI_STATUS_B] = ADP5585_GPI_STATUS_B,
308 [ADP5589_RPULL_CONFIG_A] = ADP5585_RPULL_CONFIG_A,
309 [ADP5589_RPULL_CONFIG_B] = ADP5585_RPULL_CONFIG_B,
310 [ADP5589_RPULL_CONFIG_C] = ADP5585_RPULL_CONFIG_C,
311 [ADP5589_RPULL_CONFIG_D] = ADP5585_RPULL_CONFIG_D,
312 [ADP5589_GPI_INT_LEVEL_A] = ADP5585_GPI_INT_LEVEL_A,
313 [ADP5589_GPI_INT_LEVEL_B] = ADP5585_GPI_INT_LEVEL_B,
314 [ADP5589_GPI_EVENT_EN_A] = ADP5585_GPI_EVENT_EN_A,
315 [ADP5589_GPI_EVENT_EN_B] = ADP5585_GPI_EVENT_EN_B,
316 [ADP5589_GPI_INTERRUPT_EN_A] = ADP5585_GPI_INTERRUPT_EN_A,
317 [ADP5589_GPI_INTERRUPT_EN_B] = ADP5585_GPI_INTERRUPT_EN_B,
318 [ADP5589_DEBOUNCE_DIS_A] = ADP5585_DEBOUNCE_DIS_A,
319 [ADP5589_DEBOUNCE_DIS_B] = ADP5585_DEBOUNCE_DIS_B,
320 [ADP5589_GPO_DATA_OUT_A] = ADP5585_GPO_DATA_OUT_A,
321 [ADP5589_GPO_DATA_OUT_B] = ADP5585_GPO_DATA_OUT_B,
322 [ADP5589_GPO_OUT_MODE_A] = ADP5585_GPO_OUT_MODE_A,
323 [ADP5589_GPO_OUT_MODE_B] = ADP5585_GPO_OUT_MODE_B,
324 [ADP5589_GPIO_DIRECTION_A] = ADP5585_GPIO_DIRECTION_A,
325 [ADP5589_GPIO_DIRECTION_B] = ADP5585_GPIO_DIRECTION_B,
326 [ADP5589_RESET1_EVENT_A] = ADP5585_RESET1_EVENT_A,
327 [ADP5589_RESET1_EVENT_B] = ADP5585_RESET1_EVENT_B,
328 [ADP5589_RESET1_EVENT_C] = ADP5585_RESET1_EVENT_C,
329 [ADP5589_RESET2_EVENT_A] = ADP5585_RESET2_EVENT_A,
330 [ADP5589_RESET2_EVENT_B] = ADP5585_RESET2_EVENT_B,
331 [ADP5589_RESET_CFG] = ADP5585_RESET_CFG,
332 [ADP5589_PWM_OFFT_LOW] = ADP5585_PWM_OFFT_LOW,
333 [ADP5589_PWM_OFFT_HIGH] = ADP5585_PWM_OFFT_HIGH,
334 [ADP5589_PWM_ONT_LOW] = ADP5585_PWM_ONT_LOW,
335 [ADP5589_PWM_ONT_HIGH] = ADP5585_PWM_ONT_HIGH,
336 [ADP5589_PWM_CFG] = ADP5585_PWM_CFG,
337 [ADP5589_LOGIC_1_CFG] = ADP5585_LOGIC_CFG,
338 [ADP5589_LOGIC_FF_CFG] = ADP5585_LOGIC_FF_CFG,
339 [ADP5589_LOGIC_INT_EVENT_EN] = ADP5585_LOGIC_INT_EVENT_EN,
340 [ADP5589_POLL_PTIME_CFG] = ADP5585_POLL_PTIME_CFG,
341 [ADP5589_PIN_CONFIG_A] = ADP5585_PIN_CONFIG_A,
342 [ADP5589_PIN_CONFIG_B] = ADP5585_PIN_CONFIG_B,
343 [ADP5589_PIN_CONFIG_D] = ADP5585_PIN_CONFIG_D,
344 [ADP5589_GENERAL_CFG] = ADP5585_GENERAL_CFG,
345 [ADP5589_INT_EN] = ADP5585_INT_EN,
346 };
347
adp5585_reg(unsigned char reg)348 static unsigned char adp5585_reg(unsigned char reg)
349 {
350 return adp5585_reg_lut[reg];
351 }
352
353 static const struct adp_constants const_adp5585 = {
354 .maxgpio = ADP5585_MAXGPIO,
355 .keymapsize = ADP5585_KEYMAPSIZE,
356 .gpi_pin_row_base = ADP5585_GPI_PIN_ROW_BASE,
357 .gpi_pin_row_end = ADP5585_GPI_PIN_ROW_END,
358 .gpi_pin_col_base = ADP5585_GPI_PIN_COL_BASE,
359 .gpi_pin_base = ADP5585_GPI_PIN_BASE,
360 .gpi_pin_end = ADP5585_GPI_PIN_END,
361 .gpimapsize_max = ADP5585_GPIMAPSIZE_MAX,
362 .c4_extend_cfg = 10,
363 .max_row_num = ADP5585_MAX_ROW_NUM,
364 .max_col_num = ADP5585_MAX_COL_NUM,
365 .row_mask = ADP5585_ROW_MASK,
366 .col_mask = ADP5585_COL_MASK,
367 .col_shift = ADP5585_COL_SHIFT,
368 .bank = adp5585_bank,
369 .bit = adp5585_bit,
370 .reg = adp5585_reg,
371 };
372
adp5589_read(struct i2c_client * client,u8 reg)373 static int adp5589_read(struct i2c_client *client, u8 reg)
374 {
375 int ret = i2c_smbus_read_byte_data(client, reg);
376
377 if (ret < 0)
378 dev_err(&client->dev, "Read Error\n");
379
380 return ret;
381 }
382
adp5589_write(struct i2c_client * client,u8 reg,u8 val)383 static int adp5589_write(struct i2c_client *client, u8 reg, u8 val)
384 {
385 return i2c_smbus_write_byte_data(client, reg, val);
386 }
387
388 #ifdef CONFIG_GPIOLIB
adp5589_gpio_get_value(struct gpio_chip * chip,unsigned off)389 static int adp5589_gpio_get_value(struct gpio_chip *chip, unsigned off)
390 {
391 struct adp5589_kpad *kpad = gpiochip_get_data(chip);
392 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
393 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
394 int val;
395
396 mutex_lock(&kpad->gpio_lock);
397 if (kpad->dir[bank] & bit)
398 val = kpad->dat_out[bank];
399 else
400 val = adp5589_read(kpad->client,
401 kpad->var->reg(ADP5589_GPI_STATUS_A) + bank);
402 mutex_unlock(&kpad->gpio_lock);
403
404 return !!(val & bit);
405 }
406
adp5589_gpio_set_value(struct gpio_chip * chip,unsigned off,int val)407 static void adp5589_gpio_set_value(struct gpio_chip *chip,
408 unsigned off, int val)
409 {
410 struct adp5589_kpad *kpad = gpiochip_get_data(chip);
411 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
412 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
413
414 guard(mutex)(&kpad->gpio_lock);
415
416 if (val)
417 kpad->dat_out[bank] |= bit;
418 else
419 kpad->dat_out[bank] &= ~bit;
420
421 adp5589_write(kpad->client, kpad->var->reg(ADP5589_GPO_DATA_OUT_A) +
422 bank, kpad->dat_out[bank]);
423 }
424
adp5589_gpio_direction_input(struct gpio_chip * chip,unsigned off)425 static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
426 {
427 struct adp5589_kpad *kpad = gpiochip_get_data(chip);
428 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
429 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
430
431 guard(mutex)(&kpad->gpio_lock);
432
433 kpad->dir[bank] &= ~bit;
434 return adp5589_write(kpad->client,
435 kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
436 kpad->dir[bank]);
437 }
438
adp5589_gpio_direction_output(struct gpio_chip * chip,unsigned off,int val)439 static int adp5589_gpio_direction_output(struct gpio_chip *chip,
440 unsigned off, int val)
441 {
442 struct adp5589_kpad *kpad = gpiochip_get_data(chip);
443 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
444 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
445 int error;
446
447 guard(mutex)(&kpad->gpio_lock);
448
449 kpad->dir[bank] |= bit;
450
451 if (val)
452 kpad->dat_out[bank] |= bit;
453 else
454 kpad->dat_out[bank] &= ~bit;
455
456 error = adp5589_write(kpad->client,
457 kpad->var->reg(ADP5589_GPO_DATA_OUT_A) + bank,
458 kpad->dat_out[bank]);
459 if (error)
460 return error;
461
462 error = adp5589_write(kpad->client,
463 kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
464 kpad->dir[bank]);
465 if (error)
466 return error;
467
468 return 0;
469 }
470
adp5589_build_gpiomap(struct adp5589_kpad * kpad,const struct adp5589_kpad_platform_data * pdata)471 static int adp5589_build_gpiomap(struct adp5589_kpad *kpad,
472 const struct adp5589_kpad_platform_data *pdata)
473 {
474 bool pin_used[ADP5589_MAXGPIO];
475 int n_unused = 0;
476 int i;
477
478 memset(pin_used, false, sizeof(pin_used));
479
480 for (i = 0; i < kpad->var->maxgpio; i++)
481 if (pdata->keypad_en_mask & BIT(i))
482 pin_used[i] = true;
483
484 for (i = 0; i < kpad->gpimapsize; i++)
485 pin_used[kpad->gpimap[i].pin - kpad->var->gpi_pin_base] = true;
486
487 if (kpad->extend_cfg & R4_EXTEND_CFG)
488 pin_used[4] = true;
489
490 if (kpad->extend_cfg & C4_EXTEND_CFG)
491 pin_used[kpad->var->c4_extend_cfg] = true;
492
493 if (!kpad->support_row5)
494 pin_used[5] = true;
495
496 for (i = 0; i < kpad->var->maxgpio; i++)
497 if (!pin_used[i])
498 kpad->gpiomap[n_unused++] = i;
499
500 return n_unused;
501 }
502
adp5589_gpio_add(struct adp5589_kpad * kpad)503 static int adp5589_gpio_add(struct adp5589_kpad *kpad)
504 {
505 struct device *dev = &kpad->client->dev;
506 const struct adp5589_kpad_platform_data *pdata = dev_get_platdata(dev);
507 const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
508 int i, error;
509
510 if (!gpio_data)
511 return 0;
512
513 kpad->gc.parent = dev;
514 kpad->gc.ngpio = adp5589_build_gpiomap(kpad, pdata);
515 if (kpad->gc.ngpio == 0) {
516 dev_info(dev, "No unused gpios left to export\n");
517 return 0;
518 }
519
520 kpad->gc.direction_input = adp5589_gpio_direction_input;
521 kpad->gc.direction_output = adp5589_gpio_direction_output;
522 kpad->gc.get = adp5589_gpio_get_value;
523 kpad->gc.set = adp5589_gpio_set_value;
524 kpad->gc.can_sleep = 1;
525
526 kpad->gc.base = gpio_data->gpio_start;
527 kpad->gc.label = kpad->client->name;
528 kpad->gc.owner = THIS_MODULE;
529
530 mutex_init(&kpad->gpio_lock);
531
532 error = devm_gpiochip_add_data(dev, &kpad->gc, kpad);
533 if (error)
534 return error;
535
536 for (i = 0; i <= kpad->var->bank(kpad->var->maxgpio); i++) {
537 kpad->dat_out[i] = adp5589_read(kpad->client, kpad->var->reg(
538 ADP5589_GPO_DATA_OUT_A) + i);
539 kpad->dir[i] = adp5589_read(kpad->client, kpad->var->reg(
540 ADP5589_GPIO_DIRECTION_A) + i);
541 }
542
543 return 0;
544 }
545 #else
adp5589_gpio_add(struct adp5589_kpad * kpad)546 static inline int adp5589_gpio_add(struct adp5589_kpad *kpad)
547 {
548 return 0;
549 }
550 #endif
551
adp5589_report_switches(struct adp5589_kpad * kpad,int key,int key_val)552 static void adp5589_report_switches(struct adp5589_kpad *kpad,
553 int key, int key_val)
554 {
555 int i;
556
557 for (i = 0; i < kpad->gpimapsize; i++) {
558 if (key_val == kpad->gpimap[i].pin) {
559 input_report_switch(kpad->input,
560 kpad->gpimap[i].sw_evt,
561 key & KEY_EV_PRESSED);
562 break;
563 }
564 }
565 }
566
adp5589_report_events(struct adp5589_kpad * kpad,int ev_cnt)567 static void adp5589_report_events(struct adp5589_kpad *kpad, int ev_cnt)
568 {
569 int i;
570
571 for (i = 0; i < ev_cnt; i++) {
572 int key = adp5589_read(kpad->client, ADP5589_5_FIFO_1 + i);
573 int key_val = key & KEY_EV_MASK;
574
575 if (key_val >= kpad->var->gpi_pin_base &&
576 key_val <= kpad->var->gpi_pin_end) {
577 adp5589_report_switches(kpad, key, key_val);
578 } else {
579 input_report_key(kpad->input,
580 kpad->keycode[key_val - 1],
581 key & KEY_EV_PRESSED);
582 }
583 }
584 }
585
adp5589_irq(int irq,void * handle)586 static irqreturn_t adp5589_irq(int irq, void *handle)
587 {
588 struct adp5589_kpad *kpad = handle;
589 struct i2c_client *client = kpad->client;
590 int status, ev_cnt;
591
592 status = adp5589_read(client, ADP5589_5_INT_STATUS);
593
594 if (status & OVRFLOW_INT) /* Unlikely and should never happen */
595 dev_err(&client->dev, "Event Overflow Error\n");
596
597 if (status & EVENT_INT) {
598 ev_cnt = adp5589_read(client, ADP5589_5_STATUS) & KEC;
599 if (ev_cnt) {
600 adp5589_report_events(kpad, ev_cnt);
601 input_sync(kpad->input);
602 }
603 }
604
605 adp5589_write(client, ADP5589_5_INT_STATUS, status); /* Status is W1C */
606
607 return IRQ_HANDLED;
608 }
609
adp5589_get_evcode(struct adp5589_kpad * kpad,unsigned short key)610 static int adp5589_get_evcode(struct adp5589_kpad *kpad, unsigned short key)
611 {
612 int i;
613
614 for (i = 0; i < kpad->var->keymapsize; i++)
615 if (key == kpad->keycode[i])
616 return (i + 1) | KEY_EV_PRESSED;
617
618 dev_err(&kpad->client->dev, "RESET/UNLOCK key not in keycode map\n");
619
620 return -EINVAL;
621 }
622
adp5589_setup(struct adp5589_kpad * kpad)623 static int adp5589_setup(struct adp5589_kpad *kpad)
624 {
625 struct i2c_client *client = kpad->client;
626 const struct adp5589_kpad_platform_data *pdata =
627 dev_get_platdata(&client->dev);
628 u8 (*reg) (u8) = kpad->var->reg;
629 unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
630 unsigned char pull_mask = 0;
631 int i, ret;
632
633 ret = adp5589_write(client, reg(ADP5589_PIN_CONFIG_A),
634 pdata->keypad_en_mask & kpad->var->row_mask);
635 ret |= adp5589_write(client, reg(ADP5589_PIN_CONFIG_B),
636 (pdata->keypad_en_mask >> kpad->var->col_shift) &
637 kpad->var->col_mask);
638
639 if (!kpad->is_adp5585)
640 ret |= adp5589_write(client, ADP5589_PIN_CONFIG_C,
641 (pdata->keypad_en_mask >> 16) & 0xFF);
642
643 if (!kpad->is_adp5585 && pdata->en_keylock) {
644 ret |= adp5589_write(client, ADP5589_UNLOCK1,
645 pdata->unlock_key1);
646 ret |= adp5589_write(client, ADP5589_UNLOCK2,
647 pdata->unlock_key2);
648 ret |= adp5589_write(client, ADP5589_UNLOCK_TIMERS,
649 pdata->unlock_timer & LTIME_MASK);
650 ret |= adp5589_write(client, ADP5589_LOCK_CFG, LOCK_EN);
651 }
652
653 for (i = 0; i < KEYP_MAX_EVENT; i++)
654 ret |= adp5589_read(client, ADP5589_5_FIFO_1 + i);
655
656 for (i = 0; i < pdata->gpimapsize; i++) {
657 unsigned short pin = pdata->gpimap[i].pin;
658
659 if (pin <= kpad->var->gpi_pin_row_end) {
660 evt_mode1 |= BIT(pin - kpad->var->gpi_pin_row_base);
661 } else {
662 evt_mode2 |=
663 BIT(pin - kpad->var->gpi_pin_col_base) & 0xFF;
664 if (!kpad->is_adp5585)
665 evt_mode3 |=
666 BIT(pin - kpad->var->gpi_pin_col_base) >> 8;
667 }
668 }
669
670 if (pdata->gpimapsize) {
671 ret |= adp5589_write(client, reg(ADP5589_GPI_EVENT_EN_A),
672 evt_mode1);
673 ret |= adp5589_write(client, reg(ADP5589_GPI_EVENT_EN_B),
674 evt_mode2);
675 if (!kpad->is_adp5585)
676 ret |= adp5589_write(client,
677 reg(ADP5589_GPI_EVENT_EN_C),
678 evt_mode3);
679 }
680
681 if (pdata->pull_dis_mask & pdata->pullup_en_100k &
682 pdata->pullup_en_300k & pdata->pulldown_en_300k)
683 dev_warn(&client->dev, "Conflicting pull resistor config\n");
684
685 for (i = 0; i <= kpad->var->max_row_num; i++) {
686 unsigned int val = 0, bit = BIT(i);
687 if (pdata->pullup_en_300k & bit)
688 val = 0;
689 else if (pdata->pulldown_en_300k & bit)
690 val = 1;
691 else if (pdata->pullup_en_100k & bit)
692 val = 2;
693 else if (pdata->pull_dis_mask & bit)
694 val = 3;
695
696 pull_mask |= val << (2 * (i & 0x3));
697
698 if (i % 4 == 3 || i == kpad->var->max_row_num) {
699 ret |= adp5589_write(client, reg(ADP5585_RPULL_CONFIG_A)
700 + (i >> 2), pull_mask);
701 pull_mask = 0;
702 }
703 }
704
705 for (i = 0; i <= kpad->var->max_col_num; i++) {
706 unsigned int val = 0, bit = BIT(i + kpad->var->col_shift);
707 if (pdata->pullup_en_300k & bit)
708 val = 0;
709 else if (pdata->pulldown_en_300k & bit)
710 val = 1;
711 else if (pdata->pullup_en_100k & bit)
712 val = 2;
713 else if (pdata->pull_dis_mask & bit)
714 val = 3;
715
716 pull_mask |= val << (2 * (i & 0x3));
717
718 if (i % 4 == 3 || i == kpad->var->max_col_num) {
719 ret |= adp5589_write(client,
720 reg(ADP5585_RPULL_CONFIG_C) +
721 (i >> 2), pull_mask);
722 pull_mask = 0;
723 }
724 }
725
726 if (pdata->reset1_key_1 && pdata->reset1_key_2 && pdata->reset1_key_3) {
727 ret |= adp5589_write(client, reg(ADP5589_RESET1_EVENT_A),
728 adp5589_get_evcode(kpad,
729 pdata->reset1_key_1));
730 ret |= adp5589_write(client, reg(ADP5589_RESET1_EVENT_B),
731 adp5589_get_evcode(kpad,
732 pdata->reset1_key_2));
733 ret |= adp5589_write(client, reg(ADP5589_RESET1_EVENT_C),
734 adp5589_get_evcode(kpad,
735 pdata->reset1_key_3));
736 kpad->extend_cfg |= R4_EXTEND_CFG;
737 }
738
739 if (pdata->reset2_key_1 && pdata->reset2_key_2) {
740 ret |= adp5589_write(client, reg(ADP5589_RESET2_EVENT_A),
741 adp5589_get_evcode(kpad,
742 pdata->reset2_key_1));
743 ret |= adp5589_write(client, reg(ADP5589_RESET2_EVENT_B),
744 adp5589_get_evcode(kpad,
745 pdata->reset2_key_2));
746 kpad->extend_cfg |= C4_EXTEND_CFG;
747 }
748
749 if (kpad->extend_cfg) {
750 ret |= adp5589_write(client, reg(ADP5589_RESET_CFG),
751 pdata->reset_cfg);
752 ret |= adp5589_write(client, reg(ADP5589_PIN_CONFIG_D),
753 kpad->extend_cfg);
754 }
755
756 ret |= adp5589_write(client, reg(ADP5589_DEBOUNCE_DIS_A),
757 pdata->debounce_dis_mask & kpad->var->row_mask);
758
759 ret |= adp5589_write(client, reg(ADP5589_DEBOUNCE_DIS_B),
760 (pdata->debounce_dis_mask >> kpad->var->col_shift)
761 & kpad->var->col_mask);
762
763 if (!kpad->is_adp5585)
764 ret |= adp5589_write(client, reg(ADP5589_DEBOUNCE_DIS_C),
765 (pdata->debounce_dis_mask >> 16) & 0xFF);
766
767 ret |= adp5589_write(client, reg(ADP5589_POLL_PTIME_CFG),
768 pdata->scan_cycle_time & PTIME_MASK);
769 ret |= adp5589_write(client, ADP5589_5_INT_STATUS,
770 (kpad->is_adp5585 ? 0 : LOGIC2_INT) |
771 LOGIC1_INT | OVRFLOW_INT |
772 (kpad->is_adp5585 ? 0 : LOCK_INT) |
773 GPI_INT | EVENT_INT); /* Status is W1C */
774
775 ret |= adp5589_write(client, reg(ADP5589_GENERAL_CFG),
776 INT_CFG | OSC_EN | CORE_CLK(3));
777 ret |= adp5589_write(client, reg(ADP5589_INT_EN),
778 OVRFLOW_IEN | GPI_IEN | EVENT_IEN);
779
780 if (ret < 0) {
781 dev_err(&client->dev, "Write Error\n");
782 return ret;
783 }
784
785 return 0;
786 }
787
adp5589_report_switch_state(struct adp5589_kpad * kpad)788 static void adp5589_report_switch_state(struct adp5589_kpad *kpad)
789 {
790 int gpi_stat_tmp, pin_loc;
791 int i;
792 int gpi_stat1 = adp5589_read(kpad->client,
793 kpad->var->reg(ADP5589_GPI_STATUS_A));
794 int gpi_stat2 = adp5589_read(kpad->client,
795 kpad->var->reg(ADP5589_GPI_STATUS_B));
796 int gpi_stat3 = !kpad->is_adp5585 ?
797 adp5589_read(kpad->client, ADP5589_GPI_STATUS_C) : 0;
798
799 for (i = 0; i < kpad->gpimapsize; i++) {
800 unsigned short pin = kpad->gpimap[i].pin;
801
802 if (pin <= kpad->var->gpi_pin_row_end) {
803 gpi_stat_tmp = gpi_stat1;
804 pin_loc = pin - kpad->var->gpi_pin_row_base;
805 } else if ((pin - kpad->var->gpi_pin_col_base) < 8) {
806 gpi_stat_tmp = gpi_stat2;
807 pin_loc = pin - kpad->var->gpi_pin_col_base;
808 } else {
809 gpi_stat_tmp = gpi_stat3;
810 pin_loc = pin - kpad->var->gpi_pin_col_base - 8;
811 }
812
813 if (gpi_stat_tmp < 0) {
814 dev_err(&kpad->client->dev,
815 "Can't read GPIO_DAT_STAT switch %d, default to OFF\n",
816 pin);
817 gpi_stat_tmp = 0;
818 }
819
820 input_report_switch(kpad->input,
821 kpad->gpimap[i].sw_evt,
822 !(gpi_stat_tmp & BIT(pin_loc)));
823 }
824
825 input_sync(kpad->input);
826 }
827
adp5589_keypad_add(struct adp5589_kpad * kpad,unsigned int revid)828 static int adp5589_keypad_add(struct adp5589_kpad *kpad, unsigned int revid)
829 {
830 struct i2c_client *client = kpad->client;
831 const struct adp5589_kpad_platform_data *pdata =
832 dev_get_platdata(&client->dev);
833 struct input_dev *input;
834 unsigned int i;
835 int error;
836
837 if (!((pdata->keypad_en_mask & kpad->var->row_mask) &&
838 (pdata->keypad_en_mask >> kpad->var->col_shift)) ||
839 !pdata->keymap) {
840 dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
841 return -EINVAL;
842 }
843
844 if (pdata->keymapsize != kpad->var->keymapsize) {
845 dev_err(&client->dev, "invalid keymapsize\n");
846 return -EINVAL;
847 }
848
849 if (!pdata->gpimap && pdata->gpimapsize) {
850 dev_err(&client->dev, "invalid gpimap from pdata\n");
851 return -EINVAL;
852 }
853
854 if (pdata->gpimapsize > kpad->var->gpimapsize_max) {
855 dev_err(&client->dev, "invalid gpimapsize\n");
856 return -EINVAL;
857 }
858
859 for (i = 0; i < pdata->gpimapsize; i++) {
860 unsigned short pin = pdata->gpimap[i].pin;
861
862 if (pin < kpad->var->gpi_pin_base ||
863 pin > kpad->var->gpi_pin_end) {
864 dev_err(&client->dev, "invalid gpi pin data\n");
865 return -EINVAL;
866 }
867
868 if (BIT(pin - kpad->var->gpi_pin_row_base) &
869 pdata->keypad_en_mask) {
870 dev_err(&client->dev, "invalid gpi row/col data\n");
871 return -EINVAL;
872 }
873 }
874
875 if (!client->irq) {
876 dev_err(&client->dev, "no IRQ?\n");
877 return -EINVAL;
878 }
879
880 input = devm_input_allocate_device(&client->dev);
881 if (!input)
882 return -ENOMEM;
883
884 kpad->input = input;
885
886 input->name = client->name;
887 input->phys = "adp5589-keys/input0";
888 input->dev.parent = &client->dev;
889
890 input_set_drvdata(input, kpad);
891
892 input->id.bustype = BUS_I2C;
893 input->id.vendor = 0x0001;
894 input->id.product = 0x0001;
895 input->id.version = revid;
896
897 input->keycodesize = sizeof(kpad->keycode[0]);
898 input->keycodemax = pdata->keymapsize;
899 input->keycode = kpad->keycode;
900
901 memcpy(kpad->keycode, pdata->keymap,
902 pdata->keymapsize * input->keycodesize);
903
904 kpad->gpimap = pdata->gpimap;
905 kpad->gpimapsize = pdata->gpimapsize;
906
907 /* setup input device */
908 __set_bit(EV_KEY, input->evbit);
909
910 if (pdata->repeat)
911 __set_bit(EV_REP, input->evbit);
912
913 for (i = 0; i < input->keycodemax; i++)
914 if (kpad->keycode[i] <= KEY_MAX)
915 __set_bit(kpad->keycode[i], input->keybit);
916 __clear_bit(KEY_RESERVED, input->keybit);
917
918 if (kpad->gpimapsize)
919 __set_bit(EV_SW, input->evbit);
920 for (i = 0; i < kpad->gpimapsize; i++)
921 __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
922
923 error = input_register_device(input);
924 if (error) {
925 dev_err(&client->dev, "unable to register input device\n");
926 return error;
927 }
928
929 error = devm_request_threaded_irq(&client->dev, client->irq,
930 NULL, adp5589_irq,
931 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
932 client->dev.driver->name, kpad);
933 if (error) {
934 dev_err(&client->dev, "unable to request irq %d\n", client->irq);
935 return error;
936 }
937
938 return 0;
939 }
940
adp5589_clear_config(void * data)941 static void adp5589_clear_config(void *data)
942 {
943 struct adp5589_kpad *kpad = data;
944
945 adp5589_write(kpad->client, kpad->var->reg(ADP5589_GENERAL_CFG), 0);
946 }
947
adp5589_probe(struct i2c_client * client)948 static int adp5589_probe(struct i2c_client *client)
949 {
950 const struct i2c_device_id *id = i2c_client_get_device_id(client);
951 struct adp5589_kpad *kpad;
952 const struct adp5589_kpad_platform_data *pdata =
953 dev_get_platdata(&client->dev);
954 unsigned int revid;
955 int error, ret;
956
957 if (!i2c_check_functionality(client->adapter,
958 I2C_FUNC_SMBUS_BYTE_DATA)) {
959 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
960 return -EIO;
961 }
962
963 if (!pdata) {
964 dev_err(&client->dev, "no platform data?\n");
965 return -EINVAL;
966 }
967
968 kpad = devm_kzalloc(&client->dev, sizeof(*kpad), GFP_KERNEL);
969 if (!kpad)
970 return -ENOMEM;
971
972 kpad->client = client;
973
974 switch (id->driver_data) {
975 case ADP5585_02:
976 kpad->support_row5 = true;
977 fallthrough;
978 case ADP5585_01:
979 kpad->is_adp5585 = true;
980 kpad->var = &const_adp5585;
981 break;
982 case ADP5589:
983 kpad->support_row5 = true;
984 kpad->var = &const_adp5589;
985 break;
986 }
987
988 error = devm_add_action_or_reset(&client->dev, adp5589_clear_config,
989 kpad);
990 if (error)
991 return error;
992
993 ret = adp5589_read(client, ADP5589_5_ID);
994 if (ret < 0)
995 return ret;
996
997 revid = (u8) ret & ADP5589_5_DEVICE_ID_MASK;
998
999 if (pdata->keymapsize) {
1000 error = adp5589_keypad_add(kpad, revid);
1001 if (error)
1002 return error;
1003 }
1004
1005 error = adp5589_setup(kpad);
1006 if (error)
1007 return error;
1008
1009 if (kpad->gpimapsize)
1010 adp5589_report_switch_state(kpad);
1011
1012 error = adp5589_gpio_add(kpad);
1013 if (error)
1014 return error;
1015
1016 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
1017 return 0;
1018 }
1019
adp5589_suspend(struct device * dev)1020 static int adp5589_suspend(struct device *dev)
1021 {
1022 struct i2c_client *client = to_i2c_client(dev);
1023 struct adp5589_kpad *kpad = i2c_get_clientdata(client);
1024
1025 if (kpad->input)
1026 disable_irq(client->irq);
1027
1028 return 0;
1029 }
1030
adp5589_resume(struct device * dev)1031 static int adp5589_resume(struct device *dev)
1032 {
1033 struct i2c_client *client = to_i2c_client(dev);
1034 struct adp5589_kpad *kpad = i2c_get_clientdata(client);
1035
1036 if (kpad->input)
1037 enable_irq(client->irq);
1038
1039 return 0;
1040 }
1041
1042 static DEFINE_SIMPLE_DEV_PM_OPS(adp5589_dev_pm_ops, adp5589_suspend, adp5589_resume);
1043
1044 static const struct i2c_device_id adp5589_id[] = {
1045 {"adp5589-keys", ADP5589},
1046 {"adp5585-keys", ADP5585_01},
1047 {"adp5585-02-keys", ADP5585_02}, /* Adds ROW5 to ADP5585 */
1048 {}
1049 };
1050
1051 MODULE_DEVICE_TABLE(i2c, adp5589_id);
1052
1053 static struct i2c_driver adp5589_driver = {
1054 .driver = {
1055 .name = KBUILD_MODNAME,
1056 .pm = pm_sleep_ptr(&adp5589_dev_pm_ops),
1057 },
1058 .probe = adp5589_probe,
1059 .id_table = adp5589_id,
1060 };
1061
1062 module_i2c_driver(adp5589_driver);
1063
1064 MODULE_LICENSE("GPL");
1065 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
1066 MODULE_DESCRIPTION("ADP5589/ADP5585 Keypad driver");
1067