Linux Audio

Check our new training course

Loading...
v6.8
  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
181struct 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	unsigned short keycode[ADP5588_KEYMAPSIZE];
 
 
 
192	unsigned char gpiomap[ADP5588_MAXGPIO];
 
193	struct gpio_chip gc;
194	struct mutex gpio_lock;	/* Protect cached dir, dat_out */
195	u8 dat_out[3];
196	u8 dir[3];
197	u8 int_en[3];
198	u8 irq_mask[3];
199	u8 pull_dis[3];
200};
201
202static int adp5588_read(struct i2c_client *client, u8 reg)
203{
204	int ret = i2c_smbus_read_byte_data(client, reg);
205
206	if (ret < 0)
207		dev_err(&client->dev, "Read Error\n");
208
209	return ret;
210}
211
212static int adp5588_write(struct i2c_client *client, u8 reg, u8 val)
213{
214	return i2c_smbus_write_byte_data(client, reg, val);
215}
216
217static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned int off)
 
218{
219	struct adp5588_kpad *kpad = gpiochip_get_data(chip);
220	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
221	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
222	int val;
223
224	mutex_lock(&kpad->gpio_lock);
225
226	if (kpad->dir[bank] & bit)
227		val = kpad->dat_out[bank];
228	else
229		val = adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank);
230
231	mutex_unlock(&kpad->gpio_lock);
232
233	return !!(val & bit);
234}
235
236static void adp5588_gpio_set_value(struct gpio_chip *chip,
237				   unsigned int off, int val)
238{
239	struct adp5588_kpad *kpad = gpiochip_get_data(chip);
240	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
241	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
242
243	mutex_lock(&kpad->gpio_lock);
244
245	if (val)
246		kpad->dat_out[bank] |= bit;
247	else
248		kpad->dat_out[bank] &= ~bit;
249
250	adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, kpad->dat_out[bank]);
251
252	mutex_unlock(&kpad->gpio_lock);
253}
254
255static int adp5588_gpio_set_config(struct gpio_chip *chip,  unsigned int off,
256				   unsigned long config)
257{
258	struct adp5588_kpad *kpad = gpiochip_get_data(chip);
259	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
260	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
261	bool pull_disable;
262	int ret;
263
264	switch (pinconf_to_config_param(config)) {
265	case PIN_CONFIG_BIAS_PULL_UP:
266		pull_disable = false;
267		break;
268	case PIN_CONFIG_BIAS_DISABLE:
269		pull_disable = true;
270		break;
271	default:
272		return -ENOTSUPP;
273	}
274
275	mutex_lock(&kpad->gpio_lock);
276
277	if (pull_disable)
278		kpad->pull_dis[bank] |= bit;
279	else
280		kpad->pull_dis[bank] &= bit;
281
282	ret = adp5588_write(kpad->client, GPIO_PULL1 + bank,
283			    kpad->pull_dis[bank]);
284
285	mutex_unlock(&kpad->gpio_lock);
286
287	return ret;
288}
289
290static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned int off)
291{
292	struct adp5588_kpad *kpad = gpiochip_get_data(chip);
293	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
294	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
295	int ret;
296
297	mutex_lock(&kpad->gpio_lock);
298
299	kpad->dir[bank] &= ~bit;
300	ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
301
302	mutex_unlock(&kpad->gpio_lock);
303
304	return ret;
305}
306
307static int adp5588_gpio_direction_output(struct gpio_chip *chip,
308					 unsigned int off, int val)
309{
310	struct adp5588_kpad *kpad = gpiochip_get_data(chip);
311	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
312	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
313	int ret;
314
315	mutex_lock(&kpad->gpio_lock);
316
317	kpad->dir[bank] |= bit;
318
319	if (val)
320		kpad->dat_out[bank] |= bit;
321	else
322		kpad->dat_out[bank] &= ~bit;
323
324	ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
325			    kpad->dat_out[bank]);
326	if (ret)
327		goto out_unlock;
328
329	ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
330
331out_unlock:
332	mutex_unlock(&kpad->gpio_lock);
333
334	return ret;
335}
336
337static int adp5588_build_gpiomap(struct adp5588_kpad *kpad)
 
