Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * Samsung keypad driver
  3 *
  4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
  5 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  6 * Author: Donghwa Lee <dh09.lee@samsung.com>
  7 *
  8 * This program is free software; you can redistribute  it and/or modify it
  9 * under  the terms of  the GNU General  Public License as published by the
 10 * Free Software Foundation;  either version 2 of the  License, or (at your
 11 * option) any later version.
 12 */
 13
 14#include <linux/clk.h>
 15#include <linux/delay.h>
 16#include <linux/err.h>
 17#include <linux/init.h>
 18#include <linux/input.h>
 19#include <linux/interrupt.h>
 20#include <linux/io.h>
 21#include <linux/module.h>
 22#include <linux/platform_device.h>
 
 
 23#include <linux/slab.h>
 
 24#include <linux/sched.h>
 25#include <plat/keypad.h>
 26
 27#define SAMSUNG_KEYIFCON			0x00
 28#define SAMSUNG_KEYIFSTSCLR			0x04
 29#define SAMSUNG_KEYIFCOL			0x08
 30#define SAMSUNG_KEYIFROW			0x0c
 31#define SAMSUNG_KEYIFFC				0x10
 32
 33/* SAMSUNG_KEYIFCON */
 34#define SAMSUNG_KEYIFCON_INT_F_EN		(1 << 0)
 35#define SAMSUNG_KEYIFCON_INT_R_EN		(1 << 1)
 36#define SAMSUNG_KEYIFCON_DF_EN			(1 << 2)
 37#define SAMSUNG_KEYIFCON_FC_EN			(1 << 3)
 38#define SAMSUNG_KEYIFCON_WAKEUPEN		(1 << 4)
 39
 40/* SAMSUNG_KEYIFSTSCLR */
 41#define SAMSUNG_KEYIFSTSCLR_P_INT_MASK		(0xff << 0)
 42#define SAMSUNG_KEYIFSTSCLR_R_INT_MASK		(0xff << 8)
 43#define SAMSUNG_KEYIFSTSCLR_R_INT_OFFSET	8
 44#define S5PV210_KEYIFSTSCLR_P_INT_MASK		(0x3fff << 0)
 45#define S5PV210_KEYIFSTSCLR_R_INT_MASK		(0x3fff << 16)
 46#define S5PV210_KEYIFSTSCLR_R_INT_OFFSET	16
 47
 48/* SAMSUNG_KEYIFCOL */
 49#define SAMSUNG_KEYIFCOL_MASK			(0xff << 0)
 50#define S5PV210_KEYIFCOLEN_MASK			(0xff << 8)
 51
 52/* SAMSUNG_KEYIFROW */
 53#define SAMSUNG_KEYIFROW_MASK			(0xff << 0)
 54#define S5PV210_KEYIFROW_MASK			(0x3fff << 0)
 55
 56/* SAMSUNG_KEYIFFC */
 57#define SAMSUNG_KEYIFFC_MASK			(0x3ff << 0)
 58
 59enum samsung_keypad_type {
 60	KEYPAD_TYPE_SAMSUNG,
 61	KEYPAD_TYPE_S5PV210,
 62};
 63
 64struct samsung_keypad {
 65	struct input_dev *input_dev;
 
 66	struct clk *clk;
 67	void __iomem *base;
 68	wait_queue_head_t wait;
 69	bool stopped;
 
 70	int irq;
 
 71	unsigned int row_shift;
 72	unsigned int rows;
 73	unsigned int cols;
 74	unsigned int row_state[SAMSUNG_MAX_COLS];
 75	unsigned short keycodes[];
 76};
 77
 78static int samsung_keypad_is_s5pv210(struct device *dev)
 79{
 80	struct platform_device *pdev = to_platform_device(dev);
 81	enum samsung_keypad_type type =
 82		platform_get_device_id(pdev)->driver_data;
 83
 84	return type == KEYPAD_TYPE_S5PV210;
 85}
 86
 87static void samsung_keypad_scan(struct samsung_keypad *keypad,
 88				unsigned int *row_state)
 89{
 90	struct device *dev = keypad->input_dev->dev.parent;
 91	unsigned int col;
 92	unsigned int val;
 93
 94	for (col = 0; col < keypad->cols; col++) {
 95		if (samsung_keypad_is_s5pv210(dev)) {
 96			val = S5PV210_KEYIFCOLEN_MASK;
 97			val &= ~(1 << col) << 8;
 98		} else {
 99			val = SAMSUNG_KEYIFCOL_MASK;
100			val &= ~(1 << col);
101		}
102
103		writel(val, keypad->base + SAMSUNG_KEYIFCOL);
104		mdelay(1);
105
106		val = readl(keypad->base + SAMSUNG_KEYIFROW);
107		row_state[col] = ~val & ((1 << keypad->rows) - 1);
108	}
109
110	/* KEYIFCOL reg clear */
111	writel(0, keypad->base + SAMSUNG_KEYIFCOL);
112}
113
114static bool samsung_keypad_report(struct samsung_keypad *keypad,
115				  unsigned int *row_state)
116{
117	struct input_dev *input_dev = keypad->input_dev;
118	unsigned int changed;
119	unsigned int pressed;
120	unsigned int key_down = 0;
121	unsigned int val;
122	unsigned int col, row;
123
124	for (col = 0; col < keypad->cols; col++) {
125		changed = row_state[col] ^ keypad->row_state[col];
126		key_down |= row_state[col];
127		if (!changed)
128			continue;
129
130		for (row = 0; row < keypad->rows; row++) {
131			if (!(changed & (1 << row)))
132				continue;
133
134			pressed = row_state[col] & (1 << row);
135
136			dev_dbg(&keypad->input_dev->dev,
137				"key %s, row: %d, col: %d\n",
138				pressed ? "pressed" : "released", row, col);
139
140			val = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
141
142			input_event(input_dev, EV_MSC, MSC_SCAN, val);
143			input_report_key(input_dev,
144					keypad->keycodes[val], pressed);
145		}
146		input_sync(keypad->input_dev);
147	}
148
149	memcpy(keypad->row_state, row_state, sizeof(keypad->row_state));
150
151	return key_down;
152}
153
154static irqreturn_t samsung_keypad_irq(int irq, void *dev_id)
155{
156	struct samsung_keypad *keypad = dev_id;
157	unsigned int row_state[SAMSUNG_MAX_COLS];
158	unsigned int val;
159	bool key_down;
160
 
 
161	do {
162		val = readl(keypad->base + SAMSUNG_KEYIFSTSCLR);
163		/* Clear interrupt. */
164		writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
165
166		samsung_keypad_scan(keypad, row_state);
167
168		key_down = samsung_keypad_report(keypad, row_state);
169		if (key_down)
170			wait_event_timeout(keypad->wait, keypad->stopped,
171					   msecs_to_jiffies(50));
172
173	} while (key_down && !keypad->stopped);
174
 
 
175	return IRQ_HANDLED;
176}
177
178static void samsung_keypad_start(struct samsung_keypad *keypad)
179{
180	unsigned int val;
181
 
 
182	/* Tell IRQ thread that it may poll the device. */
183	keypad->stopped = false;
184
185	clk_enable(keypad->clk);
186
187	/* Enable interrupt bits. */
188	val = readl(keypad->base + SAMSUNG_KEYIFCON);
189	val |= SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN;
190	writel(val, keypad->base + SAMSUNG_KEYIFCON);
191
192	/* KEYIFCOL reg clear. */
193	writel(0, keypad->base + SAMSUNG_KEYIFCOL);
 
 
194}
195
196static void samsung_keypad_stop(struct samsung_keypad *keypad)
197{
198	unsigned int val;
199
 
 
200	/* Signal IRQ thread to stop polling and disable the handler. */
201	keypad->stopped = true;
202	wake_up(&keypad->wait);
203	disable_irq(keypad->irq);
204
205	/* Clear interrupt. */
206	writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
207
208	/* Disable interrupt bits. */
209	val = readl(keypad->base + SAMSUNG_KEYIFCON);
210	val &= ~(SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN);
211	writel(val, keypad->base + SAMSUNG_KEYIFCON);
212
213	clk_disable(keypad->clk);
214
215	/*
216	 * Now that chip should not generate interrupts we can safely
217	 * re-enable the handler.
218	 */
219	enable_irq(keypad->irq);
 
 
220}
221
222static int samsung_keypad_open(struct input_dev *input_dev)
223{
224	struct samsung_keypad *keypad = input_get_drvdata(input_dev);
225
226	samsung_keypad_start(keypad);
227
228	return 0;
229}
230
231static void samsung_keypad_close(struct input_dev *input_dev)
232{
233	struct samsung_keypad *keypad = input_get_drvdata(input_dev);
234
235	samsung_keypad_stop(keypad);
236}
237
238static int __devinit samsung_keypad_probe(struct platform_device *pdev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239{
240	const struct samsung_keypad_platdata *pdata;
241	const struct matrix_keymap_data *keymap_data;
242	struct samsung_keypad *keypad;
243	struct resource *res;
244	struct input_dev *input_dev;
245	unsigned int row_shift;
246	unsigned int keymap_size;
247	int error;
248
249	pdata = pdev->dev.platform_data;
250	if (!pdata) {
251		dev_err(&pdev->dev, "no platform data defined\n");
252		return -EINVAL;
 
253	}
254
255	keymap_data = pdata->keymap_data;
256	if (!keymap_data) {
257		dev_err(&pdev->dev, "no keymap data defined\n");
258		return -EINVAL;
259	}
260
261	if (!pdata->rows || pdata->rows > SAMSUNG_MAX_ROWS)
262		return -EINVAL;
263
264	if (!pdata->cols || pdata->cols > SAMSUNG_MAX_COLS)
265		return -EINVAL;
266
267	/* initialize the gpio */
268	if (pdata->cfg_gpio)
269		pdata->cfg_gpio(pdata->rows, pdata->cols);
270
271	row_shift = get_count_order(pdata->cols);
272	keymap_size = (pdata->rows << row_shift) * sizeof(keypad->keycodes[0]);
273
274	keypad = kzalloc(sizeof(*keypad) + keymap_size, GFP_KERNEL);
275	input_dev = input_allocate_device();
276	if (!keypad || !input_dev) {
277		error = -ENOMEM;
278		goto err_free_mem;
279	}
280
281	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
282	if (!res) {
283		error = -ENODEV;
284		goto err_free_mem;
285	}
286
287	keypad->base = ioremap(res->start, resource_size(res));
288	if (!keypad->base) {
289		error = -EBUSY;
290		goto err_free_mem;
291	}
292
293	keypad->clk = clk_get(&pdev->dev, "keypad");
294	if (IS_ERR(keypad->clk)) {
295		dev_err(&pdev->dev, "failed to get keypad clk\n");
296		error = PTR_ERR(keypad->clk);
297		goto err_unmap_base;
 
 
 
 
 
298	}
299
300	keypad->input_dev = input_dev;
 
301	keypad->row_shift = row_shift;
302	keypad->rows = pdata->rows;
303	keypad->cols = pdata->cols;
 
304	init_waitqueue_head(&keypad->wait);
305
 
 
 
 
 
 
306	input_dev->name = pdev->name;
307	input_dev->id.bustype = BUS_HOST;
308	input_dev->dev.parent = &pdev->dev;
309	input_set_drvdata(input_dev, keypad);
310
311	input_dev->open = samsung_keypad_open;
312	input_dev->close = samsung_keypad_close;
313
314	input_dev->evbit[0] = BIT_MASK(EV_KEY);
315	if (!pdata->no_autorepeat)
316		input_dev->evbit[0] |= BIT_MASK(EV_REP);
 
 
 
 
317
318	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
 
 
319
320	input_dev->keycode = keypad->keycodes;
321	input_dev->keycodesize = sizeof(keypad->keycodes[0]);
322	input_dev->keycodemax = pdata->rows << row_shift;
323
324	matrix_keypad_build_keymap(keymap_data, row_shift,
325			input_dev->keycode, input_dev->keybit);
326
327	keypad->irq = platform_get_irq(pdev, 0);
328	if (keypad->irq < 0) {
329		error = keypad->irq;
330		goto err_put_clk;
331	}
332
333	error = request_threaded_irq(keypad->irq, NULL, samsung_keypad_irq,
334			IRQF_ONESHOT, dev_name(&pdev->dev), keypad);
 
335	if (error) {
336		dev_err(&pdev->dev, "failed to register keypad interrupt\n");
337		goto err_put_clk;
338	}
339
 
 
 
 
340	error = input_register_device(keypad->input_dev);
341	if (error)
342		goto err_free_irq;
343
344	device_init_wakeup(&pdev->dev, pdata->wakeup);
345	platform_set_drvdata(pdev, keypad);
 
 
 
346	return 0;
347
348err_free_irq:
349	free_irq(keypad->irq, keypad);
350err_put_clk:
351	clk_put(keypad->clk);
352err_unmap_base:
353	iounmap(keypad->base);
354err_free_mem:
355	input_free_device(input_dev);
356	kfree(keypad);
357
358	return error;
359}
360
361static int __devexit samsung_keypad_remove(struct platform_device *pdev)
362{
363	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
364
365	device_init_wakeup(&pdev->dev, 0);
366	platform_set_drvdata(pdev, NULL);
367
368	input_unregister_device(keypad->input_dev);
369
370	/*
371	 * It is safe to free IRQ after unregistering device because
372	 * samsung_keypad_close will shut off interrupts.
373	 */
374	free_irq(keypad->irq, keypad);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375
376	clk_put(keypad->clk);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377
378	iounmap(keypad->base);
379	kfree(keypad);
 
 
 
 
380
381	return 0;
382}
383
384#ifdef CONFIG_PM
385static void samsung_keypad_toggle_wakeup(struct samsung_keypad *keypad,
386					 bool enable)
387{
388	struct device *dev = keypad->input_dev->dev.parent;
389	unsigned int val;
390
391	clk_enable(keypad->clk);
392
393	val = readl(keypad->base + SAMSUNG_KEYIFCON);
394	if (enable) {
395		val |= SAMSUNG_KEYIFCON_WAKEUPEN;
396		if (device_may_wakeup(dev))
397			enable_irq_wake(keypad->irq);
398	} else {
399		val &= ~SAMSUNG_KEYIFCON_WAKEUPEN;
400		if (device_may_wakeup(dev))
401			disable_irq_wake(keypad->irq);
402	}
403	writel(val, keypad->base + SAMSUNG_KEYIFCON);
404
405	clk_disable(keypad->clk);
406}
407
408static int samsung_keypad_suspend(struct device *dev)
409{
410	struct platform_device *pdev = to_platform_device(dev);
411	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
412	struct input_dev *input_dev = keypad->input_dev;
413
414	mutex_lock(&input_dev->mutex);
415
416	if (input_dev->users)
417		samsung_keypad_stop(keypad);
418
419	samsung_keypad_toggle_wakeup(keypad, true);
420
421	mutex_unlock(&input_dev->mutex);
422
423	return 0;
424}
425
426static int samsung_keypad_resume(struct device *dev)
427{
428	struct platform_device *pdev = to_platform_device(dev);
429	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
430	struct input_dev *input_dev = keypad->input_dev;
431
432	mutex_lock(&input_dev->mutex);
433
434	samsung_keypad_toggle_wakeup(keypad, false);
435
436	if (input_dev->users)
437		samsung_keypad_start(keypad);
438
439	mutex_unlock(&input_dev->mutex);
440
441	return 0;
442}
443
444static const struct dev_pm_ops samsung_keypad_pm_ops = {
445	.suspend	= samsung_keypad_suspend,
446	.resume		= samsung_keypad_resume,
 
447};
 
 
 
 
 
 
 
 
448#endif
449
450static struct platform_device_id samsung_keypad_driver_ids[] = {
451	{
452		.name		= "samsung-keypad",
453		.driver_data	= KEYPAD_TYPE_SAMSUNG,
454	}, {
455		.name		= "s5pv210-keypad",
456		.driver_data	= KEYPAD_TYPE_S5PV210,
457	},
458	{ },
459};
460MODULE_DEVICE_TABLE(platform, samsung_keypad_driver_ids);
461
462static struct platform_driver samsung_keypad_driver = {
463	.probe		= samsung_keypad_probe,
464	.remove		= __devexit_p(samsung_keypad_remove),
465	.driver		= {
466		.name	= "samsung-keypad",
467		.owner	= THIS_MODULE,
468#ifdef CONFIG_PM
469		.pm	= &samsung_keypad_pm_ops,
470#endif
471	},
472	.id_table	= samsung_keypad_driver_ids,
473};
474
475static int __init samsung_keypad_init(void)
476{
477	return platform_driver_register(&samsung_keypad_driver);
478}
479module_init(samsung_keypad_init);
480
481static void __exit samsung_keypad_exit(void)
482{
483	platform_driver_unregister(&samsung_keypad_driver);
484}
485module_exit(samsung_keypad_exit);
486
487MODULE_DESCRIPTION("Samsung keypad driver");
488MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
489MODULE_AUTHOR("Donghwa Lee <dh09.lee@samsung.com>");
490MODULE_LICENSE("GPL");
491MODULE_ALIAS("platform:samsung-keypad");
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Samsung keypad driver
  4 *
  5 * Copyright (C) 2010 Samsung Electronics Co.Ltd
  6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  7 * Author: Donghwa Lee <dh09.lee@samsung.com>
 
 
 
 
 
  8 */
  9
 10#include <linux/clk.h>
 11#include <linux/delay.h>
 12#include <linux/err.h>
 
 13#include <linux/input.h>
 14#include <linux/interrupt.h>
 15#include <linux/io.h>
 16#include <linux/module.h>
 17#include <linux/platform_device.h>
 18#include <linux/pm.h>
 19#include <linux/pm_runtime.h>
 20#include <linux/slab.h>
 21#include <linux/of.h>
 22#include <linux/sched.h>
 23#include <linux/input/samsung-keypad.h>
 24
 25#define SAMSUNG_KEYIFCON			0x00
 26#define SAMSUNG_KEYIFSTSCLR			0x04
 27#define SAMSUNG_KEYIFCOL			0x08
 28#define SAMSUNG_KEYIFROW			0x0c
 29#define SAMSUNG_KEYIFFC				0x10
 30
 31/* SAMSUNG_KEYIFCON */
 32#define SAMSUNG_KEYIFCON_INT_F_EN		(1 << 0)
 33#define SAMSUNG_KEYIFCON_INT_R_EN		(1 << 1)
 34#define SAMSUNG_KEYIFCON_DF_EN			(1 << 2)
 35#define SAMSUNG_KEYIFCON_FC_EN			(1 << 3)
 36#define SAMSUNG_KEYIFCON_WAKEUPEN		(1 << 4)
 37
 38/* SAMSUNG_KEYIFSTSCLR */
 39#define SAMSUNG_KEYIFSTSCLR_P_INT_MASK		(0xff << 0)
 40#define SAMSUNG_KEYIFSTSCLR_R_INT_MASK		(0xff << 8)
 41#define SAMSUNG_KEYIFSTSCLR_R_INT_OFFSET	8
 42#define S5PV210_KEYIFSTSCLR_P_INT_MASK		(0x3fff << 0)
 43#define S5PV210_KEYIFSTSCLR_R_INT_MASK		(0x3fff << 16)
 44#define S5PV210_KEYIFSTSCLR_R_INT_OFFSET	16
 45
 46/* SAMSUNG_KEYIFCOL */
 47#define SAMSUNG_KEYIFCOL_MASK			(0xff << 0)
 48#define S5PV210_KEYIFCOLEN_MASK			(0xff << 8)
 49
 50/* SAMSUNG_KEYIFROW */
 51#define SAMSUNG_KEYIFROW_MASK			(0xff << 0)
 52#define S5PV210_KEYIFROW_MASK			(0x3fff << 0)
 53
 54/* SAMSUNG_KEYIFFC */
 55#define SAMSUNG_KEYIFFC_MASK			(0x3ff << 0)
 56
 57enum samsung_keypad_type {
 58	KEYPAD_TYPE_SAMSUNG,
 59	KEYPAD_TYPE_S5PV210,
 60};
 61
 62struct samsung_keypad {
 63	struct input_dev *input_dev;
 64	struct platform_device *pdev;
 65	struct clk *clk;
 66	void __iomem *base;
 67	wait_queue_head_t wait;
 68	bool stopped;
 69	bool wake_enabled;
 70	int irq;
 71	enum samsung_keypad_type type;
 72	unsigned int row_shift;
 73	unsigned int rows;
 74	unsigned int cols;
 75	unsigned int row_state[SAMSUNG_MAX_COLS];
 76	unsigned short keycodes[];
 77};
 78
 
 
 
 
 
 
 
 
 
 79static void samsung_keypad_scan(struct samsung_keypad *keypad,
 80				unsigned int *row_state)
 81{
 
 82	unsigned int col;
 83	unsigned int val;
 84
 85	for (col = 0; col < keypad->cols; col++) {
 86		if (keypad->type == KEYPAD_TYPE_S5PV210) {
 87			val = S5PV210_KEYIFCOLEN_MASK;
 88			val &= ~(1 << col) << 8;
 89		} else {
 90			val = SAMSUNG_KEYIFCOL_MASK;
 91			val &= ~(1 << col);
 92		}
 93
 94		writel(val, keypad->base + SAMSUNG_KEYIFCOL);
 95		mdelay(1);
 96
 97		val = readl(keypad->base + SAMSUNG_KEYIFROW);
 98		row_state[col] = ~val & ((1 << keypad->rows) - 1);
 99	}
100
101	/* KEYIFCOL reg clear */
102	writel(0, keypad->base + SAMSUNG_KEYIFCOL);
103}
104
105static bool samsung_keypad_report(struct samsung_keypad *keypad,
106				  unsigned int *row_state)
107{
108	struct input_dev *input_dev = keypad->input_dev;
109	unsigned int changed;
110	unsigned int pressed;
111	unsigned int key_down = 0;
112	unsigned int val;
113	unsigned int col, row;
114
115	for (col = 0; col < keypad->cols; col++) {
116		changed = row_state[col] ^ keypad->row_state[col];
117		key_down |= row_state[col];
118		if (!changed)
119			continue;
120
121		for (row = 0; row < keypad->rows; row++) {
122			if (!(changed & (1 << row)))
123				continue;
124
125			pressed = row_state[col] & (1 << row);
126
127			dev_dbg(&keypad->input_dev->dev,
128				"key %s, row: %d, col: %d\n",
129				pressed ? "pressed" : "released", row, col);
130
131			val = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
132
133			input_event(input_dev, EV_MSC, MSC_SCAN, val);
134			input_report_key(input_dev,
135					keypad->keycodes[val], pressed);
136		}
137		input_sync(keypad->input_dev);
138	}
139
140	memcpy(keypad->row_state, row_state, sizeof(keypad->row_state));
141
142	return key_down;
143}
144
145static irqreturn_t samsung_keypad_irq(int irq, void *dev_id)
146{
147	struct samsung_keypad *keypad = dev_id;
148	unsigned int row_state[SAMSUNG_MAX_COLS];
 
149	bool key_down;
150
151	pm_runtime_get_sync(&keypad->pdev->dev);
152
153	do {
154		readl(keypad->base + SAMSUNG_KEYIFSTSCLR);
155		/* Clear interrupt. */
156		writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
157
158		samsung_keypad_scan(keypad, row_state);
159
160		key_down = samsung_keypad_report(keypad, row_state);
161		if (key_down)
162			wait_event_timeout(keypad->wait, keypad->stopped,
163					   msecs_to_jiffies(50));
164
165	} while (key_down && !keypad->stopped);
166
167	pm_runtime_put(&keypad->pdev->dev);
168
169	return IRQ_HANDLED;
170}
171
172static void samsung_keypad_start(struct samsung_keypad *keypad)
173{
174	unsigned int val;
175
176	pm_runtime_get_sync(&keypad->pdev->dev);
177
178	/* Tell IRQ thread that it may poll the device. */
179	keypad->stopped = false;
180
181	clk_enable(keypad->clk);
182
183	/* Enable interrupt bits. */
184	val = readl(keypad->base + SAMSUNG_KEYIFCON);
185	val |= SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN;
186	writel(val, keypad->base + SAMSUNG_KEYIFCON);
187
188	/* KEYIFCOL reg clear. */
189	writel(0, keypad->base + SAMSUNG_KEYIFCOL);
190
191	pm_runtime_put(&keypad->pdev->dev);
192}
193
194static void samsung_keypad_stop(struct samsung_keypad *keypad)
195{
196	unsigned int val;
197
198	pm_runtime_get_sync(&keypad->pdev->dev);
199
200	/* Signal IRQ thread to stop polling and disable the handler. */
201	keypad->stopped = true;
202	wake_up(&keypad->wait);
203	disable_irq(keypad->irq);
204
205	/* Clear interrupt. */
206	writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
207
208	/* Disable interrupt bits. */
209	val = readl(keypad->base + SAMSUNG_KEYIFCON);
210	val &= ~(SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN);
211	writel(val, keypad->base + SAMSUNG_KEYIFCON);
212
213	clk_disable(keypad->clk);
214
215	/*
216	 * Now that chip should not generate interrupts we can safely
217	 * re-enable the handler.
218	 */
219	enable_irq(keypad->irq);
220
221	pm_runtime_put(&keypad->pdev->dev);
222}
223
224static int samsung_keypad_open(struct input_dev *input_dev)
225{
226	struct samsung_keypad *keypad = input_get_drvdata(input_dev);
227
228	samsung_keypad_start(keypad);
229
230	return 0;
231}
232
233static void samsung_keypad_close(struct input_dev *input_dev)
234{
235	struct samsung_keypad *keypad = input_get_drvdata(input_dev);
236
237	samsung_keypad_stop(keypad);
238}
239
240#ifdef CONFIG_OF
241static struct samsung_keypad_platdata *
242samsung_keypad_parse_dt(struct device *dev)
243{
244	struct samsung_keypad_platdata *pdata;
245	struct matrix_keymap_data *keymap_data;
246	uint32_t *keymap, num_rows = 0, num_cols = 0;
247	struct device_node *np = dev->of_node, *key_np;
248	unsigned int key_count;
249
250	if (!np) {
251		dev_err(dev, "missing device tree data\n");
252		return ERR_PTR(-EINVAL);
253	}
254
255	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
256	if (!pdata) {
257		dev_err(dev, "could not allocate memory for platform data\n");
258		return ERR_PTR(-ENOMEM);
259	}
260
261	of_property_read_u32(np, "samsung,keypad-num-rows", &num_rows);
262	of_property_read_u32(np, "samsung,keypad-num-columns", &num_cols);
263	if (!num_rows || !num_cols) {
264		dev_err(dev, "number of keypad rows/columns not specified\n");
265		return ERR_PTR(-EINVAL);
266	}
267	pdata->rows = num_rows;
268	pdata->cols = num_cols;
269
270	keymap_data = devm_kzalloc(dev, sizeof(*keymap_data), GFP_KERNEL);
271	if (!keymap_data) {
272		dev_err(dev, "could not allocate memory for keymap data\n");
273		return ERR_PTR(-ENOMEM);
274	}
275	pdata->keymap_data = keymap_data;
276
277	key_count = of_get_child_count(np);
278	keymap_data->keymap_size = key_count;
279	keymap = devm_kcalloc(dev, key_count, sizeof(uint32_t), GFP_KERNEL);
280	if (!keymap) {
281		dev_err(dev, "could not allocate memory for keymap\n");
282		return ERR_PTR(-ENOMEM);
283	}
284	keymap_data->keymap = keymap;
285
286	for_each_child_of_node(np, key_np) {
287		u32 row, col, key_code;
288		of_property_read_u32(key_np, "keypad,row", &row);
289		of_property_read_u32(key_np, "keypad,column", &col);
290		of_property_read_u32(key_np, "linux,code", &key_code);
291		*keymap++ = KEY(row, col, key_code);
292	}
293
294	pdata->no_autorepeat = of_property_read_bool(np, "linux,input-no-autorepeat");
295
296	pdata->wakeup = of_property_read_bool(np, "wakeup-source") ||
297			/* legacy name */
298			of_property_read_bool(np, "linux,input-wakeup");
299
300
301	return pdata;
302}
303#else
304static struct samsung_keypad_platdata *
305samsung_keypad_parse_dt(struct device *dev)
306{
307	dev_err(dev, "no platform data defined\n");
308
309	return ERR_PTR(-EINVAL);
310}
311#endif
312
313static int samsung_keypad_probe(struct platform_device *pdev)
314{
315	const struct samsung_keypad_platdata *pdata;
316	const struct matrix_keymap_data *keymap_data;
317	struct samsung_keypad *keypad;
318	struct resource *res;
319	struct input_dev *input_dev;
320	unsigned int row_shift;
321	unsigned int keymap_size;
322	int error;
323
324	pdata = dev_get_platdata(&pdev->dev);
325	if (!pdata) {
326		pdata = samsung_keypad_parse_dt(&pdev->dev);
327		if (IS_ERR(pdata))
328			return PTR_ERR(pdata);
329	}
330
331	keymap_data = pdata->keymap_data;
332	if (!keymap_data) {
333		dev_err(&pdev->dev, "no keymap data defined\n");
334		return -EINVAL;
335	}
336
337	if (!pdata->rows || pdata->rows > SAMSUNG_MAX_ROWS)
338		return -EINVAL;
339
340	if (!pdata->cols || pdata->cols > SAMSUNG_MAX_COLS)
341		return -EINVAL;
342
343	/* initialize the gpio */
344	if (pdata->cfg_gpio)
345		pdata->cfg_gpio(pdata->rows, pdata->cols);
346
347	row_shift = get_count_order(pdata->cols);
348	keymap_size = (pdata->rows << row_shift) * sizeof(keypad->keycodes[0]);
349
350	keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad) + keymap_size,
351			      GFP_KERNEL);
352	input_dev = devm_input_allocate_device(&pdev->dev);
353	if (!keypad || !input_dev)
354		return -ENOMEM;
 
