1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * twl4030_keypad.c - driver for 8x8 keypad controller in twl4030 chips 4 * 5 * Copyright (C) 2007 Texas Instruments, Inc. 6 * Copyright (C) 2008 Nokia Corporation 7 * 8 * Code re-written for 2430SDP by: 9 * Syed Mohammed Khasim <x0khasim@ti.com> 10 * 11 * Initial Code: 12 * Manjunatha G K <manjugk@ti.com> 13 */ 14 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/interrupt.h> 18 #include <linux/input.h> 19 #include <linux/platform_device.h> 20 #include <linux/mfd/twl.h> 21 #include <linux/slab.h> 22 #include <linux/of.h> 23 24 /* 25 * The TWL4030 family chips include a keypad controller that supports 26 * up to an 8x8 switch matrix. The controller can issue system wakeup 27 * events, since it uses only the always-on 32KiHz oscillator, and has 28 * an internal state machine that decodes pressed keys, including 29 * multi-key combinations. 30 * 31 * See the TPS65950 documentation; that's the general availability 32 * version of the TWL5030 second generation part. 33 */ 34 #define TWL4030_MAX_ROWS 8 /* TWL4030 hard limit */ 35 #define TWL4030_MAX_COLS 8 36 /* 37 * Note that we add space for an extra column so that we can handle 38 * row lines connected to the gnd (see twl4030_col_xlate()). 39 */ 40 #define TWL4030_ROW_SHIFT 4 41 #define TWL4030_KEYMAP_SIZE (TWL4030_MAX_ROWS << TWL4030_ROW_SHIFT) 42 43 struct twl4030_keypad { 44 unsigned short keymap[TWL4030_KEYMAP_SIZE]; 45 u16 kp_state[TWL4030_MAX_ROWS]; 46 unsigned int n_rows; 47 unsigned int n_cols; 48 int irq; 49 50 struct device *dbg_dev; 51 struct input_dev *input; 52 }; 53 54 /*----------------------------------------------------------------------*/ 55 56 /* arbitrary prescaler value 0..7 */ 57 #define PTV_PRESCALER 4 58 59 /* Register Offsets */ 60 #define KEYP_CTRL 0x00 61 #define KEYP_DEB 0x01 62 #define KEYP_LONG_KEY 0x02 63 #define KEYP_LK_PTV 0x03 64 #define KEYP_TIMEOUT_L 0x04 65 #define KEYP_TIMEOUT_H 0x05 66 #define KEYP_KBC 0x06 67 #define KEYP_KBR 0x07 68 #define KEYP_SMS 0x08 69 #define KEYP_FULL_CODE_7_0 0x09 /* row 0 column status */ 70 #define KEYP_FULL_CODE_15_8 0x0a /* ... row 1 ... */ 71 #define KEYP_FULL_CODE_23_16 0x0b 72 #define KEYP_FULL_CODE_31_24 0x0c 73 #define KEYP_FULL_CODE_39_32 0x0d 74 #define KEYP_FULL_CODE_47_40 0x0e 75 #define KEYP_FULL_CODE_55_48 0x0f 76 #define KEYP_FULL_CODE_63_56 0x10 77 #define KEYP_ISR1 0x11 78 #define KEYP_IMR1 0x12 79 #define KEYP_ISR2 0x13 80 #define KEYP_IMR2 0x14 81 #define KEYP_SIR 0x15 82 #define KEYP_EDR 0x16 /* edge triggers */ 83 #define KEYP_SIH_CTRL 0x17 84 85 /* KEYP_CTRL_REG Fields */ 86 #define KEYP_CTRL_SOFT_NRST BIT(0) 87 #define KEYP_CTRL_SOFTMODEN BIT(1) 88 #define KEYP_CTRL_LK_EN BIT(2) 89 #define KEYP_CTRL_TOE_EN BIT(3) 90 #define KEYP_CTRL_TOLE_EN BIT(4) 91 #define KEYP_CTRL_RP_EN BIT(5) 92 #define KEYP_CTRL_KBD_ON BIT(6) 93 94 /* KEYP_DEB, KEYP_LONG_KEY, KEYP_TIMEOUT_x*/ 95 #define KEYP_PERIOD_US(t, prescale) ((t) / (31 << ((prescale) + 1)) - 1) 96 97 /* KEYP_LK_PTV_REG Fields */ 98 #define KEYP_LK_PTV_PTV_SHIFT 5 99 100 /* KEYP_{IMR,ISR,SIR} Fields */ 101 #define KEYP_IMR1_MIS BIT(3) 102 #define KEYP_IMR1_TO BIT(2) 103 #define KEYP_IMR1_LK BIT(1) 104 #define KEYP_IMR1_KP BIT(0) 105 106 /* KEYP_EDR Fields */ 107 #define KEYP_EDR_KP_FALLING 0x01 108 #define KEYP_EDR_KP_RISING 0x02 109 #define KEYP_EDR_KP_BOTH 0x03 110 #define KEYP_EDR_LK_FALLING 0x04 111 #define KEYP_EDR_LK_RISING 0x08 112 #define KEYP_EDR_TO_FALLING 0x10 113 #define KEYP_EDR_TO_RISING 0x20 114 #define KEYP_EDR_MIS_FALLING 0x40 115 #define KEYP_EDR_MIS_RISING 0x80 116 117 118 /*----------------------------------------------------------------------*/ 119 120 static int twl4030_kpread(struct twl4030_keypad *kp, 121 u8 *data, u32 reg, u8 num_bytes) 122 { 123 int ret = twl_i2c_read(TWL4030_MODULE_KEYPAD, data, reg, num_bytes); 124 125 if (ret < 0) 126 dev_warn(kp->dbg_dev, 127 "Couldn't read TWL4030: %X - ret %d[%x]\n", 128 reg, ret, ret); 129 130 return ret; 131 } 132 133 static int twl4030_kpwrite_u8(struct twl4030_keypad *kp, u8 data, u32 reg) 134 { 135 int ret = twl_i2c_write_u8(TWL4030_MODULE_KEYPAD, data, reg); 136 137 if (ret < 0) 138 dev_warn(kp->dbg_dev, 139 "Could not write TWL4030: %X - ret %d[%x]\n", 140 reg, ret, ret); 141 142 return ret; 143 } 144 145 static inline u16 twl4030_col_xlate(struct twl4030_keypad *kp, u8 col) 146 { 147 /* 148 * If all bits in a row are active for all columns then 149 * we have that row line connected to gnd. Mark this 150 * key on as if it was on matrix position n_cols (i.e. 151 * one higher than the size of the matrix). 152 */ 153 if (col == 0xFF) 154 return 1 << kp->n_cols; 155 else 156 return col & ((1 << kp->n_cols) - 1); 157 } 158 159 static int twl4030_read_kp_matrix_state(struct twl4030_keypad *kp, u16 *state) 160 { 161 u8 new_state[TWL4030_MAX_ROWS]; 162 int row; 163 int ret = twl4030_kpread(kp, new_state, 164 KEYP_FULL_CODE_7_0, kp->n_rows); 165 if (ret >= 0) 166 for (row = 0; row < kp->n_rows; row++) 167 state[row] = twl4030_col_xlate(kp, new_state[row]); 168 169 return ret; 170 } 171 172 static bool twl4030_is_in_ghost_state(struct twl4030_keypad *kp, u16 *key_state) 173 { 174 int i; 175 u16 check = 0; 176 177 for (i = 0; i < kp->n_rows; i++) { 178 u16 col = key_state[i]; 179 180 if ((col & check) && hweight16(col) > 1) 181 return true; 182 183 check |= col; 184 } 185 186 return false; 187 } 188 189 static void twl4030_kp_scan(struct twl4030_keypad *kp, bool release_all) 190 { 191 struct input_dev *input = kp->input; 192 u16 new_state[TWL4030_MAX_ROWS]; 193 int col, row; 194 195 if (release_all) { 196 memset(new_state, 0, sizeof(new_state)); 197 } else { 198 /* check for any changes */ 199 int ret = twl4030_read_kp_matrix_state(kp, new_state); 200 201 if (ret < 0) /* panic ... */ 202 return; 203 204 if (twl4030_is_in_ghost_state(kp, new_state)) 205 return; 206 } 207 208 /* check for changes and print those */ 209 for (row = 0; row < kp->n_rows; row++) { 210 int changed = new_state[row] ^ kp->kp_state[row]; 211 212 if (!changed) 213 continue; 214 215 /* Extra column handles "all gnd" rows */ 216 for (col = 0; col < kp->n_cols + 1; col++) { 217 int code; 218 219 if (!(changed & (1 << col))) 220 continue; 221 222 dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col, 223 (new_state[row] & (1 << col)) ? 224 "press" : "release"); 225 226 code = MATRIX_SCAN_CODE(row, col, TWL4030_ROW_SHIFT); 227 input_event(input, EV_MSC, MSC_SCAN, code); 228 input_report_key(input, kp->keymap[code], 229 new_state[row] & (1 << col)); 230 } 231 kp->kp_state[row] = new_state[row]; 232 } 233 input_sync(input); 234 } 235 236 /* 237 * Keypad interrupt handler 238 */ 239 static irqreturn_t do_kp_irq(int irq, void *_kp) 240 { 241 struct twl4030_keypad *kp = _kp; 242 u8 reg; 243 int ret; 244 245 /* Read & Clear TWL4030 pending interrupt */ 246 ret = twl4030_kpread(kp, ®, KEYP_ISR1, 1); 247 248 /* 249 * Release all keys if I2C has gone bad or 250 * the KEYP has gone to idle state. 251 */ 252 if (ret >= 0 && (reg & KEYP_IMR1_KP)) 253 twl4030_kp_scan(kp, false); 254 else 255 twl4030_kp_scan(kp, true); 256 257 return IRQ_HANDLED; 258 } 259 260 static int twl4030_kp_program(struct twl4030_keypad *kp) 261 { 262 u8 reg; 263 int i; 264 265 /* Enable controller, with hardware decoding but not autorepeat */ 266 reg = KEYP_CTRL_SOFT_NRST | KEYP_CTRL_SOFTMODEN 267 | KEYP_CTRL_TOE_EN | KEYP_CTRL_KBD_ON; 268 if (twl4030_kpwrite_u8(kp, reg, KEYP_CTRL) < 0) 269 return -EIO; 270 271 /* 272 * NOTE: we could use sih_setup() here to package keypad 273 * event sources as four different IRQs ... but we don't. 274 */ 275 276 /* Enable TO rising and KP rising and falling edge detection */ 277 reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING; 278 if (twl4030_kpwrite_u8(kp, reg, KEYP_EDR) < 0) 279 return -EIO; 280 281 /* Set PTV prescaler Field */ 282 reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT); 283 if (twl4030_kpwrite_u8(kp, reg, KEYP_LK_PTV) < 0) 284 return -EIO; 285 286 /* Set key debounce time to 20 ms */ 287 i = KEYP_PERIOD_US(20000, PTV_PRESCALER); 288 if (twl4030_kpwrite_u8(kp, i, KEYP_DEB) < 0) 289 return -EIO; 290 291 /* Set timeout period to 200 ms */ 292 i = KEYP_PERIOD_US(200000, PTV_PRESCALER); 293 if (twl4030_kpwrite_u8(kp, (i & 0xFF), KEYP_TIMEOUT_L) < 0) 294 return -EIO; 295 296 if (twl4030_kpwrite_u8(kp, (i >> 8), KEYP_TIMEOUT_H) < 0) 297 return -EIO; 298 299 /* 300 * Enable Clear-on-Read; disable remembering events that fire 301 * after the IRQ but before our handler acks (reads) them. 302 */ 303 reg = TWL4030_SIH_CTRL_COR_MASK | TWL4030_SIH_CTRL_PENDDIS_MASK; 304 if (twl4030_kpwrite_u8(kp, reg, KEYP_SIH_CTRL) < 0) 305 return -EIO; 306 307 /* initialize key state; irqs update it from here on */ 308 if (twl4030_read_kp_matrix_state(kp, kp->kp_state) < 0) 309 return -EIO; 310 311 return 0; 312 } 313 314 /* 315 * Registers keypad device with input subsystem 316 * and configures TWL4030 keypad registers 317 */ 318 static int twl4030_kp_probe(struct platform_device *pdev) 319 { 320 struct twl4030_keypad *kp; 321 struct input_dev *input; 322 u8 reg; 323 int error; 324 325 kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL); 326 if (!kp) 327 return -ENOMEM; 328 329 input = devm_input_allocate_device(&pdev->dev); 330 if (!input) 331 return -ENOMEM; 332 333 /* get the debug device */ 334 kp->dbg_dev = &pdev->dev; 335 kp->input = input; 336 337 /* setup input device */ 338 input->name = "TWL4030 Keypad"; 339 input->phys = "twl4030_keypad/input0"; 340 341 input->id.bustype = BUS_HOST; 342 input->id.vendor = 0x0001; 343 input->id.product = 0x0001; 344 input->id.version = 0x0003; 345 346 error = matrix_keypad_parse_properties(&pdev->dev, 347 &kp->n_rows, &kp->n_cols); 348 if (error) 349 return error; 350 351 if (kp->n_rows > TWL4030_MAX_ROWS || kp->n_cols > TWL4030_MAX_COLS) { 352 dev_err(&pdev->dev, 353 "Invalid rows/cols amount specified in platform/devicetree data\n"); 354 return -EINVAL; 355 } 356 357 kp->irq = platform_get_irq(pdev, 0); 358 if (kp->irq < 0) 359 return kp->irq; 360 361 error = matrix_keypad_build_keymap(NULL, NULL, 362 TWL4030_MAX_ROWS, 363 1 << TWL4030_ROW_SHIFT, 364 kp->keymap, input); 365 if (error) { 366 dev_err(kp->dbg_dev, "Failed to build keymap\n"); 367 return error; 368 } 369 370 input_set_capability(input, EV_MSC, MSC_SCAN); 371 __set_bit(EV_REP, input->evbit); 372 373 error = input_register_device(input); 374 if (error) { 375 dev_err(kp->dbg_dev, 376 "Unable to register twl4030 keypad device\n"); 377 return error; 378 } 379 380 error = twl4030_kp_program(kp); 381 if (error) 382 return error; 383 384 /* 385 * This ISR will always execute in kernel thread context because of 386 * the need to access the TWL4030 over the I2C bus. 387 * 388 * NOTE: we assume this host is wired to TWL4040 INT1, not INT2 ... 389 */ 390 error = devm_request_threaded_irq(&pdev->dev, kp->irq, NULL, do_kp_irq, 391 0, pdev->name, kp); 392 if (error) { 393 dev_info(kp->dbg_dev, "request_irq failed for irq no=%d: %d\n", 394 kp->irq, error); 395 return error; 396 } 397 398 /* Enable KP and TO interrupts now. */ 399 reg = (u8) ~(KEYP_IMR1_KP | KEYP_IMR1_TO); 400 if (twl4030_kpwrite_u8(kp, reg, KEYP_IMR1)) { 401 /* mask all events - we don't care about the result */ 402 (void) twl4030_kpwrite_u8(kp, 0xff, KEYP_IMR1); 403 return -EIO; 404 } 405 406 return 0; 407 } 408 409 #ifdef CONFIG_OF 410 static const struct of_device_id twl4030_keypad_dt_match_table[] = { 411 { .compatible = "ti,twl4030-keypad" }, 412 {}, 413 }; 414 MODULE_DEVICE_TABLE(of, twl4030_keypad_dt_match_table); 415 #endif 416 417 /* 418 * NOTE: twl4030 are multi-function devices connected via I2C. 419 * So this device is a child of an I2C parent, thus it needs to 420 * support unplug/replug (which most platform devices don't). 421 */ 422 423 static struct platform_driver twl4030_kp_driver = { 424 .probe = twl4030_kp_probe, 425 .driver = { 426 .name = "twl4030_keypad", 427 .of_match_table = of_match_ptr(twl4030_keypad_dt_match_table), 428 }, 429 }; 430 module_platform_driver(twl4030_kp_driver); 431 432 MODULE_AUTHOR("Texas Instruments"); 433 MODULE_DESCRIPTION("TWL4030 Keypad Driver"); 434 MODULE_LICENSE("GPL"); 435 MODULE_ALIAS("platform:twl4030_keypad"); 436