2 * Description: keypad driver for ADP5589, ADP5585
3 * I2C QWERTY Keypad and IO Expander
4 * Bugs: Enter bugs at http://blackfin.uclinux.org/
6 * Copyright (C) 2010-2011 Analog Devices Inc.
7 * Licensed under the GPL-2.
10 #include <linux/module.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/workqueue.h>
14 #include <linux/errno.h>
16 #include <linux/platform_device.h>
17 #include <linux/input.h>
18 #include <linux/i2c.h>
19 #include <linux/gpio.h>
20 #include <linux/slab.h>
22 #include <linux/input/adp5589.h>
24 /* ADP5589/ADP5585 Common Registers */
25 #define ADP5589_5_ID 0x00
26 #define ADP5589_5_INT_STATUS 0x01
27 #define ADP5589_5_STATUS 0x02
28 #define ADP5589_5_FIFO_1 0x03
29 #define ADP5589_5_FIFO_2 0x04
30 #define ADP5589_5_FIFO_3 0x05
31 #define ADP5589_5_FIFO_4 0x06
32 #define ADP5589_5_FIFO_5 0x07
33 #define ADP5589_5_FIFO_6 0x08
34 #define ADP5589_5_FIFO_7 0x09
35 #define ADP5589_5_FIFO_8 0x0A
36 #define ADP5589_5_FIFO_9 0x0B
37 #define ADP5589_5_FIFO_10 0x0C
38 #define ADP5589_5_FIFO_11 0x0D
39 #define ADP5589_5_FIFO_12 0x0E
40 #define ADP5589_5_FIFO_13 0x0F
41 #define ADP5589_5_FIFO_14 0x10
42 #define ADP5589_5_FIFO_15 0x11
43 #define ADP5589_5_FIFO_16 0x12
44 #define ADP5589_5_GPI_INT_STAT_A 0x13
45 #define ADP5589_5_GPI_INT_STAT_B 0x14
47 /* ADP5589 Registers */
48 #define ADP5589_GPI_INT_STAT_C 0x15
49 #define ADP5589_GPI_STATUS_A 0x16
50 #define ADP5589_GPI_STATUS_B 0x17
51 #define ADP5589_GPI_STATUS_C 0x18
52 #define ADP5589_RPULL_CONFIG_A 0x19
53 #define ADP5589_RPULL_CONFIG_B 0x1A
54 #define ADP5589_RPULL_CONFIG_C 0x1B
55 #define ADP5589_RPULL_CONFIG_D 0x1C
56 #define ADP5589_RPULL_CONFIG_E 0x1D
57 #define ADP5589_GPI_INT_LEVEL_A 0x1E
58 #define ADP5589_GPI_INT_LEVEL_B 0x1F
59 #define ADP5589_GPI_INT_LEVEL_C 0x20
60 #define ADP5589_GPI_EVENT_EN_A 0x21
61 #define ADP5589_GPI_EVENT_EN_B 0x22
62 #define ADP5589_GPI_EVENT_EN_C 0x23
63 #define ADP5589_GPI_INTERRUPT_EN_A 0x24
64 #define ADP5589_GPI_INTERRUPT_EN_B 0x25
65 #define ADP5589_GPI_INTERRUPT_EN_C 0x26
66 #define ADP5589_DEBOUNCE_DIS_A 0x27
67 #define ADP5589_DEBOUNCE_DIS_B 0x28
68 #define ADP5589_DEBOUNCE_DIS_C 0x29
69 #define ADP5589_GPO_DATA_OUT_A 0x2A
70 #define ADP5589_GPO_DATA_OUT_B 0x2B
71 #define ADP5589_GPO_DATA_OUT_C 0x2C
72 #define ADP5589_GPO_OUT_MODE_A 0x2D
73 #define ADP5589_GPO_OUT_MODE_B 0x2E
74 #define ADP5589_GPO_OUT_MODE_C 0x2F
75 #define ADP5589_GPIO_DIRECTION_A 0x30
76 #define ADP5589_GPIO_DIRECTION_B 0x31
77 #define ADP5589_GPIO_DIRECTION_C 0x32
78 #define ADP5589_UNLOCK1 0x33
79 #define ADP5589_UNLOCK2 0x34
80 #define ADP5589_EXT_LOCK_EVENT 0x35
81 #define ADP5589_UNLOCK_TIMERS 0x36
82 #define ADP5589_LOCK_CFG 0x37
83 #define ADP5589_RESET1_EVENT_A 0x38
84 #define ADP5589_RESET1_EVENT_B 0x39
85 #define ADP5589_RESET1_EVENT_C 0x3A
86 #define ADP5589_RESET2_EVENT_A 0x3B
87 #define ADP5589_RESET2_EVENT_B 0x3C
88 #define ADP5589_RESET_CFG 0x3D
89 #define ADP5589_PWM_OFFT_LOW 0x3E
90 #define ADP5589_PWM_OFFT_HIGH 0x3F
91 #define ADP5589_PWM_ONT_LOW 0x40
92 #define ADP5589_PWM_ONT_HIGH 0x41
93 #define ADP5589_PWM_CFG 0x42
94 #define ADP5589_CLOCK_DIV_CFG 0x43
95 #define ADP5589_LOGIC_1_CFG 0x44
96 #define ADP5589_LOGIC_2_CFG 0x45
97 #define ADP5589_LOGIC_FF_CFG 0x46
98 #define ADP5589_LOGIC_INT_EVENT_EN 0x47
99 #define ADP5589_POLL_PTIME_CFG 0x48
100 #define ADP5589_PIN_CONFIG_A 0x49
101 #define ADP5589_PIN_CONFIG_B 0x4A
102 #define ADP5589_PIN_CONFIG_C 0x4B
103 #define ADP5589_PIN_CONFIG_D 0x4C
104 #define ADP5589_GENERAL_CFG 0x4D
105 #define ADP5589_INT_EN 0x4E
107 /* ADP5585 Registers */
108 #define ADP5585_GPI_STATUS_A 0x15
109 #define ADP5585_GPI_STATUS_B 0x16
110 #define ADP5585_RPULL_CONFIG_A 0x17
111 #define ADP5585_RPULL_CONFIG_B 0x18
112 #define ADP5585_RPULL_CONFIG_C 0x19
113 #define ADP5585_RPULL_CONFIG_D 0x1A
114 #define ADP5585_GPI_INT_LEVEL_A 0x1B
115 #define ADP5585_GPI_INT_LEVEL_B 0x1C
116 #define ADP5585_GPI_EVENT_EN_A 0x1D
117 #define ADP5585_GPI_EVENT_EN_B 0x1E
118 #define ADP5585_GPI_INTERRUPT_EN_A 0x1F
119 #define ADP5585_GPI_INTERRUPT_EN_B 0x20
120 #define ADP5585_DEBOUNCE_DIS_A 0x21
121 #define ADP5585_DEBOUNCE_DIS_B 0x22
122 #define ADP5585_GPO_DATA_OUT_A 0x23
123 #define ADP5585_GPO_DATA_OUT_B 0x24
124 #define ADP5585_GPO_OUT_MODE_A 0x25
125 #define ADP5585_GPO_OUT_MODE_B 0x26
126 #define ADP5585_GPIO_DIRECTION_A 0x27
127 #define ADP5585_GPIO_DIRECTION_B 0x28
128 #define ADP5585_RESET1_EVENT_A 0x29
129 #define ADP5585_RESET1_EVENT_B 0x2A
130 #define ADP5585_RESET1_EVENT_C 0x2B
131 #define ADP5585_RESET2_EVENT_A 0x2C
132 #define ADP5585_RESET2_EVENT_B 0x2D
133 #define ADP5585_RESET_CFG 0x2E
134 #define ADP5585_PWM_OFFT_LOW 0x2F
135 #define ADP5585_PWM_OFFT_HIGH 0x30
136 #define ADP5585_PWM_ONT_LOW 0x31
137 #define ADP5585_PWM_ONT_HIGH 0x32
138 #define ADP5585_PWM_CFG 0x33
139 #define ADP5585_LOGIC_CFG 0x34
140 #define ADP5585_LOGIC_FF_CFG 0x35
141 #define ADP5585_LOGIC_INT_EVENT_EN 0x36
142 #define ADP5585_POLL_PTIME_CFG 0x37
143 #define ADP5585_PIN_CONFIG_A 0x38
144 #define ADP5585_PIN_CONFIG_B 0x39
145 #define ADP5585_PIN_CONFIG_D 0x3A
146 #define ADP5585_GENERAL_CFG 0x3B
147 #define ADP5585_INT_EN 0x3C
150 #define ADP5589_5_DEVICE_ID_MASK 0xF
151 #define ADP5589_5_MAN_ID_MASK 0xF
152 #define ADP5589_5_MAN_ID_SHIFT 4
153 #define ADP5589_5_MAN_ID 0x02
155 /* GENERAL_CFG Register */
156 #define OSC_EN (1 << 7)
157 #define CORE_CLK(x) (((x) & 0x3) << 5)
158 #define LCK_TRK_LOGIC (1 << 4) /* ADP5589 only */
159 #define LCK_TRK_GPI (1 << 3) /* ADP5589 only */
160 #define INT_CFG (1 << 1)
161 #define RST_CFG (1 << 0)
163 /* INT_EN Register */
164 #define LOGIC2_IEN (1 << 5) /* ADP5589 only */
165 #define LOGIC1_IEN (1 << 4)
166 #define LOCK_IEN (1 << 3) /* ADP5589 only */
167 #define OVRFLOW_IEN (1 << 2)
168 #define GPI_IEN (1 << 1)
169 #define EVENT_IEN (1 << 0)
171 /* Interrupt Status Register */
172 #define LOGIC2_INT (1 << 5) /* ADP5589 only */
173 #define LOGIC1_INT (1 << 4)
174 #define LOCK_INT (1 << 3) /* ADP5589 only */
175 #define OVRFLOW_INT (1 << 2)
176 #define GPI_INT (1 << 1)
177 #define EVENT_INT (1 << 0)
179 /* STATUS Register */
180 #define LOGIC2_STAT (1 << 7) /* ADP5589 only */
181 #define LOGIC1_STAT (1 << 6)
182 #define LOCK_STAT (1 << 5) /* ADP5589 only */
185 /* PIN_CONFIG_D Register */
186 #define C4_EXTEND_CFG (1 << 6) /* RESET2 */
187 #define R4_EXTEND_CFG (1 << 5) /* RESET1 */
190 #define LOCK_EN (1 << 0)
192 #define PTIME_MASK 0x3
193 #define LTIME_MASK 0x3 /* ADP5589 only */
195 /* Key Event Register xy */
196 #define KEY_EV_PRESSED (1 << 7)
197 #define KEY_EV_MASK (0x7F)
199 #define KEYP_MAX_EVENT 16
200 #define ADP5589_MAXGPIO 19
201 #define ADP5585_MAXGPIO 11 /* 10 on the ADP5585-01, 11 on ADP5585-02 */
209 struct adp_constants
{
224 u8 (*bank
) (u8 offset
);
225 u8 (*bit
) (u8 offset
);
229 struct adp5589_kpad
{
230 struct i2c_client
*client
;
231 struct input_dev
*input
;
232 const struct adp_constants
*var
;
233 unsigned short keycode
[ADP5589_KEYMAPSIZE
];
234 const struct adp5589_gpi_map
*gpimap
;
235 unsigned short gpimapsize
;
238 bool adp5585_support_row5
;
239 #ifdef CONFIG_GPIOLIB
240 unsigned char gpiomap
[ADP5589_MAXGPIO
];
243 struct mutex gpio_lock
; /* Protect cached dir, dat_out */
250 * ADP5589 / ADP5585 derivative / variant handling
256 static unsigned char adp5589_bank(unsigned char offset
)
261 static unsigned char adp5589_bit(unsigned char offset
)
263 return 1u << (offset
& 0x7);
266 static unsigned char adp5589_reg(unsigned char reg
)
271 static const struct adp_constants const_adp5589
= {
272 .maxgpio
= ADP5589_MAXGPIO
,
273 .keymapsize
= ADP5589_KEYMAPSIZE
,
274 .gpi_pin_row_base
= ADP5589_GPI_PIN_ROW_BASE
,
275 .gpi_pin_row_end
= ADP5589_GPI_PIN_ROW_END
,
276 .gpi_pin_col_base
= ADP5589_GPI_PIN_COL_BASE
,
277 .gpi_pin_base
= ADP5589_GPI_PIN_BASE
,
278 .gpi_pin_end
= ADP5589_GPI_PIN_END
,
279 .gpimapsize_max
= ADP5589_GPIMAPSIZE_MAX
,
281 .max_row_num
= ADP5589_MAX_ROW_NUM
,
282 .max_col_num
= ADP5589_MAX_COL_NUM
,
283 .row_mask
= ADP5589_ROW_MASK
,
284 .col_mask
= ADP5589_COL_MASK
,
285 .col_shift
= ADP5589_COL_SHIFT
,
286 .bank
= adp5589_bank
,
293 static unsigned char adp5585_bank(unsigned char offset
)
295 return offset
> ADP5585_MAX_ROW_NUM
;
298 static unsigned char adp5585_bit(unsigned char offset
)
300 return (offset
> ADP5585_MAX_ROW_NUM
) ?
301 1u << (offset
- ADP5585_COL_SHIFT
) : 1u << offset
;
304 static const unsigned char adp5585_reg_lut
[] = {
305 [ADP5589_GPI_STATUS_A
] = ADP5585_GPI_STATUS_A
,
306 [ADP5589_GPI_STATUS_B
] = ADP5585_GPI_STATUS_B
,
307 [ADP5589_RPULL_CONFIG_A
] = ADP5585_RPULL_CONFIG_A
,
308 [ADP5589_RPULL_CONFIG_B
] = ADP5585_RPULL_CONFIG_B
,
309 [ADP5589_RPULL_CONFIG_C
] = ADP5585_RPULL_CONFIG_C
,
310 [ADP5589_RPULL_CONFIG_D
] = ADP5585_RPULL_CONFIG_D
,
311 [ADP5589_GPI_INT_LEVEL_A
] = ADP5585_GPI_INT_LEVEL_A
,
312 [ADP5589_GPI_INT_LEVEL_B
] = ADP5585_GPI_INT_LEVEL_B
,
313 [ADP5589_GPI_EVENT_EN_A
] = ADP5585_GPI_EVENT_EN_A
,
314 [ADP5589_GPI_EVENT_EN_B
] = ADP5585_GPI_EVENT_EN_B
,
315 [ADP5589_GPI_INTERRUPT_EN_A
] = ADP5585_GPI_INTERRUPT_EN_A
,
316 [ADP5589_GPI_INTERRUPT_EN_B
] = ADP5585_GPI_INTERRUPT_EN_B
,
317 [ADP5589_DEBOUNCE_DIS_A
] = ADP5585_DEBOUNCE_DIS_A
,
318 [ADP5589_DEBOUNCE_DIS_B
] = ADP5585_DEBOUNCE_DIS_B
,
319 [ADP5589_GPO_DATA_OUT_A
] = ADP5585_GPO_DATA_OUT_A
,
320 [ADP5589_GPO_DATA_OUT_B
] = ADP5585_GPO_DATA_OUT_B
,
321 [ADP5589_GPO_OUT_MODE_A
] = ADP5585_GPO_OUT_MODE_A
,
322 [ADP5589_GPO_OUT_MODE_B
] = ADP5585_GPO_OUT_MODE_B
,
323 [ADP5589_GPIO_DIRECTION_A
] = ADP5585_GPIO_DIRECTION_A
,
324 [ADP5589_GPIO_DIRECTION_B
] = ADP5585_GPIO_DIRECTION_B
,
325 [ADP5589_RESET1_EVENT_A
] = ADP5585_RESET1_EVENT_A
,
326 [ADP5589_RESET1_EVENT_B
] = ADP5585_RESET1_EVENT_B
,
327 [ADP5589_RESET1_EVENT_C
] = ADP5585_RESET1_EVENT_C
,
328 [ADP5589_RESET2_EVENT_A
] = ADP5585_RESET2_EVENT_A
,
329 [ADP5589_RESET2_EVENT_B
] = ADP5585_RESET2_EVENT_B
,
330 [ADP5589_RESET_CFG
] = ADP5585_RESET_CFG
,
331 [ADP5589_PWM_OFFT_LOW
] = ADP5585_PWM_OFFT_LOW
,
332 [ADP5589_PWM_OFFT_HIGH
] = ADP5585_PWM_OFFT_HIGH
,
333 [ADP5589_PWM_ONT_LOW
] = ADP5585_PWM_ONT_LOW
,
334 [ADP5589_PWM_ONT_HIGH
] = ADP5585_PWM_ONT_HIGH
,
335 [ADP5589_PWM_CFG
] = ADP5585_PWM_CFG
,
336 [ADP5589_LOGIC_1_CFG
] = ADP5585_LOGIC_CFG
,
337 [ADP5589_LOGIC_FF_CFG
] = ADP5585_LOGIC_FF_CFG
,
338 [ADP5589_LOGIC_INT_EVENT_EN
] = ADP5585_LOGIC_INT_EVENT_EN
,
339 [ADP5589_POLL_PTIME_CFG
] = ADP5585_POLL_PTIME_CFG
,
340 [ADP5589_PIN_CONFIG_A
] = ADP5585_PIN_CONFIG_A
,
341 [ADP5589_PIN_CONFIG_B
] = ADP5585_PIN_CONFIG_B
,
342 [ADP5589_PIN_CONFIG_D
] = ADP5585_PIN_CONFIG_D
,
343 [ADP5589_GENERAL_CFG
] = ADP5585_GENERAL_CFG
,
344 [ADP5589_INT_EN
] = ADP5585_INT_EN
,
347 static unsigned char adp5585_reg(unsigned char reg
)
349 return adp5585_reg_lut
[reg
];
352 static const struct adp_constants const_adp5585
= {
353 .maxgpio
= ADP5585_MAXGPIO
,
354 .keymapsize
= ADP5585_KEYMAPSIZE
,
355 .gpi_pin_row_base
= ADP5585_GPI_PIN_ROW_BASE
,
356 .gpi_pin_row_end
= ADP5585_GPI_PIN_ROW_END
,
357 .gpi_pin_col_base
= ADP5585_GPI_PIN_COL_BASE
,
358 .gpi_pin_base
= ADP5585_GPI_PIN_BASE
,
359 .gpi_pin_end
= ADP5585_GPI_PIN_END
,
360 .gpimapsize_max
= ADP5585_GPIMAPSIZE_MAX
,
362 .max_row_num
= ADP5585_MAX_ROW_NUM
,
363 .max_col_num
= ADP5585_MAX_COL_NUM
,
364 .row_mask
= ADP5585_ROW_MASK
,
365 .col_mask
= ADP5585_COL_MASK
,
366 .col_shift
= ADP5585_COL_SHIFT
,
367 .bank
= adp5585_bank
,
372 static int adp5589_read(struct i2c_client
*client
, u8 reg
)
374 int ret
= i2c_smbus_read_byte_data(client
, reg
);
377 dev_err(&client
->dev
, "Read Error\n");
382 static int adp5589_write(struct i2c_client
*client
, u8 reg
, u8 val
)
384 return i2c_smbus_write_byte_data(client
, reg
, val
);
387 #ifdef CONFIG_GPIOLIB
388 static int adp5589_gpio_get_value(struct gpio_chip
*chip
, unsigned off
)
390 struct adp5589_kpad
*kpad
= container_of(chip
, struct adp5589_kpad
, gc
);
391 unsigned int bank
= kpad
->var
->bank(kpad
->gpiomap
[off
]);
392 unsigned int bit
= kpad
->var
->bit(kpad
->gpiomap
[off
]);
394 return !!(adp5589_read(kpad
->client
,
395 kpad
->var
->reg(ADP5589_GPI_STATUS_A
) + bank
) &
399 static void adp5589_gpio_set_value(struct gpio_chip
*chip
,
400 unsigned off
, int val
)
402 struct adp5589_kpad
*kpad
= container_of(chip
, struct adp5589_kpad
, gc
);
403 unsigned int bank
= kpad
->var
->bank(kpad
->gpiomap
[off
]);
404 unsigned int bit
= kpad
->var
->bit(kpad
->gpiomap
[off
]);
406 mutex_lock(&kpad
->gpio_lock
);
409 kpad
->dat_out
[bank
] |= bit
;
411 kpad
->dat_out
[bank
] &= ~bit
;
413 adp5589_write(kpad
->client
, kpad
->var
->reg(ADP5589_GPO_DATA_OUT_A
) +
414 bank
, kpad
->dat_out
[bank
]);
416 mutex_unlock(&kpad
->gpio_lock
);
419 static int adp5589_gpio_direction_input(struct gpio_chip
*chip
, unsigned off
)
421 struct adp5589_kpad
*kpad
= container_of(chip
, struct adp5589_kpad
, gc
);
422 unsigned int bank
= kpad
->var
->bank(kpad
->gpiomap
[off
]);
423 unsigned int bit
= kpad
->var
->bit(kpad
->gpiomap
[off
]);
426 mutex_lock(&kpad
->gpio_lock
);
428 kpad
->dir
[bank
] &= ~bit
;
429 ret
= adp5589_write(kpad
->client
,
430 kpad
->var
->reg(ADP5589_GPIO_DIRECTION_A
) + bank
,
433 mutex_unlock(&kpad
->gpio_lock
);
438 static int adp5589_gpio_direction_output(struct gpio_chip
*chip
,
439 unsigned off
, int val
)
441 struct adp5589_kpad
*kpad
= container_of(chip
, struct adp5589_kpad
, gc
);
442 unsigned int bank
= kpad
->var
->bank(kpad
->gpiomap
[off
]);
443 unsigned int bit
= kpad
->var
->bit(kpad
->gpiomap
[off
]);
446 mutex_lock(&kpad
->gpio_lock
);
448 kpad
->dir
[bank
] |= bit
;
451 kpad
->dat_out
[bank
] |= bit
;
453 kpad
->dat_out
[bank
] &= ~bit
;
455 ret
= adp5589_write(kpad
->client
, kpad
->var
->reg(ADP5589_GPO_DATA_OUT_A
)
456 + bank
, kpad
->dat_out
[bank
]);
457 ret
|= adp5589_write(kpad
->client
,
458 kpad
->var
->reg(ADP5589_GPIO_DIRECTION_A
) + bank
,
461 mutex_unlock(&kpad
->gpio_lock
);
466 static int adp5589_build_gpiomap(struct adp5589_kpad
*kpad
,
467 const struct adp5589_kpad_platform_data
*pdata
)
469 bool pin_used
[ADP5589_MAXGPIO
];
473 memset(pin_used
, false, sizeof(pin_used
));
475 for (i
= 0; i
< kpad
->var
->maxgpio
; i
++)
476 if (pdata
->keypad_en_mask
& (1 << i
))
479 for (i
= 0; i
< kpad
->gpimapsize
; i
++)
480 pin_used
[kpad
->gpimap
[i
].pin
- kpad
->var
->gpi_pin_base
] = true;
482 if (kpad
->extend_cfg
& R4_EXTEND_CFG
)
485 if (kpad
->extend_cfg
& C4_EXTEND_CFG
)
486 pin_used
[kpad
->var
->c4_extend_cfg
] = true;
488 if (!kpad
->adp5585_support_row5
)
491 for (i
= 0; i
< kpad
->var
->maxgpio
; i
++)
493 kpad
->gpiomap
[n_unused
++] = i
;
498 static int adp5589_gpio_add(struct adp5589_kpad
*kpad
)
500 struct device
*dev
= &kpad
->client
->dev
;
501 const struct adp5589_kpad_platform_data
*pdata
= dev_get_platdata(dev
);
502 const struct adp5589_gpio_platform_data
*gpio_data
= pdata
->gpio_data
;
508 kpad
->gc
.ngpio
= adp5589_build_gpiomap(kpad
, pdata
);
509 if (kpad
->gc
.ngpio
== 0) {
510 dev_info(dev
, "No unused gpios left to export\n");
514 kpad
->export_gpio
= true;
516 kpad
->gc
.direction_input
= adp5589_gpio_direction_input
;
517 kpad
->gc
.direction_output
= adp5589_gpio_direction_output
;
518 kpad
->gc
.get
= adp5589_gpio_get_value
;
519 kpad
->gc
.set
= adp5589_gpio_set_value
;
520 kpad
->gc
.can_sleep
= 1;
522 kpad
->gc
.base
= gpio_data
->gpio_start
;
523 kpad
->gc
.label
= kpad
->client
->name
;
524 kpad
->gc
.owner
= THIS_MODULE
;
526 mutex_init(&kpad
->gpio_lock
);
528 error
= gpiochip_add(&kpad
->gc
);
530 dev_err(dev
, "gpiochip_add failed, err: %d\n", error
);
534 for (i
= 0; i
<= kpad
->var
->bank(kpad
->var
->maxgpio
); i
++) {
535 kpad
->dat_out
[i
] = adp5589_read(kpad
->client
, kpad
->var
->reg(
536 ADP5589_GPO_DATA_OUT_A
) + i
);
537 kpad
->dir
[i
] = adp5589_read(kpad
->client
, kpad
->var
->reg(
538 ADP5589_GPIO_DIRECTION_A
) + i
);
541 if (gpio_data
->setup
) {
542 error
= gpio_data
->setup(kpad
->client
,
543 kpad
->gc
.base
, kpad
->gc
.ngpio
,
546 dev_warn(dev
, "setup failed, %d\n", error
);
552 static void adp5589_gpio_remove(struct adp5589_kpad
*kpad
)
554 struct device
*dev
= &kpad
->client
->dev
;
555 const struct adp5589_kpad_platform_data
*pdata
= dev_get_platdata(dev
);
556 const struct adp5589_gpio_platform_data
*gpio_data
= pdata
->gpio_data
;
559 if (!kpad
->export_gpio
)
562 if (gpio_data
->teardown
) {
563 error
= gpio_data
->teardown(kpad
->client
,
564 kpad
->gc
.base
, kpad
->gc
.ngpio
,
567 dev_warn(dev
, "teardown failed %d\n", error
);
570 gpiochip_remove(&kpad
->gc
);
573 static inline int adp5589_gpio_add(struct adp5589_kpad
*kpad
)
578 static inline void adp5589_gpio_remove(struct adp5589_kpad
*kpad
)
583 static void adp5589_report_switches(struct adp5589_kpad
*kpad
,
584 int key
, int key_val
)
588 for (i
= 0; i
< kpad
->gpimapsize
; i
++) {
589 if (key_val
== kpad
->gpimap
[i
].pin
) {
590 input_report_switch(kpad
->input
,
591 kpad
->gpimap
[i
].sw_evt
,
592 key
& KEY_EV_PRESSED
);
598 static void adp5589_report_events(struct adp5589_kpad
*kpad
, int ev_cnt
)
602 for (i
= 0; i
< ev_cnt
; i
++) {
603 int key
= adp5589_read(kpad
->client
, ADP5589_5_FIFO_1
+ i
);
604 int key_val
= key
& KEY_EV_MASK
;
606 if (key_val
>= kpad
->var
->gpi_pin_base
&&
607 key_val
<= kpad
->var
->gpi_pin_end
) {
608 adp5589_report_switches(kpad
, key
, key_val
);
610 input_report_key(kpad
->input
,
611 kpad
->keycode
[key_val
- 1],
612 key
& KEY_EV_PRESSED
);
617 static irqreturn_t
adp5589_irq(int irq
, void *handle
)
619 struct adp5589_kpad
*kpad
= handle
;
620 struct i2c_client
*client
= kpad
->client
;
623 status
= adp5589_read(client
, ADP5589_5_INT_STATUS
);
625 if (status
& OVRFLOW_INT
) /* Unlikely and should never happen */
626 dev_err(&client
->dev
, "Event Overflow Error\n");
628 if (status
& EVENT_INT
) {
629 ev_cnt
= adp5589_read(client
, ADP5589_5_STATUS
) & KEC
;
631 adp5589_report_events(kpad
, ev_cnt
);
632 input_sync(kpad
->input
);
636 adp5589_write(client
, ADP5589_5_INT_STATUS
, status
); /* Status is W1C */
641 static int adp5589_get_evcode(struct adp5589_kpad
*kpad
, unsigned short key
)
645 for (i
= 0; i
< kpad
->var
->keymapsize
; i
++)
646 if (key
== kpad
->keycode
[i
])
647 return (i
+ 1) | KEY_EV_PRESSED
;
649 dev_err(&kpad
->client
->dev
, "RESET/UNLOCK key not in keycode map\n");
654 static int adp5589_setup(struct adp5589_kpad
*kpad
)
656 struct i2c_client
*client
= kpad
->client
;
657 const struct adp5589_kpad_platform_data
*pdata
=
658 dev_get_platdata(&client
->dev
);
659 u8 (*reg
) (u8
) = kpad
->var
->reg
;
660 unsigned char evt_mode1
= 0, evt_mode2
= 0, evt_mode3
= 0;
661 unsigned char pull_mask
= 0;
664 ret
= adp5589_write(client
, reg(ADP5589_PIN_CONFIG_A
),
665 pdata
->keypad_en_mask
& kpad
->var
->row_mask
);
666 ret
|= adp5589_write(client
, reg(ADP5589_PIN_CONFIG_B
),
667 (pdata
->keypad_en_mask
>> kpad
->var
->col_shift
) &
668 kpad
->var
->col_mask
);
670 if (!kpad
->is_adp5585
)
671 ret
|= adp5589_write(client
, ADP5589_PIN_CONFIG_C
,
672 (pdata
->keypad_en_mask
>> 16) & 0xFF);
674 if (!kpad
->is_adp5585
&& pdata
->en_keylock
) {
675 ret
|= adp5589_write(client
, ADP5589_UNLOCK1
,
677 ret
|= adp5589_write(client
, ADP5589_UNLOCK2
,
679 ret
|= adp5589_write(client
, ADP5589_UNLOCK_TIMERS
,
680 pdata
->unlock_timer
& LTIME_MASK
);
681 ret
|= adp5589_write(client
, ADP5589_LOCK_CFG
, LOCK_EN
);
684 for (i
= 0; i
< KEYP_MAX_EVENT
; i
++)
685 ret
|= adp5589_read(client
, ADP5589_5_FIFO_1
+ i
);
687 for (i
= 0; i
< pdata
->gpimapsize
; i
++) {
688 unsigned short pin
= pdata
->gpimap
[i
].pin
;
690 if (pin
<= kpad
->var
->gpi_pin_row_end
) {
691 evt_mode1
|= (1 << (pin
- kpad
->var
->gpi_pin_row_base
));
694 ((1 << (pin
- kpad
->var
->gpi_pin_col_base
)) & 0xFF);
695 if (!kpad
->is_adp5585
)
696 evt_mode3
|= ((1 << (pin
-
697 kpad
->var
->gpi_pin_col_base
)) >> 8);
701 if (pdata
->gpimapsize
) {
702 ret
|= adp5589_write(client
, reg(ADP5589_GPI_EVENT_EN_A
),
704 ret
|= adp5589_write(client
, reg(ADP5589_GPI_EVENT_EN_B
),
706 if (!kpad
->is_adp5585
)
707 ret
|= adp5589_write(client
,
708 reg(ADP5589_GPI_EVENT_EN_C
),
712 if (pdata
->pull_dis_mask
& pdata
->pullup_en_100k
&
713 pdata
->pullup_en_300k
& pdata
->pulldown_en_300k
)
714 dev_warn(&client
->dev
, "Conflicting pull resistor config\n");
716 for (i
= 0; i
<= kpad
->var
->max_row_num
; i
++) {
717 unsigned val
= 0, bit
= (1 << i
);
718 if (pdata
->pullup_en_300k
& bit
)
720 else if (pdata
->pulldown_en_300k
& bit
)
722 else if (pdata
->pullup_en_100k
& bit
)
724 else if (pdata
->pull_dis_mask
& bit
)
727 pull_mask
|= val
<< (2 * (i
& 0x3));
729 if (i
% 4 == 3 || i
== kpad
->var
->max_row_num
) {
730 ret
|= adp5589_write(client
, reg(ADP5585_RPULL_CONFIG_A
)
731 + (i
>> 2), pull_mask
);
736 for (i
= 0; i
<= kpad
->var
->max_col_num
; i
++) {
737 unsigned val
= 0, bit
= 1 << (i
+ kpad
->var
->col_shift
);
738 if (pdata
->pullup_en_300k
& bit
)
740 else if (pdata
->pulldown_en_300k
& bit
)
742 else if (pdata
->pullup_en_100k
& bit
)
744 else if (pdata
->pull_dis_mask
& bit
)
747 pull_mask
|= val
<< (2 * (i
& 0x3));
749 if (i
% 4 == 3 || i
== kpad
->var
->max_col_num
) {
750 ret
|= adp5589_write(client
,
751 reg(ADP5585_RPULL_CONFIG_C
) +
752 (i
>> 2), pull_mask
);
757 if (pdata
->reset1_key_1
&& pdata
->reset1_key_2
&& pdata
->reset1_key_3
) {
758 ret
|= adp5589_write(client
, reg(ADP5589_RESET1_EVENT_A
),
759 adp5589_get_evcode(kpad
,
760 pdata
->reset1_key_1
));
761 ret
|= adp5589_write(client
, reg(ADP5589_RESET1_EVENT_B
),
762 adp5589_get_evcode(kpad
,
763 pdata
->reset1_key_2
));
764 ret
|= adp5589_write(client
, reg(ADP5589_RESET1_EVENT_C
),
765 adp5589_get_evcode(kpad
,
766 pdata
->reset1_key_3
));
767 kpad
->extend_cfg
|= R4_EXTEND_CFG
;
770 if (pdata
->reset2_key_1
&& pdata
->reset2_key_2
) {
771 ret
|= adp5589_write(client
, reg(ADP5589_RESET2_EVENT_A
),
772 adp5589_get_evcode(kpad
,
773 pdata
->reset2_key_1
));
774 ret
|= adp5589_write(client
, reg(ADP5589_RESET2_EVENT_B
),
775 adp5589_get_evcode(kpad
,
776 pdata
->reset2_key_2
));
777 kpad
->extend_cfg
|= C4_EXTEND_CFG
;
780 if (kpad
->extend_cfg
) {
781 ret
|= adp5589_write(client
, reg(ADP5589_RESET_CFG
),
783 ret
|= adp5589_write(client
, reg(ADP5589_PIN_CONFIG_D
),
787 ret
|= adp5589_write(client
, reg(ADP5589_DEBOUNCE_DIS_A
),
788 pdata
->debounce_dis_mask
& kpad
->var
->row_mask
);
790 ret
|= adp5589_write(client
, reg(ADP5589_DEBOUNCE_DIS_B
),
791 (pdata
->debounce_dis_mask
>> kpad
->var
->col_shift
)
792 & kpad
->var
->col_mask
);
794 if (!kpad
->is_adp5585
)
795 ret
|= adp5589_write(client
, reg(ADP5589_DEBOUNCE_DIS_C
),
796 (pdata
->debounce_dis_mask
>> 16) & 0xFF);
798 ret
|= adp5589_write(client
, reg(ADP5589_POLL_PTIME_CFG
),
799 pdata
->scan_cycle_time
& PTIME_MASK
);
800 ret
|= adp5589_write(client
, ADP5589_5_INT_STATUS
,
801 (kpad
->is_adp5585
? 0 : LOGIC2_INT
) |
802 LOGIC1_INT
| OVRFLOW_INT
|
803 (kpad
->is_adp5585
? 0 : LOCK_INT
) |
804 GPI_INT
| EVENT_INT
); /* Status is W1C */
806 ret
|= adp5589_write(client
, reg(ADP5589_GENERAL_CFG
),
807 INT_CFG
| OSC_EN
| CORE_CLK(3));
808 ret
|= adp5589_write(client
, reg(ADP5589_INT_EN
),
809 OVRFLOW_IEN
| GPI_IEN
| EVENT_IEN
);
812 dev_err(&client
->dev
, "Write Error\n");
819 static void adp5589_report_switch_state(struct adp5589_kpad
*kpad
)
821 int gpi_stat_tmp
, pin_loc
;
823 int gpi_stat1
= adp5589_read(kpad
->client
,
824 kpad
->var
->reg(ADP5589_GPI_STATUS_A
));
825 int gpi_stat2
= adp5589_read(kpad
->client
,
826 kpad
->var
->reg(ADP5589_GPI_STATUS_B
));
827 int gpi_stat3
= !kpad
->is_adp5585
?
828 adp5589_read(kpad
->client
, ADP5589_GPI_STATUS_C
) : 0;
830 for (i
= 0; i
< kpad
->gpimapsize
; i
++) {
831 unsigned short pin
= kpad
->gpimap
[i
].pin
;
833 if (pin
<= kpad
->var
->gpi_pin_row_end
) {
834 gpi_stat_tmp
= gpi_stat1
;
835 pin_loc
= pin
- kpad
->var
->gpi_pin_row_base
;
836 } else if ((pin
- kpad
->var
->gpi_pin_col_base
) < 8) {
837 gpi_stat_tmp
= gpi_stat2
;
838 pin_loc
= pin
- kpad
->var
->gpi_pin_col_base
;
840 gpi_stat_tmp
= gpi_stat3
;
841 pin_loc
= pin
- kpad
->var
->gpi_pin_col_base
- 8;
844 if (gpi_stat_tmp
< 0) {
845 dev_err(&kpad
->client
->dev
,
846 "Can't read GPIO_DAT_STAT switch %d, default to OFF\n",
851 input_report_switch(kpad
->input
,
852 kpad
->gpimap
[i
].sw_evt
,
853 !(gpi_stat_tmp
& (1 << pin_loc
)));
856 input_sync(kpad
->input
);
859 static int adp5589_probe(struct i2c_client
*client
,
860 const struct i2c_device_id
*id
)
862 struct adp5589_kpad
*kpad
;
863 const struct adp5589_kpad_platform_data
*pdata
=
864 dev_get_platdata(&client
->dev
);
865 struct input_dev
*input
;
870 if (!i2c_check_functionality(client
->adapter
,
871 I2C_FUNC_SMBUS_BYTE_DATA
)) {
872 dev_err(&client
->dev
, "SMBUS Byte Data not Supported\n");
877 dev_err(&client
->dev
, "no platform data?\n");
881 kpad
= kzalloc(sizeof(*kpad
), GFP_KERNEL
);
885 switch (id
->driver_data
) {
887 kpad
->adp5585_support_row5
= true;
889 kpad
->is_adp5585
= true;
890 kpad
->var
= &const_adp5585
;
893 kpad
->var
= &const_adp5589
;
897 if (!((pdata
->keypad_en_mask
& kpad
->var
->row_mask
) &&
898 (pdata
->keypad_en_mask
>> kpad
->var
->col_shift
)) ||
900 dev_err(&client
->dev
, "no rows, cols or keymap from pdata\n");
905 if (pdata
->keymapsize
!= kpad
->var
->keymapsize
) {
906 dev_err(&client
->dev
, "invalid keymapsize\n");
911 if (!pdata
->gpimap
&& pdata
->gpimapsize
) {
912 dev_err(&client
->dev
, "invalid gpimap from pdata\n");
917 if (pdata
->gpimapsize
> kpad
->var
->gpimapsize_max
) {
918 dev_err(&client
->dev
, "invalid gpimapsize\n");
923 for (i
= 0; i
< pdata
->gpimapsize
; i
++) {
924 unsigned short pin
= pdata
->gpimap
[i
].pin
;
926 if (pin
< kpad
->var
->gpi_pin_base
||
927 pin
> kpad
->var
->gpi_pin_end
) {
928 dev_err(&client
->dev
, "invalid gpi pin data\n");
933 if ((1 << (pin
- kpad
->var
->gpi_pin_row_base
)) &
934 pdata
->keypad_en_mask
) {
935 dev_err(&client
->dev
, "invalid gpi row/col data\n");
942 dev_err(&client
->dev
, "no IRQ?\n");
947 input
= input_allocate_device();
953 kpad
->client
= client
;
956 ret
= adp5589_read(client
, ADP5589_5_ID
);
962 revid
= (u8
) ret
& ADP5589_5_DEVICE_ID_MASK
;
964 input
->name
= client
->name
;
965 input
->phys
= "adp5589-keys/input0";
966 input
->dev
.parent
= &client
->dev
;
968 input_set_drvdata(input
, kpad
);
970 input
->id
.bustype
= BUS_I2C
;
971 input
->id
.vendor
= 0x0001;
972 input
->id
.product
= 0x0001;
973 input
->id
.version
= revid
;
975 input
->keycodesize
= sizeof(kpad
->keycode
[0]);
976 input
->keycodemax
= pdata
->keymapsize
;
977 input
->keycode
= kpad
->keycode
;
979 memcpy(kpad
->keycode
, pdata
->keymap
,
980 pdata
->keymapsize
* input
->keycodesize
);
982 kpad
->gpimap
= pdata
->gpimap
;
983 kpad
->gpimapsize
= pdata
->gpimapsize
;
985 /* setup input device */
986 __set_bit(EV_KEY
, input
->evbit
);
989 __set_bit(EV_REP
, input
->evbit
);
991 for (i
= 0; i
< input
->keycodemax
; i
++)
992 if (kpad
->keycode
[i
] <= KEY_MAX
)
993 __set_bit(kpad
->keycode
[i
], input
->keybit
);
994 __clear_bit(KEY_RESERVED
, input
->keybit
);
996 if (kpad
->gpimapsize
)
997 __set_bit(EV_SW
, input
->evbit
);
998 for (i
= 0; i
< kpad
->gpimapsize
; i
++)
999 __set_bit(kpad
->gpimap
[i
].sw_evt
, input
->swbit
);
1001 error
= input_register_device(input
);
1003 dev_err(&client
->dev
, "unable to register input device\n");
1004 goto err_free_input
;
1007 error
= request_threaded_irq(client
->irq
, NULL
, adp5589_irq
,
1008 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
1009 client
->dev
.driver
->name
, kpad
);
1011 dev_err(&client
->dev
, "irq %d busy?\n", client
->irq
);
1015 error
= adp5589_setup(kpad
);
1019 if (kpad
->gpimapsize
)
1020 adp5589_report_switch_state(kpad
);
1022 error
= adp5589_gpio_add(kpad
);
1026 device_init_wakeup(&client
->dev
, 1);
1027 i2c_set_clientdata(client
, kpad
);
1029 dev_info(&client
->dev
, "Rev.%d keypad, irq %d\n", revid
, client
->irq
);
1033 free_irq(client
->irq
, kpad
);
1035 input_unregister_device(input
);
1038 input_free_device(input
);
1045 static int adp5589_remove(struct i2c_client
*client
)
1047 struct adp5589_kpad
*kpad
= i2c_get_clientdata(client
);
1049 adp5589_write(client
, kpad
->var
->reg(ADP5589_GENERAL_CFG
), 0);
1050 free_irq(client
->irq
, kpad
);
1051 input_unregister_device(kpad
->input
);
1052 adp5589_gpio_remove(kpad
);
1058 #ifdef CONFIG_PM_SLEEP
1059 static int adp5589_suspend(struct device
*dev
)
1061 struct adp5589_kpad
*kpad
= dev_get_drvdata(dev
);
1062 struct i2c_client
*client
= kpad
->client
;
1064 disable_irq(client
->irq
);
1066 if (device_may_wakeup(&client
->dev
))
1067 enable_irq_wake(client
->irq
);
1072 static int adp5589_resume(struct device
*dev
)
1074 struct adp5589_kpad
*kpad
= dev_get_drvdata(dev
);
1075 struct i2c_client
*client
= kpad
->client
;
1077 if (device_may_wakeup(&client
->dev
))
1078 disable_irq_wake(client
->irq
);
1080 enable_irq(client
->irq
);
1086 static SIMPLE_DEV_PM_OPS(adp5589_dev_pm_ops
, adp5589_suspend
, adp5589_resume
);
1088 static const struct i2c_device_id adp5589_id
[] = {
1089 {"adp5589-keys", ADP5589
},
1090 {"adp5585-keys", ADP5585_01
},
1091 {"adp5585-02-keys", ADP5585_02
}, /* Adds ROW5 to ADP5585 */
1095 MODULE_DEVICE_TABLE(i2c
, adp5589_id
);
1097 static struct i2c_driver adp5589_driver
= {
1099 .name
= KBUILD_MODNAME
,
1100 .pm
= &adp5589_dev_pm_ops
,
1102 .probe
= adp5589_probe
,
1103 .remove
= adp5589_remove
,
1104 .id_table
= adp5589_id
,
1107 module_i2c_driver(adp5589_driver
);
1109 MODULE_LICENSE("GPL");
1110 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
1111 MODULE_DESCRIPTION("ADP5589/ADP5585 Keypad driver");