linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Shevchenko <andy.shevchenko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: Aaron Brice <aaron.brice-vSOi0k5x9wFWk0Htik3J/w@public.gmane.org>,
	linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	Mark Brown <broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
Cc: Andy Shevchenko
	<andy.shevchenko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Subject: [PATCH v1 2/2][RFC] spi: fsl-dspi: use double baud rate in approximation
Date: Thu,  2 Apr 2015 12:11:47 +0300	[thread overview]
Message-ID: <1427965907-28125-3-git-send-email-andy.shevchenko@gmail.com> (raw)
In-Reply-To: <1427965907-28125-1-git-send-email-andy.shevchenko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>

The CTAR register has DBR bit which allows to double a baud rate, though
it makes duty cycle deviate from 50/50.

This patch tries to approximate better using DBR. It helps in case of
highest possible baud rates.

Signed-off-by: Andy Shevchenko <andy.shevchenko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
 drivers/spi/spi-fsl-dspi.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/drivers/spi/spi-fsl-dspi.c b/drivers/spi/spi-fsl-dspi.c
index 31cdee5..92d387a 100644
--- a/drivers/spi/spi-fsl-dspi.c
+++ b/drivers/spi/spi-fsl-dspi.c
@@ -47,6 +47,7 @@
 #define SPI_TCR			0x08
 
 #define SPI_CTAR(x)		(0x0c + (((x) & 0x3) * 4))
+#define SPI_CTAR_DBR(x)		(((x) & 0x00000001) << 31)
 #define SPI_CTAR_FMSZ(x)	(((x) & 0x0000000f) << 27)
 #define SPI_CTAR_CPOL(x)	((x) << 26)
 #define SPI_CTAR_CPHA(x)	((x) << 25)
@@ -139,8 +140,8 @@ static inline int is_double_byte_mode(struct fsl_dspi *dspi)
 	return ((val & SPI_FRAME_BITS_MASK) == SPI_FRAME_BITS(8)) ? 0 : 1;
 }
 
-static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
-		unsigned long clkrate)
+static void hz_to_spi_baud(char *dbr, char *pbr, char *br,
+		int speed_hz, unsigned long clkrate)
 {
 	/* Valid baud rate pre-scaler values */
 	int pbr_tbl[4] = {2, 3, 5, 7};
@@ -151,17 +152,21 @@ static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
 		4096,	8192,	16384,	32768,
 	};
 	unsigned long r = INT_MAX, tmp;
-	int i, j;
-
-	for (i = 0; i < ARRAY_SIZE(brs); i++)
-		for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
-			tmp = abs(clkrate / pbr_tbl[j] / brs[i] - speed_hz);
-			if (tmp >= r)
-				continue;
-			r = tmp;
-			*br = i;
-			*pbr = j;
+	int i, j, k;
+
+	for (k = 0; k < 2; k++) {
+		for (i = 0; i < ARRAY_SIZE(brs); i++) {
+			for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
+				tmp = abs(clkrate / pbr_tbl[j] / brs[i] - speed_hz);
+				if (tmp >= r)
+					continue;
+				r = tmp;
+				*br = i;
+				*pbr = j;
+				*dbr = k;
+			}
 		}
+	}
 }
 
 static int dspi_transfer_write(struct fsl_dspi *dspi)
@@ -342,7 +347,7 @@ static int dspi_setup(struct spi_device *spi)
 {
 	struct chip_data *chip;
 	struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
-	unsigned char br = 0, pbr = 0, fmsz = 0;
+	unsigned char br = 0, pbr = 0, dbr = 0, fmsz = 0;
 
 	if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
 		fmsz = spi->bits_per_word - 1;
@@ -364,13 +369,14 @@ static int dspi_setup(struct spi_device *spi)
 
 	chip->void_write_data = 0;
 
-	hz_to_spi_baud(&pbr, &br,
+	hz_to_spi_baud(&dbr, &pbr, &br,
 			spi->max_speed_hz, clk_get_rate(dspi->clk));
 
 	chip->ctar_val =  SPI_CTAR_FMSZ(fmsz)
 		| SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
 		| SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
 		| SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
+		| SPI_CTAR_DBR(dbr)
 		| SPI_CTAR_PBR(pbr)
 		| SPI_CTAR_BR(br);
 
-- 
1.9.3

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2015-04-02  9:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-02  9:11 [PATCH v1 0/2] spi: fsl-dspi: better approximation for baudrate Andy Shevchenko
     [not found] ` <1427965907-28125-1-git-send-email-andy.shevchenko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-04-02  9:11   ` [PATCH v1 1/2] spi: fsl-dspi: increase precision of baud rate approximation Andy Shevchenko
2015-04-02  9:11   ` Andy Shevchenko [this message]
2015-04-03 17:50   ` [PATCH v1 0/2] spi: fsl-dspi: better approximation for baudrate Aaron Brice
     [not found]     ` <551ED2F6.8020701-vSOi0k5x9wFWk0Htik3J/w@public.gmane.org>
2015-04-03 20:59       ` Andy Shevchenko
     [not found]         ` <CAHp75Vfa9b0s6rXMbUERbbGRowGp4bidKH1UZ4a8HAx23oSrLw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-04-03 22:44           ` Aaron Brice
     [not found]             ` <551F17B1.3010109-vSOi0k5x9wFWk0Htik3J/w@public.gmane.org>
2015-04-06 16:49               ` Mark Brown

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=1427965907-28125-3-git-send-email-andy.shevchenko@gmail.com \
    --to=andy.shevchenko-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
    --cc=aaron.brice-vSOi0k5x9wFWk0Htik3J/w@public.gmane.org \
    --cc=broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.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;
as well as URLs for NNTP newsgroup(s).