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