Linux Audio

Check our new training course

Loading...
v3.15
 
  1/*
  2 * Touch Screen driver for EETI's I2C connected touch screen panels
  3 *   Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
  4 *
  5 * See EETI's software guide for the protocol specification:
  6 *   http://home.eeti.com.tw/web20/eg/guide.htm
  7 *
  8 * Based on migor_ts.c
  9 *   Copyright (c) 2008 Magnus Damm
 10 *   Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
 11 *
 12 * This file is free software; you can redistribute it and/or
 13 * modify it under the terms of the GNU  General Public
 14 * License as published by the Free Software Foundation; either
 15 * version 2 of the License, or (at your option) any later version.
 16 *
 17 * This file is distributed in the hope that it will be useful,
 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 20 *  General Public License for more details.
 21 *
 22 * You should have received a copy of the GNU General Public
 23 * License along with this library; if not, write to the Free Software
 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 25 */
 26
 27#include <linux/module.h>
 28#include <linux/moduleparam.h>
 29#include <linux/kernel.h>
 30#include <linux/input.h>
 
 31#include <linux/interrupt.h>
 32#include <linux/i2c.h>
 33#include <linux/timer.h>
 34#include <linux/gpio.h>
 35#include <linux/input/eeti_ts.h>
 36#include <linux/slab.h>
 
 37
 38static bool flip_x;
 39module_param(flip_x, bool, 0644);
 40MODULE_PARM_DESC(flip_x, "flip x coordinate");
 41
 42static bool flip_y;
 43module_param(flip_y, bool, 0644);
 44MODULE_PARM_DESC(flip_y, "flip y coordinate");
 45
 46struct eeti_ts_priv {
 47	struct i2c_client *client;
 48	struct input_dev *input;
 49	struct work_struct work;
 
 50	struct mutex mutex;
 51	int irq_gpio, irq, irq_active_high;
 52};
 53
 54#define EETI_TS_BITDEPTH	(11)
 55#define EETI_MAXVAL		((1 << (EETI_TS_BITDEPTH + 1)) - 1)
 56
 57#define REPORT_BIT_PRESSED	(1 << 0)
 58#define REPORT_BIT_AD0		(1 << 1)
 59#define REPORT_BIT_AD1		(1 << 2)
 60#define REPORT_BIT_HAS_PRESSURE	(1 << 6)
 61#define REPORT_RES_BITS(v)	(((v) >> 1) + EETI_TS_BITDEPTH)
 62
 63static inline int eeti_ts_irq_active(struct eeti_ts_priv *priv)
 64{
 65	return gpio_get_value(priv->irq_gpio) == priv->irq_active_high;
 66}
 67
 68static void eeti_ts_read(struct work_struct *work)
 69{
 70	char buf[6];
 71	unsigned int x, y, res, pressed, to = 100;
 72	struct eeti_ts_priv *priv =
 73		container_of(work, struct eeti_ts_priv, work);
 74
 75	mutex_lock(&priv->mutex);
 76
 77	while (eeti_ts_irq_active(priv) && --to)
 78		i2c_master_recv(priv->client, buf, sizeof(buf));
 79
 80	if (!to) {
 81		dev_err(&priv->client->dev,
 82			"unable to clear IRQ - line stuck?\n");
 83		goto out;
 84	}
 85
 86	/* drop non-report packets */
 87	if (!(buf[0] & 0x80))
 88		goto out;
 89
 90	pressed = buf[0] & REPORT_BIT_PRESSED;
 91	res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
 92	x = buf[2] | (buf[1] << 8);
 93	y = buf[4] | (buf[3] << 8);
 
 94
 95	/* fix the range to 11 bits */
 96	x >>= res - EETI_TS_BITDEPTH;
 97	y >>= res - EETI_TS_BITDEPTH;
 98
 99	if (flip_x)
100		x = EETI_MAXVAL - x;
101
102	if (flip_y)
103		y = EETI_MAXVAL - y;
 
 
104
105	if (buf[0] & REPORT_BIT_HAS_PRESSURE)
106		input_report_abs(priv->input, ABS_PRESSURE, buf[5]);
 
 
107
108	input_report_abs(priv->input, ABS_X, x);
109	input_report_abs(priv->input, ABS_Y, y);
110	input_report_key(priv->input, BTN_TOUCH, !!pressed);
111	input_sync(priv->input);
 
 
 
 
112
113out:
114	mutex_unlock(&priv->mutex);
 
 
 
115}
116
117static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
118{
119	struct eeti_ts_priv *priv = dev_id;
 
 
 
120
121	 /* postpone I2C transactions as we are atomic */
122	schedule_work(&priv->work);
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
 
 
124	return IRQ_HANDLED;
125}
126
127static void eeti_ts_start(struct eeti_ts_priv *priv)
128{
129	enable_irq(priv->irq);
 
 
 
130
131	/* Read the events once to arm the IRQ */
132	eeti_ts_read(&priv->work);
 
 
 
 
 
 
 
133}
134
135static void eeti_ts_stop(struct eeti_ts_priv *priv)
136{
137	disable_irq(priv->irq);
138	cancel_work_sync(&priv->work);
 
 
 
 
 
139}
140
141static int eeti_ts_open(struct input_dev *dev)
142{
143	struct eeti_ts_priv *priv = input_get_drvdata(dev);
144
145	eeti_ts_start(priv);
146
147	return 0;
148}
149
150static void eeti_ts_close(struct input_dev *dev)
151{
152	struct eeti_ts_priv *priv = input_get_drvdata(dev);
153
154	eeti_ts_stop(priv);
155}
156
157static int eeti_ts_probe(struct i2c_client *client,
158				   const struct i2c_device_id *idp)
159{
160	struct eeti_ts_platform_data *pdata = dev_get_platdata(&client->dev);
161	struct eeti_ts_priv *priv;
162	struct input_dev *input;
163	unsigned int irq_flags;
164	int err = -ENOMEM;
165
166	/*
167	 * In contrast to what's described in the datasheet, there seems
168	 * to be no way of probing the presence of that device using I2C
169	 * commands. So we need to blindly believe it is there, and wait
170	 * for interrupts to occur.
171	 */
172
173	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
174	if (!priv) {
175		dev_err(&client->dev, "failed to allocate driver data\n");
176		goto err0;
177	}
178
179	mutex_init(&priv->mutex);
180	input = input_allocate_device();
181
 
182	if (!input) {
183		dev_err(&client->dev, "Failed to allocate input device.\n");
184		goto err1;
185	}
186
187	input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
188	input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
189
190	input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
191	input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
192	input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
193
 
 
194	input->name = client->name;
195	input->id.bustype = BUS_I2C;
196	input->dev.parent = &client->dev;
197	input->open = eeti_ts_open;
198	input->close = eeti_ts_close;
199
200	priv->client = client;
201	priv->input = input;
202	priv->irq_gpio = pdata->irq_gpio;
203	priv->irq = gpio_to_irq(pdata->irq_gpio);
204
205	err = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name);
206	if (err < 0)
207		goto err1;
208
209	priv->irq_active_high = pdata->irq_active_high;
210
211	irq_flags = priv->irq_active_high ?
212		IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
213
214	INIT_WORK(&priv->work, eeti_ts_read);
215	i2c_set_clientdata(client, priv);
216	input_set_drvdata(input, priv);
217
218	err = input_register_device(input);
219	if (err)
220		goto err2;
221
222	err = request_irq(priv->irq, eeti_ts_isr, irq_flags,
223			  client->name, priv);
224	if (err) {
225		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
226		goto err3;
227	}
228
229	/*
230	 * Disable the device for now. It will be enabled once the
231	 * input device is opened.
232	 */
233	eeti_ts_stop(priv);
234
235	device_init_wakeup(&client->dev, 0);
236	return 0;
237
238err3:
239	input_unregister_device(input);
240	input = NULL; /* so we dont try to free it below */
241err2:
242	gpio_free(pdata->irq_gpio);
243err1:
244	input_free_device(input);
245	kfree(priv);
246err0:
247	return err;
248}
249
250static int eeti_ts_remove(struct i2c_client *client)
251{
252	struct eeti_ts_priv *priv = i2c_get_clientdata(client);
253
254	free_irq(priv->irq, priv);
255	/*
256	 * eeti_ts_stop() leaves IRQ disabled. We need to re-enable it
257	 * so that device still works if we reload the driver.
258	 */
259	enable_irq(priv->irq);
260
261	input_unregister_device(priv->input);
262	kfree(priv);
263
264	return 0;
265}
266
267#ifdef CONFIG_PM_SLEEP
268static int eeti_ts_suspend(struct device *dev)
269{
270	struct i2c_client *client = to_i2c_client(dev);
271	struct eeti_ts_priv *priv = i2c_get_clientdata(client);
272	struct input_dev *input_dev = priv->input;
273
274	mutex_lock(&input_dev->mutex);
275
276	if (input_dev->users)
277		eeti_ts_stop(priv);
278
279	mutex_unlock(&input_dev->mutex);
280
281	if (device_may_wakeup(&client->dev))
282		enable_irq_wake(priv->irq);
283
284	return 0;
285}
286
287static int eeti_ts_resume(struct device *dev)
288{
289	struct i2c_client *client = to_i2c_client(dev);
290	struct eeti_ts_priv *priv = i2c_get_clientdata(client);
291	struct input_dev *input_dev = priv->input;
292
293	if (device_may_wakeup(&client->dev))
294		disable_irq_wake(priv->irq);
295
296	mutex_lock(&input_dev->mutex);
297
298	if (input_dev->users)
299		eeti_ts_start(priv);
300
301	mutex_unlock(&input_dev->mutex);
302
303	return 0;
304}
305#endif
306
307static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
308
309static const struct i2c_device_id eeti_ts_id[] = {
310	{ "eeti_ts", 0 },
311	{ }
312};
313MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
314
 
 
 
 
 
 
 
