Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * OMAP4 Keypad Driver
  3 *
  4 * Copyright (C) 2010 Texas Instruments
  5 *
  6 * Author: Abraham Arce <x0066660@ti.com>
  7 * Initial Code: Syed Rafiuddin <rafiuddin.syed@ti.com>
  8 *
  9 * This program is free software; you can redistribute it and/or modify
 10 * it under the terms of the GNU General Public License as published by
 11 * the Free Software Foundation; either version 2 of the License, or
 12 * (at your option) any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program; if not, write to the Free Software
 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 22 */
 23
 24#include <linux/module.h>
 25#include <linux/init.h>
 26#include <linux/interrupt.h>
 27#include <linux/platform_device.h>
 28#include <linux/errno.h>
 29#include <linux/io.h>
 
 30#include <linux/input.h>
 
 31#include <linux/slab.h>
 32#include <linux/pm_runtime.h>
 33
 34#include <plat/omap4-keypad.h>
 35
 36/* OMAP4 registers */
 37#define OMAP4_KBD_REVISION		0x00
 38#define OMAP4_KBD_SYSCONFIG		0x10
 39#define OMAP4_KBD_SYSSTATUS		0x14
 40#define OMAP4_KBD_IRQSTATUS		0x18
 41#define OMAP4_KBD_IRQENABLE		0x1C
 42#define OMAP4_KBD_WAKEUPENABLE		0x20
 43#define OMAP4_KBD_PENDING		0x24
 44#define OMAP4_KBD_CTRL			0x28
 45#define OMAP4_KBD_DEBOUNCINGTIME	0x2C
 46#define OMAP4_KBD_LONGKEYTIME		0x30
 47#define OMAP4_KBD_TIMEOUT		0x34
 48#define OMAP4_KBD_STATEMACHINE		0x38
 49#define OMAP4_KBD_ROWINPUTS		0x3C
 50#define OMAP4_KBD_COLUMNOUTPUTS		0x40
 51#define OMAP4_KBD_FULLCODE31_0		0x44
 52#define OMAP4_KBD_FULLCODE63_32		0x48
 53
 54/* OMAP4 bit definitions */
 55#define OMAP4_DEF_IRQENABLE_EVENTEN	(1 << 0)
 56#define OMAP4_DEF_IRQENABLE_LONGKEY	(1 << 1)
 57#define OMAP4_DEF_IRQENABLE_TIMEOUTEN	(1 << 2)
 58#define OMAP4_DEF_WUP_EVENT_ENA		(1 << 0)
 59#define OMAP4_DEF_WUP_LONG_KEY_ENA	(1 << 1)
 60#define OMAP4_DEF_CTRL_NOSOFTMODE	(1 << 1)
 61#define OMAP4_DEF_CTRLPTVVALUE		(1 << 2)
 62#define OMAP4_DEF_CTRLPTV		(1 << 1)
 63
 64/* OMAP4 values */
 65#define OMAP4_VAL_IRQDISABLE		0x00
 66#define OMAP4_VAL_DEBOUNCINGTIME	0x07
 67#define OMAP4_VAL_FUNCTIONALCFG		0x1E
 68
 69#define OMAP4_MASK_IRQSTATUSDISABLE	0xFFFF
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 70
 71struct omap4_keypad {
 72	struct input_dev *input;
 73
 74	void __iomem *base;
 75	int irq;
 
 76
 77	unsigned int rows;
 78	unsigned int cols;
 
 
 79	unsigned int row_shift;
 
 80	unsigned char key_state[8];
 81	unsigned short keymap[];
 82};
 83
 84/* Interrupt handler */
 85static irqreturn_t omap4_keypad_interrupt(int irq, void *dev_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 86{
 87	struct omap4_keypad *keypad_data = dev_id;
 88	struct input_dev *input_dev = keypad_data->input;
 89	unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)];
 90	unsigned int col, row, code, changed;
 91	u32 *new_state = (u32 *) key_state;
 92
 93	/* Disable interrupts */
 94	__raw_writel(OMAP4_VAL_IRQDISABLE,
 95		     keypad_data->base + OMAP4_KBD_IRQENABLE);
 96
 97	*new_state = __raw_readl(keypad_data->base + OMAP4_KBD_FULLCODE31_0);
 98	*(new_state + 1) = __raw_readl(keypad_data->base
 99						+ OMAP4_KBD_FULLCODE63_32);
