mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Karoly Pados <pados@pados.hu>
To: Johan Hovold <johan@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Karoly Pados <pados@pados.hu>
Subject: [PATCH] USB: serial: cp210x: Improve baudrate support for CP2102N
Date: Fri, 15 Jun 2018 23:29:57 +0200	[thread overview]
Message-ID: <20180615212957.26539-1-pados@pados.hu> (raw)

The CP2102N supports more baudrates than earlier chips by SiLabs.
This patch adds support for all rates documented in the datasheet
of this device.

Signed-off-by: Karoly Pados <pados@pados.hu>
---
 drivers/usb/serial/cp210x.c | 39 ++++++++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index b1849f657e01..793b86252c46 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -357,6 +357,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 */
@@ -758,8 +761,12 @@ static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl)
 /*
  * cp210x_quantise_baudrate
  * Quantises the baud rate as per AN205 Table 1
+ * The CP2102N is fully (except for baud rate aliasing) software-
+ * compatible, but supports some additional baudrates. However, there is
+ * no quantitisation table available for this model, so in this case we
+ * take the supported baudrate which is closest to the requested one.
  */
-static unsigned int cp210x_quantise_baudrate(unsigned int baud)
+static unsigned int cp210x_quantise_baudrate(unsigned int baud, bool cp2102n)
 {
 	if (baud <= 300)
 		baud = 300;
@@ -790,10 +797,17 @@ static unsigned int cp210x_quantise_baudrate(unsigned int baud)
 	else if (baud <= 491520)   baud = 460800;
 	else if (baud <= 567138)   baud = 500000;
 	else if (baud <= 670254)   baud = 576000;
-	else if (baud < 1000000)
-		baud = 921600;
-	else if (baud > 2000000)
-		baud = 2000000;
+	else if (cp2102n) {
+		if (baud <= 960800)        baud = 921600;
+		else if (baud <= 1100000)  baud = 1000000;
+		else if (baud <= 1350000)  baud = 1200000;
+		else if (baud <= 1750000)  baud = 1500000;
+		else if (baud <= 2500000)  baud = 2000000;
+		else                       baud = 3000000;
+	} else {
+		if (baud < 1000000)        baud = 921600;
+		else if (baud > 2000000)   baud = 2000000;
+	}
 	return baud;
 }
 
@@ -1045,16 +1059,19 @@ static void cp210x_get_termios_port(struct usb_serial_port *port,
 static void cp210x_change_speed(struct tty_struct *tty,
 		struct usb_serial_port *port, struct ktermios *old_termios)
 {
-	u32 baud;
-
-	baud = tty->termios.c_ospeed;
+	bool is_cp2102n;
+	u32 baud = tty->termios.c_ospeed;
+	struct cp210x_serial_private *priv = usb_get_serial_data(port->serial);
 
-	/* This maps the requested rate to a rate valid on cp2102 or cp2103,
-	 * or to an arbitrary rate in [1M,2M].
+	/* This maps the requested rate to a rate valid on cp2102(n) or
+	 * cp2103 or to an arbitrary rate in [1M,2M].
 	 *
 	 * NOTE: B0 is not implemented.
 	 */
-	baud = cp210x_quantise_baudrate(baud);
+	is_cp2102n =	(priv->partnum == CP210X_PARTNUM_CP2102N_QFN28) ||
+			(priv->partnum == CP210X_PARTNUM_CP2102N_QFN24) ||
+			(priv->partnum == CP210X_PARTNUM_CP2102N_QFN20);
+	baud = cp210x_quantise_baudrate(baud, is_cp2102n);
 
 	dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
 	if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
-- 
2.17.1


             reply	other threads:[~2018-06-15 21:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-15 21:29 Karoly Pados [this message]
2018-06-19  9:15 ` Johan Hovold
2018-06-19  9:50 ` Karoly Pados
2018-06-20  9:39   ` Johan Hovold
2018-06-20  9:51   ` Karoly Pados
2018-06-20 10:57     ` Johan Hovold

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180615212957.26539-1-pados@pados.hu \
    --to=pados@pados.hu \
    --cc=gregkh@linuxfoundation.org \
    --cc=johan@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®