All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] spi: sh-msiof: Update calculation of frequency dividing
@ 2015-01-15  1:50 ` Nobuhiro Iwamatsu
  0 siblings, 0 replies; 6+ messages in thread
From: Nobuhiro Iwamatsu @ 2015-01-15  1:50 UTC (permalink / raw)
  To: linux-spi
  Cc: linux-sh, sergei.shtylyov, geert, yoshihiro.shimoda.uh,
	Nobuhiro Iwamatsu

sh-msiof of frequency dividing does not perform the calculation, driver have
to manage setting value in the table. It is not possible to set frequency
dividing value close to the actual data in this way. This changes from
frequency dividing of table management to setting by calculation.
This driver is able to set a value close to the actual data.

Signed-off-by: Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
---
 V2:
   - use u32 instead of unsigned long.
   - Fix loop.
   - Use SCR_BRPS instead of hoard code shift.
   - Add check of div maximum value.

 drivers/spi/spi-sh-msiof.c | 45 +++++++++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/drivers/spi/spi-sh-msiof.c b/drivers/spi/spi-sh-msiof.c
index 96a5fc0..545ffff 100644
--- a/drivers/spi/spi-sh-msiof.c
+++ b/drivers/spi/spi-sh-msiof.c
@@ -241,42 +241,41 @@ static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
 
 static struct {
 	unsigned short div;
-	unsigned short scr;
-} const sh_msiof_spi_clk_table[] = {
-	{ 1,	SCR_BRPS( 1) | SCR_BRDV_DIV_1 },
-	{ 2,	SCR_BRPS( 1) | SCR_BRDV_DIV_2 },
-	{ 4,	SCR_BRPS( 1) | SCR_BRDV_DIV_4 },
-	{ 8,	SCR_BRPS( 1) | SCR_BRDV_DIV_8 },
-	{ 16,	SCR_BRPS( 1) | SCR_BRDV_DIV_16 },
-	{ 32,	SCR_BRPS( 1) | SCR_BRDV_DIV_32 },
-	{ 64,	SCR_BRPS(32) | SCR_BRDV_DIV_2 },
-	{ 128,	SCR_BRPS(32) | SCR_BRDV_DIV_4 },
-	{ 256,	SCR_BRPS(32) | SCR_BRDV_DIV_8 },
-	{ 512,	SCR_BRPS(32) | SCR_BRDV_DIV_16 },
-	{ 1024,	SCR_BRPS(32) | SCR_BRDV_DIV_32 },
+	unsigned short brdv;
+} const sh_msiof_spi_div_table[] = {
+	{ 1,	SCR_BRDV_DIV_1 },
+	{ 2,	SCR_BRDV_DIV_2 },
+	{ 4,	SCR_BRDV_DIV_4 },
+	{ 8,	SCR_BRDV_DIV_8 },
+	{ 16,	SCR_BRDV_DIV_16 },
+	{ 32,	SCR_BRDV_DIV_32 },
 };
 
-static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
+static int sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
 				      unsigned long parent_rate, u32 spi_hz)
 {
 	unsigned long div = 1024;
+	u32 brps, scr;
 	size_t k;
 
 	if (!WARN_ON(!spi_hz || !parent_rate))
 		div = DIV_ROUND_UP(parent_rate, spi_hz);
 
-	/* TODO: make more fine grained */
+	if (div > 1024) /* Max of divider is 1024 */
+		return -EINVAL;
 
-	for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
-		if (sh_msiof_spi_clk_table[k].div >= div)
+	for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_div_table); k++) {
+		brps = DIV_ROUND_UP(div, sh_msiof_spi_div_table[k].div);
+		if (brps <= 32) /* max of brdv is 32 */
 			break;
 	}
 
-	k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_clk_table) - 1);
-
-	sh_msiof_write(p, TSCR, sh_msiof_spi_clk_table[k].scr);
+	scr = sh_msiof_spi_div_table[k].brdv | SCR_BRPS(brps);
+	sh_msiof_write(p, TSCR, scr);
 	if (!(p->chipdata->master_flags & SPI_MASTER_MUST_TX))
-		sh_msiof_write(p, RSCR, sh_msiof_spi_clk_table[k].scr);
+		sh_msiof_write(p, RSCR, scr);
+
+	return 0;
 }
 
 static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
@@ -804,7 +803,9 @@ static int sh_msiof_transfer_one(struct spi_master *master,
 	int ret;
 
 	/* setup clocks (clock already enabled in chipselect()) */
-	sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
+	ret = sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
+	if (ret)
+		return ret;
 
 	while (master->dma_tx && len > 15) {
 		/*
-- 
2.1.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2] spi: sh-msiof: Update calculation of frequency dividing
@ 2015-01-15  1:50 ` Nobuhiro Iwamatsu
  0 siblings, 0 replies; 6+ messages in thread
