~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to drivers/input/keyboard/adp5589-keys.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Description:  keypad driver for ADP5589
 
3
 *               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>
 
11
#include <linux/version.h>
 
12
#include <linux/init.h>
 
13
#include <linux/interrupt.h>
 
14
#include <linux/irq.h>
 
15
#include <linux/workqueue.h>
 
16
#include <linux/errno.h>
 
17
#include <linux/pm.h>
 
18
#include <linux/platform_device.h>
 
19
#include <linux/input.h>
 
20
#include <linux/i2c.h>
 
21
#include <linux/gpio.h>
 
22
#include <linux/slab.h>
 
23
 
 
24
#include <linux/input/adp5589.h>
 
25
 
 
26
/* GENERAL_CFG Register */
 
27
#define OSC_EN          (1 << 7)
 
28
#define CORE_CLK(x)     (((x) & 0x3) << 5)
 
29
#define LCK_TRK_LOGIC   (1 << 4)
 
30
#define LCK_TRK_GPI     (1 << 3)
 
31
#define INT_CFG         (1 << 1)
 
32
#define RST_CFG         (1 << 0)
 
33
 
 
34
/* INT_EN Register */
 
35
#define LOGIC2_IEN      (1 << 5)
 
36
#define LOGIC1_IEN      (1 << 4)
 
37
#define LOCK_IEN        (1 << 3)
 
38
#define OVRFLOW_IEN     (1 << 2)
 
39
#define GPI_IEN         (1 << 1)
 
40
#define EVENT_IEN       (1 << 0)
 
41
 
 
42
/* Interrupt Status Register */
 
43
#define LOGIC2_INT      (1 << 5)
 
44
#define LOGIC1_INT      (1 << 4)
 
45
#define LOCK_INT        (1 << 3)
 
46
#define OVRFLOW_INT     (1 << 2)
 
47
#define GPI_INT         (1 << 1)
 
48
#define EVENT_INT       (1 << 0)
 
49
 
 
50
/* STATUS Register */
 
51
 
 
52
#define LOGIC2_STAT     (1 << 7)
 
53
#define LOGIC1_STAT     (1 << 6)
 
54
#define LOCK_STAT       (1 << 5)
 
55
#define KEC             0xF
 
56
 
 
57
/* PIN_CONFIG_D Register */
 
58
#define C4_EXTEND_CFG   (1 << 6)        /* RESET2 */
 
59
#define R4_EXTEND_CFG   (1 << 5)        /* RESET1 */
 
60
 
 
61
/* LOCK_CFG */
 
62
#define LOCK_EN         (1 << 0)
 
63
 
 
64
#define PTIME_MASK      0x3
 
65
#define LTIME_MASK      0x3
 
66
 
 
67
/* Key Event Register xy */
 
68
#define KEY_EV_PRESSED          (1 << 7)
 
69
#define KEY_EV_MASK             (0x7F)
 
70
 
 
71
#define KEYP_MAX_EVENT          16
 
72
 
 
73
#define MAXGPIO                 19
 
74
#define ADP_BANK(offs)          ((offs) >> 3)
 
75
#define ADP_BIT(offs)           (1u << ((offs) & 0x7))
 
76
 
 
77
struct adp5589_kpad {
 
78
        struct i2c_client *client;
 
79
        struct input_dev *input;
 
80
        unsigned short keycode[ADP5589_KEYMAPSIZE];
 
81
        const struct adp5589_gpi_map *gpimap;
 
82
        unsigned short gpimapsize;
 
83
        unsigned extend_cfg;
 
84
#ifdef CONFIG_GPIOLIB
 
85
        unsigned char gpiomap[MAXGPIO];
 
86
        bool export_gpio;
 
87
        struct gpio_chip gc;
 
88
        struct mutex gpio_lock; /* Protect cached dir, dat_out */
 
89
        u8 dat_out[3];
 
90
        u8 dir[3];
 
91
#endif
 
92
};
 
93
 
 
94
static int adp5589_read(struct i2c_client *client, u8 reg)
 
95
{
 
96
        int ret = i2c_smbus_read_byte_data(client, reg);
 
97
 
 
98
        if (ret < 0)
 
99
                dev_err(&client->dev, "Read Error\n");
 
100
 
 
101
        return ret;
 
102
}
 
