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>
3710494dceSDaniel Mack 
3890ab5ee9SRusty Russell static bool flip_x;
3910494dceSDaniel Mack module_param(flip_x, bool, 0644);
4010494dceSDaniel Mack MODULE_PARM_DESC(flip_x, "flip x coordinate");
4110494dceSDaniel Mack 
4290ab5ee9SRusty Russell static bool flip_y;
4310494dceSDaniel Mack module_param(flip_y, bool, 0644);
4410494dceSDaniel Mack MODULE_PARM_DESC(flip_y, "flip y coordinate");
4510494dceSDaniel Mack 
4610494dceSDaniel Mack struct eeti_ts_priv {
4710494dceSDaniel Mack 	struct i2c_client *client;
4810494dceSDaniel Mack 	struct input_dev *input;
4910494dceSDaniel Mack 	struct work_struct work;
5010494dceSDaniel Mack 	struct mutex mutex;
514eef6cbfSArnd Bergmann 	int irq_gpio, irq, irq_active_high;
5210494dceSDaniel Mack };
5310494dceSDaniel Mack 
5410494dceSDaniel Mack #define EETI_TS_BITDEPTH	(11)
5510494dceSDaniel Mack #define EETI_MAXVAL		((1 << (EETI_TS_BITDEPTH + 1)) - 1)
5610494dceSDaniel Mack 
5710494dceSDaniel Mack #define REPORT_BIT_PRESSED	(1 << 0)
5810494dceSDaniel Mack #define REPORT_BIT_AD0		(1 << 1)
5910494dceSDaniel Mack #define REPORT_BIT_AD1		(1 << 2)
6010494dceSDaniel Mack #define REPORT_BIT_HAS_PRESSURE	(1 << 6)
6110494dceSDaniel Mack #define REPORT_RES_BITS(v)	(((v) >> 1) + EETI_TS_BITDEPTH)
6210494dceSDaniel Mack 
6325a70e38SDaniel Mack static inline int eeti_ts_irq_active(struct eeti_ts_priv *priv)
6425a70e38SDaniel Mack {
654eef6cbfSArnd Bergmann 	return gpio_get_value(priv->irq_gpio) == priv->irq_active_high;
6625a70e38SDaniel Mack }
6725a70e38SDaniel Mack 
6810494dceSDaniel Mack static void eeti_ts_read(struct work_struct *work)
6910494dceSDaniel Mack {
7010494dceSDaniel Mack 	char buf[6];
7110494dceSDaniel Mack 	unsigned int x, y, res, pressed, to = 100;
7210494dceSDaniel Mack 	struct eeti_ts_priv *priv =
7310494dceSDaniel Mack 		container_of(work, struct eeti_ts_priv, work);
7410494dceSDaniel Mack 
7510494dceSDaniel Mack 	mutex_lock(&priv->mutex);
7610494dceSDaniel Mack 
7725a70e38SDaniel Mack 	while (eeti_ts_irq_active(priv) && --to)
7810494dceSDaniel Mack 		i2c_master_recv(priv->client, buf, sizeof(buf));
7910494dceSDaniel Mack 
8010494dceSDaniel Mack 	if (!to) {
8110494dceSDaniel Mack 		dev_err(&priv->client->dev,
8210494dceSDaniel Mack 			"unable to clear IRQ - line stuck?\n");
8310494dceSDaniel Mack 		goto out;
8410494dceSDaniel Mack 	}
8510494dceSDaniel Mack 
8610494dceSDaniel Mack 	/* drop non-report packets */
8710494dceSDaniel Mack 	if (!(buf[0] & 0x80))
8810494dceSDaniel Mack 		goto out;
8910494dceSDaniel Mack 
9010494dceSDaniel Mack 	pressed = buf[0] & REPORT_BIT_PRESSED;
9110494dceSDaniel Mack 	res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
9210494dceSDaniel Mack 	x = buf[2] | (buf[1] << 8);
9310494dceSDaniel Mack 	y = buf[4] | (buf[3] << 8);
9410494dceSDaniel Mack 
9510494dceSDaniel Mack 	/* fix the range to 11 bits */
9610494dceSDaniel Mack 	x >>= res - EETI_TS_BITDEPTH;
9710494dceSDaniel Mack 	y >>= res - EETI_TS_BITDEPTH;
9810494dceSDaniel Mack 
9910494dceSDaniel Mack 	if (flip_x)
10010494dceSDaniel Mack 		x = EETI_MAXVAL - x;
10110494dceSDaniel Mack 
10210494dceSDaniel Mack 	if (flip_y)
10310494dceSDaniel Mack 		y = EETI_MAXVAL - y;
10410494dceSDaniel Mack 
10510494dceSDaniel Mack 	if (buf[0] & REPORT_BIT_HAS_PRESSURE)
10610494dceSDaniel Mack 		input_report_abs(priv->input, ABS_PRESSURE, buf[5]);
10710494dceSDaniel Mack 
10810494dceSDaniel Mack 	input_report_abs(priv->input, ABS_X, x);
10910494dceSDaniel Mack 	input_report_abs(priv->input, ABS_Y, y);
11010494dceSDaniel Mack 	input_report_key(priv->input, BTN_TOUCH, !!pressed);
11110494dceSDaniel Mack 	input_sync(priv->input);
11210494dceSDaniel Mack 
11310494dceSDaniel Mack out:
11410494dceSDaniel Mack 	mutex_unlock(&priv->mutex);
11510494dceSDaniel Mack }
11610494dceSDaniel Mack 
11710494dceSDaniel Mack static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
11810494dceSDaniel Mack {
11910494dceSDaniel Mack 	struct eeti_ts_priv *priv = dev_id;
12010494dceSDaniel Mack 
12110494dceSDaniel Mack 	 /* postpone I2C transactions as we are atomic */
12210494dceSDaniel Mack 	schedule_work(&priv->work);
12310494dceSDaniel Mack 
12410494dceSDaniel Mack 	return IRQ_HANDLED;
12510494dceSDaniel Mack }
12610494dceSDaniel Mack 
1277fbef0d1SDaniel Mack static void eeti_ts_start(struct eeti_ts_priv *priv)
12810494dceSDaniel Mack {
12910494dceSDaniel Mack 	enable_irq(priv->irq);
13010494dceSDaniel Mack 
13110494dceSDaniel Mack 	/* Read the events once to arm the IRQ */
13210494dceSDaniel Mack 	eeti_ts_read(&priv->work);
1337fbef0d1SDaniel Mack }
1347fbef0d1SDaniel Mack 
1357fbef0d1SDaniel Mack static void eeti_ts_stop(struct eeti_ts_priv *priv)
1367fbef0d1SDaniel Mack {
1377fbef0d1SDaniel Mack 	disable_irq(priv->irq);
1387fbef0d1SDaniel Mack 	cancel_work_sync(&priv->work);
1397fbef0d1SDaniel Mack }
1407fbef0d1SDaniel Mack 
1417fbef0d1SDaniel Mack static int eeti_ts_open(struct input_dev *dev)
1427fbef0d1SDaniel Mack {
1437fbef0d1SDaniel Mack 	struct eeti_ts_priv *priv = input_get_drvdata(dev);
1447fbef0d1SDaniel Mack 
1457fbef0d1SDaniel Mack 	eeti_ts_start(priv);
14610494dceSDaniel Mack 
14710494dceSDaniel Mack 	return 0;
14810494dceSDaniel Mack }
14910494dceSDaniel Mack 
15010494dceSDaniel Mack static void eeti_ts_close(struct input_dev *dev)
15110494dceSDaniel Mack {
15210494dceSDaniel Mack 	struct eeti_ts_priv *priv = input_get_drvdata(dev);
15310494dceSDaniel Mack 
1547fbef0d1SDaniel Mack 	eeti_ts_stop(priv);
15510494dceSDaniel Mack }
15610494dceSDaniel Mack 
15710494dceSDaniel Mack static int __devinit eeti_ts_probe(struct i2c_client *client,
15810494dceSDaniel Mack 				   const struct i2c_device_id *idp)
15910494dceSDaniel Mack {
1604eef6cbfSArnd Bergmann 	struct eeti_ts_platform_data *pdata = client->dev.platform_data;
16110494dceSDaniel Mack 	struct eeti_ts_priv *priv;
16210494dceSDaniel Mack 	struct input_dev *input;
16325a70e38SDaniel Mack 	unsigned int irq_flags;
16410494dceSDaniel Mack 	int err = -ENOMEM;
16510494dceSDaniel Mack 
1667fbef0d1SDaniel Mack 	/*
1677fbef0d1SDaniel Mack 	 * In contrast to what's described in the datasheet, there seems
16810494dceSDaniel Mack 	 * to be no way of probing the presence of that device using I2C
16910494dceSDaniel Mack 	 * commands. So we need to blindly believe it is there, and wait
1707fbef0d1SDaniel Mack 	 * for interrupts to occur.
1717fbef0d1SDaniel Mack 	 */
17210494dceSDaniel Mack 
17310494dceSDaniel Mack 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
17410494dceSDaniel Mack 	if (!priv) {
17510494dceSDaniel Mack 		dev_err(&client->dev, "failed to allocate driver data\n");
17610494dceSDaniel Mack 		goto err0;
17710494dceSDaniel Mack 	}
17810494dceSDaniel Mack 
17910494dceSDaniel Mack 	mutex_init(&priv->mutex);
18010494dceSDaniel Mack 	input = input_allocate_device();
18110494dceSDaniel Mack 
18210494dceSDaniel Mack 	if (!input) {
18310494dceSDaniel Mack 		dev_err(&client->dev, "Failed to allocate input device.\n");
18410494dceSDaniel Mack 		goto err1;
18510494dceSDaniel Mack 	}
18610494dceSDaniel Mack 
18710494dceSDaniel Mack 	input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
18810494dceSDaniel Mack 	input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
18910494dceSDaniel Mack 
19010494dceSDaniel Mack 	input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
19110494dceSDaniel Mack 	input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
19210494dceSDaniel Mack 	input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
19310494dceSDaniel Mack 
19410494dceSDaniel Mack 	input->name = client->name;
19510494dceSDaniel Mack 	input->id.bustype = BUS_I2C;
19610494dceSDaniel Mack 	input->dev.parent = &client->dev;
19710494dceSDaniel Mack 	input->open = eeti_ts_open;
19810494dceSDaniel Mack 	input->close = eeti_ts_close;
19910494dceSDaniel Mack 
20010494dceSDaniel Mack 	priv->client = client;
20110494dceSDaniel Mack 	priv->input = input;
2024eef6cbfSArnd Bergmann 	priv->irq_gpio = pdata->irq_gpio;
2034eef6cbfSArnd Bergmann 	priv->irq = gpio_to_irq(pdata->irq_gpio);
20410494dceSDaniel Mack 
2054eef6cbfSArnd Bergmann 	err = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name);
2064eef6cbfSArnd Bergmann 	if (err < 0)
2074eef6cbfSArnd Bergmann 		goto err1;
20825a70e38SDaniel Mack 
20925a70e38SDaniel Mack 	if (pdata)
21025a70e38SDaniel Mack 		priv->irq_active_high = pdata->irq_active_high;
21125a70e38SDaniel Mack 
21225a70e38SDaniel Mack 	irq_flags = priv->irq_active_high ?
21325a70e38SDaniel Mack 		IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
21425a70e38SDaniel Mack 
21510494dceSDaniel Mack 	INIT_WORK(&priv->work, eeti_ts_read);
21610494dceSDaniel Mack 	i2c_set_clientdata(client, priv);
21710494dceSDaniel Mack 	input_set_drvdata(input, priv);
21810494dceSDaniel Mack 
21910494dceSDaniel Mack 	err = input_register_device(input);
22010494dceSDaniel Mack 	if (err)
2214eef6cbfSArnd Bergmann 		goto err2;
22210494dceSDaniel Mack 
22325a70e38SDaniel Mack 	err = request_irq(priv->irq, eeti_ts_isr, irq_flags,
22410494dceSDaniel Mack 			  client->name, priv);
22510494dceSDaniel Mack 	if (err) {
22610494dceSDaniel Mack 		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
2274eef6cbfSArnd Bergmann 		goto err3;
22810494dceSDaniel Mack 	}
22910494dceSDaniel Mack 
2307fbef0d1SDaniel Mack 	/*
2317fbef0d1SDaniel Mack 	 * Disable the device for now. It will be enabled once the
2327fbef0d1SDaniel Mack 	 * input device is opened.
2337fbef0d1SDaniel Mack 	 */
2347fbef0d1SDaniel Mack 	eeti_ts_stop(priv);
23510494dceSDaniel Mack 
23610494dceSDaniel Mack 	device_init_wakeup(&client->dev, 0);
23710494dceSDaniel Mack 	return 0;
23810494dceSDaniel Mack 
2394eef6cbfSArnd Bergmann err3:
24010494dceSDaniel Mack 	input_unregister_device(input);
24110494dceSDaniel Mack 	input = NULL; /* so we dont try to free it below */
2424eef6cbfSArnd Bergmann err2:
2434eef6cbfSArnd Bergmann 	gpio_free(pdata->irq_gpio);
24410494dceSDaniel Mack err1:
24510494dceSDaniel Mack 	input_free_device(input);
24610494dceSDaniel Mack 	kfree(priv);
24710494dceSDaniel Mack err0:
24810494dceSDaniel Mack 	return err;
24910494dceSDaniel Mack }
25010494dceSDaniel Mack 
25110494dceSDaniel Mack static int __devexit eeti_ts_remove(struct i2c_client *client)
25210494dceSDaniel Mack {
25310494dceSDaniel Mack 	struct eeti_ts_priv *priv = i2c_get_clientdata(client);
25410494dceSDaniel Mack 
25510494dceSDaniel Mack 	free_irq(priv->irq, priv);
2567fbef0d1SDaniel Mack 	/*
2577fbef0d1SDaniel Mack 	 * eeti_ts_stop() leaves IRQ disabled. We need to re-enable it
2587fbef0d1SDaniel Mack 	 * so that device still works if we reload the driver.
2597fbef0d1SDaniel Mack 	 */
2607fbef0d1SDaniel Mack 	enable_irq(priv->irq);
2617fbef0d1SDaniel Mack 
26210494dceSDaniel Mack 	input_unregister_device(priv->input);
26310494dceSDaniel Mack 	kfree(priv);
26410494dceSDaniel Mack 
26510494dceSDaniel Mack 	return 0;
26610494dceSDaniel Mack }
26710494dceSDaniel Mack 
26810494dceSDaniel Mack #ifdef CONFIG_PM
26985012fffSMark Brown static int eeti_ts_suspend(struct device *dev)
27010494dceSDaniel Mack {
27185012fffSMark Brown 	struct i2c_client *client = to_i2c_client(dev);
27210494dceSDaniel Mack 	struct eeti_ts_priv *priv = i2c_get_clientdata(client);
2737fbef0d1SDaniel Mack 	struct input_dev *input_dev = priv->input;
2747fbef0d1SDaniel Mack 
2757fbef0d1SDaniel Mack 	mutex_lock(&input_dev->mutex);
2767fbef0d1SDaniel Mack 
2777fbef0d1SDaniel Mack 	if (input_dev->users)
2787fbef0d1SDaniel Mack 		eeti_ts_stop(priv);
2797fbef0d1SDaniel Mack 
2807fbef0d1SDaniel Mack 	mutex_unlock(&input_dev->mutex);
28110494dceSDaniel Mack 
28210494dceSDaniel Mack 	if (device_may_wakeup(&client->dev))
28310494dceSDaniel Mack 		enable_irq_wake(priv->irq);
28410494dceSDaniel Mack 
28510494dceSDaniel Mack 	return 0;
28610494dceSDaniel Mack }
28710494dceSDaniel Mack 
28885012fffSMark Brown static int eeti_ts_resume(struct device *dev)
28910494dceSDaniel Mack {
29085012fffSMark Brown 	struct i2c_client *client = to_i2c_client(dev);
29110494dceSDaniel Mack 	struct eeti_ts_priv *priv = i2c_get_clientdata(client);
2927fbef0d1SDaniel Mack 	struct input_dev *input_dev = priv->input;
29310494dceSDaniel Mack 
29410494dceSDaniel Mack 	if (device_may_wakeup(&client->dev))
29510494dceSDaniel Mack 		disable_irq_wake(priv->irq);
29610494dceSDaniel Mack 
2977fbef0d1SDaniel Mack 	mutex_lock(&input_dev->mutex);
2987fbef0d1SDaniel Mack 
2997fbef0d1SDaniel Mack 	if (input_dev->users)
3007fbef0d1SDaniel Mack 		eeti_ts_start(priv);
3017fbef0d1SDaniel Mack 
3027fbef0d1SDaniel Mack 	mutex_unlock(&input_dev->mutex);
3037fbef0d1SDaniel Mack 
30410494dceSDaniel Mack 	return 0;
30510494dceSDaniel Mack }
30685012fffSMark Brown 
30785012fffSMark Brown static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
30810494dceSDaniel Mack #endif
30910494dceSDaniel Mack 
31010494dceSDaniel Mack static const struct i2c_device_id eeti_ts_id[] = {
31110494dceSDaniel Mack 	{ "eeti_ts", 0 },
31210494dceSDaniel Mack 	{ }
31310494dceSDaniel Mack };
31410494dceSDaniel Mack MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
31510494dceSDaniel Mack 
31610494dceSDaniel Mack static struct i2c_driver eeti_ts_driver = {
31710494dceSDaniel Mack 	.driver = {
31810494dceSDaniel Mack 		.name = "eeti_ts",
31985012fffSMark Brown #ifdef CONFIG_PM
32085012fffSMark Brown 		.pm = &eeti_ts_pm,
32185012fffSMark Brown #endif
32210494dceSDaniel Mack 	},
32310494dceSDaniel Mack 	.probe = eeti_ts_probe,
3241cb0aa88SBill Pemberton 	.remove = eeti_ts_remove,
32510494dceSDaniel Mack 	.id_table = eeti_ts_id,
32610494dceSDaniel Mack };
32710494dceSDaniel Mack 
3281b92c1cfSAxel Lin module_i2c_driver(eeti_ts_driver);
32910494dceSDaniel Mack 
33010494dceSDaniel Mack MODULE_DESCRIPTION("EETI Touchscreen driver");
33110494dceSDaniel Mack MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
33210494dceSDaniel Mack MODULE_LICENSE("GPL");
333