355
356	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
357	if (!res)
358		return -ENODEV;
 
 
359
360	keypad->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
361	if (!keypad->base)
362		return -EBUSY;
 
 
363
364	keypad->clk = devm_clk_get(&pdev->dev, "keypad");
365	if (IS_ERR(keypad->clk)) {
366		dev_err(&pdev->dev, "failed to get keypad clk\n");
367		return PTR_ERR(keypad->clk);
368	}
369
370	error = clk_prepare(keypad->clk);
371	if (error) {
372		dev_err(&pdev->dev, "keypad clock prepare failed\n");
373		return error;
374	}
375
376	keypad->input_dev = input_dev;
377	keypad->pdev = pdev;
378	keypad->row_shift = row_shift;
379	keypad->rows = pdata->rows;
380	keypad->cols = pdata->cols;
381	keypad->stopped = true;
382	init_waitqueue_head(&keypad->wait);
383
384	if (pdev->dev.of_node)
385		keypad->type = of_device_is_compatible(pdev->dev.of_node,
386					"samsung,s5pv210-keypad");
387	else
388		keypad->type = platform_get_device_id(pdev)->driver_data;
389
390	input_dev->name = pdev->name;
391	input_dev->id.bustype = BUS_HOST;
392	input_dev->dev.parent = &pdev->dev;
 
