2 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
3 * Author: Addy Ke <addy.ke@rock-chips.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include <linux/clk.h>
17 #include <linux/dmaengine.h>
18 #include <linux/module.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/platform_device.h>
22 #include <linux/spi/spi.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/scatterlist.h>
26 #define DRIVER_NAME "rockchip-spi"
28 #define ROCKCHIP_SPI_CLR_BITS(reg, bits) \
29 writel_relaxed(readl_relaxed(reg) & ~(bits), reg)
30 #define ROCKCHIP_SPI_SET_BITS(reg, bits) \
31 writel_relaxed(readl_relaxed(reg) | (bits), reg)
33 /* SPI register offsets */
34 #define ROCKCHIP_SPI_CTRLR0 0x0000
35 #define ROCKCHIP_SPI_CTRLR1 0x0004
36 #define ROCKCHIP_SPI_SSIENR 0x0008
37 #define ROCKCHIP_SPI_SER 0x000c
38 #define ROCKCHIP_SPI_BAUDR 0x0010
39 #define ROCKCHIP_SPI_TXFTLR 0x0014
40 #define ROCKCHIP_SPI_RXFTLR 0x0018
41 #define ROCKCHIP_SPI_TXFLR 0x001c
42 #define ROCKCHIP_SPI_RXFLR 0x0020
43 #define ROCKCHIP_SPI_SR 0x0024
44 #define ROCKCHIP_SPI_IPR 0x0028
45 #define ROCKCHIP_SPI_IMR 0x002c
46 #define ROCKCHIP_SPI_ISR 0x0030
47 #define ROCKCHIP_SPI_RISR 0x0034
48 #define ROCKCHIP_SPI_ICR 0x0038
49 #define ROCKCHIP_SPI_DMACR 0x003c
50 #define ROCKCHIP_SPI_DMATDLR 0x0040
51 #define ROCKCHIP_SPI_DMARDLR 0x0044
52 #define ROCKCHIP_SPI_TXDR 0x0400
53 #define ROCKCHIP_SPI_RXDR 0x0800
55 /* Bit fields in CTRLR0 */
56 #define CR0_DFS_OFFSET 0
58 #define CR0_CFS_OFFSET 2
60 #define CR0_SCPH_OFFSET 6
62 #define CR0_SCPOL_OFFSET 7
64 #define CR0_CSM_OFFSET 8
65 #define CR0_CSM_KEEP 0x0
66 /* ss_n be high for half sclk_out cycles */
67 #define CR0_CSM_HALF 0X1
68 /* ss_n be high for one sclk_out cycle */
69 #define CR0_CSM_ONE 0x2
71 /* ss_n to sclk_out delay */
72 #define CR0_SSD_OFFSET 10
74 * The period between ss_n active and
75 * sclk_out active is half sclk_out cycles
77 #define CR0_SSD_HALF 0x0
79 * The period between ss_n active and
80 * sclk_out active is one sclk_out cycle
82 #define CR0_SSD_ONE 0x1
84 #define CR0_EM_OFFSET 11
85 #define CR0_EM_LITTLE 0x0
86 #define CR0_EM_BIG 0x1
88 #define CR0_FBM_OFFSET 12
89 #define CR0_FBM_MSB 0x0
90 #define CR0_FBM_LSB 0x1
92 #define CR0_BHT_OFFSET 13
93 #define CR0_BHT_16BIT 0x0
94 #define CR0_BHT_8BIT 0x1
96 #define CR0_RSD_OFFSET 14
98 #define CR0_FRF_OFFSET 16
99 #define CR0_FRF_SPI 0x0
100 #define CR0_FRF_SSP 0x1
101 #define CR0_FRF_MICROWIRE 0x2
103 #define CR0_XFM_OFFSET 18
104 #define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET)
105 #define CR0_XFM_TR 0x0
106 #define CR0_XFM_TO 0x1
107 #define CR0_XFM_RO 0x2
109 #define CR0_OPM_OFFSET 20
110 #define CR0_OPM_MASTER 0x0
111 #define CR0_OPM_SLAVE 0x1
113 #define CR0_MTM_OFFSET 0x21
115 /* Bit fields in SER, 2bit */
118 /* Bit fields in SR, 5bit */
120 #define SR_BUSY (1 << 0)
121 #define SR_TF_FULL (1 << 1)
122 #define SR_TF_EMPTY (1 << 2)
123 #define SR_RF_EMPTY (1 << 3)
124 #define SR_RF_FULL (1 << 4)
126 /* Bit fields in ISR, IMR, ISR, RISR, 5bit */
127 #define INT_MASK 0x1f
128 #define INT_TF_EMPTY (1 << 0)
129 #define INT_TF_OVERFLOW (1 << 1)
130 #define INT_RF_UNDERFLOW (1 << 2)
131 #define INT_RF_OVERFLOW (1 << 3)
132 #define INT_RF_FULL (1 << 4)
134 /* Bit fields in ICR, 4bit */
135 #define ICR_MASK 0x0f
136 #define ICR_ALL (1 << 0)
137 #define ICR_RF_UNDERFLOW (1 << 1)
138 #define ICR_RF_OVERFLOW (1 << 2)
139 #define ICR_TF_OVERFLOW (1 << 3)
141 /* Bit fields in DMACR */
142 #define RF_DMA_EN (1 << 0)
143 #define TF_DMA_EN (1 << 1)
145 /* Driver state flags */
146 #define RXDMA (1 << 0)
147 #define TXDMA (1 << 1)
149 /* sclk_out: spi master internal logic in rk3x can support 50Mhz */
150 #define MAX_SCLK_OUT 50000000
153 * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However,
154 * the controller seems to hang when given 0x10000, so stick with this for now.
156 #define ROCKCHIP_SPI_MAX_TRANLEN 0xffff
158 #define ROCKCHIP_SPI_MAX_CS_NUM 2
160 struct rockchip_spi_dma_data {
165 struct rockchip_spi {
167 struct spi_master *master;
170 struct clk *apb_pclk;
176 /*depth of the FIFO buffer */
178 /* max bus freq supported */
194 bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
197 struct sg_table tx_sg;
198 struct sg_table rx_sg;
199 struct rockchip_spi_dma_data dma_rx;
200 struct rockchip_spi_dma_data dma_tx;
203 static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
205 writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR);
208 static inline void spi_set_clk(struct rockchip_spi *rs, u16 div)
210 writel_relaxed(div, rs->regs + ROCKCHIP_SPI_BAUDR);
213 static inline void wait_for_idle(struct rockchip_spi *rs)
215 unsigned long timeout = jiffies + msecs_to_jiffies(5);
218 if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
220 } while (!time_after(jiffies, timeout));
222 dev_warn(rs->dev, "spi controller is in busy state!\n");
225 static u32 get_fifo_len(struct rockchip_spi *rs)
229 for (fifo = 2; fifo < 32; fifo++) {
230 writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR);
231 if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR))
235 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR);
237 return (fifo == 31) ? 0 : fifo;
240 static inline u32 tx_max(struct rockchip_spi *rs)
242 u32 tx_left, tx_room;
244 tx_left = (rs->tx_end - rs->tx) / rs->n_bytes;
245 tx_room = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
247 return min(tx_left, tx_room);
250 static inline u32 rx_max(struct rockchip_spi *rs)
252 u32 rx_left = (rs->rx_end - rs->rx) / rs->n_bytes;
253 u32 rx_room = (u32)readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
255 return min(rx_left, rx_room);
258 static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
260 struct spi_master *master = spi->master;
261 struct rockchip_spi *rs = spi_master_get_devdata(master);
262 bool cs_asserted = !enable;
264 /* Return immediately for no-op */
265 if (cs_asserted == rs->cs_asserted[spi->chip_select])
269 /* Keep things powered as long as CS is asserted */
270 pm_runtime_get_sync(rs->dev);
272 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER,
273 BIT(spi->chip_select));
275 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER,
276 BIT(spi->chip_select));
278 /* Drop reference from when we first asserted CS */
279 pm_runtime_put(rs->dev);
282 rs->cs_asserted[spi->chip_select] = cs_asserted;
285 static int rockchip_spi_prepare_message(struct spi_master *master,
286 struct spi_message *msg)
288 struct rockchip_spi *rs = spi_master_get_devdata(master);
289 struct spi_device *spi = msg->spi;
291 rs->mode = spi->mode;
296 static void rockchip_spi_handle_err(struct spi_master *master,
297 struct spi_message *msg)
299 struct rockchip_spi *rs = spi_master_get_devdata(master);
301 /* stop running spi transfer
302 * this also flushes both rx and tx fifos
304 spi_enable_chip(rs, false);
306 if (atomic_read(&rs->state) & TXDMA)
307 dmaengine_terminate_async(rs->dma_tx.ch);
309 if (atomic_read(&rs->state) & RXDMA)
310 dmaengine_terminate_async(rs->dma_rx.ch);
313 static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
315 u32 max = tx_max(rs);
319 if (rs->n_bytes == 1)
320 txw = *(u8 *)(rs->tx);
322 txw = *(u16 *)(rs->tx);
324 writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
325 rs->tx += rs->n_bytes;
329 static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
331 u32 max = rx_max(rs);
335 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
336 if (rs->n_bytes == 1)
337 *(u8 *)(rs->rx) = (u8)rxw;
339 *(u16 *)(rs->rx) = (u16)rxw;
340 rs->rx += rs->n_bytes;
344 static int rockchip_spi_pio_transfer(struct rockchip_spi *rs)
348 spi_enable_chip(rs, true);
352 remain = rs->tx_end - rs->tx;
353 rockchip_spi_pio_writer(rs);
357 remain = rs->rx_end - rs->rx;
358 rockchip_spi_pio_reader(rs);
364 /* If tx, wait until the FIFO data completely. */
368 spi_enable_chip(rs, false);
373 static void rockchip_spi_dma_rxcb(void *data)
375 struct rockchip_spi *rs = data;
376 int state = atomic_fetch_andnot(RXDMA, &rs->state);
381 spi_enable_chip(rs, false);
382 spi_finalize_current_transfer(rs->master);
385 static void rockchip_spi_dma_txcb(void *data)
387 struct rockchip_spi *rs = data;
388 int state = atomic_fetch_andnot(TXDMA, &rs->state);
393 /* Wait until the FIFO data completely. */
396 spi_enable_chip(rs, false);
397 spi_finalize_current_transfer(rs->master);
400 static int rockchip_spi_prepare_dma(struct rockchip_spi *rs)
402 struct dma_async_tx_descriptor *rxdesc, *txdesc;
404 atomic_set(&rs->state, 0);
408 struct dma_slave_config rxconf = {
409 .direction = DMA_DEV_TO_MEM,
410 .src_addr = rs->dma_rx.addr,
411 .src_addr_width = rs->n_bytes,
415 dmaengine_slave_config(rs->dma_rx.ch, &rxconf);
417 rxdesc = dmaengine_prep_slave_sg(
419 rs->rx_sg.sgl, rs->rx_sg.nents,
420 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
424 rxdesc->callback = rockchip_spi_dma_rxcb;
425 rxdesc->callback_param = rs;
430 struct dma_slave_config txconf = {
431 .direction = DMA_MEM_TO_DEV,
432 .dst_addr = rs->dma_tx.addr,
433 .dst_addr_width = rs->n_bytes,
434 .dst_maxburst = rs->fifo_len / 2,
437 dmaengine_slave_config(rs->dma_tx.ch, &txconf);
439 txdesc = dmaengine_prep_slave_sg(
441 rs->tx_sg.sgl, rs->tx_sg.nents,
442 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
445 dmaengine_terminate_sync(rs->dma_rx.ch);
449 txdesc->callback = rockchip_spi_dma_txcb;
450 txdesc->callback_param = rs;
453 /* rx must be started before tx due to spi instinct */
455 atomic_or(RXDMA, &rs->state);
456 dmaengine_submit(rxdesc);
457 dma_async_issue_pending(rs->dma_rx.ch);
460 spi_enable_chip(rs, true);
463 atomic_or(TXDMA, &rs->state);
464 dmaengine_submit(txdesc);
465 dma_async_issue_pending(rs->dma_tx.ch);
468 /* 1 means the transfer is in progress */
472 static void rockchip_spi_config(struct rockchip_spi *rs)
478 u32 cr0 = CR0_FRF_SPI << CR0_FRF_OFFSET
479 | CR0_BHT_8BIT << CR0_BHT_OFFSET
480 | CR0_SSD_ONE << CR0_SSD_OFFSET
481 | CR0_EM_BIG << CR0_EM_OFFSET;
483 cr0 |= (rs->n_bytes << CR0_DFS_OFFSET);
484 cr0 |= ((rs->mode & 0x3) << CR0_SCPH_OFFSET);
485 cr0 |= (rs->tmode << CR0_XFM_OFFSET);
494 if (WARN_ON(rs->speed > MAX_SCLK_OUT))
495 rs->speed = MAX_SCLK_OUT;
497 /* the minimum divisor is 2 */
498 if (rs->max_freq < 2 * rs->speed) {
499 clk_set_rate(rs->spiclk, 2 * rs->speed);
500 rs->max_freq = clk_get_rate(rs->spiclk);
503 /* div doesn't support odd number */
504 div = DIV_ROUND_UP(rs->max_freq, rs->speed);
505 div = (div + 1) & 0xfffe;
507 /* Rx sample delay is expressed in parent clock cycles (max 3) */
508 rsd = DIV_ROUND_CLOSEST(rs->rsd_nsecs * (rs->max_freq >> 8),
510 if (!rsd && rs->rsd_nsecs) {
511 pr_warn_once("rockchip-spi: %u Hz are too slow to express %u ns delay\n",
512 rs->max_freq, rs->rsd_nsecs);
513 } else if (rsd > 3) {
515 pr_warn_once("rockchip-spi: %u Hz are too fast to express %u ns delay, clamping at %u ns\n",
516 rs->max_freq, rs->rsd_nsecs,
517 rsd * 1000000000U / rs->max_freq);
519 cr0 |= rsd << CR0_RSD_OFFSET;
521 writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
523 if (rs->n_bytes == 1)
524 writel_relaxed(rs->len - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);
525 else if (rs->n_bytes == 2)
526 writel_relaxed((rs->len / 2) - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);
528 writel_relaxed((rs->len * 2) - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);
530 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_TXFTLR);
531 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
533 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_DMATDLR);
534 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR);
535 writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
537 spi_set_clk(rs, div);
539 dev_dbg(rs->dev, "cr0 0x%x, div %d\n", cr0, div);
542 static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
544 return ROCKCHIP_SPI_MAX_TRANLEN;
547 static int rockchip_spi_transfer_one(
548 struct spi_master *master,
549 struct spi_device *spi,
550 struct spi_transfer *xfer)
552 struct rockchip_spi *rs = spi_master_get_devdata(master);
554 WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
555 (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
557 if (!xfer->tx_buf && !xfer->rx_buf) {
558 dev_err(rs->dev, "No buffer for transfer\n");
562 if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
563 dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
567 rs->speed = xfer->speed_hz;
568 rs->bpw = xfer->bits_per_word;
569 rs->n_bytes = rs->bpw >> 3;
571 rs->tx = xfer->tx_buf;
572 rs->tx_end = rs->tx + xfer->len;
573 rs->rx = xfer->rx_buf;
574 rs->rx_end = rs->rx + xfer->len;
577 rs->tx_sg = xfer->tx_sg;
578 rs->rx_sg = xfer->rx_sg;
580 if (rs->tx && rs->rx)
581 rs->tmode = CR0_XFM_TR;
583 rs->tmode = CR0_XFM_TO;
585 rs->tmode = CR0_XFM_RO;
587 /* we need prepare dma before spi was enabled */
588 if (master->can_dma && master->can_dma(master, spi, xfer))
593 rockchip_spi_config(rs);
596 return rockchip_spi_prepare_dma(rs);
598 return rockchip_spi_pio_transfer(rs);
601 static bool rockchip_spi_can_dma(struct spi_master *master,
602 struct spi_device *spi,
603 struct spi_transfer *xfer)
605 struct rockchip_spi *rs = spi_master_get_devdata(master);
607 return (xfer->len > rs->fifo_len);
610 static int rockchip_spi_probe(struct platform_device *pdev)
613 struct rockchip_spi *rs;
614 struct spi_master *master;
615 struct resource *mem;
618 master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi));
622 platform_set_drvdata(pdev, master);
624 rs = spi_master_get_devdata(master);
626 /* Get basic io resource and map it */
627 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
628 rs->regs = devm_ioremap_resource(&pdev->dev, mem);
629 if (IS_ERR(rs->regs)) {
630 ret = PTR_ERR(rs->regs);
634 rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
635 if (IS_ERR(rs->apb_pclk)) {
636 dev_err(&pdev->dev, "Failed to get apb_pclk\n");
637 ret = PTR_ERR(rs->apb_pclk);
641 rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
642 if (IS_ERR(rs->spiclk)) {
643 dev_err(&pdev->dev, "Failed to get spi_pclk\n");
644 ret = PTR_ERR(rs->spiclk);
648 ret = clk_prepare_enable(rs->apb_pclk);
650 dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
654 ret = clk_prepare_enable(rs->spiclk);
656 dev_err(&pdev->dev, "Failed to enable spi_clk\n");
657 goto err_disable_apbclk;
660 spi_enable_chip(rs, false);
663 rs->dev = &pdev->dev;
664 rs->max_freq = clk_get_rate(rs->spiclk);
666 if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
668 rs->rsd_nsecs = rsd_nsecs;
670 rs->fifo_len = get_fifo_len(rs);
672 dev_err(&pdev->dev, "Failed to get fifo length\n");
674 goto err_disable_spiclk;
677 pm_runtime_set_active(&pdev->dev);
678 pm_runtime_enable(&pdev->dev);
680 master->auto_runtime_pm = true;
681 master->bus_num = pdev->id;
682 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
683 master->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM;
684 master->dev.of_node = pdev->dev.of_node;
685 master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8);
687 master->set_cs = rockchip_spi_set_cs;
688 master->prepare_message = rockchip_spi_prepare_message;
689 master->transfer_one = rockchip_spi_transfer_one;
690 master->max_transfer_size = rockchip_spi_max_transfer_size;
691 master->handle_err = rockchip_spi_handle_err;
692 master->flags = SPI_MASTER_GPIO_SS;
694 rs->dma_tx.ch = dma_request_chan(rs->dev, "tx");
695 if (IS_ERR(rs->dma_tx.ch)) {
696 /* Check tx to see if we need defer probing driver */
697 if (PTR_ERR(rs->dma_tx.ch) == -EPROBE_DEFER) {
699 goto err_disable_pm_runtime;
701 dev_warn(rs->dev, "Failed to request TX DMA channel\n");
702 rs->dma_tx.ch = NULL;
705 rs->dma_rx.ch = dma_request_chan(rs->dev, "rx");
706 if (IS_ERR(rs->dma_rx.ch)) {
707 if (PTR_ERR(rs->dma_rx.ch) == -EPROBE_DEFER) {
709 goto err_free_dma_tx;
711 dev_warn(rs->dev, "Failed to request RX DMA channel\n");
712 rs->dma_rx.ch = NULL;
715 if (rs->dma_tx.ch && rs->dma_rx.ch) {
716 rs->dma_tx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_TXDR);
717 rs->dma_rx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_RXDR);
719 master->can_dma = rockchip_spi_can_dma;
720 master->dma_tx = rs->dma_tx.ch;
721 master->dma_rx = rs->dma_rx.ch;
724 ret = devm_spi_register_master(&pdev->dev, master);
726 dev_err(&pdev->dev, "Failed to register master\n");
727 goto err_free_dma_rx;
734 dma_release_channel(rs->dma_rx.ch);
737 dma_release_channel(rs->dma_tx.ch);
738 err_disable_pm_runtime:
739 pm_runtime_disable(&pdev->dev);
741 clk_disable_unprepare(rs->spiclk);
743 clk_disable_unprepare(rs->apb_pclk);
745 spi_master_put(master);
750 static int rockchip_spi_remove(struct platform_device *pdev)
752 struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
753 struct rockchip_spi *rs = spi_master_get_devdata(master);
755 pm_runtime_get_sync(&pdev->dev);
757 clk_disable_unprepare(rs->spiclk);
758 clk_disable_unprepare(rs->apb_pclk);
760 pm_runtime_put_noidle(&pdev->dev);
761 pm_runtime_disable(&pdev->dev);
762 pm_runtime_set_suspended(&pdev->dev);
765 dma_release_channel(rs->dma_tx.ch);
767 dma_release_channel(rs->dma_rx.ch);
769 spi_master_put(master);
774 #ifdef CONFIG_PM_SLEEP
775 static int rockchip_spi_suspend(struct device *dev)
778 struct spi_master *master = dev_get_drvdata(dev);
779 struct rockchip_spi *rs = spi_master_get_devdata(master);
781 ret = spi_master_suspend(rs->master);
785 ret = pm_runtime_force_suspend(dev);
789 pinctrl_pm_select_sleep_state(dev);
794 static int rockchip_spi_resume(struct device *dev)
797 struct spi_master *master = dev_get_drvdata(dev);
798 struct rockchip_spi *rs = spi_master_get_devdata(master);
800 pinctrl_pm_select_default_state(dev);
802 ret = pm_runtime_force_resume(dev);
806 ret = spi_master_resume(rs->master);
808 clk_disable_unprepare(rs->spiclk);
809 clk_disable_unprepare(rs->apb_pclk);
814 #endif /* CONFIG_PM_SLEEP */
817 static int rockchip_spi_runtime_suspend(struct device *dev)
819 struct spi_master *master = dev_get_drvdata(dev);
820 struct rockchip_spi *rs = spi_master_get_devdata(master);
822 clk_disable_unprepare(rs->spiclk);
823 clk_disable_unprepare(rs->apb_pclk);
828 static int rockchip_spi_runtime_resume(struct device *dev)
831 struct spi_master *master = dev_get_drvdata(dev);
832 struct rockchip_spi *rs = spi_master_get_devdata(master);
834 ret = clk_prepare_enable(rs->apb_pclk);
838 ret = clk_prepare_enable(rs->spiclk);
840 clk_disable_unprepare(rs->apb_pclk);
844 #endif /* CONFIG_PM */
846 static const struct dev_pm_ops rockchip_spi_pm = {
847 SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
848 SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
849 rockchip_spi_runtime_resume, NULL)
852 static const struct of_device_id rockchip_spi_dt_match[] = {
853 { .compatible = "rockchip,rv1108-spi", },
854 { .compatible = "rockchip,rk3036-spi", },
855 { .compatible = "rockchip,rk3066-spi", },
856 { .compatible = "rockchip,rk3188-spi", },
857 { .compatible = "rockchip,rk3228-spi", },
858 { .compatible = "rockchip,rk3288-spi", },
859 { .compatible = "rockchip,rk3368-spi", },
860 { .compatible = "rockchip,rk3399-spi", },
863 MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
865 static struct platform_driver rockchip_spi_driver = {
868 .pm = &rockchip_spi_pm,
869 .of_match_table = of_match_ptr(rockchip_spi_dt_match),
871 .probe = rockchip_spi_probe,
872 .remove = rockchip_spi_remove,
875 module_platform_driver(rockchip_spi_driver);
877 MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
878 MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
879 MODULE_LICENSE("GPL v2");