blob: 71eb0411e37938118de6895d7fcebc549ebc4252 [file] [log] [blame]
David Brownell9d834062009-08-25 19:24:14 -07001/*
2 * twl4030_keypad.c - driver for 8x8 keypad controller in twl4030 chips
3 *
4 * Copyright (C) 2007 Texas Instruments, Inc.
5 * Copyright (C) 2008 Nokia Corporation
6 *
7 * Code re-written for 2430SDP by:
8 * Syed Mohammed Khasim <x0khasim@ti.com>
9 *
10 * Initial Code:
11 * Manjunatha G K <manjugk@ti.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/interrupt.h>
32#include <linux/input.h>
33#include <linux/platform_device.h>
Santosh Shilimkarb07682b2009-12-13 20:05:51 +010034#include <linux/i2c/twl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Sebastian Reichel7abf38d2014-01-04 00:41:03 -080036#include <linux/of.h>
David Brownell9d834062009-08-25 19:24:14 -070037
David Brownell9d834062009-08-25 19:24:14 -070038/*
39 * The TWL4030 family chips include a keypad controller that supports
40 * up to an 8x8 switch matrix. The controller can issue system wakeup
41 * events, since it uses only the always-on 32KiHz oscillator, and has
42 * an internal state machine that decodes pressed keys, including
43 * multi-key combinations.
44 *
45 * This driver lets boards define what keycodes they wish to report for
46 * which scancodes, as part of the "struct twl4030_keypad_data" used in
47 * the probe() routine.
48 *
49 * See the TPS65950 documentation; that's the general availability
50 * version of the TWL5030 second generation part.
51 */
52#define TWL4030_MAX_ROWS 8 /* TWL4030 hard limit */
53#define TWL4030_MAX_COLS 8
Dmitry Torokhov3fea6022010-07-20 20:25:35 -070054/*
55 * Note that we add space for an extra column so that we can handle
56 * row lines connected to the gnd (see twl4030_col_xlate()).
57 */
58#define TWL4030_ROW_SHIFT 4
59#define TWL4030_KEYMAP_SIZE (TWL4030_MAX_ROWS << TWL4030_ROW_SHIFT)
David Brownell9d834062009-08-25 19:24:14 -070060
61struct twl4030_keypad {
62 unsigned short keymap[TWL4030_KEYMAP_SIZE];
63 u16 kp_state[TWL4030_MAX_ROWS];
Sebastian Reichel7abf38d2014-01-04 00:41:03 -080064 bool autorepeat;
David Brownell9d834062009-08-25 19:24:14 -070065 unsigned n_rows;
66 unsigned n_cols;
67 unsigned irq;
68
69 struct device *dbg_dev;
70 struct input_dev *input;
71};
72
73/*----------------------------------------------------------------------*/
74
75/* arbitrary prescaler value 0..7 */
76#define PTV_PRESCALER 4
77
78/* Register Offsets */
79#define KEYP_CTRL 0x00
80#define KEYP_DEB 0x01
81#define KEYP_LONG_KEY 0x02
82#define KEYP_LK_PTV 0x03
83#define KEYP_TIMEOUT_L 0x04
84#define KEYP_TIMEOUT_H 0x05
85#define KEYP_KBC 0x06
86#define KEYP_KBR 0x07
87#define KEYP_SMS 0x08
88#define KEYP_FULL_CODE_7_0 0x09 /* row 0 column status */
89#define KEYP_FULL_CODE_15_8 0x0a /* ... row 1 ... */
90#define KEYP_FULL_CODE_23_16 0x0b
91#define KEYP_FULL_CODE_31_24 0x0c
92#define KEYP_FULL_CODE_39_32 0x0d
93#define KEYP_FULL_CODE_47_40 0x0e
94#define KEYP_FULL_CODE_55_48 0x0f
95#define KEYP_FULL_CODE_63_56 0x10
96#define KEYP_ISR1 0x11
97#define KEYP_IMR1 0x12
98#define KEYP_ISR2 0x13
99#define KEYP_IMR2 0x14
100#define KEYP_SIR 0x15
101#define KEYP_EDR 0x16 /* edge triggers */
102#define KEYP_SIH_CTRL 0x17
103
104/* KEYP_CTRL_REG Fields */
105#define KEYP_CTRL_SOFT_NRST BIT(0)
106#define KEYP_CTRL_SOFTMODEN BIT(1)
107#define KEYP_CTRL_LK_EN BIT(2)
108#define KEYP_CTRL_TOE_EN BIT(3)
109#define KEYP_CTRL_TOLE_EN BIT(4)
110#define KEYP_CTRL_RP_EN BIT(5)
111#define KEYP_CTRL_KBD_ON BIT(6)
112
113/* KEYP_DEB, KEYP_LONG_KEY, KEYP_TIMEOUT_x*/
114#define KEYP_PERIOD_US(t, prescale) ((t) / (31 << (prescale + 1)) - 1)
115
116/* KEYP_LK_PTV_REG Fields */
117#define KEYP_LK_PTV_PTV_SHIFT 5
118
119/* KEYP_{IMR,ISR,SIR} Fields */
120#define KEYP_IMR1_MIS BIT(3)
121#define KEYP_IMR1_TO BIT(2)
122#define KEYP_IMR1_LK BIT(1)
123#define KEYP_IMR1_KP BIT(0)
124
125/* KEYP_EDR Fields */
126#define KEYP_EDR_KP_FALLING 0x01
127#define KEYP_EDR_KP_RISING 0x02
128#define KEYP_EDR_KP_BOTH 0x03
129#define KEYP_EDR_LK_FALLING 0x04
130#define KEYP_EDR_LK_RISING 0x08
131#define KEYP_EDR_TO_FALLING 0x10
132#define KEYP_EDR_TO_RISING 0x20
133#define KEYP_EDR_MIS_FALLING 0x40
134#define KEYP_EDR_MIS_RISING 0x80
135
136
137/*----------------------------------------------------------------------*/
138
139static int twl4030_kpread(struct twl4030_keypad *kp,
140 u8 *data, u32 reg, u8 num_bytes)
141{
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100142 int ret = twl_i2c_read(TWL4030_MODULE_KEYPAD, data, reg, num_bytes);
David Brownell9d834062009-08-25 19:24:14 -0700143
144 if (ret < 0)
145 dev_warn(kp->dbg_dev,
146 "Couldn't read TWL4030: %X - ret %d[%x]\n",
147 reg, ret, ret);
148
149 return ret;
150}
151
152static int twl4030_kpwrite_u8(struct twl4030_keypad *kp, u8 data, u32 reg)
153{
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100154 int ret = twl_i2c_write_u8(TWL4030_MODULE_KEYPAD, data, reg);
David Brownell9d834062009-08-25 19:24:14 -0700155
156 if (ret < 0)
157 dev_warn(kp->dbg_dev,
158 "Could not write TWL4030: %X - ret %d[%x]\n",
159 reg, ret, ret);
160
161 return ret;
162}
163
164static inline u16 twl4030_col_xlate(struct twl4030_keypad *kp, u8 col)
165{
166 /* If all bits in a row are active for all coloumns then
167 * we have that row line connected to gnd. Mark this
168 * key on as if it was on matrix position n_cols (ie
169 * one higher than the size of the matrix).
170 */
171 if (col == 0xFF)
172 return 1 << kp->n_cols;
173 else
174 return col & ((1 << kp->n_cols) - 1);
175}
176
177static int twl4030_read_kp_matrix_state(struct twl4030_keypad *kp, u16 *state)
178{
179 u8 new_state[TWL4030_MAX_ROWS];
180 int row;
181 int ret = twl4030_kpread(kp, new_state,
182 KEYP_FULL_CODE_7_0, kp->n_rows);
183 if (ret >= 0)
184 for (row = 0; row < kp->n_rows; row++)
185 state[row] = twl4030_col_xlate(kp, new_state[row]);
186
187 return ret;
188}
189
Dmitry Torokhov3fea6022010-07-20 20:25:35 -0700190static bool twl4030_is_in_ghost_state(struct twl4030_keypad *kp, u16 *key_state)
David Brownell9d834062009-08-25 19:24:14 -0700191{
192 int i;
193 u16 check = 0;
194
195 for (i = 0; i < kp->n_rows; i++) {
196 u16 col = key_state[i];
197
198 if ((col & check) && hweight16(col) > 1)
Dmitry Torokhov3fea6022010-07-20 20:25:35 -0700199 return true;
David Brownell9d834062009-08-25 19:24:14 -0700200
201 check |= col;
202 }
203
Dmitry Torokhov3fea6022010-07-20 20:25:35 -0700204 return false;
David Brownell9d834062009-08-25 19:24:14 -0700205}
206
207static void twl4030_kp_scan(struct twl4030_keypad *kp, bool release_all)
208{
209 struct input_dev *input = kp->input;
210 u16 new_state[TWL4030_MAX_ROWS];
211 int col, row;
212
213 if (release_all)
214 memset(new_state, 0, sizeof(new_state));
215 else {
216 /* check for any changes */
217 int ret = twl4030_read_kp_matrix_state(kp, new_state);
218
219 if (ret < 0) /* panic ... */
220 return;
221
222 if (twl4030_is_in_ghost_state(kp, new_state))
223 return;
224 }
225
226 /* check for changes and print those */
227 for (row = 0; row < kp->n_rows; row++) {
228 int changed = new_state[row] ^ kp->kp_state[row];
229
230 if (!changed)
231 continue;
232
Dmitry Torokhov3fea6022010-07-20 20:25:35 -0700233 /* Extra column handles "all gnd" rows */
234 for (col = 0; col < kp->n_cols + 1; col++) {
David Brownell9d834062009-08-25 19:24:14 -0700235 int code;
236
237 if (!(changed & (1 << col)))
238 continue;
239
240 dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col,
241 (new_state[row] & (1 << col)) ?
242 "press" : "release");
243
244 code = MATRIX_SCAN_CODE(row, col, TWL4030_ROW_SHIFT);
245 input_event(input, EV_MSC, MSC_SCAN, code);
246 input_report_key(input, kp->keymap[code],
247 new_state[row] & (1 << col));
248 }
249 kp->kp_state[row] = new_state[row];
250 }
251 input_sync(input);
252}
253
254/*
255 * Keypad interrupt handler
256 */
257static irqreturn_t do_kp_irq(int irq, void *_kp)
258{
259 struct twl4030_keypad *kp = _kp;
260 u8 reg;
261 int ret;
262
David Brownell9d834062009-08-25 19:24:14 -0700263 /* Read & Clear TWL4030 pending interrupt */
264 ret = twl4030_kpread(kp, &reg, KEYP_ISR1, 1);
265
266 /* Release all keys if I2C has gone bad or
267 * the KEYP has gone to idle state */
268 if (ret >= 0 && (reg & KEYP_IMR1_KP))
269 twl4030_kp_scan(kp, false);
270 else
271 twl4030_kp_scan(kp, true);
272
273 return IRQ_HANDLED;
274}
275
Bill Pemberton5298cc42012-11-23 21:38:25 -0800276static int twl4030_kp_program(struct twl4030_keypad *kp)
David Brownell9d834062009-08-25 19:24:14 -0700277{
278 u8 reg;
279 int i;
280
281 /* Enable controller, with hardware decoding but not autorepeat */
282 reg = KEYP_CTRL_SOFT_NRST | KEYP_CTRL_SOFTMODEN
283 | KEYP_CTRL_TOE_EN | KEYP_CTRL_KBD_ON;
284 if (twl4030_kpwrite_u8(kp, reg, KEYP_CTRL) < 0)
285 return -EIO;
286
287 /* NOTE: we could use sih_setup() here to package keypad
288 * event sources as four different IRQs ... but we don't.
289 */
290
291 /* Enable TO rising and KP rising and falling edge detection */
292 reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING;
293 if (twl4030_kpwrite_u8(kp, reg, KEYP_EDR) < 0)
294 return -EIO;
295
296 /* Set PTV prescaler Field */
297 reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT);
298 if (twl4030_kpwrite_u8(kp, reg, KEYP_LK_PTV) < 0)
299 return -EIO;
300
301 /* Set key debounce time to 20 ms */
302 i = KEYP_PERIOD_US(20000, PTV_PRESCALER);
303 if (twl4030_kpwrite_u8(kp, i, KEYP_DEB) < 0)
304 return -EIO;
305
Felipe Contreras4936f972012-02-01 09:12:23 -0800306 /* Set timeout period to 200 ms */
David Brownell9d834062009-08-25 19:24:14 -0700307 i = KEYP_PERIOD_US(200000, PTV_PRESCALER);
308 if (twl4030_kpwrite_u8(kp, (i & 0xFF), KEYP_TIMEOUT_L) < 0)
309 return -EIO;
310
311 if (twl4030_kpwrite_u8(kp, (i >> 8), KEYP_TIMEOUT_H) < 0)
312 return -EIO;
313
314 /*
315 * Enable Clear-on-Read; disable remembering events that fire
316 * after the IRQ but before our handler acks (reads) them,
317 */
318 reg = TWL4030_SIH_CTRL_COR_MASK | TWL4030_SIH_CTRL_PENDDIS_MASK;
319 if (twl4030_kpwrite_u8(kp, reg, KEYP_SIH_CTRL) < 0)
320 return -EIO;
321
322 /* initialize key state; irqs update it from here on */
323 if (twl4030_read_kp_matrix_state(kp, kp->kp_state) < 0)
324 return -EIO;
325
326 return 0;
327}
328
329/*
330 * Registers keypad device with input subsystem
331 * and configures TWL4030 keypad registers
332 */
Bill Pemberton5298cc42012-11-23 21:38:25 -0800333static int twl4030_kp_probe(struct platform_device *pdev)
David Brownell9d834062009-08-25 19:24:14 -0700334{
Jingoo Hanc838cb32013-12-05 19:21:10 -0800335 struct twl4030_keypad_data *pdata = dev_get_platdata(&pdev->dev);
Sebastian Reichel7abf38d2014-01-04 00:41:03 -0800336 const struct matrix_keymap_data *keymap_data = NULL;
David Brownell9d834062009-08-25 19:24:14 -0700337 struct twl4030_keypad *kp;
338 struct input_dev *input;
339 u8 reg;
340 int error;
341
Dmitry Torokhov049f1cb2014-01-04 00:52:48 -0800342 kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL);
343 if (!kp)
344 return -ENOMEM;
345
346 input = devm_input_allocate_device(&pdev->dev);
347 if (!input)
348 return -ENOMEM;
David Brownell9d834062009-08-25 19:24:14 -0700349
Sebastian Reichel7abf38d2014-01-04 00:41:03 -0800350 /* get the debug device */
351 kp->dbg_dev = &pdev->dev;
352 kp->input = input;
David Brownell9d834062009-08-25 19:24:14 -0700353
354 /* setup input device */
David Brownell9d834062009-08-25 19:24:14 -0700355 input->name = "TWL4030 Keypad";
356 input->phys = "twl4030_keypad/input0";
David Brownell9d834062009-08-25 19:24:14 -0700357
358 input->id.bustype = BUS_HOST;
359 input->id.vendor = 0x0001;
360 input->id.product = 0x0001;
361 input->id.version = 0x0003;
362
Sebastian Reichel7abf38d2014-01-04 00:41:03 -0800363 if (pdata) {
364 if (!pdata->rows || !pdata->cols || !pdata->keymap_data) {
365 dev_err(&pdev->dev, "Missing platform_data\n");
Dmitry Torokhov049f1cb2014-01-04 00:52:48 -0800366 return -EINVAL;
Sebastian Reichel7abf38d2014-01-04 00:41:03 -0800367 }
368
369 kp->n_rows = pdata->rows;
370 kp->n_cols = pdata->cols;
371 kp->autorepeat = pdata->rep;
372 keymap_data = pdata->keymap_data;
373 } else {
374 error = matrix_keypad_parse_of_params(&pdev->dev, &kp->n_rows,
375 &kp->n_cols);
376 if (error)
Dmitry Torokhov049f1cb2014-01-04 00:52:48 -0800377 return error;
Sebastian Reichel7abf38d2014-01-04 00:41:03 -0800378
379 kp->autorepeat = true;
380 }
381
382 if (kp->n_rows > TWL4030_MAX_ROWS || kp->n_cols > TWL4030_MAX_COLS) {
383 dev_err(&pdev->dev,
384 "Invalid rows/cols amount specified in platform/devicetree data\n");
Dmitry Torokhov049f1cb2014-01-04 00:52:48 -0800385 return -EINVAL;
Sebastian Reichel7abf38d2014-01-04 00:41:03 -0800386 }
387
388 kp->irq = platform_get_irq(pdev, 0);
389 if (!kp->irq) {
390 dev_err(&pdev->dev, "no keyboard irq assigned\n");
Dmitry Torokhov049f1cb2014-01-04 00:52:48 -0800391 return -EINVAL;
Sebastian Reichel7abf38d2014-01-04 00:41:03 -0800392 }
393
Dmitry Torokhov19328112012-05-10 22:37:08 -0700394 error = matrix_keypad_build_keymap(keymap_data, NULL,
395 TWL4030_MAX_ROWS,
396 1 << TWL4030_ROW_SHIFT,
397 kp->keymap, input);
398 if (error) {
399 dev_err(kp->dbg_dev, "Failed to build keymap\n");
Dmitry Torokhov049f1cb2014-01-04 00:52:48 -0800400 return error;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700401 }
David Brownell9d834062009-08-25 19:24:14 -0700402
Dmitry Torokhov19328112012-05-10 22:37:08 -0700403 input_set_capability(input, EV_MSC, MSC_SCAN);
404 /* Enable auto repeat feature of Linux input subsystem */
Sebastian Reichel7abf38d2014-01-04 00:41:03 -0800405 if (kp->autorepeat)
Dmitry Torokhov19328112012-05-10 22:37:08 -0700406 __set_bit(EV_REP, input->evbit);
David Brownell9d834062009-08-25 19:24:14 -0700407
408 error = input_register_device(input);
409 if (error) {
410 dev_err(kp->dbg_dev,
411 "Unable to register twl4030 keypad device\n");
Dmitry Torokhov049f1cb2014-01-04 00:52:48 -0800412 return error;
David Brownell9d834062009-08-25 19:24:14 -0700413 }
414
415 error = twl4030_kp_program(kp);
416 if (error)
Dmitry Torokhov049f1cb2014-01-04 00:52:48 -0800417 return error;
David Brownell9d834062009-08-25 19:24:14 -0700418
419 /*
420 * This ISR will always execute in kernel thread context because of
421 * the need to access the TWL4030 over the I2C bus.
422 *
423 * NOTE: we assume this host is wired to TWL4040 INT1, not INT2 ...
424 */
Dmitry Torokhov049f1cb2014-01-04 00:52:48 -0800425 error = devm_request_threaded_irq(&pdev->dev, kp->irq, NULL, do_kp_irq,
426 0, pdev->name, kp);
David Brownell9d834062009-08-25 19:24:14 -0700427 if (error) {
Dmitry Torokhov049f1cb2014-01-04 00:52:48 -0800428 dev_info(kp->dbg_dev, "request_irq failed for irq no=%d: %d\n",
429 kp->irq, error);
430 return error;
David Brownell9d834062009-08-25 19:24:14 -0700431 }
432
433 /* Enable KP and TO interrupts now. */
434 reg = (u8) ~(KEYP_IMR1_KP | KEYP_IMR1_TO);
435 if (twl4030_kpwrite_u8(kp, reg, KEYP_IMR1)) {
Dmitry Torokhov049f1cb2014-01-04 00:52:48 -0800436 /* mask all events - we don't care about the result */
437 (void) twl4030_kpwrite_u8(kp, 0xff, KEYP_IMR1);
438 return -EIO;
David Brownell9d834062009-08-25 19:24:14 -0700439 }
440
441 platform_set_drvdata(pdev, kp);
442 return 0;
David Brownell9d834062009-08-25 19:24:14 -0700443}
444
Sebastian Reichel7abf38d2014-01-04 00:41:03 -0800445#ifdef CONFIG_OF
446static const struct of_device_id twl4030_keypad_dt_match_table[] = {
447 { .compatible = "ti,twl4030-keypad" },
448 {},
449};
450MODULE_DEVICE_TABLE(of, twl4030_keypad_dt_match_table);
451#endif
452
David Brownell9d834062009-08-25 19:24:14 -0700453/*
454 * NOTE: twl4030 are multi-function devices connected via I2C.
455 * So this device is a child of an I2C parent, thus it needs to
456 * support unplug/replug (which most platform devices don't).
457 */
458
459static struct platform_driver twl4030_kp_driver = {
460 .probe = twl4030_kp_probe,
David Brownell9d834062009-08-25 19:24:14 -0700461 .driver = {
462 .name = "twl4030_keypad",
463 .owner = THIS_MODULE,
Sebastian Reichel7abf38d2014-01-04 00:41:03 -0800464 .of_match_table = of_match_ptr(twl4030_keypad_dt_match_table),
David Brownell9d834062009-08-25 19:24:14 -0700465 },
466};
JJ Ding5146c842011-11-29 11:08:39 -0800467module_platform_driver(twl4030_kp_driver);
David Brownell9d834062009-08-25 19:24:14 -0700468
469MODULE_AUTHOR("Texas Instruments");
470MODULE_DESCRIPTION("TWL4030 Keypad Driver");
471MODULE_LICENSE("GPL");
472MODULE_ALIAS("platform:twl4030_keypad");