Skip to contentSkip to conversation

spi: Allow using spi_bitbang_setup() with custom txrx_bufs()

PATCH In review Magnus Damm 2 participants 6 messages Nov 25, 2009 – Nov 26, 2009 SPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: Allow using spi_bitbang_setup() with custom txrx_bufs()

From: Magnus Damm <[email protected]>

This patch modifies the shared spi bitbanging code to allow using spi_bitbang_setup() even though the txrx_word[] callbacks are unset. Useful for drivers that want to make use of spi_bitbang_setup() but have their own txrx_bufs() callback.

While at it, drop the MSIOF driver workaround.

---

Depends on the MSIOF driver.

 drivers/spi/spi_bitbang.c  |   25 ++++++++++++++-----------
 drivers/spi/spi_sh_msiof.c |   11 -----------
 2 files changed, 14 insertions(+), 22 deletions(-)
drivers/spi/spi_bitbang.c +14 -11
@@ -176,6 +176,14 @@ int spi_bitbang_setup_transfer(struct sp
 }
 EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
 
+static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
+{
+	struct spi_bitbang_cs	*cs = spi->controller_state;
+	unsigned		nsecs = cs->nsecs;
+
+	return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t);
+}
+
 /**
  * spi_bitbang_setup - default setup for per-word I/O loops
  */
