linux/drivers/input/keyboard/nspire-keypad.c
<<
>>
Prefs
   1/*
   2 *  Copyright (C) 2013 Daniel Tang <tangrs@tangrs.id.au>
   3 *
   4 * This program is free software; you can redistribute it and/or modify
   5 * it under the terms of the GNU General Public License version 2, as
   6 * published by the Free Software Foundation.
   7 */
   8
   9#include <linux/input/matrix_keypad.h>
  10#include <linux/platform_device.h>
  11#include <linux/interrupt.h>
  12#include <linux/io.h>
  13#include <linux/delay.h>
  14#include <linux/input.h>
  15#include <linux/slab.h>
  16#include <linux/clk.h>
  17#include <linux/module.h>
  18#include <linux/of.h>
  19
  20#define KEYPAD_SCAN_MODE        0x00
  21#define KEYPAD_CNTL             0x04
  22#define KEYPAD_INT              0x08
  23#define KEYPAD_INTMSK           0x0C
  24
  25#define KEYPAD_DATA             0x10
  26#define KEYPAD_GPIO             0x30
  27
  28#define KEYPAD_UNKNOWN_INT      0x40
  29#define KEYPAD_UNKNOWN_INT_STS  0x44
  30
  31#define KEYPAD_BITMASK_COLS     11
  32#define KEYPAD_BITMASK_ROWS     8
  33
  34struct nspire_keypad {
  35        void __iomem *reg_base;
  36        u32 int_mask;
  37
  38        struct input_dev *input;
  39        struct clk *clk;
  40
  41        struct matrix_keymap_data *keymap;
  42        int row_shift;
  43
  44        /* Maximum delay estimated assuming 33MHz APB */
  45        u32 scan_interval;      /* In microseconds (~2000us max) */
  46        u32 row_delay;          /* In microseconds (~500us max) */
  47
  48        u16 state[KEYPAD_BITMASK_ROWS];
  49
  50        bool active_low;
  51};
  52
  53static irqreturn_t nspire_keypad_irq(int irq, void *dev_id)
  54{
  55        struct nspire_keypad *keypad = dev_id;
  56        struct input_dev *input = keypad->input;
  57        unsigned short *keymap = input->keycode;
  58        unsigned int code;
  59        int row, col;
  60        u32 int_sts;
  61        u16 state[8];
  62        u16 bits, changed;
  63
  64        int_sts = readl(keypad->reg_base + KEYPAD_INT) & keypad->int_mask;
  65        if (!int_sts)
  66                return IRQ_NONE;
  67
  68        memcpy_fromio(state, keypad->reg_base + KEYPAD_DATA, sizeof(state));
  69
  70        for (row = 0; row < KEYPAD_BITMASK_ROWS; row++) {
  71                bits = state[row];
  72                if (keypad->active_low)
  73                        bits = ~bits;
  74
  75                changed = bits ^ keypad->state[row];
  76                if (!changed)
  77                        continue;
  78
  79                keypad->state[row] = bits;
  80
  81                for (col = 0; col < KEYPAD_BITMASK_COLS; col++) {
  82                        if (!(changed & (1U << col)))
  83                                continue;
  84
  85                        code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
  86                        input_event(input, EV_MSC, MSC_SCAN, code);
  87                        input_report_key(input, keymap[code],
  88                                         bits & (1U << col));
  89                }
  90        }
  91
  92        input_sync(input);
  93
  94        writel(0x3, keypad->reg_base + KEYPAD_INT);
  95
  96        return IRQ_HANDLED;
  97}
  98
  99static int nspire_keypad_chip_init(struct nspire_keypad *keypad)
 100{
 101        unsigned long val = 0, cycles_per_us, delay_cycles, row_delay_cycles;
 102
 103        cycles_per_us = (clk_get_rate(keypad->clk) / 1000000);
 104        if (cycles_per_us == 0)
 105                cycles_per_us = 1;
 106
 107        delay_cycles = cycles_per_us * keypad->scan_interval;
 108        WARN_ON(delay_cycles >= (1 << 16)); /* Overflow */
 109        delay_cycles &= 0xffff;
 110
 111        row_delay_cycles = cycles_per_us * keypad->row_delay;
 112        WARN_ON(row_delay_cycles >= (1 << 14)); /* Overflow */
 113        row_delay_cycles &= 0x3fff;
 114
 115        val |= 3 << 0; /* Set scan mode to 3 (continuous scan) */
 116        val |= row_delay_cycles << 2; /* Delay between scanning each row */
 117        val |= delay_cycles << 16; /* Delay between scans */
 118        writel(val, keypad->reg_base + KEYPAD_SCAN_MODE);
 119
 120        val = (KEYPAD_BITMASK_ROWS & 0xff) | (KEYPAD_BITMASK_COLS & 0xff)<<8;
 121        writel(val, keypad->reg_base + KEYPAD_CNTL);
 122
 123        /* Enable interrupts */
 124        keypad->int_mask = 1 << 1;
 125        writel(keypad->int_mask, keypad->reg_base + KEYPAD_INTMSK);
 126
 127        /* Disable GPIO interrupts to prevent hanging on touchpad */
 128        /* Possibly used to detect touchpad events */
 129        writel(0, keypad->reg_base + KEYPAD_UNKNOWN_INT);
 130        /* Acknowledge existing interrupts */
 131        writel(~0, keypad->reg_base + KEYPAD_UNKNOWN_INT_STS);
 132
 133        return 0;
 134}
 135
 136static int nspire_keypad_open(struct input_dev *input)
 137{
 138        struct nspire_keypad *keypad = input_get_drvdata(input);
 139        int error;
 140
 141        error = clk_prepare_enable(keypad->clk);
 142        if (error)
 143                return error;
 144
 145        error = nspire_keypad_chip_init(keypad);
 146        if (error) {
 147                clk_disable_unprepare(keypad->clk);
 148                return error;
 149        }
 150
 151        return 0;
 152}
 153
 154static void nspire_keypad_close(struct input_dev *input)
 155{
 156        struct nspire_keypad *keypad = input_get_drvdata(input);
 157
 158        clk_disable_unprepare(keypad->clk);
 159}
 160
 161static int nspire_keypad_probe(struct platform_device *pdev)
 162{
 163        const struct device_node *of_node = pdev->dev.of_node;
 164        struct nspire_keypad *keypad;
 165        struct input_dev *input;
 166        struct resource *res;
 167        int irq;
 168        int error;
 169
 170        irq = platform_get_irq(pdev, 0);
 171        if (irq < 0) {
 172                dev_err(&pdev->dev, "failed to get keypad irq\n");
 173                return -EINVAL;
 174        }
 175
 176        keypad = devm_kzalloc(&pdev->dev, sizeof(struct nspire_keypad),
 177                              GFP_KERNEL);
 178        if (!keypad) {
 179                dev_err(&pdev->dev, "failed to allocate keypad memory\n");
 180                return -ENOMEM;
 181        }
 182
 183        keypad->row_shift = get_count_order(KEYPAD_BITMASK_COLS);
 184
 185        error = of_property_read_u32(of_node, "scan-interval",
 186                                     &keypad->scan_interval);
 187        if (error) {
 188                dev_err(&pdev->dev, "failed to get scan-interval\n");
 189                return error;
 190        }
 191
 192        error = of_property_read_u32(of_node, "row-delay",
 193                                     &keypad->row_delay);
 194        if (error) {
 195                dev_err(&pdev->dev, "failed to get row-delay\n");
 196                return error;
 197        }
 198
 199        keypad->active_low = of_property_read_bool(of_node, "active-low");
 200
 201        keypad->clk = devm_clk_get(&pdev->dev, NULL);
 202        if (IS_ERR(keypad->clk)) {
 203                dev_err(&pdev->dev, "unable to get clock\n");
 204                return PTR_ERR(keypad->clk);
 205        }
 206
 207        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 208        keypad->reg_base = devm_ioremap_resource(&pdev->dev, res);
 209        if (IS_ERR(keypad->reg_base))
 210                return PTR_ERR(keypad->reg_base);
 211
 212        keypad->input = input = devm_input_allocate_device(&pdev->dev);
 213        if (!input) {
 214                dev_err(&pdev->dev, "failed to allocate input device\n");
 215                return -ENOMEM;
 216        }
 217
 218        input_set_drvdata(input, keypad);
 219
 220        input->id.bustype = BUS_HOST;
 221        input->name = "nspire-keypad";
 222        input->open = nspire_keypad_open;
 223        input->close = nspire_keypad_close;
 224
 225        __set_bit(EV_KEY, input->evbit);
 226        __set_bit(EV_REP, input->evbit);
 227        input_set_capability(input, EV_MSC, MSC_SCAN);
 228
 229        error = matrix_keypad_build_keymap(NULL, NULL,
 230                                           KEYPAD_BITMASK_ROWS,
 231                                           KEYPAD_BITMASK_COLS,
 232                                           NULL, input);
 233        if (error) {
 234                dev_err(&pdev->dev, "building keymap failed\n");
 235                return error;
 236        }
 237
 238        error = devm_request_irq(&pdev->dev, irq, nspire_keypad_irq, 0,
 239                                 "nspire_keypad", keypad);
 240        if (error) {
 241                dev_err(&pdev->dev, "allocate irq %d failed\n", irq);
 242                return error;
 243        }
 244
 245        error = input_register_device(input);
 246        if (error) {
 247                dev_err(&pdev->dev,
 248                        "unable to register input device: %d\n", error);
 249                return error;
 250        }
 251
 252        dev_dbg(&pdev->dev,
 253                "TI-NSPIRE keypad at %pR (scan_interval=%uus, row_delay=%uus%s)\n",
 254                res, keypad->row_delay, keypad->scan_interval,
 255                keypad->active_low ? ", active_low" : "");
 256
 257        return 0;
 258}
 259
 260static const struct of_device_id nspire_keypad_dt_match[] = {
 261        { .compatible = "ti,nspire-keypad" },
 262        { },
 263};
 264MODULE_DEVICE_TABLE(of, nspire_keypad_dt_match);
 265
 266static struct platform_driver nspire_keypad_driver = {
 267        .driver = {
 268                .name = "nspire-keypad",
 269                .of_match_table = nspire_keypad_dt_match,
 270        },
 271        .probe = nspire_keypad_probe,
 272};
 273
 274module_platform_driver(nspire_keypad_driver);
 275
 276MODULE_LICENSE("GPL");
 277MODULE_DESCRIPTION("TI-NSPIRE Keypad Driver");
 278