100
101	for (row = 0; row < keypad_data->rows; row++) {
102		changed = key_state[row] ^ keypad_data->key_state[row];
103		if (!changed)
104			continue;
105
106		for (col = 0; col < keypad_data->cols; col++) {
107			if (changed & (1 << col)) {
108				code = MATRIX_SCAN_CODE(row, col,
109						keypad_data->row_shift);
110				input_event(input_dev, EV_MSC, MSC_SCAN, code);
111				input_report_key(input_dev,
112						 keypad_data->keymap[code],
113						 key_state[row] & (1 << col));
114			}
115		}
116	}
117
118	input_sync(input_dev);
119
120	memcpy(keypad_data->key_state, key_state,
121		sizeof(keypad_data->key_state));
122
123	/* clear pending interrupts */
124	__raw_writel(__raw_readl(keypad_data->base + OMAP4_KBD_IRQSTATUS),
125			keypad_data->base + OMAP4_KBD_IRQSTATUS);
126
127	/* enable interrupts */
128	__raw_writel(OMAP4_DEF_IRQENABLE_EVENTEN | OMAP4_DEF_IRQENABLE_LONGKEY,
129			keypad_data->base + OMAP4_KBD_IRQENABLE);
130
131	return IRQ_HANDLED;
132}
133
134static int omap4_keypad_open(struct input_dev *input)
135{
136	struct omap4_keypad *keypad_data = input_get_drvdata(input);
137
138	pm_runtime_get_sync(input->dev.parent);
139
140	disable_irq(keypad_data->irq);
141
142	__raw_writel(OMAP4_VAL_FUNCTIONALCFG,
143			keypad_data->base + OMAP4_KBD_CTRL);
144	__raw_writel(OMAP4_VAL_DEBOUNCINGTIME,
145			keypad_data->base + OMAP4_KBD_DEBOUNCINGTIME);
146	__raw_writel(OMAP4_VAL_IRQDISABLE,
147			keypad_data->base + OMAP4_KBD_IRQSTATUS);
148	__raw_writel(OMAP4_DEF_IRQENABLE_EVENTEN | OMAP4_DEF_IRQENABLE_LONGKEY,
149			keypad_data->base + OMAP4_KBD_IRQENABLE);
150	__raw_writel(OMAP4_DEF_WUP_EVENT_ENA | OMAP4_DEF_WUP_LONG_KEY_ENA,
151			keypad_data->base + OMAP4_KBD_WAKEUPENABLE);
 
 
 
152
153	enable_irq(keypad_data->irq);
154
155	return 0;
156}
157
158static void omap4_keypad_close(struct input_dev *input)
159{
160	struct omap4_keypad *keypad_data = input_get_drvdata(input);
161
162	disable_irq(keypad_data->irq);
163
164	/* Disable interrupts */
165	__raw_writel(OMAP4_VAL_IRQDISABLE,
166		     keypad_data->base + OMAP4_KBD_IRQENABLE);
 
167
168	/* clear pending interrupts */
169	__raw_writel(__raw_readl(keypad_data->base + OMAP4_KBD_IRQSTATUS),
170			keypad_data->base + OMAP4_KBD_IRQSTATUS);
171
172	enable_irq(keypad_data->irq);
173
174	pm_runtime_put_sync(input->dev.parent);
175}
176
177static int __devinit omap4_keypad_probe(struct platform_device *pdev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178{
179	const struct omap4_keypad_platform_data *pdata;
180	struct omap4_keypad *keypad_data;
181	struct input_dev *input_dev;
182	struct resource *res;
183	resource_size_t size;
184	unsigned int row_shift, max_keys;
185	int irq;
186	int error;
187
188	/* platform data */
189	pdata = pdev->dev.platform_data;
190	if (!pdata) {
191		dev_err(&pdev->dev, "no platform data defined\n");
192		return -EINVAL;
193	}
194
195	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
196	if (!res) {
197		dev_err(&pdev->dev, "no base address specified\n");
198		return -EINVAL;
199	}
200
201	irq = platform_get_irq(pdev, 0);
202	if (!irq) {
203		dev_err(&pdev->dev, "no keyboard irq assigned\n");
204		return -EINVAL;
205	}
206
207	if (!pdata->keymap_data) {
208		dev_err(&pdev->dev, "no keymap data defined\n");
209		return -EINVAL;
210	}
211
212	row_shift = get_count_order(pdata->cols);
213	max_keys = pdata->rows << row_shift;
214
215	keypad_data = kzalloc(sizeof(struct omap4_keypad) +
216				max_keys * sizeof(keypad_data->keymap[0]),
217			      GFP_KERNEL);
218	if (!keypad_data) {
219		dev_err(&pdev->dev, "keypad_data memory allocation failed\n");
220		return -ENOMEM;
221	}
222
223	size = resource_size(res);
224
225	res = request_mem_region(res->start, size, pdev->name);
 
 
 
 
226	if (!res) {
227		dev_err(&pdev->dev, "can't request mem region\n");
228		error = -EBUSY;
229		goto err_free_keypad;
230	}
231
232	keypad_data->base = ioremap(res->start, resource_size(res));
233	if (!keypad_data->base) {
234		dev_err(&pdev->dev, "can't ioremap mem resource\n");
235		error = -ENOMEM;
236		goto err_release_mem;
237	}
238
239	keypad_data->irq = irq;
240	keypad_data->row_shift = row_shift;
241	keypad_data->rows = pdata->rows;
242	keypad_data->cols = pdata->cols;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
244	/* input device allocation */
245	keypad_data->input = input_dev = input_allocate_device();
246	if (!input_dev) {
247		error = -ENOMEM;
248		goto err_unmap;
249	}
250
251	input_dev->name = pdev->name;
252	input_dev->dev.parent = &pdev->dev;
253	input_dev->id.bustype = BUS_HOST;
254	input_dev->id.vendor = 0x0001;
255	input_dev->id.product = 0x0001;
256	input_dev->id.version = 0x0001;
257
258	input_dev->open = omap4_keypad_open;
259	input_dev->close = omap4_keypad_close;
260
261	input_dev->keycode	= keypad_data->keymap;
262	input_dev->keycodesize	= sizeof(keypad_data->keymap[0]);
263	input_dev->keycodemax	= max_keys;
264
265	__set_bit(EV_KEY, input_dev->evbit);
266	__set_bit(EV_REP, input_dev->evbit);
267
268	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
 
 
269
270	input_set_drvdata(input_dev, keypad_data);
271
272	matrix_keypad_build_keymap(pdata->keymap_data, row_shift,
273			input_dev->keycode, input_dev->keybit);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
275	error = request_irq(keypad_data->irq, omap4_keypad_interrupt,
276			     IRQF_TRIGGER_RISING,
277			     "omap4-keypad", keypad_data);
278	if (error) {
279		dev_err(&pdev->dev, "failed to register interrupt\n");
280		goto err_free_input;
281	}
282
283	pm_runtime_enable(&pdev->dev);
 
284
285	error = input_register_device(keypad_data->input);
286	if (error < 0) {
287		dev_err(&pdev->dev, "failed to register input device\n");
288		goto err_pm_disable;
289	}
290
291	platform_set_drvdata(pdev, keypad_data);
292	return 0;
293
294err_pm_disable:
295	pm_runtime_disable(&pdev->dev);
296	free_irq(keypad_data->irq, keypad_data);
 
 
297err_free_input:
298	input_free_device(input_dev);
 
 
299err_unmap:
300	iounmap(keypad_data->base);
301err_release_mem:
302	release_mem_region(res->start, size);
303err_free_keypad:
304	kfree(keypad_data);
305	return error;
306}
307
308static int __devexit omap4_keypad_remove(struct platform_device *pdev)
309{
310	struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
311	struct resource *res;
312
313	free_irq(keypad_data->irq, keypad_data);
314
315	pm_runtime_disable(&pdev->dev);
316
317	input_unregister_device(keypad_data->input);
318
319	iounmap(keypad_data->base);
320
321	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
322	release_mem_region(res->start, resource_size(res));
323
 
324	kfree(keypad_data);
325	platform_set_drvdata(pdev, NULL);
326
327	return 0;
328}
329
330static struct platform_driver omap4_keypad_driver = {
331	.probe		= omap4_keypad_probe,
332	.remove		= __devexit_p(omap4_keypad_remove),
333	.driver		= {
334		.name	= "omap4-keypad",
335		.owner	= THIS_MODULE,
336	},
337};
 
338
339static int __init omap4_keypad_init(void)
 
340{
341	return platform_driver_register(&omap4_keypad_driver);
 
 
 
 
 
 
 
 
 
 
342}
343module_init(omap4_keypad_init);
344
345static void __exit omap4_keypad_exit(void)
346{
347	platform_driver_unregister(&omap4_keypad_driver);
 
 
 
 
 
 
 
 
348}
349module_exit(omap4_keypad_exit);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
350
351MODULE_AUTHOR("Texas Instruments");
352MODULE_DESCRIPTION("OMAP4 Keypad Driver");
353MODULE_LICENSE("GPL");
354MODULE_ALIAS("platform:omap4-keypad");
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * OMAP4 Keypad Driver
  4 *
  5 * Copyright (C) 2010 Texas Instruments
  6 *
  7 * Author: Abraham Arce <x0066660@ti.com>
  8 * Initial Code: Syed Rafiuddin <rafiuddin.syed@ti.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  9 */
 10
 11#include <linux/module.h>
 
 12#include <linux/interrupt.h>
 13#include <linux/platform_device.h>
 14#include <linux/errno.h>
 15#include <linux/io.h>
 16#include <linux/of.h>
 17#include <linux/input.h>
 18#include <linux/input/matrix_keypad.h>
 19#include <linux/slab.h>
 20#include <linux/pm_runtime.h>
 21
 
 
 22/* OMAP4 registers */
 23#define OMAP4_KBD_REVISION		0x00
 24#define OMAP4_KBD_SYSCONFIG		0x10
 25#define OMAP4_KBD_SYSSTATUS		0x14
 26#define OMAP4_KBD_IRQSTATUS		0x18
 27#define OMAP4_KBD_IRQENABLE		0x1C
 28#define OMAP4_KBD_WAKEUPENABLE		0x20
 29#define OMAP4_KBD_PENDING		0x24
 30#define OMAP4_KBD_CTRL			0x28
 31#define OMAP4_KBD_DEBOUNCINGTIME	0x2C
 32#define OMAP4_KBD_LONGKEYTIME		0x30
 33#define OMAP4_KBD_TIMEOUT		0x34
 34#define OMAP4_KBD_STATEMACHINE		0x38
 35#define OMAP4_KBD_ROWINPUTS		0x3C
 36#define OMAP4_KBD_COLUMNOUTPUTS		0x40
 37#define OMAP4_KBD_FULLCODE31_0		0x44
 38#define OMAP4_KBD_FULLCODE63_32		0x48
 39
 40/* OMAP4 bit definitions */
 41#define OMAP4_DEF_IRQENABLE_EVENTEN	BIT(0)
 42#define OMAP4_DEF_IRQENABLE_LONGKEY	BIT(1)
 43#define OMAP4_DEF_WUP_EVENT_ENA		BIT(0)
 44#define OMAP4_DEF_WUP_LONG_KEY_ENA	BIT(1)
 45#define OMAP4_DEF_CTRL_NOSOFTMODE	BIT(1)
 46#define OMAP4_DEF_CTRL_PTV_SHIFT	2
 
 
 47
 48/* OMAP4 values */
 49#define OMAP4_VAL_IRQDISABLE		0x0
 
 
 50
 51/*
 52 * Errata i689: If a key is released for a time shorter than debounce time,
 53 * the keyboard will idle and never detect the key release. The workaround
 54 * is to use at least a 12ms debounce time. See omap5432 TRM chapter
 55 * "26.4.6.2 Keyboard Controller Timer" for more information.
 56 */
 57#define OMAP4_KEYPAD_PTV_DIV_128        0x6
 58#define OMAP4_KEYPAD_DEBOUNCINGTIME_MS(dbms, ptv)     \
 59	((((dbms) * 1000) / ((1 << ((ptv) + 1)) * (1000000 / 32768))) - 1)
 60#define OMAP4_VAL_DEBOUNCINGTIME_16MS					\
 61	OMAP4_KEYPAD_DEBOUNCINGTIME_MS(16, OMAP4_KEYPAD_PTV_DIV_128)
 62
 63enum {
 64	KBD_REVISION_OMAP4 = 0,
 65	KBD_REVISION_OMAP5,
 66};
 67
 68struct omap4_keypad {
 69	struct input_dev *input;
 70
 71	void __iomem *base;
 72	bool irq_wake_enabled;
 73	unsigned int irq;
 74
 75	unsigned int rows;
 76	unsigned int cols;
 77	u32 reg_offset;
 78	u32 irqreg_offset;
 79	unsigned int row_shift;
 80	bool no_autorepeat;
 81	unsigned char key_state[8];
 82	unsigned short *keymap;
 83};
 84
 85static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset)
 86{
 87	return __raw_readl(keypad_data->base +
 88				keypad_data->reg_offset + offset);
 89}
 90
 91static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value)
 92{
 93	__raw_writel(value,
 94		     keypad_data->base + keypad_data->reg_offset + offset);
 95}
 96
 97static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset)
 98{
 99	return __raw_readl(keypad_data->base +
100				keypad_data->irqreg_offset + offset);
101}
102
103static void kbd_write_irqreg(struct omap4_keypad *keypad_data,
104			     u32 offset, u32 value)
105{
106	__raw_writel(value,
107		     keypad_data->base + keypad_data->irqreg_offset + offset);
108}
109
110
111/* Interrupt handlers */
112static irqreturn_t omap4_keypad_irq_handler(int irq, void *dev_id)
113{
114	struct omap4_keypad *keypad_data = dev_id;
115
116	if (kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS))
117		return IRQ_WAKE_THREAD;
118
119	return IRQ_NONE;
120}
121
122static irqreturn_t omap4_keypad_irq_thread_fn(int irq, void *dev_id)
123{
124	struct omap4_keypad *keypad_data = dev_id;
125	struct input_dev *input_dev = keypad_data->input;
126	unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)];
127	unsigned int col, row, code, changed;
128	u32 *new_state = (u32 *) key_state;
129
130	*new_state = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0);
131	*(new_state + 1) = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32);
 
 
 
 
 
