locomokbd.c 8.0 KB
Newer Older
L
Linus Torvalds 已提交
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 44
/*
 *  Copyright (c) 2005 John Lenz
 *
 * Based on from xtkbd.c
 */

/*
 * LoCoMo keyboard driver for Linux/ARM
 */

/*
 * 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/config.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>

#include <asm/hardware/locomo.h>
#include <asm/irq.h>

MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
MODULE_DESCRIPTION("LoCoMo keyboard driver");
MODULE_LICENSE("GPL");

45
#define LOCOMOKBD_NUMKEYS	128
L
Linus Torvalds 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

#define KEY_ACTIVITY		KEY_F16
#define KEY_CONTACT		KEY_F18
#define KEY_CENTER		KEY_F15

static unsigned char locomokbd_keycode[LOCOMOKBD_NUMKEYS] = {
	0, KEY_ESC, KEY_ACTIVITY, 0, 0, 0, 0, 0, 0, 0,				/* 0 - 9 */
	0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_HOME, KEY_CONTACT,			/* 10 - 19 */
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,						/* 20 - 29 */
	0, 0, 0, KEY_CENTER, 0, KEY_MAIL, 0, 0, 0, 0,				/* 30 - 39 */
	0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RIGHT,					/* 40 - 49 */
	KEY_UP, KEY_LEFT, 0, 0, KEY_P, 0, KEY_O, KEY_I, KEY_Y, KEY_T,		/* 50 - 59 */
	KEY_E, KEY_W, 0, 0, 0, 0, KEY_DOWN, KEY_ENTER, 0, 0,			/* 60 - 69 */
	KEY_BACKSPACE, 0, KEY_L, KEY_U, KEY_H, KEY_R, KEY_D, KEY_Q, 0, 0,	/* 70 - 79 */
	0, 0, 0, 0, 0, 0, KEY_ENTER, KEY_RIGHTSHIFT, KEY_K, KEY_J,		/* 80 - 89 */
	KEY_G, KEY_F, KEY_X, KEY_S, 0, 0, 0, 0, 0, 0,				/* 90 - 99 */
	0, 0, KEY_DOT, 0, KEY_COMMA, KEY_N, KEY_B, KEY_C, KEY_Z, KEY_A,		/* 100 - 109 */
	KEY_LEFTSHIFT, KEY_TAB, KEY_LEFTCTRL, 0, 0, 0, 0, 0, 0, 0,		/* 110 - 119 */
64
	KEY_M, KEY_SPACE, KEY_V, KEY_APOSTROPHE, KEY_SLASH, 0, 0, 0		/* 120 - 128 */
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78
};

#define KB_ROWS			16
#define KB_COLS			8
#define KB_ROWMASK(r)		(1 << (r))
#define SCANCODE(c,r)		( ((c)<<4) + (r) + 1 )
#define	NR_SCANCODES		128

#define KB_DELAY		8
#define SCAN_INTERVAL		(HZ/10)
#define LOCOMOKBD_PRESSED	1

struct locomokbd {
	unsigned char keycode[LOCOMOKBD_NUMKEYS];
79
	struct input_dev *input;
L
Linus Torvalds 已提交
80 81 82 83 84
	char phys[32];

	struct locomo_dev *ldev;
	unsigned long base;
	spinlock_t lock;
85

L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93 94 95 96 97
	struct timer_list timer;
};

/* helper functions for reading the keyboard matrix */
static inline void locomokbd_charge_all(unsigned long membase)
{
	locomo_writel(0x00FF, membase + LOCOMO_KSC);
}

static inline void locomokbd_activate_all(unsigned long membase)
{
	unsigned long r;
98

L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	locomo_writel(0, membase + LOCOMO_KSC);
	r = locomo_readl(membase + LOCOMO_KIC);
	r &= 0xFEFF;
	locomo_writel(r, membase + LOCOMO_KIC);
}

static inline void locomokbd_activate_col(unsigned long membase, int col)
{
	unsigned short nset;
	unsigned short nbset;

	nset = 0xFF & ~(1 << col);
	nbset = (nset << 8) + nset;
	locomo_writel(nbset, membase + LOCOMO_KSC);
}

static inline void locomokbd_reset_col(unsigned long membase, int col)
{
	unsigned short nbset;

	nbset = ((0xFF & ~(1 << col)) << 8) + 0xFF;
	locomo_writel(nbset, membase + LOCOMO_KSC);
}

/*
 * The LoCoMo keyboard only generates interrupts when a key is pressed.
 * So when a key is pressed, we enable a timer.  This timer scans the
 * keyboard, and this is how we detect when the key is released.
 */