338{
339	bool pin_used[ADP5588_MAXGPIO];
340	int n_unused = 0;
341	int i;
342
343	memset(pin_used, 0, sizeof(pin_used));
344
345	for (i = 0; i < kpad->rows; i++)
346		pin_used[i] = true;
347
348	for (i = 0; i < kpad->cols; i++)
349		pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true;
350
 
 
 
351	for (i = 0; i < ADP5588_MAXGPIO; i++)
352		if (!pin_used[i])
353			kpad->gpiomap[n_unused++] = i;
354
355	return n_unused;
356}
357
358static void adp5588_irq_bus_lock(struct irq_data *d)
359{
360	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
361	struct adp5588_kpad *kpad = gpiochip_get_data(gc);
362
363	mutex_lock(&kpad->gpio_lock);
364}
365
366static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
367{
368	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
369	struct adp5588_kpad *kpad = gpiochip_get_data(gc);
370	int i;
371
372	for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
373		if (kpad->int_en[i] ^ kpad->irq_mask[i]) {
374			kpad->int_en[i] = kpad->irq_mask[i];
375			adp5588_write(kpad->client, GPI_EM1 + i, kpad->int_en[i]);
376		}
377	}
378
379	mutex_unlock(&kpad->gpio_lock);
380}
381
382static void adp5588_irq_mask(struct irq_data *d)
383{
384	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
385	struct adp5588_kpad *kpad = gpiochip_get_data(gc);
386	irq_hw_number_t hwirq = irqd_to_hwirq(d);
387	unsigned long real_irq = kpad->gpiomap[hwirq];
388
389	kpad->irq_mask[ADP5588_BANK(real_irq)] &= ~ADP5588_BIT(real_irq);
390	gpiochip_disable_irq(gc, hwirq);
391}
392
393static void adp5588_irq_unmask(struct irq_data *d)
394{
395	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
396	struct adp5588_kpad *kpad = gpiochip_get_data(gc);
397	irq_hw_number_t hwirq = irqd_to_hwirq(d);
398	unsigned long real_irq = kpad->gpiomap[hwirq];
399
400	gpiochip_enable_irq(gc, hwirq);
401	kpad->irq_mask[ADP5588_BANK(real_irq)] |= ADP5588_BIT(real_irq);
402}
403
404static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
405{
406	if (!(type & IRQ_TYPE_EDGE_BOTH))
407		return -EINVAL;
408
409	irq_set_handler_locked(d, handle_edge_irq);
410
411	return 0;
412}
413
414static const struct irq_chip adp5588_irq_chip = {
415	.name = "adp5588",
416	.irq_mask = adp5588_irq_mask,
417	.irq_unmask = adp5588_irq_unmask,
418	.irq_bus_lock = adp5588_irq_bus_lock,
419	.irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock,
420	.irq_set_type = adp5588_irq_set_type,
421	.flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_IMMUTABLE,
422	GPIOCHIP_IRQ_RESOURCE_HELPERS,
423};
424
425static int adp5588_gpio_add(struct adp5588_kpad *kpad)
426{
427	struct device *dev = &kpad->client->dev;
428	struct gpio_irq_chip *girq;
 
429	int i, error;
430
431	kpad->gc.ngpio = adp5588_build_gpiomap(kpad);
 
 
 
432	if (kpad->gc.ngpio == 0) {
433		dev_info(dev, "No unused gpios left to export\n");
434		return 0;
435	}
436
437	kpad->gc.parent = &kpad->client->dev;
 
438	kpad->gc.direction_input = adp5588_gpio_direction_input;
439	kpad->gc.direction_output = adp5588_gpio_direction_output;
440	kpad->gc.get = adp5588_gpio_get_value;
441	kpad->gc.set = adp5588_gpio_set_value;
442	kpad->gc.set_config = adp5588_gpio_set_config;
443	kpad->gc.can_sleep = 1;
444
445	kpad->gc.base = -1;
446	kpad->gc.label = kpad->client->name;
447	kpad->gc.owner = THIS_MODULE;
448
449	girq = &kpad->gc.irq;
450	gpio_irq_chip_set_chip(girq, &adp5588_irq_chip);
451	girq->handler = handle_bad_irq;
452	girq->threaded = true;
453
454	mutex_init(&kpad->gpio_lock);
455
456	error = devm_gpiochip_add_data(dev, &kpad->gc, kpad);
457	if (error) {
458		dev_err(dev, "gpiochip_add failed: %d\n", error);
459		return error;
460	}
461
462	for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
463		kpad->dat_out[i] = adp5588_read(kpad->client,
464						GPIO_DAT_OUT1 + i);
465		kpad->dir[i] = adp5588_read(kpad->client, GPIO_DIR1 + i);
466		kpad->pull_dis[i] = adp5588_read(kpad->client, GPIO_PULL1 + i);
467	}
468
469	return 0;
470}
471
472static unsigned long adp5588_gpiomap_get_hwirq(struct device *dev,
473					       const u8 *map, unsigned int gpio,
474					       unsigned int ngpios)
475{
476	unsigned int hwirq;
477
478	for (hwirq = 0; hwirq < ngpios; hwirq++)
479		if (map[hwirq] == gpio)
480			return hwirq;
481
482	/* should never happen */
483	dev_warn_ratelimited(dev, "could not find the hwirq for gpio(%u)\n", gpio);
484
485	return ADP5588_INVALID_HWIRQ;
486}
487
488static void adp5588_gpio_irq_handle(struct adp5588_kpad *kpad, int key_val,
489				    int key_press)
490{
491	unsigned int irq, gpio = key_val - GPI_PIN_BASE, irq_type;
492	struct i2c_client *client = kpad->client;
493	struct irq_data *irqd;
494	unsigned long hwirq;
495
496	hwirq = adp5588_gpiomap_get_hwirq(&client->dev, kpad->gpiomap,
497					  gpio, kpad->gc.ngpio);
498	if (hwirq == ADP5588_INVALID_HWIRQ) {
499		dev_err(&client->dev, "Could not get hwirq for key(%u)\n", key_val);
500		return;
501	}
502
503	irq = irq_find_mapping(kpad->gc.irq.domain, hwirq);
504	if (!irq)
505		return;
506
507	irqd = irq_get_irq_data(irq);
508	if (!irqd) {
509		dev_err(&client->dev, "Could not get irq(%u) data\n", irq);
510		return;
 
 
511	}
512
513	irq_type = irqd_get_trigger_type(irqd);
 
 
 
 
 
 
514
515	/*
516	 * Default is active low which means key_press is asserted on
517	 * the falling edge.
518	 */
519	if ((irq_type & IRQ_TYPE_EDGE_RISING && !key_press) ||
520	    (irq_type & IRQ_TYPE_EDGE_FALLING && key_press))
521		handle_nested_irq(irq);
522}
 
