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> 3510494dceSDaniel Mack 3610494dceSDaniel Mack static int flip_x; 3710494dceSDaniel Mack module_param(flip_x, bool, 0644); 3810494dceSDaniel Mack MODULE_PARM_DESC(flip_x, "flip x coordinate"); 3910494dceSDaniel Mack 4010494dceSDaniel Mack static int flip_y; 4110494dceSDaniel Mack module_param(flip_y, bool, 0644); 4210494dceSDaniel Mack MODULE_PARM_DESC(flip_y, "flip y coordinate"); 4310494dceSDaniel Mack 4410494dceSDaniel Mack struct eeti_ts_priv { 4510494dceSDaniel Mack struct i2c_client *client; 4610494dceSDaniel Mack struct input_dev *input; 4710494dceSDaniel Mack struct work_struct work; 4810494dceSDaniel Mack struct mutex mutex; 4910494dceSDaniel Mack int irq; 5010494dceSDaniel Mack }; 5110494dceSDaniel Mack 5210494dceSDaniel Mack #define EETI_TS_BITDEPTH (11) 5310494dceSDaniel Mack #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1) 5410494dceSDaniel Mack 5510494dceSDaniel Mack #define REPORT_BIT_PRESSED (1 << 0) 5610494dceSDaniel Mack #define REPORT_BIT_AD0 (1 << 1) 5710494dceSDaniel Mack #define REPORT_BIT_AD1 (1 << 2) 5810494dceSDaniel Mack #define REPORT_BIT_HAS_PRESSURE (1 << 6) 5910494dceSDaniel Mack #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH) 6010494dceSDaniel Mack 6110494dceSDaniel Mack static void eeti_ts_read(struct work_struct *work) 6210494dceSDaniel Mack { 6310494dceSDaniel Mack char buf[6]; 6410494dceSDaniel Mack unsigned int x, y, res, pressed, to = 100; 6510494dceSDaniel Mack struct eeti_ts_priv *priv = 6610494dceSDaniel Mack container_of(work, struct eeti_ts_priv, work); 6710494dceSDaniel Mack 6810494dceSDaniel Mack mutex_lock(&priv->mutex); 6910494dceSDaniel Mack 7010494dceSDaniel Mack while (!gpio_get_value(irq_to_gpio(priv->irq)) && --to) 7110494dceSDaniel Mack i2c_master_recv(priv->client, buf, sizeof(buf)); 7210494dceSDaniel Mack 7310494dceSDaniel Mack if (!to) { 7410494dceSDaniel Mack dev_err(&priv->client->dev, 7510494dceSDaniel Mack "unable to clear IRQ - line stuck?\n"); 7610494dceSDaniel Mack goto out; 7710494dceSDaniel Mack } 7810494dceSDaniel Mack 7910494dceSDaniel Mack /* drop non-report packets */ 8010494dceSDaniel Mack if (!(buf[0] & 0x80)) 8110494dceSDaniel Mack goto out; 8210494dceSDaniel Mack 8310494dceSDaniel Mack pressed = buf[0] & REPORT_BIT_PRESSED; 8410494dceSDaniel Mack res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1)); 8510494dceSDaniel Mack x = buf[2] | (buf[1] << 8); 8610494dceSDaniel Mack y = buf[4] | (buf[3] << 8); 8710494dceSDaniel Mack 8810494dceSDaniel Mack /* fix the range to 11 bits */ 8910494dceSDaniel Mack x >>= res - EETI_TS_BITDEPTH; 9010494dceSDaniel Mack y >>= res - EETI_TS_BITDEPTH; 9110494dceSDaniel Mack 9210494dceSDaniel Mack if (flip_x) 9310494dceSDaniel Mack x = EETI_MAXVAL - x; 9410494dceSDaniel Mack 9510494dceSDaniel Mack if (flip_y) 9610494dceSDaniel Mack y = EETI_MAXVAL - y; 9710494dceSDaniel Mack 9810494dceSDaniel Mack if (buf[0] & REPORT_BIT_HAS_PRESSURE) 9910494dceSDaniel Mack input_report_abs(priv->input, ABS_PRESSURE, buf[5]); 10010494dceSDaniel Mack 10110494dceSDaniel Mack input_report_abs(priv->input, ABS_X, x); 10210494dceSDaniel Mack input_report_abs(priv->input, ABS_Y, y); 10310494dceSDaniel Mack input_report_key(priv->input, BTN_TOUCH, !!pressed); 10410494dceSDaniel Mack input_sync(priv->input); 10510494dceSDaniel Mack 10610494dceSDaniel Mack out: 10710494dceSDaniel Mack mutex_unlock(&priv->mutex); 10810494dceSDaniel Mack } 10910494dceSDaniel Mack 11010494dceSDaniel Mack static irqreturn_t eeti_ts_isr(int irq, void *dev_id) 11110494dceSDaniel Mack { 11210494dceSDaniel Mack struct eeti_ts_priv *priv = dev_id; 11310494dceSDaniel Mack 11410494dceSDaniel Mack /* postpone I2C transactions as we are atomic */ 11510494dceSDaniel Mack schedule_work(&priv->work); 11610494dceSDaniel Mack 11710494dceSDaniel Mack return IRQ_HANDLED; 11810494dceSDaniel Mack } 11910494dceSDaniel Mack 12010494dceSDaniel Mack static int eeti_ts_open(struct input_dev *dev) 12110494dceSDaniel Mack { 12210494dceSDaniel Mack struct eeti_ts_priv *priv = input_get_drvdata(dev); 12310494dceSDaniel Mack 12410494dceSDaniel Mack enable_irq(priv->irq); 12510494dceSDaniel Mack 12610494dceSDaniel Mack /* Read the events once to arm the IRQ */ 12710494dceSDaniel Mack eeti_ts_read(&priv->work); 12810494dceSDaniel Mack 12910494dceSDaniel Mack return 0; 13010494dceSDaniel Mack } 13110494dceSDaniel Mack 13210494dceSDaniel Mack static void eeti_ts_close(struct input_dev *dev) 13310494dceSDaniel Mack { 13410494dceSDaniel Mack struct eeti_ts_priv *priv = input_get_drvdata(dev); 13510494dceSDaniel Mack 13610494dceSDaniel Mack disable_irq(priv->irq); 13710494dceSDaniel Mack cancel_work_sync(&priv->work); 13810494dceSDaniel Mack } 13910494dceSDaniel Mack 14010494dceSDaniel Mack static int __devinit eeti_ts_probe(struct i2c_client *client, 14110494dceSDaniel Mack const struct i2c_device_id *idp) 14210494dceSDaniel Mack { 14310494dceSDaniel Mack struct eeti_ts_priv *priv; 14410494dceSDaniel Mack struct input_dev *input; 14510494dceSDaniel Mack int err = -ENOMEM; 14610494dceSDaniel Mack 14710494dceSDaniel Mack /* In contrast to what's described in the datasheet, there seems 14810494dceSDaniel Mack * to be no way of probing the presence of that device using I2C 14910494dceSDaniel Mack * commands. So we need to blindly believe it is there, and wait 15010494dceSDaniel Mack * for interrupts to occur. */ 15110494dceSDaniel Mack 15210494dceSDaniel Mack priv = kzalloc(sizeof(*priv), GFP_KERNEL); 15310494dceSDaniel Mack if (!priv) { 15410494dceSDaniel Mack dev_err(&client->dev, "failed to allocate driver data\n"); 15510494dceSDaniel Mack goto err0; 15610494dceSDaniel Mack } 15710494dceSDaniel Mack 15810494dceSDaniel Mack mutex_init(&priv->mutex); 15910494dceSDaniel Mack input = input_allocate_device(); 16010494dceSDaniel Mack 16110494dceSDaniel Mack if (!input) { 16210494dceSDaniel Mack dev_err(&client->dev, "Failed to allocate input device.\n"); 16310494dceSDaniel Mack goto err1; 16410494dceSDaniel Mack } 16510494dceSDaniel Mack 16610494dceSDaniel Mack input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 16710494dceSDaniel Mack input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); 16810494dceSDaniel Mack 16910494dceSDaniel Mack input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0); 17010494dceSDaniel Mack input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0); 17110494dceSDaniel Mack input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0); 17210494dceSDaniel Mack 17310494dceSDaniel Mack input->name = client->name; 17410494dceSDaniel Mack input->id.bustype = BUS_I2C; 17510494dceSDaniel Mack input->dev.parent = &client->dev; 17610494dceSDaniel Mack input->open = eeti_ts_open; 17710494dceSDaniel Mack input->close = eeti_ts_close; 17810494dceSDaniel Mack 17910494dceSDaniel Mack priv->client = client; 18010494dceSDaniel Mack priv->input = input; 18110494dceSDaniel Mack priv->irq = client->irq; 18210494dceSDaniel Mack 18310494dceSDaniel Mack INIT_WORK(&priv->work, eeti_ts_read); 18410494dceSDaniel Mack i2c_set_clientdata(client, priv); 18510494dceSDaniel Mack input_set_drvdata(input, priv); 18610494dceSDaniel Mack 18710494dceSDaniel Mack err = input_register_device(input); 18810494dceSDaniel Mack if (err) 18910494dceSDaniel Mack goto err1; 19010494dceSDaniel Mack 19110494dceSDaniel Mack err = request_irq(priv->irq, eeti_ts_isr, IRQF_TRIGGER_FALLING, 19210494dceSDaniel Mack client->name, priv); 19310494dceSDaniel Mack if (err) { 19410494dceSDaniel Mack dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); 19510494dceSDaniel Mack goto err2; 19610494dceSDaniel Mack } 19710494dceSDaniel Mack 19810494dceSDaniel Mack /* Disable the irq for now. It will be enabled once the input device 19910494dceSDaniel Mack * is opened. */ 20010494dceSDaniel Mack disable_irq(priv->irq); 20110494dceSDaniel Mack 20210494dceSDaniel Mack device_init_wakeup(&client->dev, 0); 20310494dceSDaniel Mack return 0; 20410494dceSDaniel Mack 20510494dceSDaniel Mack err2: 20610494dceSDaniel Mack input_unregister_device(input); 20710494dceSDaniel Mack input = NULL; /* so we dont try to free it below */ 20810494dceSDaniel Mack err1: 20910494dceSDaniel Mack input_free_device(input); 21010494dceSDaniel Mack i2c_set_clientdata(client, NULL); 21110494dceSDaniel Mack kfree(priv); 21210494dceSDaniel Mack err0: 21310494dceSDaniel Mack return err; 21410494dceSDaniel Mack } 21510494dceSDaniel Mack 21610494dceSDaniel Mack static int __devexit eeti_ts_remove(struct i2c_client *client) 21710494dceSDaniel Mack { 21810494dceSDaniel Mack struct eeti_ts_priv *priv = i2c_get_clientdata(client); 21910494dceSDaniel Mack 22010494dceSDaniel Mack free_irq(priv->irq, priv); 22110494dceSDaniel Mack input_unregister_device(priv->input); 22210494dceSDaniel Mack i2c_set_clientdata(client, NULL); 22310494dceSDaniel Mack kfree(priv); 22410494dceSDaniel Mack 22510494dceSDaniel Mack return 0; 22610494dceSDaniel Mack } 22710494dceSDaniel Mack 22810494dceSDaniel Mack #ifdef CONFIG_PM 22910494dceSDaniel Mack static int eeti_ts_suspend(struct i2c_client *client, pm_message_t mesg) 23010494dceSDaniel Mack { 23110494dceSDaniel Mack struct eeti_ts_priv *priv = i2c_get_clientdata(client); 23210494dceSDaniel Mack 23310494dceSDaniel Mack if (device_may_wakeup(&client->dev)) 23410494dceSDaniel Mack enable_irq_wake(priv->irq); 23510494dceSDaniel Mack 23610494dceSDaniel Mack return 0; 23710494dceSDaniel Mack } 23810494dceSDaniel Mack 23910494dceSDaniel Mack static int eeti_ts_resume(struct i2c_client *client) 24010494dceSDaniel Mack { 24110494dceSDaniel Mack struct eeti_ts_priv *priv = i2c_get_clientdata(client); 24210494dceSDaniel Mack 24310494dceSDaniel Mack if (device_may_wakeup(&client->dev)) 24410494dceSDaniel Mack disable_irq_wake(priv->irq); 24510494dceSDaniel Mack 24610494dceSDaniel Mack return 0; 24710494dceSDaniel Mack } 24810494dceSDaniel Mack #else 24910494dceSDaniel Mack #define eeti_ts_suspend NULL 25010494dceSDaniel Mack #define eeti_ts_resume NULL 25110494dceSDaniel Mack #endif 25210494dceSDaniel Mack 25310494dceSDaniel Mack static const struct i2c_device_id eeti_ts_id[] = { 25410494dceSDaniel Mack { "eeti_ts", 0 }, 25510494dceSDaniel Mack { } 25610494dceSDaniel Mack }; 25710494dceSDaniel Mack MODULE_DEVICE_TABLE(i2c, eeti_ts_id); 25810494dceSDaniel Mack 25910494dceSDaniel Mack static struct i2c_driver eeti_ts_driver = { 26010494dceSDaniel Mack .driver = { 26110494dceSDaniel Mack .name = "eeti_ts", 26210494dceSDaniel Mack }, 26310494dceSDaniel Mack .probe = eeti_ts_probe, 26410494dceSDaniel Mack .remove = __devexit_p(eeti_ts_remove), 26510494dceSDaniel Mack .suspend = eeti_ts_suspend, 26610494dceSDaniel Mack .resume = eeti_ts_resume, 26710494dceSDaniel Mack .id_table = eeti_ts_id, 26810494dceSDaniel Mack }; 26910494dceSDaniel Mack 27010494dceSDaniel Mack static int __init eeti_ts_init(void) 27110494dceSDaniel Mack { 27210494dceSDaniel Mack return i2c_add_driver(&eeti_ts_driver); 27310494dceSDaniel Mack } 27410494dceSDaniel Mack 27510494dceSDaniel Mack static void __exit eeti_ts_exit(void) 27610494dceSDaniel Mack { 27710494dceSDaniel Mack i2c_del_driver(&eeti_ts_driver); 27810494dceSDaniel Mack } 27910494dceSDaniel Mack 28010494dceSDaniel Mack MODULE_DESCRIPTION("EETI Touchscreen driver"); 28110494dceSDaniel Mack MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); 28210494dceSDaniel Mack MODULE_LICENSE("GPL"); 28310494dceSDaniel Mack 28410494dceSDaniel Mack module_init(eeti_ts_init); 28510494dceSDaniel Mack module_exit(eeti_ts_exit); 28610494dceSDaniel Mack 287