103
 
 
104
static int adp5589_write(struct i2c_client *client, u8 reg, u8 val)
 
105
{
 
106
        return i2c_smbus_write_byte_data(client, reg, val);
 
107
}
 
108
 
 
109
#ifdef CONFIG_GPIOLIB
 
110
static int adp5589_gpio_get_value(struct gpio_chip *chip, unsigned off)
 
111
{
 
112
        struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
 
113
        unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
 
114
        unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
 
115
 
 
116
        return !!(adp5589_read(kpad->client, ADP5589_GPI_STATUS_A + bank) &
 
117
                  bit);
 
118
}
 
119
 
 
120
static void adp5589_gpio_set_value(struct gpio_chip *chip,
 
121
                                   unsigned off, int val)
 
122
{
 
123
        struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
 
124
        unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
 
125
        unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
 
126
 
 
127
        mutex_lock(&kpad->gpio_lock);
 
128
 
 
129
        if (val)
 
130
                kpad->dat_out[bank] |= bit;
 
131
        else
 
132
                kpad->dat_out[bank] &= ~bit;
 
133
 
 
134
        adp5589_write(kpad->client, ADP5589_GPO_DATA_OUT_A + bank,
 
135
                      kpad->dat_out[bank]);
 
136
 
 
137
        mutex_unlock(&kpad->gpio_lock);
 
138
}
 
139
 
 
140
static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
 
141
{
 
142
        struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
 
143
        unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
 
144
        unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
 
145
        int ret;
 
146
 
 
147
        mutex_lock(&kpad->gpio_lock);
 
148
 
 
149
        kpad->dir[bank] &= ~bit;
 
150
        ret = adp5589_write(kpad->client, ADP5589_GPIO_DIRECTION_A + bank,
 
151
                            kpad->dir[bank]);
 
152
 
 
153
        mutex_unlock(&kpad->gpio_lock);
 
154
 
 
155
        return ret;
 
156
}
 
157
 
 
158
static int adp5589_gpio_direction_output(struct gpio_chip *chip,
 
159
                                         unsigned off, int val)
 
160
{
 
161
        struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
 
162
        unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
 
163
        unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
 
164
        int ret;
 
165
 
 
166
        mutex_lock(&kpad->gpio_lock);
 
167
 
 
168
        kpad->dir[bank] |= bit;
 
169
 
 
170
        if (val)
 
171
                kpad->dat_out[bank] |= bit;
 
172
        else
 
173
                kpad->dat_out[bank] &= ~bit;
 
174
 
 
175
        ret = adp5589_write(kpad->client, ADP5589_GPO_DATA_OUT_A + bank,
 
176
                            kpad->dat_out[bank]);
 
177
        ret |= adp5589_write(kpad->client, ADP5589_GPIO_DIRECTION_A + bank,
 
178
                             kpad->dir[bank]);
 
179
 
 
180
        mutex_unlock(&kpad->gpio_lock);
 
181
 
 
182
        return ret;
 
183
}
 
184
 
 
185
static int __devinit adp5589_build_gpiomap(struct adp5589_kpad *kpad,
 
186
                                const struct adp5589_kpad_platform_data *pdata)
 
187
{
 
188
        bool pin_used[MAXGPIO];
 
189
        int n_unused = 0;
 
190
        int i;
 
191
 
 
192
        memset(pin_used, false, sizeof(pin_used));
 
193
 
 
194
        for (i = 0; i < MAXGPIO; i++)
 
195
                if (pdata->keypad_en_mask & (1 << i))
 
196
                        pin_used[i] = true;
 
197
 
 
198
        for (i = 0; i < kpad->gpimapsize; i++)
 
199
                pin_used[kpad->gpimap[i].pin - ADP5589_GPI_PIN_BASE] = true;
 
200
 
 
201
        if (kpad->extend_cfg & R4_EXTEND_CFG)
 
202
                pin_used[4] = true;
 
203
 
 
204
        if (kpad->extend_cfg & C4_EXTEND_CFG)
 
205
                pin_used[12] = true;
 
206
 
 
207
        for (i = 0; i < MAXGPIO; i++)
 
208
                if (!pin_used[i])
 
209
                        kpad->gpiomap[n_unused++] = i;
 
210
 
 
211
        return n_unused;
 
212
}
 
