From patchwork Tue Apr 5 18:15:12 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johan Hovold X-Patchwork-Id: 688451 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p35IFmq0027340 for ; Tue, 5 Apr 2011 18:15:49 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754716Ab1DESPs (ORCPT ); Tue, 5 Apr 2011 14:15:48 -0400 Received: from mail-ey0-f174.google.com ([209.85.215.174]:54438 "EHLO mail-ey0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755595Ab1DESPr (ORCPT ); Tue, 5 Apr 2011 14:15:47 -0400 Received: by eyx24 with SMTP id 24so205122eyx.19 for ; Tue, 05 Apr 2011 11:15:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=ELSzIZf9+NeVOS3YsyFjJhFArQvdoMK2VKbMa3yhvas=; b=N0Am9cODiU0dSuaALslwkSFEC0ppqy2bxvFV7x0AtnwYMJMJ0QB2Rg+dySxrtgGDxO oYvOFo7xJHcrytebnsFreBfbtWvjcjhR86Lx4WM+4iUojk8Rw7rXprJW9yTFmx6F1O6D 9O3qYOCINpAzm4S0o1O1wk9xwrrMe2BWhK7VM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=U6fTmeMkT/V6/M8MbRbfwAec+1aCflhJgTYGz87FrmCJba0rEEXShJjycpOZ97rvSW Mdt6PbKlAe4d83Z9LlGnoOISoebtjMMOxuinFQD1xDhV86KnnrE941yMYgO35o+LFAXH BoSHtcLyOLTXNidWpEWv53Ni4ObPmZBaHDvC8= Received: by 10.213.2.69 with SMTP id 5mr2307472ebi.115.1302027346222; Tue, 05 Apr 2011 11:15:46 -0700 (PDT) Received: from xi (c83-249-240-82.bredband.comhem.se [83.249.240.82]) by mx.google.com with ESMTPS id q53sm4147688eeh.11.2011.04.05.11.15.44 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 05 Apr 2011 11:15:45 -0700 (PDT) Received: from johan by xi with local (Exim 4.74) (envelope-from ) id 1Q7An1-00074q-2C; Tue, 05 Apr 2011 20:15:43 +0200 From: Johan Hovold To: Dmitry Torokhov Cc: Daniel Mack , linux-input@vger.kernel.org, Johan Hovold Subject: [PATCH 1/2] input: rotary-encoder: refactor and clean up Date: Tue, 5 Apr 2011 20:15:12 +0200 Message-Id: <1302027313-27130-2-git-send-email-jhovold@gmail.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1302027313-27130-1-git-send-email-jhovold@gmail.com> References: <1302027313-27130-1-git-send-email-jhovold@gmail.com> Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Tue, 05 Apr 2011 18:15:49 +0000 (UTC) Refactor state retrieval and event reporting in interrupt handler. Remove a few empty lines. Signed-off-by: Johan Hovold Reviewed-by: H Hartley Sweeten --- drivers/input/misc/rotary_encoder.c | 83 ++++++++++++++++++++-------------- 1 files changed, 49 insertions(+), 34 deletions(-) diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c index 7e64d01..253e502 100644 --- a/drivers/input/misc/rotary_encoder.c +++ b/drivers/input/misc/rotary_encoder.c @@ -40,58 +40,74 @@ struct rotary_encoder { unsigned char dir; /* 0 - clockwise, 1 - CCW */ }; -static irqreturn_t rotary_encoder_irq(int irq, void *dev_id) +static int rotary_encoder_get_state(struct rotary_encoder_platform_data *pdata) { - struct rotary_encoder *encoder = dev_id; - struct rotary_encoder_platform_data *pdata = encoder->pdata; int a = !!gpio_get_value(pdata->gpio_a); int b = !!gpio_get_value(pdata->gpio_b); - int state; a ^= pdata->inverted_a; b ^= pdata->inverted_b; - state = (a << 1) | b; - switch (state) { + return ((a << 1) | b); +} - case 0x0: - if (!encoder->armed) - break; +static void rotary_encoder_report_event(struct rotary_encoder *encoder) +{ + struct rotary_encoder_platform_data *pdata = encoder->pdata; + unsigned pos; + int value; - if (pdata->relative_axis) { - input_report_rel(encoder->input, pdata->axis, - encoder->dir ? -1 : 1); - } else { - unsigned int pos = encoder->pos; - - if (encoder->dir) { - /* turning counter-clockwise */ - if (pdata->rollover) - pos += pdata->steps; - if (pos) - pos--; - } else { - /* turning clockwise */ - if (pdata->rollover || pos < pdata->steps) - pos++; - } + if (pdata->relative_axis) { + if (encoder->dir) + value = -1; + else + value = 1; + + input_report_rel(encoder->input, pdata->axis, value); + } else { + pos = encoder->pos; + + if (encoder->dir) { + /* turning counter-clockwise */ if (pdata->rollover) - pos %= pdata->steps; - encoder->pos = pos; - input_report_abs(encoder->input, pdata->axis, - encoder->pos); + pos += pdata->steps; + if (pos) + pos--; + } else { + /* turning clockwise */ + if (pdata->rollover || pos < pdata->steps) + pos++; } - input_sync(encoder->input); + if (pdata->rollover) + pos %= pdata->steps; + + encoder->pos = pos; + input_report_abs(encoder->input, pdata->axis, encoder->pos); + } + + input_sync(encoder->input); +} + +static irqreturn_t rotary_encoder_irq(int irq, void *dev_id) +{ + struct rotary_encoder *encoder = dev_id; + int state; + + state = rotary_encoder_get_state(encoder->pdata); + + switch (state) { + case 0x0: + if (!encoder->armed) + break; + rotary_encoder_report_event(encoder); encoder->armed = false; break; - case 0x1: case 0x2: if (encoder->armed) encoder->dir = state - 1; break; - case 0x3: encoder->armed = true; break; @@ -254,4 +270,3 @@ MODULE_ALIAS("platform:" DRV_NAME); MODULE_DESCRIPTION("GPIO rotary encoder driver"); MODULE_AUTHOR("Daniel Mack "); MODULE_LICENSE("GPL v2"); -