0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/bitfield.h>
0009 #include <linux/debugfs.h>
0010 #include <linux/clk.h>
0011 #include <linux/delay.h>
0012 #include <linux/dmaengine.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/iopoll.h>
0015 #include <linux/module.h>
0016 #include <linux/of_platform.h>
0017 #include <linux/pinctrl/consumer.h>
0018 #include <linux/pm_runtime.h>
0019 #include <linux/reset.h>
0020 #include <linux/spi/spi.h>
0021
0022 #define DRIVER_NAME "spi_stm32"
0023
0024
0025 #define STM32F4_SPI_CR1 0x00
0026 #define STM32F4_SPI_CR2 0x04
0027 #define STM32F4_SPI_SR 0x08
0028 #define STM32F4_SPI_DR 0x0C
0029 #define STM32F4_SPI_I2SCFGR 0x1C
0030
0031
0032 #define STM32F4_SPI_CR1_CPHA BIT(0)
0033 #define STM32F4_SPI_CR1_CPOL BIT(1)
0034 #define STM32F4_SPI_CR1_MSTR BIT(2)
0035 #define STM32F4_SPI_CR1_BR_SHIFT 3
0036 #define STM32F4_SPI_CR1_BR GENMASK(5, 3)
0037 #define STM32F4_SPI_CR1_SPE BIT(6)
0038 #define STM32F4_SPI_CR1_LSBFRST BIT(7)
0039 #define STM32F4_SPI_CR1_SSI BIT(8)
0040 #define STM32F4_SPI_CR1_SSM BIT(9)
0041 #define STM32F4_SPI_CR1_RXONLY BIT(10)
0042 #define STM32F4_SPI_CR1_DFF BIT(11)
0043 #define STM32F4_SPI_CR1_CRCNEXT BIT(12)
0044 #define STM32F4_SPI_CR1_CRCEN BIT(13)
0045 #define STM32F4_SPI_CR1_BIDIOE BIT(14)
0046 #define STM32F4_SPI_CR1_BIDIMODE BIT(15)
0047 #define STM32F4_SPI_CR1_BR_MIN 0
0048 #define STM32F4_SPI_CR1_BR_MAX (GENMASK(5, 3) >> 3)
0049
0050
0051 #define STM32F4_SPI_CR2_RXDMAEN BIT(0)
0052 #define STM32F4_SPI_CR2_TXDMAEN BIT(1)
0053 #define STM32F4_SPI_CR2_SSOE BIT(2)
0054 #define STM32F4_SPI_CR2_FRF BIT(4)
0055 #define STM32F4_SPI_CR2_ERRIE BIT(5)
0056 #define STM32F4_SPI_CR2_RXNEIE BIT(6)
0057 #define STM32F4_SPI_CR2_TXEIE BIT(7)
0058
0059
0060 #define STM32F4_SPI_SR_RXNE BIT(0)
0061 #define STM32F4_SPI_SR_TXE BIT(1)
0062 #define STM32F4_SPI_SR_CHSIDE BIT(2)
0063 #define STM32F4_SPI_SR_UDR BIT(3)
0064 #define STM32F4_SPI_SR_CRCERR BIT(4)
0065 #define STM32F4_SPI_SR_MODF BIT(5)
0066 #define STM32F4_SPI_SR_OVR BIT(6)
0067 #define STM32F4_SPI_SR_BSY BIT(7)
0068 #define STM32F4_SPI_SR_FRE BIT(8)
0069
0070
0071 #define STM32F4_SPI_I2SCFGR_I2SMOD BIT(11)
0072
0073
0074 #define STM32F4_SPI_BR_DIV_MIN (2 << STM32F4_SPI_CR1_BR_MIN)
0075 #define STM32F4_SPI_BR_DIV_MAX (2 << STM32F4_SPI_CR1_BR_MAX)
0076
0077
0078 #define STM32H7_SPI_CR1 0x00
0079 #define STM32H7_SPI_CR2 0x04
0080 #define STM32H7_SPI_CFG1 0x08
0081 #define STM32H7_SPI_CFG2 0x0C
0082 #define STM32H7_SPI_IER 0x10
0083 #define STM32H7_SPI_SR 0x14
0084 #define STM32H7_SPI_IFCR 0x18
0085 #define STM32H7_SPI_TXDR 0x20
0086 #define STM32H7_SPI_RXDR 0x30
0087 #define STM32H7_SPI_I2SCFGR 0x50
0088
0089
0090 #define STM32H7_SPI_CR1_SPE BIT(0)
0091 #define STM32H7_SPI_CR1_MASRX BIT(8)
0092 #define STM32H7_SPI_CR1_CSTART BIT(9)
0093 #define STM32H7_SPI_CR1_CSUSP BIT(10)
0094 #define STM32H7_SPI_CR1_HDDIR BIT(11)
0095 #define STM32H7_SPI_CR1_SSI BIT(12)
0096
0097
0098 #define STM32H7_SPI_CR2_TSIZE GENMASK(15, 0)
0099 #define STM32H7_SPI_TSIZE_MAX GENMASK(15, 0)
0100
0101
0102 #define STM32H7_SPI_CFG1_DSIZE GENMASK(4, 0)
0103 #define STM32H7_SPI_CFG1_FTHLV GENMASK(8, 5)
0104 #define STM32H7_SPI_CFG1_RXDMAEN BIT(14)
0105 #define STM32H7_SPI_CFG1_TXDMAEN BIT(15)
0106 #define STM32H7_SPI_CFG1_MBR GENMASK(30, 28)
0107 #define STM32H7_SPI_CFG1_MBR_SHIFT 28
0108 #define STM32H7_SPI_CFG1_MBR_MIN 0
0109 #define STM32H7_SPI_CFG1_MBR_MAX (GENMASK(30, 28) >> 28)
0110
0111
0112 #define STM32H7_SPI_CFG2_MIDI GENMASK(7, 4)
0113 #define STM32H7_SPI_CFG2_COMM GENMASK(18, 17)
0114 #define STM32H7_SPI_CFG2_SP GENMASK(21, 19)
0115 #define STM32H7_SPI_CFG2_MASTER BIT(22)
0116 #define STM32H7_SPI_CFG2_LSBFRST BIT(23)
0117 #define STM32H7_SPI_CFG2_CPHA BIT(24)
0118 #define STM32H7_SPI_CFG2_CPOL BIT(25)
0119 #define STM32H7_SPI_CFG2_SSM BIT(26)
0120 #define STM32H7_SPI_CFG2_AFCNTR BIT(31)
0121
0122
0123 #define STM32H7_SPI_IER_RXPIE BIT(0)
0124 #define STM32H7_SPI_IER_TXPIE BIT(1)
0125 #define STM32H7_SPI_IER_DXPIE BIT(2)
0126 #define STM32H7_SPI_IER_EOTIE BIT(3)
0127 #define STM32H7_SPI_IER_TXTFIE BIT(4)
0128 #define STM32H7_SPI_IER_OVRIE BIT(6)
0129 #define STM32H7_SPI_IER_MODFIE BIT(9)
0130 #define STM32H7_SPI_IER_ALL GENMASK(10, 0)
0131
0132
0133 #define STM32H7_SPI_SR_RXP BIT(0)
0134 #define STM32H7_SPI_SR_TXP BIT(1)
0135 #define STM32H7_SPI_SR_EOT BIT(3)
0136 #define STM32H7_SPI_SR_OVR BIT(6)
0137 #define STM32H7_SPI_SR_MODF BIT(9)
0138 #define STM32H7_SPI_SR_SUSP BIT(11)
0139 #define STM32H7_SPI_SR_RXPLVL GENMASK(14, 13)
0140 #define STM32H7_SPI_SR_RXWNE BIT(15)
0141
0142
0143 #define STM32H7_SPI_IFCR_ALL GENMASK(11, 3)
0144
0145
0146 #define STM32H7_SPI_I2SCFGR_I2SMOD BIT(0)
0147
0148
0149 #define STM32H7_SPI_MBR_DIV_MIN (2 << STM32H7_SPI_CFG1_MBR_MIN)
0150 #define STM32H7_SPI_MBR_DIV_MAX (2 << STM32H7_SPI_CFG1_MBR_MAX)
0151
0152
0153 #define STM32H7_SPI_FULL_DUPLEX 0
0154 #define STM32H7_SPI_SIMPLEX_TX 1
0155 #define STM32H7_SPI_SIMPLEX_RX 2
0156 #define STM32H7_SPI_HALF_DUPLEX 3
0157
0158
0159 #define SPI_FULL_DUPLEX 0
0160 #define SPI_SIMPLEX_TX 1
0161 #define SPI_SIMPLEX_RX 2
0162 #define SPI_3WIRE_TX 3
0163 #define SPI_3WIRE_RX 4
0164
0165 #define STM32_SPI_AUTOSUSPEND_DELAY 1
0166
0167
0168
0169
0170
0171 #define SPI_DMA_MIN_BYTES 16
0172
0173
0174
0175
0176
0177
0178
0179 struct stm32_spi_reg {
0180 int reg;
0181 int mask;
0182 int shift;
0183 };
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197 struct stm32_spi_regspec {
0198 const struct stm32_spi_reg en;
0199 const struct stm32_spi_reg dma_rx_en;
0200 const struct stm32_spi_reg dma_tx_en;
0201 const struct stm32_spi_reg cpol;
0202 const struct stm32_spi_reg cpha;
0203 const struct stm32_spi_reg lsb_first;
0204 const struct stm32_spi_reg br;
0205 const struct stm32_spi_reg rx;
0206 const struct stm32_spi_reg tx;
0207 };
0208
0209 struct stm32_spi;
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236 struct stm32_spi_cfg {
0237 const struct stm32_spi_regspec *regs;
0238 int (*get_fifo_size)(struct stm32_spi *spi);
0239 int (*get_bpw_mask)(struct stm32_spi *spi);
0240 void (*disable)(struct stm32_spi *spi);
0241 int (*config)(struct stm32_spi *spi);
0242 void (*set_bpw)(struct stm32_spi *spi);
0243 int (*set_mode)(struct stm32_spi *spi, unsigned int comm_type);
0244 void (*set_data_idleness)(struct stm32_spi *spi, u32 length);
0245 int (*set_number_of_data)(struct stm32_spi *spi, u32 length);
0246 void (*transfer_one_dma_start)(struct stm32_spi *spi);
0247 void (*dma_rx_cb)(void *data);
0248 void (*dma_tx_cb)(void *data);
0249 int (*transfer_one_irq)(struct stm32_spi *spi);
0250 irqreturn_t (*irq_handler_event)(int irq, void *dev_id);
0251 irqreturn_t (*irq_handler_thread)(int irq, void *dev_id);
0252 unsigned int baud_rate_div_min;
0253 unsigned int baud_rate_div_max;
0254 bool has_fifo;
0255 u16 flags;
0256 };
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284 struct stm32_spi {
0285 struct device *dev;
0286 struct spi_master *master;
0287 const struct stm32_spi_cfg *cfg;
0288 void __iomem *base;
0289 struct clk *clk;
0290 u32 clk_rate;
0291 spinlock_t lock;
0292 int irq;
0293 unsigned int fifo_size;
0294
0295 unsigned int cur_midi;
0296 unsigned int cur_speed;
0297 unsigned int cur_bpw;
0298 unsigned int cur_fthlv;
0299 unsigned int cur_comm;
0300 unsigned int cur_xferlen;
0301 bool cur_usedma;
0302
0303 const void *tx_buf;
0304 void *rx_buf;
0305 int tx_len;
0306 int rx_len;
0307 struct dma_chan *dma_tx;
0308 struct dma_chan *dma_rx;
0309 dma_addr_t phys_addr;
0310 };
0311
0312 static const struct stm32_spi_regspec stm32f4_spi_regspec = {
0313 .en = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_SPE },
0314
0315 .dma_rx_en = { STM32F4_SPI_CR2, STM32F4_SPI_CR2_RXDMAEN },
0316 .dma_tx_en = { STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXDMAEN },
0317
0318 .cpol = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPOL },
0319 .cpha = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPHA },
0320 .lsb_first = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_LSBFRST },
0321 .br = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_BR, STM32F4_SPI_CR1_BR_SHIFT },
0322
0323 .rx = { STM32F4_SPI_DR },
0324 .tx = { STM32F4_SPI_DR },
0325 };
0326
0327 static const struct stm32_spi_regspec stm32h7_spi_regspec = {
0328
0329
0330
0331 .en = { STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE },
0332
0333 .dma_rx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_RXDMAEN },
0334 .dma_tx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN },
0335
0336 .cpol = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPOL },
0337 .cpha = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPHA },
0338 .lsb_first = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_LSBFRST },
0339 .br = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_MBR,
0340 STM32H7_SPI_CFG1_MBR_SHIFT },
0341
0342 .rx = { STM32H7_SPI_RXDR },
0343 .tx = { STM32H7_SPI_TXDR },
0344 };
0345
0346 static inline void stm32_spi_set_bits(struct stm32_spi *spi,
0347 u32 offset, u32 bits)
0348 {
0349 writel_relaxed(readl_relaxed(spi->base + offset) | bits,
0350 spi->base + offset);
0351 }
0352
0353 static inline void stm32_spi_clr_bits(struct stm32_spi *spi,
0354 u32 offset, u32 bits)
0355 {
0356 writel_relaxed(readl_relaxed(spi->base + offset) & ~bits,
0357 spi->base + offset);
0358 }
0359
0360
0361
0362
0363
0364 static int stm32h7_spi_get_fifo_size(struct stm32_spi *spi)
0365 {
0366 unsigned long flags;
0367 u32 count = 0;
0368
0369 spin_lock_irqsave(&spi->lock, flags);
0370
0371 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
0372
0373 while (readl_relaxed(spi->base + STM32H7_SPI_SR) & STM32H7_SPI_SR_TXP)
0374 writeb_relaxed(++count, spi->base + STM32H7_SPI_TXDR);
0375
0376 stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
0377
0378 spin_unlock_irqrestore(&spi->lock, flags);
0379
0380 dev_dbg(spi->dev, "%d x 8-bit fifo size\n", count);
0381
0382 return count;
0383 }
0384
0385
0386
0387
0388
0389 static int stm32f4_spi_get_bpw_mask(struct stm32_spi *spi)
0390 {
0391 dev_dbg(spi->dev, "8-bit or 16-bit data frame supported\n");
0392 return SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
0393 }
0394
0395
0396
0397
0398
0399 static int stm32h7_spi_get_bpw_mask(struct stm32_spi *spi)
0400 {
0401 unsigned long flags;
0402 u32 cfg1, max_bpw;
0403
0404 spin_lock_irqsave(&spi->lock, flags);
0405
0406
0407
0408
0409
0410 stm32_spi_set_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_DSIZE);
0411
0412 cfg1 = readl_relaxed(spi->base + STM32H7_SPI_CFG1);
0413 max_bpw = FIELD_GET(STM32H7_SPI_CFG1_DSIZE, cfg1) + 1;
0414
0415 spin_unlock_irqrestore(&spi->lock, flags);
0416
0417 dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw);
0418
0419 return SPI_BPW_RANGE_MASK(4, max_bpw);
0420 }
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431 static int stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz,
0432 u32 min_div, u32 max_div)
0433 {
0434 u32 div, mbrdiv;
0435
0436
0437 div = DIV_ROUND_UP(spi->clk_rate & ~0x1, speed_hz);
0438
0439
0440
0441
0442
0443
0444
0445
0446 if ((div < min_div) || (div > max_div))
0447 return -EINVAL;
0448
0449
0450 if (div & (div - 1))
0451 mbrdiv = fls(div);
0452 else
0453 mbrdiv = fls(div) - 1;
0454
0455 spi->cur_speed = spi->clk_rate / (1 << mbrdiv);
0456
0457 return mbrdiv - 1;
0458 }
0459
0460
0461
0462
0463
0464
0465 static u32 stm32h7_spi_prepare_fthlv(struct stm32_spi *spi, u32 xfer_len)
0466 {
0467 u32 packet, bpw;
0468
0469
0470 packet = clamp(xfer_len, 1U, spi->fifo_size / 2);
0471
0472
0473 bpw = DIV_ROUND_UP(spi->cur_bpw, 8);
0474 return DIV_ROUND_UP(packet, bpw);
0475 }
0476
0477
0478
0479
0480
0481
0482
0483
0484 static void stm32f4_spi_write_tx(struct stm32_spi *spi)
0485 {
0486 if ((spi->tx_len > 0) && (readl_relaxed(spi->base + STM32F4_SPI_SR) &
0487 STM32F4_SPI_SR_TXE)) {
0488 u32 offs = spi->cur_xferlen - spi->tx_len;
0489
0490 if (spi->cur_bpw == 16) {
0491 const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
0492
0493 writew_relaxed(*tx_buf16, spi->base + STM32F4_SPI_DR);
0494 spi->tx_len -= sizeof(u16);
0495 } else {
0496 const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
0497
0498 writeb_relaxed(*tx_buf8, spi->base + STM32F4_SPI_DR);
0499 spi->tx_len -= sizeof(u8);
0500 }
0501 }
0502
0503 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
0504 }
0505
0506
0507
0508
0509
0510
0511
0512
0513 static void stm32h7_spi_write_txfifo(struct stm32_spi *spi)
0514 {
0515 while ((spi->tx_len > 0) &&
0516 (readl_relaxed(spi->base + STM32H7_SPI_SR) &
0517 STM32H7_SPI_SR_TXP)) {
0518 u32 offs = spi->cur_xferlen - spi->tx_len;
0519
0520 if (spi->tx_len >= sizeof(u32)) {
0521 const u32 *tx_buf32 = (const u32 *)(spi->tx_buf + offs);
0522
0523 writel_relaxed(*tx_buf32, spi->base + STM32H7_SPI_TXDR);
0524 spi->tx_len -= sizeof(u32);
0525 } else if (spi->tx_len >= sizeof(u16)) {
0526 const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
0527
0528 writew_relaxed(*tx_buf16, spi->base + STM32H7_SPI_TXDR);
0529 spi->tx_len -= sizeof(u16);
0530 } else {
0531 const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
0532
0533 writeb_relaxed(*tx_buf8, spi->base + STM32H7_SPI_TXDR);
0534 spi->tx_len -= sizeof(u8);
0535 }
0536 }
0537
0538 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
0539 }
0540
0541
0542
0543
0544
0545
0546
0547
0548 static void stm32f4_spi_read_rx(struct stm32_spi *spi)
0549 {
0550 if ((spi->rx_len > 0) && (readl_relaxed(spi->base + STM32F4_SPI_SR) &
0551 STM32F4_SPI_SR_RXNE)) {
0552 u32 offs = spi->cur_xferlen - spi->rx_len;
0553
0554 if (spi->cur_bpw == 16) {
0555 u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
0556
0557 *rx_buf16 = readw_relaxed(spi->base + STM32F4_SPI_DR);
0558 spi->rx_len -= sizeof(u16);
0559 } else {
0560 u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
0561
0562 *rx_buf8 = readb_relaxed(spi->base + STM32F4_SPI_DR);
0563 spi->rx_len -= sizeof(u8);
0564 }
0565 }
0566
0567 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->rx_len);
0568 }
0569
0570
0571
0572
0573
0574
0575
0576
0577 static void stm32h7_spi_read_rxfifo(struct stm32_spi *spi)
0578 {
0579 u32 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
0580 u32 rxplvl = FIELD_GET(STM32H7_SPI_SR_RXPLVL, sr);
0581
0582 while ((spi->rx_len > 0) &&
0583 ((sr & STM32H7_SPI_SR_RXP) ||
0584 ((sr & STM32H7_SPI_SR_EOT) &&
0585 ((sr & STM32H7_SPI_SR_RXWNE) || (rxplvl > 0))))) {
0586 u32 offs = spi->cur_xferlen - spi->rx_len;
0587
0588 if ((spi->rx_len >= sizeof(u32)) ||
0589 (sr & STM32H7_SPI_SR_RXWNE)) {
0590 u32 *rx_buf32 = (u32 *)(spi->rx_buf + offs);
0591
0592 *rx_buf32 = readl_relaxed(spi->base + STM32H7_SPI_RXDR);
0593 spi->rx_len -= sizeof(u32);
0594 } else if ((spi->rx_len >= sizeof(u16)) ||
0595 (!(sr & STM32H7_SPI_SR_RXWNE) &&
0596 (rxplvl >= 2 || spi->cur_bpw > 8))) {
0597 u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
0598
0599 *rx_buf16 = readw_relaxed(spi->base + STM32H7_SPI_RXDR);
0600 spi->rx_len -= sizeof(u16);
0601 } else {
0602 u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
0603
0604 *rx_buf8 = readb_relaxed(spi->base + STM32H7_SPI_RXDR);
0605 spi->rx_len -= sizeof(u8);
0606 }
0607
0608 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
0609 rxplvl = FIELD_GET(STM32H7_SPI_SR_RXPLVL, sr);
0610 }
0611
0612 dev_dbg(spi->dev, "%s: %d bytes left (sr=%08x)\n",
0613 __func__, spi->rx_len, sr);
0614 }
0615
0616
0617
0618
0619
0620 static void stm32_spi_enable(struct stm32_spi *spi)
0621 {
0622 dev_dbg(spi->dev, "enable controller\n");
0623
0624 stm32_spi_set_bits(spi, spi->cfg->regs->en.reg,
0625 spi->cfg->regs->en.mask);
0626 }
0627
0628
0629
0630
0631
0632 static void stm32f4_spi_disable(struct stm32_spi *spi)
0633 {
0634 unsigned long flags;
0635 u32 sr;
0636
0637 dev_dbg(spi->dev, "disable controller\n");
0638
0639 spin_lock_irqsave(&spi->lock, flags);
0640
0641 if (!(readl_relaxed(spi->base + STM32F4_SPI_CR1) &
0642 STM32F4_SPI_CR1_SPE)) {
0643 spin_unlock_irqrestore(&spi->lock, flags);
0644 return;
0645 }
0646
0647
0648 stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXEIE |
0649 STM32F4_SPI_CR2_RXNEIE |
0650 STM32F4_SPI_CR2_ERRIE);
0651
0652
0653 if (readl_relaxed_poll_timeout_atomic(spi->base + STM32F4_SPI_SR,
0654 sr, !(sr & STM32F4_SPI_SR_BSY),
0655 10, 100000) < 0) {
0656 dev_warn(spi->dev, "disabling condition timeout\n");
0657 }
0658
0659 if (spi->cur_usedma && spi->dma_tx)
0660 dmaengine_terminate_all(spi->dma_tx);
0661 if (spi->cur_usedma && spi->dma_rx)
0662 dmaengine_terminate_all(spi->dma_rx);
0663
0664 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_SPE);
0665
0666 stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXDMAEN |
0667 STM32F4_SPI_CR2_RXDMAEN);
0668
0669
0670 readl_relaxed(spi->base + STM32F4_SPI_DR);
0671 readl_relaxed(spi->base + STM32F4_SPI_SR);
0672
0673 spin_unlock_irqrestore(&spi->lock, flags);
0674 }
0675
0676
0677
0678
0679
0680
0681
0682 static void stm32h7_spi_disable(struct stm32_spi *spi)
0683 {
0684 unsigned long flags;
0685 u32 cr1;
0686
0687 dev_dbg(spi->dev, "disable controller\n");
0688
0689 spin_lock_irqsave(&spi->lock, flags);
0690
0691 cr1 = readl_relaxed(spi->base + STM32H7_SPI_CR1);
0692
0693 if (!(cr1 & STM32H7_SPI_CR1_SPE)) {
0694 spin_unlock_irqrestore(&spi->lock, flags);
0695 return;
0696 }
0697
0698 if (spi->cur_usedma && spi->dma_tx)
0699 dmaengine_terminate_all(spi->dma_tx);
0700 if (spi->cur_usedma && spi->dma_rx)
0701 dmaengine_terminate_all(spi->dma_rx);
0702
0703 stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
0704
0705 stm32_spi_clr_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN |
0706 STM32H7_SPI_CFG1_RXDMAEN);
0707
0708
0709 writel_relaxed(0, spi->base + STM32H7_SPI_IER);
0710 writel_relaxed(STM32H7_SPI_IFCR_ALL, spi->base + STM32H7_SPI_IFCR);
0711
0712 spin_unlock_irqrestore(&spi->lock, flags);
0713 }
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724 static bool stm32_spi_can_dma(struct spi_master *master,
0725 struct spi_device *spi_dev,
0726 struct spi_transfer *transfer)
0727 {
0728 unsigned int dma_size;
0729 struct stm32_spi *spi = spi_master_get_devdata(master);
0730
0731 if (spi->cfg->has_fifo)
0732 dma_size = spi->fifo_size;
0733 else
0734 dma_size = SPI_DMA_MIN_BYTES;
0735
0736 dev_dbg(spi->dev, "%s: %s\n", __func__,
0737 (transfer->len > dma_size) ? "true" : "false");
0738
0739 return (transfer->len > dma_size);
0740 }
0741
0742
0743
0744
0745
0746
0747 static irqreturn_t stm32f4_spi_irq_event(int irq, void *dev_id)
0748 {
0749 struct spi_master *master = dev_id;
0750 struct stm32_spi *spi = spi_master_get_devdata(master);
0751 u32 sr, mask = 0;
0752 bool end = false;
0753
0754 spin_lock(&spi->lock);
0755
0756 sr = readl_relaxed(spi->base + STM32F4_SPI_SR);
0757
0758
0759
0760
0761 sr &= ~STM32F4_SPI_SR_BSY;
0762
0763 if (!spi->cur_usedma && (spi->cur_comm == SPI_SIMPLEX_TX ||
0764 spi->cur_comm == SPI_3WIRE_TX)) {
0765
0766 sr &= ~(STM32F4_SPI_SR_OVR | STM32F4_SPI_SR_RXNE);
0767 mask |= STM32F4_SPI_SR_TXE;
0768 }
0769
0770 if (!spi->cur_usedma && (spi->cur_comm == SPI_FULL_DUPLEX ||
0771 spi->cur_comm == SPI_SIMPLEX_RX ||
0772 spi->cur_comm == SPI_3WIRE_RX)) {
0773
0774 sr &= ~STM32F4_SPI_SR_TXE;
0775 mask |= STM32F4_SPI_SR_RXNE | STM32F4_SPI_SR_OVR;
0776 }
0777
0778 if (!(sr & mask)) {
0779 dev_dbg(spi->dev, "spurious IT (sr=0x%08x)\n", sr);
0780 spin_unlock(&spi->lock);
0781 return IRQ_NONE;
0782 }
0783
0784 if (sr & STM32F4_SPI_SR_OVR) {
0785 dev_warn(spi->dev, "Overrun: received value discarded\n");
0786
0787
0788 readl_relaxed(spi->base + STM32F4_SPI_DR);
0789 readl_relaxed(spi->base + STM32F4_SPI_SR);
0790
0791
0792
0793
0794
0795
0796 end = true;
0797 goto end_irq;
0798 }
0799
0800 if (sr & STM32F4_SPI_SR_TXE) {
0801 if (spi->tx_buf)
0802 stm32f4_spi_write_tx(spi);
0803 if (spi->tx_len == 0)
0804 end = true;
0805 }
0806
0807 if (sr & STM32F4_SPI_SR_RXNE) {
0808 stm32f4_spi_read_rx(spi);
0809 if (spi->rx_len == 0)
0810 end = true;
0811 else if (spi->tx_buf)
0812 stm32f4_spi_write_tx(spi);
0813 }
0814
0815 end_irq:
0816 if (end) {
0817
0818 stm32_spi_clr_bits(spi, STM32F4_SPI_CR2,
0819 STM32F4_SPI_CR2_TXEIE |
0820 STM32F4_SPI_CR2_RXNEIE |
0821 STM32F4_SPI_CR2_ERRIE);
0822 spin_unlock(&spi->lock);
0823 return IRQ_WAKE_THREAD;
0824 }
0825
0826 spin_unlock(&spi->lock);
0827 return IRQ_HANDLED;
0828 }
0829
0830
0831
0832
0833
0834
0835 static irqreturn_t stm32f4_spi_irq_thread(int irq, void *dev_id)
0836 {
0837 struct spi_master *master = dev_id;
0838 struct stm32_spi *spi = spi_master_get_devdata(master);
0839
0840 spi_finalize_current_transfer(master);
0841 stm32f4_spi_disable(spi);
0842
0843 return IRQ_HANDLED;
0844 }
0845
0846
0847
0848
0849
0850
0851 static irqreturn_t stm32h7_spi_irq_thread(int irq, void *dev_id)
0852 {
0853 struct spi_master *master = dev_id;
0854 struct stm32_spi *spi = spi_master_get_devdata(master);
0855 u32 sr, ier, mask;
0856 unsigned long flags;
0857 bool end = false;
0858
0859 spin_lock_irqsave(&spi->lock, flags);
0860
0861 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
0862 ier = readl_relaxed(spi->base + STM32H7_SPI_IER);
0863
0864 mask = ier;
0865
0866
0867
0868
0869
0870 mask |= STM32H7_SPI_SR_SUSP;
0871
0872
0873
0874
0875 if ((spi->cur_comm == SPI_FULL_DUPLEX) && !spi->cur_usedma)
0876 mask |= STM32H7_SPI_SR_TXP | STM32H7_SPI_SR_RXP;
0877
0878 if (!(sr & mask)) {
0879 dev_warn(spi->dev, "spurious IT (sr=0x%08x, ier=0x%08x)\n",
0880 sr, ier);
0881 spin_unlock_irqrestore(&spi->lock, flags);
0882 return IRQ_NONE;
0883 }
0884
0885 if (sr & STM32H7_SPI_SR_SUSP) {
0886 static DEFINE_RATELIMIT_STATE(rs,
0887 DEFAULT_RATELIMIT_INTERVAL * 10,
0888 1);
0889 if (__ratelimit(&rs))
0890 dev_dbg_ratelimited(spi->dev, "Communication suspended\n");
0891 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
0892 stm32h7_spi_read_rxfifo(spi);
0893
0894
0895
0896
0897 if (spi->cur_usedma)
0898 end = true;
0899 }
0900
0901 if (sr & STM32H7_SPI_SR_MODF) {
0902 dev_warn(spi->dev, "Mode fault: transfer aborted\n");
0903 end = true;
0904 }
0905
0906 if (sr & STM32H7_SPI_SR_OVR) {
0907 dev_err(spi->dev, "Overrun: RX data lost\n");
0908 end = true;
0909 }
0910
0911 if (sr & STM32H7_SPI_SR_EOT) {
0912 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
0913 stm32h7_spi_read_rxfifo(spi);
0914 if (!spi->cur_usedma ||
0915 (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX))
0916 end = true;
0917 }
0918
0919 if (sr & STM32H7_SPI_SR_TXP)
0920 if (!spi->cur_usedma && (spi->tx_buf && (spi->tx_len > 0)))
0921 stm32h7_spi_write_txfifo(spi);
0922
0923 if (sr & STM32H7_SPI_SR_RXP)
0924 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
0925 stm32h7_spi_read_rxfifo(spi);
0926
0927 writel_relaxed(sr & mask, spi->base + STM32H7_SPI_IFCR);
0928
0929 spin_unlock_irqrestore(&spi->lock, flags);
0930
0931 if (end) {
0932 stm32h7_spi_disable(spi);
0933 spi_finalize_current_transfer(master);
0934 }
0935
0936 return IRQ_HANDLED;
0937 }
0938
0939
0940
0941
0942
0943
0944 static int stm32_spi_prepare_msg(struct spi_master *master,
0945 struct spi_message *msg)
0946 {
0947 struct stm32_spi *spi = spi_master_get_devdata(master);
0948 struct spi_device *spi_dev = msg->spi;
0949 struct device_node *np = spi_dev->dev.of_node;
0950 unsigned long flags;
0951 u32 clrb = 0, setb = 0;
0952
0953
0954 spi->cur_midi = 0;
0955 if (np && !of_property_read_u32(np, "st,spi-midi-ns", &spi->cur_midi))
0956 dev_dbg(spi->dev, "%dns inter-data idleness\n", spi->cur_midi);
0957
0958 if (spi_dev->mode & SPI_CPOL)
0959 setb |= spi->cfg->regs->cpol.mask;
0960 else
0961 clrb |= spi->cfg->regs->cpol.mask;
0962
0963 if (spi_dev->mode & SPI_CPHA)
0964 setb |= spi->cfg->regs->cpha.mask;
0965 else
0966 clrb |= spi->cfg->regs->cpha.mask;
0967
0968 if (spi_dev->mode & SPI_LSB_FIRST)
0969 setb |= spi->cfg->regs->lsb_first.mask;
0970 else
0971 clrb |= spi->cfg->regs->lsb_first.mask;
0972
0973 dev_dbg(spi->dev, "cpol=%d cpha=%d lsb_first=%d cs_high=%d\n",
0974 !!(spi_dev->mode & SPI_CPOL),
0975 !!(spi_dev->mode & SPI_CPHA),
0976 !!(spi_dev->mode & SPI_LSB_FIRST),
0977 !!(spi_dev->mode & SPI_CS_HIGH));
0978
0979
0980
0981
0982
0983 if (spi->cfg->set_number_of_data) {
0984 int ret;
0985
0986 ret = spi_split_transfers_maxsize(master, msg,
0987 STM32H7_SPI_TSIZE_MAX,
0988 GFP_KERNEL | GFP_DMA);
0989 if (ret)
0990 return ret;
0991 }
0992
0993 spin_lock_irqsave(&spi->lock, flags);
0994
0995
0996 if (clrb || setb)
0997 writel_relaxed(
0998 (readl_relaxed(spi->base + spi->cfg->regs->cpol.reg) &
0999 ~clrb) | setb,
1000 spi->base + spi->cfg->regs->cpol.reg);
1001
1002 spin_unlock_irqrestore(&spi->lock, flags);
1003
1004 return 0;
1005 }
1006
1007
1008
1009
1010
1011
1012
1013 static void stm32f4_spi_dma_tx_cb(void *data)
1014 {
1015 struct stm32_spi *spi = data;
1016
1017 if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
1018 spi_finalize_current_transfer(spi->master);
1019 stm32f4_spi_disable(spi);
1020 }
1021 }
1022
1023
1024
1025
1026
1027
1028
1029 static void stm32_spi_dma_rx_cb(void *data)
1030 {
1031 struct stm32_spi *spi = data;
1032
1033 spi_finalize_current_transfer(spi->master);
1034 spi->cfg->disable(spi);
1035 }
1036
1037
1038
1039
1040
1041
1042
1043
1044 static void stm32_spi_dma_config(struct stm32_spi *spi,
1045 struct dma_slave_config *dma_conf,
1046 enum dma_transfer_direction dir)
1047 {
1048 enum dma_slave_buswidth buswidth;
1049 u32 maxburst;
1050
1051 if (spi->cur_bpw <= 8)
1052 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
1053 else if (spi->cur_bpw <= 16)
1054 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1055 else
1056 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
1057
1058 if (spi->cfg->has_fifo) {
1059
1060 if (spi->cur_fthlv == 2)
1061 maxburst = 1;
1062 else
1063 maxburst = spi->cur_fthlv;
1064 } else {
1065 maxburst = 1;
1066 }
1067
1068 memset(dma_conf, 0, sizeof(struct dma_slave_config));
1069 dma_conf->direction = dir;
1070 if (dma_conf->direction == DMA_DEV_TO_MEM) {
1071 dma_conf->src_addr = spi->phys_addr + spi->cfg->regs->rx.reg;
1072 dma_conf->src_addr_width = buswidth;
1073 dma_conf->src_maxburst = maxburst;
1074
1075 dev_dbg(spi->dev, "Rx DMA config buswidth=%d, maxburst=%d\n",
1076 buswidth, maxburst);
1077 } else if (dma_conf->direction == DMA_MEM_TO_DEV) {
1078 dma_conf->dst_addr = spi->phys_addr + spi->cfg->regs->tx.reg;
1079 dma_conf->dst_addr_width = buswidth;
1080 dma_conf->dst_maxburst = maxburst;
1081
1082 dev_dbg(spi->dev, "Tx DMA config buswidth=%d, maxburst=%d\n",
1083 buswidth, maxburst);
1084 }
1085 }
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095 static int stm32f4_spi_transfer_one_irq(struct stm32_spi *spi)
1096 {
1097 unsigned long flags;
1098 u32 cr2 = 0;
1099
1100
1101 if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
1102 cr2 |= STM32F4_SPI_CR2_TXEIE;
1103 } else if (spi->cur_comm == SPI_FULL_DUPLEX ||
1104 spi->cur_comm == SPI_SIMPLEX_RX ||
1105 spi->cur_comm == SPI_3WIRE_RX) {
1106
1107
1108
1109
1110 cr2 |= STM32F4_SPI_CR2_RXNEIE | STM32F4_SPI_CR2_ERRIE;
1111 } else {
1112 return -EINVAL;
1113 }
1114
1115 spin_lock_irqsave(&spi->lock, flags);
1116
1117 stm32_spi_set_bits(spi, STM32F4_SPI_CR2, cr2);
1118
1119 stm32_spi_enable(spi);
1120
1121
1122 if (spi->tx_buf)
1123 stm32f4_spi_write_tx(spi);
1124
1125 spin_unlock_irqrestore(&spi->lock, flags);
1126
1127 return 1;
1128 }
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138 static int stm32h7_spi_transfer_one_irq(struct stm32_spi *spi)
1139 {
1140 unsigned long flags;
1141 u32 ier = 0;
1142
1143
1144 if (spi->tx_buf && spi->rx_buf)
1145 ier |= STM32H7_SPI_IER_DXPIE;
1146 else if (spi->tx_buf)
1147 ier |= STM32H7_SPI_IER_TXPIE;
1148 else if (spi->rx_buf)
1149 ier |= STM32H7_SPI_IER_RXPIE;
1150
1151
1152 ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE |
1153 STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
1154
1155 spin_lock_irqsave(&spi->lock, flags);
1156
1157 stm32_spi_enable(spi);
1158
1159
1160 if (spi->tx_buf)
1161 stm32h7_spi_write_txfifo(spi);
1162
1163 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
1164
1165 writel_relaxed(ier, spi->base + STM32H7_SPI_IER);
1166
1167 spin_unlock_irqrestore(&spi->lock, flags);
1168
1169 return 1;
1170 }
1171
1172
1173
1174
1175
1176
1177 static void stm32f4_spi_transfer_one_dma_start(struct stm32_spi *spi)
1178 {
1179
1180 if (spi->cur_comm == SPI_SIMPLEX_RX || spi->cur_comm == SPI_3WIRE_RX ||
1181 spi->cur_comm == SPI_FULL_DUPLEX) {
1182
1183
1184
1185
1186
1187 stm32_spi_set_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_ERRIE);
1188 }
1189
1190 stm32_spi_enable(spi);
1191 }
1192
1193
1194
1195
1196
1197
1198 static void stm32h7_spi_transfer_one_dma_start(struct stm32_spi *spi)
1199 {
1200 uint32_t ier = STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
1201
1202
1203 if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX)
1204 ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE;
1205
1206 stm32_spi_set_bits(spi, STM32H7_SPI_IER, ier);
1207
1208 stm32_spi_enable(spi);
1209
1210 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
1211 }
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221 static int stm32_spi_transfer_one_dma(struct stm32_spi *spi,
1222 struct spi_transfer *xfer)
1223 {
1224 struct dma_slave_config tx_dma_conf, rx_dma_conf;
1225 struct dma_async_tx_descriptor *tx_dma_desc, *rx_dma_desc;
1226 unsigned long flags;
1227
1228 spin_lock_irqsave(&spi->lock, flags);
1229
1230 rx_dma_desc = NULL;
1231 if (spi->rx_buf && spi->dma_rx) {
1232 stm32_spi_dma_config(spi, &rx_dma_conf, DMA_DEV_TO_MEM);
1233 dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
1234
1235
1236 stm32_spi_set_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1237 spi->cfg->regs->dma_rx_en.mask);
1238
1239 rx_dma_desc = dmaengine_prep_slave_sg(
1240 spi->dma_rx, xfer->rx_sg.sgl,
1241 xfer->rx_sg.nents,
1242 rx_dma_conf.direction,
1243 DMA_PREP_INTERRUPT);
1244 }
1245
1246 tx_dma_desc = NULL;
1247 if (spi->tx_buf && spi->dma_tx) {
1248 stm32_spi_dma_config(spi, &tx_dma_conf, DMA_MEM_TO_DEV);
1249 dmaengine_slave_config(spi->dma_tx, &tx_dma_conf);
1250
1251 tx_dma_desc = dmaengine_prep_slave_sg(
1252 spi->dma_tx, xfer->tx_sg.sgl,
1253 xfer->tx_sg.nents,
1254 tx_dma_conf.direction,
1255 DMA_PREP_INTERRUPT);
1256 }
1257
1258 if ((spi->tx_buf && spi->dma_tx && !tx_dma_desc) ||
1259 (spi->rx_buf && spi->dma_rx && !rx_dma_desc))
1260 goto dma_desc_error;
1261
1262 if (spi->cur_comm == SPI_FULL_DUPLEX && (!tx_dma_desc || !rx_dma_desc))
1263 goto dma_desc_error;
1264
1265 if (rx_dma_desc) {
1266 rx_dma_desc->callback = spi->cfg->dma_rx_cb;
1267 rx_dma_desc->callback_param = spi;
1268
1269 if (dma_submit_error(dmaengine_submit(rx_dma_desc))) {
1270 dev_err(spi->dev, "Rx DMA submit failed\n");
1271 goto dma_desc_error;
1272 }
1273
1274 dma_async_issue_pending(spi->dma_rx);
1275 }
1276
1277 if (tx_dma_desc) {
1278 if (spi->cur_comm == SPI_SIMPLEX_TX ||
1279 spi->cur_comm == SPI_3WIRE_TX) {
1280 tx_dma_desc->callback = spi->cfg->dma_tx_cb;
1281 tx_dma_desc->callback_param = spi;
1282 }
1283
1284 if (dma_submit_error(dmaengine_submit(tx_dma_desc))) {
1285 dev_err(spi->dev, "Tx DMA submit failed\n");
1286 goto dma_submit_error;
1287 }
1288
1289 dma_async_issue_pending(spi->dma_tx);
1290
1291
1292 stm32_spi_set_bits(spi, spi->cfg->regs->dma_tx_en.reg,
1293 spi->cfg->regs->dma_tx_en.mask);
1294 }
1295
1296 spi->cfg->transfer_one_dma_start(spi);
1297
1298 spin_unlock_irqrestore(&spi->lock, flags);
1299
1300 return 1;
1301
1302 dma_submit_error:
1303 if (spi->dma_rx)
1304 dmaengine_terminate_all(spi->dma_rx);
1305
1306 dma_desc_error:
1307 stm32_spi_clr_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1308 spi->cfg->regs->dma_rx_en.mask);
1309
1310 spin_unlock_irqrestore(&spi->lock, flags);
1311
1312 dev_info(spi->dev, "DMA issue: fall back to irq transfer\n");
1313
1314 spi->cur_usedma = false;
1315 return spi->cfg->transfer_one_irq(spi);
1316 }
1317
1318
1319
1320
1321
1322 static void stm32f4_spi_set_bpw(struct stm32_spi *spi)
1323 {
1324 if (spi->cur_bpw == 16)
1325 stm32_spi_set_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_DFF);
1326 else
1327 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_DFF);
1328 }
1329
1330
1331
1332
1333
1334 static void stm32h7_spi_set_bpw(struct stm32_spi *spi)
1335 {
1336 u32 bpw, fthlv;
1337 u32 cfg1_clrb = 0, cfg1_setb = 0;
1338
1339 bpw = spi->cur_bpw - 1;
1340
1341 cfg1_clrb |= STM32H7_SPI_CFG1_DSIZE;
1342 cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_DSIZE, bpw);
1343
1344 spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi, spi->cur_xferlen);
1345 fthlv = spi->cur_fthlv - 1;
1346
1347 cfg1_clrb |= STM32H7_SPI_CFG1_FTHLV;
1348 cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_FTHLV, fthlv);
1349
1350 writel_relaxed(
1351 (readl_relaxed(spi->base + STM32H7_SPI_CFG1) &
1352 ~cfg1_clrb) | cfg1_setb,
1353 spi->base + STM32H7_SPI_CFG1);
1354 }
1355
1356
1357
1358
1359
1360
1361 static void stm32_spi_set_mbr(struct stm32_spi *spi, u32 mbrdiv)
1362 {
1363 u32 clrb = 0, setb = 0;
1364
1365 clrb |= spi->cfg->regs->br.mask;
1366 setb |= (mbrdiv << spi->cfg->regs->br.shift) & spi->cfg->regs->br.mask;
1367
1368 writel_relaxed((readl_relaxed(spi->base + spi->cfg->regs->br.reg) &
1369 ~clrb) | setb,
1370 spi->base + spi->cfg->regs->br.reg);
1371 }
1372
1373
1374
1375
1376
1377
1378 static unsigned int stm32_spi_communication_type(struct spi_device *spi_dev,
1379 struct spi_transfer *transfer)
1380 {
1381 unsigned int type = SPI_FULL_DUPLEX;
1382
1383 if (spi_dev->mode & SPI_3WIRE) {
1384
1385
1386
1387
1388
1389
1390 if (!transfer->tx_buf)
1391 type = SPI_3WIRE_RX;
1392 else
1393 type = SPI_3WIRE_TX;
1394 } else {
1395 if (!transfer->tx_buf)
1396 type = SPI_SIMPLEX_RX;
1397 else if (!transfer->rx_buf)
1398 type = SPI_SIMPLEX_TX;
1399 }
1400
1401 return type;
1402 }
1403
1404
1405
1406
1407
1408
1409 static int stm32f4_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
1410 {
1411 if (comm_type == SPI_3WIRE_TX || comm_type == SPI_SIMPLEX_TX) {
1412 stm32_spi_set_bits(spi, STM32F4_SPI_CR1,
1413 STM32F4_SPI_CR1_BIDIMODE |
1414 STM32F4_SPI_CR1_BIDIOE);
1415 } else if (comm_type == SPI_FULL_DUPLEX ||
1416 comm_type == SPI_SIMPLEX_RX) {
1417 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1,
1418 STM32F4_SPI_CR1_BIDIMODE |
1419 STM32F4_SPI_CR1_BIDIOE);
1420 } else if (comm_type == SPI_3WIRE_RX) {
1421 stm32_spi_set_bits(spi, STM32F4_SPI_CR1,
1422 STM32F4_SPI_CR1_BIDIMODE);
1423 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1,
1424 STM32F4_SPI_CR1_BIDIOE);
1425 } else {
1426 return -EINVAL;
1427 }
1428
1429 return 0;
1430 }
1431
1432
1433
1434
1435
1436
1437 static int stm32h7_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
1438 {
1439 u32 mode;
1440 u32 cfg2_clrb = 0, cfg2_setb = 0;
1441
1442 if (comm_type == SPI_3WIRE_RX) {
1443 mode = STM32H7_SPI_HALF_DUPLEX;
1444 stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1445 } else if (comm_type == SPI_3WIRE_TX) {
1446 mode = STM32H7_SPI_HALF_DUPLEX;
1447 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1448 } else if (comm_type == SPI_SIMPLEX_RX) {
1449 mode = STM32H7_SPI_SIMPLEX_RX;
1450 } else if (comm_type == SPI_SIMPLEX_TX) {
1451 mode = STM32H7_SPI_SIMPLEX_TX;
1452 } else {
1453 mode = STM32H7_SPI_FULL_DUPLEX;
1454 }
1455
1456 cfg2_clrb |= STM32H7_SPI_CFG2_COMM;
1457 cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_COMM, mode);
1458
1459 writel_relaxed(
1460 (readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1461 ~cfg2_clrb) | cfg2_setb,
1462 spi->base + STM32H7_SPI_CFG2);
1463
1464 return 0;
1465 }
1466
1467
1468
1469
1470
1471
1472
1473 static void stm32h7_spi_data_idleness(struct stm32_spi *spi, u32 len)
1474 {
1475 u32 cfg2_clrb = 0, cfg2_setb = 0;
1476
1477 cfg2_clrb |= STM32H7_SPI_CFG2_MIDI;
1478 if ((len > 1) && (spi->cur_midi > 0)) {
1479 u32 sck_period_ns = DIV_ROUND_UP(NSEC_PER_SEC, spi->cur_speed);
1480 u32 midi = min_t(u32,
1481 DIV_ROUND_UP(spi->cur_midi, sck_period_ns),
1482 FIELD_GET(STM32H7_SPI_CFG2_MIDI,
1483 STM32H7_SPI_CFG2_MIDI));
1484
1485
1486 dev_dbg(spi->dev, "period=%dns, midi=%d(=%dns)\n",
1487 sck_period_ns, midi, midi * sck_period_ns);
1488 cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_MIDI, midi);
1489 }
1490
1491 writel_relaxed((readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1492 ~cfg2_clrb) | cfg2_setb,
1493 spi->base + STM32H7_SPI_CFG2);
1494 }
1495
1496
1497
1498
1499
1500
1501 static int stm32h7_spi_number_of_data(struct stm32_spi *spi, u32 nb_words)
1502 {
1503 if (nb_words <= STM32H7_SPI_TSIZE_MAX) {
1504 writel_relaxed(FIELD_PREP(STM32H7_SPI_CR2_TSIZE, nb_words),
1505 spi->base + STM32H7_SPI_CR2);
1506 } else {
1507 return -EMSGSIZE;
1508 }
1509
1510 return 0;
1511 }
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521 static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
1522 struct spi_device *spi_dev,
1523 struct spi_transfer *transfer)
1524 {
1525 unsigned long flags;
1526 unsigned int comm_type;
1527 int nb_words, ret = 0;
1528 int mbr;
1529
1530 spin_lock_irqsave(&spi->lock, flags);
1531
1532 spi->cur_xferlen = transfer->len;
1533
1534 spi->cur_bpw = transfer->bits_per_word;
1535 spi->cfg->set_bpw(spi);
1536
1537
1538 mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz,
1539 spi->cfg->baud_rate_div_min,
1540 spi->cfg->baud_rate_div_max);
1541 if (mbr < 0) {
1542 ret = mbr;
1543 goto out;
1544 }
1545
1546 transfer->speed_hz = spi->cur_speed;
1547 stm32_spi_set_mbr(spi, mbr);
1548
1549 comm_type = stm32_spi_communication_type(spi_dev, transfer);
1550 ret = spi->cfg->set_mode(spi, comm_type);
1551 if (ret < 0)
1552 goto out;
1553
1554 spi->cur_comm = comm_type;
1555
1556 if (spi->cfg->set_data_idleness)
1557 spi->cfg->set_data_idleness(spi, transfer->len);
1558
1559 if (spi->cur_bpw <= 8)
1560 nb_words = transfer->len;
1561 else if (spi->cur_bpw <= 16)
1562 nb_words = DIV_ROUND_UP(transfer->len * 8, 16);
1563 else
1564 nb_words = DIV_ROUND_UP(transfer->len * 8, 32);
1565
1566 if (spi->cfg->set_number_of_data) {
1567 ret = spi->cfg->set_number_of_data(spi, nb_words);
1568 if (ret < 0)
1569 goto out;
1570 }
1571
1572 dev_dbg(spi->dev, "transfer communication mode set to %d\n",
1573 spi->cur_comm);
1574 dev_dbg(spi->dev,
1575 "data frame of %d-bit, data packet of %d data frames\n",
1576 spi->cur_bpw, spi->cur_fthlv);
1577 dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed);
1578 dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n",
1579 spi->cur_xferlen, nb_words);
1580 dev_dbg(spi->dev, "dma %s\n",
1581 (spi->cur_usedma) ? "enabled" : "disabled");
1582
1583 out:
1584 spin_unlock_irqrestore(&spi->lock, flags);
1585
1586 return ret;
1587 }
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598 static int stm32_spi_transfer_one(struct spi_master *master,
1599 struct spi_device *spi_dev,
1600 struct spi_transfer *transfer)
1601 {
1602 struct stm32_spi *spi = spi_master_get_devdata(master);
1603 int ret;
1604
1605 spi->tx_buf = transfer->tx_buf;
1606 spi->rx_buf = transfer->rx_buf;
1607 spi->tx_len = spi->tx_buf ? transfer->len : 0;
1608 spi->rx_len = spi->rx_buf ? transfer->len : 0;
1609
1610 spi->cur_usedma = (master->can_dma &&
1611 master->can_dma(master, spi_dev, transfer));
1612
1613 ret = stm32_spi_transfer_one_setup(spi, spi_dev, transfer);
1614 if (ret) {
1615 dev_err(spi->dev, "SPI transfer setup failed\n");
1616 return ret;
1617 }
1618
1619 if (spi->cur_usedma)
1620 return stm32_spi_transfer_one_dma(spi, transfer);
1621 else
1622 return spi->cfg->transfer_one_irq(spi);
1623 }
1624
1625
1626
1627
1628
1629
1630 static int stm32_spi_unprepare_msg(struct spi_master *master,
1631 struct spi_message *msg)
1632 {
1633 struct stm32_spi *spi = spi_master_get_devdata(master);
1634
1635 spi->cfg->disable(spi);
1636
1637 return 0;
1638 }
1639
1640
1641
1642
1643
1644 static int stm32f4_spi_config(struct stm32_spi *spi)
1645 {
1646 unsigned long flags;
1647
1648 spin_lock_irqsave(&spi->lock, flags);
1649
1650
1651 stm32_spi_clr_bits(spi, STM32F4_SPI_I2SCFGR,
1652 STM32F4_SPI_I2SCFGR_I2SMOD);
1653
1654
1655
1656
1657
1658
1659
1660
1661 stm32_spi_set_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_SSI |
1662 STM32F4_SPI_CR1_BIDIOE |
1663 STM32F4_SPI_CR1_MSTR |
1664 STM32F4_SPI_CR1_SSM);
1665
1666 spin_unlock_irqrestore(&spi->lock, flags);
1667
1668 return 0;
1669 }
1670
1671
1672
1673
1674
1675 static int stm32h7_spi_config(struct stm32_spi *spi)
1676 {
1677 unsigned long flags;
1678
1679 spin_lock_irqsave(&spi->lock, flags);
1680
1681
1682 stm32_spi_clr_bits(spi, STM32H7_SPI_I2SCFGR,
1683 STM32H7_SPI_I2SCFGR_I2SMOD);
1684
1685
1686
1687
1688
1689
1690 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SSI |
1691 STM32H7_SPI_CR1_HDDIR |
1692 STM32H7_SPI_CR1_MASRX);
1693
1694
1695
1696
1697
1698
1699
1700 stm32_spi_set_bits(spi, STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_MASTER |
1701 STM32H7_SPI_CFG2_SSM |
1702 STM32H7_SPI_CFG2_AFCNTR);
1703
1704 spin_unlock_irqrestore(&spi->lock, flags);
1705
1706 return 0;
1707 }
1708
1709 static const struct stm32_spi_cfg stm32f4_spi_cfg = {
1710 .regs = &stm32f4_spi_regspec,
1711 .get_bpw_mask = stm32f4_spi_get_bpw_mask,
1712 .disable = stm32f4_spi_disable,
1713 .config = stm32f4_spi_config,
1714 .set_bpw = stm32f4_spi_set_bpw,
1715 .set_mode = stm32f4_spi_set_mode,
1716 .transfer_one_dma_start = stm32f4_spi_transfer_one_dma_start,
1717 .dma_tx_cb = stm32f4_spi_dma_tx_cb,
1718 .dma_rx_cb = stm32_spi_dma_rx_cb,
1719 .transfer_one_irq = stm32f4_spi_transfer_one_irq,
1720 .irq_handler_event = stm32f4_spi_irq_event,
1721 .irq_handler_thread = stm32f4_spi_irq_thread,
1722 .baud_rate_div_min = STM32F4_SPI_BR_DIV_MIN,
1723 .baud_rate_div_max = STM32F4_SPI_BR_DIV_MAX,
1724 .has_fifo = false,
1725 .flags = SPI_MASTER_MUST_TX,
1726 };
1727
1728 static const struct stm32_spi_cfg stm32h7_spi_cfg = {
1729 .regs = &stm32h7_spi_regspec,
1730 .get_fifo_size = stm32h7_spi_get_fifo_size,
1731 .get_bpw_mask = stm32h7_spi_get_bpw_mask,
1732 .disable = stm32h7_spi_disable,
1733 .config = stm32h7_spi_config,
1734 .set_bpw = stm32h7_spi_set_bpw,
1735 .set_mode = stm32h7_spi_set_mode,
1736 .set_data_idleness = stm32h7_spi_data_idleness,
1737 .set_number_of_data = stm32h7_spi_number_of_data,
1738 .transfer_one_dma_start = stm32h7_spi_transfer_one_dma_start,
1739 .dma_rx_cb = stm32_spi_dma_rx_cb,
1740
1741
1742
1743
1744 .transfer_one_irq = stm32h7_spi_transfer_one_irq,
1745 .irq_handler_thread = stm32h7_spi_irq_thread,
1746 .baud_rate_div_min = STM32H7_SPI_MBR_DIV_MIN,
1747 .baud_rate_div_max = STM32H7_SPI_MBR_DIV_MAX,
1748 .has_fifo = true,
1749 };
1750
1751 static const struct of_device_id stm32_spi_of_match[] = {
1752 { .compatible = "st,stm32h7-spi", .data = (void *)&stm32h7_spi_cfg },
1753 { .compatible = "st,stm32f4-spi", .data = (void *)&stm32f4_spi_cfg },
1754 {},
1755 };
1756 MODULE_DEVICE_TABLE(of, stm32_spi_of_match);
1757
1758 static int stm32_spi_probe(struct platform_device *pdev)
1759 {
1760 struct spi_master *master;
1761 struct stm32_spi *spi;
1762 struct resource *res;
1763 struct reset_control *rst;
1764 int ret;
1765
1766 master = devm_spi_alloc_master(&pdev->dev, sizeof(struct stm32_spi));
1767 if (!master) {
1768 dev_err(&pdev->dev, "spi master allocation failed\n");
1769 return -ENOMEM;
1770 }
1771 platform_set_drvdata(pdev, master);
1772
1773 spi = spi_master_get_devdata(master);
1774 spi->dev = &pdev->dev;
1775 spi->master = master;
1776 spin_lock_init(&spi->lock);
1777
1778 spi->cfg = (const struct stm32_spi_cfg *)
1779 of_match_device(pdev->dev.driver->of_match_table,
1780 &pdev->dev)->data;
1781
1782 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1783 spi->base = devm_ioremap_resource(&pdev->dev, res);
1784 if (IS_ERR(spi->base))
1785 return PTR_ERR(spi->base);
1786
1787 spi->phys_addr = (dma_addr_t)res->start;
1788
1789 spi->irq = platform_get_irq(pdev, 0);
1790 if (spi->irq <= 0)
1791 return dev_err_probe(&pdev->dev, spi->irq,
1792 "failed to get irq\n");
1793
1794 ret = devm_request_threaded_irq(&pdev->dev, spi->irq,
1795 spi->cfg->irq_handler_event,
1796 spi->cfg->irq_handler_thread,
1797 IRQF_ONESHOT, pdev->name, master);
1798 if (ret) {
1799 dev_err(&pdev->dev, "irq%d request failed: %d\n", spi->irq,
1800 ret);
1801 return ret;
1802 }
1803
1804 spi->clk = devm_clk_get(&pdev->dev, NULL);
1805 if (IS_ERR(spi->clk)) {
1806 ret = PTR_ERR(spi->clk);
1807 dev_err(&pdev->dev, "clk get failed: %d\n", ret);
1808 return ret;
1809 }
1810
1811 ret = clk_prepare_enable(spi->clk);
1812 if (ret) {
1813 dev_err(&pdev->dev, "clk enable failed: %d\n", ret);
1814 return ret;
1815 }
1816 spi->clk_rate = clk_get_rate(spi->clk);
1817 if (!spi->clk_rate) {
1818 dev_err(&pdev->dev, "clk rate = 0\n");
1819 ret = -EINVAL;
1820 goto err_clk_disable;
1821 }
1822
1823 rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
1824 if (rst) {
1825 if (IS_ERR(rst)) {
1826 ret = dev_err_probe(&pdev->dev, PTR_ERR(rst),
1827 "failed to get reset\n");
1828 goto err_clk_disable;
1829 }
1830
1831 reset_control_assert(rst);
1832 udelay(2);
1833 reset_control_deassert(rst);
1834 }
1835
1836 if (spi->cfg->has_fifo)
1837 spi->fifo_size = spi->cfg->get_fifo_size(spi);
1838
1839 ret = spi->cfg->config(spi);
1840 if (ret) {
1841 dev_err(&pdev->dev, "controller configuration failed: %d\n",
1842 ret);
1843 goto err_clk_disable;
1844 }
1845
1846 master->dev.of_node = pdev->dev.of_node;
1847 master->auto_runtime_pm = true;
1848 master->bus_num = pdev->id;
1849 master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST |
1850 SPI_3WIRE;
1851 master->bits_per_word_mask = spi->cfg->get_bpw_mask(spi);
1852 master->max_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_min;
1853 master->min_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_max;
1854 master->use_gpio_descriptors = true;
1855 master->prepare_message = stm32_spi_prepare_msg;
1856 master->transfer_one = stm32_spi_transfer_one;
1857 master->unprepare_message = stm32_spi_unprepare_msg;
1858 master->flags = spi->cfg->flags;
1859
1860 spi->dma_tx = dma_request_chan(spi->dev, "tx");
1861 if (IS_ERR(spi->dma_tx)) {
1862 ret = PTR_ERR(spi->dma_tx);
1863 spi->dma_tx = NULL;
1864 if (ret == -EPROBE_DEFER)
1865 goto err_clk_disable;
1866
1867 dev_warn(&pdev->dev, "failed to request tx dma channel\n");
1868 } else {
1869 master->dma_tx = spi->dma_tx;
1870 }
1871
1872 spi->dma_rx = dma_request_chan(spi->dev, "rx");
1873 if (IS_ERR(spi->dma_rx)) {
1874 ret = PTR_ERR(spi->dma_rx);
1875 spi->dma_rx = NULL;
1876 if (ret == -EPROBE_DEFER)
1877 goto err_dma_release;
1878
1879 dev_warn(&pdev->dev, "failed to request rx dma channel\n");
1880 } else {
1881 master->dma_rx = spi->dma_rx;
1882 }
1883
1884 if (spi->dma_tx || spi->dma_rx)
1885 master->can_dma = stm32_spi_can_dma;
1886
1887 pm_runtime_set_autosuspend_delay(&pdev->dev,
1888 STM32_SPI_AUTOSUSPEND_DELAY);
1889 pm_runtime_use_autosuspend(&pdev->dev);
1890 pm_runtime_set_active(&pdev->dev);
1891 pm_runtime_get_noresume(&pdev->dev);
1892 pm_runtime_enable(&pdev->dev);
1893
1894 ret = spi_register_master(master);
1895 if (ret) {
1896 dev_err(&pdev->dev, "spi master registration failed: %d\n",
1897 ret);
1898 goto err_pm_disable;
1899 }
1900
1901 pm_runtime_mark_last_busy(&pdev->dev);
1902 pm_runtime_put_autosuspend(&pdev->dev);
1903
1904 dev_info(&pdev->dev, "driver initialized\n");
1905
1906 return 0;
1907
1908 err_pm_disable:
1909 pm_runtime_disable(&pdev->dev);
1910 pm_runtime_put_noidle(&pdev->dev);
1911 pm_runtime_set_suspended(&pdev->dev);
1912 pm_runtime_dont_use_autosuspend(&pdev->dev);
1913 err_dma_release:
1914 if (spi->dma_tx)
1915 dma_release_channel(spi->dma_tx);
1916 if (spi->dma_rx)
1917 dma_release_channel(spi->dma_rx);
1918 err_clk_disable:
1919 clk_disable_unprepare(spi->clk);
1920
1921 return ret;
1922 }
1923
1924 static int stm32_spi_remove(struct platform_device *pdev)
1925 {
1926 struct spi_master *master = platform_get_drvdata(pdev);
1927 struct stm32_spi *spi = spi_master_get_devdata(master);
1928
1929 pm_runtime_get_sync(&pdev->dev);
1930
1931 spi_unregister_master(master);
1932 spi->cfg->disable(spi);
1933
1934 pm_runtime_disable(&pdev->dev);
1935 pm_runtime_put_noidle(&pdev->dev);
1936 pm_runtime_set_suspended(&pdev->dev);
1937 pm_runtime_dont_use_autosuspend(&pdev->dev);
1938
1939 if (master->dma_tx)
1940 dma_release_channel(master->dma_tx);
1941 if (master->dma_rx)
1942 dma_release_channel(master->dma_rx);
1943
1944 clk_disable_unprepare(spi->clk);
1945
1946
1947 pinctrl_pm_select_sleep_state(&pdev->dev);
1948
1949 return 0;
1950 }
1951
1952 static int __maybe_unused stm32_spi_runtime_suspend(struct device *dev)
1953 {
1954 struct spi_master *master = dev_get_drvdata(dev);
1955 struct stm32_spi *spi = spi_master_get_devdata(master);
1956
1957 clk_disable_unprepare(spi->clk);
1958
1959 return pinctrl_pm_select_sleep_state(dev);
1960 }
1961
1962 static int __maybe_unused stm32_spi_runtime_resume(struct device *dev)
1963 {
1964 struct spi_master *master = dev_get_drvdata(dev);
1965 struct stm32_spi *spi = spi_master_get_devdata(master);
1966 int ret;
1967
1968 ret = pinctrl_pm_select_default_state(dev);
1969 if (ret)
1970 return ret;
1971
1972 return clk_prepare_enable(spi->clk);
1973 }
1974
1975 static int __maybe_unused stm32_spi_suspend(struct device *dev)
1976 {
1977 struct spi_master *master = dev_get_drvdata(dev);
1978 int ret;
1979
1980 ret = spi_master_suspend(master);
1981 if (ret)
1982 return ret;
1983
1984 return pm_runtime_force_suspend(dev);
1985 }
1986
1987 static int __maybe_unused stm32_spi_resume(struct device *dev)
1988 {
1989 struct spi_master *master = dev_get_drvdata(dev);
1990 struct stm32_spi *spi = spi_master_get_devdata(master);
1991 int ret;
1992
1993 ret = pm_runtime_force_resume(dev);
1994 if (ret)
1995 return ret;
1996
1997 ret = spi_master_resume(master);
1998 if (ret) {
1999 clk_disable_unprepare(spi->clk);
2000 return ret;
2001 }
2002
2003 ret = pm_runtime_resume_and_get(dev);
2004 if (ret < 0) {
2005 dev_err(dev, "Unable to power device:%d\n", ret);
2006 return ret;
2007 }
2008
2009 spi->cfg->config(spi);
2010
2011 pm_runtime_mark_last_busy(dev);
2012 pm_runtime_put_autosuspend(dev);
2013
2014 return 0;
2015 }
2016
2017 static const struct dev_pm_ops stm32_spi_pm_ops = {
2018 SET_SYSTEM_SLEEP_PM_OPS(stm32_spi_suspend, stm32_spi_resume)
2019 SET_RUNTIME_PM_OPS(stm32_spi_runtime_suspend,
2020 stm32_spi_runtime_resume, NULL)
2021 };
2022
2023 static struct platform_driver stm32_spi_driver = {
2024 .probe = stm32_spi_probe,
2025 .remove = stm32_spi_remove,
2026 .driver = {
2027 .name = DRIVER_NAME,
2028 .pm = &stm32_spi_pm_ops,
2029 .of_match_table = stm32_spi_of_match,
2030 },
2031 };
2032
2033 module_platform_driver(stm32_spi_driver);
2034
2035 MODULE_ALIAS("platform:" DRIVER_NAME);
2036 MODULE_DESCRIPTION("STMicroelectronics STM32 SPI Controller driver");
2037 MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
2038 MODULE_LICENSE("GPL v2");