linux/drivers/input/keyboard/max7359_keypad.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * max7359_keypad.c - MAX7359 Key Switch Controller Driver
   4 *
   5 * Copyright (C) 2009 Samsung Electronics
   6 * Kim Kyuwon <q1.kim@samsung.com>
   7 *
   8 * Based on pxa27x_keypad.c
   9 *
  10 * Datasheet: http://www.maxim-ic.com/quick_view2.cfm/qv_pk/5456
  11 */
  12
  13#include <linux/module.h>
  14#include <linux/i2c.h>
  15#include <linux/slab.h>
  16#include <linux/interrupt.h>
  17#include <linux/pm.h>
  18#include <linux/input.h>
  19#include <linux/input/matrix_keypad.h>
  20
  21#define MAX7359_MAX_KEY_ROWS    8
  22#define MAX7359_MAX_KEY_COLS    8
  23#define MAX7359_MAX_KEY_NUM     (MAX7359_MAX_KEY_ROWS * MAX7359_MAX_KEY_COLS)
  24#define MAX7359_ROW_SHIFT       3
  25
  26/*
  27 * MAX7359 registers
  28 */
  29#define MAX7359_REG_KEYFIFO     0x00
  30#define MAX7359_REG_CONFIG      0x01
  31#define MAX7359_REG_DEBOUNCE    0x02
  32#define MAX7359_REG_INTERRUPT   0x03
  33#define MAX7359_REG_PORTS       0x04
  34#define MAX7359_REG_KEYREP      0x05
  35#define MAX7359_REG_SLEEP       0x06
  36
  37/*
  38 * Configuration register bits
  39 */
  40#define MAX7359_CFG_SLEEP       (1 << 7)
  41#define MAX7359_CFG_INTERRUPT   (1 << 5)
  42#define MAX7359_CFG_KEY_RELEASE (1 << 3)
  43#define MAX7359_CFG_WAKEUP      (1 << 1)
  44#define MAX7359_CFG_TIMEOUT     (1 << 0)
  45
  46/*
  47 * Autosleep register values (ms)
  48 */
  49#define MAX7359_AUTOSLEEP_8192  0x01
  50#define MAX7359_AUTOSLEEP_4096  0x02
  51#define MAX7359_AUTOSLEEP_2048  0x03
  52#define MAX7359_AUTOSLEEP_1024  0x04
  53#define MAX7359_AUTOSLEEP_512   0x05
  54#define MAX7359_AUTOSLEEP_256   0x06
  55
  56struct max7359_keypad {
  57        /* matrix key code map */
  58        unsigned short keycodes[MAX7359_MAX_KEY_NUM];
  59
  60        struct input_dev *input_dev;
  61        struct i2c_client *client;
  62};
  63
  64static int max7359_write_reg(struct i2c_client *client, u8 reg, u8 val)
  65{
  66        int ret = i2c_smbus_write_byte_data(client, reg, val);
  67
  68        if (ret < 0)
  69                dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
  70                        __func__, reg, val, ret);
  71        return ret;
  72}
  73
  74static int max7359_read_reg(struct i2c_client *client, int reg)
  75{
  76        int ret = i2c_smbus_read_byte_data(client, reg);
  77
  78        if (ret < 0)
  79                dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
  80                        __func__, reg, ret);
  81        return ret;
  82}
  83
  84/* runs in an IRQ thread -- can (and will!) sleep */
  85static irqreturn_t max7359_interrupt(int irq, void *dev_id)
  86{
  87        struct max7359_keypad *keypad = dev_id;
  88        struct input_dev *input_dev = keypad->input_dev;
  89        int val, row, col, release, code;
  90
  91        val = max7359_read_reg(keypad->client, MAX7359_REG_KEYFIFO);
  92        row = val & 0x7;
  93        col = (val >> 3) & 0x7;
  94        release = val & 0x40;
  95
  96        code = MATRIX_SCAN_CODE(row, col, MAX7359_ROW_SHIFT);
  97
  98        dev_dbg(&keypad->client->dev,
  99                "key[%d:%d] %s\n", row, col, release ? "release" : "press");
 100
 101        input_event(input_dev, EV_MSC, MSC_SCAN, code);
 102        input_report_key(input_dev, keypad->keycodes[code], !release);
 103        input_sync(input_dev);
 104
 105        return IRQ_HANDLED;
 106}
 107
 108/*
 109 * Let MAX7359 fall into a deep sleep:
 110 * If no keys are pressed, enter sleep mode for 8192 ms. And if any
 111 * key is pressed, the MAX7359 returns to normal operating mode.
 112 */
 113static inline void max7359_fall_deepsleep(struct i2c_client *client)
 114{
 115        max7359_write_reg(client, MAX7359_REG_SLEEP, MAX7359_AUTOSLEEP_8192);
 116}
 117
 118/*
 119 * Let MAX7359 take a catnap:
 120 * Autosleep just for 256 ms.
 121 */
 122static inline void max7359_take_catnap(struct i2c_client *client)
 123{
 124        max7359_write_reg(client, MAX7359_REG_SLEEP, MAX7359_AUTOSLEEP_256);
 125}
 126
 127static int max7359_open(struct input_dev *dev)
 128{
 129        struct max7359_keypad *keypad = input_get_drvdata(dev);
 130
 131        max7359_take_catnap(keypad->client);
 132
 133        return 0;
 134}
 135
 136static void max7359_close(struct input_dev *dev)
 137{
 138        struct max7359_keypad *keypad = input_get_drvdata(dev);
 139
 140        max7359_fall_deepsleep(keypad->client);
 141}
 142
 143static void max7359_initialize(struct i2c_client *client)
 144{
 145        max7359_write_reg(client, MAX7359_REG_CONFIG,
 146                MAX7359_CFG_KEY_RELEASE | /* Key release enable */
 147                MAX7359_CFG_WAKEUP); /* Key press wakeup enable */
 148
 149        /* Full key-scan functionality */
 150        max7359_write_reg(client, MAX7359_REG_DEBOUNCE, 0x1F);
 151
 152        /* nINT asserts every debounce cycles */
 153        max7359_write_reg(client, MAX7359_REG_INTERRUPT, 0x01);
 154
 155        max7359_fall_deepsleep(client);
 156}
 157
 158static int max7359_probe(struct i2c_client *client,
 159                                        const struct i2c_device_id *id)
 160{
 161        const struct matrix_keymap_data *keymap_data =
 162                        dev_get_platdata(&client->dev);
 163        struct max7359_keypad *keypad;
 164        struct input_dev *input_dev;
 165        int ret;
 166        int error;
 167
 168        if (!client->irq) {
 169                dev_err(&client->dev, "The irq number should not be zero\n");
 170                return -EINVAL;
 171        }
 172
 173        /* Detect MAX7359: The initial Keys FIFO value is '0x3F' */
 174        ret = max7359_read_reg(client, MAX7359_REG_KEYFIFO);
 175        if (ret < 0) {
 176                dev_err(&client->dev, "failed to detect device\n");
 177                return -ENODEV;
 178        }
 179
 180        dev_dbg(&client->dev, "keys FIFO is 0x%02x\n", ret);
 181
 182        keypad = devm_kzalloc(&client->dev, sizeof(struct max7359_keypad),
 183                              GFP_KERNEL);
 184        if (!keypad) {
 185                dev_err(&client->dev, "failed to allocate memory\n");
 186                return -ENOMEM;
 187        }
 188
 189        input_dev = devm_input_allocate_device(&client->dev);
 190        if (!input_dev) {
 191                dev_err(&client->dev, "failed to allocate input device\n");
 192                return -ENOMEM;
 193        }
 194
 195        keypad->client = client;
 196        keypad->input_dev = input_dev;
 197
 198        input_dev->name = client->name;
 199        input_dev->id.bustype = BUS_I2C;
 200        input_dev->open = max7359_open;
 201        input_dev->close = max7359_close;
 202        input_dev->dev.parent = &client->dev;
 203
 204        input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
 205        input_dev->keycodesize = sizeof(keypad->keycodes[0]);
 206        input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
 207        input_dev->keycode = keypad->keycodes;
 208
 209        input_set_capability(input_dev, EV_MSC, MSC_SCAN);
 210        input_set_drvdata(input_dev, keypad);
 211
 212        error = matrix_keypad_build_keymap(keymap_data, NULL,
 213                                           MAX7359_MAX_KEY_ROWS,
 214                                           MAX7359_MAX_KEY_COLS,
 215                                           keypad->keycodes,
 216                                           input_dev);
 217        if (error) {
 218                dev_err(&client->dev, "failed to build keymap\n");
 219                return error;
 220        }
 221
 222        error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
 223                                          max7359_interrupt,
 224                                          IRQF_TRIGGER_LOW | IRQF_ONESHOT,
 225                                          client->name, keypad);
 226        if (error) {
 227                dev_err(&client->dev, "failed to register interrupt\n");
 228                return error;
 229        }
 230
 231        /* Register the input device */
 232        error = input_register_device(input_dev);
 233        if (error) {
 234                dev_err(&client->dev, "failed to register input device\n");
 235                return error;
 236        }
 237
 238        /* Initialize MAX7359 */
 239        max7359_initialize(client);
 240
 241        device_init_wakeup(&client->dev, 1);
 242
 243        return 0;
 244}
 245
 246#ifdef CONFIG_PM_SLEEP
 247static int max7359_suspend(struct device *dev)
 248{
 249        struct i2c_client *client = to_i2c_client(dev);
 250
 251        max7359_fall_deepsleep(client);
 252
 253        if (device_may_wakeup(&client->dev))
 254                enable_irq_wake(client->irq);
 255
 256        return 0;
 257}
 258
 259static int max7359_resume(struct device *dev)
 260{
 261        struct i2c_client *client = to_i2c_client(dev);
 262
 263        if (device_may_wakeup(&client->dev))
 264                disable_irq_wake(client->irq);
 265
 266        /* Restore the default setting */
 267        max7359_take_catnap(client);
 268
 269        return 0;
 270}
 271#endif
 272
 273static SIMPLE_DEV_PM_OPS(max7359_pm, max7359_suspend, max7359_resume);
 274
 275static const struct i2c_device_id max7359_ids[] = {
 276        { "max7359", 0 },
 277        { }
 278};
 279MODULE_DEVICE_TABLE(i2c, max7359_ids);
 280
 281static struct i2c_driver max7359_i2c_driver = {
 282        .driver = {
 283                .name = "max7359",
 284                .pm   = &max7359_pm,
 285        },
 286        .probe          = max7359_probe,
 287        .id_table       = max7359_ids,
 288};
 289
 290module_i2c_driver(max7359_i2c_driver);
 291
 292MODULE_AUTHOR("Kim Kyuwon <q1.kim@samsung.com>");
 293MODULE_DESCRIPTION("MAX7359 Key Switch Controller Driver");
 294MODULE_LICENSE("GPL v2");
 295