315static struct i2c_driver eeti_ts_driver = {
316	.driver = {
317		.name = "eeti_ts",
318		.pm = &eeti_ts_pm,
 
319	},
320	.probe = eeti_ts_probe,
321	.remove = eeti_ts_remove,
322	.id_table = eeti_ts_id,
323};
324
325module_i2c_driver(eeti_ts_driver);
326
327MODULE_DESCRIPTION("EETI Touchscreen driver");
328MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
329MODULE_LICENSE("GPL");
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Touch Screen driver for EETI's I2C connected touch screen panels
  4 *   Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org>
  5 *
  6 * See EETI's software guide for the protocol specification:
  7 *   http://home.eeti.com.tw/documentation.html
  8 *
  9 * Based on migor_ts.c
 10 *   Copyright (c) 2008 Magnus Damm
 11 *   Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 12 */
 13
 14#include <linux/module.h>
 
 15#include <linux/kernel.h>
 16#include <linux/input.h>
 17#include <linux/input/touchscreen.h>
 18#include <linux/interrupt.h>
 19#include <linux/i2c.h>
 20#include <linux/timer.h>
 21#include <linux/gpio/consumer.h>
 22#include <linux/of.h>
 23#include <linux/slab.h>
 24#include <asm/unaligned.h>
 25
 26struct eeti_ts {
 
 
 
 
 
 
 
 
 27	struct i2c_client *client;
 28	struct input_dev *input;
 29	struct gpio_desc *attn_gpio;
 30	struct touchscreen_properties props;
 31	struct mutex mutex;
 32	bool running;
 33};
 34
 35#define EETI_TS_BITDEPTH	(11)
 36#define EETI_MAXVAL		((1 << (EETI_TS_BITDEPTH + 1)) - 1)
 37
 38#define REPORT_BIT_PRESSED	BIT(0)
 39#define REPORT_BIT_AD0		BIT(1)
 40#define REPORT_BIT_AD1		BIT(2)
 41#define REPORT_BIT_HAS_PRESSURE	BIT(6)
 42#define REPORT_RES_BITS(v)	(((v) >> 1) + EETI_TS_BITDEPTH)
 43
 44static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
 45{
 46	unsigned int res;
 47	u16 x, y;
 48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 49	res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
 50
 51	x = get_unaligned_be16(&buf[1]);
 52	y = get_unaligned_be16(&buf[3]);
 53
 54	/* fix the range to 11 bits */
 55	x >>= res - EETI_TS_BITDEPTH;
 56	y >>= res - EETI_TS_BITDEPTH;
 57
 58	if (buf[0] & REPORT_BIT_HAS_PRESSURE)
 59		input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
 60
 61	touchscreen_report_pos(eeti->input, &eeti->props, x, y, false);
 62	input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
 63	input_sync(eeti->input);
 64}
 65
 66static int eeti_ts_read(struct eeti_ts *eeti)
 67{
 68	int len, error;
 69	char buf[6];
 70
 71	len = i2c_master_recv(eeti->client, buf, sizeof(buf));
 72	if (len != sizeof(buf)) {
 73		error = len < 0 ? len : -EIO;
 74		dev_err(&eeti->client->dev,
 75			"failed to read touchscreen data: %d\n",
 76			error);
 77		return error;
 78	}
 79
 80	/* Motion packet */
 81	if (buf[0] & 0x80)
 82		eeti_ts_report_event(eeti, buf);
 83
 84	return 0;
 85}
 86
 87static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
 88{
 89	struct eeti_ts *eeti = dev_id;
 90	int error;
 91
 92	mutex_lock(&eeti->mutex);
 93
 94	do {
 95		/*
 96		 * If we have attention GPIO, trust it. Otherwise we'll read
 97		 * once and exit. We assume that in this case we are using
 98		 * level triggered interrupt and it will get raised again
 99		 * if/when there is more data.
100		 */
101		if (eeti->attn_gpio &&
102		    !gpiod_get_value_cansleep(eeti->attn_gpio)) {
103			break;
104		}
105
106		error = eeti_ts_read(eeti);
107		if (error)
108			break;
109
110	} while (eeti->running && eeti->attn_gpio);
111
112	mutex_unlock(&eeti->mutex);
113	return IRQ_HANDLED;
114}
115
116static void eeti_ts_start(struct eeti_ts *eeti)
117{
118	mutex_lock(&eeti->mutex);
119
120	eeti->running = true;
121	enable_irq(eeti->client->irq);
122
123	/*
124	 * Kick the controller in case we are using edge interrupt and
125	 * we missed our edge while interrupt was disabled. We expect
126	 * the attention GPIO to be wired in this case.
127	 */
128	if (eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio))
129		eeti_ts_read(eeti);
130
131	mutex_unlock(&eeti->mutex);
132}
133
134static void eeti_ts_stop(struct eeti_ts *eeti)
135{
136	/*
137	 * Not locking here, just setting a flag and expect that the
138	 * interrupt thread will notice the flag eventually.
139	 */
140	eeti->running = false;
141	wmb();
142	disable_irq(eeti->client->irq);
143}
144
145static int eeti_ts_open(struct input_dev *dev)
146{
147	struct eeti_ts *eeti = input_get_drvdata(dev);
148
149	eeti_ts_start(eeti);
150
151	return 0;
152}
153
154static void eeti_ts_close(struct input_dev *dev)
155{
156	struct eeti_ts *eeti = input_get_drvdata(dev);
157
158	eeti_ts_stop(eeti);
159}
160
161static int eeti_ts_probe(struct i2c_client *client)
 
