blob: 314d922ca607c15c752b20caed7f25e6e8b1b5be [file] [log] [blame]
Marco Felschb445bfc2018-09-25 11:42:28 +02001/* SPDX-License-Identifier: GPL-2.0-or-later
2 *
David Brownell8ae12a02006-01-08 13:34:19 -08003 * Copyright (C) 2005 David Brownell
David Brownell8ae12a02006-01-08 13:34:19 -08004 */
5
6#ifndef __LINUX_SPI_H
7#define __LINUX_SPI_H
8
Randy Dunlap0a30c5c2009-01-04 12:00:47 -08009#include <linux/device.h>
Anton Vorontsov75368bf2009-09-22 16:46:04 -070010#include <linux/mod_devicetable.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Linus Walleijffbbdd212012-02-22 10:05:38 +010012#include <linux/kthread.h>
Mark Brownb1589352013-10-05 11:50:40 +010013#include <linux/completion.h>
Mark Brown6ad45a22014-02-02 13:47:47 +000014#include <linux/scatterlist.h>
Randy Dunlap0a30c5c2009-01-04 12:00:47 -080015
Mark Brown99adef32014-01-16 12:22:43 +000016struct dma_chan;
Dmitry Torokhov826cf172017-02-28 14:25:18 -080017struct property_entry;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020018struct spi_controller;
Martin Sperleca2ebc2015-06-22 13:00:36 +000019struct spi_transfer;
Boris Brezillonc36ff262018-04-26 18:18:14 +020020struct spi_controller_mem_ops;
David Brownellb8852442006-01-08 13:34:23 -080021
David Brownell8ae12a02006-01-08 13:34:19 -080022/*
Geert Uytterhoeven6c364062017-05-22 15:11:41 +020023 * INTERFACES between SPI master-side drivers and SPI slave protocol handlers,
24 * and SPI infrastructure.
David Brownell8ae12a02006-01-08 13:34:19 -080025 */
26extern struct bus_type spi_bus_type;
27
28/**
Martin Sperleca2ebc2015-06-22 13:00:36 +000029 * struct spi_statistics - statistics for spi transfers
Geliang Tang0243ed42015-09-15 04:59:21 -070030 * @lock: lock protecting this structure
Martin Sperleca2ebc2015-06-22 13:00:36 +000031 *
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 *
Martin Sperl6b7bc062015-06-22 13:02:04 +000047 * @transfer_bytes_histo:
48 * transfer bytes histogramm
Martin Sperld9f12122015-12-14 15:20:20 +000049 *
50 * @transfers_split_maxsize:
51 * number of transfers that have been split because of
52 * maxsize limit
Martin Sperleca2ebc2015-06-22 13:00:36 +000053 */
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
Martin Sperl6b7bc062015-06-22 13:02:04 +000070#define SPI_STATISTICS_HISTO_SIZE 17
71 unsigned long transfer_bytes_histo[SPI_STATISTICS_HISTO_SIZE];
Martin Sperld9f12122015-12-14 15:20:20 +000072
73 unsigned long transfers_split_maxsize;
Martin Sperleca2ebc2015-06-22 13:00:36 +000074};
75
76void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
77 struct spi_transfer *xfer,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020078 struct spi_controller *ctlr);
Martin Sperleca2ebc2015-06-22 13:00:36 +000079
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/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020092 * struct spi_device - Controller side proxy for an SPI slave device
David Brownell8ae12a02006-01-08 13:34:19 -080093 * @dev: Driver model representation of the device.
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020094 * @controller: SPI controller used with the device.
95 * @master: Copy of controller, for backwards compatibility.
David Brownell8ae12a02006-01-08 13:34:19 -080096 * @max_speed_hz: Maximum clock rate to be used with this chip
97 * (on this board); may be changed by the device's driver.
Imre Deak4cff33f2006-02-17 10:02:18 -080098 * The spi_transfer.speed_hz can override this for each transfer.
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020099 * @chip_select: Chipselect, distinguishing chips handled by @controller.
David Brownell8ae12a02006-01-08 13:34:19 -0800100 * @mode: The spi mode defines how data is clocked out and in.
101 * This may be changed by the device's driver.
David Brownell33e34dc2007-05-08 00:32:21 -0700102 * 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).
David Brownell8ae12a02006-01-08 13:34:19 -0800105 * @bits_per_word: Data transfers involve one or more words; word sizes
David Brownell747d8442006-04-02 10:33:37 -0800106 * like eight or 12 bits are common. In-memory wordsizes are
David Brownell8ae12a02006-01-08 13:34:19 -0800107 * powers of two bytes (e.g. 20 bit samples use 32 bits).
David Brownellccf77cc2006-04-03 15:46:22 -0700108 * This may be changed by the device's driver, or left at the
109 * default (0) indicating protocol words are eight bit bytes.
Imre Deak4cff33f2006-02-17 10:02:18 -0800110 * The spi_transfer.bits_per_word can override this for each transfer.
David Brownell8ae12a02006-01-08 13:34:19 -0800111 * @irq: Negative, or the number passed to request_irq() to receive
David Brownell747d8442006-04-02 10:33:37 -0800112 * interrupts from this device.
David Brownell8ae12a02006-01-08 13:34:19 -0800113 * @controller_state: Controller's runtime state
David Brownellb8852442006-01-08 13:34:23 -0800114 * @controller_data: Board-specific definitions for controller, such as
David Brownell747d8442006-04-02 10:33:37 -0800115 * FIFO initialization parameters; from board_info.controller_data
David Brownell33e34dc2007-05-08 00:32:21 -0700116 * @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
Andreas Larsson446411e2013-02-13 14:20:25 +0100119 * @cs_gpio: gpio number of the chipselect line (optional, -ENOENT when
Geert Uytterhoeven8d26fdf2017-11-30 14:35:08 +0100120 * not using a GPIO line)
David Brownell8ae12a02006-01-08 13:34:19 -0800121 *
Martin Sperleca2ebc2015-06-22 13:00:36 +0000122 * @statistics: statistics for the spi_device
123 *
David Brownell33e34dc2007-05-08 00:32:21 -0700124 * A @spi_device is used to interchange data between an SPI slave
David Brownell8ae12a02006-01-08 13:34:19 -0800125 * (usually a discrete chip) and CPU memory.
126 *
David Brownell33e34dc2007-05-08 00:32:21 -0700127 * In @dev, the platform_data is used to hold information about this
David Brownell8ae12a02006-01-08 13:34:19 -0800128 * 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
David Brownell33e34dc2007-05-08 00:32:21 -0700130 * variant with slightly different functionality; another might be
131 * information about how this particular board wires the chip's pins.
David Brownell8ae12a02006-01-08 13:34:19 -0800132 */
133struct spi_device {
134 struct device dev;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200135 struct spi_controller *controller;
136 struct spi_controller *master; /* compatibility layer */
David Brownell8ae12a02006-01-08 13:34:19 -0800137 u32 max_speed_hz;
138 u8 chip_select;
Trent Piepho89c1f60742013-12-13 18:27:44 -0800139 u8 bits_per_word;
wangyuhangf477b7f2013-08-11 18:15:17 +0800140 u16 mode;
David Brownellb8852442006-01-08 13:34:23 -0800141#define SPI_CPHA 0x01 /* clock phase */
142#define SPI_CPOL 0x02 /* clock polarity */
David Brownell0c8684612006-01-08 13:34:25 -0800143#define SPI_MODE_0 (0|0) /* (original MicroWire) */
144#define SPI_MODE_1 (0|SPI_CPHA)
David Brownell8ae12a02006-01-08 13:34:19 -0800145#define SPI_MODE_2 (SPI_CPOL|0)
146#define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
David Brownellb8852442006-01-08 13:34:23 -0800147#define SPI_CS_HIGH 0x04 /* chipselect active high? */
David Brownellccf77cc2006-04-03 15:46:22 -0700148#define SPI_LSB_FIRST 0x08 /* per-word bits-on-wire */
David Brownellc06e6772007-07-17 04:04:03 -0700149#define SPI_3WIRE 0x10 /* SI/SO signals shared */
Anton Vorontsov4ef7af52007-07-31 00:38:43 -0700150#define SPI_LOOP 0x20 /* loopback mode */
David Brownellb55f6272009-06-30 11:41:26 -0700151#define SPI_NO_CS 0x40 /* 1 dev/bus, no chipselect */
152#define SPI_READY 0x80 /* slave pulls low to pause */
wangyuhangf477b7f2013-08-11 18:15:17 +0800153#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 */
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +0000157#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 */
Mark Brownae218f72018-12-04 17:31:49 +0000160#define SPI_3WIRE_HIZ 0x8000 /* high impedance turnaround */
David Brownell8ae12a02006-01-08 13:34:19 -0800161 int irq;
162 void *controller_state;
David Brownellb8852442006-01-08 13:34:23 -0800163 void *controller_data;
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700164 char modalias[SPI_NAME_SIZE];
Trent Piepho50395632018-09-20 19:18:32 +0000165 const char *driver_override;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100166 int cs_gpio; /* chip select gpio */
David Brownell8ae12a02006-01-08 13:34:19 -0800167
Martin Sperleca2ebc2015-06-22 13:00:36 +0000168 /* the statistics */
169 struct spi_statistics statistics;
170
David Brownell33e34dc2007-05-08 00:32:21 -0700171 /*
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
David Brownell33e34dc2007-05-08 00:32:21 -0700176 * - chipselect delays
177 * - ...
178 */
David Brownell8ae12a02006-01-08 13:34:19 -0800179};
180
181static inline struct spi_device *to_spi_device(struct device *dev)
182{
David Brownellb8852442006-01-08 13:34:23 -0800183 return dev ? container_of(dev, struct spi_device, dev) : NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800184}
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
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200198/* ctldata is for the bus_controller driver's runtime state */
David Brownell8ae12a02006-01-08 13:34:19 -0800199static 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
Ben Dooks9b40ff42007-02-12 00:52:41 -0800209/* 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}
David Brownell8ae12a02006-01-08 13:34:19 -0800220
221struct spi_message;
Mark Brownb1589352013-10-05 11:50:40 +0100222struct spi_transfer;
David Brownellb8852442006-01-08 13:34:23 -0800223
David Brownell2604288f2007-07-31 00:39:44 -0700224/**
225 * struct spi_driver - Host side "protocol" driver
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700226 * @id_table: List of SPI devices supported by this driver
David Brownell2604288f2007-07-31 00:39:44 -0700227 * @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
David Brownell2604288f2007-07-31 00:39:44 -0700234 * @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 */
David Brownellb8852442006-01-08 13:34:23 -0800249struct spi_driver {
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700250 const struct spi_device_id *id_table;
David Brownellb8852442006-01-08 13:34:23 -0800251 int (*probe)(struct spi_device *spi);
252 int (*remove)(struct spi_device *spi);
253 void (*shutdown)(struct spi_device *spi);
David Brownellb8852442006-01-08 13:34:23 -0800254 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
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500262extern int __spi_register_driver(struct module *owner, struct spi_driver *sdrv);
David Brownellb8852442006-01-08 13:34:23 -0800263
David Brownell33e34dc2007-05-08 00:32:21 -0700264/**
265 * spi_unregister_driver - reverse effect of spi_register_driver
266 * @sdrv: the driver to unregister
267 * Context: can sleep
268 */
David Brownellb8852442006-01-08 13:34:23 -0800269static inline void spi_unregister_driver(struct spi_driver *sdrv)
270{
Ben Dooksddc1e972007-02-12 00:52:43 -0800271 if (sdrv)
272 driver_unregister(&sdrv->driver);
David Brownellb8852442006-01-08 13:34:23 -0800273}
274
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500275/* 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
Lars-Peter Clausen3acbb012011-11-16 10:13:37 +0100279/**
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)
David Brownellb8852442006-01-08 13:34:23 -0800290
David Brownell8ae12a02006-01-08 13:34:19 -0800291/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200292 * struct spi_controller - interface to SPI master or slave controller
Tony Jones49dce682007-10-16 01:27:48 -0700293 * @dev: device interface to this driver
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200294 * @list: link with the global spi_controller list
David Brownell8ae12a02006-01-08 13:34:19 -0800295 * @bus_num: board-specific (and often SOC-specific) identifier for a
David Brownell747d8442006-04-02 10:33:37 -0800296 * given SPI controller.
David Brownellb8852442006-01-08 13:34:23 -0800297 * @num_chipselect: chipselects are used to distinguish individual
David Brownell747d8442006-04-02 10:33:37 -0800298 * 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.
Mike Rapoportfd5e1912009-04-06 19:00:56 -0700301 * @dma_alignment: SPI controller constraint on DMA buffers alignment.
Randy Dunlapb73b2552009-09-22 16:46:00 -0700302 * @mode_bits: flags understood by this controller driver
Stephen Warren543bb252013-03-26 20:37:57 -0600303 * @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
Masanari Iidae2278672014-02-18 22:54:36 +0900305 * supported. If set, the SPI core will reject any transfer with an
Stephen Warren543bb252013-03-26 20:37:57 -0600306 * 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.
Mark Browna2fd4f92013-07-10 14:57:26 +0100308 * @min_speed_hz: Lowest supported transfer speed
309 * @max_speed_hz: Highest supported transfer speed
Randy Dunlapb73b2552009-09-22 16:46:00 -0700310 * @flags: other constraints relevant to this driver
Geert Uytterhoeven6c364062017-05-22 15:11:41 +0200311 * @slave: indicates that this is an SPI slave controller
Randy Dunlapee7683a2016-02-05 17:31:39 -0800312 * @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.
Heiner Kallweit5090cc6a2016-08-17 21:08:01 +0200314 * @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.
Mark Brownef4d96e2016-07-21 23:53:31 +0100316 * @io_mutex: mutex for physical bus access
Ernst Schwab5c79a5a2010-08-16 15:10:11 +0200317 * @bus_lock_spinlock: spinlock for SPI bus locking
Mark Brownef4d96e2016-07-21 23:53:31 +0100318 * @bus_lock_mutex: mutex for exclusion of multiple callers
Ernst Schwab5c79a5a2010-08-16 15:10:11 +0200319 * @bus_lock_flag: indicates that the SPI bus is locked for exclusive use
David Brownell8ae12a02006-01-08 13:34:19 -0800320 * @setup: updates the device mode and clocking records used by a
David Brownell80224562007-02-12 00:52:46 -0800321 * device's SPI controller; protocol code may call this. This
322 * must fail if an unrecognized or unsupported mode is requested.
David Brownell33e34dc2007-05-08 00:32:21 -0700323 * It's always safe to call this unless transfers are pending on
324 * the device whose settings are being modified.
David Brownell8ae12a02006-01-08 13:34:19 -0800325 * @transfer: adds a message to the controller's transfer queue.
326 * @cleanup: frees controller-specific state
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200327 * @can_dma: determine whether this controller supports DMA
328 * @queued: whether this controller is providing an internal message queue
Linus Walleijffbbdd212012-02-22 10:05:38 +0100329 * @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
Mark Brown0461a412014-12-09 21:38:05 +0000334 * @idling: the device is entering idle state
Linus Walleijffbbdd212012-02-22 10:05:38 +0100335 * @cur_msg: the currently in-flight message
Mark Brown2841a5f2013-10-05 00:23:12 +0100336 * @cur_msg_prepared: spi_prepare_message was called for the currently
337 * in-flight message
Thierry Reding2c675682014-08-08 13:02:36 +0200338 * @cur_msg_mapped: message has been mapped for DMA
Masanari Iidae2278672014-02-18 22:54:36 +0900339 * @xfer_completion: used by core transfer_one_message()
Linus Walleijffbbdd212012-02-22 10:05:38 +0100340 * @busy: message pump is busy
341 * @running: message pump is running
342 * @rt: whether this queue is set to run as a realtime task
Mark Brown49834de2013-07-28 14:47:02 +0100343 * @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
Mark Brown6ad45a22014-02-02 13:47:47 +0000346 * @max_dma_len: Maximum length of a DMA transfer for the device.
Linus Walleijffbbdd212012-02-22 10:05:38 +0100347 * @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
Baruch Siache9305332014-01-25 22:36:15 +0200354 * message
Randy Dunlapdbabe0d2012-04-17 17:03:50 -0700355 * @unprepare_transfer_hardware: there are currently no more messages on the
Linus Walleijffbbdd212012-02-22 10:05:38 +0100356 * queue so the subsystem notifies the driver that it may relax the
357 * hardware by issuing this call
Geert Uytterhoevenbd6857a2014-01-21 16:10:07 +0100358 * @set_cs: set the logic level of the chip select line. May be called
Mark Brownb1589352013-10-05 11:50:40 +0100359 * from interrupt context.
Mark Brown2841a5f2013-10-05 00:23:12 +0100360 * @prepare_message: set up the controller to transfer a single message,
361 * for example doing DMA mapping. Called from threaded
362 * context.
Geert Uytterhoeven05167122014-01-21 16:10:06 +0100363 * @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
Baruch Siach6e5f5262014-01-25 22:36:13 +0200368 * 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.
Geert Uytterhoevenff61eb42015-04-07 20:39:19 +0200372 * @handle_err: the subsystem calls the driver to handle an error that occurs
Andy Shevchenkob716c4f2015-02-27 17:34:15 +0200373 * in the generic implementation of transfer_one_message().
Boris Brezillonc36ff262018-04-26 18:18:14 +0200374 * @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.
Mark Brown2841a5f2013-10-05 00:23:12 +0100377 * @unprepare_message: undo any work done by prepare_message().
Geert Uytterhoeven6c364062017-05-22 15:11:41 +0200378 * @slave_abort: abort the ongoing transfer request on an SPI slave controller
Andreas Larsson095c3752013-01-29 15:53:41 +0100379 * @cs_gpios: Array of GPIOs to use as chip select lines; one per CS
Andreas Larsson446411e2013-02-13 14:20:25 +0100380 * number. Any individual value may be -ENOENT for CS lines that
Andreas Larsson095c3752013-01-29 15:53:41 +0100381 * are not GPIOs (driven by the SPI controller itself).
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200382 * @statistics: statistics for the spi_controller
Thierry Reding2c675682014-08-08 13:02:36 +0200383 * @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
Mika Westerberga0a90712016-02-08 17:14:28 +0200387 * @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.
David Brownell8ae12a02006-01-08 13:34:19 -0800390 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200391 * Each SPI controller can communicate with one or more @spi_device
David Brownell8ae12a02006-01-08 13:34:19 -0800392 * 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
David Brownell33e34dc2007-05-08 00:32:21 -0700398 * 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
David Brownell8ae12a02006-01-08 13:34:19 -0800400 * message's completion function when the transaction completes.
401 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200402struct spi_controller {
Tony Jones49dce682007-10-16 01:27:48 -0700403 struct device dev;
David Brownell8ae12a02006-01-08 13:34:19 -0800404
Feng Tang2b9603a2010-08-02 15:52:15 +0800405 struct list_head list;
406
David Brownella020ed72006-04-03 15:49:04 -0700407 /* other than negative (== assign one dynamically), bus_num is fully
David Brownell8ae12a02006-01-08 13:34:19 -0800408 * board-specific. usually that simplifies to being SOC-specific.
David Brownella020ed72006-04-03 15:49:04 -0700409 * example: one SOC has three SPI controllers, numbered 0..2,
David Brownell8ae12a02006-01-08 13:34:19 -0800410 * and one board's schematics might show it using SPI-2. software
411 * would normally use bus_num=2 for that controller.
412 */
David Brownella020ed72006-04-03 15:49:04 -0700413 s16 bus_num;
David Brownell8ae12a02006-01-08 13:34:19 -0800414
415 /* chipselects will be integral to many controllers; some others
416 * might use board-specific GPIOs.
417 */
418 u16 num_chipselect;
419
Mike Rapoportfd5e1912009-04-06 19:00:56 -0700420 /* some SPI controllers pose alignment requirements on DMAable
421 * buffers; let protocol drivers know about these requirements.
422 */
423 u16 dma_alignment;
424
David Brownelle7db06b2009-06-17 16:26:04 -0700425 /* spi_device.mode flags understood by this controller driver */
426 u16 mode_bits;
427
Stephen Warren543bb252013-03-26 20:37:57 -0600428 /* bitmask of supported bits_per_word for transfers */
429 u32 bits_per_word_mask;
Stephen Warren2922a8d2013-05-21 20:36:34 -0600430#define SPI_BPW_MASK(bits) BIT((bits) - 1)
Stephen Warrenb6aa23c2013-08-01 16:08:57 -0600431#define SPI_BIT_MASK(bits) (((bits) == 32) ? ~0U : (BIT(bits) - 1))
Stephen Warreneca89602013-05-30 09:59:40 -0600432#define SPI_BPW_RANGE_MASK(min, max) (SPI_BIT_MASK(max) - SPI_BIT_MASK(min - 1))
Stephen Warren543bb252013-03-26 20:37:57 -0600433
Mark Browna2fd4f92013-07-10 14:57:26 +0100434 /* limits on transfer speed */
435 u32 min_speed_hz;
436 u32 max_speed_hz;
437
David Brownell70d60272009-06-30 11:41:27 -0700438 /* other constraints relevant to this driver */
439 u16 flags;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200440#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 */
David Brownell70d60272009-06-30 11:41:27 -0700447
Geert Uytterhoeven6c364062017-05-22 15:11:41 +0200448 /* flag indicating this is an SPI slave controller */
449 bool slave;
450
Michal Suchanek4acad4a2015-12-02 10:38:21 +0000451 /*
Heiner Kallweit5090cc6a2016-08-17 21:08:01 +0200452 * on some hardware transfer / message size may be constrained
Michal Suchanek4acad4a2015-12-02 10:38:21 +0000453 * the limit may depend on device transfer settings
454 */
455 size_t (*max_transfer_size)(struct spi_device *spi);
Heiner Kallweit5090cc6a2016-08-17 21:08:01 +0200456 size_t (*max_message_size)(struct spi_device *spi);
Michal Suchanek4acad4a2015-12-02 10:38:21 +0000457
Mark Brownef4d96e2016-07-21 23:53:31 +0100458 /* I/O mutex */
459 struct mutex io_mutex;
460
Ernst Schwabcf32b71e2010-06-28 17:49:29 -0700461 /* 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
David Brownell6e538aa2009-04-21 12:24:49 -0700468 /* 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 */
David Brownell8ae12a02006-01-08 13:34:19 -0800474 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 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200484 * + The controller's main job is to process its message queue,
485 * selecting a chip (for masters), then transferring data
David Brownell8ae12a02006-01-08 13:34:19 -0800486 * + 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
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200498 /* called on release() to free memory provided by spi_controller */
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -0800499 void (*cleanup)(struct spi_device *spi);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100500
501 /*
Mark Brown99adef32014-01-16 12:22:43 +0000502 * 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 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200508 bool (*can_dma)(struct spi_controller *ctlr,
Mark Brown99adef32014-01-16 12:22:43 +0000509 struct spi_device *spi,
510 struct spi_transfer *xfer);
511
512 /*
Linus Walleijffbbdd212012-02-22 10:05:38 +0100513 * These hooks are for drivers that want to use the generic
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200514 * controller transfer queueing mechanism. If these are used, the
Linus Walleijffbbdd212012-02-22 10:05:38 +0100515 * 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;
Mark Brown0461a412014-12-09 21:38:05 +0000525 bool idling;
Linus Walleijffbbdd212012-02-22 10:05:38 +0100526 bool busy;
527 bool running;
528 bool rt;
Mark Brown49834de2013-07-28 14:47:02 +0100529 bool auto_runtime_pm;
Mark Brown2841a5f2013-10-05 00:23:12 +0100530 bool cur_msg_prepared;
Mark Brown99adef32014-01-16 12:22:43 +0000531 bool cur_msg_mapped;
Mark Brownb1589352013-10-05 11:50:40 +0100532 struct completion xfer_completion;
Mark Brown6ad45a22014-02-02 13:47:47 +0000533 size_t max_dma_len;
Linus Walleijffbbdd212012-02-22 10:05:38 +0100534
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200535 int (*prepare_transfer_hardware)(struct spi_controller *ctlr);
536 int (*transfer_one_message)(struct spi_controller *ctlr,
Linus Walleijffbbdd212012-02-22 10:05:38 +0100537 struct spi_message *mesg);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200538 int (*unprepare_transfer_hardware)(struct spi_controller *ctlr);
539 int (*prepare_message)(struct spi_controller *ctlr,
Mark Brown2841a5f2013-10-05 00:23:12 +0100540 struct spi_message *message);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200541 int (*unprepare_message)(struct spi_controller *ctlr,
Mark Brown2841a5f2013-10-05 00:23:12 +0100542 struct spi_message *message);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200543 int (*slave_abort)(struct spi_controller *ctlr);
Mark Brown49834de2013-07-28 14:47:02 +0100544
Mark Brownb1589352013-10-05 11:50:40 +0100545 /*
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);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200550 int (*transfer_one)(struct spi_controller *ctlr, struct spi_device *spi,
Mark Brownb1589352013-10-05 11:50:40 +0100551 struct spi_transfer *transfer);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200552 void (*handle_err)(struct spi_controller *ctlr,
Andy Shevchenkob716c4f2015-02-27 17:34:15 +0200553 struct spi_message *message);
Mark Brownb1589352013-10-05 11:50:40 +0100554
Boris Brezillonc36ff262018-04-26 18:18:14 +0200555 /* Optimized handlers for SPI memory-like operations. */
556 const struct spi_controller_mem_ops *mem_ops;
557
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100558 /* gpio chip select */
559 int *cs_gpios;
Mark Brown99adef32014-01-16 12:22:43 +0000560
Martin Sperleca2ebc2015-06-22 13:00:36 +0000561 /* statistics */
562 struct spi_statistics statistics;
563
Mark Brown99adef32014-01-16 12:22:43 +0000564 /* DMA channels for use with core dmaengine helpers */
565 struct dma_chan *dma_tx;
566 struct dma_chan *dma_rx;
Mark Brown3a2eba92014-01-28 20:17:03 +0000567
568 /* dummy data for full duplex devices */
569 void *dummy_rx;
570 void *dummy_tx;
Mika Westerberga0a90712016-02-08 17:14:28 +0200571
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200572 int (*fw_translate_cs)(struct spi_controller *ctlr, unsigned cs);
David Brownell8ae12a02006-01-08 13:34:19 -0800573};
574
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200575static inline void *spi_controller_get_devdata(struct spi_controller *ctlr)
David Brownell0c8684612006-01-08 13:34:25 -0800576{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200577 return dev_get_drvdata(&ctlr->dev);
David Brownell0c8684612006-01-08 13:34:25 -0800578}
579
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200580static inline void spi_controller_set_devdata(struct spi_controller *ctlr,
581 void *data)
David Brownell0c8684612006-01-08 13:34:25 -0800582{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200583 dev_set_drvdata(&ctlr->dev, data);
David Brownell0c8684612006-01-08 13:34:25 -0800584}
585
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200586static inline struct spi_controller *spi_controller_get(struct spi_controller *ctlr)
David Brownell0c8684612006-01-08 13:34:25 -0800587{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200588 if (!ctlr || !get_device(&ctlr->dev))
David Brownell0c8684612006-01-08 13:34:25 -0800589 return NULL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200590 return ctlr;
David Brownell0c8684612006-01-08 13:34:25 -0800591}
592
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200593static inline void spi_controller_put(struct spi_controller *ctlr)
David Brownell0c8684612006-01-08 13:34:25 -0800594{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200595 if (ctlr)
596 put_device(&ctlr->dev);
David Brownell0c8684612006-01-08 13:34:25 -0800597}
598
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200599static inline bool spi_controller_is_slave(struct spi_controller *ctlr)
Geert Uytterhoeven6c364062017-05-22 15:11:41 +0200600{
601 return IS_ENABLED(CONFIG_SPI_SLAVE) && ctlr->slave;
602}
603
Linus Walleijffbbdd212012-02-22 10:05:38 +0100604/* PM calls that need to be issued by the driver */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200605extern int spi_controller_suspend(struct spi_controller *ctlr);
606extern int spi_controller_resume(struct spi_controller *ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100607
608/* Calls the driver make to interact with the message queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200609extern 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);
David Brownell0c8684612006-01-08 13:34:25 -0800612
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200613/* 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);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +0200616
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200617static inline struct spi_controller *spi_alloc_master(struct device *host,
618 unsigned int size)
Geert Uytterhoeven6c364062017-05-22 15:11:41 +0200619{
620 return __spi_alloc_controller(host, size, false);
621}
622
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200623static inline struct spi_controller *spi_alloc_slave(struct device *host,
624 unsigned int size)
Geert Uytterhoeven6c364062017-05-22 15:11:41 +0200625{
626 if (!IS_ENABLED(CONFIG_SPI_SLAVE))
627 return NULL;
628
629 return __spi_alloc_controller(host, size, true);
630}
David Brownell8ae12a02006-01-08 13:34:19 -0800631
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200632extern 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);
David Brownell8ae12a02006-01-08 13:34:19 -0800636
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200637extern struct spi_controller *spi_busnum_to_master(u16 busnum);
David Brownell8ae12a02006-01-08 13:34:19 -0800638
Martin Sperld780c372015-12-14 15:20:18 +0000639/*
640 * SPI resource management while processing a SPI message
641 */
642
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200643typedef void (*spi_res_release_t)(struct spi_controller *ctlr,
Martin Sperld6497812016-02-18 15:53:10 +0000644 struct spi_message *msg,
645 void *res);
646
Martin Sperld780c372015-12-14 15:20:18 +0000647/**
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 */
Martin Sperld780c372015-12-14 15:20:18 +0000656struct 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
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200668extern void spi_res_release(struct spi_controller *ctlr,
Martin Sperld780c372015-12-14 15:20:18 +0000669 struct spi_message *message);
670
David Brownell8ae12a02006-01-08 13:34:19 -0800671/*---------------------------------------------------------------------------*/
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
Vitaly Wool8275c642006-01-08 13:34:28 -0800692 * @tx_buf: data to be written (dma-safe memory), or NULL
693 * @rx_buf: data to be read (dma-safe memory), or NULL
David Brownell33e34dc2007-05-08 00:32:21 -0700694 * @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
Masanari Iidae2278672014-02-18 22:54:36 +0900696 * @tx_nbits: number of bits used for writing. If 0 the default
wangyuhangf477b7f2013-08-11 18:15:17 +0800697 * (SPI_NBITS_SINGLE) is used.
698 * @rx_nbits: number of bits used for reading. If 0 the default
699 * (SPI_NBITS_SINGLE) is used.
David Brownell8ae12a02006-01-08 13:34:19 -0800700 * @len: size of rx and tx buffers (in bytes)
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200701 * @speed_hz: Select a speed other than the device default for this
David Brownell33e34dc2007-05-08 00:32:21 -0700702 * transfer. If 0 the default (from @spi_device) is used.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200703 * @bits_per_word: select a bits_per_word other than the device default
David Brownell33e34dc2007-05-08 00:32:21 -0700704 * for this transfer. If 0 the default (from @spi_device) is used.
David Brownell8ae12a02006-01-08 13:34:19 -0800705 * @cs_change: affects chipselect after this transfer completes
706 * @delay_usecs: microseconds to delay after this transfer before
David Brownell747d8442006-04-02 10:33:37 -0800707 * (optionally) changing the chipselect status, then starting
David Brownell33e34dc2007-05-08 00:32:21 -0700708 * the next transfer or completing this @spi_message.
Baolin Wangeeaceb82018-08-16 20:54:49 +0800709 * @word_delay: clock cycles to inter word delay after each word size
710 * (set by bits_per_word) transmission.
David Brownell33e34dc2007-05-08 00:32:21 -0700711 * @transfer_list: transfers are sequenced through @spi_message.transfers
Mark Brown6ad45a22014-02-02 13:47:47 +0000712 * @tx_sg: Scatterlist for transmit, currently not for client use
713 * @rx_sg: Scatterlist for receive, currently not for client use
David Brownell8ae12a02006-01-08 13:34:19 -0800714 *
715 * SPI transfers always write the same number of bytes as they read.
David Brownell33e34dc2007-05-08 00:32:21 -0700716 * Protocol drivers should always provide @rx_buf and/or @tx_buf.
David Brownell8ae12a02006-01-08 13:34:19 -0800717 * 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 *
David Brownell4b1badf2006-12-29 16:48:39 -0800721 * If the transmit buffer is null, zeroes will be shifted out
David Brownell33e34dc2007-05-08 00:32:21 -0700722 * while filling @rx_buf. If the receive buffer is null, the data
Vitaly Wool8275c642006-01-08 13:34:28 -0800723 * 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 *
David Brownell80224562007-02-12 00:52:46 -0800728 * 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
David Brownell33e34dc2007-05-08 00:32:21 -0700731 * (@len = 2N) and hold N sixteen bit words in CPU byte order.
David Brownell80224562007-02-12 00:52:46 -0800732 *
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 *
Vitaly Wool8275c642006-01-08 13:34:28 -0800738 * All SPI transfers start with the relevant chipselect active. Normally
739 * it stays selected until after the last transfer in a message. Drivers
David Brownell33e34dc2007-05-08 00:32:21 -0700740 * can affect the chipselect signal using cs_change.
David Brownell8ae12a02006-01-08 13:34:19 -0800741 *
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
David Brownellf5a9c772007-06-16 10:16:08 -0700749 * 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.
David Brownell0c8684612006-01-08 13:34:25 -0800757 *
Masanari Iidae2278672014-02-18 22:54:36 +0900758 * When SPI can transfer in 1x,2x or 4x. It can get this transfer information
wangyuhangf477b7f2013-08-11 18:15:17 +0800759 * 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 *
David Brownell0c8684612006-01-08 13:34:25 -0800763 * 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
Vitaly Wool8275c642006-01-08 13:34:28 -0800766 * insulate against future API updates. After you submit a message
767 * and its transfers, ignore them until its completion callback.
David Brownell8ae12a02006-01-08 13:34:19 -0800768 */
769struct spi_transfer {
770 /* it's ok if tx_buf == rx_buf (right?)
771 * for MicroWire, one buffer must be null
David Brownell0c8684612006-01-08 13:34:25 -0800772 * buffers must work with dma_*map_single() calls, unless
773 * spi_message.is_dma_mapped reports a pre-existing mapping
David Brownell8ae12a02006-01-08 13:34:19 -0800774 */
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;
Mark Brown6ad45a22014-02-02 13:47:47 +0000781 struct sg_table tx_sg;
782 struct sg_table rx_sg;
David Brownell8ae12a02006-01-08 13:34:19 -0800783
784 unsigned cs_change:1;
Mark Brownd3fbd452014-01-10 17:09:53 +0000785 unsigned tx_nbits:3;
786 unsigned rx_nbits:3;
wangyuhangf477b7f2013-08-11 18:15:17 +0800787#define SPI_NBITS_SINGLE 0x01 /* 1bit transfer */
788#define SPI_NBITS_DUAL 0x02 /* 2bits transfer */
789#define SPI_NBITS_QUAD 0x04 /* 4bits transfer */
Imre Deak4cff33f2006-02-17 10:02:18 -0800790 u8 bits_per_word;
David Brownell8ae12a02006-01-08 13:34:19 -0800791 u16 delay_usecs;
Imre Deak4cff33f2006-02-17 10:02:18 -0800792 u32 speed_hz;
Baolin Wangeeaceb82018-08-16 20:54:49 +0800793 u16 word_delay;
Vitaly Wool8275c642006-01-08 13:34:28 -0800794
795 struct list_head transfer_list;
David Brownell8ae12a02006-01-08 13:34:19 -0800796};
797
798/**
799 * struct spi_message - one multi-segment SPI transaction
Vitaly Wool8275c642006-01-08 13:34:28 -0800800 * @transfers: list of transfer segments in this transaction
David Brownell8ae12a02006-01-08 13:34:19 -0800801 * @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
Thierry Reding2c675682014-08-08 13:02:36 +0200806 * @frame_length: the total number of bytes in the message
David Brownellb8852442006-01-08 13:34:23 -0800807 * @actual_length: the total number of bytes that were transferred in all
808 * successful segments
David Brownell8ae12a02006-01-08 13:34:19 -0800809 * @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
Martin Sperld780c372015-12-14 15:20:18 +0000812 * @resources: for resource management when the spi message is processed
David Brownell0c8684612006-01-08 13:34:25 -0800813 *
David Brownell33e34dc2007-05-08 00:32:21 -0700814 * A @spi_message is used to execute an atomic sequence of data transfers,
Vitaly Wool8275c642006-01-08 13:34:28 -0800815 * 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
Marcin Bisc6331ba2015-03-01 13:49:32 +0100820 * sent to a given spi_device are always executed in FIFO order.
Vitaly Wool8275c642006-01-08 13:34:28 -0800821 *
David Brownell0c8684612006-01-08 13:34:25 -0800822 * 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
Vitaly Wool8275c642006-01-08 13:34:28 -0800825 * insulate against future API updates. After you submit a message
826 * and its transfers, ignore them until its completion callback.
David Brownell8ae12a02006-01-08 13:34:19 -0800827 */
828struct spi_message {
David Brownell747d8442006-04-02 10:33:37 -0800829 struct list_head transfers;
David Brownell8ae12a02006-01-08 13:34:19 -0800830
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
David Brownellb8852442006-01-08 13:34:23 -0800842 * others (with multi-message pipelines) could need a flag to
David Brownell8ae12a02006-01-08 13:34:19 -0800843 * tell them about such special cases.
844 */
845
846 /* completion is reported through a callback */
David Brownell747d8442006-04-02 10:33:37 -0800847 void (*complete)(void *context);
David Brownell8ae12a02006-01-08 13:34:19 -0800848 void *context;
Sourav Poddar078726c2013-07-18 15:31:25 +0530849 unsigned frame_length;
David Brownell8ae12a02006-01-08 13:34:19 -0800850 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
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200855 * complete(), that's the spi_controller controller driver.
David Brownell8ae12a02006-01-08 13:34:19 -0800856 */
857 struct list_head queue;
858 void *state;
Martin Sperld780c372015-12-14 15:20:18 +0000859
860 /* list of spi_res reources when the spi message is processed */
861 struct list_head resources;
David Brownell8ae12a02006-01-08 13:34:19 -0800862};
863
Martin Sperl49ddedf2015-11-27 13:56:03 +0000864static inline void spi_message_init_no_memset(struct spi_message *m)
865{
866 INIT_LIST_HEAD(&m->transfers);
Martin Sperld780c372015-12-14 15:20:18 +0000867 INIT_LIST_HEAD(&m->resources);
Martin Sperl49ddedf2015-11-27 13:56:03 +0000868}
869
Vitaly Wool8275c642006-01-08 13:34:28 -0800870static inline void spi_message_init(struct spi_message *m)
871{
872 memset(m, 0, sizeof *m);
Martin Sperl49ddedf2015-11-27 13:56:03 +0000873 spi_message_init_no_memset(m);
Vitaly Wool8275c642006-01-08 13:34:28 -0800874}
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
Lars-Peter Clausen6d9eecd2013-01-09 17:31:00 +0000888/**
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
David Brownell0c8684612006-01-08 13:34:25 -0800908/* 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) {
Shubhrajyoti D8f536022012-02-27 19:29:05 +0530920 unsigned i;
Vitaly Wool8275c642006-01-08 13:34:28 -0800921 struct spi_transfer *t = (struct spi_transfer *)(m + 1);
922
Emiliano Ingrassiaed77d6b2017-03-28 09:49:29 +0200923 spi_message_init_no_memset(m);
Vitaly Wool8275c642006-01-08 13:34:28 -0800924 for (i = 0; i < ntrans; i++, t++)
925 spi_message_add_tail(t, m);
David Brownell0c8684612006-01-08 13:34:25 -0800926 }
927 return m;
928}
929
930static inline void spi_message_free(struct spi_message *m)
931{
932 kfree(m);
933}
934
David Brownell7d077192009-06-17 16:26:03 -0700935extern int spi_setup(struct spi_device *spi);
David Brownell568d0692009-09-22 16:46:18 -0700936extern int spi_async(struct spi_device *spi, struct spi_message *message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -0700937extern int spi_async_locked(struct spi_device *spi,
938 struct spi_message *message);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +0200939extern int spi_slave_abort(struct spi_device *spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800940
Michal Suchanek4acad4a2015-12-02 10:38:21 +0000941static inline size_t
Heiner Kallweit5090cc6a2016-08-17 21:08:01 +0200942spi_max_message_size(struct spi_device *spi)
943{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200944 struct spi_controller *ctlr = spi->controller;
945
946 if (!ctlr->max_message_size)
Heiner Kallweit5090cc6a2016-08-17 21:08:01 +0200947 return SIZE_MAX;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200948 return ctlr->max_message_size(spi);
Heiner Kallweit5090cc6a2016-08-17 21:08:01 +0200949}
950
951static inline size_t
Michal Suchanek4acad4a2015-12-02 10:38:21 +0000952spi_max_transfer_size(struct spi_device *spi)
953{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200954 struct spi_controller *ctlr = spi->controller;
Heiner Kallweit5090cc6a2016-08-17 21:08:01 +0200955 size_t tr_max = SIZE_MAX;
956 size_t msg_max = spi_max_message_size(spi);
957
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200958 if (ctlr->max_transfer_size)
959 tr_max = ctlr->max_transfer_size(spi);
Heiner Kallweit5090cc6a2016-08-17 21:08:01 +0200960
961 /* transfer size limit must not be greater than messsage size limit */
962 return min(tr_max, msg_max);
Michal Suchanek4acad4a2015-12-02 10:38:21 +0000963}
964
David Brownell8ae12a02006-01-08 13:34:19 -0800965/*---------------------------------------------------------------------------*/
966
Martin Sperl523baf5a2015-12-14 15:20:19 +0000967/* SPI transfer replacement methods which make use of spi_res */
968
Javier Martinez Canillasc76d9ae2016-03-10 15:01:13 -0300969struct spi_replaced_transfers;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200970typedef void (*spi_replaced_release_t)(struct spi_controller *ctlr,
Javier Martinez Canillasc76d9ae2016-03-10 15:01:13 -0300971 struct spi_message *msg,
972 struct spi_replaced_transfers *res);
Martin Sperl523baf5a2015-12-14 15:20:19 +0000973/**
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 */
Martin Sperl523baf5a2015-12-14 15:20:19 +0000992struct 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
Martin Sperld9f12122015-12-14 15:20:20 +00001012/* SPI transfer transformation methods */
1013
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001014extern int spi_split_transfers_maxsize(struct spi_controller *ctlr,
Martin Sperld9f12122015-12-14 15:20:20 +00001015 struct spi_message *msg,
1016 size_t maxsize,
1017 gfp_t gfp);
1018
1019/*---------------------------------------------------------------------------*/
1020
David Brownell8ae12a02006-01-08 13:34:19 -08001021/* 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);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07001027extern int spi_sync_locked(struct spi_device *spi, struct spi_message *message);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001028extern int spi_bus_lock(struct spi_controller *ctlr);
1029extern int spi_bus_unlock(struct spi_controller *ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08001030
1031/**
Lars-Peter Clausen6d9eecd2013-01-09 17:31:00 +00001032 * 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 *
Javier Martinez Canillasa1fdeaa2015-10-22 18:59:22 +02001042 * Return: Return: zero on success, else a negative error code.
Lars-Peter Clausen6d9eecd2013-01-09 17:31:00 +00001043 */
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
Geert Uytterhoeven323117a2016-09-13 15:38:25 +02001055/**
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
David Brownell0c8684612006-01-08 13:34:25 -08001101/* this copies txbuf and rxbuf data; for small transfers only! */
David Brownell8ae12a02006-01-08 13:34:19 -08001102extern int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02001103 const void *txbuf, unsigned n_tx,
1104 void *rxbuf, unsigned n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08001105
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
David Brownell33e34dc2007-05-08 00:32:21 -07001110 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001111 *
Javier Martinez Canillasa1fdeaa2015-10-22 18:59:22 +02001112 * 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.
David Brownell8ae12a02006-01-08 13:34:19 -08001116 */
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
David Brownell33e34dc2007-05-08 00:32:21 -07001132 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001133 *
David Brownell8ae12a02006-01-08 13:34:19 -08001134 * The number is returned in wire-order, which is at least sometimes
1135 * big-endian.
Javier Martinez Canillasa1fdeaa2015-10-22 18:59:22 +02001136 *
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.
David Brownell8ae12a02006-01-08 13:34:19 -08001141 */
1142static inline ssize_t spi_w8r16(struct spi_device *spi, u8 cmd)
1143{
1144 ssize_t status;
1145 u16 result;
1146
Geert Uytterhoeven269ccca2014-01-12 13:59:06 +01001147 status = spi_write_then_read(spi, &cmd, 1, &result, 2);
David Brownell8ae12a02006-01-08 13:34:19 -08001148
1149 /* return negative errno or unsigned value */
1150 return (status < 0) ? status : result;
1151}
1152
Lars-Peter Clausen05071aa2013-09-27 16:34:27 +02001153/**
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 *
Lars-Peter Clausen05071aa2013-09-27 16:34:27 +02001159 * 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 *
Javier Martinez Canillasa1fdeaa2015-10-22 18:59:22 +02001162 * 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.
Lars-Peter Clausen05071aa2013-09-27 16:34:27 +02001166 */
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
David Brownell8ae12a02006-01-08 13:34:19 -08001180/*---------------------------------------------------------------------------*/
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
David Brownell2604288f2007-07-31 00:39:44 -07001196/**
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.
Dmitry Torokhov826cf172017-02-28 14:25:18 -08001201 * @properties: Additional device properties for the device.
David Brownell2604288f2007-07-31 00:39:44 -07001202 * @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.
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001207 * @bus_num: Identifies which spi_controller parents the spi_device; unused
David Brownell2604288f2007-07-31 00:39:44 -07001208 * 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 */
David Brownell8ae12a02006-01-08 13:34:19 -08001228struct 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,
David Brownellb8852442006-01-08 13:34:23 -08001233 * controller_data goes to spi_device.controller_data,
Dmitry Torokhov826cf172017-02-28 14:25:18 -08001234 * device properties are copied and attached to spi_device,
David Brownell8ae12a02006-01-08 13:34:19 -08001235 * irq is copied too
1236 */
Anton Vorontsov75368bf2009-09-22 16:46:04 -07001237 char modalias[SPI_NAME_SIZE];
David Brownell8ae12a02006-01-08 13:34:19 -08001238 const void *platform_data;
Dmitry Torokhov826cf172017-02-28 14:25:18 -08001239 const struct property_entry *properties;
David Brownellb8852442006-01-08 13:34:23 -08001240 void *controller_data;
David Brownell8ae12a02006-01-08 13:34:19 -08001241 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
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001248 * spi_controller that will probably be registered later.
David Brownell8ae12a02006-01-08 13:34:19 -08001249 *
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
David Brownell980a01c2006-06-28 07:47:15 -07001256 /* mode becomes spi_device.mode, and is essential for chips
1257 * where the default of SPI_CS_HIGH = 0 is wrong.
1258 */
wangyuhangf477b7f2013-08-11 18:15:17 +08001259 u16 mode;
David Brownell980a01c2006-06-28 07:47:15 -07001260
David Brownell8ae12a02006-01-08 13:34:19 -08001261 /* ... 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:
David Brownell8ae12a02006-01-08 13:34:19 -08001264 * - 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
David Brownell8ae12a02006-01-08 13:34:19 -08001278/* If you're hotplugging an adapter with devices (parport, usb, etc)
David Brownell0c8684612006-01-08 13:34:25 -08001279 * use spi_new_device() to describe each device. You can also call
1280 * spi_unregister_device() to start making that device vanish, but
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001281 * normally that would be handled by spi_unregister_controller().
Grant Likelydc87c982008-05-15 16:50:22 -06001282 *
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.
David Brownell8ae12a02006-01-08 13:34:19 -08001288 */
1289extern struct spi_device *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001290spi_alloc_device(struct spi_controller *ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -06001291
1292extern int
1293spi_add_device(struct spi_device *spi);
1294
1295extern struct spi_device *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001296spi_new_device(struct spi_controller *, struct spi_board_info *);
David Brownell8ae12a02006-01-08 13:34:19 -08001297
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +01001298extern void spi_unregister_device(struct spi_device *spi);
David Brownell8ae12a02006-01-08 13:34:19 -08001299
Anton Vorontsov75368bf2009-09-22 16:46:04 -07001300extern const struct spi_device_id *
1301spi_get_device_id(const struct spi_device *sdev);
1302
Beniamino Galvanib6713582014-11-22 16:21:39 +01001303static inline bool
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001304spi_transfer_is_last(struct spi_controller *ctlr, struct spi_transfer *xfer)
Beniamino Galvanib6713582014-11-22 16:21:39 +01001305{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001306 return list_is_last(&xfer->transfer_list, &ctlr->cur_msg->transfers);
Beniamino Galvanib6713582014-11-22 16:21:39 +01001307}
1308
Marco Felsch5f143af2018-09-25 11:42:29 +02001309/* 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) */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001325
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
David Brownell8ae12a02006-01-08 13:34:19 -08001348#endif /* __LINUX_SPI_H */