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