Path: blob/master/drivers/input/keyboard/locomokbd.c
15109 views
/*1* LoCoMo keyboard driver for Linux-based ARM PDAs:2* - SHARP Zaurus Collie (SL-5500)3* - SHARP Zaurus Poodle (SL-5600)4*5* Copyright (c) 2005 John Lenz6* Based on from xtkbd.c7*8*9* This program is free software; you can redistribute it and/or modify10* it under the terms of the GNU General Public License as published by11* the Free Software Foundation; either version 2 of the License, or12* (at your option) any later version.13*14* This program is distributed in the hope that it will be useful,15* but WITHOUT ANY WARRANTY; without even the implied warranty of16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the17* GNU General Public License for more details.18*19* You should have received a copy of the GNU General Public License20* along with this program; if not, write to the Free Software21* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA22*23*/2425#include <linux/slab.h>26#include <linux/module.h>27#include <linux/init.h>28#include <linux/input.h>29#include <linux/delay.h>30#include <linux/device.h>31#include <linux/interrupt.h>32#include <linux/ioport.h>3334#include <asm/hardware/locomo.h>35#include <asm/irq.h>3637MODULE_AUTHOR("John Lenz <[email protected]>");38MODULE_DESCRIPTION("LoCoMo keyboard driver");39MODULE_LICENSE("GPL");4041#define LOCOMOKBD_NUMKEYS 1284243#define KEY_ACTIVITY KEY_F1644#define KEY_CONTACT KEY_F1845#define KEY_CENTER KEY_F154647static const unsigned char48locomokbd_keycode[LOCOMOKBD_NUMKEYS] __devinitconst = {490, KEY_ESC, KEY_ACTIVITY, 0, 0, 0, 0, 0, 0, 0, /* 0 - 9 */500, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_HOME, KEY_CONTACT, /* 10 - 19 */510, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 29 */520, 0, 0, KEY_CENTER, 0, KEY_MAIL, 0, 0, 0, 0, /* 30 - 39 */530, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RIGHT, /* 40 - 49 */54KEY_UP, KEY_LEFT, 0, 0, KEY_P, 0, KEY_O, KEY_I, KEY_Y, KEY_T, /* 50 - 59 */55KEY_E, KEY_W, 0, 0, 0, 0, KEY_DOWN, KEY_ENTER, 0, 0, /* 60 - 69 */56KEY_BACKSPACE, 0, KEY_L, KEY_U, KEY_H, KEY_R, KEY_D, KEY_Q, 0, 0, /* 70 - 79 */570, 0, 0, 0, 0, 0, KEY_ENTER, KEY_RIGHTSHIFT, KEY_K, KEY_J, /* 80 - 89 */58KEY_G, KEY_F, KEY_X, KEY_S, 0, 0, 0, 0, 0, 0, /* 90 - 99 */590, 0, KEY_DOT, 0, KEY_COMMA, KEY_N, KEY_B, KEY_C, KEY_Z, KEY_A, /* 100 - 109 */60KEY_LEFTSHIFT, KEY_TAB, KEY_LEFTCTRL, 0, 0, 0, 0, 0, 0, 0, /* 110 - 119 */61KEY_M, KEY_SPACE, KEY_V, KEY_APOSTROPHE, KEY_SLASH, 0, 0, 0 /* 120 - 128 */62};6364#define KB_ROWS 1665#define KB_COLS 866#define KB_ROWMASK(r) (1 << (r))67#define SCANCODE(c,r) ( ((c)<<4) + (r) + 1 )6869#define KB_DELAY 870#define SCAN_INTERVAL (HZ/10)7172struct locomokbd {73unsigned char keycode[LOCOMOKBD_NUMKEYS];74struct input_dev *input;75char phys[32];7677unsigned long base;78spinlock_t lock;7980struct timer_list timer;81unsigned long suspend_jiffies;82unsigned int count_cancel;83};8485/* helper functions for reading the keyboard matrix */86static inline void locomokbd_charge_all(unsigned long membase)87{88locomo_writel(0x00FF, membase + LOCOMO_KSC);89}9091static inline void locomokbd_activate_all(unsigned long membase)92{93unsigned long r;9495locomo_writel(0, membase + LOCOMO_KSC);96r = locomo_readl(membase + LOCOMO_KIC);97r &= 0xFEFF;98locomo_writel(r, membase + LOCOMO_KIC);99}100101static inline void locomokbd_activate_col(unsigned long membase, int col)102{103unsigned short nset;104unsigned short nbset;105106nset = 0xFF & ~(1 << col);107nbset = (nset << 8) + nset;108locomo_writel(nbset, membase + LOCOMO_KSC);109}110111static inline void locomokbd_reset_col(unsigned long membase, int col)112{113unsigned short nbset;114115nbset = ((0xFF & ~(1 << col)) << 8) + 0xFF;116locomo_writel(nbset, membase + LOCOMO_KSC);117}118119/*120* The LoCoMo keyboard only generates interrupts when a key is pressed.121* So when a key is pressed, we enable a timer. This timer scans the122* keyboard, and this is how we detect when the key is released.123*/124125/* Scan the hardware keyboard and push any changes up through the input layer */126static void locomokbd_scankeyboard(struct locomokbd *locomokbd)127{128unsigned int row, col, rowd;129unsigned long flags;130unsigned int num_pressed;131unsigned long membase = locomokbd->base;132133spin_lock_irqsave(&locomokbd->lock, flags);134135locomokbd_charge_all(membase);136137num_pressed = 0;138for (col = 0; col < KB_COLS; col++) {139140locomokbd_activate_col(membase, col);141udelay(KB_DELAY);142143rowd = ~locomo_readl(membase + LOCOMO_KIB);144for (row = 0; row < KB_ROWS; row++) {145unsigned int scancode, pressed, key;146147scancode = SCANCODE(col, row);148pressed = rowd & KB_ROWMASK(row);149key = locomokbd->keycode[scancode];150151input_report_key(locomokbd->input, key, pressed);152if (likely(!pressed))153continue;154155num_pressed++;156157/* The "Cancel/ESC" key is labeled "On/Off" on158* Collie and Poodle and should suspend the device159* if it was pressed for more than a second. */160if (unlikely(key == KEY_ESC)) {161if (!time_after(jiffies,162locomokbd->suspend_jiffies + HZ))163continue;164if (locomokbd->count_cancel++165!= (HZ/SCAN_INTERVAL + 1))166continue;167input_event(locomokbd->input, EV_PWR,168KEY_SUSPEND, 1);169locomokbd->suspend_jiffies = jiffies;170} else171locomokbd->count_cancel = 0;172}173locomokbd_reset_col(membase, col);174}175locomokbd_activate_all(membase);176177input_sync(locomokbd->input);178179/* if any keys are pressed, enable the timer */180if (num_pressed)181mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL);182else183locomokbd->count_cancel = 0;184185spin_unlock_irqrestore(&locomokbd->lock, flags);186}187188/*189* LoCoMo keyboard interrupt handler.190*/191static irqreturn_t locomokbd_interrupt(int irq, void *dev_id)192{193struct locomokbd *locomokbd = dev_id;194u16 r;195196r = locomo_readl(locomokbd->base + LOCOMO_KIC);197if ((r & 0x0001) == 0)198return IRQ_HANDLED;199200locomo_writel(r & ~0x0100, locomokbd->base + LOCOMO_KIC); /* Ack */201202/** wait chattering delay **/203udelay(100);204205locomokbd_scankeyboard(locomokbd);206return IRQ_HANDLED;207}208209/*210* LoCoMo timer checking for released keys211*/212static void locomokbd_timer_callback(unsigned long data)213{214struct locomokbd *locomokbd = (struct locomokbd *) data;215216locomokbd_scankeyboard(locomokbd);217}218219static int locomokbd_open(struct input_dev *dev)220{221struct locomokbd *locomokbd = input_get_drvdata(dev);222u16 r;223224r = locomo_readl(locomokbd->base + LOCOMO_KIC) | 0x0010;225locomo_writel(r, locomokbd->base + LOCOMO_KIC);226return 0;227}228229static void locomokbd_close(struct input_dev *dev)230{231struct locomokbd *locomokbd = input_get_drvdata(dev);232u16 r;233234r = locomo_readl(locomokbd->base + LOCOMO_KIC) & ~0x0010;235locomo_writel(r, locomokbd->base + LOCOMO_KIC);236}237238static int __devinit locomokbd_probe(struct locomo_dev *dev)239{240struct locomokbd *locomokbd;241struct input_dev *input_dev;242int i, err;243244locomokbd = kzalloc(sizeof(struct locomokbd), GFP_KERNEL);245input_dev = input_allocate_device();246if (!locomokbd || !input_dev) {247err = -ENOMEM;248goto err_free_mem;249}250251/* try and claim memory region */252if (!request_mem_region((unsigned long) dev->mapbase,253dev->length,254LOCOMO_DRIVER_NAME(dev))) {255err = -EBUSY;256printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n");257goto err_free_mem;258}259260locomo_set_drvdata(dev, locomokbd);261262locomokbd->base = (unsigned long) dev->mapbase;263264spin_lock_init(&locomokbd->lock);265266init_timer(&locomokbd->timer);267locomokbd->timer.function = locomokbd_timer_callback;268locomokbd->timer.data = (unsigned long) locomokbd;269270locomokbd->suspend_jiffies = jiffies;271272locomokbd->input = input_dev;273strcpy(locomokbd->phys, "locomokbd/input0");274275input_dev->name = "LoCoMo keyboard";276input_dev->phys = locomokbd->phys;277input_dev->id.bustype = BUS_HOST;278input_dev->id.vendor = 0x0001;279input_dev->id.product = 0x0001;280input_dev->id.version = 0x0100;281input_dev->open = locomokbd_open;282input_dev->close = locomokbd_close;283input_dev->dev.parent = &dev->dev;284285input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |286BIT_MASK(EV_PWR);287input_dev->keycode = locomokbd->keycode;288input_dev->keycodesize = sizeof(locomokbd_keycode[0]);289input_dev->keycodemax = ARRAY_SIZE(locomokbd_keycode);290291input_set_drvdata(input_dev, locomokbd);292293memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode));294for (i = 0; i < LOCOMOKBD_NUMKEYS; i++)295set_bit(locomokbd->keycode[i], input_dev->keybit);296clear_bit(0, input_dev->keybit);297298/* attempt to get the interrupt */299err = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd);300if (err) {301printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n");302goto err_release_region;303}304305err = input_register_device(locomokbd->input);306if (err)307goto err_free_irq;308309return 0;310311err_free_irq:312free_irq(dev->irq[0], locomokbd);313err_release_region:314release_mem_region((unsigned long) dev->mapbase, dev->length);315locomo_set_drvdata(dev, NULL);316err_free_mem:317input_free_device(input_dev);318kfree(locomokbd);319320return err;321}322323static int __devexit locomokbd_remove(struct locomo_dev *dev)324{325struct locomokbd *locomokbd = locomo_get_drvdata(dev);326327free_irq(dev->irq[0], locomokbd);328329del_timer_sync(&locomokbd->timer);330331input_unregister_device(locomokbd->input);332locomo_set_drvdata(dev, NULL);333334release_mem_region((unsigned long) dev->mapbase, dev->length);335336kfree(locomokbd);337338return 0;339}340341static struct locomo_driver keyboard_driver = {342.drv = {343.name = "locomokbd"344},345.devid = LOCOMO_DEVID_KEYBOARD,346.probe = locomokbd_probe,347.remove = __devexit_p(locomokbd_remove),348};349350static int __init locomokbd_init(void)351{352return locomo_driver_register(&keyboard_driver);353}354355static void __exit locomokbd_exit(void)356{357locomo_driver_unregister(&keyboard_driver);358}359360module_init(locomokbd_init);361module_exit(locomokbd_exit);362363364