11da177e4SLinus Torvalds /* 2*e6cdd156SHelge Deller * LoCoMo keyboard driver for Linux-based ARM PDAs: 3*e6cdd156SHelge Deller * - SHARP Zaurus Collie (SL-5500) 4*e6cdd156SHelge Deller * - SHARP Zaurus Poodle (SL-5600) 51da177e4SLinus Torvalds * 6*e6cdd156SHelge Deller * Copyright (c) 2005 John Lenz 71da177e4SLinus Torvalds * Based on from xtkbd.c 8*e6cdd156SHelge Deller * 9*e6cdd156SHelge Deller * 101da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 111da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 121da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 131da177e4SLinus Torvalds * (at your option) any later version. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 161da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 171da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 181da177e4SLinus Torvalds * GNU General Public License for more details. 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 211da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 221da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include <linux/slab.h> 271da177e4SLinus Torvalds #include <linux/module.h> 281da177e4SLinus Torvalds #include <linux/init.h> 291da177e4SLinus Torvalds #include <linux/input.h> 301da177e4SLinus Torvalds #include <linux/delay.h> 311da177e4SLinus Torvalds #include <linux/device.h> 321da177e4SLinus Torvalds #include <linux/interrupt.h> 331da177e4SLinus Torvalds #include <linux/ioport.h> 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds #include <asm/hardware/locomo.h> 361da177e4SLinus Torvalds #include <asm/irq.h> 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>"); 391da177e4SLinus Torvalds MODULE_DESCRIPTION("LoCoMo keyboard driver"); 401da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #define LOCOMOKBD_NUMKEYS 128 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #define KEY_ACTIVITY KEY_F16 451da177e4SLinus Torvalds #define KEY_CONTACT KEY_F18 461da177e4SLinus Torvalds #define KEY_CENTER KEY_F15 471da177e4SLinus Torvalds 48*e6cdd156SHelge Deller static const unsigned char 49*e6cdd156SHelge Deller locomokbd_keycode[LOCOMOKBD_NUMKEYS] __devinitconst = { 501da177e4SLinus Torvalds 0, KEY_ESC, KEY_ACTIVITY, 0, 0, 0, 0, 0, 0, 0, /* 0 - 9 */ 511da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_HOME, KEY_CONTACT, /* 10 - 19 */ 521da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 29 */ 531da177e4SLinus Torvalds 0, 0, 0, KEY_CENTER, 0, KEY_MAIL, 0, 0, 0, 0, /* 30 - 39 */ 541da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RIGHT, /* 40 - 49 */ 551da177e4SLinus Torvalds KEY_UP, KEY_LEFT, 0, 0, KEY_P, 0, KEY_O, KEY_I, KEY_Y, KEY_T, /* 50 - 59 */ 561da177e4SLinus Torvalds KEY_E, KEY_W, 0, 0, 0, 0, KEY_DOWN, KEY_ENTER, 0, 0, /* 60 - 69 */ 571da177e4SLinus Torvalds KEY_BACKSPACE, 0, KEY_L, KEY_U, KEY_H, KEY_R, KEY_D, KEY_Q, 0, 0, /* 70 - 79 */ 581da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, KEY_ENTER, KEY_RIGHTSHIFT, KEY_K, KEY_J, /* 80 - 89 */ 591da177e4SLinus Torvalds KEY_G, KEY_F, KEY_X, KEY_S, 0, 0, 0, 0, 0, 0, /* 90 - 99 */ 601da177e4SLinus Torvalds 0, 0, KEY_DOT, 0, KEY_COMMA, KEY_N, KEY_B, KEY_C, KEY_Z, KEY_A, /* 100 - 109 */ 611da177e4SLinus Torvalds KEY_LEFTSHIFT, KEY_TAB, KEY_LEFTCTRL, 0, 0, 0, 0, 0, 0, 0, /* 110 - 119 */ 621da177e4SLinus Torvalds KEY_M, KEY_SPACE, KEY_V, KEY_APOSTROPHE, KEY_SLASH, 0, 0, 0 /* 120 - 128 */ 631da177e4SLinus Torvalds }; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #define KB_ROWS 16 661da177e4SLinus Torvalds #define KB_COLS 8 671da177e4SLinus Torvalds #define KB_ROWMASK(r) (1 << (r)) 681da177e4SLinus Torvalds #define SCANCODE(c,r) ( ((c)<<4) + (r) + 1 ) 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds #define KB_DELAY 8 711da177e4SLinus Torvalds #define SCAN_INTERVAL (HZ/10) 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds struct locomokbd { 741da177e4SLinus Torvalds unsigned char keycode[LOCOMOKBD_NUMKEYS]; 75438c9da5SDmitry Torokhov struct input_dev *input; 761da177e4SLinus Torvalds char phys[32]; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds unsigned long base; 791da177e4SLinus Torvalds spinlock_t lock; 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds struct timer_list timer; 82*e6cdd156SHelge Deller unsigned long suspend_jiffies; 83*e6cdd156SHelge Deller unsigned int count_cancel; 841da177e4SLinus Torvalds }; 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds /* helper functions for reading the keyboard matrix */ 871da177e4SLinus Torvalds static inline void locomokbd_charge_all(unsigned long membase) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds locomo_writel(0x00FF, membase + LOCOMO_KSC); 901da177e4SLinus Torvalds } 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds static inline void locomokbd_activate_all(unsigned long membase) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds unsigned long r; 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds locomo_writel(0, membase + LOCOMO_KSC); 971da177e4SLinus Torvalds r = locomo_readl(membase + LOCOMO_KIC); 981da177e4SLinus Torvalds r &= 0xFEFF; 991da177e4SLinus Torvalds locomo_writel(r, membase + LOCOMO_KIC); 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds static inline void locomokbd_activate_col(unsigned long membase, int col) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds unsigned short nset; 1051da177e4SLinus Torvalds unsigned short nbset; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds nset = 0xFF & ~(1 << col); 1081da177e4SLinus Torvalds nbset = (nset << 8) + nset; 1091da177e4SLinus Torvalds locomo_writel(nbset, membase + LOCOMO_KSC); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds static inline void locomokbd_reset_col(unsigned long membase, int col) 1131da177e4SLinus Torvalds { 1141da177e4SLinus Torvalds unsigned short nbset; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds nbset = ((0xFF & ~(1 << col)) << 8) + 0xFF; 1171da177e4SLinus Torvalds locomo_writel(nbset, membase + LOCOMO_KSC); 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds /* 1211da177e4SLinus Torvalds * The LoCoMo keyboard only generates interrupts when a key is pressed. 1221da177e4SLinus Torvalds * So when a key is pressed, we enable a timer. This timer scans the 1231da177e4SLinus Torvalds * keyboard, and this is how we detect when the key is released. 1241da177e4SLinus Torvalds */ 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* Scan the hardware keyboard and push any changes up through the input layer */ 1277d12e780SDavid Howells static void locomokbd_scankeyboard(struct locomokbd *locomokbd) 1281da177e4SLinus Torvalds { 129*e6cdd156SHelge Deller unsigned int row, col, rowd; 1301da177e4SLinus Torvalds unsigned long flags; 1311da177e4SLinus Torvalds unsigned int num_pressed; 1321da177e4SLinus Torvalds unsigned long membase = locomokbd->base; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds spin_lock_irqsave(&locomokbd->lock, flags); 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds locomokbd_charge_all(membase); 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds num_pressed = 0; 1391da177e4SLinus Torvalds for (col = 0; col < KB_COLS; col++) { 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds locomokbd_activate_col(membase, col); 1421da177e4SLinus Torvalds udelay(KB_DELAY); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds rowd = ~locomo_readl(membase + LOCOMO_KIB); 1451da177e4SLinus Torvalds for (row = 0; row < KB_ROWS; row++) { 146*e6cdd156SHelge Deller unsigned int scancode, pressed, key; 147*e6cdd156SHelge Deller 1481da177e4SLinus Torvalds scancode = SCANCODE(col, row); 149*e6cdd156SHelge Deller pressed = rowd & KB_ROWMASK(row); 150*e6cdd156SHelge Deller key = locomokbd->keycode[scancode]; 151*e6cdd156SHelge Deller 152*e6cdd156SHelge Deller input_report_key(locomokbd->input, key, pressed); 153*e6cdd156SHelge Deller if (likely(!pressed)) 154*e6cdd156SHelge Deller continue; 155*e6cdd156SHelge Deller 156*e6cdd156SHelge Deller num_pressed++; 157*e6cdd156SHelge Deller 158*e6cdd156SHelge Deller /* The "Cancel/ESC" key is labeled "On/Off" on 159*e6cdd156SHelge Deller * Collie and Poodle and should suspend the device 160*e6cdd156SHelge Deller * if it was pressed for more than a second. */ 161*e6cdd156SHelge Deller if (unlikely(key == KEY_ESC)) { 162*e6cdd156SHelge Deller if (!time_after(jiffies, 163*e6cdd156SHelge Deller locomokbd->suspend_jiffies + HZ)) 164*e6cdd156SHelge Deller continue; 165*e6cdd156SHelge Deller if (locomokbd->count_cancel++ 166*e6cdd156SHelge Deller != (HZ/SCAN_INTERVAL + 1)) 167*e6cdd156SHelge Deller continue; 168*e6cdd156SHelge Deller input_event(locomokbd->input, EV_PWR, 169*e6cdd156SHelge Deller KEY_SUSPEND, 1); 170*e6cdd156SHelge Deller locomokbd->suspend_jiffies = jiffies; 171*e6cdd156SHelge Deller } else 172*e6cdd156SHelge Deller locomokbd->count_cancel = 0; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds locomokbd_reset_col(membase, col); 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds locomokbd_activate_all(membase); 1771da177e4SLinus Torvalds 178438c9da5SDmitry Torokhov input_sync(locomokbd->input); 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds /* if any keys are pressed, enable the timer */ 1811da177e4SLinus Torvalds if (num_pressed) 1821da177e4SLinus Torvalds mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL); 183*e6cdd156SHelge Deller else 184*e6cdd156SHelge Deller locomokbd->count_cancel = 0; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds spin_unlock_irqrestore(&locomokbd->lock, flags); 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* 1901da177e4SLinus Torvalds * LoCoMo keyboard interrupt handler. 1911da177e4SLinus Torvalds */ 1927d12e780SDavid Howells static irqreturn_t locomokbd_interrupt(int irq, void *dev_id) 1931da177e4SLinus Torvalds { 1941da177e4SLinus Torvalds struct locomokbd *locomokbd = dev_id; 1951da177e4SLinus Torvalds /** wait chattering delay **/ 1961da177e4SLinus Torvalds udelay(100); 1971da177e4SLinus Torvalds 1987d12e780SDavid Howells locomokbd_scankeyboard(locomokbd); 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds return IRQ_HANDLED; 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* 2041da177e4SLinus Torvalds * LoCoMo timer checking for released keys 2051da177e4SLinus Torvalds */ 2061da177e4SLinus Torvalds static void locomokbd_timer_callback(unsigned long data) 2071da177e4SLinus Torvalds { 2081da177e4SLinus Torvalds struct locomokbd *locomokbd = (struct locomokbd *) data; 209*e6cdd156SHelge Deller 21036bd262bSRussell King locomokbd_scankeyboard(locomokbd); 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 213*e6cdd156SHelge Deller static int __devinit locomokbd_probe(struct locomo_dev *dev) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds struct locomokbd *locomokbd; 216438c9da5SDmitry Torokhov struct input_dev *input_dev; 2172b03b60eSDmitry Torokhov int i, err; 2181da177e4SLinus Torvalds 219438c9da5SDmitry Torokhov locomokbd = kzalloc(sizeof(struct locomokbd), GFP_KERNEL); 220438c9da5SDmitry Torokhov input_dev = input_allocate_device(); 221438c9da5SDmitry Torokhov if (!locomokbd || !input_dev) { 2222b03b60eSDmitry Torokhov err = -ENOMEM; 2232b03b60eSDmitry Torokhov goto err_free_mem; 224438c9da5SDmitry Torokhov } 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds /* try and claim memory region */ 2271da177e4SLinus Torvalds if (!request_mem_region((unsigned long) dev->mapbase, 2281da177e4SLinus Torvalds dev->length, 2291da177e4SLinus Torvalds LOCOMO_DRIVER_NAME(dev))) { 2302b03b60eSDmitry Torokhov err = -EBUSY; 2311da177e4SLinus Torvalds printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n"); 2322b03b60eSDmitry Torokhov goto err_free_mem; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds locomo_set_drvdata(dev, locomokbd); 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds locomokbd->base = (unsigned long) dev->mapbase; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds spin_lock_init(&locomokbd->lock); 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds init_timer(&locomokbd->timer); 2421da177e4SLinus Torvalds locomokbd->timer.function = locomokbd_timer_callback; 2431da177e4SLinus Torvalds locomokbd->timer.data = (unsigned long) locomokbd; 2441da177e4SLinus Torvalds 245*e6cdd156SHelge Deller locomokbd->suspend_jiffies = jiffies; 246*e6cdd156SHelge Deller 247438c9da5SDmitry Torokhov locomokbd->input = input_dev; 248438c9da5SDmitry Torokhov strcpy(locomokbd->phys, "locomokbd/input0"); 2491da177e4SLinus Torvalds 250438c9da5SDmitry Torokhov input_dev->name = "LoCoMo keyboard"; 251438c9da5SDmitry Torokhov input_dev->phys = locomokbd->phys; 252d7a767ddSPavel Machek input_dev->id.bustype = BUS_HOST; 253438c9da5SDmitry Torokhov input_dev->id.vendor = 0x0001; 254438c9da5SDmitry Torokhov input_dev->id.product = 0x0001; 255438c9da5SDmitry Torokhov input_dev->id.version = 0x0100; 256469ba4dfSDmitry Torokhov input_dev->dev.parent = &dev->dev; 257438c9da5SDmitry Torokhov 258*e6cdd156SHelge Deller input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | 259*e6cdd156SHelge Deller BIT_MASK(EV_PWR); 260438c9da5SDmitry Torokhov input_dev->keycode = locomokbd->keycode; 261*e6cdd156SHelge Deller input_dev->keycodesize = sizeof(locomokbd_keycode[0]); 262438c9da5SDmitry Torokhov input_dev->keycodemax = ARRAY_SIZE(locomokbd_keycode); 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode)); 2651da177e4SLinus Torvalds for (i = 0; i < LOCOMOKBD_NUMKEYS; i++) 266438c9da5SDmitry Torokhov set_bit(locomokbd->keycode[i], input_dev->keybit); 267438c9da5SDmitry Torokhov clear_bit(0, input_dev->keybit); 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds /* attempt to get the interrupt */ 2702b03b60eSDmitry Torokhov err = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd); 2712b03b60eSDmitry Torokhov if (err) { 2721da177e4SLinus Torvalds printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n"); 2732b03b60eSDmitry Torokhov goto err_release_region; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 2762b03b60eSDmitry Torokhov err = input_register_device(locomokbd->input); 2772b03b60eSDmitry Torokhov if (err) 2782b03b60eSDmitry Torokhov goto err_free_irq; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds return 0; 2811da177e4SLinus Torvalds 2822b03b60eSDmitry Torokhov err_free_irq: 2832b03b60eSDmitry Torokhov free_irq(dev->irq[0], locomokbd); 2842b03b60eSDmitry Torokhov err_release_region: 2851da177e4SLinus Torvalds release_mem_region((unsigned long) dev->mapbase, dev->length); 2861da177e4SLinus Torvalds locomo_set_drvdata(dev, NULL); 2872b03b60eSDmitry Torokhov err_free_mem: 288438c9da5SDmitry Torokhov input_free_device(input_dev); 2891da177e4SLinus Torvalds kfree(locomokbd); 2901da177e4SLinus Torvalds 2912b03b60eSDmitry Torokhov return err; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 294*e6cdd156SHelge Deller static int __devexit locomokbd_remove(struct locomo_dev *dev) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds struct locomokbd *locomokbd = locomo_get_drvdata(dev); 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds free_irq(dev->irq[0], locomokbd); 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds del_timer_sync(&locomokbd->timer); 3011da177e4SLinus Torvalds 302438c9da5SDmitry Torokhov input_unregister_device(locomokbd->input); 3031da177e4SLinus Torvalds locomo_set_drvdata(dev, NULL); 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds release_mem_region((unsigned long) dev->mapbase, dev->length); 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds kfree(locomokbd); 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds return 0; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds static struct locomo_driver keyboard_driver = { 3131da177e4SLinus Torvalds .drv = { 3141da177e4SLinus Torvalds .name = "locomokbd" 3151da177e4SLinus Torvalds }, 3161da177e4SLinus Torvalds .devid = LOCOMO_DEVID_KEYBOARD, 3171da177e4SLinus Torvalds .probe = locomokbd_probe, 318*e6cdd156SHelge Deller .remove = __devexit_p(locomokbd_remove), 3191da177e4SLinus Torvalds }; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds static int __init locomokbd_init(void) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds return locomo_driver_register(&keyboard_driver); 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds static void __exit locomokbd_exit(void) 3271da177e4SLinus Torvalds { 3281da177e4SLinus Torvalds locomo_driver_unregister(&keyboard_driver); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds module_init(locomokbd_init); 3321da177e4SLinus Torvalds module_exit(locomokbd_exit); 333