162{
163	struct device *dev = &client->dev;
164	struct eeti_ts *eeti;
165	struct input_dev *input;
166	int error;
 
167
168	/*
169	 * In contrast to what's described in the datasheet, there seems
170	 * to be no way of probing the presence of that device using I2C
171	 * commands. So we need to blindly believe it is there, and wait
172	 * for interrupts to occur.
173	 */
174
175	eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
176	if (!eeti) {
177		dev_err(dev, "failed to allocate driver data\n");
178		return -ENOMEM;
179	}
180
181	mutex_init(&eeti->mutex);
 
182
183	input = devm_input_allocate_device(dev);
184	if (!input) {
185		dev_err(dev, "Failed to allocate input device.\n");
186		return -ENOMEM;
187	}
188
189	input_set_capability(input, EV_KEY, BTN_TOUCH);
 
190
191	input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
192	input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
193	input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
194
195	touchscreen_parse_properties(input, false, &eeti->props);
196
197	input->name = client->name;
198	input->id.bustype = BUS_I2C;
 
199	input->open = eeti_ts_open;
200	input->close = eeti_ts_close;
201
202	eeti->client = client;
203	eeti->input = input;
204
205	eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
206	if (IS_ERR(eeti->attn_gpio))
207		return PTR_ERR(eeti->attn_gpio);
208
209	i2c_set_clientdata(client, eeti);
210	input_set_drvdata(input, eeti);
211
212	error = devm_request_threaded_irq(dev, client->irq,
213					  NULL, eeti_ts_isr,
214					  IRQF_ONESHOT,
215					  client->name, eeti);
216	if (error) {
217		dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
218			error);
219		return error;
 
 
 
 
 
 
 
 
 
220	}
221
222	/*
223	 * Disable the device for now. It will be enabled once the
224	 * input device is opened.
225	 */
226	eeti_ts_stop(eeti);
227
228	error = input_register_device(input);
229	if (error)
230		return error;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
232	return 0;
233}
234
 
