Commit 7a3eff4ce962de50bd9a1d83a9fce178c04999d3

Authored by Peng Fan
Committed by Simon Glass
1 parent 102412c415

dm: spi: introduce dm api

Introduce dm_spi_claim_bus, dm_spi_release_bus and dm_spi_xfer
Convert spi_claim_bus, spi_release_bus and spi_xfer to use
the new API.

Signed-off-by: Peng Fan <van.freenix@gmail.com>
Cc: Simon Glass <sjg@chromium.org>
Cc: Jagan Teki <jteki@openedev.com>
Acked-by: Simon Glass <sjg@chromium.org>

Showing 2 changed files with 73 additions and 7 deletions Side-by-side Diff

drivers/spi/spi-uclass.c
... ... @@ -45,12 +45,12 @@
45 45 return 0;
46 46 }
47 47  
48   -int spi_claim_bus(struct spi_slave *slave)
  48 +int dm_spi_claim_bus(struct udevice *dev)
49 49 {
50   - struct udevice *dev = slave->dev;
51 50 struct udevice *bus = dev->parent;
52 51 struct dm_spi_ops *ops = spi_get_ops(bus);
53 52 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
  53 + struct spi_slave *slave = dev_get_parent_priv(dev);
54 54 int speed;
55 55 int ret;
56 56  
57 57  
... ... @@ -73,9 +73,8 @@
73 73 return ops->claim_bus ? ops->claim_bus(dev) : 0;
74 74 }
75 75  
76   -void spi_release_bus(struct spi_slave *slave)
  76 +void dm_spi_release_bus(struct udevice *dev)
77 77 {
78   - struct udevice *dev = slave->dev;
79 78 struct udevice *bus = dev->parent;
80 79 struct dm_spi_ops *ops = spi_get_ops(bus);
81 80  
82 81  
83 82  
... ... @@ -83,16 +82,31 @@
83 82 ops->release_bus(dev);
84 83 }
85 84  
86   -int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
87   - const void *dout, void *din, unsigned long flags)
  85 +int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
  86 + const void *dout, void *din, unsigned long flags)
88 87 {
89   - struct udevice *dev = slave->dev;
90 88 struct udevice *bus = dev->parent;
91 89  
92 90 if (bus->uclass->uc_drv->id != UCLASS_SPI)
93 91 return -EOPNOTSUPP;
94 92  
95 93 return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags);
  94 +}
  95 +
  96 +int spi_claim_bus(struct spi_slave *slave)
  97 +{
  98 + return dm_spi_claim_bus(slave->dev);
  99 +}
  100 +
  101 +void spi_release_bus(struct spi_slave *slave)
  102 +{
  103 + dm_spi_release_bus(slave->dev);
  104 +}
  105 +
  106 +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  107 + const void *dout, void *din, unsigned long flags)
  108 +{
  109 + return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
96 110 }
97 111  
98 112 static int spi_post_bind(struct udevice *dev)
... ... @@ -612,6 +612,58 @@
612 612 struct udevice *bus, struct udevice *slave,
613 613 struct udevice **emulp);
614 614  
  615 +/**
  616 + * Claim the bus and prepare it for communication with a given slave.
  617 + *
  618 + * This must be called before doing any transfers with a SPI slave. It
  619 + * will enable and initialize any SPI hardware as necessary, and make
  620 + * sure that the SCK line is in the correct idle state. It is not
  621 + * allowed to claim the same bus for several slaves without releasing
  622 + * the bus in between.
  623 + *
  624 + * @dev: The SPI slave device
  625 + *
  626 + * Returns: 0 if the bus was claimed successfully, or a negative value
  627 + * if it wasn't.
  628 + */
  629 +int dm_spi_claim_bus(struct udevice *dev);
  630 +
  631 +/**
  632 + * Release the SPI bus
  633 + *
  634 + * This must be called once for every call to dm_spi_claim_bus() after
  635 + * all transfers have finished. It may disable any SPI hardware as
  636 + * appropriate.
  637 + *
  638 + * @slave: The SPI slave device
  639 + */
  640 +void dm_spi_release_bus(struct udevice *dev);
  641 +
  642 +/**
  643 + * SPI transfer
  644 + *
  645 + * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  646 + * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  647 + *
  648 + * The source of the outgoing bits is the "dout" parameter and the
  649 + * destination of the input bits is the "din" parameter. Note that "dout"
  650 + * and "din" can point to the same memory location, in which case the
  651 + * input data overwrites the output data (since both are buffered by
  652 + * temporary variables, this is OK).
  653 + *
  654 + * dm_spi_xfer() interface:
  655 + * @dev: The SPI slave device which will be sending/receiving the data.
  656 + * @bitlen: How many bits to write and read.
  657 + * @dout: Pointer to a string of bits to send out. The bits are
  658 + * held in a byte array and are sent MSB first.
  659 + * @din: Pointer to a string of bits that will be filled in.
  660 + * @flags: A bitwise combination of SPI_XFER_* flags.
  661 + *
  662 + * Returns: 0 on success, not 0 on failure
  663 + */
  664 +int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
  665 + const void *dout, void *din, unsigned long flags);
  666 +
615 667 /* Access the operations for a SPI device */
616 668 #define spi_get_ops(dev) ((struct dm_spi_ops *)(dev)->driver->ops)
617 669 #define spi_emul_get_ops(dev) ((struct dm_spi_emul_ops *)(dev)->driver->ops)