aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/keyboard/adp5588-keys.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/keyboard/adp5588-keys.c')
-rw-r--r--drivers/input/keyboard/adp5588-keys.c351
1 files changed, 342 insertions, 9 deletions
diff --git a/drivers/input/keyboard/adp5588-keys.c b/drivers/input/keyboard/adp5588-keys.c
index 744600eff222..d6918cb966c0 100644
--- a/drivers/input/keyboard/adp5588-keys.c
+++ b/drivers/input/keyboard/adp5588-keys.c
@@ -19,6 +19,7 @@
19#include <linux/platform_device.h> 19#include <linux/platform_device.h>
20#include <linux/input.h> 20#include <linux/input.h>
21#include <linux/i2c.h> 21#include <linux/i2c.h>
22#include <linux/gpio.h>
22#include <linux/slab.h> 23#include <linux/slab.h>
23 24
24#include <linux/i2c/adp5588.h> 25#include <linux/i2c/adp5588.h>
@@ -54,6 +55,10 @@
54 55
55#define KEYP_MAX_EVENT 10 56#define KEYP_MAX_EVENT 10
56 57
58#define MAXGPIO 18
59#define ADP_BANK(offs) ((offs) >> 3)
60#define ADP_BIT(offs) (1u << ((offs) & 0x7))
61
57/* 62/*
58 * Early pre 4.0 Silicon required to delay readout by at least 25ms, 63 * Early pre 4.0 Silicon required to delay readout by at least 25ms,
59 * since the Event Counter Register updated 25ms after the interrupt 64 * since the Event Counter Register updated 25ms after the interrupt
@@ -67,6 +72,16 @@ struct adp5588_kpad {
67 struct delayed_work work; 72 struct delayed_work work;
68 unsigned long delay; 73 unsigned long delay;
69 unsigned short keycode[ADP5588_KEYMAPSIZE]; 74 unsigned short keycode[ADP5588_KEYMAPSIZE];
75 const struct adp5588_gpi_map *gpimap;
76 unsigned short gpimapsize;
77#ifdef CONFIG_GPIOLIB
78 unsigned char gpiomap[MAXGPIO];
79 bool export_gpio;
80 struct gpio_chip gc;
81 struct mutex gpio_lock; /* Protect cached dir, dat_out */
82 u8 dat_out[3];
83 u8 dir[3];
84#endif
70}; 85};
71 86
72static int adp5588_read(struct i2c_client *client, u8 reg) 87static int adp5588_read(struct i2c_client *client, u8 reg)
@@ -84,12 +99,222 @@ static int adp5588_write(struct i2c_client *client, u8 reg, u8 val)
84 return i2c_smbus_write_byte_data(client, reg, val); 99 return i2c_smbus_write_byte_data(client, reg, val);
85} 100}
86 101
102#ifdef CONFIG_GPIOLIB
103static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
104{
105 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
106 unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
107 unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
108
109 return !!(adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank) & bit);
110}
111
112static void adp5588_gpio_set_value(struct gpio_chip *chip,
113 unsigned off, int val)
114{
115 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
116 unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
117 unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
118
119 mutex_lock(&kpad->gpio_lock);
120
121 if (val)
122 kpad->dat_out[bank] |= bit;
123 else
124 kpad->dat_out[bank] &= ~bit;
125
126 adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
127 kpad->dat_out[bank]);
128
129 mutex_unlock(&kpad->gpio_lock);
130}
131
132static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
133{
134 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
135 unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
136 unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
137 int ret;
138
139 mutex_lock(&kpad->gpio_lock);
140
141 kpad->dir[bank] &= ~bit;
142 ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
143
144 mutex_unlock(&kpad->gpio_lock);
145
146 return ret;
147}
148
149static int adp5588_gpio_direction_output(struct gpio_chip *chip,
150 unsigned off, int val)
151{
152 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
153 unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
154 unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
155 int ret;
156
157 mutex_lock(&kpad->gpio_lock);
158
159 kpad->dir[bank] |= bit;
160
161 if (val)
162 kpad->dat_out[bank] |= bit;
163 else
164 kpad->dat_out[bank] &= ~bit;
165
166 ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
167 kpad->dat_out[bank]);
168 ret |= adp5588_write(kpad->client, GPIO_DIR1 + bank,
169 kpad->dir[bank]);
170
171 mutex_unlock(&kpad->gpio_lock);
172
173 return ret;
174}
175
176static int __devinit adp5588_build_gpiomap(struct adp5588_kpad *kpad,
177 const struct adp5588_kpad_platform_data *pdata)
178{
179 bool pin_used[MAXGPIO];
180 int n_unused = 0;
181 int i;
182
183 memset(pin_used, 0, sizeof(pin_used));
184
185 for (i = 0; i < pdata->rows; i++)
186 pin_used[i] = true;
187
188 for (i = 0; i < pdata->cols; i++)
189 pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true;
190
191 for (i = 0; i < kpad->gpimapsize; i++)
192 pin_used[kpad->gpimap[i].pin - GPI_PIN_BASE] = true;
193
194 for (i = 0; i < MAXGPIO; i++)
195 if (!pin_used[i])
196 kpad->gpiomap[n_unused++] = i;
197
198 return n_unused;
199}
200
201static int __devinit adp5588_gpio_add(struct adp5588_kpad *kpad)
202{
203 struct device *dev = &kpad->client->dev;
204 const struct adp5588_kpad_platform_data *pdata = dev->platform_data;
205 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
206 int i, error;
207
208 if (!gpio_data)
209 return 0;
210
211 kpad->gc.ngpio = adp5588_build_gpiomap(kpad, pdata);
212 if (kpad->gc.ngpio == 0) {
213 dev_info(dev, "No unused gpios left to export\n");
214 return 0;
215 }
216
217 kpad->export_gpio = true;
218
219 kpad->gc.direction_input = adp5588_gpio_direction_input;
220 kpad->gc.direction_output = adp5588_gpio_direction_output;
221 kpad->gc.get = adp5588_gpio_get_value;
222 kpad->gc.set = adp5588_gpio_set_value;
223 kpad->gc.can_sleep = 1;
224
225 kpad->gc.base = gpio_data->gpio_start;
226 kpad->gc.label = kpad->client->name;
227 kpad->gc.owner = THIS_MODULE;
228
229 mutex_init(&kpad->gpio_lock);
230
231 error = gpiochip_add(&kpad->gc);
232 if (error) {
233 dev_err(dev, "gpiochip_add failed, err: %d\n", error);
234 return error;
235 }
236
237 for (i = 0; i <= ADP_BANK(MAXGPIO); i++) {
238 kpad->dat_out[i] = adp5588_read(kpad->client,
239 GPIO_DAT_OUT1 + i);
240 kpad->dir[i] = adp5588_read(kpad->client, GPIO_DIR1 + i);
241 }
242
243 if (gpio_data->setup) {
244 error = gpio_data->setup(kpad->client,
245 kpad->gc.base, kpad->gc.ngpio,
246 gpio_data->context);
247 if (error)
248 dev_warn(dev, "setup failed, %d\n", error);
249 }
250
251 return 0;
252}
253
254static void __devexit adp5588_gpio_remove(struct adp5588_kpad *kpad)
255{
256 struct device *dev = &kpad->client->dev;
257 const struct adp5588_kpad_platform_data *pdata = dev->platform_data;
258 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
259 int error;
260
261 if (!kpad->export_gpio)
262 return;
263
264 if (gpio_data->teardown) {
265 error = gpio_data->teardown(kpad->client,
266 kpad->gc.base, kpad->gc.ngpio,
267 gpio_data->context);
268 if (error)
269 dev_warn(dev, "teardown failed %d\n", error);
270 }
271
272 error = gpiochip_remove(&kpad->gc);
273 if (error)
274 dev_warn(dev, "gpiochip_remove failed %d\n", error);
275}
276#else
277static inline int adp5588_gpio_add(struct adp5588_kpad *kpad)
278{
279 return 0;
280}
281
282static inline void adp5588_gpio_remove(struct adp5588_kpad *kpad)
283{
284}
285#endif
286
287static void adp5588_report_events(struct adp5588_kpad *kpad, int ev_cnt)
288{
289 int i, j;
290
291 for (i = 0; i < ev_cnt; i++) {
292 int key = adp5588_read(kpad->client, Key_EVENTA + i);
293 int key_val = key & KEY_EV_MASK;
294
295 if (key_val >= GPI_PIN_BASE && key_val <= GPI_PIN_END) {
296 for (j = 0; j < kpad->gpimapsize; j++) {
297 if (key_val == kpad->gpimap[j].pin) {
298 input_report_switch(kpad->input,
299 kpad->gpimap[j].sw_evt,
300 key & KEY_EV_PRESSED);
301 break;
302 }
303 }
304 } else {
305 input_report_key(kpad->input,
306 kpad->keycode[key_val - 1],
307 key & KEY_EV_PRESSED);
308 }
309 }
310}
311
87static void adp5588_work(struct work_struct *work) 312static void adp5588_work(struct work_struct *work)
88{ 313{
89 struct adp5588_kpad *kpad = container_of(work, 314 struct adp5588_kpad *kpad = container_of(work,
90 struct adp5588_kpad, work.work); 315 struct adp5588_kpad, work.work);
91 struct i2c_client *client = kpad->client; 316 struct i2c_client *client = kpad->client;
92 int i, key, status, ev_cnt; 317 int status, ev_cnt;
93 318
94 status = adp5588_read(client, INT_STAT); 319 status = adp5588_read(client, INT_STAT);
95 320
@@ -99,12 +324,7 @@ static void adp5588_work(struct work_struct *work)
99 if (status & KE_INT) { 324 if (status & KE_INT) {
100 ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & KEC; 325 ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & KEC;
101 if (ev_cnt) { 326 if (ev_cnt) {
102 for (i = 0; i < ev_cnt; i++) { 327 adp5588_report_events(kpad, ev_cnt);
103 key = adp5588_read(client, Key_EVENTA + i);
104 input_report_key(kpad->input,
105 kpad->keycode[(key & KEY_EV_MASK) - 1],
106 key & KEY_EV_PRESSED);
107 }
108 input_sync(kpad->input); 328 input_sync(kpad->input);
109 } 329 }
110 } 330 }
@@ -128,8 +348,10 @@ static irqreturn_t adp5588_irq(int irq, void *handle)
128 348
129static int __devinit adp5588_setup(struct i2c_client *client) 349static int __devinit adp5588_setup(struct i2c_client *client)
130{ 350{
131 struct adp5588_kpad_platform_data *pdata = client->dev.platform_data; 351 const struct adp5588_kpad_platform_data *pdata = client->dev.platform_data;
352 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
132 int i, ret; 353 int i, ret;
354 unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
133 355
134 ret = adp5588_write(client, KP_GPIO1, KP_SEL(pdata->rows)); 356 ret = adp5588_write(client, KP_GPIO1, KP_SEL(pdata->rows));
135 ret |= adp5588_write(client, KP_GPIO2, KP_SEL(pdata->cols) & 0xFF); 357 ret |= adp5588_write(client, KP_GPIO2, KP_SEL(pdata->cols) & 0xFF);
@@ -144,6 +366,32 @@ static int __devinit adp5588_setup(struct i2c_client *client)
144 for (i = 0; i < KEYP_MAX_EVENT; i++) 366 for (i = 0; i < KEYP_MAX_EVENT; i++)
145 ret |= adp5588_read(client, Key_EVENTA); 367 ret |= adp5588_read(client, Key_EVENTA);
146 368
369 for (i = 0; i < pdata->gpimapsize; i++) {
370 unsigned short pin = pdata->gpimap[i].pin;
371
372 if (pin <= GPI_PIN_ROW_END) {
373 evt_mode1 |= (1 << (pin - GPI_PIN_ROW_BASE));
374 } else {
375 evt_mode2 |= ((1 << (pin - GPI_PIN_COL_BASE)) & 0xFF);
376 evt_mode3 |= ((1 << (pin - GPI_PIN_COL_BASE)) >> 8);
377 }
378 }
379
380 if (pdata->gpimapsize) {
381 ret |= adp5588_write(client, GPI_EM1, evt_mode1);
382 ret |= adp5588_write(client, GPI_EM2, evt_mode2);
383 ret |= adp5588_write(client, GPI_EM3, evt_mode3);
384 }
385
386 if (gpio_data) {
387 for (i = 0; i <= ADP_BANK(MAXGPIO); i++) {
388 int pull_mask = gpio_data->pullup_dis_mask;
389
390 ret |= adp5588_write(client, GPIO_PULL1 + i,
391 (pull_mask >> (8 * i)) & 0xFF);
392 }
393 }
394
147 ret |= adp5588_write(client, INT_STAT, CMP2_INT | CMP1_INT | 395 ret |= adp5588_write(client, INT_STAT, CMP2_INT | CMP1_INT |
148 OVR_FLOW_INT | K_LCK_INT | 396 OVR_FLOW_INT | K_LCK_INT |
149 GPI_INT | KE_INT); /* Status is W1C */ 397 GPI_INT | KE_INT); /* Status is W1C */
@@ -158,11 +406,49 @@ static int __devinit adp5588_setup(struct i2c_client *client)
158 return 0; 406 return 0;
159} 407}
160 408
409static void __devinit adp5588_report_switch_state(struct adp5588_kpad *kpad)
410{
411 int gpi_stat1 = adp5588_read(kpad->client, GPIO_DAT_STAT1);
412 int gpi_stat2 = adp5588_read(kpad->client, GPIO_DAT_STAT2);
413 int gpi_stat3 = adp5588_read(kpad->client, GPIO_DAT_STAT3);
414 int gpi_stat_tmp, pin_loc;
415 int i;
416
417 for (i = 0; i < kpad->gpimapsize; i++) {
418 unsigned short pin = kpad->gpimap[i].pin;
419
420 if (pin <= GPI_PIN_ROW_END) {
421 gpi_stat_tmp = gpi_stat1;
422 pin_loc = pin - GPI_PIN_ROW_BASE;
423 } else if ((pin - GPI_PIN_COL_BASE) < 8) {
424 gpi_stat_tmp = gpi_stat2;
425 pin_loc = pin - GPI_PIN_COL_BASE;
426 } else {
427 gpi_stat_tmp = gpi_stat3;
428 pin_loc = pin - GPI_PIN_COL_BASE - 8;
429 }
430
431 if (gpi_stat_tmp < 0) {
432 dev_err(&kpad->client->dev,
433 "Can't read GPIO_DAT_STAT switch %d default to OFF\n",
434 pin);
435 gpi_stat_tmp = 0;
436 }
437
438 input_report_switch(kpad->input,
439 kpad->gpimap[i].sw_evt,
440 !(gpi_stat_tmp & (1 << pin_loc)));
441 }
442
443 input_sync(kpad->input);
444}
445
446
161static int __devinit adp5588_probe(struct i2c_client *client, 447static int __devinit adp5588_probe(struct i2c_client *client,
162 const struct i2c_device_id *id) 448 const struct i2c_device_id *id)
163{ 449{
164 struct adp5588_kpad *kpad; 450 struct adp5588_kpad *kpad;
165 struct adp5588_kpad_platform_data *pdata = client->dev.platform_data; 451 const struct adp5588_kpad_platform_data *pdata = client->dev.platform_data;
166 struct input_dev *input; 452 struct input_dev *input;
167 unsigned int revid; 453 unsigned int revid;
168 int ret, i; 454 int ret, i;
@@ -189,6 +475,37 @@ static int __devinit adp5588_probe(struct i2c_client *client,
189 return -EINVAL; 475 return -EINVAL;
190 } 476 }
191 477
478 if (!pdata->gpimap && pdata->gpimapsize) {
479 dev_err(&client->dev, "invalid gpimap from pdata\n");
480 return -EINVAL;
481 }
482
483 if (pdata->gpimapsize > ADP5588_GPIMAPSIZE_MAX) {
484 dev_err(&client->dev, "invalid gpimapsize\n");
485 return -EINVAL;
486 }
487
488 for (i = 0; i < pdata->gpimapsize; i++) {
489 unsigned short pin = pdata->gpimap[i].pin;
490
491 if (pin < GPI_PIN_BASE || pin > GPI_PIN_END) {
492 dev_err(&client->dev, "invalid gpi pin data\n");
493 return -EINVAL;
494 }
495
496 if (pin <= GPI_PIN_ROW_END) {
497 if (pin - GPI_PIN_ROW_BASE + 1 <= pdata->rows) {
498 dev_err(&client->dev, "invalid gpi row data\n");
499 return -EINVAL;
500 }
501 } else {
502 if (pin - GPI_PIN_COL_BASE + 1 <= pdata->cols) {
503 dev_err(&client->dev, "invalid gpi col data\n");
504 return -EINVAL;
505 }
506 }
507 }
508
192 if (!client->irq) { 509 if (!client->irq) {
193 dev_err(&client->dev, "no IRQ?\n"); 510 dev_err(&client->dev, "no IRQ?\n");
194 return -EINVAL; 511 return -EINVAL;
@@ -233,6 +550,9 @@ static int __devinit adp5588_probe(struct i2c_client *client,
233 memcpy(kpad->keycode, pdata->keymap, 550 memcpy(kpad->keycode, pdata->keymap,
234 pdata->keymapsize * input->keycodesize); 551 pdata->keymapsize * input->keycodesize);
235 552
553 kpad->gpimap = pdata->gpimap;
554 kpad->gpimapsize = pdata->gpimapsize;
555
236 /* setup input device */ 556 /* setup input device */
237 __set_bit(EV_KEY, input->evbit); 557 __set_bit(EV_KEY, input->evbit);
238 558
@@ -243,6 +563,11 @@ static int __devinit adp5588_probe(struct i2c_client *client,
243 __set_bit(kpad->keycode[i] & KEY_MAX, input->keybit); 563 __set_bit(kpad->keycode[i] & KEY_MAX, input->keybit);
244 __clear_bit(KEY_RESERVED, input->keybit); 564 __clear_bit(KEY_RESERVED, input->keybit);
245 565
566 if (kpad->gpimapsize)
567 __set_bit(EV_SW, input->evbit);
568 for (i = 0; i < kpad->gpimapsize; i++)
569 __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
570
246 error = input_register_device(input); 571 error = input_register_device(input);
247 if (error) { 572 if (error) {
248 dev_err(&client->dev, "unable to register input device\n"); 573 dev_err(&client->dev, "unable to register input device\n");
@@ -261,6 +586,13 @@ static int __devinit adp5588_probe(struct i2c_client *client,
261 if (error) 586 if (error)
262 goto err_free_irq; 587 goto err_free_irq;
263 588
589 if (kpad->gpimapsize)
590 adp5588_report_switch_state(kpad);
591
592 error = adp5588_gpio_add(kpad);
593 if (error)
594 goto err_free_irq;
595
264 device_init_wakeup(&client->dev, 1); 596 device_init_wakeup(&client->dev, 1);
265 i2c_set_clientdata(client, kpad); 597 i2c_set_clientdata(client, kpad);
266 598
@@ -287,6 +619,7 @@ static int __devexit adp5588_remove(struct i2c_client *client)
287 free_irq(client->irq, kpad); 619 free_irq(client->irq, kpad);
288 cancel_delayed_work_sync(&kpad->work); 620 cancel_delayed_work_sync(&kpad->work);
289 input_unregister_device(kpad->input); 621 input_unregister_device(kpad->input);
622 adp5588_gpio_remove(kpad);
290 kfree(kpad); 623 kfree(kpad);
291 624
292 return 0; 625 return 0;