PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/input/keyboard/w90p910_keypad.c

https://bitbucket.org/ndreys/linux-sunxi
C | 282 lines | 211 code | 56 blank | 15 comment | 14 complexity | 44299c5be091a5f55e8054db6fc0afee MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1/*
  2 * Copyright (c) 2008-2009 Nuvoton technology corporation.
  3 *
  4 * Wan ZongShun <mcuos.com@gmail.com>
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation;version 2 of the License.
  9 *
 10 */
 11
 12#include <linux/kernel.h>
 13#include <linux/module.h>
 14#include <linux/init.h>
 15#include <linux/interrupt.h>
 16#include <linux/input.h>
 17#include <linux/device.h>
 18#include <linux/platform_device.h>
 19#include <linux/clk.h>
 20#include <linux/err.h>
 21#include <linux/io.h>
 22#include <linux/slab.h>
 23
 24#include <mach/w90p910_keypad.h>
 25
 26/* Keypad Interface Control Registers */
 27#define KPI_CONF		0x00
 28#define KPI_3KCONF		0x04
 29#define KPI_LPCONF		0x08
 30#define KPI_STATUS		0x0C
 31
 32#define IS1KEY			(0x01 << 16)
 33#define INTTR			(0x01 << 21)
 34#define KEY0R			(0x0f << 3)
 35#define KEY0C			0x07
 36#define DEBOUNCE_BIT		0x08
 37#define KSIZE0			(0x01 << 16)
 38#define KSIZE1			(0x01 << 17)
 39#define KPSEL			(0x01 << 19)
 40#define ENKP			(0x01 << 18)
 41
 42#define KGET_RAW(n)		(((n) & KEY0R) >> 3)
 43#define KGET_COLUMN(n)		((n) & KEY0C)
 44
 45#define W90P910_MAX_KEY_NUM	(8 * 8)
 46#define W90P910_ROW_SHIFT	3
 47
 48struct w90p910_keypad {
 49	const struct w90p910_keypad_platform_data *pdata;
 50	struct clk *clk;
 51	struct input_dev *input_dev;
 52	void __iomem *mmio_base;
 53	int irq;
 54	unsigned short keymap[W90P910_MAX_KEY_NUM];
 55};
 56
 57static void w90p910_keypad_scan_matrix(struct w90p910_keypad *keypad,
 58							unsigned int status)
 59{
 60	struct input_dev *input_dev = keypad->input_dev;
 61	unsigned int row = KGET_RAW(status);
 62	unsigned int col = KGET_COLUMN(status);
 63	unsigned int code = MATRIX_SCAN_CODE(row, col, W90P910_ROW_SHIFT);
 64	unsigned int key = keypad->keymap[code];
 65
 66	input_event(input_dev, EV_MSC, MSC_SCAN, code);
 67	input_report_key(input_dev, key, 1);
 68	input_sync(input_dev);
 69
 70	input_event(input_dev, EV_MSC, MSC_SCAN, code);
 71	input_report_key(input_dev, key, 0);
 72	input_sync(input_dev);
 73}
 74
 75static irqreturn_t w90p910_keypad_irq_handler(int irq, void *dev_id)
 76{
 77	struct w90p910_keypad *keypad = dev_id;
 78	unsigned int  kstatus, val;
 79
 80	kstatus = __raw_readl(keypad->mmio_base + KPI_STATUS);
 81
 82	val = INTTR | IS1KEY;
 83
 84	if (kstatus & val)
 85		w90p910_keypad_scan_matrix(keypad, kstatus);
 86
 87	return IRQ_HANDLED;
 88}
 89
 90static int w90p910_keypad_open(struct input_dev *dev)
 91{
 92	struct w90p910_keypad *keypad = input_get_drvdata(dev);
 93	const struct w90p910_keypad_platform_data *pdata = keypad->pdata;
 94	unsigned int val, config;
 95
 96	/* Enable unit clock */
 97	clk_enable(keypad->clk);
 98
 99	val = __raw_readl(keypad->mmio_base + KPI_CONF);
100	val |= (KPSEL | ENKP);
101	val &= ~(KSIZE0 | KSIZE1);
102
103	config = pdata->prescale | (pdata->debounce << DEBOUNCE_BIT);
104
105	val |= config;
106
107	__raw_writel(val, keypad->mmio_base + KPI_CONF);
108
109	return 0;
110}
111
112static void w90p910_keypad_close(struct input_dev *dev)
113{
114	struct w90p910_keypad *keypad = input_get_drvdata(dev);
115
116	/* Disable clock unit */
117	clk_disable(keypad->clk);
118}
119
120static int __devinit w90p910_keypad_probe(struct platform_device *pdev)
121{
122	const struct w90p910_keypad_platform_data *pdata =
123						pdev->dev.platform_data;
124	const struct matrix_keymap_data *keymap_data;
125	struct w90p910_keypad *keypad;
126	struct input_dev *input_dev;
127	struct resource *res;
128	int irq;
129	int error;
130
131	if (!pdata) {
132		dev_err(&pdev->dev, "no platform data defined\n");
133		return -EINVAL;
134	}
135
136	keymap_data = pdata->keymap_data;
137
138	irq = platform_get_irq(pdev, 0);
139	if (irq < 0) {
140		dev_err(&pdev->dev, "failed to get keypad irq\n");
141		return -ENXIO;
142	}
143
144	keypad = kzalloc(sizeof(struct w90p910_keypad), GFP_KERNEL);
145	input_dev = input_allocate_device();
146	if (!keypad || !input_dev) {
147		dev_err(&pdev->dev, "failed to allocate driver data\n");
148		error = -ENOMEM;
149		goto failed_free;
150	}
151
152	keypad->pdata = pdata;
153	keypad->input_dev = input_dev;
154	keypad->irq = irq;
155
156	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
157	if (res == NULL) {
158		dev_err(&pdev->dev, "failed to get I/O memory\n");
159		error = -ENXIO;
160		goto failed_free;
161	}
162
163	res = request_mem_region(res->start, resource_size(res), pdev->name);
164	if (res == NULL) {
165		dev_err(&pdev->dev, "failed to request I/O memory\n");
166		error = -EBUSY;
167		goto failed_free;
168	}
169
170	keypad->mmio_base = ioremap(res->start, resource_size(res));
171	if (keypad->mmio_base == NULL) {
172		dev_err(&pdev->dev, "failed to remap I/O memory\n");
173		error = -ENXIO;
174		goto failed_free_res;
175	}
176
177	keypad->clk = clk_get(&pdev->dev, NULL);
178	if (IS_ERR(keypad->clk)) {
179		dev_err(&pdev->dev, "failed to get keypad clock\n");
180		error = PTR_ERR(keypad->clk);
181		goto failed_free_io;
182	}
183
184	/* set multi-function pin for w90p910 kpi. */
185	mfp_set_groupi(&pdev->dev);
186
187	input_dev->name = pdev->name;
188	input_dev->id.bustype = BUS_HOST;
189	input_dev->open = w90p910_keypad_open;
190	input_dev->close = w90p910_keypad_close;
191	input_dev->dev.parent = &pdev->dev;
192
193	input_dev->keycode = keypad->keymap;
194	input_dev->keycodesize = sizeof(keypad->keymap[0]);
195	input_dev->keycodemax = ARRAY_SIZE(keypad->keymap);
196
197	input_set_drvdata(input_dev, keypad);
198
199	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
200	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
201
202	matrix_keypad_build_keymap(keymap_data, W90P910_ROW_SHIFT,
203				   input_dev->keycode, input_dev->keybit);
204
205	error = request_irq(keypad->irq, w90p910_keypad_irq_handler,
206			    IRQF_DISABLED, pdev->name, keypad);
207	if (error) {
208		dev_err(&pdev->dev, "failed to request IRQ\n");
209		goto failed_put_clk;
210	}
211
212	/* Register the input device */
213	error = input_register_device(input_dev);
214	if (error) {
215		dev_err(&pdev->dev, "failed to register input device\n");
216		goto failed_free_irq;
217	}
218
219	platform_set_drvdata(pdev, keypad);
220	return 0;
221
222failed_free_irq:
223	free_irq(irq, pdev);
224failed_put_clk:
225	clk_put(keypad->clk);
226failed_free_io:
227	iounmap(keypad->mmio_base);
228failed_free_res:
229	release_mem_region(res->start, resource_size(res));
230failed_free:
231	input_free_device(input_dev);
232	kfree(keypad);
233	return error;
234}
235
236static int __devexit w90p910_keypad_remove(struct platform_device *pdev)
237{
238	struct w90p910_keypad *keypad = platform_get_drvdata(pdev);
239	struct resource *res;
240
241	free_irq(keypad->irq, pdev);
242
243	clk_put(keypad->clk);
244
245	input_unregister_device(keypad->input_dev);
246
247	iounmap(keypad->mmio_base);
248	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
249	release_mem_region(res->start, resource_size(res));
250
251	platform_set_drvdata(pdev, NULL);
252	kfree(keypad);
253
254	return 0;
255}
256
257static struct platform_driver w90p910_keypad_driver = {
258	.probe		= w90p910_keypad_probe,
259	.remove		= __devexit_p(w90p910_keypad_remove),
260	.driver		= {
261		.name	= "nuc900-kpi",
262		.owner	= THIS_MODULE,
263	},
264};
265
266static int __init w90p910_keypad_init(void)
267{
268	return platform_driver_register(&w90p910_keypad_driver);
269}
270
271static void __exit w90p910_keypad_exit(void)
272{
273	platform_driver_unregister(&w90p910_keypad_driver);
274}
275
276module_init(w90p910_keypad_init);
277module_exit(w90p910_keypad_exit);
278
279MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
280MODULE_DESCRIPTION("w90p910 keypad driver");
281MODULE_LICENSE("GPL");
282MODULE_ALIAS("platform:nuc900-keypad");