From patchwork Tue Mar 1 08:32:19 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: 8463071 Return-Path: X-Original-To: patchwork-linux-input@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 10C7C9F38C for ; Tue, 1 Mar 2016 08:33:01 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 56A342025A for ; Tue, 1 Mar 2016 08:32:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7F3FD20148 for ; Tue, 1 Mar 2016 08:32:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752381AbcCAIc4 (ORCPT ); Tue, 1 Mar 2016 03:32:56 -0500 Received: from mail-db3on0091.outbound.protection.outlook.com ([157.55.234.91]:52991 "EHLO emea01-db3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751200AbcCAIcz (ORCPT ); Tue, 1 Mar 2016 03:32:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NextfourGroupOy.onmicrosoft.com; s=selector1-nextfour-com; h=From:To:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=HrWl4YT2ACLvZZ9ED3X2xJv48+HbZT1ZZaFm+Eihfds=; b=FOc+t/mHgouN4hdrmll6HbdqNvoT1JgmE/0RFs96+1qkRNTjNHiIbNxKKv1+GP3X/SBUIEi6Bq0NRyswAAUoC2Y9hfKe4HNsHm+eha7ju/5X8EUO3ONeh3+0tfHVlLPr1E2xdxgCLgcyhhT5P2M787mHROFSSKpS5wxzUcWIn88= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=none action=none header.from=nextfour.com; Received: from mjp-ThinkCentre-M92p.nextfour.com (194.157.170.34) by DB5PR07MB0901.eurprd07.prod.outlook.com (10.161.196.17) with Microsoft SMTP Server (TLS) id 15.1.415.20; Tue, 1 Mar 2016 08:32:51 +0000 From: To: CC: , , =?UTF-8?q?Mika=20Penttil=C3=A4?= Subject: [PATCH v4 1/2] Input: Driver for SiS 9200 family I2C touchscreen controller. Date: Tue, 1 Mar 2016 10:32:19 +0200 Message-ID: <1456821140-26720-2-git-send-email-mika.penttila@nextfour.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1456821140-26720-1-git-send-email-mika.penttila@nextfour.com> References: <1456821140-26720-1-git-send-email-mika.penttila@nextfour.com> MIME-Version: 1.0 X-Originating-IP: [194.157.170.34] X-ClientProxiedBy: DB5PR06CA0017.eurprd06.prod.outlook.com (25.162.165.27) To DB5PR07MB0901.eurprd07.prod.outlook.com (25.161.196.17) X-Microsoft-Exchange-Diagnostics: 1; DB5PR07MB0901; 2:BwqYtyqEN48TwR15i+XY4sBd+Y5Vs3KtXsq+j2YIplVxKJUANXFVTNBHPZVYI7aL1qMasakDlrNvbu7092XvzoK0Midfhw4BcnAl6CuUAoPGen0F0z6KFpTDsy1uMysNeKw+vj+YOfGqmdMYKEJLBA==; 3:Slope4y+M/atC2DdyLLZpWOGQs3uwSfZ/6sgvyWPhDlPlFKs0B010GaYtUFG7z5FFN67YCLzVOTlkK6n2rWztMLBHCDRBa+xqdRbLsa3NwbtEzn5pIbaQ6DQ5b5Hsav4; 25:MUHZoMLR82QTtAwPsm9V3YFVIGOaWRaUHY1/ybz+KRQN+GjjZ+pDCmuyBB9Cjamq12Z3pHdHpHRuaOBKGSF2QYarjfzryHNCfBHw5/M7jPTXP2liI/TzGh9JdPwDHowv7wwY28ve591pBDQycqRYxt/ZanOA+q8CSFCiK1OJBdZeRTXOMCFj+9m7QyINkH/rfKxUzn9BaXDrd5n8PZYe5YMCq+FcHnojMcKycyMsggGgBXrELhxw2Dp5mDnr9eb3s6I0Jem3BcTB1wP6sDTJKU/ij3k5adNf/F6Uc4HHxbfT5dVNQ+kxpf9Z4LnEyGUMaa19hZKJYUDwTNK1lkk1/g== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:DB5PR07MB0901; X-MS-Office365-Filtering-Correlation-Id: 0321d6c7-40e8-4c99-33b5-08d341ac141f X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001); SRVR:DB5PR07MB0901; BCL:0; PCL:0; RULEID:; SRVR:DB5PR07MB0901; X-Microsoft-Exchange-Diagnostics: 1; DB5PR07MB0901; 4:oGpPYgGcX8b+Ss0BpM7lNsxKMCZdnmudRVQahpyKvB0tForhfXnJwPkmmDhQtvR9itybwuHgvdoJV4Szab/MvOyFnxGaUEHJjp/lzh3p4S7OCSv2ieBjzdAz8MHzoa86DP7KI1lYcaNxdzQOvMuHQxzvQHgMddMZ/PiiUuAIRB+5zlJx578HWDmMvR2xtVbSGjf6BsNWGekg+ee9Bm9Ef02ydlcVwne6U/NLVUjHfQ8fsM8X6MLydvG1joxpmcnK3nytnkFEdW5uY/3ufhy/z6HAp0q2rI9lfz5JNJ2RNUPzzne6j6B/9MLcrbNYwZexXXIqICWxx4fr0rGVUoVCE4HY9EIb98Q1sGEk/KvnAveH6o4/3eXB0Hy6nU9Ckiws X-Forefront-PRVS: 086831DFB4 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(36756003)(86362001)(575784001)(110136002)(189998001)(50466002)(4001430100002)(50226001)(5004730100002)(2351001)(23676002)(229853001)(5820100001)(33646002)(76176999)(5001960100003)(92566002)(2950100001)(42186005)(50986999)(66066001)(5008740100001)(2906002)(53416004)(1096002)(40100003)(122386002)(47776003)(2876002)(77096005)(4326007)(107886002)(87976001)(586003)(6116002)(19580405001)(3846002)(86152002)(2870700001)(19580395003)(81156008)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:DB5PR07MB0901; H:mjp-ThinkCentre-M92p.nextfour.com; FPR:; SPF:None; MLV:nov; PTR:InfoNoRecords; LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtEQjVQUjA3TUIwOTAxOzIzOjgyMWJFK256OVdFcGFDTS9xcmNiOTNVeG5R?= =?utf-8?B?YkN2QlJZS0VpTklyY25TRW5UNkNTenR6RmJ4RFFROGU4cDFMVzBjbjV4Sngv?= =?utf-8?B?cWV3STl2SGJDRUpSLytQenFsWk10VHkvVFViUFZTNkpEUWN6S3hvZW5LT3Y3?= =?utf-8?B?NXVoUGsxU3cyWnl3TkdNVW81Y0xBN0tUMzdxdUEwUnZRRzJuK2QzUlEyOUFm?= =?utf-8?B?Uzd3Q3kyc3VpV3Y5RFJrRjN1VW8vRWdORVlEMXE3M3FzV2hSeGhGbmlUTWJJ?= =?utf-8?B?R3djRWJyNnkzOG5iazFocmQxemVJazZBNzR3VFdMbVpmenl5dmh6eEdhSTIy?= =?utf-8?B?VnJNVW1SY1BoQ2lyc1R0eW11Sy9WSjAwY2dNdmIyYnA1OTZWWitYTmtaYjJ2?= =?utf-8?B?Y3FWYUcvYmpZL3BwdThFbDE2MTNQQ1FYbUpleWRJRVhQSDliWmdOWWNNRlVS?= =?utf-8?B?NzBEYmNZQVZJSHdza3lpUkZzV085cGQ2bG5TZzVDRktOWWw4ZUlVSFZlQjVo?= =?utf-8?B?cDBCVlFmeGgzVXIzTkUrVVNpRmdlMVJlQk9UNXA4MFdjNG5QZkRUbGNhYzZT?= =?utf-8?B?WDRUU2VoTDlBdjVFbmluV04zS2ZrZVJsVFRUSGpQdTk5Q2E3bnA3dzhUeU44?= =?utf-8?B?Y2UxSFVZR0l4T1U0K0dIYk5XSlNKMDJyMHhibUYzUkFSUVpJUGNiU2tkSWdr?= =?utf-8?B?bWgvV2dRNWdVNlIxbWUxQUhTRDNFcnFOUW5qN0dXekg2blFxMFY5VUpUUGVl?= =?utf-8?B?OHRBTDhEMjc4SjBVSkJpR0o3YXkwQXU0Tzl1U09CTUV0VFJIWk8yelV0WVRZ?= =?utf-8?B?c3Y2QzBKNFJKR1UrVDA0Wjg3TkxOdnhHcExjaDJKcXMrSUJ0NXljemx1Uk15?= =?utf-8?B?d1hmaUZqaWJpRHBzeW5EMVNCUFFybm1JQ21aRzZ2bXNxcHlISGx6QTNmOWJo?= =?utf-8?B?TzZWdlRneVFoUXdTZ2paNk8xeTR6L0pqeXFkRHpQUGhEaTN2aGN5TEx6bFVV?= =?utf-8?B?dGNnWDBkUUxURVJsWE5rbCtkc0hyRUVuYmlZeGJ1OE1ZV1pNZlJKVmZhbkd4?= =?utf-8?B?b1A0cG5EZE1tWFBDRTF3NmQ2UzJRZi9XZkZ5ZlNqSHhvLzNGOGJPNDBDMldi?= =?utf-8?B?endhc3JPWmo0eHV1WFRvSTVvbTNDVFU3aGFuS3R3MUVVL3NnRlhnZFV4MkVH?= =?utf-8?B?Q2VLck9BMmQ0b2hJMkV5M0Y2dnB3d0tnR25KRWlYamFNWitYUUNhbTNrelJk?= =?utf-8?B?Qy9semV6eGlwSDJhWFNoaFQ0R3FtYVRFNnRwa0dia3p1c1ZueUR2Qk5OSG5Q?= =?utf-8?B?eUtTbjM5RExCSWNnc1A2QVBkRjlSUzhvNGpnVEtQNFRvczE2cUtnVmw5QnpM?= =?utf-8?B?SURpQTMreVJHUmpSNXI5SlFtb0g5ZGRxOUVLdE1uMGhIZmZnZEFDZFduQlNI?= =?utf-8?B?N2Q0anN4SWVJcnhBUUFMazRaM2NCMFJRNTczamVFVC9Qc0dEYTBhWnpRQTl4?= =?utf-8?B?VTFaMGl2RGU2OGJmREc3YzI5ZnVCZVcvQ2ZsYVh4bGJvSTNnRDA4L0FTT3Ew?= =?utf-8?B?Vkx5SkV2d3hOcThPNVYyN3Jyd2JGVEE9PQ==?= X-Microsoft-Exchange-Diagnostics: 1; DB5PR07MB0901; 5:aSm6S6LWP/2Q0tQDC/IR9G8wplnCUDDayl8/f5DkdaWFIe6NUg7CxcP3RZXXR2CCd2UH0tLK24rY+2w7MiNyHeZKct/L1eG36xmnLxhgjWjmLVvy4H5j3d7ySTdxb8EoJPjfWiRXeRhBpMjBzrHBag==; 24:vASRji7UP+Gf397GSx/E37aHcZglINVrwQvbizYHef3ib324gLr6ZyEfCTjbZEuk9fhJJFDHi4jDkCM3Ze35UpVDQmEXxyIigpSnnGXzhpQ= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: nextfour.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Mar 2016 08:32:51.3391 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB5PR07MB0901 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Mika Penttilä Multitouch protocol B support. 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 | 11 + drivers/input/touchscreen/Makefile | 1 + drivers/input/touchscreen/sis_i2c.c | 461 ++++++++++++++++++++++++++++++++++++ 3 files changed, 473 insertions(+) create mode 100644 drivers/input/touchscreen/sis_i2c.c diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig index 3370a1a..577ef0b 100644 --- a/drivers/input/touchscreen/Kconfig +++ b/drivers/input/touchscreen/Kconfig @@ -1133,5 +1133,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 fa6bc56..98fd8d3 100644 --- a/drivers/input/touchscreen/Makefile +++ b/drivers/input/touchscreen/Makefile @@ -61,6 +61,7 @@ obj-$(CONFIG_TOUCHSCREEN_PCAP) += pcap_ts.o obj-$(CONFIG_TOUCHSCREEN_PENMOUNT) += penmount.o obj-$(CONFIG_TOUCHSCREEN_PIXCIR) += pixcir_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 diff --git a/drivers/input/touchscreen/sis_i2c.c b/drivers/input/touchscreen/sis_i2c.c new file mode 100644 index 0000000..f1b66fa --- /dev/null +++ b/drivers/input/touchscreen/sis_i2c.c @@ -0,0 +1,461 @@ +/* drivers/input/touchscreen/sis_i2c.c + * - I2C Touch panel driver for SiS 9200 family + * + * Copyright (C) 2011 SiS, Inc. + * Copyright (C) 2015 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 +#include +#include +#include +#include +#include + +#define SIS_I2C_NAME "sis_i2c_ts" +#define MAX_FINGERS 10 + +#define SIS_MAX_X 4095 +#define SIS_MAX_Y 4095 + +#define PACKET_BUFFER_SIZE 128 + +#define SIS_CMD_NORMAL 0x0 + +#define TOUCHDOWN 0x3 +#define TOUCHUP 0x0 +#define MAX_BYTE 64 +#define PRESSURE_MAX 255 + +/* Resolution diagonal */ +#define AREA_LENGTH_LONGER 5792 +/*((SIS_MAX_X^2) + (SIS_MAX_Y^2))^0.5*/ +#define AREA_LENGTH_SHORT 5792 +#define AREA_UNIT (5792/32) + +#define P_BYTECOUNT 0 +#define ALL_IN_ONE_PACKAGE 0x10 +#define IS_TOUCH(x) ((x) & 0x1) +#define IS_HIDI2C(x) (((x) & 0xF) == 0x06) +#define IS_AREA(x) (((x) >> 4) & 0x1) +#define IS_PRESSURE(x) (((x) >> 5) & 0x1) +#define IS_SCANTIME(x) (((x) >> 6) & 0x1) + +#define NORMAL_LEN_PER_POINT 6 +#define AREA_LEN_PER_POINT 2 +#define PRESSURE_LEN_PER_POINT 1 + +#define TOUCH_FORMAT 0x1 +#define HIDI2C_FORMAT 0x6 +#define P_REPORT_ID 2 +#define BYTE_BYTECOUNT 2 +#define BYTE_REPORTID 1 +#define BYTE_CRC_HIDI2C 0 +#define BYTE_CRC_I2C 2 +#define BYTE_SCANTIME 2 + +struct _touchpoint { + u8 id; + u16 x, y; + u16 pressure; + u16 width; + u16 height; +}; + +struct sistp_driver_data { + int fingers; + struct _touchpoint pt[MAX_FINGERS]; +}; + +struct sis_ts_data { + struct gpio_desc *irq_gpiod; + struct gpio_desc *reset_gpiod; + struct i2c_client *client; + struct input_dev *input_dev; +struct sistp_driver_data tpinfo; +}; + +static int sis_cul_unit(u8 report_id) +{ + int ret = NORMAL_LEN_PER_POINT; + + if (report_id != ALL_IN_ONE_PACKAGE) { + + if (IS_AREA(report_id) /*&& IS_TOUCH(report_id)*/) + ret += AREA_LEN_PER_POINT; + + if (IS_PRESSURE(report_id)) + ret += PRESSURE_LEN_PER_POINT; + } + + return ret; +} + +static int sis_readpacket(struct i2c_client *client, u8 cmd, u8 *buf) +{ + u8 tmpbuf[MAX_BYTE] = {0}; + int ret = -1; + int touchnum = 0; + int p_count = 0; + int touch_format_id = 0; + int location = 0; + bool read_first = true; + +/* + * I2C touch report format + * + * The controller sends one or two + * 64 byte reports (depending on how many + * contacts down etc). We read first 64 bytes + * and then the second chunk if needed. + * The packets are individually CRC + * checksummed. + * + * buf[0] = Low 8 bits of byte count value + * buf[1] = High 8 bits of byte counte value + * buf[2] = Report ID + * buf[touch num * 6 + 2 ] = Touch information + * 1 touch point has 6 bytes, it could be none if no touch + * buf[touch num * 6 + 3] = Touch numbers + * + * One touch point information include 6 bytes, the order is + * + * 1. status = touch down or touch up + * 2. id = finger id + * 3. x axis low 8 bits + * 4. x axis high 8 bits + * 5. y axis low 8 bits + * 6. y axis high 8 bits + */ + do { + if (location >= PACKET_BUFFER_SIZE) { + dev_err(&client->dev, "sis_readpacket: buffer overflow\n"); + return -1; + } + + ret = i2c_master_recv(client, tmpbuf, MAX_BYTE); + + if (ret <= 0) { + return touchnum; + } else if (tmpbuf[P_BYTECOUNT] > MAX_BYTE) { + dev_err(&client->dev, "sis_readpacket: invalid bytecout\n"); + return -1; + } + + if (tmpbuf[P_BYTECOUNT] < 10) + return touchnum; + + if (read_first) + if (tmpbuf[P_BYTECOUNT] == 0) + return 0; /* touchnum is 0 */ + + touch_format_id = tmpbuf[P_REPORT_ID] & 0xf; + + if ((touch_format_id != TOUCH_FORMAT) + && (touch_format_id != HIDI2C_FORMAT) + && (tmpbuf[P_REPORT_ID] != ALL_IN_ONE_PACKAGE)) { + dev_err(&client->dev, "sis_readpacket: invalid reportid\n"); + return -1; + } + + p_count = (int) tmpbuf[P_BYTECOUNT] - 1; /* start from 0 */ + if (tmpbuf[P_REPORT_ID] != ALL_IN_ONE_PACKAGE) { + if (IS_TOUCH(tmpbuf[P_REPORT_ID])) { + p_count -= BYTE_CRC_I2C; /* delete 2 byte crc */ + } else if (IS_HIDI2C(tmpbuf[P_REPORT_ID])) { + p_count -= BYTE_CRC_HIDI2C; + } else { + dev_err(&client->dev, "sis_readpacket: delete crc error\n"); + return -1; + } + if (IS_SCANTIME(tmpbuf[P_REPORT_ID])) + p_count -= BYTE_SCANTIME; + } + + if (read_first) + touchnum = tmpbuf[p_count]; + else { + if (tmpbuf[p_count] != 0) { + dev_err(&client->dev, "sis_readpacket: nonzero point count in tail packet\n"); + return -1; + } + } + + if ((touch_format_id != HIDI2C_FORMAT) && + (tmpbuf[P_BYTECOUNT] > 3)) { + int crc_end = p_count + + (IS_SCANTIME(tmpbuf[P_REPORT_ID]) * 2); + u16 buf_crc = + crc_itu_t(0, tmpbuf + 2, crc_end - 1); + int l_package_crc = + (IS_SCANTIME(tmpbuf[P_REPORT_ID]) * 2) + + p_count + 1; + u16 package_crc = + get_unaligned_le16(&tmpbuf[l_package_crc]); + if (buf_crc != package_crc) { + dev_err(&client->dev, "sis_readpacket: CRC Error\n"); + return -1; + } + } + + memcpy(&buf[location], &tmpbuf[0], 64); + /* Buf_Data [0~63] [64~128] */ + location += MAX_BYTE; + read_first = false; + } while (tmpbuf[P_REPORT_ID] != ALL_IN_ONE_PACKAGE && + tmpbuf[p_count] > 5); + + return touchnum; +} + +static irqreturn_t sis_ts_irq_handler(int irq, void *dev_id) +{ + struct sis_ts_data *ts = dev_id; + struct sistp_driver_data *tpinfo = &ts->tpinfo; + + int ret = -1; + int point_unit; + u8 buf[PACKET_BUFFER_SIZE] = {0}; + u8 i = 0, fingers = 0; + u8 px = 0, py = 0, pstatus = 0; + u8 p_area = 0; + u8 p_preasure = 0; + int slot; + +redo: + /* I2C or SMBUS block data read */ + ret = sis_readpacket(ts->client, SIS_CMD_NORMAL, buf); + + if (ret < 0) + goto recheck_irq; + + else if (ret == 0) { + fingers = 0; + goto label_sync_input; + } + + point_unit = sis_cul_unit(buf[P_REPORT_ID]); + fingers = ret; + + tpinfo->fingers = fingers = (fingers > MAX_FINGERS ? 0 : fingers); + + for (i = 0; i < fingers; i++) { + if ((buf[P_REPORT_ID] != ALL_IN_ONE_PACKAGE) && (i >= 5)) { + pstatus = BYTE_BYTECOUNT + BYTE_REPORTID + + ((i - 5) * point_unit); + pstatus += 64; + } else { + pstatus = BYTE_BYTECOUNT + BYTE_REPORTID + + (i * point_unit); + } + /* X and Y coordinate locations */ + px = pstatus + 2; + py = px + 2; + + if ((buf[pstatus]) == TOUCHUP) { + tpinfo->pt[i].width = 0; + tpinfo->pt[i].height = 0; + tpinfo->pt[i].pressure = 0; + } else if (buf[P_REPORT_ID] == ALL_IN_ONE_PACKAGE + && (buf[pstatus]) == TOUCHDOWN) { + tpinfo->pt[i].width = 1; + tpinfo->pt[i].height = 1; + tpinfo->pt[i].pressure = 1; + } else if ((buf[pstatus]) == TOUCHDOWN) { + p_area = py + 2; + p_preasure = py + 2 + (IS_AREA(buf[P_REPORT_ID]) * 2); + + if (IS_AREA(buf[P_REPORT_ID])) { + tpinfo->pt[i].width = buf[p_area]; + tpinfo->pt[i].height = buf[p_area + 1]; + } else { + tpinfo->pt[i].width = 1; + tpinfo->pt[i].height = 1; + } + + if (IS_PRESSURE(buf[P_REPORT_ID])) + tpinfo->pt[i].pressure = (buf[p_preasure]); + else + tpinfo->pt[i].pressure = 1; + } else { + dev_err(&ts->client->dev, "Touch status error\n"); + goto recheck_irq; + } + tpinfo->pt[i].id = (buf[pstatus + 1]); + tpinfo->pt[i].x = le16_to_cpu(get_unaligned_le16(&buf[px])); + tpinfo->pt[i].y = le16_to_cpu(get_unaligned_le16(&buf[py])); + + slot = input_mt_get_slot_by_key( + ts->input_dev, tpinfo->pt[i].id); + + if (slot < 0) + continue; + + input_mt_slot(ts->input_dev, slot); + input_mt_report_slot_state(ts->input_dev, + MT_TOOL_FINGER, tpinfo->pt[i].pressure); + + if (tpinfo->pt[i].pressure) { + + tpinfo->pt[i].width *= AREA_UNIT; + input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, + tpinfo->pt[i].width); + tpinfo->pt[i].height *= AREA_UNIT; + input_report_abs(ts->input_dev, ABS_MT_TOUCH_MINOR, + tpinfo->pt[i].height); + input_report_abs(ts->input_dev, ABS_MT_PRESSURE, + tpinfo->pt[i].pressure); + input_report_abs(ts->input_dev, ABS_MT_POSITION_X, + tpinfo->pt[i].x); + input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, + tpinfo->pt[i].y); + } + } + +label_sync_input: + input_mt_sync_frame(ts->input_dev); + input_sync(ts->input_dev); + +recheck_irq: + if (ts->irq_gpiod) { + /* + * If provided and interrupt gpio and + * irq is still asserted, + * read data until interrupt is deasserted. + */ + ret = gpiod_get_value_cansleep(ts->irq_gpiod); + if (ret == 1) + goto redo; + } + + return IRQ_HANDLED; +} + +static void sis_ts_reset(struct i2c_client *client, struct sis_ts_data *ts) +{ + + ts->irq_gpiod = devm_gpiod_get_optional(&client->dev, + "irq", GPIOD_IN); + ts->reset_gpiod = devm_gpiod_get_optional(&client->dev, + "reset", GPIOD_OUT_LOW); + + if (ts->reset_gpiod) { + /* Get out of reset */ + msleep(1); + gpiod_set_value(ts->reset_gpiod, 1); + msleep(1); + gpiod_set_value(ts->reset_gpiod, 0); + msleep(100); + } +} + +static int sis_ts_probe( + struct i2c_client *client, const struct i2c_device_id *id) +{ + int error = 0; + struct sis_ts_data *ts = NULL; + + ts = devm_kzalloc(&client->dev, sizeof(struct sis_ts_data), GFP_KERNEL); + if (!ts) + return -ENOMEM; + + sis_ts_reset(client, ts); + + ts->client = client; + i2c_set_clientdata(client, ts); + + ts->input_dev = devm_input_allocate_device(&client->dev); + if (!ts->input_dev) { + dev_err(&client->dev, "sis_ts_probe: Failed to allocate input device\n"); + return -ENOMEM; + } + + ts->input_dev->name = "sis_touch"; + ts->input_dev->id.bustype = BUS_I2C; + + input_set_abs_params(ts->input_dev, ABS_MT_PRESSURE, + 0, PRESSURE_MAX, 0, 0); + input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, + 0, AREA_LENGTH_LONGER, 0, 0); + input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MINOR, + 0, AREA_LENGTH_SHORT, 0, 0); + input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, + 0, SIS_MAX_X, 0, 0); + input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, + 0, SIS_MAX_Y, 0, 0); + + error = input_mt_init_slots(ts->input_dev, MAX_FINGERS, + INPUT_MT_DROP_UNUSED | INPUT_MT_DIRECT); + + if (error) { + dev_err(&client->dev, + "failed to initialize MT slots: %d\n", error); + return error; + } + + error = input_register_device(ts->input_dev); + if (error) { + dev_err(&client->dev, + "unable to register input device: %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, "request irq failed\n"); + return error; + } + + return 0; +} + +static const struct i2c_device_id sis_ts_id[] = { + { SIS_I2C_NAME, 0 }, + { } +}; + +MODULE_DEVICE_TABLE(i2c, sis_ts_id); + +#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 struct i2c_driver sis_ts_driver = { + .probe = sis_ts_probe, + .id_table = sis_ts_id, + .driver = { + .name = SIS_I2C_NAME, + .of_match_table = of_match_ptr(sis_ts_dt_ids), + }, +}; + +module_i2c_driver(sis_ts_driver); +MODULE_DESCRIPTION("SiS 9200 Family Touchscreen Driver"); +MODULE_LICENSE("GPL v2");