omap-keypad.c 9.7 KB
Newer Older
K
Komal Shah 已提交
1 2 3 4 5 6
/*
 * linux/drivers/input/keyboard/omap-keypad.c
 *
 * OMAP Keypad Driver
 *
 * Copyright (C) 2003 Nokia Corporation
7
 * Written by Timo Teräs <ext-timo.teras@nokia.com>
K
Komal Shah 已提交
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
 *
 * Added support for H2 & H3 Keypad
 * Copyright (C) 2004 Texas Instruments
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/types.h>
#include <linux/input.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/mutex.h>
#include <linux/errno.h>
37
#include <linux/slab.h>
38 39
#include <linux/gpio.h>
#include <linux/platform_data/gpio-omap.h>
40
#include <linux/platform_data/keypad-omap.h>
K
Komal Shah 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

#undef NEW_BOARD_LEARNING_MODE

static void omap_kp_tasklet(unsigned long);
static void omap_kp_timer(unsigned long);

static unsigned char keypad_state[8];
static DEFINE_MUTEX(kp_enable_mutex);
static int kp_enable = 1;
static int kp_cur_group = -1;

struct omap_kp {
	struct input_dev *input;
	struct timer_list timer;
	int irq;
	unsigned int rows;
	unsigned int cols;
	unsigned long delay;
	unsigned int debounce;
60
	unsigned short keymap[];
K
Komal Shah 已提交
61 62
};

63
static DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
K
Komal Shah 已提交
64 65 66 67 68 69 70 71

static unsigned int *row_gpios;
static unsigned int *col_gpios;

#ifdef CONFIG_ARCH_OMAP2
static void set_col_gpio_val(struct omap_kp *omap_kp, u8 value)
{
	int col;
72 73 74

	for (col = 0; col < omap_kp->cols; col++)
		gpio_set_value(col_gpios[col], value & (1 << col));
K
Komal Shah 已提交
75 76 77 78 79 80 81 82
}

static u8 get_row_gpio_val(struct omap_kp *omap_kp)
{
	int row;
	u8 value = 0;

	for (row = 0; row < omap_kp->rows; row++) {
83
		if (gpio_get_value(row_gpios[row]))
K
Komal Shah 已提交
84 85 86 87 88 89 90 91 92
			value |= (1 << row);
	}
	return value;
}
#else
#define		set_col_gpio_val(x, y)	do {} while (0)
#define		get_row_gpio_val(x)	0
#endif

93
static irqreturn_t omap_kp_interrupt(int irq, void *dev_id)
K
Komal Shah 已提交
94 95
{
	/* disable keyboard interrupt and schedule for handling */
96
	omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
K
Komal Shah 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

	tasklet_schedule(&kp_tasklet);

	return IRQ_HANDLED;
}

static void omap_kp_timer(unsigned long data)
{
	tasklet_schedule(&kp_tasklet);
}

static void omap_kp_scan_keypad(struct omap_kp *omap_kp, unsigned char *state)
{
	int col = 0;

112 113
	/* disable keyboard interrupt and schedule for handling */
	omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
K
Komal Shah 已提交
114

115 116 117 118 119
	/* read the keypad status */
	omap_writew(0xff, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBC);
	for (col = 0; col < omap_kp->cols; col++) {
		omap_writew(~(1 << col) & 0xff,
			    OMAP1_MPUIO_BASE + OMAP_MPUIO_KBC);
K
Komal Shah 已提交
120

121
		udelay(omap_kp->delay);
K
Komal Shah 已提交
122

123 124
		state[col] = ~omap_readw(OMAP1_MPUIO_BASE +
					 OMAP_MPUIO_KBR_LATCH) & 0xff;
K
Komal Shah 已提交
125
	}
126 127
	omap_writew(0x00, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBC);
	udelay(2);
K
Komal Shah 已提交
128 129 130 131 132
}

