/src/barebox/include/spi/spi.h
Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-2.0-only */ |
2 | | #ifndef __INCLUDE_SPI_H |
3 | | #define __INCLUDE_SPI_H |
4 | | |
5 | | #include <driver.h> |
6 | | #include <slice.h> |
7 | | #include <linux/err.h> |
8 | | #include <linux/kernel.h> |
9 | | #include <linux/string.h> |
10 | | #include <linux/bitops.h> |
11 | | #include <linux/gpio/consumer.h> |
12 | | |
13 | | struct spi_controller; |
14 | | struct spi_transfer; |
15 | | struct spi_controller_mem_ops; |
16 | | struct spi_message; |
17 | | |
18 | | /** |
19 | | * struct spi_delay - SPI delay information |
20 | | * @value: Value for the delay |
21 | | * @unit: Unit for the delay |
22 | | */ |
23 | | struct spi_delay { |
24 | 0 | #define SPI_DELAY_UNIT_USECS 0 |
25 | 0 | #define SPI_DELAY_UNIT_NSECS 1 |
26 | 0 | #define SPI_DELAY_UNIT_SCK 2 |
27 | | u16 value; |
28 | | u8 unit; |
29 | | }; |
30 | | |
31 | | extern int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer); |
32 | | extern int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer); |
33 | | |
34 | | struct spi_board_info { |
35 | | char *name; |
36 | | int max_speed_hz; |
37 | | int bus_num; |
38 | | int chip_select; |
39 | | |
40 | | /* mode becomes spi_device.mode, and is essential for chips |
41 | | * where the default of SPI_CS_HIGH = 0 is wrong. |
42 | | */ |
43 | | u8 mode; |
44 | | u8 bits_per_word; |
45 | | void *platform_data; |
46 | | struct device_node *device_node; |
47 | | |
48 | | /* CS delays */ |
49 | | struct spi_delay cs_setup; |
50 | | struct spi_delay cs_hold; |
51 | | struct spi_delay cs_inactive; |
52 | | }; |
53 | | |
54 | | /** |
55 | | * struct spi_device - Controller side proxy for an SPI slave device |
56 | | * @dev: Driver model representation of the device. |
57 | | * @controller: SPI controller used with the device. |
58 | | * @master: Copy of controller, for backwards compatibility |
59 | | * @max_speed_hz: Maximum clock rate to be used with this chip |
60 | | * (on this board); may be changed by the device's driver. |
61 | | * The spi_transfer.speed_hz can override this for each transfer. |
62 | | * @chip_select: Chipselect, distinguishing chips handled by @controller. |
63 | | * @mode: The spi mode defines how data is clocked out and in. |
64 | | * This may be changed by the device's driver. |
65 | | * The "active low" default for chipselect mode can be overridden |
66 | | * (by specifying SPI_CS_HIGH) as can the "MSB first" default for |
67 | | * each word in a transfer (by specifying SPI_LSB_FIRST). |
68 | | * @bits_per_word: Data transfers involve one or more words; word sizes |
69 | | * like eight or 12 bits are common. In-memory wordsizes are |
70 | | * powers of two bytes (e.g. 20 bit samples use 32 bits). |
71 | | * This may be changed by the device's driver, or left at the |
72 | | * default (0) indicating protocol words are eight bit bytes. |
73 | | * The spi_transfer.bits_per_word can override this for each transfer |
74 | | * (FIXME: not currently implemented by most drivers). |
75 | | * @irq: Negative, or the number passed to request_irq() to receive |
76 | | * interrupts from this device. |
77 | | * @controller_state: Controller's runtime state |
78 | | * @controller_data: Board-specific definitions for controller, such as |
79 | | * FIFO initialization parameters; from board_info.controller_data |
80 | | * @modalias: Name of the driver to use with this device, or an alias |
81 | | * for that name. This appears in the sysfs "modalias" attribute |
82 | | * for driver coldplugging, and in uevents used for hotplugging |
83 | | * |
84 | | * A @spi_device is used to interchange data between an SPI slave |
85 | | * (usually a discrete chip) and CPU memory. |
86 | | * |
87 | | * In @dev, the platform_data is used to hold information about this |
88 | | * device that's meaningful to the device's protocol driver, but not |
89 | | * to its controller. One example might be an identifier for a chip |
90 | | * variant with slightly different functionality; another might be |
91 | | * information about how this particular board wires the chip's pins. |
92 | | */ |
93 | | struct spi_device { |
94 | | struct device dev; |
95 | | struct spi_controller *controller; |
96 | | struct spi_controller *master; /* compatibility layer */ |
97 | | struct spi_mem *mem; |
98 | | u32 max_speed_hz; |
99 | | u8 chip_select; |
100 | | u8 mode; |
101 | 0 | #define SPI_CPHA 0x01 /* clock phase */ |
102 | 0 | #define SPI_CPOL 0x02 /* clock polarity */ |
103 | 0 | #define SPI_MODE_0 (0|0) /* (original MicroWire) */ |
104 | | #define SPI_MODE_1 (0|SPI_CPHA) |
105 | | #define SPI_MODE_2 (SPI_CPOL|0) |
106 | | #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) |
107 | 0 | #define SPI_CS_HIGH 0x04 /* chipselect active high? */ |
108 | | #define SPI_LSB_FIRST 0x08 /* per-word bits-on-wire */ |
109 | 0 | #define SPI_3WIRE 0x10 /* SI/SO signals shared */ |
110 | | #define SPI_LOOP 0x20 /* loopback mode */ |
111 | 0 | #define SPI_NO_CS 0x40 /* 1 dev/bus, no chipselect */ |
112 | | #define SPI_READY 0x80 /* slave pulls low to pause */ |
113 | 0 | #define SPI_TX_DUAL 0x100 /* transmit with 2 wires */ |
114 | 0 | #define SPI_TX_QUAD 0x200 /* transmit with 4 wires */ |
115 | 0 | #define SPI_RX_DUAL 0x400 /* receive with 2 wires */ |
116 | 0 | #define SPI_RX_QUAD 0x800 /* receive with 4 wires */ |
117 | | #define SPI_CS_WORD 0x1000 /* toggle cs after each word */ |
118 | 0 | #define SPI_TX_OCTAL 0x2000 /* transmit with 8 wires */ |
119 | 0 | #define SPI_RX_OCTAL 0x4000 /* receive with 8 wires */ |
120 | | #define SPI_3WIRE_HIZ 0x8000 /* high impedance turnaround */ |
121 | | u8 bits_per_word; |
122 | | int irq; |
123 | | void *controller_state; |
124 | | void *controller_data; |
125 | | const char *modalias; |
126 | | struct gpio_desc *cs_gpiod; /* Chip select gpio desc */ |
127 | | /* CS delays */ |
128 | | struct spi_delay cs_setup; |
129 | | struct spi_delay cs_hold; |
130 | | struct spi_delay cs_inactive; |
131 | | |
132 | | /* |
133 | | * likely need more hooks for more protocol options affecting how |
134 | | * the controller talks to each chip, like: |
135 | | * - memory packing (12 bit samples into low bits, others zeroed) |
136 | | * - priority |
137 | | * - drop chipselect after each word |
138 | | * - chipselect delays |
139 | | * - ... |
140 | | */ |
141 | | }; |
142 | | |
143 | | static inline struct spi_device *to_spi_device(struct device *dev) |
144 | 0 | { |
145 | 0 | return dev ? container_of(dev, struct spi_device, dev) : NULL; |
146 | 0 | } Unexecuted instantiation: m25p80.c:to_spi_device Unexecuted instantiation: spi.c:to_spi_device Unexecuted instantiation: spi-mem.c:to_spi_device Unexecuted instantiation: gpio_spi.c:to_spi_device Unexecuted instantiation: at25.c:to_spi_device |
147 | | |
148 | | static inline u8 spi_get_chipselect(const struct spi_device *spi, u8 idx) |
149 | 0 | { |
150 | 0 | BUG_ON(idx != 0); |
151 | 0 |
|
152 | 0 | return spi->chip_select; |
153 | 0 | } Unexecuted instantiation: m25p80.c:spi_get_chipselect Unexecuted instantiation: spi.c:spi_get_chipselect Unexecuted instantiation: spi-mem.c:spi_get_chipselect Unexecuted instantiation: gpio_spi.c:spi_get_chipselect Unexecuted instantiation: at25.c:spi_get_chipselect |
154 | | |
155 | | /* ctldata is for the bus_controller driver's runtime state */ |
156 | | static inline void *spi_get_ctldata(const struct spi_device *spi) |
157 | 0 | { |
158 | 0 | return spi->controller_state; |
159 | 0 | } Unexecuted instantiation: m25p80.c:spi_get_ctldata Unexecuted instantiation: spi.c:spi_get_ctldata Unexecuted instantiation: spi-mem.c:spi_get_ctldata Unexecuted instantiation: gpio_spi.c:spi_get_ctldata Unexecuted instantiation: at25.c:spi_get_ctldata |
160 | | |
161 | | static inline void spi_set_ctldata(struct spi_device *spi, void *state) |
162 | 0 | { |
163 | 0 | spi->controller_state = state; |
164 | 0 | } Unexecuted instantiation: m25p80.c:spi_set_ctldata Unexecuted instantiation: spi.c:spi_set_ctldata Unexecuted instantiation: spi-mem.c:spi_set_ctldata Unexecuted instantiation: gpio_spi.c:spi_set_ctldata Unexecuted instantiation: at25.c:spi_set_ctldata |
165 | | |
166 | | /** |
167 | | * struct spi_controller - interface to SPI master or slave controller |
168 | | * @dev: device interface to this driver |
169 | | * @bus_num: board-specific (and often SOC-specific) identifier for a |
170 | | * given SPI controller. |
171 | | * @mem_ops: optimized/dedicated operations for interactions with SPI |
172 | | * memory. This field is optional and should only be implemented |
173 | | * if the controller has native support for memory like operations. |
174 | | * @max_transfer_size: function that returns the max transfer size for |
175 | | * a &spi_device; may be %NULL, so the default %SIZE_MAX will be used. |
176 | | * @max_message_size: function that returns the max message size for |
177 | | * a &spi_device; may be %NULL, so the default %SIZE_MAX will be used. |
178 | | * @num_chipselect: chipselects are used to distinguish individual |
179 | | * SPI slaves, and are numbered from zero to num_chipselects. |
180 | | * each slave has a chipselect signal, but it's common that not |
181 | | * every chipselect is connected to a slave. |
182 | | * @bits_per_word_mask: A mask indicating which values of bits_per_word are |
183 | | * supported by the driver. Bit n indicates that a bits_per_word n+1 is |
184 | | * supported. If set, the SPI core will reject any transfer with an |
185 | | * unsupported bits_per_word. If not set, this value is simply ignored, |
186 | | * and it's up to the individual driver to perform any validation. |
187 | | * @max_speed_hz: Highest supported transfer speed |
188 | | * @flags: other constraints relevant to this driver |
189 | | * @setup: updates the device mode and clocking records used by a |
190 | | * device's SPI controller; protocol code may call this. This |
191 | | * must fail if an unrecognized or unsupported mode is requested. |
192 | | * It's always safe to call this unless transfers are pending on |
193 | | * the device whose settings are being modified. |
194 | | * @set_cs_timing: optional hook for SPI devices to request SPI master |
195 | | * controller for configuring specific CS setup time, hold time and inactive |
196 | | * delay interms of clock counts |
197 | | * @transfer: adds a message to the controller's transfer queue. |
198 | | * @cleanup: frees controller-specific state |
199 | | * @set_cs: set the logic level of the chip select line. May be called |
200 | | * from interrupt context. |
201 | | * @prepare_message: set up the controller to transfer a single message, |
202 | | * for example doing DMA mapping. Called from threaded |
203 | | * context. |
204 | | * @transfer_one: transfer a single spi_transfer. |
205 | | * |
206 | | * - return 0 if the transfer is finished, |
207 | | * - return 1 if the transfer is still in progress. When |
208 | | * the driver is finished with this transfer it must |
209 | | * call spi_finalize_current_transfer() so the subsystem |
210 | | * can issue the next transfer. If the transfer fails, the |
211 | | * driver must set the flag SPI_TRANS_FAIL_IO to |
212 | | * spi_transfer->error first, before calling |
213 | | * spi_finalize_current_transfer(). |
214 | | * Note: transfer_one and transfer_one_message are mutually |
215 | | * exclusive; when both are set, the generic subsystem does |
216 | | * not call your transfer_one callback. |
217 | | * @handle_err: the subsystem calls the driver to handle an error that occurs |
218 | | * in the generic implementation of transfer_one_message(). |
219 | | * @cs_gpiods: Array of GPIO descriptors to use as chip select lines; one per CS |
220 | | * number. Any individual value may be NULL for CS lines that |
221 | | * are not GPIOs (driven by the SPI controller itself). |
222 | | * @use_gpio_descriptors: Turns on the code in the SPI core to parse and grab |
223 | | * GPIO descriptors. This will fill in @cs_gpiods and SPI devices will have |
224 | | * the cs_gpiod assigned if a GPIO line is found for the chipselect. |
225 | | * @list: link with the global spi_controller list |
226 | | * |
227 | | * Each SPI controller can communicate with one or more @spi_device |
228 | | * children. These make a small bus, sharing MOSI, MISO and SCK signals |
229 | | * but not chip select signals. Each device may be configured to use a |
230 | | * different clock rate, since those shared signals are ignored unless |
231 | | * the chip is selected. |
232 | | * |
233 | | * The driver for an SPI controller manages access to those devices through |
234 | | * a queue of spi_message transactions, copying data between CPU memory and |
235 | | * an SPI slave device. For each such message it queues, it calls the |
236 | | * message's completion function when the transaction completes. |
237 | | */ |
238 | | struct spi_controller { |
239 | | struct device *dev; |
240 | | |
241 | | struct slice slice; |
242 | | |
243 | | /* other than negative (== assign one dynamically), bus_num is fully |
244 | | * board-specific. usually that simplifies to being SOC-specific. |
245 | | * example: one SOC has three SPI controllers, numbered 0..2, |
246 | | * and one board's schematics might show it using SPI-2. software |
247 | | * would normally use bus_num=2 for that controller. |
248 | | */ |
249 | | s16 bus_num; |
250 | | |
251 | | /* Optimized handlers for SPI memory-like operations */ |
252 | | const struct spi_controller_mem_ops *mem_ops; |
253 | | |
254 | | /* Bitmask of supported bits_per_word for transfers */ |
255 | | u32 bits_per_word_mask; |
256 | | #define SPI_BPW_MASK(bits) BIT((bits) - 1) |
257 | | #define SPI_BPW_RANGE_MASK(min, max) GENMASK((max) - 1, (min) - 1) |
258 | | |
259 | | /* |
260 | | * on some hardware transfer size may be constrained |
261 | | * the limit may depend on device transfer settings |
262 | | */ |
263 | | size_t (*max_transfer_size)(struct spi_device *spi); |
264 | | size_t (*max_message_size)(struct spi_device *spi); |
265 | | |
266 | | /* chipselects will be integral to many controllers; some others |
267 | | * might use board-specific GPIOs. |
268 | | */ |
269 | | u16 num_chipselect; |
270 | | |
271 | | /* limits on transfer speed */ |
272 | | u32 max_speed_hz; |
273 | | |
274 | | /* Other constraints relevant to this driver */ |
275 | | u16 flags; |
276 | | #define SPI_CONTROLLER_HALF_DUPLEX BIT(0) /* Can't do full duplex */ |
277 | | #define SPI_CONTROLLER_NO_RX BIT(1) /* Can't do buffer read */ |
278 | | #define SPI_CONTROLLER_NO_TX BIT(2) /* Can't do buffer write */ |
279 | | #define SPI_CONTROLLER_MUST_RX BIT(3) /* Requires rx */ |
280 | | #define SPI_CONTROLLER_MUST_TX BIT(4) /* Requires tx */ |
281 | 0 | #define SPI_CONTROLLER_GPIO_SS BIT(5) /* GPIO CS must select slave */ |
282 | | #define SPI_CONTROLLER_SUSPENDED BIT(6) /* Currently suspended */ |
283 | | |
284 | | /* setup mode and clock, etc (spi driver may call many times) */ |
285 | | int (*setup)(struct spi_device *spi); |
286 | | |
287 | | /* |
288 | | * set_cs_timing() method is for SPI controllers that supports |
289 | | * configuring CS timing. |
290 | | * |
291 | | * This hook allows SPI client drivers to request SPI controllers |
292 | | * to configure specific CS timing through spi_set_cs_timing(). |
293 | | */ |
294 | | int (*set_cs_timing)(struct spi_device *spi); |
295 | | |
296 | | /* bidirectional bulk transfers |
297 | | * |
298 | | * + The transfer() method may not sleep; its main role is |
299 | | * just to add the message to the queue. |
300 | | * + For now there's no remove-from-queue operation, or |
301 | | * any other request management |
302 | | * + To a given spi_device, message queueing is pure fifo |
303 | | * |
304 | | * + The controller's main job is to process its message queue, |
305 | | * selecting a chip then transferring data |
306 | | * + If there are multiple spi_device children, the i/o queue |
307 | | * arbitration algorithm is unspecified (round robin, fifo, |
308 | | * priority, reservations, preemption, etc) |
309 | | * |
310 | | * + Chipselect stays active during the entire message |
311 | | * (unless modified by spi_transfer.cs_change != 0). |
312 | | * + The message transfers use clock and SPI mode parameters |
313 | | * previously established by setup() for this device |
314 | | */ |
315 | | int (*transfer)(struct spi_device *spi, |
316 | | struct spi_message *mesg); |
317 | | |
318 | | /* called on release() to free memory provided by spi_controller */ |
319 | | void (*cleanup)(struct spi_device *spi); |
320 | | |
321 | | /* |
322 | | * These hooks are for drivers that want to use the generic |
323 | | * controller transfer mechanism. If these are used, the |
324 | | * transfer() function above must NOT be specified by the driver. |
325 | | * Over time we expect SPI drivers to be phased over to this API. |
326 | | */ |
327 | | int (*prepare_message)(struct spi_controller *ctlr, |
328 | | struct spi_message *message); |
329 | | |
330 | | /* |
331 | | * These hooks are for drivers that use a generic implementation |
332 | | * of transfer_one_message() provided by the core. |
333 | | */ |
334 | | void (*set_cs)(struct spi_device *spi, bool enable); |
335 | | int (*transfer_one)(struct spi_controller *ctlr, struct spi_device *spi, |
336 | | struct spi_transfer *transfer); |
337 | | void (*handle_err)(struct spi_controller *ctlr, |
338 | | struct spi_message *message); |
339 | | |
340 | | /* GPIO chip select */ |
341 | | struct gpio_desc **cs_gpiods; |
342 | | bool use_gpio_descriptors; |
343 | | |
344 | | struct list_head list; |
345 | | }; |
346 | | |
347 | | static inline void *spi_controller_get_devdata(struct spi_controller *ctlr) |
348 | 0 | { |
349 | 0 | if (ctlr->dev->platform_data) |
350 | 0 | return ctlr->dev->platform_data; |
351 | 0 | else |
352 | 0 | return ERR_PTR(-EINVAL); |
353 | 0 | } Unexecuted instantiation: m25p80.c:spi_controller_get_devdata Unexecuted instantiation: spi.c:spi_controller_get_devdata Unexecuted instantiation: spi-mem.c:spi_controller_get_devdata Unexecuted instantiation: gpio_spi.c:spi_controller_get_devdata Unexecuted instantiation: at25.c:spi_controller_get_devdata |
354 | | |
355 | | static inline void spi_controller_set_devdata(struct spi_controller *ctlr, |
356 | | void *data) |
357 | 0 | { |
358 | 0 | ctlr->dev->platform_data = data; |
359 | 0 | } Unexecuted instantiation: m25p80.c:spi_controller_set_devdata Unexecuted instantiation: spi.c:spi_controller_set_devdata Unexecuted instantiation: spi-mem.c:spi_controller_set_devdata Unexecuted instantiation: gpio_spi.c:spi_controller_set_devdata Unexecuted instantiation: at25.c:spi_controller_set_devdata |
360 | | |
361 | | static inline size_t spi_max_message_size(struct spi_device *spi) |
362 | 0 | { |
363 | 0 | struct spi_controller *ctrl = spi->controller; |
364 | 0 | if (!ctrl->max_transfer_size) |
365 | 0 | return SIZE_MAX; |
366 | 0 | return ctrl->max_transfer_size(spi); |
367 | 0 | } Unexecuted instantiation: m25p80.c:spi_max_message_size Unexecuted instantiation: spi.c:spi_max_message_size Unexecuted instantiation: spi-mem.c:spi_max_message_size Unexecuted instantiation: gpio_spi.c:spi_max_message_size Unexecuted instantiation: at25.c:spi_max_message_size |
368 | | |
369 | | static inline size_t spi_max_transfer_size(struct spi_device *spi) |
370 | 0 | { |
371 | 0 | struct spi_controller *ctrl = spi->controller; |
372 | 0 | size_t tr_max = SIZE_MAX; |
373 | 0 | size_t msg_max = spi_max_message_size(spi); |
374 | |
|
375 | 0 | if (ctrl->max_transfer_size) |
376 | 0 | tr_max = ctrl->max_transfer_size(spi); |
377 | |
|
378 | 0 | return min(tr_max, msg_max); |
379 | 0 | } Unexecuted instantiation: m25p80.c:spi_max_transfer_size Unexecuted instantiation: spi.c:spi_max_transfer_size Unexecuted instantiation: spi-mem.c:spi_max_transfer_size Unexecuted instantiation: gpio_spi.c:spi_max_transfer_size Unexecuted instantiation: at25.c:spi_max_transfer_size |
380 | | |
381 | | #define spi_master spi_controller |
382 | 0 | #define spi_register_master(_ctrl) spi_register_controller(_ctrl) |
383 | | |
384 | | /*---------------------------------------------------------------------------*/ |
385 | | |
386 | | /* |
387 | | * I/O INTERFACE between SPI controller and protocol drivers |
388 | | * |
389 | | * Protocol drivers use a queue of spi_messages, each transferring data |
390 | | * between the controller and memory buffers. |
391 | | * |
392 | | * The spi_messages themselves consist of a series of read+write transfer |
393 | | * segments. Those segments always read the same number of bits as they |
394 | | * write; but one or the other is easily ignored by passing a null buffer |
395 | | * pointer. (This is unlike most types of I/O API, because SPI hardware |
396 | | * is full duplex.) |
397 | | * |
398 | | * NOTE: Allocation of spi_transfer and spi_message memory is entirely |
399 | | * up to the protocol driver, which guarantees the integrity of both (as |
400 | | * well as the data buffers) for as long as the message is queued. |
401 | | */ |
402 | | |
403 | | /** |
404 | | * struct spi_transfer - a read/write buffer pair |
405 | | * @tx_buf: data to be written (dma-safe memory), or NULL |
406 | | * @rx_buf: data to be read (dma-safe memory), or NULL |
407 | | * @len: size of rx and tx buffers (in bytes) |
408 | | * @speed_hz: Select a speed other then the device default for this |
409 | | * transfer. If 0 the default (from @spi_device) is used. |
410 | | * @effective_speed_hz: the effective SCK-speed that was used to |
411 | | * transfer this transfer. Set to 0 if the SPI bus driver does |
412 | | * not support it. |
413 | | * @bits_per_word: select a bits_per_word other then the device default |
414 | | * for this transfer. If 0 the default (from @spi_device) is used. |
415 | | * @cs_change: affects chipselect after this transfer completes |
416 | | * @delay: delay to be introduced after this transfer before |
417 | | * (optionally) changing the chipselect status, then starting |
418 | | * the next transfer or completing this @spi_message. |
419 | | * @transfer_list: transfers are sequenced through @spi_message.transfers |
420 | | * |
421 | | * SPI transfers always write the same number of bytes as they read. |
422 | | * Protocol drivers should always provide @rx_buf and/or @tx_buf. |
423 | | * |
424 | | * If the transmit buffer is null, zeroes will be shifted out |
425 | | * while filling @rx_buf. If the receive buffer is null, the data |
426 | | * shifted in will be discarded. Only "len" bytes shift out (or in). |
427 | | * It's an error to try to shift out a partial word. (For example, by |
428 | | * shifting out three bytes with word size of sixteen or twenty bits; |
429 | | * the former uses two bytes per word, the latter uses four bytes.) |
430 | | * |
431 | | * In-memory data values are always in native CPU byte order, translated |
432 | | * from the wire byte order (big-endian except with SPI_LSB_FIRST). So |
433 | | * for example when bits_per_word is sixteen, buffers are 2N bytes long |
434 | | * (@len = 2N) and hold N sixteen bit words in CPU byte order. |
435 | | * |
436 | | * When the word size of the SPI transfer is not a power-of-two multiple |
437 | | * of eight bits, those in-memory words include extra bits. In-memory |
438 | | * words are always seen by protocol drivers as right-justified, so the |
439 | | * undefined (rx) or unused (tx) bits are always the most significant bits. |
440 | | * |
441 | | * All SPI transfers start with the relevant chipselect active. Normally |
442 | | * it stays selected until after the last transfer in a message. Drivers |
443 | | * can affect the chipselect signal using cs_change. |
444 | | * |
445 | | * (i) If the transfer isn't the last one in the message, this flag is |
446 | | * used to make the chipselect briefly go inactive in the middle of the |
447 | | * message. Toggling chipselect in this way may be needed to terminate |
448 | | * a chip command, letting a single spi_message perform all of group of |
449 | | * chip transactions together. |
450 | | * |
451 | | * (ii) When the transfer is the last one in the message, the chip may |
452 | | * stay selected until the next transfer. On multi-device SPI busses |
453 | | * with nothing blocking messages going to other devices, this is just |
454 | | * a performance hint; starting a message to another device deselects |
455 | | * this one. But in other cases, this can be used to ensure correctness. |
456 | | * Some devices need protocol transactions to be built from a series of |
457 | | * spi_message submissions, where the content of one message is determined |
458 | | * by the results of previous messages and where the whole transaction |
459 | | * ends when the chipselect goes intactive. |
460 | | * |
461 | | * The code that submits an spi_message (and its spi_transfers) |
462 | | * to the lower layers is responsible for managing its memory. |
463 | | * Zero-initialize every field you don't set up explicitly, to |
464 | | * insulate against future API updates. After you submit a message |
465 | | * and its transfers, ignore them until its completion callback. |
466 | | */ |
467 | | struct spi_transfer { |
468 | | /* it's ok if tx_buf == rx_buf (right?) |
469 | | * for MicroWire, one buffer must be null |
470 | | * buffers must work with dma_*map_single() calls, unless |
471 | | * spi_message.is_dma_mapped reports a pre-existing mapping |
472 | | */ |
473 | | const void *tx_buf; |
474 | | void *rx_buf; |
475 | | unsigned len; |
476 | | |
477 | | unsigned cs_change:1; |
478 | | u8 bits_per_word; |
479 | | struct spi_delay delay; |
480 | | u32 speed_hz; |
481 | | |
482 | | u32 effective_speed_hz; |
483 | | |
484 | | struct list_head transfer_list; |
485 | | }; |
486 | | |
487 | | /** |
488 | | * struct spi_message - one multi-segment SPI transaction |
489 | | * @transfers: list of transfer segments in this transaction |
490 | | * @spi: SPI device to which the transaction is queued |
491 | | * @actual_length: the total number of bytes that were transferred in all |
492 | | * successful segments |
493 | | * @status: zero for success, else negative errno |
494 | | * @queue: for use by whichever driver currently owns the message |
495 | | * @state: for use by whichever driver currently owns the message |
496 | | * |
497 | | * A @spi_message is used to execute an atomic sequence of data transfers, |
498 | | * each represented by a struct spi_transfer. The sequence is "atomic" |
499 | | * in the sense that no other spi_message may use that SPI bus until that |
500 | | * sequence completes. On some systems, many such sequences can execute as |
501 | | * as single programmed DMA transfer. On all systems, these messages are |
502 | | * queued, and might complete after transactions to other devices. Messages |
503 | | * sent to a given spi_device are alway executed in FIFO order. |
504 | | * |
505 | | * The code that submits an spi_message (and its spi_transfers) |
506 | | * to the lower layers is responsible for managing its memory. |
507 | | * Zero-initialize every field you don't set up explicitly, to |
508 | | * insulate against future API updates. After you submit a message |
509 | | * and its transfers, ignore them until its completion callback. |
510 | | */ |
511 | | struct spi_message { |
512 | | struct list_head transfers; |
513 | | |
514 | | struct spi_device *spi; |
515 | | |
516 | | /* REVISIT: we might want a flag affecting the behavior of the |
517 | | * last transfer ... allowing things like "read 16 bit length L" |
518 | | * immediately followed by "read L bytes". Basically imposing |
519 | | * a specific message scheduling algorithm. |
520 | | * |
521 | | * Some controller drivers (message-at-a-time queue processing) |
522 | | * could provide that as their default scheduling algorithm. But |
523 | | * others (with multi-message pipelines) could need a flag to |
524 | | * tell them about such special cases. |
525 | | */ |
526 | | |
527 | | unsigned actual_length; |
528 | | int status; |
529 | | |
530 | | /* for optional use by whatever driver currently owns the |
531 | | * spi_message ... between calls to spi_async and then later |
532 | | * complete(), that's the spi_master controller driver. |
533 | | */ |
534 | | struct list_head queue; |
535 | | void *state; |
536 | | }; |
537 | | |
538 | | static inline void spi_message_init(struct spi_message *m) |
539 | 0 | { |
540 | 0 | memset(m, 0, sizeof *m); |
541 | 0 | INIT_LIST_HEAD(&m->transfers); |
542 | 0 | } Unexecuted instantiation: m25p80.c:spi_message_init Unexecuted instantiation: spi.c:spi_message_init Unexecuted instantiation: spi-mem.c:spi_message_init Unexecuted instantiation: gpio_spi.c:spi_message_init Unexecuted instantiation: at25.c:spi_message_init |
543 | | |
544 | | static inline void |
545 | | spi_message_add_tail(struct spi_transfer *t, struct spi_message *m) |
546 | 0 | { |
547 | 0 | list_add_tail(&t->transfer_list, &m->transfers); |
548 | 0 | } Unexecuted instantiation: m25p80.c:spi_message_add_tail Unexecuted instantiation: spi.c:spi_message_add_tail Unexecuted instantiation: spi-mem.c:spi_message_add_tail Unexecuted instantiation: gpio_spi.c:spi_message_add_tail Unexecuted instantiation: at25.c:spi_message_add_tail |
549 | | |
550 | | /** |
551 | | * spi_message_init_with_transfers - Initialize spi_message and append transfers |
552 | | * @m: spi_message to be initialized |
553 | | * @xfers: An array of spi transfers |
554 | | * @num_xfers: Number of items in the xfer array |
555 | | * |
556 | | * This function initializes the given spi_message and adds each spi_transfer in |
557 | | * the given array to the message. |
558 | | */ |
559 | | static inline void |
560 | | spi_message_init_with_transfers(struct spi_message *m, |
561 | | struct spi_transfer *xfers, unsigned int num_xfers) |
562 | 0 | { |
563 | 0 | unsigned int i; |
564 | 0 |
|
565 | 0 | spi_message_init(m); |
566 | 0 | for (i = 0; i < num_xfers; ++i) |
567 | 0 | spi_message_add_tail(&xfers[i], m); |
568 | 0 | } Unexecuted instantiation: m25p80.c:spi_message_init_with_transfers Unexecuted instantiation: spi.c:spi_message_init_with_transfers Unexecuted instantiation: spi-mem.c:spi_message_init_with_transfers Unexecuted instantiation: gpio_spi.c:spi_message_init_with_transfers Unexecuted instantiation: at25.c:spi_message_init_with_transfers |
569 | | |
570 | | static inline void |
571 | | spi_transfer_del(struct spi_transfer *t) |
572 | 0 | { |
573 | 0 | list_del(&t->transfer_list); |
574 | 0 | } Unexecuted instantiation: m25p80.c:spi_transfer_del Unexecuted instantiation: spi.c:spi_transfer_del Unexecuted instantiation: spi-mem.c:spi_transfer_del Unexecuted instantiation: gpio_spi.c:spi_transfer_del Unexecuted instantiation: at25.c:spi_transfer_del |
575 | | |
576 | | /** |
577 | | * spi_is_bpw_supported - Check if bits per word is supported |
578 | | * @spi: SPI device |
579 | | * @bpw: Bits per word |
580 | | * |
581 | | * This function checks to see if the SPI controller supports @bpw. |
582 | | * |
583 | | * Returns: |
584 | | * True if @bpw is supported, false otherwise. |
585 | | */ |
586 | | static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw) |
587 | 0 | { |
588 | 0 | u32 bpw_mask = spi->master->bits_per_word_mask; |
589 | 0 |
|
590 | 0 | if (bpw == 8 || (bpw <= 32 && bpw_mask & SPI_BPW_MASK(bpw))) |
591 | 0 | return true; |
592 | 0 |
|
593 | 0 | return false; |
594 | 0 | } Unexecuted instantiation: m25p80.c:spi_is_bpw_supported Unexecuted instantiation: spi.c:spi_is_bpw_supported Unexecuted instantiation: spi-mem.c:spi_is_bpw_supported Unexecuted instantiation: gpio_spi.c:spi_is_bpw_supported Unexecuted instantiation: at25.c:spi_is_bpw_supported |
595 | | |
596 | | /* All these synchronous SPI transfer routines are utilities layered |
597 | | * over the core async transfer primitive. Here, "synchronous" means |
598 | | * they will sleep uninterruptibly until the async transfer completes. |
599 | | */ |
600 | | |
601 | | int spi_sync(struct spi_device *spi, struct spi_message *message); |
602 | | |
603 | | /** |
604 | | * spi_sync_transfer - synchronous SPI data transfer |
605 | | * @spi: device with which data will be exchanged |
606 | | * @xfers: An array of spi_transfers |
607 | | * @num_xfers: Number of items in the xfer array |
608 | | * Context: can sleep |
609 | | * |
610 | | * Does a synchronous SPI data transfer of the given spi_transfer array. |
611 | | * |
612 | | * For more specific semantics see spi_sync(). |
613 | | * |
614 | | * Return: zero on success, else a negative error code. |
615 | | */ |
616 | | static inline int |
617 | | spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers, |
618 | | unsigned int num_xfers) |
619 | 0 | { |
620 | 0 | struct spi_message msg; |
621 | 0 |
|
622 | 0 | spi_message_init_with_transfers(&msg, xfers, num_xfers); |
623 | 0 |
|
624 | 0 | return spi_sync(spi, &msg); |
625 | 0 | } Unexecuted instantiation: m25p80.c:spi_sync_transfer Unexecuted instantiation: spi.c:spi_sync_transfer Unexecuted instantiation: spi-mem.c:spi_sync_transfer Unexecuted instantiation: gpio_spi.c:spi_sync_transfer Unexecuted instantiation: at25.c:spi_sync_transfer |
626 | | |
627 | | struct spi_device *spi_new_device(struct spi_controller *ctrl, |
628 | | struct spi_board_info *chip); |
629 | | int spi_register_controller(struct spi_controller *ctrl); |
630 | | |
631 | | #ifdef CONFIG_SPI |
632 | | int spi_register_board_info(struct spi_board_info const *info, int num); |
633 | | #else |
634 | | static inline int spi_register_board_info(struct spi_board_info const *info, |
635 | | int num) |
636 | | { |
637 | | return 0; |
638 | | } |
639 | | #endif |
640 | | |
641 | | /** |
642 | | * spi_write - SPI synchronous write |
643 | | * @spi: device to which data will be written |
644 | | * @buf: data buffer |
645 | | * @len: data buffer size |
646 | | * Context: can sleep |
647 | | * |
648 | | * This writes the buffer and returns zero or a negative error code. |
649 | | * Callable only from contexts that can sleep. |
650 | | */ |
651 | | static inline int |
652 | | spi_write(struct spi_device *spi, const void *buf, size_t len) |
653 | 0 | { |
654 | 0 | struct spi_transfer t = { |
655 | 0 | .tx_buf = buf, |
656 | 0 | .len = len, |
657 | 0 | }; |
658 | 0 | struct spi_message m; |
659 | |
|
660 | 0 | spi_message_init(&m); |
661 | 0 | spi_message_add_tail(&t, &m); |
662 | 0 | return spi_sync(spi, &m); |
663 | 0 | } Unexecuted instantiation: m25p80.c:spi_write Unexecuted instantiation: spi.c:spi_write Unexecuted instantiation: spi-mem.c:spi_write Unexecuted instantiation: gpio_spi.c:spi_write Unexecuted instantiation: at25.c:spi_write |
664 | | |
665 | | /** |
666 | | * spi_read - SPI synchronous read |
667 | | * @spi: device from which data will be read |
668 | | * @buf: data buffer |
669 | | * @len: data buffer size |
670 | | * Context: can sleep |
671 | | * |
672 | | * This reads the buffer and returns zero or a negative error code. |
673 | | * Callable only from contexts that can sleep. |
674 | | */ |
675 | | static inline int |
676 | | spi_read(struct spi_device *spi, void *buf, size_t len) |
677 | 0 | { |
678 | 0 | struct spi_transfer t = { |
679 | 0 | .rx_buf = buf, |
680 | 0 | .len = len, |
681 | 0 | }; |
682 | 0 | struct spi_message m; |
683 | 0 |
|
684 | 0 | spi_message_init(&m); |
685 | 0 | spi_message_add_tail(&t, &m); |
686 | 0 | return spi_sync(spi, &m); |
687 | 0 | } Unexecuted instantiation: m25p80.c:spi_read Unexecuted instantiation: spi.c:spi_read Unexecuted instantiation: spi-mem.c:spi_read Unexecuted instantiation: gpio_spi.c:spi_read Unexecuted instantiation: at25.c:spi_read |
688 | | |
689 | | /* this copies txbuf and rxbuf data; for small transfers only! */ |
690 | | extern int spi_write_then_read(struct spi_device *spi, |
691 | | const void *txbuf, unsigned n_tx, |
692 | | void *rxbuf, unsigned n_rx); |
693 | | |
694 | | /** |
695 | | * spi_w8r8 - SPI synchronous 8 bit write followed by 8 bit read |
696 | | * @spi: device with which data will be exchanged |
697 | | * @cmd: command to be written before data is read back |
698 | | * Context: can sleep |
699 | | * |
700 | | * This returns the (unsigned) eight bit number returned by the |
701 | | * device, or else a negative error code. Callable only from |
702 | | * contexts that can sleep. |
703 | | */ |
704 | | static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd) |
705 | 0 | { |
706 | 0 | ssize_t status; |
707 | 0 | u8 result; |
708 | |
|
709 | 0 | status = spi_write_then_read(spi, &cmd, 1, &result, 1); |
710 | | |
711 | | /* return negative errno or unsigned value */ |
712 | 0 | return (status < 0) ? status : result; |
713 | 0 | } Unexecuted instantiation: m25p80.c:spi_w8r8 Unexecuted instantiation: spi.c:spi_w8r8 Unexecuted instantiation: spi-mem.c:spi_w8r8 Unexecuted instantiation: gpio_spi.c:spi_w8r8 Unexecuted instantiation: at25.c:spi_w8r8 |
714 | | |
715 | | extern struct bus_type spi_bus; |
716 | | |
717 | | static inline bool dev_bus_is_spi(struct device *dev) |
718 | 0 | { |
719 | 0 | return IS_ENABLED(CONFIG_SPI) && dev->bus == &spi_bus; |
720 | 0 | } Unexecuted instantiation: m25p80.c:dev_bus_is_spi Unexecuted instantiation: spi.c:dev_bus_is_spi Unexecuted instantiation: spi-mem.c:dev_bus_is_spi Unexecuted instantiation: gpio_spi.c:dev_bus_is_spi Unexecuted instantiation: at25.c:dev_bus_is_spi |
721 | | |
722 | | struct spi_controller *spi_get_controller(int bus); |
723 | | |
724 | | static inline int spi_driver_register(struct driver *drv) |
725 | 64 | { |
726 | 64 | drv->bus = &spi_bus; |
727 | 64 | return register_driver(drv); |
728 | 64 | } m25p80.c:spi_driver_register Line | Count | Source | 725 | 32 | { | 726 | 32 | drv->bus = &spi_bus; | 727 | 32 | return register_driver(drv); | 728 | 32 | } |
Unexecuted instantiation: spi.c:spi_driver_register Unexecuted instantiation: spi-mem.c:spi_driver_register Unexecuted instantiation: gpio_spi.c:spi_driver_register at25.c:spi_driver_register Line | Count | Source | 725 | 32 | { | 726 | 32 | drv->bus = &spi_bus; | 727 | 32 | return register_driver(drv); | 728 | 32 | } |
|
729 | | |
730 | | static inline struct slice *spi_device_slice(struct spi_device *spi) |
731 | 0 | { |
732 | 0 | return &spi->controller->slice; |
733 | 0 | } Unexecuted instantiation: m25p80.c:spi_device_slice Unexecuted instantiation: spi.c:spi_device_slice Unexecuted instantiation: spi-mem.c:spi_device_slice Unexecuted instantiation: gpio_spi.c:spi_device_slice Unexecuted instantiation: at25.c:spi_device_slice |
734 | | |
735 | | #ifdef CONFIG_SPI |
736 | | #define coredevice_spi_driver(drv) \ |
737 | | register_driver_macro(coredevice,spi,drv) |
738 | | #define device_spi_driver(drv) \ |
739 | | register_driver_macro(device,spi,drv) |
740 | | #else |
741 | | #define coredevice_spi_driver(drv) |
742 | | #define device_spi_driver(drv) |
743 | | #endif |
744 | | |
745 | | #endif /* __INCLUDE_SPI_H */ |