523
524static void adp5588_report_events(struct adp5588_kpad *kpad, int ev_cnt)
525{
526	int i;
527
528	for (i = 0; i < ev_cnt; i++) {
529		int key = adp5588_read(kpad->client, KEY_EVENTA + i);
530		int key_val = key & KEY_EV_MASK;
531		int key_press = key & KEY_EV_PRESSED;
532
533		if (key_val >= GPI_PIN_BASE && key_val <= GPI_PIN_END) {
534			/* gpio line used as IRQ source */
535			adp5588_gpio_irq_handle(kpad, key_val, key_press);
 
 
 
 
 
 
536		} else {
537			int row = (key_val - 1) / ADP5588_COLS_MAX;
538			int col =  (key_val - 1) % ADP5588_COLS_MAX;
539			int code = MATRIX_SCAN_CODE(row, col, kpad->row_shift);
540
541			dev_dbg_ratelimited(&kpad->client->dev,
542					    "report key(%d) r(%d) c(%d) code(%d)\n",
543					    key_val, row, col, kpad->keycode[code]);
544
545			input_report_key(kpad->input,
546					 kpad->keycode[code], key_press);
 
547		}
548	}
549}
550
551static irqreturn_t adp5588_hard_irq(int irq, void *handle)
552{
553	struct adp5588_kpad *kpad = handle;
554
555	kpad->irq_time = ktime_get();
556
557	return IRQ_WAKE_THREAD;
558}
559
560static irqreturn_t adp5588_thread_irq(int irq, void *handle)
561{
562	struct adp5588_kpad *kpad = handle;
563	struct i2c_client *client = kpad->client;
564	ktime_t target_time, now;
565	unsigned long delay;
566	int status, ev_cnt;
567
568	/*
569	 * Readout needs to wait for at least 25ms after the notification
570	 * for REVID < 4.
571	 */
572	if (kpad->delay) {
573		target_time = ktime_add_ms(kpad->irq_time, kpad->delay);
574		now = ktime_get();
575		if (ktime_before(now, target_time)) {
576			delay = ktime_to_us(ktime_sub(target_time, now));
577			usleep_range(delay, delay + 1000);
578		}
579	}
580
581	status = adp5588_read(client, INT_STAT);
582
583	if (status & ADP5588_OVR_FLOW_INT)	/* Unlikely and should never happen */
584		dev_err(&client->dev, "Event Overflow Error\n");
585
586	if (status & ADP5588_KE_INT) {
587		ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & ADP5588_KEC;
588		if (ev_cnt) {
589			adp5588_report_events(kpad, ev_cnt);
590			input_sync(kpad->input);
591		}
592	}
593
594	adp5588_write(client, INT_STAT, status); /* Status is W1C */
 
 
 
 
 
 
 
 
 
 
 
 
 
595
596	return IRQ_HANDLED;
597}
598
599static int adp5588_setup(struct adp5588_kpad *kpad)
600{
601	struct i2c_client *client = kpad->client;
 
 
602	int i, ret;
 
603
604	ret = adp5588_write(client, KP_GPIO1, KP_SEL(kpad->rows));
605	if (ret)
606		return ret;
607
608	ret = adp5588_write(client, KP_GPIO2, KP_SEL(kpad->cols) & 0xFF);
609	if (ret)
610		return ret;
 
 
611
612	ret = adp5588_write(client, KP_GPIO3, KP_SEL(kpad->cols) >> 8);
613	if (ret)
614		return ret;
615
616	for (i = 0; i < kpad->nkeys_unlock; i++) {
617		ret = adp5588_write(client, UNLOCK1 + i, kpad->unlock_keys[i]);
618		if (ret)
619			return ret;
620	}
621
622	if (kpad->nkeys_unlock) {
623		ret = adp5588_write(client, KEY_LCK_EC_STAT, ADP5588_K_LCK_EN);
624		if (ret)
625			return ret;
 
 
626	}
627
628	for (i = 0; i < KEYP_MAX_EVENT; i++) {
629		ret = adp5588_read(client, KEY_EVENTA);
630		if (ret)
631			return ret;
632	}
633
634	ret = adp5588_write(client, INT_STAT,
635			    ADP5588_CMP2_INT | ADP5588_CMP1_INT |
636			    ADP5588_OVR_FLOW_INT | ADP5588_K_LCK_INT |
637			    ADP5588_GPI_INT | ADP5588_KE_INT); /* Status is W1C */
638	if (ret)
639		return ret;
640
641	return adp5588_write(client, CFG, ADP5588_INT_CFG |
642			     ADP5588_OVR_FLOW_IEN | ADP5588_KE_IEN);
643}
 
