From patchwork Thu Apr 2 09:11:46 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 6145631 Return-Path: X-Original-To: patchwork-linux-spi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 6B091BF4A7 for ; Thu, 2 Apr 2015 09:21:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9467120375 for ; Thu, 2 Apr 2015 09:21:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9B8B42038C for ; Thu, 2 Apr 2015 09:21:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752713AbbDBJVF (ORCPT ); Thu, 2 Apr 2015 05:21:05 -0400 Received: from gw03.mail.saunalahti.fi ([195.197.172.111]:52192 "EHLO gw03.mail.saunalahti.fi" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752505AbbDBJVE (ORCPT ); Thu, 2 Apr 2015 05:21:04 -0400 Received: from localhost.localdomain (a91-152-110-231.elisa-laajakaista.fi [91.152.110.231]) by gw03.mail.saunalahti.fi (Postfix) with ESMTP id 645B720021; Thu, 2 Apr 2015 12:11:43 +0300 (EEST) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by localhost.localdomain (8.14.8/8.14.8) with ESMTP id t329C5sq028177; Thu, 2 Apr 2015 12:12:05 +0300 Received: (from andy@localhost) by localhost.localdomain (8.14.8/8.14.8/Submit) id t329Br5I028174; Thu, 2 Apr 2015 12:11:53 +0300 X-Authentication-Warning: localhost.localdomain: andy set sender to andy.shevchenko@gmail.com using -f From: Andy Shevchenko To: Aaron Brice , linux-spi@vger.kernel.org, Mark Brown Cc: Andy Shevchenko Subject: [PATCH v1 1/2] spi: fsl-dspi: increase precision of baud rate approximation Date: Thu, 2 Apr 2015 12:11:46 +0300 Message-Id: <1427965907-28125-2-git-send-email-andy.shevchenko@gmail.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1427965907-28125-1-git-send-email-andy.shevchenko@gmail.com> References: <1427965907-28125-1-git-send-email-andy.shevchenko@gmail.com> Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,RCVD_IN_DNSWL_HI,T_RP_MATCHES_RCVD,UNPARSEABLE_RELAY autolearn=ham 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 The current approximation relies on scale comparison which is wrong in two ways: a) the required scale doesn't take into consideration remainder of the division, and b) minimal scale doesn't guarantee the best approximation. This patch change the approach to use comparison between remainders instead of direct scale testing. Signed-off-by: Andy Shevchenko --- drivers/spi/spi-fsl-dspi.c | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/drivers/spi/spi-fsl-dspi.c b/drivers/spi/spi-fsl-dspi.c index 96cac87..31cdee5 100644 --- a/drivers/spi/spi-fsl-dspi.c +++ b/drivers/spi/spi-fsl-dspi.c @@ -144,34 +144,24 @@ static void hz_to_spi_baud(char *pbr, char *br, int speed_hz, { /* Valid baud rate pre-scaler values */ int pbr_tbl[4] = {2, 3, 5, 7}; - int brs[16] = { 2, 4, 6, 8, + int brs[16] = { + 2, 4, 6, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, - 4096, 8192, 16384, 32768 }; - int scale_needed, scale, minscale = INT_MAX; + 4096, 8192, 16384, 32768, + }; + unsigned long r = INT_MAX, tmp; int i, j; - scale_needed = clkrate / speed_hz; - for (i = 0; i < ARRAY_SIZE(brs); i++) for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) { - scale = brs[i] * pbr_tbl[j]; - if (scale >= scale_needed) { - if (scale < minscale) { - minscale = scale; - *br = i; - *pbr = j; - } - break; - } + tmp = abs(clkrate / pbr_tbl[j] / brs[i] - speed_hz); + if (tmp >= r) + continue; + r = tmp; + *br = i; + *pbr = j; } - - if (minscale == INT_MAX) { - pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n", - speed_hz, clkrate); - *pbr = ARRAY_SIZE(pbr_tbl) - 1; - *br = ARRAY_SIZE(brs) - 1; - } } static int dspi_transfer_write(struct fsl_dspi *dspi)