From patchwork Wed Oct 31 20:16:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolaj Fogh X-Patchwork-Id: 10663239 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7E52B1751 for ; Wed, 31 Oct 2018 20:16:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6B3F32B7E4 for ; Wed, 31 Oct 2018 20:16:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 596DD2B7E7; Wed, 31 Oct 2018 20:16:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F0B682B7E4 for ; Wed, 31 Oct 2018 20:16:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729344AbeKAFQ3 (ORCPT ); Thu, 1 Nov 2018 01:16:29 -0400 Received: from mail-ed1-f68.google.com ([209.85.208.68]:40060 "EHLO mail-ed1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726119AbeKAFQ3 (ORCPT ); Thu, 1 Nov 2018 01:16:29 -0400 Received: by mail-ed1-f68.google.com with SMTP id z12-v6so8978582edp.7 for ; Wed, 31 Oct 2018 13:16:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=87jAPNGCGz9A6VmfUvLgOyArbO6h9nHgvnTWOt/Cje4=; b=lxn3q/Malnvoh8aKg6sJT1OwTa8nk3I8RSLnXepSY0KHo1D5bwes4T5vrkoS+e29qA HI71gLdwQvAcOI8uHem/eLSpCuW7Valh+GcvDbUFnhCxxTkLHc9kxTkR7PRA0ZKyTQ3A 3qWlC6tME0mdqNbebuysmLhKpoAH4xCBFpQTJEG4cVNh9QARpMzVWqPrJ7RjItt4lVoW xKFg7os/m5K9thtn3D/GmRJFFHdXOW4W2G6VIAmdVLXa6OtgyXl1KKFul5FAtzVB2egR 6pwiwQ+4RRyTfMshV4DNRfIcZSxlziP8Ex3CAN9VRXtcbonmxzXpdlZfWn8s4d5fl6sD GAMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=87jAPNGCGz9A6VmfUvLgOyArbO6h9nHgvnTWOt/Cje4=; b=l5Tyeow5iYzFs4WB3k5Wogr3uBu3jn9ajX9KzEXT+UtEHJReINh29jMbWBaZluzdnh YKeJQwguRlBoiFVzg+NLXhS4hU3MxI+wsDWYKAnPgs8ky2z5yGJlpyf77Jitl2vkZ/rq n2058oggoWcv+jy22W6bGQx8RnVD8xrqL1Vk4FnJEqwmSAQHJ7mLPq+drh6aUn09NtvK RlMhQ5CfKELtjJ7EKybk4Q7LklKMJVQ1Z3AxXcwvNh4AlDSHZC0OyldkRmiPOD12Y+ws i3Y/ajxOLRX8mBnZX3nK7EkRwXqKpttZub2KgDhKYyUJO8YZ57xEmETVbbxS/H1aHyjc SmCQ== X-Gm-Message-State: AGRZ1gLXhGS043Qowk0W5CZ1UKuGRI/5kteRCx9HNemMX8gVJuyfeDO2 Qs8FEX3T1qUegE/73vBga/58ogBp X-Google-Smtp-Source: AJdET5cbQ/xYtt90D8D8zka/oEJ0exYPfkImGWuZx1bEbtDKeBi247gOqf95P04mUnRg3xTgOSKagQ== X-Received: by 2002:a50:99ce:: with SMTP id n14-v6mr3308896edb.7.1541017012063; Wed, 31 Oct 2018 13:16:52 -0700 (PDT) Received: from nfogh-Ryzen (xd93ddde1.cust.hiper.dk. [217.61.221.225]) by smtp.gmail.com with ESMTPSA id i17-v6sm3795734edq.75.2018.10.31.13.16.49 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 31 Oct 2018 13:16:50 -0700 (PDT) Date: Wed, 31 Oct 2018 21:16:48 +0100 From: Nikolaj Fogh To: nfogh@vectory.com Cc: linux-usb@vger.kernel.org Subject: [PATCH] Improve the accuracy of the baud rate generator by using round-to-closest instead of truncating when calculating baud rate divisors. Message-ID: <20181031200913.GA6372@nfogh-Ryzen> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP I have experienced that the ftdi_sio driver gives less-than-optimal baud rates as the driver truncates instead of rounds to nearest during baud rate divisor calculation. This patch improves on the baud rate generation. The generated baud rate corresponds to the optimal baud rate achievable with the chip. This is what the windows driver gives as well. Signed-off-by: Nikolaj Fogh --- drivers/usb/serial/ftdi_sio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c index 609198d9594c..0edbd3427548 100644 --- a/drivers/usb/serial/ftdi_sio.c +++ b/drivers/usb/serial/ftdi_sio.c @@ -1130,7 +1130,7 @@ static unsigned short int ftdi_232am_baud_base_to_divisor(int baud, int base) { unsigned short int divisor; /* divisor shifted 3 bits to the left */ - int divisor3 = base / 2 / baud; + int divisor3 = DIV_ROUND_CLOSEST(base, 2 * baud); if ((divisor3 & 0x7) == 7) divisor3++; /* round x.7/8 up to x+1 */ divisor = divisor3 >> 3; @@ -1156,7 +1156,7 @@ static u32 ftdi_232bm_baud_base_to_divisor(int baud, int base) static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 }; u32 divisor; /* divisor shifted 3 bits to the left */ - int divisor3 = base / 2 / baud; + int divisor3 = DIV_ROUND_CLOSEST(base, 2 * baud); divisor = divisor3 >> 3; divisor |= (u32)divfrac[divisor3 & 0x7] << 14; /* Deal with special cases for highest baud rates. */ @@ -1179,7 +1179,7 @@ static u32 ftdi_2232h_baud_base_to_divisor(int baud, int base) int divisor3; /* hi-speed baud rate is 10-bit sampling instead of 16-bit */ - divisor3 = base * 8 / (baud * 10); + divisor3 = DIV_ROUND_CLOSEST(base * 8, baud * 10); divisor = divisor3 >> 3; divisor |= (u32)divfrac[divisor3 & 0x7] << 14;