xref: /linux/drivers/input/keyboard/adp5588-keys.c (revision 0227b49b50276657243e54f5609e65c4f0eaaf4d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * File: drivers/input/keyboard/adp5588_keys.c
4  * Description:  keypad driver for ADP5588 and ADP5587
5  *		 I2C QWERTY Keypad and IO Expander
6  * Bugs: Enter bugs at http://blackfin.uclinux.org/
7  *
8  * Copyright (C) 2008-2010 Analog Devices Inc.
9  */
10 
11 #include <linux/bits.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/i2c.h>
17 #include <linux/input.h>
18 #include <linux/input/matrix_keypad.h>
19 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/ktime.h>
22 #include <linux/module.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/pinctrl/pinconf-generic.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/slab.h>
29 #include <linux/timekeeping.h>
30 
31 #define DEV_ID 0x00		/* Device ID */
32 #define CFG 0x01		/* Configuration Register1 */
33 #define INT_STAT 0x02		/* Interrupt Status Register */
34 #define KEY_LCK_EC_STAT 0x03	/* Key Lock and Event Counter Register */
35 #define KEY_EVENTA 0x04		/* Key Event Register A */
36 #define KEY_EVENTB 0x05		/* Key Event Register B */
37 #define KEY_EVENTC 0x06		/* Key Event Register C */
38 #define KEY_EVENTD 0x07		/* Key Event Register D */
39 #define KEY_EVENTE 0x08		/* Key Event Register E */
40 #define KEY_EVENTF 0x09		/* Key Event Register F */
41 #define KEY_EVENTG 0x0A		/* Key Event Register G */
42 #define KEY_EVENTH 0x0B		/* Key Event Register H */
43 #define KEY_EVENTI 0x0C		/* Key Event Register I */
44 #define KEY_EVENTJ 0x0D		/* Key Event Register J */
45 #define KP_LCK_TMR 0x0E		/* Keypad Lock1 to Lock2 Timer */
46 #define UNLOCK1 0x0F		/* Unlock Key1 */
47 #define UNLOCK2 0x10		/* Unlock Key2 */
48 #define GPIO_INT_STAT1 0x11	/* GPIO Interrupt Status */
49 #define GPIO_INT_STAT2 0x12	/* GPIO Interrupt Status */
50 #define GPIO_INT_STAT3 0x13	/* GPIO Interrupt Status */
51 #define GPIO_DAT_STAT1 0x14	/* GPIO Data Status, Read twice to clear */
52 #define GPIO_DAT_STAT2 0x15	/* GPIO Data Status, Read twice to clear */
53 #define GPIO_DAT_STAT3 0x16	/* GPIO Data Status, Read twice to clear */
54 #define GPIO_DAT_OUT1 0x17	/* GPIO DATA OUT */
55 #define GPIO_DAT_OUT2 0x18	/* GPIO DATA OUT */
56 #define GPIO_DAT_OUT3 0x19	/* GPIO DATA OUT */
57 #define GPIO_INT_EN1 0x1A	/* GPIO Interrupt Enable */
58 #define GPIO_INT_EN2 0x1B	/* GPIO Interrupt Enable */
59 #define GPIO_INT_EN3 0x1C	/* GPIO Interrupt Enable */
60 #define KP_GPIO1 0x1D		/* Keypad or GPIO Selection */
61 #define KP_GPIO2 0x1E		/* Keypad or GPIO Selection */
62 #define KP_GPIO3 0x1F		/* Keypad or GPIO Selection */
63 #define GPI_EM1 0x20		/* GPI Event Mode 1 */
64 #define GPI_EM2 0x21		/* GPI Event Mode 2 */
65 #define GPI_EM3 0x22		/* GPI Event Mode 3 */
66 #define GPIO_DIR1 0x23		/* GPIO Data Direction */
67 #define GPIO_DIR2 0x24		/* GPIO Data Direction */
68 #define GPIO_DIR3 0x25		/* GPIO Data Direction */
69 #define GPIO_INT_LVL1 0x26	/* GPIO Edge/Level Detect */
70 #define GPIO_INT_LVL2 0x27	/* GPIO Edge/Level Detect */
71 #define GPIO_INT_LVL3 0x28	/* GPIO Edge/Level Detect */
72 #define DEBOUNCE_DIS1 0x29	/* Debounce Disable */
73 #define DEBOUNCE_DIS2 0x2A	/* Debounce Disable */
74 #define DEBOUNCE_DIS3 0x2B	/* Debounce Disable */
75 #define GPIO_PULL1 0x2C		/* GPIO Pull Disable */
76 #define GPIO_PULL2 0x2D		/* GPIO Pull Disable */
77 #define GPIO_PULL3 0x2E		/* GPIO Pull Disable */
78 #define CMP_CFG_STAT 0x30	/* Comparator Configuration and Status Register */
79 #define CMP_CONFG_SENS1 0x31	/* Sensor1 Comparator Configuration Register */
80 #define CMP_CONFG_SENS2 0x32	/* L2 Light Sensor Reference Level, Output Falling for Sensor 1 */
81 #define CMP1_LVL2_TRIP 0x33	/* L2 Light Sensor Hysteresis (Active when Output Rising) for Sensor 1 */
82 #define CMP1_LVL2_HYS 0x34	/* L3 Light Sensor Reference Level, Output Falling For Sensor 1 */
83 #define CMP1_LVL3_TRIP 0x35	/* L3 Light Sensor Hysteresis (Active when Output Rising) For Sensor 1 */
84 #define CMP1_LVL3_HYS 0x36	/* Sensor 2 Comparator Configuration Register */
85 #define CMP2_LVL2_TRIP 0x37	/* L2 Light Sensor Reference Level, Output Falling for Sensor 2 */
86 #define CMP2_LVL2_HYS 0x38	/* L2 Light Sensor Hysteresis (Active when Output Rising) for Sensor 2 */
87 #define CMP2_LVL3_TRIP 0x39	/* L3 Light Sensor Reference Level, Output Falling For Sensor 2 */
88 #define CMP2_LVL3_HYS 0x3A	/* L3 Light Sensor Hysteresis (Active when Output Rising) For Sensor 2 */
89 #define CMP1_ADC_DAT_R1 0x3B	/* Comparator 1 ADC data Register1 */
90 #define CMP1_ADC_DAT_R2 0x3C	/* Comparator 1 ADC data Register2 */
91 #define CMP2_ADC_DAT_R1 0x3D	/* Comparator 2 ADC data Register1 */
92 #define CMP2_ADC_DAT_R2 0x3E	/* Comparator 2 ADC data Register2 */
93 
94 #define ADP5588_DEVICE_ID_MASK	0xF
95 
96  /* Configuration Register1 */
97 #define ADP5588_AUTO_INC	BIT(7)
98 #define ADP5588_GPIEM_CFG	BIT(6)
99 #define ADP5588_OVR_FLOW_M	BIT(5)
100 #define ADP5588_INT_CFG		BIT(4)
101 #define ADP5588_OVR_FLOW_IEN	BIT(3)
102 #define ADP5588_K_LCK_IM	BIT(2)
103 #define ADP5588_GPI_IEN		BIT(1)
104 #define ADP5588_KE_IEN		BIT(0)
105 
106 /* Interrupt Status Register */
107 #define ADP5588_CMP2_INT	BIT(5)
108 #define ADP5588_CMP1_INT	BIT(4)
109 #define ADP5588_OVR_FLOW_INT	BIT(3)
110 #define ADP5588_K_LCK_INT	BIT(2)
111 #define ADP5588_GPI_INT		BIT(1)
112 #define ADP5588_KE_INT		BIT(0)
113 
114 /* Key Lock and Event Counter Register */
115 #define ADP5588_K_LCK_EN	BIT(6)
116 #define ADP5588_LCK21		0x30
117 #define ADP5588_KEC		GENMASK(3, 0)
118 
119 #define ADP5588_MAXGPIO		18
120 #define ADP5588_BANK(offs)	((offs) >> 3)
121 #define ADP5588_BIT(offs)	(1u << ((offs) & 0x7))
122 
123 /* Put one of these structures in i2c_board_info platform_data */
124 
125 /*
126  * 128 so it fits matrix-keymap maximum number of keys when the full
127  * 10cols * 8rows are used.
128  */
129 #define ADP5588_KEYMAPSIZE 128
130 
131 #define GPI_PIN_ROW0 97
132 #define GPI_PIN_ROW1 98
133 #define GPI_PIN_ROW2 99
134 #define GPI_PIN_ROW3 100
135 #define GPI_PIN_ROW4 101
136 #define GPI_PIN_ROW5 102
137 #define GPI_PIN_ROW6 103
138 #define GPI_PIN_ROW7 104
139 #define GPI_PIN_COL0 105
140 #define GPI_PIN_COL1 106
141 #define GPI_PIN_COL2 107
142 #define GPI_PIN_COL3 108
143 #define GPI_PIN_COL4 109
144 #define GPI_PIN_COL5 110
145 #define GPI_PIN_COL6 111
146 #define GPI_PIN_COL7 112
147 #define GPI_PIN_COL8 113
148 #define GPI_PIN_COL9 114
149 
150 #define GPI_PIN_ROW_BASE GPI_PIN_ROW0
151 #define GPI_PIN_ROW_END GPI_PIN_ROW7
152 #define GPI_PIN_COL_BASE GPI_PIN_COL0
153 #define GPI_PIN_COL_END GPI_PIN_COL9
154 
155 #define GPI_PIN_BASE GPI_PIN_ROW_BASE
156 #define GPI_PIN_END GPI_PIN_COL_END
157 
158 #define ADP5588_ROWS_MAX (GPI_PIN_ROW7 - GPI_PIN_ROW0 + 1)
159 #define ADP5588_COLS_MAX (GPI_PIN_COL9 - GPI_PIN_COL0 + 1)
160 
161 #define ADP5588_GPIMAPSIZE_MAX (GPI_PIN_END - GPI_PIN_BASE + 1)
162 
163 /* Key Event Register xy */
164 #define KEY_EV_PRESSED		BIT(7)
165 #define KEY_EV_MASK		GENMASK(6, 0)
166 
167 #define KP_SEL(x)		(BIT(x) - 1)	/* 2^x-1 */
168 
169 #define KEYP_MAX_EVENT		10
170 
171 /*
172  * Early pre 4.0 Silicon required to delay readout by at least 25ms,
173  * since the Event Counter Register updated 25ms after the interrupt
174  * asserted.
175  */
176 #define WA_DELAYED_READOUT_REVID(rev)		((rev) < 4)
177 #define WA_DELAYED_READOUT_TIME			25
178 
179 #define ADP5588_INVALID_HWIRQ	(~0UL)
180 
181 struct adp5588_kpad {
182 	struct i2c_client *client;
183 	struct input_dev *input;
184 	ktime_t irq_time;
185 	unsigned long delay;
186 	u32 row_shift;
187 	u32 rows;
188 	u32 cols;
189 	u32 unlock_keys[2];
190 	int nkeys_unlock;
191 	bool gpio_only;
192 	unsigned short keycode[ADP5588_KEYMAPSIZE];
193 	unsigned char gpiomap[ADP5588_MAXGPIO];
194 	struct gpio_chip gc;
195 	struct mutex gpio_lock;	/* Protect cached dir, dat_out */
196 	u8 dat_out[3];
197 	u8 dir[3];
198 	u8 int_en[3];
199 	u8 irq_mask[3];
200 	u8 pull_dis[3];
201 };
202 
adp5588_read(struct i2c_client * client,u8 reg)203 static int adp5588_read(struct i2c_client *client, u8 reg)
204 {
205 	int ret = i2c_smbus_read_byte_data(client, reg);
206 
207 	if (ret < 0)
208 		dev_err(&client->dev, "Read Error\n");
209 
210 	return ret;
211 }
212 
adp5588_write(struct i2c_client * client,u8 reg,u8 val)213 static int adp5588_write(struct i2c_client *client, u8 reg, u8 val)
214 {
215 	return i2c_smbus_write_byte_data(client, reg, val);
216 }
217 
adp5588_gpio_get_value(struct gpio_chip * chip,unsigned int off)218 static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned int off)
219 {
220 	struct adp5588_kpad *kpad = gpiochip_get_data(chip);
221 	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
222 	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
223 	int val;
224 
225 	guard(mutex)(&kpad->gpio_lock);
226 
227 	if (kpad->dir[bank] & bit)
228 		val = kpad->dat_out[bank];
229 	else
230 		val = adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank);
231 
232 	return !!(val & bit);
233 }
234 
adp5588_gpio_set_value(struct gpio_chip * chip,unsigned int off,int val)235 static int adp5588_gpio_set_value(struct gpio_chip *chip, unsigned int off,
236 				  int val)
237 {
238 	struct adp5588_kpad *kpad = gpiochip_get_data(chip);
239 	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
240 	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
241 
242 	guard(mutex)(&kpad->gpio_lock);
243 
244 	if (val)
245 		kpad->dat_out[bank] |= bit;
246 	else
247 		kpad->dat_out[bank] &= ~bit;
248 
249 	return adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
250 			     kpad->dat_out[bank]);
251 }
252 
adp5588_gpio_set_config(struct gpio_chip * chip,unsigned int off,unsigned long config)253 static int adp5588_gpio_set_config(struct gpio_chip *chip,  unsigned int off,
254 				   unsigned long config)
255 {
256 	struct adp5588_kpad *kpad = gpiochip_get_data(chip);
257 	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
258 	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
259 	bool pull_disable;
260 
261 	switch (pinconf_to_config_param(config)) {
262 	case PIN_CONFIG_BIAS_PULL_UP:
263 		pull_disable = false;
264 		break;
265 	case PIN_CONFIG_BIAS_DISABLE:
266 		pull_disable = true;
267 		break;
268 	default:
269 		return -ENOTSUPP;
270 	}
271 
272 	guard(mutex)(&kpad->gpio_lock);
273 
274 	if (pull_disable)
275 		kpad->pull_dis[bank] |= bit;
276 	else
277 		kpad->pull_dis[bank] &= bit;
278 
279 	return adp5588_write(kpad->client, GPIO_PULL1 + bank,
280 			     kpad->pull_dis[bank]);
281 }
282 
adp5588_gpio_direction_input(struct gpio_chip * chip,unsigned int off)283 static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned int off)
284 {
285 	struct adp5588_kpad *kpad = gpiochip_get_data(chip);
286 	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
287 	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
288 
289 	guard(mutex)(&kpad->gpio_lock);
290 
291 	kpad->dir[bank] &= ~bit;
292 	return adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
293 }
294 
adp5588_gpio_direction_output(struct gpio_chip * chip,unsigned int off,int val)295 static int adp5588_gpio_direction_output(struct gpio_chip *chip,
296 					 unsigned int off, int val)
297 {
298 	struct adp5588_kpad *kpad = gpiochip_get_data(chip);
299 	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
300 	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
301 	int error;
302 
303 	guard(mutex)(&kpad->gpio_lock);
304 
305 	kpad->dir[bank] |= bit;
306 
307 	if (val)
308 		kpad->dat_out[bank] |= bit;
309 	else
310 		kpad->dat_out[bank] &= ~bit;
311 
312 	error = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
313 			      kpad->dat_out[bank]);
314 	if (error)
315 		return error;
316 
317 	error = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
318 	if (error)
319 		return error;
320 
321 	return 0;
322 }
323 
adp5588_build_gpiomap(struct adp5588_kpad * kpad)324 static int adp5588_build_gpiomap(struct adp5588_kpad *kpad)
325 {
326 	bool pin_used[ADP5588_MAXGPIO];
327 	int n_unused = 0;
328 	int i;
329 
330 	memset(pin_used, 0, sizeof(pin_used));
331 
332 	for (i = 0; i < kpad->rows; i++)
333 		pin_used[i] = true;
334 
335 	for (i = 0; i < kpad->cols; i++)
336 		pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true;
337 
338 	for (i = 0; i < ADP5588_MAXGPIO; i++)
339 		if (!pin_used[i])
340 			kpad->gpiomap[n_unused++] = i;
341 
342 	return n_unused;
343 }
344 
adp5588_irq_bus_lock(struct irq_data * d)345 static void adp5588_irq_bus_lock(struct irq_data *d)
346 {
347 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
348 	struct adp5588_kpad *kpad = gpiochip_get_data(gc);
349 
350 	mutex_lock(&kpad->gpio_lock);
351 }
352 
adp5588_irq_bus_sync_unlock(struct irq_data * d)353 static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
354 {
355 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
356 	struct adp5588_kpad *kpad = gpiochip_get_data(gc);
357 	int i;
358 
359 	for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
360 		if (kpad->int_en[i] ^ kpad->irq_mask[i]) {
361 			kpad->int_en[i] = kpad->irq_mask[i];
362 			adp5588_write(kpad->client, GPI_EM1 + i, kpad->int_en[i]);
363 		}
364 	}
365 
366 	mutex_unlock(&kpad->gpio_lock);
367 }
368 
adp5588_irq_mask(struct irq_data * d)369 static void adp5588_irq_mask(struct irq_data *d)
370 {
371 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
372 	struct adp5588_kpad *kpad = gpiochip_get_data(gc);
373 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
374 	unsigned long real_irq = kpad->gpiomap[hwirq];
375 
376 	kpad->irq_mask[ADP5588_BANK(real_irq)] &= ~ADP5588_BIT(real_irq);
377 	gpiochip_disable_irq(gc, hwirq);
378 }
379 
adp5588_irq_unmask(struct irq_data * d)380 static void adp5588_irq_unmask(struct irq_data *d)
381 {
382 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
383 	struct adp5588_kpad *kpad = gpiochip_get_data(gc);
384 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
385 	unsigned long real_irq = kpad->gpiomap[hwirq];
386 
387 	gpiochip_enable_irq(gc, hwirq);
388 	kpad->irq_mask[ADP5588_BANK(real_irq)] |= ADP5588_BIT(real_irq);
389 }
390 
adp5588_irq_set_type(struct irq_data * d,unsigned int type)391 static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
392 {
393 	if (!(type & IRQ_TYPE_EDGE_BOTH))
394 		return -EINVAL;
395 
396 	irq_set_handler_locked(d, handle_edge_irq);
397 
398 	return 0;
399 }
400 
401 static const struct irq_chip adp5588_irq_chip = {
402 	.name = "adp5588",
403 	.irq_mask = adp5588_irq_mask,
404 	.irq_unmask = adp5588_irq_unmask,
405 	.irq_bus_lock = adp5588_irq_bus_lock,
406 	.irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock,
407 	.irq_set_type = adp5588_irq_set_type,
408 	.flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_IMMUTABLE,
409 	GPIOCHIP_IRQ_RESOURCE_HELPERS,
410 };
411 
adp5588_gpio_add(struct adp5588_kpad * kpad)412 static int adp5588_gpio_add(struct adp5588_kpad *kpad)
413 {
414 	struct device *dev = &kpad->client->dev;
415 	struct gpio_irq_chip *girq;
416 	int i, error;
417 
418 	kpad->gc.ngpio = adp5588_build_gpiomap(kpad);
419 	if (kpad->gc.ngpio == 0) {
420 		dev_info(dev, "No unused gpios left to export\n");
421 		return 0;
422 	}
423 
424 	kpad->gc.parent = &kpad->client->dev;
425 	kpad->gc.direction_input = adp5588_gpio_direction_input;
426 	kpad->gc.direction_output = adp5588_gpio_direction_output;
427 	kpad->gc.get = adp5588_gpio_get_value;
428 	kpad->gc.set = adp5588_gpio_set_value;
429 	kpad->gc.set_config = adp5588_gpio_set_config;
430 	kpad->gc.can_sleep = 1;
431 
432 	kpad->gc.base = -1;
433 	kpad->gc.label = kpad->client->name;
434 	kpad->gc.owner = THIS_MODULE;
435 
436 	if (device_property_present(dev, "interrupt-controller")) {
437 		if (!kpad->client->irq) {
438 			dev_err(dev, "Unable to serve as interrupt controller without interrupt");
439 			return -EINVAL;
440 		}
441 
442 		girq = &kpad->gc.irq;
443 		gpio_irq_chip_set_chip(girq, &adp5588_irq_chip);
444 		girq->handler = handle_bad_irq;
445 		girq->threaded = true;
446 	}
447 
448 	mutex_init(&kpad->gpio_lock);
449 
450 	error = devm_gpiochip_add_data(dev, &kpad->gc, kpad);
451 	if (error) {
452 		dev_err(dev, "gpiochip_add failed: %d\n", error);
453 		return error;
454 	}
455 
456 	for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
457 		kpad->dat_out[i] = adp5588_read(kpad->client,
458 						GPIO_DAT_OUT1 + i);
459 		kpad->dir[i] = adp5588_read(kpad->client, GPIO_DIR1 + i);
460 		kpad->pull_dis[i] = adp5588_read(kpad->client, GPIO_PULL1 + i);
461 	}
462 
463 	return 0;
464 }
465 
adp5588_gpiomap_get_hwirq(struct device * dev,const u8 * map,unsigned int gpio,unsigned int ngpios)466 static unsigned long adp5588_gpiomap_get_hwirq(struct device *dev,
467 					       const u8 *map, unsigned int gpio,
468 					       unsigned int ngpios)
469 {
470 	unsigned int hwirq;
471 
472 	for (hwirq = 0; hwirq < ngpios; hwirq++)
473 		if (map[hwirq] == gpio)
474 			return hwirq;
475 
476 	/* should never happen */
477 	dev_warn_ratelimited(dev, "could not find the hwirq for gpio(%u)\n", gpio);
478 
479 	return ADP5588_INVALID_HWIRQ;
480 }
481 
adp5588_gpio_irq_handle(struct adp5588_kpad * kpad,int key_val,int key_press)482 static void adp5588_gpio_irq_handle(struct adp5588_kpad *kpad, int key_val,
483 				    int key_press)
484 {
485 	unsigned int irq, gpio = key_val - GPI_PIN_BASE, irq_type;
486 	struct i2c_client *client = kpad->client;
487 	struct irq_data *irqd;
488 	unsigned long hwirq;
489 
490 	hwirq = adp5588_gpiomap_get_hwirq(&client->dev, kpad->gpiomap,
491 					  gpio, kpad->gc.ngpio);
492 	if (hwirq == ADP5588_INVALID_HWIRQ) {
493 		dev_err(&client->dev, "Could not get hwirq for key(%u)\n", key_val);
494 		return;
495 	}
496 
497 	irq = irq_find_mapping(kpad->gc.irq.domain, hwirq);
498 	if (!irq)
499 		return;
500 
501 	irqd = irq_get_irq_data(irq);
502 	if (!irqd) {
503 		dev_err(&client->dev, "Could not get irq(%u) data\n", irq);
504 		return;
505 	}
506 
507 	irq_type = irqd_get_trigger_type(irqd);
508 
509 	/*
510 	 * Default is active low which means key_press is asserted on
511 	 * the falling edge.
512 	 */
513 	if ((irq_type & IRQ_TYPE_EDGE_RISING && !key_press) ||
514 	    (irq_type & IRQ_TYPE_EDGE_FALLING && key_press))
515 		handle_nested_irq(irq);
516 }
517 
adp5588_report_events(struct adp5588_kpad * kpad,int ev_cnt)518 static void adp5588_report_events(struct adp5588_kpad *kpad, int ev_cnt)
519 {
520 	int i;
521 
522 	for (i = 0; i < ev_cnt; i++) {
523 		int key = adp5588_read(kpad->client, KEY_EVENTA + i);
524 		int key_val = key & KEY_EV_MASK;
525 		int key_press = key & KEY_EV_PRESSED;
526 
527 		if (key_val >= GPI_PIN_BASE && key_val <= GPI_PIN_END) {
528 			/* gpio line used as IRQ source */
529 			adp5588_gpio_irq_handle(kpad, key_val, key_press);
530 		} else {
531 			int row = (key_val - 1) / ADP5588_COLS_MAX;
532 			int col =  (key_val - 1) % ADP5588_COLS_MAX;
533 			int code = MATRIX_SCAN_CODE(row, col, kpad->row_shift);
534 
535 			dev_dbg_ratelimited(&kpad->client->dev,
536 					    "report key(%d) r(%d) c(%d) code(%d)\n",
537 					    key_val, row, col, kpad->keycode[code]);
538 
539 			input_report_key(kpad->input,
540 					 kpad->keycode[code], key_press);
541 		}
542 	}
543 }
544 
adp5588_hard_irq(int irq,void * handle)545 static irqreturn_t adp5588_hard_irq(int irq, void *handle)
546 {
547 	struct adp5588_kpad *kpad = handle;
548 
549 	kpad->irq_time = ktime_get();
550 
551 	return IRQ_WAKE_THREAD;
552 }
553 
adp5588_thread_irq(int irq,void * handle)554 static irqreturn_t adp5588_thread_irq(int irq, void *handle)
555 {
556 	struct adp5588_kpad *kpad = handle;
557 	struct i2c_client *client = kpad->client;
558 	ktime_t target_time, now;
559 	unsigned long delay;
560 	int status, ev_cnt;
561 
562 	/*
563 	 * Readout needs to wait for at least 25ms after the notification
564 	 * for REVID < 4.
565 	 */
566 	if (kpad->delay) {
567 		target_time = ktime_add_ms(kpad->irq_time, kpad->delay);
568 		now = ktime_get();
569 		if (ktime_before(now, target_time)) {
570 			delay = ktime_to_us(ktime_sub(target_time, now));
571 			usleep_range(delay, delay + 1000);
572 		}
573 	}
574 
575 	status = adp5588_read(client, INT_STAT);
576 
577 	if (status & ADP5588_OVR_FLOW_INT)	/* Unlikely and should never happen */
578 		dev_err(&client->dev, "Event Overflow Error\n");
579 
580 	if (status & ADP5588_KE_INT) {
581 		ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & ADP5588_KEC;
582 		if (ev_cnt) {
583 			adp5588_report_events(kpad, ev_cnt);
584 			input_sync(kpad->input);
585 		}
586 	}
587 
588 	adp5588_write(client, INT_STAT, status); /* Status is W1C */
589 
590 	return IRQ_HANDLED;
591 }
592 
adp5588_setup(struct adp5588_kpad * kpad)593 static int adp5588_setup(struct adp5588_kpad *kpad)
594 {
595 	struct i2c_client *client = kpad->client;
596 	int i, ret;
597 
598 	ret = adp5588_write(client, KP_GPIO1, KP_SEL(kpad->rows));
599 	if (ret)
600 		return ret;
601 
602 	ret = adp5588_write(client, KP_GPIO2, KP_SEL(kpad->cols) & 0xFF);
603 	if (ret)
604 		return ret;
605 
606 	ret = adp5588_write(client, KP_GPIO3, KP_SEL(kpad->cols) >> 8);
607 	if (ret)
608 		return ret;
609 
610 	for (i = 0; i < kpad->nkeys_unlock; i++) {
611 		ret = adp5588_write(client, UNLOCK1 + i, kpad->unlock_keys[i]);
612 		if (ret)
613 			return ret;
614 	}
615 
616 	if (kpad->nkeys_unlock) {
617 		ret = adp5588_write(client, KEY_LCK_EC_STAT, ADP5588_K_LCK_EN);
618 		if (ret)
619 			return ret;
620 	}
621 
622 	for (i = 0; i < KEYP_MAX_EVENT; i++) {
623 		ret = adp5588_read(client, KEY_EVENTA);
624 		if (ret < 0)
625 			return ret;
626 	}
627 
628 	ret = adp5588_write(client, INT_STAT,
629 			    ADP5588_CMP2_INT | ADP5588_CMP1_INT |
630 			    ADP5588_OVR_FLOW_INT | ADP5588_K_LCK_INT |
631 			    ADP5588_GPI_INT | ADP5588_KE_INT); /* Status is W1C */
632 	if (ret)
633 		return ret;
634 
635 	return adp5588_write(client, CFG, ADP5588_INT_CFG |
636 			     ADP5588_OVR_FLOW_IEN | ADP5588_KE_IEN);
637 }
638 
adp5588_fw_parse(struct adp5588_kpad * kpad)639 static int adp5588_fw_parse(struct adp5588_kpad *kpad)
640 {
641 	struct i2c_client *client = kpad->client;
642 	int ret, i;
643 
644 	/*
645 	 * Check if the device is to be operated purely in GPIO mode. To do
646 	 * so, check that no keypad rows or columns have been specified,
647 	 * since all GPINS should be configured as GPIO.
648 	 */
649 	if (!device_property_present(&client->dev, "keypad,num-rows") &&
650 	    !device_property_present(&client->dev, "keypad,num-columns")) {
651 		/* If purely GPIO, skip keypad setup */
652 		kpad->gpio_only = true;
653 		return 0;
654 	}
655 
656 	ret = matrix_keypad_parse_properties(&client->dev, &kpad->rows,
657 					     &kpad->cols);
658 	if (ret)
659 		return ret;
660 
661 	if (kpad->rows > ADP5588_ROWS_MAX || kpad->cols > ADP5588_COLS_MAX) {
662 		dev_err(&client->dev, "Invalid nr of rows(%u) or cols(%u)\n",
663 			kpad->rows, kpad->cols);
664 		return -EINVAL;
665 	}
666 
667 	ret = matrix_keypad_build_keymap(NULL, NULL, kpad->rows, kpad->cols,
668 					 kpad->keycode, kpad->input);
669 	if (ret)
670 		return ret;
671 
672 	kpad->row_shift = get_count_order(kpad->cols);
673 
674 	if (device_property_read_bool(&client->dev, "autorepeat"))
675 		__set_bit(EV_REP, kpad->input->evbit);
676 
677 	kpad->nkeys_unlock = device_property_count_u32(&client->dev,
678 						       "adi,unlock-keys");
679 	if (kpad->nkeys_unlock <= 0) {
680 		/* so that we don't end up enabling key lock */
681 		kpad->nkeys_unlock = 0;
682 		return 0;
683 	}
684 
685 	if (kpad->nkeys_unlock > ARRAY_SIZE(kpad->unlock_keys)) {
686 		dev_err(&client->dev, "number of unlock keys(%d) > (%zu)\n",
687 			kpad->nkeys_unlock, ARRAY_SIZE(kpad->unlock_keys));
688 		return -EINVAL;
689 	}
690 
691 	ret = device_property_read_u32_array(&client->dev, "adi,unlock-keys",
692 					     kpad->unlock_keys,
693 					     kpad->nkeys_unlock);
694 	if (ret)
695 		return ret;
696 
697 	for (i = 0; i < kpad->nkeys_unlock; i++) {
698 		/*
699 		 * Even though it should be possible (as stated in the datasheet)
700 		 * to use GPIs (which are part of the keys event) as unlock keys,
701 		 * it was not working at all and was leading to overflow events
702 		 * at some point. Hence, for now, let's just allow keys which are
703 		 * part of keypad matrix to be used and if a reliable way of
704 		 * using GPIs is found, this condition can be removed/lightened.
705 		 */
706 		if (kpad->unlock_keys[i] >= kpad->cols * kpad->rows) {
707 			dev_err(&client->dev, "Invalid unlock key(%d)\n",
708 				kpad->unlock_keys[i]);
709 			return -EINVAL;
710 		}
711 
712 		/*
713 		 * Firmware properties keys start from 0 but on the device they
714 		 * start from 1.
715 		 */
716 		kpad->unlock_keys[i] += 1;
717 	}
718 
719 	return 0;
720 }
721 
adp5588_probe(struct i2c_client * client)722 static int adp5588_probe(struct i2c_client *client)
723 {
724 	struct adp5588_kpad *kpad;
725 	struct input_dev *input;
726 	struct gpio_desc *gpio;
727 	unsigned int revid;
728 	int ret;
729 	int error;
730 
731 	if (!i2c_check_functionality(client->adapter,
732 				     I2C_FUNC_SMBUS_BYTE_DATA)) {
733 		dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
734 		return -EIO;
735 	}
736 
737 	kpad = devm_kzalloc(&client->dev, sizeof(*kpad), GFP_KERNEL);
738 	if (!kpad)
739 		return -ENOMEM;
740 
741 	input = devm_input_allocate_device(&client->dev);
742 	if (!input)
743 		return -ENOMEM;
744 
745 	kpad->client = client;
746 	kpad->input = input;
747 
748 	error = adp5588_fw_parse(kpad);
749 	if (error)
750 		return error;
751 
752 	error = devm_regulator_get_enable(&client->dev, "vcc");
753 	if (error)
754 		return error;
755 
756 	gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
757 	if (IS_ERR(gpio))
758 		return PTR_ERR(gpio);
759 
760 	if (gpio) {
761 		fsleep(30);
762 		gpiod_set_value_cansleep(gpio, 0);
763 		fsleep(60);
764 	}
765 
766 	ret = adp5588_read(client, DEV_ID);
767 	if (ret < 0)
768 		return ret;
769 
770 	revid = ret & ADP5588_DEVICE_ID_MASK;
771 	if (WA_DELAYED_READOUT_REVID(revid))
772 		kpad->delay = msecs_to_jiffies(WA_DELAYED_READOUT_TIME);
773 
774 	input->name = client->name;
775 	input->phys = "adp5588-keys/input0";
776 
777 	input_set_drvdata(input, kpad);
778 
779 	input->id.bustype = BUS_I2C;
780 	input->id.vendor = 0x0001;
781 	input->id.product = 0x0001;
782 	input->id.version = revid;
783 
784 	error = input_register_device(input);
785 	if (error) {
786 		dev_err(&client->dev, "unable to register input device: %d\n",
787 			error);
788 		return error;
789 	}
790 
791 	error = adp5588_setup(kpad);
792 	if (error)
793 		return error;
794 
795 	error = adp5588_gpio_add(kpad);
796 	if (error)
797 		return error;
798 
799 	if (client->irq) {
800 		error = devm_request_threaded_irq(&client->dev, client->irq,
801 						  adp5588_hard_irq, adp5588_thread_irq,
802 						  IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
803 						  client->dev.driver->name, kpad);
804 		if (error) {
805 			dev_err(&client->dev, "failed to request irq %d: %d\n",
806 				client->irq, error);
807 			return error;
808 		}
809 	}
810 
811 	dev_info(&client->dev, "Rev.%d controller\n", revid);
812 	return 0;
813 }
814 
adp5588_remove(struct i2c_client * client)815 static void adp5588_remove(struct i2c_client *client)
816 {
817 	adp5588_write(client, CFG, 0);
818 
819 	/* all resources will be freed by devm */
820 }
821 
adp5588_suspend(struct device * dev)822 static int adp5588_suspend(struct device *dev)
823 {
824 	struct i2c_client *client = to_i2c_client(dev);
825 
826 	if (client->irq)
827 		disable_irq(client->irq);
828 
829 	return 0;
830 }
831 
adp5588_resume(struct device * dev)832 static int adp5588_resume(struct device *dev)
833 {
834 	struct i2c_client *client = to_i2c_client(dev);
835 
836 	if (client->irq)
837 		enable_irq(client->irq);
838 
839 	return 0;
840 }
841 
842 static DEFINE_SIMPLE_DEV_PM_OPS(adp5588_dev_pm_ops, adp5588_suspend, adp5588_resume);
843 
844 static const struct i2c_device_id adp5588_id[] = {
845 	{ "adp5588-keys" },
846 	{ "adp5587-keys" },
847 	{ }
848 };
849 MODULE_DEVICE_TABLE(i2c, adp5588_id);
850 
851 static const struct of_device_id adp5588_of_match[] = {
852 	{ .compatible = "adi,adp5588" },
853 	{ .compatible = "adi,adp5587" },
854 	{}
855 };
856 MODULE_DEVICE_TABLE(of, adp5588_of_match);
857 
858 static struct i2c_driver adp5588_driver = {
859 	.driver = {
860 		.name = KBUILD_MODNAME,
861 		.of_match_table = adp5588_of_match,
862 		.pm   = pm_sleep_ptr(&adp5588_dev_pm_ops),
863 	},
864 	.probe    = adp5588_probe,
865 	.remove   = adp5588_remove,
866 	.id_table = adp5588_id,
867 };
868 
869 module_i2c_driver(adp5588_driver);
870 
871 MODULE_LICENSE("GPL");
872 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
873 MODULE_DESCRIPTION("ADP5588/87 Keypad driver");
874