Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / spi / spi-rockchip.c
blob09d8e92400eb89b706c560d759d35f5d8f25feab
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
4 * Author: Addy Ke <addy.ke@rock-chips.com>
5 */
7 #include <linux/clk.h>
8 #include <linux/dmaengine.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/pinctrl/consumer.h>
13 #include <linux/platform_device.h>
14 #include <linux/spi/spi.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/scatterlist.h>
18 #define DRIVER_NAME "rockchip-spi"
20 #define ROCKCHIP_SPI_CLR_BITS(reg, bits) \
21 writel_relaxed(readl_relaxed(reg) & ~(bits), reg)
22 #define ROCKCHIP_SPI_SET_BITS(reg, bits) \
23 writel_relaxed(readl_relaxed(reg) | (bits), reg)
25 /* SPI register offsets */
26 #define ROCKCHIP_SPI_CTRLR0 0x0000
27 #define ROCKCHIP_SPI_CTRLR1 0x0004
28 #define ROCKCHIP_SPI_SSIENR 0x0008
29 #define ROCKCHIP_SPI_SER 0x000c
30 #define ROCKCHIP_SPI_BAUDR 0x0010
31 #define ROCKCHIP_SPI_TXFTLR 0x0014
32 #define ROCKCHIP_SPI_RXFTLR 0x0018
33 #define ROCKCHIP_SPI_TXFLR 0x001c
34 #define ROCKCHIP_SPI_RXFLR 0x0020
35 #define ROCKCHIP_SPI_SR 0x0024
36 #define ROCKCHIP_SPI_IPR 0x0028
37 #define ROCKCHIP_SPI_IMR 0x002c
38 #define ROCKCHIP_SPI_ISR 0x0030
39 #define ROCKCHIP_SPI_RISR 0x0034
40 #define ROCKCHIP_SPI_ICR 0x0038
41 #define ROCKCHIP_SPI_DMACR 0x003c
42 #define ROCKCHIP_SPI_DMATDLR 0x0040
43 #define ROCKCHIP_SPI_DMARDLR 0x0044
44 #define ROCKCHIP_SPI_VERSION 0x0048
45 #define ROCKCHIP_SPI_TXDR 0x0400
46 #define ROCKCHIP_SPI_RXDR 0x0800
48 /* Bit fields in CTRLR0 */
49 #define CR0_DFS_OFFSET 0
50 #define CR0_DFS_4BIT 0x0
51 #define CR0_DFS_8BIT 0x1
52 #define CR0_DFS_16BIT 0x2
54 #define CR0_CFS_OFFSET 2
56 #define CR0_SCPH_OFFSET 6
58 #define CR0_SCPOL_OFFSET 7
60 #define CR0_CSM_OFFSET 8
61 #define CR0_CSM_KEEP 0x0
62 /* ss_n be high for half sclk_out cycles */
63 #define CR0_CSM_HALF 0X1
64 /* ss_n be high for one sclk_out cycle */
65 #define CR0_CSM_ONE 0x2
67 /* ss_n to sclk_out delay */
68 #define CR0_SSD_OFFSET 10
70 * The period between ss_n active and
71 * sclk_out active is half sclk_out cycles
73 #define CR0_SSD_HALF 0x0
75 * The period between ss_n active and
76 * sclk_out active is one sclk_out cycle
78 #define CR0_SSD_ONE 0x1
80 #define CR0_EM_OFFSET 11
81 #define CR0_EM_LITTLE 0x0
82 #define CR0_EM_BIG 0x1
84 #define CR0_FBM_OFFSET 12
85 #define CR0_FBM_MSB 0x0
86 #define CR0_FBM_LSB 0x1
88 #define CR0_BHT_OFFSET 13
89 #define CR0_BHT_16BIT 0x0
90 #define CR0_BHT_8BIT 0x1
92 #define CR0_RSD_OFFSET 14
93 #define CR0_RSD_MAX 0x3
95 #define CR0_FRF_OFFSET 16
96 #define CR0_FRF_SPI 0x0
97 #define CR0_FRF_SSP 0x1
98 #define CR0_FRF_MICROWIRE 0x2
100 #define CR0_XFM_OFFSET 18
101 #define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET)
102 #define CR0_XFM_TR 0x0
103 #define CR0_XFM_TO 0x1
104 #define CR0_XFM_RO 0x2
106 #define CR0_OPM_OFFSET 20
107 #define CR0_OPM_MASTER 0x0
108 #define CR0_OPM_SLAVE 0x1
110 #define CR0_MTM_OFFSET 0x21
112 /* Bit fields in SER, 2bit */
113 #define SER_MASK 0x3
115 /* Bit fields in BAUDR */
116 #define BAUDR_SCKDV_MIN 2
117 #define BAUDR_SCKDV_MAX 65534
119 /* Bit fields in SR, 5bit */
120 #define SR_MASK 0x1f
121 #define SR_BUSY (1 << 0)
122 #define SR_TF_FULL (1 << 1)
123 #define SR_TF_EMPTY (1 << 2)
124 #define SR_RF_EMPTY (1 << 3)
125 #define SR_RF_FULL (1 << 4)
127 /* Bit fields in ISR, IMR, ISR, RISR, 5bit */
128 #define INT_MASK 0x1f
129 #define INT_TF_EMPTY (1 << 0)
130 #define INT_TF_OVERFLOW (1 << 1)
131 #define INT_RF_UNDERFLOW (1 << 2)
132 #define INT_RF_OVERFLOW (1 << 3)
133 #define INT_RF_FULL (1 << 4)
135 /* Bit fields in ICR, 4bit */
136 #define ICR_MASK 0x0f
137 #define ICR_ALL (1 << 0)
138 #define ICR_RF_UNDERFLOW (1 << 1)
139 #define ICR_RF_OVERFLOW (1 << 2)
140 #define ICR_TF_OVERFLOW (1 << 3)
142 /* Bit fields in DMACR */
143 #define RF_DMA_EN (1 << 0)
144 #define TF_DMA_EN (1 << 1)
146 /* Driver state flags */
147 #define RXDMA (1 << 0)
148 #define TXDMA (1 << 1)
150 /* sclk_out: spi master internal logic in rk3x can support 50Mhz */
151 #define MAX_SCLK_OUT 50000000U
154 * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However,
155 * the controller seems to hang when given 0x10000, so stick with this for now.
157 #define ROCKCHIP_SPI_MAX_TRANLEN 0xffff
159 #define ROCKCHIP_SPI_MAX_CS_NUM 2
160 #define ROCKCHIP_SPI_VER2_TYPE1 0x05EC0002
161 #define ROCKCHIP_SPI_VER2_TYPE2 0x00110002
163 #define ROCKCHIP_AUTOSUSPEND_TIMEOUT 2000
165 struct rockchip_spi {
166 struct device *dev;
168 struct clk *spiclk;
169 struct clk *apb_pclk;
171 void __iomem *regs;
172 dma_addr_t dma_addr_rx;
173 dma_addr_t dma_addr_tx;
175 const void *tx;
176 void *rx;
177 unsigned int tx_left;
178 unsigned int rx_left;
180 atomic_t state;
182 /*depth of the FIFO buffer */
183 u32 fifo_len;
184 /* frequency of spiclk */
185 u32 freq;
187 u8 n_bytes;
188 u8 rsd;
190 bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
192 bool slave_abort;
195 static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
197 writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR);
200 static inline void wait_for_idle(struct rockchip_spi *rs)
202 unsigned long timeout = jiffies + msecs_to_jiffies(5);
204 do {
205 if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
206 return;
207 } while (!time_after(jiffies, timeout));
209 dev_warn(rs->dev, "spi controller is in busy state!\n");
212 static u32 get_fifo_len(struct rockchip_spi *rs)
214 u32 ver;
216 ver = readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION);
218 switch (ver) {
219 case ROCKCHIP_SPI_VER2_TYPE1:
220 case ROCKCHIP_SPI_VER2_TYPE2:
221 return 64;
222 default:
223 return 32;
227 static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
229 struct spi_controller *ctlr = spi->controller;
230 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
231 bool cs_asserted = !enable;
233 /* Return immediately for no-op */
234 if (cs_asserted == rs->cs_asserted[spi->chip_select])
235 return;
237 if (cs_asserted) {
238 /* Keep things powered as long as CS is asserted */
239 pm_runtime_get_sync(rs->dev);
241 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER,
242 BIT(spi->chip_select));
243 } else {
244 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER,
245 BIT(spi->chip_select));
247 /* Drop reference from when we first asserted CS */
248 pm_runtime_put(rs->dev);
251 rs->cs_asserted[spi->chip_select] = cs_asserted;
254 static void rockchip_spi_handle_err(struct spi_controller *ctlr,
255 struct spi_message *msg)
257 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
259 /* stop running spi transfer
260 * this also flushes both rx and tx fifos
262 spi_enable_chip(rs, false);
264 /* make sure all interrupts are masked */
265 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
267 if (atomic_read(&rs->state) & TXDMA)
268 dmaengine_terminate_async(ctlr->dma_tx);
270 if (atomic_read(&rs->state) & RXDMA)
271 dmaengine_terminate_async(ctlr->dma_rx);
274 static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
276 u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
277 u32 words = min(rs->tx_left, tx_free);
279 rs->tx_left -= words;
280 for (; words; words--) {
281 u32 txw;
283 if (rs->n_bytes == 1)
284 txw = *(u8 *)rs->tx;
285 else
286 txw = *(u16 *)rs->tx;
288 writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
289 rs->tx += rs->n_bytes;
293 static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
295 u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
296 u32 rx_left = (rs->rx_left > words) ? rs->rx_left - words : 0;
298 /* the hardware doesn't allow us to change fifo threshold
299 * level while spi is enabled, so instead make sure to leave
300 * enough words in the rx fifo to get the last interrupt
301 * exactly when all words have been received
303 if (rx_left) {
304 u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1;
306 if (rx_left < ftl) {
307 rx_left = ftl;
308 words = rs->rx_left - rx_left;
312 rs->rx_left = rx_left;
313 for (; words; words--) {
314 u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
316 if (!rs->rx)
317 continue;
319 if (rs->n_bytes == 1)
320 *(u8 *)rs->rx = (u8)rxw;
321 else
322 *(u16 *)rs->rx = (u16)rxw;
323 rs->rx += rs->n_bytes;
327 static irqreturn_t rockchip_spi_isr(int irq, void *dev_id)
329 struct spi_controller *ctlr = dev_id;
330 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
332 if (rs->tx_left)
333 rockchip_spi_pio_writer(rs);
335 rockchip_spi_pio_reader(rs);
336 if (!rs->rx_left) {
337 spi_enable_chip(rs, false);
338 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
339 spi_finalize_current_transfer(ctlr);
342 return IRQ_HANDLED;
345 static int rockchip_spi_prepare_irq(struct rockchip_spi *rs,
346 struct spi_transfer *xfer)
348 rs->tx = xfer->tx_buf;
349 rs->rx = xfer->rx_buf;
350 rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0;
351 rs->rx_left = xfer->len / rs->n_bytes;
353 writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR);
354 spi_enable_chip(rs, true);
356 if (rs->tx_left)
357 rockchip_spi_pio_writer(rs);
359 /* 1 means the transfer is in progress */
360 return 1;
363 static void rockchip_spi_dma_rxcb(void *data)
365 struct spi_controller *ctlr = data;
366 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
367 int state = atomic_fetch_andnot(RXDMA, &rs->state);
369 if (state & TXDMA && !rs->slave_abort)
370 return;
372 spi_enable_chip(rs, false);
373 spi_finalize_current_transfer(ctlr);
376 static void rockchip_spi_dma_txcb(void *data)
378 struct spi_controller *ctlr = data;
379 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
380 int state = atomic_fetch_andnot(TXDMA, &rs->state);
382 if (state & RXDMA && !rs->slave_abort)
383 return;
385 /* Wait until the FIFO data completely. */
386 wait_for_idle(rs);
388 spi_enable_chip(rs, false);
389 spi_finalize_current_transfer(ctlr);
392 static u32 rockchip_spi_calc_burst_size(u32 data_len)
394 u32 i;
396 /* burst size: 1, 2, 4, 8 */
397 for (i = 1; i < 8; i <<= 1) {
398 if (data_len & i)
399 break;
402 return i;
405 static int rockchip_spi_prepare_dma(struct rockchip_spi *rs,
406 struct spi_controller *ctlr, struct spi_transfer *xfer)
408 struct dma_async_tx_descriptor *rxdesc, *txdesc;
410 atomic_set(&rs->state, 0);
412 rxdesc = NULL;
413 if (xfer->rx_buf) {
414 struct dma_slave_config rxconf = {
415 .direction = DMA_DEV_TO_MEM,
416 .src_addr = rs->dma_addr_rx,
417 .src_addr_width = rs->n_bytes,
418 .src_maxburst = rockchip_spi_calc_burst_size(xfer->len /
419 rs->n_bytes),
422 dmaengine_slave_config(ctlr->dma_rx, &rxconf);
424 rxdesc = dmaengine_prep_slave_sg(
425 ctlr->dma_rx,
426 xfer->rx_sg.sgl, xfer->rx_sg.nents,
427 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
428 if (!rxdesc)
429 return -EINVAL;
431 rxdesc->callback = rockchip_spi_dma_rxcb;
432 rxdesc->callback_param = ctlr;
435 txdesc = NULL;
436 if (xfer->tx_buf) {
437 struct dma_slave_config txconf = {
438 .direction = DMA_MEM_TO_DEV,
439 .dst_addr = rs->dma_addr_tx,
440 .dst_addr_width = rs->n_bytes,
441 .dst_maxburst = rs->fifo_len / 4,
444 dmaengine_slave_config(ctlr->dma_tx, &txconf);
446 txdesc = dmaengine_prep_slave_sg(
447 ctlr->dma_tx,
448 xfer->tx_sg.sgl, xfer->tx_sg.nents,
449 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
450 if (!txdesc) {
451 if (rxdesc)
452 dmaengine_terminate_sync(ctlr->dma_rx);
453 return -EINVAL;
456 txdesc->callback = rockchip_spi_dma_txcb;
457 txdesc->callback_param = ctlr;
460 /* rx must be started before tx due to spi instinct */
461 if (rxdesc) {
462 atomic_or(RXDMA, &rs->state);
463 dmaengine_submit(rxdesc);
464 dma_async_issue_pending(ctlr->dma_rx);
467 spi_enable_chip(rs, true);
469 if (txdesc) {
470 atomic_or(TXDMA, &rs->state);
471 dmaengine_submit(txdesc);
472 dma_async_issue_pending(ctlr->dma_tx);
475 /* 1 means the transfer is in progress */
476 return 1;
479 static void rockchip_spi_config(struct rockchip_spi *rs,
480 struct spi_device *spi, struct spi_transfer *xfer,
481 bool use_dma, bool slave_mode)
483 u32 cr0 = CR0_FRF_SPI << CR0_FRF_OFFSET
484 | CR0_BHT_8BIT << CR0_BHT_OFFSET
485 | CR0_SSD_ONE << CR0_SSD_OFFSET
486 | CR0_EM_BIG << CR0_EM_OFFSET;
487 u32 cr1;
488 u32 dmacr = 0;
490 if (slave_mode)
491 cr0 |= CR0_OPM_SLAVE << CR0_OPM_OFFSET;
492 rs->slave_abort = false;
494 cr0 |= rs->rsd << CR0_RSD_OFFSET;
495 cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET;
496 if (spi->mode & SPI_LSB_FIRST)
497 cr0 |= CR0_FBM_LSB << CR0_FBM_OFFSET;
499 if (xfer->rx_buf && xfer->tx_buf)
500 cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET;
501 else if (xfer->rx_buf)
502 cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET;
503 else if (use_dma)
504 cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET;
506 switch (xfer->bits_per_word) {
507 case 4:
508 cr0 |= CR0_DFS_4BIT << CR0_DFS_OFFSET;
509 cr1 = xfer->len - 1;
510 break;
511 case 8:
512 cr0 |= CR0_DFS_8BIT << CR0_DFS_OFFSET;
513 cr1 = xfer->len - 1;
514 break;
515 case 16:
516 cr0 |= CR0_DFS_16BIT << CR0_DFS_OFFSET;
517 cr1 = xfer->len / 2 - 1;
518 break;
519 default:
520 /* we only whitelist 4, 8 and 16 bit words in
521 * ctlr->bits_per_word_mask, so this shouldn't
522 * happen
524 unreachable();
527 if (use_dma) {
528 if (xfer->tx_buf)
529 dmacr |= TF_DMA_EN;
530 if (xfer->rx_buf)
531 dmacr |= RF_DMA_EN;
534 writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
535 writel_relaxed(cr1, rs->regs + ROCKCHIP_SPI_CTRLR1);
537 /* unfortunately setting the fifo threshold level to generate an
538 * interrupt exactly when the fifo is full doesn't seem to work,
539 * so we need the strict inequality here
541 if (xfer->len < rs->fifo_len)
542 writel_relaxed(xfer->len - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
543 else
544 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
546 writel_relaxed(rs->fifo_len / 2, rs->regs + ROCKCHIP_SPI_DMATDLR);
547 writel_relaxed(rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes) - 1,
548 rs->regs + ROCKCHIP_SPI_DMARDLR);
549 writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
551 /* the hardware only supports an even clock divisor, so
552 * round divisor = spiclk / speed up to nearest even number
553 * so that the resulting speed is <= the requested speed
555 writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz),
556 rs->regs + ROCKCHIP_SPI_BAUDR);
559 static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
561 return ROCKCHIP_SPI_MAX_TRANLEN;
564 static int rockchip_spi_slave_abort(struct spi_controller *ctlr)
566 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
568 rs->slave_abort = true;
569 complete(&ctlr->xfer_completion);
571 return 0;
574 static int rockchip_spi_transfer_one(
575 struct spi_controller *ctlr,
576 struct spi_device *spi,
577 struct spi_transfer *xfer)
579 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
580 bool use_dma;
582 WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
583 (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
585 if (!xfer->tx_buf && !xfer->rx_buf) {
586 dev_err(rs->dev, "No buffer for transfer\n");
587 return -EINVAL;
590 if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
591 dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
592 return -EINVAL;
595 rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2;
597 use_dma = ctlr->can_dma ? ctlr->can_dma(ctlr, spi, xfer) : false;
599 rockchip_spi_config(rs, spi, xfer, use_dma, ctlr->slave);
601 if (use_dma)
602 return rockchip_spi_prepare_dma(rs, ctlr, xfer);
604 return rockchip_spi_prepare_irq(rs, xfer);
607 static bool rockchip_spi_can_dma(struct spi_controller *ctlr,
608 struct spi_device *spi,
609 struct spi_transfer *xfer)
611 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
612 unsigned int bytes_per_word = xfer->bits_per_word <= 8 ? 1 : 2;
614 /* if the numbor of spi words to transfer is less than the fifo
615 * length we can just fill the fifo and wait for a single irq,
616 * so don't bother setting up dma
618 return xfer->len / bytes_per_word >= rs->fifo_len;
621 static int rockchip_spi_probe(struct platform_device *pdev)
623 int ret;
624 struct rockchip_spi *rs;
625 struct spi_controller *ctlr;
626 struct resource *mem;
627 struct device_node *np = pdev->dev.of_node;
628 u32 rsd_nsecs;
629 bool slave_mode;
631 slave_mode = of_property_read_bool(np, "spi-slave");
633 if (slave_mode)
634 ctlr = spi_alloc_slave(&pdev->dev,
635 sizeof(struct rockchip_spi));
636 else
637 ctlr = spi_alloc_master(&pdev->dev,
638 sizeof(struct rockchip_spi));
640 if (!ctlr)
641 return -ENOMEM;
643 platform_set_drvdata(pdev, ctlr);
645 rs = spi_controller_get_devdata(ctlr);
646 ctlr->slave = slave_mode;
648 /* Get basic io resource and map it */
649 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
650 rs->regs = devm_ioremap_resource(&pdev->dev, mem);
651 if (IS_ERR(rs->regs)) {
652 ret = PTR_ERR(rs->regs);
653 goto err_put_ctlr;
656 rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
657 if (IS_ERR(rs->apb_pclk)) {
658 dev_err(&pdev->dev, "Failed to get apb_pclk\n");
659 ret = PTR_ERR(rs->apb_pclk);
660 goto err_put_ctlr;
663 rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
664 if (IS_ERR(rs->spiclk)) {
665 dev_err(&pdev->dev, "Failed to get spi_pclk\n");
666 ret = PTR_ERR(rs->spiclk);
667 goto err_put_ctlr;
670 ret = clk_prepare_enable(rs->apb_pclk);
671 if (ret < 0) {
672 dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
673 goto err_put_ctlr;
676 ret = clk_prepare_enable(rs->spiclk);
677 if (ret < 0) {
678 dev_err(&pdev->dev, "Failed to enable spi_clk\n");
679 goto err_disable_apbclk;
682 spi_enable_chip(rs, false);
684 ret = platform_get_irq(pdev, 0);
685 if (ret < 0)
686 goto err_disable_spiclk;
688 ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL,
689 IRQF_ONESHOT, dev_name(&pdev->dev), ctlr);
690 if (ret)
691 goto err_disable_spiclk;
693 rs->dev = &pdev->dev;
694 rs->freq = clk_get_rate(rs->spiclk);
696 if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
697 &rsd_nsecs)) {
698 /* rx sample delay is expressed in parent clock cycles (max 3) */
699 u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8),
700 1000000000 >> 8);
701 if (!rsd) {
702 dev_warn(rs->dev, "%u Hz are too slow to express %u ns delay\n",
703 rs->freq, rsd_nsecs);
704 } else if (rsd > CR0_RSD_MAX) {
705 rsd = CR0_RSD_MAX;
706 dev_warn(rs->dev, "%u Hz are too fast to express %u ns delay, clamping at %u ns\n",
707 rs->freq, rsd_nsecs,
708 CR0_RSD_MAX * 1000000000U / rs->freq);
710 rs->rsd = rsd;
713 rs->fifo_len = get_fifo_len(rs);
714 if (!rs->fifo_len) {
715 dev_err(&pdev->dev, "Failed to get fifo length\n");
716 ret = -EINVAL;
717 goto err_disable_spiclk;
720 pm_runtime_set_autosuspend_delay(&pdev->dev, ROCKCHIP_AUTOSUSPEND_TIMEOUT);
721 pm_runtime_use_autosuspend(&pdev->dev);
722 pm_runtime_set_active(&pdev->dev);
723 pm_runtime_enable(&pdev->dev);
725 ctlr->auto_runtime_pm = true;
726 ctlr->bus_num = pdev->id;
727 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST;
728 if (slave_mode) {
729 ctlr->mode_bits |= SPI_NO_CS;
730 ctlr->slave_abort = rockchip_spi_slave_abort;
731 } else {
732 ctlr->flags = SPI_MASTER_GPIO_SS;
733 ctlr->max_native_cs = ROCKCHIP_SPI_MAX_CS_NUM;
735 * rk spi0 has two native cs, spi1..5 one cs only
736 * if num-cs is missing in the dts, default to 1
738 if (of_property_read_u16(np, "num-cs", &ctlr->num_chipselect))
739 ctlr->num_chipselect = 1;
740 ctlr->use_gpio_descriptors = true;
742 ctlr->dev.of_node = pdev->dev.of_node;
743 ctlr->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4);
744 ctlr->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX;
745 ctlr->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT);
747 ctlr->set_cs = rockchip_spi_set_cs;
748 ctlr->transfer_one = rockchip_spi_transfer_one;
749 ctlr->max_transfer_size = rockchip_spi_max_transfer_size;
750 ctlr->handle_err = rockchip_spi_handle_err;
752 ctlr->dma_tx = dma_request_chan(rs->dev, "tx");
753 if (IS_ERR(ctlr->dma_tx)) {
754 /* Check tx to see if we need defer probing driver */
755 if (PTR_ERR(ctlr->dma_tx) == -EPROBE_DEFER) {
756 ret = -EPROBE_DEFER;
757 goto err_disable_pm_runtime;
759 dev_warn(rs->dev, "Failed to request TX DMA channel\n");
760 ctlr->dma_tx = NULL;
763 ctlr->dma_rx = dma_request_chan(rs->dev, "rx");
764 if (IS_ERR(ctlr->dma_rx)) {
765 if (PTR_ERR(ctlr->dma_rx) == -EPROBE_DEFER) {
766 ret = -EPROBE_DEFER;
767 goto err_free_dma_tx;
769 dev_warn(rs->dev, "Failed to request RX DMA channel\n");
770 ctlr->dma_rx = NULL;
773 if (ctlr->dma_tx && ctlr->dma_rx) {
774 rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR;
775 rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR;
776 ctlr->can_dma = rockchip_spi_can_dma;
779 ret = devm_spi_register_controller(&pdev->dev, ctlr);
780 if (ret < 0) {
781 dev_err(&pdev->dev, "Failed to register controller\n");
782 goto err_free_dma_rx;
785 return 0;
787 err_free_dma_rx:
788 if (ctlr->dma_rx)
789 dma_release_channel(ctlr->dma_rx);
790 err_free_dma_tx:
791 if (ctlr->dma_tx)
792 dma_release_channel(ctlr->dma_tx);
793 err_disable_pm_runtime:
794 pm_runtime_disable(&pdev->dev);
795 err_disable_spiclk:
796 clk_disable_unprepare(rs->spiclk);
797 err_disable_apbclk:
798 clk_disable_unprepare(rs->apb_pclk);
799 err_put_ctlr:
800 spi_controller_put(ctlr);
802 return ret;
805 static int rockchip_spi_remove(struct platform_device *pdev)
807 struct spi_controller *ctlr = spi_controller_get(platform_get_drvdata(pdev));
808 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
810 pm_runtime_get_sync(&pdev->dev);
812 clk_disable_unprepare(rs->spiclk);
813 clk_disable_unprepare(rs->apb_pclk);
815 pm_runtime_put_noidle(&pdev->dev);
816 pm_runtime_disable(&pdev->dev);
817 pm_runtime_set_suspended(&pdev->dev);
819 if (ctlr->dma_tx)
820 dma_release_channel(ctlr->dma_tx);
821 if (ctlr->dma_rx)
822 dma_release_channel(ctlr->dma_rx);
824 spi_controller_put(ctlr);
826 return 0;
829 #ifdef CONFIG_PM_SLEEP
830 static int rockchip_spi_suspend(struct device *dev)
832 int ret;
833 struct spi_controller *ctlr = dev_get_drvdata(dev);
835 ret = spi_controller_suspend(ctlr);
836 if (ret < 0)
837 return ret;
839 ret = pm_runtime_force_suspend(dev);
840 if (ret < 0)
841 return ret;
843 pinctrl_pm_select_sleep_state(dev);
845 return 0;
848 static int rockchip_spi_resume(struct device *dev)
850 int ret;
851 struct spi_controller *ctlr = dev_get_drvdata(dev);
852 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
854 pinctrl_pm_select_default_state(dev);
856 ret = pm_runtime_force_resume(dev);
857 if (ret < 0)
858 return ret;
860 ret = spi_controller_resume(ctlr);
861 if (ret < 0) {
862 clk_disable_unprepare(rs->spiclk);
863 clk_disable_unprepare(rs->apb_pclk);
866 return 0;
868 #endif /* CONFIG_PM_SLEEP */
870 #ifdef CONFIG_PM
871 static int rockchip_spi_runtime_suspend(struct device *dev)
873 struct spi_controller *ctlr = dev_get_drvdata(dev);
874 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
876 clk_disable_unprepare(rs->spiclk);
877 clk_disable_unprepare(rs->apb_pclk);
879 return 0;
882 static int rockchip_spi_runtime_resume(struct device *dev)
884 int ret;
885 struct spi_controller *ctlr = dev_get_drvdata(dev);
886 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
888 ret = clk_prepare_enable(rs->apb_pclk);
889 if (ret < 0)
890 return ret;
892 ret = clk_prepare_enable(rs->spiclk);
893 if (ret < 0)
894 clk_disable_unprepare(rs->apb_pclk);
896 return 0;
898 #endif /* CONFIG_PM */
900 static const struct dev_pm_ops rockchip_spi_pm = {
901 SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
902 SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
903 rockchip_spi_runtime_resume, NULL)
906 static const struct of_device_id rockchip_spi_dt_match[] = {
907 { .compatible = "rockchip,px30-spi", },
908 { .compatible = "rockchip,rk3036-spi", },
909 { .compatible = "rockchip,rk3066-spi", },
910 { .compatible = "rockchip,rk3188-spi", },
911 { .compatible = "rockchip,rk3228-spi", },
912 { .compatible = "rockchip,rk3288-spi", },
913 { .compatible = "rockchip,rk3308-spi", },
914 { .compatible = "rockchip,rk3328-spi", },
915 { .compatible = "rockchip,rk3368-spi", },
916 { .compatible = "rockchip,rk3399-spi", },
917 { .compatible = "rockchip,rv1108-spi", },
918 { },
920 MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
922 static struct platform_driver rockchip_spi_driver = {
923 .driver = {
924 .name = DRIVER_NAME,
925 .pm = &rockchip_spi_pm,
926 .of_match_table = of_match_ptr(rockchip_spi_dt_match),
928 .probe = rockchip_spi_probe,
929 .remove = rockchip_spi_remove,
932 module_platform_driver(rockchip_spi_driver);
934 MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
935 MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
936 MODULE_LICENSE("GPL v2");