w90p910_keypad.c 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
 * Copyright (c) 2008-2009 Nuvoton technology corporation.
 *
 * Wan ZongShun <mcuos.com@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation;version 2 of the License.
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/input.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/io.h>

#include <mach/w90p910_keypad.h>

/* Keypad Interface Control Registers */
#define KPI_CONF		0x00
#define KPI_3KCONF		0x04
#define KPI_LPCONF		0x08
#define KPI_STATUS		0x0C

#define IS1KEY			(0x01 << 16)
#define INTTR			(0x01 << 21)
#define KEY0R			(0x0f << 3)
#define KEY0C			0x07
#define DEBOUNCE_BIT		0x08
#define KSIZE0			(0x01 << 16)
#define KSIZE1			(0x01 << 17)
#define KPSEL			(0x01 << 19)
#define ENKP			(0x01 << 18)

#define KGET_RAW(n)		(((n) & KEY0R) >> 3)
#define KGET_COLUMN(n)		((n) & KEY0C)

44 45
#define W90P910_MAX_KEY_NUM	(8 * 8)
#define W90P910_ROW_SHIFT	3
46 47

struct w90p910_keypad {
48
	const struct w90p910_keypad_platform_data *pdata;
49 50 51 52
	struct clk *clk;
	struct input_dev *input_dev;
	void __iomem *mmio_base;
	int irq;
53
	unsigned short keymap[W90P910_MAX_KEY_NUM];
54 55 56 57 58
};

static void w90p910_keypad_scan_matrix(struct w90p910_keypad *keypad,
							unsigned int status)
{
59 60 61 62 63 64 65 66 67 68 69 70 71
	struct input_dev *input_dev = keypad->input_dev;
	unsigned int row = KGET_RAW(status);
	unsigned int col = KGET_COLUMN(status);
	unsigned int code = MATRIX_SCAN_CODE(row, col, W90P910_ROW_SHIFT);
	unsigned int key = keypad->keymap[code];

	input_event(input_dev, EV_MSC, MSC_SCAN, code);
	input_report_key(input_dev, key, 1);
	input_sync(input_dev);

	input_event(input_dev, EV_MSC, MSC_SCAN, code);
	input_report_key(input_dev, key, 0);
	input_sync(input_dev);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
}

static irqreturn_t w90p910_keypad_irq_handler(int irq, void *dev_id)
{
	struct w90p910_keypad *keypad = dev_id;
	unsigned int  kstatus, val;

	kstatus = __raw_readl(keypad->mmio_base + KPI_STATUS);

	val = INTTR | IS1KEY;

	if (kstatus & val)
		w90p910_keypad_scan_matrix(keypad, kstatus);

	return IRQ_HANDLED;
}

static int w90p910_keypad_open(struct input_dev *dev)
{
	struct w90p910_keypad *keypad = input_get_drvdata(dev);
92
	const struct w90p910_keypad_platform_data *pdata = keypad->pdata;
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	unsigned int val, config;

	/* Enable unit clock */
	clk_enable(keypad->clk);

	val = __raw_readl(keypad->mmio_base + KPI_CONF);
	val |= (KPSEL | ENKP);
	val &= ~(KSIZE0 | KSIZE1);

	config = pdata->prescale | (pdata->debounce << DEBOUNCE_BIT);

	val |= config;

	__raw_writel(val, keypad->mmio_base + KPI_CONF);

	return 0;
}

static void w90p910_keypad_close(struct input_dev *dev)
{
	struct w90p910_keypad *keypad = input_get_drvdata(dev);

	/* Disable clock unit */
	clk_disable(keypad->clk);
}

