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