linux/drivers/input/keyboard/sh_keysc.c
<<
>>
Prefs
   1/*
   2 * SuperH KEYSC Keypad Driver
   3 *
   4 * Copyright (C) 2008 Magnus Damm
   5 *
   6 * Based on gpio_keys.c, Copyright 2005 Phil Blundell
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License version 2 as
  10 * published by the Free Software Foundation.
  11 */
  12
  13#include <linux/kernel.h>
  14#include <linux/module.h>
  15#include <linux/init.h>
  16#include <linux/interrupt.h>
  17#include <linux/irq.h>
  18#include <linux/delay.h>
  19#include <linux/platform_device.h>
  20#include <linux/input.h>
  21#include <linux/input/sh_keysc.h>
  22#include <linux/bitmap.h>
  23#include <linux/clk.h>
  24#include <linux/io.h>
  25#include <linux/slab.h>
  26
  27static const struct {
  28        unsigned char kymd, keyout, keyin;
  29} sh_keysc_mode[] = {
  30        [SH_KEYSC_MODE_1] = { 0, 6, 5 },
  31        [SH_KEYSC_MODE_2] = { 1, 5, 6 },
  32        [SH_KEYSC_MODE_3] = { 2, 4, 7 },
  33        [SH_KEYSC_MODE_4] = { 3, 6, 6 },
  34        [SH_KEYSC_MODE_5] = { 4, 6, 7 },
  35        [SH_KEYSC_MODE_6] = { 5, 7, 7 },
  36};
  37
  38struct sh_keysc_priv {
  39        void __iomem *iomem_base;
  40        struct clk *clk;
  41        DECLARE_BITMAP(last_keys, SH_KEYSC_MAXKEYS);
  42        struct input_dev *input;
  43        struct sh_keysc_info pdata;
  44};
  45
  46#define KYCR1 0
  47#define KYCR2 1
  48#define KYINDR 2
  49#define KYOUTDR 3
  50
  51#define KYCR2_IRQ_LEVEL    0x10
  52#define KYCR2_IRQ_DISABLED 0x00
  53
  54static unsigned long sh_keysc_read(struct sh_keysc_priv *p, int reg_nr)
  55{
  56        return ioread16(p->iomem_base + (reg_nr << 2));
  57}
  58
  59static void sh_keysc_write(struct sh_keysc_priv *p, int reg_nr,
  60                           unsigned long value)
  61{
  62        iowrite16(value, p->iomem_base + (reg_nr << 2));
  63}
  64
  65static void sh_keysc_level_mode(struct sh_keysc_priv *p,
  66                                unsigned long keys_set)
  67{
  68        struct sh_keysc_info *pdata = &p->pdata;
  69
  70        sh_keysc_write(p, KYOUTDR, 0);
  71        sh_keysc_write(p, KYCR2, KYCR2_IRQ_LEVEL | (keys_set << 8));
  72
  73        if (pdata->kycr2_delay)
  74                udelay(pdata->kycr2_delay);
  75}
  76
  77static void sh_keysc_map_dbg(struct device *dev, unsigned long *map,
  78                             const char *str)
  79{
  80        int k;
  81
  82        for (k = 0; k < BITS_TO_LONGS(SH_KEYSC_MAXKEYS); k++)
  83                dev_dbg(dev, "%s[%d] 0x%lx\n", str, k, map[k]);
  84}
  85
  86static irqreturn_t sh_keysc_isr(int irq, void *dev_id)
  87{
  88        struct platform_device *pdev = dev_id;
  89        struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
  90        struct sh_keysc_info *pdata = &priv->pdata;
  91        int keyout_nr = sh_keysc_mode[pdata->mode].keyout;
  92        int keyin_nr = sh_keysc_mode[pdata->mode].keyin;
  93        DECLARE_BITMAP(keys, SH_KEYSC_MAXKEYS);
  94        DECLARE_BITMAP(keys0, SH_KEYSC_MAXKEYS);
  95        DECLARE_BITMAP(keys1, SH_KEYSC_MAXKEYS);
  96        unsigned char keyin_set, tmp;
  97        int i, k, n;
  98
  99        dev_dbg(&pdev->dev, "isr!\n");
 100
 101        bitmap_fill(keys1, SH_KEYSC_MAXKEYS);
 102        bitmap_zero(keys0, SH_KEYSC_MAXKEYS);
 103
 104        do {
 105                bitmap_zero(keys, SH_KEYSC_MAXKEYS);
 106                keyin_set = 0;
 107
 108                sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);
 109
 110                for (i = 0; i < keyout_nr; i++) {
 111                        n = keyin_nr * i;
 112
 113                        /* drive one KEYOUT pin low, read KEYIN pins */
 114                        sh_keysc_write(priv, KYOUTDR, 0xffff ^ (3 << (i * 2)));
 115                        udelay(pdata->delay);
 116                        tmp = sh_keysc_read(priv, KYINDR);
 117
 118                        /* set bit if key press has been detected */
 119                        for (k = 0; k < keyin_nr; k++) {
 120                                if (tmp & (1 << k))
 121                                        __set_bit(n + k, keys);
 122                        }
 123
 124                        /* keep track of which KEYIN bits that have been set */
 125                        keyin_set |= tmp ^ ((1 << keyin_nr) - 1);
 126                }
 127
 128                sh_keysc_level_mode(priv, keyin_set);
 129
 130                bitmap_complement(keys, keys, SH_KEYSC_MAXKEYS);
 131                bitmap_and(keys1, keys1, keys, SH_KEYSC_MAXKEYS);
 132                bitmap_or(keys0, keys0, keys, SH_KEYSC_MAXKEYS);
 133
 134                sh_keysc_map_dbg(&pdev->dev, keys, "keys");
 135
 136        } while (sh_keysc_read(priv, KYCR2) & 0x01);
 137
 138        sh_keysc_map_dbg(&pdev->dev, priv->last_keys, "last_keys");
 139        sh_keysc_map_dbg(&pdev->dev, keys0, "keys0");
 140        sh_keysc_map_dbg(&pdev->dev, keys1, "keys1");
 141
 142        for (i = 0; i < SH_KEYSC_MAXKEYS; i++) {
 143                k = pdata->keycodes[i];
 144                if (!k)
 145                        continue;
 146
 147                if (test_bit(i, keys0) == test_bit(i, priv->last_keys))
 148                        continue;
 149
 150                if (test_bit(i, keys1) || test_bit(i, keys0)) {
 151                        input_event(priv->input, EV_KEY, k, 1);
 152                        __set_bit(i, priv->last_keys);
 153                }
 154
 155                if (!test_bit(i, keys1)) {
 156                        input_event(priv->input, EV_KEY, k, 0);
 157                        __clear_bit(i, priv->last_keys);
 158                }
 159
 160        }
 161        input_sync(priv->input);
 162
 163        return IRQ_HANDLED;
 164}
 165
 166static int __devinit sh_keysc_probe(struct platform_device *pdev)
 167{
 168        struct sh_keysc_priv *priv;
 169        struct sh_keysc_info *pdata;
 170        struct resource *res;
 171        struct input_dev *input;
 172        char clk_name[8];
 173        int i;
 174        int irq, error;
 175
 176        if (!pdev->dev.platform_data) {
 177                dev_err(&pdev->dev, "no platform data defined\n");
 178                error = -EINVAL;
 179                goto err0;
 180        }
 181
 182        error = -ENXIO;
 183        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 184        if (res == NULL) {
 185                dev_err(&pdev->dev, "failed to get I/O memory\n");
 186                goto err0;
 187        }
 188
 189        irq = platform_get_irq(pdev, 0);
 190        if (irq < 0) {
 191                dev_err(&pdev->dev, "failed to get irq\n");
 192                goto err0;
 193        }
 194
 195        priv = kzalloc(sizeof(*priv), GFP_KERNEL);
 196        if (priv == NULL) {
 197                dev_err(&pdev->dev, "failed to allocate driver data\n");
 198                error = -ENOMEM;
 199                goto err0;
 200        }
 201
 202        platform_set_drvdata(pdev, priv);
 203        memcpy(&priv->pdata, pdev->dev.platform_data, sizeof(priv->pdata));
 204        pdata = &priv->pdata;
 205
 206        priv->iomem_base = ioremap_nocache(res->start, resource_size(res));
 207        if (priv->iomem_base == NULL) {
 208                dev_err(&pdev->dev, "failed to remap I/O memory\n");
 209                error = -ENXIO;
 210                goto err1;
 211        }
 212
 213        snprintf(clk_name, sizeof(clk_name), "keysc%d", pdev->id);
 214        priv->clk = clk_get(&pdev->dev, clk_name);
 215        if (IS_ERR(priv->clk)) {
 216                dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
 217                error = PTR_ERR(priv->clk);
 218                goto err2;
 219        }
 220
 221        priv->input = input_allocate_device();
 222        if (!priv->input) {
 223                dev_err(&pdev->dev, "failed to allocate input device\n");
 224                error = -ENOMEM;
 225                goto err3;
 226        }
 227
 228        input = priv->input;
 229        input->evbit[0] = BIT_MASK(EV_KEY);
 230
 231        input->name = pdev->name;
 232        input->phys = "sh-keysc-keys/input0";
 233        input->dev.parent = &pdev->dev;
 234
 235        input->id.bustype = BUS_HOST;
 236        input->id.vendor = 0x0001;
 237        input->id.product = 0x0001;
 238        input->id.version = 0x0100;
 239
 240        input->keycode = pdata->keycodes;
 241        input->keycodesize = sizeof(pdata->keycodes[0]);
 242        input->keycodemax = ARRAY_SIZE(pdata->keycodes);
 243
 244        error = request_irq(irq, sh_keysc_isr, 0, pdev->name, pdev);
 245        if (error) {
 246                dev_err(&pdev->dev, "failed to request IRQ\n");
 247                goto err4;
 248        }
 249
 250        for (i = 0; i < SH_KEYSC_MAXKEYS; i++)
 251                __set_bit(pdata->keycodes[i], input->keybit);
 252        __clear_bit(KEY_RESERVED, input->keybit);
 253
 254        error = input_register_device(input);
 255        if (error) {
 256                dev_err(&pdev->dev, "failed to register input device\n");
 257                goto err5;
 258        }
 259
 260        clk_enable(priv->clk);
 261
 262        sh_keysc_write(priv, KYCR1, (sh_keysc_mode[pdata->mode].kymd << 8) |
 263                       pdata->scan_timing);
 264        sh_keysc_level_mode(priv, 0);
 265
 266        device_init_wakeup(&pdev->dev, 1);
 267
 268        return 0;
 269
 270 err5:
 271        free_irq(irq, pdev);
 272 err4:
 273        input_free_device(input);
 274 err3:
 275        clk_put(priv->clk);
 276 err2:
 277        iounmap(priv->iomem_base);
 278 err1:
 279        platform_set_drvdata(pdev, NULL);
 280        kfree(priv);
 281 err0:
 282        return error;
 283}
 284
 285static int __devexit sh_keysc_remove(struct platform_device *pdev)
 286{
 287        struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
 288
 289        sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);
 290
 291        input_unregister_device(priv->input);
 292        free_irq(platform_get_irq(pdev, 0), pdev);
 293        iounmap(priv->iomem_base);
 294
 295        clk_disable(priv->clk);
 296        clk_put(priv->clk);
 297
 298        platform_set_drvdata(pdev, NULL);
 299        kfree(priv);
 300
 301        return 0;
 302}
 303
 304static int sh_keysc_suspend(struct device *dev)
 305{
 306        struct platform_device *pdev = to_platform_device(dev);
 307        struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
 308        int irq = platform_get_irq(pdev, 0);
 309        unsigned short value;
 310
 311        value = sh_keysc_read(priv, KYCR1);
 312
 313        if (device_may_wakeup(dev)) {
 314                value |= 0x80;
 315                enable_irq_wake(irq);
 316        } else {
 317                value &= ~0x80;
 318        }
 319
 320        sh_keysc_write(priv, KYCR1, value);
 321
 322        return 0;
 323}
 324
 325static int sh_keysc_resume(struct device *dev)
 326{
 327        struct platform_device *pdev = to_platform_device(dev);
 328        int irq = platform_get_irq(pdev, 0);
 329
 330        if (device_may_wakeup(dev))
 331                disable_irq_wake(irq);
 332
 333        return 0;
 334}
 335
 336static const struct dev_pm_ops sh_keysc_dev_pm_ops = {
 337        .suspend = sh_keysc_suspend,
 338        .resume = sh_keysc_resume,
 339};
 340
 341struct platform_driver sh_keysc_device_driver = {
 342        .probe          = sh_keysc_probe,
 343        .remove         = __devexit_p(sh_keysc_remove),
 344        .driver         = {
 345                .name   = "sh_keysc",
 346                .pm     = &sh_keysc_dev_pm_ops,
 347        }
 348};
 349
 350static int __init sh_keysc_init(void)
 351{
 352        return platform_driver_register(&sh_keysc_device_driver);
 353}
 354
 355static void __exit sh_keysc_exit(void)
 356{
 357        platform_driver_unregister(&sh_keysc_device_driver);
 358}
 359
 360module_init(sh_keysc_init);
 361module_exit(sh_keysc_exit);
 362
 363MODULE_AUTHOR("Magnus Damm");
 364MODULE_DESCRIPTION("SuperH KEYSC Keypad Driver");
 365MODULE_LICENSE("GPL");
 366
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.