644
645static int adp5588_fw_parse(struct adp5588_kpad *kpad)
646{
647	struct i2c_client *client = kpad->client;
648	int ret, i;
 
 
 
 
649
650	ret = matrix_keypad_parse_properties(&client->dev, &kpad->rows,
651					     &kpad->cols);
652	if (ret)
653		return ret;
654
655	if (kpad->rows > ADP5588_ROWS_MAX || kpad->cols > ADP5588_COLS_MAX) {
656		dev_err(&client->dev, "Invalid nr of rows(%u) or cols(%u)\n",
657			kpad->rows, kpad->cols);
658		return -EINVAL;
659	}
660
661	ret = matrix_keypad_build_keymap(NULL, NULL, kpad->rows, kpad->cols,
662					 kpad->keycode, kpad->input);
663	if (ret)
664		return ret;
665
666	kpad->row_shift = get_count_order(kpad->cols);
667
668	if (device_property_read_bool(&client->dev, "autorepeat"))
669		__set_bit(EV_REP, kpad->input->evbit);
670
671	kpad->nkeys_unlock = device_property_count_u32(&client->dev,
672						       "adi,unlock-keys");
673	if (kpad->nkeys_unlock <= 0) {
674		/* so that we don't end up enabling key lock */
675		kpad->nkeys_unlock = 0;
676		return 0;
677	}
678
679	if (kpad->nkeys_unlock > ARRAY_SIZE(kpad->unlock_keys)) {
680		dev_err(&client->dev, "number of unlock keys(%d) > (%zu)\n",
681			kpad->nkeys_unlock, ARRAY_SIZE(kpad->unlock_keys));
682		return -EINVAL;
683	}
684
685	ret = device_property_read_u32_array(&client->dev, "adi,unlock-keys",
686					     kpad->unlock_keys,
687					     kpad->nkeys_unlock);
688	if (ret)
689		return ret;
 
 
 
 
 
690
691	for (i = 0; i < kpad->nkeys_unlock; i++) {
692		/*
693		 * Even though it should be possible (as stated in the datasheet)
694		 * to use GPIs (which are part of the keys event) as unlock keys,
695		 * it was not working at all and was leading to overflow events
696		 * at some point. Hence, for now, let's just allow keys which are
697		 * part of keypad matrix to be used and if a reliable way of
698		 * using GPIs is found, this condition can be removed/lightened.
699		 */
700		if (kpad->unlock_keys[i] >= kpad->cols * kpad->rows) {
701			dev_err(&client->dev, "Invalid unlock key(%d)\n",
702				kpad->unlock_keys[i]);
703			return -EINVAL;
704		}
705
706		/*
707		 * Firmware properties keys start from 0 but on the device they
708		 * start from 1.
709		 */
710		kpad->unlock_keys[i] += 1;
711	}
712
713	return 0;
714}
715
716static int adp5588_probe(struct i2c_client *client)
 
 
717{
718	struct adp5588_kpad *kpad;
 
 
719	struct input_dev *input;
720	struct gpio_desc *gpio;
721	unsigned int revid;
722	int ret;
723	int error;
724
725	if (!i2c_check_functionality(client->adapter,
726				     I2C_FUNC_SMBUS_BYTE_DATA)) {
727		dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
728		return -EIO;
729	}
730
731	kpad = devm_kzalloc(&client->dev, sizeof(*kpad), GFP_KERNEL);
732	if (!kpad)
733		return -ENOMEM;
734
735	input = devm_input_allocate_device(&client->dev);
736	if (!input)
737		return -ENOMEM;
738
739	kpad->client = client;
740	kpad->input = input;
 
 
741
742	error = adp5588_fw_parse(kpad);
743	if (error)
744		return error;
 
745
746	error = devm_regulator_get_enable(&client->dev, "vcc");
747	if (error)
748		return error;
 
749
750	gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
751	if (IS_ERR(gpio))
752		return PTR_ERR(gpio);
753
754	if (gpio) {
755		fsleep(30);
756		gpiod_set_value_cansleep(gpio, 0);
757		fsleep(60);
758	}
759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760	ret = adp5588_read(client, DEV_ID);
761	if (ret < 0)
762		return ret;
 
 
763
764	revid = ret & ADP5588_DEVICE_ID_MASK;
765	if (WA_DELAYED_READOUT_REVID(revid))
766		kpad->delay = msecs_to_jiffies(WA_DELAYED_READOUT_TIME);
767
768	input->name = client->name;
769	input->phys = "adp5588-keys/input0";
 
770
771	input_set_drvdata(input, kpad);
772
773	input->id.bustype = BUS_I2C;
774	input->id.vendor = 0x0001;
775	input->id.product = 0x0001;
776	input->id.version = revid;
777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
778	error = input_register_device(input);
779	if (error) {
780		dev_err(&client->dev, "unable to register input device: %d\n",
781			error);
782		return error;
783	}
784
785	error = adp5588_setup(kpad);
 
 
 
 
 
 
 
 
786	if (error)
787		return error;
 
 
 
788
789	error = adp5588_gpio_add(kpad);
790	if (error)
791		return error;
792
793	error = devm_request_threaded_irq(&client->dev, client->irq,
794					  adp5588_hard_irq, adp5588_thread_irq,
795					  IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
796					  client->dev.driver->name, kpad);
797	if (error) {
798		dev_err(&client->dev, "failed to request irq %d: %d\n",
799			client->irq, error);
800		return error;
801	}
802
803	dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
804	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
805}
806
807static void adp5588_remove(struct i2c_client *client)
808{
 
 
809	adp5588_write(client, CFG, 0);
 
 
 
 
 
810
811	/* all resources will be freed by devm */
812}
813
 