213
 
 
214
static int __devinit adp5589_gpio_add(struct adp5589_kpad *kpad)
 
215
{
 
216
        struct device *dev = &kpad->client->dev;
 
217
        const struct adp5589_kpad_platform_data *pdata = dev->platform_data;
 
218
        const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
 
219
        int i, error;
 
220
 
 
221
        if (!gpio_data)
 
222
                return 0;
 
223
 
 
224
        kpad->gc.ngpio = adp5589_build_gpiomap(kpad, pdata);
 
225
        if (kpad->gc.ngpio == 0) {
 
226
                dev_info(dev, "No unused gpios left to export\n");
 
227
                return 0;
 
228
        }
 
229
 
 
230
        kpad->export_gpio = true;
 
231
 
 
232
        kpad->gc.direction_input = adp5589_gpio_direction_input;
 
233
        kpad->gc.direction_output = adp5589_gpio_direction_output;
 
234
        kpad->gc.get = adp5589_gpio_get_value;
 
235
        kpad->gc.set = adp5589_gpio_set_value;
 
236
        kpad->gc.can_sleep = 1;
 
237
 
 
238
        kpad->gc.base = gpio_data->gpio_start;
 
239
        kpad->gc.label = kpad->client->name;
 
240
        kpad->gc.owner = THIS_MODULE;
 
241
 
 
242
        mutex_init(&kpad->gpio_lock);
 
243
 
 
244
        error = gpiochip_add(&kpad->gc);
 
245
        if (error) {
 
246
                dev_err(dev, "gpiochip_add failed, err: %d\n", error);
 
247
                return error;
 
248
        }
 
249
 
 
250
        for (i = 0; i <= ADP_BANK(MAXGPIO); i++) {
 
251
                kpad->dat_out[i] = adp5589_read(kpad->client,
 
252
                                                ADP5589_GPO_DATA_OUT_A + i);
 
253
                kpad->dir[i] = adp5589_read(kpad->client,
 
254
                                            ADP5589_GPIO_DIRECTION_A + i);
 
255
        }
 
256
 
 
257
        if (gpio_data->setup) {
 
258
                error = gpio_data->setup(kpad->client,
 
259
                                         kpad->gc.base, kpad->gc.ngpio,
 
260
                                         gpio_data->context);
 
261
                if (error)
 
262
                        dev_warn(dev, "setup failed, %d\n", error);
 
263
        }
 
264
 
 
265
        return 0;
 
266
}
 
267
 
 
268
static void __devexit adp5589_gpio_remove(struct adp5589_kpad *kpad)
 
269
{
 
270
        struct device *dev = &kpad->client->dev;
 
271
        const struct adp5589_kpad_platform_data *pdata = dev->platform_data;
 
272
        const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
 
273
        int error;
 
274
 
 
275
        if (!kpad->export_gpio)
 
276
                return;
 
277
 
 
278
        if (gpio_data->teardown) {
 
279
                error = gpio_data->teardown(kpad->client,
 
280
                                            kpad->gc.base, kpad->gc.ngpio,
 
281
                                            gpio_data->context);
 
282
                if (error)
 
283
                        dev_warn(dev, "teardown failed %d\n", error);
 
284
        }
 
285
 
 
286
        error = gpiochip_remove(&kpad->gc);
 
287
        if (error)
 
288
                dev_warn(dev, "gpiochip_remove failed %d\n", error);
 
289
}
 
290
#else
 
291
static inline int adp5589_gpio_add(struct adp5589_kpad *kpad)
 
292
{
 
293
        return 0;
 
294
}
 
295
 
 
296
static inline void adp5589_gpio_remove(struct adp5589_kpad *kpad)
 
297
{
 
298
}
 
299
#endif
 
300
 
 
301
static void adp5589_report_switches(struct adp5589_kpad *kpad,
 
302
                                    int key, int key_val)
 
303
{
 
304
        int i;
 
305
 
 
306
        for (i = 0; i < kpad->gpimapsize; i++) {
 
307
                if (key_val == kpad->gpimap[i].pin) {
 
308
                        input_report_switch(kpad->input,
 
309
                                            kpad->gpimap[i].sw_evt,
 
310
                                            key & KEY_EV_PRESSED);
 
311
                        break;
 
312
                }
 
313
        }
 
314
}
 
