1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * File: drivers/input/keyboard/adp5588_keys.c 4 * Description: keypad driver for ADP5588 and ADP5587 5 * I2C QWERTY Keypad and IO Expander 6 * Bugs: Enter bugs at http://blackfin.uclinux.org/ 7 * 8 * Copyright (C) 2008-2010 Analog Devices Inc. 9 */ 10 11 #include <linux/delay.h> 12 #include <linux/errno.h> 13 #include <linux/gpio/driver.h> 14 #include <linux/i2c.h> 15 #include <linux/input.h> 16 #include <linux/input/matrix_keypad.h> 17 #include <linux/interrupt.h> 18 #include <linux/irq.h> 19 #include <linux/ktime.h> 20 #include <linux/module.h> 21 #include <linux/mod_devicetable.h> 22 #include <linux/pinctrl/pinconf-generic.h> 23 #include <linux/platform_device.h> 24 #include <linux/pm.h> 25 #include <linux/slab.h> 26 #include <linux/timekeeping.h> 27 28 #define DEV_ID 0x00 /* Device ID */ 29 #define CFG 0x01 /* Configuration Register1 */ 30 #define INT_STAT 0x02 /* Interrupt Status Register */ 31 #define KEY_LCK_EC_STAT 0x03 /* Key Lock and Event Counter Register */ 32 #define Key_EVENTA 0x04 /* Key Event Register A */ 33 #define Key_EVENTB 0x05 /* Key Event Register B */ 34 #define Key_EVENTC 0x06 /* Key Event Register C */ 35 #define Key_EVENTD 0x07 /* Key Event Register D */ 36 #define Key_EVENTE 0x08 /* Key Event Register E */ 37 #define Key_EVENTF 0x09 /* Key Event Register F */ 38 #define Key_EVENTG 0x0A /* Key Event Register G */ 39 #define Key_EVENTH 0x0B /* Key Event Register H */ 40 #define Key_EVENTI 0x0C /* Key Event Register I */ 41 #define Key_EVENTJ 0x0D /* Key Event Register J */ 42 #define KP_LCK_TMR 0x0E /* Keypad Lock1 to Lock2 Timer */ 43 #define UNLOCK1 0x0F /* Unlock Key1 */ 44 #define UNLOCK2 0x10 /* Unlock Key2 */ 45 #define GPIO_INT_STAT1 0x11 /* GPIO Interrupt Status */ 46 #define GPIO_INT_STAT2 0x12 /* GPIO Interrupt Status */ 47 #define GPIO_INT_STAT3 0x13 /* GPIO Interrupt Status */ 48 #define GPIO_DAT_STAT1 0x14 /* GPIO Data Status, Read twice to clear */ 49 #define GPIO_DAT_STAT2 0x15 /* GPIO Data Status, Read twice to clear */ 50 #define GPIO_DAT_STAT3 0x16 /* GPIO Data Status, Read twice to clear */ 51 #define GPIO_DAT_OUT1 0x17 /* GPIO DATA OUT */ 52 #define GPIO_DAT_OUT2 0x18 /* GPIO DATA OUT */ 53 #define GPIO_DAT_OUT3 0x19 /* GPIO DATA OUT */ 54 #define GPIO_INT_EN1 0x1A /* GPIO Interrupt Enable */ 55 #define GPIO_INT_EN2 0x1B /* GPIO Interrupt Enable */ 56 #define GPIO_INT_EN3 0x1C /* GPIO Interrupt Enable */ 57 #define KP_GPIO1 0x1D /* Keypad or GPIO Selection */ 58 #define KP_GPIO2 0x1E /* Keypad or GPIO Selection */ 59 #define KP_GPIO3 0x1F /* Keypad or GPIO Selection */ 60 #define GPI_EM1 0x20 /* GPI Event Mode 1 */ 61 #define GPI_EM2 0x21 /* GPI Event Mode 2 */ 62 #define GPI_EM3 0x22 /* GPI Event Mode 3 */ 63 #define GPIO_DIR1 0x23 /* GPIO Data Direction */ 64 #define GPIO_DIR2 0x24 /* GPIO Data Direction */ 65 #define GPIO_DIR3 0x25 /* GPIO Data Direction */ 66 #define GPIO_INT_LVL1 0x26 /* GPIO Edge/Level Detect */ 67 #define GPIO_INT_LVL2 0x27 /* GPIO Edge/Level Detect */ 68 #define GPIO_INT_LVL3 0x28 /* GPIO Edge/Level Detect */ 69 #define Debounce_DIS1 0x29 /* Debounce Disable */ 70 #define Debounce_DIS2 0x2A /* Debounce Disable */ 71 #define Debounce_DIS3 0x2B /* Debounce Disable */ 72 #define GPIO_PULL1 0x2C /* GPIO Pull Disable */ 73 #define GPIO_PULL2 0x2D /* GPIO Pull Disable */ 74 #define GPIO_PULL3 0x2E /* GPIO Pull Disable */ 75 #define CMP_CFG_STAT 0x30 /* Comparator Configuration and Status Register */ 76 #define CMP_CONFG_SENS1 0x31 /* Sensor1 Comparator Configuration Register */ 77 #define CMP_CONFG_SENS2 0x32 /* L2 Light Sensor Reference Level, Output Falling for Sensor 1 */ 78 #define CMP1_LVL2_TRIP 0x33 /* L2 Light Sensor Hysteresis (Active when Output Rising) for Sensor 1 */ 79 #define CMP1_LVL2_HYS 0x34 /* L3 Light Sensor Reference Level, Output Falling For Sensor 1 */ 80 #define CMP1_LVL3_TRIP 0x35 /* L3 Light Sensor Hysteresis (Active when Output Rising) For Sensor 1 */ 81 #define CMP1_LVL3_HYS 0x36 /* Sensor 2 Comparator Configuration Register */ 82 #define CMP2_LVL2_TRIP 0x37 /* L2 Light Sensor Reference Level, Output Falling for Sensor 2 */ 83 #define CMP2_LVL2_HYS 0x38 /* L2 Light Sensor Hysteresis (Active when Output Rising) for Sensor 2 */ 84 #define CMP2_LVL3_TRIP 0x39 /* L3 Light Sensor Reference Level, Output Falling For Sensor 2 */ 85 #define CMP2_LVL3_HYS 0x3A /* L3 Light Sensor Hysteresis (Active when Output Rising) For Sensor 2 */ 86 #define CMP1_ADC_DAT_R1 0x3B /* Comparator 1 ADC data Register1 */ 87 #define CMP1_ADC_DAT_R2 0x3C /* Comparator 1 ADC data Register2 */ 88 #define CMP2_ADC_DAT_R1 0x3D /* Comparator 2 ADC data Register1 */ 89 #define CMP2_ADC_DAT_R2 0x3E /* Comparator 2 ADC data Register2 */ 90 91 #define ADP5588_DEVICE_ID_MASK 0xF 92 93 /* Configuration Register1 */ 94 #define ADP5588_AUTO_INC (1 << 7) 95 #define ADP5588_GPIEM_CFG (1 << 6) 96 #define ADP5588_OVR_FLOW_M (1 << 5) 97 #define ADP5588_INT_CFG (1 << 4) 98 #define ADP5588_OVR_FLOW_IEN (1 << 3) 99 #define ADP5588_K_LCK_IM (1 << 2) 100 #define ADP5588_GPI_IEN (1 << 1) 101 #define ADP5588_KE_IEN (1 << 0) 102 103 /* Interrupt Status Register */ 104 #define ADP5588_CMP2_INT (1 << 5) 105 #define ADP5588_CMP1_INT (1 << 4) 106 #define ADP5588_OVR_FLOW_INT (1 << 3) 107 #define ADP5588_K_LCK_INT (1 << 2) 108 #define ADP5588_GPI_INT (1 << 1) 109 #define ADP5588_KE_INT (1 << 0) 110 111 /* Key Lock and Event Counter Register */ 112 #define ADP5588_K_LCK_EN (1 << 6) 113 #define ADP5588_LCK21 0x30 114 #define ADP5588_KEC 0xF 115 116 #define ADP5588_MAXGPIO 18 117 #define ADP5588_BANK(offs) ((offs) >> 3) 118 #define ADP5588_BIT(offs) (1u << ((offs) & 0x7)) 119 120 /* Put one of these structures in i2c_board_info platform_data */ 121 122 /* 123 * 128 so it fits matrix-keymap maximum number of keys when the full 124 * 10cols * 8rows are used. 125 */ 126 #define ADP5588_KEYMAPSIZE 128 127 128 #define GPI_PIN_ROW0 97 129 #define GPI_PIN_ROW1 98 130 #define GPI_PIN_ROW2 99 131 #define GPI_PIN_ROW3 100 132 #define GPI_PIN_ROW4 101 133 #define GPI_PIN_ROW5 102 134 #define GPI_PIN_ROW6 103 135 #define GPI_PIN_ROW7 104 136 #define GPI_PIN_COL0 105 137 #define GPI_PIN_COL1 106 138 #define GPI_PIN_COL2 107 139 #define GPI_PIN_COL3 108 140 #define GPI_PIN_COL4 109 141 #define GPI_PIN_COL5 110 142 #define GPI_PIN_COL6 111 143 #define GPI_PIN_COL7 112 144 #define GPI_PIN_COL8 113 145 #define GPI_PIN_COL9 114 146 147 #define GPI_PIN_ROW_BASE GPI_PIN_ROW0 148 #define GPI_PIN_ROW_END GPI_PIN_ROW7 149 #define GPI_PIN_COL_BASE GPI_PIN_COL0 150 #define GPI_PIN_COL_END GPI_PIN_COL9 151 152 #define GPI_PIN_BASE GPI_PIN_ROW_BASE 153 #define GPI_PIN_END GPI_PIN_COL_END 154 155 #define ADP5588_ROWS_MAX (GPI_PIN_ROW7 - GPI_PIN_ROW0 + 1) 156 #define ADP5588_COLS_MAX (GPI_PIN_COL9 - GPI_PIN_COL0 + 1) 157 158 #define ADP5588_GPIMAPSIZE_MAX (GPI_PIN_END - GPI_PIN_BASE + 1) 159 160 /* Key Event Register xy */ 161 #define KEY_EV_PRESSED (1 << 7) 162 #define KEY_EV_MASK (0x7F) 163 164 #define KP_SEL(x) (0xFFFF >> (16 - x)) /* 2^x-1 */ 165 166 #define KEYP_MAX_EVENT 10 167 168 /* 169 * Early pre 4.0 Silicon required to delay readout by at least 25ms, 170 * since the Event Counter Register updated 25ms after the interrupt 171 * asserted. 172 */ 173 #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4) 174 #define WA_DELAYED_READOUT_TIME 25 175 176 #define ADP5588_INVALID_HWIRQ (~0UL) 177 178 struct adp5588_kpad { 179 struct i2c_client *client; 180 struct input_dev *input; 181 ktime_t irq_time; 182 unsigned long delay; 183 u32 row_shift; 184 u32 rows; 185 u32 cols; 186 u32 unlock_keys[2]; 187 int nkeys_unlock; 188 unsigned short keycode[ADP5588_KEYMAPSIZE]; 189 unsigned char gpiomap[ADP5588_MAXGPIO]; 190 struct gpio_chip gc; 191 struct mutex gpio_lock; /* Protect cached dir, dat_out */ 192 u8 dat_out[3]; 193 u8 dir[3]; 194 u8 int_en[3]; 195 u8 irq_mask[3]; 196 u8 pull_dis[3]; 197 }; 198 199 static int adp5588_read(struct i2c_client *client, u8 reg) 200 { 201 int ret = i2c_smbus_read_byte_data(client, reg); 202 203 if (ret < 0) 204 dev_err(&client->dev, "Read Error\n"); 205 206 return ret; 207 } 208 209 static int adp5588_write(struct i2c_client *client, u8 reg, u8 val) 210 { 211 return i2c_smbus_write_byte_data(client, reg, val); 212 } 213 214 static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off) 215 { 216 struct adp5588_kpad *kpad = gpiochip_get_data(chip); 217 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); 218 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); 219 int val; 220 221 mutex_lock(&kpad->gpio_lock); 222 223 if (kpad->dir[bank] & bit) 224 val = kpad->dat_out[bank]; 225 else 226 val = adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank); 227 228 mutex_unlock(&kpad->gpio_lock); 229 230 return !!(val & bit); 231 } 232 233 static void adp5588_gpio_set_value(struct gpio_chip *chip, 234 unsigned off, int val) 235 { 236 struct adp5588_kpad *kpad = gpiochip_get_data(chip); 237 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); 238 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); 239 240 mutex_lock(&kpad->gpio_lock); 241 242 if (val) 243 kpad->dat_out[bank] |= bit; 244 else 245 kpad->dat_out[bank] &= ~bit; 246 247 adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, 248 kpad->dat_out[bank]); 249 250 mutex_unlock(&kpad->gpio_lock); 251 } 252 253 static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off, 254 unsigned long config) 255 { 256 struct adp5588_kpad *kpad = gpiochip_get_data(chip); 257 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); 258 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); 259 bool pull_disable; 260 int ret; 261 262 switch (pinconf_to_config_param(config)) { 263 case PIN_CONFIG_BIAS_PULL_UP: 264 pull_disable = false; 265 break; 266 case PIN_CONFIG_BIAS_DISABLE: 267 pull_disable = true; 268 break; 269 default: 270 return -ENOTSUPP; 271 } 272 273 mutex_lock(&kpad->gpio_lock); 274 275 if (pull_disable) 276 kpad->pull_dis[bank] |= bit; 277 else 278 kpad->pull_dis[bank] &= bit; 279 280 ret = adp5588_write(kpad->client, GPIO_PULL1 + bank, 281 kpad->pull_dis[bank]); 282 283 mutex_unlock(&kpad->gpio_lock); 284 285 return ret; 286 } 287 288 static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off) 289 { 290 struct adp5588_kpad *kpad = gpiochip_get_data(chip); 291 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); 292 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); 293 int ret; 294 295 mutex_lock(&kpad->gpio_lock); 296 297 kpad->dir[bank] &= ~bit; 298 ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]); 299 300 mutex_unlock(&kpad->gpio_lock); 301 302 return ret; 303 } 304 305 static int adp5588_gpio_direction_output(struct gpio_chip *chip, 306 unsigned off, int val) 307 { 308 struct adp5588_kpad *kpad = gpiochip_get_data(chip); 309 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); 310 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); 311 int ret; 312 313 mutex_lock(&kpad->gpio_lock); 314 315 kpad->dir[bank] |= bit; 316 317 if (val) 318 kpad->dat_out[bank] |= bit; 319 else 320 kpad->dat_out[bank] &= ~bit; 321 322 ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, 323 kpad->dat_out[bank]); 324 if (ret) 325 goto out_unlock; 326 327 ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, 328 kpad->dir[bank]); 329 330 out_unlock: 331 mutex_unlock(&kpad->gpio_lock); 332 333 return ret; 334 } 335 336 static int adp5588_build_gpiomap(struct adp5588_kpad *kpad) 337 { 338 bool pin_used[ADP5588_MAXGPIO]; 339 int n_unused = 0; 340 int i; 341 342 memset(pin_used, 0, sizeof(pin_used)); 343 344 for (i = 0; i < kpad->rows; i++) 345 pin_used[i] = true; 346 347 for (i = 0; i < kpad->cols; i++) 348 pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true; 349 350 for (i = 0; i < ADP5588_MAXGPIO; i++) 351 if (!pin_used[i]) 352 kpad->gpiomap[n_unused++] = i; 353 354 return n_unused; 355 } 356 357 static void adp5588_irq_bus_lock(struct irq_data *d) 358 { 359 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 360 struct adp5588_kpad *kpad = gpiochip_get_data(gc); 361 362 mutex_lock(&kpad->gpio_lock); 363 } 364 365 static void adp5588_irq_bus_sync_unlock(struct irq_data *d) 366 { 367 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 368 struct adp5588_kpad *kpad = gpiochip_get_data(gc); 369 int i; 370 371 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) { 372 if (kpad->int_en[i] ^ kpad->irq_mask[i]) { 373 kpad->int_en[i] = kpad->irq_mask[i]; 374 adp5588_write(kpad->client, GPI_EM1 + i, kpad->int_en[i]); 375 } 376 } 377 378 mutex_unlock(&kpad->gpio_lock); 379 } 380 381 static void adp5588_irq_mask(struct irq_data *d) 382 { 383 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 384 struct adp5588_kpad *kpad = gpiochip_get_data(gc); 385 irq_hw_number_t hwirq = irqd_to_hwirq(d); 386 unsigned long real_irq = kpad->gpiomap[hwirq]; 387 388 kpad->irq_mask[ADP5588_BANK(real_irq)] &= ~ADP5588_BIT(real_irq); 389 gpiochip_disable_irq(gc, hwirq); 390 } 391 392 static void adp5588_irq_unmask(struct irq_data *d) 393 { 394 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 395 struct adp5588_kpad *kpad = gpiochip_get_data(gc); 396 irq_hw_number_t hwirq = irqd_to_hwirq(d); 397 unsigned long real_irq = kpad->gpiomap[hwirq]; 398 399 gpiochip_enable_irq(gc, hwirq); 400 kpad->irq_mask[ADP5588_BANK(real_irq)] |= ADP5588_BIT(real_irq); 401 } 402 403 static int adp5588_irq_set_type(struct irq_data *d, unsigned int type) 404 { 405 if (!(type & IRQ_TYPE_EDGE_BOTH)) 406 return -EINVAL; 407 408 irq_set_handler_locked(d, handle_edge_irq); 409 410 return 0; 411 } 412 413 static const struct irq_chip adp5588_irq_chip = { 414 .name = "adp5588", 415 .irq_mask = adp5588_irq_mask, 416 .irq_unmask = adp5588_irq_unmask, 417 .irq_bus_lock = adp5588_irq_bus_lock, 418 .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock, 419 .irq_set_type = adp5588_irq_set_type, 420 .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_IMMUTABLE, 421 GPIOCHIP_IRQ_RESOURCE_HELPERS, 422 }; 423 424 static int adp5588_gpio_add(struct adp5588_kpad *kpad) 425 { 426 struct device *dev = &kpad->client->dev; 427 struct gpio_irq_chip *girq; 428 int i, error; 429 430 kpad->gc.ngpio = adp5588_build_gpiomap(kpad); 431 if (kpad->gc.ngpio == 0) { 432 dev_info(dev, "No unused gpios left to export\n"); 433 return 0; 434 } 435 436 kpad->gc.parent = &kpad->client->dev; 437 kpad->gc.direction_input = adp5588_gpio_direction_input; 438 kpad->gc.direction_output = adp5588_gpio_direction_output; 439 kpad->gc.get = adp5588_gpio_get_value; 440 kpad->gc.set = adp5588_gpio_set_value; 441 kpad->gc.set_config = adp5588_gpio_set_config; 442 kpad->gc.can_sleep = 1; 443 444 kpad->gc.base = -1; 445 kpad->gc.label = kpad->client->name; 446 kpad->gc.owner = THIS_MODULE; 447 448 girq = &kpad->gc.irq; 449 gpio_irq_chip_set_chip(girq, &adp5588_irq_chip); 450 girq->handler = handle_bad_irq; 451 girq->threaded = true; 452 453 mutex_init(&kpad->gpio_lock); 454 455 error = devm_gpiochip_add_data(dev, &kpad->gc, kpad); 456 if (error) { 457 dev_err(dev, "gpiochip_add failed: %d\n", error); 458 return error; 459 } 460 461 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) { 462 kpad->dat_out[i] = adp5588_read(kpad->client, 463 GPIO_DAT_OUT1 + i); 464 kpad->dir[i] = adp5588_read(kpad->client, GPIO_DIR1 + i); 465 kpad->pull_dis[i] = adp5588_read(kpad->client, GPIO_PULL1 + i); 466 } 467 468 return 0; 469 } 470 471 static unsigned long adp5588_gpiomap_get_hwirq(struct device *dev, 472 const u8 *map, unsigned int gpio, 473 unsigned int ngpios) 474 { 475 unsigned int hwirq; 476 477 for (hwirq = 0; hwirq < ngpios; hwirq++) 478 if (map[hwirq] == gpio) 479 return hwirq; 480 481 /* should never happen */ 482 dev_warn_ratelimited(dev, "could not find the hwirq for gpio(%u)\n", gpio); 483 484 return ADP5588_INVALID_HWIRQ; 485 } 486 487 static void adp5588_gpio_irq_handle(struct adp5588_kpad *kpad, int key_val, 488 int key_press) 489 { 490 unsigned int irq, gpio = key_val - GPI_PIN_BASE, irq_type; 491 struct i2c_client *client = kpad->client; 492 struct irq_data *irqd; 493 unsigned long hwirq; 494 495 hwirq = adp5588_gpiomap_get_hwirq(&client->dev, kpad->gpiomap, 496 gpio, kpad->gc.ngpio); 497 if (hwirq == ADP5588_INVALID_HWIRQ) { 498 dev_err(&client->dev, "Could not get hwirq for key(%u)\n", key_val); 499 return; 500 } 501 502 irq = irq_find_mapping(kpad->gc.irq.domain, hwirq); 503 if (!irq) 504 return; 505 506 irqd = irq_get_irq_data(irq); 507 if (!irqd) { 508 dev_err(&client->dev, "Could not get irq(%u) data\n", irq); 509 return; 510 } 511 512 irq_type = irqd_get_trigger_type(irqd); 513 514 /* 515 * Default is active low which means key_press is asserted on 516 * the falling edge. 517 */ 518 if ((irq_type & IRQ_TYPE_EDGE_RISING && !key_press) || 519 (irq_type & IRQ_TYPE_EDGE_FALLING && key_press)) 520 handle_nested_irq(irq); 521 } 522 523 static void adp5588_report_events(struct adp5588_kpad *kpad, int ev_cnt) 524 { 525 int i; 526 527 for (i = 0; i < ev_cnt; i++) { 528 int key = adp5588_read(kpad->client, Key_EVENTA + i); 529 int key_val = key & KEY_EV_MASK; 530 int key_press = key & KEY_EV_PRESSED; 531 532 if (key_val >= GPI_PIN_BASE && key_val <= GPI_PIN_END) { 533 /* gpio line used as IRQ source */ 534 adp5588_gpio_irq_handle(kpad, key_val, key_press); 535 } else { 536 int row = (key_val - 1) / ADP5588_COLS_MAX; 537 int col = (key_val - 1) % ADP5588_COLS_MAX; 538 int code = MATRIX_SCAN_CODE(row, col, kpad->row_shift); 539 540 dev_dbg_ratelimited(&kpad->client->dev, 541 "report key(%d) r(%d) c(%d) code(%d)\n", 542 key_val, row, col, kpad->keycode[code]); 543 544 input_report_key(kpad->input, 545 kpad->keycode[code], key_press); 546 } 547 } 548 } 549 550 static irqreturn_t adp5588_hard_irq(int irq, void *handle) 551 { 552 struct adp5588_kpad *kpad = handle; 553 554 kpad->irq_time = ktime_get(); 555 556 return IRQ_WAKE_THREAD; 557 } 558 559 static irqreturn_t adp5588_thread_irq(int irq, void *handle) 560 { 561 struct adp5588_kpad *kpad = handle; 562 struct i2c_client *client = kpad->client; 563 ktime_t target_time, now; 564 unsigned long delay; 565 int status, ev_cnt; 566 567 /* 568 * Readout needs to wait for at least 25ms after the notification 569 * for REVID < 4. 570 */ 571 if (kpad->delay) { 572 target_time = ktime_add_ms(kpad->irq_time, kpad->delay); 573 now = ktime_get(); 574 if (ktime_before(now, target_time)) { 575 delay = ktime_to_us(ktime_sub(target_time, now)); 576 usleep_range(delay, delay + 1000); 577 } 578 } 579 580 status = adp5588_read(client, INT_STAT); 581 582 if (status & ADP5588_OVR_FLOW_INT) /* Unlikely and should never happen */ 583 dev_err(&client->dev, "Event Overflow Error\n"); 584 585 if (status & ADP5588_KE_INT) { 586 ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & ADP5588_KEC; 587 if (ev_cnt) { 588 adp5588_report_events(kpad, ev_cnt); 589 input_sync(kpad->input); 590 } 591 } 592 593 adp5588_write(client, INT_STAT, status); /* Status is W1C */ 594 595 return IRQ_HANDLED; 596 } 597 598 static int adp5588_setup(struct adp5588_kpad *kpad) 599 { 600 struct i2c_client *client = kpad->client; 601 int i, ret; 602 603 ret = adp5588_write(client, KP_GPIO1, KP_SEL(kpad->rows)); 604 if (ret) 605 return ret; 606 607 ret = adp5588_write(client, KP_GPIO2, KP_SEL(kpad->cols) & 0xFF); 608 if (ret) 609 return ret; 610 611 ret = adp5588_write(client, KP_GPIO3, KP_SEL(kpad->cols) >> 8); 612 if (ret) 613 return ret; 614 615 for (i = 0; i < kpad->nkeys_unlock; i++) { 616 ret = adp5588_write(client, UNLOCK1 + i, kpad->unlock_keys[i]); 617 if (ret) 618 return ret; 619 } 620 621 if (kpad->nkeys_unlock) { 622 ret = adp5588_write(client, KEY_LCK_EC_STAT, ADP5588_K_LCK_EN); 623 if (ret) 624 return ret; 625 } 626 627 for (i = 0; i < KEYP_MAX_EVENT; i++) { 628 ret = adp5588_read(client, Key_EVENTA); 629 if (ret) 630 return ret; 631 } 632 633 ret = adp5588_write(client, INT_STAT, 634 ADP5588_CMP2_INT | ADP5588_CMP1_INT | 635 ADP5588_OVR_FLOW_INT | ADP5588_K_LCK_INT | 636 ADP5588_GPI_INT | ADP5588_KE_INT); /* Status is W1C */ 637 if (ret) 638 return ret; 639 640 return adp5588_write(client, CFG, ADP5588_INT_CFG | 641 ADP5588_OVR_FLOW_IEN | 642 ADP5588_KE_IEN); 643 } 644 645 static int adp5588_fw_parse(struct adp5588_kpad *kpad) 646 { 647 struct i2c_client *client = kpad->client; 648 int ret, i; 649 650 ret = matrix_keypad_parse_properties(&client->dev, &kpad->rows, 651 &kpad->cols); 652 if (ret) 653 return ret; 654 655 if (kpad->rows > ADP5588_ROWS_MAX || kpad->cols > ADP5588_COLS_MAX) { 656 dev_err(&client->dev, "Invalid nr of rows(%u) or cols(%u)\n", 657 kpad->rows, kpad->cols); 658 return -EINVAL; 659 } 660 661 ret = matrix_keypad_build_keymap(NULL, NULL, kpad->rows, kpad->cols, 662 kpad->keycode, kpad->input); 663 if (ret) 664 return ret; 665 666 kpad->row_shift = get_count_order(kpad->cols); 667 668 if (device_property_read_bool(&client->dev, "autorepeat")) 669 __set_bit(EV_REP, kpad->input->evbit); 670 671 kpad->nkeys_unlock = device_property_count_u32(&client->dev, 672 "adi,unlock-keys"); 673 if (kpad->nkeys_unlock <= 0) { 674 /* so that we don't end up enabling key lock */ 675 kpad->nkeys_unlock = 0; 676 return 0; 677 } 678 679 if (kpad->nkeys_unlock > ARRAY_SIZE(kpad->unlock_keys)) { 680 dev_err(&client->dev, "number of unlock keys(%d) > (%zu)\n", 681 kpad->nkeys_unlock, ARRAY_SIZE(kpad->unlock_keys)); 682 return -EINVAL; 683 } 684 685 ret = device_property_read_u32_array(&client->dev, "adi,unlock-keys", 686 kpad->unlock_keys, 687 kpad->nkeys_unlock); 688 if (ret) 689 return ret; 690 691 for (i = 0; i < kpad->nkeys_unlock; i++) { 692 /* 693 * Even though it should be possible (as stated in the datasheet) 694 * to use GPIs (which are part of the keys event) as unlock keys, 695 * it was not working at all and was leading to overflow events 696 * at some point. Hence, for now, let's just allow keys which are 697 * part of keypad matrix to be used and if a reliable way of 698 * using GPIs is found, this condition can be removed/lightened. 699 */ 700 if (kpad->unlock_keys[i] >= kpad->cols * kpad->rows) { 701 dev_err(&client->dev, "Invalid unlock key(%d)\n", 702 kpad->unlock_keys[i]); 703 return -EINVAL; 704 } 705 706 /* 707 * Firmware properties keys start from 0 but on the device they 708 * start from 1. 709 */ 710 kpad->unlock_keys[i] += 1; 711 } 712 713 return 0; 714 } 715 716 static int adp5588_probe(struct i2c_client *client, 717 const struct i2c_device_id *id) 718 { 719 struct adp5588_kpad *kpad; 720 struct input_dev *input; 721 unsigned int revid; 722 int ret; 723 int error; 724 725 if (!i2c_check_functionality(client->adapter, 726 I2C_FUNC_SMBUS_BYTE_DATA)) { 727 dev_err(&client->dev, "SMBUS Byte Data not Supported\n"); 728 return -EIO; 729 } 730 731 kpad = devm_kzalloc(&client->dev, sizeof(*kpad), GFP_KERNEL); 732 if (!kpad) 733 return -ENOMEM; 734 735 input = devm_input_allocate_device(&client->dev); 736 if (!input) 737 return -ENOMEM; 738 739 kpad->client = client; 740 kpad->input = input; 741 742 error = adp5588_fw_parse(kpad); 743 if (error) 744 return error; 745 746 ret = adp5588_read(client, DEV_ID); 747 if (ret < 0) 748 return ret; 749 750 revid = (u8) ret & ADP5588_DEVICE_ID_MASK; 751 if (WA_DELAYED_READOUT_REVID(revid)) 752 kpad->delay = msecs_to_jiffies(WA_DELAYED_READOUT_TIME); 753 754 input->name = client->name; 755 input->phys = "adp5588-keys/input0"; 756 757 input_set_drvdata(input, kpad); 758 759 input->id.bustype = BUS_I2C; 760 input->id.vendor = 0x0001; 761 input->id.product = 0x0001; 762 input->id.version = revid; 763 764 error = input_register_device(input); 765 if (error) { 766 dev_err(&client->dev, "unable to register input device: %d\n", 767 error); 768 return error; 769 } 770 771 error = adp5588_setup(kpad); 772 if (error) 773 return error; 774 775 error = adp5588_gpio_add(kpad); 776 if (error) 777 return error; 778 779 error = devm_request_threaded_irq(&client->dev, client->irq, 780 adp5588_hard_irq, adp5588_thread_irq, 781 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 782 client->dev.driver->name, kpad); 783 if (error) { 784 dev_err(&client->dev, "failed to request irq %d: %d\n", 785 client->irq, error); 786 return error; 787 } 788 789 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq); 790 return 0; 791 } 792 793 static void adp5588_remove(struct i2c_client *client) 794 { 795 adp5588_write(client, CFG, 0); 796 797 /* all resources will be freed by devm */ 798 } 799 800 static int __maybe_unused adp5588_suspend(struct device *dev) 801 { 802 struct i2c_client *client = to_i2c_client(dev); 803 804 disable_irq(client->irq); 805 806 return 0; 807 } 808 809 static int __maybe_unused adp5588_resume(struct device *dev) 810 { 811 struct i2c_client *client = to_i2c_client(dev); 812 813 enable_irq(client->irq); 814 815 return 0; 816 } 817 818 static SIMPLE_DEV_PM_OPS(adp5588_dev_pm_ops, adp5588_suspend, adp5588_resume); 819 820 static const struct i2c_device_id adp5588_id[] = { 821 { "adp5588-keys", 0 }, 822 { "adp5587-keys", 0 }, 823 { } 824 }; 825 MODULE_DEVICE_TABLE(i2c, adp5588_id); 826 827 static const struct of_device_id adp5588_of_match[] = { 828 { .compatible = "adi,adp5588" }, 829 { .compatible = "adi,adp5587" }, 830 {} 831 }; 832 MODULE_DEVICE_TABLE(of, adp5588_of_match); 833 834 static struct i2c_driver adp5588_driver = { 835 .driver = { 836 .name = KBUILD_MODNAME, 837 .of_match_table = adp5588_of_match, 838 .pm = &adp5588_dev_pm_ops, 839 }, 840 .probe = adp5588_probe, 841 .remove = adp5588_remove, 842 .id_table = adp5588_id, 843 }; 844 845 module_i2c_driver(adp5588_driver); 846 847 MODULE_LICENSE("GPL"); 848 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); 849 MODULE_DESCRIPTION("ADP5588/87 Keypad driver"); 850