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;
310378008aSDmitry Torokhov 	bool running;
3210494dceSDaniel Mack };
3310494dceSDaniel Mack 
3410494dceSDaniel Mack #define EETI_TS_BITDEPTH	(11)
3510494dceSDaniel Mack #define EETI_MAXVAL		((1 << (EETI_TS_BITDEPTH + 1)) - 1)
3610494dceSDaniel Mack 
37272c03bbSDmitry Torokhov #define REPORT_BIT_PRESSED	BIT(0)
38272c03bbSDmitry Torokhov #define REPORT_BIT_AD0		BIT(1)
39272c03bbSDmitry Torokhov #define REPORT_BIT_AD1		BIT(2)
40272c03bbSDmitry Torokhov #define REPORT_BIT_HAS_PRESSURE	BIT(6)
4110494dceSDaniel Mack #define REPORT_RES_BITS(v)	(((v) >> 1) + EETI_TS_BITDEPTH)
4210494dceSDaniel Mack 
430378008aSDmitry Torokhov static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
4410494dceSDaniel Mack {
450378008aSDmitry Torokhov 	unsigned int res;
460378008aSDmitry Torokhov 	u16 x, y;
4710494dceSDaniel Mack 
4810494dceSDaniel Mack 	res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
49e9f66cdbSDmitry Torokhov 
50e9f66cdbSDmitry Torokhov 	x = get_unaligned_be16(&buf[1]);
51e9f66cdbSDmitry Torokhov 	y = get_unaligned_be16(&buf[3]);
5210494dceSDaniel Mack 
5310494dceSDaniel Mack 	/* fix the range to 11 bits */
5410494dceSDaniel Mack 	x >>= res - EETI_TS_BITDEPTH;
5510494dceSDaniel Mack 	y >>= res - EETI_TS_BITDEPTH;
5610494dceSDaniel Mack 
5710494dceSDaniel Mack 	if (buf[0] & REPORT_BIT_HAS_PRESSURE)
58720bebdfSDmitry Torokhov 		input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
5910494dceSDaniel Mack 
60a114cbd0SDaniel Mack 	touchscreen_report_pos(eeti->input, &eeti->props, x, y, false);
610378008aSDmitry Torokhov 	input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
62720bebdfSDmitry Torokhov 	input_sync(eeti->input);
6310494dceSDaniel Mack }
6410494dceSDaniel Mack 
6510494dceSDaniel Mack static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
6610494dceSDaniel Mack {
67720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = dev_id;
680378008aSDmitry Torokhov 	int len;
690378008aSDmitry Torokhov 	int error;
700378008aSDmitry Torokhov 	char buf[6];
7110494dceSDaniel Mack 
720378008aSDmitry Torokhov 	do {
730378008aSDmitry Torokhov 		len = i2c_master_recv(eeti->client, buf, sizeof(buf));
740378008aSDmitry Torokhov 		if (len != sizeof(buf)) {
750378008aSDmitry Torokhov 			error = len < 0 ? len : -EIO;
760378008aSDmitry Torokhov 			dev_err(&eeti->client->dev,
770378008aSDmitry Torokhov 				"failed to read touchscreen data: %d\n",
780378008aSDmitry Torokhov 				error);
790378008aSDmitry Torokhov 			break;
800378008aSDmitry Torokhov 		}
810378008aSDmitry Torokhov 
820378008aSDmitry Torokhov 		if (buf[0] & 0x80) {
830378008aSDmitry Torokhov 			/* Motion packet */
840378008aSDmitry Torokhov 			eeti_ts_report_event(eeti, buf);
850378008aSDmitry Torokhov 		}
86d99caa47SDmitry Torokhov 	} while (eeti->running &&
87d99caa47SDmitry Torokhov 		 eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio));
8810494dceSDaniel Mack 
8910494dceSDaniel Mack 	return IRQ_HANDLED;
9010494dceSDaniel Mack }
9110494dceSDaniel Mack 
92720bebdfSDmitry Torokhov static void eeti_ts_start(struct eeti_ts *eeti)
9310494dceSDaniel Mack {
940378008aSDmitry Torokhov 	eeti->running = true;
950378008aSDmitry Torokhov 	wmb();
962d388499SDmitry Torokhov 	enable_irq(eeti->client->irq);
977fbef0d1SDaniel Mack }
987fbef0d1SDaniel Mack 
99720bebdfSDmitry Torokhov static void eeti_ts_stop(struct eeti_ts *eeti)
1007fbef0d1SDaniel Mack {
1010378008aSDmitry Torokhov 	eeti->running = false;
1020378008aSDmitry Torokhov 	wmb();
1032d388499SDmitry Torokhov 	disable_irq(eeti->client->irq);
1047fbef0d1SDaniel Mack }
1057fbef0d1SDaniel Mack 
1067fbef0d1SDaniel Mack static int eeti_ts_open(struct input_dev *dev)
1077fbef0d1SDaniel Mack {
108720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = input_get_drvdata(dev);
1097fbef0d1SDaniel Mack 
110720bebdfSDmitry Torokhov 	eeti_ts_start(eeti);
11110494dceSDaniel Mack 
11210494dceSDaniel Mack 	return 0;
11310494dceSDaniel Mack }
11410494dceSDaniel Mack 
11510494dceSDaniel Mack static void eeti_ts_close(struct input_dev *dev)
11610494dceSDaniel Mack {
117720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = input_get_drvdata(dev);
11810494dceSDaniel Mack 
119720bebdfSDmitry Torokhov 	eeti_ts_stop(eeti);
12010494dceSDaniel Mack }
12110494dceSDaniel Mack 
1225298cc4cSBill Pemberton static int eeti_ts_probe(struct i2c_client *client,
12310494dceSDaniel Mack 			 const struct i2c_device_id *idp)
12410494dceSDaniel Mack {
1256f9fab69SDmitry Torokhov 	struct device *dev = &client->dev;
126720bebdfSDmitry Torokhov 	struct eeti_ts *eeti;
12710494dceSDaniel Mack 	struct input_dev *input;
1286f9fab69SDmitry Torokhov 	int error;
12910494dceSDaniel Mack 
1307fbef0d1SDaniel Mack 	/*
1317fbef0d1SDaniel Mack 	 * In contrast to what's described in the datasheet, there seems
13210494dceSDaniel Mack 	 * to be no way of probing the presence of that device using I2C
13310494dceSDaniel Mack 	 * commands. So we need to blindly believe it is there, and wait
1347fbef0d1SDaniel Mack 	 * for interrupts to occur.
1357fbef0d1SDaniel Mack 	 */
13610494dceSDaniel Mack 
1376f9fab69SDmitry Torokhov 	eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
138720bebdfSDmitry Torokhov 	if (!eeti) {
1396f9fab69SDmitry Torokhov 		dev_err(dev, "failed to allocate driver data\n");
140ad03ae44SGuenter Roeck 		return -ENOMEM;
14110494dceSDaniel Mack 	}
14210494dceSDaniel Mack 
1436f9fab69SDmitry Torokhov 	input = devm_input_allocate_device(dev);
14410494dceSDaniel Mack 	if (!input) {
1456f9fab69SDmitry Torokhov 		dev_err(dev, "Failed to allocate input device.\n");
1466f9fab69SDmitry Torokhov 		return -ENOMEM;
14710494dceSDaniel Mack 	}
14810494dceSDaniel Mack 
14942e02a6aSDmitry Torokhov 	input_set_capability(input, EV_KEY, BTN_TOUCH);
15010494dceSDaniel Mack 
15110494dceSDaniel Mack 	input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
15210494dceSDaniel Mack 	input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
15310494dceSDaniel Mack 	input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
15410494dceSDaniel Mack 
155a114cbd0SDaniel Mack 	touchscreen_parse_properties(input, false, &eeti->props);
156a114cbd0SDaniel Mack 
15710494dceSDaniel Mack 	input->name = client->name;
15810494dceSDaniel Mack 	input->id.bustype = BUS_I2C;
15910494dceSDaniel Mack 	input->open = eeti_ts_open;
16010494dceSDaniel Mack 	input->close = eeti_ts_close;
16110494dceSDaniel Mack 
162720bebdfSDmitry Torokhov 	eeti->client = client;
163720bebdfSDmitry Torokhov 	eeti->input = input;
16410494dceSDaniel Mack 
165d99caa47SDmitry Torokhov 	eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
166d99caa47SDmitry Torokhov 	if (IS_ERR(eeti->attn_gpio))
167d99caa47SDmitry Torokhov 		return PTR_ERR(eeti->attn_gpio);
16825a70e38SDaniel Mack 
169720bebdfSDmitry Torokhov 	i2c_set_clientdata(client, eeti);
170720bebdfSDmitry Torokhov 	input_set_drvdata(input, eeti);
17110494dceSDaniel Mack 
1720378008aSDmitry Torokhov 	error = devm_request_threaded_irq(dev, client->irq,
1730378008aSDmitry Torokhov 					  NULL, eeti_ts_isr,
174d422be5fSDmitry Torokhov 					  IRQF_ONESHOT,
175720bebdfSDmitry Torokhov 					  client->name, eeti);
1766f9fab69SDmitry Torokhov 	if (error) {
1776f9fab69SDmitry Torokhov 		dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
1786f9fab69SDmitry Torokhov 			error);
1796f9fab69SDmitry Torokhov 		return error;
18010494dceSDaniel Mack 	}
18110494dceSDaniel Mack 
1827fbef0d1SDaniel Mack 	/*
1837fbef0d1SDaniel Mack 	 * Disable the device for now. It will be enabled once the
1847fbef0d1SDaniel Mack 	 * input device is opened.
1857fbef0d1SDaniel Mack 	 */
186720bebdfSDmitry Torokhov 	eeti_ts_stop(eeti);
18710494dceSDaniel Mack 
1880378008aSDmitry Torokhov 	error = input_register_device(input);
1890378008aSDmitry Torokhov 	if (error)
1900378008aSDmitry Torokhov 		return error;
1910378008aSDmitry Torokhov 
19210494dceSDaniel Mack 	return 0;
19310494dceSDaniel Mack }
19410494dceSDaniel Mack 
19502b6a58bSJingoo Han static int __maybe_unused eeti_ts_suspend(struct device *dev)
19610494dceSDaniel Mack {
19785012fffSMark Brown 	struct i2c_client *client = to_i2c_client(dev);
198720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = i2c_get_clientdata(client);
199720bebdfSDmitry Torokhov 	struct input_dev *input_dev = eeti->input;
2007fbef0d1SDaniel Mack 
2017fbef0d1SDaniel Mack 	mutex_lock(&input_dev->mutex);
2027fbef0d1SDaniel Mack 
2037fbef0d1SDaniel Mack 	if (input_dev->users)
204720bebdfSDmitry Torokhov 		eeti_ts_stop(eeti);
2057fbef0d1SDaniel Mack 
2067fbef0d1SDaniel Mack 	mutex_unlock(&input_dev->mutex);
20710494dceSDaniel Mack 
20810494dceSDaniel Mack 	if (device_may_wakeup(&client->dev))
2092d388499SDmitry Torokhov 		enable_irq_wake(client->irq);
21010494dceSDaniel Mack 
21110494dceSDaniel Mack 	return 0;
21210494dceSDaniel Mack }
21310494dceSDaniel Mack 
21402b6a58bSJingoo Han static int __maybe_unused eeti_ts_resume(struct device *dev)
21510494dceSDaniel Mack {
21685012fffSMark Brown 	struct i2c_client *client = to_i2c_client(dev);
217720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = i2c_get_clientdata(client);
218720bebdfSDmitry Torokhov 	struct input_dev *input_dev = eeti->input;
21910494dceSDaniel Mack 
22010494dceSDaniel Mack 	if (device_may_wakeup(&client->dev))
2212d388499SDmitry Torokhov 		disable_irq_wake(client->irq);
22210494dceSDaniel Mack 
2237fbef0d1SDaniel Mack 	mutex_lock(&input_dev->mutex);
2247fbef0d1SDaniel Mack 
2257fbef0d1SDaniel Mack 	if (input_dev->users)
226720bebdfSDmitry Torokhov 		eeti_ts_start(eeti);
2277fbef0d1SDaniel Mack 
2287fbef0d1SDaniel Mack 	mutex_unlock(&input_dev->mutex);
2297fbef0d1SDaniel Mack 
23010494dceSDaniel Mack 	return 0;
23110494dceSDaniel Mack }
23285012fffSMark Brown 
23385012fffSMark Brown static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
23410494dceSDaniel Mack 
23510494dceSDaniel Mack static const struct i2c_device_id eeti_ts_id[] = {
23610494dceSDaniel Mack 	{ "eeti_ts", 0 },
23710494dceSDaniel Mack 	{ }
23810494dceSDaniel Mack };
23910494dceSDaniel Mack MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
24010494dceSDaniel Mack 
241e32d7f1bSDaniel Mack #ifdef CONFIG_OF
242e32d7f1bSDaniel Mack static const struct of_device_id of_eeti_ts_match[] = {
243e32d7f1bSDaniel Mack 	{ .compatible = "eeti,exc3000-i2c", },
244e32d7f1bSDaniel Mack 	{ }
245e32d7f1bSDaniel Mack };
246e32d7f1bSDaniel Mack #endif
247e32d7f1bSDaniel Mack 
24810494dceSDaniel Mack static struct i2c_driver eeti_ts_driver = {
24910494dceSDaniel Mack 	.driver = {
25010494dceSDaniel Mack 		.name = "eeti_ts",
25185012fffSMark Brown 		.pm = &eeti_ts_pm,
252e32d7f1bSDaniel Mack 		.of_match_table = of_match_ptr(of_eeti_ts_match),
25310494dceSDaniel Mack 	},
25410494dceSDaniel Mack 	.probe = eeti_ts_probe,
25510494dceSDaniel Mack 	.id_table = eeti_ts_id,
25610494dceSDaniel Mack };
25710494dceSDaniel Mack 
2581b92c1cfSAxel Lin module_i2c_driver(eeti_ts_driver);
25910494dceSDaniel Mack 
26010494dceSDaniel Mack MODULE_DESCRIPTION("EETI Touchscreen driver");
261fd8135b6SDaniel Mack MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
26210494dceSDaniel Mack MODULE_LICENSE("GPL");
263