From patchwork Thu Sep 12 12:36:45 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guennadi Liakhovetski X-Patchwork-Id: 274545 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 376852C039D for ; Thu, 12 Sep 2013 22:36:58 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754627Ab3ILMgz (ORCPT ); Thu, 12 Sep 2013 08:36:55 -0400 Received: from moutng.kundenserver.de ([212.227.126.187]:52597 "EHLO moutng.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754152Ab3ILMgw (ORCPT ); Thu, 12 Sep 2013 08:36:52 -0400 Received: from axis700.grange (dslb-088-076-068-089.pools.arcor-ip.net [88.76.68.89]) by mrelayeu.kundenserver.de (node=mreu0) with ESMTP (Nemesis) id 0Lj7Ag-1Vx0zn1ve2-00dasA; Thu, 12 Sep 2013 14:36:49 +0200 Received: from 6a.grange (6a.grange [192.168.1.11]) by axis700.grange (Postfix) with ESMTPS id F246B40BB6; Thu, 12 Sep 2013 14:36:48 +0200 (CEST) Received: from lyakh by 6a.grange with local (Exim 4.72) (envelope-from ) id 1VK68e-0002ln-P4; Thu, 12 Sep 2013 14:36:48 +0200 From: Guennadi Liakhovetski To: linux-i2c@vger.kernel.org Cc: Magnus Damm , linux-sh@vger.kernel.org, Wolfram Sang , devicetree@vger.kernel.org, Grant Likely , Rob Herring , Guennadi Liakhovetski Subject: [PATCH v2 2/5] i2c: rcar: get clock rate only once and simplify calculation Date: Thu, 12 Sep 2013 14:36:45 +0200 Message-Id: <1378989408-10618-3-git-send-email-g.liakhovetski@gmx.de> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1378989408-10618-1-git-send-email-g.liakhovetski@gmx.de> References: <1378989408-10618-1-git-send-email-g.liakhovetski@gmx.de> X-Provags-ID: V02:K0:4IBQnXCUO/C2N/cl5zIXaj7p9mrK/3JCcZ6gEKiEK7o uJYxx91qJVYXRtkp9nTc36/Pk37pWngMjcpg+W319NRaItDmpy QcCFR7uWAFSPvcZu4sV7lKro9mW2SPVeLthOXk5JSjzZzMJ7wz f/LjSOuJEM1bMbSuQHREr2OhmcRXdTOvxTn1hzIqkcFYk2lNwu dAjAtNe5Froxu9whRUrnR8lR+KAq7rH9DZsDg4CVMLDysqbkeL ngUJC9IuN2+nlkBU1r2AIllgyfM5C6mk7IRcr9DQmdIKl2AoT4 cLEO/Kzzi+LPFW8Lcu+bKDtgVRRWmBIY4pDPM02V6aQX/OT2nU RnPKIpZHHbRudFcrfx8kwFeR4+g447356jA/j7OnkDl2U8fDp2 1ulXBsbjIQIMQ== Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org There is no need to repeatedly query clock frequency, where it is not expected to change. The complete loop can also trivially be replaced with a simple division. A further loop below the one, being simplified, could also be replaced, but that would get more complicated. Signed-off-by: Guennadi Liakhovetski --- drivers/i2c/busses/i2c-rcar.c | 20 +++++++++++++------- 1 files changed, 13 insertions(+), 7 deletions(-) diff --git a/drivers/i2c/busses/i2c-rcar.c b/drivers/i2c/busses/i2c-rcar.c index 15eef94..9325db4 100644 --- a/drivers/i2c/busses/i2c-rcar.c +++ b/drivers/i2c/busses/i2c-rcar.c @@ -231,6 +231,7 @@ static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, u32 round, ick; u32 scl; u32 cdf_width; + unsigned long rate; if (!clkp) { dev_err(dev, "there is no peripheral_clk\n"); @@ -264,15 +265,14 @@ static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, * clkp : peripheral_clk * F[] : integer up-valuation */ - for (cdf = 0; cdf < (1 << cdf_width); cdf++) { - ick = clk_get_rate(clkp) / (1 + cdf); - if (ick < 20000000) - goto ick_find; + rate = clk_get_rate(clkp); + cdf = rate / 20000000; + if (cdf >= 1 << cdf_width) { + dev_err(dev, "Input clock %lu too high\n", rate); + return -EIO; } - dev_err(dev, "there is no best CDF\n"); - return -EIO; + ick = rate / (cdf + 1); -ick_find: /* * it is impossible to calculate large scale * number on u32. separate it @@ -290,6 +290,12 @@ ick_find: * * Calculation result (= SCL) should be less than * bus_speed for hardware safety + * + * We could use something along the lines of + * div = ick / (bus_speed + 1) + 1; + * scgd = (div - 20 - round + 7) / 8; + * scl = ick / (20 + (scgd * 8) + round); + * (not fully verified) but that would get pretty involved */ for (scgd = 0; scgd < 0x40; scgd++) { scl = ick / (20 + (scgd * 8) + round);