814static int adp5588_suspend(struct device *dev)
815{
816	struct i2c_client *client = to_i2c_client(dev);
 
817
818	disable_irq(client->irq);
 
 
 
 
819
820	return 0;
821}
822
823static int adp5588_resume(struct device *dev)
824{
825	struct i2c_client *client = to_i2c_client(dev);
 
 
 
 
826
827	enable_irq(client->irq);
828
829	return 0;
830}
831
832static DEFINE_SIMPLE_DEV_PM_OPS(adp5588_dev_pm_ops, adp5588_suspend, adp5588_resume);
 
 
 
 
833
834static const struct i2c_device_id adp5588_id[] = {
835	{ "adp5588-keys", 0 },
836	{ "adp5587-keys", 0 },
837	{ }
838};
839MODULE_DEVICE_TABLE(i2c, adp5588_id);
840
841static const struct of_device_id adp5588_of_match[] = {
842	{ .compatible = "adi,adp5588" },
843	{ .compatible = "adi,adp5587" },
844	{}
845};
846MODULE_DEVICE_TABLE(of, adp5588_of_match);
847
848static struct i2c_driver adp5588_driver = {
849	.driver = {
850		.name = KBUILD_MODNAME,
851		.of_match_table = adp5588_of_match,
852		.pm   = pm_sleep_ptr(&adp5588_dev_pm_ops),
 
853	},
854	.probe    = adp5588_probe,
855	.remove   = adp5588_remove,
856	.id_table = adp5588_id,
857};
858
859module_i2c_driver(adp5588_driver);
860
861MODULE_LICENSE("GPL");
862MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
863MODULE_DESCRIPTION("ADP5588/87 Keypad driver");
v4.6
 
  1/*
  2 * File: drivers/input/keyboard/adp5588_keys.c
  3 * Description:  keypad driver for ADP5588 and ADP5587
  4 *		 I2C QWERTY Keypad and IO Expander
  5 * Bugs: Enter bugs at http://blackfin.uclinux.org/
  6 *
  7 * Copyright (C) 2008-2010 Analog Devices Inc.
  8 * Licensed under the GPL-2 or later.
  9 */
 10
 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/platform_device.h>
 18#include <linux/input.h>
 19#include <linux/i2c.h>
 20#include <linux/gpio.h>
 21#include <linux/slab.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 22
 23#include <linux/i2c/adp5588.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 24
 25/* Key Event Register xy */
 26#define KEY_EV_PRESSED		(1 << 7)
 27#define KEY_EV_MASK		(0x7F)
 28
 29#define KP_SEL(x)		(0xFFFF >> (16 - x))	/* 2^x-1 */
 30
 31#define KEYP_MAX_EVENT		10
 32
 33/*
 34 * Early pre 4.0 Silicon required to delay readout by at least 25ms,
 35 * since the Event Counter Register updated 25ms after the interrupt
 36 * asserted.
 37 */
 38#define WA_DELAYED_READOUT_REVID(rev)		((rev) < 4)
 
 
 
 39
 40struct adp5588_kpad {
 41	struct i2c_client *client;
 42	struct input_dev *input;
 43	struct delayed_work work;
 44	unsigned long delay;
 
 
 
 
 
 45	unsigned short keycode[ADP5588_KEYMAPSIZE];
 46	const struct adp5588_gpi_map *gpimap;
 47	unsigned short gpimapsize;
 48#ifdef CONFIG_GPIOLIB
 49	unsigned char gpiomap[ADP5588_MAXGPIO];
 50	bool export_gpio;
 51	struct gpio_chip gc;
 52	struct mutex gpio_lock;	/* Protect cached dir, dat_out */
 53	u8 dat_out[3];
 54	u8 dir[3];
 55#endif
 
 
 56};
 57
 58static int adp5588_read(struct i2c_client *client, u8 reg)
 59{
 60	int ret = i2c_smbus_read_byte_data(client, reg);
 61
 62	if (ret < 0)
 63		dev_err(&client->dev, "Read Error\n");
 64
 65	return ret;
 66}
 67
 68static int adp5588_write(struct i2c_client *client, u8 reg, u8 val)
 69{
 70	return i2c_smbus_write_byte_data(client, reg, val);
 71}
 72
 73#ifdef CONFIG_GPIOLIB
 74static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
 75{
 76	struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
 77	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
 78	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
 79	int val;
 80
 81	mutex_lock(&kpad->gpio_lock);
 82
 83	if (kpad->dir[bank] & bit)
 84		val = kpad->dat_out[bank];
 85	else
 86		val = adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank);
 87
 88	mutex_unlock(&kpad->gpio_lock);
 89
 90	return !!(val & bit);
 91}
 92
 93static void adp5588_gpio_set_value(struct gpio_chip *chip,
 94				   unsigned off, int val)
 95{
 96	struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
 97	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
 98	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
 99
100	mutex_lock(&kpad->gpio_lock);
101
102	if (val)
103		kpad->dat_out[bank] |= bit;
104	else
105		kpad->dat_out[bank] &= ~bit;
106
107	adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
108			   kpad->dat_out[bank]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
110	mutex_unlock(&kpad->gpio_lock);
 
 
111}
112
113static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
114{
115	struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
116	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
117	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
118	int ret;
119
120	mutex_lock(&kpad->gpio_lock);
121
122	kpad->dir[bank] &= ~bit;
123	ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
124
125	mutex_unlock(&kpad->gpio_lock);
126
127	return ret;
128}
129
130static int adp5588_gpio_direction_output(struct gpio_chip *chip,
131					 unsigned off, int val)
132{
133	struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
134	unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
135	unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
136	int ret;
137
138	mutex_lock(&kpad->gpio_lock);
139
140	kpad->dir[bank] |= bit;
141
142	if (val)
143		kpad->dat_out[bank] |= bit;
144	else
145		kpad->dat_out[bank] &= ~bit;
146
147	ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
148				 kpad->dat_out[bank]);
149	ret |= adp5588_write(kpad->client, GPIO_DIR1 + bank,
150				 kpad->dir[bank]);
 
 
151
 
