1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Wacom Penabled Driver for I2C
5 * Copyright (c) 2011 - 2013 Tatsunosuke Tobita, Wacom.
9 #include <linux/bits.h>
10 #include <linux/module.h>
11 #include <linux/input.h>
12 #include <linux/i2c.h>
13 #include <linux/slab.h>
14 #include <linux/irq.h>
15 #include <linux/interrupt.h>
16 #include <asm/unaligned.h>
18 /* Bitmasks (for data[3]) */
19 #define WACOM_TIP_SWITCH BIT(0)
20 #define WACOM_BARREL_SWITCH BIT(1)
21 #define WACOM_ERASER BIT(2)
22 #define WACOM_INVERT BIT(3)
23 #define WACOM_BARREL_SWITCH_2 BIT(4)
24 #define WACOM_IN_PROXIMITY BIT(5)
27 #define WACOM_COMMAND_LSB 0x04
28 #define WACOM_COMMAND_MSB 0x00
30 #define WACOM_DATA_LSB 0x05
31 #define WACOM_DATA_MSB 0x00
34 #define REPORT_FEATURE 0x30
36 /* Requests / operations */
37 #define OPCODE_GET_REPORT 0x02
39 #define WACOM_QUERY_REPORT 3
40 #define WACOM_QUERY_SIZE 19
42 struct wacom_features {
50 struct i2c_client *client;
51 struct input_dev *input;
52 u8 data[WACOM_QUERY_SIZE];
57 static int wacom_query_device(struct i2c_client *client,
58 struct wacom_features *features)
60 u8 get_query_data_cmd[] = {
63 REPORT_FEATURE | WACOM_QUERY_REPORT,
68 u8 data[WACOM_QUERY_SIZE];
71 struct i2c_msg msgs[] = {
72 /* Request reading of feature ReportID: 3 (Pen Query Data) */
76 .len = sizeof(get_query_data_cmd),
77 .buf = get_query_data_cmd,
87 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
90 if (ret != ARRAY_SIZE(msgs))
93 features->x_max = get_unaligned_le16(&data[3]);
94 features->y_max = get_unaligned_le16(&data[5]);
95 features->pressure_max = get_unaligned_le16(&data[11]);
96 features->fw_version = get_unaligned_le16(&data[13]);
99 "x_max:%d, y_max:%d, pressure:%d, fw:%d\n",
100 features->x_max, features->y_max,
101 features->pressure_max, features->fw_version);
106 static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
108 struct wacom_i2c *wac_i2c = dev_id;
109 struct input_dev *input = wac_i2c->input;
110 u8 *data = wac_i2c->data;
111 unsigned int x, y, pressure;
112 unsigned char tsw, f1, f2, ers;
115 error = i2c_master_recv(wac_i2c->client,
116 wac_i2c->data, sizeof(wac_i2c->data));
120 tsw = data[3] & WACOM_TIP_SWITCH;
121 ers = data[3] & WACOM_ERASER;
122 f1 = data[3] & WACOM_BARREL_SWITCH;
123 f2 = data[3] & WACOM_BARREL_SWITCH_2;
124 x = le16_to_cpup((__le16 *)&data[4]);
125 y = le16_to_cpup((__le16 *)&data[6]);
126 pressure = le16_to_cpup((__le16 *)&data[8]);
129 wac_i2c->tool = (data[3] & (WACOM_ERASER | WACOM_INVERT)) ?
130 BTN_TOOL_RUBBER : BTN_TOOL_PEN;
132 wac_i2c->prox = data[3] & WACOM_IN_PROXIMITY;
134 input_report_key(input, BTN_TOUCH, tsw || ers);
135 input_report_key(input, wac_i2c->tool, wac_i2c->prox);
136 input_report_key(input, BTN_STYLUS, f1);
137 input_report_key(input, BTN_STYLUS2, f2);
138 input_report_abs(input, ABS_X, x);
139 input_report_abs(input, ABS_Y, y);
140 input_report_abs(input, ABS_PRESSURE, pressure);
147 static int wacom_i2c_open(struct input_dev *dev)
149 struct wacom_i2c *wac_i2c = input_get_drvdata(dev);
150 struct i2c_client *client = wac_i2c->client;
152 enable_irq(client->irq);
157 static void wacom_i2c_close(struct input_dev *dev)
159 struct wacom_i2c *wac_i2c = input_get_drvdata(dev);
160 struct i2c_client *client = wac_i2c->client;
162 disable_irq(client->irq);
165 static int wacom_i2c_probe(struct i2c_client *client,
166 const struct i2c_device_id *id)
168 struct device *dev = &client->dev;
169 struct wacom_i2c *wac_i2c;
170 struct input_dev *input;
171 struct wacom_features features = { 0 };
174 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
175 dev_err(dev, "i2c_check_functionality error\n");
179 error = wacom_query_device(client, &features);
183 wac_i2c = devm_kzalloc(dev, sizeof(*wac_i2c), GFP_KERNEL);
187 wac_i2c->client = client;
189 input = devm_input_allocate_device(dev);
193 wac_i2c->input = input;
195 input->name = "Wacom I2C Digitizer";
196 input->id.bustype = BUS_I2C;
197 input->id.vendor = 0x56a;
198 input->id.version = features.fw_version;
199 input->open = wacom_i2c_open;
200 input->close = wacom_i2c_close;
202 input->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
204 __set_bit(BTN_TOOL_PEN, input->keybit);
205 __set_bit(BTN_TOOL_RUBBER, input->keybit);
206 __set_bit(BTN_STYLUS, input->keybit);
207 __set_bit(BTN_STYLUS2, input->keybit);
208 __set_bit(BTN_TOUCH, input->keybit);
210 input_set_abs_params(input, ABS_X, 0, features.x_max, 0, 0);
211 input_set_abs_params(input, ABS_Y, 0, features.y_max, 0, 0);
212 input_set_abs_params(input, ABS_PRESSURE,
213 0, features.pressure_max, 0, 0);
215 input_set_drvdata(input, wac_i2c);
217 error = devm_request_threaded_irq(dev, client->irq, NULL, wacom_i2c_irq,
218 IRQF_ONESHOT, "wacom_i2c", wac_i2c);
220 dev_err(dev, "Failed to request IRQ: %d\n", error);
224 /* Disable the IRQ, we'll enable it in wac_i2c_open() */
225 disable_irq(client->irq);
227 error = input_register_device(wac_i2c->input);
229 dev_err(dev, "Failed to register input device: %d\n", error);
236 static int __maybe_unused wacom_i2c_suspend(struct device *dev)
238 struct i2c_client *client = to_i2c_client(dev);
240 disable_irq(client->irq);
245 static int __maybe_unused wacom_i2c_resume(struct device *dev)
247 struct i2c_client *client = to_i2c_client(dev);
249 enable_irq(client->irq);
254 static SIMPLE_DEV_PM_OPS(wacom_i2c_pm, wacom_i2c_suspend, wacom_i2c_resume);
256 static const struct i2c_device_id wacom_i2c_id[] = {
257 { "WAC_I2C_EMR", 0 },
260 MODULE_DEVICE_TABLE(i2c, wacom_i2c_id);
262 static struct i2c_driver wacom_i2c_driver = {
268 .probe = wacom_i2c_probe,
269 .id_table = wacom_i2c_id,
271 module_i2c_driver(wacom_i2c_driver);
274 MODULE_DESCRIPTION("WACOM EMR I2C Driver");
275 MODULE_LICENSE("GPL");