1  /* SPDX-License-Identifier: GPL-2.0-or-later
2   *
3   * Copyright (C) 2005 David Brownell
4   */
5  
6  #ifndef __LINUX_SPI_H
7  #define __LINUX_SPI_H
8  
9  #include <linux/acpi.h>
10  #include <linux/bits.h>
11  #include <linux/completion.h>
12  #include <linux/device.h>
13  #include <linux/gpio/consumer.h>
14  #include <linux/kthread.h>
15  #include <linux/mod_devicetable.h>
16  #include <linux/overflow.h>
17  #include <linux/scatterlist.h>
18  #include <linux/slab.h>
19  #include <linux/u64_stats_sync.h>
20  
21  #include <uapi/linux/spi/spi.h>
22  
23  /* Max no. of CS supported per spi device */
24  #define SPI_CS_CNT_MAX 16
25  
26  struct dma_chan;
27  struct software_node;
28  struct ptp_system_timestamp;
29  struct spi_controller;
30  struct spi_transfer;
31  struct spi_controller_mem_ops;
32  struct spi_controller_mem_caps;
33  struct spi_message;
34  
35  /*
36   * INTERFACES between SPI master-side drivers and SPI slave protocol handlers,
37   * and SPI infrastructure.
38   */
39  extern const struct bus_type spi_bus_type;
40  
41  /**
42   * struct spi_statistics - statistics for spi transfers
43   * @syncp:         seqcount to protect members in this struct for per-cpu update
44   *                 on 32-bit systems
45   *
46   * @messages:      number of spi-messages handled
47   * @transfers:     number of spi_transfers handled
48   * @errors:        number of errors during spi_transfer
49   * @timedout:      number of timeouts during spi_transfer
50   *
51   * @spi_sync:      number of times spi_sync is used
52   * @spi_sync_immediate:
53   *                 number of times spi_sync is executed immediately
54   *                 in calling context without queuing and scheduling
55   * @spi_async:     number of times spi_async is used
56   *
57   * @bytes:         number of bytes transferred to/from device
58   * @bytes_tx:      number of bytes sent to device
59   * @bytes_rx:      number of bytes received from device
60   *
61   * @transfer_bytes_histo:
62   *                 transfer bytes histogram
63   *
64   * @transfers_split_maxsize:
65   *                 number of transfers that have been split because of
66   *                 maxsize limit
67   */
68  struct spi_statistics {
69  	struct u64_stats_sync	syncp;
70  
71  	u64_stats_t		messages;
72  	u64_stats_t		transfers;
73  	u64_stats_t		errors;
74  	u64_stats_t		timedout;
75  
76  	u64_stats_t		spi_sync;
77  	u64_stats_t		spi_sync_immediate;
78  	u64_stats_t		spi_async;
79  
80  	u64_stats_t		bytes;
81  	u64_stats_t		bytes_rx;
82  	u64_stats_t		bytes_tx;
83  
84  #define SPI_STATISTICS_HISTO_SIZE 17
85  	u64_stats_t	transfer_bytes_histo[SPI_STATISTICS_HISTO_SIZE];
86  
87  	u64_stats_t	transfers_split_maxsize;
88  };
89  
90  #define SPI_STATISTICS_ADD_TO_FIELD(pcpu_stats, field, count)		\
91  	do {								\
92  		struct spi_statistics *__lstats;			\
93  		get_cpu();						\
94  		__lstats = this_cpu_ptr(pcpu_stats);			\
95  		u64_stats_update_begin(&__lstats->syncp);		\
96  		u64_stats_add(&__lstats->field, count);			\
97  		u64_stats_update_end(&__lstats->syncp);			\
98  		put_cpu();						\
99  	} while (0)
100  
101  #define SPI_STATISTICS_INCREMENT_FIELD(pcpu_stats, field)		\
102  	do {								\
103  		struct spi_statistics *__lstats;			\
104  		get_cpu();						\
105  		__lstats = this_cpu_ptr(pcpu_stats);			\
106  		u64_stats_update_begin(&__lstats->syncp);		\
107  		u64_stats_inc(&__lstats->field);			\
108  		u64_stats_update_end(&__lstats->syncp);			\
109  		put_cpu();						\
110  	} while (0)
111  
112  /**
113   * struct spi_delay - SPI delay information
114   * @value: Value for the delay
115   * @unit: Unit for the delay
116   */
117  struct spi_delay {
118  #define SPI_DELAY_UNIT_USECS	0
119  #define SPI_DELAY_UNIT_NSECS	1
120  #define SPI_DELAY_UNIT_SCK	2
121  	u16	value;
122  	u8	unit;
123  };
124  
125  extern int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer);
126  extern int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer);
127  extern void spi_transfer_cs_change_delay_exec(struct spi_message *msg,
128  						  struct spi_transfer *xfer);
129  
130  /**
131   * struct spi_device - Controller side proxy for an SPI slave device
132   * @dev: Driver model representation of the device.
133   * @controller: SPI controller used with the device.
134   * @max_speed_hz: Maximum clock rate to be used with this chip
135   *	(on this board); may be changed by the device's driver.
136   *	The spi_transfer.speed_hz can override this for each transfer.
137   * @chip_select: Array of physical chipselect, spi->chipselect[i] gives
138   *	the corresponding physical CS for logical CS i.
139   * @mode: The spi mode defines how data is clocked out and in.
140   *	This may be changed by the device's driver.
141   *	The "active low" default for chipselect mode can be overridden
142   *	(by specifying SPI_CS_HIGH) as can the "MSB first" default for
143   *	each word in a transfer (by specifying SPI_LSB_FIRST).
144   * @bits_per_word: Data transfers involve one or more words; word sizes
145   *	like eight or 12 bits are common.  In-memory wordsizes are
146   *	powers of two bytes (e.g. 20 bit samples use 32 bits).
147   *	This may be changed by the device's driver, or left at the
148   *	default (0) indicating protocol words are eight bit bytes.
149   *	The spi_transfer.bits_per_word can override this for each transfer.
150   * @rt: Make the pump thread real time priority.
151   * @irq: Negative, or the number passed to request_irq() to receive
152   *	interrupts from this device.
153   * @controller_state: Controller's runtime state
154   * @controller_data: Board-specific definitions for controller, such as
155   *	FIFO initialization parameters; from board_info.controller_data
156   * @modalias: Name of the driver to use with this device, or an alias
157   *	for that name.  This appears in the sysfs "modalias" attribute
158   *	for driver coldplugging, and in uevents used for hotplugging
159   * @driver_override: If the name of a driver is written to this attribute, then
160   *	the device will bind to the named driver and only the named driver.
161   *	Do not set directly, because core frees it; use driver_set_override() to
162   *	set or clear it.
163   * @cs_gpiod: Array of GPIO descriptors of the corresponding chipselect lines
164   *	(optional, NULL when not using a GPIO line)
165   * @word_delay: delay to be inserted between consecutive
166   *	words of a transfer
167   * @cs_setup: delay to be introduced by the controller after CS is asserted
168   * @cs_hold: delay to be introduced by the controller before CS is deasserted
169   * @cs_inactive: delay to be introduced by the controller after CS is
170   *	deasserted. If @cs_change_delay is used from @spi_transfer, then the
171   *	two delays will be added up.
172   * @pcpu_statistics: statistics for the spi_device
173   * @cs_index_mask: Bit mask of the active chipselect(s) in the chipselect array
174   *
175   * A @spi_device is used to interchange data between an SPI slave
176   * (usually a discrete chip) and CPU memory.
177   *
178   * In @dev, the platform_data is used to hold information about this
179   * device that's meaningful to the device's protocol driver, but not
180   * to its controller.  One example might be an identifier for a chip
181   * variant with slightly different functionality; another might be
182   * information about how this particular board wires the chip's pins.
183   */
184  struct spi_device {
185  	struct device		dev;
186  	struct spi_controller	*controller;
187  	u32			max_speed_hz;
188  	u8			chip_select[SPI_CS_CNT_MAX];
189  	u8			bits_per_word;
190  	bool			rt;
191  #define SPI_NO_TX		BIT(31)		/* No transmit wire */
192  #define SPI_NO_RX		BIT(30)		/* No receive wire */
193  	/*
194  	 * TPM specification defines flow control over SPI. Client device
195  	 * can insert a wait state on MISO when address is transmitted by
196  	 * controller on MOSI. Detecting the wait state in software is only
197  	 * possible for full duplex controllers. For controllers that support
198  	 * only half-duplex, the wait state detection needs to be implemented
199  	 * in hardware. TPM devices would set this flag when hardware flow
200  	 * control is expected from SPI controller.
201  	 */
202  #define SPI_TPM_HW_FLOW		BIT(29)		/* TPM HW flow control */
203  	/*
204  	 * All bits defined above should be covered by SPI_MODE_KERNEL_MASK.
205  	 * The SPI_MODE_KERNEL_MASK has the SPI_MODE_USER_MASK counterpart,
206  	 * which is defined in 'include/uapi/linux/spi/spi.h'.
207  	 * The bits defined here are from bit 31 downwards, while in
208  	 * SPI_MODE_USER_MASK are from 0 upwards.
209  	 * These bits must not overlap. A static assert check should make sure of that.
210  	 * If adding extra bits, make sure to decrease the bit index below as well.
211  	 */
212  #define SPI_MODE_KERNEL_MASK	(~(BIT(29) - 1))
213  	u32			mode;
214  	int			irq;
215  	void			*controller_state;
216  	void			*controller_data;
217  	char			modalias[SPI_NAME_SIZE];
218  	const char		*driver_override;
219  	struct gpio_desc	*cs_gpiod[SPI_CS_CNT_MAX];	/* Chip select gpio desc */
220  	struct spi_delay	word_delay; /* Inter-word delay */
221  	/* CS delays */
222  	struct spi_delay	cs_setup;
223  	struct spi_delay	cs_hold;
224  	struct spi_delay	cs_inactive;
225  
226  	/* The statistics */
227  	struct spi_statistics __percpu	*pcpu_statistics;
228  
229  	/* Bit mask of the chipselect(s) that the driver need to use from
230  	 * the chipselect array.When the controller is capable to handle
231  	 * multiple chip selects & memories are connected in parallel
232  	 * then more than one bit need to be set in cs_index_mask.
233  	 */
234  	u32			cs_index_mask : SPI_CS_CNT_MAX;
235  
236  	/*
237  	 * Likely need more hooks for more protocol options affecting how
238  	 * the controller talks to each chip, like:
239  	 *  - memory packing (12 bit samples into low bits, others zeroed)
240  	 *  - priority
241  	 *  - chipselect delays
242  	 *  - ...
243  	 */
244  };
245  
246  /* Make sure that SPI_MODE_KERNEL_MASK & SPI_MODE_USER_MASK don't overlap */
247  static_assert((SPI_MODE_KERNEL_MASK & SPI_MODE_USER_MASK) == 0,
248  	      "SPI_MODE_USER_MASK & SPI_MODE_KERNEL_MASK must not overlap");
249  
to_spi_device(const struct device * dev)250  static inline struct spi_device *to_spi_device(const struct device *dev)
251  {
252  	return dev ? container_of(dev, struct spi_device, dev) : NULL;
253  }
254  
255  /* Most drivers won't need to care about device refcounting */
spi_dev_get(struct spi_device * spi)256  static inline struct spi_device *spi_dev_get(struct spi_device *spi)
257  {
258  	return (spi && get_device(&spi->dev)) ? spi : NULL;
259  }
260  
spi_dev_put(struct spi_device * spi)261  static inline void spi_dev_put(struct spi_device *spi)
262  {
263  	if (spi)
264  		put_device(&spi->dev);
265  }
266  
267  /* ctldata is for the bus_controller driver's runtime state */
spi_get_ctldata(const struct spi_device * spi)268  static inline void *spi_get_ctldata(const struct spi_device *spi)
269  {
270  	return spi->controller_state;
271  }
272  
spi_set_ctldata(struct spi_device * spi,void * state)273  static inline void spi_set_ctldata(struct spi_device *spi, void *state)
274  {
275  	spi->controller_state = state;
276  }
277  
278  /* Device driver data */
279  
spi_set_drvdata(struct spi_device * spi,void * data)280  static inline void spi_set_drvdata(struct spi_device *spi, void *data)
281  {
282  	dev_set_drvdata(&spi->dev, data);
283  }
284  
spi_get_drvdata(const struct spi_device * spi)285  static inline void *spi_get_drvdata(const struct spi_device *spi)
286  {
287  	return dev_get_drvdata(&spi->dev);
288  }
289  
spi_get_chipselect(const struct spi_device * spi,u8 idx)290  static inline u8 spi_get_chipselect(const struct spi_device *spi, u8 idx)
291  {
292  	return spi->chip_select[idx];
293  }
294  
spi_set_chipselect(struct spi_device * spi,u8 idx,u8 chipselect)295  static inline void spi_set_chipselect(struct spi_device *spi, u8 idx, u8 chipselect)
296  {
297  	spi->chip_select[idx] = chipselect;
298  }
299  
spi_get_csgpiod(const struct spi_device * spi,u8 idx)300  static inline struct gpio_desc *spi_get_csgpiod(const struct spi_device *spi, u8 idx)
301  {
302  	return spi->cs_gpiod[idx];
303  }
304  
spi_set_csgpiod(struct spi_device * spi,u8 idx,struct gpio_desc * csgpiod)305  static inline void spi_set_csgpiod(struct spi_device *spi, u8 idx, struct gpio_desc *csgpiod)
306  {
307  	spi->cs_gpiod[idx] = csgpiod;
308  }
309  
spi_is_csgpiod(struct spi_device * spi)310  static inline bool spi_is_csgpiod(struct spi_device *spi)
311  {
312  	u8 idx;
313  
314  	for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
315  		if (spi_get_csgpiod(spi, idx))
316  			return true;
317  	}
318  	return false;
319  }
320  
321  /**
322   * struct spi_driver - Host side "protocol" driver
323   * @id_table: List of SPI devices supported by this driver
324   * @probe: Binds this driver to the SPI device.  Drivers can verify
325   *	that the device is actually present, and may need to configure
326   *	characteristics (such as bits_per_word) which weren't needed for
327   *	the initial configuration done during system setup.
328   * @remove: Unbinds this driver from the SPI device
329   * @shutdown: Standard shutdown callback used during system state
330   *	transitions such as powerdown/halt and kexec
331   * @driver: SPI device drivers should initialize the name and owner
332   *	field of this structure.
333   *
334   * This represents the kind of device driver that uses SPI messages to
335   * interact with the hardware at the other end of a SPI link.  It's called
336   * a "protocol" driver because it works through messages rather than talking
337   * directly to SPI hardware (which is what the underlying SPI controller
338   * driver does to pass those messages).  These protocols are defined in the
339   * specification for the device(s) supported by the driver.
340   *
341   * As a rule, those device protocols represent the lowest level interface
342   * supported by a driver, and it will support upper level interfaces too.
343   * Examples of such upper levels include frameworks like MTD, networking,
344   * MMC, RTC, filesystem character device nodes, and hardware monitoring.
345   */
346  struct spi_driver {
347  	const struct spi_device_id *id_table;
348  	int			(*probe)(struct spi_device *spi);
349  	void			(*remove)(struct spi_device *spi);
350  	void			(*shutdown)(struct spi_device *spi);
351  	struct device_driver	driver;
352  };
353  
354  #define to_spi_driver(__drv)   \
355  	( __drv ? container_of_const(__drv, struct spi_driver, driver) : NULL )
356  
357  extern int __spi_register_driver(struct module *owner, struct spi_driver *sdrv);
358  
359  /**
360   * spi_unregister_driver - reverse effect of spi_register_driver
361   * @sdrv: the driver to unregister
362   * Context: can sleep
363   */
spi_unregister_driver(struct spi_driver * sdrv)364  static inline void spi_unregister_driver(struct spi_driver *sdrv)
365  {
366  	if (sdrv)
367  		driver_unregister(&sdrv->driver);
368  }
369  
370  extern struct spi_device *spi_new_ancillary_device(struct spi_device *spi, u8 chip_select);
371  
372  /* Use a define to avoid include chaining to get THIS_MODULE */
373  #define spi_register_driver(driver) \
374  	__spi_register_driver(THIS_MODULE, driver)
375  
376  /**
377   * module_spi_driver() - Helper macro for registering a SPI driver
378   * @__spi_driver: spi_driver struct
379   *
380   * Helper macro for SPI drivers which do not do anything special in module
381   * init/exit. This eliminates a lot of boilerplate. Each module may only
382   * use this macro once, and calling it replaces module_init() and module_exit()
383   */
384  #define module_spi_driver(__spi_driver) \
385  	module_driver(__spi_driver, spi_register_driver, \
386  			spi_unregister_driver)
387  
388  /**
389   * struct spi_controller - interface to SPI master or slave controller
390   * @dev: device interface to this driver
391   * @list: link with the global spi_controller list
392   * @bus_num: board-specific (and often SOC-specific) identifier for a
393   *	given SPI controller.
394   * @num_chipselect: chipselects are used to distinguish individual
395   *	SPI slaves, and are numbered from zero to num_chipselects.
396   *	each slave has a chipselect signal, but it's common that not
397   *	every chipselect is connected to a slave.
398   * @dma_alignment: SPI controller constraint on DMA buffers alignment.
399   * @mode_bits: flags understood by this controller driver
400   * @buswidth_override_bits: flags to override for this controller driver
401   * @bits_per_word_mask: A mask indicating which values of bits_per_word are
402   *	supported by the driver. Bit n indicates that a bits_per_word n+1 is
403   *	supported. If set, the SPI core will reject any transfer with an
404   *	unsupported bits_per_word. If not set, this value is simply ignored,
405   *	and it's up to the individual driver to perform any validation.
406   * @min_speed_hz: Lowest supported transfer speed
407   * @max_speed_hz: Highest supported transfer speed
408   * @flags: other constraints relevant to this driver
409   * @slave: indicates that this is an SPI slave controller
410   * @target: indicates that this is an SPI target controller
411   * @devm_allocated: whether the allocation of this struct is devres-managed
412   * @max_transfer_size: function that returns the max transfer size for
413   *	a &spi_device; may be %NULL, so the default %SIZE_MAX will be used.
414   * @max_message_size: function that returns the max message size for
415   *	a &spi_device; may be %NULL, so the default %SIZE_MAX will be used.
416   * @io_mutex: mutex for physical bus access
417   * @add_lock: mutex to avoid adding devices to the same chipselect
418   * @bus_lock_spinlock: spinlock for SPI bus locking
419   * @bus_lock_mutex: mutex for exclusion of multiple callers
420   * @bus_lock_flag: indicates that the SPI bus is locked for exclusive use
421   * @setup: updates the device mode and clocking records used by a
422   *	device's SPI controller; protocol code may call this.  This
423   *	must fail if an unrecognized or unsupported mode is requested.
424   *	It's always safe to call this unless transfers are pending on
425   *	the device whose settings are being modified.
426   * @set_cs_timing: optional hook for SPI devices to request SPI master
427   * controller for configuring specific CS setup time, hold time and inactive
428   * delay interms of clock counts
429   * @transfer: adds a message to the controller's transfer queue.
430   * @cleanup: frees controller-specific state
431   * @can_dma: determine whether this controller supports DMA
432   * @dma_map_dev: device which can be used for DMA mapping
433   * @cur_rx_dma_dev: device which is currently used for RX DMA mapping
434   * @cur_tx_dma_dev: device which is currently used for TX DMA mapping
435   * @queued: whether this controller is providing an internal message queue
436   * @kworker: pointer to thread struct for message pump
437   * @pump_messages: work struct for scheduling work to the message pump
438   * @queue_lock: spinlock to synchronise access to message queue
439   * @queue: message queue
440   * @cur_msg: the currently in-flight message
441   * @cur_msg_completion: a completion for the current in-flight message
442   * @cur_msg_incomplete: Flag used internally to opportunistically skip
443   *	the @cur_msg_completion. This flag is used to check if the driver has
444   *	already called spi_finalize_current_message().
445   * @cur_msg_need_completion: Flag used internally to opportunistically skip
446   *	the @cur_msg_completion. This flag is used to signal the context that
447   *	is running spi_finalize_current_message() that it needs to complete()
448   * @fallback: fallback to PIO if DMA transfer return failure with
449   *	SPI_TRANS_FAIL_NO_START.
450   * @last_cs_mode_high: was (mode & SPI_CS_HIGH) true on the last call to set_cs.
451   * @last_cs: the last chip_select that is recorded by set_cs, -1 on non chip
452   *           selected
453   * @last_cs_index_mask: bit mask the last chip selects that were used
454   * @xfer_completion: used by core transfer_one_message()
455   * @busy: message pump is busy
456   * @running: message pump is running
457   * @rt: whether this queue is set to run as a realtime task
458   * @auto_runtime_pm: the core should ensure a runtime PM reference is held
459   *                   while the hardware is prepared, using the parent
460   *                   device for the spidev
461   * @max_dma_len: Maximum length of a DMA transfer for the device.
462   * @prepare_transfer_hardware: a message will soon arrive from the queue
463   *	so the subsystem requests the driver to prepare the transfer hardware
464   *	by issuing this call
465   * @transfer_one_message: the subsystem calls the driver to transfer a single
466   *	message while queuing transfers that arrive in the meantime. When the
467   *	driver is finished with this message, it must call
468   *	spi_finalize_current_message() so the subsystem can issue the next
469   *	message
470   * @unprepare_transfer_hardware: there are currently no more messages on the
471   *	queue so the subsystem notifies the driver that it may relax the
472   *	hardware by issuing this call
473   *
474   * @set_cs: set the logic level of the chip select line.  May be called
475   *          from interrupt context.
476   * @optimize_message: optimize the message for reuse
477   * @unoptimize_message: release resources allocated by optimize_message
478   * @prepare_message: set up the controller to transfer a single message,
479   *                   for example doing DMA mapping.  Called from threaded
480   *                   context.
481   * @transfer_one: transfer a single spi_transfer.
482   *
483   *                  - return 0 if the transfer is finished,
484   *                  - return 1 if the transfer is still in progress. When
485   *                    the driver is finished with this transfer it must
486   *                    call spi_finalize_current_transfer() so the subsystem
487   *                    can issue the next transfer. If the transfer fails, the
488   *                    driver must set the flag SPI_TRANS_FAIL_IO to
489   *                    spi_transfer->error first, before calling
490   *                    spi_finalize_current_transfer().
491   *                    Note: transfer_one and transfer_one_message are mutually
492   *                    exclusive; when both are set, the generic subsystem does
493   *                    not call your transfer_one callback.
494   * @handle_err: the subsystem calls the driver to handle an error that occurs
495   *		in the generic implementation of transfer_one_message().
496   * @mem_ops: optimized/dedicated operations for interactions with SPI memory.
497   *	     This field is optional and should only be implemented if the
498   *	     controller has native support for memory like operations.
499   * @mem_caps: controller capabilities for the handling of memory operations.
500   * @unprepare_message: undo any work done by prepare_message().
501   * @target_abort: abort the ongoing transfer request on an SPI target controller
502   * @cs_gpiods: Array of GPIO descriptors to use as chip select lines; one per CS
503   *	number. Any individual value may be NULL for CS lines that
504   *	are not GPIOs (driven by the SPI controller itself).
505   * @use_gpio_descriptors: Turns on the code in the SPI core to parse and grab
506   *	GPIO descriptors. This will fill in @cs_gpiods and SPI devices will have
507   *	the cs_gpiod assigned if a GPIO line is found for the chipselect.
508   * @unused_native_cs: When cs_gpiods is used, spi_register_controller() will
509   *	fill in this field with the first unused native CS, to be used by SPI
510   *	controller drivers that need to drive a native CS when using GPIO CS.
511   * @max_native_cs: When cs_gpiods is used, and this field is filled in,
512   *	spi_register_controller() will validate all native CS (including the
513   *	unused native CS) against this value.
514   * @pcpu_statistics: statistics for the spi_controller
515   * @dma_tx: DMA transmit channel
516   * @dma_rx: DMA receive channel
517   * @dummy_rx: dummy receive buffer for full-duplex devices
518   * @dummy_tx: dummy transmit buffer for full-duplex devices
519   * @fw_translate_cs: If the boot firmware uses different numbering scheme
520   *	what Linux expects, this optional hook can be used to translate
521   *	between the two.
522   * @ptp_sts_supported: If the driver sets this to true, it must provide a
523   *	time snapshot in @spi_transfer->ptp_sts as close as possible to the
524   *	moment in time when @spi_transfer->ptp_sts_word_pre and
525   *	@spi_transfer->ptp_sts_word_post were transmitted.
526   *	If the driver does not set this, the SPI core takes the snapshot as
527   *	close to the driver hand-over as possible.
528   * @irq_flags: Interrupt enable state during PTP system timestamping
529   * @queue_empty: signal green light for opportunistically skipping the queue
530   *	for spi_sync transfers.
531   * @must_async: disable all fast paths in the core
532   * @defer_optimize_message: set to true if controller cannot pre-optimize messages
533   *	and needs to defer the optimization step until the message is actually
534   *	being transferred
535   *
536   * Each SPI controller can communicate with one or more @spi_device
537   * children.  These make a small bus, sharing MOSI, MISO and SCK signals
538   * but not chip select signals.  Each device may be configured to use a
539   * different clock rate, since those shared signals are ignored unless
540   * the chip is selected.
541   *
542   * The driver for an SPI controller manages access to those devices through
543   * a queue of spi_message transactions, copying data between CPU memory and
544   * an SPI slave device.  For each such message it queues, it calls the
545   * message's completion function when the transaction completes.
546   */
547  struct spi_controller {
548  	struct device	dev;
549  
550  	struct list_head list;
551  
552  	/*
553  	 * Other than negative (== assign one dynamically), bus_num is fully
554  	 * board-specific. Usually that simplifies to being SoC-specific.
555  	 * example: one SoC has three SPI controllers, numbered 0..2,
556  	 * and one board's schematics might show it using SPI-2. Software
557  	 * would normally use bus_num=2 for that controller.
558  	 */
559  	s16			bus_num;
560  
561  	/*
562  	 * Chipselects will be integral to many controllers; some others
563  	 * might use board-specific GPIOs.
564  	 */
565  	u16			num_chipselect;
566  
567  	/* Some SPI controllers pose alignment requirements on DMAable
568  	 * buffers; let protocol drivers know about these requirements.
569  	 */
570  	u16			dma_alignment;
571  
572  	/* spi_device.mode flags understood by this controller driver */
573  	u32			mode_bits;
574  
575  	/* spi_device.mode flags override flags for this controller */
576  	u32			buswidth_override_bits;
577  
578  	/* Bitmask of supported bits_per_word for transfers */
579  	u32			bits_per_word_mask;
580  #define SPI_BPW_MASK(bits) BIT((bits) - 1)
581  #define SPI_BPW_RANGE_MASK(min, max) GENMASK((max) - 1, (min) - 1)
582  
583  	/* Limits on transfer speed */
584  	u32			min_speed_hz;
585  	u32			max_speed_hz;
586  
587  	/* Other constraints relevant to this driver */
588  	u16			flags;
589  #define SPI_CONTROLLER_HALF_DUPLEX	BIT(0)	/* Can't do full duplex */
590  #define SPI_CONTROLLER_NO_RX		BIT(1)	/* Can't do buffer read */
591  #define SPI_CONTROLLER_NO_TX		BIT(2)	/* Can't do buffer write */
592  #define SPI_CONTROLLER_MUST_RX		BIT(3)	/* Requires rx */
593  #define SPI_CONTROLLER_MUST_TX		BIT(4)	/* Requires tx */
594  #define SPI_CONTROLLER_GPIO_SS		BIT(5)	/* GPIO CS must select slave */
595  #define SPI_CONTROLLER_SUSPENDED	BIT(6)	/* Currently suspended */
596  	/*
597  	 * The spi-controller has multi chip select capability and can
598  	 * assert/de-assert more than one chip select at once.
599  	 */
600  #define SPI_CONTROLLER_MULTI_CS		BIT(7)
601  
602  	/* Flag indicating if the allocation of this struct is devres-managed */
603  	bool			devm_allocated;
604  
605  	union {
606  		/* Flag indicating this is an SPI slave controller */
607  		bool			slave;
608  		/* Flag indicating this is an SPI target controller */
609  		bool			target;
610  	};
611  
612  	/*
613  	 * On some hardware transfer / message size may be constrained
614  	 * the limit may depend on device transfer settings.
615  	 */
616  	size_t (*max_transfer_size)(struct spi_device *spi);
617  	size_t (*max_message_size)(struct spi_device *spi);
618  
619  	/* I/O mutex */
620  	struct mutex		io_mutex;
621  
622  	/* Used to avoid adding the same CS twice */
623  	struct mutex		add_lock;
624  
625  	/* Lock and mutex for SPI bus locking */
626  	spinlock_t		bus_lock_spinlock;
627  	struct mutex		bus_lock_mutex;
628  
629  	/* Flag indicating that the SPI bus is locked for exclusive use */
630  	bool			bus_lock_flag;
631  
632  	/*
633  	 * Setup mode and clock, etc (SPI driver may call many times).
634  	 *
635  	 * IMPORTANT:  this may be called when transfers to another
636  	 * device are active.  DO NOT UPDATE SHARED REGISTERS in ways
637  	 * which could break those transfers.
638  	 */
639  	int			(*setup)(struct spi_device *spi);
640  
641  	/*
642  	 * set_cs_timing() method is for SPI controllers that supports
643  	 * configuring CS timing.
644  	 *
645  	 * This hook allows SPI client drivers to request SPI controllers
646  	 * to configure specific CS timing through spi_set_cs_timing() after
647  	 * spi_setup().
648  	 */
649  	int (*set_cs_timing)(struct spi_device *spi);
650  
651  	/*
652  	 * Bidirectional bulk transfers
653  	 *
654  	 * + The transfer() method may not sleep; its main role is
655  	 *   just to add the message to the queue.
656  	 * + For now there's no remove-from-queue operation, or
657  	 *   any other request management
658  	 * + To a given spi_device, message queueing is pure FIFO
659  	 *
660  	 * + The controller's main job is to process its message queue,
661  	 *   selecting a chip (for masters), then transferring data
662  	 * + If there are multiple spi_device children, the i/o queue
663  	 *   arbitration algorithm is unspecified (round robin, FIFO,
664  	 *   priority, reservations, preemption, etc)
665  	 *
666  	 * + Chipselect stays active during the entire message
667  	 *   (unless modified by spi_transfer.cs_change != 0).
668  	 * + The message transfers use clock and SPI mode parameters
669  	 *   previously established by setup() for this device
670  	 */
671  	int			(*transfer)(struct spi_device *spi,
672  						struct spi_message *mesg);
673  
674  	/* Called on release() to free memory provided by spi_controller */
675  	void			(*cleanup)(struct spi_device *spi);
676  
677  	/*
678  	 * Used to enable core support for DMA handling, if can_dma()
679  	 * exists and returns true then the transfer will be mapped
680  	 * prior to transfer_one() being called.  The driver should
681  	 * not modify or store xfer and dma_tx and dma_rx must be set
682  	 * while the device is prepared.
683  	 */
684  	bool			(*can_dma)(struct spi_controller *ctlr,
685  					   struct spi_device *spi,
686  					   struct spi_transfer *xfer);
687  	struct device *dma_map_dev;
688  	struct device *cur_rx_dma_dev;
689  	struct device *cur_tx_dma_dev;
690  
691  	/*
692  	 * These hooks are for drivers that want to use the generic
693  	 * controller transfer queueing mechanism. If these are used, the
694  	 * transfer() function above must NOT be specified by the driver.
695  	 * Over time we expect SPI drivers to be phased over to this API.
696  	 */
697  	bool				queued;
698  	struct kthread_worker		*kworker;
699  	struct kthread_work		pump_messages;
700  	spinlock_t			queue_lock;
701  	struct list_head		queue;
702  	struct spi_message		*cur_msg;
703  	struct completion               cur_msg_completion;
704  	bool				cur_msg_incomplete;
705  	bool				cur_msg_need_completion;
706  	bool				busy;
707  	bool				running;
708  	bool				rt;
709  	bool				auto_runtime_pm;
710  	bool                            fallback;
711  	bool				last_cs_mode_high;
712  	s8				last_cs[SPI_CS_CNT_MAX];
713  	u32				last_cs_index_mask : SPI_CS_CNT_MAX;
714  	struct completion               xfer_completion;
715  	size_t				max_dma_len;
716  
717  	int (*optimize_message)(struct spi_message *msg);
718  	int (*unoptimize_message)(struct spi_message *msg);
719  	int (*prepare_transfer_hardware)(struct spi_controller *ctlr);
720  	int (*transfer_one_message)(struct spi_controller *ctlr,
721  				    struct spi_message *mesg);
722  	int (*unprepare_transfer_hardware)(struct spi_controller *ctlr);
723  	int (*prepare_message)(struct spi_controller *ctlr,
724  			       struct spi_message *message);
725  	int (*unprepare_message)(struct spi_controller *ctlr,
726  				 struct spi_message *message);
727  	int (*target_abort)(struct spi_controller *ctlr);
728  
729  	/*
730  	 * These hooks are for drivers that use a generic implementation
731  	 * of transfer_one_message() provided by the core.
732  	 */
733  	void (*set_cs)(struct spi_device *spi, bool enable);
734  	int (*transfer_one)(struct spi_controller *ctlr, struct spi_device *spi,
735  			    struct spi_transfer *transfer);
736  	void (*handle_err)(struct spi_controller *ctlr,
737  			   struct spi_message *message);
738  
739  	/* Optimized handlers for SPI memory-like operations. */
740  	const struct spi_controller_mem_ops *mem_ops;
741  	const struct spi_controller_mem_caps *mem_caps;
742  
743  	/* GPIO chip select */
744  	struct gpio_desc	**cs_gpiods;
745  	bool			use_gpio_descriptors;
746  	s8			unused_native_cs;
747  	s8			max_native_cs;
748  
749  	/* Statistics */
750  	struct spi_statistics __percpu	*pcpu_statistics;
751  
752  	/* DMA channels for use with core dmaengine helpers */
753  	struct dma_chan		*dma_tx;
754  	struct dma_chan		*dma_rx;
755  
756  	/* Dummy data for full duplex devices */
757  	void			*dummy_rx;
758  	void			*dummy_tx;
759  
760  	int (*fw_translate_cs)(struct spi_controller *ctlr, unsigned cs);
761  
762  	/*
763  	 * Driver sets this field to indicate it is able to snapshot SPI
764  	 * transfers (needed e.g. for reading the time of POSIX clocks)
765  	 */
766  	bool			ptp_sts_supported;
767  
768  	/* Interrupt enable state during PTP system timestamping */
769  	unsigned long		irq_flags;
770  
771  	/* Flag for enabling opportunistic skipping of the queue in spi_sync */
772  	bool			queue_empty;
773  	bool			must_async;
774  	bool			defer_optimize_message;
775  };
776  
spi_controller_get_devdata(struct spi_controller * ctlr)777  static inline void *spi_controller_get_devdata(struct spi_controller *ctlr)
778  {
779  	return dev_get_drvdata(&ctlr->dev);
780  }
781  
spi_controller_set_devdata(struct spi_controller * ctlr,void * data)782  static inline void spi_controller_set_devdata(struct spi_controller *ctlr,
783  					      void *data)
784  {
785  	dev_set_drvdata(&ctlr->dev, data);
786  }
787  
spi_controller_get(struct spi_controller * ctlr)788  static inline struct spi_controller *spi_controller_get(struct spi_controller *ctlr)
789  {
790  	if (!ctlr || !get_device(&ctlr->dev))
791  		return NULL;
792  	return ctlr;
793  }
794  
spi_controller_put(struct spi_controller * ctlr)795  static inline void spi_controller_put(struct spi_controller *ctlr)
796  {
797  	if (ctlr)
798  		put_device(&ctlr->dev);
799  }
800  
spi_controller_is_target(struct spi_controller * ctlr)801  static inline bool spi_controller_is_target(struct spi_controller *ctlr)
802  {
803  	return IS_ENABLED(CONFIG_SPI_SLAVE) && ctlr->target;
804  }
805  
806  /* PM calls that need to be issued by the driver */
807  extern int spi_controller_suspend(struct spi_controller *ctlr);
808  extern int spi_controller_resume(struct spi_controller *ctlr);
809  
810  /* Calls the driver make to interact with the message queue */
811  extern struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr);
812  extern void spi_finalize_current_message(struct spi_controller *ctlr);
813  extern void spi_finalize_current_transfer(struct spi_controller *ctlr);
814  
815  /* Helper calls for driver to timestamp transfer */
816  void spi_take_timestamp_pre(struct spi_controller *ctlr,
817  			    struct spi_transfer *xfer,
818  			    size_t progress, bool irqs_off);
819  void spi_take_timestamp_post(struct spi_controller *ctlr,
820  			     struct spi_transfer *xfer,
821  			     size_t progress, bool irqs_off);
822  
823  /* The SPI driver core manages memory for the spi_controller classdev */
824  extern struct spi_controller *__spi_alloc_controller(struct device *host,
825  						unsigned int size, bool slave);
826  
spi_alloc_host(struct device * dev,unsigned int size)827  static inline struct spi_controller *spi_alloc_host(struct device *dev,
828  						    unsigned int size)
829  {
830  	return __spi_alloc_controller(dev, size, false);
831  }
832  
spi_alloc_target(struct device * dev,unsigned int size)833  static inline struct spi_controller *spi_alloc_target(struct device *dev,
834  						      unsigned int size)
835  {
836  	if (!IS_ENABLED(CONFIG_SPI_SLAVE))
837  		return NULL;
838  
839  	return __spi_alloc_controller(dev, size, true);
840  }
841  
842  struct spi_controller *__devm_spi_alloc_controller(struct device *dev,
843  						   unsigned int size,
844  						   bool slave);
845  
devm_spi_alloc_host(struct device * dev,unsigned int size)846  static inline struct spi_controller *devm_spi_alloc_host(struct device *dev,
847  							 unsigned int size)
848  {
849  	return __devm_spi_alloc_controller(dev, size, false);
850  }
851  
devm_spi_alloc_target(struct device * dev,unsigned int size)852  static inline struct spi_controller *devm_spi_alloc_target(struct device *dev,
853  							   unsigned int size)
854  {
855  	if (!IS_ENABLED(CONFIG_SPI_SLAVE))
856  		return NULL;
857  
858  	return __devm_spi_alloc_controller(dev, size, true);
859  }
860  
861  extern int spi_register_controller(struct spi_controller *ctlr);
862  extern int devm_spi_register_controller(struct device *dev,
863  					struct spi_controller *ctlr);
864  extern void spi_unregister_controller(struct spi_controller *ctlr);
865  
866  #if IS_ENABLED(CONFIG_ACPI) && IS_ENABLED(CONFIG_SPI_MASTER)
867  extern struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev);
868  extern struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr,
869  						struct acpi_device *adev,
870  						int index);
871  int acpi_spi_count_resources(struct acpi_device *adev);
872  #else
acpi_spi_find_controller_by_adev(struct acpi_device * adev)873  static inline struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
874  {
875  	return NULL;
876  }
877  
acpi_spi_device_alloc(struct spi_controller * ctlr,struct acpi_device * adev,int index)878  static inline struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr,
879  						       struct acpi_device *adev,
880  						       int index)
881  {
882  	return ERR_PTR(-ENODEV);
883  }
884  
acpi_spi_count_resources(struct acpi_device * adev)885  static inline int acpi_spi_count_resources(struct acpi_device *adev)
886  {
887  	return 0;
888  }
889  #endif
890  
891  /*
892   * SPI resource management while processing a SPI message
893   */
894  
895  typedef void (*spi_res_release_t)(struct spi_controller *ctlr,
896  				  struct spi_message *msg,
897  				  void *res);
898  
899  /**
900   * struct spi_res - SPI resource management structure
901   * @entry:   list entry
902   * @release: release code called prior to freeing this resource
903   * @data:    extra data allocated for the specific use-case
904   *
905   * This is based on ideas from devres, but focused on life-cycle
906   * management during spi_message processing.
907   */
908  struct spi_res {
909  	struct list_head        entry;
910  	spi_res_release_t       release;
911  	unsigned long long      data[]; /* Guarantee ull alignment */
912  };
913  
914  /*---------------------------------------------------------------------------*/
915  
916  /*
917   * I/O INTERFACE between SPI controller and protocol drivers
918   *
919   * Protocol drivers use a queue of spi_messages, each transferring data
920   * between the controller and memory buffers.
921   *
922   * The spi_messages themselves consist of a series of read+write transfer
923   * segments.  Those segments always read the same number of bits as they
924   * write; but one or the other is easily ignored by passing a NULL buffer
925   * pointer.  (This is unlike most types of I/O API, because SPI hardware
926   * is full duplex.)
927   *
928   * NOTE:  Allocation of spi_transfer and spi_message memory is entirely
929   * up to the protocol driver, which guarantees the integrity of both (as
930   * well as the data buffers) for as long as the message is queued.
931   */
932  
933  /**
934   * struct spi_transfer - a read/write buffer pair
935   * @tx_buf: data to be written (DMA-safe memory), or NULL
936   * @rx_buf: data to be read (DMA-safe memory), or NULL
937   * @tx_dma: DMA address of tx_buf, currently not for client use
938   * @rx_dma: DMA address of rx_buf, currently not for client use
939   * @tx_nbits: number of bits used for writing. If 0 the default
940   *      (SPI_NBITS_SINGLE) is used.
941   * @rx_nbits: number of bits used for reading. If 0 the default
942   *      (SPI_NBITS_SINGLE) is used.
943   * @len: size of rx and tx buffers (in bytes)
944   * @speed_hz: Select a speed other than the device default for this
945   *      transfer. If 0 the default (from @spi_device) is used.
946   * @bits_per_word: select a bits_per_word other than the device default
947   *      for this transfer. If 0 the default (from @spi_device) is used.
948   * @dummy_data: indicates transfer is dummy bytes transfer.
949   * @cs_off: performs the transfer with chipselect off.
950   * @cs_change: affects chipselect after this transfer completes
951   * @cs_change_delay: delay between cs deassert and assert when
952   *      @cs_change is set and @spi_transfer is not the last in @spi_message
953   * @delay: delay to be introduced after this transfer before
954   *	(optionally) changing the chipselect status, then starting
955   *	the next transfer or completing this @spi_message.
956   * @word_delay: inter word delay to be introduced after each word size
957   *	(set by bits_per_word) transmission.
958   * @effective_speed_hz: the effective SCK-speed that was used to
959   *      transfer this transfer. Set to 0 if the SPI bus driver does
960   *      not support it.
961   * @transfer_list: transfers are sequenced through @spi_message.transfers
962   * @tx_sg_mapped: If true, the @tx_sg is mapped for DMA
963   * @rx_sg_mapped: If true, the @rx_sg is mapped for DMA
964   * @tx_sg: Scatterlist for transmit, currently not for client use
965   * @rx_sg: Scatterlist for receive, currently not for client use
966   * @ptp_sts_word_pre: The word (subject to bits_per_word semantics) offset
967   *	within @tx_buf for which the SPI device is requesting that the time
968   *	snapshot for this transfer begins. Upon completing the SPI transfer,
969   *	this value may have changed compared to what was requested, depending
970   *	on the available snapshotting resolution (DMA transfer,
971   *	@ptp_sts_supported is false, etc).
972   * @ptp_sts_word_post: See @ptp_sts_word_post. The two can be equal (meaning
973   *	that a single byte should be snapshotted).
974   *	If the core takes care of the timestamp (if @ptp_sts_supported is false
975   *	for this controller), it will set @ptp_sts_word_pre to 0, and
976   *	@ptp_sts_word_post to the length of the transfer. This is done
977   *	purposefully (instead of setting to spi_transfer->len - 1) to denote
978   *	that a transfer-level snapshot taken from within the driver may still
979   *	be of higher quality.
980   * @ptp_sts: Pointer to a memory location held by the SPI slave device where a
981   *	PTP system timestamp structure may lie. If drivers use PIO or their
982   *	hardware has some sort of assist for retrieving exact transfer timing,
983   *	they can (and should) assert @ptp_sts_supported and populate this
984   *	structure using the ptp_read_system_*ts helper functions.
985   *	The timestamp must represent the time at which the SPI slave device has
986   *	processed the word, i.e. the "pre" timestamp should be taken before
987   *	transmitting the "pre" word, and the "post" timestamp after receiving
988   *	transmit confirmation from the controller for the "post" word.
989   * @timestamped: true if the transfer has been timestamped
990   * @error: Error status logged by SPI controller driver.
991   *
992   * SPI transfers always write the same number of bytes as they read.
993   * Protocol drivers should always provide @rx_buf and/or @tx_buf.
994   * In some cases, they may also want to provide DMA addresses for
995   * the data being transferred; that may reduce overhead, when the
996   * underlying driver uses DMA.
997   *
998   * If the transmit buffer is NULL, zeroes will be shifted out
999   * while filling @rx_buf.  If the receive buffer is NULL, the data
1000   * shifted in will be discarded.  Only "len" bytes shift out (or in).
1001   * It's an error to try to shift out a partial word.  (For example, by
1002   * shifting out three bytes with word size of sixteen or twenty bits;
1003   * the former uses two bytes per word, the latter uses four bytes.)
1004   *
1005   * In-memory data values are always in native CPU byte order, translated
1006   * from the wire byte order (big-endian except with SPI_LSB_FIRST).  So
1007   * for example when bits_per_word is sixteen, buffers are 2N bytes long
1008   * (@len = 2N) and hold N sixteen bit words in CPU byte order.
1009   *
1010   * When the word size of the SPI transfer is not a power-of-two multiple
1011   * of eight bits, those in-memory words include extra bits.  In-memory
1012   * words are always seen by protocol drivers as right-justified, so the
1013   * undefined (rx) or unused (tx) bits are always the most significant bits.
1014   *
1015   * All SPI transfers start with the relevant chipselect active.  Normally
1016   * it stays selected until after the last transfer in a message.  Drivers
1017   * can affect the chipselect signal using cs_change.
1018   *
1019   * (i) If the transfer isn't the last one in the message, this flag is
1020   * used to make the chipselect briefly go inactive in the middle of the
1021   * message.  Toggling chipselect in this way may be needed to terminate
1022   * a chip command, letting a single spi_message perform all of group of
1023   * chip transactions together.
1024   *
1025   * (ii) When the transfer is the last one in the message, the chip may
1026   * stay selected until the next transfer.  On multi-device SPI busses
1027   * with nothing blocking messages going to other devices, this is just
1028   * a performance hint; starting a message to another device deselects
1029   * this one.  But in other cases, this can be used to ensure correctness.
1030   * Some devices need protocol transactions to be built from a series of
1031   * spi_message submissions, where the content of one message is determined
1032   * by the results of previous messages and where the whole transaction
1033   * ends when the chipselect goes inactive.
1034   *
1035   * When SPI can transfer in 1x,2x or 4x. It can get this transfer information
1036   * from device through @tx_nbits and @rx_nbits. In Bi-direction, these
1037   * two should both be set. User can set transfer mode with SPI_NBITS_SINGLE(1x)
1038   * SPI_NBITS_DUAL(2x) and SPI_NBITS_QUAD(4x) to support these three transfer.
1039   *
1040   * The code that submits an spi_message (and its spi_transfers)
1041   * to the lower layers is responsible for managing its memory.
1042   * Zero-initialize every field you don't set up explicitly, to
1043   * insulate against future API updates.  After you submit a message
1044   * and its transfers, ignore them until its completion callback.
1045   */
1046  struct spi_transfer {
1047  	/*
1048  	 * It's okay if tx_buf == rx_buf (right?).
1049  	 * For MicroWire, one buffer must be NULL.
1050  	 * Buffers must work with dma_*map_single() calls.
1051  	 */
1052  	const void	*tx_buf;
1053  	void		*rx_buf;
1054  	unsigned	len;
1055  
1056  #define SPI_TRANS_FAIL_NO_START	BIT(0)
1057  #define SPI_TRANS_FAIL_IO	BIT(1)
1058  	u16		error;
1059  
1060  	bool		tx_sg_mapped;
1061  	bool		rx_sg_mapped;
1062  
1063  	struct sg_table tx_sg;
1064  	struct sg_table rx_sg;
1065  	dma_addr_t	tx_dma;
1066  	dma_addr_t	rx_dma;
1067  
1068  	unsigned	dummy_data:1;
1069  	unsigned	cs_off:1;
1070  	unsigned	cs_change:1;
1071  	unsigned	tx_nbits:4;
1072  	unsigned	rx_nbits:4;
1073  	unsigned	timestamped:1;
1074  #define	SPI_NBITS_SINGLE	0x01 /* 1-bit transfer */
1075  #define	SPI_NBITS_DUAL		0x02 /* 2-bit transfer */
1076  #define	SPI_NBITS_QUAD		0x04 /* 4-bit transfer */
1077  #define	SPI_NBITS_OCTAL	0x08 /* 8-bit transfer */
1078  	u8		bits_per_word;
1079  	struct spi_delay	delay;
1080  	struct spi_delay	cs_change_delay;
1081  	struct spi_delay	word_delay;
1082  	u32		speed_hz;
1083  
1084  	u32		effective_speed_hz;
1085  
1086  	unsigned int	ptp_sts_word_pre;
1087  	unsigned int	ptp_sts_word_post;
1088  
1089  	struct ptp_system_timestamp *ptp_sts;
1090  
1091  	struct list_head transfer_list;
1092  };
1093  
1094  /**
1095   * struct spi_message - one multi-segment SPI transaction
1096   * @transfers: list of transfer segments in this transaction
1097   * @spi: SPI device to which the transaction is queued
1098   * @pre_optimized: peripheral driver pre-optimized the message
1099   * @optimized: the message is in the optimized state
1100   * @prepared: spi_prepare_message was called for the this message
1101   * @status: zero for success, else negative errno
1102   * @complete: called to report transaction completions
1103   * @context: the argument to complete() when it's called
1104   * @frame_length: the total number of bytes in the message
1105   * @actual_length: the total number of bytes that were transferred in all
1106   *	successful segments
1107   * @queue: for use by whichever driver currently owns the message
1108   * @state: for use by whichever driver currently owns the message
1109   * @opt_state: for use by whichever driver currently owns the message
1110   * @resources: for resource management when the SPI message is processed
1111   *
1112   * A @spi_message is used to execute an atomic sequence of data transfers,
1113   * each represented by a struct spi_transfer.  The sequence is "atomic"
1114   * in the sense that no other spi_message may use that SPI bus until that
1115   * sequence completes.  On some systems, many such sequences can execute as
1116   * a single programmed DMA transfer.  On all systems, these messages are
1117   * queued, and might complete after transactions to other devices.  Messages
1118   * sent to a given spi_device are always executed in FIFO order.
1119   *
1120   * The code that submits an spi_message (and its spi_transfers)
1121   * to the lower layers is responsible for managing its memory.
1122   * Zero-initialize every field you don't set up explicitly, to
1123   * insulate against future API updates.  After you submit a message
1124   * and its transfers, ignore them until its completion callback.
1125   */
1126  struct spi_message {
1127  	struct list_head	transfers;
1128  
1129  	struct spi_device	*spi;
1130  
1131  	/* spi_optimize_message() was called for this message */
1132  	bool			pre_optimized;
1133  	/* __spi_optimize_message() was called for this message */
1134  	bool			optimized;
1135  
1136  	/* spi_prepare_message() was called for this message */
1137  	bool			prepared;
1138  
1139  	/*
1140  	 * REVISIT: we might want a flag affecting the behavior of the
1141  	 * last transfer ... allowing things like "read 16 bit length L"
1142  	 * immediately followed by "read L bytes".  Basically imposing
1143  	 * a specific message scheduling algorithm.
1144  	 *
1145  	 * Some controller drivers (message-at-a-time queue processing)
1146  	 * could provide that as their default scheduling algorithm.  But
1147  	 * others (with multi-message pipelines) could need a flag to
1148  	 * tell them about such special cases.
1149  	 */
1150  
1151  	/* Completion is reported through a callback */
1152  	int			status;
1153  	void			(*complete)(void *context);
1154  	void			*context;
1155  	unsigned		frame_length;
1156  	unsigned		actual_length;
1157  
1158  	/*
1159  	 * For optional use by whatever driver currently owns the
1160  	 * spi_message ...  between calls to spi_async and then later
1161  	 * complete(), that's the spi_controller controller driver.
1162  	 */
1163  	struct list_head	queue;
1164  	void			*state;
1165  	/*
1166  	 * Optional state for use by controller driver between calls to
1167  	 * __spi_optimize_message() and __spi_unoptimize_message().
1168  	 */
1169  	void			*opt_state;
1170  
1171  	/* List of spi_res resources when the SPI message is processed */
1172  	struct list_head        resources;
1173  };
1174  
spi_message_init_no_memset(struct spi_message * m)1175  static inline void spi_message_init_no_memset(struct spi_message *m)
1176  {
1177  	INIT_LIST_HEAD(&m->transfers);
1178  	INIT_LIST_HEAD(&m->resources);
1179  }
1180  
spi_message_init(struct spi_message * m)1181  static inline void spi_message_init(struct spi_message *m)
1182  {
1183  	memset(m, 0, sizeof *m);
1184  	spi_message_init_no_memset(m);
1185  }
1186  
1187  static inline void
spi_message_add_tail(struct spi_transfer * t,struct spi_message * m)1188  spi_message_add_tail(struct spi_transfer *t, struct spi_message *m)
1189  {
1190  	list_add_tail(&t->transfer_list, &m->transfers);
1191  }
1192  
1193  static inline void
spi_transfer_del(struct spi_transfer * t)1194  spi_transfer_del(struct spi_transfer *t)
1195  {
1196  	list_del(&t->transfer_list);
1197  }
1198  
1199  static inline int
spi_transfer_delay_exec(struct spi_transfer * t)1200  spi_transfer_delay_exec(struct spi_transfer *t)
1201  {
1202  	return spi_delay_exec(&t->delay, t);
1203  }
1204  
1205  /**
1206   * spi_message_init_with_transfers - Initialize spi_message and append transfers
1207   * @m: spi_message to be initialized
1208   * @xfers: An array of SPI transfers
1209   * @num_xfers: Number of items in the xfer array
1210   *
1211   * This function initializes the given spi_message and adds each spi_transfer in
1212   * the given array to the message.
1213   */
1214  static inline void
spi_message_init_with_transfers(struct spi_message * m,struct spi_transfer * xfers,unsigned int num_xfers)1215  spi_message_init_with_transfers(struct spi_message *m,
1216  struct spi_transfer *xfers, unsigned int num_xfers)
1217  {
1218  	unsigned int i;
1219  
1220  	spi_message_init(m);
1221  	for (i = 0; i < num_xfers; ++i)
1222  		spi_message_add_tail(&xfers[i], m);
1223  }
1224  
1225  /*
1226   * It's fine to embed message and transaction structures in other data
1227   * structures so long as you don't free them while they're in use.
1228   */
spi_message_alloc(unsigned ntrans,gfp_t flags)1229  static inline struct spi_message *spi_message_alloc(unsigned ntrans, gfp_t flags)
1230  {
1231  	struct spi_message_with_transfers {
1232  		struct spi_message m;
1233  		struct spi_transfer t[];
1234  	} *mwt;
1235  	unsigned i;
1236  
1237  	mwt = kzalloc(struct_size(mwt, t, ntrans), flags);
1238  	if (!mwt)
1239  		return NULL;
1240  
1241  	spi_message_init_no_memset(&mwt->m);
1242  	for (i = 0; i < ntrans; i++)
1243  		spi_message_add_tail(&mwt->t[i], &mwt->m);
1244  
1245  	return &mwt->m;
1246  }
1247  
spi_message_free(struct spi_message * m)1248  static inline void spi_message_free(struct spi_message *m)
1249  {
1250  	kfree(m);
1251  }
1252  
1253  extern int spi_optimize_message(struct spi_device *spi, struct spi_message *msg);
1254  extern void spi_unoptimize_message(struct spi_message *msg);
1255  extern int devm_spi_optimize_message(struct device *dev, struct spi_device *spi,
1256  				     struct spi_message *msg);
1257  
1258  extern int spi_setup(struct spi_device *spi);
1259  extern int spi_async(struct spi_device *spi, struct spi_message *message);
1260  extern int spi_target_abort(struct spi_device *spi);
1261  
1262  static inline size_t
spi_max_message_size(struct spi_device * spi)1263  spi_max_message_size(struct spi_device *spi)
1264  {
1265  	struct spi_controller *ctlr = spi->controller;
1266  
1267  	if (!ctlr->max_message_size)
1268  		return SIZE_MAX;
1269  	return ctlr->max_message_size(spi);
1270  }
1271  
1272  static inline size_t
spi_max_transfer_size(struct spi_device * spi)1273  spi_max_transfer_size(struct spi_device *spi)
1274  {
1275  	struct spi_controller *ctlr = spi->controller;
1276  	size_t tr_max = SIZE_MAX;
1277  	size_t msg_max = spi_max_message_size(spi);
1278  
1279  	if (ctlr->max_transfer_size)
1280  		tr_max = ctlr->max_transfer_size(spi);
1281  
1282  	/* Transfer size limit must not be greater than message size limit */
1283  	return min(tr_max, msg_max);
1284  }
1285  
1286  /**
1287   * spi_is_bpw_supported - Check if bits per word is supported
1288   * @spi: SPI device
1289   * @bpw: Bits per word
1290   *
1291   * This function checks to see if the SPI controller supports @bpw.
1292   *
1293   * Returns:
1294   * True if @bpw is supported, false otherwise.
1295   */
spi_is_bpw_supported(struct spi_device * spi,u32 bpw)1296  static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw)
1297  {
1298  	u32 bpw_mask = spi->controller->bits_per_word_mask;
1299  
1300  	if (bpw == 8 || (bpw <= 32 && bpw_mask & SPI_BPW_MASK(bpw)))
1301  		return true;
1302  
1303  	return false;
1304  }
1305  
1306  /**
1307   * spi_controller_xfer_timeout - Compute a suitable timeout value
1308   * @ctlr: SPI device
1309   * @xfer: Transfer descriptor
1310   *
1311   * Compute a relevant timeout value for the given transfer. We derive the time
1312   * that it would take on a single data line and take twice this amount of time
1313   * with a minimum of 500ms to avoid false positives on loaded systems.
1314   *
1315   * Returns: Transfer timeout value in milliseconds.
1316   */
spi_controller_xfer_timeout(struct spi_controller * ctlr,struct spi_transfer * xfer)1317  static inline unsigned int spi_controller_xfer_timeout(struct spi_controller *ctlr,
1318  						       struct spi_transfer *xfer)
1319  {
1320  	return max(xfer->len * 8 * 2 / (xfer->speed_hz / 1000), 500U);
1321  }
1322  
1323  /*---------------------------------------------------------------------------*/
1324  
1325  /* SPI transfer replacement methods which make use of spi_res */
1326  
1327  struct spi_replaced_transfers;
1328  typedef void (*spi_replaced_release_t)(struct spi_controller *ctlr,
1329  				       struct spi_message *msg,
1330  				       struct spi_replaced_transfers *res);
1331  /**
1332   * struct spi_replaced_transfers - structure describing the spi_transfer
1333   *                                 replacements that have occurred
1334   *                                 so that they can get reverted
1335   * @release:            some extra release code to get executed prior to
1336   *                      releasing this structure
1337   * @extradata:          pointer to some extra data if requested or NULL
1338   * @replaced_transfers: transfers that have been replaced and which need
1339   *                      to get restored
1340   * @replaced_after:     the transfer after which the @replaced_transfers
1341   *                      are to get re-inserted
1342   * @inserted:           number of transfers inserted
1343   * @inserted_transfers: array of spi_transfers of array-size @inserted,
1344   *                      that have been replacing replaced_transfers
1345   *
1346   * Note: that @extradata will point to @inserted_transfers[@inserted]
1347   * if some extra allocation is requested, so alignment will be the same
1348   * as for spi_transfers.
1349   */
1350  struct spi_replaced_transfers {
1351  	spi_replaced_release_t release;
1352  	void *extradata;
1353  	struct list_head replaced_transfers;
1354  	struct list_head *replaced_after;
1355  	size_t inserted;
1356  	struct spi_transfer inserted_transfers[];
1357  };
1358  
1359  /*---------------------------------------------------------------------------*/
1360  
1361  /* SPI transfer transformation methods */
1362  
1363  extern int spi_split_transfers_maxsize(struct spi_controller *ctlr,
1364  				       struct spi_message *msg,
1365  				       size_t maxsize);
1366  extern int spi_split_transfers_maxwords(struct spi_controller *ctlr,
1367  					struct spi_message *msg,
1368  					size_t maxwords);
1369  
1370  /*---------------------------------------------------------------------------*/
1371  
1372  /*
1373   * All these synchronous SPI transfer routines are utilities layered
1374   * over the core async transfer primitive.  Here, "synchronous" means
1375   * they will sleep uninterruptibly until the async transfer completes.
1376   */
1377  
1378  extern int spi_sync(struct spi_device *spi, struct spi_message *message);
1379  extern int spi_sync_locked(struct spi_device *spi, struct spi_message *message);
1380  extern int spi_bus_lock(struct spi_controller *ctlr);
1381  extern int spi_bus_unlock(struct spi_controller *ctlr);
1382  
1383  /**
1384   * spi_sync_transfer - synchronous SPI data transfer
1385   * @spi: device with which data will be exchanged
1386   * @xfers: An array of spi_transfers
1387   * @num_xfers: Number of items in the xfer array
1388   * Context: can sleep
1389   *
1390   * Does a synchronous SPI data transfer of the given spi_transfer array.
1391   *
1392   * For more specific semantics see spi_sync().
1393   *
1394   * Return: zero on success, else a negative error code.
1395   */
1396  static inline int
spi_sync_transfer(struct spi_device * spi,struct spi_transfer * xfers,unsigned int num_xfers)1397  spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
1398  	unsigned int num_xfers)
1399  {
1400  	struct spi_message msg;
1401  
1402  	spi_message_init_with_transfers(&msg, xfers, num_xfers);
1403  
1404  	return spi_sync(spi, &msg);
1405  }
1406  
1407  /**
1408   * spi_write - SPI synchronous write
1409   * @spi: device to which data will be written
1410   * @buf: data buffer
1411   * @len: data buffer size
1412   * Context: can sleep
1413   *
1414   * This function writes the buffer @buf.
1415   * Callable only from contexts that can sleep.
1416   *
1417   * Return: zero on success, else a negative error code.
1418   */
1419  static inline int
spi_write(struct spi_device * spi,const void * buf,size_t len)1420  spi_write(struct spi_device *spi, const void *buf, size_t len)
1421  {
1422  	struct spi_transfer	t = {
1423  			.tx_buf		= buf,
1424  			.len		= len,
1425  		};
1426  
1427  	return spi_sync_transfer(spi, &t, 1);
1428  }
1429  
1430  /**
1431   * spi_read - SPI synchronous read
1432   * @spi: device from which data will be read
1433   * @buf: data buffer
1434   * @len: data buffer size
1435   * Context: can sleep
1436   *
1437   * This function reads the buffer @buf.
1438   * Callable only from contexts that can sleep.
1439   *
1440   * Return: zero on success, else a negative error code.
1441   */
1442  static inline int
spi_read(struct spi_device * spi,void * buf,size_t len)1443  spi_read(struct spi_device *spi, void *buf, size_t len)
1444  {
1445  	struct spi_transfer	t = {
1446  			.rx_buf		= buf,
1447  			.len		= len,
1448  		};
1449  
1450  	return spi_sync_transfer(spi, &t, 1);
1451  }
1452  
1453  /* This copies txbuf and rxbuf data; for small transfers only! */
1454  extern int spi_write_then_read(struct spi_device *spi,
1455  		const void *txbuf, unsigned n_tx,
1456  		void *rxbuf, unsigned n_rx);
1457  
1458  /**
1459   * spi_w8r8 - SPI synchronous 8 bit write followed by 8 bit read
1460   * @spi: device with which data will be exchanged
1461   * @cmd: command to be written before data is read back
1462   * Context: can sleep
1463   *
1464   * Callable only from contexts that can sleep.
1465   *
1466   * Return: the (unsigned) eight bit number returned by the
1467   * device, or else a negative error code.
1468   */
spi_w8r8(struct spi_device * spi,u8 cmd)1469  static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd)
1470  {
1471  	ssize_t			status;
1472  	u8			result;
1473  
1474  	status = spi_write_then_read(spi, &cmd, 1, &result, 1);
1475  
1476  	/* Return negative errno or unsigned value */
1477  	return (status < 0) ? status : result;
1478  }
1479  
1480  /**
1481   * spi_w8r16 - SPI synchronous 8 bit write followed by 16 bit read
1482   * @spi: device with which data will be exchanged
1483   * @cmd: command to be written before data is read back
1484   * Context: can sleep
1485   *
1486   * The number is returned in wire-order, which is at least sometimes
1487   * big-endian.
1488   *
1489   * Callable only from contexts that can sleep.
1490   *
1491   * Return: the (unsigned) sixteen bit number returned by the
1492   * device, or else a negative error code.
1493   */
spi_w8r16(struct spi_device * spi,u8 cmd)1494  static inline ssize_t spi_w8r16(struct spi_device *spi, u8 cmd)
1495  {
1496  	ssize_t			status;
1497  	u16			result;
1498  
1499  	status = spi_write_then_read(spi, &cmd, 1, &result, 2);
1500  
1501  	/* Return negative errno or unsigned value */
1502  	return (status < 0) ? status : result;
1503  }
1504  
1505  /**
1506   * spi_w8r16be - SPI synchronous 8 bit write followed by 16 bit big-endian read
1507   * @spi: device with which data will be exchanged
1508   * @cmd: command to be written before data is read back
1509   * Context: can sleep
1510   *
1511   * This function is similar to spi_w8r16, with the exception that it will
1512   * convert the read 16 bit data word from big-endian to native endianness.
1513   *
1514   * Callable only from contexts that can sleep.
1515   *
1516   * Return: the (unsigned) sixteen bit number returned by the device in CPU
1517   * endianness, or else a negative error code.
1518   */
spi_w8r16be(struct spi_device * spi,u8 cmd)1519  static inline ssize_t spi_w8r16be(struct spi_device *spi, u8 cmd)
1520  
1521  {
1522  	ssize_t status;
1523  	__be16 result;
1524  
1525  	status = spi_write_then_read(spi, &cmd, 1, &result, 2);
1526  	if (status < 0)
1527  		return status;
1528  
1529  	return be16_to_cpu(result);
1530  }
1531  
1532  /*---------------------------------------------------------------------------*/
1533  
1534  /*
1535   * INTERFACE between board init code and SPI infrastructure.
1536   *
1537   * No SPI driver ever sees these SPI device table segments, but
1538   * it's how the SPI core (or adapters that get hotplugged) grows
1539   * the driver model tree.
1540   *
1541   * As a rule, SPI devices can't be probed.  Instead, board init code
1542   * provides a table listing the devices which are present, with enough
1543   * information to bind and set up the device's driver.  There's basic
1544   * support for non-static configurations too; enough to handle adding
1545   * parport adapters, or microcontrollers acting as USB-to-SPI bridges.
1546   */
1547  
1548  /**
1549   * struct spi_board_info - board-specific template for a SPI device
1550   * @modalias: Initializes spi_device.modalias; identifies the driver.
1551   * @platform_data: Initializes spi_device.platform_data; the particular
1552   *	data stored there is driver-specific.
1553   * @swnode: Software node for the device.
1554   * @controller_data: Initializes spi_device.controller_data; some
1555   *	controllers need hints about hardware setup, e.g. for DMA.
1556   * @irq: Initializes spi_device.irq; depends on how the board is wired.
1557   * @max_speed_hz: Initializes spi_device.max_speed_hz; based on limits
1558   *	from the chip datasheet and board-specific signal quality issues.
1559   * @bus_num: Identifies which spi_controller parents the spi_device; unused
1560   *	by spi_new_device(), and otherwise depends on board wiring.
1561   * @chip_select: Initializes spi_device.chip_select; depends on how
1562   *	the board is wired.
1563   * @mode: Initializes spi_device.mode; based on the chip datasheet, board
1564   *	wiring (some devices support both 3WIRE and standard modes), and
1565   *	possibly presence of an inverter in the chipselect path.
1566   *
1567   * When adding new SPI devices to the device tree, these structures serve
1568   * as a partial device template.  They hold information which can't always
1569   * be determined by drivers.  Information that probe() can establish (such
1570   * as the default transfer wordsize) is not included here.
1571   *
1572   * These structures are used in two places.  Their primary role is to
1573   * be stored in tables of board-specific device descriptors, which are
1574   * declared early in board initialization and then used (much later) to
1575   * populate a controller's device tree after the that controller's driver
1576   * initializes.  A secondary (and atypical) role is as a parameter to
1577   * spi_new_device() call, which happens after those controller drivers
1578   * are active in some dynamic board configuration models.
1579   */
1580  struct spi_board_info {
1581  	/*
1582  	 * The device name and module name are coupled, like platform_bus;
1583  	 * "modalias" is normally the driver name.
1584  	 *
1585  	 * platform_data goes to spi_device.dev.platform_data,
1586  	 * controller_data goes to spi_device.controller_data,
1587  	 * IRQ is copied too.
1588  	 */
1589  	char		modalias[SPI_NAME_SIZE];
1590  	const void	*platform_data;
1591  	const struct software_node *swnode;
1592  	void		*controller_data;
1593  	int		irq;
1594  
1595  	/* Slower signaling on noisy or low voltage boards */
1596  	u32		max_speed_hz;
1597  
1598  
1599  	/*
1600  	 * bus_num is board specific and matches the bus_num of some
1601  	 * spi_controller that will probably be registered later.
1602  	 *
1603  	 * chip_select reflects how this chip is wired to that master;
1604  	 * it's less than num_chipselect.
1605  	 */
1606  	u16		bus_num;
1607  	u16		chip_select;
1608  
1609  	/*
1610  	 * mode becomes spi_device.mode, and is essential for chips
1611  	 * where the default of SPI_CS_HIGH = 0 is wrong.
1612  	 */
1613  	u32		mode;
1614  
1615  	/*
1616  	 * ... may need additional spi_device chip config data here.
1617  	 * avoid stuff protocol drivers can set; but include stuff
1618  	 * needed to behave without being bound to a driver:
1619  	 *  - quirks like clock rate mattering when not selected
1620  	 */
1621  };
1622  
1623  #ifdef	CONFIG_SPI
1624  extern int
1625  spi_register_board_info(struct spi_board_info const *info, unsigned n);
1626  #else
1627  /* Board init code may ignore whether SPI is configured or not */
1628  static inline int
spi_register_board_info(struct spi_board_info const * info,unsigned n)1629  spi_register_board_info(struct spi_board_info const *info, unsigned n)
1630  	{ return 0; }
1631  #endif
1632  
1633  /*
1634   * If you're hotplugging an adapter with devices (parport, USB, etc)
1635   * use spi_new_device() to describe each device.  You can also call
1636   * spi_unregister_device() to start making that device vanish, but
1637   * normally that would be handled by spi_unregister_controller().
1638   *
1639   * You can also use spi_alloc_device() and spi_add_device() to use a two
1640   * stage registration sequence for each spi_device. This gives the caller
1641   * some more control over the spi_device structure before it is registered,
1642   * but requires that caller to initialize fields that would otherwise
1643   * be defined using the board info.
1644   */
1645  extern struct spi_device *
1646  spi_alloc_device(struct spi_controller *ctlr);
1647  
1648  extern int
1649  spi_add_device(struct spi_device *spi);
1650  
1651  extern struct spi_device *
1652  spi_new_device(struct spi_controller *, struct spi_board_info *);
1653  
1654  extern void spi_unregister_device(struct spi_device *spi);
1655  
1656  extern const struct spi_device_id *
1657  spi_get_device_id(const struct spi_device *sdev);
1658  
1659  extern const void *
1660  spi_get_device_match_data(const struct spi_device *sdev);
1661  
1662  static inline bool
spi_transfer_is_last(struct spi_controller * ctlr,struct spi_transfer * xfer)1663  spi_transfer_is_last(struct spi_controller *ctlr, struct spi_transfer *xfer)
1664  {
1665  	return list_is_last(&xfer->transfer_list, &ctlr->cur_msg->transfers);
1666  }
1667  
1668  #endif /* __LINUX_SPI_H */
1669