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>
3410494dceSDaniel Mack #include <linux/gpio.h>
3525a70e38SDaniel Mack #include <linux/input/eeti_ts.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;
5010494dceSDaniel Mack 	struct work_struct work;
5110494dceSDaniel Mack 	struct mutex mutex;
524eef6cbfSArnd Bergmann 	int irq_gpio, irq, irq_active_high;
5310494dceSDaniel Mack };
5410494dceSDaniel Mack 
5510494dceSDaniel Mack #define EETI_TS_BITDEPTH	(11)
5610494dceSDaniel Mack #define EETI_MAXVAL		((1 << (EETI_TS_BITDEPTH + 1)) - 1)
5710494dceSDaniel Mack 
58272c03bbSDmitry Torokhov #define REPORT_BIT_PRESSED	BIT(0)
59272c03bbSDmitry Torokhov #define REPORT_BIT_AD0		BIT(1)
60272c03bbSDmitry Torokhov #define REPORT_BIT_AD1		BIT(2)
61272c03bbSDmitry Torokhov #define REPORT_BIT_HAS_PRESSURE	BIT(6)
6210494dceSDaniel Mack #define REPORT_RES_BITS(v)	(((v) >> 1) + EETI_TS_BITDEPTH)
6310494dceSDaniel Mack 
64720bebdfSDmitry Torokhov static inline int eeti_ts_irq_active(struct eeti_ts *eeti)
6525a70e38SDaniel Mack {
66720bebdfSDmitry Torokhov 	return gpio_get_value(eeti->irq_gpio) == eeti->irq_active_high;
6725a70e38SDaniel Mack }
6825a70e38SDaniel Mack 
6910494dceSDaniel Mack static void eeti_ts_read(struct work_struct *work)
7010494dceSDaniel Mack {
7110494dceSDaniel Mack 	char buf[6];
7210494dceSDaniel Mack 	unsigned int x, y, res, pressed, to = 100;
73720bebdfSDmitry Torokhov 	struct eeti_ts *eeti =
74720bebdfSDmitry Torokhov 		container_of(work, struct eeti_ts, work);
7510494dceSDaniel Mack 
76720bebdfSDmitry Torokhov 	mutex_lock(&eeti->mutex);
7710494dceSDaniel Mack 
78720bebdfSDmitry Torokhov 	while (eeti_ts_irq_active(eeti) && --to)
79720bebdfSDmitry Torokhov 		i2c_master_recv(eeti->client, buf, sizeof(buf));
8010494dceSDaniel Mack 
8110494dceSDaniel Mack 	if (!to) {
82720bebdfSDmitry Torokhov 		dev_err(&eeti->client->dev,
8310494dceSDaniel Mack 			"unable to clear IRQ - line stuck?\n");
8410494dceSDaniel Mack 		goto out;
8510494dceSDaniel Mack 	}
8610494dceSDaniel Mack 
8710494dceSDaniel Mack 	/* drop non-report packets */
8810494dceSDaniel Mack 	if (!(buf[0] & 0x80))
8910494dceSDaniel Mack 		goto out;
9010494dceSDaniel Mack 
9110494dceSDaniel Mack 	pressed = buf[0] & REPORT_BIT_PRESSED;
9210494dceSDaniel Mack 	res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
93e9f66cdbSDmitry Torokhov 
94e9f66cdbSDmitry Torokhov 	x = get_unaligned_be16(&buf[1]);
95e9f66cdbSDmitry Torokhov 	y = get_unaligned_be16(&buf[3]);
9610494dceSDaniel Mack 
9710494dceSDaniel Mack 	/* fix the range to 11 bits */
9810494dceSDaniel Mack 	x >>= res - EETI_TS_BITDEPTH;
9910494dceSDaniel Mack 	y >>= res - EETI_TS_BITDEPTH;
10010494dceSDaniel Mack 
10110494dceSDaniel Mack 	if (flip_x)
10210494dceSDaniel Mack 		x = EETI_MAXVAL - x;
10310494dceSDaniel Mack 
10410494dceSDaniel Mack 	if (flip_y)
10510494dceSDaniel Mack 		y = EETI_MAXVAL - y;
10610494dceSDaniel Mack 
10710494dceSDaniel Mack 	if (buf[0] & REPORT_BIT_HAS_PRESSURE)
108720bebdfSDmitry Torokhov 		input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
10910494dceSDaniel Mack 
110720bebdfSDmitry Torokhov 	input_report_abs(eeti->input, ABS_X, x);
111720bebdfSDmitry Torokhov 	input_report_abs(eeti->input, ABS_Y, y);
112720bebdfSDmitry Torokhov 	input_report_key(eeti->input, BTN_TOUCH, !!pressed);
113720bebdfSDmitry Torokhov 	input_sync(eeti->input);
11410494dceSDaniel Mack 
11510494dceSDaniel Mack out:
116720bebdfSDmitry Torokhov 	mutex_unlock(&eeti->mutex);
11710494dceSDaniel Mack }
11810494dceSDaniel Mack 
11910494dceSDaniel Mack static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
12010494dceSDaniel Mack {
121720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = dev_id;
12210494dceSDaniel Mack 
12310494dceSDaniel Mack 	 /* postpone I2C transactions as we are atomic */
124720bebdfSDmitry Torokhov 	schedule_work(&eeti->work);
12510494dceSDaniel Mack 
12610494dceSDaniel Mack 	return IRQ_HANDLED;
12710494dceSDaniel Mack }
12810494dceSDaniel Mack 
129720bebdfSDmitry Torokhov static void eeti_ts_start(struct eeti_ts *eeti)
13010494dceSDaniel Mack {
131720bebdfSDmitry Torokhov 	enable_irq(eeti->irq);
13210494dceSDaniel Mack 
13310494dceSDaniel Mack 	/* Read the events once to arm the IRQ */
134720bebdfSDmitry Torokhov 	eeti_ts_read(&eeti->work);
1357fbef0d1SDaniel Mack }
1367fbef0d1SDaniel Mack 
137720bebdfSDmitry Torokhov static void eeti_ts_stop(struct eeti_ts *eeti)
1387fbef0d1SDaniel Mack {
139720bebdfSDmitry Torokhov 	disable_irq(eeti->irq);
140720bebdfSDmitry Torokhov 	cancel_work_sync(&eeti->work);
1417fbef0d1SDaniel Mack }
1427fbef0d1SDaniel Mack 
1437fbef0d1SDaniel Mack static int eeti_ts_open(struct input_dev *dev)
1447fbef0d1SDaniel Mack {
145720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = input_get_drvdata(dev);
1467fbef0d1SDaniel Mack 
147720bebdfSDmitry Torokhov 	eeti_ts_start(eeti);
14810494dceSDaniel Mack 
14910494dceSDaniel Mack 	return 0;
15010494dceSDaniel Mack }
15110494dceSDaniel Mack 
15210494dceSDaniel Mack static void eeti_ts_close(struct input_dev *dev)
15310494dceSDaniel Mack {
154720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = input_get_drvdata(dev);
15510494dceSDaniel Mack 
156720bebdfSDmitry Torokhov 	eeti_ts_stop(eeti);
15710494dceSDaniel Mack }
15810494dceSDaniel Mack 
1595298cc4cSBill Pemberton static int eeti_ts_probe(struct i2c_client *client,
16010494dceSDaniel Mack 			 const struct i2c_device_id *idp)
16110494dceSDaniel Mack {
1626f9fab69SDmitry Torokhov 	struct device *dev = &client->dev;
1636f9fab69SDmitry Torokhov 	struct eeti_ts_platform_data *pdata = dev_get_platdata(dev);
164720bebdfSDmitry Torokhov 	struct eeti_ts *eeti;
16510494dceSDaniel Mack 	struct input_dev *input;
16625a70e38SDaniel Mack 	unsigned int irq_flags;
1676f9fab69SDmitry Torokhov 	int error;
16810494dceSDaniel Mack 
1697fbef0d1SDaniel Mack 	/*
1707fbef0d1SDaniel Mack 	 * In contrast to what's described in the datasheet, there seems
17110494dceSDaniel Mack 	 * to be no way of probing the presence of that device using I2C
17210494dceSDaniel Mack 	 * commands. So we need to blindly believe it is there, and wait
1737fbef0d1SDaniel Mack 	 * for interrupts to occur.
1747fbef0d1SDaniel Mack 	 */
17510494dceSDaniel Mack 
1766f9fab69SDmitry Torokhov 	eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
177720bebdfSDmitry Torokhov 	if (!eeti) {
1786f9fab69SDmitry Torokhov 		dev_err(dev, "failed to allocate driver data\n");
179ad03ae44SGuenter Roeck 		return -ENOMEM;
18010494dceSDaniel Mack 	}
18110494dceSDaniel Mack 
182720bebdfSDmitry Torokhov 	mutex_init(&eeti->mutex);
183720bebdfSDmitry Torokhov 
1846f9fab69SDmitry Torokhov 	input = devm_input_allocate_device(dev);
18510494dceSDaniel Mack 	if (!input) {
1866f9fab69SDmitry Torokhov 		dev_err(dev, "Failed to allocate input device.\n");
1876f9fab69SDmitry Torokhov 		return -ENOMEM;
18810494dceSDaniel Mack 	}
18910494dceSDaniel Mack 
19042e02a6aSDmitry Torokhov 	input_set_capability(input, EV_KEY, BTN_TOUCH);
19110494dceSDaniel Mack 
19210494dceSDaniel Mack 	input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
19310494dceSDaniel Mack 	input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
19410494dceSDaniel Mack 	input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
19510494dceSDaniel Mack 
19610494dceSDaniel Mack 	input->name = client->name;
19710494dceSDaniel Mack 	input->id.bustype = BUS_I2C;
19810494dceSDaniel Mack 	input->open = eeti_ts_open;
19910494dceSDaniel Mack 	input->close = eeti_ts_close;
20010494dceSDaniel Mack 
201720bebdfSDmitry Torokhov 	eeti->client = client;
202720bebdfSDmitry Torokhov 	eeti->input = input;
203720bebdfSDmitry Torokhov 	eeti->irq_gpio = pdata->irq_gpio;
204720bebdfSDmitry Torokhov 	eeti->irq = gpio_to_irq(pdata->irq_gpio);
20510494dceSDaniel Mack 
2066f9fab69SDmitry Torokhov 	error = devm_gpio_request_one(dev, pdata->irq_gpio, GPIOF_IN,
2076f9fab69SDmitry Torokhov 				      client->name);
2086f9fab69SDmitry Torokhov 	if (error)
2096f9fab69SDmitry Torokhov 		return error;
21025a70e38SDaniel Mack 
211720bebdfSDmitry Torokhov 	eeti->irq_active_high = pdata->irq_active_high;
21225a70e38SDaniel Mack 
213720bebdfSDmitry Torokhov 	irq_flags = eeti->irq_active_high ?
21425a70e38SDaniel Mack 		IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
21525a70e38SDaniel Mack 
216720bebdfSDmitry Torokhov 	INIT_WORK(&eeti->work, eeti_ts_read);
217720bebdfSDmitry Torokhov 	i2c_set_clientdata(client, eeti);
218720bebdfSDmitry Torokhov 	input_set_drvdata(input, eeti);
21910494dceSDaniel Mack 
2206f9fab69SDmitry Torokhov 	error = input_register_device(input);
2216f9fab69SDmitry Torokhov 	if (error)
2226f9fab69SDmitry Torokhov 		return error;
22310494dceSDaniel Mack 
2246f9fab69SDmitry Torokhov 	error = devm_request_irq(dev, eeti->irq, eeti_ts_isr, irq_flags,
225720bebdfSDmitry Torokhov 				 client->name, eeti);
2266f9fab69SDmitry Torokhov 	if (error) {
2276f9fab69SDmitry Torokhov 		dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
2286f9fab69SDmitry Torokhov 			error);
2296f9fab69SDmitry Torokhov 		return error;
23010494dceSDaniel Mack 	}
23110494dceSDaniel Mack 
2327fbef0d1SDaniel Mack 	/*
2337fbef0d1SDaniel Mack 	 * Disable the device for now. It will be enabled once the
2347fbef0d1SDaniel Mack 	 * input device is opened.
2357fbef0d1SDaniel Mack 	 */
236720bebdfSDmitry Torokhov 	eeti_ts_stop(eeti);
23710494dceSDaniel Mack 
23810494dceSDaniel Mack 	return 0;
23910494dceSDaniel Mack }
24010494dceSDaniel Mack 
24102b6a58bSJingoo Han static int __maybe_unused eeti_ts_suspend(struct device *dev)
24210494dceSDaniel Mack {
24385012fffSMark Brown 	struct i2c_client *client = to_i2c_client(dev);
244720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = i2c_get_clientdata(client);
245720bebdfSDmitry Torokhov 	struct input_dev *input_dev = eeti->input;
2467fbef0d1SDaniel Mack 
2477fbef0d1SDaniel Mack 	mutex_lock(&input_dev->mutex);
2487fbef0d1SDaniel Mack 
2497fbef0d1SDaniel Mack 	if (input_dev->users)
250720bebdfSDmitry Torokhov 		eeti_ts_stop(eeti);
2517fbef0d1SDaniel Mack 
2527fbef0d1SDaniel Mack 	mutex_unlock(&input_dev->mutex);
25310494dceSDaniel Mack 
25410494dceSDaniel Mack 	if (device_may_wakeup(&client->dev))
255720bebdfSDmitry Torokhov 		enable_irq_wake(eeti->irq);
25610494dceSDaniel Mack 
25710494dceSDaniel Mack 	return 0;
25810494dceSDaniel Mack }
25910494dceSDaniel Mack 
26002b6a58bSJingoo Han static int __maybe_unused eeti_ts_resume(struct device *dev)
26110494dceSDaniel Mack {
26285012fffSMark Brown 	struct i2c_client *client = to_i2c_client(dev);
263720bebdfSDmitry Torokhov 	struct eeti_ts *eeti = i2c_get_clientdata(client);
264720bebdfSDmitry Torokhov 	struct input_dev *input_dev = eeti->input;
26510494dceSDaniel Mack 
26610494dceSDaniel Mack 	if (device_may_wakeup(&client->dev))
267720bebdfSDmitry Torokhov 		disable_irq_wake(eeti->irq);
26810494dceSDaniel Mack 
2697fbef0d1SDaniel Mack 	mutex_lock(&input_dev->mutex);
2707fbef0d1SDaniel Mack 
2717fbef0d1SDaniel Mack 	if (input_dev->users)
272720bebdfSDmitry Torokhov 		eeti_ts_start(eeti);
2737fbef0d1SDaniel Mack 
2747fbef0d1SDaniel Mack 	mutex_unlock(&input_dev->mutex);
2757fbef0d1SDaniel Mack 
27610494dceSDaniel Mack 	return 0;
27710494dceSDaniel Mack }
27885012fffSMark Brown 
27985012fffSMark Brown static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
28010494dceSDaniel Mack 
28110494dceSDaniel Mack static const struct i2c_device_id eeti_ts_id[] = {
28210494dceSDaniel Mack 	{ "eeti_ts", 0 },
28310494dceSDaniel Mack 	{ }
28410494dceSDaniel Mack };
28510494dceSDaniel Mack MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
28610494dceSDaniel Mack 
28710494dceSDaniel Mack static struct i2c_driver eeti_ts_driver = {
28810494dceSDaniel Mack 	.driver = {
28910494dceSDaniel Mack 		.name = "eeti_ts",
29085012fffSMark Brown 		.pm = &eeti_ts_pm,
29110494dceSDaniel Mack 	},
29210494dceSDaniel Mack 	.probe = eeti_ts_probe,
29310494dceSDaniel Mack 	.id_table = eeti_ts_id,
29410494dceSDaniel Mack };
29510494dceSDaniel Mack 
2961b92c1cfSAxel Lin module_i2c_driver(eeti_ts_driver);
29710494dceSDaniel Mack 
29810494dceSDaniel Mack MODULE_DESCRIPTION("EETI Touchscreen driver");
29910494dceSDaniel Mack MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
30010494dceSDaniel Mack MODULE_LICENSE("GPL");
301