From: Nobuhiro Iwamatsu @ 2015-01-15  1:50 UTC (permalink / raw)
  To: linux-spi
  Cc: linux-sh, sergei.shtylyov, geert, yoshihiro.shimoda.uh,
	Nobuhiro Iwamatsu

sh-msiof of frequency dividing does not perform the calculation, driver have
to manage setting value in the table. It is not possible to set frequency
dividing value close to the actual data in this way. This changes from
frequency dividing of table management to setting by calculation.
This driver is able to set a value close to the actual data.

Signed-off-by: Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
---
 V2:
   - use u32 instead of unsigned long.
   - Fix loop.
   - Use SCR_BRPS instead of hoard code shift.
   - Add check of div maximum value.

 drivers/spi/spi-sh-msiof.c | 45 +++++++++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/drivers/spi/spi-sh-msiof.c b/drivers/spi/spi-sh-msiof.c
index 96a5fc0..545ffff 100644
--- a/drivers/spi/spi-sh-msiof.c
+++ b/drivers/spi/spi-sh-msiof.c
@@ -241,42 +241,41 @@ static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
 
 static struct {
 	unsigned short div;
-	unsigned short scr;
-} const sh_msiof_spi_clk_table[] = {
-	{ 1,	SCR_BRPS( 1) | SCR_BRDV_DIV_1 },
-	{ 2,	SCR_BRPS( 1) | SCR_BRDV_DIV_2 },
-	{ 4,	SCR_BRPS( 1) | SCR_BRDV_DIV_4 },
-	{ 8,	SCR_BRPS( 1) | SCR_BRDV_DIV_8 },
-	{ 16,	SCR_BRPS( 1) | SCR_BRDV_DIV_16 },
-	{ 32,	SCR_BRPS( 1) | SCR_BRDV_DIV_32 },
-	{ 64,	SCR_BRPS(32) | SCR_BRDV_DIV_2 },
-	{ 128,	SCR_BRPS(32) | SCR_BRDV_DIV_4 },
-	{ 256,	SCR_BRPS(32) | SCR_BRDV_DIV_8 },
-	{ 512,	SCR_BRPS(32) | SCR_BRDV_DIV_16 },
-	{ 1024,	SCR_BRPS(32) | SCR_BRDV_DIV_32 },
+	unsigned short brdv;
+} const sh_msiof_spi_div_table[] = {
+	{ 1,	SCR_BRDV_DIV_1 },
+	{ 2,	SCR_BRDV_DIV_2 },
+	{ 4,	SCR_BRDV_DIV_4 },
+	{ 8,	SCR_BRDV_DIV_8 },
+	{ 16,	SCR_BRDV_DIV_16 },
+	{ 32,	SCR_BRDV_DIV_32 },
 };
 
-static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
+static int sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
 				      unsigned long parent_rate, u32 spi_hz)
 {
 	unsigned long div = 1024;
+	u32 brps, scr;
 	size_t k;
 
 	if (!WARN_ON(!spi_hz || !parent_rate))
 		div = DIV_ROUND_UP(parent_rate, spi_hz);
 
-	/* TODO: make more fine grained */
+	if (div > 1024) /* Max of divider is 1024 */
+		return -EINVAL;
 
-	for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
-		if (sh_msiof_spi_clk_table[k].div >= div)
+	for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_div_table); k++) {
+		brps = DIV_ROUND_UP(div, sh_msiof_spi_div_table[k].div);
+		if (brps <= 32) /* max of brdv is 32 */
 			break;
 	}
 
-	k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_clk_table) - 1);
-
-	sh_msiof_write(p, TSCR, sh_msiof_spi_clk_table[k].scr);
+	scr = sh_msiof_spi_div_table[k].brdv | SCR_BRPS(brps);
+	sh_msiof_write(p, TSCR, scr);
 	if (!(p->chipdata->master_flags & SPI_MASTER_MUST_TX))
-		sh_msiof_write(p, RSCR, sh_msiof_spi_clk_table[k].scr);
+		sh_msiof_write(p, RSCR, scr);
+
+	return 0;
 }
 
 static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