152	mutex_unlock(&kpad->gpio_lock);
153
154	return ret;
155}
156
157static int adp5588_build_gpiomap(struct adp5588_kpad *kpad,
158				const struct adp5588_kpad_platform_data *pdata)
159{
160	bool pin_used[ADP5588_MAXGPIO];
161	int n_unused = 0;
162	int i;
163
164	memset(pin_used, 0, sizeof(pin_used));
165
166	for (i = 0; i < pdata->rows; i++)
167		pin_used[i] = true;
168
169	for (i = 0; i < pdata->cols; i++)
170		pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true;
171
172	for (i = 0; i < kpad->gpimapsize; i++)
173		pin_used[kpad->gpimap[i].pin - GPI_PIN_BASE] = true;
174
175	for (i = 0; i < ADP5588_MAXGPIO; i++)
176		if (!pin_used[i])
177			kpad->gpiomap[n_unused++] = i;
178
179	return n_unused;
180}
181
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182static int adp5588_gpio_add(struct adp5588_kpad *kpad)
183{
184	struct device *dev = &kpad->client->dev;
185	const struct adp5588_kpad_platform_data *pdata = dev_get_platdata(dev);
186	const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
187	int i, error;
188
189	if (!gpio_data)
190		return 0;
191
192	kpad->gc.ngpio = adp5588_build_gpiomap(kpad, pdata);
193	if (kpad->gc.ngpio == 0) {
194		dev_info(dev, "No unused gpios left to export\n");
195		return 0;
196	}
197
198	kpad->export_gpio = true;
199
200	kpad->gc.direction_input = adp5588_gpio_direction_input;
201	kpad->gc.direction_output = adp5588_gpio_direction_output;
202	kpad->gc.get = adp5588_gpio_get_value;
203	kpad->gc.set = adp5588_gpio_set_value;
 
204	kpad->gc.can_sleep = 1;
205
206	kpad->gc.base = gpio_data->gpio_start;
207	kpad->gc.label = kpad->client->name;
208	kpad->gc.owner = THIS_MODULE;
209	kpad->gc.names = gpio_data->names;
 
 
 
 
210
211	mutex_init(&kpad->gpio_lock);
212
213	error = gpiochip_add(&kpad->gc);
214	if (error) {
215		dev_err(dev, "gpiochip_add failed, err: %d\n", error);
216		return error;
217	}
218
219	for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
220		kpad->dat_out[i] = adp5588_read(kpad->client,
221						GPIO_DAT_OUT1 + i);
222		kpad->dir[i] = adp5588_read(kpad->client, GPIO_DIR1 + i);
 
223	}
224
225	if (gpio_data->setup) {
226		error = gpio_data->setup(kpad->client,
227					 kpad->gc.base, kpad->gc.ngpio,
228					 gpio_data->context);
229		if (error)
230			dev_warn(dev, "setup failed, %d\n", error);
231	}
 
 
 
 
 
 
 
 
232
233	return 0;
234}
235
236static void adp5588_gpio_remove(struct adp5588_kpad *kpad)
 
237{
238	struct device *dev = &kpad->client->dev;
239	const struct adp5588_kpad_platform_data *pdata = dev_get_platdata(dev);
240	const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
241	int error;
 
 
 
 
 
 
 
242
243	if (!kpad->export_gpio)
 
244		return;
245
246	if (gpio_data->teardown) {
247		error = gpio_data->teardown(kpad->client,
248					    kpad->gc.base, kpad->gc.ngpio,
249					    gpio_data->context);
250		if (error)
251			dev_warn(dev, "teardown failed %d\n", error);
252	}
253
254	gpiochip_remove(&kpad->gc);
255}
256#else
257static inline int adp5588_gpio_add(struct adp5588_kpad *kpad)
258{
259	return 0;
260}
261
262static inline void adp5588_gpio_remove(struct adp5588_kpad *kpad)
263{
 
 
 
 
 
264}
265#endif
266
267static void adp5588_report_events(struct adp5588_kpad *kpad, int ev_cnt)
268{
269	int i, j;
270
271	for (i = 0; i < ev_cnt; i++) {
272		int key = adp5588_read(kpad->client, Key_EVENTA + i);
273		int key_val = key & KEY_EV_MASK;
 
274
275		if (key_val >= GPI_PIN_BASE && key_val <= GPI_PIN_END) {
276			for (j = 0; j < kpad->gpimapsize; j++) {
277				if (key_val == kpad->gpimap[j].pin) {
278					input_report_switch(kpad->input,
279							kpad->gpimap[j].sw_evt,
280							key & KEY_EV_PRESSED);
281					break;
282				}
283			}
284		} else {
 
 
 
 
 
 
 
 
285			input_report_key(kpad->input,
286					 kpad->keycode[key_val - 1],
287					 key & KEY_EV_PRESSED);
288		}
289	}
290}
291
292static void adp5588_work(struct work_struct *work)
293{
294	struct adp5588_kpad *kpad = container_of(work,
295						struct adp5588_kpad, work.work);
 
 
 
 
 
 
 
 
296	struct i2c_client *client = kpad->client;
 
 
297	int status, ev_cnt;
298
 
 
 
 
 
 
 
 
 
 
 
 
 
299	status = adp5588_read(client, INT_STAT);
300
301	if (status & ADP5588_OVR_FLOW_INT)	/* Unlikely and should never happen */
302		dev_err(&client->dev, "Event Overflow Error\n");
303
304	if (status & ADP5588_KE_INT) {
305		ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & ADP5588_KEC;
306		if (ev_cnt) {
307			adp5588_report_events(kpad, ev_cnt);
308			input_sync(kpad->input);
309		}
310	}
 
311	adp5588_write(client, INT_STAT, status); /* Status is W1C */
312}
313
314static irqreturn_t adp5588_irq(int irq, void *handle)
315{
316	struct adp5588_kpad *kpad = handle;
317
318	/*
319	 * use keventd context to read the event fifo registers
320	 * Schedule readout at least 25ms after notification for
321	 * REVID < 4
322	 */
323
324	schedule_delayed_work(&kpad->work, kpad->delay);
325
326	return IRQ_HANDLED;
327}
328
329static int adp5588_setup(struct i2c_client *client)
330{
331	const struct adp5588_kpad_platform_data *pdata =
332			dev_get_platdata(&client->dev);
333	const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
334	int i, ret;
335	unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
336
337	ret = adp5588_write(client, KP_GPIO1, KP_SEL(pdata->rows));
338	ret |= adp5588_write(client, KP_GPIO2, KP_SEL(pdata->cols) & 0xFF);
339	ret |= adp5588_write(client, KP_GPIO3, KP_SEL(pdata->cols) >> 8);
340
341	if (pdata->en_keylock) {
342		ret |= adp5588_write(client, UNLOCK1, pdata->unlock_key1);
343		ret |= adp5588_write(client, UNLOCK2, pdata->unlock_key2);
344		ret |= adp5588_write(client, KEY_LCK_EC_STAT, ADP5588_K_LCK_EN);
345	}
346
347	for (i = 0; i < KEYP_MAX_EVENT; i++)
348		ret |= adp5588_read(client, Key_EVENTA);
 
349
350	for (i = 0; i < pdata->gpimapsize; i++) {
351		unsigned short pin = pdata->gpimap[i].pin;
 
 
 
352
353		if (pin <= GPI_PIN_ROW_END) {
354			evt_mode1 |= (1 << (pin - GPI_PIN_ROW_BASE));
355		} else {
356			evt_mode2 |= ((1 << (pin - GPI_PIN_COL_BASE)) & 0xFF);
357			evt_mode3 |= ((1 << (pin - GPI_PIN_COL_BASE)) >> 8);
358		}
359	}
360
361	if (pdata->gpimapsize) {
362		ret |= adp5588_write(client, GPI_EM1, evt_mode1);
363		ret |= adp5588_write(client, GPI_EM2, evt_mode2);
364		ret |= adp5588_write(client, GPI_EM3, evt_mode3);
365	}
366
367	if (gpio_data) {
368		for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
369			int pull_mask = gpio_data->pullup_dis_mask;
 
 
 
370
371			ret |= adp5588_write(client, GPIO_PULL1 + i,
372				(pull_mask >> (8 * i)) & 0xFF);
373		}
374	}
375
376	ret |= adp5588_write(client, INT_STAT,
377				ADP5588_CMP2_INT | ADP5588_CMP1_INT |
378				ADP5588_OVR_FLOW_INT | ADP5588_K_LCK_INT |
379				ADP5588_GPI_INT | ADP5588_KE_INT); /* Status is W1C */
380
381	ret |= adp5588_write(client, CFG, ADP5588_INT_CFG |
382					  ADP5588_OVR_FLOW_IEN |
383					  ADP5588_KE_IEN);
384
385	if (ret < 0) {
386		dev_err(&client->dev, "Write Error\n");
 
387		return ret;
 
 
 
 
 
388	}
389
390	return 0;
391}
 
 
 
 
 
 
 