315
 
 
316
static void adp5589_report_events(struct adp5589_kpad *kpad, int ev_cnt)
 
317
{
 
318
        int i;
 
319
 
 
320
        for (i = 0; i < ev_cnt; i++) {
 
321
                int key = adp5589_read(kpad->client, ADP5589_FIFO_1 + i);
 
322
                int key_val = key & KEY_EV_MASK;
 
323
 
 
324
                if (key_val >= ADP5589_GPI_PIN_BASE &&
 
325
                    key_val <= ADP5589_GPI_PIN_END) {
 
326
                        adp5589_report_switches(kpad, key, key_val);
 
327
                } else {
 
328
                        input_report_key(kpad->input,
 
329
                                         kpad->keycode[key_val - 1],
 
330
                                         key & KEY_EV_PRESSED);
 
331
                }
 
332
        }
 
333
}
 
334
 
 
335
static irqreturn_t adp5589_irq(int irq, void *handle)
 
336
{
 
337
        struct adp5589_kpad *kpad = handle;
 
338
        struct i2c_client *client = kpad->client;
 
339
        int status, ev_cnt;
 
340
 
 
341
        status = adp5589_read(client, ADP5589_INT_STATUS);
 
342
 
 
343
        if (status & OVRFLOW_INT)       /* Unlikely and should never happen */
 
344
                dev_err(&client->dev, "Event Overflow Error\n");
 
345
 
 
346
        if (status & EVENT_INT) {
 
347
                ev_cnt = adp5589_read(client, ADP5589_STATUS) & KEC;
 
348
                if (ev_cnt) {
 
349
                        adp5589_report_events(kpad, ev_cnt);
 
350
                        input_sync(kpad->input);
 
351
                }
 
352
        }
 
353
 
 
354
        adp5589_write(client, ADP5589_INT_STATUS, status);      /* Status is W1C */
 
355
 
 
356
        return IRQ_HANDLED;
 
357
}
 
358
 
 
359
static int __devinit adp5589_get_evcode(struct adp5589_kpad *kpad, unsigned short key)
 
360
{
 
361
        int i;
 
362
 
 
363
        for (i = 0; i < ADP5589_KEYMAPSIZE; i++)
 
364
                if (key == kpad->keycode[i])
 
365
                        return (i + 1) | KEY_EV_PRESSED;
 
366
 
 
367
        dev_err(&kpad->client->dev, "RESET/UNLOCK key not in keycode map\n");
 
368
 
 
369
        return -EINVAL;
 
370
}
 
371
 
 
372
static int __devinit adp5589_setup(struct adp5589_kpad *kpad)
 
