From patchwork Tue Aug 2 09:52:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Mika_Penttil=C3=A4?= X-Patchwork-Id: 9260699 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4954660754 for ; Wed, 3 Aug 2016 02:44:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3717B28534 for ; Wed, 3 Aug 2016 02:44:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2B72128542; Wed, 3 Aug 2016 02:44:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EEBCA28534 for ; Wed, 3 Aug 2016 02:43:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757174AbcHCCn6 (ORCPT ); Tue, 2 Aug 2016 22:43:58 -0400 Received: from mail-db5eur01on0051.outbound.protection.outlook.com ([104.47.2.51]:57834 "EHLO EUR01-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1757308AbcHCCnx (ORCPT ); Tue, 2 Aug 2016 22:43:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NextfourGroupOy.onmicrosoft.com; s=selector1-nextfour-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=LYG7OEalPK/vxaAybzEn3+tCxrjiAp1JBRNXHOBclHQ=; b=k9vvsfxc0ftOJaVSaujBUyP+bU6hfXQ5bV4kJUR79VfZmS8KIzBCJdpfAf2jL0FDAX/GGqKJ7lTP51RTtmBLR0pwYjIY0TyrRKkKN1Rp7bJ09IMmFviZWWtGTUPTHeSgGJ9I2wfNi1mo5cQeDh5ileSon4p5lspPQrG5TfER4p8= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=mika.penttila@nextfour.com; Received: from mjp-ThinkCentre-M92p.nextfour.com (194.157.170.34) by HE1PR0701MB2171.eurprd07.prod.outlook.com (10.168.36.20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.549.15; Tue, 2 Aug 2016 09:52:52 +0000 From: To: , CC: , , =?UTF-8?q?Mika=20Penttil=C3=A4?= Subject: [PATCH v6 1/2] Input: Driver for SiS-9200 family I2C touchscreen controller Date: Tue, 2 Aug 2016 12:52:30 +0300 Message-ID: <1470131552-13401-2-git-send-email-mika.penttila@nextfour.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1470131552-13401-1-git-send-email-mika.penttila@nextfour.com> References: <1470131552-13401-1-git-send-email-mika.penttila@nextfour.com> MIME-Version: 1.0 X-Originating-IP: [194.157.170.34] X-ClientProxiedBy: VI1PR0901CA0079.eurprd09.prod.outlook.com (10.167.203.175) To HE1PR0701MB2171.eurprd07.prod.outlook.com (10.168.36.20) X-MS-Office365-Filtering-Correlation-Id: a36ad801-65fa-4d89-d921-08d3babac58b X-Microsoft-Exchange-Diagnostics: 1; HE1PR0701MB2171; 2:JoFTD4KFsLtCNsfngxOaN/RX0lNiExNJwoZK1RtnQUrEYD2YfP5+UBR5Go6juBdsyBhyUqpgv5JjKbzF5NyVdYKZXi2XF1V8U3qDmMXwb3zWNzlPCUJnGxQ8iiY8RpJ1GgTla7rQlk0O/azkAzvyeOcv5/TiwKIVdDyTCAq8cNwRIrDRSpS1N/KEAPoOZym8; 3:cEdI5ScYsDweJALS6aklOsWAJyB6G8rXiV575I/LbVNVuZVcgaHyO5DNrmlVtV35KN9igVMz0YEQ4NgoM5mOLefFdjcEPsZD7UhsqUhiUMlVQCW3ahrzkptWVY3HvInR; 25:lLvXIC3KaP4dV5vesPJ7BpfyLX0RImabulHUQdRhkQgqu3sHjWPRvEDYpgaklOzTG50pG2Omo3/MGU0+K2jGuvQLTyBYgQs0uxPAQyDswodG7BCLrnyTkGR1ZKagwkkHgeVYQghcxGERL5uS64K4NJKBs5S0cVzHMo6yuD5xCA31fBqIQMoe/CS4BQX20RfTOjcHzt8DKyziY2HiPIJswK02PsMCBJoDx1cSLhtWEPqbW4IHbw0ACE9krE2YklodjavzZqOntT977kLTRypOHfYN8+iUxVMrfcFdIoAkwr+Ey2Z54GIiwzFYdYtih147z2su3CVjekbiSC/8yt1GFB0e9i2P19c2kCXJ8by1sL+Kjx8zCH9CJRPA8pG+YdPyl+o1M4BZU7BZsOX97EI/5tYsovzlhVL08Gcc1rLTrcc= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:HE1PR0701MB2171; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0701MB2171; 31:Q86sThZMzSyMqnrYa/azrQ39u6o141dyj7Fe5DNqA5nuAD4snOF/jwzx6kN2Ztnymc3X3iRDEQzszEQL0mPekaakBv/YYvGXM/jHexEtMvQ15O4So07vwQoEm3oRl86zM9AtVc5UO1l1tZweqsqQx2k7wKnn4UpTg3uNqCRpJ+6t9OOp9YQfe1tWA1awvmZx5MedVKXu/NP/KDZBSRwjz/G5EaWRrGDix/Y32hXfMLU=; 4:TKY9RAbTNzOnG8UUIyIr27KO387+mhDwgNHE49e8g2kDJF97Dmbya/fz/O1dAqJh5Rg/VyqTtlNfXuRQ0BYLtPVUv+fKJxItdOwbIGaDytwp2mwbkxucJAIr7gyvj+hZN2Xjw5M4qC5W0jfDSuXHxq62az+W9FZLe98ix8CCI8kKobJI/dhtjtDvumMPDJaNDSMUV6Z5771A5GwYif6bpZFqqMvVlI8sy2OcBh9iiC+PiMc7OzoP+DQN0MtwTOD7zMXdfnog6VvasZTC4SqPbO2QDx+M5y9MJi9N7moE4GHRwIlrkVWzjC+bXTmDAu7f+vQ5LmkBlHD93yGcM5kH02xdFc71FLiXIo9KlvBbuoDrRevJXycVn3j6GPs+jPdeW4d6Eq5WHXM90uTV4dNCKD1a9prgboOGmLl/GFIBA4p+QDFvQYqjgManha5IDeuV X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(20558992708506)(211171220733660); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046); SRVR:HE1PR0701MB2171; BCL:0; PCL:0; RULEID:; SRVR:HE1PR0701MB2171; X-Forefront-PRVS: 0022134A87 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(189002)(49754004)(199003)(23676002)(189998001)(2870700001)(107886002)(305945005)(86152002)(7736002)(7846002)(50226002)(68736007)(575784001)(4326007)(105586002)(81166006)(2876002)(36756003)(4001430100002)(229853001)(106356001)(2950100001)(2906002)(92566002)(50466002)(47776003)(77096005)(97736004)(33646002)(5820100001)(69596002)(6116002)(586003)(50986999)(76176999)(101416001)(19580405001)(81156014)(66066001)(5001770100001)(3846002)(19580395003)(8676002)(42186005)(86362001)(53416004)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:HE1PR0701MB2171; H:mjp-ThinkCentre-M92p.nextfour.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: nextfour.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtIRTFQUjA3MDFNQjIxNzE7MjM6b1lzc3RRNEFuWHcrdmJMSk5CYmZwTCsr?= =?utf-8?B?Qk1YMTVyanNuZkdGUnd2ZEw3ZVRWa0RrbjBvdHJabUQzNT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2jDy3c93y/KAkfIc2x13lQNxpO2KdbnIaCJ+iA0Dr+w0o5enLw98O9PAxY0HxryAz7qM4RPQ7dVytC3M8HDIkTmPwO1/uLazQS6+VC5HQ2f2IzPE1cFnbXmfJZ9Ox6DfLtgVAtr+pWEntmEXE=; 5:CzegnCmDjAG354gNHIhhKM3AubPtIIuZ3/GaVIgjOxAacdRVpJAc1bopfzbQcJQjVktP4omdT7pCOOCrrC9SW30s2ZD/FL7ySt5OdM84hiN5Sl9jUYUDGLLJkEuFl+ESNkUcIz0IXgoLZYv+k55MzA==; 24:M1tXDtvsZsElgau6vFEWrBPBOS+yCDNdgo/IlOOZSMxO+he3E3qVLq8cXOD964QQcADDhgsnJW5pEzhHF6SsoRIOfVTLGj23XeEt4QOMtcY=; 7:yfP8ZWFaDfeGn+QTpakTsFjvetIn4wAQHvqltSWNzZdHa4PhdyvkQJw1GLax4ASNhdmVInPnhnKWFp37EN+dvj7UxrWWJi0aGKyJFqV17c9h72CbaEKAJF/ucl2IvQyd8gzPhJLgnML84+c5zLqfXQtqNb/YVfWYtP95JXEiN0aNq01miEzyIXl6JUmzy0OvbWviiYV6QJmVdskKSuS9dvbN/bIoMiyaYr4Zr1IaGblKZsThMaDRTmcYDPyrC0Vb SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: nextfour.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Aug 2016 09:52:52.3676 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0701MB2171 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Mika Penttilä Multitouch protocol B support. v6: - rebased to 4.7 - simplify logic : send input reports while reading i2c packets - more cleanups v5: - rebased to 4.6.0-rc6 v4: - cleanups and fixes according to review feedback - irq and reset gpios are now optional, if not spesified it is expected the firmware / hw takes care of reset states and irq flow - irq and reset gpio polarities come from dts/firmware - report parsing and reporting are done at once - error handling improvements - misc cleanups - added comments v3: - cleanup unused defines - added acked-bys from SiS v2: - use gpio descriptor api - probe cleanups - error handling cleanups Signed-off-by: Mika Penttilä Acked-by: Tammy Tseng Acked-by: Yuger Yu --- drivers/input/touchscreen/Kconfig | 12 ++ drivers/input/touchscreen/Makefile | 2 + drivers/input/touchscreen/sis_i2c.c | 409 ++++++++++++++++++++++++++++++++++++ 3 files changed, 423 insertions(+) create mode 100644 drivers/input/touchscreen/sis_i2c.c diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig index ee02dc7..0633e8c 100644 --- a/drivers/input/touchscreen/Kconfig +++ b/drivers/input/touchscreen/Kconfig @@ -1181,4 +1181,16 @@ config TOUCHSCREEN_ROHM_BU21023 To compile this driver as a module, choose M here: the module will be called bu21023_ts. +config TOUCHSCREEN_SIS_I2C + tristate "SiS 9200 family I2C touchscreen driver" + depends on I2C + depends on GPIOLIB + help + This enables support for SiS 9200 family over I2C based touchscreens. + + If unsure, say N. + + To compile this driver as a module, choose M here: the + module will be called sis_i2c. + endif diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile index 3315882..cbb484f 100644 --- a/drivers/input/touchscreen/Makefile +++ b/drivers/input/touchscreen/Makefile @@ -64,6 +64,7 @@ obj-$(CONFIG_TOUCHSCREEN_PENMOUNT) += penmount.o obj-$(CONFIG_TOUCHSCREEN_PIXCIR) += pixcir_i2c_ts.o obj-$(CONFIG_TOUCHSCREEN_RM_TS) += raydium_i2c_ts.o obj-$(CONFIG_TOUCHSCREEN_S3C2410) += s3c2410_ts.o +obj-$(CONFIG_TOUCHSCREEN_SIS_I2C) += sis_i2c.o obj-$(CONFIG_TOUCHSCREEN_ST1232) += st1232.o obj-$(CONFIG_TOUCHSCREEN_STMPE) += stmpe-ts.o obj-$(CONFIG_TOUCHSCREEN_SUN4I) += sun4i-ts.o @@ -97,3 +98,4 @@ obj-$(CONFIG_TOUCHSCREEN_TPS6507X) += tps6507x-ts.o obj-$(CONFIG_TOUCHSCREEN_ZFORCE) += zforce_ts.o obj-$(CONFIG_TOUCHSCREEN_COLIBRI_VF50) += colibri-vf50-ts.o obj-$(CONFIG_TOUCHSCREEN_ROHM_BU21023) += rohm_bu21023.o + diff --git a/drivers/input/touchscreen/sis_i2c.c b/drivers/input/touchscreen/sis_i2c.c new file mode 100644 index 0000000..51caad3 --- /dev/null +++ b/drivers/input/touchscreen/sis_i2c.c @@ -0,0 +1,409 @@ +/* + * Touch Screen driver for SiS 9200 family I2C Touch panels + * + * Copyright (C) 2015 SiS, Inc. + * Copyright (C) 2016 Nextfour Group + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SIS_I2C_NAME "sis_i2c_ts" + +/* + * The I2C packet format: + * le16 byte count + * u8 Report ID + * + * u8 Number of contacts + * le16 Scan Time (optional) + * le16 CRC + * + * One touch point information consists of 6+ bytes, the order is: + * u8 contact state + * u8 finger id + * le16 x axis + * le16 y axis + * u8 contact width (optional) + * u8 contact height (optional) + * u8 pressure (optional) + * + * Maximum amount of data transmitted in one shot is 64 bytes, if controller + * needs to report more contacts than fit in one packet it will send true number + * of contacts in first packet and 0 as number of contacts in second packet. + */ + +#define SIS_MAX_PACKET_SIZE 64 + +#define SIS_PKT_LEN_OFFSET 0 +#define SIS_PKT_REPORT_OFFSET 2 /* Report ID/type */ +#define SIS_PKT_CONTACT_OFFSET 3 /* First contact */ + +#define SIS_SCAN_TIME_LEN 2 + +/* Supported report types */ +#define SIS_ALL_IN_ONE_PACKAGE 0x10 +#define SIS_PKT_IS_TOUCH(x) (((x) & 0x0f) == 0x01) +#define SIS_PKT_IS_HIDI2C(x) (((x) & 0x0f) == 0x06) + +/* Contact properties within report */ +#define SIS_PKT_HAS_AREA(x) ((x) & BIT(4)) +#define SIS_PKT_HAS_PRESSURE(x) ((x) & BIT(5)) +#define SIS_PKT_HAS_SCANTIME(x) ((x) & BIT(6)) + +/* Contact size */ +#define SIS_BASE_LEN_PER_CONTACT 6 +#define SIS_AREA_LEN_PER_CONTACT 2 +#define SIS_PRESSURE_LEN_PER_CONTACT 1 + +/* Offsets within contact data */ +#define SIS_CONTACT_STATUS_OFFSET 0 +#define SIS_CONTACT_ID_OFFSET 1 /* Contact ID */ +#define SIS_CONTACT_X_OFFSET 2 +#define SIS_CONTACT_Y_OFFSET 4 +#define SIS_CONTACT_WIDTH_OFFSET 6 +#define SIS_CONTACT_HEIGHT_OFFSET 7 +#define SIS_CONTACT_PRESSURE_OFFSET(id) (SIS_PKT_HAS_AREA(id) ? 8 : 6) + +/* Individual contact state */ +#define SIS_STATUS_UP 0x0 +#define SIS_STATUS_DOWN 0x3 + +/* Touchscreen parameters */ +#define SIS_MAX_FINGERS 10 +#define SIS_MAX_X 4095 +#define SIS_MAX_Y 4095 +#define SIS_MAX_PRESSURE 255 + +/* Resolution diagonal */ +#define SIS_AREA_LENGTH_LONGER 5792 +/*((SIS_MAX_X^2) + (SIS_MAX_Y^2))^0.5*/ +#define SIS_AREA_LENGTH_SHORT 5792 +#define SIS_AREA_UNIT (5792/32) + +struct sis_ts_data { + struct i2c_client *client; + struct input_dev *input; + + struct gpio_desc *attn_gpio; + struct gpio_desc *reset_gpio; + + u8 packet[SIS_MAX_PACKET_SIZE]; +}; + +static int sis_read_packet(struct i2c_client *client, u8 *buf, + unsigned int *num_contacts, + unsigned int *contact_size) +{ + int count_idx; + int ret; + u16 len; + u16 crc, pkg_crc; + u8 report_id; + + ret = i2c_master_recv(client, buf, SIS_MAX_PACKET_SIZE); + if (ret <= 0) + return -EIO; + + len = get_unaligned_le16(&buf[SIS_PKT_LEN_OFFSET]); + if (len > SIS_MAX_PACKET_SIZE) { + dev_err(&client->dev, + "%s: invalid packet length (%d vs %d)\n", + __func__, len, SIS_MAX_PACKET_SIZE); + return -E2BIG; + } + + if (len < 10) + return -EINVAL; + + report_id = buf[SIS_PKT_REPORT_OFFSET]; + count_idx = len - 1; + *contact_size = SIS_BASE_LEN_PER_CONTACT; + + if (report_id != SIS_ALL_IN_ONE_PACKAGE) { + if (SIS_PKT_IS_TOUCH(report_id)) { + /* + * Calculate CRC ignoring packet length + * in the beginning and CRC transmitted + * at the end of the packet. + */ + crc = crc_itu_t(0, buf + 2, + len - 2 - 2); + pkg_crc = get_unaligned_le16(&buf[len - 2]); + + if (crc != pkg_crc) { + dev_err(&client->dev, + "%s: CRC Error (%d vs %d)\n", + __func__, crc, pkg_crc); + return -EINVAL; + } + + count_idx -= 2; + + } else if (!SIS_PKT_IS_HIDI2C(report_id)) { + dev_err(&client->dev, + "%s: invalid packet ID %#02x\n", + __func__, report_id); + return -EINVAL; + } + + if (SIS_PKT_HAS_SCANTIME(report_id)) + count_idx -= SIS_SCAN_TIME_LEN; + + if (SIS_PKT_HAS_AREA(report_id)) + *contact_size += SIS_AREA_LEN_PER_CONTACT; + if (SIS_PKT_HAS_PRESSURE(report_id)) + *contact_size += SIS_PRESSURE_LEN_PER_CONTACT; + } + + *num_contacts = buf[count_idx]; + return 0; +} + +static int sis_ts_report_contact(struct sis_ts_data *ts, const u8 *data, u8 id) +{ + struct input_dev *input = ts->input; + int slot; + u8 status = data[SIS_CONTACT_STATUS_OFFSET]; + u8 pressure; + u8 height, width; + + if (status != SIS_STATUS_DOWN && status != SIS_STATUS_UP) { + dev_err(&ts->client->dev, "Unexpected touch status: %#02x\n", + data[SIS_CONTACT_STATUS_OFFSET]); + return -EINVAL; + } + + slot = input_mt_get_slot_by_key(input, data[SIS_CONTACT_ID_OFFSET]); + if (slot < 0) + return -ENOENT; + + input_mt_slot(input, slot); + input_mt_report_slot_state(input, MT_TOOL_FINGER, + status == SIS_STATUS_DOWN); + + if (status == SIS_STATUS_DOWN) { + pressure = height = width = 1; + if (id != SIS_ALL_IN_ONE_PACKAGE) { + if (SIS_PKT_HAS_AREA(id)) { + width = data[SIS_CONTACT_WIDTH_OFFSET]; + height = data[SIS_CONTACT_HEIGHT_OFFSET]; + } + + if (SIS_PKT_HAS_PRESSURE(id)) + pressure = data[SIS_CONTACT_PRESSURE_OFFSET(id)]; + } + + input_report_abs(input, ABS_MT_TOUCH_MAJOR, + width * SIS_AREA_UNIT); + input_report_abs(input, ABS_MT_TOUCH_MINOR, + height * SIS_AREA_UNIT); + input_report_abs(input, ABS_MT_PRESSURE, pressure); + input_report_abs(input, ABS_MT_POSITION_X, + get_unaligned_le16(&data[SIS_CONTACT_X_OFFSET])); + input_report_abs(input, ABS_MT_POSITION_Y, + get_unaligned_le16(&data[SIS_CONTACT_Y_OFFSET])); + } + + return 0; +} + +static void sis_ts_handle_packet(struct sis_ts_data *ts) +{ + const u8 *contact; + unsigned int num_to_report = 0; + unsigned int num_contacts; + unsigned int num_reported; + unsigned int contact_size; + int error; + u8 report_id; + + do { + error = sis_read_packet(ts->client, ts->packet, + &num_contacts, &contact_size); + if (error) + break; + + if (num_to_report == 0) { + num_to_report = num_contacts; + } else if (num_contacts != 0) { + dev_err(&ts->client->dev, + "%s: nonzero (%d) point count in tail packet\n", + __func__, num_contacts); + break; + } + + report_id = ts->packet[SIS_PKT_REPORT_OFFSET]; + contact = &ts->packet[SIS_PKT_CONTACT_OFFSET]; + num_reported = 0; + + while (num_to_report > 0) { + error = sis_ts_report_contact(ts, contact, report_id); + if (error) + break; + + contact += contact_size; + num_to_report--; + num_reported++; + + if (report_id != SIS_ALL_IN_ONE_PACKAGE && + num_reported >= 5) { + /* + * The remainder of contacts is sent + * in the 2nd packet. + */ + break; + } + } + } while (num_to_report > 0); + + input_mt_sync_frame(ts->input); + input_sync(ts->input); +} + +static irqreturn_t sis_ts_irq_handler(int irq, void *dev_id) +{ + struct sis_ts_data *ts = dev_id; + + do { + sis_ts_handle_packet(ts); + } while (ts->attn_gpio && gpiod_get_value_cansleep(ts->attn_gpio)); + + return IRQ_HANDLED; +} + +static void sis_ts_reset(struct sis_ts_data *ts) +{ + if (ts->reset_gpio) { + /* Get out of reset */ + usleep_range(1000, 2000); + gpiod_set_value(ts->reset_gpio, 1); + usleep_range(1000, 2000); + gpiod_set_value(ts->reset_gpio, 0); + msleep(100); + } +} + +static int sis_ts_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct sis_ts_data *ts; + struct input_dev *input; + int error; + + ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); + if (!ts) + return -ENOMEM; + + ts->client = client; + i2c_set_clientdata(client, ts); + + ts->attn_gpio = devm_gpiod_get_optional(&client->dev, + "attn", GPIOD_IN); + if (IS_ERR(ts->attn_gpio)) { + error = PTR_ERR(ts->attn_gpio); + if (error != -EPROBE_DEFER) + dev_err(&client->dev, + "Failed to get attention GPIO: %d\n", error); + return error; + } + + ts->reset_gpio = devm_gpiod_get_optional(&client->dev, + "reset", GPIOD_OUT_LOW); + if (IS_ERR(ts->reset_gpio)) { + error = PTR_ERR(ts->reset_gpio); + if (error != -EPROBE_DEFER) + dev_err(&client->dev, + "Failed to get reset GPIO: %d\n", error); + return error; + } + + sis_ts_reset(ts); + + ts->input = input = devm_input_allocate_device(&client->dev); + if (!input) { + dev_err(&client->dev, "Failed to allocate input device\n"); + return -ENOMEM; + } + + input->name = "SiS Touchscreen"; + input->id.bustype = BUS_I2C; + + input_set_abs_params(input, ABS_MT_POSITION_X, 0, SIS_MAX_X, 0, 0); + input_set_abs_params(input, ABS_MT_POSITION_Y, 0, SIS_MAX_Y, 0, 0); + input_set_abs_params(input, ABS_MT_PRESSURE, 0, SIS_MAX_PRESSURE, 0, 0); + input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, + 0, SIS_AREA_LENGTH_LONGER, 0, 0); + input_set_abs_params(input, ABS_MT_TOUCH_MINOR, + 0, SIS_AREA_LENGTH_SHORT, 0, 0); + + error = input_mt_init_slots(input, SIS_MAX_FINGERS, INPUT_MT_DIRECT); + if (error) { + dev_err(&client->dev, + "Failed to initialize MT slots: %d\n", error); + return error; + } + + error = devm_request_threaded_irq(&client->dev, client->irq, + NULL, sis_ts_irq_handler, + IRQF_ONESHOT, + client->name, ts); + if (error) { + dev_err(&client->dev, "Failed to request IRQ: %d\n", error); + return error; + } + + error = input_register_device(ts->input); + if (error) { + dev_err(&client->dev, + "Failed to register input device: %d\n", error); + return error; + } + + return 0; +} +#ifdef CONFIG_OF +static const struct of_device_id sis_ts_dt_ids[] = { +{ .compatible = "sis,9200-ts" }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, sis_ts_dt_ids); +#endif + +static const struct i2c_device_id sis_ts_id[] = { + { SIS_I2C_NAME, 0 }, + { "9200-ts", 0 }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(i2c, sis_ts_id); + +static struct i2c_driver sis_ts_driver = { + .driver = { + .name = SIS_I2C_NAME, + .of_match_table = of_match_ptr(sis_ts_dt_ids), + }, + .probe = sis_ts_probe, + .id_table = sis_ts_id, +}; +module_i2c_driver(sis_ts_driver); + +MODULE_DESCRIPTION("SiS 9200 Family Touchscreen Driver"); +MODULE_LICENSE("GPL v2"); +MODULE_AUTHOR("Mika Penttilä ");