linux/drivers/input/keyboard/omap4-keypad.c
<<
>>
Prefs
   1/*
   2 * OMAP4 Keypad Driver
   3 *
   4 * Copyright (C) 2010 Texas Instruments
   5 *
   6 * Author: Abraham Arce <x0066660@ti.com>
   7 * Initial Code: Syed Rafiuddin <rafiuddin.syed@ti.com>
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; either version 2 of the License, or
  12 * (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 of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 * GNU General Public License for more details.
  18 *
  19 * You should have received a copy of the GNU General Public License
  20 * along with this program; if not, write to the Free Software
  21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22 */
  23
  24#include <linux/module.h>
  25#include <linux/init.h>
  26#include <linux/interrupt.h>
  27#include <linux/platform_device.h>
  28#include <linux/errno.h>
  29#include <linux/io.h>
  30#include <linux/of.h>
  31#include <linux/input.h>
  32#include <linux/slab.h>
  33#include <linux/pm_runtime.h>
  34
  35#include <linux/platform_data/omap4-keypad.h>
  36
  37/* OMAP4 registers */
  38#define OMAP4_KBD_REVISION              0x00
  39#define OMAP4_KBD_SYSCONFIG             0x10
  40#define OMAP4_KBD_SYSSTATUS             0x14
  41#define OMAP4_KBD_IRQSTATUS             0x18
  42#define OMAP4_KBD_IRQENABLE             0x1C
  43#define OMAP4_KBD_WAKEUPENABLE          0x20
  44#define OMAP4_KBD_PENDING               0x24
  45#define OMAP4_KBD_CTRL                  0x28
  46#define OMAP4_KBD_DEBOUNCINGTIME        0x2C
  47#define OMAP4_KBD_LONGKEYTIME           0x30
  48#define OMAP4_KBD_TIMEOUT               0x34
  49#define OMAP4_KBD_STATEMACHINE          0x38
  50#define OMAP4_KBD_ROWINPUTS             0x3C
  51#define OMAP4_KBD_COLUMNOUTPUTS         0x40
  52#define OMAP4_KBD_FULLCODE31_0          0x44
  53#define OMAP4_KBD_FULLCODE63_32         0x48
  54
  55/* OMAP4 bit definitions */
  56#define OMAP4_DEF_IRQENABLE_EVENTEN     BIT(0)
  57#define OMAP4_DEF_IRQENABLE_LONGKEY     BIT(1)
  58#define OMAP4_DEF_WUP_EVENT_ENA         BIT(0)
  59#define OMAP4_DEF_WUP_LONG_KEY_ENA      BIT(1)
  60#define OMAP4_DEF_CTRL_NOSOFTMODE       BIT(1)
  61#define OMAP4_DEF_CTRL_PTV_SHIFT        2
  62
  63/* OMAP4 values */
  64#define OMAP4_VAL_IRQDISABLE            0x0
  65#define OMAP4_VAL_DEBOUNCINGTIME        0x7
  66#define OMAP4_VAL_PVT                   0x7
  67
  68enum {
  69        KBD_REVISION_OMAP4 = 0,
  70        KBD_REVISION_OMAP5,
  71};
  72
  73struct omap4_keypad {
  74        struct input_dev *input;
  75
  76        void __iomem *base;
  77        bool irq_wake_enabled;
  78        unsigned int irq;
  79
  80        unsigned int rows;
  81        unsigned int cols;
  82        u32 reg_offset;
  83        u32 irqreg_offset;
  84        unsigned int row_shift;
  85        bool no_autorepeat;
  86        unsigned char key_state[8];
  87        unsigned short *keymap;
  88};
  89
  90static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset)
  91{
  92        return __raw_readl(keypad_data->base +
  93                                keypad_data->reg_offset + offset);
  94}
  95
  96static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value)
  97{
  98        __raw_writel(value,
  99                     keypad_data->base + keypad_data->reg_offset + offset);
 100}
 101
 102static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset)
 103{
 104        return __raw_readl(keypad_data->base +
 105                                keypad_data->irqreg_offset + offset);
 106}
 107
 108static void kbd_write_irqreg(struct omap4_keypad *keypad_data,
 109                             u32 offset, u32 value)
 110{
 111        __raw_writel(value,
 112                     keypad_data->base + keypad_data->irqreg_offset + offset);
 113}
 114
 115
 116/* Interrupt handlers */
 117static irqreturn_t omap4_keypad_irq_handler(int irq, void *dev_id)
 118{
 119        struct omap4_keypad *keypad_data = dev_id;
 120
 121        if (kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)) {
 122                /* Disable interrupts */
 123                kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
 124                                 OMAP4_VAL_IRQDISABLE);
 125                return IRQ_WAKE_THREAD;
 126        }
 127
 128        return IRQ_NONE;
 129}
 130
 131static irqreturn_t omap4_keypad_irq_thread_fn(int irq, void *dev_id)
 132{
 133        struct omap4_keypad *keypad_data = dev_id;
 134        struct input_dev *input_dev = keypad_data->input;
 135        unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)];
 136        unsigned int col, row, code, changed;
 137        u32 *new_state = (u32 *) key_state;
 138
 139        *new_state = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0);
 140        *(new_state + 1) = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32);
 141
 142        for (row = 0; row < keypad_data->rows; row++) {
 143                changed = key_state[row] ^ keypad_data->key_state[row];
 144                if (!changed)
 145                        continue;
 146
 147                for (col = 0; col < keypad_data->cols; col++) {
 148                        if (changed & (1 << col)) {
 149                                code = MATRIX_SCAN_CODE(row, col,
 150                                                keypad_data->row_shift);
 151                                input_event(input_dev, EV_MSC, MSC_SCAN, code);
 152                                input_report_key(input_dev,
 153                                                 keypad_data->keymap[code],
 154                                                 key_state[row] & (1 << col));
 155                        }
 156                }
 157        }
 158
 159        input_sync(input_dev);
 160
 161        memcpy(keypad_data->key_state, key_state,
 162                sizeof(keypad_data->key_state));
 163
 164        /* clear pending interrupts */
 165        kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
 166                         kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
 167
 168        /* enable interrupts */
 169        kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
 170                OMAP4_DEF_IRQENABLE_EVENTEN |
 171                                OMAP4_DEF_IRQENABLE_LONGKEY);
 172
 173        return IRQ_HANDLED;
 174}
 175
 176static int omap4_keypad_open(struct input_dev *input)
 177{
 178        struct omap4_keypad *keypad_data = input_get_drvdata(input);
 179
 180        pm_runtime_get_sync(input->dev.parent);
 181
 182        disable_irq(keypad_data->irq);
 183
 184        kbd_writel(keypad_data, OMAP4_KBD_CTRL,
 185                        OMAP4_DEF_CTRL_NOSOFTMODE |
 186                        (OMAP4_VAL_PVT << OMAP4_DEF_CTRL_PTV_SHIFT));
 187        kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME,
 188                        OMAP4_VAL_DEBOUNCINGTIME);
 189        /* clear pending interrupts */
 190        kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
 191                         kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
 192        kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
 193                        OMAP4_DEF_IRQENABLE_EVENTEN |
 194                                OMAP4_DEF_IRQENABLE_LONGKEY);
 195        kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE,
 196                        OMAP4_DEF_WUP_EVENT_ENA | OMAP4_DEF_WUP_LONG_KEY_ENA);
 197
 198        enable_irq(keypad_data->irq);
 199
 200        return 0;
 201}
 202
 203static void omap4_keypad_close(struct input_dev *input)
 204{
 205        struct omap4_keypad *keypad_data = input_get_drvdata(input);
 206
 207        disable_irq(keypad_data->irq);
 208
 209        /* Disable interrupts */
 210        kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
 211                         OMAP4_VAL_IRQDISABLE);
 212
 213        /* clear pending interrupts */
 214        kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
 215                         kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
 216
 217        enable_irq(keypad_data->irq);
 218
 219        pm_runtime_put_sync(input->dev.parent);
 220}
 221
 222#ifdef CONFIG_OF
 223static int omap4_keypad_parse_dt(struct device *dev,
 224                                 struct omap4_keypad *keypad_data)
 225{
 226        struct device_node *np = dev->of_node;
 227        int err;
 228
 229        err = matrix_keypad_parse_of_params(dev, &keypad_data->rows,
 230                                            &keypad_data->cols);
 231        if (err)
 232                return err;
 233
 234        if (of_get_property(np, "linux,input-no-autorepeat", NULL))
 235                keypad_data->no_autorepeat = true;
 236
 237        return 0;
 238}
 239#else
 240static inline int omap4_keypad_parse_dt(struct device *dev,
 241                                        struct omap4_keypad *keypad_data)
 242{
 243        return -ENOSYS;
 244}
 245#endif
 246
 247static int omap4_keypad_probe(struct platform_device *pdev)
 248{
 249        const struct omap4_keypad_platform_data *pdata =
 250                                dev_get_platdata(&pdev->dev);
 251        const struct matrix_keymap_data *keymap_data =
 252                                pdata ? pdata->keymap_data : NULL;
 253        struct omap4_keypad *keypad_data;
 254        struct input_dev *input_dev;
 255        struct resource *res;
 256        unsigned int max_keys;
 257        int rev;
 258        int irq;
 259        int error;
 260
 261        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 262        if (!res) {
 263                dev_err(&pdev->dev, "no base address specified\n");
 264                return -EINVAL;
 265        }
 266
 267        irq = platform_get_irq(pdev, 0);
 268        if (!irq) {
 269                dev_err(&pdev->dev, "no keyboard irq assigned\n");
 270                return -EINVAL;
 271        }
 272
 273        keypad_data = kzalloc(sizeof(struct omap4_keypad), GFP_KERNEL);
 274        if (!keypad_data) {
 275                dev_err(&pdev->dev, "keypad_data memory allocation failed\n");
 276                return -ENOMEM;
 277        }
 278
 279        keypad_data->irq = irq;
 280
 281        if (pdata) {
 282                keypad_data->rows = pdata->rows;
 283                keypad_data->cols = pdata->cols;
 284        } else {
 285                error = omap4_keypad_parse_dt(&pdev->dev, keypad_data);
 286                if (error)
 287                        return error;
 288        }
 289
 290        res = request_mem_region(res->start, resource_size(res), pdev->name);
 291        if (!res) {
 292                dev_err(&pdev->dev, "can't request mem region\n");
 293                error = -EBUSY;
 294                goto err_free_keypad;
 295        }
 296
 297        keypad_data->base = ioremap(res->start, resource_size(res));
 298        if (!keypad_data->base) {
 299                dev_err(&pdev->dev, "can't ioremap mem resource\n");
 300                error = -ENOMEM;
 301                goto err_release_mem;
 302        }
 303
 304
 305        /*
 306         * Enable clocks for the keypad module so that we can read
 307         * revision register.
 308         */
 309        pm_runtime_enable(&pdev->dev);
 310        error = pm_runtime_get_sync(&pdev->dev);
 311        if (error) {
 312                dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
 313                goto err_unmap;
 314        }
 315        rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
 316        rev &= 0x03 << 30;
 317        rev >>= 30;
 318        switch (rev) {
 319        case KBD_REVISION_OMAP4:
 320                keypad_data->reg_offset = 0x00;
 321                keypad_data->irqreg_offset = 0x00;
 322                break;
 323        case KBD_REVISION_OMAP5:
 324                keypad_data->reg_offset = 0x10;
 325                keypad_data->irqreg_offset = 0x0c;
 326                break;
 327        default:
 328                dev_err(&pdev->dev,
 329                        "Keypad reports unsupported revision %d", rev);
 330                error = -EINVAL;
 331                goto err_pm_put_sync;
 332        }
 333
 334        /* input device allocation */
 335        keypad_data->input = input_dev = input_allocate_device();
 336        if (!input_dev) {
 337                error = -ENOMEM;
 338                goto err_pm_put_sync;
 339        }
 340
 341        input_dev->name = pdev->name;
 342        input_dev->dev.parent = &pdev->dev;
 343        input_dev->id.bustype = BUS_HOST;
 344        input_dev->id.vendor = 0x0001;
 345        input_dev->id.product = 0x0001;
 346        input_dev->id.version = 0x0001;
 347
 348        input_dev->open = omap4_keypad_open;
 349        input_dev->close = omap4_keypad_close;
 350
 351        input_set_capability(input_dev, EV_MSC, MSC_SCAN);
 352        if (!keypad_data->no_autorepeat)
 353                __set_bit(EV_REP, input_dev->evbit);
 354
 355        input_set_drvdata(input_dev, keypad_data);
 356
 357        keypad_data->row_shift = get_count_order(keypad_data->cols);
 358        max_keys = keypad_data->rows << keypad_data->row_shift;
 359        keypad_data->keymap = kzalloc(max_keys * sizeof(keypad_data->keymap[0]),
 360                                      GFP_KERNEL);
 361        if (!keypad_data->keymap) {
 362                dev_err(&pdev->dev, "Not enough memory for keymap\n");
 363                error = -ENOMEM;
 364                goto err_free_input;
 365        }
 366
 367        error = matrix_keypad_build_keymap(keymap_data, NULL,
 368                                           keypad_data->rows, keypad_data->cols,
 369                                           keypad_data->keymap, input_dev);
 370        if (error) {
 371                dev_err(&pdev->dev, "failed to build keymap\n");
 372                goto err_free_keymap;
 373        }
 374
 375        error = request_threaded_irq(keypad_data->irq, omap4_keypad_irq_handler,
 376                                     omap4_keypad_irq_thread_fn, 0,
 377                                     "omap4-keypad", keypad_data);
 378        if (error) {
 379                dev_err(&pdev->dev, "failed to register interrupt\n");
 380                goto err_free_input;
 381        }
 382
 383        device_init_wakeup(&pdev->dev, true);
 384        pm_runtime_put_sync(&pdev->dev);
 385
 386        error = input_register_device(keypad_data->input);
 387        if (error < 0) {
 388                dev_err(&pdev->dev, "failed to register input device\n");
 389                goto err_pm_disable;
 390        }
 391
 392        platform_set_drvdata(pdev, keypad_data);
 393        return 0;
 394
 395err_pm_disable:
 396        pm_runtime_disable(&pdev->dev);
 397        device_init_wakeup(&pdev->dev, false);
 398        free_irq(keypad_data->irq, keypad_data);
 399err_free_keymap:
 400        kfree(keypad_data->keymap);
 401err_free_input:
 402        input_free_device(input_dev);
 403err_pm_put_sync:
 404        pm_runtime_put_sync(&pdev->dev);
 405err_unmap:
 406        iounmap(keypad_data->base);
 407err_release_mem:
 408        release_mem_region(res->start, resource_size(res));
 409err_free_keypad:
 410        kfree(keypad_data);
 411        return error;
 412}
 413
 414static int omap4_keypad_remove(struct platform_device *pdev)
 415{
 416        struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
 417        struct resource *res;
 418
 419        free_irq(keypad_data->irq, keypad_data);
 420
 421        pm_runtime_disable(&pdev->dev);
 422
 423        device_init_wakeup(&pdev->dev, false);
 424
 425        input_unregister_device(keypad_data->input);
 426
 427        iounmap(keypad_data->base);
 428
 429        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 430        release_mem_region(res->start, resource_size(res));
 431
 432        kfree(keypad_data->keymap);
 433        kfree(keypad_data);
 434
 435        return 0;
 436}
 437
 438#ifdef CONFIG_OF
 439static const struct of_device_id omap_keypad_dt_match[] = {
 440        { .compatible = "ti,omap4-keypad" },
 441        {},
 442};
 443MODULE_DEVICE_TABLE(of, omap_keypad_dt_match);
 444#endif
 445
 446#ifdef CONFIG_PM_SLEEP
 447static int omap4_keypad_suspend(struct device *dev)
 448{
 449        struct platform_device *pdev = to_platform_device(dev);
 450        struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
 451        int error;
 452
 453        if (device_may_wakeup(&pdev->dev)) {
 454                error = enable_irq_wake(keypad_data->irq);
 455                if (!error)
 456                        keypad_data->irq_wake_enabled = true;
 457        }
 458
 459        return 0;
 460}
 461
 462static int omap4_keypad_resume(struct device *dev)
 463{
 464        struct platform_device *pdev = to_platform_device(dev);
 465        struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
 466
 467        if (device_may_wakeup(&pdev->dev) && keypad_data->irq_wake_enabled) {
 468                disable_irq_wake(keypad_data->irq);
 469                keypad_data->irq_wake_enabled = false;
 470        }
 471
 472        return 0;
 473}
 474#endif
 475
 476static SIMPLE_DEV_PM_OPS(omap4_keypad_pm_ops,
 477                         omap4_keypad_suspend, omap4_keypad_resume);
 478
 479static struct platform_driver omap4_keypad_driver = {
 480        .probe          = omap4_keypad_probe,
 481        .remove         = omap4_keypad_remove,
 482        .driver         = {
 483                .name   = "omap4-keypad",
 484                .owner  = THIS_MODULE,
 485                .pm     = &omap4_keypad_pm_ops,
 486                .of_match_table = of_match_ptr(omap_keypad_dt_match),
 487        },
 488};
 489module_platform_driver(omap4_keypad_driver);
 490
 491MODULE_AUTHOR("Texas Instruments");
 492MODULE_DESCRIPTION("OMAP4 Keypad Driver");
 493MODULE_LICENSE("GPL");
 494MODULE_ALIAS("platform:omap4-keypad");
 495