xref: /linux/drivers/input/keyboard/locomokbd.c (revision 36bd262b3f2ac723dadd20ce35539c8c738877f1)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Copyright (c) 2005 John Lenz
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Based on from xtkbd.c
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * LoCoMo keyboard driver for Linux/ARM
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /*
121da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
131da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
141da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
151da177e4SLinus Torvalds  * (at your option) any later version.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
181da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
191da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
201da177e4SLinus Torvalds  * GNU General Public License for more details.
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
231da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
241da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include <linux/slab.h>
291da177e4SLinus Torvalds #include <linux/module.h>
301da177e4SLinus Torvalds #include <linux/init.h>
311da177e4SLinus Torvalds #include <linux/input.h>
321da177e4SLinus Torvalds #include <linux/delay.h>
331da177e4SLinus Torvalds #include <linux/device.h>
341da177e4SLinus Torvalds #include <linux/interrupt.h>
351da177e4SLinus Torvalds #include <linux/ioport.h>
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include <asm/hardware/locomo.h>
381da177e4SLinus Torvalds #include <asm/irq.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
411da177e4SLinus Torvalds MODULE_DESCRIPTION("LoCoMo keyboard driver");
421da177e4SLinus Torvalds MODULE_LICENSE("GPL");
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #define LOCOMOKBD_NUMKEYS	128
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #define KEY_ACTIVITY		KEY_F16
471da177e4SLinus Torvalds #define KEY_CONTACT		KEY_F18
481da177e4SLinus Torvalds #define KEY_CENTER		KEY_F15
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static unsigned char locomokbd_keycode[LOCOMOKBD_NUMKEYS] = {
511da177e4SLinus Torvalds 	0, KEY_ESC, KEY_ACTIVITY, 0, 0, 0, 0, 0, 0, 0,				/* 0 - 9 */
521da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_HOME, KEY_CONTACT,			/* 10 - 19 */
531da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,						/* 20 - 29 */
541da177e4SLinus Torvalds 	0, 0, 0, KEY_CENTER, 0, KEY_MAIL, 0, 0, 0, 0,				/* 30 - 39 */
551da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RIGHT,					/* 40 - 49 */
561da177e4SLinus Torvalds 	KEY_UP, KEY_LEFT, 0, 0, KEY_P, 0, KEY_O, KEY_I, KEY_Y, KEY_T,		/* 50 - 59 */
571da177e4SLinus Torvalds 	KEY_E, KEY_W, 0, 0, 0, 0, KEY_DOWN, KEY_ENTER, 0, 0,			/* 60 - 69 */
581da177e4SLinus Torvalds 	KEY_BACKSPACE, 0, KEY_L, KEY_U, KEY_H, KEY_R, KEY_D, KEY_Q, 0, 0,	/* 70 - 79 */
591da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0, KEY_ENTER, KEY_RIGHTSHIFT, KEY_K, KEY_J,		/* 80 - 89 */
601da177e4SLinus Torvalds 	KEY_G, KEY_F, KEY_X, KEY_S, 0, 0, 0, 0, 0, 0,				/* 90 - 99 */
611da177e4SLinus Torvalds 	0, 0, KEY_DOT, 0, KEY_COMMA, KEY_N, KEY_B, KEY_C, KEY_Z, KEY_A,		/* 100 - 109 */
621da177e4SLinus Torvalds 	KEY_LEFTSHIFT, KEY_TAB, KEY_LEFTCTRL, 0, 0, 0, 0, 0, 0, 0,		/* 110 - 119 */
631da177e4SLinus Torvalds 	KEY_M, KEY_SPACE, KEY_V, KEY_APOSTROPHE, KEY_SLASH, 0, 0, 0		/* 120 - 128 */
641da177e4SLinus Torvalds };
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #define KB_ROWS			16
671da177e4SLinus Torvalds #define KB_COLS			8
681da177e4SLinus Torvalds #define KB_ROWMASK(r)		(1 << (r))
691da177e4SLinus Torvalds #define SCANCODE(c,r)		( ((c)<<4) + (r) + 1 )
701da177e4SLinus Torvalds #define	NR_SCANCODES		128
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #define KB_DELAY		8
731da177e4SLinus Torvalds #define SCAN_INTERVAL		(HZ/10)
741da177e4SLinus Torvalds #define LOCOMOKBD_PRESSED	1
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds struct locomokbd {
771da177e4SLinus Torvalds 	unsigned char keycode[LOCOMOKBD_NUMKEYS];
78438c9da5SDmitry Torokhov 	struct input_dev *input;
791da177e4SLinus Torvalds 	char phys[32];
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	struct locomo_dev *ldev;
821da177e4SLinus Torvalds 	unsigned long base;
831da177e4SLinus Torvalds 	spinlock_t lock;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	struct timer_list timer;
861da177e4SLinus Torvalds };
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /* helper functions for reading the keyboard matrix */
891da177e4SLinus Torvalds static inline void locomokbd_charge_all(unsigned long membase)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	locomo_writel(0x00FF, membase + LOCOMO_KSC);
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds static inline void locomokbd_activate_all(unsigned long membase)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	unsigned long r;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	locomo_writel(0, membase + LOCOMO_KSC);
991da177e4SLinus Torvalds 	r = locomo_readl(membase + LOCOMO_KIC);
1001da177e4SLinus Torvalds 	r &= 0xFEFF;
1011da177e4SLinus Torvalds 	locomo_writel(r, membase + LOCOMO_KIC);
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds static inline void locomokbd_activate_col(unsigned long membase, int col)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	unsigned short nset;
1071da177e4SLinus Torvalds 	unsigned short nbset;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	nset = 0xFF & ~(1 << col);
1101da177e4SLinus Torvalds 	nbset = (nset << 8) + nset;
1111da177e4SLinus Torvalds 	locomo_writel(nbset, membase + LOCOMO_KSC);
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds static inline void locomokbd_reset_col(unsigned long membase, int col)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	unsigned short nbset;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	nbset = ((0xFF & ~(1 << col)) << 8) + 0xFF;
1191da177e4SLinus Torvalds 	locomo_writel(nbset, membase + LOCOMO_KSC);
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /*
1231da177e4SLinus Torvalds  * The LoCoMo keyboard only generates interrupts when a key is pressed.
1241da177e4SLinus Torvalds  * So when a key is pressed, we enable a timer.  This timer scans the
1251da177e4SLinus Torvalds  * keyboard, and this is how we detect when the key is released.
1261da177e4SLinus Torvalds  */
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /* Scan the hardware keyboard and push any changes up through the input layer */
1297d12e780SDavid Howells static void locomokbd_scankeyboard(struct locomokbd *locomokbd)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	unsigned int row, col, rowd, scancode;
1321da177e4SLinus Torvalds 	unsigned long flags;
1331da177e4SLinus Torvalds 	unsigned int num_pressed;
1341da177e4SLinus Torvalds 	unsigned long membase = locomokbd->base;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	spin_lock_irqsave(&locomokbd->lock, flags);
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	locomokbd_charge_all(membase);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	num_pressed = 0;
1411da177e4SLinus Torvalds 	for (col = 0; col < KB_COLS; col++) {
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 		locomokbd_activate_col(membase, col);
1441da177e4SLinus Torvalds 		udelay(KB_DELAY);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 		rowd = ~locomo_readl(membase + LOCOMO_KIB);
1471da177e4SLinus Torvalds 		for (row = 0; row < KB_ROWS; row++) {
1481da177e4SLinus Torvalds 			scancode = SCANCODE(col, row);
1491da177e4SLinus Torvalds 			if (rowd & KB_ROWMASK(row)) {
1501da177e4SLinus Torvalds 				num_pressed += 1;
151438c9da5SDmitry Torokhov 				input_report_key(locomokbd->input, locomokbd->keycode[scancode], 1);
1521da177e4SLinus Torvalds 			} else {
153438c9da5SDmitry Torokhov 				input_report_key(locomokbd->input, locomokbd->keycode[scancode], 0);
1541da177e4SLinus Torvalds 			}
1551da177e4SLinus Torvalds 		}
1561da177e4SLinus Torvalds 		locomokbd_reset_col(membase, col);
1571da177e4SLinus Torvalds 	}
1581da177e4SLinus Torvalds 	locomokbd_activate_all(membase);
1591da177e4SLinus Torvalds 
160438c9da5SDmitry Torokhov 	input_sync(locomokbd->input);
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	/* if any keys are pressed, enable the timer */
1631da177e4SLinus Torvalds 	if (num_pressed)
1641da177e4SLinus Torvalds 		mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	spin_unlock_irqrestore(&locomokbd->lock, flags);
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /*
1701da177e4SLinus Torvalds  * LoCoMo keyboard interrupt handler.
1711da177e4SLinus Torvalds  */
1727d12e780SDavid Howells static irqreturn_t locomokbd_interrupt(int irq, void *dev_id)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	struct locomokbd *locomokbd = dev_id;
1751da177e4SLinus Torvalds 	/** wait chattering delay **/
1761da177e4SLinus Torvalds 	udelay(100);
1771da177e4SLinus Torvalds 
1787d12e780SDavid Howells 	locomokbd_scankeyboard(locomokbd);
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	return IRQ_HANDLED;
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /*
1841da177e4SLinus Torvalds  * LoCoMo timer checking for released keys
1851da177e4SLinus Torvalds  */
1861da177e4SLinus Torvalds static void locomokbd_timer_callback(unsigned long data)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	struct locomokbd *locomokbd = (struct locomokbd *) data;
189*36bd262bSRussell King 	locomokbd_scankeyboard(locomokbd);
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds static int locomokbd_probe(struct locomo_dev *dev)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	struct locomokbd *locomokbd;
195438c9da5SDmitry Torokhov 	struct input_dev *input_dev;
1961da177e4SLinus Torvalds 	int i, ret;
1971da177e4SLinus Torvalds 
198438c9da5SDmitry Torokhov 	locomokbd = kzalloc(sizeof(struct locomokbd), GFP_KERNEL);
199438c9da5SDmitry Torokhov 	input_dev = input_allocate_device();
200438c9da5SDmitry Torokhov 	if (!locomokbd || !input_dev) {
201438c9da5SDmitry Torokhov 		ret = -ENOMEM;
202438c9da5SDmitry Torokhov 		goto free;
203438c9da5SDmitry Torokhov 	}
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	/* try and claim memory region */
2061da177e4SLinus Torvalds 	if (!request_mem_region((unsigned long) dev->mapbase,
2071da177e4SLinus Torvalds 				dev->length,
2081da177e4SLinus Torvalds 				LOCOMO_DRIVER_NAME(dev))) {
2091da177e4SLinus Torvalds 		ret = -EBUSY;
2101da177e4SLinus Torvalds 		printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n");
2111da177e4SLinus Torvalds 		goto free;
2121da177e4SLinus Torvalds 	}
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	locomokbd->ldev = dev;
2151da177e4SLinus Torvalds 	locomo_set_drvdata(dev, locomokbd);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	locomokbd->base = (unsigned long) dev->mapbase;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	spin_lock_init(&locomokbd->lock);
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	init_timer(&locomokbd->timer);
2221da177e4SLinus Torvalds 	locomokbd->timer.function = locomokbd_timer_callback;
2231da177e4SLinus Torvalds 	locomokbd->timer.data = (unsigned long) locomokbd;
2241da177e4SLinus Torvalds 
225438c9da5SDmitry Torokhov 	locomokbd->input = input_dev;
226438c9da5SDmitry Torokhov 	strcpy(locomokbd->phys, "locomokbd/input0");
2271da177e4SLinus Torvalds 
228438c9da5SDmitry Torokhov 	input_dev->name = "LoCoMo keyboard";
229438c9da5SDmitry Torokhov 	input_dev->phys = locomokbd->phys;
230d7a767ddSPavel Machek 	input_dev->id.bustype = BUS_HOST;
231438c9da5SDmitry Torokhov 	input_dev->id.vendor = 0x0001;
232438c9da5SDmitry Torokhov 	input_dev->id.product = 0x0001;
233438c9da5SDmitry Torokhov 	input_dev->id.version = 0x0100;
234438c9da5SDmitry Torokhov 	input_dev->private = locomokbd;
235438c9da5SDmitry Torokhov 
236438c9da5SDmitry Torokhov 	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
237438c9da5SDmitry Torokhov 	input_dev->keycode = locomokbd->keycode;
238438c9da5SDmitry Torokhov 	input_dev->keycodesize = sizeof(unsigned char);
239438c9da5SDmitry Torokhov 	input_dev->keycodemax = ARRAY_SIZE(locomokbd_keycode);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode));
2421da177e4SLinus Torvalds 	for (i = 0; i < LOCOMOKBD_NUMKEYS; i++)
243438c9da5SDmitry Torokhov 		set_bit(locomokbd->keycode[i], input_dev->keybit);
244438c9da5SDmitry Torokhov 	clear_bit(0, input_dev->keybit);
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	/* attempt to get the interrupt */
2471da177e4SLinus Torvalds 	ret = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd);
2481da177e4SLinus Torvalds 	if (ret) {
2491da177e4SLinus Torvalds 		printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n");
2501da177e4SLinus Torvalds 		goto out;
2511da177e4SLinus Torvalds 	}
2521da177e4SLinus Torvalds 
253438c9da5SDmitry Torokhov 	input_register_device(locomokbd->input);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	return 0;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds out:
2581da177e4SLinus Torvalds 	release_mem_region((unsigned long) dev->mapbase, dev->length);
2591da177e4SLinus Torvalds 	locomo_set_drvdata(dev, NULL);
2601da177e4SLinus Torvalds free:
261438c9da5SDmitry Torokhov 	input_free_device(input_dev);
2621da177e4SLinus Torvalds 	kfree(locomokbd);
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	return ret;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds static int locomokbd_remove(struct locomo_dev *dev)
2681da177e4SLinus Torvalds {
2691da177e4SLinus Torvalds 	struct locomokbd *locomokbd = locomo_get_drvdata(dev);
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	free_irq(dev->irq[0], locomokbd);
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	del_timer_sync(&locomokbd->timer);
2741da177e4SLinus Torvalds 
275438c9da5SDmitry Torokhov 	input_unregister_device(locomokbd->input);
2761da177e4SLinus Torvalds 	locomo_set_drvdata(dev, NULL);
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	release_mem_region((unsigned long) dev->mapbase, dev->length);
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	kfree(locomokbd);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	return 0;
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds static struct locomo_driver keyboard_driver = {
2861da177e4SLinus Torvalds 	.drv = {
2871da177e4SLinus Torvalds 		.name = "locomokbd"
2881da177e4SLinus Torvalds 	},
2891da177e4SLinus Torvalds 	.devid	= LOCOMO_DEVID_KEYBOARD,
2901da177e4SLinus Torvalds 	.probe	= locomokbd_probe,
2911da177e4SLinus Torvalds 	.remove	= locomokbd_remove,
2921da177e4SLinus Torvalds };
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static int __init locomokbd_init(void)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	return locomo_driver_register(&keyboard_driver);
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds static void __exit locomokbd_exit(void)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	locomo_driver_unregister(&keyboard_driver);
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds module_init(locomokbd_init);
3051da177e4SLinus Torvalds module_exit(locomokbd_exit);
306