Commit d7af6a485126a0d08a0a9a56721e42a3e78b5b53

Authored by Simon Glass
1 parent 547cea19b8

dm: spi: Add a uclass for SPI

Add a uclass which provides access to SPI buses and includes operations
required by SPI.

For a time driver model will need to co-exist with the legacy SPI interface
so some parts of the header file are changed depending on which is in use.
The exports are adjusted also since some functions are not available with
driver model.

Boards must define CONFIG_DM_SPI to use driver model for SPI.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Jagannadha Sutradharudu Teki <jagannadh.teki@gmail.com>
(Discussed some follow-up comments which will address in future add-ons)

Showing 5 changed files with 650 additions and 4 deletions Side-by-side Diff

... ... @@ -27,10 +27,12 @@
27 27 # define i2c_write dummy
28 28 # define i2c_read dummy
29 29 #endif
30   -#ifndef CONFIG_CMD_SPI
  30 +#if !defined(CONFIG_CMD_SPI) || defined(CONFIG_DM_SPI)
31 31 # define spi_init dummy
32 32 # define spi_setup_slave dummy
33 33 # define spi_free_slave dummy
  34 +#endif
  35 +#ifndef CONFIG_CMD_SPI
34 36 # define spi_claim_bus dummy
35 37 # define spi_release_bus dummy
36 38 # define spi_xfer dummy
drivers/spi/Makefile
... ... @@ -6,7 +6,11 @@
6 6 #
7 7  
8 8 # There are many options which enable SPI, so make this library available
  9 +ifdef CONFIG_DM_SPI
  10 +obj-y += spi-uclass.o
  11 +else
9 12 obj-y += spi.o
  13 +endif
