1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2018 Exceet Electronics GmbH
4 * Copyright (C) 2018 Bootlin
9 #ifndef __LINUX_SPI_MEM_H
10 #define __LINUX_SPI_MEM_H
12 #include <linux/spi/spi.h>
14 #define SPI_MEM_OP_CMD(__opcode, __buswidth) \
16 .buswidth = __buswidth, \
20 #define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth) \
24 .buswidth = __buswidth, \
27 #define SPI_MEM_OP_NO_ADDR { }
29 #define SPI_MEM_OP_DUMMY(__nbytes, __buswidth) \
32 .buswidth = __buswidth, \
35 #define SPI_MEM_OP_NO_DUMMY { }
37 #define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth) \
39 .dir = SPI_MEM_DATA_IN, \
42 .buswidth = __buswidth, \
45 #define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, __buswidth) \
47 .dir = SPI_MEM_DATA_OUT, \
50 .buswidth = __buswidth, \
53 #define SPI_MEM_OP_NO_DATA { }
56 * enum spi_mem_data_dir - describes the direction of a SPI memory data
57 * transfer from the controller perspective
58 * @SPI_MEM_DATA_IN: data coming from the SPI memory
59 * @SPI_MEM_DATA_OUT: data sent the SPI memory
61 enum spi_mem_data_dir {
67 * struct spi_mem_op - describes a SPI memory operation
68 * @cmd.buswidth: number of IO lines used to transmit the command
69 * @cmd.opcode: operation opcode
70 * @addr.nbytes: number of address bytes to send. Can be zero if the operation
71 * does not need to send an address
72 * @addr.buswidth: number of IO lines used to transmit the address cycles
73 * @addr.val: address value. This value is always sent MSB first on the bus.
74 * Note that only @addr.nbytes are taken into account in this
75 * address value, so users should make sure the value fits in the
76 * assigned number of bytes.
77 * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can
78 * be zero if the operation does not require dummy bytes
79 * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes
80 * @data.buswidth: number of IO lanes used to send/receive the data
81 * @data.dir: direction of the transfer
82 * @data.buf.in: input buffer
83 * @data.buf.out: output buffer
104 enum spi_mem_data_dir dir;
106 /* buf.{in,out} must be DMA-able. */
114 #define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \
123 * struct spi_mem - describes a SPI memory device
124 * @spi: the underlying SPI device
125 * @drvpriv: spi_mem_drviver private data
127 * Extra information that describe the SPI memory device and may be needed by
128 * the controller to properly handle this device should be placed here.
130 * One example would be the device size since some controller expose their SPI
131 * mem devices through a io-mapped region.
134 struct spi_device *spi;
139 * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
141 * @mem: memory device
142 * @data: data to attach to the memory device
144 static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
150 * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
152 * @mem: memory device
154 * Return: the data attached to the mem device.
156 static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
162 * struct spi_controller_mem_ops - SPI memory operations
163 * @adjust_op_size: shrink the data xfer of an operation to match controller's
164 * limitations (can be alignment of max RX/TX size
166 * @supports_op: check if an operation is supported by the controller
167 * @exec_op: execute a SPI memory operation
169 * This interface should be implemented by SPI controllers providing an
170 * high-level interface to execute SPI memory operation, which is usually the
171 * case for QSPI controllers.
173 struct spi_controller_mem_ops {
174 int (*adjust_op_size)(struct spi_mem *mem, struct spi_mem_op *op);
175 bool (*supports_op)(struct spi_mem *mem,
176 const struct spi_mem_op *op);
177 int (*exec_op)(struct spi_mem *mem,
178 const struct spi_mem_op *op);
182 * struct spi_mem_driver - SPI memory driver
183 * @spidrv: inherit from a SPI driver
184 * @probe: probe a SPI memory. Usually where detection/initialization takes
186 * @remove: remove a SPI memory
187 * @shutdown: take appropriate action when the system is shutdown
189 * This is just a thin wrapper around a spi_driver. The core takes care of
190 * allocating the spi_mem object and forwarding the probe/remove/shutdown
191 * request to the spi_mem_driver. The reason we use this wrapper is because
192 * we might have to stuff more information into the spi_mem struct to let
193 * SPI controllers know more about the SPI memory they interact with, and
194 * having this intermediate layer allows us to do that without adding more
195 * useless fields to the spi_device object.
197 struct spi_mem_driver {
198 struct spi_driver spidrv;
199 int (*probe)(struct spi_mem *mem);
200 int (*remove)(struct spi_mem *mem);
201 void (*shutdown)(struct spi_mem *mem);
204 #if IS_ENABLED(CONFIG_SPI_MEM)
205 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
206 const struct spi_mem_op *op,
207 struct sg_table *sg);
209 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
210 const struct spi_mem_op *op,
211 struct sg_table *sg);
214 spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
215 const struct spi_mem_op *op,
222 spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
223 const struct spi_mem_op *op,
227 #endif /* CONFIG_SPI_MEM */
229 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op);
231 bool spi_mem_supports_op(struct spi_mem *mem,
232 const struct spi_mem_op *op);
234 int spi_mem_exec_op(struct spi_mem *mem,
235 const struct spi_mem_op *op);
237 int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
238 struct module *owner);
240 void spi_mem_driver_unregister(struct spi_mem_driver *drv);
242 #define spi_mem_driver_register(__drv) \
243 spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
245 #define module_spi_mem_driver(__drv) \
246 module_driver(__drv, spi_mem_driver_register, \
247 spi_mem_driver_unregister)
249 #endif /* __LINUX_SPI_MEM_H */