373
{
 
374
        struct i2c_client *client = kpad->client;
 
375
        const struct adp5589_kpad_platform_data *pdata =
 
376
            client->dev.platform_data;
 
377
        int i, ret;
 
378
        unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
 
379
        unsigned char pull_mask = 0;
 
380
 
 
381
        ret = adp5589_write(client, ADP5589_PIN_CONFIG_A,
 
382
                            pdata->keypad_en_mask & 0xFF);
 
383
        ret |= adp5589_write(client, ADP5589_PIN_CONFIG_B,
 
384
                             (pdata->keypad_en_mask >> 8) & 0xFF);
 
385
        ret |= adp5589_write(client, ADP5589_PIN_CONFIG_C,
 
386
                             (pdata->keypad_en_mask >> 16) & 0xFF);
 
387
 
 
388
        if (pdata->en_keylock) {
 
389
                ret |= adp5589_write(client, ADP5589_UNLOCK1,
 
390
                                     pdata->unlock_key1);
 
391
                ret |= adp5589_write(client, ADP5589_UNLOCK2,
 
392
                                     pdata->unlock_key2);
 
393
                ret |= adp5589_write(client, ADP5589_UNLOCK_TIMERS,
 
394
                                     pdata->unlock_timer & LTIME_MASK);
 
395
                ret |= adp5589_write(client, ADP5589_LOCK_CFG, LOCK_EN);
 
396
        }
 
397
 
 
398
        for (i = 0; i < KEYP_MAX_EVENT; i++)
 
399
                ret |= adp5589_read(client, ADP5589_FIFO_1 + i);
 
400
 
 
401
        for (i = 0; i < pdata->gpimapsize; i++) {
 
402
                unsigned short pin = pdata->gpimap[i].pin;
 
403
 
 
404
                if (pin <= ADP5589_GPI_PIN_ROW_END) {
 
405
                        evt_mode1 |= (1 << (pin - ADP5589_GPI_PIN_ROW_BASE));
 
406
                } else {
 
407
                        evt_mode2 |=
 
408
                            ((1 << (pin - ADP5589_GPI_PIN_COL_BASE)) & 0xFF);
 
409
                        evt_mode3 |=
 
410
                            ((1 << (pin - ADP5589_GPI_PIN_COL_BASE)) >> 8);
 
411
                }
 
412
        }
 
413
 
 
414
        if (pdata->gpimapsize) {
 
415
                ret |= adp5589_write(client, ADP5589_GPI_EVENT_EN_A, evt_mode1);
 
416
                ret |= adp5589_write(client, ADP5589_GPI_EVENT_EN_B, evt_mode2);
 
417
                ret |= adp5589_write(client, ADP5589_GPI_EVENT_EN_C, evt_mode3);
 
418
        }
 
419
 
 
420
        if (pdata->pull_dis_mask & pdata->pullup_en_100k &
 
421
            pdata->pullup_en_300k & pdata->pulldown_en_300k)
 
422
                dev_warn(&client->dev, "Conflicting pull resistor config\n");
 
423
 
 
424
        for (i = 0; i < MAXGPIO; i++) {
 
425
                unsigned val = 0;
 
426
 
 
427
                if (pdata->pullup_en_300k & (1 << i))
 
428
                        val = 0;
 
429
                else if (pdata->pulldown_en_300k & (1 << i))
 
430
                        val = 1;
 
431
                else if (pdata->pullup_en_100k & (1 << i))
 
432
                        val = 2;
 
433
                else if (pdata->pull_dis_mask & (1 << i))
 
434
                        val = 3;
 
435
 
 
436
                pull_mask |= val << (2 * (i & 0x3));
 
437
 
 
438
                if ((i & 0x3) == 0x3 || i == MAXGPIO - 1) {
 
439
                        ret |= adp5589_write(client,
 
440
                                             ADP5589_RPULL_CONFIG_A + (i >> 2),
 
441
                                             pull_mask);
 
442
                        pull_mask = 0;
 
443
                }
 
444
        }
 
445
 
 
446
        if (pdata->reset1_key_1 && pdata->reset1_key_2 && pdata->reset1_key_3) {
 
447
                ret |= adp5589_write(client, ADP5589_RESET1_EVENT_A,
 
448
                                     adp5589_get_evcode(kpad,
 
449
                                                        pdata->reset1_key_1));
 
450
                ret |= adp5589_write(client, ADP5589_RESET1_EVENT_B,
 
451
                                     adp5589_get_evcode(kpad,
 
452
                                                        pdata->reset1_key_2));
 
453
                ret |= adp5589_write(client, ADP5589_RESET1_EVENT_C,
 
454
                                     adp5589_get_evcode(kpad,
 
455
                                                        pdata->reset1_key_3));
 
456
                kpad->extend_cfg |= R4_EXTEND_CFG;
 
457
        }
 
458
 
 
459
        if (pdata->reset2_key_1 && pdata->reset2_key_2) {
 
460
                ret |= adp5589_write(client, ADP5589_RESET2_EVENT_A,
 
461
                                     adp5589_get_evcode(kpad,
 
462
                                                        pdata->reset2_key_1));
 
463
                ret |= adp5589_write(client, ADP5589_RESET2_EVENT_B,
 
464
                                     adp5589_get_evcode(kpad,
 
465
                                                        pdata->reset2_key_2));
 
466
                kpad->extend_cfg |= C4_EXTEND_CFG;
 
467
        }
 
468
 
 
469
        if (kpad->extend_cfg) {
 
470
                ret |= adp5589_write(client, ADP5589_RESET_CFG,
 
471
                                     pdata->reset_cfg);
 
472
                ret |= adp5589_write(client, ADP5589_PIN_CONFIG_D,
 
473
                                     kpad->extend_cfg);
 
474
        }
 
475
 
 
476
        for (i = 0; i <= ADP_BANK(MAXGPIO); i++)
 
477
                ret |= adp5589_write(client, ADP5589_DEBOUNCE_DIS_A + i,
 
478
                                     pdata->debounce_dis_mask >> (i * 8));
 
479
 
 
480
        ret |= adp5589_write(client, ADP5589_POLL_PTIME_CFG,
 
481
                             pdata->scan_cycle_time & PTIME_MASK);
 
482
        ret |= adp5589_write(client, ADP5589_INT_STATUS, LOGIC2_INT |
 
483
                             LOGIC1_INT | OVRFLOW_INT | LOCK_INT |
 
484
                             GPI_INT | EVENT_INT);      /* Status is W1C */
 
485
 
 
486
        ret |= adp5589_write(client, ADP5589_GENERAL_CFG,
 
487
                             INT_CFG | OSC_EN | CORE_CLK(3));
 
488
        ret |= adp5589_write(client, ADP5589_INT_EN,
 
489
                             OVRFLOW_IEN | GPI_IEN | EVENT_IEN);
 
490
 
 
491
        if (ret < 0) {
 
492
                dev_err(&client->dev, "Write Error\n");
 
493
                return ret;
 
494
        }
 
495
 
 
496
        return 0;
 
497
}
 