10 14  
11 15 obj-$(CONFIG_EP93XX_SPI) += ep93xx_spi.o
12 16 obj-$(CONFIG_ALTERA_SPI) += altera_spi.o
drivers/spi/spi-uclass.c
  1 +/*
  2 + * Copyright (c) 2014 Google, Inc
  3 + *
  4 + * SPDX-License-Identifier: GPL-2.0+
  5 + */
  6 +
  7 +#include <common.h>
  8 +#include <dm.h>
  9 +#include <errno.h>
  10 +#include <fdtdec.h>
  11 +#include <malloc.h>
  12 +#include <spi.h>
  13 +#include <dm/device-internal.h>
  14 +#include <dm/uclass-internal.h>
  15 +#include <dm/root.h>
  16 +#include <dm/lists.h>
  17 +#include <dm/util.h>
  18 +
  19 +DECLARE_GLOBAL_DATA_PTR;
  20 +
  21 +static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
  22 +{
  23 + struct dm_spi_ops *ops;
  24 + int ret;
  25 +
  26 + ops = spi_get_ops(bus);
  27 + if (ops->set_speed)
  28 + ret = ops->set_speed(bus, speed);
  29 + else
  30 + ret = -EINVAL;
  31 + if (ret) {
  32 + printf("Cannot set speed (err=%d)\n", ret);
  33 + return ret;
  34 + }
  35 +
  36 + if (ops->set_mode)
  37 + ret = ops->set_mode(bus, mode);
  38 + else
  39 + ret = -EINVAL;
  40 + if (ret) {
  41 + printf("Cannot set mode (err=%d)\n", ret);
  42 + return ret;
  43 + }
  44 +
  45 + return 0;
  46 +}
  47 +
  48 +int spi_claim_bus(struct spi_slave *slave)
  49 +{
  50 + struct udevice *dev = slave->dev;
  51 + struct udevice *bus = dev->parent;
  52 + struct dm_spi_ops *ops = spi_get_ops(bus);
  53 + struct dm_spi_bus *spi = bus->uclass_priv;
  54 + int speed;
  55 + int ret;
  56 +
  57 + speed = slave->max_hz;
  58 + if (spi->max_hz) {
  59 + if (speed)
  60 + speed = min(speed, spi->max_hz);
  61 + else
  62 + speed = spi->max_hz;
  63 + }
  64 + if (!speed)
  65 + speed = 100000;
  66 + ret = spi_set_speed_mode(bus, speed, slave->mode);
  67 + if (ret)
  68 + return ret;
  69 +
  70 + return ops->claim_bus ? ops->claim_bus(bus) : 0;
  71 +}
  72 +
  73 +void spi_release_bus(struct spi_slave *slave)
  74 +{
  75 + struct udevice *dev = slave->dev;
  76 + struct udevice *bus = dev->parent;
  77 + struct dm_spi_ops *ops = spi_get_ops(bus);
  78 +
  79 + if (ops->release_bus)
  80 + ops->release_bus(bus);
  81 +}
  82 +
  83 +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  84 + const void *dout, void *din, unsigned long flags)
  85 +{
  86 + struct udevice *dev = slave->dev;
  87 + struct udevice *bus = dev->parent;
  88 +
  89 + if (bus->uclass->uc_drv->id != UCLASS_SPI)
  90 + return -EOPNOTSUPP;
  91 +
  92 + return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags);
  93 +}
  94 +
  95 +int spi_post_bind(struct udevice *dev)
  96 +{
  97 + /* Scan the bus for devices */
  98 + return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
  99 +}
  100 +
  101 +int spi_post_probe(struct udevice *dev)
  102 +{
  103 + struct dm_spi_bus *spi = dev->uclass_priv;
  104 +
  105 + spi->max_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  106 + "spi-max-frequency", 0);
  107 +
  108 + return 0;
  109 +}
  110 +
  111 +int spi_chip_select(struct udevice *dev)
  112 +{
  113 + struct spi_slave *slave = dev_get_parentdata(dev);
  114 +
  115 + return slave ? slave->cs : -ENOENT;
  116 +}
  117 +
  118 +/**
  119 + * spi_find_chip_select() - Find the slave attached to chip select
  120 + *
  121 + * @bus: SPI bus to search
  122 + * @cs: Chip select to look for
  123 + * @devp: Returns the slave device if found
  124 + * @return 0 if found, -ENODEV on error
  125 + */
  126 +static int spi_find_chip_select(struct udevice *bus, int cs,
  127 + struct udevice **devp)
  128 +{
  129 + struct udevice *dev;
  130 +
  131 + for (device_find_first_child(bus, &dev); dev;
  132 + device_find_next_child(&dev)) {
  133 + struct spi_slave store;
  134 + struct spi_slave *slave = dev_get_parentdata(dev);
  135 +
  136 + if (!slave) {
  137 + slave = &store;
  138 + spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset,
  139 + slave);
  140 + }
  141 + debug("%s: slave=%p, cs=%d\n", __func__, slave,
  142 + slave ? slave->cs : -1);
  143 + if (slave && slave->cs == cs) {
  144 + *devp = dev;
  145 + return 0;
  146 + }
  147 + }
  148 +
  149 + return -ENODEV;
  150 +}
  151 +
  152 +int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
  153 +{
  154 + struct spi_cs_info info;
  155 + struct udevice *bus;
  156 + int ret;
  157 +
  158 + ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
  159 + if (ret) {
  160 + debug("%s: No bus %d\n", __func__, busnum);
  161 + return ret;
  162 + }
  163 +
  164 + return spi_cs_info(bus, cs, &info);
  165 +}
  166 +
  167 +int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
  168 +{
  169 + struct spi_cs_info local_info;
  170 + struct dm_spi_ops *ops;
  171 + int ret;
  172 +
  173 + if (!info)
  174 + info = &local_info;
  175 +
  176 + /* If there is a device attached, return it */
  177 + info->dev = NULL;
  178 + ret = spi_find_chip_select(bus, cs, &info->dev);
  179 + if (!ret)
  180 + return 0;
  181 +
  182 + /*
  183 + * Otherwise ask the driver. For the moment we don't have CS info.
  184 + * When we do we could provide the driver with a helper function
  185 + * to figure out what chip selects are valid, or just handle the
  186 + * request.
  187 + */
  188 + ops = spi_get_ops(bus);
  189 + if (ops->cs_info)
  190 + return ops->cs_info(bus, cs, info);
  191 +
  192 + /*
  193 + * We could assume there is at least one valid chip select, but best
  194 + * to be sure and return an error in this case. The driver didn't
  195 + * care enough to tell us.
  196 + */
  197 + return -ENODEV;
  198 +}
  199 +
  200 +int spi_bind_device(struct udevice *bus, int cs, const char *drv_name,
  201 + const char *dev_name, struct udevice **devp)
  202 +{
  203 + struct driver *drv;
  204 + int ret;
  205 +
  206 + drv = lists_driver_lookup_name(drv_name);
  207 + if (!drv) {
  208 + printf("Cannot find driver '%s'\n", drv_name);
  209 + return -ENOENT;
  210 + }
  211 + ret = device_bind(bus, drv, dev_name, NULL, -1, devp);
  212 + if (ret) {
  213 + printf("Cannot create device named '%s' (err=%d)\n",
  214 + dev_name, ret);
  215 + return ret;
  216 + }
  217 +
  218 + return 0;
  219 +}
  220 +
  221 +int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
  222 + struct udevice **devp)
  223 +{
  224 + struct udevice *bus, *dev;
  225 + int ret;
  226 +
  227 + ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
  228 + if (ret) {
  229 + debug("%s: No bus %d\n", __func__, busnum);
  230 + return ret;
  231 + }
  232 + ret = spi_find_chip_select(bus, cs, &dev);
  233 + if (ret) {
  234 + debug("%s: No cs %d\n", __func__, cs);
  235 + return ret;
  236 + }
  237 + *busp = bus;
  238 + *devp = dev;
  239 +
  240 + return ret;
  241 +}
  242 +
  243 +int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
  244 + const char *drv_name, const char *dev_name,
  245 + struct udevice **busp, struct spi_slave **devp)
  246 +{
  247 + struct udevice *bus, *dev;
  248 + struct spi_slave *slave;
  249 + bool created = false;
  250 + int ret;
  251 +
  252 + ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
  253 + if (ret) {
  254 + printf("Invalid bus %d (err=%d)\n", busnum, ret);
  255 + return ret;
  256 + }
  257 + ret = spi_find_chip_select(bus, cs, &dev);
  258 +
  259 + /*
  260 + * If there is no such device, create one automatically. This means
  261 + * that we don't need a device tree node or platform data for the
  262 + * SPI flash chip - we will bind to the correct driver.
  263 + */
  264 + if (ret == -ENODEV && drv_name) {
  265 + debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
  266 + __func__, dev_name, busnum, cs, drv_name);
  267 + ret = spi_bind_device(bus, cs, drv_name, dev_name, &dev);
  268 + if (ret)
  269 + return ret;
  270 + created = true;
  271 + } else if (ret) {
  272 + printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
  273 + ret);
  274 + return ret;
  275 + }
  276 +
  277 + if (!device_active(dev)) {
  278 + slave = (struct spi_slave *)calloc(1,
  279 + sizeof(struct spi_slave));
  280 + if (!slave) {
  281 + ret = -ENOMEM;
  282 + goto err;
  283 + }
  284 +
  285 + ret = spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset,
  286 + slave);
  287 + if (ret)
  288 + goto err;
  289 + slave->cs = cs;
  290 + slave->dev = dev;
  291 + ret = device_probe_child(dev, slave);
  292 + free(slave);
  293 + if (ret)
  294 + goto err;
  295 + }
  296 +
  297 + ret = spi_set_speed_mode(bus, speed, mode);
  298 + if (ret)
  299 + goto err;
  300 +
  301 + *busp = bus;
  302 + *devp = dev_get_parentdata(dev);
  303 + debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
  304 +
  305 + return 0;
  306 +
  307 +err:
  308 + if (created) {
  309 + device_remove(dev);
  310 + device_unbind(dev);
  311 + }
  312 +
  313 + return ret;
  314 +}
  315 +
  316 +/* Compatibility function - to be removed */
  317 +struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
  318 + int bus_node)
  319 +{
  320 + struct udevice *bus, *dev;
  321 + int ret;
  322 +
  323 + ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus);
  324 + if (ret)
  325 + return NULL;
  326 + ret = device_get_child_by_of_offset(bus, node, &dev);
  327 + if (ret)
  328 + return NULL;
  329 + return dev_get_parentdata(dev);
  330 +}
  331 +
  332 +/* Compatibility function - to be removed */
  333 +struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
  334 + unsigned int speed, unsigned int mode)
  335 +{
  336 + struct spi_slave *slave;
  337 + struct udevice *dev;
  338 + int ret;
  339 +
  340 + ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
  341 + &slave);
  342 + if (ret)
  343 + return NULL;
  344 +
  345 + return slave;
  346 +}
  347 +
  348 +void spi_free_slave(struct spi_slave *slave)
  349 +{
  350 + device_remove(slave->dev);
  351 + slave->dev = NULL;
  352 +}
  353 +
  354 +int spi_ofdata_to_platdata(const void *blob, int node,
  355 + struct spi_slave *spi)
  356 +{
  357 + int mode = 0;
  358 +
  359 + spi->cs = fdtdec_get_int(blob, node, "reg", -1);
  360 + spi->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 0);
  361 + if (fdtdec_get_bool(blob, node, "spi-cpol"))
  362 + mode |= SPI_CPOL;
  363 + if (fdtdec_get_bool(blob, node, "spi-cpha"))
  364 + mode |= SPI_CPHA;
  365 + if (fdtdec_get_bool(blob, node, "spi-cs-high"))
  366 + mode |= SPI_CS_HIGH;
  367 + if (fdtdec_get_bool(blob, node, "spi-half-duplex"))
  368 + mode |= SPI_PREAMBLE;
  369 + spi->mode = mode;
  370 +
  371 + return 0;
  372 +}
  373 +
  374 +UCLASS_DRIVER(spi) = {
  375 + .id = UCLASS_SPI,
  376 + .name = "spi",
  377 + .post_bind = spi_post_bind,
  378 + .post_probe = spi_post_probe,
  379 + .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
  380 +};
  381 +
  382 +UCLASS_DRIVER(spi_generic) = {
  383 + .id = UCLASS_SPI_GENERIC,
  384 + .name = "spi_generic",
  385 +};
  386 +
  387 +U_BOOT_DRIVER(spi_generic_drv) = {
  388 + .name = "spi_generic_drv",
  389 + .id = UCLASS_SPI_GENERIC,
  390 +};
