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