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; 18936bd262bSRussell 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; 1962b03b60eSDmitry Torokhov int i, err; 1971da177e4SLinus Torvalds 198438c9da5SDmitry Torokhov locomokbd = kzalloc(sizeof(struct locomokbd), GFP_KERNEL); 199438c9da5SDmitry Torokhov input_dev = input_allocate_device(); 200438c9da5SDmitry Torokhov if (!locomokbd || !input_dev) { 2012b03b60eSDmitry Torokhov err = -ENOMEM; 2022b03b60eSDmitry Torokhov goto err_free_mem; 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))) { 2092b03b60eSDmitry Torokhov err = -EBUSY; 2101da177e4SLinus Torvalds printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n"); 2112b03b60eSDmitry Torokhov goto err_free_mem; 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; 234*469ba4dfSDmitry Torokhov input_dev->dev.parent = &dev->dev; 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 */ 2472b03b60eSDmitry Torokhov err = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd); 2482b03b60eSDmitry Torokhov if (err) { 2491da177e4SLinus Torvalds printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n"); 2502b03b60eSDmitry Torokhov goto err_release_region; 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 2532b03b60eSDmitry Torokhov err = input_register_device(locomokbd->input); 2542b03b60eSDmitry Torokhov if (err) 2552b03b60eSDmitry Torokhov goto err_free_irq; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds return 0; 2581da177e4SLinus Torvalds 2592b03b60eSDmitry Torokhov err_free_irq: 2602b03b60eSDmitry Torokhov free_irq(dev->irq[0], locomokbd); 2612b03b60eSDmitry Torokhov err_release_region: 2621da177e4SLinus Torvalds release_mem_region((unsigned long) dev->mapbase, dev->length); 2631da177e4SLinus Torvalds locomo_set_drvdata(dev, NULL); 2642b03b60eSDmitry Torokhov err_free_mem: 265438c9da5SDmitry Torokhov input_free_device(input_dev); 2661da177e4SLinus Torvalds kfree(locomokbd); 2671da177e4SLinus Torvalds 2682b03b60eSDmitry Torokhov return err; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds static int locomokbd_remove(struct locomo_dev *dev) 2721da177e4SLinus Torvalds { 2731da177e4SLinus Torvalds struct locomokbd *locomokbd = locomo_get_drvdata(dev); 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds free_irq(dev->irq[0], locomokbd); 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds del_timer_sync(&locomokbd->timer); 2781da177e4SLinus Torvalds 279438c9da5SDmitry Torokhov input_unregister_device(locomokbd->input); 2801da177e4SLinus Torvalds locomo_set_drvdata(dev, NULL); 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds release_mem_region((unsigned long) dev->mapbase, dev->length); 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds kfree(locomokbd); 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds return 0; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds static struct locomo_driver keyboard_driver = { 2901da177e4SLinus Torvalds .drv = { 2911da177e4SLinus Torvalds .name = "locomokbd" 2921da177e4SLinus Torvalds }, 2931da177e4SLinus Torvalds .devid = LOCOMO_DEVID_KEYBOARD, 2941da177e4SLinus Torvalds .probe = locomokbd_probe, 2951da177e4SLinus Torvalds .remove = locomokbd_remove, 2961da177e4SLinus Torvalds }; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds static int __init locomokbd_init(void) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds return locomo_driver_register(&keyboard_driver); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds static void __exit locomokbd_exit(void) 3041da177e4SLinus Torvalds { 3051da177e4SLinus Torvalds locomo_driver_unregister(&keyboard_driver); 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds module_init(locomokbd_init); 3091da177e4SLinus Torvalds module_exit(locomokbd_exit); 310