Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later
0002  *
0003  * Copyright (C) 2005 David Brownell
0004  */
0005 
0006 #ifndef __LINUX_SPI_H
0007 #define __LINUX_SPI_H
0008 
0009 #include <linux/bits.h>
0010 #include <linux/device.h>
0011 #include <linux/mod_devicetable.h>
0012 #include <linux/slab.h>
0013 #include <linux/kthread.h>
0014 #include <linux/completion.h>
0015 #include <linux/scatterlist.h>
0016 #include <linux/gpio/consumer.h>
0017 
0018 #include <uapi/linux/spi/spi.h>
0019 #include <linux/acpi.h>
0020 #include <linux/u64_stats_sync.h>
0021 
0022 struct dma_chan;
0023 struct software_node;
0024 struct ptp_system_timestamp;
0025 struct spi_controller;
0026 struct spi_transfer;
0027 struct spi_controller_mem_ops;
0028 struct spi_controller_mem_caps;
0029 
0030 /*
0031  * INTERFACES between SPI master-side drivers and SPI slave protocol handlers,
0032  * and SPI infrastructure.
0033  */
0034 extern struct bus_type spi_bus_type;
0035 
0036 /**
0037  * struct spi_statistics - statistics for spi transfers
0038  * @syncp:         seqcount to protect members in this struct for per-cpu udate
0039  *                 on 32-bit systems
0040  *
0041  * @messages:      number of spi-messages handled
0042  * @transfers:     number of spi_transfers handled
0043  * @errors:        number of errors during spi_transfer
0044  * @timedout:      number of timeouts during spi_transfer
0045  *
0046  * @spi_sync:      number of times spi_sync is used
0047  * @spi_sync_immediate:
0048  *                 number of times spi_sync is executed immediately
0049  *                 in calling context without queuing and scheduling
0050  * @spi_async:     number of times spi_async is used
0051  *
0052  * @bytes:         number of bytes transferred to/from device
0053  * @bytes_tx:      number of bytes sent to device
0054  * @bytes_rx:      number of bytes received from device
0055  *
0056  * @transfer_bytes_histo:
0057  *                 transfer bytes histogramm
0058  *
0059  * @transfers_split_maxsize:
0060  *                 number of transfers that have been split because of
0061  *                 maxsize limit
0062  */
0063 struct spi_statistics {
0064     struct u64_stats_sync   syncp;
0065 
0066     u64_stats_t     messages;
0067     u64_stats_t     transfers;
0068     u64_stats_t     errors;
0069     u64_stats_t     timedout;
0070 
0071     u64_stats_t     spi_sync;
0072     u64_stats_t     spi_sync_immediate;
0073     u64_stats_t     spi_async;
0074 
0075     u64_stats_t     bytes;
0076     u64_stats_t     bytes_rx;
0077     u64_stats_t     bytes_tx;
0078 
0079 #define SPI_STATISTICS_HISTO_SIZE 17
0080     u64_stats_t transfer_bytes_histo[SPI_STATISTICS_HISTO_SIZE];
0081 
0082     u64_stats_t transfers_split_maxsize;
0083 };
0084 
0085 #define SPI_STATISTICS_ADD_TO_FIELD(pcpu_stats, field, count)       \
0086     do {                                \
0087         struct spi_statistics *__lstats;            \
0088         get_cpu();                      \
0089         __lstats = this_cpu_ptr(pcpu_stats);            \
0090         u64_stats_update_begin(&__lstats->syncp);       \
0091         u64_stats_add(&__lstats->field, count);         \
0092         u64_stats_update_end(&__lstats->syncp);         \
0093         put_cpu();                      \
0094     } while (0)
0095 
0096 #define SPI_STATISTICS_INCREMENT_FIELD(pcpu_stats, field)       \
0097     do {                                \
0098         struct spi_statistics *__lstats;            \
0099         get_cpu();                      \
0100         __lstats = this_cpu_ptr(pcpu_stats);            \
0101         u64_stats_update_begin(&__lstats->syncp);       \
0102         u64_stats_inc(&__lstats->field);            \
0103         u64_stats_update_end(&__lstats->syncp);         \
0104         put_cpu();                      \
0105     } while (0)
0106 
0107 /**
0108  * struct spi_delay - SPI delay information
0109  * @value: Value for the delay
0110  * @unit: Unit for the delay
0111  */
0112 struct spi_delay {
0113 #define SPI_DELAY_UNIT_USECS    0
0114 #define SPI_DELAY_UNIT_NSECS    1
0115 #define SPI_DELAY_UNIT_SCK  2
0116     u16 value;
0117     u8  unit;
0118 };
0119 
0120 extern int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer);
0121 extern int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer);
0122 
0123 /**
0124  * struct spi_device - Controller side proxy for an SPI slave device
0125  * @dev: Driver model representation of the device.
0126  * @controller: SPI controller used with the device.
0127  * @master: Copy of controller, for backwards compatibility.
0128  * @max_speed_hz: Maximum clock rate to be used with this chip
0129  *  (on this board); may be changed by the device's driver.
0130  *  The spi_transfer.speed_hz can override this for each transfer.
0131  * @chip_select: Chipselect, distinguishing chips handled by @controller.
0132  * @mode: The spi mode defines how data is clocked out and in.
0133  *  This may be changed by the device's driver.
0134  *  The "active low" default for chipselect mode can be overridden
0135  *  (by specifying SPI_CS_HIGH) as can the "MSB first" default for
0136  *  each word in a transfer (by specifying SPI_LSB_FIRST).
0137  * @bits_per_word: Data transfers involve one or more words; word sizes
0138  *  like eight or 12 bits are common.  In-memory wordsizes are
0139  *  powers of two bytes (e.g. 20 bit samples use 32 bits).
0140  *  This may be changed by the device's driver, or left at the
0141  *  default (0) indicating protocol words are eight bit bytes.
0142  *  The spi_transfer.bits_per_word can override this for each transfer.
0143  * @rt: Make the pump thread real time priority.
0144  * @irq: Negative, or the number passed to request_irq() to receive
0145  *  interrupts from this device.
0146  * @controller_state: Controller's runtime state
0147  * @controller_data: Board-specific definitions for controller, such as
0148  *  FIFO initialization parameters; from board_info.controller_data
0149  * @modalias: Name of the driver to use with this device, or an alias
0150  *  for that name.  This appears in the sysfs "modalias" attribute
0151  *  for driver coldplugging, and in uevents used for hotplugging
0152  * @driver_override: If the name of a driver is written to this attribute, then
0153  *  the device will bind to the named driver and only the named driver.
0154  *  Do not set directly, because core frees it; use driver_set_override() to
0155  *  set or clear it.
0156  * @cs_gpiod: gpio descriptor of the chipselect line (optional, NULL when
0157  *  not using a GPIO line)
0158  * @word_delay: delay to be inserted between consecutive
0159  *  words of a transfer
0160  * @cs_setup: delay to be introduced by the controller after CS is asserted
0161  * @cs_hold: delay to be introduced by the controller before CS is deasserted
0162  * @cs_inactive: delay to be introduced by the controller after CS is
0163  *  deasserted. If @cs_change_delay is used from @spi_transfer, then the
0164  *  two delays will be added up.
0165  * @pcpu_statistics: statistics for the spi_device
0166  *
0167  * A @spi_device is used to interchange data between an SPI slave
0168  * (usually a discrete chip) and CPU memory.
0169  *
0170  * In @dev, the platform_data is used to hold information about this
0171  * device that's meaningful to the device's protocol driver, but not
0172  * to its controller.  One example might be an identifier for a chip
0173  * variant with slightly different functionality; another might be
0174  * information about how this particular board wires the chip's pins.
0175  */
0176 struct spi_device {
0177     struct device       dev;
0178     struct spi_controller   *controller;
0179     struct spi_controller   *master;    /* Compatibility layer */
0180     u32         max_speed_hz;
0181     u8          chip_select;
0182     u8          bits_per_word;
0183     bool            rt;
0184 #define SPI_NO_TX   BIT(31)     /* No transmit wire */
0185 #define SPI_NO_RX   BIT(30)     /* No receive wire */
0186     /*
0187      * All bits defined above should be covered by SPI_MODE_KERNEL_MASK.
0188      * The SPI_MODE_KERNEL_MASK has the SPI_MODE_USER_MASK counterpart,
0189      * which is defined in 'include/uapi/linux/spi/spi.h'.
0190      * The bits defined here are from bit 31 downwards, while in
0191      * SPI_MODE_USER_MASK are from 0 upwards.
0192      * These bits must not overlap. A static assert check should make sure of that.
0193      * If adding extra bits, make sure to decrease the bit index below as well.
0194      */
0195 #define SPI_MODE_KERNEL_MASK    (~(BIT(30) - 1))
0196     u32         mode;
0197     int         irq;
0198     void            *controller_state;
0199     void            *controller_data;
0200     char            modalias[SPI_NAME_SIZE];
0201     const char      *driver_override;
0202     struct gpio_desc    *cs_gpiod;  /* Chip select gpio desc */
0203     struct spi_delay    word_delay; /* Inter-word delay */
0204     /* CS delays */
0205     struct spi_delay    cs_setup;
0206     struct spi_delay    cs_hold;
0207     struct spi_delay    cs_inactive;
0208 
0209     /* The statistics */
0210     struct spi_statistics __percpu  *pcpu_statistics;
0211 
0212     /*
0213      * likely need more hooks for more protocol options affecting how
0214      * the controller talks to each chip, like:
0215      *  - memory packing (12 bit samples into low bits, others zeroed)
0216      *  - priority
0217      *  - chipselect delays
0218      *  - ...
0219      */
0220 };
0221 
0222 /* Make sure that SPI_MODE_KERNEL_MASK & SPI_MODE_USER_MASK don't overlap */
0223 static_assert((SPI_MODE_KERNEL_MASK & SPI_MODE_USER_MASK) == 0,
0224           "SPI_MODE_USER_MASK & SPI_MODE_KERNEL_MASK must not overlap");
0225 
0226 static inline struct spi_device *to_spi_device(struct device *dev)
0227 {
0228     return dev ? container_of(dev, struct spi_device, dev) : NULL;
0229 }
0230 
0231 /* Most drivers won't need to care about device refcounting */
0232 static inline struct spi_device *spi_dev_get(struct spi_device *spi)
0233 {
0234     return (spi && get_device(&spi->dev)) ? spi : NULL;
0235 }
0236 
0237 static inline void spi_dev_put(struct spi_device *spi)
0238 {
0239     if (spi)
0240         put_device(&spi->dev);
0241 }
0242 
0243 /* ctldata is for the bus_controller driver's runtime state */
0244 static inline void *spi_get_ctldata(struct spi_device *spi)
0245 {
0246     return spi->controller_state;
0247 }
0248 
0249 static inline void spi_set_ctldata(struct spi_device *spi, void *state)
0250 {
0251     spi->controller_state = state;
0252 }
0253 
0254 /* Device driver data */
0255 
0256 static inline void spi_set_drvdata(struct spi_device *spi, void *data)
0257 {
0258     dev_set_drvdata(&spi->dev, data);
0259 }
0260 
0261 static inline void *spi_get_drvdata(struct spi_device *spi)
0262 {
0263     return dev_get_drvdata(&spi->dev);
0264 }
0265 
0266 struct spi_message;
0267 
0268 /**
0269  * struct spi_driver - Host side "protocol" driver
0270  * @id_table: List of SPI devices supported by this driver
0271  * @probe: Binds this driver to the spi device.  Drivers can verify
0272  *  that the device is actually present, and may need to configure
0273  *  characteristics (such as bits_per_word) which weren't needed for
0274  *  the initial configuration done during system setup.
0275  * @remove: Unbinds this driver from the spi device
0276  * @shutdown: Standard shutdown callback used during system state
0277  *  transitions such as powerdown/halt and kexec
0278  * @driver: SPI device drivers should initialize the name and owner
0279  *  field of this structure.
0280  *
0281  * This represents the kind of device driver that uses SPI messages to
0282  * interact with the hardware at the other end of a SPI link.  It's called
0283  * a "protocol" driver because it works through messages rather than talking
0284  * directly to SPI hardware (which is what the underlying SPI controller
0285  * driver does to pass those messages).  These protocols are defined in the
0286  * specification for the device(s) supported by the driver.
0287  *
0288  * As a rule, those device protocols represent the lowest level interface
0289  * supported by a driver, and it will support upper level interfaces too.
0290  * Examples of such upper levels include frameworks like MTD, networking,
0291  * MMC, RTC, filesystem character device nodes, and hardware monitoring.
0292  */
0293 struct spi_driver {
0294     const struct spi_device_id *id_table;
0295     int         (*probe)(struct spi_device *spi);
0296     void            (*remove)(struct spi_device *spi);
0297     void            (*shutdown)(struct spi_device *spi);
0298     struct device_driver    driver;
0299 };
0300 
0301 static inline struct spi_driver *to_spi_driver(struct device_driver *drv)
0302 {
0303     return drv ? container_of(drv, struct spi_driver, driver) : NULL;
0304 }
0305 
0306 extern int __spi_register_driver(struct module *owner, struct spi_driver *sdrv);
0307 
0308 /**
0309  * spi_unregister_driver - reverse effect of spi_register_driver
0310  * @sdrv: the driver to unregister
0311  * Context: can sleep
0312  */
0313 static inline void spi_unregister_driver(struct spi_driver *sdrv)
0314 {
0315     if (sdrv)
0316         driver_unregister(&sdrv->driver);
0317 }
0318 
0319 extern struct spi_device *spi_new_ancillary_device(struct spi_device *spi, u8 chip_select);
0320 
0321 /* Use a define to avoid include chaining to get THIS_MODULE */
0322 #define spi_register_driver(driver) \
0323     __spi_register_driver(THIS_MODULE, driver)
0324 
0325 /**
0326  * module_spi_driver() - Helper macro for registering a SPI driver
0327  * @__spi_driver: spi_driver struct
0328  *
0329  * Helper macro for SPI drivers which do not do anything special in module
0330  * init/exit. This eliminates a lot of boilerplate. Each module may only
0331  * use this macro once, and calling it replaces module_init() and module_exit()
0332  */
0333 #define module_spi_driver(__spi_driver) \
0334     module_driver(__spi_driver, spi_register_driver, \
0335             spi_unregister_driver)
0336 
0337 /**
0338  * struct spi_controller - interface to SPI master or slave controller
0339  * @dev: device interface to this driver
0340  * @list: link with the global spi_controller list
0341  * @bus_num: board-specific (and often SOC-specific) identifier for a
0342  *  given SPI controller.
0343  * @num_chipselect: chipselects are used to distinguish individual
0344  *  SPI slaves, and are numbered from zero to num_chipselects.
0345  *  each slave has a chipselect signal, but it's common that not
0346  *  every chipselect is connected to a slave.
0347  * @dma_alignment: SPI controller constraint on DMA buffers alignment.
0348  * @mode_bits: flags understood by this controller driver
0349  * @buswidth_override_bits: flags to override for this controller driver
0350  * @bits_per_word_mask: A mask indicating which values of bits_per_word are
0351  *  supported by the driver. Bit n indicates that a bits_per_word n+1 is
0352  *  supported. If set, the SPI core will reject any transfer with an
0353  *  unsupported bits_per_word. If not set, this value is simply ignored,
0354  *  and it's up to the individual driver to perform any validation.
0355  * @min_speed_hz: Lowest supported transfer speed
0356  * @max_speed_hz: Highest supported transfer speed
0357  * @flags: other constraints relevant to this driver
0358  * @slave: indicates that this is an SPI slave controller
0359  * @devm_allocated: whether the allocation of this struct is devres-managed
0360  * @max_transfer_size: function that returns the max transfer size for
0361  *  a &spi_device; may be %NULL, so the default %SIZE_MAX will be used.
0362  * @max_message_size: function that returns the max message size for
0363  *  a &spi_device; may be %NULL, so the default %SIZE_MAX will be used.
0364  * @io_mutex: mutex for physical bus access
0365  * @add_lock: mutex to avoid adding devices to the same chipselect
0366  * @bus_lock_spinlock: spinlock for SPI bus locking
0367  * @bus_lock_mutex: mutex for exclusion of multiple callers
0368  * @bus_lock_flag: indicates that the SPI bus is locked for exclusive use
0369  * @setup: updates the device mode and clocking records used by a
0370  *  device's SPI controller; protocol code may call this.  This
0371  *  must fail if an unrecognized or unsupported mode is requested.
0372  *  It's always safe to call this unless transfers are pending on
0373  *  the device whose settings are being modified.
0374  * @set_cs_timing: optional hook for SPI devices to request SPI master
0375  * controller for configuring specific CS setup time, hold time and inactive
0376  * delay interms of clock counts
0377  * @transfer: adds a message to the controller's transfer queue.
0378  * @cleanup: frees controller-specific state
0379  * @can_dma: determine whether this controller supports DMA
0380  * @dma_map_dev: device which can be used for DMA mapping
0381  * @queued: whether this controller is providing an internal message queue
0382  * @kworker: pointer to thread struct for message pump
0383  * @pump_messages: work struct for scheduling work to the message pump
0384  * @queue_lock: spinlock to syncronise access to message queue
0385  * @queue: message queue
0386  * @cur_msg: the currently in-flight message
0387  * @cur_msg_completion: a completion for the current in-flight message
0388  * @cur_msg_incomplete: Flag used internally to opportunistically skip
0389  *  the @cur_msg_completion. This flag is used to check if the driver has
0390  *  already called spi_finalize_current_message().
0391  * @cur_msg_need_completion: Flag used internally to opportunistically skip
0392  *  the @cur_msg_completion. This flag is used to signal the context that
0393  *  is running spi_finalize_current_message() that it needs to complete()
0394  * @cur_msg_mapped: message has been mapped for DMA
0395  * @last_cs: the last chip_select that is recorded by set_cs, -1 on non chip
0396  *           selected
0397  * @last_cs_mode_high: was (mode & SPI_CS_HIGH) true on the last call to set_cs.
0398  * @xfer_completion: used by core transfer_one_message()
0399  * @busy: message pump is busy
0400  * @running: message pump is running
0401  * @rt: whether this queue is set to run as a realtime task
0402  * @auto_runtime_pm: the core should ensure a runtime PM reference is held
0403  *                   while the hardware is prepared, using the parent
0404  *                   device for the spidev
0405  * @max_dma_len: Maximum length of a DMA transfer for the device.
0406  * @prepare_transfer_hardware: a message will soon arrive from the queue
0407  *  so the subsystem requests the driver to prepare the transfer hardware
0408  *  by issuing this call
0409  * @transfer_one_message: the subsystem calls the driver to transfer a single
0410  *  message while queuing transfers that arrive in the meantime. When the
0411  *  driver is finished with this message, it must call
0412  *  spi_finalize_current_message() so the subsystem can issue the next
0413  *  message
0414  * @unprepare_transfer_hardware: there are currently no more messages on the
0415  *  queue so the subsystem notifies the driver that it may relax the
0416  *  hardware by issuing this call
0417  *
0418  * @set_cs: set the logic level of the chip select line.  May be called
0419  *          from interrupt context.
0420  * @prepare_message: set up the controller to transfer a single message,
0421  *                   for example doing DMA mapping.  Called from threaded
0422  *                   context.
0423  * @transfer_one: transfer a single spi_transfer.
0424  *
0425  *                  - return 0 if the transfer is finished,
0426  *                  - return 1 if the transfer is still in progress. When
0427  *                    the driver is finished with this transfer it must
0428  *                    call spi_finalize_current_transfer() so the subsystem
0429  *                    can issue the next transfer. Note: transfer_one and
0430  *                    transfer_one_message are mutually exclusive; when both
0431  *                    are set, the generic subsystem does not call your
0432  *                    transfer_one callback.
0433  * @handle_err: the subsystem calls the driver to handle an error that occurs
0434  *      in the generic implementation of transfer_one_message().
0435  * @mem_ops: optimized/dedicated operations for interactions with SPI memory.
0436  *       This field is optional and should only be implemented if the
0437  *       controller has native support for memory like operations.
0438  * @mem_caps: controller capabilities for the handling of memory operations.
0439  * @unprepare_message: undo any work done by prepare_message().
0440  * @slave_abort: abort the ongoing transfer request on an SPI slave controller
0441  * @cs_gpiods: Array of GPIO descs to use as chip select lines; one per CS
0442  *  number. Any individual value may be NULL for CS lines that
0443  *  are not GPIOs (driven by the SPI controller itself).
0444  * @use_gpio_descriptors: Turns on the code in the SPI core to parse and grab
0445  *  GPIO descriptors. This will fill in @cs_gpiods and SPI devices will have
0446  *  the cs_gpiod assigned if a GPIO line is found for the chipselect.
0447  * @unused_native_cs: When cs_gpiods is used, spi_register_controller() will
0448  *  fill in this field with the first unused native CS, to be used by SPI
0449  *  controller drivers that need to drive a native CS when using GPIO CS.
0450  * @max_native_cs: When cs_gpiods is used, and this field is filled in,
0451  *  spi_register_controller() will validate all native CS (including the
0452  *  unused native CS) against this value.
0453  * @pcpu_statistics: statistics for the spi_controller
0454  * @dma_tx: DMA transmit channel
0455  * @dma_rx: DMA receive channel
0456  * @dummy_rx: dummy receive buffer for full-duplex devices
0457  * @dummy_tx: dummy transmit buffer for full-duplex devices
0458  * @fw_translate_cs: If the boot firmware uses different numbering scheme
0459  *  what Linux expects, this optional hook can be used to translate
0460  *  between the two.
0461  * @ptp_sts_supported: If the driver sets this to true, it must provide a
0462  *  time snapshot in @spi_transfer->ptp_sts as close as possible to the
0463  *  moment in time when @spi_transfer->ptp_sts_word_pre and
0464  *  @spi_transfer->ptp_sts_word_post were transmitted.
0465  *  If the driver does not set this, the SPI core takes the snapshot as
0466  *  close to the driver hand-over as possible.
0467  * @irq_flags: Interrupt enable state during PTP system timestamping
0468  * @fallback: fallback to pio if dma transfer return failure with
0469  *  SPI_TRANS_FAIL_NO_START.
0470  * @queue_empty: signal green light for opportunistically skipping the queue
0471  *  for spi_sync transfers.
0472  * @must_async: disable all fast paths in the core
0473  *
0474  * Each SPI controller can communicate with one or more @spi_device
0475  * children.  These make a small bus, sharing MOSI, MISO and SCK signals
0476  * but not chip select signals.  Each device may be configured to use a
0477  * different clock rate, since those shared signals are ignored unless
0478  * the chip is selected.
0479  *
0480  * The driver for an SPI controller manages access to those devices through
0481  * a queue of spi_message transactions, copying data between CPU memory and
0482  * an SPI slave device.  For each such message it queues, it calls the
0483  * message's completion function when the transaction completes.
0484  */
0485 struct spi_controller {
0486     struct device   dev;
0487 
0488     struct list_head list;
0489 
0490     /* Other than negative (== assign one dynamically), bus_num is fully
0491      * board-specific.  usually that simplifies to being SOC-specific.
0492      * example:  one SOC has three SPI controllers, numbered 0..2,
0493      * and one board's schematics might show it using SPI-2.  software
0494      * would normally use bus_num=2 for that controller.
0495      */
0496     s16         bus_num;
0497 
0498     /* chipselects will be integral to many controllers; some others
0499      * might use board-specific GPIOs.
0500      */
0501     u16         num_chipselect;
0502 
0503     /* Some SPI controllers pose alignment requirements on DMAable
0504      * buffers; let protocol drivers know about these requirements.
0505      */
0506     u16         dma_alignment;
0507 
0508     /* spi_device.mode flags understood by this controller driver */
0509     u32         mode_bits;
0510 
0511     /* spi_device.mode flags override flags for this controller */
0512     u32         buswidth_override_bits;
0513 
0514     /* Bitmask of supported bits_per_word for transfers */
0515     u32         bits_per_word_mask;
0516 #define SPI_BPW_MASK(bits) BIT((bits) - 1)
0517 #define SPI_BPW_RANGE_MASK(min, max) GENMASK((max) - 1, (min) - 1)
0518 
0519     /* Limits on transfer speed */
0520     u32         min_speed_hz;
0521     u32         max_speed_hz;
0522 
0523     /* Other constraints relevant to this driver */
0524     u16         flags;
0525 #define SPI_CONTROLLER_HALF_DUPLEX  BIT(0)  /* Can't do full duplex */
0526 #define SPI_CONTROLLER_NO_RX        BIT(1)  /* Can't do buffer read */
0527 #define SPI_CONTROLLER_NO_TX        BIT(2)  /* Can't do buffer write */
0528 #define SPI_CONTROLLER_MUST_RX      BIT(3)  /* Requires rx */
0529 #define SPI_CONTROLLER_MUST_TX      BIT(4)  /* Requires tx */
0530 
0531 #define SPI_MASTER_GPIO_SS      BIT(5)  /* GPIO CS must select slave */
0532 
0533     /* Flag indicating if the allocation of this struct is devres-managed */
0534     bool            devm_allocated;
0535 
0536     /* Flag indicating this is an SPI slave controller */
0537     bool            slave;
0538 
0539     /*
0540      * on some hardware transfer / message size may be constrained
0541      * the limit may depend on device transfer settings
0542      */
0543     size_t (*max_transfer_size)(struct spi_device *spi);
0544     size_t (*max_message_size)(struct spi_device *spi);
0545 
0546     /* I/O mutex */
0547     struct mutex        io_mutex;
0548 
0549     /* Used to avoid adding the same CS twice */
0550     struct mutex        add_lock;
0551 
0552     /* Lock and mutex for SPI bus locking */
0553     spinlock_t      bus_lock_spinlock;
0554     struct mutex        bus_lock_mutex;
0555 
0556     /* Flag indicating that the SPI bus is locked for exclusive use */
0557     bool            bus_lock_flag;
0558 
0559     /* Setup mode and clock, etc (spi driver may call many times).
0560      *
0561      * IMPORTANT:  this may be called when transfers to another
0562      * device are active.  DO NOT UPDATE SHARED REGISTERS in ways
0563      * which could break those transfers.
0564      */
0565     int         (*setup)(struct spi_device *spi);
0566 
0567     /*
0568      * set_cs_timing() method is for SPI controllers that supports
0569      * configuring CS timing.
0570      *
0571      * This hook allows SPI client drivers to request SPI controllers
0572      * to configure specific CS timing through spi_set_cs_timing() after
0573      * spi_setup().
0574      */
0575     int (*set_cs_timing)(struct spi_device *spi);
0576 
0577     /* Bidirectional bulk transfers
0578      *
0579      * + The transfer() method may not sleep; its main role is
0580      *   just to add the message to the queue.
0581      * + For now there's no remove-from-queue operation, or
0582      *   any other request management
0583      * + To a given spi_device, message queueing is pure fifo
0584      *
0585      * + The controller's main job is to process its message queue,
0586      *   selecting a chip (for masters), then transferring data
0587      * + If there are multiple spi_device children, the i/o queue
0588      *   arbitration algorithm is unspecified (round robin, fifo,
0589      *   priority, reservations, preemption, etc)
0590      *
0591      * + Chipselect stays active during the entire message
0592      *   (unless modified by spi_transfer.cs_change != 0).
0593      * + The message transfers use clock and SPI mode parameters
0594      *   previously established by setup() for this device
0595      */
0596     int         (*transfer)(struct spi_device *spi,
0597                         struct spi_message *mesg);
0598 
0599     /* Called on release() to free memory provided by spi_controller */
0600     void            (*cleanup)(struct spi_device *spi);
0601 
0602     /*
0603      * Used to enable core support for DMA handling, if can_dma()
0604      * exists and returns true then the transfer will be mapped
0605      * prior to transfer_one() being called.  The driver should
0606      * not modify or store xfer and dma_tx and dma_rx must be set
0607      * while the device is prepared.
0608      */
0609     bool            (*can_dma)(struct spi_controller *ctlr,
0610                        struct spi_device *spi,
0611                        struct spi_transfer *xfer);
0612     struct device *dma_map_dev;
0613 
0614     /*
0615      * These hooks are for drivers that want to use the generic
0616      * controller transfer queueing mechanism. If these are used, the
0617      * transfer() function above must NOT be specified by the driver.
0618      * Over time we expect SPI drivers to be phased over to this API.
0619      */
0620     bool                queued;
0621     struct kthread_worker       *kworker;
0622     struct kthread_work     pump_messages;
0623     spinlock_t          queue_lock;
0624     struct list_head        queue;
0625     struct spi_message      *cur_msg;
0626     struct completion               cur_msg_completion;
0627     bool                cur_msg_incomplete;
0628     bool                cur_msg_need_completion;
0629     bool                busy;
0630     bool                running;
0631     bool                rt;
0632     bool                auto_runtime_pm;
0633     bool                cur_msg_mapped;
0634     char                last_cs;
0635     bool                last_cs_mode_high;
0636     bool                            fallback;
0637     struct completion               xfer_completion;
0638     size_t              max_dma_len;
0639 
0640     int (*prepare_transfer_hardware)(struct spi_controller *ctlr);
0641     int (*transfer_one_message)(struct spi_controller *ctlr,
0642                     struct spi_message *mesg);
0643     int (*unprepare_transfer_hardware)(struct spi_controller *ctlr);
0644     int (*prepare_message)(struct spi_controller *ctlr,
0645                    struct spi_message *message);
0646     int (*unprepare_message)(struct spi_controller *ctlr,
0647                  struct spi_message *message);
0648     int (*slave_abort)(struct spi_controller *ctlr);
0649 
0650     /*
0651      * These hooks are for drivers that use a generic implementation
0652      * of transfer_one_message() provided by the core.
0653      */
0654     void (*set_cs)(struct spi_device *spi, bool enable);
0655     int (*transfer_one)(struct spi_controller *ctlr, struct spi_device *spi,
0656                 struct spi_transfer *transfer);
0657     void (*handle_err)(struct spi_controller *ctlr,
0658                struct spi_message *message);
0659 
0660     /* Optimized handlers for SPI memory-like operations. */
0661     const struct spi_controller_mem_ops *mem_ops;
0662     const struct spi_controller_mem_caps *mem_caps;
0663 
0664     /* gpio chip select */
0665     struct gpio_desc    **cs_gpiods;
0666     bool            use_gpio_descriptors;
0667     s8          unused_native_cs;
0668     s8          max_native_cs;
0669 
0670     /* Statistics */
0671     struct spi_statistics __percpu  *pcpu_statistics;
0672 
0673     /* DMA channels for use with core dmaengine helpers */
0674     struct dma_chan     *dma_tx;
0675     struct dma_chan     *dma_rx;
0676 
0677     /* Dummy data for full duplex devices */
0678     void            *dummy_rx;
0679     void            *dummy_tx;
0680 
0681     int (*fw_translate_cs)(struct spi_controller *ctlr, unsigned cs);
0682 
0683     /*
0684      * Driver sets this field to indicate it is able to snapshot SPI
0685      * transfers (needed e.g. for reading the time of POSIX clocks)
0686      */
0687     bool            ptp_sts_supported;
0688 
0689     /* Interrupt enable state during PTP system timestamping */
0690     unsigned long       irq_flags;
0691 
0692     /* Flag for enabling opportunistic skipping of the queue in spi_sync */
0693     bool            queue_empty;
0694     bool            must_async;
0695 };
0696 
0697 static inline void *spi_controller_get_devdata(struct spi_controller *ctlr)
0698 {
0699     return dev_get_drvdata(&ctlr->dev);
0700 }
0701 
0702 static inline void spi_controller_set_devdata(struct spi_controller *ctlr,
0703                           void *data)
0704 {
0705     dev_set_drvdata(&ctlr->dev, data);
0706 }
0707 
0708 static inline struct spi_controller *spi_controller_get(struct spi_controller *ctlr)
0709 {
0710     if (!ctlr || !get_device(&ctlr->dev))
0711         return NULL;
0712     return ctlr;
0713 }
0714 
0715 static inline void spi_controller_put(struct spi_controller *ctlr)
0716 {
0717     if (ctlr)
0718         put_device(&ctlr->dev);
0719 }
0720 
0721 static inline bool spi_controller_is_slave(struct spi_controller *ctlr)
0722 {
0723     return IS_ENABLED(CONFIG_SPI_SLAVE) && ctlr->slave;
0724 }
0725 
0726 /* PM calls that need to be issued by the driver */
0727 extern int spi_controller_suspend(struct spi_controller *ctlr);
0728 extern int spi_controller_resume(struct spi_controller *ctlr);
0729 
0730 /* Calls the driver make to interact with the message queue */
0731 extern struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr);
0732 extern void spi_finalize_current_message(struct spi_controller *ctlr);
0733 extern void spi_finalize_current_transfer(struct spi_controller *ctlr);
0734 
0735 /* Helper calls for driver to timestamp transfer */
0736 void spi_take_timestamp_pre(struct spi_controller *ctlr,
0737                 struct spi_transfer *xfer,
0738                 size_t progress, bool irqs_off);
0739 void spi_take_timestamp_post(struct spi_controller *ctlr,
0740                  struct spi_transfer *xfer,
0741                  size_t progress, bool irqs_off);
0742 
0743 /* The spi driver core manages memory for the spi_controller classdev */
0744 extern struct spi_controller *__spi_alloc_controller(struct device *host,
0745                         unsigned int size, bool slave);
0746 
0747 static inline struct spi_controller *spi_alloc_master(struct device *host,
0748                               unsigned int size)
0749 {
0750     return __spi_alloc_controller(host, size, false);
0751 }
0752 
0753 static inline struct spi_controller *spi_alloc_slave(struct device *host,
0754                              unsigned int size)
0755 {
0756     if (!IS_ENABLED(CONFIG_SPI_SLAVE))
0757         return NULL;
0758 
0759     return __spi_alloc_controller(host, size, true);
0760 }
0761 
0762 struct spi_controller *__devm_spi_alloc_controller(struct device *dev,
0763                            unsigned int size,
0764                            bool slave);
0765 
0766 static inline struct spi_controller *devm_spi_alloc_master(struct device *dev,
0767                                unsigned int size)
0768 {
0769     return __devm_spi_alloc_controller(dev, size, false);
0770 }
0771 
0772 static inline struct spi_controller *devm_spi_alloc_slave(struct device *dev,
0773                               unsigned int size)
0774 {
0775     if (!IS_ENABLED(CONFIG_SPI_SLAVE))
0776         return NULL;
0777 
0778     return __devm_spi_alloc_controller(dev, size, true);
0779 }
0780 
0781 extern int spi_register_controller(struct spi_controller *ctlr);
0782 extern int devm_spi_register_controller(struct device *dev,
0783                     struct spi_controller *ctlr);
0784 extern void spi_unregister_controller(struct spi_controller *ctlr);
0785 
0786 #if IS_ENABLED(CONFIG_ACPI)
0787 extern struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr,
0788                         struct acpi_device *adev,
0789                         int index);
0790 int acpi_spi_count_resources(struct acpi_device *adev);
0791 #endif
0792 
0793 /*
0794  * SPI resource management while processing a SPI message
0795  */
0796 
0797 typedef void (*spi_res_release_t)(struct spi_controller *ctlr,
0798                   struct spi_message *msg,
0799                   void *res);
0800 
0801 /**
0802  * struct spi_res - spi resource management structure
0803  * @entry:   list entry
0804  * @release: release code called prior to freeing this resource
0805  * @data:    extra data allocated for the specific use-case
0806  *
0807  * this is based on ideas from devres, but focused on life-cycle
0808  * management during spi_message processing
0809  */
0810 struct spi_res {
0811     struct list_head        entry;
0812     spi_res_release_t       release;
0813     unsigned long long      data[]; /* Guarantee ull alignment */
0814 };
0815 
0816 /*---------------------------------------------------------------------------*/
0817 
0818 /*
0819  * I/O INTERFACE between SPI controller and protocol drivers
0820  *
0821  * Protocol drivers use a queue of spi_messages, each transferring data
0822  * between the controller and memory buffers.
0823  *
0824  * The spi_messages themselves consist of a series of read+write transfer
0825  * segments.  Those segments always read the same number of bits as they
0826  * write; but one or the other is easily ignored by passing a null buffer
0827  * pointer.  (This is unlike most types of I/O API, because SPI hardware
0828  * is full duplex.)
0829  *
0830  * NOTE:  Allocation of spi_transfer and spi_message memory is entirely
0831  * up to the protocol driver, which guarantees the integrity of both (as
0832  * well as the data buffers) for as long as the message is queued.
0833  */
0834 
0835 /**
0836  * struct spi_transfer - a read/write buffer pair
0837  * @tx_buf: data to be written (dma-safe memory), or NULL
0838  * @rx_buf: data to be read (dma-safe memory), or NULL
0839  * @tx_dma: DMA address of tx_buf, if @spi_message.is_dma_mapped
0840  * @rx_dma: DMA address of rx_buf, if @spi_message.is_dma_mapped
0841  * @tx_nbits: number of bits used for writing. If 0 the default
0842  *      (SPI_NBITS_SINGLE) is used.
0843  * @rx_nbits: number of bits used for reading. If 0 the default
0844  *      (SPI_NBITS_SINGLE) is used.
0845  * @len: size of rx and tx buffers (in bytes)
0846  * @speed_hz: Select a speed other than the device default for this
0847  *      transfer. If 0 the default (from @spi_device) is used.
0848  * @bits_per_word: select a bits_per_word other than the device default
0849  *      for this transfer. If 0 the default (from @spi_device) is used.
0850  * @dummy_data: indicates transfer is dummy bytes transfer.
0851  * @cs_change: affects chipselect after this transfer completes
0852  * @cs_change_delay: delay between cs deassert and assert when
0853  *      @cs_change is set and @spi_transfer is not the last in @spi_message
0854  * @delay: delay to be introduced after this transfer before
0855  *  (optionally) changing the chipselect status, then starting
0856  *  the next transfer or completing this @spi_message.
0857  * @word_delay: inter word delay to be introduced after each word size
0858  *  (set by bits_per_word) transmission.
0859  * @effective_speed_hz: the effective SCK-speed that was used to
0860  *      transfer this transfer. Set to 0 if the spi bus driver does
0861  *      not support it.
0862  * @transfer_list: transfers are sequenced through @spi_message.transfers
0863  * @tx_sg: Scatterlist for transmit, currently not for client use
0864  * @rx_sg: Scatterlist for receive, currently not for client use
0865  * @ptp_sts_word_pre: The word (subject to bits_per_word semantics) offset
0866  *  within @tx_buf for which the SPI device is requesting that the time
0867  *  snapshot for this transfer begins. Upon completing the SPI transfer,
0868  *  this value may have changed compared to what was requested, depending
0869  *  on the available snapshotting resolution (DMA transfer,
0870  *  @ptp_sts_supported is false, etc).
0871  * @ptp_sts_word_post: See @ptp_sts_word_post. The two can be equal (meaning
0872  *  that a single byte should be snapshotted).
0873  *  If the core takes care of the timestamp (if @ptp_sts_supported is false
0874  *  for this controller), it will set @ptp_sts_word_pre to 0, and
0875  *  @ptp_sts_word_post to the length of the transfer. This is done
0876  *  purposefully (instead of setting to spi_transfer->len - 1) to denote
0877  *  that a transfer-level snapshot taken from within the driver may still
0878  *  be of higher quality.
0879  * @ptp_sts: Pointer to a memory location held by the SPI slave device where a
0880  *  PTP system timestamp structure may lie. If drivers use PIO or their
0881  *  hardware has some sort of assist for retrieving exact transfer timing,
0882  *  they can (and should) assert @ptp_sts_supported and populate this
0883  *  structure using the ptp_read_system_*ts helper functions.
0884  *  The timestamp must represent the time at which the SPI slave device has
0885  *  processed the word, i.e. the "pre" timestamp should be taken before
0886  *  transmitting the "pre" word, and the "post" timestamp after receiving
0887  *  transmit confirmation from the controller for the "post" word.
0888  * @timestamped: true if the transfer has been timestamped
0889  * @error: Error status logged by spi controller driver.
0890  *
0891  * SPI transfers always write the same number of bytes as they read.
0892  * Protocol drivers should always provide @rx_buf and/or @tx_buf.
0893  * In some cases, they may also want to provide DMA addresses for
0894  * the data being transferred; that may reduce overhead, when the
0895  * underlying driver uses dma.
0896  *
0897  * If the transmit buffer is null, zeroes will be shifted out
0898  * while filling @rx_buf.  If the receive buffer is null, the data
0899  * shifted in will be discarded.  Only "len" bytes shift out (or in).
0900  * It's an error to try to shift out a partial word.  (For example, by
0901  * shifting out three bytes with word size of sixteen or twenty bits;
0902  * the former uses two bytes per word, the latter uses four bytes.)
0903  *
0904  * In-memory data values are always in native CPU byte order, translated
0905  * from the wire byte order (big-endian except with SPI_LSB_FIRST).  So
0906  * for example when bits_per_word is sixteen, buffers are 2N bytes long
0907  * (@len = 2N) and hold N sixteen bit words in CPU byte order.
0908  *
0909  * When the word size of the SPI transfer is not a power-of-two multiple
0910  * of eight bits, those in-memory words include extra bits.  In-memory
0911  * words are always seen by protocol drivers as right-justified, so the
0912  * undefined (rx) or unused (tx) bits are always the most significant bits.
0913  *
0914  * All SPI transfers start with the relevant chipselect active.  Normally
0915  * it stays selected until after the last transfer in a message.  Drivers
0916  * can affect the chipselect signal using cs_change.
0917  *
0918  * (i) If the transfer isn't the last one in the message, this flag is
0919  * used to make the chipselect briefly go inactive in the middle of the
0920  * message.  Toggling chipselect in this way may be needed to terminate
0921  * a chip command, letting a single spi_message perform all of group of
0922  * chip transactions together.
0923  *
0924  * (ii) When the transfer is the last one in the message, the chip may
0925  * stay selected until the next transfer.  On multi-device SPI busses
0926  * with nothing blocking messages going to other devices, this is just
0927  * a performance hint; starting a message to another device deselects
0928  * this one.  But in other cases, this can be used to ensure correctness.
0929  * Some devices need protocol transactions to be built from a series of
0930  * spi_message submissions, where the content of one message is determined
0931  * by the results of previous messages and where the whole transaction
0932  * ends when the chipselect goes intactive.
0933  *
0934  * When SPI can transfer in 1x,2x or 4x. It can get this transfer information
0935  * from device through @tx_nbits and @rx_nbits. In Bi-direction, these
0936  * two should both be set. User can set transfer mode with SPI_NBITS_SINGLE(1x)
0937  * SPI_NBITS_DUAL(2x) and SPI_NBITS_QUAD(4x) to support these three transfer.
0938  *
0939  * The code that submits an spi_message (and its spi_transfers)
0940  * to the lower layers is responsible for managing its memory.
0941  * Zero-initialize every field you don't set up explicitly, to
0942  * insulate against future API updates.  After you submit a message
0943  * and its transfers, ignore them until its completion callback.
0944  */
0945 struct spi_transfer {
0946     /* It's ok if tx_buf == rx_buf (right?)
0947      * for MicroWire, one buffer must be null
0948      * buffers must work with dma_*map_single() calls, unless
0949      *   spi_message.is_dma_mapped reports a pre-existing mapping
0950      */
0951     const void  *tx_buf;
0952     void        *rx_buf;
0953     unsigned    len;
0954 
0955     dma_addr_t  tx_dma;
0956     dma_addr_t  rx_dma;
0957     struct sg_table tx_sg;
0958     struct sg_table rx_sg;
0959 
0960     unsigned    dummy_data:1;
0961     unsigned    cs_change:1;
0962     unsigned    tx_nbits:3;
0963     unsigned    rx_nbits:3;
0964 #define SPI_NBITS_SINGLE    0x01 /* 1bit transfer */
0965 #define SPI_NBITS_DUAL      0x02 /* 2bits transfer */
0966 #define SPI_NBITS_QUAD      0x04 /* 4bits transfer */
0967     u8      bits_per_word;
0968     struct spi_delay    delay;
0969     struct spi_delay    cs_change_delay;
0970     struct spi_delay    word_delay;
0971     u32     speed_hz;
0972 
0973     u32     effective_speed_hz;
0974 
0975     unsigned int    ptp_sts_word_pre;
0976     unsigned int    ptp_sts_word_post;
0977 
0978     struct ptp_system_timestamp *ptp_sts;
0979 
0980     bool        timestamped;
0981 
0982     struct list_head transfer_list;
0983 
0984 #define SPI_TRANS_FAIL_NO_START BIT(0)
0985     u16     error;
0986 };
0987 
0988 /**
0989  * struct spi_message - one multi-segment SPI transaction
0990  * @transfers: list of transfer segments in this transaction
0991  * @spi: SPI device to which the transaction is queued
0992  * @is_dma_mapped: if true, the caller provided both dma and cpu virtual
0993  *  addresses for each transfer buffer
0994  * @complete: called to report transaction completions
0995  * @context: the argument to complete() when it's called
0996  * @frame_length: the total number of bytes in the message
0997  * @actual_length: the total number of bytes that were transferred in all
0998  *  successful segments
0999  * @status: zero for success, else negative errno
1000  * @queue: for use by whichever driver currently owns the message
1001  * @state: for use by whichever driver currently owns the message
1002  * @resources: for resource management when the spi message is processed
1003  * @prepared: spi_prepare_message was called for the this message
1004  *
1005  * A @spi_message is used to execute an atomic sequence of data transfers,
1006  * each represented by a struct spi_transfer.  The sequence is "atomic"
1007  * in the sense that no other spi_message may use that SPI bus until that
1008  * sequence completes.  On some systems, many such sequences can execute as
1009  * a single programmed DMA transfer.  On all systems, these messages are
1010  * queued, and might complete after transactions to other devices.  Messages
1011  * sent to a given spi_device are always executed in FIFO order.
1012  *
1013  * The code that submits an spi_message (and its spi_transfers)
1014  * to the lower layers is responsible for managing its memory.
1015  * Zero-initialize every field you don't set up explicitly, to
1016  * insulate against future API updates.  After you submit a message
1017  * and its transfers, ignore them until its completion callback.
1018  */
1019 struct spi_message {
1020     struct list_head    transfers;
1021 
1022     struct spi_device   *spi;
1023 
1024     unsigned        is_dma_mapped:1;
1025 
1026     /* REVISIT:  we might want a flag affecting the behavior of the
1027      * last transfer ... allowing things like "read 16 bit length L"
1028      * immediately followed by "read L bytes".  Basically imposing
1029      * a specific message scheduling algorithm.
1030      *
1031      * Some controller drivers (message-at-a-time queue processing)
1032      * could provide that as their default scheduling algorithm.  But
1033      * others (with multi-message pipelines) could need a flag to
1034      * tell them about such special cases.
1035      */
1036 
1037     /* Completion is reported through a callback */
1038     void            (*complete)(void *context);
1039     void            *context;
1040     unsigned        frame_length;
1041     unsigned        actual_length;
1042     int         status;
1043 
1044     /* For optional use by whatever driver currently owns the
1045      * spi_message ...  between calls to spi_async and then later
1046      * complete(), that's the spi_controller controller driver.
1047      */
1048     struct list_head    queue;
1049     void            *state;
1050 
1051     /* List of spi_res reources when the spi message is processed */
1052     struct list_head        resources;
1053 
1054     /* spi_prepare_message() was called for this message */
1055     bool            prepared;
1056 };
1057 
1058 static inline void spi_message_init_no_memset(struct spi_message *m)
1059 {
1060     INIT_LIST_HEAD(&m->transfers);
1061     INIT_LIST_HEAD(&m->resources);
1062 }
1063 
1064 static inline void spi_message_init(struct spi_message *m)
1065 {
1066     memset(m, 0, sizeof *m);
1067     spi_message_init_no_memset(m);
1068 }
1069 
1070 static inline void
1071 spi_message_add_tail(struct spi_transfer *t, struct spi_message *m)
1072 {
1073     list_add_tail(&t->transfer_list, &m->transfers);
1074 }
1075 
1076 static inline void
1077 spi_transfer_del(struct spi_transfer *t)
1078 {
1079     list_del(&t->transfer_list);
1080 }
1081 
1082 static inline int
1083 spi_transfer_delay_exec(struct spi_transfer *t)
1084 {
1085     return spi_delay_exec(&t->delay, t);
1086 }
1087 
1088 /**
1089  * spi_message_init_with_transfers - Initialize spi_message and append transfers
1090  * @m: spi_message to be initialized
1091  * @xfers: An array of spi transfers
1092  * @num_xfers: Number of items in the xfer array
1093  *
1094  * This function initializes the given spi_message and adds each spi_transfer in
1095  * the given array to the message.
1096  */
1097 static inline void
1098 spi_message_init_with_transfers(struct spi_message *m,
1099 struct spi_transfer *xfers, unsigned int num_xfers)
1100 {
1101     unsigned int i;
1102 
1103     spi_message_init(m);
1104     for (i = 0; i < num_xfers; ++i)
1105         spi_message_add_tail(&xfers[i], m);
1106 }
1107 
1108 /* It's fine to embed message and transaction structures in other data
1109  * structures so long as you don't free them while they're in use.
1110  */
1111 
1112 static inline struct spi_message *spi_message_alloc(unsigned ntrans, gfp_t flags)
1113 {
1114     struct spi_message *m;
1115 
1116     m = kzalloc(sizeof(struct spi_message)
1117             + ntrans * sizeof(struct spi_transfer),
1118             flags);
1119     if (m) {
1120         unsigned i;
1121         struct spi_transfer *t = (struct spi_transfer *)(m + 1);
1122 
1123         spi_message_init_no_memset(m);
1124         for (i = 0; i < ntrans; i++, t++)
1125             spi_message_add_tail(t, m);
1126     }
1127     return m;
1128 }
1129 
1130 static inline void spi_message_free(struct spi_message *m)
1131 {
1132     kfree(m);
1133 }
1134 
1135 extern int spi_setup(struct spi_device *spi);
1136 extern int spi_async(struct spi_device *spi, struct spi_message *message);
1137 extern int spi_slave_abort(struct spi_device *spi);
1138 
1139 static inline size_t
1140 spi_max_message_size(struct spi_device *spi)
1141 {
1142     struct spi_controller *ctlr = spi->controller;
1143 
1144     if (!ctlr->max_message_size)
1145         return SIZE_MAX;
1146     return ctlr->max_message_size(spi);
1147 }
1148 
1149 static inline size_t
1150 spi_max_transfer_size(struct spi_device *spi)
1151 {
1152     struct spi_controller *ctlr = spi->controller;
1153     size_t tr_max = SIZE_MAX;
1154     size_t msg_max = spi_max_message_size(spi);
1155 
1156     if (ctlr->max_transfer_size)
1157         tr_max = ctlr->max_transfer_size(spi);
1158 
1159     /* Transfer size limit must not be greater than message size limit */
1160     return min(tr_max, msg_max);
1161 }
1162 
1163 /**
1164  * spi_is_bpw_supported - Check if bits per word is supported
1165  * @spi: SPI device
1166  * @bpw: Bits per word
1167  *
1168  * This function checks to see if the SPI controller supports @bpw.
1169  *
1170  * Returns:
1171  * True if @bpw is supported, false otherwise.
1172  */
1173 static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw)
1174 {
1175     u32 bpw_mask = spi->master->bits_per_word_mask;
1176 
1177     if (bpw == 8 || (bpw <= 32 && bpw_mask & SPI_BPW_MASK(bpw)))
1178         return true;
1179 
1180     return false;
1181 }
1182 
1183 /*---------------------------------------------------------------------------*/
1184 
1185 /* SPI transfer replacement methods which make use of spi_res */
1186 
1187 struct spi_replaced_transfers;
1188 typedef void (*spi_replaced_release_t)(struct spi_controller *ctlr,
1189                        struct spi_message *msg,
1190                        struct spi_replaced_transfers *res);
1191 /**
1192  * struct spi_replaced_transfers - structure describing the spi_transfer
1193  *                                 replacements that have occurred
1194  *                                 so that they can get reverted
1195  * @release:            some extra release code to get executed prior to
1196  *                      relasing this structure
1197  * @extradata:          pointer to some extra data if requested or NULL
1198  * @replaced_transfers: transfers that have been replaced and which need
1199  *                      to get restored
1200  * @replaced_after:     the transfer after which the @replaced_transfers
1201  *                      are to get re-inserted
1202  * @inserted:           number of transfers inserted
1203  * @inserted_transfers: array of spi_transfers of array-size @inserted,
1204  *                      that have been replacing replaced_transfers
1205  *
1206  * note: that @extradata will point to @inserted_transfers[@inserted]
1207  * if some extra allocation is requested, so alignment will be the same
1208  * as for spi_transfers
1209  */
1210 struct spi_replaced_transfers {
1211     spi_replaced_release_t release;
1212     void *extradata;
1213     struct list_head replaced_transfers;
1214     struct list_head *replaced_after;
1215     size_t inserted;
1216     struct spi_transfer inserted_transfers[];
1217 };
1218 
1219 /*---------------------------------------------------------------------------*/
1220 
1221 /* SPI transfer transformation methods */
1222 
1223 extern int spi_split_transfers_maxsize(struct spi_controller *ctlr,
1224                        struct spi_message *msg,
1225                        size_t maxsize,
1226                        gfp_t gfp);
1227 
1228 /*---------------------------------------------------------------------------*/
1229 
1230 /* All these synchronous SPI transfer routines are utilities layered
1231  * over the core async transfer primitive.  Here, "synchronous" means
1232  * they will sleep uninterruptibly until the async transfer completes.
1233  */
1234 
1235 extern int spi_sync(struct spi_device *spi, struct spi_message *message);
1236 extern int spi_sync_locked(struct spi_device *spi, struct spi_message *message);
1237 extern int spi_bus_lock(struct spi_controller *ctlr);
1238 extern int spi_bus_unlock(struct spi_controller *ctlr);
1239 
1240 /**
1241  * spi_sync_transfer - synchronous SPI data transfer
1242  * @spi: device with which data will be exchanged
1243  * @xfers: An array of spi_transfers
1244  * @num_xfers: Number of items in the xfer array
1245  * Context: can sleep
1246  *
1247  * Does a synchronous SPI data transfer of the given spi_transfer array.
1248  *
1249  * For more specific semantics see spi_sync().
1250  *
1251  * Return: zero on success, else a negative error code.
1252  */
1253 static inline int
1254 spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
1255     unsigned int num_xfers)
1256 {
1257     struct spi_message msg;
1258 
1259     spi_message_init_with_transfers(&msg, xfers, num_xfers);
1260 
1261     return spi_sync(spi, &msg);
1262 }
1263 
1264 /**
1265  * spi_write - SPI synchronous write
1266  * @spi: device to which data will be written
1267  * @buf: data buffer
1268  * @len: data buffer size
1269  * Context: can sleep
1270  *
1271  * This function writes the buffer @buf.
1272  * Callable only from contexts that can sleep.
1273  *
1274  * Return: zero on success, else a negative error code.
1275  */
1276 static inline int
1277 spi_write(struct spi_device *spi, const void *buf, size_t len)
1278 {
1279     struct spi_transfer t = {
1280             .tx_buf     = buf,
1281             .len        = len,
1282         };
1283 
1284     return spi_sync_transfer(spi, &t, 1);
1285 }
1286 
1287 /**
1288  * spi_read - SPI synchronous read
1289  * @spi: device from which data will be read
1290  * @buf: data buffer
1291  * @len: data buffer size
1292  * Context: can sleep
1293  *
1294  * This function reads the buffer @buf.
1295  * Callable only from contexts that can sleep.
1296  *
1297  * Return: zero on success, else a negative error code.
1298  */
1299 static inline int
1300 spi_read(struct spi_device *spi, void *buf, size_t len)
1301 {
1302     struct spi_transfer t = {
1303             .rx_buf     = buf,
1304             .len        = len,
1305         };
1306 
1307     return spi_sync_transfer(spi, &t, 1);
1308 }
1309 
1310 /* This copies txbuf and rxbuf data; for small transfers only! */
1311 extern int spi_write_then_read(struct spi_device *spi,
1312         const void *txbuf, unsigned n_tx,
1313         void *rxbuf, unsigned n_rx);
1314 
1315 /**
1316  * spi_w8r8 - SPI synchronous 8 bit write followed by 8 bit read
1317  * @spi: device with which data will be exchanged
1318  * @cmd: command to be written before data is read back
1319  * Context: can sleep
1320  *
1321  * Callable only from contexts that can sleep.
1322  *
1323  * Return: the (unsigned) eight bit number returned by the
1324  * device, or else a negative error code.
1325  */
1326 static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd)
1327 {
1328     ssize_t         status;
1329     u8          result;
1330 
1331     status = spi_write_then_read(spi, &cmd, 1, &result, 1);
1332 
1333     /* Return negative errno or unsigned value */
1334     return (status < 0) ? status : result;
1335 }
1336 
1337 /**
1338  * spi_w8r16 - SPI synchronous 8 bit write followed by 16 bit read
1339  * @spi: device with which data will be exchanged
1340  * @cmd: command to be written before data is read back
1341  * Context: can sleep
1342  *
1343  * The number is returned in wire-order, which is at least sometimes
1344  * big-endian.
1345  *
1346  * Callable only from contexts that can sleep.
1347  *
1348  * Return: the (unsigned) sixteen bit number returned by the
1349  * device, or else a negative error code.
1350  */
1351 static inline ssize_t spi_w8r16(struct spi_device *spi, u8 cmd)
1352 {
1353     ssize_t         status;
1354     u16         result;
1355 
1356     status = spi_write_then_read(spi, &cmd, 1, &result, 2);
1357 
1358     /* Return negative errno or unsigned value */
1359     return (status < 0) ? status : result;
1360 }
1361 
1362 /**
1363  * spi_w8r16be - SPI synchronous 8 bit write followed by 16 bit big-endian read
1364  * @spi: device with which data will be exchanged
1365  * @cmd: command to be written before data is read back
1366  * Context: can sleep
1367  *
1368  * This function is similar to spi_w8r16, with the exception that it will
1369  * convert the read 16 bit data word from big-endian to native endianness.
1370  *
1371  * Callable only from contexts that can sleep.
1372  *
1373  * Return: the (unsigned) sixteen bit number returned by the device in cpu
1374  * endianness, or else a negative error code.
1375  */
1376 static inline ssize_t spi_w8r16be(struct spi_device *spi, u8 cmd)
1377 
1378 {
1379     ssize_t status;
1380     __be16 result;
1381 
1382     status = spi_write_then_read(spi, &cmd, 1, &result, 2);
1383     if (status < 0)
1384         return status;
1385 
1386     return be16_to_cpu(result);
1387 }
1388 
1389 /*---------------------------------------------------------------------------*/
1390 
1391 /*
1392  * INTERFACE between board init code and SPI infrastructure.
1393  *
1394  * No SPI driver ever sees these SPI device table segments, but
1395  * it's how the SPI core (or adapters that get hotplugged) grows
1396  * the driver model tree.
1397  *
1398  * As a rule, SPI devices can't be probed.  Instead, board init code
1399  * provides a table listing the devices which are present, with enough
1400  * information to bind and set up the device's driver.  There's basic
1401  * support for nonstatic configurations too; enough to handle adding
1402  * parport adapters, or microcontrollers acting as USB-to-SPI bridges.
1403  */
1404 
1405 /**
1406  * struct spi_board_info - board-specific template for a SPI device
1407  * @modalias: Initializes spi_device.modalias; identifies the driver.
1408  * @platform_data: Initializes spi_device.platform_data; the particular
1409  *  data stored there is driver-specific.
1410  * @swnode: Software node for the device.
1411  * @controller_data: Initializes spi_device.controller_data; some
1412  *  controllers need hints about hardware setup, e.g. for DMA.
1413  * @irq: Initializes spi_device.irq; depends on how the board is wired.
1414  * @max_speed_hz: Initializes spi_device.max_speed_hz; based on limits
1415  *  from the chip datasheet and board-specific signal quality issues.
1416  * @bus_num: Identifies which spi_controller parents the spi_device; unused
1417  *  by spi_new_device(), and otherwise depends on board wiring.
1418  * @chip_select: Initializes spi_device.chip_select; depends on how
1419  *  the board is wired.
1420  * @mode: Initializes spi_device.mode; based on the chip datasheet, board
1421  *  wiring (some devices support both 3WIRE and standard modes), and
1422  *  possibly presence of an inverter in the chipselect path.
1423  *
1424  * When adding new SPI devices to the device tree, these structures serve
1425  * as a partial device template.  They hold information which can't always
1426  * be determined by drivers.  Information that probe() can establish (such
1427  * as the default transfer wordsize) is not included here.
1428  *
1429  * These structures are used in two places.  Their primary role is to
1430  * be stored in tables of board-specific device descriptors, which are
1431  * declared early in board initialization and then used (much later) to
1432  * populate a controller's device tree after the that controller's driver
1433  * initializes.  A secondary (and atypical) role is as a parameter to
1434  * spi_new_device() call, which happens after those controller drivers
1435  * are active in some dynamic board configuration models.
1436  */
1437 struct spi_board_info {
1438     /* The device name and module name are coupled, like platform_bus;
1439      * "modalias" is normally the driver name.
1440      *
1441      * platform_data goes to spi_device.dev.platform_data,
1442      * controller_data goes to spi_device.controller_data,
1443      * irq is copied too
1444      */
1445     char        modalias[SPI_NAME_SIZE];
1446     const void  *platform_data;
1447     const struct software_node *swnode;
1448     void        *controller_data;
1449     int     irq;
1450 
1451     /* Slower signaling on noisy or low voltage boards */
1452     u32     max_speed_hz;
1453 
1454 
1455     /* bus_num is board specific and matches the bus_num of some
1456      * spi_controller that will probably be registered later.
1457      *
1458      * chip_select reflects how this chip is wired to that master;
1459      * it's less than num_chipselect.
1460      */
1461     u16     bus_num;
1462     u16     chip_select;
1463 
1464     /* mode becomes spi_device.mode, and is essential for chips
1465      * where the default of SPI_CS_HIGH = 0 is wrong.
1466      */
1467     u32     mode;
1468 
1469     /* ... may need additional spi_device chip config data here.
1470      * avoid stuff protocol drivers can set; but include stuff
1471      * needed to behave without being bound to a driver:
1472      *  - quirks like clock rate mattering when not selected
1473      */
1474 };
1475 
1476 #ifdef  CONFIG_SPI
1477 extern int
1478 spi_register_board_info(struct spi_board_info const *info, unsigned n);
1479 #else
1480 /* Board init code may ignore whether SPI is configured or not */
1481 static inline int
1482 spi_register_board_info(struct spi_board_info const *info, unsigned n)
1483     { return 0; }
1484 #endif
1485 
1486 /* If you're hotplugging an adapter with devices (parport, usb, etc)
1487  * use spi_new_device() to describe each device.  You can also call
1488  * spi_unregister_device() to start making that device vanish, but
1489  * normally that would be handled by spi_unregister_controller().
1490  *
1491  * You can also use spi_alloc_device() and spi_add_device() to use a two
1492  * stage registration sequence for each spi_device. This gives the caller
1493  * some more control over the spi_device structure before it is registered,
1494  * but requires that caller to initialize fields that would otherwise
1495  * be defined using the board info.
1496  */
1497 extern struct spi_device *
1498 spi_alloc_device(struct spi_controller *ctlr);
1499 
1500 extern int
1501 spi_add_device(struct spi_device *spi);
1502 
1503 extern struct spi_device *
1504 spi_new_device(struct spi_controller *, struct spi_board_info *);
1505 
1506 extern void spi_unregister_device(struct spi_device *spi);
1507 
1508 extern const struct spi_device_id *
1509 spi_get_device_id(const struct spi_device *sdev);
1510 
1511 static inline bool
1512 spi_transfer_is_last(struct spi_controller *ctlr, struct spi_transfer *xfer)
1513 {
1514     return list_is_last(&xfer->transfer_list, &ctlr->cur_msg->transfers);
1515 }
1516 
1517 /* Compatibility layer */
1518 #define spi_master          spi_controller
1519 
1520 #define SPI_MASTER_HALF_DUPLEX      SPI_CONTROLLER_HALF_DUPLEX
1521 #define SPI_MASTER_NO_RX        SPI_CONTROLLER_NO_RX
1522 #define SPI_MASTER_NO_TX        SPI_CONTROLLER_NO_TX
1523 #define SPI_MASTER_MUST_RX      SPI_CONTROLLER_MUST_RX
1524 #define SPI_MASTER_MUST_TX      SPI_CONTROLLER_MUST_TX
1525 
1526 #define spi_master_get_devdata(_ctlr)   spi_controller_get_devdata(_ctlr)
1527 #define spi_master_set_devdata(_ctlr, _data)    \
1528     spi_controller_set_devdata(_ctlr, _data)
1529 #define spi_master_get(_ctlr)       spi_controller_get(_ctlr)
1530 #define spi_master_put(_ctlr)       spi_controller_put(_ctlr)
1531 #define spi_master_suspend(_ctlr)   spi_controller_suspend(_ctlr)
1532 #define spi_master_resume(_ctlr)    spi_controller_resume(_ctlr)
1533 
1534 #define spi_register_master(_ctlr)  spi_register_controller(_ctlr)
1535 #define devm_spi_register_master(_dev, _ctlr) \
1536     devm_spi_register_controller(_dev, _ctlr)
1537 #define spi_unregister_master(_ctlr)    spi_unregister_controller(_ctlr)
1538 
1539 #endif /* __LINUX_SPI_H */