From 5a966261c0dfb836f54444ff3893638802183cac Mon Sep 17 00:00:00 2001 From: Tatsunosuke Tobita Date: Sun, 25 Mar 2012 17:23:19 -0700 Subject: Input: add support for Wacom Stylus device with I2C interface This adds support for Wacom Stylus device with I2C interface. [Dan Carpenter : fix NULL-pointer dereference in error handling path.] Signed-off-by: Tatsunosuke Tobita Signed-off-by: Dmitry Torokhov --- drivers/input/touchscreen/wacom_i2c.c | 315 ++++++++++++++++++++++++++++++++++ 1 file changed, 315 insertions(+) create mode 100644 drivers/input/touchscreen/wacom_i2c.c (limited to 'drivers/input/touchscreen/wacom_i2c.c') diff --git a/drivers/input/touchscreen/wacom_i2c.c b/drivers/input/touchscreen/wacom_i2c.c new file mode 100644 index 000000000000..b8ca4a6bc91a --- /dev/null +++ b/drivers/input/touchscreen/wacom_i2c.c @@ -0,0 +1,315 @@ +/* + * Wacom Penabled Driver for I2C + * + * Copyright (c) 2011 Tatsunosuke Tobita, Wacom. + * + * + * This program is free software; you can redistribute it + * and/or modify it under the terms of the GNU General + * Public License as published by the Free Software + * Foundation; either version of 2 of the License, + * or (at your option) any later version. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define WACOM_CMD_QUERY0 0x04 +#define WACOM_CMD_QUERY1 0x00 +#define WACOM_CMD_QUERY2 0x33 +#define WACOM_CMD_QUERY3 0x02 +#define WACOM_CMD_THROW0 0x05 +#define WACOM_CMD_THROW1 0x00 +#define WACOM_QUERY_SIZE 19 +#define WACOM_RETRY_CNT 100 + +struct wacom_features { + int x_max; + int y_max; + int pressure_max; + char fw_version; +}; + +struct wacom_i2c { + struct i2c_client *client; + struct input_dev *input; + unsigned int gpio; + u8 data[WACOM_QUERY_SIZE]; +}; + +static int wacom_query_device(struct i2c_client *client, + struct wacom_features *features) +{ + int ret; + u8 cmd1[] = { WACOM_CMD_QUERY0, WACOM_CMD_QUERY1, + WACOM_CMD_QUERY2, WACOM_CMD_QUERY3 }; + u8 cmd2[] = { WACOM_CMD_THROW0, WACOM_CMD_THROW1 }; + u8 data[WACOM_QUERY_SIZE]; + struct i2c_msg msgs[] = { + { + .addr = client->addr, + .flags = 0, + .len = sizeof(cmd1), + .buf = cmd1, + }, + { + .addr = client->addr, + .flags = 0, + .len = sizeof(cmd2), + .buf = cmd2, + }, + { + .addr = client->addr, + .flags = I2C_M_RD, + .len = sizeof(data), + .buf = data, + }, + }; + + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); + if (ret < 0) + return ret; + if (ret != ARRAY_SIZE(msgs)) + return -EIO; + + features->x_max = get_unaligned_le16(&data[3]); + features->y_max = get_unaligned_le16(&data[5]); + features->pressure_max = get_unaligned_le16(&data[11]); + features->fw_version = get_unaligned_le16(&data[13]); + + dev_dbg(&client->dev, + "x_max:%d, y_max:%d, pressure:%d, fw:%d\n", + features->x_max, features->y_max, + features->pressure_max, features->fw_version); + + return 0; +} + +static int wacom_i2c_fetch_data(struct wacom_i2c *wac_i2c) +{ + int retries = 0; + int ret; + + do { + ret = i2c_master_recv(wac_i2c->client, + wac_i2c->data, sizeof(wac_i2c->data)); + } while (gpio_get_value(wac_i2c->gpio) == 0 && + retries++ < WACOM_RETRY_CNT); + + if (retries >= WACOM_RETRY_CNT) + ret = -EIO; + + return ret < 0 ? ret : 0; +} + +static irqreturn_t wacom_i2c_irq(int irq, void *dev_id) +{ + struct wacom_i2c *wac_i2c = dev_id; + struct input_dev *input = wac_i2c->input; + u8 *data = wac_i2c->data; + unsigned int x, y, pressure; + unsigned char tsw, f1, f2, ers; + int error; + + error = wacom_i2c_fetch_data(wac_i2c); + if (error) + goto out; + + tsw = data[3] & 0x01; + ers = data[3] & 0x04; + f1 = data[3] & 0x02; + f2 = data[3] & 0x10; + x = le16_to_cpup((__le16 *)&data[4]); + y = le16_to_cpup((__le16 *)&data[6]); + pressure = le16_to_cpup((__le16 *)&data[8]); + + input_report_key(input, BTN_TOUCH, tsw || ers); + input_report_key(input, BTN_TOOL_PEN, tsw); + input_report_key(input, BTN_TOOL_RUBBER, ers); + input_report_key(input, BTN_STYLUS, f1); + input_report_key(input, BTN_STYLUS2, f2); + input_report_abs(input, ABS_X, x); + input_report_abs(input, ABS_Y, y); + input_report_abs(input, ABS_PRESSURE, pressure); + input_sync(input); + +out: + return IRQ_HANDLED; +} + +static int wacom_i2c_open(struct input_dev *dev) +{ + struct wacom_i2c *wac_i2c = input_get_drvdata(dev); + struct i2c_client *client = wac_i2c->client; + int error; + + /* Clear the device buffer */ + error = wacom_i2c_fetch_data(wac_i2c); + if (error) + return error; + + enable_irq(client->irq); + + return 0; +} + +static void wacom_i2c_close(struct input_dev *dev) +{ + struct wacom_i2c *wac_i2c = input_get_drvdata(dev); + struct i2c_client *client = wac_i2c->client; + + disable_irq(client->irq); +} + +static int __devinit wacom_i2c_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct wacom_i2c *wac_i2c; + struct input_dev *input; + struct wacom_features features; + int gpio; + int error; + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { + dev_err(&client->dev, "i2c_check_functionality error\n"); + return -EIO; + } + + gpio = irq_to_gpio(client->irq); + if (gpio < 0) { + error = gpio; + dev_err(&client->dev, + "irq_to_gpio() failed, error: %d\n", error); + return error; + } + + error = wacom_query_device(client, &features); + if (error) + return error; + + wac_i2c = kzalloc(sizeof(*wac_i2c), GFP_KERNEL); + input = input_allocate_device(); + if (!wac_i2c || !input) { + error = -ENOMEM; + goto err_free_mem; + } + + wac_i2c->client = client; + wac_i2c->input = input; + wac_i2c->gpio = gpio; + + input->name = "Wacom I2C Digitizer"; + input->id.bustype = BUS_I2C; + input->id.vendor = 0x56a; + input->id.version = features.fw_version; + input->dev.parent = &client->dev; + input->open = wacom_i2c_open; + input->close = wacom_i2c_close; + + input->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); + + __set_bit(BTN_TOOL_PEN, input->keybit); + __set_bit(BTN_TOOL_RUBBER, input->keybit); + __set_bit(BTN_STYLUS, input->keybit); + __set_bit(BTN_STYLUS2, input->keybit); + __set_bit(BTN_TOUCH, input->keybit); + + input_set_abs_params(input, ABS_X, 0, features.x_max, 0, 0); + input_set_abs_params(input, ABS_Y, 0, features.y_max, 0, 0); + input_set_abs_params(input, ABS_PRESSURE, + 0, features.pressure_max, 0, 0); + + input_set_drvdata(input, wac_i2c); + + error = request_threaded_irq(client->irq, NULL, wacom_i2c_irq, + IRQF_TRIGGER_FALLING, + "wacom_i2c", wac_i2c); + if (error) { + dev_err(&client->dev, + "Failed to enable IRQ, error: %d\n", error); + goto err_free_mem; + } + + /* Disable the IRQ, we'll enable it in wac_i2c_open() */ + disable_irq(client->irq); + + error = input_register_device(wac_i2c->input); + if (error) { + dev_err(&client->dev, + "Failed to register input device, error: %d\n", error); + goto err_free_irq; + } + + i2c_set_clientdata(client, wac_i2c); + return 0; + +err_free_irq: + free_irq(client->irq, wac_i2c); +err_free_mem: + input_free_device(input); + kfree(wac_i2c); + + return error; +} + +static int __devexit wacom_i2c_remove(struct i2c_client *client) +{ + struct wacom_i2c *wac_i2c = i2c_get_clientdata(client); + + free_irq(client->irq, wac_i2c); + input_unregister_device(wac_i2c->input); + kfree(wac_i2c); + + return 0; +} + +#ifdef CONFIG_PM_SLEEP +static int wacom_i2c_suspend(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + + disable_irq(client->irq); + + return 0; +} + +static int wacom_i2c_resume(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + + enable_irq(client->irq); + + return 0; +} +#endif + +static SIMPLE_DEV_PM_OPS(wacom_i2c_pm, wacom_i2c_suspend, wacom_i2c_resume); + +static const struct i2c_device_id wacom_i2c_id[] = { + { "WAC_I2C_EMR", 0 }, + { }, +}; +MODULE_DEVICE_TABLE(i2c, wacom_i2c_id); + +static struct i2c_driver wacom_i2c_driver = { + .driver = { + .name = "wacom_i2c", + .owner = THIS_MODULE, + .pm = &wacom_i2c_pm, + }, + + .probe = wacom_i2c_probe, + .remove = __devexit_p(wacom_i2c_remove), + .id_table = wacom_i2c_id, +}; +module_i2c_driver(wacom_i2c_driver); + +MODULE_AUTHOR("Tatsunosuke Tobita "); +MODULE_DESCRIPTION("WACOM EMR I2C Driver"); +MODULE_LICENSE("GPL"); -- cgit v1.2.3 From d568778298f58330bcc8cc23845676d1143c8d33 Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Tue, 10 Apr 2012 00:21:09 -0700 Subject: Input: wacom_i2c - do not use irq_to_gpio Because irq_to_gpio() is not available on many platforms let's switch to level-triggered one-shot IRQs that will stay active as long as there is data to be read. Tested-by: Tobita Tatsunosuke Signed-off-by: Dmitry Torokhov --- drivers/input/touchscreen/wacom_i2c.c | 41 ++++------------------------------- 1 file changed, 4 insertions(+), 37 deletions(-) (limited to 'drivers/input/touchscreen/wacom_i2c.c') diff --git a/drivers/input/touchscreen/wacom_i2c.c b/drivers/input/touchscreen/wacom_i2c.c index b8ca4a6bc91a..35572575d34a 100644 --- a/drivers/input/touchscreen/wacom_i2c.c +++ b/drivers/input/touchscreen/wacom_i2c.c @@ -39,7 +39,6 @@ struct wacom_features { struct wacom_i2c { struct i2c_client *client; struct input_dev *input; - unsigned int gpio; u8 data[WACOM_QUERY_SIZE]; }; @@ -91,23 +90,6 @@ static int wacom_query_device(struct i2c_client *client, return 0; } -static int wacom_i2c_fetch_data(struct wacom_i2c *wac_i2c) -{ - int retries = 0; - int ret; - - do { - ret = i2c_master_recv(wac_i2c->client, - wac_i2c->data, sizeof(wac_i2c->data)); - } while (gpio_get_value(wac_i2c->gpio) == 0 && - retries++ < WACOM_RETRY_CNT); - - if (retries >= WACOM_RETRY_CNT) - ret = -EIO; - - return ret < 0 ? ret : 0; -} - static irqreturn_t wacom_i2c_irq(int irq, void *dev_id) { struct wacom_i2c *wac_i2c = dev_id; @@ -117,8 +99,9 @@ static irqreturn_t wacom_i2c_irq(int irq, void *dev_id) unsigned char tsw, f1, f2, ers; int error; - error = wacom_i2c_fetch_data(wac_i2c); - if (error) + error = i2c_master_recv(wac_i2c->client, + wac_i2c->data, sizeof(wac_i2c->data)); + if (error < 0) goto out; tsw = data[3] & 0x01; @@ -147,12 +130,6 @@ static int wacom_i2c_open(struct input_dev *dev) { struct wacom_i2c *wac_i2c = input_get_drvdata(dev); struct i2c_client *client = wac_i2c->client; - int error; - - /* Clear the device buffer */ - error = wacom_i2c_fetch_data(wac_i2c); - if (error) - return error; enable_irq(client->irq); @@ -173,7 +150,6 @@ static int __devinit wacom_i2c_probe(struct i2c_client *client, struct wacom_i2c *wac_i2c; struct input_dev *input; struct wacom_features features; - int gpio; int error; if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { @@ -181,14 +157,6 @@ static int __devinit wacom_i2c_probe(struct i2c_client *client, return -EIO; } - gpio = irq_to_gpio(client->irq); - if (gpio < 0) { - error = gpio; - dev_err(&client->dev, - "irq_to_gpio() failed, error: %d\n", error); - return error; - } - error = wacom_query_device(client, &features); if (error) return error; @@ -202,7 +170,6 @@ static int __devinit wacom_i2c_probe(struct i2c_client *client, wac_i2c->client = client; wac_i2c->input = input; - wac_i2c->gpio = gpio; input->name = "Wacom I2C Digitizer"; input->id.bustype = BUS_I2C; @@ -228,7 +195,7 @@ static int __devinit wacom_i2c_probe(struct i2c_client *client, input_set_drvdata(input, wac_i2c); error = request_threaded_irq(client->irq, NULL, wacom_i2c_irq, - IRQF_TRIGGER_FALLING, + IRQF_TRIGGER_LOW | IRQF_ONESHOT, "wacom_i2c", wac_i2c); if (error) { dev_err(&client->dev, -- cgit v1.2.3