Linux Audio

Check our new training course

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