From patchwork Thu Apr 7 18:58:48 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 8776441 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 B321C9F3D1 for ; Thu, 7 Apr 2016 18:59:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CFFF120256 for ; Thu, 7 Apr 2016 18:59:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CC502201EC for ; Thu, 7 Apr 2016 18:59:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932547AbcDGS7M (ORCPT ); Thu, 7 Apr 2016 14:59:12 -0400 Received: from arcturus.kleine-koenig.org ([78.47.169.190]:58458 "EHLO arcturus.kleine-koenig.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932458AbcDGS7K (ORCPT ); Thu, 7 Apr 2016 14:59:10 -0400 Received: by arcturus.kleine-koenig.org (Postfix, from userid 1000) id 7CAE59BD09; Thu, 7 Apr 2016 20:59:01 +0200 (CEST) From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= To: linux-input@vger.kernel.org, devicetree@vger.kernel.org, Dmitry Torokhov , Rob Herring Cc: kernel@pengutronix.de Subject: [PATCH v3] Input: rotary_encoder - support binary encoding of states Date: Thu, 7 Apr 2016 20:58:48 +0200 Message-Id: <1460055528-13814-1-git-send-email-u.kleine-koenig@pengutronix.de> X-Mailer: git-send-email 2.8.0.rc3 MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable 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 It's not advisable to use this encoding, but to support existing devices add support for this to the driver. Signed-off-by: Uwe Kleine-König --- Notes: Changes since (implicit) v1, sent with Message-Id: 1458680914-4533-1-git-send-email-u.kleine-koenig@pengutronix.de - switch format of dt properties to use strings Changes since v2, sent with Message-Id: 1458806232-22403-1-git-send-email-u.kleine-koenig@pengutronix.de - be more strict about parsing the encoding string .../devicetree/bindings/input/rotary-encoder.txt | 4 ++++ drivers/input/misc/rotary_encoder.c | 26 +++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Documentation/devicetree/bindings/input/rotary-encoder.txt b/Documentation/devicetree/bindings/input/rotary-encoder.txt index 6c9f0c8a846c..e85ce3dea480 100644 --- a/Documentation/devicetree/bindings/input/rotary-encoder.txt +++ b/Documentation/devicetree/bindings/input/rotary-encoder.txt @@ -20,6 +20,8 @@ Optional properties: 2: Half-period mode 4: Quarter-period mode - wakeup-source: Boolean, rotary encoder can wake up the system. +- rotary-encoder,encoding: String, the method used to encode steps. + Supported are "gray" (the default and more common) and "binary". Deprecated properties: - rotary-encoder,half-period: Makes the driver work on half-period mode. @@ -34,6 +36,7 @@ Example: compatible = "rotary-encoder"; gpios = <&gpio 19 1>, <&gpio 20 0>; /* GPIO19 is inverted */ linux,axis = <0>; /* REL_X */ + rotary-encoder,encoding = "gray"; rotary-encoder,relative-axis; }; @@ -42,5 +45,6 @@ Example: gpios = <&gpio 21 0>, <&gpio 22 0>; linux,axis = <1>; /* ABS_Y */ rotary-encoder,steps = <24>; + rotary-encoder,encoding = "binary"; rotary-encoder,rollover; }; diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c index 96c486de49e0..1e17e28188c6 100644 --- a/drivers/input/misc/rotary_encoder.c +++ b/drivers/input/misc/rotary_encoder.c @@ -28,6 +28,11 @@ #define DRV_NAME "rotary-encoder" +enum rotary_encoder_encoding { + ROTENC_GRAY, + ROTENC_BINARY, +}; + struct rotary_encoder { struct input_dev *input; @@ -37,6 +42,7 @@ struct rotary_encoder { u32 axis; bool relative_axis; bool rollover; + enum rotary_encoder_encoding encoding; unsigned int pos; @@ -57,9 +63,11 @@ static unsigned rotary_encoder_get_state(struct rotary_encoder *encoder) for (i = 0; i < encoder->gpios->ndescs; ++i) { int val = gpiod_get_value_cansleep(encoder->gpios->desc[i]); - /* convert from gray encoding to normal */ - if (ret & 1) - val = !val; + + if (encoder->encoding == ROTENC_GRAY) + /* convert from gray encoding to binary */ + if (ret & 1) + val = !val; ret = ret << 1 | val; } @@ -183,6 +191,7 @@ static int rotary_encoder_probe(struct platform_device *pdev) struct device *dev = &pdev->dev; struct rotary_encoder *encoder; struct input_dev *input; + const char *encoding; irq_handler_t handler; u32 steps_per_period; unsigned int i; @@ -213,6 +222,17 @@ static int rotary_encoder_probe(struct platform_device *pdev) encoder->rollover = device_property_read_bool(dev, "rotary-encoder,rollover"); + err = device_property_read_string(dev, "rotary-encoder,encoding", + &encoding); + if (!err && !strcmp(encoding, "binary")) { + encoder->encoding = ROTENC_BINARY; + } else if (err || !strcmp(encoding, "gray")) { + encoder->encoding = ROTENC_GRAY; + } else { + dev_err(dev, "unknown encoding setting"); + return -EINVAL; + } + device_property_read_u32(dev, "linux,axis", &encoder->axis); encoder->relative_axis = device_property_read_bool(dev, "rotary-encoder,relative-axis");