1*1a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 3e6cdd156SHelge Deller * LoCoMo keyboard driver for Linux-based ARM PDAs: 4e6cdd156SHelge Deller * - SHARP Zaurus Collie (SL-5500) 5e6cdd156SHelge Deller * - SHARP Zaurus Poodle (SL-5600) 61da177e4SLinus Torvalds * 7e6cdd156SHelge Deller * Copyright (c) 2005 John Lenz 81da177e4SLinus Torvalds * Based on from xtkbd.c 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/slab.h> 121da177e4SLinus Torvalds #include <linux/module.h> 131da177e4SLinus Torvalds #include <linux/init.h> 141da177e4SLinus Torvalds #include <linux/input.h> 151da177e4SLinus Torvalds #include <linux/delay.h> 161da177e4SLinus Torvalds #include <linux/device.h> 171da177e4SLinus Torvalds #include <linux/interrupt.h> 181da177e4SLinus Torvalds #include <linux/ioport.h> 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds #include <asm/hardware/locomo.h> 211da177e4SLinus Torvalds #include <asm/irq.h> 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>"); 241da177e4SLinus Torvalds MODULE_DESCRIPTION("LoCoMo keyboard driver"); 251da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #define LOCOMOKBD_NUMKEYS 128 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds #define KEY_ACTIVITY KEY_F16 301da177e4SLinus Torvalds #define KEY_CONTACT KEY_F18 311da177e4SLinus Torvalds #define KEY_CENTER KEY_F15 321da177e4SLinus Torvalds 33e6cdd156SHelge Deller static const unsigned char 3478f50c24SBill Pemberton locomokbd_keycode[LOCOMOKBD_NUMKEYS] = { 351da177e4SLinus Torvalds 0, KEY_ESC, KEY_ACTIVITY, 0, 0, 0, 0, 0, 0, 0, /* 0 - 9 */ 361da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_HOME, KEY_CONTACT, /* 10 - 19 */ 371da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 29 */ 381da177e4SLinus Torvalds 0, 0, 0, KEY_CENTER, 0, KEY_MAIL, 0, 0, 0, 0, /* 30 - 39 */ 391da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RIGHT, /* 40 - 49 */ 401da177e4SLinus Torvalds KEY_UP, KEY_LEFT, 0, 0, KEY_P, 0, KEY_O, KEY_I, KEY_Y, KEY_T, /* 50 - 59 */ 411da177e4SLinus Torvalds KEY_E, KEY_W, 0, 0, 0, 0, KEY_DOWN, KEY_ENTER, 0, 0, /* 60 - 69 */ 421da177e4SLinus Torvalds KEY_BACKSPACE, 0, KEY_L, KEY_U, KEY_H, KEY_R, KEY_D, KEY_Q, 0, 0, /* 70 - 79 */ 431da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, KEY_ENTER, KEY_RIGHTSHIFT, KEY_K, KEY_J, /* 80 - 89 */ 441da177e4SLinus Torvalds KEY_G, KEY_F, KEY_X, KEY_S, 0, 0, 0, 0, 0, 0, /* 90 - 99 */ 451da177e4SLinus Torvalds 0, 0, KEY_DOT, 0, KEY_COMMA, KEY_N, KEY_B, KEY_C, KEY_Z, KEY_A, /* 100 - 109 */ 461da177e4SLinus Torvalds KEY_LEFTSHIFT, KEY_TAB, KEY_LEFTCTRL, 0, 0, 0, 0, 0, 0, 0, /* 110 - 119 */ 471da177e4SLinus Torvalds KEY_M, KEY_SPACE, KEY_V, KEY_APOSTROPHE, KEY_SLASH, 0, 0, 0 /* 120 - 128 */ 481da177e4SLinus Torvalds }; 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #define KB_ROWS 16 511da177e4SLinus Torvalds #define KB_COLS 8 521da177e4SLinus Torvalds #define KB_ROWMASK(r) (1 << (r)) 531da177e4SLinus Torvalds #define SCANCODE(c,r) ( ((c)<<4) + (r) + 1 ) 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #define KB_DELAY 8 561da177e4SLinus Torvalds #define SCAN_INTERVAL (HZ/10) 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds struct locomokbd { 591da177e4SLinus Torvalds unsigned char keycode[LOCOMOKBD_NUMKEYS]; 60438c9da5SDmitry Torokhov struct input_dev *input; 611da177e4SLinus Torvalds char phys[32]; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds unsigned long base; 641da177e4SLinus Torvalds spinlock_t lock; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds struct timer_list timer; 67e6cdd156SHelge Deller unsigned long suspend_jiffies; 68e6cdd156SHelge Deller unsigned int count_cancel; 691da177e4SLinus Torvalds }; 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* helper functions for reading the keyboard matrix */ 721da177e4SLinus Torvalds static inline void locomokbd_charge_all(unsigned long membase) 731da177e4SLinus Torvalds { 741da177e4SLinus Torvalds locomo_writel(0x00FF, membase + LOCOMO_KSC); 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds static inline void locomokbd_activate_all(unsigned long membase) 781da177e4SLinus Torvalds { 791da177e4SLinus Torvalds unsigned long r; 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds locomo_writel(0, membase + LOCOMO_KSC); 821da177e4SLinus Torvalds r = locomo_readl(membase + LOCOMO_KIC); 831da177e4SLinus Torvalds r &= 0xFEFF; 841da177e4SLinus Torvalds locomo_writel(r, membase + LOCOMO_KIC); 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds static inline void locomokbd_activate_col(unsigned long membase, int col) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds unsigned short nset; 901da177e4SLinus Torvalds unsigned short nbset; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds nset = 0xFF & ~(1 << col); 931da177e4SLinus Torvalds nbset = (nset << 8) + nset; 941da177e4SLinus Torvalds locomo_writel(nbset, membase + LOCOMO_KSC); 951da177e4SLinus Torvalds } 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds static inline void locomokbd_reset_col(unsigned long membase, int col) 981da177e4SLinus Torvalds { 991da177e4SLinus Torvalds unsigned short nbset; 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds nbset = ((0xFF & ~(1 << col)) << 8) + 0xFF; 1021da177e4SLinus Torvalds locomo_writel(nbset, membase + LOCOMO_KSC); 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds /* 1061da177e4SLinus Torvalds * The LoCoMo keyboard only generates interrupts when a key is pressed. 1071da177e4SLinus Torvalds * So when a key is pressed, we enable a timer. This timer scans the 1081da177e4SLinus Torvalds * keyboard, and this is how we detect when the key is released. 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* Scan the hardware keyboard and push any changes up through the input layer */ 1127d12e780SDavid Howells static void locomokbd_scankeyboard(struct locomokbd *locomokbd) 1131da177e4SLinus Torvalds { 114e6cdd156SHelge Deller unsigned int row, col, rowd; 1151da177e4SLinus Torvalds unsigned long flags; 1161da177e4SLinus Torvalds unsigned int num_pressed; 1171da177e4SLinus Torvalds unsigned long membase = locomokbd->base; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds spin_lock_irqsave(&locomokbd->lock, flags); 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds locomokbd_charge_all(membase); 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds num_pressed = 0; 1241da177e4SLinus Torvalds for (col = 0; col < KB_COLS; col++) { 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds locomokbd_activate_col(membase, col); 1271da177e4SLinus Torvalds udelay(KB_DELAY); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds rowd = ~locomo_readl(membase + LOCOMO_KIB); 1301da177e4SLinus Torvalds for (row = 0; row < KB_ROWS; row++) { 131e6cdd156SHelge Deller unsigned int scancode, pressed, key; 132e6cdd156SHelge Deller 1331da177e4SLinus Torvalds scancode = SCANCODE(col, row); 134e6cdd156SHelge Deller pressed = rowd & KB_ROWMASK(row); 135e6cdd156SHelge Deller key = locomokbd->keycode[scancode]; 136e6cdd156SHelge Deller 137e6cdd156SHelge Deller input_report_key(locomokbd->input, key, pressed); 138e6cdd156SHelge Deller if (likely(!pressed)) 139e6cdd156SHelge Deller continue; 140e6cdd156SHelge Deller 141e6cdd156SHelge Deller num_pressed++; 142e6cdd156SHelge Deller 143e6cdd156SHelge Deller /* The "Cancel/ESC" key is labeled "On/Off" on 144e6cdd156SHelge Deller * Collie and Poodle and should suspend the device 145e6cdd156SHelge Deller * if it was pressed for more than a second. */ 146e6cdd156SHelge Deller if (unlikely(key == KEY_ESC)) { 147e6cdd156SHelge Deller if (!time_after(jiffies, 148e6cdd156SHelge Deller locomokbd->suspend_jiffies + HZ)) 149e6cdd156SHelge Deller continue; 150e6cdd156SHelge Deller if (locomokbd->count_cancel++ 151e6cdd156SHelge Deller != (HZ/SCAN_INTERVAL + 1)) 152e6cdd156SHelge Deller continue; 153e6cdd156SHelge Deller input_event(locomokbd->input, EV_PWR, 154e6cdd156SHelge Deller KEY_SUSPEND, 1); 155e6cdd156SHelge Deller locomokbd->suspend_jiffies = jiffies; 156e6cdd156SHelge Deller } else 157e6cdd156SHelge Deller locomokbd->count_cancel = 0; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds locomokbd_reset_col(membase, col); 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds locomokbd_activate_all(membase); 1621da177e4SLinus Torvalds 163438c9da5SDmitry Torokhov input_sync(locomokbd->input); 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds /* if any keys are pressed, enable the timer */ 1661da177e4SLinus Torvalds if (num_pressed) 1671da177e4SLinus Torvalds mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL); 168e6cdd156SHelge Deller else 169e6cdd156SHelge Deller locomokbd->count_cancel = 0; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds spin_unlock_irqrestore(&locomokbd->lock, flags); 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds /* 1751da177e4SLinus Torvalds * LoCoMo keyboard interrupt handler. 1761da177e4SLinus Torvalds */ 1777d12e780SDavid Howells static irqreturn_t locomokbd_interrupt(int irq, void *dev_id) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds struct locomokbd *locomokbd = dev_id; 180da8065acSEric Miao u16 r; 181da8065acSEric Miao 182da8065acSEric Miao r = locomo_readl(locomokbd->base + LOCOMO_KIC); 183da8065acSEric Miao if ((r & 0x0001) == 0) 184da8065acSEric Miao return IRQ_HANDLED; 185da8065acSEric Miao 186da8065acSEric Miao locomo_writel(r & ~0x0100, locomokbd->base + LOCOMO_KIC); /* Ack */ 187da8065acSEric Miao 1881da177e4SLinus Torvalds /** wait chattering delay **/ 1891da177e4SLinus Torvalds udelay(100); 1901da177e4SLinus Torvalds 1917d12e780SDavid Howells locomokbd_scankeyboard(locomokbd); 1921da177e4SLinus Torvalds return IRQ_HANDLED; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /* 1961da177e4SLinus Torvalds * LoCoMo timer checking for released keys 1971da177e4SLinus Torvalds */ 1984ea40278SKees Cook static void locomokbd_timer_callback(struct timer_list *t) 1991da177e4SLinus Torvalds { 2004ea40278SKees Cook struct locomokbd *locomokbd = from_timer(locomokbd, t, timer); 201e6cdd156SHelge Deller 20236bd262bSRussell King locomokbd_scankeyboard(locomokbd); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 205da8065acSEric Miao static int locomokbd_open(struct input_dev *dev) 206da8065acSEric Miao { 207da8065acSEric Miao struct locomokbd *locomokbd = input_get_drvdata(dev); 208da8065acSEric Miao u16 r; 209da8065acSEric Miao 210da8065acSEric Miao r = locomo_readl(locomokbd->base + LOCOMO_KIC) | 0x0010; 211da8065acSEric Miao locomo_writel(r, locomokbd->base + LOCOMO_KIC); 212da8065acSEric Miao return 0; 213da8065acSEric Miao } 214da8065acSEric Miao 215da8065acSEric Miao static void locomokbd_close(struct input_dev *dev) 216da8065acSEric Miao { 217da8065acSEric Miao struct locomokbd *locomokbd = input_get_drvdata(dev); 218da8065acSEric Miao u16 r; 219da8065acSEric Miao 220da8065acSEric Miao r = locomo_readl(locomokbd->base + LOCOMO_KIC) & ~0x0010; 221da8065acSEric Miao locomo_writel(r, locomokbd->base + LOCOMO_KIC); 222da8065acSEric Miao } 223da8065acSEric Miao 2245298cc4cSBill Pemberton static int locomokbd_probe(struct locomo_dev *dev) 2251da177e4SLinus Torvalds { 2261da177e4SLinus Torvalds struct locomokbd *locomokbd; 227438c9da5SDmitry Torokhov struct input_dev *input_dev; 2282b03b60eSDmitry Torokhov int i, err; 2291da177e4SLinus Torvalds 230438c9da5SDmitry Torokhov locomokbd = kzalloc(sizeof(struct locomokbd), GFP_KERNEL); 231438c9da5SDmitry Torokhov input_dev = input_allocate_device(); 232438c9da5SDmitry Torokhov if (!locomokbd || !input_dev) { 2332b03b60eSDmitry Torokhov err = -ENOMEM; 2342b03b60eSDmitry Torokhov goto err_free_mem; 235438c9da5SDmitry Torokhov } 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds /* try and claim memory region */ 2381da177e4SLinus Torvalds if (!request_mem_region((unsigned long) dev->mapbase, 2391da177e4SLinus Torvalds dev->length, 2401da177e4SLinus Torvalds LOCOMO_DRIVER_NAME(dev))) { 2412b03b60eSDmitry Torokhov err = -EBUSY; 2421da177e4SLinus Torvalds printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n"); 2432b03b60eSDmitry Torokhov goto err_free_mem; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds locomo_set_drvdata(dev, locomokbd); 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds locomokbd->base = (unsigned long) dev->mapbase; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds spin_lock_init(&locomokbd->lock); 2511da177e4SLinus Torvalds 2524ea40278SKees Cook timer_setup(&locomokbd->timer, locomokbd_timer_callback, 0); 2531da177e4SLinus Torvalds 254e6cdd156SHelge Deller locomokbd->suspend_jiffies = jiffies; 255e6cdd156SHelge Deller 256438c9da5SDmitry Torokhov locomokbd->input = input_dev; 257438c9da5SDmitry Torokhov strcpy(locomokbd->phys, "locomokbd/input0"); 2581da177e4SLinus Torvalds 259438c9da5SDmitry Torokhov input_dev->name = "LoCoMo keyboard"; 260438c9da5SDmitry Torokhov input_dev->phys = locomokbd->phys; 261d7a767ddSPavel Machek input_dev->id.bustype = BUS_HOST; 262438c9da5SDmitry Torokhov input_dev->id.vendor = 0x0001; 263438c9da5SDmitry Torokhov input_dev->id.product = 0x0001; 264438c9da5SDmitry Torokhov input_dev->id.version = 0x0100; 265da8065acSEric Miao input_dev->open = locomokbd_open; 266da8065acSEric Miao input_dev->close = locomokbd_close; 267469ba4dfSDmitry Torokhov input_dev->dev.parent = &dev->dev; 268438c9da5SDmitry Torokhov 269e6cdd156SHelge Deller input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | 270e6cdd156SHelge Deller BIT_MASK(EV_PWR); 271438c9da5SDmitry Torokhov input_dev->keycode = locomokbd->keycode; 272e6cdd156SHelge Deller input_dev->keycodesize = sizeof(locomokbd_keycode[0]); 273438c9da5SDmitry Torokhov input_dev->keycodemax = ARRAY_SIZE(locomokbd_keycode); 2741da177e4SLinus Torvalds 275da8065acSEric Miao input_set_drvdata(input_dev, locomokbd); 276da8065acSEric Miao 2771da177e4SLinus Torvalds memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode)); 2781da177e4SLinus Torvalds for (i = 0; i < LOCOMOKBD_NUMKEYS; i++) 279438c9da5SDmitry Torokhov set_bit(locomokbd->keycode[i], input_dev->keybit); 280438c9da5SDmitry Torokhov clear_bit(0, input_dev->keybit); 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /* attempt to get the interrupt */ 2832b03b60eSDmitry Torokhov err = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd); 2842b03b60eSDmitry Torokhov if (err) { 2851da177e4SLinus Torvalds printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n"); 2862b03b60eSDmitry Torokhov goto err_release_region; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2892b03b60eSDmitry Torokhov err = input_register_device(locomokbd->input); 2902b03b60eSDmitry Torokhov if (err) 2912b03b60eSDmitry Torokhov goto err_free_irq; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds return 0; 2941da177e4SLinus Torvalds 2952b03b60eSDmitry Torokhov err_free_irq: 2962b03b60eSDmitry Torokhov free_irq(dev->irq[0], locomokbd); 2972b03b60eSDmitry Torokhov err_release_region: 2981da177e4SLinus Torvalds release_mem_region((unsigned long) dev->mapbase, dev->length); 2991da177e4SLinus Torvalds locomo_set_drvdata(dev, NULL); 3002b03b60eSDmitry Torokhov err_free_mem: 301438c9da5SDmitry Torokhov input_free_device(input_dev); 3021da177e4SLinus Torvalds kfree(locomokbd); 3031da177e4SLinus Torvalds 3042b03b60eSDmitry Torokhov return err; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 307e2619cf7SBill Pemberton static int locomokbd_remove(struct locomo_dev *dev) 3081da177e4SLinus Torvalds { 3091da177e4SLinus Torvalds struct locomokbd *locomokbd = locomo_get_drvdata(dev); 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds free_irq(dev->irq[0], locomokbd); 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds del_timer_sync(&locomokbd->timer); 3141da177e4SLinus Torvalds 315438c9da5SDmitry Torokhov input_unregister_device(locomokbd->input); 3161da177e4SLinus Torvalds locomo_set_drvdata(dev, NULL); 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds release_mem_region((unsigned long) dev->mapbase, dev->length); 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds kfree(locomokbd); 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds return 0; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds static struct locomo_driver keyboard_driver = { 3261da177e4SLinus Torvalds .drv = { 3271da177e4SLinus Torvalds .name = "locomokbd" 3281da177e4SLinus Torvalds }, 3291da177e4SLinus Torvalds .devid = LOCOMO_DEVID_KEYBOARD, 3301da177e4SLinus Torvalds .probe = locomokbd_probe, 3311cb0aa88SBill Pemberton .remove = locomokbd_remove, 3321da177e4SLinus Torvalds }; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds static int __init locomokbd_init(void) 3351da177e4SLinus Torvalds { 3361da177e4SLinus Torvalds return locomo_driver_register(&keyboard_driver); 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds static void __exit locomokbd_exit(void) 3401da177e4SLinus Torvalds { 3411da177e4SLinus Torvalds locomo_driver_unregister(&keyboard_driver); 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds module_init(locomokbd_init); 3451da177e4SLinus Torvalds module_exit(locomokbd_exit); 346