include/dm/uclass-id.h
... ... @@ -22,6 +22,8 @@
22 22 /* U-Boot uclasses start here */
23 23 UCLASS_GPIO, /* Bank of general-purpose I/O pins */
24 24 UCLASS_SERIAL, /* Serial UART */
  25 + UCLASS_SPI, /* SPI bus */
  26 + UCLASS_SPI_GENERIC, /* Generic SPI flash target */
25 27  
26 28 UCLASS_COUNT,
27 29 UCLASS_INVALID = -1,
... ... @@ -54,12 +54,31 @@
54 54  
55 55 #define SPI_DEFAULT_WORDLEN 8
56 56  
  57 +#ifdef CONFIG_DM_SPI
  58 +struct dm_spi_bus {
  59 + uint max_hz;
  60 +};
  61 +
  62 +#endif /* CONFIG_DM_SPI */
  63 +
57 64 /**
58 65 * struct spi_slave - Representation of a SPI slave
59 66 *
60   - * Drivers are expected to extend this with controller-specific data.
  67 + * For driver model this is the per-child data used by the SPI bus. It can
  68 + * be accessed using dev_get_parentdata() on the slave device. Each SPI
  69 + * driver should define this child data in its U_BOOT_DRIVER() definition:
61 70 *
62   - * @bus: ID of the bus that the slave is attached to.
  71 + * .per_child_auto_alloc_size = sizeof(struct spi_slave),
  72 + *
  73 + * If not using driver model, drivers are expected to extend this with
  74 + * controller-specific data.
  75 + *
  76 + * @dev: SPI slave device
  77 + * @max_hz: Maximum speed for this slave
  78 + * @mode: SPI mode to use for this slave (see SPI mode flags)
  79 + * @bus: ID of the bus that the slave is attached to. For
  80 + * driver model this is the sequence number of the SPI
  81 + * bus (bus->seq) so does not need to be stored
63 82 * @cs: ID of the chip select connected to the slave.
64 83 * @op_mode_rx: SPI RX operation mode.
65 84 * @op_mode_tx: SPI TX operation mode.
66 85  
... ... @@ -71,7 +90,13 @@
71 90 * @flags: Indication of SPI flags.
72 91 */
73 92 struct spi_slave {
  93 +#ifdef CONFIG_DM_SPI
  94 + struct udevice *dev; /* struct spi_slave is dev->parentdata */
  95 + uint max_hz;
  96 + uint mode;
  97 +#else
74 98 unsigned int bus;
  99 +#endif
75 100 unsigned int cs;
76 101 u8 op_mode_rx;
77 102 u8 op_mode_tx;
78 103  
... ... @@ -228,8 +253,9 @@
228 253 * Returns: 1 if bus:cs identifies a valid chip on this board, 0
229 254 * otherwise.
230 255 */
231   -int spi_cs_is_valid(unsigned int bus, unsigned int cs);
  256 +int spi_cs_is_valid(unsigned int bus, unsigned int cs);
232 257  
  258 +#ifndef CONFIG_DM_SPI
233 259 /**
234 260 * Activate a SPI chipselect.
235 261 * This function is provided by the board code when using a driver
... ... @@ -255,6 +281,7 @@
255 281 * @hz: The transfer speed
256 282 */
257 283 void spi_set_speed(struct spi_slave *slave, uint hz);
  284 +#endif
258 285  
259 286 /**
260 287 * Write 8 bits, then read 8 bits.
... ... @@ -304,6 +331,227 @@
304 331 */
305 332 struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum,
306 333 int node);
  334 +
  335 +#ifdef CONFIG_DM_SPI
  336 +
  337 +/**
  338 + * struct spi_cs_info - Information about a bus chip select
  339 + *
  340 + * @dev: Connected device, or NULL if none
  341 + */
  342 +struct spi_cs_info {
  343 + struct udevice *dev;
  344 +};
  345 +
  346 +/**
  347 + * struct struct dm_spi_ops - Driver model SPI operations
  348 + *
  349 + * The uclass interface is implemented by all SPI devices which use
  350 + * driver model.
  351 + */
  352 +struct dm_spi_ops {
  353 + /**
  354 + * Claim the bus and prepare it for communication.
  355 + *
  356 + * The device provided is the slave device. It's parent controller
  357 + * will be used to provide the communication.
  358 + *
  359 + * This must be called before doing any transfers with a SPI slave. It
  360 + * will enable and initialize any SPI hardware as necessary, and make
  361 + * sure that the SCK line is in the correct idle state. It is not
  362 + * allowed to claim the same bus for several slaves without releasing
  363 + * the bus in between.
  364 + *
  365 + * @bus: The SPI slave
  366 + *
  367 + * Returns: 0 if the bus was claimed successfully, or a negative value
  368 + * if it wasn't.
  369 + */
  370 + int (*claim_bus)(struct udevice *bus);
  371 +
  372 + /**
  373 + * Release the SPI bus
  374 + *
  375 + * This must be called once for every call to spi_claim_bus() after
  376 + * all transfers have finished. It may disable any SPI hardware as
  377 + * appropriate.
  378 + *
  379 + * @bus: The SPI slave
  380 + */
  381 + int (*release_bus)(struct udevice *bus);
  382 +
  383 + /**
  384 + * Set the word length for SPI transactions
  385 + *
  386 + * Set the word length (number of bits per word) for SPI transactions.
  387 + *
  388 + * @bus: The SPI slave
  389 + * @wordlen: The number of bits in a word
  390 + *
  391 + * Returns: 0 on success, -ve on failure.
  392 + */
  393 + int (*set_wordlen)(struct udevice *bus, unsigned int wordlen);
  394 +
  395 + /**
  396 + * SPI transfer
  397 + *
  398 + * This writes "bitlen" bits out the SPI MOSI port and simultaneously
  399 + * clocks "bitlen" bits in the SPI MISO port. That's just the way SPI
  400 + * works.
  401 + *
  402 + * The source of the outgoing bits is the "dout" parameter and the
  403 + * destination of the input bits is the "din" parameter. Note that
  404 + * "dout" and "din" can point to the same memory location, in which
  405 + * case the input data overwrites the output data (since both are
  406 + * buffered by temporary variables, this is OK).
  407 + *
  408 + * spi_xfer() interface:
  409 + * @dev: The slave device to communicate with
  410 + * @bitlen: How many bits to write and read.
  411 + * @dout: Pointer to a string of bits to send out. The bits are
  412 + * held in a byte array and are sent MSB first.
  413 + * @din: Pointer to a string of bits that will be filled in.
  414 + * @flags: A bitwise combination of SPI_XFER_* flags.
  415 + *
  416 + * Returns: 0 on success, not -1 on failure
  417 + */
  418 + int (*xfer)(struct udevice *dev, unsigned int bitlen, const void *dout,
  419 + void *din, unsigned long flags);
  420 +
  421 + /**
  422 + * Set transfer speed.
  423 + * This sets a new speed to be applied for next spi_xfer().
  424 + * @bus: The SPI bus
  425 + * @hz: The transfer speed
  426 + * @return 0 if OK, -ve on error
  427 + */
  428 + int (*set_speed)(struct udevice *bus, uint hz);
  429 +
  430 + /**
  431 + * Set the SPI mode/flags
  432 + *
  433 + * It is unclear if we want to set speed and mode together instead
  434 + * of separately.
  435 + *
  436 + * @bus: The SPI bus
  437 + * @mode: Requested SPI mode (SPI_... flags)
  438 + * @return 0 if OK, -ve on error
  439 + */
  440 + int (*set_mode)(struct udevice *bus, uint mode);
  441 +
  442 + /**
  443 + * Get information on a chip select
  444 + *
  445 + * This is only called when the SPI uclass does not know about a
  446 + * chip select, i.e. it has no attached device. It gives the driver
  447 + * a chance to allow activity on that chip select even so.
  448 + *
  449 + * @bus: The SPI bus
  450 + * @cs: The chip select (0..n-1)
  451 + * @info: Returns information about the chip select, if valid.
  452 + * On entry info->dev is NULL
  453 + * @return 0 if OK (and @info is set up), -ENODEV if the chip select
  454 + * is invalid, other -ve value on error
  455 + */
  456 + int (*cs_info)(struct udevice *bus, uint cs, struct spi_cs_info *info);
  457 +};
  458 +
  459 +/**
  460 + * spi_find_bus_and_cs() - Find bus and slave devices by number
  461 + *
  462 + * Given a bus number and chip select, this finds the corresponding bus
  463 + * device and slave device. Neither device is activated by this function,
  464 + * although they may have been activated previously.
  465 + *
  466 + * @busnum: SPI bus number
  467 + * @cs: Chip select to look for
  468 + * @busp: Returns bus device
  469 + * @devp: Return slave device
  470 + * @return 0 if found, -ENODEV on error
  471 + */
  472 +int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
  473 + struct udevice **devp);
  474 +
  475 +/**
  476 + * spi_get_bus_and_cs() - Find and activate bus and slave devices by number
  477 + *
  478 + * Given a bus number and chip select, this finds the corresponding bus
  479 + * device and slave device.
  480 + *
  481 + * If no such slave exists, and drv_name is not NULL, then a new slave device
  482 + * is automatically bound on this chip select.
  483 + *
  484 + * Ths new slave device is probed ready for use with the given speed and mode.
  485 + *
  486 + * @busnum: SPI bus number
  487 + * @cs: Chip select to look for
  488 + * @speed: SPI speed to use for this slave
  489 + * @mode: SPI mode to use for this slave
  490 + * @drv_name: Name of driver to attach to this chip select
  491 + * @dev_name: Name of the new device thus created
  492 + * @busp: Returns bus device
  493 + * @devp: Return slave device
  494 + * @return 0 if found, -ve on error
  495 + */
  496 +int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
  497 + const char *drv_name, const char *dev_name,
  498 + struct udevice **busp, struct spi_slave **devp);
  499 +
  500 +/**
  501 + * spi_chip_select() - Get the chip select for a slave
  502 + *
  503 + * @return the chip select this slave is attached to
  504 + */
  505 +int spi_chip_select(struct udevice *slave);
  506 +
  507 +/**
  508 + * spi_bind_device() - bind a device to a bus's chip select
  509 + *
  510 + * This binds a new device to an given chip select (which must be unused).
  511 + *
  512 + * @bus: SPI bus to search
  513 + * @cs: Chip select to attach to
  514 + * @drv_name: Name of driver to attach to this chip select
  515 + * @dev_name: Name of the new device thus created
  516 + * @devp: Returns the newly bound device
  517 + */
  518 +int spi_bind_device(struct udevice *bus, int cs, const char *drv_name,
  519 + const char *dev_name, struct udevice **devp);
  520 +
  521 +/**
  522 + * spi_ofdata_to_platdata() - decode standard SPI platform data
  523 + *
  524 + * This decodes the speed and mode from a device tree node and puts it into
  525 + * the spi_slave structure.
  526 + *
  527 + * @blob: Device tree blob
  528 + * @node: Node offset to read from
  529 + * @spi: Place to put the decoded information
  530 + */
  531 +int spi_ofdata_to_platdata(const void *blob, int node, struct spi_slave *spi);
  532 +
  533 +/**
  534 + * spi_cs_info() - Check information on a chip select
  535 + *
  536 + * This checks a particular chip select on a bus to see if it has a device
  537 + * attached, or is even valid.
  538 + *
  539 + * @bus: The SPI bus
  540 + * @cs: The chip select (0..n-1)
  541 + * @info: Returns information about the chip select, if valid
  542 + * @return 0 if OK (and @info is set up), -ENODEV if the chip select
  543 + * is invalid, other -ve value on error
  544 + */
  545 +int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info);
  546 +
  547 +struct sandbox_state;
  548 +int sandbox_spi_get_emul(struct sandbox_state *state,
  549 + struct udevice *bus, struct udevice *slave,
  550 + struct udevice **emulp);
  551 +
  552 +/* Access the serial operations for a device */
  553 +#define spi_get_ops(dev) ((struct dm_spi_ops *)(dev)->driver->ops)
  554 +#endif /* CONFIG_DM_SPI */
307 555  
308 556 #endif /* _SPI_H_ */