498
 
 
499
static void __devinit adp5589_report_switch_state(struct adp5589_kpad *kpad)
 
500
{
 
501
        int gpi_stat1 = adp5589_read(kpad->client, ADP5589_GPI_STATUS_A);
 
502
        int gpi_stat2 = adp5589_read(kpad->client, ADP5589_GPI_STATUS_B);
 
503
        int gpi_stat3 = adp5589_read(kpad->client, ADP5589_GPI_STATUS_C);
 
504
        int gpi_stat_tmp, pin_loc;
 
505
        int i;
 
506
 
 
507
        for (i = 0; i < kpad->gpimapsize; i++) {
 
508
                unsigned short pin = kpad->gpimap[i].pin;
 
509
 
 
510
                if (pin <= ADP5589_GPI_PIN_ROW_END) {
 
511
                        gpi_stat_tmp = gpi_stat1;
 
512
                        pin_loc = pin - ADP5589_GPI_PIN_ROW_BASE;
 
513
                } else if ((pin - ADP5589_GPI_PIN_COL_BASE) < 8) {
 
514
                        gpi_stat_tmp = gpi_stat2;
 
515
                        pin_loc = pin - ADP5589_GPI_PIN_COL_BASE;
 
516
                } else {
 
517
                        gpi_stat_tmp = gpi_stat3;
 
518
                        pin_loc = pin - ADP5589_GPI_PIN_COL_BASE - 8;
 
519
                }
 
520
 
 
521
                if (gpi_stat_tmp < 0) {
 
522
                        dev_err(&kpad->client->dev,
 
523
                                "Can't read GPIO_DAT_STAT switch"
 
524
                                " %d default to OFF\n", pin);
 
525
                        gpi_stat_tmp = 0;
 
526
                }
 
527
 
 
528
                input_report_switch(kpad->input,
 
529
                                    kpad->gpimap[i].sw_evt,
 
530
                                    !(gpi_stat_tmp & (1 << pin_loc)));
 
531
        }
 
532
 
 
533
        input_sync(kpad->input);
 
534
}
 
535
 
 
536
static int __devinit adp5589_probe(struct i2c_client *client,
 
537
                                   const struct i2c_device_id *id)
 
