Commit df3b23ae3a7bb0627d595e6139ea43655d5acaa4

Authored by Simon Glass
1 parent 5e74934d40

dm: spi: Move cmd device code into its own function

In preparation for changing the error handling in this code for driver
model, move it into its own function.

Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

Showing 1 changed file with 32 additions and 21 deletions Side-by-side Diff

... ... @@ -11,6 +11,7 @@
11 11  
12 12 #include <common.h>
13 13 #include <command.h>
  14 +#include <errno.h>
14 15 #include <spi.h>
15 16  
16 17 /*-----------------------------------------------------------------------
... ... @@ -38,6 +39,35 @@
38 39 static uchar dout[MAX_SPI_BYTES];
39 40 static uchar din[MAX_SPI_BYTES];
40 41  
  42 +static int do_spi_xfer(int bus, int cs)
  43 +{
  44 + struct spi_slave *slave;
  45 + int rcode = 0;
  46 +
  47 + slave = spi_setup_slave(bus, cs, 1000000, mode);
  48 + if (!slave) {
  49 + printf("Invalid device %d:%d\n", bus, cs);
  50 + return -EINVAL;
  51 + }
  52 +
  53 + spi_claim_bus(slave);
  54 + if (spi_xfer(slave, bitlen, dout, din,
  55 + SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
  56 + printf("Error during SPI transaction\n");
  57 + rcode = -EIO;
  58 + } else {
  59 + int j;
  60 +
  61 + for (j = 0; j < ((bitlen + 7) / 8); j++)
  62 + printf("%02X", din[j]);
  63 + printf("\n");
  64 + }
  65 + spi_release_bus(slave);
  66 + spi_free_slave(slave);
  67 +
  68 + return rcode;
  69 +}
  70 +
41 71 /*
42 72 * SPI read/write
43 73 *
44 74  
... ... @@ -51,11 +81,9 @@
51 81  
52 82 int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
53 83 {
54   - struct spi_slave *slave;
55 84 char *cp = 0;
56 85 uchar tmp;
57 86 int j;
58   - int rcode = 0;
59 87  
60 88 /*
61 89 * We use the last specified parameters, unless new ones are
62 90  
63 91  
... ... @@ -103,27 +131,10 @@
103 131 return 1;
104 132 }
105 133  
106   - slave = spi_setup_slave(bus, cs, 1000000, mode);
107   - if (!slave) {
108   - printf("Invalid device %d:%d\n", bus, cs);
  134 + if (do_spi_xfer(bus, cs))
109 135 return 1;
110   - }
111 136  
112   - spi_claim_bus(slave);
113   - if(spi_xfer(slave, bitlen, dout, din,
114   - SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
115   - printf("Error during SPI transaction\n");
116   - rcode = 1;
117   - } else {
118   - for(j = 0; j < ((bitlen + 7) / 8); j++) {
119   - printf("%02X", din[j]);
120   - }
121   - printf("\n");
122   - }
123   - spi_release_bus(slave);
124   - spi_free_slave(slave);
125   -
126   - return rcode;
  137 + return 0;
127 138 }
128 139  
129 140 /***************************************************/