From patchwork Wed Apr 7 16:04:03 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Felipe Balbi X-Patchwork-Id: 91084 X-Patchwork-Delegate: tony@atomide.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o37G5qBL005807 for ; Wed, 7 Apr 2010 16:05:52 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932902Ab0DGQFv (ORCPT ); Wed, 7 Apr 2010 12:05:51 -0400 Received: from smtp.nokia.com ([192.100.105.134]:62328 "EHLO mgw-mx09.nokia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932823Ab0DGQFR (ORCPT ); Wed, 7 Apr 2010 12:05:17 -0400 Received: from vaebh105.NOE.Nokia.com (vaebh105.europe.nokia.com [10.160.244.31]) by mgw-mx09.nokia.com (Switch-3.3.3/Switch-3.3.3) with ESMTP id o37G56Aw007315 for ; Wed, 7 Apr 2010 11:05:17 -0500 Received: from esebh102.NOE.Nokia.com ([172.21.138.183]) by vaebh105.NOE.Nokia.com with Microsoft SMTPSVC(6.0.3790.3959); Wed, 7 Apr 2010 19:04:56 +0300 Received: from mgw-da02.ext.nokia.com ([147.243.128.26]) by esebh102.NOE.Nokia.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.3959); Wed, 7 Apr 2010 19:04:55 +0300 Received: from localhost.localdomain (esdhcp04088.research.nokia.com [172.21.40.88]) by mgw-da02.ext.nokia.com (Switch-3.3.3/Switch-3.3.3) with ESMTP id o37G4YMu015130; Wed, 7 Apr 2010 19:04:51 +0300 From: felipe.balbi@nokia.com To: Linux OMAP Mailing List Cc: Felipe Balbi Subject: [RFC PATCH 12/37] cbus: introduce cbus_send/receive_data wrappers Date: Wed, 7 Apr 2010 19:04:03 +0300 Message-Id: <1270656268-7034-13-git-send-email-felipe.balbi@nokia.com> X-Mailer: git-send-email 1.7.0.rc0.33.g7c3932 In-Reply-To: <1270656268-7034-1-git-send-email-felipe.balbi@nokia.com> References: <1270656268-7034-1-git-send-email-felipe.balbi@nokia.com> X-OriginalArrivalTime: 07 Apr 2010 16:04:55.0906 (UTC) FILETIME=[10BC2C20:01CAD66C] X-Nokia-AV: Clean Sender: linux-omap-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-omap@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Wed, 07 Apr 2010 16:05:52 +0000 (UTC) diff --git a/drivers/cbus/cbus.c b/drivers/cbus/cbus.c index 0791251..bae7b3e 100644 --- a/drivers/cbus/cbus.c +++ b/drivers/cbus/cbus.c @@ -58,7 +58,8 @@ struct cbus_host { static struct cbus_host *cbus_host; -static int cbus_send_bit(struct cbus_host *host, int bit, int set_to_input) +static int cbus_send_bit(struct cbus_host *host, unsigned bit, + unsigned input) { int ret = 0; @@ -66,7 +67,7 @@ static int cbus_send_bit(struct cbus_host *host, int bit, int set_to_input) gpio_set_value(host->clk_gpio, 1); /* The data bit is read on the rising edge of CLK */ - if (set_to_input) + if (input) ret = gpio_direction_input(host->dat_gpio); gpio_set_value(host->clk_gpio, 0); @@ -74,6 +75,23 @@ static int cbus_send_bit(struct cbus_host *host, int bit, int set_to_input) return ret; } +static int cbus_send_data(struct cbus_host *host, unsigned data, unsigned len, + unsigned input) +{ + int ret = 0; + int i; + + for (i = len; i > 0; i--) { + ret = cbus_send_bit(host, data & (1 << (i - 1)), + input && (i == 1)); + if (ret < 0) + goto out; + } + +out: + return ret; +} + static int cbus_receive_bit(struct cbus_host *host) { int ret; @@ -88,12 +106,31 @@ out: return ret; } +static int cbus_receive_data(struct cbus_host *host, unsigned len) +{ + int ret = 0; + int i; + + for (i = 16; i > 0; i--) { + int bit = cbus_receive_bit(host); + + if (bit < 0) + goto out; + + if (bit) + ret |= 1 << (i - 1); + } + +out: + return ret; +} + static int cbus_transfer(struct cbus_host *host, unsigned rw, unsigned dev, unsigned reg, unsigned data) { unsigned long flags; + int input = 0; int ret = 0; - int i; /* We don't want interrupts disturbing our transfer */ spin_lock_irqsave(&host->lock, flags); @@ -105,12 +142,10 @@ static int cbus_transfer(struct cbus_host *host, unsigned rw, unsigned dev, gpio_direction_output(host->dat_gpio, 1); /* Send the device address */ - for (i = 3; i > 0; i--) { - ret = cbus_send_bit(host, dev & (1 << (i - 1)), 0); - if (ret < 0) { - dev_dbg(host->dev, "failed sending device addr\n"); - goto out; - } + ret = cbus_send_data(host, dev, 3, 0); + if (ret < 0) { + dev_dbg(host->dev, "failed sending device addr\n"); + goto out; } /* Send the rw flag */ @@ -121,44 +156,29 @@ static int cbus_transfer(struct cbus_host *host, unsigned rw, unsigned dev, } /* Send the register address */ - for (i = 5; i > 0; i--) { - int set_to_input = 0; - - if (rw && i == 1) - set_to_input = 1; + if (rw) + input = true; - ret = cbus_send_bit(host, reg & (1 << (i - 1)), set_to_input); - if (ret < 0) { - dev_dbg(host->dev, "failed sending register addr\n"); - goto out; - } + ret = cbus_send_data(host, reg, 5, input); + if (ret < 0) { + dev_dbg(host->dev, "failed sending register addr\n"); + goto out; } if (!rw) { - for (i = 16; i > 0; i--) { - ret = cbus_send_bit(host, data & (1 << (i - 1)), 0); - if (ret < 0) { - dev_dbg(host->dev, "failed sending data\n"); - goto out; - } + ret = cbus_send_data(host, data, 16, 0); + if (ret < 0) { + dev_dbg(host->dev, "failed sending data\n"); + goto out; } } else { gpio_set_value(host->clk_gpio, 1); - for (i = 16; i > 0; i--) { - u8 bit = cbus_receive_bit(host); - - if (bit < 0) { - dev_dbg(host->dev, "failed receiving data\n"); - goto out; - } - - if (bit) - data |= 1 << (i - 1); + ret = cbus_receive_data(host, 16); + if (ret < 0) { + dev_dbg(host->dev, "failed receiving data\n"); + goto out; } - - /* return the data received */ - ret = data; } /* Indicate end of transfer, SEL goes up until next transfer */