393
394	input_dev->open = samsung_keypad_open;
395	input_dev->close = samsung_keypad_close;
396
397	error = matrix_keypad_build_keymap(keymap_data, NULL,
398					   pdata->rows, pdata->cols,
399					   keypad->keycodes, input_dev);
400	if (error) {
401		dev_err(&pdev->dev, "failed to build keymap\n");
402		goto err_unprepare_clk;
403	}
404
405	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
406	if (!pdata->no_autorepeat)
407		__set_bit(EV_REP, input_dev->evbit);
408
409	input_set_drvdata(input_dev, keypad);
 
 
 
 
 
410
411	keypad->irq = platform_get_irq(pdev, 0);
412	if (keypad->irq < 0) {
413		error = keypad->irq;
414		goto err_unprepare_clk;
415	}
416
417	error = devm_request_threaded_irq(&pdev->dev, keypad->irq, NULL,
418					  samsung_keypad_irq, IRQF_ONESHOT,
419					  dev_name(&pdev->dev), keypad);
420	if (error) {
421		dev_err(&pdev->dev, "failed to register keypad interrupt\n");
422		goto err_unprepare_clk;
423	}
424
425	device_init_wakeup(&pdev->dev, pdata->wakeup);
426	platform_set_drvdata(pdev, keypad);
427	pm_runtime_enable(&pdev->dev);
428
429	error = input_register_device(keypad->input_dev);
430	if (error)
431		goto err_disable_runtime_pm;
432
433	if (pdev->dev.of_node) {
434		devm_kfree(&pdev->dev, (void *)pdata->keymap_data->keymap);
435		devm_kfree(&pdev->dev, (void *)pdata->keymap_data);
436		devm_kfree(&pdev->dev, (void *)pdata);
437	}
438	return 0;
439
440err_disable_runtime_pm:
441	pm_runtime_disable(&pdev->dev);
442err_unprepare_clk:
443	clk_unprepare(keypad->clk);
 
 
 
 
 
 
444	return error;
445}
446
447static void samsung_keypad_remove(struct platform_device *pdev)
448{
449	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
450
451	pm_runtime_disable(&pdev->dev);
 
452
453	input_unregister_device(keypad->input_dev);
454
455	clk_unprepare(keypad->clk);
456}
457
458static int samsung_keypad_runtime_suspend(struct device *dev)
459{
460	struct platform_device *pdev = to_platform_device(dev);
461	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
462	unsigned int val;
463	int error;
464
465	if (keypad->stopped)
466		return 0;
467
468	/* This may fail on some SoCs due to lack of controller support */
469	error = enable_irq_wake(keypad->irq);
470	if (!error)
471		keypad->wake_enabled = true;
472
473	val = readl(keypad->base + SAMSUNG_KEYIFCON);
474	val |= SAMSUNG_KEYIFCON_WAKEUPEN;
475	writel(val, keypad->base + SAMSUNG_KEYIFCON);
476
477	clk_disable(keypad->clk);
478
479	return 0;
480}
481
482static int samsung_keypad_runtime_resume(struct device *dev)
483{
484	struct platform_device *pdev = to_platform_device(dev);
485	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
486	unsigned int val;
487
488	if (keypad->stopped)
489		return 0;
490
491	clk_enable(keypad->clk);
492
493	val = readl(keypad->base + SAMSUNG_KEYIFCON);
494	val &= ~SAMSUNG_KEYIFCON_WAKEUPEN;
495	writel(val, keypad->base + SAMSUNG_KEYIFCON);
496
497	if (keypad->wake_enabled)
498		disable_irq_wake(keypad->irq);
499
500	return 0;
501}
502
 
