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; 440378008aSDmitry Torokhov bool running; 4510494dceSDaniel Mack }; 4610494dceSDaniel Mack 4710494dceSDaniel Mack #define EETI_TS_BITDEPTH (11) 4810494dceSDaniel Mack #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1) 4910494dceSDaniel Mack 50272c03bbSDmitry Torokhov #define REPORT_BIT_PRESSED BIT(0) 51272c03bbSDmitry Torokhov #define REPORT_BIT_AD0 BIT(1) 52272c03bbSDmitry Torokhov #define REPORT_BIT_AD1 BIT(2) 53272c03bbSDmitry Torokhov #define REPORT_BIT_HAS_PRESSURE BIT(6) 5410494dceSDaniel Mack #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH) 5510494dceSDaniel Mack 560378008aSDmitry Torokhov static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf) 5710494dceSDaniel Mack { 580378008aSDmitry Torokhov unsigned int res; 590378008aSDmitry Torokhov u16 x, y; 6010494dceSDaniel Mack 6110494dceSDaniel Mack res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1)); 62e9f66cdbSDmitry Torokhov 63e9f66cdbSDmitry Torokhov x = get_unaligned_be16(&buf[1]); 64e9f66cdbSDmitry Torokhov y = get_unaligned_be16(&buf[3]); 6510494dceSDaniel Mack 6610494dceSDaniel Mack /* fix the range to 11 bits */ 6710494dceSDaniel Mack x >>= res - EETI_TS_BITDEPTH; 6810494dceSDaniel Mack y >>= res - EETI_TS_BITDEPTH; 6910494dceSDaniel Mack 7010494dceSDaniel Mack if (buf[0] & REPORT_BIT_HAS_PRESSURE) 71720bebdfSDmitry Torokhov input_report_abs(eeti->input, ABS_PRESSURE, buf[5]); 7210494dceSDaniel Mack 73a114cbd0SDaniel Mack touchscreen_report_pos(eeti->input, &eeti->props, x, y, false); 740378008aSDmitry Torokhov input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED); 75720bebdfSDmitry Torokhov input_sync(eeti->input); 7610494dceSDaniel Mack } 7710494dceSDaniel Mack 7810494dceSDaniel Mack static irqreturn_t eeti_ts_isr(int irq, void *dev_id) 7910494dceSDaniel Mack { 80720bebdfSDmitry Torokhov struct eeti_ts *eeti = dev_id; 810378008aSDmitry Torokhov int len; 820378008aSDmitry Torokhov int error; 830378008aSDmitry Torokhov char buf[6]; 8410494dceSDaniel Mack 850378008aSDmitry Torokhov do { 860378008aSDmitry Torokhov len = i2c_master_recv(eeti->client, buf, sizeof(buf)); 870378008aSDmitry Torokhov if (len != sizeof(buf)) { 880378008aSDmitry Torokhov error = len < 0 ? len : -EIO; 890378008aSDmitry Torokhov dev_err(&eeti->client->dev, 900378008aSDmitry Torokhov "failed to read touchscreen data: %d\n", 910378008aSDmitry Torokhov error); 920378008aSDmitry Torokhov break; 930378008aSDmitry Torokhov } 940378008aSDmitry Torokhov 950378008aSDmitry Torokhov if (buf[0] & 0x80) { 960378008aSDmitry Torokhov /* Motion packet */ 970378008aSDmitry Torokhov eeti_ts_report_event(eeti, buf); 980378008aSDmitry Torokhov } 99d99caa47SDmitry Torokhov } while (eeti->running && 100d99caa47SDmitry Torokhov eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio)); 10110494dceSDaniel Mack 10210494dceSDaniel Mack return IRQ_HANDLED; 10310494dceSDaniel Mack } 10410494dceSDaniel Mack 105720bebdfSDmitry Torokhov static void eeti_ts_start(struct eeti_ts *eeti) 10610494dceSDaniel Mack { 1070378008aSDmitry Torokhov eeti->running = true; 1080378008aSDmitry Torokhov wmb(); 1092d388499SDmitry Torokhov enable_irq(eeti->client->irq); 1107fbef0d1SDaniel Mack } 1117fbef0d1SDaniel Mack 112720bebdfSDmitry Torokhov static void eeti_ts_stop(struct eeti_ts *eeti) 1137fbef0d1SDaniel Mack { 1140378008aSDmitry Torokhov eeti->running = false; 1150378008aSDmitry Torokhov wmb(); 1162d388499SDmitry Torokhov disable_irq(eeti->client->irq); 1177fbef0d1SDaniel Mack } 1187fbef0d1SDaniel Mack 1197fbef0d1SDaniel Mack static int eeti_ts_open(struct input_dev *dev) 1207fbef0d1SDaniel Mack { 121720bebdfSDmitry Torokhov struct eeti_ts *eeti = input_get_drvdata(dev); 1227fbef0d1SDaniel Mack 123720bebdfSDmitry Torokhov eeti_ts_start(eeti); 12410494dceSDaniel Mack 12510494dceSDaniel Mack return 0; 12610494dceSDaniel Mack } 12710494dceSDaniel Mack 12810494dceSDaniel Mack static void eeti_ts_close(struct input_dev *dev) 12910494dceSDaniel Mack { 130720bebdfSDmitry Torokhov struct eeti_ts *eeti = input_get_drvdata(dev); 13110494dceSDaniel Mack 132720bebdfSDmitry Torokhov eeti_ts_stop(eeti); 13310494dceSDaniel Mack } 13410494dceSDaniel Mack 1355298cc4cSBill Pemberton static int eeti_ts_probe(struct i2c_client *client, 13610494dceSDaniel Mack const struct i2c_device_id *idp) 13710494dceSDaniel Mack { 1386f9fab69SDmitry Torokhov struct device *dev = &client->dev; 139720bebdfSDmitry Torokhov struct eeti_ts *eeti; 14010494dceSDaniel Mack struct input_dev *input; 1416f9fab69SDmitry Torokhov int error; 14210494dceSDaniel Mack 1437fbef0d1SDaniel Mack /* 1447fbef0d1SDaniel Mack * In contrast to what's described in the datasheet, there seems 14510494dceSDaniel Mack * to be no way of probing the presence of that device using I2C 14610494dceSDaniel Mack * commands. So we need to blindly believe it is there, and wait 1477fbef0d1SDaniel Mack * for interrupts to occur. 1487fbef0d1SDaniel Mack */ 14910494dceSDaniel Mack 1506f9fab69SDmitry Torokhov eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL); 151720bebdfSDmitry Torokhov if (!eeti) { 1526f9fab69SDmitry Torokhov dev_err(dev, "failed to allocate driver data\n"); 153ad03ae44SGuenter Roeck return -ENOMEM; 15410494dceSDaniel Mack } 15510494dceSDaniel Mack 1566f9fab69SDmitry Torokhov input = devm_input_allocate_device(dev); 15710494dceSDaniel Mack if (!input) { 1586f9fab69SDmitry Torokhov dev_err(dev, "Failed to allocate input device.\n"); 1596f9fab69SDmitry Torokhov return -ENOMEM; 16010494dceSDaniel Mack } 16110494dceSDaniel Mack 16242e02a6aSDmitry Torokhov input_set_capability(input, EV_KEY, BTN_TOUCH); 16310494dceSDaniel Mack 16410494dceSDaniel Mack input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0); 16510494dceSDaniel Mack input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0); 16610494dceSDaniel Mack input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0); 16710494dceSDaniel Mack 168a114cbd0SDaniel Mack touchscreen_parse_properties(input, false, &eeti->props); 169a114cbd0SDaniel Mack 17010494dceSDaniel Mack input->name = client->name; 17110494dceSDaniel Mack input->id.bustype = BUS_I2C; 17210494dceSDaniel Mack input->open = eeti_ts_open; 17310494dceSDaniel Mack input->close = eeti_ts_close; 17410494dceSDaniel Mack 175720bebdfSDmitry Torokhov eeti->client = client; 176720bebdfSDmitry Torokhov eeti->input = input; 17710494dceSDaniel Mack 178d99caa47SDmitry Torokhov eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN); 179d99caa47SDmitry Torokhov if (IS_ERR(eeti->attn_gpio)) 180d99caa47SDmitry Torokhov return PTR_ERR(eeti->attn_gpio); 18125a70e38SDaniel Mack 182720bebdfSDmitry Torokhov i2c_set_clientdata(client, eeti); 183720bebdfSDmitry Torokhov input_set_drvdata(input, eeti); 18410494dceSDaniel Mack 1850378008aSDmitry Torokhov error = devm_request_threaded_irq(dev, client->irq, 1860378008aSDmitry Torokhov NULL, eeti_ts_isr, 187d422be5fSDmitry Torokhov IRQF_ONESHOT, 188720bebdfSDmitry Torokhov client->name, eeti); 1896f9fab69SDmitry Torokhov if (error) { 1906f9fab69SDmitry Torokhov dev_err(dev, "Unable to request touchscreen IRQ: %d\n", 1916f9fab69SDmitry Torokhov error); 1926f9fab69SDmitry Torokhov return error; 19310494dceSDaniel Mack } 19410494dceSDaniel Mack 1957fbef0d1SDaniel Mack /* 1967fbef0d1SDaniel Mack * Disable the device for now. It will be enabled once the 1977fbef0d1SDaniel Mack * input device is opened. 1987fbef0d1SDaniel Mack */ 199720bebdfSDmitry Torokhov eeti_ts_stop(eeti); 20010494dceSDaniel Mack 2010378008aSDmitry Torokhov error = input_register_device(input); 2020378008aSDmitry Torokhov if (error) 2030378008aSDmitry Torokhov return error; 2040378008aSDmitry Torokhov 20510494dceSDaniel Mack return 0; 20610494dceSDaniel Mack } 20710494dceSDaniel Mack 20802b6a58bSJingoo Han static int __maybe_unused eeti_ts_suspend(struct device *dev) 20910494dceSDaniel Mack { 21085012fffSMark Brown struct i2c_client *client = to_i2c_client(dev); 211720bebdfSDmitry Torokhov struct eeti_ts *eeti = i2c_get_clientdata(client); 212720bebdfSDmitry Torokhov struct input_dev *input_dev = eeti->input; 2137fbef0d1SDaniel Mack 2147fbef0d1SDaniel Mack mutex_lock(&input_dev->mutex); 2157fbef0d1SDaniel Mack 2167fbef0d1SDaniel Mack if (input_dev->users) 217720bebdfSDmitry Torokhov eeti_ts_stop(eeti); 2187fbef0d1SDaniel Mack 2197fbef0d1SDaniel Mack mutex_unlock(&input_dev->mutex); 22010494dceSDaniel Mack 22110494dceSDaniel Mack if (device_may_wakeup(&client->dev)) 2222d388499SDmitry Torokhov enable_irq_wake(client->irq); 22310494dceSDaniel Mack 22410494dceSDaniel Mack return 0; 22510494dceSDaniel Mack } 22610494dceSDaniel Mack 22702b6a58bSJingoo Han static int __maybe_unused eeti_ts_resume(struct device *dev) 22810494dceSDaniel Mack { 22985012fffSMark Brown struct i2c_client *client = to_i2c_client(dev); 230720bebdfSDmitry Torokhov struct eeti_ts *eeti = i2c_get_clientdata(client); 231720bebdfSDmitry Torokhov struct input_dev *input_dev = eeti->input; 23210494dceSDaniel Mack 23310494dceSDaniel Mack if (device_may_wakeup(&client->dev)) 2342d388499SDmitry Torokhov disable_irq_wake(client->irq); 23510494dceSDaniel Mack 2367fbef0d1SDaniel Mack mutex_lock(&input_dev->mutex); 2377fbef0d1SDaniel Mack 2387fbef0d1SDaniel Mack if (input_dev->users) 239720bebdfSDmitry Torokhov eeti_ts_start(eeti); 2407fbef0d1SDaniel Mack 2417fbef0d1SDaniel Mack mutex_unlock(&input_dev->mutex); 2427fbef0d1SDaniel Mack 24310494dceSDaniel Mack return 0; 24410494dceSDaniel Mack } 24585012fffSMark Brown 24685012fffSMark Brown static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume); 24710494dceSDaniel Mack 24810494dceSDaniel Mack static const struct i2c_device_id eeti_ts_id[] = { 24910494dceSDaniel Mack { "eeti_ts", 0 }, 25010494dceSDaniel Mack { } 25110494dceSDaniel Mack }; 25210494dceSDaniel Mack MODULE_DEVICE_TABLE(i2c, eeti_ts_id); 25310494dceSDaniel Mack 254e32d7f1bSDaniel Mack #ifdef CONFIG_OF 255e32d7f1bSDaniel Mack static const struct of_device_id of_eeti_ts_match[] = { 256e32d7f1bSDaniel Mack { .compatible = "eeti,exc3000-i2c", }, 257e32d7f1bSDaniel Mack { } 258e32d7f1bSDaniel Mack }; 259e32d7f1bSDaniel Mack #endif 260e32d7f1bSDaniel Mack 26110494dceSDaniel Mack static struct i2c_driver eeti_ts_driver = { 26210494dceSDaniel Mack .driver = { 26310494dceSDaniel Mack .name = "eeti_ts", 26485012fffSMark Brown .pm = &eeti_ts_pm, 265e32d7f1bSDaniel Mack .of_match_table = of_match_ptr(of_eeti_ts_match), 26610494dceSDaniel Mack }, 26710494dceSDaniel Mack .probe = eeti_ts_probe, 26810494dceSDaniel Mack .id_table = eeti_ts_id, 26910494dceSDaniel Mack }; 27010494dceSDaniel Mack 2711b92c1cfSAxel Lin module_i2c_driver(eeti_ts_driver); 27210494dceSDaniel Mack 27310494dceSDaniel Mack MODULE_DESCRIPTION("EETI Touchscreen driver"); 274fd8135b6SDaniel Mack MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>"); 27510494dceSDaniel Mack MODULE_LICENSE("GPL"); 276