1 /* SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2005 David Brownell
9 #include <linux/bits.h>
10 #include <linux/device.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/slab.h>
13 #include <linux/kthread.h>
14 #include <linux/completion.h>
15 #include <linux/scatterlist.h>
16 #include <linux/gpio/consumer.h>
18 #include <uapi/linux/spi/spi.h>
19 #include <linux/acpi.h>
20 #include <linux/u64_stats_sync.h>
24 struct ptp_system_timestamp;
25 struct spi_controller;
27 struct spi_controller_mem_ops;
28 struct spi_controller_mem_caps;
32 * INTERFACES between SPI master-side drivers and SPI slave protocol handlers,
33 * and SPI infrastructure.
35 extern struct bus_type spi_bus_type;
38 * struct spi_statistics - statistics for spi transfers
39 * @syncp: seqcount to protect members in this struct for per-cpu udate
42 * @messages: number of spi-messages handled
43 * @transfers: number of spi_transfers handled
44 * @errors: number of errors during spi_transfer
45 * @timedout: number of timeouts during spi_transfer
47 * @spi_sync: number of times spi_sync is used
48 * @spi_sync_immediate:
49 * number of times spi_sync is executed immediately
50 * in calling context without queuing and scheduling
51 * @spi_async: number of times spi_async is used
53 * @bytes: number of bytes transferred to/from device
54 * @bytes_tx: number of bytes sent to device
55 * @bytes_rx: number of bytes received from device
57 * @transfer_bytes_histo:
58 * transfer bytes histogramm
60 * @transfers_split_maxsize:
61 * number of transfers that have been split because of
64 struct spi_statistics {
65 struct u64_stats_sync syncp;
68 u64_stats_t transfers;
73 u64_stats_t spi_sync_immediate;
74 u64_stats_t spi_async;
80 #define SPI_STATISTICS_HISTO_SIZE 17
81 u64_stats_t transfer_bytes_histo[SPI_STATISTICS_HISTO_SIZE];
83 u64_stats_t transfers_split_maxsize;
86 #define SPI_STATISTICS_ADD_TO_FIELD(pcpu_stats, field, count) \
88 struct spi_statistics *__lstats; \
90 __lstats = this_cpu_ptr(pcpu_stats); \
91 u64_stats_update_begin(&__lstats->syncp); \
92 u64_stats_add(&__lstats->field, count); \
93 u64_stats_update_end(&__lstats->syncp); \
97 #define SPI_STATISTICS_INCREMENT_FIELD(pcpu_stats, field) \
99 struct spi_statistics *__lstats; \
101 __lstats = this_cpu_ptr(pcpu_stats); \
102 u64_stats_update_begin(&__lstats->syncp); \
103 u64_stats_inc(&__lstats->field); \
104 u64_stats_update_end(&__lstats->syncp); \
109 * struct spi_delay - SPI delay information
110 * @value: Value for the delay
111 * @unit: Unit for the delay
114 #define SPI_DELAY_UNIT_USECS 0
115 #define SPI_DELAY_UNIT_NSECS 1
116 #define SPI_DELAY_UNIT_SCK 2
121 extern int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer);
122 extern int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer);
123 extern void spi_transfer_cs_change_delay_exec(struct spi_message *msg,
124 struct spi_transfer *xfer);
127 * struct spi_device - Controller side proxy for an SPI slave device
128 * @dev: Driver model representation of the device.
129 * @controller: SPI controller used with the device.
130 * @master: Copy of controller, for backwards compatibility.
131 * @max_speed_hz: Maximum clock rate to be used with this chip
132 * (on this board); may be changed by the device's driver.
133 * The spi_transfer.speed_hz can override this for each transfer.
134 * @chip_select: Chipselect, distinguishing chips handled by @controller.
135 * @mode: The spi mode defines how data is clocked out and in.
136 * This may be changed by the device's driver.
137 * The "active low" default for chipselect mode can be overridden
138 * (by specifying SPI_CS_HIGH) as can the "MSB first" default for
139 * each word in a transfer (by specifying SPI_LSB_FIRST).
140 * @bits_per_word: Data transfers involve one or more words; word sizes
141 * like eight or 12 bits are common. In-memory wordsizes are
142 * powers of two bytes (e.g. 20 bit samples use 32 bits).
143 * This may be changed by the device's driver, or left at the
144 * default (0) indicating protocol words are eight bit bytes.
145 * The spi_transfer.bits_per_word can override this for each transfer.
146 * @rt: Make the pump thread real time priority.
147 * @irq: Negative, or the number passed to request_irq() to receive
148 * interrupts from this device.
149 * @controller_state: Controller's runtime state
150 * @controller_data: Board-specific definitions for controller, such as
151 * FIFO initialization parameters; from board_info.controller_data
152 * @modalias: Name of the driver to use with this device, or an alias
153 * for that name. This appears in the sysfs "modalias" attribute
154 * for driver coldplugging, and in uevents used for hotplugging
155 * @driver_override: If the name of a driver is written to this attribute, then
156 * the device will bind to the named driver and only the named driver.
157 * Do not set directly, because core frees it; use driver_set_override() to
159 * @cs_gpiod: gpio descriptor of the chipselect line (optional, NULL when
160 * not using a GPIO line)
161 * @word_delay: delay to be inserted between consecutive
162 * words of a transfer
163 * @cs_setup: delay to be introduced by the controller after CS is asserted
164 * @cs_hold: delay to be introduced by the controller before CS is deasserted
165 * @cs_inactive: delay to be introduced by the controller after CS is
166 * deasserted. If @cs_change_delay is used from @spi_transfer, then the
167 * two delays will be added up.
168 * @pcpu_statistics: statistics for the spi_device
170 * A @spi_device is used to interchange data between an SPI slave
171 * (usually a discrete chip) and CPU memory.
173 * In @dev, the platform_data is used to hold information about this
174 * device that's meaningful to the device's protocol driver, but not
175 * to its controller. One example might be an identifier for a chip
176 * variant with slightly different functionality; another might be
177 * information about how this particular board wires the chip's pins.
181 struct spi_controller *controller;
182 struct spi_controller *master; /* Compatibility layer */
187 #define SPI_NO_TX BIT(31) /* No transmit wire */
188 #define SPI_NO_RX BIT(30) /* No receive wire */
190 * TPM specification defines flow control over SPI. Client device
191 * can insert a wait state on MISO when address is transmitted by
192 * controller on MOSI. Detecting the wait state in software is only
193 * possible for full duplex controllers. For controllers that support
194 * only half-duplex, the wait state detection needs to be implemented
195 * in hardware. TPM devices would set this flag when hardware flow
196 * control is expected from SPI controller.
198 #define SPI_TPM_HW_FLOW BIT(29) /* TPM HW flow control */
200 * All bits defined above should be covered by SPI_MODE_KERNEL_MASK.
201 * The SPI_MODE_KERNEL_MASK has the SPI_MODE_USER_MASK counterpart,
202 * which is defined in 'include/uapi/linux/spi/spi.h'.
203 * The bits defined here are from bit 31 downwards, while in
204 * SPI_MODE_USER_MASK are from 0 upwards.
205 * These bits must not overlap. A static assert check should make sure of that.
206 * If adding extra bits, make sure to decrease the bit index below as well.
208 #define SPI_MODE_KERNEL_MASK (~(BIT(29) - 1))
211 void *controller_state;
212 void *controller_data;
213 char modalias[SPI_NAME_SIZE];
214 const char *driver_override;
215 struct gpio_desc *cs_gpiod; /* Chip select gpio desc */
216 struct spi_delay word_delay; /* Inter-word delay */
218 struct spi_delay cs_setup;
219 struct spi_delay cs_hold;
220 struct spi_delay cs_inactive;
223 struct spi_statistics __percpu *pcpu_statistics;
226 * likely need more hooks for more protocol options affecting how
227 * the controller talks to each chip, like:
228 * - memory packing (12 bit samples into low bits, others zeroed)
230 * - chipselect delays
235 /* Make sure that SPI_MODE_KERNEL_MASK & SPI_MODE_USER_MASK don't overlap */
236 static_assert((SPI_MODE_KERNEL_MASK & SPI_MODE_USER_MASK) == 0,
237 "SPI_MODE_USER_MASK & SPI_MODE_KERNEL_MASK must not overlap");
239 static inline struct spi_device *to_spi_device(const struct device *dev)
241 return dev ? container_of(dev, struct spi_device, dev) : NULL;
244 /* Most drivers won't need to care about device refcounting */
245 static inline struct spi_device *spi_dev_get(struct spi_device *spi)
247 return (spi && get_device(&spi->dev)) ? spi : NULL;
250 static inline void spi_dev_put(struct spi_device *spi)
253 put_device(&spi->dev);
256 /* ctldata is for the bus_controller driver's runtime state */
257 static inline void *spi_get_ctldata(const struct spi_device *spi)
259 return spi->controller_state;
262 static inline void spi_set_ctldata(struct spi_device *spi, void *state)
264 spi->controller_state = state;
267 /* Device driver data */
269 static inline void spi_set_drvdata(struct spi_device *spi, void *data)
271 dev_set_drvdata(&spi->dev, data);
274 static inline void *spi_get_drvdata(const struct spi_device *spi)
276 return dev_get_drvdata(&spi->dev);
279 static inline u8 spi_get_chipselect(const struct spi_device *spi, u8 idx)
281 return spi->chip_select;
284 static inline void spi_set_chipselect(struct spi_device *spi, u8 idx, u8 chipselect)
286 spi->chip_select = chipselect;
289 static inline struct gpio_desc *spi_get_csgpiod(const struct spi_device *spi, u8 idx)
291 return spi->cs_gpiod;
294 static inline void spi_set_csgpiod(struct spi_device *spi, u8 idx, struct gpio_desc *csgpiod)
296 spi->cs_gpiod = csgpiod;
300 * struct spi_driver - Host side "protocol" driver
301 * @id_table: List of SPI devices supported by this driver
302 * @probe: Binds this driver to the spi device. Drivers can verify
303 * that the device is actually present, and may need to configure
304 * characteristics (such as bits_per_word) which weren't needed for
305 * the initial configuration done during system setup.
306 * @remove: Unbinds this driver from the spi device
307 * @shutdown: Standard shutdown callback used during system state
308 * transitions such as powerdown/halt and kexec
309 * @driver: SPI device drivers should initialize the name and owner
310 * field of this structure.
312 * This represents the kind of device driver that uses SPI messages to
313 * interact with the hardware at the other end of a SPI link. It's called
314 * a "protocol" driver because it works through messages rather than talking
315 * directly to SPI hardware (which is what the underlying SPI controller
316 * driver does to pass those messages). These protocols are defined in the
317 * specification for the device(s) supported by the driver.
319 * As a rule, those device protocols represent the lowest level interface
320 * supported by a driver, and it will support upper level interfaces too.
321 * Examples of such upper levels include frameworks like MTD, networking,
322 * MMC, RTC, filesystem character device nodes, and hardware monitoring.
325 const struct spi_device_id *id_table;
326 int (*probe)(struct spi_device *spi);
327 void (*remove)(struct spi_device *spi);
328 void (*shutdown)(struct spi_device *spi);
329 struct device_driver driver;
332 static inline struct spi_driver *to_spi_driver(struct device_driver *drv)
334 return drv ? container_of(drv, struct spi_driver, driver) : NULL;
337 extern int __spi_register_driver(struct module *owner, struct spi_driver *sdrv);
340 * spi_unregister_driver - reverse effect of spi_register_driver
341 * @sdrv: the driver to unregister
344 static inline void spi_unregister_driver(struct spi_driver *sdrv)
347 driver_unregister(&sdrv->driver);
350 extern struct spi_device *spi_new_ancillary_device(struct spi_device *spi, u8 chip_select);
352 /* Use a define to avoid include chaining to get THIS_MODULE */
353 #define spi_register_driver(driver) \
354 __spi_register_driver(THIS_MODULE, driver)
357 * module_spi_driver() - Helper macro for registering a SPI driver
358 * @__spi_driver: spi_driver struct
360 * Helper macro for SPI drivers which do not do anything special in module
361 * init/exit. This eliminates a lot of boilerplate. Each module may only
362 * use this macro once, and calling it replaces module_init() and module_exit()
364 #define module_spi_driver(__spi_driver) \
365 module_driver(__spi_driver, spi_register_driver, \
366 spi_unregister_driver)
369 * struct spi_controller - interface to SPI master or slave controller
370 * @dev: device interface to this driver
371 * @list: link with the global spi_controller list
372 * @bus_num: board-specific (and often SOC-specific) identifier for a
373 * given SPI controller.
374 * @num_chipselect: chipselects are used to distinguish individual
375 * SPI slaves, and are numbered from zero to num_chipselects.
376 * each slave has a chipselect signal, but it's common that not
377 * every chipselect is connected to a slave.
378 * @dma_alignment: SPI controller constraint on DMA buffers alignment.
379 * @mode_bits: flags understood by this controller driver
380 * @buswidth_override_bits: flags to override for this controller driver
381 * @bits_per_word_mask: A mask indicating which values of bits_per_word are
382 * supported by the driver. Bit n indicates that a bits_per_word n+1 is
383 * supported. If set, the SPI core will reject any transfer with an
384 * unsupported bits_per_word. If not set, this value is simply ignored,
385 * and it's up to the individual driver to perform any validation.
386 * @min_speed_hz: Lowest supported transfer speed
387 * @max_speed_hz: Highest supported transfer speed
388 * @flags: other constraints relevant to this driver
389 * @slave: indicates that this is an SPI slave controller
390 * @target: indicates that this is an SPI target controller
391 * @devm_allocated: whether the allocation of this struct is devres-managed
392 * @max_transfer_size: function that returns the max transfer size for
393 * a &spi_device; may be %NULL, so the default %SIZE_MAX will be used.
394 * @max_message_size: function that returns the max message size for
395 * a &spi_device; may be %NULL, so the default %SIZE_MAX will be used.
396 * @io_mutex: mutex for physical bus access
397 * @add_lock: mutex to avoid adding devices to the same chipselect
398 * @bus_lock_spinlock: spinlock for SPI bus locking
399 * @bus_lock_mutex: mutex for exclusion of multiple callers
400 * @bus_lock_flag: indicates that the SPI bus is locked for exclusive use
401 * @setup: updates the device mode and clocking records used by a
402 * device's SPI controller; protocol code may call this. This
403 * must fail if an unrecognized or unsupported mode is requested.
404 * It's always safe to call this unless transfers are pending on
405 * the device whose settings are being modified.
406 * @set_cs_timing: optional hook for SPI devices to request SPI master
407 * controller for configuring specific CS setup time, hold time and inactive
408 * delay interms of clock counts
409 * @transfer: adds a message to the controller's transfer queue.
410 * @cleanup: frees controller-specific state
411 * @can_dma: determine whether this controller supports DMA
412 * @dma_map_dev: device which can be used for DMA mapping
413 * @cur_rx_dma_dev: device which is currently used for RX DMA mapping
414 * @cur_tx_dma_dev: device which is currently used for TX DMA mapping
415 * @queued: whether this controller is providing an internal message queue
416 * @kworker: pointer to thread struct for message pump
417 * @pump_messages: work struct for scheduling work to the message pump
418 * @queue_lock: spinlock to syncronise access to message queue
419 * @queue: message queue
420 * @cur_msg: the currently in-flight message
421 * @cur_msg_completion: a completion for the current in-flight message
422 * @cur_msg_incomplete: Flag used internally to opportunistically skip
423 * the @cur_msg_completion. This flag is used to check if the driver has
424 * already called spi_finalize_current_message().
425 * @cur_msg_need_completion: Flag used internally to opportunistically skip
426 * the @cur_msg_completion. This flag is used to signal the context that
427 * is running spi_finalize_current_message() that it needs to complete()
428 * @cur_msg_mapped: message has been mapped for DMA
429 * @last_cs: the last chip_select that is recorded by set_cs, -1 on non chip
431 * @last_cs_mode_high: was (mode & SPI_CS_HIGH) true on the last call to set_cs.
432 * @xfer_completion: used by core transfer_one_message()
433 * @busy: message pump is busy
434 * @running: message pump is running
435 * @rt: whether this queue is set to run as a realtime task
436 * @auto_runtime_pm: the core should ensure a runtime PM reference is held
437 * while the hardware is prepared, using the parent
438 * device for the spidev
439 * @max_dma_len: Maximum length of a DMA transfer for the device.
440 * @prepare_transfer_hardware: a message will soon arrive from the queue
441 * so the subsystem requests the driver to prepare the transfer hardware
442 * by issuing this call
443 * @transfer_one_message: the subsystem calls the driver to transfer a single
444 * message while queuing transfers that arrive in the meantime. When the
445 * driver is finished with this message, it must call
446 * spi_finalize_current_message() so the subsystem can issue the next
448 * @unprepare_transfer_hardware: there are currently no more messages on the
449 * queue so the subsystem notifies the driver that it may relax the
450 * hardware by issuing this call
452 * @set_cs: set the logic level of the chip select line. May be called
453 * from interrupt context.
454 * @prepare_message: set up the controller to transfer a single message,
455 * for example doing DMA mapping. Called from threaded
457 * @transfer_one: transfer a single spi_transfer.
459 * - return 0 if the transfer is finished,
460 * - return 1 if the transfer is still in progress. When
461 * the driver is finished with this transfer it must
462 * call spi_finalize_current_transfer() so the subsystem
463 * can issue the next transfer. Note: transfer_one and
464 * transfer_one_message are mutually exclusive; when both
465 * are set, the generic subsystem does not call your
466 * transfer_one callback.
467 * @handle_err: the subsystem calls the driver to handle an error that occurs
468 * in the generic implementation of transfer_one_message().
469 * @mem_ops: optimized/dedicated operations for interactions with SPI memory.
470 * This field is optional and should only be implemented if the
471 * controller has native support for memory like operations.
472 * @mem_caps: controller capabilities for the handling of memory operations.
473 * @unprepare_message: undo any work done by prepare_message().
474 * @slave_abort: abort the ongoing transfer request on an SPI slave controller
475 * @target_abort: abort the ongoing transfer request on an SPI target controller
476 * @cs_gpiods: Array of GPIO descs to use as chip select lines; one per CS
477 * number. Any individual value may be NULL for CS lines that
478 * are not GPIOs (driven by the SPI controller itself).
479 * @use_gpio_descriptors: Turns on the code in the SPI core to parse and grab
480 * GPIO descriptors. This will fill in @cs_gpiods and SPI devices will have
481 * the cs_gpiod assigned if a GPIO line is found for the chipselect.
482 * @unused_native_cs: When cs_gpiods is used, spi_register_controller() will
483 * fill in this field with the first unused native CS, to be used by SPI
484 * controller drivers that need to drive a native CS when using GPIO CS.
485 * @max_native_cs: When cs_gpiods is used, and this field is filled in,
486 * spi_register_controller() will validate all native CS (including the
487 * unused native CS) against this value.
488 * @pcpu_statistics: statistics for the spi_controller
489 * @dma_tx: DMA transmit channel
490 * @dma_rx: DMA receive channel
491 * @dummy_rx: dummy receive buffer for full-duplex devices
492 * @dummy_tx: dummy transmit buffer for full-duplex devices
493 * @fw_translate_cs: If the boot firmware uses different numbering scheme
494 * what Linux expects, this optional hook can be used to translate
496 * @ptp_sts_supported: If the driver sets this to true, it must provide a
497 * time snapshot in @spi_transfer->ptp_sts as close as possible to the
498 * moment in time when @spi_transfer->ptp_sts_word_pre and
499 * @spi_transfer->ptp_sts_word_post were transmitted.
500 * If the driver does not set this, the SPI core takes the snapshot as
501 * close to the driver hand-over as possible.
502 * @irq_flags: Interrupt enable state during PTP system timestamping
503 * @fallback: fallback to pio if dma transfer return failure with
504 * SPI_TRANS_FAIL_NO_START.
505 * @queue_empty: signal green light for opportunistically skipping the queue
506 * for spi_sync transfers.
507 * @must_async: disable all fast paths in the core
509 * Each SPI controller can communicate with one or more @spi_device
510 * children. These make a small bus, sharing MOSI, MISO and SCK signals
511 * but not chip select signals. Each device may be configured to use a
512 * different clock rate, since those shared signals are ignored unless
513 * the chip is selected.
515 * The driver for an SPI controller manages access to those devices through
516 * a queue of spi_message transactions, copying data between CPU memory and
517 * an SPI slave device. For each such message it queues, it calls the
518 * message's completion function when the transaction completes.
520 struct spi_controller {
523 struct list_head list;
525 /* Other than negative (== assign one dynamically), bus_num is fully
526 * board-specific. usually that simplifies to being SOC-specific.
527 * example: one SOC has three SPI controllers, numbered 0..2,
528 * and one board's schematics might show it using SPI-2. software
529 * would normally use bus_num=2 for that controller.
533 /* chipselects will be integral to many controllers; some others
534 * might use board-specific GPIOs.
538 /* Some SPI controllers pose alignment requirements on DMAable
539 * buffers; let protocol drivers know about these requirements.
543 /* spi_device.mode flags understood by this controller driver */
546 /* spi_device.mode flags override flags for this controller */
547 u32 buswidth_override_bits;
549 /* Bitmask of supported bits_per_word for transfers */
550 u32 bits_per_word_mask;
551 #define SPI_BPW_MASK(bits) BIT((bits) - 1)
552 #define SPI_BPW_RANGE_MASK(min, max) GENMASK((max) - 1, (min) - 1)
554 /* Limits on transfer speed */
558 /* Other constraints relevant to this driver */
560 #define SPI_CONTROLLER_HALF_DUPLEX BIT(0) /* Can't do full duplex */
561 #define SPI_CONTROLLER_NO_RX BIT(1) /* Can't do buffer read */
562 #define SPI_CONTROLLER_NO_TX BIT(2) /* Can't do buffer write */
563 #define SPI_CONTROLLER_MUST_RX BIT(3) /* Requires rx */
564 #define SPI_CONTROLLER_MUST_TX BIT(4) /* Requires tx */
566 #define SPI_MASTER_GPIO_SS BIT(5) /* GPIO CS must select slave */
568 /* Flag indicating if the allocation of this struct is devres-managed */
572 /* Flag indicating this is an SPI slave controller */
574 /* Flag indicating this is an SPI target controller */
579 * on some hardware transfer / message size may be constrained
580 * the limit may depend on device transfer settings
582 size_t (*max_transfer_size)(struct spi_device *spi);
583 size_t (*max_message_size)(struct spi_device *spi);
586 struct mutex io_mutex;
588 /* Used to avoid adding the same CS twice */
589 struct mutex add_lock;
591 /* Lock and mutex for SPI bus locking */
592 spinlock_t bus_lock_spinlock;
593 struct mutex bus_lock_mutex;
595 /* Flag indicating that the SPI bus is locked for exclusive use */
598 /* Setup mode and clock, etc (spi driver may call many times).
600 * IMPORTANT: this may be called when transfers to another
601 * device are active. DO NOT UPDATE SHARED REGISTERS in ways
602 * which could break those transfers.
604 int (*setup)(struct spi_device *spi);
607 * set_cs_timing() method is for SPI controllers that supports
608 * configuring CS timing.
610 * This hook allows SPI client drivers to request SPI controllers
611 * to configure specific CS timing through spi_set_cs_timing() after
614 int (*set_cs_timing)(struct spi_device *spi);
616 /* Bidirectional bulk transfers
618 * + The transfer() method may not sleep; its main role is
619 * just to add the message to the queue.
620 * + For now there's no remove-from-queue operation, or
621 * any other request management
622 * + To a given spi_device, message queueing is pure fifo
624 * + The controller's main job is to process its message queue,
625 * selecting a chip (for masters), then transferring data
626 * + If there are multiple spi_device children, the i/o queue
627 * arbitration algorithm is unspecified (round robin, fifo,
628 * priority, reservations, preemption, etc)
630 * + Chipselect stays active during the entire message
631 * (unless modified by spi_transfer.cs_change != 0).
632 * + The message transfers use clock and SPI mode parameters
633 * previously established by setup() for this device
635 int (*transfer)(struct spi_device *spi,
636 struct spi_message *mesg);
638 /* Called on release() to free memory provided by spi_controller */
639 void (*cleanup)(struct spi_device *spi);
642 * Used to enable core support for DMA handling, if can_dma()
643 * exists and returns true then the transfer will be mapped
644 * prior to transfer_one() being called. The driver should
645 * not modify or store xfer and dma_tx and dma_rx must be set
646 * while the device is prepared.
648 bool (*can_dma)(struct spi_controller *ctlr,
649 struct spi_device *spi,
650 struct spi_transfer *xfer);
651 struct device *dma_map_dev;
652 struct device *cur_rx_dma_dev;
653 struct device *cur_tx_dma_dev;
656 * These hooks are for drivers that want to use the generic
657 * controller transfer queueing mechanism. If these are used, the
658 * transfer() function above must NOT be specified by the driver.
659 * Over time we expect SPI drivers to be phased over to this API.
662 struct kthread_worker *kworker;
663 struct kthread_work pump_messages;
664 spinlock_t queue_lock;
665 struct list_head queue;
666 struct spi_message *cur_msg;
667 struct completion cur_msg_completion;
668 bool cur_msg_incomplete;
669 bool cur_msg_need_completion;
673 bool auto_runtime_pm;
676 bool last_cs_mode_high;
678 struct completion xfer_completion;
681 int (*prepare_transfer_hardware)(struct spi_controller *ctlr);
682 int (*transfer_one_message)(struct spi_controller *ctlr,
683 struct spi_message *mesg);
684 int (*unprepare_transfer_hardware)(struct spi_controller *ctlr);
685 int (*prepare_message)(struct spi_controller *ctlr,
686 struct spi_message *message);
687 int (*unprepare_message)(struct spi_controller *ctlr,
688 struct spi_message *message);
690 int (*slave_abort)(struct spi_controller *ctlr);
691 int (*target_abort)(struct spi_controller *ctlr);
695 * These hooks are for drivers that use a generic implementation
696 * of transfer_one_message() provided by the core.
698 void (*set_cs)(struct spi_device *spi, bool enable);
699 int (*transfer_one)(struct spi_controller *ctlr, struct spi_device *spi,
700 struct spi_transfer *transfer);
701 void (*handle_err)(struct spi_controller *ctlr,
702 struct spi_message *message);
704 /* Optimized handlers for SPI memory-like operations. */
705 const struct spi_controller_mem_ops *mem_ops;
706 const struct spi_controller_mem_caps *mem_caps;
708 /* gpio chip select */
709 struct gpio_desc **cs_gpiods;
710 bool use_gpio_descriptors;
715 struct spi_statistics __percpu *pcpu_statistics;
717 /* DMA channels for use with core dmaengine helpers */
718 struct dma_chan *dma_tx;
719 struct dma_chan *dma_rx;
721 /* Dummy data for full duplex devices */
725 int (*fw_translate_cs)(struct spi_controller *ctlr, unsigned cs);
728 * Driver sets this field to indicate it is able to snapshot SPI
729 * transfers (needed e.g. for reading the time of POSIX clocks)
731 bool ptp_sts_supported;
733 /* Interrupt enable state during PTP system timestamping */
734 unsigned long irq_flags;
736 /* Flag for enabling opportunistic skipping of the queue in spi_sync */
741 static inline void *spi_controller_get_devdata(struct spi_controller *ctlr)
743 return dev_get_drvdata(&ctlr->dev);
746 static inline void spi_controller_set_devdata(struct spi_controller *ctlr,
749 dev_set_drvdata(&ctlr->dev, data);
752 static inline struct spi_controller *spi_controller_get(struct spi_controller *ctlr)
754 if (!ctlr || !get_device(&ctlr->dev))
759 static inline void spi_controller_put(struct spi_controller *ctlr)
762 put_device(&ctlr->dev);
765 static inline bool spi_controller_is_slave(struct spi_controller *ctlr)
767 return IS_ENABLED(CONFIG_SPI_SLAVE) && ctlr->slave;
770 static inline bool spi_controller_is_target(struct spi_controller *ctlr)
772 return IS_ENABLED(CONFIG_SPI_SLAVE) && ctlr->target;
775 /* PM calls that need to be issued by the driver */
776 extern int spi_controller_suspend(struct spi_controller *ctlr);
777 extern int spi_controller_resume(struct spi_controller *ctlr);
779 /* Calls the driver make to interact with the message queue */
780 extern struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr);
781 extern void spi_finalize_current_message(struct spi_controller *ctlr);
782 extern void spi_finalize_current_transfer(struct spi_controller *ctlr);
784 /* Helper calls for driver to timestamp transfer */
785 void spi_take_timestamp_pre(struct spi_controller *ctlr,
786 struct spi_transfer *xfer,
787 size_t progress, bool irqs_off);
788 void spi_take_timestamp_post(struct spi_controller *ctlr,
789 struct spi_transfer *xfer,
790 size_t progress, bool irqs_off);
792 /* The spi driver core manages memory for the spi_controller classdev */
793 extern struct spi_controller *__spi_alloc_controller(struct device *host,
794 unsigned int size, bool slave);
796 static inline struct spi_controller *spi_alloc_master(struct device *host,
799 return __spi_alloc_controller(host, size, false);
802 static inline struct spi_controller *spi_alloc_slave(struct device *host,
805 if (!IS_ENABLED(CONFIG_SPI_SLAVE))
808 return __spi_alloc_controller(host, size, true);
811 static inline struct spi_controller *spi_alloc_host(struct device *dev,
814 return __spi_alloc_controller(dev, size, false);
817 static inline struct spi_controller *spi_alloc_target(struct device *dev,
820 if (!IS_ENABLED(CONFIG_SPI_SLAVE))
823 return __spi_alloc_controller(dev, size, true);
826 struct spi_controller *__devm_spi_alloc_controller(struct device *dev,
830 static inline struct spi_controller *devm_spi_alloc_master(struct device *dev,
833 return __devm_spi_alloc_controller(dev, size, false);
836 static inline struct spi_controller *devm_spi_alloc_slave(struct device *dev,
839 if (!IS_ENABLED(CONFIG_SPI_SLAVE))
842 return __devm_spi_alloc_controller(dev, size, true);
845 static inline struct spi_controller *devm_spi_alloc_host(struct device *dev,
848 return __devm_spi_alloc_controller(dev, size, false);
851 static inline struct spi_controller *devm_spi_alloc_target(struct device *dev,
854 if (!IS_ENABLED(CONFIG_SPI_SLAVE))
857 return __devm_spi_alloc_controller(dev, size, true);
860 extern int spi_register_controller(struct spi_controller *ctlr);
861 extern int devm_spi_register_controller(struct device *dev,
862 struct spi_controller *ctlr);
863 extern void spi_unregister_controller(struct spi_controller *ctlr);
865 #if IS_ENABLED(CONFIG_ACPI)
866 extern struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr,
867 struct acpi_device *adev,
869 int acpi_spi_count_resources(struct acpi_device *adev);
873 * SPI resource management while processing a SPI message
876 typedef void (*spi_res_release_t)(struct spi_controller *ctlr,
877 struct spi_message *msg,
881 * struct spi_res - spi resource management structure
883 * @release: release code called prior to freeing this resource
884 * @data: extra data allocated for the specific use-case
886 * this is based on ideas from devres, but focused on life-cycle
887 * management during spi_message processing
890 struct list_head entry;
891 spi_res_release_t release;
892 unsigned long long data[]; /* Guarantee ull alignment */
895 /*---------------------------------------------------------------------------*/
898 * I/O INTERFACE between SPI controller and protocol drivers
900 * Protocol drivers use a queue of spi_messages, each transferring data
901 * between the controller and memory buffers.
903 * The spi_messages themselves consist of a series of read+write transfer
904 * segments. Those segments always read the same number of bits as they
905 * write; but one or the other is easily ignored by passing a null buffer
906 * pointer. (This is unlike most types of I/O API, because SPI hardware
909 * NOTE: Allocation of spi_transfer and spi_message memory is entirely
910 * up to the protocol driver, which guarantees the integrity of both (as
911 * well as the data buffers) for as long as the message is queued.
915 * struct spi_transfer - a read/write buffer pair
916 * @tx_buf: data to be written (dma-safe memory), or NULL
917 * @rx_buf: data to be read (dma-safe memory), or NULL
918 * @tx_dma: DMA address of tx_buf, if @spi_message.is_dma_mapped
919 * @rx_dma: DMA address of rx_buf, if @spi_message.is_dma_mapped
920 * @tx_nbits: number of bits used for writing. If 0 the default
921 * (SPI_NBITS_SINGLE) is used.
922 * @rx_nbits: number of bits used for reading. If 0 the default
923 * (SPI_NBITS_SINGLE) is used.
924 * @len: size of rx and tx buffers (in bytes)
925 * @speed_hz: Select a speed other than the device default for this
926 * transfer. If 0 the default (from @spi_device) is used.
927 * @bits_per_word: select a bits_per_word other than the device default
928 * for this transfer. If 0 the default (from @spi_device) is used.
929 * @dummy_data: indicates transfer is dummy bytes transfer.
930 * @cs_off: performs the transfer with chipselect off.
931 * @cs_change: affects chipselect after this transfer completes
932 * @cs_change_delay: delay between cs deassert and assert when
933 * @cs_change is set and @spi_transfer is not the last in @spi_message
934 * @delay: delay to be introduced after this transfer before
935 * (optionally) changing the chipselect status, then starting
936 * the next transfer or completing this @spi_message.
937 * @word_delay: inter word delay to be introduced after each word size
938 * (set by bits_per_word) transmission.
939 * @effective_speed_hz: the effective SCK-speed that was used to
940 * transfer this transfer. Set to 0 if the spi bus driver does
942 * @transfer_list: transfers are sequenced through @spi_message.transfers
943 * @tx_sg: Scatterlist for transmit, currently not for client use
944 * @rx_sg: Scatterlist for receive, currently not for client use
945 * @ptp_sts_word_pre: The word (subject to bits_per_word semantics) offset
946 * within @tx_buf for which the SPI device is requesting that the time
947 * snapshot for this transfer begins. Upon completing the SPI transfer,
948 * this value may have changed compared to what was requested, depending
949 * on the available snapshotting resolution (DMA transfer,
950 * @ptp_sts_supported is false, etc).
951 * @ptp_sts_word_post: See @ptp_sts_word_post. The two can be equal (meaning
952 * that a single byte should be snapshotted).
953 * If the core takes care of the timestamp (if @ptp_sts_supported is false
954 * for this controller), it will set @ptp_sts_word_pre to 0, and
955 * @ptp_sts_word_post to the length of the transfer. This is done
956 * purposefully (instead of setting to spi_transfer->len - 1) to denote
957 * that a transfer-level snapshot taken from within the driver may still
958 * be of higher quality.
959 * @ptp_sts: Pointer to a memory location held by the SPI slave device where a
960 * PTP system timestamp structure may lie. If drivers use PIO or their
961 * hardware has some sort of assist for retrieving exact transfer timing,
962 * they can (and should) assert @ptp_sts_supported and populate this
963 * structure using the ptp_read_system_*ts helper functions.
964 * The timestamp must represent the time at which the SPI slave device has
965 * processed the word, i.e. the "pre" timestamp should be taken before
966 * transmitting the "pre" word, and the "post" timestamp after receiving
967 * transmit confirmation from the controller for the "post" word.
968 * @timestamped: true if the transfer has been timestamped
969 * @error: Error status logged by spi controller driver.
971 * SPI transfers always write the same number of bytes as they read.
972 * Protocol drivers should always provide @rx_buf and/or @tx_buf.
973 * In some cases, they may also want to provide DMA addresses for
974 * the data being transferred; that may reduce overhead, when the
975 * underlying driver uses dma.
977 * If the transmit buffer is null, zeroes will be shifted out
978 * while filling @rx_buf. If the receive buffer is null, the data
979 * shifted in will be discarded. Only "len" bytes shift out (or in).
980 * It's an error to try to shift out a partial word. (For example, by
981 * shifting out three bytes with word size of sixteen or twenty bits;
982 * the former uses two bytes per word, the latter uses four bytes.)
984 * In-memory data values are always in native CPU byte order, translated
985 * from the wire byte order (big-endian except with SPI_LSB_FIRST). So
986 * for example when bits_per_word is sixteen, buffers are 2N bytes long
987 * (@len = 2N) and hold N sixteen bit words in CPU byte order.
989 * When the word size of the SPI transfer is not a power-of-two multiple
990 * of eight bits, those in-memory words include extra bits. In-memory
991 * words are always seen by protocol drivers as right-justified, so the
992 * undefined (rx) or unused (tx) bits are always the most significant bits.
994 * All SPI transfers start with the relevant chipselect active. Normally
995 * it stays selected until after the last transfer in a message. Drivers
996 * can affect the chipselect signal using cs_change.
998 * (i) If the transfer isn't the last one in the message, this flag is
999 * used to make the chipselect briefly go inactive in the middle of the
1000 * message. Toggling chipselect in this way may be needed to terminate
1001 * a chip command, letting a single spi_message perform all of group of
1002 * chip transactions together.
1004 * (ii) When the transfer is the last one in the message, the chip may
1005 * stay selected until the next transfer. On multi-device SPI busses
1006 * with nothing blocking messages going to other devices, this is just
1007 * a performance hint; starting a message to another device deselects
1008 * this one. But in other cases, this can be used to ensure correctness.
1009 * Some devices need protocol transactions to be built from a series of
1010 * spi_message submissions, where the content of one message is determined
1011 * by the results of previous messages and where the whole transaction
1012 * ends when the chipselect goes intactive.
1014 * When SPI can transfer in 1x,2x or 4x. It can get this transfer information
1015 * from device through @tx_nbits and @rx_nbits. In Bi-direction, these
1016 * two should both be set. User can set transfer mode with SPI_NBITS_SINGLE(1x)
1017 * SPI_NBITS_DUAL(2x) and SPI_NBITS_QUAD(4x) to support these three transfer.
1019 * The code that submits an spi_message (and its spi_transfers)
1020 * to the lower layers is responsible for managing its memory.
1021 * Zero-initialize every field you don't set up explicitly, to
1022 * insulate against future API updates. After you submit a message
1023 * and its transfers, ignore them until its completion callback.
1025 struct spi_transfer {
1026 /* It's ok if tx_buf == rx_buf (right?)
1027 * for MicroWire, one buffer must be null
1028 * buffers must work with dma_*map_single() calls, unless
1029 * spi_message.is_dma_mapped reports a pre-existing mapping
1035 #define SPI_TRANS_FAIL_NO_START BIT(0)
1040 struct sg_table tx_sg;
1041 struct sg_table rx_sg;
1043 unsigned dummy_data:1;
1045 unsigned cs_change:1;
1046 unsigned tx_nbits:3;
1047 unsigned rx_nbits:3;
1048 unsigned timestamped:1;
1049 #define SPI_NBITS_SINGLE 0x01 /* 1bit transfer */
1050 #define SPI_NBITS_DUAL 0x02 /* 2bits transfer */
1051 #define SPI_NBITS_QUAD 0x04 /* 4bits transfer */
1053 struct spi_delay delay;
1054 struct spi_delay cs_change_delay;
1055 struct spi_delay word_delay;
1058 u32 effective_speed_hz;
1060 unsigned int ptp_sts_word_pre;
1061 unsigned int ptp_sts_word_post;
1063 struct ptp_system_timestamp *ptp_sts;
1065 struct list_head transfer_list;
1069 * struct spi_message - one multi-segment SPI transaction
1070 * @transfers: list of transfer segments in this transaction
1071 * @spi: SPI device to which the transaction is queued
1072 * @is_dma_mapped: if true, the caller provided both dma and cpu virtual
1073 * addresses for each transfer buffer
1074 * @complete: called to report transaction completions
1075 * @context: the argument to complete() when it's called
1076 * @frame_length: the total number of bytes in the message
1077 * @actual_length: the total number of bytes that were transferred in all
1078 * successful segments
1079 * @status: zero for success, else negative errno
1080 * @queue: for use by whichever driver currently owns the message
1081 * @state: for use by whichever driver currently owns the message
1082 * @resources: for resource management when the spi message is processed
1083 * @prepared: spi_prepare_message was called for the this message
1085 * A @spi_message is used to execute an atomic sequence of data transfers,
1086 * each represented by a struct spi_transfer. The sequence is "atomic"
1087 * in the sense that no other spi_message may use that SPI bus until that
1088 * sequence completes. On some systems, many such sequences can execute as
1089 * a single programmed DMA transfer. On all systems, these messages are
1090 * queued, and might complete after transactions to other devices. Messages
1091 * sent to a given spi_device are always executed in FIFO order.
1093 * The code that submits an spi_message (and its spi_transfers)
1094 * to the lower layers is responsible for managing its memory.
1095 * Zero-initialize every field you don't set up explicitly, to
1096 * insulate against future API updates. After you submit a message
1097 * and its transfers, ignore them until its completion callback.
1099 struct spi_message {
1100 struct list_head transfers;
1102 struct spi_device *spi;
1104 unsigned is_dma_mapped:1;
1106 /* spi_prepare_message() was called for this message */
1109 /* REVISIT: we might want a flag affecting the behavior of the
1110 * last transfer ... allowing things like "read 16 bit length L"
1111 * immediately followed by "read L bytes". Basically imposing
1112 * a specific message scheduling algorithm.
1114 * Some controller drivers (message-at-a-time queue processing)
1115 * could provide that as their default scheduling algorithm. But
1116 * others (with multi-message pipelines) could need a flag to
1117 * tell them about such special cases.
1120 /* Completion is reported through a callback */
1122 void (*complete)(void *context);
1124 unsigned frame_length;
1125 unsigned actual_length;
1127 /* For optional use by whatever driver currently owns the
1128 * spi_message ... between calls to spi_async and then later
1129 * complete(), that's the spi_controller controller driver.
1131 struct list_head queue;
1134 /* List of spi_res reources when the spi message is processed */
1135 struct list_head resources;
1138 static inline void spi_message_init_no_memset(struct spi_message *m)
1140 INIT_LIST_HEAD(&m->transfers);
1141 INIT_LIST_HEAD(&m->resources);
1144 static inline void spi_message_init(struct spi_message *m)
1146 memset(m, 0, sizeof *m);
1147 spi_message_init_no_memset(m);
1151 spi_message_add_tail(struct spi_transfer *t, struct spi_message *m)
1153 list_add_tail(&t->transfer_list, &m->transfers);
1157 spi_transfer_del(struct spi_transfer *t)
1159 list_del(&t->transfer_list);
1163 spi_transfer_delay_exec(struct spi_transfer *t)
1165 return spi_delay_exec(&t->delay, t);
1169 * spi_message_init_with_transfers - Initialize spi_message and append transfers
1170 * @m: spi_message to be initialized
1171 * @xfers: An array of spi transfers
1172 * @num_xfers: Number of items in the xfer array
1174 * This function initializes the given spi_message and adds each spi_transfer in
1175 * the given array to the message.
1178 spi_message_init_with_transfers(struct spi_message *m,
1179 struct spi_transfer *xfers, unsigned int num_xfers)
1183 spi_message_init(m);
1184 for (i = 0; i < num_xfers; ++i)
1185 spi_message_add_tail(&xfers[i], m);
1188 /* It's fine to embed message and transaction structures in other data
1189 * structures so long as you don't free them while they're in use.
1192 static inline struct spi_message *spi_message_alloc(unsigned ntrans, gfp_t flags)
1194 struct spi_message *m;
1196 m = kzalloc(sizeof(struct spi_message)
1197 + ntrans * sizeof(struct spi_transfer),
1201 struct spi_transfer *t = (struct spi_transfer *)(m + 1);
1203 spi_message_init_no_memset(m);
1204 for (i = 0; i < ntrans; i++, t++)
1205 spi_message_add_tail(t, m);
1210 static inline void spi_message_free(struct spi_message *m)
1215 extern int spi_setup(struct spi_device *spi);
1216 extern int spi_async(struct spi_device *spi, struct spi_message *message);
1217 extern int spi_slave_abort(struct spi_device *spi);
1218 extern int spi_target_abort(struct spi_device *spi);
1220 static inline size_t
1221 spi_max_message_size(struct spi_device *spi)
1223 struct spi_controller *ctlr = spi->controller;
1225 if (!ctlr->max_message_size)
1227 return ctlr->max_message_size(spi);
1230 static inline size_t
1231 spi_max_transfer_size(struct spi_device *spi)
1233 struct spi_controller *ctlr = spi->controller;
1234 size_t tr_max = SIZE_MAX;
1235 size_t msg_max = spi_max_message_size(spi);
1237 if (ctlr->max_transfer_size)
1238 tr_max = ctlr->max_transfer_size(spi);
1240 /* Transfer size limit must not be greater than message size limit */
1241 return min(tr_max, msg_max);
1245 * spi_is_bpw_supported - Check if bits per word is supported
1247 * @bpw: Bits per word
1249 * This function checks to see if the SPI controller supports @bpw.
1252 * True if @bpw is supported, false otherwise.
1254 static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw)
1256 u32 bpw_mask = spi->master->bits_per_word_mask;
1258 if (bpw == 8 || (bpw <= 32 && bpw_mask & SPI_BPW_MASK(bpw)))
1264 /*---------------------------------------------------------------------------*/
1266 /* SPI transfer replacement methods which make use of spi_res */
1268 struct spi_replaced_transfers;
1269 typedef void (*spi_replaced_release_t)(struct spi_controller *ctlr,
1270 struct spi_message *msg,
1271 struct spi_replaced_transfers *res);
1273 * struct spi_replaced_transfers - structure describing the spi_transfer
1274 * replacements that have occurred
1275 * so that they can get reverted
1276 * @release: some extra release code to get executed prior to
1277 * relasing this structure
1278 * @extradata: pointer to some extra data if requested or NULL
1279 * @replaced_transfers: transfers that have been replaced and which need
1281 * @replaced_after: the transfer after which the @replaced_transfers
1282 * are to get re-inserted
1283 * @inserted: number of transfers inserted
1284 * @inserted_transfers: array of spi_transfers of array-size @inserted,
1285 * that have been replacing replaced_transfers
1287 * note: that @extradata will point to @inserted_transfers[@inserted]
1288 * if some extra allocation is requested, so alignment will be the same
1289 * as for spi_transfers
1291 struct spi_replaced_transfers {
1292 spi_replaced_release_t release;
1294 struct list_head replaced_transfers;
1295 struct list_head *replaced_after;
1297 struct spi_transfer inserted_transfers[];
1300 /*---------------------------------------------------------------------------*/
1302 /* SPI transfer transformation methods */
1304 extern int spi_split_transfers_maxsize(struct spi_controller *ctlr,
1305 struct spi_message *msg,
1308 extern int spi_split_transfers_maxwords(struct spi_controller *ctlr,
1309 struct spi_message *msg,
1313 /*---------------------------------------------------------------------------*/
1315 /* All these synchronous SPI transfer routines are utilities layered
1316 * over the core async transfer primitive. Here, "synchronous" means
1317 * they will sleep uninterruptibly until the async transfer completes.
1320 extern int spi_sync(struct spi_device *spi, struct spi_message *message);
1321 extern int spi_sync_locked(struct spi_device *spi, struct spi_message *message);
1322 extern int spi_bus_lock(struct spi_controller *ctlr);
1323 extern int spi_bus_unlock(struct spi_controller *ctlr);
1326 * spi_sync_transfer - synchronous SPI data transfer
1327 * @spi: device with which data will be exchanged
1328 * @xfers: An array of spi_transfers
1329 * @num_xfers: Number of items in the xfer array
1330 * Context: can sleep
1332 * Does a synchronous SPI data transfer of the given spi_transfer array.
1334 * For more specific semantics see spi_sync().
1336 * Return: zero on success, else a negative error code.
1339 spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
1340 unsigned int num_xfers)
1342 struct spi_message msg;
1344 spi_message_init_with_transfers(&msg, xfers, num_xfers);
1346 return spi_sync(spi, &msg);
1350 * spi_write - SPI synchronous write
1351 * @spi: device to which data will be written
1353 * @len: data buffer size
1354 * Context: can sleep
1356 * This function writes the buffer @buf.
1357 * Callable only from contexts that can sleep.
1359 * Return: zero on success, else a negative error code.
1362 spi_write(struct spi_device *spi, const void *buf, size_t len)
1364 struct spi_transfer t = {
1369 return spi_sync_transfer(spi, &t, 1);
1373 * spi_read - SPI synchronous read
1374 * @spi: device from which data will be read
1376 * @len: data buffer size
1377 * Context: can sleep
1379 * This function reads the buffer @buf.
1380 * Callable only from contexts that can sleep.
1382 * Return: zero on success, else a negative error code.
1385 spi_read(struct spi_device *spi, void *buf, size_t len)
1387 struct spi_transfer t = {
1392 return spi_sync_transfer(spi, &t, 1);
1395 /* This copies txbuf and rxbuf data; for small transfers only! */
1396 extern int spi_write_then_read(struct spi_device *spi,
1397 const void *txbuf, unsigned n_tx,
1398 void *rxbuf, unsigned n_rx);
1401 * spi_w8r8 - SPI synchronous 8 bit write followed by 8 bit read
1402 * @spi: device with which data will be exchanged
1403 * @cmd: command to be written before data is read back
1404 * Context: can sleep
1406 * Callable only from contexts that can sleep.
1408 * Return: the (unsigned) eight bit number returned by the
1409 * device, or else a negative error code.
1411 static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd)
1416 status = spi_write_then_read(spi, &cmd, 1, &result, 1);
1418 /* Return negative errno or unsigned value */
1419 return (status < 0) ? status : result;
1423 * spi_w8r16 - SPI synchronous 8 bit write followed by 16 bit read
1424 * @spi: device with which data will be exchanged
1425 * @cmd: command to be written before data is read back
1426 * Context: can sleep
1428 * The number is returned in wire-order, which is at least sometimes
1431 * Callable only from contexts that can sleep.
1433 * Return: the (unsigned) sixteen bit number returned by the
1434 * device, or else a negative error code.
1436 static inline ssize_t spi_w8r16(struct spi_device *spi, u8 cmd)
1441 status = spi_write_then_read(spi, &cmd, 1, &result, 2);
1443 /* Return negative errno or unsigned value */
1444 return (status < 0) ? status : result;
1448 * spi_w8r16be - SPI synchronous 8 bit write followed by 16 bit big-endian read
1449 * @spi: device with which data will be exchanged
1450 * @cmd: command to be written before data is read back
1451 * Context: can sleep
1453 * This function is similar to spi_w8r16, with the exception that it will
1454 * convert the read 16 bit data word from big-endian to native endianness.
1456 * Callable only from contexts that can sleep.
1458 * Return: the (unsigned) sixteen bit number returned by the device in cpu
1459 * endianness, or else a negative error code.
1461 static inline ssize_t spi_w8r16be(struct spi_device *spi, u8 cmd)
1467 status = spi_write_then_read(spi, &cmd, 1, &result, 2);
1471 return be16_to_cpu(result);
1474 /*---------------------------------------------------------------------------*/
1477 * INTERFACE between board init code and SPI infrastructure.
1479 * No SPI driver ever sees these SPI device table segments, but
1480 * it's how the SPI core (or adapters that get hotplugged) grows
1481 * the driver model tree.
1483 * As a rule, SPI devices can't be probed. Instead, board init code
1484 * provides a table listing the devices which are present, with enough
1485 * information to bind and set up the device's driver. There's basic
1486 * support for nonstatic configurations too; enough to handle adding
1487 * parport adapters, or microcontrollers acting as USB-to-SPI bridges.
1491 * struct spi_board_info - board-specific template for a SPI device
1492 * @modalias: Initializes spi_device.modalias; identifies the driver.
1493 * @platform_data: Initializes spi_device.platform_data; the particular
1494 * data stored there is driver-specific.
1495 * @swnode: Software node for the device.
1496 * @controller_data: Initializes spi_device.controller_data; some
1497 * controllers need hints about hardware setup, e.g. for DMA.
1498 * @irq: Initializes spi_device.irq; depends on how the board is wired.
1499 * @max_speed_hz: Initializes spi_device.max_speed_hz; based on limits
1500 * from the chip datasheet and board-specific signal quality issues.
1501 * @bus_num: Identifies which spi_controller parents the spi_device; unused
1502 * by spi_new_device(), and otherwise depends on board wiring.
1503 * @chip_select: Initializes spi_device.chip_select; depends on how
1504 * the board is wired.
1505 * @mode: Initializes spi_device.mode; based on the chip datasheet, board
1506 * wiring (some devices support both 3WIRE and standard modes), and
1507 * possibly presence of an inverter in the chipselect path.
1509 * When adding new SPI devices to the device tree, these structures serve
1510 * as a partial device template. They hold information which can't always
1511 * be determined by drivers. Information that probe() can establish (such
1512 * as the default transfer wordsize) is not included here.
1514 * These structures are used in two places. Their primary role is to
1515 * be stored in tables of board-specific device descriptors, which are
1516 * declared early in board initialization and then used (much later) to
1517 * populate a controller's device tree after the that controller's driver
1518 * initializes. A secondary (and atypical) role is as a parameter to
1519 * spi_new_device() call, which happens after those controller drivers
1520 * are active in some dynamic board configuration models.
1522 struct spi_board_info {
1523 /* The device name and module name are coupled, like platform_bus;
1524 * "modalias" is normally the driver name.
1526 * platform_data goes to spi_device.dev.platform_data,
1527 * controller_data goes to spi_device.controller_data,
1530 char modalias[SPI_NAME_SIZE];
1531 const void *platform_data;
1532 const struct software_node *swnode;
1533 void *controller_data;
1536 /* Slower signaling on noisy or low voltage boards */
1540 /* bus_num is board specific and matches the bus_num of some
1541 * spi_controller that will probably be registered later.
1543 * chip_select reflects how this chip is wired to that master;
1544 * it's less than num_chipselect.
1549 /* mode becomes spi_device.mode, and is essential for chips
1550 * where the default of SPI_CS_HIGH = 0 is wrong.
1554 /* ... may need additional spi_device chip config data here.
1555 * avoid stuff protocol drivers can set; but include stuff
1556 * needed to behave without being bound to a driver:
1557 * - quirks like clock rate mattering when not selected
1563 spi_register_board_info(struct spi_board_info const *info, unsigned n);
1565 /* Board init code may ignore whether SPI is configured or not */
1567 spi_register_board_info(struct spi_board_info const *info, unsigned n)
1571 /* If you're hotplugging an adapter with devices (parport, usb, etc)
1572 * use spi_new_device() to describe each device. You can also call
1573 * spi_unregister_device() to start making that device vanish, but
1574 * normally that would be handled by spi_unregister_controller().
1576 * You can also use spi_alloc_device() and spi_add_device() to use a two
1577 * stage registration sequence for each spi_device. This gives the caller
1578 * some more control over the spi_device structure before it is registered,
1579 * but requires that caller to initialize fields that would otherwise
1580 * be defined using the board info.
1582 extern struct spi_device *
1583 spi_alloc_device(struct spi_controller *ctlr);
1586 spi_add_device(struct spi_device *spi);
1588 extern struct spi_device *
1589 spi_new_device(struct spi_controller *, struct spi_board_info *);
1591 extern void spi_unregister_device(struct spi_device *spi);
1593 extern const struct spi_device_id *
1594 spi_get_device_id(const struct spi_device *sdev);
1597 spi_get_device_match_data(const struct spi_device *sdev);
1600 spi_transfer_is_last(struct spi_controller *ctlr, struct spi_transfer *xfer)
1602 return list_is_last(&xfer->transfer_list, &ctlr->cur_msg->transfers);
1605 /* Compatibility layer */
1606 #define spi_master spi_controller
1608 #define SPI_MASTER_HALF_DUPLEX SPI_CONTROLLER_HALF_DUPLEX
1609 #define SPI_MASTER_NO_RX SPI_CONTROLLER_NO_RX
1610 #define SPI_MASTER_NO_TX SPI_CONTROLLER_NO_TX
1611 #define SPI_MASTER_MUST_RX SPI_CONTROLLER_MUST_RX
1612 #define SPI_MASTER_MUST_TX SPI_CONTROLLER_MUST_TX
1614 #define spi_master_get_devdata(_ctlr) spi_controller_get_devdata(_ctlr)
1615 #define spi_master_set_devdata(_ctlr, _data) \
1616 spi_controller_set_devdata(_ctlr, _data)
1617 #define spi_master_get(_ctlr) spi_controller_get(_ctlr)
1618 #define spi_master_put(_ctlr) spi_controller_put(_ctlr)
1619 #define spi_master_suspend(_ctlr) spi_controller_suspend(_ctlr)
1620 #define spi_master_resume(_ctlr) spi_controller_resume(_ctlr)
1622 #define spi_register_master(_ctlr) spi_register_controller(_ctlr)
1623 #define devm_spi_register_master(_dev, _ctlr) \
1624 devm_spi_register_controller(_dev, _ctlr)
1625 #define spi_unregister_master(_ctlr) spi_unregister_controller(_ctlr)
1627 #endif /* __LINUX_SPI_H */