1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Wacom Penabled Driver for I2C
5 * Copyright (c) 2011 - 2013 Tatsunosuke Tobita, Wacom.
6 * <tobita.tatsunosuke@wacom.co.jp>
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 <linux/gpio.h>
16 #include <asm/unaligned.h>
18 #define WACOM_CMD_QUERY0 0x04
19 #define WACOM_CMD_QUERY1 0x00
20 #define WACOM_CMD_QUERY2 0x33
21 #define WACOM_CMD_QUERY3 0x02
22 #define WACOM_CMD_THROW0 0x05
23 #define WACOM_CMD_THROW1 0x00
24 #define WACOM_QUERY_SIZE 19
26 struct wacom_features
{
34 struct i2c_client
*client
;
35 struct input_dev
*input
;
36 u8 data
[WACOM_QUERY_SIZE
];
41 static int wacom_query_device(struct i2c_client
*client
,
42 struct wacom_features
*features
)
45 u8 cmd1
[] = { WACOM_CMD_QUERY0
, WACOM_CMD_QUERY1
,
46 WACOM_CMD_QUERY2
, WACOM_CMD_QUERY3
};
47 u8 cmd2
[] = { WACOM_CMD_THROW0
, WACOM_CMD_THROW1
};
48 u8 data
[WACOM_QUERY_SIZE
];
49 struct i2c_msg msgs
[] = {
70 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
73 if (ret
!= ARRAY_SIZE(msgs
))
76 features
->x_max
= get_unaligned_le16(&data
[3]);
77 features
->y_max
= get_unaligned_le16(&data
[5]);
78 features
->pressure_max
= get_unaligned_le16(&data
[11]);
79 features
->fw_version
= get_unaligned_le16(&data
[13]);
82 "x_max:%d, y_max:%d, pressure:%d, fw:%d\n",
83 features
->x_max
, features
->y_max
,
84 features
->pressure_max
, features
->fw_version
);
89 static irqreturn_t
wacom_i2c_irq(int irq
, void *dev_id
)
91 struct wacom_i2c
*wac_i2c
= dev_id
;
92 struct input_dev
*input
= wac_i2c
->input
;
93 u8
*data
= wac_i2c
->data
;
94 unsigned int x
, y
, pressure
;
95 unsigned char tsw
, f1
, f2
, ers
;
98 error
= i2c_master_recv(wac_i2c
->client
,
99 wac_i2c
->data
, sizeof(wac_i2c
->data
));
103 tsw
= data
[3] & 0x01;
104 ers
= data
[3] & 0x04;
107 x
= le16_to_cpup((__le16
*)&data
[4]);
108 y
= le16_to_cpup((__le16
*)&data
[6]);
109 pressure
= le16_to_cpup((__le16
*)&data
[8]);
112 wac_i2c
->tool
= (data
[3] & 0x0c) ?
113 BTN_TOOL_RUBBER
: BTN_TOOL_PEN
;
115 wac_i2c
->prox
= data
[3] & 0x20;
117 input_report_key(input
, BTN_TOUCH
, tsw
|| ers
);
118 input_report_key(input
, wac_i2c
->tool
, wac_i2c
->prox
);
119 input_report_key(input
, BTN_STYLUS
, f1
);
120 input_report_key(input
, BTN_STYLUS2
, f2
);
121 input_report_abs(input
, ABS_X
, x
);
122 input_report_abs(input
, ABS_Y
, y
);
123 input_report_abs(input
, ABS_PRESSURE
, pressure
);
130 static int wacom_i2c_open(struct input_dev
*dev
)
132 struct wacom_i2c
*wac_i2c
= input_get_drvdata(dev
);
133 struct i2c_client
*client
= wac_i2c
->client
;
135 enable_irq(client
->irq
);
140 static void wacom_i2c_close(struct input_dev
*dev
)
142 struct wacom_i2c
*wac_i2c
= input_get_drvdata(dev
);
143 struct i2c_client
*client
= wac_i2c
->client
;
145 disable_irq(client
->irq
);
148 static int wacom_i2c_probe(struct i2c_client
*client
,
149 const struct i2c_device_id
*id
)
151 struct wacom_i2c
*wac_i2c
;
152 struct input_dev
*input
;
153 struct wacom_features features
= { 0 };
156 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
157 dev_err(&client
->dev
, "i2c_check_functionality error\n");
161 error
= wacom_query_device(client
, &features
);
165 wac_i2c
= kzalloc(sizeof(*wac_i2c
), GFP_KERNEL
);
166 input
= input_allocate_device();
167 if (!wac_i2c
|| !input
) {
172 wac_i2c
->client
= client
;
173 wac_i2c
->input
= input
;
175 input
->name
= "Wacom I2C Digitizer";
176 input
->id
.bustype
= BUS_I2C
;
177 input
->id
.vendor
= 0x56a;
178 input
->id
.version
= features
.fw_version
;
179 input
->dev
.parent
= &client
->dev
;
180 input
->open
= wacom_i2c_open
;
181 input
->close
= wacom_i2c_close
;
183 input
->evbit
[0] |= BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
185 __set_bit(BTN_TOOL_PEN
, input
->keybit
);
186 __set_bit(BTN_TOOL_RUBBER
, input
->keybit
);
187 __set_bit(BTN_STYLUS
, input
->keybit
);
188 __set_bit(BTN_STYLUS2
, input
->keybit
);
189 __set_bit(BTN_TOUCH
, input
->keybit
);
191 input_set_abs_params(input
, ABS_X
, 0, features
.x_max
, 0, 0);
192 input_set_abs_params(input
, ABS_Y
, 0, features
.y_max
, 0, 0);
193 input_set_abs_params(input
, ABS_PRESSURE
,
194 0, features
.pressure_max
, 0, 0);
196 input_set_drvdata(input
, wac_i2c
);
198 error
= request_threaded_irq(client
->irq
, NULL
, wacom_i2c_irq
,
199 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
200 "wacom_i2c", wac_i2c
);
202 dev_err(&client
->dev
,
203 "Failed to enable IRQ, error: %d\n", error
);
207 /* Disable the IRQ, we'll enable it in wac_i2c_open() */
208 disable_irq(client
->irq
);
210 error
= input_register_device(wac_i2c
->input
);
212 dev_err(&client
->dev
,
213 "Failed to register input device, error: %d\n", error
);
217 i2c_set_clientdata(client
, wac_i2c
);
221 free_irq(client
->irq
, wac_i2c
);
223 input_free_device(input
);
229 static int wacom_i2c_remove(struct i2c_client
*client
)
231 struct wacom_i2c
*wac_i2c
= i2c_get_clientdata(client
);
233 free_irq(client
->irq
, wac_i2c
);
234 input_unregister_device(wac_i2c
->input
);
240 static int __maybe_unused
wacom_i2c_suspend(struct device
*dev
)
242 struct i2c_client
*client
= to_i2c_client(dev
);
244 disable_irq(client
->irq
);
249 static int __maybe_unused
wacom_i2c_resume(struct device
*dev
)
251 struct i2c_client
*client
= to_i2c_client(dev
);
253 enable_irq(client
->irq
);
258 static SIMPLE_DEV_PM_OPS(wacom_i2c_pm
, wacom_i2c_suspend
, wacom_i2c_resume
);
260 static const struct i2c_device_id wacom_i2c_id
[] = {
261 { "WAC_I2C_EMR", 0 },
264 MODULE_DEVICE_TABLE(i2c
, wacom_i2c_id
);
266 static struct i2c_driver wacom_i2c_driver
= {
272 .probe
= wacom_i2c_probe
,
273 .remove
= wacom_i2c_remove
,
274 .id_table
= wacom_i2c_id
,
276 module_i2c_driver(wacom_i2c_driver
);
278 MODULE_AUTHOR("Tatsunosuke Tobita <tobita.tatsunosuke@wacom.co.jp>");
279 MODULE_DESCRIPTION("WACOM EMR I2C Driver");
280 MODULE_LICENSE("GPL");