0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/delay.h>
0012 #include <linux/errno.h>
0013 #include <linux/gpio/driver.h>
0014 #include <linux/i2c.h>
0015 #include <linux/input.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/irq.h>
0018 #include <linux/ktime.h>
0019 #include <linux/module.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/pm.h>
0022 #include <linux/slab.h>
0023 #include <linux/timekeeping.h>
0024
0025 #include <linux/platform_data/adp5588.h>
0026
0027
0028 #define KEY_EV_PRESSED (1 << 7)
0029 #define KEY_EV_MASK (0x7F)
0030
0031 #define KP_SEL(x) (0xFFFF >> (16 - x))
0032
0033 #define KEYP_MAX_EVENT 10
0034
0035
0036
0037
0038
0039
0040 #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
0041 #define WA_DELAYED_READOUT_TIME 25
0042
0043 struct adp5588_kpad {
0044 struct i2c_client *client;
0045 struct input_dev *input;
0046 ktime_t irq_time;
0047 unsigned long delay;
0048 unsigned short keycode[ADP5588_KEYMAPSIZE];
0049 const struct adp5588_gpi_map *gpimap;
0050 unsigned short gpimapsize;
0051 #ifdef CONFIG_GPIOLIB
0052 unsigned char gpiomap[ADP5588_MAXGPIO];
0053 struct gpio_chip gc;
0054 struct mutex gpio_lock;
0055 u8 dat_out[3];
0056 u8 dir[3];
0057 #endif
0058 };
0059
0060 static int adp5588_read(struct i2c_client *client, u8 reg)
0061 {
0062 int ret = i2c_smbus_read_byte_data(client, reg);
0063
0064 if (ret < 0)
0065 dev_err(&client->dev, "Read Error\n");
0066
0067 return ret;
0068 }
0069
0070 static int adp5588_write(struct i2c_client *client, u8 reg, u8 val)
0071 {
0072 return i2c_smbus_write_byte_data(client, reg, val);
0073 }
0074
0075 #ifdef CONFIG_GPIOLIB
0076 static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
0077 {
0078 struct adp5588_kpad *kpad = gpiochip_get_data(chip);
0079 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
0080 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
0081 int val;
0082
0083 mutex_lock(&kpad->gpio_lock);
0084
0085 if (kpad->dir[bank] & bit)
0086 val = kpad->dat_out[bank];
0087 else
0088 val = adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank);
0089
0090 mutex_unlock(&kpad->gpio_lock);
0091
0092 return !!(val & bit);
0093 }
0094
0095 static void adp5588_gpio_set_value(struct gpio_chip *chip,
0096 unsigned off, int val)
0097 {
0098 struct adp5588_kpad *kpad = gpiochip_get_data(chip);
0099 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
0100 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
0101
0102 mutex_lock(&kpad->gpio_lock);
0103
0104 if (val)
0105 kpad->dat_out[bank] |= bit;
0106 else
0107 kpad->dat_out[bank] &= ~bit;
0108
0109 adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
0110 kpad->dat_out[bank]);
0111
0112 mutex_unlock(&kpad->gpio_lock);
0113 }
0114
0115 static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
0116 {
0117 struct adp5588_kpad *kpad = gpiochip_get_data(chip);
0118 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
0119 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
0120 int ret;
0121
0122 mutex_lock(&kpad->gpio_lock);
0123
0124 kpad->dir[bank] &= ~bit;
0125 ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
0126
0127 mutex_unlock(&kpad->gpio_lock);
0128
0129 return ret;
0130 }
0131
0132 static int adp5588_gpio_direction_output(struct gpio_chip *chip,
0133 unsigned off, int val)
0134 {
0135 struct adp5588_kpad *kpad = gpiochip_get_data(chip);
0136 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
0137 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
0138 int ret;
0139
0140 mutex_lock(&kpad->gpio_lock);
0141
0142 kpad->dir[bank] |= bit;
0143
0144 if (val)
0145 kpad->dat_out[bank] |= bit;
0146 else
0147 kpad->dat_out[bank] &= ~bit;
0148
0149 ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
0150 kpad->dat_out[bank]);
0151 ret |= adp5588_write(kpad->client, GPIO_DIR1 + bank,
0152 kpad->dir[bank]);
0153
0154 mutex_unlock(&kpad->gpio_lock);
0155
0156 return ret;
0157 }
0158
0159 static int adp5588_build_gpiomap(struct adp5588_kpad *kpad,
0160 const struct adp5588_kpad_platform_data *pdata)
0161 {
0162 bool pin_used[ADP5588_MAXGPIO];
0163 int n_unused = 0;
0164 int i;
0165
0166 memset(pin_used, 0, sizeof(pin_used));
0167
0168 for (i = 0; i < pdata->rows; i++)
0169 pin_used[i] = true;
0170
0171 for (i = 0; i < pdata->cols; i++)
0172 pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true;
0173
0174 for (i = 0; i < kpad->gpimapsize; i++)
0175 pin_used[kpad->gpimap[i].pin - GPI_PIN_BASE] = true;
0176
0177 for (i = 0; i < ADP5588_MAXGPIO; i++)
0178 if (!pin_used[i])
0179 kpad->gpiomap[n_unused++] = i;
0180
0181 return n_unused;
0182 }
0183
0184 static void adp5588_gpio_do_teardown(void *_kpad)
0185 {
0186 struct adp5588_kpad *kpad = _kpad;
0187 struct device *dev = &kpad->client->dev;
0188 const struct adp5588_kpad_platform_data *pdata = dev_get_platdata(dev);
0189 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
0190 int error;
0191
0192 error = gpio_data->teardown(kpad->client,
0193 kpad->gc.base, kpad->gc.ngpio,
0194 gpio_data->context);
0195 if (error)
0196 dev_warn(&kpad->client->dev, "teardown failed %d\n", error);
0197 }
0198
0199 static int adp5588_gpio_add(struct adp5588_kpad *kpad)
0200 {
0201 struct device *dev = &kpad->client->dev;
0202 const struct adp5588_kpad_platform_data *pdata = dev_get_platdata(dev);
0203 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
0204 int i, error;
0205
0206 if (!gpio_data)
0207 return 0;
0208
0209 kpad->gc.ngpio = adp5588_build_gpiomap(kpad, pdata);
0210 if (kpad->gc.ngpio == 0) {
0211 dev_info(dev, "No unused gpios left to export\n");
0212 return 0;
0213 }
0214
0215 kpad->gc.direction_input = adp5588_gpio_direction_input;
0216 kpad->gc.direction_output = adp5588_gpio_direction_output;
0217 kpad->gc.get = adp5588_gpio_get_value;
0218 kpad->gc.set = adp5588_gpio_set_value;
0219 kpad->gc.can_sleep = 1;
0220
0221 kpad->gc.base = gpio_data->gpio_start;
0222 kpad->gc.label = kpad->client->name;
0223 kpad->gc.owner = THIS_MODULE;
0224 kpad->gc.names = gpio_data->names;
0225
0226 mutex_init(&kpad->gpio_lock);
0227
0228 error = devm_gpiochip_add_data(dev, &kpad->gc, kpad);
0229 if (error) {
0230 dev_err(dev, "gpiochip_add failed: %d\n", error);
0231 return error;
0232 }
0233
0234 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
0235 kpad->dat_out[i] = adp5588_read(kpad->client,
0236 GPIO_DAT_OUT1 + i);
0237 kpad->dir[i] = adp5588_read(kpad->client, GPIO_DIR1 + i);
0238 }
0239
0240 if (gpio_data->setup) {
0241 error = gpio_data->setup(kpad->client,
0242 kpad->gc.base, kpad->gc.ngpio,
0243 gpio_data->context);
0244 if (error)
0245 dev_warn(dev, "setup failed: %d\n", error);
0246 }
0247
0248 if (gpio_data->teardown) {
0249 error = devm_add_action(dev, adp5588_gpio_do_teardown, kpad);
0250 if (error)
0251 dev_warn(dev, "failed to schedule teardown: %d\n",
0252 error);
0253 }
0254
0255 return 0;
0256 }
0257
0258 #else
0259 static inline int adp5588_gpio_add(struct adp5588_kpad *kpad)
0260 {
0261 return 0;
0262 }
0263 #endif
0264
0265 static void adp5588_report_events(struct adp5588_kpad *kpad, int ev_cnt)
0266 {
0267 int i, j;
0268
0269 for (i = 0; i < ev_cnt; i++) {
0270 int key = adp5588_read(kpad->client, Key_EVENTA + i);
0271 int key_val = key & KEY_EV_MASK;
0272
0273 if (key_val >= GPI_PIN_BASE && key_val <= GPI_PIN_END) {
0274 for (j = 0; j < kpad->gpimapsize; j++) {
0275 if (key_val == kpad->gpimap[j].pin) {
0276 input_report_switch(kpad->input,
0277 kpad->gpimap[j].sw_evt,
0278 key & KEY_EV_PRESSED);
0279 break;
0280 }
0281 }
0282 } else {
0283 input_report_key(kpad->input,
0284 kpad->keycode[key_val - 1],
0285 key & KEY_EV_PRESSED);
0286 }
0287 }
0288 }
0289
0290 static irqreturn_t adp5588_hard_irq(int irq, void *handle)
0291 {
0292 struct adp5588_kpad *kpad = handle;
0293
0294 kpad->irq_time = ktime_get();
0295
0296 return IRQ_WAKE_THREAD;
0297 }
0298
0299 static irqreturn_t adp5588_thread_irq(int irq, void *handle)
0300 {
0301 struct adp5588_kpad *kpad = handle;
0302 struct i2c_client *client = kpad->client;
0303 ktime_t target_time, now;
0304 unsigned long delay;
0305 int status, ev_cnt;
0306
0307
0308
0309
0310
0311 if (kpad->delay) {
0312 target_time = ktime_add_ms(kpad->irq_time, kpad->delay);
0313 now = ktime_get();
0314 if (ktime_before(now, target_time)) {
0315 delay = ktime_to_us(ktime_sub(target_time, now));
0316 usleep_range(delay, delay + 1000);
0317 }
0318 }
0319
0320 status = adp5588_read(client, INT_STAT);
0321
0322 if (status & ADP5588_OVR_FLOW_INT)
0323 dev_err(&client->dev, "Event Overflow Error\n");
0324
0325 if (status & ADP5588_KE_INT) {
0326 ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & ADP5588_KEC;
0327 if (ev_cnt) {
0328 adp5588_report_events(kpad, ev_cnt);
0329 input_sync(kpad->input);
0330 }
0331 }
0332
0333 adp5588_write(client, INT_STAT, status);
0334
0335 return IRQ_HANDLED;
0336 }
0337
0338 static int adp5588_setup(struct i2c_client *client)
0339 {
0340 const struct adp5588_kpad_platform_data *pdata =
0341 dev_get_platdata(&client->dev);
0342 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
0343 int i, ret;
0344 unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
0345
0346 ret = adp5588_write(client, KP_GPIO1, KP_SEL(pdata->rows));
0347 ret |= adp5588_write(client, KP_GPIO2, KP_SEL(pdata->cols) & 0xFF);
0348 ret |= adp5588_write(client, KP_GPIO3, KP_SEL(pdata->cols) >> 8);
0349
0350 if (pdata->en_keylock) {
0351 ret |= adp5588_write(client, UNLOCK1, pdata->unlock_key1);
0352 ret |= adp5588_write(client, UNLOCK2, pdata->unlock_key2);
0353 ret |= adp5588_write(client, KEY_LCK_EC_STAT, ADP5588_K_LCK_EN);
0354 }
0355
0356 for (i = 0; i < KEYP_MAX_EVENT; i++)
0357 ret |= adp5588_read(client, Key_EVENTA);
0358
0359 for (i = 0; i < pdata->gpimapsize; i++) {
0360 unsigned short pin = pdata->gpimap[i].pin;
0361
0362 if (pin <= GPI_PIN_ROW_END) {
0363 evt_mode1 |= (1 << (pin - GPI_PIN_ROW_BASE));
0364 } else {
0365 evt_mode2 |= ((1 << (pin - GPI_PIN_COL_BASE)) & 0xFF);
0366 evt_mode3 |= ((1 << (pin - GPI_PIN_COL_BASE)) >> 8);
0367 }
0368 }
0369
0370 if (pdata->gpimapsize) {
0371 ret |= adp5588_write(client, GPI_EM1, evt_mode1);
0372 ret |= adp5588_write(client, GPI_EM2, evt_mode2);
0373 ret |= adp5588_write(client, GPI_EM3, evt_mode3);
0374 }
0375
0376 if (gpio_data) {
0377 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
0378 int pull_mask = gpio_data->pullup_dis_mask;
0379
0380 ret |= adp5588_write(client, GPIO_PULL1 + i,
0381 (pull_mask >> (8 * i)) & 0xFF);
0382 }
0383 }
0384
0385 ret |= adp5588_write(client, INT_STAT,
0386 ADP5588_CMP2_INT | ADP5588_CMP1_INT |
0387 ADP5588_OVR_FLOW_INT | ADP5588_K_LCK_INT |
0388 ADP5588_GPI_INT | ADP5588_KE_INT);
0389
0390 ret |= adp5588_write(client, CFG, ADP5588_INT_CFG |
0391 ADP5588_OVR_FLOW_IEN |
0392 ADP5588_KE_IEN);
0393
0394 if (ret < 0) {
0395 dev_err(&client->dev, "Write Error\n");
0396 return ret;
0397 }
0398
0399 return 0;
0400 }
0401
0402 static void adp5588_report_switch_state(struct adp5588_kpad *kpad)
0403 {
0404 int gpi_stat1 = adp5588_read(kpad->client, GPIO_DAT_STAT1);
0405 int gpi_stat2 = adp5588_read(kpad->client, GPIO_DAT_STAT2);
0406 int gpi_stat3 = adp5588_read(kpad->client, GPIO_DAT_STAT3);
0407 int gpi_stat_tmp, pin_loc;
0408 int i;
0409
0410 for (i = 0; i < kpad->gpimapsize; i++) {
0411 unsigned short pin = kpad->gpimap[i].pin;
0412
0413 if (pin <= GPI_PIN_ROW_END) {
0414 gpi_stat_tmp = gpi_stat1;
0415 pin_loc = pin - GPI_PIN_ROW_BASE;
0416 } else if ((pin - GPI_PIN_COL_BASE) < 8) {
0417 gpi_stat_tmp = gpi_stat2;
0418 pin_loc = pin - GPI_PIN_COL_BASE;
0419 } else {
0420 gpi_stat_tmp = gpi_stat3;
0421 pin_loc = pin - GPI_PIN_COL_BASE - 8;
0422 }
0423
0424 if (gpi_stat_tmp < 0) {
0425 dev_err(&kpad->client->dev,
0426 "Can't read GPIO_DAT_STAT switch %d default to OFF\n",
0427 pin);
0428 gpi_stat_tmp = 0;
0429 }
0430
0431 input_report_switch(kpad->input,
0432 kpad->gpimap[i].sw_evt,
0433 !(gpi_stat_tmp & (1 << pin_loc)));
0434 }
0435
0436 input_sync(kpad->input);
0437 }
0438
0439
0440 static int adp5588_probe(struct i2c_client *client,
0441 const struct i2c_device_id *id)
0442 {
0443 struct adp5588_kpad *kpad;
0444 const struct adp5588_kpad_platform_data *pdata =
0445 dev_get_platdata(&client->dev);
0446 struct input_dev *input;
0447 unsigned int revid;
0448 int ret, i;
0449 int error;
0450
0451 if (!i2c_check_functionality(client->adapter,
0452 I2C_FUNC_SMBUS_BYTE_DATA)) {
0453 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
0454 return -EIO;
0455 }
0456
0457 if (!pdata) {
0458 dev_err(&client->dev, "no platform data?\n");
0459 return -EINVAL;
0460 }
0461
0462 if (!pdata->rows || !pdata->cols || !pdata->keymap) {
0463 dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
0464 return -EINVAL;
0465 }
0466
0467 if (pdata->keymapsize != ADP5588_KEYMAPSIZE) {
0468 dev_err(&client->dev, "invalid keymapsize\n");
0469 return -EINVAL;
0470 }
0471
0472 if (!pdata->gpimap && pdata->gpimapsize) {
0473 dev_err(&client->dev, "invalid gpimap from pdata\n");
0474 return -EINVAL;
0475 }
0476
0477 if (pdata->gpimapsize > ADP5588_GPIMAPSIZE_MAX) {
0478 dev_err(&client->dev, "invalid gpimapsize\n");
0479 return -EINVAL;
0480 }
0481
0482 for (i = 0; i < pdata->gpimapsize; i++) {
0483 unsigned short pin = pdata->gpimap[i].pin;
0484
0485 if (pin < GPI_PIN_BASE || pin > GPI_PIN_END) {
0486 dev_err(&client->dev, "invalid gpi pin data\n");
0487 return -EINVAL;
0488 }
0489
0490 if (pin <= GPI_PIN_ROW_END) {
0491 if (pin - GPI_PIN_ROW_BASE + 1 <= pdata->rows) {
0492 dev_err(&client->dev, "invalid gpi row data\n");
0493 return -EINVAL;
0494 }
0495 } else {
0496 if (pin - GPI_PIN_COL_BASE + 1 <= pdata->cols) {
0497 dev_err(&client->dev, "invalid gpi col data\n");
0498 return -EINVAL;
0499 }
0500 }
0501 }
0502
0503 if (!client->irq) {
0504 dev_err(&client->dev, "no IRQ?\n");
0505 return -EINVAL;
0506 }
0507
0508 kpad = devm_kzalloc(&client->dev, sizeof(*kpad), GFP_KERNEL);
0509 if (!kpad)
0510 return -ENOMEM;
0511
0512 input = devm_input_allocate_device(&client->dev);
0513 if (!input)
0514 return -ENOMEM;
0515
0516 kpad->client = client;
0517 kpad->input = input;
0518
0519 ret = adp5588_read(client, DEV_ID);
0520 if (ret < 0)
0521 return ret;
0522
0523 revid = (u8) ret & ADP5588_DEVICE_ID_MASK;
0524 if (WA_DELAYED_READOUT_REVID(revid))
0525 kpad->delay = msecs_to_jiffies(WA_DELAYED_READOUT_TIME);
0526
0527 input->name = client->name;
0528 input->phys = "adp5588-keys/input0";
0529
0530 input_set_drvdata(input, kpad);
0531
0532 input->id.bustype = BUS_I2C;
0533 input->id.vendor = 0x0001;
0534 input->id.product = 0x0001;
0535 input->id.version = revid;
0536
0537 input->keycodesize = sizeof(kpad->keycode[0]);
0538 input->keycodemax = pdata->keymapsize;
0539 input->keycode = kpad->keycode;
0540
0541 memcpy(kpad->keycode, pdata->keymap,
0542 pdata->keymapsize * input->keycodesize);
0543
0544 kpad->gpimap = pdata->gpimap;
0545 kpad->gpimapsize = pdata->gpimapsize;
0546
0547
0548 __set_bit(EV_KEY, input->evbit);
0549
0550 if (pdata->repeat)
0551 __set_bit(EV_REP, input->evbit);
0552
0553 for (i = 0; i < input->keycodemax; i++)
0554 if (kpad->keycode[i] <= KEY_MAX)
0555 __set_bit(kpad->keycode[i], input->keybit);
0556 __clear_bit(KEY_RESERVED, input->keybit);
0557
0558 if (kpad->gpimapsize)
0559 __set_bit(EV_SW, input->evbit);
0560 for (i = 0; i < kpad->gpimapsize; i++)
0561 __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
0562
0563 error = input_register_device(input);
0564 if (error) {
0565 dev_err(&client->dev, "unable to register input device: %d\n",
0566 error);
0567 return error;
0568 }
0569
0570 error = devm_request_threaded_irq(&client->dev, client->irq,
0571 adp5588_hard_irq, adp5588_thread_irq,
0572 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
0573 client->dev.driver->name, kpad);
0574 if (error) {
0575 dev_err(&client->dev, "failed to request irq %d: %d\n",
0576 client->irq, error);
0577 return error;
0578 }
0579
0580 error = adp5588_setup(client);
0581 if (error)
0582 return error;
0583
0584 if (kpad->gpimapsize)
0585 adp5588_report_switch_state(kpad);
0586
0587 error = adp5588_gpio_add(kpad);
0588 if (error)
0589 return error;
0590
0591 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
0592 return 0;
0593 }
0594
0595 static int adp5588_remove(struct i2c_client *client)
0596 {
0597 adp5588_write(client, CFG, 0);
0598
0599
0600 return 0;
0601 }
0602
0603 static int __maybe_unused adp5588_suspend(struct device *dev)
0604 {
0605 struct i2c_client *client = to_i2c_client(dev);
0606
0607 disable_irq(client->irq);
0608
0609 return 0;
0610 }
0611
0612 static int __maybe_unused adp5588_resume(struct device *dev)
0613 {
0614 struct i2c_client *client = to_i2c_client(dev);
0615
0616 enable_irq(client->irq);
0617
0618 return 0;
0619 }
0620
0621 static SIMPLE_DEV_PM_OPS(adp5588_dev_pm_ops, adp5588_suspend, adp5588_resume);
0622
0623 static const struct i2c_device_id adp5588_id[] = {
0624 { "adp5588-keys", 0 },
0625 { "adp5587-keys", 0 },
0626 { }
0627 };
0628 MODULE_DEVICE_TABLE(i2c, adp5588_id);
0629
0630 static struct i2c_driver adp5588_driver = {
0631 .driver = {
0632 .name = KBUILD_MODNAME,
0633 .pm = &adp5588_dev_pm_ops,
0634 },
0635 .probe = adp5588_probe,
0636 .remove = adp5588_remove,
0637 .id_table = adp5588_id,
0638 };
0639
0640 module_i2c_driver(adp5588_driver);
0641
0642 MODULE_LICENSE("GPL");
0643 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
0644 MODULE_DESCRIPTION("ADP5588/87 Keypad driver");