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; 502d388499SDmitry Torokhov int irq_gpio, irq_active_high; 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 63720bebdfSDmitry Torokhov static inline int eeti_ts_irq_active(struct eeti_ts *eeti) 6425a70e38SDaniel Mack { 65173e4d81SDmitry Torokhov return gpio_get_value_cansleep(eeti->irq_gpio) == eeti->irq_active_high; 6625a70e38SDaniel Mack } 6725a70e38SDaniel Mack 680378008aSDmitry Torokhov static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf) 6910494dceSDaniel Mack { 700378008aSDmitry Torokhov unsigned int res; 710378008aSDmitry Torokhov u16 x, y; 7210494dceSDaniel Mack 7310494dceSDaniel Mack res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1)); 74e9f66cdbSDmitry Torokhov 75e9f66cdbSDmitry Torokhov x = get_unaligned_be16(&buf[1]); 76e9f66cdbSDmitry Torokhov y = get_unaligned_be16(&buf[3]); 7710494dceSDaniel Mack 7810494dceSDaniel Mack /* fix the range to 11 bits */ 7910494dceSDaniel Mack x >>= res - EETI_TS_BITDEPTH; 8010494dceSDaniel Mack y >>= res - EETI_TS_BITDEPTH; 8110494dceSDaniel Mack 8210494dceSDaniel Mack if (flip_x) 8310494dceSDaniel Mack x = EETI_MAXVAL - x; 8410494dceSDaniel Mack 8510494dceSDaniel Mack if (flip_y) 8610494dceSDaniel Mack y = EETI_MAXVAL - y; 8710494dceSDaniel Mack 8810494dceSDaniel Mack if (buf[0] & REPORT_BIT_HAS_PRESSURE) 89720bebdfSDmitry Torokhov input_report_abs(eeti->input, ABS_PRESSURE, buf[5]); 9010494dceSDaniel Mack 91720bebdfSDmitry Torokhov input_report_abs(eeti->input, ABS_X, x); 92720bebdfSDmitry Torokhov input_report_abs(eeti->input, ABS_Y, y); 930378008aSDmitry Torokhov input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED); 94720bebdfSDmitry Torokhov input_sync(eeti->input); 9510494dceSDaniel Mack } 9610494dceSDaniel Mack 9710494dceSDaniel Mack static irqreturn_t eeti_ts_isr(int irq, void *dev_id) 9810494dceSDaniel Mack { 99720bebdfSDmitry Torokhov struct eeti_ts *eeti = dev_id; 1000378008aSDmitry Torokhov int len; 1010378008aSDmitry Torokhov int error; 1020378008aSDmitry Torokhov char buf[6]; 10310494dceSDaniel Mack 1040378008aSDmitry Torokhov do { 1050378008aSDmitry Torokhov len = i2c_master_recv(eeti->client, buf, sizeof(buf)); 1060378008aSDmitry Torokhov if (len != sizeof(buf)) { 1070378008aSDmitry Torokhov error = len < 0 ? len : -EIO; 1080378008aSDmitry Torokhov dev_err(&eeti->client->dev, 1090378008aSDmitry Torokhov "failed to read touchscreen data: %d\n", 1100378008aSDmitry Torokhov error); 1110378008aSDmitry Torokhov break; 1120378008aSDmitry Torokhov } 1130378008aSDmitry Torokhov 1140378008aSDmitry Torokhov if (buf[0] & 0x80) { 1150378008aSDmitry Torokhov /* Motion packet */ 1160378008aSDmitry Torokhov eeti_ts_report_event(eeti, buf); 1170378008aSDmitry Torokhov } 1180378008aSDmitry Torokhov } while (eeti->running && eeti_ts_irq_active(eeti)); 11910494dceSDaniel Mack 12010494dceSDaniel Mack return IRQ_HANDLED; 12110494dceSDaniel Mack } 12210494dceSDaniel Mack 123720bebdfSDmitry Torokhov static void eeti_ts_start(struct eeti_ts *eeti) 12410494dceSDaniel Mack { 1250378008aSDmitry Torokhov eeti->running = true; 1260378008aSDmitry Torokhov wmb(); 1272d388499SDmitry Torokhov enable_irq(eeti->client->irq); 1287fbef0d1SDaniel Mack } 1297fbef0d1SDaniel Mack 130720bebdfSDmitry Torokhov static void eeti_ts_stop(struct eeti_ts *eeti) 1317fbef0d1SDaniel Mack { 1320378008aSDmitry Torokhov eeti->running = false; 1330378008aSDmitry Torokhov wmb(); 1342d388499SDmitry Torokhov disable_irq(eeti->client->irq); 1357fbef0d1SDaniel Mack } 1367fbef0d1SDaniel Mack 1377fbef0d1SDaniel Mack static int eeti_ts_open(struct input_dev *dev) 1387fbef0d1SDaniel Mack { 139720bebdfSDmitry Torokhov struct eeti_ts *eeti = input_get_drvdata(dev); 1407fbef0d1SDaniel Mack 141720bebdfSDmitry Torokhov eeti_ts_start(eeti); 14210494dceSDaniel Mack 14310494dceSDaniel Mack return 0; 14410494dceSDaniel Mack } 14510494dceSDaniel Mack 14610494dceSDaniel Mack static void eeti_ts_close(struct input_dev *dev) 14710494dceSDaniel Mack { 148720bebdfSDmitry Torokhov struct eeti_ts *eeti = input_get_drvdata(dev); 14910494dceSDaniel Mack 150720bebdfSDmitry Torokhov eeti_ts_stop(eeti); 15110494dceSDaniel Mack } 15210494dceSDaniel Mack 1535298cc4cSBill Pemberton static int eeti_ts_probe(struct i2c_client *client, 15410494dceSDaniel Mack const struct i2c_device_id *idp) 15510494dceSDaniel Mack { 1566f9fab69SDmitry Torokhov struct device *dev = &client->dev; 1576f9fab69SDmitry Torokhov struct eeti_ts_platform_data *pdata = dev_get_platdata(dev); 158720bebdfSDmitry Torokhov struct eeti_ts *eeti; 15910494dceSDaniel Mack struct input_dev *input; 1606f9fab69SDmitry Torokhov int error; 16110494dceSDaniel Mack 1627fbef0d1SDaniel Mack /* 1637fbef0d1SDaniel Mack * In contrast to what's described in the datasheet, there seems 16410494dceSDaniel Mack * to be no way of probing the presence of that device using I2C 16510494dceSDaniel Mack * commands. So we need to blindly believe it is there, and wait 1667fbef0d1SDaniel Mack * for interrupts to occur. 1677fbef0d1SDaniel Mack */ 16810494dceSDaniel Mack 1696f9fab69SDmitry Torokhov eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL); 170720bebdfSDmitry Torokhov if (!eeti) { 1716f9fab69SDmitry Torokhov dev_err(dev, "failed to allocate driver data\n"); 172ad03ae44SGuenter Roeck return -ENOMEM; 17310494dceSDaniel Mack } 17410494dceSDaniel Mack 1756f9fab69SDmitry Torokhov input = devm_input_allocate_device(dev); 17610494dceSDaniel Mack if (!input) { 1776f9fab69SDmitry Torokhov dev_err(dev, "Failed to allocate input device.\n"); 1786f9fab69SDmitry Torokhov return -ENOMEM; 17910494dceSDaniel Mack } 18010494dceSDaniel Mack 18142e02a6aSDmitry Torokhov input_set_capability(input, EV_KEY, BTN_TOUCH); 18210494dceSDaniel Mack 18310494dceSDaniel Mack input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0); 18410494dceSDaniel Mack input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0); 18510494dceSDaniel Mack input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0); 18610494dceSDaniel Mack 18710494dceSDaniel Mack input->name = client->name; 18810494dceSDaniel Mack input->id.bustype = BUS_I2C; 18910494dceSDaniel Mack input->open = eeti_ts_open; 19010494dceSDaniel Mack input->close = eeti_ts_close; 19110494dceSDaniel Mack 192720bebdfSDmitry Torokhov eeti->client = client; 193720bebdfSDmitry Torokhov eeti->input = input; 194720bebdfSDmitry Torokhov eeti->irq_gpio = pdata->irq_gpio; 19510494dceSDaniel Mack 1966f9fab69SDmitry Torokhov error = devm_gpio_request_one(dev, pdata->irq_gpio, GPIOF_IN, 1976f9fab69SDmitry Torokhov client->name); 1986f9fab69SDmitry Torokhov if (error) 1996f9fab69SDmitry Torokhov return error; 20025a70e38SDaniel Mack 201720bebdfSDmitry Torokhov eeti->irq_active_high = pdata->irq_active_high; 20225a70e38SDaniel Mack 203720bebdfSDmitry Torokhov i2c_set_clientdata(client, eeti); 204720bebdfSDmitry Torokhov input_set_drvdata(input, eeti); 20510494dceSDaniel Mack 2060378008aSDmitry Torokhov error = devm_request_threaded_irq(dev, client->irq, 2070378008aSDmitry Torokhov NULL, eeti_ts_isr, 208d422be5fSDmitry Torokhov IRQF_ONESHOT, 209720bebdfSDmitry Torokhov client->name, eeti); 2106f9fab69SDmitry Torokhov if (error) { 2116f9fab69SDmitry Torokhov dev_err(dev, "Unable to request touchscreen IRQ: %d\n", 2126f9fab69SDmitry Torokhov error); 2136f9fab69SDmitry Torokhov return error; 21410494dceSDaniel Mack } 21510494dceSDaniel Mack 2167fbef0d1SDaniel Mack /* 2177fbef0d1SDaniel Mack * Disable the device for now. It will be enabled once the 2187fbef0d1SDaniel Mack * input device is opened. 2197fbef0d1SDaniel Mack */ 220720bebdfSDmitry Torokhov eeti_ts_stop(eeti); 22110494dceSDaniel Mack 2220378008aSDmitry Torokhov error = input_register_device(input); 2230378008aSDmitry Torokhov if (error) 2240378008aSDmitry Torokhov return error; 2250378008aSDmitry Torokhov 22610494dceSDaniel Mack return 0; 22710494dceSDaniel Mack } 22810494dceSDaniel Mack 22902b6a58bSJingoo Han static int __maybe_unused eeti_ts_suspend(struct device *dev) 23010494dceSDaniel Mack { 23185012fffSMark Brown struct i2c_client *client = to_i2c_client(dev); 232720bebdfSDmitry Torokhov struct eeti_ts *eeti = i2c_get_clientdata(client); 233720bebdfSDmitry Torokhov struct input_dev *input_dev = eeti->input; 2347fbef0d1SDaniel Mack 2357fbef0d1SDaniel Mack mutex_lock(&input_dev->mutex); 2367fbef0d1SDaniel Mack 2377fbef0d1SDaniel Mack if (input_dev->users) 238720bebdfSDmitry Torokhov eeti_ts_stop(eeti); 2397fbef0d1SDaniel Mack 2407fbef0d1SDaniel Mack mutex_unlock(&input_dev->mutex); 24110494dceSDaniel Mack 24210494dceSDaniel Mack if (device_may_wakeup(&client->dev)) 2432d388499SDmitry Torokhov enable_irq_wake(client->irq); 24410494dceSDaniel Mack 24510494dceSDaniel Mack return 0; 24610494dceSDaniel Mack } 24710494dceSDaniel Mack 24802b6a58bSJingoo Han static int __maybe_unused eeti_ts_resume(struct device *dev) 24910494dceSDaniel Mack { 25085012fffSMark Brown struct i2c_client *client = to_i2c_client(dev); 251720bebdfSDmitry Torokhov struct eeti_ts *eeti = i2c_get_clientdata(client); 252720bebdfSDmitry Torokhov struct input_dev *input_dev = eeti->input; 25310494dceSDaniel Mack 25410494dceSDaniel Mack if (device_may_wakeup(&client->dev)) 2552d388499SDmitry Torokhov disable_irq_wake(client->irq); 25610494dceSDaniel Mack 2577fbef0d1SDaniel Mack mutex_lock(&input_dev->mutex); 2587fbef0d1SDaniel Mack 2597fbef0d1SDaniel Mack if (input_dev->users) 260720bebdfSDmitry Torokhov eeti_ts_start(eeti); 2617fbef0d1SDaniel Mack 2627fbef0d1SDaniel Mack mutex_unlock(&input_dev->mutex); 2637fbef0d1SDaniel Mack 26410494dceSDaniel Mack return 0; 26510494dceSDaniel Mack } 26685012fffSMark Brown 26785012fffSMark Brown static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume); 26810494dceSDaniel Mack 26910494dceSDaniel Mack static const struct i2c_device_id eeti_ts_id[] = { 27010494dceSDaniel Mack { "eeti_ts", 0 }, 27110494dceSDaniel Mack { } 27210494dceSDaniel Mack }; 27310494dceSDaniel Mack MODULE_DEVICE_TABLE(i2c, eeti_ts_id); 27410494dceSDaniel Mack 27510494dceSDaniel Mack static struct i2c_driver eeti_ts_driver = { 27610494dceSDaniel Mack .driver = { 27710494dceSDaniel Mack .name = "eeti_ts", 27885012fffSMark Brown .pm = &eeti_ts_pm, 27910494dceSDaniel Mack }, 28010494dceSDaniel Mack .probe = eeti_ts_probe, 28110494dceSDaniel Mack .id_table = eeti_ts_id, 28210494dceSDaniel Mack }; 28310494dceSDaniel Mack 2841b92c1cfSAxel Lin module_i2c_driver(eeti_ts_driver); 28510494dceSDaniel Mack 28610494dceSDaniel Mack MODULE_DESCRIPTION("EETI Touchscreen driver"); 28710494dceSDaniel Mack MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); 28810494dceSDaniel Mack MODULE_LICENSE("GPL"); 289