503static void samsung_keypad_toggle_wakeup(struct samsung_keypad *keypad,
504					 bool enable)
505{
 
506	unsigned int val;
507
508	clk_enable(keypad->clk);
509
510	val = readl(keypad->base + SAMSUNG_KEYIFCON);
511	if (enable) {
512		val |= SAMSUNG_KEYIFCON_WAKEUPEN;
513		if (device_may_wakeup(&keypad->pdev->dev))
514			enable_irq_wake(keypad->irq);
515	} else {
516		val &= ~SAMSUNG_KEYIFCON_WAKEUPEN;
517		if (device_may_wakeup(&keypad->pdev->dev))
518			disable_irq_wake(keypad->irq);
519	}
520	writel(val, keypad->base + SAMSUNG_KEYIFCON);
521
522	clk_disable(keypad->clk);
523}
524
525static int samsung_keypad_suspend(struct device *dev)
526{
527	struct platform_device *pdev = to_platform_device(dev);
528	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
529	struct input_dev *input_dev = keypad->input_dev;
530
531	mutex_lock(&input_dev->mutex);
532
533	if (input_device_enabled(input_dev))
534		samsung_keypad_stop(keypad);
535
536	samsung_keypad_toggle_wakeup(keypad, true);
537
538	mutex_unlock(&input_dev->mutex);
539
540	return 0;
541}
542
543static int samsung_keypad_resume(struct device *dev)
544{
545	struct platform_device *pdev = to_platform_device(dev);
546	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
547	struct input_dev *input_dev = keypad->input_dev;
548
549	mutex_lock(&input_dev->mutex);
550
551	samsung_keypad_toggle_wakeup(keypad, false);
552
553	if (input_device_enabled(input_dev))
554		samsung_keypad_start(keypad);
555
556	mutex_unlock(&input_dev->mutex);
557
558	return 0;
559}
560
561static const struct dev_pm_ops samsung_keypad_pm_ops = {
562	SYSTEM_SLEEP_PM_OPS(samsung_keypad_suspend, samsung_keypad_resume)
563	RUNTIME_PM_OPS(samsung_keypad_runtime_suspend,
564		       samsung_keypad_runtime_resume, NULL)
565};
566
567#ifdef CONFIG_OF
568static const struct of_device_id samsung_keypad_dt_match[] = {
569	{ .compatible = "samsung,s3c6410-keypad" },
570	{ .compatible = "samsung,s5pv210-keypad" },
571	{},
572};
573MODULE_DEVICE_TABLE(of, samsung_keypad_dt_match);
574#endif
575
576static const struct platform_device_id samsung_keypad_driver_ids[] = {
577	{
578		.name		= "samsung-keypad",
579		.driver_data	= KEYPAD_TYPE_SAMSUNG,
580	}, {
581		.name		= "s5pv210-keypad",
582		.driver_data	= KEYPAD_TYPE_S5PV210,
583	},
584	{ },
585};
586MODULE_DEVICE_TABLE(platform, samsung_keypad_driver_ids);
587
588static struct platform_driver samsung_keypad_driver = {
589	.probe		= samsung_keypad_probe,
590	.remove_new	= samsung_keypad_remove,
591	.driver		= {
592		.name	= "samsung-keypad",
593		.of_match_table = of_match_ptr(samsung_keypad_dt_match),
594		.pm	= pm_ptr(&samsung_keypad_pm_ops),
 
 
595	},
596	.id_table	= samsung_keypad_driver_ids,
597};
598module_platform_driver(samsung_keypad_driver);
 
 
 
 
 
 
 
 
 
 
 
599
600MODULE_DESCRIPTION("Samsung keypad driver");
601MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
602MODULE_AUTHOR("Donghwa Lee <dh09.lee@samsung.com>");
603MODULE_LICENSE("GPL");