Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * LoCoMo keyboard driver for Linux-based ARM PDAs:
0004  *  - SHARP Zaurus Collie (SL-5500)
0005  *  - SHARP Zaurus Poodle (SL-5600)
0006  *
0007  * Copyright (c) 2005 John Lenz
0008  * Based on from xtkbd.c
0009  */
0010 
0011 #include <linux/slab.h>
0012 #include <linux/module.h>
0013 #include <linux/init.h>
0014 #include <linux/input.h>
0015 #include <linux/delay.h>
0016 #include <linux/device.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/ioport.h>
0019 
0020 #include <asm/hardware/locomo.h>
0021 #include <asm/irq.h>
0022 
0023 MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
0024 MODULE_DESCRIPTION("LoCoMo keyboard driver");
0025 MODULE_LICENSE("GPL");
0026 
0027 #define LOCOMOKBD_NUMKEYS   128
0028 
0029 #define KEY_ACTIVITY        KEY_F16
0030 #define KEY_CONTACT     KEY_F18
0031 #define KEY_CENTER      KEY_F15
0032 
0033 static const unsigned char
0034 locomokbd_keycode[LOCOMOKBD_NUMKEYS] = {
0035     0, KEY_ESC, KEY_ACTIVITY, 0, 0, 0, 0, 0, 0, 0,              /* 0 - 9 */
0036     0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_HOME, KEY_CONTACT,           /* 10 - 19 */
0037     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                       /* 20 - 29 */
0038     0, 0, 0, KEY_CENTER, 0, KEY_MAIL, 0, 0, 0, 0,               /* 30 - 39 */
0039     0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RIGHT,                   /* 40 - 49 */
0040     KEY_UP, KEY_LEFT, 0, 0, KEY_P, 0, KEY_O, KEY_I, KEY_Y, KEY_T,       /* 50 - 59 */
0041     KEY_E, KEY_W, 0, 0, 0, 0, KEY_DOWN, KEY_ENTER, 0, 0,            /* 60 - 69 */
0042     KEY_BACKSPACE, 0, KEY_L, KEY_U, KEY_H, KEY_R, KEY_D, KEY_Q, 0, 0,   /* 70 - 79 */
0043     0, 0, 0, 0, 0, 0, KEY_ENTER, KEY_RIGHTSHIFT, KEY_K, KEY_J,      /* 80 - 89 */
0044     KEY_G, KEY_F, KEY_X, KEY_S, 0, 0, 0, 0, 0, 0,               /* 90 - 99 */
0045     0, 0, KEY_DOT, 0, KEY_COMMA, KEY_N, KEY_B, KEY_C, KEY_Z, KEY_A,     /* 100 - 109 */
0046     KEY_LEFTSHIFT, KEY_TAB, KEY_LEFTCTRL, 0, 0, 0, 0, 0, 0, 0,      /* 110 - 119 */
0047     KEY_M, KEY_SPACE, KEY_V, KEY_APOSTROPHE, KEY_SLASH, 0, 0, 0     /* 120 - 128 */
0048 };
0049 
0050 #define KB_ROWS         16
0051 #define KB_COLS         8
0052 #define KB_ROWMASK(r)       (1 << (r))
0053 #define SCANCODE(c,r)       ( ((c)<<4) + (r) + 1 )
0054 
0055 #define KB_DELAY        8
0056 #define SCAN_INTERVAL       (HZ/10)
0057 
0058 struct locomokbd {
0059     unsigned char keycode[LOCOMOKBD_NUMKEYS];
0060     struct input_dev *input;
0061     char phys[32];
0062 
0063     unsigned long base;
0064     spinlock_t lock;
0065 
0066     struct timer_list timer;
0067     unsigned long suspend_jiffies;
0068     unsigned int count_cancel;
0069 };
0070 
0071 /* helper functions for reading the keyboard matrix */
0072 static inline void locomokbd_charge_all(unsigned long membase)
0073 {
0074     locomo_writel(0x00FF, membase + LOCOMO_KSC);
0075 }
0076 
0077 static inline void locomokbd_activate_all(unsigned long membase)
0078 {
0079     unsigned long r;
0080 
0081     locomo_writel(0, membase + LOCOMO_KSC);
0082     r = locomo_readl(membase + LOCOMO_KIC);
0083     r &= 0xFEFF;
0084     locomo_writel(r, membase + LOCOMO_KIC);
0085 }
0086 
0087 static inline void locomokbd_activate_col(unsigned long membase, int col)
0088 {
0089     unsigned short nset;
0090     unsigned short nbset;
0091 
0092     nset = 0xFF & ~(1 << col);
0093     nbset = (nset << 8) + nset;
0094     locomo_writel(nbset, membase + LOCOMO_KSC);
0095 }
0096 
0097 static inline void locomokbd_reset_col(unsigned long membase, int col)
0098 {
0099     unsigned short nbset;
0100 
0101     nbset = ((0xFF & ~(1 << col)) << 8) + 0xFF;
0102     locomo_writel(nbset, membase + LOCOMO_KSC);
0103 }
0104 
0105 /*
0106  * The LoCoMo keyboard only generates interrupts when a key is pressed.
0107  * So when a key is pressed, we enable a timer.  This timer scans the
0108  * keyboard, and this is how we detect when the key is released.
0109  */
0110 
0111 /* Scan the hardware keyboard and push any changes up through the input layer */
0112 static void locomokbd_scankeyboard(struct locomokbd *locomokbd)
0113 {
0114     unsigned int row, col, rowd;
0115     unsigned long flags;
0116     unsigned int num_pressed;
0117     unsigned long membase = locomokbd->base;
0118 
0119     spin_lock_irqsave(&locomokbd->lock, flags);
0120 
0121     locomokbd_charge_all(membase);
0122 
0123     num_pressed = 0;
0124     for (col = 0; col < KB_COLS; col++) {
0125 
0126         locomokbd_activate_col(membase, col);
0127         udelay(KB_DELAY);
0128 
0129         rowd = ~locomo_readl(membase + LOCOMO_KIB);
0130         for (row = 0; row < KB_ROWS; row++) {
0131             unsigned int scancode, pressed, key;
0132 
0133             scancode = SCANCODE(col, row);
0134             pressed = rowd & KB_ROWMASK(row);
0135             key = locomokbd->keycode[scancode];
0136 
0137             input_report_key(locomokbd->input, key, pressed);
0138             if (likely(!pressed))
0139                 continue;
0140 
0141             num_pressed++;
0142 
0143             /* The "Cancel/ESC" key is labeled "On/Off" on
0144              * Collie and Poodle and should suspend the device
0145              * if it was pressed for more than a second. */
0146             if (unlikely(key == KEY_ESC)) {
0147                 if (!time_after(jiffies,
0148                     locomokbd->suspend_jiffies + HZ))
0149                     continue;
0150                 if (locomokbd->count_cancel++
0151                     != (HZ/SCAN_INTERVAL + 1))
0152                     continue;
0153                 input_event(locomokbd->input, EV_PWR,
0154                     KEY_SUSPEND, 1);
0155                 locomokbd->suspend_jiffies = jiffies;
0156             } else
0157                 locomokbd->count_cancel = 0;
0158         }
0159         locomokbd_reset_col(membase, col);
0160     }
0161     locomokbd_activate_all(membase);
0162 
0163     input_sync(locomokbd->input);
0164 
0165     /* if any keys are pressed, enable the timer */
0166     if (num_pressed)
0167         mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL);
0168     else
0169         locomokbd->count_cancel = 0;
0170 
0171     spin_unlock_irqrestore(&locomokbd->lock, flags);
0172 }
0173 
0174 /*
0175  * LoCoMo keyboard interrupt handler.
0176  */
0177 static irqreturn_t locomokbd_interrupt(int irq, void *dev_id)
0178 {
0179     struct locomokbd *locomokbd = dev_id;
0180     u16 r;
0181 
0182     r = locomo_readl(locomokbd->base + LOCOMO_KIC);
0183     if ((r & 0x0001) == 0)
0184         return IRQ_HANDLED;
0185 
0186     locomo_writel(r & ~0x0100, locomokbd->base + LOCOMO_KIC); /* Ack */
0187 
0188     /** wait chattering delay **/
0189     udelay(100);
0190 
0191     locomokbd_scankeyboard(locomokbd);
0192     return IRQ_HANDLED;
0193 }
0194 
0195 /*
0196  * LoCoMo timer checking for released keys
0197  */
0198 static void locomokbd_timer_callback(struct timer_list *t)
0199 {
0200     struct locomokbd *locomokbd = from_timer(locomokbd, t, timer);
0201 
0202     locomokbd_scankeyboard(locomokbd);
0203 }
0204 
0205 static int locomokbd_open(struct input_dev *dev)
0206 {
0207     struct locomokbd *locomokbd = input_get_drvdata(dev);
0208     u16 r;
0209     
0210     r = locomo_readl(locomokbd->base + LOCOMO_KIC) | 0x0010;
0211     locomo_writel(r, locomokbd->base + LOCOMO_KIC);
0212     return 0;
0213 }
0214 
0215 static void locomokbd_close(struct input_dev *dev)
0216 {
0217     struct locomokbd *locomokbd = input_get_drvdata(dev);
0218     u16 r;
0219     
0220     r = locomo_readl(locomokbd->base + LOCOMO_KIC) & ~0x0010;
0221     locomo_writel(r, locomokbd->base + LOCOMO_KIC);
0222 }
0223 
0224 static int locomokbd_probe(struct locomo_dev *dev)
0225 {
0226     struct locomokbd *locomokbd;
0227     struct input_dev *input_dev;
0228     int i, err;
0229 
0230     locomokbd = kzalloc(sizeof(struct locomokbd), GFP_KERNEL);
0231     input_dev = input_allocate_device();
0232     if (!locomokbd || !input_dev) {
0233         err = -ENOMEM;
0234         goto err_free_mem;
0235     }
0236 
0237     /* try and claim memory region */
0238     if (!request_mem_region((unsigned long) dev->mapbase,
0239                 dev->length,
0240                 LOCOMO_DRIVER_NAME(dev))) {
0241         err = -EBUSY;
0242         printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n");
0243         goto err_free_mem;
0244     }
0245 
0246     locomo_set_drvdata(dev, locomokbd);
0247 
0248     locomokbd->base = (unsigned long) dev->mapbase;
0249 
0250     spin_lock_init(&locomokbd->lock);
0251 
0252     timer_setup(&locomokbd->timer, locomokbd_timer_callback, 0);
0253 
0254     locomokbd->suspend_jiffies = jiffies;
0255 
0256     locomokbd->input = input_dev;
0257     strcpy(locomokbd->phys, "locomokbd/input0");
0258 
0259     input_dev->name = "LoCoMo keyboard";
0260     input_dev->phys = locomokbd->phys;
0261     input_dev->id.bustype = BUS_HOST;
0262     input_dev->id.vendor = 0x0001;
0263     input_dev->id.product = 0x0001;
0264     input_dev->id.version = 0x0100;
0265     input_dev->open = locomokbd_open;
0266     input_dev->close = locomokbd_close;
0267     input_dev->dev.parent = &dev->dev;
0268 
0269     input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
0270                 BIT_MASK(EV_PWR);
0271     input_dev->keycode = locomokbd->keycode;
0272     input_dev->keycodesize = sizeof(locomokbd_keycode[0]);
0273     input_dev->keycodemax = ARRAY_SIZE(locomokbd_keycode);
0274 
0275     input_set_drvdata(input_dev, locomokbd);
0276 
0277     memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode));
0278     for (i = 0; i < LOCOMOKBD_NUMKEYS; i++)
0279         set_bit(locomokbd->keycode[i], input_dev->keybit);
0280     clear_bit(0, input_dev->keybit);
0281 
0282     /* attempt to get the interrupt */
0283     err = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd);
0284     if (err) {
0285         printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n");
0286         goto err_release_region;
0287     }
0288 
0289     err = input_register_device(locomokbd->input);
0290     if (err)
0291         goto err_free_irq;
0292 
0293     return 0;
0294 
0295  err_free_irq:
0296     free_irq(dev->irq[0], locomokbd);
0297  err_release_region:
0298     release_mem_region((unsigned long) dev->mapbase, dev->length);
0299     locomo_set_drvdata(dev, NULL);
0300  err_free_mem:
0301     input_free_device(input_dev);
0302     kfree(locomokbd);
0303 
0304     return err;
0305 }
0306 
0307 static void locomokbd_remove(struct locomo_dev *dev)
0308 {
0309     struct locomokbd *locomokbd = locomo_get_drvdata(dev);
0310 
0311     free_irq(dev->irq[0], locomokbd);
0312 
0313     del_timer_sync(&locomokbd->timer);
0314 
0315     input_unregister_device(locomokbd->input);
0316     locomo_set_drvdata(dev, NULL);
0317 
0318     release_mem_region((unsigned long) dev->mapbase, dev->length);
0319 
0320     kfree(locomokbd);
0321 }
0322 
0323 static struct locomo_driver keyboard_driver = {
0324     .drv = {
0325         .name = "locomokbd"
0326     },
0327     .devid  = LOCOMO_DEVID_KEYBOARD,
0328     .probe  = locomokbd_probe,
0329     .remove = locomokbd_remove,
0330 };
0331 
0332 static int __init locomokbd_init(void)
0333 {
0334     return locomo_driver_register(&keyboard_driver);
0335 }
0336 
0337 static void __exit locomokbd_exit(void)
0338 {
0339     locomo_driver_unregister(&keyboard_driver);
0340 }
0341 
0342 module_init(locomokbd_init);
0343 module_exit(locomokbd_exit);