blob: 67d12b3427c9ee9bcb5da69d276a6bc814467586 [file] [log] [blame]
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001/*
Michael Hennerich3f48e732011-10-18 21:12:39 -07002 * Description: keypad driver for ADP5589, ADP5585
Michael Hennerich9d2e1732011-05-19 00:59:32 -07003 * I2C QWERTY Keypad and IO Expander
4 * Bugs: Enter bugs at http://blackfin.uclinux.org/
5 *
6 * Copyright (C) 2010-2011 Analog Devices Inc.
7 * Licensed under the GPL-2.
8 */
9
10#include <linux/module.h>
Michael Hennerich9d2e1732011-05-19 00:59:32 -070011#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>
22
23#include <linux/input/adp5589.h>
24
Michael Hennerich3f48e732011-10-18 21:12:39 -070025/* 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
47
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
107
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
149
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
155
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700156/* GENERAL_CFG Register */
157#define OSC_EN (1 << 7)
158#define CORE_CLK(x) (((x) & 0x3) << 5)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700159#define LCK_TRK_LOGIC (1 << 4) /* ADP5589 only */
160#define LCK_TRK_GPI (1 << 3) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700161#define INT_CFG (1 << 1)
162#define RST_CFG (1 << 0)
163
164/* INT_EN Register */
Michael Hennerich3f48e732011-10-18 21:12:39 -0700165#define LOGIC2_IEN (1 << 5) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700166#define LOGIC1_IEN (1 << 4)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700167#define LOCK_IEN (1 << 3) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700168#define OVRFLOW_IEN (1 << 2)
169#define GPI_IEN (1 << 1)
170#define EVENT_IEN (1 << 0)
171
172/* Interrupt Status Register */
Michael Hennerich3f48e732011-10-18 21:12:39 -0700173#define LOGIC2_INT (1 << 5) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700174#define LOGIC1_INT (1 << 4)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700175#define LOCK_INT (1 << 3) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700176#define OVRFLOW_INT (1 << 2)
177#define GPI_INT (1 << 1)
178#define EVENT_INT (1 << 0)
179
180/* STATUS Register */
Michael Hennerich3f48e732011-10-18 21:12:39 -0700181#define LOGIC2_STAT (1 << 7) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700182#define LOGIC1_STAT (1 << 6)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700183#define LOCK_STAT (1 << 5) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700184#define KEC 0xF
185
186/* PIN_CONFIG_D Register */
187#define C4_EXTEND_CFG (1 << 6) /* RESET2 */
188#define R4_EXTEND_CFG (1 << 5) /* RESET1 */
189
190/* LOCK_CFG */
191#define LOCK_EN (1 << 0)
192
193#define PTIME_MASK 0x3
Michael Hennerich3f48e732011-10-18 21:12:39 -0700194#define LTIME_MASK 0x3 /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700195
196/* Key Event Register xy */
197#define KEY_EV_PRESSED (1 << 7)
198#define KEY_EV_MASK (0x7F)
199
200#define KEYP_MAX_EVENT 16
Michael Hennerich3f48e732011-10-18 21:12:39 -0700201#define ADP5589_MAXGPIO 19
202#define ADP5585_MAXGPIO 11 /* 10 on the ADP5585-01, 11 on ADP5585-02 */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700203
Michael Hennerich3f48e732011-10-18 21:12:39 -0700204enum {
205 ADP5589,
206 ADP5585_01,
207 ADP5585_02
208};
209
210struct 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);
228};
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700229
230struct adp5589_kpad {
231 struct i2c_client *client;
232 struct input_dev *input;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700233 const struct adp_constants *var;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700234 unsigned short keycode[ADP5589_KEYMAPSIZE];
235 const struct adp5589_gpi_map *gpimap;
236 unsigned short gpimapsize;
237 unsigned extend_cfg;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700238 bool is_adp5585;
239 bool adp5585_support_row5;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700240#ifdef CONFIG_GPIOLIB
Michael Hennerich3f48e732011-10-18 21:12:39 -0700241 unsigned char gpiomap[ADP5589_MAXGPIO];
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700242 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
248};
249
Michael Hennerich3f48e732011-10-18 21:12:39 -0700250/*
251 * ADP5589 / ADP5585 derivative / variant handling
252 */
253
254
255/* ADP5589 */
256
257static unsigned char adp5589_bank(unsigned char offset)
258{
259 return offset >> 3;
260}
261
262static unsigned char adp5589_bit(unsigned char offset)
263{
264 return 1u << (offset & 0x7);
265}
266
267static unsigned char adp5589_reg(unsigned char reg)
268{
269 return reg;
270}
271
272static 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,
290};
291
292/* ADP5585 */
293
294static unsigned char adp5585_bank(unsigned char offset)
295{
296 return offset > ADP5585_MAX_ROW_NUM;
297}
298
299static unsigned char adp5585_bit(unsigned char offset)
300{
301 return (offset > ADP5585_MAX_ROW_NUM) ?
302 1u << (offset - ADP5585_COL_SHIFT) : 1u << offset;
303}
304
305static 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,
346};
347
348static unsigned char adp5585_reg(unsigned char reg)
349{
350 return adp5585_reg_lut[reg];
351}
352
353static 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,
371};
372
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700373static int adp5589_read(struct i2c_client *client, u8 reg)
374{
375 int ret = i2c_smbus_read_byte_data(client, reg);
376
377 if (ret < 0)
378 dev_err(&client->dev, "Read Error\n");
379
380 return ret;
381}
382
383static int adp5589_write(struct i2c_client *client, u8 reg, u8 val)
384{
385 return i2c_smbus_write_byte_data(client, reg, val);
386}
387
388#ifdef CONFIG_GPIOLIB
389static int adp5589_gpio_get_value(struct gpio_chip *chip, unsigned off)
390{
391 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700392 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
393 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700394
Michael Hennerich3f48e732011-10-18 21:12:39 -0700395 return !!(adp5589_read(kpad->client,
396 kpad->var->reg(ADP5589_GPI_STATUS_A) + bank) &
397 bit);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700398}
399
400static void adp5589_gpio_set_value(struct gpio_chip *chip,
401 unsigned off, int val)
402{
403 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700404 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
405 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700406
407 mutex_lock(&kpad->gpio_lock);
408
409 if (val)
410 kpad->dat_out[bank] |= bit;
411 else
412 kpad->dat_out[bank] &= ~bit;
413
Michael Hennerich3f48e732011-10-18 21:12:39 -0700414 adp5589_write(kpad->client, kpad->var->reg(ADP5589_GPO_DATA_OUT_A) +
415 bank, kpad->dat_out[bank]);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700416
417 mutex_unlock(&kpad->gpio_lock);
418}
419
420static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
421{
422 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700423 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
424 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700425 int ret;
426
427 mutex_lock(&kpad->gpio_lock);
428
429 kpad->dir[bank] &= ~bit;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700430 ret = adp5589_write(kpad->client,
431 kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700432 kpad->dir[bank]);
433
434 mutex_unlock(&kpad->gpio_lock);
435
436 return ret;
437}
438
439static int adp5589_gpio_direction_output(struct gpio_chip *chip,
440 unsigned off, int val)
441{
442 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700443 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
444 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700445 int ret;
446
447 mutex_lock(&kpad->gpio_lock);
448
449 kpad->dir[bank] |= bit;
450
451 if (val)
452 kpad->dat_out[bank] |= bit;
453 else
454 kpad->dat_out[bank] &= ~bit;
455
Michael Hennerich3f48e732011-10-18 21:12:39 -0700456 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,
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700460 kpad->dir[bank]);
461
462 mutex_unlock(&kpad->gpio_lock);
463
464 return ret;
465}
466
Bill Pemberton5298cc42012-11-23 21:38:25 -0800467static int adp5589_build_gpiomap(struct adp5589_kpad *kpad,
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700468 const struct adp5589_kpad_platform_data *pdata)
469{
Michael Hennerich3f48e732011-10-18 21:12:39 -0700470 bool pin_used[ADP5589_MAXGPIO];
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700471 int n_unused = 0;
472 int i;
473
474 memset(pin_used, false, sizeof(pin_used));
475
Michael Hennerich3f48e732011-10-18 21:12:39 -0700476 for (i = 0; i < kpad->var->maxgpio; i++)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700477 if (pdata->keypad_en_mask & (1 << i))
478 pin_used[i] = true;
479
480 for (i = 0; i < kpad->gpimapsize; i++)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700481 pin_used[kpad->gpimap[i].pin - kpad->var->gpi_pin_base] = true;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700482
483 if (kpad->extend_cfg & R4_EXTEND_CFG)
484 pin_used[4] = true;
485
486 if (kpad->extend_cfg & C4_EXTEND_CFG)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700487 pin_used[kpad->var->c4_extend_cfg] = true;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700488
Michael Hennerich3f48e732011-10-18 21:12:39 -0700489 if (!kpad->adp5585_support_row5)
490 pin_used[5] = true;
491
492 for (i = 0; i < kpad->var->maxgpio; i++)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700493 if (!pin_used[i])
494 kpad->gpiomap[n_unused++] = i;
495
496 return n_unused;
497}
498
Bill Pemberton5298cc42012-11-23 21:38:25 -0800499static int adp5589_gpio_add(struct adp5589_kpad *kpad)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700500{
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;
505
506 if (!gpio_data)
507 return 0;
508
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;
513 }
514
515 kpad->export_gpio = true;
516
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;
522
523 kpad->gc.base = gpio_data->gpio_start;
524 kpad->gc.label = kpad->client->name;
525 kpad->gc.owner = THIS_MODULE;
526
527 mutex_init(&kpad->gpio_lock);
528
529 error = gpiochip_add(&kpad->gc);
530 if (error) {
531 dev_err(dev, "gpiochip_add failed, err: %d\n", error);
532 return error;
533 }
534
Michael Hennerich3f48e732011-10-18 21:12:39 -0700535 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);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700540 }
541
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);
548 }
549
550 return 0;
551}
552
Bill Pembertone2619cf2012-11-23 21:50:47 -0800553static void adp5589_gpio_remove(struct adp5589_kpad *kpad)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700554{
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;
559
560 if (!kpad->export_gpio)
561 return;
562
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);
569 }
570
571 error = gpiochip_remove(&kpad->gc);
572 if (error)
573 dev_warn(dev, "gpiochip_remove failed %d\n", error);
574}
575#else
576static inline int adp5589_gpio_add(struct adp5589_kpad *kpad)
577{
578 return 0;
579}
580
581static inline void adp5589_gpio_remove(struct adp5589_kpad *kpad)
582{
583}
584#endif
585
586static void adp5589_report_switches(struct adp5589_kpad *kpad,
587 int key, int key_val)
588{
589 int i;
590
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;
597 }
598 }
599}
600
601static void adp5589_report_events(struct adp5589_kpad *kpad, int ev_cnt)
602{
603 int i;
604
605 for (i = 0; i < ev_cnt; i++) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700606 int key = adp5589_read(kpad->client, ADP5589_5_FIFO_1 + i);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700607 int key_val = key & KEY_EV_MASK;
608
Michael Hennerich3f48e732011-10-18 21:12:39 -0700609 if (key_val >= kpad->var->gpi_pin_base &&
610 key_val <= kpad->var->gpi_pin_end) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700611 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);
616 }
617 }
618}
619
620static irqreturn_t adp5589_irq(int irq, void *handle)
621{
622 struct adp5589_kpad *kpad = handle;
623 struct i2c_client *client = kpad->client;
624 int status, ev_cnt;
625
Michael Hennerich3f48e732011-10-18 21:12:39 -0700626 status = adp5589_read(client, ADP5589_5_INT_STATUS);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700627
628 if (status & OVRFLOW_INT) /* Unlikely and should never happen */
629 dev_err(&client->dev, "Event Overflow Error\n");
630
631 if (status & EVENT_INT) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700632 ev_cnt = adp5589_read(client, ADP5589_5_STATUS) & KEC;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700633 if (ev_cnt) {
634 adp5589_report_events(kpad, ev_cnt);
635 input_sync(kpad->input);
636 }
637 }
638
Michael Hennerich3f48e732011-10-18 21:12:39 -0700639 adp5589_write(client, ADP5589_5_INT_STATUS, status); /* Status is W1C */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700640
641 return IRQ_HANDLED;
642}
643
Bill Pemberton5298cc42012-11-23 21:38:25 -0800644static int adp5589_get_evcode(struct adp5589_kpad *kpad, unsigned short key)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700645{
646 int i;
647
Michael Hennerich3f48e732011-10-18 21:12:39 -0700648 for (i = 0; i < kpad->var->keymapsize; i++)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700649 if (key == kpad->keycode[i])
650 return (i + 1) | KEY_EV_PRESSED;
651
652 dev_err(&kpad->client->dev, "RESET/UNLOCK key not in keycode map\n");
653
654 return -EINVAL;
655}
656
Bill Pemberton5298cc42012-11-23 21:38:25 -0800657static int adp5589_setup(struct adp5589_kpad *kpad)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700658{
659 struct i2c_client *client = kpad->client;
660 const struct adp5589_kpad_platform_data *pdata =
Michael Hennerich3f48e732011-10-18 21:12:39 -0700661 client->dev.platform_data;
662 u8 (*reg) (u8) = kpad->var->reg;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700663 unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
664 unsigned char pull_mask = 0;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700665 int i, ret;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700666
Michael Hennerich3f48e732011-10-18 21:12:39 -0700667 ret = adp5589_write(client, reg(ADP5589_PIN_CONFIG_A),
668 pdata->keypad_en_mask & kpad->var->row_mask);
669 ret |= adp5589_write(client, reg(ADP5589_PIN_CONFIG_B),
670 (pdata->keypad_en_mask >> kpad->var->col_shift) &
671 kpad->var->col_mask);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700672
Michael Hennerich3f48e732011-10-18 21:12:39 -0700673 if (!kpad->is_adp5585)
674 ret |= adp5589_write(client, ADP5589_PIN_CONFIG_C,
675 (pdata->keypad_en_mask >> 16) & 0xFF);
676
677 if (!kpad->is_adp5585 && pdata->en_keylock) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700678 ret |= adp5589_write(client, ADP5589_UNLOCK1,
679 pdata->unlock_key1);
680 ret |= adp5589_write(client, ADP5589_UNLOCK2,
681 pdata->unlock_key2);
682 ret |= adp5589_write(client, ADP5589_UNLOCK_TIMERS,
683 pdata->unlock_timer & LTIME_MASK);
684 ret |= adp5589_write(client, ADP5589_LOCK_CFG, LOCK_EN);
685 }
686
687 for (i = 0; i < KEYP_MAX_EVENT; i++)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700688 ret |= adp5589_read(client, ADP5589_5_FIFO_1 + i);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700689
690 for (i = 0; i < pdata->gpimapsize; i++) {
691 unsigned short pin = pdata->gpimap[i].pin;
692
Michael Hennerich3f48e732011-10-18 21:12:39 -0700693 if (pin <= kpad->var->gpi_pin_row_end) {
694 evt_mode1 |= (1 << (pin - kpad->var->gpi_pin_row_base));
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700695 } else {
696 evt_mode2 |=
Michael Hennerich3f48e732011-10-18 21:12:39 -0700697 ((1 << (pin - kpad->var->gpi_pin_col_base)) & 0xFF);
698 if (!kpad->is_adp5585)
699 evt_mode3 |= ((1 << (pin -
700 kpad->var->gpi_pin_col_base)) >> 8);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700701 }
702 }
703
704 if (pdata->gpimapsize) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700705 ret |= adp5589_write(client, reg(ADP5589_GPI_EVENT_EN_A),
706 evt_mode1);
707 ret |= adp5589_write(client, reg(ADP5589_GPI_EVENT_EN_B),
708 evt_mode2);
709 if (!kpad->is_adp5585)
710 ret |= adp5589_write(client,
711 reg(ADP5589_GPI_EVENT_EN_C),
712 evt_mode3);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700713 }
714
715 if (pdata->pull_dis_mask & pdata->pullup_en_100k &
Michael Hennerich3f48e732011-10-18 21:12:39 -0700716 pdata->pullup_en_300k & pdata->pulldown_en_300k)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700717 dev_warn(&client->dev, "Conflicting pull resistor config\n");
718
Michael Hennerich3f48e732011-10-18 21:12:39 -0700719 for (i = 0; i <= kpad->var->max_row_num; i++) {
720 unsigned val = 0, bit = (1 << i);
721 if (pdata->pullup_en_300k & bit)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700722 val = 0;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700723 else if (pdata->pulldown_en_300k & bit)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700724 val = 1;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700725 else if (pdata->pullup_en_100k & bit)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700726 val = 2;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700727 else if (pdata->pull_dis_mask & bit)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700728 val = 3;
729
730 pull_mask |= val << (2 * (i & 0x3));
731
Michael Hennerich3f48e732011-10-18 21:12:39 -0700732 if (i == 3 || i == kpad->var->max_row_num) {
733 ret |= adp5589_write(client, reg(ADP5585_RPULL_CONFIG_A)
734 + (i >> 2), pull_mask);
735 pull_mask = 0;
736 }
737 }
738
739 for (i = 0; i <= kpad->var->max_col_num; i++) {
740 unsigned val = 0, bit = 1 << (i + kpad->var->col_shift);
741 if (pdata->pullup_en_300k & bit)
742 val = 0;
743 else if (pdata->pulldown_en_300k & bit)
744 val = 1;
745 else if (pdata->pullup_en_100k & bit)
746 val = 2;
747 else if (pdata->pull_dis_mask & bit)
748 val = 3;
749
750 pull_mask |= val << (2 * (i & 0x3));
751
752 if (i == 3 || i == kpad->var->max_col_num) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700753 ret |= adp5589_write(client,
Michael Hennerich3f48e732011-10-18 21:12:39 -0700754 reg(ADP5585_RPULL_CONFIG_C) +
755 (i >> 2), pull_mask);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700756 pull_mask = 0;
757 }
758 }
759
760 if (pdata->reset1_key_1 && pdata->reset1_key_2 && pdata->reset1_key_3) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700761 ret |= adp5589_write(client, reg(ADP5589_RESET1_EVENT_A),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700762 adp5589_get_evcode(kpad,
763 pdata->reset1_key_1));
Michael Hennerich3f48e732011-10-18 21:12:39 -0700764 ret |= adp5589_write(client, reg(ADP5589_RESET1_EVENT_B),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700765 adp5589_get_evcode(kpad,
766 pdata->reset1_key_2));
Michael Hennerich3f48e732011-10-18 21:12:39 -0700767 ret |= adp5589_write(client, reg(ADP5589_RESET1_EVENT_C),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700768 adp5589_get_evcode(kpad,
769 pdata->reset1_key_3));
770 kpad->extend_cfg |= R4_EXTEND_CFG;
771 }
772
773 if (pdata->reset2_key_1 && pdata->reset2_key_2) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700774 ret |= adp5589_write(client, reg(ADP5589_RESET2_EVENT_A),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700775 adp5589_get_evcode(kpad,
776 pdata->reset2_key_1));
Michael Hennerich3f48e732011-10-18 21:12:39 -0700777 ret |= adp5589_write(client, reg(ADP5589_RESET2_EVENT_B),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700778 adp5589_get_evcode(kpad,
779 pdata->reset2_key_2));
780 kpad->extend_cfg |= C4_EXTEND_CFG;
781 }
782
783 if (kpad->extend_cfg) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700784 ret |= adp5589_write(client, reg(ADP5589_RESET_CFG),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700785 pdata->reset_cfg);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700786 ret |= adp5589_write(client, reg(ADP5589_PIN_CONFIG_D),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700787 kpad->extend_cfg);
788 }
789
Michael Hennerich3f48e732011-10-18 21:12:39 -0700790 ret |= adp5589_write(client, reg(ADP5589_DEBOUNCE_DIS_A),
791 pdata->debounce_dis_mask & kpad->var->row_mask);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700792
Michael Hennerich3f48e732011-10-18 21:12:39 -0700793 ret |= adp5589_write(client, reg(ADP5589_DEBOUNCE_DIS_B),
794 (pdata->debounce_dis_mask >> kpad->var->col_shift)
795 & kpad->var->col_mask);
796
797 if (!kpad->is_adp5585)
798 ret |= adp5589_write(client, reg(ADP5589_DEBOUNCE_DIS_C),
799 (pdata->debounce_dis_mask >> 16) & 0xFF);
800
801 ret |= adp5589_write(client, reg(ADP5589_POLL_PTIME_CFG),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700802 pdata->scan_cycle_time & PTIME_MASK);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700803 ret |= adp5589_write(client, ADP5589_5_INT_STATUS,
804 (kpad->is_adp5585 ? 0 : LOGIC2_INT) |
805 LOGIC1_INT | OVRFLOW_INT |
806 (kpad->is_adp5585 ? 0 : LOCK_INT) |
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700807 GPI_INT | EVENT_INT); /* Status is W1C */
808
Michael Hennerich3f48e732011-10-18 21:12:39 -0700809 ret |= adp5589_write(client, reg(ADP5589_GENERAL_CFG),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700810 INT_CFG | OSC_EN | CORE_CLK(3));
Michael Hennerich3f48e732011-10-18 21:12:39 -0700811 ret |= adp5589_write(client, reg(ADP5589_INT_EN),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700812 OVRFLOW_IEN | GPI_IEN | EVENT_IEN);
813
814 if (ret < 0) {
815 dev_err(&client->dev, "Write Error\n");
816 return ret;
817 }
818
819 return 0;
820}
821
Bill Pemberton5298cc42012-11-23 21:38:25 -0800822static void adp5589_report_switch_state(struct adp5589_kpad *kpad)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700823{
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700824 int gpi_stat_tmp, pin_loc;
825 int i;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700826 int gpi_stat1 = adp5589_read(kpad->client,
827 kpad->var->reg(ADP5589_GPI_STATUS_A));
828 int gpi_stat2 = adp5589_read(kpad->client,
829 kpad->var->reg(ADP5589_GPI_STATUS_B));
830 int gpi_stat3 = !kpad->is_adp5585 ?
831 adp5589_read(kpad->client, ADP5589_GPI_STATUS_C) : 0;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700832
833 for (i = 0; i < kpad->gpimapsize; i++) {
834 unsigned short pin = kpad->gpimap[i].pin;
835
Michael Hennerich3f48e732011-10-18 21:12:39 -0700836 if (pin <= kpad->var->gpi_pin_row_end) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700837 gpi_stat_tmp = gpi_stat1;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700838 pin_loc = pin - kpad->var->gpi_pin_row_base;
839 } else if ((pin - kpad->var->gpi_pin_col_base) < 8) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700840 gpi_stat_tmp = gpi_stat2;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700841 pin_loc = pin - kpad->var->gpi_pin_col_base;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700842 } else {
843 gpi_stat_tmp = gpi_stat3;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700844 pin_loc = pin - kpad->var->gpi_pin_col_base - 8;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700845 }
846
847 if (gpi_stat_tmp < 0) {
848 dev_err(&kpad->client->dev,
Michael Hennerich3f48e732011-10-18 21:12:39 -0700849 "Can't read GPIO_DAT_STAT switch %d, default to OFF\n",
850 pin);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700851 gpi_stat_tmp = 0;
852 }
853
854 input_report_switch(kpad->input,
855 kpad->gpimap[i].sw_evt,
856 !(gpi_stat_tmp & (1 << pin_loc)));
857 }
858
859 input_sync(kpad->input);
860}
861
Bill Pemberton5298cc42012-11-23 21:38:25 -0800862static int adp5589_probe(struct i2c_client *client,
863 const struct i2c_device_id *id)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700864{
865 struct adp5589_kpad *kpad;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700866 const struct adp5589_kpad_platform_data *pdata =
867 client->dev.platform_data;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700868 struct input_dev *input;
869 unsigned int revid;
870 int ret, i;
871 int error;
872
873 if (!i2c_check_functionality(client->adapter,
874 I2C_FUNC_SMBUS_BYTE_DATA)) {
875 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
876 return -EIO;
877 }
878
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700879 if (!pdata) {
880 dev_err(&client->dev, "no platform data?\n");
881 return -EINVAL;
882 }
883
Michael Hennerich3f48e732011-10-18 21:12:39 -0700884 kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
885 if (!kpad)
886 return -ENOMEM;
887
888 switch (id->driver_data) {
889 case ADP5585_02:
890 kpad->adp5585_support_row5 = true;
891 case ADP5585_01:
892 kpad->is_adp5585 = true;
893 kpad->var = &const_adp5585;
894 break;
895 case ADP5589:
896 kpad->var = &const_adp5589;
897 break;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700898 }
899
Michael Hennerich3f48e732011-10-18 21:12:39 -0700900 if (!((pdata->keypad_en_mask & kpad->var->row_mask) &&
901 (pdata->keypad_en_mask >> kpad->var->col_shift)) ||
902 !pdata->keymap) {
903 dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
904 error = -EINVAL;
905 goto err_free_mem;
906 }
907
908 if (pdata->keymapsize != kpad->var->keymapsize) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700909 dev_err(&client->dev, "invalid keymapsize\n");
Michael Hennerich3f48e732011-10-18 21:12:39 -0700910 error = -EINVAL;
911 goto err_free_mem;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700912 }
913
914 if (!pdata->gpimap && pdata->gpimapsize) {
915 dev_err(&client->dev, "invalid gpimap from pdata\n");
Michael Hennerich3f48e732011-10-18 21:12:39 -0700916 error = -EINVAL;
917 goto err_free_mem;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700918 }
919
Michael Hennerich3f48e732011-10-18 21:12:39 -0700920 if (pdata->gpimapsize > kpad->var->gpimapsize_max) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700921 dev_err(&client->dev, "invalid gpimapsize\n");
Michael Hennerich3f48e732011-10-18 21:12:39 -0700922 error = -EINVAL;
923 goto err_free_mem;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700924 }
925
926 for (i = 0; i < pdata->gpimapsize; i++) {
927 unsigned short pin = pdata->gpimap[i].pin;
928
Michael Hennerich3f48e732011-10-18 21:12:39 -0700929 if (pin < kpad->var->gpi_pin_base ||
930 pin > kpad->var->gpi_pin_end) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700931 dev_err(&client->dev, "invalid gpi pin data\n");
Michael Hennerich3f48e732011-10-18 21:12:39 -0700932 error = -EINVAL;
933 goto err_free_mem;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700934 }
935
Michael Hennerich3f48e732011-10-18 21:12:39 -0700936 if ((1 << (pin - kpad->var->gpi_pin_row_base)) &
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700937 pdata->keypad_en_mask) {
938 dev_err(&client->dev, "invalid gpi row/col data\n");
Michael Hennerich3f48e732011-10-18 21:12:39 -0700939 error = -EINVAL;
940 goto err_free_mem;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700941 }
942 }
943
944 if (!client->irq) {
945 dev_err(&client->dev, "no IRQ?\n");
Michael Hennerich3f48e732011-10-18 21:12:39 -0700946 error = -EINVAL;
947 goto err_free_mem;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700948 }
949
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700950 input = input_allocate_device();
Michael Hennerich3f48e732011-10-18 21:12:39 -0700951 if (!input) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700952 error = -ENOMEM;
953 goto err_free_mem;
954 }
955
956 kpad->client = client;
957 kpad->input = input;
958
Michael Hennerich3f48e732011-10-18 21:12:39 -0700959 ret = adp5589_read(client, ADP5589_5_ID);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700960 if (ret < 0) {
961 error = ret;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700962 goto err_free_input;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700963 }
964
Michael Hennerich3f48e732011-10-18 21:12:39 -0700965 revid = (u8) ret & ADP5589_5_DEVICE_ID_MASK;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700966
967 input->name = client->name;
968 input->phys = "adp5589-keys/input0";
969 input->dev.parent = &client->dev;
970
971 input_set_drvdata(input, kpad);
972
973 input->id.bustype = BUS_I2C;
974 input->id.vendor = 0x0001;
975 input->id.product = 0x0001;
976 input->id.version = revid;
977
978 input->keycodesize = sizeof(kpad->keycode[0]);
979 input->keycodemax = pdata->keymapsize;
980 input->keycode = kpad->keycode;
981
982 memcpy(kpad->keycode, pdata->keymap,
983 pdata->keymapsize * input->keycodesize);
984
985 kpad->gpimap = pdata->gpimap;
986 kpad->gpimapsize = pdata->gpimapsize;
987
988 /* setup input device */
989 __set_bit(EV_KEY, input->evbit);
990
991 if (pdata->repeat)
992 __set_bit(EV_REP, input->evbit);
993
994 for (i = 0; i < input->keycodemax; i++)
995 __set_bit(kpad->keycode[i] & KEY_MAX, input->keybit);
996 __clear_bit(KEY_RESERVED, input->keybit);
997
998 if (kpad->gpimapsize)
999 __set_bit(EV_SW, input->evbit);
1000 for (i = 0; i < kpad->gpimapsize; i++)
1001 __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
1002
1003 error = input_register_device(input);
1004 if (error) {
1005 dev_err(&client->dev, "unable to register input device\n");
Michael Hennerich3f48e732011-10-18 21:12:39 -07001006 goto err_free_input;
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001007 }
1008
1009 error = request_threaded_irq(client->irq, NULL, adp5589_irq,
1010 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1011 client->dev.driver->name, kpad);
1012 if (error) {
1013 dev_err(&client->dev, "irq %d busy?\n", client->irq);
1014 goto err_unreg_dev;
1015 }
1016
1017 error = adp5589_setup(kpad);
1018 if (error)
1019 goto err_free_irq;
1020
1021 if (kpad->gpimapsize)
1022 adp5589_report_switch_state(kpad);
1023
1024 error = adp5589_gpio_add(kpad);
1025 if (error)
1026 goto err_free_irq;
1027
1028 device_init_wakeup(&client->dev, 1);
1029 i2c_set_clientdata(client, kpad);
1030
1031 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
1032 return 0;
1033
1034err_free_irq:
1035 free_irq(client->irq, kpad);
1036err_unreg_dev:
1037 input_unregister_device(input);
1038 input = NULL;
Michael Hennerich3f48e732011-10-18 21:12:39 -07001039err_free_input:
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001040 input_free_device(input);
Michael Hennerich3f48e732011-10-18 21:12:39 -07001041err_free_mem:
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001042 kfree(kpad);
1043
1044 return error;
1045}
1046
Bill Pembertone2619cf2012-11-23 21:50:47 -08001047static int adp5589_remove(struct i2c_client *client)
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001048{
1049 struct adp5589_kpad *kpad = i2c_get_clientdata(client);
1050
Michael Hennerich3f48e732011-10-18 21:12:39 -07001051 adp5589_write(client, kpad->var->reg(ADP5589_GENERAL_CFG), 0);
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001052 free_irq(client->irq, kpad);
1053 input_unregister_device(kpad->input);
1054 adp5589_gpio_remove(kpad);
1055 kfree(kpad);
1056
1057 return 0;
1058}
1059
1060#ifdef CONFIG_PM_SLEEP
1061static int adp5589_suspend(struct device *dev)
1062{
1063 struct adp5589_kpad *kpad = dev_get_drvdata(dev);
1064 struct i2c_client *client = kpad->client;
1065
1066 disable_irq(client->irq);
1067
1068 if (device_may_wakeup(&client->dev))
1069 enable_irq_wake(client->irq);
1070
1071 return 0;
1072}
1073
1074static int adp5589_resume(struct device *dev)
1075{
1076 struct adp5589_kpad *kpad = dev_get_drvdata(dev);
1077 struct i2c_client *client = kpad->client;
1078
1079 if (device_may_wakeup(&client->dev))
1080 disable_irq_wake(client->irq);
1081
1082 enable_irq(client->irq);
1083
1084 return 0;
1085}
1086#endif
1087
1088static SIMPLE_DEV_PM_OPS(adp5589_dev_pm_ops, adp5589_suspend, adp5589_resume);
1089
1090static const struct i2c_device_id adp5589_id[] = {
Michael Hennerich3f48e732011-10-18 21:12:39 -07001091 {"adp5589-keys", ADP5589},
1092 {"adp5585-keys", ADP5585_01},
1093 {"adp5585-02-keys", ADP5585_02}, /* Adds ROW5 to ADP5585 */
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001094 {}
1095};
1096
1097MODULE_DEVICE_TABLE(i2c, adp5589_id);
1098
1099static struct i2c_driver adp5589_driver = {
1100 .driver = {
1101 .name = KBUILD_MODNAME,
1102 .owner = THIS_MODULE,
1103 .pm = &adp5589_dev_pm_ops,
1104 },
1105 .probe = adp5589_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001106 .remove = adp5589_remove,
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001107 .id_table = adp5589_id,
1108};
1109
Axel Lin1b92c1c2012-03-16 23:05:41 -07001110module_i2c_driver(adp5589_driver);
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001111
1112MODULE_LICENSE("GPL");
1113MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
Michael Hennerich3f48e732011-10-18 21:12:39 -07001114MODULE_DESCRIPTION("ADP5589/ADP5585 Keypad driver");