drm/client: Fix drm client endless Kconfig loop
[drm/drm-misc.git] / drivers / input / keyboard / twl4030_keypad.c
blob77e0743a3cf855152fa082dfe18c610a71c105a5
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * twl4030_keypad.c - driver for 8x8 keypad controller in twl4030 chips
5 * Copyright (C) 2007 Texas Instruments, Inc.
6 * Copyright (C) 2008 Nokia Corporation
8 * Code re-written for 2430SDP by:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
11 * Initial Code:
12 * Manjunatha G K <manjugk@ti.com>
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>
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.
31 * This driver lets boards define what keycodes they wish to report for
32 * which scancodes, as part of the "struct twl4030_keypad_data" used in
33 * the probe() routine.
35 * See the TPS65950 documentation; that's the general availability
36 * version of the TWL5030 second generation part.
38 #define TWL4030_MAX_ROWS 8 /* TWL4030 hard limit */
39 #define TWL4030_MAX_COLS 8
41 * Note that we add space for an extra column so that we can handle
42 * row lines connected to the gnd (see twl4030_col_xlate()).
44 #define TWL4030_ROW_SHIFT 4
45 #define TWL4030_KEYMAP_SIZE (TWL4030_MAX_ROWS << TWL4030_ROW_SHIFT)
47 struct twl4030_keypad {
48 unsigned short keymap[TWL4030_KEYMAP_SIZE];
49 u16 kp_state[TWL4030_MAX_ROWS];
50 bool autorepeat;
51 unsigned int n_rows;
52 unsigned int n_cols;
53 int irq;
55 struct device *dbg_dev;
56 struct input_dev *input;
59 /*----------------------------------------------------------------------*/
61 /* arbitrary prescaler value 0..7 */
62 #define PTV_PRESCALER 4
64 /* Register Offsets */
65 #define KEYP_CTRL 0x00
66 #define KEYP_DEB 0x01
67 #define KEYP_LONG_KEY 0x02
68 #define KEYP_LK_PTV 0x03
69 #define KEYP_TIMEOUT_L 0x04
70 #define KEYP_TIMEOUT_H 0x05
71 #define KEYP_KBC 0x06
72 #define KEYP_KBR 0x07
73 #define KEYP_SMS 0x08
74 #define KEYP_FULL_CODE_7_0 0x09 /* row 0 column status */
75 #define KEYP_FULL_CODE_15_8 0x0a /* ... row 1 ... */
76 #define KEYP_FULL_CODE_23_16 0x0b
77 #define KEYP_FULL_CODE_31_24 0x0c
78 #define KEYP_FULL_CODE_39_32 0x0d
79 #define KEYP_FULL_CODE_47_40 0x0e
80 #define KEYP_FULL_CODE_55_48 0x0f
81 #define KEYP_FULL_CODE_63_56 0x10
82 #define KEYP_ISR1 0x11
83 #define KEYP_IMR1 0x12
84 #define KEYP_ISR2 0x13
85 #define KEYP_IMR2 0x14
86 #define KEYP_SIR 0x15
87 #define KEYP_EDR 0x16 /* edge triggers */
88 #define KEYP_SIH_CTRL 0x17
90 /* KEYP_CTRL_REG Fields */
91 #define KEYP_CTRL_SOFT_NRST BIT(0)
92 #define KEYP_CTRL_SOFTMODEN BIT(1)
93 #define KEYP_CTRL_LK_EN BIT(2)
94 #define KEYP_CTRL_TOE_EN BIT(3)
95 #define KEYP_CTRL_TOLE_EN BIT(4)
96 #define KEYP_CTRL_RP_EN BIT(5)
97 #define KEYP_CTRL_KBD_ON BIT(6)
99 /* KEYP_DEB, KEYP_LONG_KEY, KEYP_TIMEOUT_x*/
100 #define KEYP_PERIOD_US(t, prescale) ((t) / (31 << ((prescale) + 1)) - 1)
102 /* KEYP_LK_PTV_REG Fields */
103 #define KEYP_LK_PTV_PTV_SHIFT 5
105 /* KEYP_{IMR,ISR,SIR} Fields */
106 #define KEYP_IMR1_MIS BIT(3)
107 #define KEYP_IMR1_TO BIT(2)
108 #define KEYP_IMR1_LK BIT(1)
109 #define KEYP_IMR1_KP BIT(0)
111 /* KEYP_EDR Fields */
112 #define KEYP_EDR_KP_FALLING 0x01
113 #define KEYP_EDR_KP_RISING 0x02
114 #define KEYP_EDR_KP_BOTH 0x03
115 #define KEYP_EDR_LK_FALLING 0x04
116 #define KEYP_EDR_LK_RISING 0x08
117 #define KEYP_EDR_TO_FALLING 0x10
118 #define KEYP_EDR_TO_RISING 0x20
119 #define KEYP_EDR_MIS_FALLING 0x40
120 #define KEYP_EDR_MIS_RISING 0x80
123 /*----------------------------------------------------------------------*/
125 static int twl4030_kpread(struct twl4030_keypad *kp,
126 u8 *data, u32 reg, u8 num_bytes)
128 int ret = twl_i2c_read(TWL4030_MODULE_KEYPAD, data, reg, num_bytes);
130 if (ret < 0)
131 dev_warn(kp->dbg_dev,
132 "Couldn't read TWL4030: %X - ret %d[%x]\n",
133 reg, ret, ret);
135 return ret;
138 static int twl4030_kpwrite_u8(struct twl4030_keypad *kp, u8 data, u32 reg)
140 int ret = twl_i2c_write_u8(TWL4030_MODULE_KEYPAD, data, reg);
142 if (ret < 0)
143 dev_warn(kp->dbg_dev,
144 "Could not write TWL4030: %X - ret %d[%x]\n",
145 reg, ret, ret);
147 return ret;
150 static inline u16 twl4030_col_xlate(struct twl4030_keypad *kp, u8 col)
153 * If all bits in a row are active for all columns then
154 * we have that row line connected to gnd. Mark this
155 * key on as if it was on matrix position n_cols (i.e.
156 * one higher than the size of the matrix).
158 if (col == 0xFF)
159 return 1 << kp->n_cols;
160 else
161 return col & ((1 << kp->n_cols) - 1);
164 static int twl4030_read_kp_matrix_state(struct twl4030_keypad *kp, u16 *state)
166 u8 new_state[TWL4030_MAX_ROWS];
167 int row;
168 int ret = twl4030_kpread(kp, new_state,
169 KEYP_FULL_CODE_7_0, kp->n_rows);
170 if (ret >= 0)
171 for (row = 0; row < kp->n_rows; row++)
172 state[row] = twl4030_col_xlate(kp, new_state[row]);
174 return ret;
177 static bool twl4030_is_in_ghost_state(struct twl4030_keypad *kp, u16 *key_state)
179 int i;
180 u16 check = 0;
182 for (i = 0; i < kp->n_rows; i++) {
183 u16 col = key_state[i];
185 if ((col & check) && hweight16(col) > 1)
186 return true;
188 check |= col;
191 return false;
194 static void twl4030_kp_scan(struct twl4030_keypad *kp, bool release_all)
196 struct input_dev *input = kp->input;
197 u16 new_state[TWL4030_MAX_ROWS];
198 int col, row;
200 if (release_all) {
201 memset(new_state, 0, sizeof(new_state));
202 } else {
203 /* check for any changes */
204 int ret = twl4030_read_kp_matrix_state(kp, new_state);
206 if (ret < 0) /* panic ... */
207 return;
209 if (twl4030_is_in_ghost_state(kp, new_state))
210 return;
213 /* check for changes and print those */
214 for (row = 0; row < kp->n_rows; row++) {
215 int changed = new_state[row] ^ kp->kp_state[row];
217 if (!changed)
218 continue;
220 /* Extra column handles "all gnd" rows */
221 for (col = 0; col < kp->n_cols + 1; col++) {
222 int code;
224 if (!(changed & (1 << col)))
225 continue;
227 dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col,
228 (new_state[row] & (1 << col)) ?
229 "press" : "release");
231 code = MATRIX_SCAN_CODE(row, col, TWL4030_ROW_SHIFT);
232 input_event(input, EV_MSC, MSC_SCAN, code);
233 input_report_key(input, kp->keymap[code],
234 new_state[row] & (1 << col));
236 kp->kp_state[row] = new_state[row];
238 input_sync(input);
242 * Keypad interrupt handler
244 static irqreturn_t do_kp_irq(int irq, void *_kp)
246 struct twl4030_keypad *kp = _kp;
247 u8 reg;
248 int ret;
250 /* Read & Clear TWL4030 pending interrupt */
251 ret = twl4030_kpread(kp, &reg, KEYP_ISR1, 1);
254 * Release all keys if I2C has gone bad or
255 * the KEYP has gone to idle state.
257 if (ret >= 0 && (reg & KEYP_IMR1_KP))
258 twl4030_kp_scan(kp, false);
259 else
260 twl4030_kp_scan(kp, true);
262 return IRQ_HANDLED;
265 static int twl4030_kp_program(struct twl4030_keypad *kp)
267 u8 reg;
268 int i;
270 /* Enable controller, with hardware decoding but not autorepeat */
271 reg = KEYP_CTRL_SOFT_NRST | KEYP_CTRL_SOFTMODEN
272 | KEYP_CTRL_TOE_EN | KEYP_CTRL_KBD_ON;
273 if (twl4030_kpwrite_u8(kp, reg, KEYP_CTRL) < 0)
274 return -EIO;
277 * NOTE: we could use sih_setup() here to package keypad
278 * event sources as four different IRQs ... but we don't.
281 /* Enable TO rising and KP rising and falling edge detection */
282 reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING;
283 if (twl4030_kpwrite_u8(kp, reg, KEYP_EDR) < 0)
284 return -EIO;
286 /* Set PTV prescaler Field */
287 reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT);
288 if (twl4030_kpwrite_u8(kp, reg, KEYP_LK_PTV) < 0)
289 return -EIO;
291 /* Set key debounce time to 20 ms */
292 i = KEYP_PERIOD_US(20000, PTV_PRESCALER);
293 if (twl4030_kpwrite_u8(kp, i, KEYP_DEB) < 0)
294 return -EIO;
296 /* Set timeout period to 200 ms */
297 i = KEYP_PERIOD_US(200000, PTV_PRESCALER);
298 if (twl4030_kpwrite_u8(kp, (i & 0xFF), KEYP_TIMEOUT_L) < 0)
299 return -EIO;
301 if (twl4030_kpwrite_u8(kp, (i >> 8), KEYP_TIMEOUT_H) < 0)
302 return -EIO;
305 * Enable Clear-on-Read; disable remembering events that fire
306 * after the IRQ but before our handler acks (reads) them.
308 reg = TWL4030_SIH_CTRL_COR_MASK | TWL4030_SIH_CTRL_PENDDIS_MASK;
309 if (twl4030_kpwrite_u8(kp, reg, KEYP_SIH_CTRL) < 0)
310 return -EIO;
312 /* initialize key state; irqs update it from here on */
313 if (twl4030_read_kp_matrix_state(kp, kp->kp_state) < 0)
314 return -EIO;
316 return 0;
320 * Registers keypad device with input subsystem
321 * and configures TWL4030 keypad registers
323 static int twl4030_kp_probe(struct platform_device *pdev)
325 struct twl4030_keypad_data *pdata = dev_get_platdata(&pdev->dev);
326 const struct matrix_keymap_data *keymap_data = NULL;
327 struct twl4030_keypad *kp;
328 struct input_dev *input;
329 u8 reg;
330 int error;
332 kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL);
333 if (!kp)
334 return -ENOMEM;
336 input = devm_input_allocate_device(&pdev->dev);
337 if (!input)
338 return -ENOMEM;
340 /* get the debug device */
341 kp->dbg_dev = &pdev->dev;
342 kp->input = input;
344 /* setup input device */
345 input->name = "TWL4030 Keypad";
346 input->phys = "twl4030_keypad/input0";
348 input->id.bustype = BUS_HOST;
349 input->id.vendor = 0x0001;
350 input->id.product = 0x0001;
351 input->id.version = 0x0003;
353 if (pdata) {
354 if (!pdata->rows || !pdata->cols || !pdata->keymap_data) {
355 dev_err(&pdev->dev, "Missing platform_data\n");
356 return -EINVAL;
359 kp->n_rows = pdata->rows;
360 kp->n_cols = pdata->cols;
361 kp->autorepeat = pdata->rep;
362 keymap_data = pdata->keymap_data;
363 } else {
364 error = matrix_keypad_parse_properties(&pdev->dev, &kp->n_rows,
365 &kp->n_cols);
366 if (error)
367 return error;
369 kp->autorepeat = true;
372 if (kp->n_rows > TWL4030_MAX_ROWS || kp->n_cols > TWL4030_MAX_COLS) {
373 dev_err(&pdev->dev,
374 "Invalid rows/cols amount specified in platform/devicetree data\n");
375 return -EINVAL;
378 kp->irq = platform_get_irq(pdev, 0);
379 if (kp->irq < 0)
380 return kp->irq;
382 error = matrix_keypad_build_keymap(keymap_data, NULL,
383 TWL4030_MAX_ROWS,
384 1 << TWL4030_ROW_SHIFT,
385 kp->keymap, input);
386 if (error) {
387 dev_err(kp->dbg_dev, "Failed to build keymap\n");
388 return error;
391 input_set_capability(input, EV_MSC, MSC_SCAN);
392 /* Enable auto repeat feature of Linux input subsystem */
393 if (kp->autorepeat)
394 __set_bit(EV_REP, input->evbit);
396 error = input_register_device(input);
397 if (error) {
398 dev_err(kp->dbg_dev,
399 "Unable to register twl4030 keypad device\n");
400 return error;
403 error = twl4030_kp_program(kp);
404 if (error)
405 return error;
408 * This ISR will always execute in kernel thread context because of
409 * the need to access the TWL4030 over the I2C bus.
411 * NOTE: we assume this host is wired to TWL4040 INT1, not INT2 ...
413 error = devm_request_threaded_irq(&pdev->dev, kp->irq, NULL, do_kp_irq,
414 0, pdev->name, kp);
415 if (error) {
416 dev_info(kp->dbg_dev, "request_irq failed for irq no=%d: %d\n",
417 kp->irq, error);
418 return error;
421 /* Enable KP and TO interrupts now. */
422 reg = (u8) ~(KEYP_IMR1_KP | KEYP_IMR1_TO);
423 if (twl4030_kpwrite_u8(kp, reg, KEYP_IMR1)) {
424 /* mask all events - we don't care about the result */
425 (void) twl4030_kpwrite_u8(kp, 0xff, KEYP_IMR1);
426 return -EIO;
429 return 0;
432 #ifdef CONFIG_OF
433 static const struct of_device_id twl4030_keypad_dt_match_table[] = {
434 { .compatible = "ti,twl4030-keypad" },
437 MODULE_DEVICE_TABLE(of, twl4030_keypad_dt_match_table);
438 #endif
441 * NOTE: twl4030 are multi-function devices connected via I2C.
442 * So this device is a child of an I2C parent, thus it needs to
443 * support unplug/replug (which most platform devices don't).
446 static struct platform_driver twl4030_kp_driver = {
447 .probe = twl4030_kp_probe,
448 .driver = {
449 .name = "twl4030_keypad",
450 .of_match_table = of_match_ptr(twl4030_keypad_dt_match_table),
453 module_platform_driver(twl4030_kp_driver);
455 MODULE_AUTHOR("Texas Instruments");
456 MODULE_DESCRIPTION("TWL4030 Keypad Driver");
457 MODULE_LICENSE("GPL");
458 MODULE_ALIAS("platform:twl4030_keypad");