linux/drivers/input/keyboard/w90p910_keypad.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2008-2009 Nuvoton technology corporation.
   3 *
   4 * Wan ZongShun <mcuos.com@gmail.com>
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation;version 2 of the License.
   9 *
  10 */
  11
  12#include <linux/kernel.h>
  13#include <linux/module.h>
  14#include <linux/init.h>
  15#include <linux/interrupt.h>
  16#include <linux/input.h>
  17#include <linux/device.h>
  18#include <linux/platform_device.h>
  19#include <linux/clk.h>
  20#include <linux/err.h>
  21#include <linux/io.h>
  22#include <linux/slab.h>
  23
  24#include <mach/w90p910_keypad.h>
  25
  26/* Keypad Interface Control Registers */
  27#define KPI_CONF                0x00
  28#define KPI_3KCONF              0x04
  29#define KPI_LPCONF              0x08
  30#define KPI_STATUS              0x0C
  31
  32#define IS1KEY                  (0x01 << 16)
  33#define INTTR                   (0x01 << 21)
  34#define KEY0R                   (0x0f << 3)
  35#define KEY0C                   0x07
  36#define DEBOUNCE_BIT            0x08
  37#define KSIZE0                  (0x01 << 16)
  38#define KSIZE1                  (0x01 << 17)
  39#define KPSEL                   (0x01 << 19)
  40#define ENKP                    (0x01 << 18)
  41
  42#define KGET_RAW(n)             (((n) & KEY0R) >> 3)
  43#define KGET_COLUMN(n)          ((n) & KEY0C)
  44
  45#define W90P910_NUM_ROWS        8
  46#define W90P910_NUM_COLS        8
  47#define W90P910_ROW_SHIFT       3
  48
  49struct w90p910_keypad {
  50        const struct w90p910_keypad_platform_data *pdata;
  51        struct clk *clk;
  52        struct input_dev *input_dev;
  53        void __iomem *mmio_base;
  54        int irq;
  55        unsigned short keymap[W90P910_NUM_ROWS * W90P910_NUM_COLS];
  56};
  57
  58static void w90p910_keypad_scan_matrix(struct w90p910_keypad *keypad,
  59                                                        unsigned int status)
  60{
  61        struct input_dev *input_dev = keypad->input_dev;
  62        unsigned int row = KGET_RAW(status);
  63        unsigned int col = KGET_COLUMN(status);
  64        unsigned int code = MATRIX_SCAN_CODE(row, col, W90P910_ROW_SHIFT);
  65        unsigned int key = keypad->keymap[code];
  66
  67        input_event(input_dev, EV_MSC, MSC_SCAN, code);
  68        input_report_key(input_dev, key, 1);
  69        input_sync(input_dev);
  70
  71        input_event(input_dev, EV_MSC, MSC_SCAN, code);
  72        input_report_key(input_dev, key, 0);
  73        input_sync(input_dev);
  74}
  75
  76static irqreturn_t w90p910_keypad_irq_handler(int irq, void *dev_id)
  77{
  78        struct w90p910_keypad *keypad = dev_id;
  79        unsigned int  kstatus, val;
  80
  81        kstatus = __raw_readl(keypad->mmio_base + KPI_STATUS);
  82
  83        val = INTTR | IS1KEY;
  84
  85        if (kstatus & val)
  86                w90p910_keypad_scan_matrix(keypad, kstatus);
  87
  88        return IRQ_HANDLED;
  89}
  90
  91static int w90p910_keypad_open(struct input_dev *dev)
  92{
  93        struct w90p910_keypad *keypad = input_get_drvdata(dev);
  94        const struct w90p910_keypad_platform_data *pdata = keypad->pdata;
  95        unsigned int val, config;
  96
  97        /* Enable unit clock */
  98        clk_enable(keypad->clk);
  99
 100        val = __raw_readl(keypad->mmio_base + KPI_CONF);
 101        val |= (KPSEL | ENKP);
 102        val &= ~(KSIZE0 | KSIZE1);
 103
 104        config = pdata->prescale | (pdata->debounce << DEBOUNCE_BIT);
 105
 106        val |= config;
 107
 108        __raw_writel(val, keypad->mmio_base + KPI_CONF);
 109
 110        return 0;
 111}
 112
 113static void w90p910_keypad_close(struct input_dev *dev)
 114{
 115        struct w90p910_keypad *keypad = input_get_drvdata(dev);
 116
 117        /* Disable clock unit */
 118        clk_disable(keypad->clk);
 119}
 120
 121static int __devinit w90p910_keypad_probe(struct platform_device *pdev)
 122{
 123        const struct w90p910_keypad_platform_data *pdata =
 124                                                pdev->dev.platform_data;
 125        const struct matrix_keymap_data *keymap_data;
 126        struct w90p910_keypad *keypad;
 127        struct input_dev *input_dev;
 128        struct resource *res;
 129        int irq;
 130        int error;
 131
 132        if (!pdata) {
 133                dev_err(&pdev->dev, "no platform data defined\n");
 134                return -EINVAL;
 135        }
 136
 137        keymap_data = pdata->keymap_data;
 138
 139        irq = platform_get_irq(pdev, 0);
 140        if (irq < 0) {
 141                dev_err(&pdev->dev, "failed to get keypad irq\n");
 142                return -ENXIO;
 143        }
 144
 145        keypad = kzalloc(sizeof(struct w90p910_keypad), GFP_KERNEL);
 146        input_dev = input_allocate_device();
 147        if (!keypad || !input_dev) {
 148                dev_err(&pdev->dev, "failed to allocate driver data\n");
 149                error = -ENOMEM;
 150                goto failed_free;
 151        }
 152
 153        keypad->pdata = pdata;
 154        keypad->input_dev = input_dev;
 155        keypad->irq = irq;
 156
 157        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 158        if (res == NULL) {
 159                dev_err(&pdev->dev, "failed to get I/O memory\n");
 160                error = -ENXIO;
 161                goto failed_free;
 162        }
 163
 164        res = request_mem_region(res->start, resource_size(res), pdev->name);
 165        if (res == NULL) {
 166                dev_err(&pdev->dev, "failed to request I/O memory\n");
 167                error = -EBUSY;
 168                goto failed_free;
 169        }
 170
 171        keypad->mmio_base = ioremap(res->start, resource_size(res));
 172        if (keypad->mmio_base == NULL) {
 173                dev_err(&pdev->dev, "failed to remap I/O memory\n");
 174                error = -ENXIO;
 175                goto failed_free_res;
 176        }
 177
 178        keypad->clk = clk_get(&pdev->dev, NULL);
 179        if (IS_ERR(keypad->clk)) {
 180                dev_err(&pdev->dev, "failed to get keypad clock\n");
 181                error = PTR_ERR(keypad->clk);
 182                goto failed_free_io;
 183        }
 184
 185        /* set multi-function pin for w90p910 kpi. */
 186        mfp_set_groupi(&pdev->dev);
 187
 188        input_dev->name = pdev->name;
 189        input_dev->id.bustype = BUS_HOST;
 190        input_dev->open = w90p910_keypad_open;
 191        input_dev->close = w90p910_keypad_close;
 192        input_dev->dev.parent = &pdev->dev;
 193
 194        error = matrix_keypad_build_keymap(keymap_data, NULL,
 195                                           W90P910_NUM_ROWS, W90P910_NUM_COLS,
 196                                           keypad->keymap, input_dev);
 197        if (error) {
 198                dev_err(&pdev->dev, "failed to build keymap\n");
 199                goto failed_put_clk;
 200        }
 201
 202        error = request_irq(keypad->irq, w90p910_keypad_irq_handler,
 203                            0, pdev->name, keypad);
 204        if (error) {
 205                dev_err(&pdev->dev, "failed to request IRQ\n");
 206                goto failed_put_clk;
 207        }
 208
 209        __set_bit(EV_REP, input_dev->evbit);
 210        input_set_capability(input_dev, EV_MSC, MSC_SCAN);
 211        input_set_drvdata(input_dev, keypad);
 212
 213        /* Register the input device */
 214        error = input_register_device(input_dev);
 215        if (error) {
 216                dev_err(&pdev->dev, "failed to register input device\n");
 217                goto failed_free_irq;
 218        }
 219
 220        platform_set_drvdata(pdev, keypad);
 221        return 0;
 222
 223failed_free_irq:
 224        free_irq(irq, pdev);
 225failed_put_clk:
 226        clk_put(keypad->clk);
 227failed_free_io:
 228        iounmap(keypad->mmio_base);
 229failed_free_res:
 230        release_mem_region(res->start, resource_size(res));
 231failed_free:
 232        input_free_device(input_dev);
 233        kfree(keypad);
 234        return error;
 235}
 236
 237static int __devexit w90p910_keypad_remove(struct platform_device *pdev)
 238{
 239        struct w90p910_keypad *keypad = platform_get_drvdata(pdev);
 240        struct resource *res;
 241
 242        free_irq(keypad->irq, pdev);
 243
 244        clk_put(keypad->clk);
 245
 246        input_unregister_device(keypad->input_dev);
 247
 248        iounmap(keypad->mmio_base);
 249        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 250        release_mem_region(res->start, resource_size(res));
 251
 252        platform_set_drvdata(pdev, NULL);
 253        kfree(keypad);
 254
 255        return 0;
 256}
 257
 258static struct platform_driver w90p910_keypad_driver = {
 259        .probe          = w90p910_keypad_probe,
 260        .remove         = __devexit_p(w90p910_keypad_remove),
 261        .driver         = {
 262                .name   = "nuc900-kpi",
 263                .owner  = THIS_MODULE,
 264        },
 265};
 266module_platform_driver(w90p910_keypad_driver);
 267
 268MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
 269MODULE_DESCRIPTION("w90p910 keypad driver");
 270MODULE_LICENSE("GPL");
 271MODULE_ALIAS("platform:nuc900-keypad");
 272