235static int eeti_ts_suspend(struct device *dev)
236{
237	struct i2c_client *client = to_i2c_client(dev);
238	struct eeti_ts *eeti = i2c_get_clientdata(client);
239	struct input_dev *input_dev = eeti->input;
240
241	mutex_lock(&input_dev->mutex);
242
243	if (input_device_enabled(input_dev))
244		eeti_ts_stop(eeti);
245
246	mutex_unlock(&input_dev->mutex);
247
248	if (device_may_wakeup(&client->dev))
249		enable_irq_wake(client->irq);
250
251	return 0;
252}
253
254static int eeti_ts_resume(struct device *dev)
255{
256	struct i2c_client *client = to_i2c_client(dev);
257	struct eeti_ts *eeti = i2c_get_clientdata(client);
258	struct input_dev *input_dev = eeti->input;
259
260	if (device_may_wakeup(&client->dev))
261		disable_irq_wake(client->irq);
262
263	mutex_lock(&input_dev->mutex);
264
265	if (input_device_enabled(input_dev))
266		eeti_ts_start(eeti);
267
268	mutex_unlock(&input_dev->mutex);
269
270	return 0;
271}
 
272
273static DEFINE_SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
274
275static const struct i2c_device_id eeti_ts_id[] = {
276	{ "eeti_ts", 0 },
277	{ }
278};
279MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
280
281#ifdef CONFIG_OF
282static const struct of_device_id of_eeti_ts_match[] = {
283	{ .compatible = "eeti,exc3000-i2c", },
284	{ }
285};
286#endif
287
288static struct i2c_driver eeti_ts_driver = {
289	.driver = {
290		.name = "eeti_ts",
291		.pm = pm_sleep_ptr(&eeti_ts_pm),
292		.of_match_table = of_match_ptr(of_eeti_ts_match),
293	},
294	.probe = eeti_ts_probe,
 
295	.id_table = eeti_ts_id,
296};
297
298module_i2c_driver(eeti_ts_driver);
299
300MODULE_DESCRIPTION("EETI Touchscreen driver");
301MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
302MODULE_LICENSE("GPL");