1*1da177e4SLinus Torvalds /* 2*1da177e4SLinus Torvalds * Copyright (c) 2005 John Lenz 3*1da177e4SLinus Torvalds * 4*1da177e4SLinus Torvalds * Based on from xtkbd.c 5*1da177e4SLinus Torvalds */ 6*1da177e4SLinus Torvalds 7*1da177e4SLinus Torvalds /* 8*1da177e4SLinus Torvalds * LoCoMo keyboard driver for Linux/ARM 9*1da177e4SLinus Torvalds */ 10*1da177e4SLinus Torvalds 11*1da177e4SLinus Torvalds /* 12*1da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 13*1da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 14*1da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 15*1da177e4SLinus Torvalds * (at your option) any later version. 16*1da177e4SLinus Torvalds * 17*1da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 18*1da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 19*1da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20*1da177e4SLinus Torvalds * GNU General Public License for more details. 21*1da177e4SLinus Torvalds * 22*1da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 23*1da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 24*1da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 25*1da177e4SLinus Torvalds * 26*1da177e4SLinus Torvalds */ 27*1da177e4SLinus Torvalds 28*1da177e4SLinus Torvalds #include <linux/config.h> 29*1da177e4SLinus Torvalds #include <linux/slab.h> 30*1da177e4SLinus Torvalds #include <linux/module.h> 31*1da177e4SLinus Torvalds #include <linux/init.h> 32*1da177e4SLinus Torvalds #include <linux/input.h> 33*1da177e4SLinus Torvalds #include <linux/delay.h> 34*1da177e4SLinus Torvalds #include <linux/device.h> 35*1da177e4SLinus Torvalds #include <linux/interrupt.h> 36*1da177e4SLinus Torvalds #include <linux/ioport.h> 37*1da177e4SLinus Torvalds 38*1da177e4SLinus Torvalds #include <asm/hardware/locomo.h> 39*1da177e4SLinus Torvalds #include <asm/irq.h> 40*1da177e4SLinus Torvalds 41*1da177e4SLinus Torvalds MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>"); 42*1da177e4SLinus Torvalds MODULE_DESCRIPTION("LoCoMo keyboard driver"); 43*1da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 44*1da177e4SLinus Torvalds 45*1da177e4SLinus Torvalds #define LOCOMOKBD_NUMKEYS 128 46*1da177e4SLinus Torvalds 47*1da177e4SLinus Torvalds #define KEY_ACTIVITY KEY_F16 48*1da177e4SLinus Torvalds #define KEY_CONTACT KEY_F18 49*1da177e4SLinus Torvalds #define KEY_CENTER KEY_F15 50*1da177e4SLinus Torvalds 51*1da177e4SLinus Torvalds static unsigned char locomokbd_keycode[LOCOMOKBD_NUMKEYS] = { 52*1da177e4SLinus Torvalds 0, KEY_ESC, KEY_ACTIVITY, 0, 0, 0, 0, 0, 0, 0, /* 0 - 9 */ 53*1da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_HOME, KEY_CONTACT, /* 10 - 19 */ 54*1da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 29 */ 55*1da177e4SLinus Torvalds 0, 0, 0, KEY_CENTER, 0, KEY_MAIL, 0, 0, 0, 0, /* 30 - 39 */ 56*1da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RIGHT, /* 40 - 49 */ 57*1da177e4SLinus Torvalds KEY_UP, KEY_LEFT, 0, 0, KEY_P, 0, KEY_O, KEY_I, KEY_Y, KEY_T, /* 50 - 59 */ 58*1da177e4SLinus Torvalds KEY_E, KEY_W, 0, 0, 0, 0, KEY_DOWN, KEY_ENTER, 0, 0, /* 60 - 69 */ 59*1da177e4SLinus Torvalds KEY_BACKSPACE, 0, KEY_L, KEY_U, KEY_H, KEY_R, KEY_D, KEY_Q, 0, 0, /* 70 - 79 */ 60*1da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, KEY_ENTER, KEY_RIGHTSHIFT, KEY_K, KEY_J, /* 80 - 89 */ 61*1da177e4SLinus Torvalds KEY_G, KEY_F, KEY_X, KEY_S, 0, 0, 0, 0, 0, 0, /* 90 - 99 */ 62*1da177e4SLinus Torvalds 0, 0, KEY_DOT, 0, KEY_COMMA, KEY_N, KEY_B, KEY_C, KEY_Z, KEY_A, /* 100 - 109 */ 63*1da177e4SLinus Torvalds KEY_LEFTSHIFT, KEY_TAB, KEY_LEFTCTRL, 0, 0, 0, 0, 0, 0, 0, /* 110 - 119 */ 64*1da177e4SLinus Torvalds KEY_M, KEY_SPACE, KEY_V, KEY_APOSTROPHE, KEY_SLASH, 0, 0, 0 /* 120 - 128 */ 65*1da177e4SLinus Torvalds }; 66*1da177e4SLinus Torvalds 67*1da177e4SLinus Torvalds #define KB_ROWS 16 68*1da177e4SLinus Torvalds #define KB_COLS 8 69*1da177e4SLinus Torvalds #define KB_ROWMASK(r) (1 << (r)) 70*1da177e4SLinus Torvalds #define SCANCODE(c,r) ( ((c)<<4) + (r) + 1 ) 71*1da177e4SLinus Torvalds #define NR_SCANCODES 128 72*1da177e4SLinus Torvalds 73*1da177e4SLinus Torvalds #define KB_DELAY 8 74*1da177e4SLinus Torvalds #define SCAN_INTERVAL (HZ/10) 75*1da177e4SLinus Torvalds #define LOCOMOKBD_PRESSED 1 76*1da177e4SLinus Torvalds 77*1da177e4SLinus Torvalds struct locomokbd { 78*1da177e4SLinus Torvalds unsigned char keycode[LOCOMOKBD_NUMKEYS]; 79*1da177e4SLinus Torvalds struct input_dev input; 80*1da177e4SLinus Torvalds char phys[32]; 81*1da177e4SLinus Torvalds 82*1da177e4SLinus Torvalds struct locomo_dev *ldev; 83*1da177e4SLinus Torvalds unsigned long base; 84*1da177e4SLinus Torvalds spinlock_t lock; 85*1da177e4SLinus Torvalds 86*1da177e4SLinus Torvalds struct timer_list timer; 87*1da177e4SLinus Torvalds }; 88*1da177e4SLinus Torvalds 89*1da177e4SLinus Torvalds /* helper functions for reading the keyboard matrix */ 90*1da177e4SLinus Torvalds static inline void locomokbd_charge_all(unsigned long membase) 91*1da177e4SLinus Torvalds { 92*1da177e4SLinus Torvalds locomo_writel(0x00FF, membase + LOCOMO_KSC); 93*1da177e4SLinus Torvalds } 94*1da177e4SLinus Torvalds 95*1da177e4SLinus Torvalds static inline void locomokbd_activate_all(unsigned long membase) 96*1da177e4SLinus Torvalds { 97*1da177e4SLinus Torvalds unsigned long r; 98*1da177e4SLinus Torvalds 99*1da177e4SLinus Torvalds locomo_writel(0, membase + LOCOMO_KSC); 100*1da177e4SLinus Torvalds r = locomo_readl(membase + LOCOMO_KIC); 101*1da177e4SLinus Torvalds r &= 0xFEFF; 102*1da177e4SLinus Torvalds locomo_writel(r, membase + LOCOMO_KIC); 103*1da177e4SLinus Torvalds } 104*1da177e4SLinus Torvalds 105*1da177e4SLinus Torvalds static inline void locomokbd_activate_col(unsigned long membase, int col) 106*1da177e4SLinus Torvalds { 107*1da177e4SLinus Torvalds unsigned short nset; 108*1da177e4SLinus Torvalds unsigned short nbset; 109*1da177e4SLinus Torvalds 110*1da177e4SLinus Torvalds nset = 0xFF & ~(1 << col); 111*1da177e4SLinus Torvalds nbset = (nset << 8) + nset; 112*1da177e4SLinus Torvalds locomo_writel(nbset, membase + LOCOMO_KSC); 113*1da177e4SLinus Torvalds } 114*1da177e4SLinus Torvalds 115*1da177e4SLinus Torvalds static inline void locomokbd_reset_col(unsigned long membase, int col) 116*1da177e4SLinus Torvalds { 117*1da177e4SLinus Torvalds unsigned short nbset; 118*1da177e4SLinus Torvalds 119*1da177e4SLinus Torvalds nbset = ((0xFF & ~(1 << col)) << 8) + 0xFF; 120*1da177e4SLinus Torvalds locomo_writel(nbset, membase + LOCOMO_KSC); 121*1da177e4SLinus Torvalds } 122*1da177e4SLinus Torvalds 123*1da177e4SLinus Torvalds /* 124*1da177e4SLinus Torvalds * The LoCoMo keyboard only generates interrupts when a key is pressed. 125*1da177e4SLinus Torvalds * So when a key is pressed, we enable a timer. This timer scans the 126*1da177e4SLinus Torvalds * keyboard, and this is how we detect when the key is released. 127*1da177e4SLinus Torvalds */ 128*1da177e4SLinus Torvalds 129*1da177e4SLinus Torvalds /* Scan the hardware keyboard and push any changes up through the input layer */ 130*1da177e4SLinus Torvalds static void locomokbd_scankeyboard(struct locomokbd *locomokbd, struct pt_regs *regs) 131*1da177e4SLinus Torvalds { 132*1da177e4SLinus Torvalds unsigned int row, col, rowd, scancode; 133*1da177e4SLinus Torvalds unsigned long flags; 134*1da177e4SLinus Torvalds unsigned int num_pressed; 135*1da177e4SLinus Torvalds unsigned long membase = locomokbd->base; 136*1da177e4SLinus Torvalds 137*1da177e4SLinus Torvalds spin_lock_irqsave(&locomokbd->lock, flags); 138*1da177e4SLinus Torvalds 139*1da177e4SLinus Torvalds if (regs) 140*1da177e4SLinus Torvalds input_regs(&locomokbd->input, regs); 141*1da177e4SLinus Torvalds 142*1da177e4SLinus Torvalds locomokbd_charge_all(membase); 143*1da177e4SLinus Torvalds 144*1da177e4SLinus Torvalds num_pressed = 0; 145*1da177e4SLinus Torvalds for (col = 0; col < KB_COLS; col++) { 146*1da177e4SLinus Torvalds 147*1da177e4SLinus Torvalds locomokbd_activate_col(membase, col); 148*1da177e4SLinus Torvalds udelay(KB_DELAY); 149*1da177e4SLinus Torvalds 150*1da177e4SLinus Torvalds rowd = ~locomo_readl(membase + LOCOMO_KIB); 151*1da177e4SLinus Torvalds for (row = 0; row < KB_ROWS; row++ ) { 152*1da177e4SLinus Torvalds scancode = SCANCODE(col, row); 153*1da177e4SLinus Torvalds if (rowd & KB_ROWMASK(row)) { 154*1da177e4SLinus Torvalds num_pressed += 1; 155*1da177e4SLinus Torvalds input_report_key(&locomokbd->input, locomokbd->keycode[scancode], 1); 156*1da177e4SLinus Torvalds } else { 157*1da177e4SLinus Torvalds input_report_key(&locomokbd->input, locomokbd->keycode[scancode], 0); 158*1da177e4SLinus Torvalds } 159*1da177e4SLinus Torvalds } 160*1da177e4SLinus Torvalds locomokbd_reset_col(membase, col); 161*1da177e4SLinus Torvalds } 162*1da177e4SLinus Torvalds locomokbd_activate_all(membase); 163*1da177e4SLinus Torvalds 164*1da177e4SLinus Torvalds input_sync(&locomokbd->input); 165*1da177e4SLinus Torvalds 166*1da177e4SLinus Torvalds /* if any keys are pressed, enable the timer */ 167*1da177e4SLinus Torvalds if (num_pressed) 168*1da177e4SLinus Torvalds mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL); 169*1da177e4SLinus Torvalds 170*1da177e4SLinus Torvalds spin_unlock_irqrestore(&locomokbd->lock, flags); 171*1da177e4SLinus Torvalds } 172*1da177e4SLinus Torvalds 173*1da177e4SLinus Torvalds /* 174*1da177e4SLinus Torvalds * LoCoMo keyboard interrupt handler. 175*1da177e4SLinus Torvalds */ 176*1da177e4SLinus Torvalds static irqreturn_t locomokbd_interrupt(int irq, void *dev_id, struct pt_regs *regs) 177*1da177e4SLinus Torvalds { 178*1da177e4SLinus Torvalds struct locomokbd *locomokbd = dev_id; 179*1da177e4SLinus Torvalds /** wait chattering delay **/ 180*1da177e4SLinus Torvalds udelay(100); 181*1da177e4SLinus Torvalds 182*1da177e4SLinus Torvalds locomokbd_scankeyboard(locomokbd, regs); 183*1da177e4SLinus Torvalds 184*1da177e4SLinus Torvalds return IRQ_HANDLED; 185*1da177e4SLinus Torvalds } 186*1da177e4SLinus Torvalds 187*1da177e4SLinus Torvalds /* 188*1da177e4SLinus Torvalds * LoCoMo timer checking for released keys 189*1da177e4SLinus Torvalds */ 190*1da177e4SLinus Torvalds static void locomokbd_timer_callback(unsigned long data) 191*1da177e4SLinus Torvalds { 192*1da177e4SLinus Torvalds struct locomokbd *locomokbd = (struct locomokbd *) data; 193*1da177e4SLinus Torvalds locomokbd_scankeyboard(locomokbd, NULL); 194*1da177e4SLinus Torvalds } 195*1da177e4SLinus Torvalds 196*1da177e4SLinus Torvalds static int locomokbd_probe(struct locomo_dev *dev) 197*1da177e4SLinus Torvalds { 198*1da177e4SLinus Torvalds struct locomokbd *locomokbd; 199*1da177e4SLinus Torvalds int i, ret; 200*1da177e4SLinus Torvalds 201*1da177e4SLinus Torvalds locomokbd = kmalloc(sizeof(struct locomokbd), GFP_KERNEL); 202*1da177e4SLinus Torvalds if (!locomokbd) 203*1da177e4SLinus Torvalds return -ENOMEM; 204*1da177e4SLinus Torvalds 205*1da177e4SLinus Torvalds memset(locomokbd, 0, sizeof(struct locomokbd)); 206*1da177e4SLinus Torvalds 207*1da177e4SLinus Torvalds /* try and claim memory region */ 208*1da177e4SLinus Torvalds if (!request_mem_region((unsigned long) dev->mapbase, 209*1da177e4SLinus Torvalds dev->length, 210*1da177e4SLinus Torvalds LOCOMO_DRIVER_NAME(dev))) { 211*1da177e4SLinus Torvalds ret = -EBUSY; 212*1da177e4SLinus Torvalds printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n"); 213*1da177e4SLinus Torvalds goto free; 214*1da177e4SLinus Torvalds } 215*1da177e4SLinus Torvalds 216*1da177e4SLinus Torvalds locomokbd->ldev = dev; 217*1da177e4SLinus Torvalds locomo_set_drvdata(dev, locomokbd); 218*1da177e4SLinus Torvalds 219*1da177e4SLinus Torvalds locomokbd->base = (unsigned long) dev->mapbase; 220*1da177e4SLinus Torvalds 221*1da177e4SLinus Torvalds spin_lock_init(&locomokbd->lock); 222*1da177e4SLinus Torvalds 223*1da177e4SLinus Torvalds init_timer(&locomokbd->timer); 224*1da177e4SLinus Torvalds locomokbd->timer.function = locomokbd_timer_callback; 225*1da177e4SLinus Torvalds locomokbd->timer.data = (unsigned long) locomokbd; 226*1da177e4SLinus Torvalds 227*1da177e4SLinus Torvalds locomokbd->input.evbit[0] = BIT(EV_KEY) | BIT(EV_REP); 228*1da177e4SLinus Torvalds 229*1da177e4SLinus Torvalds init_input_dev(&locomokbd->input); 230*1da177e4SLinus Torvalds locomokbd->input.keycode = locomokbd->keycode; 231*1da177e4SLinus Torvalds locomokbd->input.keycodesize = sizeof(unsigned char); 232*1da177e4SLinus Torvalds locomokbd->input.keycodemax = ARRAY_SIZE(locomokbd_keycode); 233*1da177e4SLinus Torvalds locomokbd->input.private = locomokbd; 234*1da177e4SLinus Torvalds 235*1da177e4SLinus Torvalds memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode)); 236*1da177e4SLinus Torvalds for (i = 0; i < LOCOMOKBD_NUMKEYS; i++) 237*1da177e4SLinus Torvalds set_bit(locomokbd->keycode[i], locomokbd->input.keybit); 238*1da177e4SLinus Torvalds clear_bit(0, locomokbd->input.keybit); 239*1da177e4SLinus Torvalds 240*1da177e4SLinus Torvalds strcpy(locomokbd->phys, "locomokbd/input0"); 241*1da177e4SLinus Torvalds 242*1da177e4SLinus Torvalds locomokbd->input.name = "LoCoMo keyboard"; 243*1da177e4SLinus Torvalds locomokbd->input.phys = locomokbd->phys; 244*1da177e4SLinus Torvalds locomokbd->input.id.bustype = BUS_XTKBD; 245*1da177e4SLinus Torvalds locomokbd->input.id.vendor = 0x0001; 246*1da177e4SLinus Torvalds locomokbd->input.id.product = 0x0001; 247*1da177e4SLinus Torvalds locomokbd->input.id.version = 0x0100; 248*1da177e4SLinus Torvalds 249*1da177e4SLinus Torvalds /* attempt to get the interrupt */ 250*1da177e4SLinus Torvalds ret = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd); 251*1da177e4SLinus Torvalds if (ret) { 252*1da177e4SLinus Torvalds printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n"); 253*1da177e4SLinus Torvalds goto out; 254*1da177e4SLinus Torvalds } 255*1da177e4SLinus Torvalds 256*1da177e4SLinus Torvalds input_register_device(&locomokbd->input); 257*1da177e4SLinus Torvalds 258*1da177e4SLinus Torvalds printk(KERN_INFO "input: LoCoMo keyboard on locomokbd\n"); 259*1da177e4SLinus Torvalds 260*1da177e4SLinus Torvalds return 0; 261*1da177e4SLinus Torvalds 262*1da177e4SLinus Torvalds out: 263*1da177e4SLinus Torvalds release_mem_region((unsigned long) dev->mapbase, dev->length); 264*1da177e4SLinus Torvalds locomo_set_drvdata(dev, NULL); 265*1da177e4SLinus Torvalds free: 266*1da177e4SLinus Torvalds kfree(locomokbd); 267*1da177e4SLinus Torvalds 268*1da177e4SLinus Torvalds return ret; 269*1da177e4SLinus Torvalds } 270*1da177e4SLinus Torvalds 271*1da177e4SLinus Torvalds static int locomokbd_remove(struct locomo_dev *dev) 272*1da177e4SLinus Torvalds { 273*1da177e4SLinus Torvalds struct locomokbd *locomokbd = locomo_get_drvdata(dev); 274*1da177e4SLinus Torvalds 275*1da177e4SLinus Torvalds free_irq(dev->irq[0], locomokbd); 276*1da177e4SLinus Torvalds 277*1da177e4SLinus Torvalds del_timer_sync(&locomokbd->timer); 278*1da177e4SLinus Torvalds 279*1da177e4SLinus Torvalds input_unregister_device(&locomokbd->input); 280*1da177e4SLinus Torvalds locomo_set_drvdata(dev, NULL); 281*1da177e4SLinus Torvalds 282*1da177e4SLinus Torvalds release_mem_region((unsigned long) dev->mapbase, dev->length); 283*1da177e4SLinus Torvalds 284*1da177e4SLinus Torvalds kfree(locomokbd); 285*1da177e4SLinus Torvalds 286*1da177e4SLinus Torvalds return 0; 287*1da177e4SLinus Torvalds } 288*1da177e4SLinus Torvalds 289*1da177e4SLinus Torvalds static struct locomo_driver keyboard_driver = { 290*1da177e4SLinus Torvalds .drv = { 291*1da177e4SLinus Torvalds .name = "locomokbd" 292*1da177e4SLinus Torvalds }, 293*1da177e4SLinus Torvalds .devid = LOCOMO_DEVID_KEYBOARD, 294*1da177e4SLinus Torvalds .probe = locomokbd_probe, 295*1da177e4SLinus Torvalds .remove = locomokbd_remove, 296*1da177e4SLinus Torvalds }; 297*1da177e4SLinus Torvalds 298*1da177e4SLinus Torvalds static int __init locomokbd_init(void) 299*1da177e4SLinus Torvalds { 300*1da177e4SLinus Torvalds return locomo_driver_register(&keyboard_driver); 301*1da177e4SLinus Torvalds } 302*1da177e4SLinus Torvalds 303*1da177e4SLinus Torvalds static void __exit locomokbd_exit(void) 304*1da177e4SLinus Torvalds { 305*1da177e4SLinus Torvalds locomo_driver_unregister(&keyboard_driver); 306*1da177e4SLinus Torvalds } 307*1da177e4SLinus Torvalds 308*1da177e4SLinus Torvalds module_init(locomokbd_init); 309*1da177e4SLinus Torvalds module_exit(locomokbd_exit); 310