static int __devinit w90p910_keypad_probe(struct platform_device *pdev)
{
121 122 123
	const struct w90p910_keypad_platform_data *pdata =
						pdev->dev.platform_data;
	const struct matrix_keymap_data *keymap_data = pdata->keymap_data;
124 125 126
	struct w90p910_keypad *keypad;
	struct input_dev *input_dev;
	struct resource *res;
127 128 129
	int irq;
	int error;
	int i;
130

131
	if (!pdata) {
132
		dev_err(&pdev->dev, "no platform data defined\n");
133
		return -EINVAL;
134 135 136 137 138
	}

	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
		dev_err(&pdev->dev, "failed to get keypad irq\n");
139 140 141 142 143 144 145 146
		return -ENXIO;
	}

	keypad = kzalloc(sizeof(struct w90p910_keypad), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (!keypad || !input_dev) {
		dev_err(&pdev->dev, "failed to allocate driver data\n");
		error = -ENOMEM;
147 148 149
		goto failed_free;
	}

150 151 152 153
	keypad->pdata = pdata;
	keypad->input_dev = input_dev;
	keypad->irq = irq;

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (res == NULL) {
		dev_err(&pdev->dev, "failed to get I/O memory\n");
		error = -ENXIO;
		goto failed_free;
	}

	res = request_mem_region(res->start, resource_size(res), pdev->name);
	if (res == NULL) {
		dev_err(&pdev->dev, "failed to request I/O memory\n");
		error = -EBUSY;
		goto failed_free;
	}

	keypad->mmio_base = ioremap(res->start, resource_size(res));
	if (keypad->mmio_base == NULL) {
		dev_err(&pdev->dev, "failed to remap I/O memory\n");
		error = -ENXIO;
172
		goto failed_free_res;
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	}

	keypad->clk = clk_get(&pdev->dev, NULL);
	if (IS_ERR(keypad->clk)) {
		dev_err(&pdev->dev, "failed to get keypad clock\n");
		error = PTR_ERR(keypad->clk);
		goto failed_free_io;
	}

	/* set multi-function pin for w90p910 kpi. */
	mfp_set_groupi(&pdev->dev);

	input_dev->name = pdev->name;
	input_dev->id.bustype = BUS_HOST;
	input_dev->open = w90p910_keypad_open;
	input_dev->close = w90p910_keypad_close;
	input_dev->dev.parent = &pdev->dev;

191 192 193 194
	input_dev->keycode = keypad->keymap;
	input_dev->keycodesize = sizeof(keypad->keymap[0]);
	input_dev->keycodemax = ARRAY_SIZE(keypad->keymap);

195 196 197
	input_set_drvdata(input_dev, keypad);

	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	input_set_capability(input_dev, EV_MSC, MSC_SCAN);

	for (i = 0; i < keymap_data->keymap_size; i++) {
		unsigned int key = keymap_data->keymap[i];
		unsigned int row = KEY_ROW(key);
		unsigned int col = KEY_COL(key);
		unsigned short keycode = KEY_VAL(key);
		unsigned int scancode = MATRIX_SCAN_CODE(row, col,
							 W90P910_ROW_SHIFT);

		keypad->keymap[scancode] = keycode;
		__set_bit(keycode, input_dev->keybit);
	}
	__clear_bit(KEY_RESERVED, input_dev->keybit);

213

214 215
	error = request_irq(keypad->irq, w90p910_keypad_irq_handler,
			    IRQF_DISABLED, pdev->name, keypad);
216 217
	if (error) {
		dev_err(&pdev->dev, "failed to request IRQ\n");
218
		goto failed_put_clk;
219 220 221 222 223 224 225 226 227
	}

	/* Register the input device */
	error = input_register_device(input_dev);
	if (error) {
		dev_err(&pdev->dev, "failed to register input device\n");
		goto failed_free_irq;
	}

228
	platform_set_drvdata(pdev, keypad);
229 230 231 232 233 234 235 236
	return 0;

failed_free_irq:
	free_irq(irq, pdev);
failed_put_clk:
	clk_put(keypad->clk);
failed_free_io:
	iounmap(keypad->mmio_base);
237
failed_free_res:
238 239
	release_mem_region(res->start, resource_size(res));
failed_free:
240
	input_free_device(input_dev);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	kfree(keypad);
	return error;
}

static int __devexit w90p910_keypad_remove(struct platform_device *pdev)
{
	struct w90p910_keypad *keypad = platform_get_drvdata(pdev);
	struct resource *res;

	free_irq(keypad->irq, pdev);

	clk_put(keypad->clk);

	input_unregister_device(keypad->input_dev);

	iounmap(keypad->mmio_base);
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	release_mem_region(res->start, resource_size(res));

	platform_set_drvdata(pdev, NULL);
	kfree(keypad);
262

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	return 0;
}

static struct platform_driver w90p910_keypad_driver = {
	.probe		= w90p910_keypad_probe,
	.remove		= __devexit_p(w90p910_keypad_remove),
	.driver		= {
		.name	= "w90p910-keypad",
		.owner	= THIS_MODULE,
	},
};

static int __init w90p910_keypad_init(void)
{
	return platform_driver_register(&w90p910_keypad_driver);
}

static void __exit w90p910_keypad_exit(void)
{
	platform_driver_unregister(&w90p910_keypad_driver);
}

module_init(w90p910_keypad_init);
module_exit(w90p910_keypad_exit);

MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
MODULE_DESCRIPTION("w90p910 keypad driver!");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:w90p910-keypad");