/* Scan the hardware keyboard and push any changes up through the input layer */
130
static void locomokbd_scankeyboard(struct locomokbd *locomokbd, struct pt_regs *regs)
L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138
{
	unsigned int row, col, rowd, scancode;
	unsigned long flags;
	unsigned int num_pressed;
	unsigned long membase = locomokbd->base;

	spin_lock_irqsave(&locomokbd->lock, flags);

139
	input_regs(locomokbd->input, regs);
140

L
Linus Torvalds 已提交
141 142 143 144 145 146 147
	locomokbd_charge_all(membase);

	num_pressed = 0;
	for (col = 0; col < KB_COLS; col++) {

		locomokbd_activate_col(membase, col);
		udelay(KB_DELAY);
148

L
Linus Torvalds 已提交
149
		rowd = ~locomo_readl(membase + LOCOMO_KIB);
150
		for (row = 0; row < KB_ROWS; row++) {
L
Linus Torvalds 已提交
151 152 153
			scancode = SCANCODE(col, row);
			if (rowd & KB_ROWMASK(row)) {
				num_pressed += 1;
154
				input_report_key(locomokbd->input, locomokbd->keycode[scancode], 1);
L
Linus Torvalds 已提交
155
			} else {
156
				input_report_key(locomokbd->input, locomokbd->keycode[scancode], 0);
L
Linus Torvalds 已提交
157 158 159 160 161 162
			}
		}
		locomokbd_reset_col(membase, col);
	}
	locomokbd_activate_all(membase);

163
	input_sync(locomokbd->input);
L
Linus Torvalds 已提交
164 165 166 167 168 169 170 171

	/* if any keys are pressed, enable the timer */
	if (num_pressed)
		mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL);

	spin_unlock_irqrestore(&locomokbd->lock, flags);
}

172
/*
L
Linus Torvalds 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
 * LoCoMo keyboard interrupt handler.
 */
static irqreturn_t locomokbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct locomokbd *locomokbd = dev_id;
	/** wait chattering delay **/
	udelay(100);

	locomokbd_scankeyboard(locomokbd, regs);

	return IRQ_HANDLED;
}

/*
 * LoCoMo timer checking for released keys
 */
static void locomokbd_timer_callback(unsigned long data)
{
	struct locomokbd *locomokbd = (struct locomokbd *) data;
	locomokbd_scankeyboard(locomokbd, NULL);
}

static int locomokbd_probe(struct locomo_dev *dev)
{
	struct locomokbd *locomokbd;
198
	struct input_dev *input_dev;
L
Linus Torvalds 已提交
199 200
	int i, ret;

201 202 203 204 205 206
	locomokbd = kzalloc(sizeof(struct locomokbd), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (!locomokbd || !input_dev) {
		ret = -ENOMEM;
		goto free;
	}
L
Linus Torvalds 已提交
207 208

	/* try and claim memory region */
209 210
	if (!request_mem_region((unsigned long) dev->mapbase,
				dev->length,
L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
				LOCOMO_DRIVER_NAME(dev))) {
		ret = -EBUSY;
		printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n");
		goto free;
	}

	locomokbd->ldev = dev;
	locomo_set_drvdata(dev, locomokbd);

	locomokbd->base = (unsigned long) dev->mapbase;

	spin_lock_init(&locomokbd->lock);

	init_timer(&locomokbd->timer);
	locomokbd->timer.function = locomokbd_timer_callback;
	locomokbd->timer.data = (unsigned long) locomokbd;

228 229 230 231 232
	locomokbd->input = input_dev;
	strcpy(locomokbd->phys, "locomokbd/input0");

	input_dev->name = "LoCoMo keyboard";
	input_dev->phys = locomokbd->phys;
233
	input_dev->id.bustype = BUS_HOST;
234 235 236 237
	input_dev->id.vendor = 0x0001;
	input_dev->id.product = 0x0001;
	input_dev->id.version = 0x0100;
	input_dev->private = locomokbd;
238

239 240 241 242
	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
	input_dev->keycode = locomokbd->keycode;
	input_dev->keycodesize = sizeof(unsigned char);
	input_dev->keycodemax = ARRAY_SIZE(locomokbd_keycode);
L
Linus Torvalds 已提交
243 244 245

	memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode));
	for (i = 0; i < LOCOMOKBD_NUMKEYS; i++)
246 247
		set_bit(locomokbd->keycode[i], input_dev->keybit);
	clear_bit(0, input_dev->keybit);
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255

	/* attempt to get the interrupt */
	ret = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd);
	if (ret) {
		printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n");
		goto out;
	}

256
	input_register_device(locomokbd->input);
L
Linus Torvalds 已提交
257 258 259 260 261 262 263

	return 0;

out:
	release_mem_region((unsigned long) dev->mapbase, dev->length);
	locomo_set_drvdata(dev, NULL);
free:
264
	input_free_device(input_dev);
L
Linus Torvalds 已提交
265 266 267 268 269 270 271 272
	kfree(locomokbd);

	return ret;
}

static int locomokbd_remove(struct locomo_dev *dev)
{
	struct locomokbd *locomokbd = locomo_get_drvdata(dev);
273

L
Linus Torvalds 已提交
274 275 276
	free_irq(dev->irq[0], locomokbd);

	del_timer_sync(&locomokbd->timer);
277

278
	input_unregister_device(locomokbd->input);
L
Linus Torvalds 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	locomo_set_drvdata(dev, NULL);

	release_mem_region((unsigned long) dev->mapbase, dev->length);

	kfree(locomokbd);

	return 0;
}

static struct locomo_driver keyboard_driver = {
	.drv = {
		.name = "locomokbd"
	},
	.devid	= LOCOMO_DEVID_KEYBOARD,
	.probe	= locomokbd_probe,
	.remove	= locomokbd_remove,
};

static int __init locomokbd_init(void)
{
	return locomo_driver_register(&keyboard_driver);
}

static void __exit locomokbd_exit(void)
{
	locomo_driver_unregister(&keyboard_driver);
}

module_init(locomokbd_init);
module_exit(locomokbd_exit);