static void omap_kp_tasklet(unsigned long data)
{
	struct omap_kp *omap_kp_data = (struct omap_kp *) data;
133 134
	unsigned short *keycodes = omap_kp_data->input->keycode;
	unsigned int row_shift = get_count_order(omap_kp_data->cols);
K
Komal Shah 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	unsigned char new_state[8], changed, key_down = 0;
	int col, row;
	int spurious = 0;

	/* check for any changes */
	omap_kp_scan_keypad(omap_kp_data, new_state);

	/* check for changes and print those */
	for (col = 0; col < omap_kp_data->cols; col++) {
		changed = new_state[col] ^ keypad_state[col];
		key_down |= new_state[col];
		if (changed == 0)
			continue;

		for (row = 0; row < omap_kp_data->rows; row++) {
			int key;
			if (!(changed & (1 << row)))
				continue;
#ifdef NEW_BOARD_LEARNING_MODE
			printk(KERN_INFO "omap-keypad: key %d-%d %s\n", col,
			       row, (new_state[col] & (1 << row)) ?
			       "pressed" : "released");
#else
158
			key = keycodes[MATRIX_SCAN_CODE(row, col, row_shift)];
K
Komal Shah 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
			if (key < 0) {
				printk(KERN_WARNING
				      "omap-keypad: Spurious key event %d-%d\n",
				       col, row);
				/* We scan again after a couple of seconds */
				spurious = 1;
				continue;
			}

			if (!(kp_cur_group == (key & GROUP_MASK) ||
			      kp_cur_group == -1))
				continue;

			kp_cur_group = key & GROUP_MASK;
			input_report_key(omap_kp_data->input, key & ~GROUP_MASK,
					 new_state[col] & (1 << row));
#endif
		}
	}
178
	input_sync(omap_kp_data->input);
K
Komal Shah 已提交
179 180 181
	memcpy(keypad_state, new_state, sizeof(keypad_state));

	if (key_down) {
J
Josh 已提交
182
		int delay = HZ / 20;
K
Komal Shah 已提交
183 184 185 186 187 188 189
		/* some key is pressed - keep irq disabled and use timer
		 * to poll the keypad */
		if (spurious)
			delay = 2 * HZ;
		mod_timer(&omap_kp_data->timer, jiffies + delay);
	} else {
		/* enable interrupts */
190 191
		omap_writew(0, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
		kp_cur_group = -1;
192
	}
K
Komal Shah 已提交
193 194 195 196 197 198 199 200 201 202 203
}

static ssize_t omap_kp_enable_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%u\n", kp_enable);
}

static ssize_t omap_kp_enable_store(struct device *dev, struct device_attribute *attr,
				    const char *buf, size_t count)
{
204
	struct omap_kp *omap_kp = dev_get_drvdata(dev);
K
Komal Shah 已提交
205 206 207 208 209 210 211 212 213 214 215
	int state;

	if (sscanf(buf, "%u", &state) != 1)
		return -EINVAL;

	if ((state != 1) && (state != 0))
		return -EINVAL;

	mutex_lock(&kp_enable_mutex);
	if (state != kp_enable) {
		if (state)
216
			enable_irq(omap_kp->irq);
K
Komal Shah 已提交
217
		else
218
			disable_irq(omap_kp->irq);
K
Komal Shah 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
		kp_enable = state;
	}
	mutex_unlock(&kp_enable_mutex);

	return strnlen(buf, count);
}

static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, omap_kp_enable_show, omap_kp_enable_store);

#ifdef CONFIG_PM
static int omap_kp_suspend(struct platform_device *dev, pm_message_t state)
{
	/* Nothing yet */

	return 0;
}

static int omap_kp_resume(struct platform_device *dev)
{
	/* Nothing yet */

	return 0;
}
#else
#define omap_kp_suspend	NULL
#define omap_kp_resume	NULL
#endif