392
393static void adp5588_report_switch_state(struct adp5588_kpad *kpad)
394{
395	int gpi_stat1 = adp5588_read(kpad->client, GPIO_DAT_STAT1);
396	int gpi_stat2 = adp5588_read(kpad->client, GPIO_DAT_STAT2);
397	int gpi_stat3 = adp5588_read(kpad->client, GPIO_DAT_STAT3);
398	int gpi_stat_tmp, pin_loc;
399	int i;
400
401	for (i = 0; i < kpad->gpimapsize; i++) {
402		unsigned short pin = kpad->gpimap[i].pin;
 
 
 
403
404		if (pin <= GPI_PIN_ROW_END) {
405			gpi_stat_tmp = gpi_stat1;
406			pin_loc = pin - GPI_PIN_ROW_BASE;
407		} else if ((pin - GPI_PIN_COL_BASE) < 8) {
408			gpi_stat_tmp = gpi_stat2;
409			pin_loc = pin - GPI_PIN_COL_BASE;
410		} else {
411			gpi_stat_tmp = gpi_stat3;
412			pin_loc = pin - GPI_PIN_COL_BASE - 8;
413		}
414
415		if (gpi_stat_tmp < 0) {
416			dev_err(&kpad->client->dev,
417				"Can't read GPIO_DAT_STAT switch %d default to OFF\n",
418				pin);
419			gpi_stat_tmp = 0;
 
 
 
 
 
 
 
 
420		}
421
422		input_report_switch(kpad->input,
423				    kpad->gpimap[i].sw_evt,
424				    !(gpi_stat_tmp & (1 << pin_loc)));
 
 
425	}
426
427	input_sync(kpad->input);
428}
429
430
431static int adp5588_probe(struct i2c_client *client,
432			 const struct i2c_device_id *id)
433{
434	struct adp5588_kpad *kpad;
435	const struct adp5588_kpad_platform_data *pdata =
436			dev_get_platdata(&client->dev);
437	struct input_dev *input;
 
438	unsigned int revid;
439	int ret, i;
440	int error;
441
442	if (!i2c_check_functionality(client->adapter,
443					I2C_FUNC_SMBUS_BYTE_DATA)) {
444		dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
445		return -EIO;
446	}
447
448	if (!pdata) {
449		dev_err(&client->dev, "no platform data?\n");
450		return -EINVAL;
451	}
 
 
 
452
453	if (!pdata->rows || !pdata->cols || !pdata->keymap) {
454		dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
455		return -EINVAL;
456	}
457
458	if (pdata->keymapsize != ADP5588_KEYMAPSIZE) {
459		dev_err(&client->dev, "invalid keymapsize\n");
460		return -EINVAL;
461	}
462
463	if (!pdata->gpimap && pdata->gpimapsize) {
464		dev_err(&client->dev, "invalid gpimap from pdata\n");
465		return -EINVAL;
466	}
467
468	if (pdata->gpimapsize > ADP5588_GPIMAPSIZE_MAX) {
469		dev_err(&client->dev, "invalid gpimapsize\n");
470		return -EINVAL;
 
 
 
 
 
471	}
472
473	for (i = 0; i < pdata->gpimapsize; i++) {
474		unsigned short pin = pdata->gpimap[i].pin;
475
476		if (pin < GPI_PIN_BASE || pin > GPI_PIN_END) {
477			dev_err(&client->dev, "invalid gpi pin data\n");
478			return -EINVAL;
479		}
480
481		if (pin <= GPI_PIN_ROW_END) {
482			if (pin - GPI_PIN_ROW_BASE + 1 <= pdata->rows) {
483				dev_err(&client->dev, "invalid gpi row data\n");
484				return -EINVAL;
485			}
486		} else {
487			if (pin - GPI_PIN_COL_BASE + 1 <= pdata->cols) {
488				dev_err(&client->dev, "invalid gpi col data\n");
489				return -EINVAL;
490			}
491		}
492	}
493
494	if (!client->irq) {
495		dev_err(&client->dev, "no IRQ?\n");
496		return -EINVAL;
497	}
498
499	kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
500	input = input_allocate_device();
501	if (!kpad || !input) {
502		error = -ENOMEM;
503		goto err_free_mem;
504	}
505
506	kpad->client = client;
507	kpad->input = input;
508	INIT_DELAYED_WORK(&kpad->work, adp5588_work);
509
510	ret = adp5588_read(client, DEV_ID);
511	if (ret < 0) {
512		error = ret;
513		goto err_free_mem;
514	}
515
516	revid = (u8) ret & ADP5588_DEVICE_ID_MASK;
517	if (WA_DELAYED_READOUT_REVID(revid))
518		kpad->delay = msecs_to_jiffies(30);
519
520	input->name = client->name;
521	input->phys = "adp5588-keys/input0";
522	input->dev.parent = &client->dev;
523
524	input_set_drvdata(input, kpad);
525
526	input->id.bustype = BUS_I2C;
527	input->id.vendor = 0x0001;
528	input->id.product = 0x0001;
529	input->id.version = revid;
530
531	input->keycodesize = sizeof(kpad->keycode[0]);
532	input->keycodemax = pdata->keymapsize;
533	input->keycode = kpad->keycode;
534
535	memcpy(kpad->keycode, pdata->keymap,
536		pdata->keymapsize * input->keycodesize);
537
538	kpad->gpimap = pdata->gpimap;
539	kpad->gpimapsize = pdata->gpimapsize;
540
541	/* setup input device */
542	__set_bit(EV_KEY, input->evbit);
543
544	if (pdata->repeat)
545		__set_bit(EV_REP, input->evbit);
546
547	for (i = 0; i < input->keycodemax; i++)
548		if (kpad->keycode[i] <= KEY_MAX)
549			__set_bit(kpad->keycode[i], input->keybit);
550	__clear_bit(KEY_RESERVED, input->keybit);
551
552	if (kpad->gpimapsize)
553		__set_bit(EV_SW, input->evbit);
554	for (i = 0; i < kpad->gpimapsize; i++)
555		__set_bit(kpad->gpimap[i].sw_evt, input->swbit);
556
557	error = input_register_device(input);
558	if (error) {
559		dev_err(&client->dev, "unable to register input device\n");
560		goto err_free_mem;
 
561	}
562
563	error = request_irq(client->irq, adp5588_irq,
564			    IRQF_TRIGGER_FALLING,
565			    client->dev.driver->name, kpad);
566	if (error) {
567		dev_err(&client->dev, "irq %d busy?\n", client->irq);
568		goto err_unreg_dev;
569	}
570
571	error = adp5588_setup(client);
572	if (error)
573		goto err_free_irq;
574
575	if (kpad->gpimapsize)
576		adp5588_report_switch_state(kpad);
577
578	error = adp5588_gpio_add(kpad);
579	if (error)
580		goto err_free_irq;
581
582	device_init_wakeup(&client->dev, 1);
583	i2c_set_clientdata(client, kpad);
 
 
 
 
 
 
 
584
585	dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
586	return 0;
587
588 err_free_irq:
589	free_irq(client->irq, kpad);
590	cancel_delayed_work_sync(&kpad->work);
591 err_unreg_dev:
592	input_unregister_device(input);
593	input = NULL;
594 err_free_mem:
595	input_free_device(input);
596	kfree(kpad);
597
598	return error;
599}
600
601static int adp5588_remove(struct i2c_client *client)
602{
603	struct adp5588_kpad *kpad = i2c_get_clientdata(client);
604
605	adp5588_write(client, CFG, 0);
606	free_irq(client->irq, kpad);
607	cancel_delayed_work_sync(&kpad->work);
608	input_unregister_device(kpad->input);
609	adp5588_gpio_remove(kpad);
610	kfree(kpad);
611
612	return 0;
613}
614
615#ifdef CONFIG_PM
616static int adp5588_suspend(struct device *dev)
617{
618	struct adp5588_kpad *kpad = dev_get_drvdata(dev);
619	struct i2c_client *client = kpad->client;
620
621	disable_irq(client->irq);
622	cancel_delayed_work_sync(&kpad->work);
623
624	if (device_may_wakeup(&client->dev))
625		enable_irq_wake(client->irq);
626
627	return 0;
628}
629
630static int adp5588_resume(struct device *dev)
631{
632	struct adp5588_kpad *kpad = dev_get_drvdata(dev);
633	struct i2c_client *client = kpad->client;
634
635	if (device_may_wakeup(&client->dev))
636		disable_irq_wake(client->irq);
637
638	enable_irq(client->irq);
639
640	return 0;
641}
642
643static const struct dev_pm_ops adp5588_dev_pm_ops = {
644	.suspend = adp5588_suspend,
645	.resume  = adp5588_resume,
646};
647#endif
648
649static const struct i2c_device_id adp5588_id[] = {
650	{ "adp5588-keys", 0 },
651	{ "adp5587-keys", 0 },
652	{ }
653};
654MODULE_DEVICE_TABLE(i2c, adp5588_id);
655
 
 
 
 
 
 
 
656static struct i2c_driver adp5588_driver = {
657	.driver = {
658		.name = KBUILD_MODNAME,
659#ifdef CONFIG_PM
660		.pm   = &adp5588_dev_pm_ops,
661#endif
662	},
663	.probe    = adp5588_probe,
664	.remove   = adp5588_remove,
665	.id_table = adp5588_id,
666};
667
668module_i2c_driver(adp5588_driver);
669
670MODULE_LICENSE("GPL");
671MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
672MODULE_DESCRIPTION("ADP5588/87 Keypad driver");