linux/drivers/input/touchscreen/wacom_i2c.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Wacom Penabled Driver for I2C
   4 *
   5 * Copyright (c) 2011 - 2013 Tatsunosuke Tobita, Wacom.
   6 * <tobita.tatsunosuke@wacom.co.jp>
   7 */
   8
   9#include <linux/module.h>
  10#include <linux/input.h>
  11#include <linux/i2c.h>
  12#include <linux/slab.h>
  13#include <linux/irq.h>
  14#include <linux/interrupt.h>
  15#include <asm/unaligned.h>
  16
  17#define WACOM_CMD_QUERY0        0x04
  18#define WACOM_CMD_QUERY1        0x00
  19#define WACOM_CMD_QUERY2        0x33
  20#define WACOM_CMD_QUERY3        0x02
  21#define WACOM_CMD_THROW0        0x05
  22#define WACOM_CMD_THROW1        0x00
  23#define WACOM_QUERY_SIZE        19
  24
  25struct wacom_features {
  26        int x_max;
  27        int y_max;
  28        int pressure_max;
  29        char fw_version;
  30};
  31
  32struct wacom_i2c {
  33        struct i2c_client *client;
  34        struct input_dev *input;
  35        u8 data[WACOM_QUERY_SIZE];
  36        bool prox;
  37        int tool;
  38};
  39
  40static int wacom_query_device(struct i2c_client *client,
  41                              struct wacom_features *features)
  42{
  43        int ret;
  44        u8 cmd1[] = { WACOM_CMD_QUERY0, WACOM_CMD_QUERY1,
  45                        WACOM_CMD_QUERY2, WACOM_CMD_QUERY3 };
  46        u8 cmd2[] = { WACOM_CMD_THROW0, WACOM_CMD_THROW1 };
  47        u8 data[WACOM_QUERY_SIZE];
  48        struct i2c_msg msgs[] = {
  49                {
  50                        .addr = client->addr,
  51                        .flags = 0,
  52                        .len = sizeof(cmd1),
  53                        .buf = cmd1,
  54                },
  55                {
  56                        .addr = client->addr,
  57                        .flags = 0,
  58                        .len = sizeof(cmd2),
  59                        .buf = cmd2,
  60                },
  61                {
  62                        .addr = client->addr,
  63                        .flags = I2C_M_RD,
  64                        .len = sizeof(data),
  65                        .buf = data,
  66                },
  67        };
  68
  69        ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  70        if (ret < 0)
  71                return ret;
  72        if (ret != ARRAY_SIZE(msgs))
  73                return -EIO;
  74
  75        features->x_max = get_unaligned_le16(&data[3]);
  76        features->y_max = get_unaligned_le16(&data[5]);
  77        features->pressure_max = get_unaligned_le16(&data[11]);
  78        features->fw_version = get_unaligned_le16(&data[13]);
  79
  80        dev_dbg(&client->dev,
  81                "x_max:%d, y_max:%d, pressure:%d, fw:%d\n",
  82                features->x_max, features->y_max,
  83                features->pressure_max, features->fw_version);
  84
  85        return 0;
  86}
  87
  88static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
  89{
  90        struct wacom_i2c *wac_i2c = dev_id;
  91        struct input_dev *input = wac_i2c->input;
  92        u8 *data = wac_i2c->data;
  93        unsigned int x, y, pressure;
  94        unsigned char tsw, f1, f2, ers;
  95        int error;
  96
  97        error = i2c_master_recv(wac_i2c->client,
  98                                wac_i2c->data, sizeof(wac_i2c->data));
  99        if (error < 0)
 100                goto out;
 101
 102        tsw = data[3] & 0x01;
 103        ers = data[3] & 0x04;
 104        f1 = data[3] & 0x02;
 105        f2 = data[3] & 0x10;
 106        x = le16_to_cpup((__le16 *)&data[4]);
 107        y = le16_to_cpup((__le16 *)&data[6]);
 108        pressure = le16_to_cpup((__le16 *)&data[8]);
 109
 110        if (!wac_i2c->prox)
 111                wac_i2c->tool = (data[3] & 0x0c) ?
 112                        BTN_TOOL_RUBBER : BTN_TOOL_PEN;
 113
 114        wac_i2c->prox = data[3] & 0x20;
 115
 116        input_report_key(input, BTN_TOUCH, tsw || ers);
 117        input_report_key(input, wac_i2c->tool, wac_i2c->prox);
 118        input_report_key(input, BTN_STYLUS, f1);
 119        input_report_key(input, BTN_STYLUS2, f2);
 120        input_report_abs(input, ABS_X, x);
 121        input_report_abs(input, ABS_Y, y);
 122        input_report_abs(input, ABS_PRESSURE, pressure);
 123        input_sync(input);
 124
 125out:
 126        return IRQ_HANDLED;
 127}
 128
 129static int wacom_i2c_open(struct input_dev *dev)
 130{
 131        struct wacom_i2c *wac_i2c = input_get_drvdata(dev);
 132        struct i2c_client *client = wac_i2c->client;
 133
 134        enable_irq(client->irq);
 135
 136        return 0;
 137}
 138
 139static void wacom_i2c_close(struct input_dev *dev)
 140{
 141        struct wacom_i2c *wac_i2c = input_get_drvdata(dev);
 142        struct i2c_client *client = wac_i2c->client;
 143
 144        disable_irq(client->irq);
 145}
 146
 147static int wacom_i2c_probe(struct i2c_client *client,
 148                           const struct i2c_device_id *id)
 149{
 150        struct device *dev = &client->dev;
 151        struct wacom_i2c *wac_i2c;
 152        struct input_dev *input;
 153        struct wacom_features features = { 0 };
 154        int error;
 155
 156        if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
 157                dev_err(dev, "i2c_check_functionality error\n");
 158                return -EIO;
 159        }
 160
 161        error = wacom_query_device(client, &features);
 162        if (error)
 163                return error;
 164
 165        wac_i2c = devm_kzalloc(dev, sizeof(*wac_i2c), GFP_KERNEL);
 166        if (!wac_i2c)
 167                return -ENOMEM;
 168
 169        wac_i2c->client = client;
 170
 171        input = devm_input_allocate_device(dev);
 172        if (!input)
 173                return -ENOMEM;
 174
 175        wac_i2c->input = input;
 176
 177        input->name = "Wacom I2C Digitizer";
 178        input->id.bustype = BUS_I2C;
 179        input->id.vendor = 0x56a;
 180        input->id.version = features.fw_version;
 181        input->open = wacom_i2c_open;
 182        input->close = wacom_i2c_close;
 183
 184        input->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
 185
 186        __set_bit(BTN_TOOL_PEN, input->keybit);
 187        __set_bit(BTN_TOOL_RUBBER, input->keybit);
 188        __set_bit(BTN_STYLUS, input->keybit);
 189        __set_bit(BTN_STYLUS2, input->keybit);
 190        __set_bit(BTN_TOUCH, input->keybit);
 191
 192        input_set_abs_params(input, ABS_X, 0, features.x_max, 0, 0);
 193        input_set_abs_params(input, ABS_Y, 0, features.y_max, 0, 0);
 194        input_set_abs_params(input, ABS_PRESSURE,
 195                             0, features.pressure_max, 0, 0);
 196
 197        input_set_drvdata(input, wac_i2c);
 198
 199        error = devm_request_threaded_irq(dev, client->irq, NULL, wacom_i2c_irq,
 200                                          IRQF_ONESHOT, "wacom_i2c", wac_i2c);
 201        if (error) {
 202                dev_err(dev, "Failed to request IRQ: %d\n", error);
 203                return error;
 204        }
 205
 206        /* Disable the IRQ, we'll enable it in wac_i2c_open() */
 207        disable_irq(client->irq);
 208
 209        error = input_register_device(wac_i2c->input);
 210        if (error) {
 211                dev_err(dev, "Failed to register input device: %d\n", error);
 212                return error;
 213        }
 214
 215        return 0;
 216}
 217
 218static int __maybe_unused wacom_i2c_suspend(struct device *dev)
 219{
 220        struct i2c_client *client = to_i2c_client(dev);
 221
 222        disable_irq(client->irq);
 223
 224        return 0;
 225}
 226
 227static int __maybe_unused wacom_i2c_resume(struct device *dev)
 228{
 229        struct i2c_client *client = to_i2c_client(dev);
 230
 231        enable_irq(client->irq);
 232
 233        return 0;
 234}
 235
 236static SIMPLE_DEV_PM_OPS(wacom_i2c_pm, wacom_i2c_suspend, wacom_i2c_resume);
 237
 238static const struct i2c_device_id wacom_i2c_id[] = {
 239        { "WAC_I2C_EMR", 0 },
 240        { },
 241};
 242MODULE_DEVICE_TABLE(i2c, wacom_i2c_id);
 243
 244static struct i2c_driver wacom_i2c_driver = {
 245        .driver = {
 246                .name   = "wacom_i2c",
 247                .pm     = &wacom_i2c_pm,
 248        },
 249
 250        .probe          = wacom_i2c_probe,
 251        .id_table       = wacom_i2c_id,
 252};
 253module_i2c_driver(wacom_i2c_driver);
 254
 255MODULE_AUTHOR("Tatsunosuke Tobita <tobita.tatsunosuke@wacom.co.jp>");
 256MODULE_DESCRIPTION("WACOM EMR I2C Driver");
 257MODULE_LICENSE("GPL");
 258