110494dceSDaniel Mack /*
210494dceSDaniel Mack  * Touch Screen driver for EETI's I2C connected touch screen panels
310494dceSDaniel Mack  *   Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
410494dceSDaniel Mack  *
510494dceSDaniel Mack  * See EETI's software guide for the protocol specification:
610494dceSDaniel Mack  *   http://home.eeti.com.tw/web20/eg/guide.htm
710494dceSDaniel Mack  *
810494dceSDaniel Mack  * Based on migor_ts.c
910494dceSDaniel Mack  *   Copyright (c) 2008 Magnus Damm
1010494dceSDaniel Mack  *   Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
1110494dceSDaniel Mack  *
1210494dceSDaniel Mack  * This file is free software; you can redistribute it and/or
1310494dceSDaniel Mack  * modify it under the terms of the GNU  General Public
1410494dceSDaniel Mack  * License as published by the Free Software Foundation; either
1510494dceSDaniel Mack  * version 2 of the License, or (at your option) any later version.
1610494dceSDaniel Mack  *
1710494dceSDaniel Mack  * This file is distributed in the hope that it will be useful,
1810494dceSDaniel Mack  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1910494dceSDaniel Mack  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2010494dceSDaniel Mack  *  General Public License for more details.
2110494dceSDaniel Mack  *
2210494dceSDaniel Mack  * You should have received a copy of the GNU General Public
2310494dceSDaniel Mack  * License along with this library; if not, write to the Free Software
2410494dceSDaniel Mack  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2510494dceSDaniel Mack  */
2610494dceSDaniel Mack 
2710494dceSDaniel Mack #include <linux/module.h>
2810494dceSDaniel Mack #include <linux/moduleparam.h>
2910494dceSDaniel Mack #include <linux/kernel.h>
3010494dceSDaniel Mack #include <linux/input.h>
3110494dceSDaniel Mack #include <linux/interrupt.h>
3210494dceSDaniel Mack #include <linux/i2c.h>
3310494dceSDaniel Mack #include <linux/timer.h>
34d99caa47SDmitry Torokhov #include <linux/gpio/consumer.h>
35e32d7f1bSDaniel Mack #include <linux/of.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
37e9f66cdbSDmitry Torokhov #include <asm/unaligned.h>
3810494dceSDaniel Mack 
3990ab5ee9SRusty Russell static bool flip_x;
4010494dceSDaniel Mack module_param(flip_x, bool, 0644);
4110494dceSDaniel Mack MODULE_PARM_DESC(flip_x, "flip x coordinate");
4210494dceSDaniel Mack 
4390ab5ee9SRusty Russell static bool flip_y;
4410494dceSDaniel Mack module_param(flip_y, bool, 0644);
4510494dceSDaniel Mack MODULE_PARM_DESC(flip_y, "flip y coordinate");
4610494dceSDaniel Mack 
47720bebdfSDmitry Torokhov struct eeti_ts {
4810494dceSDaniel Mack 	struct i2c_client *client;
4910494dceSDaniel Mack 	struct input_dev *input;
50d99caa47SDmitry Torokhov 	struct gpio_desc *attn_gpio;
510378008aSDmitry Torokhov 	bool running;
5210494dceSDaniel Mack };
5310494dceSDaniel Mack 
5410494dceSDaniel Mack #define EETI_TS_BITDEPTH	(11)
5510494dceSDaniel Mack #define EETI_MAXVAL		((1 << (EETI_TS_BITDEPTH + 1)) - 1)
5610494dceSDaniel Mack 
57272c03bbSDmitry Torokhov #define REPORT_BIT_PRESSED	BIT(0)
58272c03bbSDmitry Torokhov #define REPORT_BIT_AD0		BIT(1)
59272c03bbSDmitry Torokhov #define REPORT_BIT_AD1		BIT(2)
60272c03bbSDmitry Torokhov #define REPORT_BIT_HAS_PRESSURE	BIT(6)
6110494dceSDaniel Mack #define REPORT_RES_BITS(v)	(((v) >> 1) + EETI_TS_BITDEPTH)
6210494dceSDaniel Mack 
630378008aSDmitry Torokhov static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
6410494dceSDaniel Mack {
650378008aSDmitry Torokhov 	unsigned int res;
660378008aSDmitry Torokhov 	u16 x, y;
6710494dceSDaniel Mack 
6810494dceSDaniel Mack 	res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
69e9f66cdbSDmitry Torokhov 
70e9f66cdbSDmitry Torokhov 	x = get_unaligned_be16(&buf[1]);
71e9f66cdbSDmitry Torokhov 	y = get_unaligned_be16(&buf[3]);
7210494dceSDaniel Mack 
7310494dceSDaniel Mack 	/* fix the range to 11 bits */
7410494dceSDaniel Mack 	x >>= res - EETI_TS_BITDEPTH;
7510494dceSDaniel Mack 	y >>= res - EETI_TS_BITDEPTH;
7610494dceSDaniel Mack 
7710494dceSDaniel Mack 	if (flip_x)
7810494dceSDaniel Mack 		x = EETI_MAXVAL - x;
7910494dceSDaniel Mack 
8010494dceSDaniel Mack 	if (flip_y)
8110494dceSDaniel Mack 		y = EETI_MAXVAL - y;
8210494dceSDaniel Mack 
8310494dceSDaniel Mack 	if (buf[0] & REPORT_BIT_HAS_PRESSURE)
84720bebdfSDmitry Torokhov 		input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
8510494dceSDaniel Mack 
86720bebdfSDmitry Torokhov 	input_report_abs(eeti->input, ABS_X, x);
87720bebdfSDmitry Torokhov 	input_report_abs(eeti->input, ABS_Y, y);
880378008aSDmitry Torokhov 	input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
89720bebdfSDmitry Torokhov 	input_sync(eeti->input);
9010494dceSDaniel Mack }
9110494dceSDaniel Mack 
9210494dceSDaniel Mack static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
9310494dceSDaniel Mack {
94720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = dev_id;
950378008aSDmitry Torokhov 	int len;
960378008aSDmitry Torokhov 	int error;
970378008aSDmitry Torokhov 	char buf[6];
9810494dceSDaniel Mack 
990378008aSDmitry Torokhov 	do {
1000378008aSDmitry Torokhov 		len = i2c_master_recv(eeti->client, buf, sizeof(buf));
1010378008aSDmitry Torokhov 		if (len != sizeof(buf)) {
1020378008aSDmitry Torokhov 			error = len < 0 ? len : -EIO;
1030378008aSDmitry Torokhov 			dev_err(&eeti->client->dev,
1040378008aSDmitry Torokhov 				"failed to read touchscreen data: %d\n",
1050378008aSDmitry Torokhov 				error);
1060378008aSDmitry Torokhov 			break;
1070378008aSDmitry Torokhov 		}
1080378008aSDmitry Torokhov 
1090378008aSDmitry Torokhov 		if (buf[0] & 0x80) {
1100378008aSDmitry Torokhov 			/* Motion packet */
1110378008aSDmitry Torokhov 			eeti_ts_report_event(eeti, buf);
1120378008aSDmitry Torokhov 		}
113d99caa47SDmitry Torokhov 	} while (eeti->running &&
114d99caa47SDmitry Torokhov 		 eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio));
11510494dceSDaniel Mack 
11610494dceSDaniel Mack 	return IRQ_HANDLED;
11710494dceSDaniel Mack }
11810494dceSDaniel Mack 
119720bebdfSDmitry Torokhov static void eeti_ts_start(struct eeti_ts *eeti)
12010494dceSDaniel Mack {
1210378008aSDmitry Torokhov 	eeti->running = true;
1220378008aSDmitry Torokhov 	wmb();
1232d388499SDmitry Torokhov 	enable_irq(eeti->client->irq);
1247fbef0d1SDaniel Mack }
1257fbef0d1SDaniel Mack 
126720bebdfSDmitry Torokhov static void eeti_ts_stop(struct eeti_ts *eeti)
1277fbef0d1SDaniel Mack {
1280378008aSDmitry Torokhov 	eeti->running = false;
1290378008aSDmitry Torokhov 	wmb();
1302d388499SDmitry Torokhov 	disable_irq(eeti->client->irq);
1317fbef0d1SDaniel Mack }
1327fbef0d1SDaniel Mack 
1337fbef0d1SDaniel Mack static int eeti_ts_open(struct input_dev *dev)
1347fbef0d1SDaniel Mack {
135720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = input_get_drvdata(dev);
1367fbef0d1SDaniel Mack 
137720bebdfSDmitry Torokhov 	eeti_ts_start(eeti);
13810494dceSDaniel Mack 
13910494dceSDaniel Mack 	return 0;
14010494dceSDaniel Mack }
14110494dceSDaniel Mack 
14210494dceSDaniel Mack static void eeti_ts_close(struct input_dev *dev)
14310494dceSDaniel Mack {
144720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = input_get_drvdata(dev);
14510494dceSDaniel Mack 
146720bebdfSDmitry Torokhov 	eeti_ts_stop(eeti);
14710494dceSDaniel Mack }
14810494dceSDaniel Mack 
1495298cc4cSBill Pemberton static int eeti_ts_probe(struct i2c_client *client,
15010494dceSDaniel Mack 			 const struct i2c_device_id *idp)
15110494dceSDaniel Mack {
1526f9fab69SDmitry Torokhov 	struct device *dev = &client->dev;
153720bebdfSDmitry Torokhov 	struct eeti_ts *eeti;
15410494dceSDaniel Mack 	struct input_dev *input;
1556f9fab69SDmitry Torokhov 	int error;
15610494dceSDaniel Mack 
1577fbef0d1SDaniel Mack 	/*
1587fbef0d1SDaniel Mack 	 * In contrast to what's described in the datasheet, there seems
15910494dceSDaniel Mack 	 * to be no way of probing the presence of that device using I2C
16010494dceSDaniel Mack 	 * commands. So we need to blindly believe it is there, and wait
1617fbef0d1SDaniel Mack 	 * for interrupts to occur.
1627fbef0d1SDaniel Mack 	 */
16310494dceSDaniel Mack 
1646f9fab69SDmitry Torokhov 	eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
165720bebdfSDmitry Torokhov 	if (!eeti) {
1666f9fab69SDmitry Torokhov 		dev_err(dev, "failed to allocate driver data\n");
167ad03ae44SGuenter Roeck 		return -ENOMEM;
16810494dceSDaniel Mack 	}
16910494dceSDaniel Mack 
1706f9fab69SDmitry Torokhov 	input = devm_input_allocate_device(dev);
17110494dceSDaniel Mack 	if (!input) {
1726f9fab69SDmitry Torokhov 		dev_err(dev, "Failed to allocate input device.\n");
1736f9fab69SDmitry Torokhov 		return -ENOMEM;
17410494dceSDaniel Mack 	}
17510494dceSDaniel Mack 
17642e02a6aSDmitry Torokhov 	input_set_capability(input, EV_KEY, BTN_TOUCH);
17710494dceSDaniel Mack 
17810494dceSDaniel Mack 	input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
17910494dceSDaniel Mack 	input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
18010494dceSDaniel Mack 	input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
18110494dceSDaniel Mack 
18210494dceSDaniel Mack 	input->name = client->name;
18310494dceSDaniel Mack 	input->id.bustype = BUS_I2C;
18410494dceSDaniel Mack 	input->open = eeti_ts_open;
18510494dceSDaniel Mack 	input->close = eeti_ts_close;
18610494dceSDaniel Mack 
187720bebdfSDmitry Torokhov 	eeti->client = client;
188720bebdfSDmitry Torokhov 	eeti->input = input;
18910494dceSDaniel Mack 
190d99caa47SDmitry Torokhov 	eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
191d99caa47SDmitry Torokhov 	if (IS_ERR(eeti->attn_gpio))
192d99caa47SDmitry Torokhov 		return PTR_ERR(eeti->attn_gpio);
19325a70e38SDaniel Mack 
194720bebdfSDmitry Torokhov 	i2c_set_clientdata(client, eeti);
195720bebdfSDmitry Torokhov 	input_set_drvdata(input, eeti);
19610494dceSDaniel Mack 
1970378008aSDmitry Torokhov 	error = devm_request_threaded_irq(dev, client->irq,
1980378008aSDmitry Torokhov 					  NULL, eeti_ts_isr,
199d422be5fSDmitry Torokhov 					  IRQF_ONESHOT,
200720bebdfSDmitry Torokhov 					  client->name, eeti);
2016f9fab69SDmitry Torokhov 	if (error) {
2026f9fab69SDmitry Torokhov 		dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
2036f9fab69SDmitry Torokhov 			error);
2046f9fab69SDmitry Torokhov 		return error;
20510494dceSDaniel Mack 	}
20610494dceSDaniel Mack 
2077fbef0d1SDaniel Mack 	/*
2087fbef0d1SDaniel Mack 	 * Disable the device for now. It will be enabled once the
2097fbef0d1SDaniel Mack 	 * input device is opened.
2107fbef0d1SDaniel Mack 	 */
211720bebdfSDmitry Torokhov 	eeti_ts_stop(eeti);
21210494dceSDaniel Mack 
2130378008aSDmitry Torokhov 	error = input_register_device(input);
2140378008aSDmitry Torokhov 	if (error)
2150378008aSDmitry Torokhov 		return error;
2160378008aSDmitry Torokhov 
21710494dceSDaniel Mack 	return 0;
21810494dceSDaniel Mack }
21910494dceSDaniel Mack 
22002b6a58bSJingoo Han static int __maybe_unused eeti_ts_suspend(struct device *dev)
22110494dceSDaniel Mack {
22285012fffSMark Brown 	struct i2c_client *client = to_i2c_client(dev);
223720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = i2c_get_clientdata(client);
224720bebdfSDmitry Torokhov 	struct input_dev *input_dev = eeti->input;
2257fbef0d1SDaniel Mack 
2267fbef0d1SDaniel Mack 	mutex_lock(&input_dev->mutex);
2277fbef0d1SDaniel Mack 
2287fbef0d1SDaniel Mack 	if (input_dev->users)
229720bebdfSDmitry Torokhov 		eeti_ts_stop(eeti);
2307fbef0d1SDaniel Mack 
2317fbef0d1SDaniel Mack 	mutex_unlock(&input_dev->mutex);
23210494dceSDaniel Mack 
23310494dceSDaniel Mack 	if (device_may_wakeup(&client->dev))
2342d388499SDmitry Torokhov 		enable_irq_wake(client->irq);
23510494dceSDaniel Mack 
23610494dceSDaniel Mack 	return 0;
23710494dceSDaniel Mack }
23810494dceSDaniel Mack 
23902b6a58bSJingoo Han static int __maybe_unused eeti_ts_resume(struct device *dev)
24010494dceSDaniel Mack {
24185012fffSMark Brown 	struct i2c_client *client = to_i2c_client(dev);
242720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = i2c_get_clientdata(client);
243720bebdfSDmitry Torokhov 	struct input_dev *input_dev = eeti->input;
24410494dceSDaniel Mack 
24510494dceSDaniel Mack 	if (device_may_wakeup(&client->dev))
2462d388499SDmitry Torokhov 		disable_irq_wake(client->irq);
24710494dceSDaniel Mack 
2487fbef0d1SDaniel Mack 	mutex_lock(&input_dev->mutex);
2497fbef0d1SDaniel Mack 
2507fbef0d1SDaniel Mack 	if (input_dev->users)
251720bebdfSDmitry Torokhov 		eeti_ts_start(eeti);
2527fbef0d1SDaniel Mack 
2537fbef0d1SDaniel Mack 	mutex_unlock(&input_dev->mutex);
2547fbef0d1SDaniel Mack 
25510494dceSDaniel Mack 	return 0;
25610494dceSDaniel Mack }
25785012fffSMark Brown 
25885012fffSMark Brown static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
25910494dceSDaniel Mack 
26010494dceSDaniel Mack static const struct i2c_device_id eeti_ts_id[] = {
26110494dceSDaniel Mack 	{ "eeti_ts", 0 },
26210494dceSDaniel Mack 	{ }
26310494dceSDaniel Mack };
26410494dceSDaniel Mack MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
26510494dceSDaniel Mack 
266e32d7f1bSDaniel Mack #ifdef CONFIG_OF
267e32d7f1bSDaniel Mack static const struct of_device_id of_eeti_ts_match[] = {
268e32d7f1bSDaniel Mack 	{ .compatible = "eeti,exc3000-i2c", },
269e32d7f1bSDaniel Mack 	{ }
270e32d7f1bSDaniel Mack };
271e32d7f1bSDaniel Mack #endif
272e32d7f1bSDaniel Mack 
27310494dceSDaniel Mack static struct i2c_driver eeti_ts_driver = {
27410494dceSDaniel Mack 	.driver = {
27510494dceSDaniel Mack 		.name = "eeti_ts",
27685012fffSMark Brown 		.pm = &eeti_ts_pm,
277e32d7f1bSDaniel Mack 		.of_match_table = of_match_ptr(of_eeti_ts_match),
27810494dceSDaniel Mack 	},
27910494dceSDaniel Mack 	.probe = eeti_ts_probe,
28010494dceSDaniel Mack 	.id_table = eeti_ts_id,
28110494dceSDaniel Mack };
28210494dceSDaniel Mack 
2831b92c1cfSAxel Lin module_i2c_driver(eeti_ts_driver);
28410494dceSDaniel Mack 
28510494dceSDaniel Mack MODULE_DESCRIPTION("EETI Touchscreen driver");
28610494dceSDaniel Mack MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
28710494dceSDaniel Mack MODULE_LICENSE("GPL");
288