11 #include <linux/module.h>
16 #include <linux/errno.h>
19 #include <linux/input.h>
20 #include <linux/i2c.h>
22 #include <linux/slab.h>
27 #define KEY_EV_PRESSED (1 << 7)
28 #define KEY_EV_MASK (0x7F)
30 #define KP_SEL(x) (0xFFFF >> (16 - x))
32 #define KEYP_MAX_EVENT 10
39 #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
53 struct mutex gpio_lock;
75 static int adp5588_gpio_get_value(
struct gpio_chip *
chip,
unsigned off)
84 static void adp5588_gpio_set_value(
struct gpio_chip *
chip,
85 unsigned off,
int val)
94 kpad->dat_out[bank] |=
bit;
96 kpad->dat_out[bank] &= ~bit;
104 static int adp5588_gpio_direction_input(
struct gpio_chip *chip,
unsigned off)
108 unsigned int bit =
ADP5588_BIT(kpad->gpiomap[off]);
113 kpad->dir[bank] &= ~bit;
121 static int adp5588_gpio_direction_output(
struct gpio_chip *chip,
122 unsigned off,
int val)
126 unsigned int bit =
ADP5588_BIT(kpad->gpiomap[off]);
131 kpad->dir[bank] |=
bit;
134 kpad->dat_out[bank] |=
bit;
136 kpad->dat_out[bank] &= ~bit;
139 kpad->dat_out[bank]);
155 memset(pin_used, 0,
sizeof(pin_used));
157 for (i = 0; i < pdata->
rows; i++)
160 for (i = 0; i < pdata->
cols; i++)
168 kpad->gpiomap[n_unused++] =
i;
183 kpad->gc.ngpio = adp5588_build_gpiomap(kpad, pdata);
184 if (kpad->gc.ngpio == 0) {
185 dev_info(dev,
"No unused gpios left to export\n");
189 kpad->export_gpio =
true;
191 kpad->gc.direction_input = adp5588_gpio_direction_input;
192 kpad->gc.direction_output = adp5588_gpio_direction_output;
193 kpad->gc.get = adp5588_gpio_get_value;
194 kpad->gc.set = adp5588_gpio_set_value;
195 kpad->gc.can_sleep = 1;
198 kpad->gc.label = kpad->
client->name;
200 kpad->gc.names = gpio_data->
names;
206 dev_err(dev,
"gpiochip_add failed, err: %d\n", error);
211 kpad->dat_out[
i] = adp5588_read(kpad->
client,
216 if (gpio_data->
setup) {
218 kpad->gc.base, kpad->gc.ngpio,
221 dev_warn(dev,
"setup failed, %d\n", error);
234 if (!kpad->export_gpio)
239 kpad->gc.base, kpad->gc.ngpio,
242 dev_warn(dev,
"teardown failed %d\n", error);
247 dev_warn(dev,
"gpiochip_remove failed %d\n", error);
250 static inline int adp5588_gpio_add(
struct adp5588_kpad *kpad)
255 static inline void adp5588_gpio_remove(
struct adp5588_kpad *kpad)
260 static void adp5588_report_events(
struct adp5588_kpad *kpad,
int ev_cnt)
264 for (i = 0; i < ev_cnt; i++) {
270 if (key_val == kpad->
gpimap[j].pin) {
271 input_report_switch(kpad->
input,
278 input_report_key(kpad->
input,
292 status = adp5588_read(client,
INT_STAT);
295 dev_err(&client->
dev,
"Event Overflow Error\n");
300 adp5588_report_events(kpad, ev_cnt);
301 input_sync(kpad->
input);
304 adp5588_write(client,
INT_STAT, status);
327 unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
354 ret |= adp5588_write(client,
GPI_EM1, evt_mode1);
355 ret |= adp5588_write(client,
GPI_EM2, evt_mode2);
356 ret |= adp5588_write(client,
GPI_EM3, evt_mode3);
364 (pull_mask >> (8 * i)) & 0xFF);
368 ret |= adp5588_write(client,
INT_STAT,
390 int gpi_stat_tmp, pin_loc;
394 unsigned short pin = kpad->
gpimap[
i].pin;
397 gpi_stat_tmp = gpi_stat1;
400 gpi_stat_tmp = gpi_stat2;
403 gpi_stat_tmp = gpi_stat3;
407 if (gpi_stat_tmp < 0) {
409 "Can't read GPIO_DAT_STAT switch %d default to OFF\n",
414 input_report_switch(kpad->
input,
416 !(gpi_stat_tmp & (1 << pin_loc)));
419 input_sync(kpad->
input);
428 struct input_dev *
input;
433 if (!i2c_check_functionality(client->
adapter,
435 dev_err(&client->
dev,
"SMBUS Byte Data not Supported\n");
445 dev_err(&client->
dev,
"no rows, cols or keymap from pdata\n");
450 dev_err(&client->
dev,
"invalid keymapsize\n");
455 dev_err(&client->
dev,
"invalid gpimap from pdata\n");
460 dev_err(&client->
dev,
"invalid gpimapsize\n");
465 unsigned short pin = pdata->
gpimap[
i].pin;
468 dev_err(&client->
dev,
"invalid gpi pin data\n");
474 dev_err(&client->
dev,
"invalid gpi row data\n");
479 dev_err(&client->
dev,
"invalid gpi col data\n");
491 input = input_allocate_device();
492 if (!kpad || !input) {
501 ret = adp5588_read(client,
DEV_ID);
511 input->name = client->
name;
512 input->phys =
"adp5588-keys/input0";
513 input->dev.parent = &client->
dev;
515 input_set_drvdata(input, kpad);
518 input->id.vendor = 0x0001;
519 input->id.product = 0x0001;
520 input->id.version = revid;
522 input->keycodesize =
sizeof(kpad->
keycode[0]);
524 input->keycode = kpad->
keycode;
538 for (i = 0; i < input->keycodemax; i++)
547 error = input_register_device(input);
549 dev_err(&client->
dev,
"unable to register input device\n");
555 client->
dev.driver->name, kpad);
561 error = adp5588_setup(client);
566 adp5588_report_switch_state(kpad);
568 error = adp5588_gpio_add(kpad);
573 i2c_set_clientdata(client, kpad);
575 dev_info(&client->
dev,
"Rev.%d keypad, irq %d\n", revid, client->
irq);
581 input_unregister_device(input);
584 input_free_device(input);
594 adp5588_write(client,
CFG, 0);
597 input_unregister_device(kpad->
input);
598 adp5588_gpio_remove(kpad);
605 static int adp5588_suspend(
struct device *dev)
613 if (device_may_wakeup(&client->
dev))
614 enable_irq_wake(client->
irq);
619 static int adp5588_resume(
struct device *dev)
624 if (device_may_wakeup(&client->
dev))
625 disable_irq_wake(client->
irq);
632 static const struct dev_pm_ops adp5588_dev_pm_ops = {
634 .resume = adp5588_resume,
639 {
"adp5588-keys", 0 },
640 {
"adp5587-keys", 0 },
647 .name = KBUILD_MODNAME,
649 .pm = &adp5588_dev_pm_ops,
652 .probe = adp5588_probe,
654 .id_table = adp5588_id,