diff mbox

[U-Boot,04/25] dm: spi: Move cmd device code into its own function

Message ID 1405385792-4469-5-git-send-email-sjg@chromium.org
State Superseded
Delegated to: Jagannadha Sutradharudu Teki
Headers show

Commit Message

Simon Glass July 15, 2014, 12:56 a.m. UTC
In preparation for changing the error handling in this code for driver
model, move it into its own function.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/cmd_spi.c | 53 ++++++++++++++++++++++++++++++++---------------------
 1 file changed, 32 insertions(+), 21 deletions(-)

Comments

Jagan Teki Aug. 25, 2014, 6:31 p.m. UTC | #1
On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
> In preparation for changing the error handling in this code for driver
> model, move it into its own function.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  common/cmd_spi.c | 53 ++++++++++++++++++++++++++++++++---------------------
>  1 file changed, 32 insertions(+), 21 deletions(-)
>

Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>

> diff --git a/common/cmd_spi.c b/common/cmd_spi.c
> index 3c8e913..be5709c 100644
> --- a/common/cmd_spi.c
> +++ b/common/cmd_spi.c
> @@ -11,6 +11,7 @@
>
>  #include <common.h>
>  #include <command.h>
> +#include <errno.h>
>  #include <spi.h>
>
>  /*-----------------------------------------------------------------------
> @@ -38,6 +39,35 @@ static int                   bitlen;
>  static uchar           dout[MAX_SPI_BYTES];
>  static uchar           din[MAX_SPI_BYTES];
>
> +static int do_spi_xfer(int bus, int cs)
> +{
> +       struct spi_slave *slave;
> +       int rcode = 0;
> +
> +       slave = spi_setup_slave(bus, cs, 1000000, mode);
> +       if (!slave) {
> +               printf("Invalid device %d:%d\n", bus, cs);
> +               return -EINVAL;
> +       }
> +
> +       spi_claim_bus(slave);
> +       if (spi_xfer(slave, bitlen, dout, din,
> +                    SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
> +               printf("Error during SPI transaction\n");
> +               rcode = -EIO;
> +       } else {
> +               int j;
> +
> +               for (j = 0; j < ((bitlen + 7) / 8); j++)
> +                       printf("%02X", din[j]);
> +               printf("\n");
> +       }
> +       spi_release_bus(slave);
> +       spi_free_slave(slave);
> +
> +       return rcode;
> +}
> +
>  /*
>   * SPI read/write
>   *
> @@ -51,11 +81,9 @@ static uchar                 din[MAX_SPI_BYTES];
>
>  int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
>  {
> -       struct spi_slave *slave;
>         char  *cp = 0;
>         uchar tmp;
>         int   j;
> -       int   rcode = 0;
>
>         /*
>          * We use the last specified parameters, unless new ones are
> @@ -103,27 +131,10 @@ int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
>                 return 1;
>         }
>
> -       slave = spi_setup_slave(bus, cs, 1000000, mode);
> -       if (!slave) {
> -               printf("Invalid device %d:%d\n", bus, cs);
> +       if (do_spi_xfer(bus, cs))
>                 return 1;
> -       }
> -
> -       spi_claim_bus(slave);
> -       if(spi_xfer(slave, bitlen, dout, din,
> -                               SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
> -               printf("Error during SPI transaction\n");
> -               rcode = 1;
> -       } else {
> -               for(j = 0; j < ((bitlen + 7) / 8); j++) {
> -                       printf("%02X", din[j]);
> -               }
> -               printf("\n");
> -       }
> -       spi_release_bus(slave);
> -       spi_free_slave(slave);
>
> -       return rcode;
> +       return 0;
>  }
>
>  /***************************************************/
> --
> 2.0.0.526.g5318336
>

thanks!
Simon Glass Sept. 2, 2014, 7:22 p.m. UTC | #2
Hi Jagan,

On 25 August 2014 12:31, Jagan Teki <jagannadh.teki@gmail.com> wrote:
> On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
>> In preparation for changing the error handling in this code for driver
>> model, move it into its own function.
>>
>> Signed-off-by: Simon Glass <sjg@chromium.org>
>> ---
>>
>>  common/cmd_spi.c | 53 ++++++++++++++++++++++++++++++++---------------------
>>  1 file changed, 32 insertions(+), 21 deletions(-)
>>
>
> Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>

Thanks. I'd quite like to pull these reviewed patches through the -dm
tree as it is easier for me to keep things consistent. Is that OK with
you?

Regards,
Simon
diff mbox

Patch

diff --git a/common/cmd_spi.c b/common/cmd_spi.c
index 3c8e913..be5709c 100644
--- a/common/cmd_spi.c
+++ b/common/cmd_spi.c
@@ -11,6 +11,7 @@ 
 
 #include <common.h>
 #include <command.h>
+#include <errno.h>
 #include <spi.h>
 
 /*-----------------------------------------------------------------------
@@ -38,6 +39,35 @@  static int   		bitlen;
 static uchar 		dout[MAX_SPI_BYTES];
 static uchar 		din[MAX_SPI_BYTES];
 
+static int do_spi_xfer(int bus, int cs)
+{
+	struct spi_slave *slave;
+	int rcode = 0;
+
+	slave = spi_setup_slave(bus, cs, 1000000, mode);
+	if (!slave) {
+		printf("Invalid device %d:%d\n", bus, cs);
+		return -EINVAL;
+	}
+
+	spi_claim_bus(slave);
+	if (spi_xfer(slave, bitlen, dout, din,
+		     SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
+		printf("Error during SPI transaction\n");
+		rcode = -EIO;
+	} else {
+		int j;
+
+		for (j = 0; j < ((bitlen + 7) / 8); j++)
+			printf("%02X", din[j]);
+		printf("\n");
+	}
+	spi_release_bus(slave);
+	spi_free_slave(slave);
+
+	return rcode;
+}
+
 /*
  * SPI read/write
  *
@@ -51,11 +81,9 @@  static uchar 		din[MAX_SPI_BYTES];
 
 int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	struct spi_slave *slave;
 	char  *cp = 0;
 	uchar tmp;
 	int   j;
-	int   rcode = 0;
 
 	/*
 	 * We use the last specified parameters, unless new ones are
@@ -103,27 +131,10 @@  int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		return 1;
 	}
 
-	slave = spi_setup_slave(bus, cs, 1000000, mode);
-	if (!slave) {
-		printf("Invalid device %d:%d\n", bus, cs);
+	if (do_spi_xfer(bus, cs))
 		return 1;
-	}
-
-	spi_claim_bus(slave);
-	if(spi_xfer(slave, bitlen, dout, din,
-				SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
-		printf("Error during SPI transaction\n");
-		rcode = 1;
-	} else {
-		for(j = 0; j < ((bitlen + 7) / 8); j++) {
-			printf("%02X", din[j]);
-		}
-		printf("\n");
-	}
-	spi_release_bus(slave);
-	spi_free_slave(slave);
 
-	return rcode;
+	return 0;
 }
 
 /***************************************************/