110494dceSDaniel Mack /* 210494dceSDaniel Mack * Touch Screen driver for EETI's I2C connected touch screen panels 3fd8135b6SDaniel Mack * Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org> 410494dceSDaniel Mack * 510494dceSDaniel Mack * See EETI's software guide for the protocol specification: 6fd8135b6SDaniel Mack * http://home.eeti.com.tw/documentation.html 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/kernel.h> 2910494dceSDaniel Mack #include <linux/input.h> 30a114cbd0SDaniel Mack #include <linux/input/touchscreen.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 39720bebdfSDmitry Torokhov struct eeti_ts { 4010494dceSDaniel Mack struct i2c_client *client; 4110494dceSDaniel Mack struct input_dev *input; 42d99caa47SDmitry Torokhov struct gpio_desc *attn_gpio; 43a114cbd0SDaniel Mack struct touchscreen_properties props; 44b283d0c3SDaniel Mack struct mutex mutex; 450378008aSDmitry Torokhov bool running; 4610494dceSDaniel Mack }; 4710494dceSDaniel Mack 4810494dceSDaniel Mack #define EETI_TS_BITDEPTH (11) 4910494dceSDaniel Mack #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1) 5010494dceSDaniel Mack 51272c03bbSDmitry Torokhov #define REPORT_BIT_PRESSED BIT(0) 52272c03bbSDmitry Torokhov #define REPORT_BIT_AD0 BIT(1) 53272c03bbSDmitry Torokhov #define REPORT_BIT_AD1 BIT(2) 54272c03bbSDmitry Torokhov #define REPORT_BIT_HAS_PRESSURE BIT(6) 5510494dceSDaniel Mack #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH) 5610494dceSDaniel Mack 570378008aSDmitry Torokhov static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf) 5810494dceSDaniel Mack { 590378008aSDmitry Torokhov unsigned int res; 600378008aSDmitry Torokhov u16 x, y; 6110494dceSDaniel Mack 6210494dceSDaniel Mack res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1)); 63e9f66cdbSDmitry Torokhov 64e9f66cdbSDmitry Torokhov x = get_unaligned_be16(&buf[1]); 65e9f66cdbSDmitry Torokhov y = get_unaligned_be16(&buf[3]); 6610494dceSDaniel Mack 6710494dceSDaniel Mack /* fix the range to 11 bits */ 6810494dceSDaniel Mack x >>= res - EETI_TS_BITDEPTH; 6910494dceSDaniel Mack y >>= res - EETI_TS_BITDEPTH; 7010494dceSDaniel Mack 7110494dceSDaniel Mack if (buf[0] & REPORT_BIT_HAS_PRESSURE) 72720bebdfSDmitry Torokhov input_report_abs(eeti->input, ABS_PRESSURE, buf[5]); 7310494dceSDaniel Mack 74a114cbd0SDaniel Mack touchscreen_report_pos(eeti->input, &eeti->props, x, y, false); 750378008aSDmitry Torokhov input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED); 76720bebdfSDmitry Torokhov input_sync(eeti->input); 7710494dceSDaniel Mack } 7810494dceSDaniel Mack 79b283d0c3SDaniel Mack static int eeti_ts_read(struct eeti_ts *eeti) 8010494dceSDaniel Mack { 81b283d0c3SDaniel Mack int len, error; 820378008aSDmitry Torokhov char buf[6]; 8310494dceSDaniel Mack 840378008aSDmitry Torokhov len = i2c_master_recv(eeti->client, buf, sizeof(buf)); 850378008aSDmitry Torokhov if (len != sizeof(buf)) { 860378008aSDmitry Torokhov error = len < 0 ? len : -EIO; 870378008aSDmitry Torokhov dev_err(&eeti->client->dev, 880378008aSDmitry Torokhov "failed to read touchscreen data: %d\n", 890378008aSDmitry Torokhov error); 90b283d0c3SDaniel Mack return error; 91b283d0c3SDaniel Mack } 92b283d0c3SDaniel Mack 93b283d0c3SDaniel Mack /* Motion packet */ 94b283d0c3SDaniel Mack if (buf[0] & 0x80) 95b283d0c3SDaniel Mack eeti_ts_report_event(eeti, buf); 96b283d0c3SDaniel Mack 97b283d0c3SDaniel Mack return 0; 98b283d0c3SDaniel Mack } 99b283d0c3SDaniel Mack 100b283d0c3SDaniel Mack static irqreturn_t eeti_ts_isr(int irq, void *dev_id) 101b283d0c3SDaniel Mack { 102b283d0c3SDaniel Mack struct eeti_ts *eeti = dev_id; 103b283d0c3SDaniel Mack int error; 104b283d0c3SDaniel Mack 105b283d0c3SDaniel Mack mutex_lock(&eeti->mutex); 106b283d0c3SDaniel Mack 107b283d0c3SDaniel Mack do { 108b283d0c3SDaniel Mack /* 109b283d0c3SDaniel Mack * If we have attention GPIO, trust it. Otherwise we'll read 110b283d0c3SDaniel Mack * once and exit. We assume that in this case we are using 111b283d0c3SDaniel Mack * level triggered interrupt and it will get raised again 112b283d0c3SDaniel Mack * if/when there is more data. 113b283d0c3SDaniel Mack */ 114b283d0c3SDaniel Mack if (eeti->attn_gpio && 115b283d0c3SDaniel Mack !gpiod_get_value_cansleep(eeti->attn_gpio)) { 1160378008aSDmitry Torokhov break; 1170378008aSDmitry Torokhov } 1180378008aSDmitry Torokhov 119b283d0c3SDaniel Mack error = eeti_ts_read(eeti); 120b283d0c3SDaniel Mack if (error) 121b283d0c3SDaniel Mack break; 12210494dceSDaniel Mack 123b283d0c3SDaniel Mack } while (eeti->running && eeti->attn_gpio); 124b283d0c3SDaniel Mack 125b283d0c3SDaniel Mack mutex_unlock(&eeti->mutex); 12610494dceSDaniel Mack return IRQ_HANDLED; 12710494dceSDaniel Mack } 12810494dceSDaniel Mack 129720bebdfSDmitry Torokhov static void eeti_ts_start(struct eeti_ts *eeti) 13010494dceSDaniel Mack { 131b283d0c3SDaniel Mack mutex_lock(&eeti->mutex); 132b283d0c3SDaniel Mack 1330378008aSDmitry Torokhov eeti->running = true; 1342d388499SDmitry Torokhov enable_irq(eeti->client->irq); 135b283d0c3SDaniel Mack 136b283d0c3SDaniel Mack /* 137b283d0c3SDaniel Mack * Kick the controller in case we are using edge interrupt and 138b283d0c3SDaniel Mack * we missed our edge while interrupt was disabled. We expect 139b283d0c3SDaniel Mack * the attention GPIO to be wired in this case. 140b283d0c3SDaniel Mack */ 141b283d0c3SDaniel Mack if (eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio)) 142b283d0c3SDaniel Mack eeti_ts_read(eeti); 143b283d0c3SDaniel Mack 144b283d0c3SDaniel Mack mutex_unlock(&eeti->mutex); 1457fbef0d1SDaniel Mack } 1467fbef0d1SDaniel Mack 147720bebdfSDmitry Torokhov static void eeti_ts_stop(struct eeti_ts *eeti) 1487fbef0d1SDaniel Mack { 149b283d0c3SDaniel Mack /* 150b283d0c3SDaniel Mack * Not locking here, just setting a flag and expect that the 151b283d0c3SDaniel Mack * interrupt thread will notice the flag eventually. 152b283d0c3SDaniel Mack */ 1530378008aSDmitry Torokhov eeti->running = false; 1540378008aSDmitry Torokhov wmb(); 1552d388499SDmitry Torokhov disable_irq(eeti->client->irq); 1567fbef0d1SDaniel Mack } 1577fbef0d1SDaniel Mack 1587fbef0d1SDaniel Mack static int eeti_ts_open(struct input_dev *dev) 1597fbef0d1SDaniel Mack { 160720bebdfSDmitry Torokhov struct eeti_ts *eeti = input_get_drvdata(dev); 1617fbef0d1SDaniel Mack 162720bebdfSDmitry Torokhov eeti_ts_start(eeti); 16310494dceSDaniel Mack 16410494dceSDaniel Mack return 0; 16510494dceSDaniel Mack } 16610494dceSDaniel Mack 16710494dceSDaniel Mack static void eeti_ts_close(struct input_dev *dev) 16810494dceSDaniel Mack { 169720bebdfSDmitry Torokhov struct eeti_ts *eeti = input_get_drvdata(dev); 17010494dceSDaniel Mack 171720bebdfSDmitry Torokhov eeti_ts_stop(eeti); 17210494dceSDaniel Mack } 17310494dceSDaniel Mack 1745298cc4cSBill Pemberton static int eeti_ts_probe(struct i2c_client *client, 17510494dceSDaniel Mack const struct i2c_device_id *idp) 17610494dceSDaniel Mack { 1776f9fab69SDmitry Torokhov struct device *dev = &client->dev; 178720bebdfSDmitry Torokhov struct eeti_ts *eeti; 17910494dceSDaniel Mack struct input_dev *input; 1806f9fab69SDmitry Torokhov int error; 18110494dceSDaniel Mack 1827fbef0d1SDaniel Mack /* 1837fbef0d1SDaniel Mack * In contrast to what's described in the datasheet, there seems 18410494dceSDaniel Mack * to be no way of probing the presence of that device using I2C 18510494dceSDaniel Mack * commands. So we need to blindly believe it is there, and wait 1867fbef0d1SDaniel Mack * for interrupts to occur. 1877fbef0d1SDaniel Mack */ 18810494dceSDaniel Mack 1896f9fab69SDmitry Torokhov eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL); 190720bebdfSDmitry Torokhov if (!eeti) { 1916f9fab69SDmitry Torokhov dev_err(dev, "failed to allocate driver data\n"); 192ad03ae44SGuenter Roeck return -ENOMEM; 19310494dceSDaniel Mack } 19410494dceSDaniel Mack 195b283d0c3SDaniel Mack mutex_init(&eeti->mutex); 196b283d0c3SDaniel Mack 1976f9fab69SDmitry Torokhov input = devm_input_allocate_device(dev); 19810494dceSDaniel Mack if (!input) { 1996f9fab69SDmitry Torokhov dev_err(dev, "Failed to allocate input device.\n"); 2006f9fab69SDmitry Torokhov return -ENOMEM; 20110494dceSDaniel Mack } 20210494dceSDaniel Mack 20342e02a6aSDmitry Torokhov input_set_capability(input, EV_KEY, BTN_TOUCH); 20410494dceSDaniel Mack 20510494dceSDaniel Mack input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0); 20610494dceSDaniel Mack input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0); 20710494dceSDaniel Mack input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0); 20810494dceSDaniel Mack 209a114cbd0SDaniel Mack touchscreen_parse_properties(input, false, &eeti->props); 210a114cbd0SDaniel Mack 21110494dceSDaniel Mack input->name = client->name; 21210494dceSDaniel Mack input->id.bustype = BUS_I2C; 21310494dceSDaniel Mack input->open = eeti_ts_open; 21410494dceSDaniel Mack input->close = eeti_ts_close; 21510494dceSDaniel Mack 216720bebdfSDmitry Torokhov eeti->client = client; 217720bebdfSDmitry Torokhov eeti->input = input; 21810494dceSDaniel Mack 219d99caa47SDmitry Torokhov eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN); 220d99caa47SDmitry Torokhov if (IS_ERR(eeti->attn_gpio)) 221d99caa47SDmitry Torokhov return PTR_ERR(eeti->attn_gpio); 22225a70e38SDaniel Mack 223720bebdfSDmitry Torokhov i2c_set_clientdata(client, eeti); 224720bebdfSDmitry Torokhov input_set_drvdata(input, eeti); 22510494dceSDaniel Mack 2260378008aSDmitry Torokhov error = devm_request_threaded_irq(dev, client->irq, 2270378008aSDmitry Torokhov NULL, eeti_ts_isr, 228d422be5fSDmitry Torokhov IRQF_ONESHOT, 229720bebdfSDmitry Torokhov client->name, eeti); 2306f9fab69SDmitry Torokhov if (error) { 2316f9fab69SDmitry Torokhov dev_err(dev, "Unable to request touchscreen IRQ: %d\n", 2326f9fab69SDmitry Torokhov error); 2336f9fab69SDmitry Torokhov return error; 23410494dceSDaniel Mack } 23510494dceSDaniel Mack 2367fbef0d1SDaniel Mack /* 2377fbef0d1SDaniel Mack * Disable the device for now. It will be enabled once the 2387fbef0d1SDaniel Mack * input device is opened. 2397fbef0d1SDaniel Mack */ 240720bebdfSDmitry Torokhov eeti_ts_stop(eeti); 24110494dceSDaniel Mack 2420378008aSDmitry Torokhov error = input_register_device(input); 2430378008aSDmitry Torokhov if (error) 2440378008aSDmitry Torokhov return error; 2450378008aSDmitry Torokhov 24610494dceSDaniel Mack return 0; 24710494dceSDaniel Mack } 24810494dceSDaniel Mack 24902b6a58bSJingoo Han static int __maybe_unused eeti_ts_suspend(struct device *dev) 25010494dceSDaniel Mack { 25185012fffSMark Brown struct i2c_client *client = to_i2c_client(dev); 252720bebdfSDmitry Torokhov struct eeti_ts *eeti = i2c_get_clientdata(client); 253720bebdfSDmitry Torokhov struct input_dev *input_dev = eeti->input; 2547fbef0d1SDaniel Mack 2557fbef0d1SDaniel Mack mutex_lock(&input_dev->mutex); 2567fbef0d1SDaniel Mack 2577fbef0d1SDaniel Mack if (input_dev->users) 258720bebdfSDmitry Torokhov eeti_ts_stop(eeti); 2597fbef0d1SDaniel Mack 2607fbef0d1SDaniel Mack mutex_unlock(&input_dev->mutex); 26110494dceSDaniel Mack 26210494dceSDaniel Mack if (device_may_wakeup(&client->dev)) 2632d388499SDmitry Torokhov enable_irq_wake(client->irq); 26410494dceSDaniel Mack 26510494dceSDaniel Mack return 0; 26610494dceSDaniel Mack } 26710494dceSDaniel Mack 26802b6a58bSJingoo Han static int __maybe_unused eeti_ts_resume(struct device *dev) 26910494dceSDaniel Mack { 27085012fffSMark Brown struct i2c_client *client = to_i2c_client(dev); 271720bebdfSDmitry Torokhov struct eeti_ts *eeti = i2c_get_clientdata(client); 272720bebdfSDmitry Torokhov struct input_dev *input_dev = eeti->input; 27310494dceSDaniel Mack 27410494dceSDaniel Mack if (device_may_wakeup(&client->dev)) 2752d388499SDmitry Torokhov disable_irq_wake(client->irq); 27610494dceSDaniel Mack 2777fbef0d1SDaniel Mack mutex_lock(&input_dev->mutex); 2787fbef0d1SDaniel Mack 2797fbef0d1SDaniel Mack if (input_dev->users) 280720bebdfSDmitry Torokhov eeti_ts_start(eeti); 2817fbef0d1SDaniel Mack 2827fbef0d1SDaniel Mack mutex_unlock(&input_dev->mutex); 2837fbef0d1SDaniel Mack 28410494dceSDaniel Mack return 0; 28510494dceSDaniel Mack } 28685012fffSMark Brown 28785012fffSMark Brown static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume); 28810494dceSDaniel Mack 28910494dceSDaniel Mack static const struct i2c_device_id eeti_ts_id[] = { 29010494dceSDaniel Mack { "eeti_ts", 0 }, 29110494dceSDaniel Mack { } 29210494dceSDaniel Mack }; 29310494dceSDaniel Mack MODULE_DEVICE_TABLE(i2c, eeti_ts_id); 29410494dceSDaniel Mack 295e32d7f1bSDaniel Mack #ifdef CONFIG_OF 296e32d7f1bSDaniel Mack static const struct of_device_id of_eeti_ts_match[] = { 297e32d7f1bSDaniel Mack { .compatible = "eeti,exc3000-i2c", }, 298e32d7f1bSDaniel Mack { } 299e32d7f1bSDaniel Mack }; 300e32d7f1bSDaniel Mack #endif 301e32d7f1bSDaniel Mack 30210494dceSDaniel Mack static struct i2c_driver eeti_ts_driver = { 30310494dceSDaniel Mack .driver = { 30410494dceSDaniel Mack .name = "eeti_ts", 30585012fffSMark Brown .pm = &eeti_ts_pm, 306e32d7f1bSDaniel Mack .of_match_table = of_match_ptr(of_eeti_ts_match), 30710494dceSDaniel Mack }, 30810494dceSDaniel Mack .probe = eeti_ts_probe, 30910494dceSDaniel Mack .id_table = eeti_ts_id, 31010494dceSDaniel Mack }; 31110494dceSDaniel Mack 3121b92c1cfSAxel Lin module_i2c_driver(eeti_ts_driver); 31310494dceSDaniel Mack 31410494dceSDaniel Mack MODULE_DESCRIPTION("EETI Touchscreen driver"); 315fd8135b6SDaniel Mack MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>"); 31610494dceSDaniel Mack MODULE_LICENSE("GPL"); 317