From patchwork Mon May 23 13:55:34 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Just X-Patchwork-Id: 9132341 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 DA655607D5 for ; Mon, 23 May 2016 19:56:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D101C2824F for ; Mon, 23 May 2016 19:56:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C49A22824E; Mon, 23 May 2016 19:56:41 +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=-5.2 required=2.0 tests=BAYES_00, DATE_IN_PAST_06_12, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,FREEMAIL_FROM,RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham 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 4DD022824E for ; Mon, 23 May 2016 19:56:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754669AbcEWT4k (ORCPT ); Mon, 23 May 2016 15:56:40 -0400 Received: from mail-io0-f194.google.com ([209.85.223.194]:33561 "EHLO mail-io0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754668AbcEWT4j (ORCPT ); Mon, 23 May 2016 15:56:39 -0400 Received: by mail-io0-f194.google.com with SMTP id s67so4895814ios.0 for ; Mon, 23 May 2016 12:56:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=AJQ23BIQBVi7oQXJm9wALB9NDfvGArXsJqQs1fRKWmY=; b=ZTPFrmQlgjx3VxK//yE/BPTfCVQteA9PluQacq/v8OZithLy18y1gt//LG1gywGBh2 pxX1zl/6ZJwA14GAgvp9Vb8z167CpuR6f6foC89KuN0+Q8g5kO2/t9wCCa7ORseLh+sA diQLAfMvlfzTmbEAOmcGNTOgNQFXI6Iev4WLaFDBKL3A7VLOEh6uR54ntzTrbyQfWI/O cOBzjhanyxPCpSYIlBo7UqmkRTQJ6Swr1CRpEpv3WGJf2a0VjuaYVQyagaUZEr6m4wbQ 3fDUQKQojYs5Yf0l1uWjwXHE29CkyxBECaiiLCt9w/LTKg5PlywdOK8+hSFBOzWAgvs2 6ynA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=AJQ23BIQBVi7oQXJm9wALB9NDfvGArXsJqQs1fRKWmY=; b=NLwvkNiAZ1XI3i+gfkOli0V6xMKF5IALXVjrUvQR5mvmZrUGsJPuSaNh5VyF6qao6r Sts4eXJFFIIzRha8H1KRf16FouWJROOoMoB7l6tC2+/dJ9tZd5ZGwBGH2wCyD+qbJjJL nVFD0JLHLWsRxVXYB7i+yVLUCebIr5bFR2KhimV5qeHzZSYUHFdoD1SbP7wWdbjARiUr HBOzeiCs+CzNdFJ23/LJayRsM7MobZ0gj+MFAeeEDt7qeo3VVIv9I4IG1dY75fOMuOci ho6SjuEFAigR+wTkOhtD9gB1hMq3Q1fGhRBw9y4yFKDZ0gJHhjVzIdaPBdcebaUyWbgJ TtDA== X-Gm-Message-State: AOPr4FXMkLWnDZI8WTGWE7TEeK493MRXzQzpT31cmC9lH9wmj2VzW8rchbVezj9ZBbrbew== X-Received: by 10.107.162.82 with SMTP id l79mr13565603ioe.193.1464033398753; Mon, 23 May 2016 12:56:38 -0700 (PDT) Received: from arch_usb.localdomain (S0106c0c1c0256baf.ed.shawcable.net. [70.74.127.58]) by smtp.gmail.com with ESMTPSA id k8sm4273124igf.7.2016.05.23.12.56.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 23 May 2016 12:56:38 -0700 (PDT) From: Stephen Just To: linux-input@vger.kernel.org, Dmitry Torokhov , Benjamin Tissoires Cc: Stephen Just , Bastien Nocera Subject: [PATCH 2/2] Input - surface3_spi: add surface pen support for Surface 3 Date: Mon, 23 May 2016 07:55:34 -0600 Message-Id: <1464011734-18657-3-git-send-email-stephenjust@gmail.com> X-Mailer: git-send-email 2.8.2 In-Reply-To: <1464011734-18657-1-git-send-email-stephenjust@gmail.com> References: <1464011734-18657-1-git-send-email-stephenjust@gmail.com> 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 This change creates a second input device which will handle input from a Surface Pen. The Surface Pen supplies a different packet header than touch events, so it is simple to handle one or the other. This patch handles both the newer Surface Pen with one button, and the older variant with a second button to switch to Eraser mode. Signed-off-by: Stephen Just Reviewed-by: Benjamin Tissoires --- drivers/input/touchscreen/surface3_spi.c | 102 +++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/drivers/input/touchscreen/surface3_spi.c b/drivers/input/touchscreen/surface3_spi.c index 204a162..52a2cde 100644 --- a/drivers/input/touchscreen/surface3_spi.c +++ b/drivers/input/touchscreen/surface3_spi.c @@ -28,11 +28,13 @@ #define SURFACE3_PACKET_SIZE 264 #define SURFACE3_REPORT_TOUCH 0xd2 +#define SURFACE3_REPORT_PEN 0x16 struct surface3_ts_data { struct spi_device *spi; struct gpio_desc *gpiod_rst[2]; struct input_dev *input_dev; + struct input_dev *pen_input_dev; u8 rd_buf[SURFACE3_PACKET_SIZE] ____cacheline_aligned; }; @@ -49,6 +51,14 @@ struct surface3_ts_data_finger { u32 padding; } __packed; +struct surface3_ts_data_pen { + u8 status; + __le16 x; + __le16 y; + __le16 pressure; + u8 padding; +} __packed; + static int surface3_spi_read(struct surface3_ts_data *ts_data) { struct spi_device *spi = ts_data->spi; @@ -114,6 +124,52 @@ static void surface3_spi_process_touch(struct surface3_ts_data *ts_data, u8 *dat input_sync(ts_data->input_dev); } +static void surface3_spi_report_pen(struct surface3_ts_data *ts_data, + struct surface3_ts_data_pen *pen) +{ + int st = pen->status; + int prox = st & 0x01; + int rubber = st & 0x18; + + input_report_key(ts_data->pen_input_dev, + BTN_TOUCH, + (st & 0x12)); + + input_report_key(ts_data->pen_input_dev, + BTN_TOOL_PEN, + prox && !rubber); + + input_report_key(ts_data->pen_input_dev, + BTN_TOOL_RUBBER, + prox && rubber); + + if (st) { + input_report_key(ts_data->pen_input_dev, + BTN_STYLUS, + st & 0x04); + + input_report_abs(ts_data->pen_input_dev, + ABS_X, + get_unaligned_le16(&pen->x)); + input_report_abs(ts_data->pen_input_dev, + ABS_Y, + get_unaligned_le16(&pen->y)); + input_report_abs(ts_data->pen_input_dev, + ABS_PRESSURE, + get_unaligned_le16(&pen->pressure)); + } +} + +static void surface3_spi_process_pen(struct surface3_ts_data *ts_data, u8 *data) +{ + struct surface3_ts_data_pen *pen; + + pen = (struct surface3_ts_data_pen *)&data[15]; + + surface3_spi_report_pen(ts_data, pen); + input_sync(ts_data->pen_input_dev); +} + static void surface3_spi_process(struct surface3_ts_data *ts_data) { const char header[] = {0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e, 0x01}; @@ -127,6 +183,8 @@ static void surface3_spi_process(struct surface3_ts_data *ts_data) if (data[9] == SURFACE3_REPORT_TOUCH) surface3_spi_process_touch(ts_data, data); + else if (data[9] == SURFACE3_REPORT_PEN) + surface3_spi_process_pen(ts_data, data); else dev_err(&ts_data->spi->dev, "%s unknown packet type: %x, ignoring...\n", @@ -224,6 +282,46 @@ static int surface3_spi_create_touch_input(struct surface3_ts_data *data) return 0; } +static int surface3_spi_create_pen_input(struct surface3_ts_data *data) +{ + struct input_dev *input; + int error; + + input = devm_input_allocate_device(&data->spi->dev); + if (!input) + return -ENOMEM; + + data->pen_input_dev = input; + + __set_bit(INPUT_PROP_DIRECT, input->propbit); + __set_bit(INPUT_PROP_POINTER, input->propbit); + input_set_abs_params(input, ABS_X, 0, 9600, 0, 0); + input_abs_set_res(input, ABS_X, 40); + input_set_abs_params(input, ABS_Y, 0, 7200, 0, 0); + input_abs_set_res(input, ABS_Y, 48); + input_set_abs_params(input, ABS_PRESSURE, 0, 1024, 0, 0); + input_set_capability(input, EV_KEY, BTN_TOUCH); + input_set_capability(input, EV_KEY, BTN_STYLUS); + input_set_capability(input, EV_KEY, BTN_TOOL_PEN); + input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER); + + input->name = "Surface3 SPI Pen Input"; + input->phys = "input/ts"; + input->id.bustype = BUS_SPI; + input->id.vendor = 0x045e; /* Microsoft */ + input->id.product = 0x0002; + input->id.version = 0x0000; + + error = input_register_device(input); + if (error) { + dev_err(&data->spi->dev, + "Failed to register input device: %d", error); + return error; + } + + return 0; +} + static int surface3_spi_probe(struct spi_device *spi) { struct surface3_ts_data *data; @@ -255,6 +353,10 @@ static int surface3_spi_probe(struct spi_device *spi) if (error) return error; + error = surface3_spi_create_pen_input(data); + if (error) + return error; + error = devm_request_threaded_irq(&spi->dev, spi->irq, NULL, surface3_spi_irq_handler, IRQF_ONESHOT,