Linux 3.4.102
[linux/fpc-iii.git] / drivers / input / keyboard / adp5589-keys.c
blob74e60321338600f92c8a778969cf9f83043a8abf
1 /*
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.
8 */
10 #include <linux/module.h>
11 #include <linux/init.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>
23 #include <linux/input/adp5589.h>
25 /* ADP5589/ADP5585 Common Registers */
26 #define ADP5589_5_ID 0x00
27 #define ADP5589_5_INT_STATUS 0x01
28 #define ADP5589_5_STATUS 0x02
29 #define ADP5589_5_FIFO_1 0x03
30 #define ADP5589_5_FIFO_2 0x04
31 #define ADP5589_5_FIFO_3 0x05
32 #define ADP5589_5_FIFO_4 0x06
33 #define ADP5589_5_FIFO_5 0x07
34 #define ADP5589_5_FIFO_6 0x08
35 #define ADP5589_5_FIFO_7 0x09
36 #define ADP5589_5_FIFO_8 0x0A
37 #define ADP5589_5_FIFO_9 0x0B
38 #define ADP5589_5_FIFO_10 0x0C
39 #define ADP5589_5_FIFO_11 0x0D
40 #define ADP5589_5_FIFO_12 0x0E
41 #define ADP5589_5_FIFO_13 0x0F
42 #define ADP5589_5_FIFO_14 0x10
43 #define ADP5589_5_FIFO_15 0x11
44 #define ADP5589_5_FIFO_16 0x12
45 #define ADP5589_5_GPI_INT_STAT_A 0x13
46 #define ADP5589_5_GPI_INT_STAT_B 0x14
48 /* ADP5589 Registers */
49 #define ADP5589_GPI_INT_STAT_C 0x15
50 #define ADP5589_GPI_STATUS_A 0x16
51 #define ADP5589_GPI_STATUS_B 0x17
52 #define ADP5589_GPI_STATUS_C 0x18
53 #define ADP5589_RPULL_CONFIG_A 0x19
54 #define ADP5589_RPULL_CONFIG_B 0x1A
55 #define ADP5589_RPULL_CONFIG_C 0x1B
56 #define ADP5589_RPULL_CONFIG_D 0x1C
57 #define ADP5589_RPULL_CONFIG_E 0x1D
58 #define ADP5589_GPI_INT_LEVEL_A 0x1E
59 #define ADP5589_GPI_INT_LEVEL_B 0x1F
60 #define ADP5589_GPI_INT_LEVEL_C 0x20
61 #define ADP5589_GPI_EVENT_EN_A 0x21
62 #define ADP5589_GPI_EVENT_EN_B 0x22
63 #define ADP5589_GPI_EVENT_EN_C 0x23
64 #define ADP5589_GPI_INTERRUPT_EN_A 0x24
65 #define ADP5589_GPI_INTERRUPT_EN_B 0x25
66 #define ADP5589_GPI_INTERRUPT_EN_C 0x26
67 #define ADP5589_DEBOUNCE_DIS_A 0x27
68 #define ADP5589_DEBOUNCE_DIS_B 0x28
69 #define ADP5589_DEBOUNCE_DIS_C 0x29
70 #define ADP5589_GPO_DATA_OUT_A 0x2A
71 #define ADP5589_GPO_DATA_OUT_B 0x2B
72 #define ADP5589_GPO_DATA_OUT_C 0x2C
73 #define ADP5589_GPO_OUT_MODE_A 0x2D
74 #define ADP5589_GPO_OUT_MODE_B 0x2E
75 #define ADP5589_GPO_OUT_MODE_C 0x2F
76 #define ADP5589_GPIO_DIRECTION_A 0x30
77 #define ADP5589_GPIO_DIRECTION_B 0x31
78 #define ADP5589_GPIO_DIRECTION_C 0x32
79 #define ADP5589_UNLOCK1 0x33
80 #define ADP5589_UNLOCK2 0x34
81 #define ADP5589_EXT_LOCK_EVENT 0x35
82 #define ADP5589_UNLOCK_TIMERS 0x36
83 #define ADP5589_LOCK_CFG 0x37
84 #define ADP5589_RESET1_EVENT_A 0x38
85 #define ADP5589_RESET1_EVENT_B 0x39
86 #define ADP5589_RESET1_EVENT_C 0x3A
87 #define ADP5589_RESET2_EVENT_A 0x3B
88 #define ADP5589_RESET2_EVENT_B 0x3C
89 #define ADP5589_RESET_CFG 0x3D
90 #define ADP5589_PWM_OFFT_LOW 0x3E
91 #define ADP5589_PWM_OFFT_HIGH 0x3F
92 #define ADP5589_PWM_ONT_LOW 0x40
93 #define ADP5589_PWM_ONT_HIGH 0x41
94 #define ADP5589_PWM_CFG 0x42
95 #define ADP5589_CLOCK_DIV_CFG 0x43
96 #define ADP5589_LOGIC_1_CFG 0x44
97 #define ADP5589_LOGIC_2_CFG 0x45
98 #define ADP5589_LOGIC_FF_CFG 0x46
99 #define ADP5589_LOGIC_INT_EVENT_EN 0x47
100 #define ADP5589_POLL_PTIME_CFG 0x48
101 #define ADP5589_PIN_CONFIG_A 0x49
102 #define ADP5589_PIN_CONFIG_B 0x4A
103 #define ADP5589_PIN_CONFIG_C 0x4B
104 #define ADP5589_PIN_CONFIG_D 0x4C
105 #define ADP5589_GENERAL_CFG 0x4D
106 #define ADP5589_INT_EN 0x4E
108 /* ADP5585 Registers */
109 #define ADP5585_GPI_STATUS_A 0x15
110 #define ADP5585_GPI_STATUS_B 0x16
111 #define ADP5585_RPULL_CONFIG_A 0x17
112 #define ADP5585_RPULL_CONFIG_B 0x18
113 #define ADP5585_RPULL_CONFIG_C 0x19
114 #define ADP5585_RPULL_CONFIG_D 0x1A
115 #define ADP5585_GPI_INT_LEVEL_A 0x1B
116 #define ADP5585_GPI_INT_LEVEL_B 0x1C
117 #define ADP5585_GPI_EVENT_EN_A 0x1D
118 #define ADP5585_GPI_EVENT_EN_B 0x1E
119 #define ADP5585_GPI_INTERRUPT_EN_A 0x1F
120 #define ADP5585_GPI_INTERRUPT_EN_B 0x20
121 #define ADP5585_DEBOUNCE_DIS_A 0x21
122 #define ADP5585_DEBOUNCE_DIS_B 0x22
123 #define ADP5585_GPO_DATA_OUT_A 0x23
124 #define ADP5585_GPO_DATA_OUT_B 0x24
125 #define ADP5585_GPO_OUT_MODE_A 0x25
126 #define ADP5585_GPO_OUT_MODE_B 0x26
127 #define ADP5585_GPIO_DIRECTION_A 0x27
128 #define ADP5585_GPIO_DIRECTION_B 0x28
129 #define ADP5585_RESET1_EVENT_A 0x29
130 #define ADP5585_RESET1_EVENT_B 0x2A
131 #define ADP5585_RESET1_EVENT_C 0x2B
132 #define ADP5585_RESET2_EVENT_A 0x2C
133 #define ADP5585_RESET2_EVENT_B 0x2D
134 #define ADP5585_RESET_CFG 0x2E
135 #define ADP5585_PWM_OFFT_LOW 0x2F
136 #define ADP5585_PWM_OFFT_HIGH 0x30
137 #define ADP5585_PWM_ONT_LOW 0x31
138 #define ADP5585_PWM_ONT_HIGH 0x32
139 #define ADP5585_PWM_CFG 0x33
140 #define ADP5585_LOGIC_CFG 0x34
141 #define ADP5585_LOGIC_FF_CFG 0x35
142 #define ADP5585_LOGIC_INT_EVENT_EN 0x36
143 #define ADP5585_POLL_PTIME_CFG 0x37
144 #define ADP5585_PIN_CONFIG_A 0x38
145 #define ADP5585_PIN_CONFIG_B 0x39
146 #define ADP5585_PIN_CONFIG_D 0x3A
147 #define ADP5585_GENERAL_CFG 0x3B
148 #define ADP5585_INT_EN 0x3C
150 /* ID Register */
151 #define ADP5589_5_DEVICE_ID_MASK 0xF
152 #define ADP5589_5_MAN_ID_MASK 0xF
153 #define ADP5589_5_MAN_ID_SHIFT 4
154 #define ADP5589_5_MAN_ID 0x02
156 /* GENERAL_CFG Register */
157 #define OSC_EN (1 << 7)
158 #define CORE_CLK(x) (((x) & 0x3) << 5)
159 #define LCK_TRK_LOGIC (1 << 4) /* ADP5589 only */
160 #define LCK_TRK_GPI (1 << 3) /* ADP5589 only */
161 #define INT_CFG (1 << 1)
162 #define RST_CFG (1 << 0)
164 /* INT_EN Register */
165 #define LOGIC2_IEN (1 << 5) /* ADP5589 only */
166 #define LOGIC1_IEN (1 << 4)
167 #define LOCK_IEN (1 << 3) /* ADP5589 only */
168 #define OVRFLOW_IEN (1 << 2)
169 #define GPI_IEN (1 << 1)
170 #define EVENT_IEN (1 << 0)
172 /* Interrupt Status Register */
173 #define LOGIC2_INT (1 << 5) /* ADP5589 only */
174 #define LOGIC1_INT (1 << 4)
175 #define LOCK_INT (1 << 3) /* ADP5589 only */
176 #define OVRFLOW_INT (1 << 2)
177 #define GPI_INT (1 << 1)
178 #define EVENT_INT (1 << 0)
180 /* STATUS Register */
181 #define LOGIC2_STAT (1 << 7) /* ADP5589 only */
182 #define LOGIC1_STAT (1 << 6)
183 #define LOCK_STAT (1 << 5) /* ADP5589 only */
184 #define KEC 0xF
186 /* PIN_CONFIG_D Register */
187 #define C4_EXTEND_CFG (1 << 6) /* RESET2 */
188 #define R4_EXTEND_CFG (1 << 5) /* RESET1 */
190 /* LOCK_CFG */
191 #define LOCK_EN (1 << 0)
193 #define PTIME_MASK 0x3
194 #define LTIME_MASK 0x3 /* ADP5589 only */
196 /* Key Event Register xy */
197 #define KEY_EV_PRESSED (1 << 7)
198 #define KEY_EV_MASK (0x7F)
200 #define KEYP_MAX_EVENT 16
201 #define ADP5589_MAXGPIO 19
202 #define ADP5585_MAXGPIO 11 /* 10 on the ADP5585-01, 11 on ADP5585-02 */
204 enum {
205 ADP5589,
206 ADP5585_01,
207 ADP5585_02
210 struct adp_constants {
211 u8 maxgpio;
212 u8 keymapsize;
213 u8 gpi_pin_row_base;
214 u8 gpi_pin_row_end;
215 u8 gpi_pin_col_base;
216 u8 gpi_pin_base;
217 u8 gpi_pin_end;
218 u8 gpimapsize_max;
219 u8 max_row_num;
220 u8 max_col_num;
221 u8 row_mask;
222 u8 col_mask;
223 u8 col_shift;
224 u8 c4_extend_cfg;
225 u8 (*bank) (u8 offset);
226 u8 (*bit) (u8 offset);
227 u8 (*reg) (u8 reg);
230 struct adp5589_kpad {
231 struct i2c_client *client;
232 struct input_dev *input;
233 const struct adp_constants *var;
234 unsigned short keycode[ADP5589_KEYMAPSIZE];
235 const struct adp5589_gpi_map *gpimap;
236 unsigned short gpimapsize;
237 unsigned extend_cfg;
238 bool is_adp5585;
239 bool adp5585_support_row5;
240 #ifdef CONFIG_GPIOLIB
241 unsigned char gpiomap[ADP5589_MAXGPIO];
242 bool export_gpio;
243 struct gpio_chip gc;
244 struct mutex gpio_lock; /* Protect cached dir, dat_out */
245 u8 dat_out[3];
246 u8 dir[3];
247 #endif
251 * ADP5589 / ADP5585 derivative / variant handling
255 /* ADP5589 */
257 static unsigned char adp5589_bank(unsigned char offset)
259 return offset >> 3;
262 static unsigned char adp5589_bit(unsigned char offset)
264 return 1u << (offset & 0x7);
267 static unsigned char adp5589_reg(unsigned char reg)
269 return reg;
272 static const struct adp_constants const_adp5589 = {
273 .maxgpio = ADP5589_MAXGPIO,
274 .keymapsize = ADP5589_KEYMAPSIZE,
275 .gpi_pin_row_base = ADP5589_GPI_PIN_ROW_BASE,
276 .gpi_pin_row_end = ADP5589_GPI_PIN_ROW_END,
277 .gpi_pin_col_base = ADP5589_GPI_PIN_COL_BASE,
278 .gpi_pin_base = ADP5589_GPI_PIN_BASE,
279 .gpi_pin_end = ADP5589_GPI_PIN_END,
280 .gpimapsize_max = ADP5589_GPIMAPSIZE_MAX,
281 .c4_extend_cfg = 12,
282 .max_row_num = ADP5589_MAX_ROW_NUM,
283 .max_col_num = ADP5589_MAX_COL_NUM,
284 .row_mask = ADP5589_ROW_MASK,
285 .col_mask = ADP5589_COL_MASK,
286 .col_shift = ADP5589_COL_SHIFT,
287 .bank = adp5589_bank,
288 .bit = adp5589_bit,
289 .reg = adp5589_reg,
292 /* ADP5585 */
294 static unsigned char adp5585_bank(unsigned char offset)
296 return offset > ADP5585_MAX_ROW_NUM;
299 static unsigned char adp5585_bit(unsigned char offset)
301 return (offset > ADP5585_MAX_ROW_NUM) ?
302 1u << (offset - ADP5585_COL_SHIFT) : 1u << offset;
305 static const unsigned char adp5585_reg_lut[] = {
306 [ADP5589_GPI_STATUS_A] = ADP5585_GPI_STATUS_A,
307 [ADP5589_GPI_STATUS_B] = ADP5585_GPI_STATUS_B,
308 [ADP5589_RPULL_CONFIG_A] = ADP5585_RPULL_CONFIG_A,
309 [ADP5589_RPULL_CONFIG_B] = ADP5585_RPULL_CONFIG_B,
310 [ADP5589_RPULL_CONFIG_C] = ADP5585_RPULL_CONFIG_C,
311 [ADP5589_RPULL_CONFIG_D] = ADP5585_RPULL_CONFIG_D,
312 [ADP5589_GPI_INT_LEVEL_A] = ADP5585_GPI_INT_LEVEL_A,
313 [ADP5589_GPI_INT_LEVEL_B] = ADP5585_GPI_INT_LEVEL_B,
314 [ADP5589_GPI_EVENT_EN_A] = ADP5585_GPI_EVENT_EN_A,
315 [ADP5589_GPI_EVENT_EN_B] = ADP5585_GPI_EVENT_EN_B,
316 [ADP5589_GPI_INTERRUPT_EN_A] = ADP5585_GPI_INTERRUPT_EN_A,
317 [ADP5589_GPI_INTERRUPT_EN_B] = ADP5585_GPI_INTERRUPT_EN_B,
318 [ADP5589_DEBOUNCE_DIS_A] = ADP5585_DEBOUNCE_DIS_A,
319 [ADP5589_DEBOUNCE_DIS_B] = ADP5585_DEBOUNCE_DIS_B,
320 [ADP5589_GPO_DATA_OUT_A] = ADP5585_GPO_DATA_OUT_A,
321 [ADP5589_GPO_DATA_OUT_B] = ADP5585_GPO_DATA_OUT_B,
322 [ADP5589_GPO_OUT_MODE_A] = ADP5585_GPO_OUT_MODE_A,
323 [ADP5589_GPO_OUT_MODE_B] = ADP5585_GPO_OUT_MODE_B,
324 [ADP5589_GPIO_DIRECTION_A] = ADP5585_GPIO_DIRECTION_A,
325 [ADP5589_GPIO_DIRECTION_B] = ADP5585_GPIO_DIRECTION_B,
326 [ADP5589_RESET1_EVENT_A] = ADP5585_RESET1_EVENT_A,
327 [ADP5589_RESET1_EVENT_B] = ADP5585_RESET1_EVENT_B,
328 [ADP5589_RESET1_EVENT_C] = ADP5585_RESET1_EVENT_C,
329 [ADP5589_RESET2_EVENT_A] = ADP5585_RESET2_EVENT_A,
330 [ADP5589_RESET2_EVENT_B] = ADP5585_RESET2_EVENT_B,
331 [ADP5589_RESET_CFG] = ADP5585_RESET_CFG,
332 [ADP5589_PWM_OFFT_LOW] = ADP5585_PWM_OFFT_LOW,
333 [ADP5589_PWM_OFFT_HIGH] = ADP5585_PWM_OFFT_HIGH,
334 [ADP5589_PWM_ONT_LOW] = ADP5585_PWM_ONT_LOW,
335 [ADP5589_PWM_ONT_HIGH] = ADP5585_PWM_ONT_HIGH,
336 [ADP5589_PWM_CFG] = ADP5585_PWM_CFG,
337 [ADP5589_LOGIC_1_CFG] = ADP5585_LOGIC_CFG,
338 [ADP5589_LOGIC_FF_CFG] = ADP5585_LOGIC_FF_CFG,
339 [ADP5589_LOGIC_INT_EVENT_EN] = ADP5585_LOGIC_INT_EVENT_EN,
340 [ADP5589_POLL_PTIME_CFG] = ADP5585_POLL_PTIME_CFG,
341 [ADP5589_PIN_CONFIG_A] = ADP5585_PIN_CONFIG_A,
342 [ADP5589_PIN_CONFIG_B] = ADP5585_PIN_CONFIG_B,
343 [ADP5589_PIN_CONFIG_D] = ADP5585_PIN_CONFIG_D,
344 [ADP5589_GENERAL_CFG] = ADP5585_GENERAL_CFG,
345 [ADP5589_INT_EN] = ADP5585_INT_EN,
348 static unsigned char adp5585_reg(unsigned char reg)
350 return adp5585_reg_lut[reg];
353 static const struct adp_constants const_adp5585 = {
354 .maxgpio = ADP5585_MAXGPIO,
355 .keymapsize = ADP5585_KEYMAPSIZE,
356 .gpi_pin_row_base = ADP5585_GPI_PIN_ROW_BASE,
357 .gpi_pin_row_end = ADP5585_GPI_PIN_ROW_END,
358 .gpi_pin_col_base = ADP5585_GPI_PIN_COL_BASE,
359 .gpi_pin_base = ADP5585_GPI_PIN_BASE,
360 .gpi_pin_end = ADP5585_GPI_PIN_END,
361 .gpimapsize_max = ADP5585_GPIMAPSIZE_MAX,
362 .c4_extend_cfg = 10,
363 .max_row_num = ADP5585_MAX_ROW_NUM,
364 .max_col_num = ADP5585_MAX_COL_NUM,
365 .row_mask = ADP5585_ROW_MASK,
366 .col_mask = ADP5585_COL_MASK,
367 .col_shift = ADP5585_COL_SHIFT,
368 .bank = adp5585_bank,
369 .bit = adp5585_bit,
370 .reg = adp5585_reg,
373 static int adp5589_read(struct i2c_client *client, u8 reg)
375 int ret = i2c_smbus_read_byte_data(client, reg);
377 if (ret < 0)
378 dev_err(&client->dev, "Read Error\n");
380 return ret;
383 static int adp5589_write(struct i2c_client *client, u8 reg, u8 val)
385 return i2c_smbus_write_byte_data(client, reg, val);
388 #ifdef CONFIG_GPIOLIB
389 static int adp5589_gpio_get_value(struct gpio_chip *chip, unsigned off)
391 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
392 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
393 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
395 return !!(adp5589_read(kpad->client,
396 kpad->var->reg(ADP5589_GPI_STATUS_A) + bank) &
397 bit);
400 static void adp5589_gpio_set_value(struct gpio_chip *chip,
401 unsigned off, int val)
403 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
404 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
405 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
407 mutex_lock(&kpad->gpio_lock);
409 if (val)
410 kpad->dat_out[bank] |= bit;
411 else
412 kpad->dat_out[bank] &= ~bit;
414 adp5589_write(kpad->client, kpad->var->reg(ADP5589_GPO_DATA_OUT_A) +
415 bank, kpad->dat_out[bank]);
417 mutex_unlock(&kpad->gpio_lock);
420 static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
422 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
423 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
424 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
425 int ret;
427 mutex_lock(&kpad->gpio_lock);
429 kpad->dir[bank] &= ~bit;
430 ret = adp5589_write(kpad->client,
431 kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
432 kpad->dir[bank]);
434 mutex_unlock(&kpad->gpio_lock);
436 return ret;
439 static int adp5589_gpio_direction_output(struct gpio_chip *chip,
440 unsigned off, int val)
442 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
443 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
444 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
445 int ret;
447 mutex_lock(&kpad->gpio_lock);
449 kpad->dir[bank] |= bit;
451 if (val)
452 kpad->dat_out[bank] |= bit;
453 else
454 kpad->dat_out[bank] &= ~bit;
456 ret = adp5589_write(kpad->client, kpad->var->reg(ADP5589_GPO_DATA_OUT_A)
457 + bank, kpad->dat_out[bank]);
458 ret |= adp5589_write(kpad->client,
459 kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
460 kpad->dir[bank]);
462 mutex_unlock(&kpad->gpio_lock);
464 return ret;
467 static int __devinit adp5589_build_gpiomap(struct adp5589_kpad *kpad,
468 const struct adp5589_kpad_platform_data *pdata)
470 bool pin_used[ADP5589_MAXGPIO];
471 int n_unused = 0;
472 int i;
474 memset(pin_used, false, sizeof(pin_used));
476 for (i = 0; i < kpad->var->maxgpio; i++)
477 if (pdata->keypad_en_mask & (1 << i))
478 pin_used[i] = true;
480 for (i = 0; i < kpad->gpimapsize; i++)
481 pin_used[kpad->gpimap[i].pin - kpad->var->gpi_pin_base] = true;
483 if (kpad->extend_cfg & R4_EXTEND_CFG)
484 pin_used[4] = true;
486 if (kpad->extend_cfg & C4_EXTEND_CFG)
487 pin_used[kpad->var->c4_extend_cfg] = true;
489 if (!kpad->adp5585_support_row5)
490 pin_used[5] = true;
492 for (i = 0; i < kpad->var->maxgpio; i++)
493 if (!pin_used[i])
494 kpad->gpiomap[n_unused++] = i;
496 return n_unused;
499 static int __devinit adp5589_gpio_add(struct adp5589_kpad *kpad)
501 struct device *dev = &kpad->client->dev;
502 const struct adp5589_kpad_platform_data *pdata = dev->platform_data;
503 const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
504 int i, error;
506 if (!gpio_data)
507 return 0;
509 kpad->gc.ngpio = adp5589_build_gpiomap(kpad, pdata);
510 if (kpad->gc.ngpio == 0) {
511 dev_info(dev, "No unused gpios left to export\n");
512 return 0;
515 kpad->export_gpio = true;
517 kpad->gc.direction_input = adp5589_gpio_direction_input;
518 kpad->gc.direction_output = adp5589_gpio_direction_output;
519 kpad->gc.get = adp5589_gpio_get_value;
520 kpad->gc.set = adp5589_gpio_set_value;
521 kpad->gc.can_sleep = 1;
523 kpad->gc.base = gpio_data->gpio_start;
524 kpad->gc.label = kpad->client->name;
525 kpad->gc.owner = THIS_MODULE;
527 mutex_init(&kpad->gpio_lock);
529 error = gpiochip_add(&kpad->gc);
530 if (error) {
531 dev_err(dev, "gpiochip_add failed, err: %d\n", error);
532 return error;
535 for (i = 0; i <= kpad->var->bank(kpad->var->maxgpio); i++) {
536 kpad->dat_out[i] = adp5589_read(kpad->client, kpad->var->reg(
537 ADP5589_GPO_DATA_OUT_A) + i);
538 kpad->dir[i] = adp5589_read(kpad->client, kpad->var->reg(
539 ADP5589_GPIO_DIRECTION_A) + i);
542 if (gpio_data->setup) {
543 error = gpio_data->setup(kpad->client,
544 kpad->gc.base, kpad->gc.ngpio,
545 gpio_data->context);
546 if (error)
547 dev_warn(dev, "setup failed, %d\n", error);
550 return 0;
553 static void __devexit adp5589_gpio_remove(struct adp5589_kpad *kpad)
555 struct device *dev = &kpad->client->dev;
556 const struct adp5589_kpad_platform_data *pdata = dev->platform_data;
557 const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
558 int error;
560 if (!kpad->export_gpio)
561 return;
563 if (gpio_data->teardown) {
564 error = gpio_data->teardown(kpad->client,
565 kpad->gc.base, kpad->gc.ngpio,
566 gpio_data->context);
567 if (error)
568 dev_warn(dev, "teardown failed %d\n", error);
571 error = gpiochip_remove(&kpad->gc);
572 if (error)
573 dev_warn(dev, "gpiochip_remove failed %d\n", error);
575 #else
576 static inline int adp5589_gpio_add(struct adp5589_kpad *kpad)
578 return 0;
581 static inline void adp5589_gpio_remove(struct adp5589_kpad *kpad)
584 #endif
586 static void adp5589_report_switches(struct adp5589_kpad *kpad,
587 int key, int key_val)
589 int i;
591 for (i = 0; i < kpad->gpimapsize; i++) {
592 if (key_val == kpad->gpimap[i].pin) {
593 input_report_switch(kpad->input,
594 kpad->gpimap[i].sw_evt,
595 key & KEY_EV_PRESSED);
596 break;
601 static void adp5589_report_events(struct adp5589_kpad *kpad, int ev_cnt)
603 int i;
605 for (i = 0; i < ev_cnt; i++) {
606 int key = adp5589_read(kpad->client, ADP5589_5_FIFO_1 + i);
607 int key_val = key & KEY_EV_MASK;
609 if (key_val >= kpad->var->gpi_pin_base &&
610 key_val <= kpad->var->gpi_pin_end) {
611 adp5589_report_switches(kpad, key, key_val);
612 } else {
613 input_report_key(kpad->input,
614 kpad->keycode[key_val - 1],
615 key & KEY_EV_PRESSED);
620 static irqreturn_t adp5589_irq(int irq, void *handle)
622 struct adp5589_kpad *kpad = handle;
623 struct i2c_client *client = kpad->client;
624 int status, ev_cnt;
626 status = adp5589_read(client, ADP5589_5_INT_STATUS);
628 if (status & OVRFLOW_INT) /* Unlikely and should never happen */
629 dev_err(&client->dev, "Event Overflow Error\n");
631 if (status & EVENT_INT) {
632 ev_cnt = adp5589_read(client, ADP5589_5_STATUS) & KEC;
633 if (ev_cnt) {
634 adp5589_report_events(kpad, ev_cnt);
635 input_sync(kpad->input);
639 adp5589_write(client, ADP5589_5_INT_STATUS, status); /* Status is W1C */
641 return IRQ_HANDLED;
644 static int __devinit adp5589_get_evcode(struct adp5589_kpad *kpad,
645 unsigned short key)
647 int i;
649 for (i = 0; i < kpad->var->keymapsize; i++)
650 if (key == kpad->keycode[i])
651 return (i + 1) | KEY_EV_PRESSED;
653 dev_err(&kpad->client->dev, "RESET/UNLOCK key not in keycode map\n");
655 return -EINVAL;
658 static int __devinit adp5589_setup(struct adp5589_kpad *kpad)
660 struct i2c_client *client = kpad->client;
661 const struct adp5589_kpad_platform_data *pdata =
662 client->dev.platform_data;
663 u8 (*reg) (u8) = kpad->var->reg;
664 unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
665 unsigned char pull_mask = 0;
666 int i, ret;
668 ret = adp5589_write(client, reg(ADP5589_PIN_CONFIG_A),
669 pdata->keypad_en_mask & kpad->var->row_mask);
670 ret |= adp5589_write(client, reg(ADP5589_PIN_CONFIG_B),
671 (pdata->keypad_en_mask >> kpad->var->col_shift) &
672 kpad->var->col_mask);
674 if (!kpad->is_adp5585)
675 ret |= adp5589_write(client, ADP5589_PIN_CONFIG_C,
676 (pdata->keypad_en_mask >> 16) & 0xFF);
678 if (!kpad->is_adp5585 && pdata->en_keylock) {
679 ret |= adp5589_write(client, ADP5589_UNLOCK1,
680 pdata->unlock_key1);
681 ret |= adp5589_write(client, ADP5589_UNLOCK2,
682 pdata->unlock_key2);
683 ret |= adp5589_write(client, ADP5589_UNLOCK_TIMERS,
684 pdata->unlock_timer & LTIME_MASK);
685 ret |= adp5589_write(client, ADP5589_LOCK_CFG, LOCK_EN);
688 for (i = 0; i < KEYP_MAX_EVENT; i++)
689 ret |= adp5589_read(client, ADP5589_5_FIFO_1 + i);
691 for (i = 0; i < pdata->gpimapsize; i++) {
692 unsigned short pin = pdata->gpimap[i].pin;
694 if (pin <= kpad->var->gpi_pin_row_end) {
695 evt_mode1 |= (1 << (pin - kpad->var->gpi_pin_row_base));
696 } else {
697 evt_mode2 |=
698 ((1 << (pin - kpad->var->gpi_pin_col_base)) & 0xFF);
699 if (!kpad->is_adp5585)
700 evt_mode3 |= ((1 << (pin -
701 kpad->var->gpi_pin_col_base)) >> 8);
705 if (pdata->gpimapsize) {
706 ret |= adp5589_write(client, reg(ADP5589_GPI_EVENT_EN_A),
707 evt_mode1);
708 ret |= adp5589_write(client, reg(ADP5589_GPI_EVENT_EN_B),
709 evt_mode2);
710 if (!kpad->is_adp5585)
711 ret |= adp5589_write(client,
712 reg(ADP5589_GPI_EVENT_EN_C),
713 evt_mode3);
716 if (pdata->pull_dis_mask & pdata->pullup_en_100k &
717 pdata->pullup_en_300k & pdata->pulldown_en_300k)
718 dev_warn(&client->dev, "Conflicting pull resistor config\n");
720 for (i = 0; i <= kpad->var->max_row_num; i++) {
721 unsigned val = 0, bit = (1 << i);
722 if (pdata->pullup_en_300k & bit)
723 val = 0;
724 else if (pdata->pulldown_en_300k & bit)
725 val = 1;
726 else if (pdata->pullup_en_100k & bit)
727 val = 2;
728 else if (pdata->pull_dis_mask & bit)
729 val = 3;
731 pull_mask |= val << (2 * (i & 0x3));
733 if (i == 3 || i == kpad->var->max_row_num) {
734 ret |= adp5589_write(client, reg(ADP5585_RPULL_CONFIG_A)
735 + (i >> 2), pull_mask);
736 pull_mask = 0;
740 for (i = 0; i <= kpad->var->max_col_num; i++) {
741 unsigned val = 0, bit = 1 << (i + kpad->var->col_shift);
742 if (pdata->pullup_en_300k & bit)
743 val = 0;
744 else if (pdata->pulldown_en_300k & bit)
745 val = 1;
746 else if (pdata->pullup_en_100k & bit)
747 val = 2;
748 else if (pdata->pull_dis_mask & bit)
749 val = 3;
751 pull_mask |= val << (2 * (i & 0x3));
753 if (i == 3 || i == kpad->var->max_col_num) {
754 ret |= adp5589_write(client,
755 reg(ADP5585_RPULL_CONFIG_C) +
756 (i >> 2), pull_mask);
757 pull_mask = 0;
761 if (pdata->reset1_key_1 && pdata->reset1_key_2 && pdata->reset1_key_3) {
762 ret |= adp5589_write(client, reg(ADP5589_RESET1_EVENT_A),
763 adp5589_get_evcode(kpad,
764 pdata->reset1_key_1));
765 ret |= adp5589_write(client, reg(ADP5589_RESET1_EVENT_B),
766 adp5589_get_evcode(kpad,
767 pdata->reset1_key_2));
768 ret |= adp5589_write(client, reg(ADP5589_RESET1_EVENT_C),
769 adp5589_get_evcode(kpad,
770 pdata->reset1_key_3));
771 kpad->extend_cfg |= R4_EXTEND_CFG;
774 if (pdata->reset2_key_1 && pdata->reset2_key_2) {
775 ret |= adp5589_write(client, reg(ADP5589_RESET2_EVENT_A),
776 adp5589_get_evcode(kpad,
777 pdata->reset2_key_1));
778 ret |= adp5589_write(client, reg(ADP5589_RESET2_EVENT_B),
779 adp5589_get_evcode(kpad,
780 pdata->reset2_key_2));
781 kpad->extend_cfg |= C4_EXTEND_CFG;
784 if (kpad->extend_cfg) {
785 ret |= adp5589_write(client, reg(ADP5589_RESET_CFG),
786 pdata->reset_cfg);
787 ret |= adp5589_write(client, reg(ADP5589_PIN_CONFIG_D),
788 kpad->extend_cfg);
791 ret |= adp5589_write(client, reg(ADP5589_DEBOUNCE_DIS_A),
792 pdata->debounce_dis_mask & kpad->var->row_mask);
794 ret |= adp5589_write(client, reg(ADP5589_DEBOUNCE_DIS_B),
795 (pdata->debounce_dis_mask >> kpad->var->col_shift)
796 & kpad->var->col_mask);
798 if (!kpad->is_adp5585)
799 ret |= adp5589_write(client, reg(ADP5589_DEBOUNCE_DIS_C),
800 (pdata->debounce_dis_mask >> 16) & 0xFF);
802 ret |= adp5589_write(client, reg(ADP5589_POLL_PTIME_CFG),
803 pdata->scan_cycle_time & PTIME_MASK);
804 ret |= adp5589_write(client, ADP5589_5_INT_STATUS,
805 (kpad->is_adp5585 ? 0 : LOGIC2_INT) |
806 LOGIC1_INT | OVRFLOW_INT |
807 (kpad->is_adp5585 ? 0 : LOCK_INT) |
808 GPI_INT | EVENT_INT); /* Status is W1C */
810 ret |= adp5589_write(client, reg(ADP5589_GENERAL_CFG),
811 INT_CFG | OSC_EN | CORE_CLK(3));
812 ret |= adp5589_write(client, reg(ADP5589_INT_EN),
813 OVRFLOW_IEN | GPI_IEN | EVENT_IEN);
815 if (ret < 0) {
816 dev_err(&client->dev, "Write Error\n");
817 return ret;
820 return 0;
823 static void __devinit adp5589_report_switch_state(struct adp5589_kpad *kpad)
825 int gpi_stat_tmp, pin_loc;
826 int i;
827 int gpi_stat1 = adp5589_read(kpad->client,
828 kpad->var->reg(ADP5589_GPI_STATUS_A));
829 int gpi_stat2 = adp5589_read(kpad->client,
830 kpad->var->reg(ADP5589_GPI_STATUS_B));
831 int gpi_stat3 = !kpad->is_adp5585 ?
832 adp5589_read(kpad->client, ADP5589_GPI_STATUS_C) : 0;
834 for (i = 0; i < kpad->gpimapsize; i++) {
835 unsigned short pin = kpad->gpimap[i].pin;
837 if (pin <= kpad->var->gpi_pin_row_end) {
838 gpi_stat_tmp = gpi_stat1;
839 pin_loc = pin - kpad->var->gpi_pin_row_base;
840 } else if ((pin - kpad->var->gpi_pin_col_base) < 8) {
841 gpi_stat_tmp = gpi_stat2;
842 pin_loc = pin - kpad->var->gpi_pin_col_base;
843 } else {
844 gpi_stat_tmp = gpi_stat3;
845 pin_loc = pin - kpad->var->gpi_pin_col_base - 8;
848 if (gpi_stat_tmp < 0) {
849 dev_err(&kpad->client->dev,
850 "Can't read GPIO_DAT_STAT switch %d, default to OFF\n",
851 pin);
852 gpi_stat_tmp = 0;
855 input_report_switch(kpad->input,
856 kpad->gpimap[i].sw_evt,
857 !(gpi_stat_tmp & (1 << pin_loc)));
860 input_sync(kpad->input);
863 static int __devinit adp5589_probe(struct i2c_client *client,
864 const struct i2c_device_id *id)
866 struct adp5589_kpad *kpad;
867 const struct adp5589_kpad_platform_data *pdata =
868 client->dev.platform_data;
869 struct input_dev *input;
870 unsigned int revid;
871 int ret, i;
872 int error;
874 if (!i2c_check_functionality(client->adapter,
875 I2C_FUNC_SMBUS_BYTE_DATA)) {
876 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
877 return -EIO;
880 if (!pdata) {
881 dev_err(&client->dev, "no platform data?\n");
882 return -EINVAL;
885 kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
886 if (!kpad)
887 return -ENOMEM;
889 switch (id->driver_data) {
890 case ADP5585_02:
891 kpad->adp5585_support_row5 = true;
892 case ADP5585_01:
893 kpad->is_adp5585 = true;
894 kpad->var = &const_adp5585;
895 break;
896 case ADP5589:
897 kpad->var = &const_adp5589;
898 break;
901 if (!((pdata->keypad_en_mask & kpad->var->row_mask) &&
902 (pdata->keypad_en_mask >> kpad->var->col_shift)) ||
903 !pdata->keymap) {
904 dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
905 error = -EINVAL;
906 goto err_free_mem;
909 if (pdata->keymapsize != kpad->var->keymapsize) {
910 dev_err(&client->dev, "invalid keymapsize\n");
911 error = -EINVAL;
912 goto err_free_mem;
915 if (!pdata->gpimap && pdata->gpimapsize) {
916 dev_err(&client->dev, "invalid gpimap from pdata\n");
917 error = -EINVAL;
918 goto err_free_mem;
921 if (pdata->gpimapsize > kpad->var->gpimapsize_max) {
922 dev_err(&client->dev, "invalid gpimapsize\n");
923 error = -EINVAL;
924 goto err_free_mem;
927 for (i = 0; i < pdata->gpimapsize; i++) {
928 unsigned short pin = pdata->gpimap[i].pin;
930 if (pin < kpad->var->gpi_pin_base ||
931 pin > kpad->var->gpi_pin_end) {
932 dev_err(&client->dev, "invalid gpi pin data\n");
933 error = -EINVAL;
934 goto err_free_mem;
937 if ((1 << (pin - kpad->var->gpi_pin_row_base)) &
938 pdata->keypad_en_mask) {
939 dev_err(&client->dev, "invalid gpi row/col data\n");
940 error = -EINVAL;
941 goto err_free_mem;
945 if (!client->irq) {
946 dev_err(&client->dev, "no IRQ?\n");
947 error = -EINVAL;
948 goto err_free_mem;
951 input = input_allocate_device();
952 if (!input) {
953 error = -ENOMEM;
954 goto err_free_mem;
957 kpad->client = client;
958 kpad->input = input;
960 ret = adp5589_read(client, ADP5589_5_ID);
961 if (ret < 0) {
962 error = ret;
963 goto err_free_input;
966 revid = (u8) ret & ADP5589_5_DEVICE_ID_MASK;
968 input->name = client->name;
969 input->phys = "adp5589-keys/input0";
970 input->dev.parent = &client->dev;
972 input_set_drvdata(input, kpad);
974 input->id.bustype = BUS_I2C;
975 input->id.vendor = 0x0001;
976 input->id.product = 0x0001;
977 input->id.version = revid;
979 input->keycodesize = sizeof(kpad->keycode[0]);
980 input->keycodemax = pdata->keymapsize;
981 input->keycode = kpad->keycode;
983 memcpy(kpad->keycode, pdata->keymap,
984 pdata->keymapsize * input->keycodesize);
986 kpad->gpimap = pdata->gpimap;
987 kpad->gpimapsize = pdata->gpimapsize;
989 /* setup input device */
990 __set_bit(EV_KEY, input->evbit);
992 if (pdata->repeat)
993 __set_bit(EV_REP, input->evbit);
995 for (i = 0; i < input->keycodemax; i++)
996 __set_bit(kpad->keycode[i] & KEY_MAX, input->keybit);
997 __clear_bit(KEY_RESERVED, input->keybit);
999 if (kpad->gpimapsize)
1000 __set_bit(EV_SW, input->evbit);
1001 for (i = 0; i < kpad->gpimapsize; i++)
1002 __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
1004 error = input_register_device(input);
1005 if (error) {
1006 dev_err(&client->dev, "unable to register input device\n");
1007 goto err_free_input;
1010 error = request_threaded_irq(client->irq, NULL, adp5589_irq,
1011 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1012 client->dev.driver->name, kpad);
1013 if (error) {
1014 dev_err(&client->dev, "irq %d busy?\n", client->irq);
1015 goto err_unreg_dev;
1018 error = adp5589_setup(kpad);
1019 if (error)
1020 goto err_free_irq;
1022 if (kpad->gpimapsize)
1023 adp5589_report_switch_state(kpad);
1025 error = adp5589_gpio_add(kpad);
1026 if (error)
1027 goto err_free_irq;
1029 device_init_wakeup(&client->dev, 1);
1030 i2c_set_clientdata(client, kpad);
1032 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
1033 return 0;
1035 err_free_irq:
1036 free_irq(client->irq, kpad);
1037 err_unreg_dev:
1038 input_unregister_device(input);
1039 input = NULL;
1040 err_free_input:
1041 input_free_device(input);
1042 err_free_mem:
1043 kfree(kpad);
1045 return error;
1048 static int __devexit adp5589_remove(struct i2c_client *client)
1050 struct adp5589_kpad *kpad = i2c_get_clientdata(client);
1052 adp5589_write(client, kpad->var->reg(ADP5589_GENERAL_CFG), 0);
1053 free_irq(client->irq, kpad);
1054 input_unregister_device(kpad->input);
1055 adp5589_gpio_remove(kpad);
1056 kfree(kpad);
1058 return 0;
1061 #ifdef CONFIG_PM_SLEEP
1062 static int adp5589_suspend(struct device *dev)
1064 struct adp5589_kpad *kpad = dev_get_drvdata(dev);
1065 struct i2c_client *client = kpad->client;
1067 disable_irq(client->irq);
1069 if (device_may_wakeup(&client->dev))
1070 enable_irq_wake(client->irq);
1072 return 0;
1075 static int adp5589_resume(struct device *dev)
1077 struct adp5589_kpad *kpad = dev_get_drvdata(dev);
1078 struct i2c_client *client = kpad->client;
1080 if (device_may_wakeup(&client->dev))
1081 disable_irq_wake(client->irq);
1083 enable_irq(client->irq);
1085 return 0;
1087 #endif
1089 static SIMPLE_DEV_PM_OPS(adp5589_dev_pm_ops, adp5589_suspend, adp5589_resume);
1091 static const struct i2c_device_id adp5589_id[] = {
1092 {"adp5589-keys", ADP5589},
1093 {"adp5585-keys", ADP5585_01},
1094 {"adp5585-02-keys", ADP5585_02}, /* Adds ROW5 to ADP5585 */
1098 MODULE_DEVICE_TABLE(i2c, adp5589_id);
1100 static struct i2c_driver adp5589_driver = {
1101 .driver = {
1102 .name = KBUILD_MODNAME,
1103 .owner = THIS_MODULE,
1104 .pm = &adp5589_dev_pm_ops,
1106 .probe = adp5589_probe,
1107 .remove = __devexit_p(adp5589_remove),
1108 .id_table = adp5589_id,
1111 module_i2c_driver(adp5589_driver);
1113 MODULE_LICENSE("GPL");
1114 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
1115 MODULE_DESCRIPTION("ADP5589/ADP5585 Keypad driver");