1736e67baSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
210494dceSDaniel Mack /*
310494dceSDaniel Mack  * Touch Screen driver for EETI's I2C connected touch screen panels
4fd8135b6SDaniel Mack  *   Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org>
510494dceSDaniel Mack  *
610494dceSDaniel Mack  * See EETI's software guide for the protocol specification:
7fd8135b6SDaniel Mack  *   http://home.eeti.com.tw/documentation.html
810494dceSDaniel Mack  *
910494dceSDaniel Mack  * Based on migor_ts.c
1010494dceSDaniel Mack  *   Copyright (c) 2008 Magnus Damm
1110494dceSDaniel Mack  *   Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
1210494dceSDaniel Mack  */
1310494dceSDaniel Mack 
1410494dceSDaniel Mack #include <linux/module.h>
1510494dceSDaniel Mack #include <linux/kernel.h>
1610494dceSDaniel Mack #include <linux/input.h>
17a114cbd0SDaniel Mack #include <linux/input/touchscreen.h>
1810494dceSDaniel Mack #include <linux/interrupt.h>
1910494dceSDaniel Mack #include <linux/i2c.h>
2010494dceSDaniel Mack #include <linux/timer.h>
21d99caa47SDmitry Torokhov #include <linux/gpio/consumer.h>
22e32d7f1bSDaniel Mack #include <linux/of.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
24e9f66cdbSDmitry Torokhov #include <asm/unaligned.h>
2510494dceSDaniel Mack 
26720bebdfSDmitry Torokhov struct eeti_ts {
2710494dceSDaniel Mack 	struct i2c_client *client;
2810494dceSDaniel Mack 	struct input_dev *input;
29d99caa47SDmitry Torokhov 	struct gpio_desc *attn_gpio;
30a114cbd0SDaniel Mack 	struct touchscreen_properties props;
31b283d0c3SDaniel Mack 	struct mutex mutex;
320378008aSDmitry Torokhov 	bool running;
3310494dceSDaniel Mack };
3410494dceSDaniel Mack 
3510494dceSDaniel Mack #define EETI_TS_BITDEPTH	(11)
3610494dceSDaniel Mack #define EETI_MAXVAL		((1 << (EETI_TS_BITDEPTH + 1)) - 1)
3710494dceSDaniel Mack 
38272c03bbSDmitry Torokhov #define REPORT_BIT_PRESSED	BIT(0)
39272c03bbSDmitry Torokhov #define REPORT_BIT_AD0		BIT(1)
40272c03bbSDmitry Torokhov #define REPORT_BIT_AD1		BIT(2)
41272c03bbSDmitry Torokhov #define REPORT_BIT_HAS_PRESSURE	BIT(6)
4210494dceSDaniel Mack #define REPORT_RES_BITS(v)	(((v) >> 1) + EETI_TS_BITDEPTH)
4310494dceSDaniel Mack 
440378008aSDmitry Torokhov static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
4510494dceSDaniel Mack {
460378008aSDmitry Torokhov 	unsigned int res;
470378008aSDmitry Torokhov 	u16 x, y;
4810494dceSDaniel Mack 
4910494dceSDaniel Mack 	res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
50e9f66cdbSDmitry Torokhov 
51e9f66cdbSDmitry Torokhov 	x = get_unaligned_be16(&buf[1]);
52e9f66cdbSDmitry Torokhov 	y = get_unaligned_be16(&buf[3]);
5310494dceSDaniel Mack 
5410494dceSDaniel Mack 	/* fix the range to 11 bits */
5510494dceSDaniel Mack 	x >>= res - EETI_TS_BITDEPTH;
5610494dceSDaniel Mack 	y >>= res - EETI_TS_BITDEPTH;
5710494dceSDaniel Mack 
5810494dceSDaniel Mack 	if (buf[0] & REPORT_BIT_HAS_PRESSURE)
59720bebdfSDmitry Torokhov 		input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
6010494dceSDaniel Mack 
61a114cbd0SDaniel Mack 	touchscreen_report_pos(eeti->input, &eeti->props, x, y, false);
620378008aSDmitry Torokhov 	input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
63720bebdfSDmitry Torokhov 	input_sync(eeti->input);
6410494dceSDaniel Mack }
6510494dceSDaniel Mack 
66b283d0c3SDaniel Mack static int eeti_ts_read(struct eeti_ts *eeti)
6710494dceSDaniel Mack {
68b283d0c3SDaniel Mack 	int len, error;
690378008aSDmitry Torokhov 	char buf[6];
7010494dceSDaniel Mack 
710378008aSDmitry Torokhov 	len = i2c_master_recv(eeti->client, buf, sizeof(buf));
720378008aSDmitry Torokhov 	if (len != sizeof(buf)) {
730378008aSDmitry Torokhov 		error = len < 0 ? len : -EIO;
740378008aSDmitry Torokhov 		dev_err(&eeti->client->dev,
750378008aSDmitry Torokhov 			"failed to read touchscreen data: %d\n",
760378008aSDmitry Torokhov 			error);
77b283d0c3SDaniel Mack 		return error;
78b283d0c3SDaniel Mack 	}
79b283d0c3SDaniel Mack 
80b283d0c3SDaniel Mack 	/* Motion packet */
81b283d0c3SDaniel Mack 	if (buf[0] & 0x80)
82b283d0c3SDaniel Mack 		eeti_ts_report_event(eeti, buf);
83b283d0c3SDaniel Mack 
84b283d0c3SDaniel Mack 	return 0;
85b283d0c3SDaniel Mack }
86b283d0c3SDaniel Mack 
87b283d0c3SDaniel Mack static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
88b283d0c3SDaniel Mack {
89b283d0c3SDaniel Mack 	struct eeti_ts *eeti = dev_id;
90b283d0c3SDaniel Mack 	int error;
91b283d0c3SDaniel Mack 
92b283d0c3SDaniel Mack 	mutex_lock(&eeti->mutex);
93b283d0c3SDaniel Mack 
94b283d0c3SDaniel Mack 	do {
95b283d0c3SDaniel Mack 		/*
96b283d0c3SDaniel Mack 		 * If we have attention GPIO, trust it. Otherwise we'll read
97b283d0c3SDaniel Mack 		 * once and exit. We assume that in this case we are using
98b283d0c3SDaniel Mack 		 * level triggered interrupt and it will get raised again
99b283d0c3SDaniel Mack 		 * if/when there is more data.
100b283d0c3SDaniel Mack 		 */
101b283d0c3SDaniel Mack 		if (eeti->attn_gpio &&
102b283d0c3SDaniel Mack 		    !gpiod_get_value_cansleep(eeti->attn_gpio)) {
1030378008aSDmitry Torokhov 			break;
1040378008aSDmitry Torokhov 		}
1050378008aSDmitry Torokhov 
106b283d0c3SDaniel Mack 		error = eeti_ts_read(eeti);
107b283d0c3SDaniel Mack 		if (error)
108b283d0c3SDaniel Mack 			break;
10910494dceSDaniel Mack 
110b283d0c3SDaniel Mack 	} while (eeti->running && eeti->attn_gpio);
111b283d0c3SDaniel Mack 
112b283d0c3SDaniel Mack 	mutex_unlock(&eeti->mutex);
11310494dceSDaniel Mack 	return IRQ_HANDLED;
11410494dceSDaniel Mack }
11510494dceSDaniel Mack 
116720bebdfSDmitry Torokhov static void eeti_ts_start(struct eeti_ts *eeti)
11710494dceSDaniel Mack {
118b283d0c3SDaniel Mack 	mutex_lock(&eeti->mutex);
119b283d0c3SDaniel Mack 
1200378008aSDmitry Torokhov 	eeti->running = true;
1212d388499SDmitry Torokhov 	enable_irq(eeti->client->irq);
122b283d0c3SDaniel Mack 
123b283d0c3SDaniel Mack 	/*
124b283d0c3SDaniel Mack 	 * Kick the controller in case we are using edge interrupt and
125b283d0c3SDaniel Mack 	 * we missed our edge while interrupt was disabled. We expect
126b283d0c3SDaniel Mack 	 * the attention GPIO to be wired in this case.
127b283d0c3SDaniel Mack 	 */
128b283d0c3SDaniel Mack 	if (eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio))
129b283d0c3SDaniel Mack 		eeti_ts_read(eeti);
130b283d0c3SDaniel Mack 
131b283d0c3SDaniel Mack 	mutex_unlock(&eeti->mutex);
1327fbef0d1SDaniel Mack }
1337fbef0d1SDaniel Mack 
134720bebdfSDmitry Torokhov static void eeti_ts_stop(struct eeti_ts *eeti)
1357fbef0d1SDaniel Mack {
136b283d0c3SDaniel Mack 	/*
137b283d0c3SDaniel Mack 	 * Not locking here, just setting a flag and expect that the
138b283d0c3SDaniel Mack 	 * interrupt thread will notice the flag eventually.
139b283d0c3SDaniel Mack 	 */
1400378008aSDmitry Torokhov 	eeti->running = false;
1410378008aSDmitry Torokhov 	wmb();
1422d388499SDmitry Torokhov 	disable_irq(eeti->client->irq);
1437fbef0d1SDaniel Mack }
1447fbef0d1SDaniel Mack 
1457fbef0d1SDaniel Mack static int eeti_ts_open(struct input_dev *dev)
1467fbef0d1SDaniel Mack {
147720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = input_get_drvdata(dev);
1487fbef0d1SDaniel Mack 
149720bebdfSDmitry Torokhov 	eeti_ts_start(eeti);
15010494dceSDaniel Mack 
15110494dceSDaniel Mack 	return 0;
15210494dceSDaniel Mack }
15310494dceSDaniel Mack 
15410494dceSDaniel Mack static void eeti_ts_close(struct input_dev *dev)
15510494dceSDaniel Mack {
156720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = input_get_drvdata(dev);
15710494dceSDaniel Mack 
158720bebdfSDmitry Torokhov 	eeti_ts_stop(eeti);
15910494dceSDaniel Mack }
16010494dceSDaniel Mack 
161923366e9SUwe Kleine-König static int eeti_ts_probe(struct i2c_client *client)
16210494dceSDaniel Mack {
1636f9fab69SDmitry Torokhov 	struct device *dev = &client->dev;
164720bebdfSDmitry Torokhov 	struct eeti_ts *eeti;
16510494dceSDaniel Mack 	struct input_dev *input;
1666f9fab69SDmitry Torokhov 	int error;
16710494dceSDaniel Mack 
1687fbef0d1SDaniel Mack 	/*
1697fbef0d1SDaniel Mack 	 * In contrast to what's described in the datasheet, there seems
17010494dceSDaniel Mack 	 * to be no way of probing the presence of that device using I2C
17110494dceSDaniel Mack 	 * commands. So we need to blindly believe it is there, and wait
1727fbef0d1SDaniel Mack 	 * for interrupts to occur.
1737fbef0d1SDaniel Mack 	 */
17410494dceSDaniel Mack 
1756f9fab69SDmitry Torokhov 	eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
176720bebdfSDmitry Torokhov 	if (!eeti) {
1776f9fab69SDmitry Torokhov 		dev_err(dev, "failed to allocate driver data\n");
178ad03ae44SGuenter Roeck 		return -ENOMEM;
17910494dceSDaniel Mack 	}
18010494dceSDaniel Mack 
181b283d0c3SDaniel Mack 	mutex_init(&eeti->mutex);
182b283d0c3SDaniel Mack 
1836f9fab69SDmitry Torokhov 	input = devm_input_allocate_device(dev);
18410494dceSDaniel Mack 	if (!input) {
1856f9fab69SDmitry Torokhov 		dev_err(dev, "Failed to allocate input device.\n");
1866f9fab69SDmitry Torokhov 		return -ENOMEM;
18710494dceSDaniel Mack 	}
18810494dceSDaniel Mack 
18942e02a6aSDmitry Torokhov 	input_set_capability(input, EV_KEY, BTN_TOUCH);
19010494dceSDaniel Mack 
19110494dceSDaniel Mack 	input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
19210494dceSDaniel Mack 	input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
19310494dceSDaniel Mack 	input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
19410494dceSDaniel Mack 
195a114cbd0SDaniel Mack 	touchscreen_parse_properties(input, false, &eeti->props);
196a114cbd0SDaniel Mack 
19710494dceSDaniel Mack 	input->name = client->name;
19810494dceSDaniel Mack 	input->id.bustype = BUS_I2C;
19910494dceSDaniel Mack 	input->open = eeti_ts_open;
20010494dceSDaniel Mack 	input->close = eeti_ts_close;
20110494dceSDaniel Mack 
202720bebdfSDmitry Torokhov 	eeti->client = client;
203720bebdfSDmitry Torokhov 	eeti->input = input;
20410494dceSDaniel Mack 
205d99caa47SDmitry Torokhov 	eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
206d99caa47SDmitry Torokhov 	if (IS_ERR(eeti->attn_gpio))
207d99caa47SDmitry Torokhov 		return PTR_ERR(eeti->attn_gpio);
20825a70e38SDaniel Mack 
209720bebdfSDmitry Torokhov 	i2c_set_clientdata(client, eeti);
210720bebdfSDmitry Torokhov 	input_set_drvdata(input, eeti);
21110494dceSDaniel Mack 
2120378008aSDmitry Torokhov 	error = devm_request_threaded_irq(dev, client->irq,
2130378008aSDmitry Torokhov 					  NULL, eeti_ts_isr,
214d422be5fSDmitry Torokhov 					  IRQF_ONESHOT,
215720bebdfSDmitry Torokhov 					  client->name, eeti);
2166f9fab69SDmitry Torokhov 	if (error) {
2176f9fab69SDmitry Torokhov 		dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
2186f9fab69SDmitry Torokhov 			error);
2196f9fab69SDmitry Torokhov 		return error;
22010494dceSDaniel Mack 	}
22110494dceSDaniel Mack 
2227fbef0d1SDaniel Mack 	/*
2237fbef0d1SDaniel Mack 	 * Disable the device for now. It will be enabled once the
2247fbef0d1SDaniel Mack 	 * input device is opened.
2257fbef0d1SDaniel Mack 	 */
226720bebdfSDmitry Torokhov 	eeti_ts_stop(eeti);
22710494dceSDaniel Mack 
2280378008aSDmitry Torokhov 	error = input_register_device(input);
2290378008aSDmitry Torokhov 	if (error)
2300378008aSDmitry Torokhov 		return error;
2310378008aSDmitry Torokhov 
23210494dceSDaniel Mack 	return 0;
23310494dceSDaniel Mack }
23410494dceSDaniel Mack 
235*993d854cSJonathan Cameron static int eeti_ts_suspend(struct device *dev)
23610494dceSDaniel Mack {
23785012fffSMark Brown 	struct i2c_client *client = to_i2c_client(dev);
238720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = i2c_get_clientdata(client);
239720bebdfSDmitry Torokhov 	struct input_dev *input_dev = eeti->input;
2407fbef0d1SDaniel Mack 
2417fbef0d1SDaniel Mack 	mutex_lock(&input_dev->mutex);
2427fbef0d1SDaniel Mack 
243d69f0a43SAndrzej Pietrasiewicz 	if (input_device_enabled(input_dev))
244720bebdfSDmitry Torokhov 		eeti_ts_stop(eeti);
2457fbef0d1SDaniel Mack 
2467fbef0d1SDaniel Mack 	mutex_unlock(&input_dev->mutex);
24710494dceSDaniel Mack 
24810494dceSDaniel Mack 	if (device_may_wakeup(&client->dev))
2492d388499SDmitry Torokhov 		enable_irq_wake(client->irq);
25010494dceSDaniel Mack 
25110494dceSDaniel Mack 	return 0;
25210494dceSDaniel Mack }
25310494dceSDaniel Mack 
254*993d854cSJonathan Cameron static int eeti_ts_resume(struct device *dev)
25510494dceSDaniel Mack {
25685012fffSMark Brown 	struct i2c_client *client = to_i2c_client(dev);
257720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = i2c_get_clientdata(client);
258720bebdfSDmitry Torokhov 	struct input_dev *input_dev = eeti->input;
25910494dceSDaniel Mack 
26010494dceSDaniel Mack 	if (device_may_wakeup(&client->dev))
2612d388499SDmitry Torokhov 		disable_irq_wake(client->irq);
26210494dceSDaniel Mack 
2637fbef0d1SDaniel Mack 	mutex_lock(&input_dev->mutex);
2647fbef0d1SDaniel Mack 
265d69f0a43SAndrzej Pietrasiewicz 	if (input_device_enabled(input_dev))
266720bebdfSDmitry Torokhov 		eeti_ts_start(eeti);
2677fbef0d1SDaniel Mack 
2687fbef0d1SDaniel Mack 	mutex_unlock(&input_dev->mutex);
2697fbef0d1SDaniel Mack 
27010494dceSDaniel Mack 	return 0;
27110494dceSDaniel Mack }
27285012fffSMark Brown 
273*993d854cSJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
27410494dceSDaniel Mack 
27510494dceSDaniel Mack static const struct i2c_device_id eeti_ts_id[] = {
27610494dceSDaniel Mack 	{ "eeti_ts", 0 },
27710494dceSDaniel Mack 	{ }
27810494dceSDaniel Mack };
27910494dceSDaniel Mack MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
28010494dceSDaniel Mack 
281e32d7f1bSDaniel Mack #ifdef CONFIG_OF
282e32d7f1bSDaniel Mack static const struct of_device_id of_eeti_ts_match[] = {
283e32d7f1bSDaniel Mack 	{ .compatible = "eeti,exc3000-i2c", },
284e32d7f1bSDaniel Mack 	{ }
285e32d7f1bSDaniel Mack };
286e32d7f1bSDaniel Mack #endif
287e32d7f1bSDaniel Mack 
28810494dceSDaniel Mack static struct i2c_driver eeti_ts_driver = {
28910494dceSDaniel Mack 	.driver = {
29010494dceSDaniel Mack 		.name = "eeti_ts",
291*993d854cSJonathan Cameron 		.pm = pm_sleep_ptr(&eeti_ts_pm),
292e32d7f1bSDaniel Mack 		.of_match_table = of_match_ptr(of_eeti_ts_match),
29310494dceSDaniel Mack 	},
294923366e9SUwe Kleine-König 	.probe_new = eeti_ts_probe,
29510494dceSDaniel Mack 	.id_table = eeti_ts_id,
29610494dceSDaniel Mack };
29710494dceSDaniel Mack 
2981b92c1cfSAxel Lin module_i2c_driver(eeti_ts_driver);
29910494dceSDaniel Mack 
30010494dceSDaniel Mack MODULE_DESCRIPTION("EETI Touchscreen driver");
301fd8135b6SDaniel Mack MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
30210494dceSDaniel Mack MODULE_LICENSE("GPL");
303