Commit | Line | Data |
---|---|---|
83cde383 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
5a966261 TT |
2 | /* |
3 | * Wacom Penabled Driver for I2C | |
4 | * | |
8855f30c | 5 | * Copyright (c) 2011 - 2013 Tatsunosuke Tobita, Wacom. |
5a966261 | 6 | * <tobita.tatsunosuke@wacom.co.jp> |
5a966261 TT |
7 | */ |
8 | ||
91e2e766 | 9 | #include <linux/bits.h> |
5a966261 TT |
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> | |
5a966261 TT |
16 | #include <asm/unaligned.h> |
17 | ||
91e2e766 AF |
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) | |
25 | ||
26 | /* Registers */ | |
fafc6638 AF |
27 | #define WACOM_COMMAND_LSB 0x04 |
28 | #define WACOM_COMMAND_MSB 0x00 | |
29 | ||
30 | #define WACOM_DATA_LSB 0x05 | |
31 | #define WACOM_DATA_MSB 0x00 | |
32 | ||
33 | /* Report types */ | |
34 | #define REPORT_FEATURE 0x30 | |
35 | ||
36 | /* Requests / operations */ | |
37 | #define OPCODE_GET_REPORT 0x02 | |
38 | ||
39 | #define WACOM_QUERY_REPORT 3 | |
5a966261 | 40 | #define WACOM_QUERY_SIZE 19 |
5a966261 TT |
41 | |
42 | struct wacom_features { | |
43 | int x_max; | |
44 | int y_max; | |
45 | int pressure_max; | |
46 | char fw_version; | |
47 | }; | |
48 | ||
49 | struct wacom_i2c { | |
50 | struct i2c_client *client; | |
51 | struct input_dev *input; | |
5a966261 | 52 | u8 data[WACOM_QUERY_SIZE]; |
8855f30c TT |
53 | bool prox; |
54 | int tool; | |
5a966261 TT |
55 | }; |
56 | ||
57 | static int wacom_query_device(struct i2c_client *client, | |
58 | struct wacom_features *features) | |
59 | { | |
fafc6638 AF |
60 | u8 get_query_data_cmd[] = { |
61 | WACOM_COMMAND_LSB, | |
62 | WACOM_COMMAND_MSB, | |
63 | REPORT_FEATURE | WACOM_QUERY_REPORT, | |
64 | OPCODE_GET_REPORT, | |
65 | WACOM_DATA_LSB, | |
66 | WACOM_DATA_MSB, | |
67 | }; | |
5a966261 | 68 | u8 data[WACOM_QUERY_SIZE]; |
fafc6638 AF |
69 | int ret; |
70 | ||
5a966261 | 71 | struct i2c_msg msgs[] = { |
fafc6638 | 72 | /* Request reading of feature ReportID: 3 (Pen Query Data) */ |
5a966261 TT |
73 | { |
74 | .addr = client->addr, | |
75 | .flags = 0, | |
fafc6638 AF |
76 | .len = sizeof(get_query_data_cmd), |
77 | .buf = get_query_data_cmd, | |
5a966261 TT |
78 | }, |
79 | { | |
80 | .addr = client->addr, | |
81 | .flags = I2C_M_RD, | |
82 | .len = sizeof(data), | |
83 | .buf = data, | |
84 | }, | |
85 | }; | |
86 | ||
87 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); | |
88 | if (ret < 0) | |
89 | return ret; | |
90 | if (ret != ARRAY_SIZE(msgs)) | |
91 | return -EIO; | |
92 | ||
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]); | |
97 | ||
98 | dev_dbg(&client->dev, | |
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); | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
5a966261 TT |
106 | static irqreturn_t wacom_i2c_irq(int irq, void *dev_id) |
107 | { | |
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; | |
113 | int error; | |
114 | ||
d5687782 DT |
115 | error = i2c_master_recv(wac_i2c->client, |
116 | wac_i2c->data, sizeof(wac_i2c->data)); | |
117 | if (error < 0) | |
5a966261 TT |
118 | goto out; |
119 | ||
91e2e766 AF |
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; | |
5a966261 TT |
124 | x = le16_to_cpup((__le16 *)&data[4]); |
125 | y = le16_to_cpup((__le16 *)&data[6]); | |
126 | pressure = le16_to_cpup((__le16 *)&data[8]); | |
127 | ||
8855f30c | 128 | if (!wac_i2c->prox) |
91e2e766 | 129 | wac_i2c->tool = (data[3] & (WACOM_ERASER | WACOM_INVERT)) ? |
8855f30c TT |
130 | BTN_TOOL_RUBBER : BTN_TOOL_PEN; |
131 | ||
91e2e766 | 132 | wac_i2c->prox = data[3] & WACOM_IN_PROXIMITY; |
8855f30c | 133 | |
5a966261 | 134 | input_report_key(input, BTN_TOUCH, tsw || ers); |
8855f30c | 135 | input_report_key(input, wac_i2c->tool, wac_i2c->prox); |
5a966261 TT |
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); | |
141 | input_sync(input); | |
142 | ||
143 | out: | |
144 | return IRQ_HANDLED; | |
145 | } | |
146 | ||
147 | static int wacom_i2c_open(struct input_dev *dev) | |
148 | { | |
149 | struct wacom_i2c *wac_i2c = input_get_drvdata(dev); | |
150 | struct i2c_client *client = wac_i2c->client; | |
5a966261 TT |
151 | |
152 | enable_irq(client->irq); | |
153 | ||
154 | return 0; | |
155 | } | |
156 | ||
157 | static void wacom_i2c_close(struct input_dev *dev) | |
158 | { | |
159 | struct wacom_i2c *wac_i2c = input_get_drvdata(dev); | |
160 | struct i2c_client *client = wac_i2c->client; | |
161 | ||
162 | disable_irq(client->irq); | |
163 | } | |
164 | ||
0a3098db | 165 | static int wacom_i2c_probe(struct i2c_client *client) |
5a966261 | 166 | { |
c75cf862 | 167 | struct device *dev = &client->dev; |
5a966261 TT |
168 | struct wacom_i2c *wac_i2c; |
169 | struct input_dev *input; | |
8830cb88 | 170 | struct wacom_features features = { 0 }; |
5a966261 TT |
171 | int error; |
172 | ||
173 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | |
c75cf862 | 174 | dev_err(dev, "i2c_check_functionality error\n"); |
5a966261 TT |
175 | return -EIO; |
176 | } | |
177 | ||
5a966261 TT |
178 | error = wacom_query_device(client, &features); |
179 | if (error) | |
180 | return error; | |
181 | ||
c75cf862 DT |
182 | wac_i2c = devm_kzalloc(dev, sizeof(*wac_i2c), GFP_KERNEL); |
183 | if (!wac_i2c) | |
184 | return -ENOMEM; | |
5a966261 TT |
185 | |
186 | wac_i2c->client = client; | |
c75cf862 DT |
187 | |
188 | input = devm_input_allocate_device(dev); | |
189 | if (!input) | |
190 | return -ENOMEM; | |
191 | ||
5a966261 | 192 | wac_i2c->input = input; |
5a966261 TT |
193 | |
194 | input->name = "Wacom I2C Digitizer"; | |
195 | input->id.bustype = BUS_I2C; | |
196 | input->id.vendor = 0x56a; | |
197 | input->id.version = features.fw_version; | |
5a966261 TT |
198 | input->open = wacom_i2c_open; |
199 | input->close = wacom_i2c_close; | |
200 | ||
201 | input->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); | |
202 | ||
203 | __set_bit(BTN_TOOL_PEN, input->keybit); | |
204 | __set_bit(BTN_TOOL_RUBBER, input->keybit); | |
205 | __set_bit(BTN_STYLUS, input->keybit); | |
206 | __set_bit(BTN_STYLUS2, input->keybit); | |
207 | __set_bit(BTN_TOUCH, input->keybit); | |
208 | ||
209 | input_set_abs_params(input, ABS_X, 0, features.x_max, 0, 0); | |
210 | input_set_abs_params(input, ABS_Y, 0, features.y_max, 0, 0); | |
211 | input_set_abs_params(input, ABS_PRESSURE, | |
212 | 0, features.pressure_max, 0, 0); | |
213 | ||
214 | input_set_drvdata(input, wac_i2c); | |
215 | ||
c75cf862 DT |
216 | error = devm_request_threaded_irq(dev, client->irq, NULL, wacom_i2c_irq, |
217 | IRQF_ONESHOT, "wacom_i2c", wac_i2c); | |
5a966261 | 218 | if (error) { |
c75cf862 DT |
219 | dev_err(dev, "Failed to request IRQ: %d\n", error); |
220 | return error; | |
5a966261 TT |
221 | } |
222 | ||
223 | /* Disable the IRQ, we'll enable it in wac_i2c_open() */ | |
224 | disable_irq(client->irq); | |
225 | ||
226 | error = input_register_device(wac_i2c->input); | |
227 | if (error) { | |
c75cf862 DT |
228 | dev_err(dev, "Failed to register input device: %d\n", error); |
229 | return error; | |
5a966261 TT |
230 | } |
231 | ||
5a966261 TT |
232 | return 0; |
233 | } | |
234 | ||
5ca74320 | 235 | static int wacom_i2c_suspend(struct device *dev) |
5a966261 TT |
236 | { |
237 | struct i2c_client *client = to_i2c_client(dev); | |
238 | ||
239 | disable_irq(client->irq); | |
240 | ||
241 | return 0; | |
242 | } | |
243 | ||
5ca74320 | 244 | static int wacom_i2c_resume(struct device *dev) |
5a966261 TT |
245 | { |
246 | struct i2c_client *client = to_i2c_client(dev); | |
247 | ||
248 | enable_irq(client->irq); | |
249 | ||
250 | return 0; | |
251 | } | |
5a966261 | 252 | |
5ca74320 | 253 | static DEFINE_SIMPLE_DEV_PM_OPS(wacom_i2c_pm, wacom_i2c_suspend, wacom_i2c_resume); |
5a966261 TT |
254 | |
255 | static const struct i2c_device_id wacom_i2c_id[] = { | |
256 | { "WAC_I2C_EMR", 0 }, | |
257 | { }, | |
258 | }; | |
259 | MODULE_DEVICE_TABLE(i2c, wacom_i2c_id); | |
260 | ||
261 | static struct i2c_driver wacom_i2c_driver = { | |
262 | .driver = { | |
263 | .name = "wacom_i2c", | |
5ca74320 | 264 | .pm = pm_sleep_ptr(&wacom_i2c_pm), |
5a966261 TT |
265 | }, |
266 | ||
0a3098db | 267 | .probe_new = wacom_i2c_probe, |
5a966261 TT |
268 | .id_table = wacom_i2c_id, |
269 | }; | |
270 | module_i2c_driver(wacom_i2c_driver); | |
271 | ||
272 | MODULE_AUTHOR("Tatsunosuke Tobita <tobita.tatsunosuke@wacom.co.jp>"); | |
273 | MODULE_DESCRIPTION("WACOM EMR I2C Driver"); | |
274 | MODULE_LICENSE("GPL"); |