1 // SPDX-License-Identifier: GPL-2.0
3 // STMicroelectronics STM32 SPI Controller driver (master mode only)
5 // Copyright (C) 2017, STMicroelectronics - All Rights Reserved
8 #include <linux/bitfield.h>
9 #include <linux/debugfs.h>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/dmaengine.h>
13 #include <linux/interrupt.h>
14 #include <linux/iopoll.h>
15 #include <linux/module.h>
16 #include <linux/of_platform.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/reset.h>
20 #include <linux/spi/spi.h>
22 #define DRIVER_NAME "spi_stm32"
24 /* STM32F4 SPI registers */
25 #define STM32F4_SPI_CR1 0x00
26 #define STM32F4_SPI_CR2 0x04
27 #define STM32F4_SPI_SR 0x08
28 #define STM32F4_SPI_DR 0x0C
29 #define STM32F4_SPI_I2SCFGR 0x1C
31 /* STM32F4_SPI_CR1 bit fields */
32 #define STM32F4_SPI_CR1_CPHA BIT(0)
33 #define STM32F4_SPI_CR1_CPOL BIT(1)
34 #define STM32F4_SPI_CR1_MSTR BIT(2)
35 #define STM32F4_SPI_CR1_BR_SHIFT 3
36 #define STM32F4_SPI_CR1_BR GENMASK(5, 3)
37 #define STM32F4_SPI_CR1_SPE BIT(6)
38 #define STM32F4_SPI_CR1_LSBFRST BIT(7)
39 #define STM32F4_SPI_CR1_SSI BIT(8)
40 #define STM32F4_SPI_CR1_SSM BIT(9)
41 #define STM32F4_SPI_CR1_RXONLY BIT(10)
42 #define STM32F4_SPI_CR1_DFF BIT(11)
43 #define STM32F4_SPI_CR1_CRCNEXT BIT(12)
44 #define STM32F4_SPI_CR1_CRCEN BIT(13)
45 #define STM32F4_SPI_CR1_BIDIOE BIT(14)
46 #define STM32F4_SPI_CR1_BIDIMODE BIT(15)
47 #define STM32F4_SPI_CR1_BR_MIN 0
48 #define STM32F4_SPI_CR1_BR_MAX (GENMASK(5, 3) >> 3)
50 /* STM32F4_SPI_CR2 bit fields */
51 #define STM32F4_SPI_CR2_RXDMAEN BIT(0)
52 #define STM32F4_SPI_CR2_TXDMAEN BIT(1)
53 #define STM32F4_SPI_CR2_SSOE BIT(2)
54 #define STM32F4_SPI_CR2_FRF BIT(4)
55 #define STM32F4_SPI_CR2_ERRIE BIT(5)
56 #define STM32F4_SPI_CR2_RXNEIE BIT(6)
57 #define STM32F4_SPI_CR2_TXEIE BIT(7)
59 /* STM32F4_SPI_SR bit fields */
60 #define STM32F4_SPI_SR_RXNE BIT(0)
61 #define STM32F4_SPI_SR_TXE BIT(1)
62 #define STM32F4_SPI_SR_CHSIDE BIT(2)
63 #define STM32F4_SPI_SR_UDR BIT(3)
64 #define STM32F4_SPI_SR_CRCERR BIT(4)
65 #define STM32F4_SPI_SR_MODF BIT(5)
66 #define STM32F4_SPI_SR_OVR BIT(6)
67 #define STM32F4_SPI_SR_BSY BIT(7)
68 #define STM32F4_SPI_SR_FRE BIT(8)
70 /* STM32F4_SPI_I2SCFGR bit fields */
71 #define STM32F4_SPI_I2SCFGR_I2SMOD BIT(11)
73 /* STM32F4 SPI Baud Rate min/max divisor */
74 #define STM32F4_SPI_BR_DIV_MIN (2 << STM32F4_SPI_CR1_BR_MIN)
75 #define STM32F4_SPI_BR_DIV_MAX (2 << STM32F4_SPI_CR1_BR_MAX)
77 /* STM32H7 SPI registers */
78 #define STM32H7_SPI_CR1 0x00
79 #define STM32H7_SPI_CR2 0x04
80 #define STM32H7_SPI_CFG1 0x08
81 #define STM32H7_SPI_CFG2 0x0C
82 #define STM32H7_SPI_IER 0x10
83 #define STM32H7_SPI_SR 0x14
84 #define STM32H7_SPI_IFCR 0x18
85 #define STM32H7_SPI_TXDR 0x20
86 #define STM32H7_SPI_RXDR 0x30
87 #define STM32H7_SPI_I2SCFGR 0x50
89 /* STM32H7_SPI_CR1 bit fields */
90 #define STM32H7_SPI_CR1_SPE BIT(0)
91 #define STM32H7_SPI_CR1_MASRX BIT(8)
92 #define STM32H7_SPI_CR1_CSTART BIT(9)
93 #define STM32H7_SPI_CR1_CSUSP BIT(10)
94 #define STM32H7_SPI_CR1_HDDIR BIT(11)
95 #define STM32H7_SPI_CR1_SSI BIT(12)
97 /* STM32H7_SPI_CR2 bit fields */
98 #define STM32H7_SPI_CR2_TSIZE GENMASK(15, 0)
99 #define STM32H7_SPI_TSIZE_MAX GENMASK(15, 0)
101 /* STM32H7_SPI_CFG1 bit fields */
102 #define STM32H7_SPI_CFG1_DSIZE GENMASK(4, 0)
103 #define STM32H7_SPI_CFG1_FTHLV GENMASK(8, 5)
104 #define STM32H7_SPI_CFG1_RXDMAEN BIT(14)
105 #define STM32H7_SPI_CFG1_TXDMAEN BIT(15)
106 #define STM32H7_SPI_CFG1_MBR GENMASK(30, 28)
107 #define STM32H7_SPI_CFG1_MBR_SHIFT 28
108 #define STM32H7_SPI_CFG1_MBR_MIN 0
109 #define STM32H7_SPI_CFG1_MBR_MAX (GENMASK(30, 28) >> 28)
111 /* STM32H7_SPI_CFG2 bit fields */
112 #define STM32H7_SPI_CFG2_MIDI GENMASK(7, 4)
113 #define STM32H7_SPI_CFG2_COMM GENMASK(18, 17)
114 #define STM32H7_SPI_CFG2_SP GENMASK(21, 19)
115 #define STM32H7_SPI_CFG2_MASTER BIT(22)
116 #define STM32H7_SPI_CFG2_LSBFRST BIT(23)
117 #define STM32H7_SPI_CFG2_CPHA BIT(24)
118 #define STM32H7_SPI_CFG2_CPOL BIT(25)
119 #define STM32H7_SPI_CFG2_SSM BIT(26)
120 #define STM32H7_SPI_CFG2_AFCNTR BIT(31)
122 /* STM32H7_SPI_IER bit fields */
123 #define STM32H7_SPI_IER_RXPIE BIT(0)
124 #define STM32H7_SPI_IER_TXPIE BIT(1)
125 #define STM32H7_SPI_IER_DXPIE BIT(2)
126 #define STM32H7_SPI_IER_EOTIE BIT(3)
127 #define STM32H7_SPI_IER_TXTFIE BIT(4)
128 #define STM32H7_SPI_IER_OVRIE BIT(6)
129 #define STM32H7_SPI_IER_MODFIE BIT(9)
130 #define STM32H7_SPI_IER_ALL GENMASK(10, 0)
132 /* STM32H7_SPI_SR bit fields */
133 #define STM32H7_SPI_SR_RXP BIT(0)
134 #define STM32H7_SPI_SR_TXP BIT(1)
135 #define STM32H7_SPI_SR_EOT BIT(3)
136 #define STM32H7_SPI_SR_OVR BIT(6)
137 #define STM32H7_SPI_SR_MODF BIT(9)
138 #define STM32H7_SPI_SR_SUSP BIT(11)
139 #define STM32H7_SPI_SR_RXPLVL GENMASK(14, 13)
140 #define STM32H7_SPI_SR_RXWNE BIT(15)
142 /* STM32H7_SPI_IFCR bit fields */
143 #define STM32H7_SPI_IFCR_ALL GENMASK(11, 3)
145 /* STM32H7_SPI_I2SCFGR bit fields */
146 #define STM32H7_SPI_I2SCFGR_I2SMOD BIT(0)
148 /* STM32H7 SPI Master Baud Rate min/max divisor */
149 #define STM32H7_SPI_MBR_DIV_MIN (2 << STM32H7_SPI_CFG1_MBR_MIN)
150 #define STM32H7_SPI_MBR_DIV_MAX (2 << STM32H7_SPI_CFG1_MBR_MAX)
152 /* STM32H7 SPI Communication mode */
153 #define STM32H7_SPI_FULL_DUPLEX 0
154 #define STM32H7_SPI_SIMPLEX_TX 1
155 #define STM32H7_SPI_SIMPLEX_RX 2
156 #define STM32H7_SPI_HALF_DUPLEX 3
158 /* SPI Communication type */
159 #define SPI_FULL_DUPLEX 0
160 #define SPI_SIMPLEX_TX 1
161 #define SPI_SIMPLEX_RX 2
162 #define SPI_3WIRE_TX 3
163 #define SPI_3WIRE_RX 4
165 #define STM32_SPI_AUTOSUSPEND_DELAY 1 /* 1 ms */
168 * use PIO for small transfers, avoiding DMA setup/teardown overhead for drivers
169 * without fifo buffers.
171 #define SPI_DMA_MIN_BYTES 16
174 * struct stm32_spi_reg - stm32 SPI register & bitfield desc
175 * @reg: register offset
176 * @mask: bitfield mask
179 struct stm32_spi_reg {
186 * struct stm32_spi_regspec - stm32 registers definition, compatible dependent data
187 * @en: enable register and SPI enable bit
188 * @dma_rx_en: SPI DMA RX enable register end SPI DMA RX enable bit
189 * @dma_tx_en: SPI DMA TX enable register end SPI DMA TX enable bit
190 * @cpol: clock polarity register and polarity bit
191 * @cpha: clock phase register and phase bit
192 * @lsb_first: LSB transmitted first register and bit
193 * @br: baud rate register and bitfields
194 * @rx: SPI RX data register
195 * @tx: SPI TX data register
197 struct stm32_spi_regspec {
198 const struct stm32_spi_reg en;
199 const struct stm32_spi_reg dma_rx_en;
200 const struct stm32_spi_reg dma_tx_en;
201 const struct stm32_spi_reg cpol;
202 const struct stm32_spi_reg cpha;
203 const struct stm32_spi_reg lsb_first;
204 const struct stm32_spi_reg br;
205 const struct stm32_spi_reg rx;
206 const struct stm32_spi_reg tx;
212 * struct stm32_spi_cfg - stm32 compatible configuration data
213 * @regs: registers descriptions
214 * @get_fifo_size: routine to get fifo size
215 * @get_bpw_mask: routine to get bits per word mask
216 * @disable: routine to disable controller
217 * @config: routine to configure controller as SPI Master
218 * @set_bpw: routine to configure registers to for bits per word
219 * @set_mode: routine to configure registers to desired mode
220 * @set_data_idleness: optional routine to configure registers to desired idle
221 * time between frames (if driver has this functionality)
222 * @set_number_of_data: optional routine to configure registers to desired
223 * number of data (if driver has this functionality)
224 * @transfer_one_dma_start: routine to start transfer a single spi_transfer
226 * @dma_rx_cb: routine to call after DMA RX channel operation is complete
227 * @dma_tx_cb: routine to call after DMA TX channel operation is complete
228 * @transfer_one_irq: routine to configure interrupts for driver
229 * @irq_handler_event: Interrupt handler for SPI controller events
230 * @irq_handler_thread: thread of interrupt handler for SPI controller
231 * @baud_rate_div_min: minimum baud rate divisor
232 * @baud_rate_div_max: maximum baud rate divisor
233 * @has_fifo: boolean to know if fifo is used for driver
234 * @flags: compatible specific SPI controller flags used at registration time
236 struct stm32_spi_cfg {
237 const struct stm32_spi_regspec *regs;
238 int (*get_fifo_size)(struct stm32_spi *spi);
239 int (*get_bpw_mask)(struct stm32_spi *spi);
240 void (*disable)(struct stm32_spi *spi);
241 int (*config)(struct stm32_spi *spi);
242 void (*set_bpw)(struct stm32_spi *spi);
243 int (*set_mode)(struct stm32_spi *spi, unsigned int comm_type);
244 void (*set_data_idleness)(struct stm32_spi *spi, u32 length);
245 int (*set_number_of_data)(struct stm32_spi *spi, u32 length);
246 void (*transfer_one_dma_start)(struct stm32_spi *spi);
247 void (*dma_rx_cb)(void *data);
248 void (*dma_tx_cb)(void *data);
249 int (*transfer_one_irq)(struct stm32_spi *spi);
250 irqreturn_t (*irq_handler_event)(int irq, void *dev_id);
251 irqreturn_t (*irq_handler_thread)(int irq, void *dev_id);
252 unsigned int baud_rate_div_min;
253 unsigned int baud_rate_div_max;
259 * struct stm32_spi - private data of the SPI controller
260 * @dev: driver model representation of the controller
261 * @master: controller master interface
262 * @cfg: compatible configuration data
263 * @base: virtual memory area
264 * @clk: hw kernel clock feeding the SPI clock generator
265 * @clk_rate: rate of the hw kernel clock feeding the SPI clock generator
266 * @lock: prevent I/O concurrent access
267 * @irq: SPI controller interrupt line
268 * @fifo_size: size of the embedded fifo in bytes
269 * @cur_midi: master inter-data idleness in ns
270 * @cur_speed: speed configured in Hz
271 * @cur_bpw: number of bits in a single SPI data frame
272 * @cur_fthlv: fifo threshold level (data frames in a single data packet)
273 * @cur_comm: SPI communication mode
274 * @cur_xferlen: current transfer length in bytes
275 * @cur_usedma: boolean to know if dma is used in current transfer
276 * @tx_buf: data to be written, or NULL
277 * @rx_buf: data to be read, or NULL
278 * @tx_len: number of data to be written in bytes
279 * @rx_len: number of data to be read in bytes
280 * @dma_tx: dma channel for TX transfer
281 * @dma_rx: dma channel for RX transfer
282 * @phys_addr: SPI registers physical base address
286 struct spi_master *master;
287 const struct stm32_spi_cfg *cfg;
291 spinlock_t lock; /* prevent I/O concurrent access */
293 unsigned int fifo_size;
295 unsigned int cur_midi;
296 unsigned int cur_speed;
297 unsigned int cur_bpw;
298 unsigned int cur_fthlv;
299 unsigned int cur_comm;
300 unsigned int cur_xferlen;
307 struct dma_chan *dma_tx;
308 struct dma_chan *dma_rx;
309 dma_addr_t phys_addr;
312 static const struct stm32_spi_regspec stm32f4_spi_regspec = {
313 .en = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_SPE },
315 .dma_rx_en = { STM32F4_SPI_CR2, STM32F4_SPI_CR2_RXDMAEN },
316 .dma_tx_en = { STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXDMAEN },
318 .cpol = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPOL },
319 .cpha = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPHA },
320 .lsb_first = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_LSBFRST },
321 .br = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_BR, STM32F4_SPI_CR1_BR_SHIFT },
323 .rx = { STM32F4_SPI_DR },
324 .tx = { STM32F4_SPI_DR },
327 static const struct stm32_spi_regspec stm32h7_spi_regspec = {
328 /* SPI data transfer is enabled but spi_ker_ck is idle.
329 * CFG1 and CFG2 registers are write protected when SPE is enabled.
331 .en = { STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE },
333 .dma_rx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_RXDMAEN },
334 .dma_tx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN },
336 .cpol = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPOL },
337 .cpha = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPHA },
338 .lsb_first = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_LSBFRST },
339 .br = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_MBR,
340 STM32H7_SPI_CFG1_MBR_SHIFT },
342 .rx = { STM32H7_SPI_RXDR },
343 .tx = { STM32H7_SPI_TXDR },
346 static inline void stm32_spi_set_bits(struct stm32_spi *spi,
347 u32 offset, u32 bits)
349 writel_relaxed(readl_relaxed(spi->base + offset) | bits,
353 static inline void stm32_spi_clr_bits(struct stm32_spi *spi,
354 u32 offset, u32 bits)
356 writel_relaxed(readl_relaxed(spi->base + offset) & ~bits,
361 * stm32h7_spi_get_fifo_size - Return fifo size
362 * @spi: pointer to the spi controller data structure
364 static int stm32h7_spi_get_fifo_size(struct stm32_spi *spi)
369 spin_lock_irqsave(&spi->lock, flags);
371 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
373 while (readl_relaxed(spi->base + STM32H7_SPI_SR) & STM32H7_SPI_SR_TXP)
374 writeb_relaxed(++count, spi->base + STM32H7_SPI_TXDR);
376 stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
378 spin_unlock_irqrestore(&spi->lock, flags);
380 dev_dbg(spi->dev, "%d x 8-bit fifo size\n", count);
386 * stm32f4_spi_get_bpw_mask - Return bits per word mask
387 * @spi: pointer to the spi controller data structure
389 static int stm32f4_spi_get_bpw_mask(struct stm32_spi *spi)
391 dev_dbg(spi->dev, "8-bit or 16-bit data frame supported\n");
392 return SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
396 * stm32h7_spi_get_bpw_mask - Return bits per word mask
397 * @spi: pointer to the spi controller data structure
399 static int stm32h7_spi_get_bpw_mask(struct stm32_spi *spi)
404 spin_lock_irqsave(&spi->lock, flags);
407 * The most significant bit at DSIZE bit field is reserved when the
408 * maximum data size of periperal instances is limited to 16-bit
410 stm32_spi_set_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_DSIZE);
412 cfg1 = readl_relaxed(spi->base + STM32H7_SPI_CFG1);
413 max_bpw = FIELD_GET(STM32H7_SPI_CFG1_DSIZE, cfg1) + 1;
415 spin_unlock_irqrestore(&spi->lock, flags);
417 dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw);
419 return SPI_BPW_RANGE_MASK(4, max_bpw);
423 * stm32_spi_prepare_mbr - Determine baud rate divisor value
424 * @spi: pointer to the spi controller data structure
425 * @speed_hz: requested speed
426 * @min_div: minimum baud rate divisor
427 * @max_div: maximum baud rate divisor
429 * Return baud rate divisor value in case of success or -EINVAL
431 static int stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz,
432 u32 min_div, u32 max_div)
436 /* Ensure spi->clk_rate is even */
437 div = DIV_ROUND_CLOSEST(spi->clk_rate & ~0x1, speed_hz);
440 * SPI framework set xfer->speed_hz to master->max_speed_hz if
441 * xfer->speed_hz is greater than master->max_speed_hz, and it returns
442 * an error when xfer->speed_hz is lower than master->min_speed_hz, so
443 * no need to check it there.
444 * However, we need to ensure the following calculations.
446 if ((div < min_div) || (div > max_div))
449 /* Determine the first power of 2 greater than or equal to div */
453 mbrdiv = fls(div) - 1;
455 spi->cur_speed = spi->clk_rate / (1 << mbrdiv);
461 * stm32h7_spi_prepare_fthlv - Determine FIFO threshold level
462 * @spi: pointer to the spi controller data structure
463 * @xfer_len: length of the message to be transferred
465 static u32 stm32h7_spi_prepare_fthlv(struct stm32_spi *spi, u32 xfer_len)
469 /* data packet should not exceed 1/2 of fifo space */
470 packet = clamp(xfer_len, 1U, spi->fifo_size / 2);
472 /* align packet size with data registers access */
473 bpw = DIV_ROUND_UP(spi->cur_bpw, 8);
474 return DIV_ROUND_UP(packet, bpw);
478 * stm32f4_spi_write_tx - Write bytes to Transmit Data Register
479 * @spi: pointer to the spi controller data structure
481 * Read from tx_buf depends on remaining bytes to avoid to read beyond
484 static void stm32f4_spi_write_tx(struct stm32_spi *spi)
486 if ((spi->tx_len > 0) && (readl_relaxed(spi->base + STM32F4_SPI_SR) &
487 STM32F4_SPI_SR_TXE)) {
488 u32 offs = spi->cur_xferlen - spi->tx_len;
490 if (spi->cur_bpw == 16) {
491 const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
493 writew_relaxed(*tx_buf16, spi->base + STM32F4_SPI_DR);
494 spi->tx_len -= sizeof(u16);
496 const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
498 writeb_relaxed(*tx_buf8, spi->base + STM32F4_SPI_DR);
499 spi->tx_len -= sizeof(u8);
503 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
507 * stm32h7_spi_write_txfifo - Write bytes in Transmit Data Register
508 * @spi: pointer to the spi controller data structure
510 * Read from tx_buf depends on remaining bytes to avoid to read beyond
513 static void stm32h7_spi_write_txfifo(struct stm32_spi *spi)
515 while ((spi->tx_len > 0) &&
516 (readl_relaxed(spi->base + STM32H7_SPI_SR) &
517 STM32H7_SPI_SR_TXP)) {
518 u32 offs = spi->cur_xferlen - spi->tx_len;
520 if (spi->tx_len >= sizeof(u32)) {
521 const u32 *tx_buf32 = (const u32 *)(spi->tx_buf + offs);
523 writel_relaxed(*tx_buf32, spi->base + STM32H7_SPI_TXDR);
524 spi->tx_len -= sizeof(u32);
525 } else if (spi->tx_len >= sizeof(u16)) {
526 const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
528 writew_relaxed(*tx_buf16, spi->base + STM32H7_SPI_TXDR);
529 spi->tx_len -= sizeof(u16);
531 const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
533 writeb_relaxed(*tx_buf8, spi->base + STM32H7_SPI_TXDR);
534 spi->tx_len -= sizeof(u8);
538 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
542 * stm32f4_spi_read_rx - Read bytes from Receive Data Register
543 * @spi: pointer to the spi controller data structure
545 * Write in rx_buf depends on remaining bytes to avoid to write beyond
548 static void stm32f4_spi_read_rx(struct stm32_spi *spi)
550 if ((spi->rx_len > 0) && (readl_relaxed(spi->base + STM32F4_SPI_SR) &
551 STM32F4_SPI_SR_RXNE)) {
552 u32 offs = spi->cur_xferlen - spi->rx_len;
554 if (spi->cur_bpw == 16) {
555 u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
557 *rx_buf16 = readw_relaxed(spi->base + STM32F4_SPI_DR);
558 spi->rx_len -= sizeof(u16);
560 u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
562 *rx_buf8 = readb_relaxed(spi->base + STM32F4_SPI_DR);
563 spi->rx_len -= sizeof(u8);
567 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->rx_len);
571 * stm32h7_spi_read_rxfifo - Read bytes in Receive Data Register
572 * @spi: pointer to the spi controller data structure
574 * Write in rx_buf depends on remaining bytes to avoid to write beyond
577 static void stm32h7_spi_read_rxfifo(struct stm32_spi *spi)
579 u32 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
580 u32 rxplvl = FIELD_GET(STM32H7_SPI_SR_RXPLVL, sr);
582 while ((spi->rx_len > 0) &&
583 ((sr & STM32H7_SPI_SR_RXP) ||
584 ((sr & STM32H7_SPI_SR_EOT) &&
585 ((sr & STM32H7_SPI_SR_RXWNE) || (rxplvl > 0))))) {
586 u32 offs = spi->cur_xferlen - spi->rx_len;
588 if ((spi->rx_len >= sizeof(u32)) ||
589 (sr & STM32H7_SPI_SR_RXWNE)) {
590 u32 *rx_buf32 = (u32 *)(spi->rx_buf + offs);
592 *rx_buf32 = readl_relaxed(spi->base + STM32H7_SPI_RXDR);
593 spi->rx_len -= sizeof(u32);
594 } else if ((spi->rx_len >= sizeof(u16)) ||
595 (!(sr & STM32H7_SPI_SR_RXWNE) &&
596 (rxplvl >= 2 || spi->cur_bpw > 8))) {
597 u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
599 *rx_buf16 = readw_relaxed(spi->base + STM32H7_SPI_RXDR);
600 spi->rx_len -= sizeof(u16);
602 u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
604 *rx_buf8 = readb_relaxed(spi->base + STM32H7_SPI_RXDR);
605 spi->rx_len -= sizeof(u8);
608 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
609 rxplvl = FIELD_GET(STM32H7_SPI_SR_RXPLVL, sr);
612 dev_dbg(spi->dev, "%s: %d bytes left (sr=%08x)\n",
613 __func__, spi->rx_len, sr);
617 * stm32_spi_enable - Enable SPI controller
618 * @spi: pointer to the spi controller data structure
620 static void stm32_spi_enable(struct stm32_spi *spi)
622 dev_dbg(spi->dev, "enable controller\n");
624 stm32_spi_set_bits(spi, spi->cfg->regs->en.reg,
625 spi->cfg->regs->en.mask);
629 * stm32f4_spi_disable - Disable SPI controller
630 * @spi: pointer to the spi controller data structure
632 static void stm32f4_spi_disable(struct stm32_spi *spi)
637 dev_dbg(spi->dev, "disable controller\n");
639 spin_lock_irqsave(&spi->lock, flags);
641 if (!(readl_relaxed(spi->base + STM32F4_SPI_CR1) &
642 STM32F4_SPI_CR1_SPE)) {
643 spin_unlock_irqrestore(&spi->lock, flags);
647 /* Disable interrupts */
648 stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXEIE |
649 STM32F4_SPI_CR2_RXNEIE |
650 STM32F4_SPI_CR2_ERRIE);
652 /* Wait until BSY = 0 */
653 if (readl_relaxed_poll_timeout_atomic(spi->base + STM32F4_SPI_SR,
654 sr, !(sr & STM32F4_SPI_SR_BSY),
656 dev_warn(spi->dev, "disabling condition timeout\n");
659 if (spi->cur_usedma && spi->dma_tx)
660 dmaengine_terminate_all(spi->dma_tx);
661 if (spi->cur_usedma && spi->dma_rx)
662 dmaengine_terminate_all(spi->dma_rx);
664 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_SPE);
666 stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXDMAEN |
667 STM32F4_SPI_CR2_RXDMAEN);
669 /* Sequence to clear OVR flag */
670 readl_relaxed(spi->base + STM32F4_SPI_DR);
671 readl_relaxed(spi->base + STM32F4_SPI_SR);
673 spin_unlock_irqrestore(&spi->lock, flags);
677 * stm32h7_spi_disable - Disable SPI controller
678 * @spi: pointer to the spi controller data structure
680 * RX-Fifo is flushed when SPI controller is disabled.
682 static void stm32h7_spi_disable(struct stm32_spi *spi)
687 dev_dbg(spi->dev, "disable controller\n");
689 spin_lock_irqsave(&spi->lock, flags);
691 cr1 = readl_relaxed(spi->base + STM32H7_SPI_CR1);
693 if (!(cr1 & STM32H7_SPI_CR1_SPE)) {
694 spin_unlock_irqrestore(&spi->lock, flags);
698 if (spi->cur_usedma && spi->dma_tx)
699 dmaengine_terminate_all(spi->dma_tx);
700 if (spi->cur_usedma && spi->dma_rx)
701 dmaengine_terminate_all(spi->dma_rx);
703 stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
705 stm32_spi_clr_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN |
706 STM32H7_SPI_CFG1_RXDMAEN);
708 /* Disable interrupts and clear status flags */
709 writel_relaxed(0, spi->base + STM32H7_SPI_IER);
710 writel_relaxed(STM32H7_SPI_IFCR_ALL, spi->base + STM32H7_SPI_IFCR);
712 spin_unlock_irqrestore(&spi->lock, flags);
716 * stm32_spi_can_dma - Determine if the transfer is eligible for DMA use
717 * @master: controller master interface
718 * @spi_dev: pointer to the spi device
719 * @transfer: pointer to spi transfer
721 * If driver has fifo and the current transfer size is greater than fifo size,
722 * use DMA. Otherwise use DMA for transfer longer than defined DMA min bytes.
724 static bool stm32_spi_can_dma(struct spi_master *master,
725 struct spi_device *spi_dev,
726 struct spi_transfer *transfer)
728 unsigned int dma_size;
729 struct stm32_spi *spi = spi_master_get_devdata(master);
731 if (spi->cfg->has_fifo)
732 dma_size = spi->fifo_size;
734 dma_size = SPI_DMA_MIN_BYTES;
736 dev_dbg(spi->dev, "%s: %s\n", __func__,
737 (transfer->len > dma_size) ? "true" : "false");
739 return (transfer->len > dma_size);
743 * stm32f4_spi_irq_event - Interrupt handler for SPI controller events
744 * @irq: interrupt line
745 * @dev_id: SPI controller master interface
747 static irqreturn_t stm32f4_spi_irq_event(int irq, void *dev_id)
749 struct spi_master *master = dev_id;
750 struct stm32_spi *spi = spi_master_get_devdata(master);
754 spin_lock(&spi->lock);
756 sr = readl_relaxed(spi->base + STM32F4_SPI_SR);
758 * BSY flag is not handled in interrupt but it is normal behavior when
761 sr &= ~STM32F4_SPI_SR_BSY;
763 if (!spi->cur_usedma && (spi->cur_comm == SPI_SIMPLEX_TX ||
764 spi->cur_comm == SPI_3WIRE_TX)) {
765 /* OVR flag shouldn't be handled for TX only mode */
766 sr &= ~(STM32F4_SPI_SR_OVR | STM32F4_SPI_SR_RXNE);
767 mask |= STM32F4_SPI_SR_TXE;
770 if (!spi->cur_usedma && (spi->cur_comm == SPI_FULL_DUPLEX ||
771 spi->cur_comm == SPI_SIMPLEX_RX ||
772 spi->cur_comm == SPI_3WIRE_RX)) {
773 /* TXE flag is set and is handled when RXNE flag occurs */
774 sr &= ~STM32F4_SPI_SR_TXE;
775 mask |= STM32F4_SPI_SR_RXNE | STM32F4_SPI_SR_OVR;
779 dev_dbg(spi->dev, "spurious IT (sr=0x%08x)\n", sr);
780 spin_unlock(&spi->lock);
784 if (sr & STM32F4_SPI_SR_OVR) {
785 dev_warn(spi->dev, "Overrun: received value discarded\n");
787 /* Sequence to clear OVR flag */
788 readl_relaxed(spi->base + STM32F4_SPI_DR);
789 readl_relaxed(spi->base + STM32F4_SPI_SR);
792 * If overrun is detected, it means that something went wrong,
793 * so stop the current transfer. Transfer can wait for next
794 * RXNE but DR is already read and end never happens.
800 if (sr & STM32F4_SPI_SR_TXE) {
802 stm32f4_spi_write_tx(spi);
803 if (spi->tx_len == 0)
807 if (sr & STM32F4_SPI_SR_RXNE) {
808 stm32f4_spi_read_rx(spi);
809 if (spi->rx_len == 0)
811 else if (spi->tx_buf)/* Load data for discontinuous mode */
812 stm32f4_spi_write_tx(spi);
817 /* Immediately disable interrupts to do not generate new one */
818 stm32_spi_clr_bits(spi, STM32F4_SPI_CR2,
819 STM32F4_SPI_CR2_TXEIE |
820 STM32F4_SPI_CR2_RXNEIE |
821 STM32F4_SPI_CR2_ERRIE);
822 spin_unlock(&spi->lock);
823 return IRQ_WAKE_THREAD;
826 spin_unlock(&spi->lock);
831 * stm32f4_spi_irq_thread - Thread of interrupt handler for SPI controller
832 * @irq: interrupt line
833 * @dev_id: SPI controller master interface
835 static irqreturn_t stm32f4_spi_irq_thread(int irq, void *dev_id)
837 struct spi_master *master = dev_id;
838 struct stm32_spi *spi = spi_master_get_devdata(master);
840 spi_finalize_current_transfer(master);
841 stm32f4_spi_disable(spi);
847 * stm32h7_spi_irq_thread - Thread of interrupt handler for SPI controller
848 * @irq: interrupt line
849 * @dev_id: SPI controller master interface
851 static irqreturn_t stm32h7_spi_irq_thread(int irq, void *dev_id)
853 struct spi_master *master = dev_id;
854 struct stm32_spi *spi = spi_master_get_devdata(master);
859 spin_lock_irqsave(&spi->lock, flags);
861 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
862 ier = readl_relaxed(spi->base + STM32H7_SPI_IER);
866 * EOTIE enables irq from EOT, SUSP and TXC events. We need to set
867 * SUSP to acknowledge it later. TXC is automatically cleared
870 mask |= STM32H7_SPI_SR_SUSP;
872 * DXPIE is set in Full-Duplex, one IT will be raised if TXP and RXP
873 * are set. So in case of Full-Duplex, need to poll TXP and RXP event.
875 if ((spi->cur_comm == SPI_FULL_DUPLEX) && !spi->cur_usedma)
876 mask |= STM32H7_SPI_SR_TXP | STM32H7_SPI_SR_RXP;
879 dev_warn(spi->dev, "spurious IT (sr=0x%08x, ier=0x%08x)\n",
881 spin_unlock_irqrestore(&spi->lock, flags);
885 if (sr & STM32H7_SPI_SR_SUSP) {
886 static DEFINE_RATELIMIT_STATE(rs,
887 DEFAULT_RATELIMIT_INTERVAL * 10,
889 ratelimit_set_flags(&rs, RATELIMIT_MSG_ON_RELEASE);
890 if (__ratelimit(&rs))
891 dev_dbg_ratelimited(spi->dev, "Communication suspended\n");
892 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
893 stm32h7_spi_read_rxfifo(spi);
895 * If communication is suspended while using DMA, it means
896 * that something went wrong, so stop the current transfer
902 if (sr & STM32H7_SPI_SR_MODF) {
903 dev_warn(spi->dev, "Mode fault: transfer aborted\n");
907 if (sr & STM32H7_SPI_SR_OVR) {
908 dev_err(spi->dev, "Overrun: RX data lost\n");
912 if (sr & STM32H7_SPI_SR_EOT) {
913 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
914 stm32h7_spi_read_rxfifo(spi);
915 if (!spi->cur_usedma ||
916 (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX))
920 if (sr & STM32H7_SPI_SR_TXP)
921 if (!spi->cur_usedma && (spi->tx_buf && (spi->tx_len > 0)))
922 stm32h7_spi_write_txfifo(spi);
924 if (sr & STM32H7_SPI_SR_RXP)
925 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
926 stm32h7_spi_read_rxfifo(spi);
928 writel_relaxed(sr & mask, spi->base + STM32H7_SPI_IFCR);
930 spin_unlock_irqrestore(&spi->lock, flags);
933 stm32h7_spi_disable(spi);
934 spi_finalize_current_transfer(master);
941 * stm32_spi_prepare_msg - set up the controller to transfer a single message
942 * @master: controller master interface
943 * @msg: pointer to spi message
945 static int stm32_spi_prepare_msg(struct spi_master *master,
946 struct spi_message *msg)
948 struct stm32_spi *spi = spi_master_get_devdata(master);
949 struct spi_device *spi_dev = msg->spi;
950 struct device_node *np = spi_dev->dev.of_node;
952 u32 clrb = 0, setb = 0;
954 /* SPI slave device may need time between data frames */
956 if (np && !of_property_read_u32(np, "st,spi-midi-ns", &spi->cur_midi))
957 dev_dbg(spi->dev, "%dns inter-data idleness\n", spi->cur_midi);
959 if (spi_dev->mode & SPI_CPOL)
960 setb |= spi->cfg->regs->cpol.mask;
962 clrb |= spi->cfg->regs->cpol.mask;
964 if (spi_dev->mode & SPI_CPHA)
965 setb |= spi->cfg->regs->cpha.mask;
967 clrb |= spi->cfg->regs->cpha.mask;
969 if (spi_dev->mode & SPI_LSB_FIRST)
970 setb |= spi->cfg->regs->lsb_first.mask;
972 clrb |= spi->cfg->regs->lsb_first.mask;
974 dev_dbg(spi->dev, "cpol=%d cpha=%d lsb_first=%d cs_high=%d\n",
975 !!(spi_dev->mode & SPI_CPOL),
976 !!(spi_dev->mode & SPI_CPHA),
977 !!(spi_dev->mode & SPI_LSB_FIRST),
978 !!(spi_dev->mode & SPI_CS_HIGH));
980 /* On STM32H7, messages should not exceed a maximum size setted
981 * afterward via the set_number_of_data function. In order to
982 * ensure that, split large messages into several messages
984 if (spi->cfg->set_number_of_data) {
987 ret = spi_split_transfers_maxsize(master, msg,
988 STM32H7_SPI_TSIZE_MAX,
989 GFP_KERNEL | GFP_DMA);
994 spin_lock_irqsave(&spi->lock, flags);
996 /* CPOL, CPHA and LSB FIRST bits have common register */
999 (readl_relaxed(spi->base + spi->cfg->regs->cpol.reg) &
1001 spi->base + spi->cfg->regs->cpol.reg);
1003 spin_unlock_irqrestore(&spi->lock, flags);
1009 * stm32f4_spi_dma_tx_cb - dma callback
1010 * @data: pointer to the spi controller data structure
1012 * DMA callback is called when the transfer is complete for DMA TX channel.
1014 static void stm32f4_spi_dma_tx_cb(void *data)
1016 struct stm32_spi *spi = data;
1018 if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
1019 spi_finalize_current_transfer(spi->master);
1020 stm32f4_spi_disable(spi);
1025 * stm32_spi_dma_rx_cb - dma callback
1026 * @data: pointer to the spi controller data structure
1028 * DMA callback is called when the transfer is complete for DMA RX channel.
1030 static void stm32_spi_dma_rx_cb(void *data)
1032 struct stm32_spi *spi = data;
1034 spi_finalize_current_transfer(spi->master);
1035 spi->cfg->disable(spi);
1039 * stm32_spi_dma_config - configure dma slave channel depending on current
1040 * transfer bits_per_word.
1041 * @spi: pointer to the spi controller data structure
1042 * @dma_conf: pointer to the dma_slave_config structure
1043 * @dir: direction of the dma transfer
1045 static void stm32_spi_dma_config(struct stm32_spi *spi,
1046 struct dma_slave_config *dma_conf,
1047 enum dma_transfer_direction dir)
1049 enum dma_slave_buswidth buswidth;
1052 if (spi->cur_bpw <= 8)
1053 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
1054 else if (spi->cur_bpw <= 16)
1055 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1057 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
1059 if (spi->cfg->has_fifo) {
1060 /* Valid for DMA Half or Full Fifo threshold */
1061 if (spi->cur_fthlv == 2)
1064 maxburst = spi->cur_fthlv;
1069 memset(dma_conf, 0, sizeof(struct dma_slave_config));
1070 dma_conf->direction = dir;
1071 if (dma_conf->direction == DMA_DEV_TO_MEM) { /* RX */
1072 dma_conf->src_addr = spi->phys_addr + spi->cfg->regs->rx.reg;
1073 dma_conf->src_addr_width = buswidth;
1074 dma_conf->src_maxburst = maxburst;
1076 dev_dbg(spi->dev, "Rx DMA config buswidth=%d, maxburst=%d\n",
1077 buswidth, maxburst);
1078 } else if (dma_conf->direction == DMA_MEM_TO_DEV) { /* TX */
1079 dma_conf->dst_addr = spi->phys_addr + spi->cfg->regs->tx.reg;
1080 dma_conf->dst_addr_width = buswidth;
1081 dma_conf->dst_maxburst = maxburst;
1083 dev_dbg(spi->dev, "Tx DMA config buswidth=%d, maxburst=%d\n",
1084 buswidth, maxburst);
1089 * stm32f4_spi_transfer_one_irq - transfer a single spi_transfer using
1091 * @spi: pointer to the spi controller data structure
1093 * It must returns 0 if the transfer is finished or 1 if the transfer is still
1096 static int stm32f4_spi_transfer_one_irq(struct stm32_spi *spi)
1098 unsigned long flags;
1101 /* Enable the interrupts relative to the current communication mode */
1102 if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
1103 cr2 |= STM32F4_SPI_CR2_TXEIE;
1104 } else if (spi->cur_comm == SPI_FULL_DUPLEX ||
1105 spi->cur_comm == SPI_SIMPLEX_RX ||
1106 spi->cur_comm == SPI_3WIRE_RX) {
1107 /* In transmit-only mode, the OVR flag is set in the SR register
1108 * since the received data are never read. Therefore set OVR
1109 * interrupt only when rx buffer is available.
1111 cr2 |= STM32F4_SPI_CR2_RXNEIE | STM32F4_SPI_CR2_ERRIE;
1116 spin_lock_irqsave(&spi->lock, flags);
1118 stm32_spi_set_bits(spi, STM32F4_SPI_CR2, cr2);
1120 stm32_spi_enable(spi);
1122 /* starting data transfer when buffer is loaded */
1124 stm32f4_spi_write_tx(spi);
1126 spin_unlock_irqrestore(&spi->lock, flags);
1132 * stm32h7_spi_transfer_one_irq - transfer a single spi_transfer using
1134 * @spi: pointer to the spi controller data structure
1136 * It must returns 0 if the transfer is finished or 1 if the transfer is still
1139 static int stm32h7_spi_transfer_one_irq(struct stm32_spi *spi)
1141 unsigned long flags;
1144 /* Enable the interrupts relative to the current communication mode */
1145 if (spi->tx_buf && spi->rx_buf) /* Full Duplex */
1146 ier |= STM32H7_SPI_IER_DXPIE;
1147 else if (spi->tx_buf) /* Half-Duplex TX dir or Simplex TX */
1148 ier |= STM32H7_SPI_IER_TXPIE;
1149 else if (spi->rx_buf) /* Half-Duplex RX dir or Simplex RX */
1150 ier |= STM32H7_SPI_IER_RXPIE;
1152 /* Enable the interrupts relative to the end of transfer */
1153 ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE |
1154 STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
1156 spin_lock_irqsave(&spi->lock, flags);
1158 stm32_spi_enable(spi);
1160 /* Be sure to have data in fifo before starting data transfer */
1162 stm32h7_spi_write_txfifo(spi);
1164 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
1166 writel_relaxed(ier, spi->base + STM32H7_SPI_IER);
1168 spin_unlock_irqrestore(&spi->lock, flags);
1174 * stm32f4_spi_transfer_one_dma_start - Set SPI driver registers to start
1175 * transfer using DMA
1176 * @spi: pointer to the spi controller data structure
1178 static void stm32f4_spi_transfer_one_dma_start(struct stm32_spi *spi)
1180 /* In DMA mode end of transfer is handled by DMA TX or RX callback. */
1181 if (spi->cur_comm == SPI_SIMPLEX_RX || spi->cur_comm == SPI_3WIRE_RX ||
1182 spi->cur_comm == SPI_FULL_DUPLEX) {
1184 * In transmit-only mode, the OVR flag is set in the SR register
1185 * since the received data are never read. Therefore set OVR
1186 * interrupt only when rx buffer is available.
1188 stm32_spi_set_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_ERRIE);
1191 stm32_spi_enable(spi);
1195 * stm32h7_spi_transfer_one_dma_start - Set SPI driver registers to start
1196 * transfer using DMA
1197 * @spi: pointer to the spi controller data structure
1199 static void stm32h7_spi_transfer_one_dma_start(struct stm32_spi *spi)
1201 uint32_t ier = STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
1203 /* Enable the interrupts */
1204 if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX)
1205 ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE;
1207 stm32_spi_set_bits(spi, STM32H7_SPI_IER, ier);
1209 stm32_spi_enable(spi);
1211 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
1215 * stm32_spi_transfer_one_dma - transfer a single spi_transfer using DMA
1216 * @spi: pointer to the spi controller data structure
1217 * @xfer: pointer to the spi_transfer structure
1219 * It must returns 0 if the transfer is finished or 1 if the transfer is still
1222 static int stm32_spi_transfer_one_dma(struct stm32_spi *spi,
1223 struct spi_transfer *xfer)
1225 struct dma_slave_config tx_dma_conf, rx_dma_conf;
1226 struct dma_async_tx_descriptor *tx_dma_desc, *rx_dma_desc;
1227 unsigned long flags;
1229 spin_lock_irqsave(&spi->lock, flags);
1232 if (spi->rx_buf && spi->dma_rx) {
1233 stm32_spi_dma_config(spi, &rx_dma_conf, DMA_DEV_TO_MEM);
1234 dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
1236 /* Enable Rx DMA request */
1237 stm32_spi_set_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1238 spi->cfg->regs->dma_rx_en.mask);
1240 rx_dma_desc = dmaengine_prep_slave_sg(
1241 spi->dma_rx, xfer->rx_sg.sgl,
1243 rx_dma_conf.direction,
1244 DMA_PREP_INTERRUPT);
1248 if (spi->tx_buf && spi->dma_tx) {
1249 stm32_spi_dma_config(spi, &tx_dma_conf, DMA_MEM_TO_DEV);
1250 dmaengine_slave_config(spi->dma_tx, &tx_dma_conf);
1252 tx_dma_desc = dmaengine_prep_slave_sg(
1253 spi->dma_tx, xfer->tx_sg.sgl,
1255 tx_dma_conf.direction,
1256 DMA_PREP_INTERRUPT);
1259 if ((spi->tx_buf && spi->dma_tx && !tx_dma_desc) ||
1260 (spi->rx_buf && spi->dma_rx && !rx_dma_desc))
1261 goto dma_desc_error;
1263 if (spi->cur_comm == SPI_FULL_DUPLEX && (!tx_dma_desc || !rx_dma_desc))
1264 goto dma_desc_error;
1267 rx_dma_desc->callback = spi->cfg->dma_rx_cb;
1268 rx_dma_desc->callback_param = spi;
1270 if (dma_submit_error(dmaengine_submit(rx_dma_desc))) {
1271 dev_err(spi->dev, "Rx DMA submit failed\n");
1272 goto dma_desc_error;
1274 /* Enable Rx DMA channel */
1275 dma_async_issue_pending(spi->dma_rx);
1279 if (spi->cur_comm == SPI_SIMPLEX_TX ||
1280 spi->cur_comm == SPI_3WIRE_TX) {
1281 tx_dma_desc->callback = spi->cfg->dma_tx_cb;
1282 tx_dma_desc->callback_param = spi;
1285 if (dma_submit_error(dmaengine_submit(tx_dma_desc))) {
1286 dev_err(spi->dev, "Tx DMA submit failed\n");
1287 goto dma_submit_error;
1289 /* Enable Tx DMA channel */
1290 dma_async_issue_pending(spi->dma_tx);
1292 /* Enable Tx DMA request */
1293 stm32_spi_set_bits(spi, spi->cfg->regs->dma_tx_en.reg,
1294 spi->cfg->regs->dma_tx_en.mask);
1297 spi->cfg->transfer_one_dma_start(spi);
1299 spin_unlock_irqrestore(&spi->lock, flags);
1305 dmaengine_terminate_all(spi->dma_rx);
1308 stm32_spi_clr_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1309 spi->cfg->regs->dma_rx_en.mask);
1311 spin_unlock_irqrestore(&spi->lock, flags);
1313 dev_info(spi->dev, "DMA issue: fall back to irq transfer\n");
1315 spi->cur_usedma = false;
1316 return spi->cfg->transfer_one_irq(spi);
1320 * stm32f4_spi_set_bpw - Configure bits per word
1321 * @spi: pointer to the spi controller data structure
1323 static void stm32f4_spi_set_bpw(struct stm32_spi *spi)
1325 if (spi->cur_bpw == 16)
1326 stm32_spi_set_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_DFF);
1328 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_DFF);
1332 * stm32h7_spi_set_bpw - configure bits per word
1333 * @spi: pointer to the spi controller data structure
1335 static void stm32h7_spi_set_bpw(struct stm32_spi *spi)
1338 u32 cfg1_clrb = 0, cfg1_setb = 0;
1340 bpw = spi->cur_bpw - 1;
1342 cfg1_clrb |= STM32H7_SPI_CFG1_DSIZE;
1343 cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_DSIZE, bpw);
1345 spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi, spi->cur_xferlen);
1346 fthlv = spi->cur_fthlv - 1;
1348 cfg1_clrb |= STM32H7_SPI_CFG1_FTHLV;
1349 cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_FTHLV, fthlv);
1352 (readl_relaxed(spi->base + STM32H7_SPI_CFG1) &
1353 ~cfg1_clrb) | cfg1_setb,
1354 spi->base + STM32H7_SPI_CFG1);
1358 * stm32_spi_set_mbr - Configure baud rate divisor in master mode
1359 * @spi: pointer to the spi controller data structure
1360 * @mbrdiv: baud rate divisor value
1362 static void stm32_spi_set_mbr(struct stm32_spi *spi, u32 mbrdiv)
1364 u32 clrb = 0, setb = 0;
1366 clrb |= spi->cfg->regs->br.mask;
1367 setb |= (mbrdiv << spi->cfg->regs->br.shift) & spi->cfg->regs->br.mask;
1369 writel_relaxed((readl_relaxed(spi->base + spi->cfg->regs->br.reg) &
1371 spi->base + spi->cfg->regs->br.reg);
1375 * stm32_spi_communication_type - return transfer communication type
1376 * @spi_dev: pointer to the spi device
1377 * @transfer: pointer to spi transfer
1379 static unsigned int stm32_spi_communication_type(struct spi_device *spi_dev,
1380 struct spi_transfer *transfer)
1382 unsigned int type = SPI_FULL_DUPLEX;
1384 if (spi_dev->mode & SPI_3WIRE) { /* MISO/MOSI signals shared */
1386 * SPI_3WIRE and xfer->tx_buf != NULL and xfer->rx_buf != NULL
1387 * is forbidden and unvalidated by SPI subsystem so depending
1388 * on the valid buffer, we can determine the direction of the
1391 if (!transfer->tx_buf)
1392 type = SPI_3WIRE_RX;
1394 type = SPI_3WIRE_TX;
1396 if (!transfer->tx_buf)
1397 type = SPI_SIMPLEX_RX;
1398 else if (!transfer->rx_buf)
1399 type = SPI_SIMPLEX_TX;
1406 * stm32f4_spi_set_mode - configure communication mode
1407 * @spi: pointer to the spi controller data structure
1408 * @comm_type: type of communication to configure
1410 static int stm32f4_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
1412 if (comm_type == SPI_3WIRE_TX || comm_type == SPI_SIMPLEX_TX) {
1413 stm32_spi_set_bits(spi, STM32F4_SPI_CR1,
1414 STM32F4_SPI_CR1_BIDIMODE |
1415 STM32F4_SPI_CR1_BIDIOE);
1416 } else if (comm_type == SPI_FULL_DUPLEX ||
1417 comm_type == SPI_SIMPLEX_RX) {
1418 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1,
1419 STM32F4_SPI_CR1_BIDIMODE |
1420 STM32F4_SPI_CR1_BIDIOE);
1421 } else if (comm_type == SPI_3WIRE_RX) {
1422 stm32_spi_set_bits(spi, STM32F4_SPI_CR1,
1423 STM32F4_SPI_CR1_BIDIMODE);
1424 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1,
1425 STM32F4_SPI_CR1_BIDIOE);
1434 * stm32h7_spi_set_mode - configure communication mode
1435 * @spi: pointer to the spi controller data structure
1436 * @comm_type: type of communication to configure
1438 static int stm32h7_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
1441 u32 cfg2_clrb = 0, cfg2_setb = 0;
1443 if (comm_type == SPI_3WIRE_RX) {
1444 mode = STM32H7_SPI_HALF_DUPLEX;
1445 stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1446 } else if (comm_type == SPI_3WIRE_TX) {
1447 mode = STM32H7_SPI_HALF_DUPLEX;
1448 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1449 } else if (comm_type == SPI_SIMPLEX_RX) {
1450 mode = STM32H7_SPI_SIMPLEX_RX;
1451 } else if (comm_type == SPI_SIMPLEX_TX) {
1452 mode = STM32H7_SPI_SIMPLEX_TX;
1454 mode = STM32H7_SPI_FULL_DUPLEX;
1457 cfg2_clrb |= STM32H7_SPI_CFG2_COMM;
1458 cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_COMM, mode);
1461 (readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1462 ~cfg2_clrb) | cfg2_setb,
1463 spi->base + STM32H7_SPI_CFG2);
1469 * stm32h7_spi_data_idleness - configure minimum time delay inserted between two
1470 * consecutive data frames in master mode
1471 * @spi: pointer to the spi controller data structure
1472 * @len: transfer len
1474 static void stm32h7_spi_data_idleness(struct stm32_spi *spi, u32 len)
1476 u32 cfg2_clrb = 0, cfg2_setb = 0;
1478 cfg2_clrb |= STM32H7_SPI_CFG2_MIDI;
1479 if ((len > 1) && (spi->cur_midi > 0)) {
1480 u32 sck_period_ns = DIV_ROUND_UP(NSEC_PER_SEC, spi->cur_speed);
1481 u32 midi = min_t(u32,
1482 DIV_ROUND_UP(spi->cur_midi, sck_period_ns),
1483 FIELD_GET(STM32H7_SPI_CFG2_MIDI,
1484 STM32H7_SPI_CFG2_MIDI));
1487 dev_dbg(spi->dev, "period=%dns, midi=%d(=%dns)\n",
1488 sck_period_ns, midi, midi * sck_period_ns);
1489 cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_MIDI, midi);
1492 writel_relaxed((readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1493 ~cfg2_clrb) | cfg2_setb,
1494 spi->base + STM32H7_SPI_CFG2);
1498 * stm32h7_spi_number_of_data - configure number of data at current transfer
1499 * @spi: pointer to the spi controller data structure
1500 * @nb_words: transfer length (in words)
1502 static int stm32h7_spi_number_of_data(struct stm32_spi *spi, u32 nb_words)
1504 if (nb_words <= STM32H7_SPI_TSIZE_MAX) {
1505 writel_relaxed(FIELD_PREP(STM32H7_SPI_CR2_TSIZE, nb_words),
1506 spi->base + STM32H7_SPI_CR2);
1515 * stm32_spi_transfer_one_setup - common setup to transfer a single
1516 * spi_transfer either using DMA or
1518 * @spi: pointer to the spi controller data structure
1519 * @spi_dev: pointer to the spi device
1520 * @transfer: pointer to spi transfer
1522 static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
1523 struct spi_device *spi_dev,
1524 struct spi_transfer *transfer)
1526 unsigned long flags;
1527 unsigned int comm_type;
1528 int nb_words, ret = 0;
1531 spin_lock_irqsave(&spi->lock, flags);
1533 spi->cur_xferlen = transfer->len;
1535 spi->cur_bpw = transfer->bits_per_word;
1536 spi->cfg->set_bpw(spi);
1538 /* Update spi->cur_speed with real clock speed */
1539 mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz,
1540 spi->cfg->baud_rate_div_min,
1541 spi->cfg->baud_rate_div_max);
1547 transfer->speed_hz = spi->cur_speed;
1548 stm32_spi_set_mbr(spi, mbr);
1550 comm_type = stm32_spi_communication_type(spi_dev, transfer);
1551 ret = spi->cfg->set_mode(spi, comm_type);
1555 spi->cur_comm = comm_type;
1557 if (spi->cfg->set_data_idleness)
1558 spi->cfg->set_data_idleness(spi, transfer->len);
1560 if (spi->cur_bpw <= 8)
1561 nb_words = transfer->len;
1562 else if (spi->cur_bpw <= 16)
1563 nb_words = DIV_ROUND_UP(transfer->len * 8, 16);
1565 nb_words = DIV_ROUND_UP(transfer->len * 8, 32);
1567 if (spi->cfg->set_number_of_data) {
1568 ret = spi->cfg->set_number_of_data(spi, nb_words);
1573 dev_dbg(spi->dev, "transfer communication mode set to %d\n",
1576 "data frame of %d-bit, data packet of %d data frames\n",
1577 spi->cur_bpw, spi->cur_fthlv);
1578 dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed);
1579 dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n",
1580 spi->cur_xferlen, nb_words);
1581 dev_dbg(spi->dev, "dma %s\n",
1582 (spi->cur_usedma) ? "enabled" : "disabled");
1585 spin_unlock_irqrestore(&spi->lock, flags);
1591 * stm32_spi_transfer_one - transfer a single spi_transfer
1592 * @master: controller master interface
1593 * @spi_dev: pointer to the spi device
1594 * @transfer: pointer to spi transfer
1596 * It must return 0 if the transfer is finished or 1 if the transfer is still
1599 static int stm32_spi_transfer_one(struct spi_master *master,
1600 struct spi_device *spi_dev,
1601 struct spi_transfer *transfer)
1603 struct stm32_spi *spi = spi_master_get_devdata(master);
1606 spi->tx_buf = transfer->tx_buf;
1607 spi->rx_buf = transfer->rx_buf;
1608 spi->tx_len = spi->tx_buf ? transfer->len : 0;
1609 spi->rx_len = spi->rx_buf ? transfer->len : 0;
1611 spi->cur_usedma = (master->can_dma &&
1612 master->can_dma(master, spi_dev, transfer));
1614 ret = stm32_spi_transfer_one_setup(spi, spi_dev, transfer);
1616 dev_err(spi->dev, "SPI transfer setup failed\n");
1620 if (spi->cur_usedma)
1621 return stm32_spi_transfer_one_dma(spi, transfer);
1623 return spi->cfg->transfer_one_irq(spi);
1627 * stm32_spi_unprepare_msg - relax the hardware
1628 * @master: controller master interface
1629 * @msg: pointer to the spi message
1631 static int stm32_spi_unprepare_msg(struct spi_master *master,
1632 struct spi_message *msg)
1634 struct stm32_spi *spi = spi_master_get_devdata(master);
1636 spi->cfg->disable(spi);
1642 * stm32f4_spi_config - Configure SPI controller as SPI master
1643 * @spi: pointer to the spi controller data structure
1645 static int stm32f4_spi_config(struct stm32_spi *spi)
1647 unsigned long flags;
1649 spin_lock_irqsave(&spi->lock, flags);
1651 /* Ensure I2SMOD bit is kept cleared */
1652 stm32_spi_clr_bits(spi, STM32F4_SPI_I2SCFGR,
1653 STM32F4_SPI_I2SCFGR_I2SMOD);
1656 * - SS input value high
1657 * - transmitter half duplex direction
1658 * - Set the master mode (default Motorola mode)
1659 * - Consider 1 master/n slaves configuration and
1660 * SS input value is determined by the SSI bit
1662 stm32_spi_set_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_SSI |
1663 STM32F4_SPI_CR1_BIDIOE |
1664 STM32F4_SPI_CR1_MSTR |
1665 STM32F4_SPI_CR1_SSM);
1667 spin_unlock_irqrestore(&spi->lock, flags);
1673 * stm32h7_spi_config - Configure SPI controller as SPI master
1674 * @spi: pointer to the spi controller data structure
1676 static int stm32h7_spi_config(struct stm32_spi *spi)
1678 unsigned long flags;
1680 spin_lock_irqsave(&spi->lock, flags);
1682 /* Ensure I2SMOD bit is kept cleared */
1683 stm32_spi_clr_bits(spi, STM32H7_SPI_I2SCFGR,
1684 STM32H7_SPI_I2SCFGR_I2SMOD);
1687 * - SS input value high
1688 * - transmitter half duplex direction
1689 * - automatic communication suspend when RX-Fifo is full
1691 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SSI |
1692 STM32H7_SPI_CR1_HDDIR |
1693 STM32H7_SPI_CR1_MASRX);
1696 * - Set the master mode (default Motorola mode)
1697 * - Consider 1 master/n slaves configuration and
1698 * SS input value is determined by the SSI bit
1699 * - keep control of all associated GPIOs
1701 stm32_spi_set_bits(spi, STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_MASTER |
1702 STM32H7_SPI_CFG2_SSM |
1703 STM32H7_SPI_CFG2_AFCNTR);
1705 spin_unlock_irqrestore(&spi->lock, flags);
1710 static const struct stm32_spi_cfg stm32f4_spi_cfg = {
1711 .regs = &stm32f4_spi_regspec,
1712 .get_bpw_mask = stm32f4_spi_get_bpw_mask,
1713 .disable = stm32f4_spi_disable,
1714 .config = stm32f4_spi_config,
1715 .set_bpw = stm32f4_spi_set_bpw,
1716 .set_mode = stm32f4_spi_set_mode,
1717 .transfer_one_dma_start = stm32f4_spi_transfer_one_dma_start,
1718 .dma_tx_cb = stm32f4_spi_dma_tx_cb,
1719 .dma_rx_cb = stm32_spi_dma_rx_cb,
1720 .transfer_one_irq = stm32f4_spi_transfer_one_irq,
1721 .irq_handler_event = stm32f4_spi_irq_event,
1722 .irq_handler_thread = stm32f4_spi_irq_thread,
1723 .baud_rate_div_min = STM32F4_SPI_BR_DIV_MIN,
1724 .baud_rate_div_max = STM32F4_SPI_BR_DIV_MAX,
1726 .flags = SPI_MASTER_MUST_TX,
1729 static const struct stm32_spi_cfg stm32h7_spi_cfg = {
1730 .regs = &stm32h7_spi_regspec,
1731 .get_fifo_size = stm32h7_spi_get_fifo_size,
1732 .get_bpw_mask = stm32h7_spi_get_bpw_mask,
1733 .disable = stm32h7_spi_disable,
1734 .config = stm32h7_spi_config,
1735 .set_bpw = stm32h7_spi_set_bpw,
1736 .set_mode = stm32h7_spi_set_mode,
1737 .set_data_idleness = stm32h7_spi_data_idleness,
1738 .set_number_of_data = stm32h7_spi_number_of_data,
1739 .transfer_one_dma_start = stm32h7_spi_transfer_one_dma_start,
1740 .dma_rx_cb = stm32_spi_dma_rx_cb,
1742 * dma_tx_cb is not necessary since in case of TX, dma is followed by
1743 * SPI access hence handling is performed within the SPI interrupt
1745 .transfer_one_irq = stm32h7_spi_transfer_one_irq,
1746 .irq_handler_thread = stm32h7_spi_irq_thread,
1747 .baud_rate_div_min = STM32H7_SPI_MBR_DIV_MIN,
1748 .baud_rate_div_max = STM32H7_SPI_MBR_DIV_MAX,
1752 static const struct of_device_id stm32_spi_of_match[] = {
1753 { .compatible = "st,stm32h7-spi", .data = (void *)&stm32h7_spi_cfg },
1754 { .compatible = "st,stm32f4-spi", .data = (void *)&stm32f4_spi_cfg },
1757 MODULE_DEVICE_TABLE(of, stm32_spi_of_match);
1759 static int stm32_spi_probe(struct platform_device *pdev)
1761 struct spi_master *master;
1762 struct stm32_spi *spi;
1763 struct resource *res;
1764 struct reset_control *rst;
1767 master = devm_spi_alloc_master(&pdev->dev, sizeof(struct stm32_spi));
1769 dev_err(&pdev->dev, "spi master allocation failed\n");
1772 platform_set_drvdata(pdev, master);
1774 spi = spi_master_get_devdata(master);
1775 spi->dev = &pdev->dev;
1776 spi->master = master;
1777 spin_lock_init(&spi->lock);
1779 spi->cfg = (const struct stm32_spi_cfg *)
1780 of_match_device(pdev->dev.driver->of_match_table,
1783 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1784 spi->base = devm_ioremap_resource(&pdev->dev, res);
1785 if (IS_ERR(spi->base))
1786 return PTR_ERR(spi->base);
1788 spi->phys_addr = (dma_addr_t)res->start;
1790 spi->irq = platform_get_irq(pdev, 0);
1792 return dev_err_probe(&pdev->dev, spi->irq,
1793 "failed to get irq\n");
1795 ret = devm_request_threaded_irq(&pdev->dev, spi->irq,
1796 spi->cfg->irq_handler_event,
1797 spi->cfg->irq_handler_thread,
1798 IRQF_ONESHOT, pdev->name, master);
1800 dev_err(&pdev->dev, "irq%d request failed: %d\n", spi->irq,
1805 spi->clk = devm_clk_get(&pdev->dev, NULL);
1806 if (IS_ERR(spi->clk)) {
1807 ret = PTR_ERR(spi->clk);
1808 dev_err(&pdev->dev, "clk get failed: %d\n", ret);
1812 ret = clk_prepare_enable(spi->clk);
1814 dev_err(&pdev->dev, "clk enable failed: %d\n", ret);
1817 spi->clk_rate = clk_get_rate(spi->clk);
1818 if (!spi->clk_rate) {
1819 dev_err(&pdev->dev, "clk rate = 0\n");
1821 goto err_clk_disable;
1824 rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
1827 ret = dev_err_probe(&pdev->dev, PTR_ERR(rst),
1828 "failed to get reset\n");
1829 goto err_clk_disable;
1832 reset_control_assert(rst);
1834 reset_control_deassert(rst);
1837 if (spi->cfg->has_fifo)
1838 spi->fifo_size = spi->cfg->get_fifo_size(spi);
1840 ret = spi->cfg->config(spi);
1842 dev_err(&pdev->dev, "controller configuration failed: %d\n",
1844 goto err_clk_disable;
1847 master->dev.of_node = pdev->dev.of_node;
1848 master->auto_runtime_pm = true;
1849 master->bus_num = pdev->id;
1850 master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST |
1852 master->bits_per_word_mask = spi->cfg->get_bpw_mask(spi);
1853 master->max_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_min;
1854 master->min_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_max;
1855 master->use_gpio_descriptors = true;
1856 master->prepare_message = stm32_spi_prepare_msg;
1857 master->transfer_one = stm32_spi_transfer_one;
1858 master->unprepare_message = stm32_spi_unprepare_msg;
1859 master->flags = spi->cfg->flags;
1861 spi->dma_tx = dma_request_chan(spi->dev, "tx");
1862 if (IS_ERR(spi->dma_tx)) {
1863 ret = PTR_ERR(spi->dma_tx);
1865 if (ret == -EPROBE_DEFER)
1866 goto err_clk_disable;
1868 dev_warn(&pdev->dev, "failed to request tx dma channel\n");
1870 master->dma_tx = spi->dma_tx;
1873 spi->dma_rx = dma_request_chan(spi->dev, "rx");
1874 if (IS_ERR(spi->dma_rx)) {
1875 ret = PTR_ERR(spi->dma_rx);
1877 if (ret == -EPROBE_DEFER)
1878 goto err_dma_release;
1880 dev_warn(&pdev->dev, "failed to request rx dma channel\n");
1882 master->dma_rx = spi->dma_rx;
1885 if (spi->dma_tx || spi->dma_rx)
1886 master->can_dma = stm32_spi_can_dma;
1888 pm_runtime_set_autosuspend_delay(&pdev->dev,
1889 STM32_SPI_AUTOSUSPEND_DELAY);
1890 pm_runtime_use_autosuspend(&pdev->dev);
1891 pm_runtime_set_active(&pdev->dev);
1892 pm_runtime_get_noresume(&pdev->dev);
1893 pm_runtime_enable(&pdev->dev);
1895 ret = spi_register_master(master);
1897 dev_err(&pdev->dev, "spi master registration failed: %d\n",
1899 goto err_pm_disable;
1902 pm_runtime_mark_last_busy(&pdev->dev);
1903 pm_runtime_put_autosuspend(&pdev->dev);
1905 dev_info(&pdev->dev, "driver initialized\n");
1910 pm_runtime_disable(&pdev->dev);
1911 pm_runtime_put_noidle(&pdev->dev);
1912 pm_runtime_set_suspended(&pdev->dev);
1913 pm_runtime_dont_use_autosuspend(&pdev->dev);
1916 dma_release_channel(spi->dma_tx);
1918 dma_release_channel(spi->dma_rx);
1920 clk_disable_unprepare(spi->clk);
1925 static int stm32_spi_remove(struct platform_device *pdev)
1927 struct spi_master *master = platform_get_drvdata(pdev);
1928 struct stm32_spi *spi = spi_master_get_devdata(master);
1930 pm_runtime_get_sync(&pdev->dev);
1932 spi_unregister_master(master);
1933 spi->cfg->disable(spi);
1935 pm_runtime_disable(&pdev->dev);
1936 pm_runtime_put_noidle(&pdev->dev);
1937 pm_runtime_set_suspended(&pdev->dev);
1938 pm_runtime_dont_use_autosuspend(&pdev->dev);
1941 dma_release_channel(master->dma_tx);
1943 dma_release_channel(master->dma_rx);
1945 clk_disable_unprepare(spi->clk);
1948 pinctrl_pm_select_sleep_state(&pdev->dev);
1953 static int __maybe_unused stm32_spi_runtime_suspend(struct device *dev)
1955 struct spi_master *master = dev_get_drvdata(dev);
1956 struct stm32_spi *spi = spi_master_get_devdata(master);
1958 clk_disable_unprepare(spi->clk);
1960 return pinctrl_pm_select_sleep_state(dev);
1963 static int __maybe_unused stm32_spi_runtime_resume(struct device *dev)
1965 struct spi_master *master = dev_get_drvdata(dev);
1966 struct stm32_spi *spi = spi_master_get_devdata(master);
1969 ret = pinctrl_pm_select_default_state(dev);
1973 return clk_prepare_enable(spi->clk);
1976 static int __maybe_unused stm32_spi_suspend(struct device *dev)
1978 struct spi_master *master = dev_get_drvdata(dev);
1981 ret = spi_master_suspend(master);
1985 return pm_runtime_force_suspend(dev);
1988 static int __maybe_unused stm32_spi_resume(struct device *dev)
1990 struct spi_master *master = dev_get_drvdata(dev);
1991 struct stm32_spi *spi = spi_master_get_devdata(master);
1994 ret = pm_runtime_force_resume(dev);
1998 ret = spi_master_resume(master);
2000 clk_disable_unprepare(spi->clk);
2004 ret = pm_runtime_resume_and_get(dev);
2006 dev_err(dev, "Unable to power device:%d\n", ret);
2010 spi->cfg->config(spi);
2012 pm_runtime_mark_last_busy(dev);
2013 pm_runtime_put_autosuspend(dev);
2018 static const struct dev_pm_ops stm32_spi_pm_ops = {
2019 SET_SYSTEM_SLEEP_PM_OPS(stm32_spi_suspend, stm32_spi_resume)
2020 SET_RUNTIME_PM_OPS(stm32_spi_runtime_suspend,
2021 stm32_spi_runtime_resume, NULL)
2024 static struct platform_driver stm32_spi_driver = {
2025 .probe = stm32_spi_probe,
2026 .remove = stm32_spi_remove,
2028 .name = DRIVER_NAME,
2029 .pm = &stm32_spi_pm_ops,
2030 .of_match_table = stm32_spi_of_match,
2034 module_platform_driver(stm32_spi_driver);
2036 MODULE_ALIAS("platform:" DRIVER_NAME);
2037 MODULE_DESCRIPTION("STMicroelectronics STM32 SPI Controller driver");
2039 MODULE_LICENSE("GPL v2");