B
Bill Pemberton 已提交
247
static int omap_kp_probe(struct platform_device *pdev)
K
Komal Shah 已提交
248 249 250 251
{
	struct omap_kp *omap_kp;
	struct input_dev *input_dev;
	struct omap_kp_platform_data *pdata =  pdev->dev.platform_data;
252
	int i, col_idx, row_idx, ret;
253
	unsigned int row_shift, keycodemax;
K
Komal Shah 已提交
254

255 256
	if (!pdata->rows || !pdata->cols || !pdata->keymap_data) {
		printk(KERN_ERR "No rows, cols or keymap_data from pdata\n");
K
Komal Shah 已提交
257 258 259
		return -EINVAL;
	}

260 261 262 263 264
	row_shift = get_count_order(pdata->cols);
	keycodemax = pdata->rows << row_shift;

	omap_kp = kzalloc(sizeof(struct omap_kp) +
			keycodemax * sizeof(unsigned short), GFP_KERNEL);
K
Komal Shah 已提交
265 266 267 268 269 270 271 272 273 274 275 276
	input_dev = input_allocate_device();
	if (!omap_kp || !input_dev) {
		kfree(omap_kp);
		input_free_device(input_dev);
		return -ENOMEM;
	}

	platform_set_drvdata(pdev, omap_kp);

	omap_kp->input = input_dev;

	/* Disable the interrupt for the MPUIO keyboard */
277
	omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
K
Komal Shah 已提交
278 279 280 281 282 283 284 285 286 287 288 289

	if (pdata->delay)
		omap_kp->delay = pdata->delay;

	if (pdata->row_gpios && pdata->col_gpios) {
		row_gpios = pdata->row_gpios;
		col_gpios = pdata->col_gpios;
	}

	omap_kp->rows = pdata->rows;
	omap_kp->cols = pdata->cols;

290 291
	col_idx = 0;
	row_idx = 0;
K
Komal Shah 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305

	setup_timer(&omap_kp->timer, omap_kp_timer, (unsigned long)omap_kp);

	/* get the irq and init timer*/
	tasklet_enable(&kp_tasklet);
	kp_tasklet.data = (unsigned long) omap_kp;

	ret = device_create_file(&pdev->dev, &dev_attr_enable);
	if (ret < 0)
		goto err2;

	/* setup input device */
	input_dev->name = "omap-keypad";
	input_dev->phys = "omap-keypad/input0";
306
	input_dev->dev.parent = &pdev->dev;
K
Komal Shah 已提交
307 308 309 310 311 312

	input_dev->id.bustype = BUS_HOST;
	input_dev->id.vendor = 0x0001;
	input_dev->id.product = 0x0001;
	input_dev->id.version = 0x0100;

313 314 315 316 317 318 319 320 321
	if (pdata->rep)
		__set_bit(EV_REP, input_dev->evbit);

	ret = matrix_keypad_build_keymap(pdata->keymap_data, NULL,
					 pdata->rows, pdata->cols,
					 omap_kp->keymap, input_dev);
	if (ret < 0)
		goto err3;

K
Komal Shah 已提交
322 323 324 325 326 327 328
	ret = input_register_device(omap_kp->input);
	if (ret < 0) {
		printk(KERN_ERR "Unable to register omap-keypad input device\n");
		goto err3;
	}

	if (pdata->dbounce)
329
		omap_writew(0xff, OMAP1_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
K
Komal Shah 已提交
330 331 332

	/* scan current status and enable interrupt */
	omap_kp_scan_keypad(omap_kp, keypad_state);
333 334 335 336 337
	omap_kp->irq = platform_get_irq(pdev, 0);
	if (omap_kp->irq >= 0) {
		if (request_irq(omap_kp->irq, omap_kp_interrupt, 0,
				"omap-keypad", omap_kp) < 0)
			goto err4;
K
Komal Shah 已提交
338
	}
339 340
	omap_writew(0, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);

K
Komal Shah 已提交
341
	return 0;
342

K
Komal Shah 已提交
343 344 345 346 347 348
err4:
	input_unregister_device(omap_kp->input);
	input_dev = NULL;
err3:
	device_remove_file(&pdev->dev, &dev_attr_enable);
err2:
J
Josh 已提交
349
	for (i = row_idx - 1; i >= 0; i--)
350
		gpio_free(row_gpios[i]);
J
Josh 已提交
351
	for (i = col_idx - 1; i >= 0; i--)
352
		gpio_free(col_gpios[i]);
K
Komal Shah 已提交
353 354 355 356 357 358 359

	kfree(omap_kp);
	input_free_device(input_dev);

	return -EINVAL;
}

B
Bill Pemberton 已提交
360
static int omap_kp_remove(struct platform_device *pdev)
K
Komal Shah 已提交
361 362 363 364 365
{
	struct omap_kp *omap_kp = platform_get_drvdata(pdev);

	/* disable keypad interrupt handling */
	tasklet_disable(&kp_tasklet);
366 367
	omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
	free_irq(omap_kp->irq, omap_kp);
K
Komal Shah 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381

	del_timer_sync(&omap_kp->timer);
	tasklet_kill(&kp_tasklet);

	/* unregister everything */
	input_unregister_device(omap_kp->input);

	kfree(omap_kp);

	return 0;
}

static struct platform_driver omap_kp_driver = {
	.probe		= omap_kp_probe,
B
Bill Pemberton 已提交
382
	.remove		= omap_kp_remove,
K
Komal Shah 已提交
383 384 385 386
	.suspend	= omap_kp_suspend,
	.resume		= omap_kp_resume,
	.driver		= {
		.name	= "omap-keypad",
387
		.owner	= THIS_MODULE,
K
Komal Shah 已提交
388 389
	},
};
390
module_platform_driver(omap_kp_driver);
K
Komal Shah 已提交
391

392
MODULE_AUTHOR("Timo Teräs");
K
Komal Shah 已提交
393 394
MODULE_DESCRIPTION("OMAP Keypad Driver");
MODULE_LICENSE("GPL");
395
MODULE_ALIAS("platform:omap-keypad");