@@ -183,6 +191,7 @@ int spi_bitbang_setup(struct spi_device 
 {
 	struct spi_bitbang_cs	*cs = spi->controller_state;
 	struct spi_bitbang	*bitbang;
+	int			mode_mask = SPI_CPOL | SPI_CPHA;
 	int			retval;
 	unsigned long		flags;
 
@@ -196,9 +205,11 @@ int spi_bitbang_setup(struct spi_device 
 	}
 
 	/* per-word shift register access, in hardware or bitbanging */
-	cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
-	if (!cs->txrx_word)
-		return -EINVAL;
+	if (bitbang->txrx_bufs == spi_bitbang_bufs) {
+		cs->txrx_word = bitbang->txrx_word[spi->mode & mode_mask];
+		if (!cs->txrx_word)
+			return -EINVAL;
+	}
 
 	retval = bitbang->setup_transfer(spi, NULL);
 	if (retval < 0)
@@ -232,14 +243,6 @@ void spi_bitbang_cleanup(struct spi_devi
 }
 EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
 
-static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
-{
-	struct spi_bitbang_cs	*cs = spi->controller_state;
-	unsigned		nsecs = cs->nsecs;
-
-	return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t);
-}
-
 /*----------------------------------------------------------------------*/
 
 /*
drivers/spi/spi_sh_msiof.c +0 -11
@@ -510,13 +510,6 @@ static int sh_msiof_spi_txrx(struct spi_
 	return bytes_done;
 }
 
-static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
-				  u32 word, u8 bits)
-{
-	BUG_ON(1); /* unused but needed by bitbang code */
-	return 0;
-}
-
 static int sh_msiof_spi_probe(struct platform_device *pdev)
 {
 	struct resource	*r;
@@ -594,10 +587,6 @@ static int sh_msiof_spi_probe(struct pla
 	p->bitbang.chipselect = sh_msiof_spi_chipselect;
 	p->bitbang.setup_transfer = sh_msiof_spi_setup_transfer;
 	p->bitbang.txrx_bufs = sh_msiof_spi_txrx;
-	p->bitbang.txrx_word[SPI_MODE_0] = sh_msiof_spi_txrx_word;
-	p->bitbang.txrx_word[SPI_MODE_1] = sh_msiof_spi_txrx_word;
-	p->bitbang.txrx_word[SPI_MODE_2] = sh_msiof_spi_txrx_word;
-	p->bitbang.txrx_word[SPI_MODE_3] = sh_msiof_spi_txrx_word;
 
 	ret = spi_bitbang_start(&p->bitbang);
 	if (ret == 0)
#1 lore raw
Quoted 46 lines · Magnus Damm

From: Magnus Damm <[email protected]>

This patch modifies the shared spi bitbanging code to allow using spi_bitbang_setup() even though the txrx_word[] callbacks are unset. Useful for drivers that want to make use of spi_bitbang_setup() but have their own txrx_bufs() callback.

While at it, drop the MSIOF driver workaround.

---

 Depends on the MSIOF driver.

 drivers/spi/spi_bitbang.c  |   25 ++++++++++++++----------- drivers/spi/spi_sh_msiof.c |   11 -----------

 2 files changed, 14 insertions(+), 22 deletions(-)
drivers/spi/spi_bitbang.c      2009-11-25 15:30:45.000000000 +0900 renamed from drivers/spi/spi_bitbang.c +9 -0
@@ -176,6 +176,14 @@ int spi_bitbang_setup_transfer(struct sp
  }
  EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
 
+static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
+{
+       struct spi_bitbang_cs   *cs = spi->controller_state;
+       unsigned                nsecs = cs->nsecs;
+
+       return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t);
+}
+
  /**
  * spi_bitbang_setup - default setup for per-word I/O loops
  */
@@ -183,6 +191,7 @@ int spi_bitbang_setup(struct spi_device
  {
        struct spi_bitbang_cs   *cs = spi->controller_state;
        struct spi_bitbang      *bitbang;
+       int                     mode_mask = SPI_CPOL | SPI_CPHA;
        int                     retval;
        unsigned long           flags;
 
@@ -196,9 +205,11 @@ int spi_bitbang_setup(struct spi_device
        }

drivers/spi/spi_bitbang.c      2009-11-25 15:30:45.000000000 +0900 int spi_bitbang_setup(struct spi_device · line 212

Quoted 10 lines
drivers/spi/spi_bitbang.c      2009-11-25 15:30:45.000000000 +0900 renamed from drivers/spi/spi_bitbang.c +5 -3
 
        /* per-word shift register access, in hardware or bitbanging */
-       cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
-       if (!cs->txrx_word)
-               return -EINVAL;
+       if (bitbang->txrx_bufs == spi_bitbang_bufs) {
+               cs->txrx_word = bitbang->txrx_word[spi->mode & mode_mask];
+               if (!cs->txrx_word)
+                       return -EINVAL;
+       }

Hmmm... this smells like an ugly hack to me. It seems to me that if some bitbang backend drivers don't want this code, then it should be encoded into a callback so it can be overridden. Thoughts.

g.

Signature · 2 lines
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
#2 lore raw

Hi Grant,

Quoted 10 lines · Grant Likely
Quoted 9 lines · Magnus Damm

This patch modifies the shared spi bitbanging code to allow using spi_bitbang_setup() even though the txrx_word[] callbacks are unset. Useful for drivers that want to make use of spi_bitbang_setup() but have their own txrx_bufs() callback.

(quoted diff) +0 -0
@@ -196,9 +205,11 @@ int spi_bitbang_setup(struct spi_device
        }
 
Quoted 13 lines

Review int spi_bitbang_setup(struct spi_device · line 212

Quoted 9 lines
(quoted diff) +5 -3
        /* per-word shift register access, in hardware or bitbanging */
-       cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
-       if (!cs->txrx_word)
-               return -EINVAL;
+       if (bitbang->txrx_bufs == spi_bitbang_bufs) {
+               cs->txrx_word = bitbang->txrx_word[spi->mode & mode_mask];
+               if (!cs->txrx_word)
+                       return -EINVAL;
+       }

Hmmm... this smells like an ugly hack to me.  It seems to me that if some bitbang backend drivers don't want this code, then it should be encoded into a callback so it can be overridden.  Thoughts.

Yeah, it's far from clean. I want to make use of spi_bitbang_setup() in my MSIOF driver, but I want to avoid dummy txtx_word[] callbacks that will be unused since i'm using a driver specific bitbang->txrx_bufs function.

I guess the attached patch is slightly cleaner? I like the idea of letting bitbang drivers use shared code for spi_bitbang_setup()/spi_bitbang_cleanup() with their private setup_transfer() function which in turn calls spi_bitbang_setup_transfer(). My impression is that there's quite a bit of duplicated setup()/cleanup() code.

/ magnus

From: Magnus Damm <[email protected]>

---

 drivers/spi/spi_bitbang.c       |   37 ++++++++++++++++++++++++++++++-------
 drivers/spi/spi_gpio.c          |    2 +-
 include/linux/spi/spi_bitbang.h |    1 +
 3 files changed, 32 insertions(+), 8 deletions(-)
drivers/spi/spi_bitbang.c +30 -7
@@ -177,7 +177,7 @@ int spi_bitbang_setup_transfer(struct sp
 EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
 
 /**
- * spi_bitbang_setup - default setup for per-word I/O loops
+ * spi_bitbang_setup - setup function when bitbang->txrx_word[] are unused
  */
 int spi_bitbang_setup(struct spi_device *spi)
 {
@@ -195,11 +195,6 @@ int spi_bitbang_setup(struct spi_device 
 		spi->controller_state = cs;
 	}
 
-	/* per-word shift register access, in hardware or bitbanging */
-	cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
-	if (!cs->txrx_word)
-		return -EINVAL;
-
 	retval = bitbang->setup_transfer(spi, NULL);
 	if (retval < 0)
 		return retval;
@@ -224,6 +219,32 @@ int spi_bitbang_setup(struct spi_device 
 EXPORT_SYMBOL_GPL(spi_bitbang_setup);
 
 /**
+ * spi_bitbang_setup_word_mode - default setup for per-word I/O loops
+ */
+int spi_bitbang_setup_word_mode(struct spi_device *spi)
+{
+	struct spi_bitbang_cs	*cs;
+	struct spi_bitbang	*bitbang;
+	void			*txrx_word;
+	int			retval;
+
+	bitbang = spi_master_get_devdata(spi->master);
+
+	/* per-word shift register access, in hardware or bitbanging */
+	txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL | SPI_CPHA)];
+	if (!txrx_word)
+		return -EINVAL;
+
+	retval = spi_bitbang_setup(spi);
+	if (!retval) {
+		cs = spi->controller_state;
+		cs->txrx_word = txrx_word;
+	}
+	return retval;
+}
+EXPORT_SYMBOL_GPL(spi_bitbang_setup_word_mode);
+
+/**
  * spi_bitbang_cleanup - default cleanup for per-word I/O loops
  */
 void spi_bitbang_cleanup(struct spi_device *spi)
@@ -455,6 +476,8 @@ int spi_bitbang_start(struct spi_bitbang
 
 	if (!bitbang->master->transfer)
 		bitbang->master->transfer = spi_bitbang_transfer;
+
+	/* "word mode" when spi_bitbang_bufs() are used */
 	if (!bitbang->txrx_bufs) {
 		bitbang->use_dma = 0;
 		bitbang->txrx_bufs = spi_bitbang_bufs;
@@ -462,7 +485,7 @@ int spi_bitbang_start(struct spi_bitbang
 			if (!bitbang->setup_transfer)
 				bitbang->setup_transfer =
 					 spi_bitbang_setup_transfer;
-			bitbang->master->setup = spi_bitbang_setup;
+			bitbang->master->setup = spi_bitbang_setup_word_mode;
 			bitbang->master->cleanup = spi_bitbang_cleanup;
 		}
 	} else if (!bitbang->master->setup)
drivers/spi/spi_gpio.c +1 -1
@@ -201,7 +201,7 @@ static int spi_gpio_setup(struct spi_dev
 		}
 	}
 	if (!status)
-		status = spi_bitbang_setup(spi);
+		status = spi_bitbang_setup_word_mode(spi);
 	if (status) {
 		if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT)
 			gpio_free(cs);
include/linux/spi/spi_bitbang.h +1 -0
@@ -58,6 +58,7 @@ struct spi_bitbang {
  * methods, if you like.
  */
 extern int spi_bitbang_setup(struct spi_device *spi);
+extern int spi_bitbang_setup_word_mode(struct spi_device *spi);
 extern void spi_bitbang_cleanup(struct spi_device *spi);
 extern int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m);
 extern int spi_bitbang_setup_transfer(struct spi_device *spi,
#3 lore raw
Quoted 23 lines · Magnus Damm

Hi Grant,

Quoted 11 lines · Grant Likely
Quoted 9 lines · Magnus Damm

       /* per-word shift register access, in hardware or bitbanging */
-       cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
-       if (!cs->txrx_word)
-               return -EINVAL;
+       if (bitbang->txrx_bufs == spi_bitbang_bufs) {
+               cs->txrx_word = bitbang->txrx_word[spi->mode & mode_mask];
+               if (!cs->txrx_word)
+                       return -EINVAL;
+       }

Quoted 3 lines

Hmmm... this smells like an ugly hack to me.  It seems to me that if some bitbang backend drivers don't want this code, then it should be encoded into a callback so it can be overridden.  Thoughts.

Yeah, it's far from clean. I want to make use of spi_bitbang_setup() in my MSIOF driver, but I want to avoid dummy txtx_word[] callbacks that will be unused since i'm using a driver specific bitbang->txrx_bufs function.

Quoted 6 lines

I guess the attached patch is slightly cleaner? I like the idea of letting bitbang drivers use shared code for spi_bitbang_setup()/spi_bitbang_cleanup() with their private setup_transfer() function which in turn calls spi_bitbang_setup_transfer(). My impression is that there's quite a bit of duplicated setup()/cleanup() code.

This is certainly less ugly. But with the points brought up in the other thread, I want to have a close look at spi-bitbang before I start applying stuff. It seems nasty. Give me a few days.

g.

Signature · 2 lines
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
#4 lore raw
Quoted 29 lines · Grant Likely
Quoted 21 lines · Magnus Damm
Quoted 11 lines · Grant Likely
Quoted 9 lines · Magnus Damm

       /* per-word shift register access, in hardware or bitbanging */
-       cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
-       if (!cs->txrx_word)
-               return -EINVAL;
+       if (bitbang->txrx_bufs == spi_bitbang_bufs) {
+               cs->txrx_word = bitbang->txrx_word[spi->mode & mode_mask];
+               if (!cs->txrx_word)
+                       return -EINVAL;
+       }

Quoted 3 lines

Hmmm... this smells like an ugly hack to me.  It seems to me that if some bitbang backend drivers don't want this code, then it should be encoded into a callback so it can be overridden.  Thoughts.

Yeah, it's far from clean. I want to make use of spi_bitbang_setup() in my MSIOF driver, but I want to avoid dummy txtx_word[] callbacks that will be unused since i'm using a driver specific bitbang->txrx_bufs function.

Quoted 6 lines

I guess the attached patch is slightly cleaner? I like the idea of letting bitbang drivers use shared code for spi_bitbang_setup()/spi_bitbang_cleanup() with their private setup_transfer() function which in turn calls spi_bitbang_setup_transfer(). My impression is that there's quite a bit of duplicated setup()/cleanup() code.

Quoted 3 lines

This is certainly less ugly.  But with the points brought up in the other thread, I want to have a close look at spi-bitbang before I start applying stuff.  It seems nasty.  Give me a few days.

Sure, I plan on posting a V2 of the MSIOF driver. I plan to keep the dummy txrx_word() function for now - this to disconnect the cleanup from the driver integration, hope that sounds like a good plan.

Cheers,

/ magnus

#5 lore raw
Quoted 30 lines · Magnus Damm
Quoted 29 lines · Grant Likely
Quoted 27 lines · Magnus Damm
Quoted 11 lines · Grant Likely
Quoted 9 lines · Magnus Damm

       /* per-word shift register access, in hardware or bitbanging */
-       cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
-       if (!cs->txrx_word)
-               return -EINVAL;
+       if (bitbang->txrx_bufs == spi_bitbang_bufs) {
+               cs->txrx_word = bitbang->txrx_word[spi->mode & mode_mask];
+               if (!cs->txrx_word)
+                       return -EINVAL;
+       }

Quoted 3 lines

Hmmm... this smells like an ugly hack to me.  It seems to me that if some bitbang backend drivers don't want this code, then it should be encoded into a callback so it can be overridden.  Thoughts.

Yeah, it's far from clean. I want to make use of spi_bitbang_setup() in my MSIOF driver, but I want to avoid dummy txtx_word[] callbacks that will be unused since i'm using a driver specific bitbang->txrx_bufs function.

I guess the attached patch is slightly cleaner? I like the idea of letting bitbang drivers use shared code for spi_bitbang_setup()/spi_bitbang_cleanup() with their private setup_transfer() function which in turn calls spi_bitbang_setup_transfer(). My impression is that there's quite a bit of duplicated setup()/cleanup() code.

Quoted 7 lines
Quoted 3 lines

This is certainly less ugly.  But with the points brought up in the other thread, I want to have a close look at spi-bitbang before I start applying stuff.  It seems nasty.  Give me a few days.

Sure, I plan on posting a V2 of the MSIOF driver. I plan to keep the dummy txrx_word() function for now - this to disconnect the cleanup from the driver integration, hope that sounds like a good plan.

Works for me. Now I just need to find someone with the time to refactor spi-bitbang. :-)

g.

Signature · 2 lines
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.