blob: 4fa34a227a0fae1be5592e6ddbcaac3020439a46 [file] [log] [blame]
Boris Brezillonc36ff262018-04-26 18:18:14 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (C) 2018 Exceet Electronics GmbH
4 * Copyright (C) 2018 Bootlin
5 *
Peter Pan7529df42018-06-22 14:28:23 +02006 * Author:
7 * Peter Pan <peterpandong@micron.com>
8 * Boris Brezillon <boris.brezillon@bootlin.com>
Boris Brezillonc36ff262018-04-26 18:18:14 +02009 */
10
11#ifndef __LINUX_SPI_MEM_H
12#define __LINUX_SPI_MEM_H
13
14#include <linux/spi/spi.h>
15
16#define SPI_MEM_OP_CMD(__opcode, __buswidth) \
17 { \
18 .buswidth = __buswidth, \
19 .opcode = __opcode, \
20 }
21
22#define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth) \
23 { \
24 .nbytes = __nbytes, \
25 .val = __val, \
26 .buswidth = __buswidth, \
27 }
28
29#define SPI_MEM_OP_NO_ADDR { }
30
31#define SPI_MEM_OP_DUMMY(__nbytes, __buswidth) \
32 { \
33 .nbytes = __nbytes, \
34 .buswidth = __buswidth, \
35 }
36
37#define SPI_MEM_OP_NO_DUMMY { }
38
39#define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth) \
40 { \
41 .dir = SPI_MEM_DATA_IN, \
42 .nbytes = __nbytes, \
43 .buf.in = __buf, \
44 .buswidth = __buswidth, \
45 }
46
47#define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, __buswidth) \
48 { \
49 .dir = SPI_MEM_DATA_OUT, \
50 .nbytes = __nbytes, \
51 .buf.out = __buf, \
52 .buswidth = __buswidth, \
53 }
54
55#define SPI_MEM_OP_NO_DATA { }
56
57/**
58 * enum spi_mem_data_dir - describes the direction of a SPI memory data
59 * transfer from the controller perspective
60 * @SPI_MEM_DATA_IN: data coming from the SPI memory
61 * @SPI_MEM_DATA_OUT: data sent the SPI memory
62 */
63enum spi_mem_data_dir {
64 SPI_MEM_DATA_IN,
65 SPI_MEM_DATA_OUT,
66};
67
68/**
69 * struct spi_mem_op - describes a SPI memory operation
70 * @cmd.buswidth: number of IO lines used to transmit the command
71 * @cmd.opcode: operation opcode
72 * @addr.nbytes: number of address bytes to send. Can be zero if the operation
73 * does not need to send an address
74 * @addr.buswidth: number of IO lines used to transmit the address cycles
75 * @addr.val: address value. This value is always sent MSB first on the bus.
76 * Note that only @addr.nbytes are taken into account in this
77 * address value, so users should make sure the value fits in the
78 * assigned number of bytes.
79 * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can
80 * be zero if the operation does not require dummy bytes
81 * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes
82 * @data.buswidth: number of IO lanes used to send/receive the data
83 * @data.dir: direction of the transfer
84 * @data.buf.in: input buffer
85 * @data.buf.out: output buffer
86 */
87struct spi_mem_op {
88 struct {
89 u8 buswidth;
90 u8 opcode;
91 } cmd;
92
93 struct {
94 u8 nbytes;
95 u8 buswidth;
96 u64 val;
97 } addr;
98
99 struct {
100 u8 nbytes;
101 u8 buswidth;
102 } dummy;
103
104 struct {
105 u8 buswidth;
106 enum spi_mem_data_dir dir;
107 unsigned int nbytes;
108 /* buf.{in,out} must be DMA-able. */
109 union {
110 void *in;
111 const void *out;
112 } buf;
113 } data;
114};
115
116#define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \
117 { \
118 .cmd = __cmd, \
119 .addr = __addr, \
120 .dummy = __dummy, \
121 .data = __data, \
122 }
123
124/**
125 * struct spi_mem - describes a SPI memory device
126 * @spi: the underlying SPI device
127 * @drvpriv: spi_mem_drviver private data
128 *
129 * Extra information that describe the SPI memory device and may be needed by
130 * the controller to properly handle this device should be placed here.
131 *
132 * One example would be the device size since some controller expose their SPI
133 * mem devices through a io-mapped region.
134 */
135struct spi_mem {
136 struct spi_device *spi;
137 void *drvpriv;
138};
139
140/**
141 * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
142 * device
143 * @mem: memory device
144 * @data: data to attach to the memory device
145 */
146static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
147{
148 mem->drvpriv = data;
149}
150
151/**
152 * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
153 * device
154 * @mem: memory device
155 *
156 * Return: the data attached to the mem device.
157 */
158static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
159{
160 return mem->drvpriv;
161}
162
163/**
164 * struct spi_controller_mem_ops - SPI memory operations
165 * @adjust_op_size: shrink the data xfer of an operation to match controller's
166 * limitations (can be alignment of max RX/TX size
167 * limitations)
168 * @supports_op: check if an operation is supported by the controller
169 * @exec_op: execute a SPI memory operation
170 *
171 * This interface should be implemented by SPI controllers providing an
172 * high-level interface to execute SPI memory operation, which is usually the
173 * case for QSPI controllers.
174 */
175struct spi_controller_mem_ops {
176 int (*adjust_op_size)(struct spi_mem *mem, struct spi_mem_op *op);
177 bool (*supports_op)(struct spi_mem *mem,
178 const struct spi_mem_op *op);
179 int (*exec_op)(struct spi_mem *mem,
180 const struct spi_mem_op *op);
181};
182
183/**
184 * struct spi_mem_driver - SPI memory driver
185 * @spidrv: inherit from a SPI driver
186 * @probe: probe a SPI memory. Usually where detection/initialization takes
187 * place
188 * @remove: remove a SPI memory
189 * @shutdown: take appropriate action when the system is shutdown
190 *
191 * This is just a thin wrapper around a spi_driver. The core takes care of
192 * allocating the spi_mem object and forwarding the probe/remove/shutdown
193 * request to the spi_mem_driver. The reason we use this wrapper is because
194 * we might have to stuff more information into the spi_mem struct to let
195 * SPI controllers know more about the SPI memory they interact with, and
196 * having this intermediate layer allows us to do that without adding more
197 * useless fields to the spi_device object.
198 */
199struct spi_mem_driver {
200 struct spi_driver spidrv;
201 int (*probe)(struct spi_mem *mem);
202 int (*remove)(struct spi_mem *mem);
203 void (*shutdown)(struct spi_mem *mem);
204};
205
206#if IS_ENABLED(CONFIG_SPI_MEM)
207int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
208 const struct spi_mem_op *op,
209 struct sg_table *sg);
210
211void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
212 const struct spi_mem_op *op,
213 struct sg_table *sg);
214#else
215static inline int
216spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
217 const struct spi_mem_op *op,
218 struct sg_table *sg)
219{
220 return -ENOTSUPP;
221}
222
223static inline void
224spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
225 const struct spi_mem_op *op,
226 struct sg_table *sg)
227{
228}
229#endif /* CONFIG_SPI_MEM */
230
231int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op);
232
233bool spi_mem_supports_op(struct spi_mem *mem,
234 const struct spi_mem_op *op);
235
236int spi_mem_exec_op(struct spi_mem *mem,
237 const struct spi_mem_op *op);
238
239int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
240 struct module *owner);
241
242void spi_mem_driver_unregister(struct spi_mem_driver *drv);
243
244#define spi_mem_driver_register(__drv) \
245 spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
246
247#define module_spi_mem_driver(__drv) \
248 module_driver(__drv, spi_mem_driver_register, \
249 spi_mem_driver_unregister)
250
251#endif /* __LINUX_SPI_MEM_H */