538
{
 
539
        struct adp5589_kpad *kpad;
 
540
        const struct adp5589_kpad_platform_data *pdata;
 
541
        struct input_dev *input;
 
542
        unsigned int revid;
 
543
        int ret, i;
 
544
        int error;
 
545
 
 
546
        if (!i2c_check_functionality(client->adapter,
 
547
                                     I2C_FUNC_SMBUS_BYTE_DATA)) {
 
548
                dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
 
549
                return -EIO;
 
550
        }
 
551
 
 
552
        pdata = client->dev.platform_data;
 
553
        if (!pdata) {
 
554
                dev_err(&client->dev, "no platform data?\n");
 
555
                return -EINVAL;
 
556
        }
 
557
 
 
558
        if (!((pdata->keypad_en_mask & 0xFF) &&
 
559
                        (pdata->keypad_en_mask >> 8)) || !pdata->keymap) {
 
560
                dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
 
561
                return -EINVAL;
 
562
        }
 
563
 
 
564
        if (pdata->keymapsize != ADP5589_KEYMAPSIZE) {
 
565
                dev_err(&client->dev, "invalid keymapsize\n");
 
566
                return -EINVAL;
 
567
        }
 
568
 
 
569
        if (!pdata->gpimap && pdata->gpimapsize) {
 
570
                dev_err(&client->dev, "invalid gpimap from pdata\n");
 
571
                return -EINVAL;
 
572
        }
 
573
 
 
574
        if (pdata->gpimapsize > ADP5589_GPIMAPSIZE_MAX) {
 
575
                dev_err(&client->dev, "invalid gpimapsize\n");
 
576
                return -EINVAL;
 
577
        }
 
578
 
 
579
        for (i = 0; i < pdata->gpimapsize; i++) {
 
580
                unsigned short pin = pdata->gpimap[i].pin;
 
581
 
 
582
                if (pin < ADP5589_GPI_PIN_BASE || pin > ADP5589_GPI_PIN_END) {
 
583
                        dev_err(&client->dev, "invalid gpi pin data\n");
 
584
                        return -EINVAL;
 
585
                }
 
586
 
 
587
                if ((1 << (pin - ADP5589_GPI_PIN_ROW_BASE)) &
 
588
                                pdata->keypad_en_mask) {
 
589
                        dev_err(&client->dev, "invalid gpi row/col data\n");
 
590
                        return -EINVAL;
 
591
                }
 
592
        }
 
593
 
 
594
        if (!client->irq) {
 
595
                dev_err(&client->dev, "no IRQ?\n");
 
596
                return -EINVAL;
 
597
        }
 
598
 
 
599
        kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
 
600
        input = input_allocate_device();
 
601
        if (!kpad || !input) {
 
602
                error = -ENOMEM;
 
603
                goto err_free_mem;
 
604
        }
 
605
 
 
606
        kpad->client = client;
 
607
        kpad->input = input;
 
608
 
 
609
        ret = adp5589_read(client, ADP5589_ID);
 
610
        if (ret < 0) {
 
611
                error = ret;
 
612
                goto err_free_mem;
 
613
        }
 
614
 
 
615
        revid = (u8) ret & ADP5589_DEVICE_ID_MASK;
 
616
 
 
617
        input->name = client->name;
 
618
        input->phys = "adp5589-keys/input0";
 
619
        input->dev.parent = &client->dev;
 
620
 
 
621
        input_set_drvdata(input, kpad);
 
622
 
 
623
        input->id.bustype = BUS_I2C;
 
624
        input->id.vendor = 0x0001;
 
625
        input->id.product = 0x0001;
 
626
        input->id.version = revid;
 
627
 
 
628
        input->keycodesize = sizeof(kpad->keycode[0]);
 
629
        input->keycodemax = pdata->keymapsize;
 
630
        input->keycode = kpad->keycode;
 
631
 
 
632
        memcpy(kpad->keycode, pdata->keymap,
 
633
               pdata->keymapsize * input->keycodesize);
 
634
 
 
635
        kpad->gpimap = pdata->gpimap;
 
636
        kpad->gpimapsize = pdata->gpimapsize;
 
637
 
 
638
        /* setup input device */
 
639
        __set_bit(EV_KEY, input->evbit);
 
640
 
 
641
        if (pdata->repeat)
 
642
                __set_bit(EV_REP, input->evbit);
 
643
 
 
644
        for (i = 0; i < input->keycodemax; i++)
 
645
                __set_bit(kpad->keycode[i] & KEY_MAX, input->keybit);
 
646
        __clear_bit(KEY_RESERVED, input->keybit);
 
647
 
 
648
        if (kpad->gpimapsize)
 
649
                __set_bit(EV_SW, input->evbit);
 
650
        for (i = 0; i < kpad->gpimapsize; i++)
 
651
                __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
 
652
 
 
653
        error = input_register_device(input);
 
654
        if (error) {
 
655
                dev_err(&client->dev, "unable to register input device\n");
 
656
                goto err_free_mem;
 
657
        }
 
658
 
 
659
        error = request_threaded_irq(client->irq, NULL, adp5589_irq,
 
660
                                     IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
 
661
                                     client->dev.driver->name, kpad);
 
662
        if (error) {
 
663
                dev_err(&client->dev, "irq %d busy?\n", client->irq);
 
664
                goto err_unreg_dev;
 
665
        }
 
666
 
 
667
        error = adp5589_setup(kpad);
 
668
        if (error)
 
669
                goto err_free_irq;
 
670
 
 
671
        if (kpad->gpimapsize)
 
672
                adp5589_report_switch_state(kpad);
 
673
 
 
674
        error = adp5589_gpio_add(kpad);
 
675
        if (error)
 
676
                goto err_free_irq;
 
677
 
 
678
        device_init_wakeup(&client->dev, 1);
 
679
        i2c_set_clientdata(client, kpad);
 
680
 
 
681
        dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
 
682
        return 0;
 
683
 
 
684
err_free_irq:
 
685
        free_irq(client->irq, kpad);
 
686
err_unreg_dev:
 
687
        input_unregister_device(input);
 
688
        input = NULL;
 
689
err_free_mem:
 
690
        input_free_device(input);
 
691
        kfree(kpad);
 
692
 
 
693
        return error;
 
694
}
 
