From patchwork Wed Jul 18 12:24:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johan Hovold X-Patchwork-Id: 10532325 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 99D0060554 for ; Wed, 18 Jul 2018 12:25:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8FFB929076 for ; Wed, 18 Jul 2018 12:25:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8429F29085; Wed, 18 Jul 2018 12:25:36 +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=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 9A64C29076 for ; Wed, 18 Jul 2018 12:25:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731244AbeGRNDO (ORCPT ); Wed, 18 Jul 2018 09:03:14 -0400 Received: from mail-lj1-f196.google.com ([209.85.208.196]:38207 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731154AbeGRNDN (ORCPT ); Wed, 18 Jul 2018 09:03:13 -0400 Received: by mail-lj1-f196.google.com with SMTP id p6-v6so3921088ljc.5 for ; Wed, 18 Jul 2018 05:25:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=HeRdNybZweyelwJsmWOpJem3xy7B9MERVIKFq4fY7fY=; b=OtyeWUglkgSkkqcGxEWjeA1NTVzKr9/V0Yn+TLrAys+mChHHCMoTA/xRQNIU/Y2lgW VdMTG7QNHGzHv+WgOGY+5lY/TysCqgKBXTPQZ5TX1NRipbZrjBN14ZePV6Tj24RC+5Y+ O64cCTmgVRZL+lvH8vp3SYih/LvxYffXZj6HTc3rqOuqJQk9/fRtFgaIPn2rinuOljWN sFT1qqwDm81PrzzfTVb2bwVIisrtJabTpS6Amhm2Mg5XlYasopowHX4/WlssqvOrOqfv rYcOnWs48qHm3blyMFCDxLfu4/7dR4vP7nHBUbYWuXqGL3igadhXqG5aBS7DpL1dv4yF /WuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=HeRdNybZweyelwJsmWOpJem3xy7B9MERVIKFq4fY7fY=; b=enft+KNCx9gOC+wU3YJ6Vfn96eLElmmonc2s+ry+LxZOfWX+OnAQ0DLOQC3pxE0qiv c1kK7u9e4u6UMRSizsafuxIbD88/LLuiuhuKwJsG0YWIYP+q2e+gf5aByNBIYUbbif39 NZ54XTRZtNfbwDKPUrmZSFT8yr10hjRh0BkMMkOIlrfKsTbKAlQxmDvk0rdME3iaFOnD TysZ7X+1Pg0PG6dCMrElO8qe3a2iyr5pIzjjdRF5+9Hay1dp374VbNGIIaDynnnYqA3Q 5aV+Mj2h23+yV4h4K4TJ3TFxyXo77xYDpiSt3yMOGJZ8Ph65/Wms6YsWwxzXLsQFXLPN QxGA== X-Gm-Message-State: AOUpUlEf78Z6wybKL1KX/Yh8JzV3clD1GLHwWRwC+UE+qUpRT7h0J2HV MxCnu8U/g0d3V/zz0C/6zIrwExk4 X-Google-Smtp-Source: AAOMgpdL/LAmi1EPxwBtZUkaYdNWrcYfj3ufi6aVr1W8LoCe8wLc85S4kbGONyRjG4v7YUMyEwrEqQ== X-Received: by 2002:a2e:21d5:: with SMTP id h82-v6mr4466189lji.46.1531916731028; Wed, 18 Jul 2018 05:25:31 -0700 (PDT) Received: from xi.terra (c-8bb2e655.07-184-6d6c6d4.bbcust.telenor.se. [85.230.178.139]) by smtp.gmail.com with ESMTPSA id x3-v6sm599293ljb.25.2018.07.18.05.25.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Jul 2018 05:25:29 -0700 (PDT) Received: from johan by xi.terra with local (Exim 4.91) (envelope-from ) id 1fflW8-0003tm-KH; Wed, 18 Jul 2018 14:25:16 +0200 From: Johan Hovold To: Johan Hovold Cc: Greg Kroah-Hartman , Karoly Pados , linux-usb@vger.kernel.org Subject: [PATCH 3/5] USB: serial: cp210x: improve baudrate support for CP2102N Date: Wed, 18 Jul 2018 14:24:59 +0200 Message-Id: <20180718122501.14926-4-johan@kernel.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180718122501.14926-1-johan@kernel.org> References: <20180718122501.14926-1-johan@kernel.org> 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 From: Karoly Pados CP2102N devices support a lot more baudrates than earlier chips by SiLabs. These devices are not constrained anymore by the table in AN205, and are able to generate almost any baudrate in the supported range with only minimal errors. This has also been verified with a scope on a physical device. This patch adds support for all baudrates supported by the CP2102N. Signed-off-by: Karoly Pados [ johan: rework on top of an205 and max-speed patches ] Signed-off-by: Johan Hovold Reviewed-by: Greg Kroah-Hartman --- drivers/usb/serial/cp210x.c | 39 ++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c index 4281f2bfe0e1..3778685c7b99 100644 --- a/drivers/usb/serial/cp210x.c +++ b/drivers/usb/serial/cp210x.c @@ -355,6 +355,9 @@ static struct usb_serial_driver * const serial_drivers[] = { #define CP210X_PARTNUM_CP2104 0x04 #define CP210X_PARTNUM_CP2105 0x05 #define CP210X_PARTNUM_CP2108 0x08 +#define CP210X_PARTNUM_CP2102N_QFN28 0x20 +#define CP210X_PARTNUM_CP2102N_QFN24 0x21 +#define CP210X_PARTNUM_CP2102N_QFN20 0x22 #define CP210X_PARTNUM_UNKNOWN 0xFF /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */ @@ -454,6 +457,15 @@ struct cp210x_gpio_write { u8 state; } __packed; +static bool cp210x_is_cp2102n(struct usb_serial *serial) +{ + struct cp210x_serial_private *priv = usb_get_serial_data(serial); + + return (priv->partnum == CP210X_PARTNUM_CP2102N_QFN28) || + (priv->partnum == CP210X_PARTNUM_CP2102N_QFN24) || + (priv->partnum == CP210X_PARTNUM_CP2102N_QFN20); +} + /* * Helper to get interface number when we only have struct usb_serial. */ @@ -1053,20 +1065,32 @@ static speed_t cp210x_get_an205_rate(speed_t baud) static void cp210x_change_speed(struct tty_struct *tty, struct usb_serial_port *port, struct ktermios *old_termios) { - struct cp210x_serial_private *priv = usb_get_serial_data(port->serial); + struct usb_serial *serial = port->serial; + struct cp210x_serial_private *priv = usb_get_serial_data(serial); u32 baud; baud = tty->termios.c_ospeed; - /* This maps the requested rate to a rate valid on cp2102 or cp2103, - * or to an arbitrary rate in [1M, max_speed] + /* + * This maps the requested rate to the actual rate on cp2102n, a valid + * rate on cp2102 or cp2103, or to an arbitrary rate in + * [1M, max_speed]. * * NOTE: B0 is not implemented. */ - if (baud < 1000000) + if (cp210x_is_cp2102n(serial)) { + int clk_div; + int prescaler; + + baud = clamp(baud, 300u, priv->max_speed); + prescaler = (baud <= 365) ? 4 : 1; + clk_div = DIV_ROUND_CLOSEST(48000000, 2 * prescaler * baud); + baud = 48000000 / (2 * prescaler * clk_div); + } else if (baud < 1000000) { baud = cp210x_get_an205_rate(baud); - else if (baud > priv->max_speed) + } else if (baud > priv->max_speed) { baud = priv->max_speed; + } dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud); if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) { @@ -1520,6 +1544,11 @@ static void cp210x_init_max_speed(struct usb_serial *serial) else max = 921600; /* SCI */ break; + case CP210X_PARTNUM_CP2102N_QFN28: + case CP210X_PARTNUM_CP2102N_QFN24: + case CP210X_PARTNUM_CP2102N_QFN20: + max = 3000000; + break; default: max = 2000000; break;