Linux Audio

Check our new training course

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