695
 
 
696
static int __devexit adp5589_remove(struct i2c_client *client)
 
697
{
 
698
        struct adp5589_kpad *kpad = i2c_get_clientdata(client);
 
699
 
 
700
        adp5589_write(client, ADP5589_GENERAL_CFG, 0);
 
701
        free_irq(client->irq, kpad);
 
702
        input_unregister_device(kpad->input);
 
703
        adp5589_gpio_remove(kpad);
 
704
        kfree(kpad);
 
705
 
 
706
        return 0;
 
707
}
 
708
 
 
709
#ifdef CONFIG_PM_SLEEP
 
710
static int adp5589_suspend(struct device *dev)
 
711
{
 
712
        struct adp5589_kpad *kpad = dev_get_drvdata(dev);
 
713
        struct i2c_client *client = kpad->client;
 
714
 
 
715
        disable_irq(client->irq);
 
716
 
 
717
        if (device_may_wakeup(&client->dev))
 
718
                enable_irq_wake(client->irq);
 
719
 
 
720
        return 0;
 
721
}
 
722
 
 
723
static int adp5589_resume(struct device *dev)
 
724
{
 
725
        struct adp5589_kpad *kpad = dev_get_drvdata(dev);
 
726
        struct i2c_client *client = kpad->client;
 
727
 
 
728
        if (device_may_wakeup(&client->dev))
 
729
                disable_irq_wake(client->irq);
 
730
 
 
731
        enable_irq(client->irq);
 
732
 
 
733
        return 0;
 
734
}
 
735
#endif
 
736
 
 
737
static SIMPLE_DEV_PM_OPS(adp5589_dev_pm_ops, adp5589_suspend, adp5589_resume);
 
738
 
 
739
static const struct i2c_device_id adp5589_id[] = {
 
740
        {"adp5589-keys", 0},
 
741
        {}
 
742
};
 
743
 
 
744
MODULE_DEVICE_TABLE(i2c, adp5589_id);
 
745
 
 
746
static struct i2c_driver adp5589_driver = {
 
747
        .driver = {
 
748
                .name = KBUILD_MODNAME,
 
749
                .owner = THIS_MODULE,
 
750
                .pm = &adp5589_dev_pm_ops,
 
751
        },
 
752
        .probe = adp5589_probe,
 
753
        .remove = __devexit_p(adp5589_remove),
 
754
        .id_table = adp5589_id,
 
755
};
 
756
 
 
757
static int __init adp5589_init(void)
 
758
{
 
759
        return i2c_add_driver(&adp5589_driver);
 
760
}
 
761
module_init(adp5589_init);
 
762
 
 
763
static void __exit adp5589_exit(void)
 
764
{
 
765
        i2c_del_driver(&adp5589_driver);
 
766
}
 
767
module_exit(adp5589_exit);
 
768
 
 
769
MODULE_LICENSE("GPL");
 
770
MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
 
771
MODULE_DESCRIPTION("ADP5589 Keypad driver");