132
133	for (row = 0; row < keypad_data->rows; row++) {
134		changed = key_state[row] ^ keypad_data->key_state[row];
135		if (!changed)
136			continue;
137
138		for (col = 0; col < keypad_data->cols; col++) {
139			if (changed & (1 << col)) {
140				code = MATRIX_SCAN_CODE(row, col,
141						keypad_data->row_shift);
142				input_event(input_dev, EV_MSC, MSC_SCAN, code);
143				input_report_key(input_dev,
144						 keypad_data->keymap[code],
145						 key_state[row] & (1 << col));
146			}
147		}
148	}
149
150	input_sync(input_dev);
151
152	memcpy(keypad_data->key_state, key_state,
153		sizeof(keypad_data->key_state));
154
155	/* clear pending interrupts */
156	kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
157			 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
 
 
 
 
158
159	return IRQ_HANDLED;
160}
161
162static int omap4_keypad_open(struct input_dev *input)
163{
164	struct omap4_keypad *keypad_data = input_get_drvdata(input);
165
166	pm_runtime_get_sync(input->dev.parent);
167
168	disable_irq(keypad_data->irq);
169
170	kbd_writel(keypad_data, OMAP4_KBD_CTRL,
171			OMAP4_DEF_CTRL_NOSOFTMODE |
172			(OMAP4_KEYPAD_PTV_DIV_128 << OMAP4_DEF_CTRL_PTV_SHIFT));
173	kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME,
174			OMAP4_VAL_DEBOUNCINGTIME_16MS);
175	/* clear pending interrupts */
176	kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
177			 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
178	kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
179			OMAP4_DEF_IRQENABLE_EVENTEN |
180				OMAP4_DEF_IRQENABLE_LONGKEY);
181	kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE,
182			OMAP4_DEF_WUP_EVENT_ENA | OMAP4_DEF_WUP_LONG_KEY_ENA);
183
184	enable_irq(keypad_data->irq);
185
186	return 0;
187}
188
189static void omap4_keypad_close(struct input_dev *input)
190{
191	struct omap4_keypad *keypad_data = input_get_drvdata(input);
192
193	disable_irq(keypad_data->irq);
194
195	/* Disable interrupts and wake-up events */
196	kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
197			 OMAP4_VAL_IRQDISABLE);
198	kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE, 0);
199
200	/* clear pending interrupts */
201	kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
202			 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
203
204	enable_irq(keypad_data->irq);
205
206	pm_runtime_put_sync(input->dev.parent);
207}
208
209static int omap4_keypad_parse_dt(struct device *dev,
210				 struct omap4_keypad *keypad_data)
211{
212	struct device_node *np = dev->of_node;
213	int err;
214
215	err = matrix_keypad_parse_properties(dev, &keypad_data->rows,
216					     &keypad_data->cols);
217	if (err)
218		return err;
219
220	if (of_get_property(np, "linux,input-no-autorepeat", NULL))
221		keypad_data->no_autorepeat = true;
222
223	return 0;
224}
225
226static int omap4_keypad_probe(struct platform_device *pdev)
227{
 
228	struct omap4_keypad *keypad_data;
229	struct input_dev *input_dev;
230	struct resource *res;
231	unsigned int max_keys;
232	int rev;
233	int irq;
234	int error;
235
 
 
 
 
 
 
 
236	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
237	if (!res) {
238		dev_err(&pdev->dev, "no base address specified\n");
239		return -EINVAL;
240	}
241
242	irq = platform_get_irq(pdev, 0);
243	if (!irq) {
244		dev_err(&pdev->dev, "no keyboard irq assigned\n");
245		return -EINVAL;
246	}
247
248	keypad_data = kzalloc(sizeof(struct omap4_keypad), GFP_KERNEL);
 
 
 
 
 
 
 
 
 
 
249	if (!keypad_data) {
250		dev_err(&pdev->dev, "keypad_data memory allocation failed\n");
251		return -ENOMEM;
252	}
253
254	keypad_data->irq = irq;
255
256	error = omap4_keypad_parse_dt(&pdev->dev, keypad_data);
257	if (error)
258		goto err_free_keypad;
259
260	res = request_mem_region(res->start, resource_size(res), pdev->name);
261	if (!res) {
262		dev_err(&pdev->dev, "can't request mem region\n");
263		error = -EBUSY;
264		goto err_free_keypad;
265	}
266
267	keypad_data->base = ioremap(res->start, resource_size(res));
268	if (!keypad_data->base) {
269		dev_err(&pdev->dev, "can't ioremap mem resource\n");
270		error = -ENOMEM;
271		goto err_release_mem;
272	}
273
274
275	/*
276	 * Enable clocks for the keypad module so that we can read
277	 * revision register.
278	 */
279	pm_runtime_enable(&pdev->dev);
280	error = pm_runtime_get_sync(&pdev->dev);
281	if (error) {
282		dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
283		goto err_unmap;
284	}
285	rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
286	rev &= 0x03 << 30;
287	rev >>= 30;
288	switch (rev) {
289	case KBD_REVISION_OMAP4:
290		keypad_data->reg_offset = 0x00;
291		keypad_data->irqreg_offset = 0x00;
292		break;
293	case KBD_REVISION_OMAP5:
294		keypad_data->reg_offset = 0x10;
295		keypad_data->irqreg_offset = 0x0c;
296		break;
297	default:
298		dev_err(&pdev->dev,
299			"Keypad reports unsupported revision %d", rev);
300		error = -EINVAL;
301		goto err_pm_put_sync;
302	}
303
304	/* input device allocation */
305	keypad_data->input = input_dev = input_allocate_device();
306	if (!input_dev) {
307		error = -ENOMEM;
308		goto err_pm_put_sync;
309	}
310
311	input_dev->name = pdev->name;
312	input_dev->dev.parent = &pdev->dev;
313	input_dev->id.bustype = BUS_HOST;
314	input_dev->id.vendor = 0x0001;
315	input_dev->id.product = 0x0001;
316	input_dev->id.version = 0x0001;
317
318	input_dev->open = omap4_keypad_open;
319	input_dev->close = omap4_keypad_close;
320
 
 
 
 
 
 
 
321	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
322	if (!keypad_data->no_autorepeat)
323		__set_bit(EV_REP, input_dev->evbit);
324
325	input_set_drvdata(input_dev, keypad_data);
326
327	keypad_data->row_shift = get_count_order(keypad_data->cols);
328	max_keys = keypad_data->rows << keypad_data->row_shift;
329	keypad_data->keymap = kcalloc(max_keys,
330				      sizeof(keypad_data->keymap[0]),
331				      GFP_KERNEL);
332	if (!keypad_data->keymap) {
333		dev_err(&pdev->dev, "Not enough memory for keymap\n");
334		error = -ENOMEM;
335		goto err_free_input;
336	}
337
338	error = matrix_keypad_build_keymap(NULL, NULL,
339					   keypad_data->rows, keypad_data->cols,
340					   keypad_data->keymap, input_dev);
341	if (error) {
342		dev_err(&pdev->dev, "failed to build keymap\n");
343		goto err_free_keymap;
344	}
345
346	error = request_threaded_irq(keypad_data->irq, omap4_keypad_irq_handler,
347				     omap4_keypad_irq_thread_fn, IRQF_ONESHOT,
348				     "omap4-keypad", keypad_data);
349	if (error) {
350		dev_err(&pdev->dev, "failed to register interrupt\n");
351		goto err_free_keymap;
352	}
353
354	device_init_wakeup(&pdev->dev, true);
355	pm_runtime_put_sync(&pdev->dev);
356
357	error = input_register_device(keypad_data->input);
358	if (error < 0) {
359		dev_err(&pdev->dev, "failed to register input device\n");
360		goto err_pm_disable;
361	}
362
363	platform_set_drvdata(pdev, keypad_data);
364	return 0;
365
366err_pm_disable:
367	pm_runtime_disable(&pdev->dev);
368	free_irq(keypad_data->irq, keypad_data);
369err_free_keymap:
370	kfree(keypad_data->keymap);
371err_free_input:
372	input_free_device(input_dev);
373err_pm_put_sync:
374	pm_runtime_put_sync(&pdev->dev);
375err_unmap:
376	iounmap(keypad_data->base);
377err_release_mem:
378	release_mem_region(res->start, resource_size(res));
379err_free_keypad:
380	kfree(keypad_data);
381	return error;
382}
383
384static int omap4_keypad_remove(struct platform_device *pdev)
385{
386	struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
387	struct resource *res;
388
389	free_irq(keypad_data->irq, keypad_data);
390
391	pm_runtime_disable(&pdev->dev);
392
393	input_unregister_device(keypad_data->input);
394
395	iounmap(keypad_data->base);
396
397	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
398	release_mem_region(res->start, resource_size(res));
399
400	kfree(keypad_data->keymap);
401	kfree(keypad_data);
 
402
403	return 0;
404}
405
406static const struct of_device_id omap_keypad_dt_match[] = {
407	{ .compatible = "ti,omap4-keypad" },
408	{},
 
 
 
 
409};
410MODULE_DEVICE_TABLE(of, omap_keypad_dt_match);
411
412#ifdef CONFIG_PM_SLEEP
413static int omap4_keypad_suspend(struct device *dev)
414{
415	struct platform_device *pdev = to_platform_device(dev);
416	struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
417	int error;
418
419	if (device_may_wakeup(&pdev->dev)) {
420		error = enable_irq_wake(keypad_data->irq);
421		if (!error)
422			keypad_data->irq_wake_enabled = true;
423	}
424
425	return 0;
426}
 
427
428static int omap4_keypad_resume(struct device *dev)
429{
430	struct platform_device *pdev = to_platform_device(dev);
431	struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
432
433	if (device_may_wakeup(&pdev->dev) && keypad_data->irq_wake_enabled) {
434		disable_irq_wake(keypad_data->irq);
435		keypad_data->irq_wake_enabled = false;
436	}
437
438	return 0;
439}
440#endif
441
442static SIMPLE_DEV_PM_OPS(omap4_keypad_pm_ops,
443			 omap4_keypad_suspend, omap4_keypad_resume);
444
445static struct platform_driver omap4_keypad_driver = {
446	.probe		= omap4_keypad_probe,
447	.remove		= omap4_keypad_remove,
448	.driver		= {
449		.name	= "omap4-keypad",
450		.pm	= &omap4_keypad_pm_ops,
451		.of_match_table = omap_keypad_dt_match,
452	},
453};
454module_platform_driver(omap4_keypad_driver);
455
456MODULE_AUTHOR("Texas Instruments");
457MODULE_DESCRIPTION("OMAP4 Keypad Driver");
458MODULE_LICENSE("GPL");
459MODULE_ALIAS("platform:omap4-keypad");