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