@@ -804,7 +803,9 @@ static int sh_msiof_transfer_one(struct spi_master *master,
 	int ret;
 
 	/* setup clocks (clock already enabled in chipselect()) */
-	sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
+	ret = sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
+	if (ret)
+		return ret;
 
 	while (master->dma_tx && len > 15) {
 		/*
-- 
2.1.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] spi: sh-msiof: Update calculation of frequency dividing
  2015-01-15  1:50 ` Nobuhiro Iwamatsu
@ 2015-01-15 14:21   ` Geert Uytterhoeven
  -1 siblings, 0 replies; 6+ messages in thread
From: Geert Uytterhoeven @ 2015-01-15 14:21 UTC (permalink / raw)
  To: Nobuhiro Iwamatsu
  Cc: linux-spi, Linux-sh list, Sergei Shtylyov, Yoshihiro Shimoda

Hi Iwamatsu-san,

Thanks for the update!

On Thu, Jan 15, 2015 at 2:50 AM, Nobuhiro Iwamatsu
<nobuhiro.iwamatsu.yj@renesas.com> wrote:
> @@ -804,7 +803,9 @@ static int sh_msiof_transfer_one(struct spi_master *master,
>         int ret;
>
>         /* setup clocks (clock already enabled in chipselect()) */
> -       sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
> +       ret = sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
> +       if (ret)
> +               return ret;

I think this can be handled by the core, if we fill in spi_master.min_speed_hz.

BTW, you should also CC Mark Brown when sending SPI patches.

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] spi: sh-msiof: Update calculation of frequency dividing
@ 2015-01-15 14:21   ` Geert Uytterhoeven
  0 siblings, 0 replies; 6+ messages in thread
From: Geert Uytterhoeven @ 2015-01-15 14:21 UTC (permalink / raw)
  To: Nobuhiro Iwamatsu
  Cc: linux-spi, Linux-sh list, Sergei Shtylyov, Yoshihiro Shimoda

Hi Iwamatsu-san,

Thanks for the update!

On Thu, Jan 15, 2015 at 2:50 AM, Nobuhiro Iwamatsu
<nobuhiro.iwamatsu.yj@renesas.com> wrote:
> @@ -804,7 +803,9 @@ static int sh_msiof_transfer_one(struct spi_master *master,
>         int ret;
>
>         /* setup clocks (clock already enabled in chipselect()) */
> -       sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
> +       ret = sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
> +       if (ret)
> +               return ret;

I think this can be handled by the core, if we fill in spi_master.min_speed_hz.

BTW, you should also CC Mark Brown when sending SPI patches.

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] spi: sh-msiof: Update calculation of frequency dividing
  2015-01-15 14:21   ` Geert Uytterhoeven
@ 2015-01-30  6:13     ` Nobuhiro Iwamatsu
  -1 siblings, 0 replies; 6+ messages in thread
From: Nobuhiro Iwamatsu @ 2015-01-30  6:13 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: linux-spi, Linux-sh list, Sergei Shtylyov, Yoshihiro Shimoda

Hi,

(2015/01/15 23:21), Geert Uytterhoeven wrote:
> Hi Iwamatsu-san,
>
> Thanks for the update!
>
> On Thu, Jan 15, 2015 at 2:50 AM, Nobuhiro Iwamatsu
> <nobuhiro.iwamatsu.yj@renesas.com>  wrote:
>> @@ -804,7 +803,9 @@ static int sh_msiof_transfer_one(struct spi_master *master,
>>          int ret;
>>
>>          /* setup clocks (clock already enabled in chipselect()) */
>> -       sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
>> +       ret = sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
>> +       if (ret)
>> +               return ret;
>
> I think this can be handled by the core, if we fill in spi_master.min_speed_hz.

I see. I removed this change.

>
> BTW, you should also CC Mark Brown when sending SPI patches.

OK, I will add him form next patch.

>
> Gr{oetje,eeting}s,
>
>                          Geert

Best regards,
   Nobuhiro

>
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
>
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
>                                  -- Linus Torvalds


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] spi: sh-msiof: Update calculation of frequency dividing
@ 2015-01-30  6:13     ` Nobuhiro Iwamatsu
  0 siblings, 0 replies; 6+ messages in thread
From: Nobuhiro Iwamatsu @ 2015-01-30  6:13 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: linux-spi, Linux-sh list, Sergei Shtylyov, Yoshihiro Shimoda

Hi,

(2015/01/15 23:21), Geert Uytterhoeven wrote:
> Hi Iwamatsu-san,
>
> Thanks for the update!
>
> On Thu, Jan 15, 2015 at 2:50 AM, Nobuhiro Iwamatsu
> <nobuhiro.iwamatsu.yj@renesas.com>  wrote:
>> @@ -804,7 +803,9 @@ static int sh_msiof_transfer_one(struct spi_master *master,
>>          int ret;
>>
>>          /* setup clocks (clock already enabled in chipselect()) */
>> -       sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
>> +       ret = sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
>> +       if (ret)
>> +               return ret;
>
> I think this can be handled by the core, if we fill in spi_master.min_speed_hz.

I see. I removed this change.

>
> BTW, you should also CC Mark Brown when sending SPI patches.

OK, I will add him form next patch.

>
> Gr{oetje,eeting}s,
>
>                          Geert

Best regards,
   Nobuhiro

>
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
>
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
>                                  -- Linus Torvalds


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-01-30  6:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-15  1:50 [PATCH v2] spi: sh-msiof: Update calculation of frequency dividing Nobuhiro Iwamatsu
2015-01-15  1:50 ` Nobuhiro Iwamatsu
2015-01-15 14:21 ` Geert Uytterhoeven
2015-01-15 14:21   ` Geert Uytterhoeven
2015-01-30  6:13   ` Nobuhiro Iwamatsu
2015-01-30  6:13     ` Nobuhiro Iwamatsu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.