xref: /linux/drivers/spi/spi-rockchip.c (revision d66571a20f68f153c045c9822bf4c288e7511168)
12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
264e36824Saddy ke /*
364e36824Saddy ke  * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
45dcc44edSAddy Ke  * Author: Addy Ke <addy.ke@rock-chips.com>
564e36824Saddy ke  */
664e36824Saddy ke 
764e36824Saddy ke #include <linux/clk.h>
864e36824Saddy ke #include <linux/dmaengine.h>
98af0c18aSSuren Baghdasaryan #include <linux/interrupt.h>
10ec5c5d8aSShawn Lin #include <linux/module.h>
11ec5c5d8aSShawn Lin #include <linux/of.h>
1223e291c2SBrian Norris #include <linux/pinctrl/consumer.h>
13ec5c5d8aSShawn Lin #include <linux/platform_device.h>
14ec5c5d8aSShawn Lin #include <linux/spi/spi.h>
15ec5c5d8aSShawn Lin #include <linux/pm_runtime.h>
16ec5c5d8aSShawn Lin #include <linux/scatterlist.h>
1764e36824Saddy ke 
1864e36824Saddy ke #define DRIVER_NAME "rockchip-spi"
1964e36824Saddy ke 
20aa099382SJeffy Chen #define ROCKCHIP_SPI_CLR_BITS(reg, bits) \
21aa099382SJeffy Chen 		writel_relaxed(readl_relaxed(reg) & ~(bits), reg)
22aa099382SJeffy Chen #define ROCKCHIP_SPI_SET_BITS(reg, bits) \
23aa099382SJeffy Chen 		writel_relaxed(readl_relaxed(reg) | (bits), reg)
24aa099382SJeffy Chen 
2564e36824Saddy ke /* SPI register offsets */
2664e36824Saddy ke #define ROCKCHIP_SPI_CTRLR0			0x0000
2764e36824Saddy ke #define ROCKCHIP_SPI_CTRLR1			0x0004
2864e36824Saddy ke #define ROCKCHIP_SPI_SSIENR			0x0008
2964e36824Saddy ke #define ROCKCHIP_SPI_SER			0x000c
3064e36824Saddy ke #define ROCKCHIP_SPI_BAUDR			0x0010
3164e36824Saddy ke #define ROCKCHIP_SPI_TXFTLR			0x0014
3264e36824Saddy ke #define ROCKCHIP_SPI_RXFTLR			0x0018
3364e36824Saddy ke #define ROCKCHIP_SPI_TXFLR			0x001c
3464e36824Saddy ke #define ROCKCHIP_SPI_RXFLR			0x0020
3564e36824Saddy ke #define ROCKCHIP_SPI_SR				0x0024
3664e36824Saddy ke #define ROCKCHIP_SPI_IPR			0x0028
3764e36824Saddy ke #define ROCKCHIP_SPI_IMR			0x002c
3864e36824Saddy ke #define ROCKCHIP_SPI_ISR			0x0030
3964e36824Saddy ke #define ROCKCHIP_SPI_RISR			0x0034
4064e36824Saddy ke #define ROCKCHIP_SPI_ICR			0x0038
4164e36824Saddy ke #define ROCKCHIP_SPI_DMACR			0x003c
4264e36824Saddy ke #define ROCKCHIP_SPI_DMATDLR		0x0040
4364e36824Saddy ke #define ROCKCHIP_SPI_DMARDLR		0x0044
4464e36824Saddy ke #define ROCKCHIP_SPI_TXDR			0x0400
4564e36824Saddy ke #define ROCKCHIP_SPI_RXDR			0x0800
4664e36824Saddy ke 
4764e36824Saddy ke /* Bit fields in CTRLR0 */
4864e36824Saddy ke #define CR0_DFS_OFFSET				0
4965498c6aSEmil Renner Berthing #define CR0_DFS_4BIT				0x0
5065498c6aSEmil Renner Berthing #define CR0_DFS_8BIT				0x1
5165498c6aSEmil Renner Berthing #define CR0_DFS_16BIT				0x2
5264e36824Saddy ke 
5364e36824Saddy ke #define CR0_CFS_OFFSET				2
5464e36824Saddy ke 
5564e36824Saddy ke #define CR0_SCPH_OFFSET				6
5664e36824Saddy ke 
5764e36824Saddy ke #define CR0_SCPOL_OFFSET			7
5864e36824Saddy ke 
5964e36824Saddy ke #define CR0_CSM_OFFSET				8
6064e36824Saddy ke #define CR0_CSM_KEEP				0x0
6164e36824Saddy ke /* ss_n be high for half sclk_out cycles */
6264e36824Saddy ke #define CR0_CSM_HALF				0X1
6364e36824Saddy ke /* ss_n be high for one sclk_out cycle */
6464e36824Saddy ke #define CR0_CSM_ONE					0x2
6564e36824Saddy ke 
6664e36824Saddy ke /* ss_n to sclk_out delay */
6764e36824Saddy ke #define CR0_SSD_OFFSET				10
6864e36824Saddy ke /*
6964e36824Saddy ke  * The period between ss_n active and
7064e36824Saddy ke  * sclk_out active is half sclk_out cycles
7164e36824Saddy ke  */
7264e36824Saddy ke #define CR0_SSD_HALF				0x0
7364e36824Saddy ke /*
7464e36824Saddy ke  * The period between ss_n active and
7564e36824Saddy ke  * sclk_out active is one sclk_out cycle
7664e36824Saddy ke  */
7764e36824Saddy ke #define CR0_SSD_ONE					0x1
7864e36824Saddy ke 
7964e36824Saddy ke #define CR0_EM_OFFSET				11
8064e36824Saddy ke #define CR0_EM_LITTLE				0x0
8164e36824Saddy ke #define CR0_EM_BIG					0x1
8264e36824Saddy ke 
8364e36824Saddy ke #define CR0_FBM_OFFSET				12
8464e36824Saddy ke #define CR0_FBM_MSB					0x0
8564e36824Saddy ke #define CR0_FBM_LSB					0x1
8664e36824Saddy ke 
8764e36824Saddy ke #define CR0_BHT_OFFSET				13
8864e36824Saddy ke #define CR0_BHT_16BIT				0x0
8964e36824Saddy ke #define CR0_BHT_8BIT				0x1
9064e36824Saddy ke 
9164e36824Saddy ke #define CR0_RSD_OFFSET				14
9274b7efa8SEmil Renner Berthing #define CR0_RSD_MAX				0x3
9364e36824Saddy ke 
9464e36824Saddy ke #define CR0_FRF_OFFSET				16
9564e36824Saddy ke #define CR0_FRF_SPI					0x0
9664e36824Saddy ke #define CR0_FRF_SSP					0x1
9764e36824Saddy ke #define CR0_FRF_MICROWIRE			0x2
9864e36824Saddy ke 
9964e36824Saddy ke #define CR0_XFM_OFFSET				18
10064e36824Saddy ke #define CR0_XFM_MASK				(0x03 << SPI_XFM_OFFSET)
10164e36824Saddy ke #define CR0_XFM_TR					0x0
10264e36824Saddy ke #define CR0_XFM_TO					0x1
10364e36824Saddy ke #define CR0_XFM_RO					0x2
10464e36824Saddy ke 
10564e36824Saddy ke #define CR0_OPM_OFFSET				20
10664e36824Saddy ke #define CR0_OPM_MASTER				0x0
10764e36824Saddy ke #define CR0_OPM_SLAVE				0x1
10864e36824Saddy ke 
10964e36824Saddy ke #define CR0_MTM_OFFSET				0x21
11064e36824Saddy ke 
11164e36824Saddy ke /* Bit fields in SER, 2bit */
11264e36824Saddy ke #define SER_MASK					0x3
11364e36824Saddy ke 
114420b82f8SEmil Renner Berthing /* Bit fields in BAUDR */
115420b82f8SEmil Renner Berthing #define BAUDR_SCKDV_MIN				2
116420b82f8SEmil Renner Berthing #define BAUDR_SCKDV_MAX				65534
117420b82f8SEmil Renner Berthing 
11864e36824Saddy ke /* Bit fields in SR, 5bit */
11964e36824Saddy ke #define SR_MASK						0x1f
12064e36824Saddy ke #define SR_BUSY						(1 << 0)
12164e36824Saddy ke #define SR_TF_FULL					(1 << 1)
12264e36824Saddy ke #define SR_TF_EMPTY					(1 << 2)
12364e36824Saddy ke #define SR_RF_EMPTY					(1 << 3)
12464e36824Saddy ke #define SR_RF_FULL					(1 << 4)
12564e36824Saddy ke 
12664e36824Saddy ke /* Bit fields in ISR, IMR, ISR, RISR, 5bit */
12764e36824Saddy ke #define INT_MASK					0x1f
12864e36824Saddy ke #define INT_TF_EMPTY				(1 << 0)
12964e36824Saddy ke #define INT_TF_OVERFLOW				(1 << 1)
13064e36824Saddy ke #define INT_RF_UNDERFLOW			(1 << 2)
13164e36824Saddy ke #define INT_RF_OVERFLOW				(1 << 3)
13264e36824Saddy ke #define INT_RF_FULL					(1 << 4)
13364e36824Saddy ke 
13464e36824Saddy ke /* Bit fields in ICR, 4bit */
13564e36824Saddy ke #define ICR_MASK					0x0f
13664e36824Saddy ke #define ICR_ALL						(1 << 0)
13764e36824Saddy ke #define ICR_RF_UNDERFLOW			(1 << 1)
13864e36824Saddy ke #define ICR_RF_OVERFLOW				(1 << 2)
13964e36824Saddy ke #define ICR_TF_OVERFLOW				(1 << 3)
14064e36824Saddy ke 
14164e36824Saddy ke /* Bit fields in DMACR */
14264e36824Saddy ke #define RF_DMA_EN					(1 << 0)
14364e36824Saddy ke #define TF_DMA_EN					(1 << 1)
14464e36824Saddy ke 
145fab3e487SEmil Renner Berthing /* Driver state flags */
146fab3e487SEmil Renner Berthing #define RXDMA					(1 << 0)
147fab3e487SEmil Renner Berthing #define TXDMA					(1 << 1)
14864e36824Saddy ke 
149f9cfd522SAddy Ke /* sclk_out: spi master internal logic in rk3x can support 50Mhz */
150420b82f8SEmil Renner Berthing #define MAX_SCLK_OUT				50000000U
151f9cfd522SAddy Ke 
1525185a81cSBrian Norris /*
1535185a81cSBrian Norris  * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However,
1545185a81cSBrian Norris  * the controller seems to hang when given 0x10000, so stick with this for now.
1555185a81cSBrian Norris  */
1565185a81cSBrian Norris #define ROCKCHIP_SPI_MAX_TRANLEN		0xffff
1575185a81cSBrian Norris 
158aa099382SJeffy Chen #define ROCKCHIP_SPI_MAX_CS_NUM			2
159aa099382SJeffy Chen 
16064e36824Saddy ke struct rockchip_spi {
16164e36824Saddy ke 	struct device *dev;
16264e36824Saddy ke 
16364e36824Saddy ke 	struct clk *spiclk;
16464e36824Saddy ke 	struct clk *apb_pclk;
16564e36824Saddy ke 
16664e36824Saddy ke 	void __iomem *regs;
167eee06a9eSEmil Renner Berthing 	dma_addr_t dma_addr_rx;
168eee06a9eSEmil Renner Berthing 	dma_addr_t dma_addr_tx;
169fab3e487SEmil Renner Berthing 
17001b59ce5SEmil Renner Berthing 	const void *tx;
17101b59ce5SEmil Renner Berthing 	void *rx;
17201b59ce5SEmil Renner Berthing 	unsigned int tx_left;
17301b59ce5SEmil Renner Berthing 	unsigned int rx_left;
17401b59ce5SEmil Renner Berthing 
175fab3e487SEmil Renner Berthing 	atomic_t state;
176fab3e487SEmil Renner Berthing 
17764e36824Saddy ke 	/*depth of the FIFO buffer */
17864e36824Saddy ke 	u32 fifo_len;
179420b82f8SEmil Renner Berthing 	/* frequency of spiclk */
180420b82f8SEmil Renner Berthing 	u32 freq;
18164e36824Saddy ke 
18264e36824Saddy ke 	u8 n_bytes;
18374b7efa8SEmil Renner Berthing 	u8 rsd;
18464e36824Saddy ke 
185aa099382SJeffy Chen 	bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
18664e36824Saddy ke };
18764e36824Saddy ke 
18830688e4eSEmil Renner Berthing static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
18964e36824Saddy ke {
19030688e4eSEmil Renner Berthing 	writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR);
19164e36824Saddy ke }
19264e36824Saddy ke 
1932df08e78SAddy Ke static inline void wait_for_idle(struct rockchip_spi *rs)
1942df08e78SAddy Ke {
1952df08e78SAddy Ke 	unsigned long timeout = jiffies + msecs_to_jiffies(5);
1962df08e78SAddy Ke 
1972df08e78SAddy Ke 	do {
1982df08e78SAddy Ke 		if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
1992df08e78SAddy Ke 			return;
20064bc0110SDoug Anderson 	} while (!time_after(jiffies, timeout));
2012df08e78SAddy Ke 
2022df08e78SAddy Ke 	dev_warn(rs->dev, "spi controller is in busy state!\n");
2032df08e78SAddy Ke }
2042df08e78SAddy Ke 
20564e36824Saddy ke static u32 get_fifo_len(struct rockchip_spi *rs)
20664e36824Saddy ke {
20764e36824Saddy ke 	u32 fifo;
20864e36824Saddy ke 
20964e36824Saddy ke 	for (fifo = 2; fifo < 32; fifo++) {
21064e36824Saddy ke 		writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR);
21164e36824Saddy ke 		if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR))
21264e36824Saddy ke 			break;
21364e36824Saddy ke 	}
21464e36824Saddy ke 
21564e36824Saddy ke 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR);
21664e36824Saddy ke 
21764e36824Saddy ke 	return (fifo == 31) ? 0 : fifo;
21864e36824Saddy ke }
21964e36824Saddy ke 
22064e36824Saddy ke static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
22164e36824Saddy ke {
222*d66571a2SChris Ruehl 	struct spi_controller *ctlr = spi->controller;
223*d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
224aa099382SJeffy Chen 	bool cs_asserted = !enable;
225b920cc31SHuibin Hong 
226aa099382SJeffy Chen 	/* Return immediately for no-op */
227aa099382SJeffy Chen 	if (cs_asserted == rs->cs_asserted[spi->chip_select])
228aa099382SJeffy Chen 		return;
229aa099382SJeffy Chen 
230aa099382SJeffy Chen 	if (cs_asserted) {
231aa099382SJeffy Chen 		/* Keep things powered as long as CS is asserted */
232b920cc31SHuibin Hong 		pm_runtime_get_sync(rs->dev);
23364e36824Saddy ke 
234aa099382SJeffy Chen 		ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER,
235aa099382SJeffy Chen 				      BIT(spi->chip_select));
236aa099382SJeffy Chen 	} else {
237aa099382SJeffy Chen 		ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER,
238aa099382SJeffy Chen 				      BIT(spi->chip_select));
23964e36824Saddy ke 
240aa099382SJeffy Chen 		/* Drop reference from when we first asserted CS */
241aa099382SJeffy Chen 		pm_runtime_put(rs->dev);
242aa099382SJeffy Chen 	}
24364e36824Saddy ke 
244aa099382SJeffy Chen 	rs->cs_asserted[spi->chip_select] = cs_asserted;
24564e36824Saddy ke }
24664e36824Saddy ke 
247*d66571a2SChris Ruehl static void rockchip_spi_handle_err(struct spi_controller *ctlr,
24864e36824Saddy ke 				    struct spi_message *msg)
24964e36824Saddy ke {
250*d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
25164e36824Saddy ke 
252ce386100SEmil Renner Berthing 	/* stop running spi transfer
253ce386100SEmil Renner Berthing 	 * this also flushes both rx and tx fifos
2545dcc44edSAddy Ke 	 */
255ce386100SEmil Renner Berthing 	spi_enable_chip(rs, false);
256ce386100SEmil Renner Berthing 
25701b59ce5SEmil Renner Berthing 	/* make sure all interrupts are masked */
25801b59ce5SEmil Renner Berthing 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
25901b59ce5SEmil Renner Berthing 
260fab3e487SEmil Renner Berthing 	if (atomic_read(&rs->state) & TXDMA)
261*d66571a2SChris Ruehl 		dmaengine_terminate_async(ctlr->dma_tx);
262fab3e487SEmil Renner Berthing 
263ce386100SEmil Renner Berthing 	if (atomic_read(&rs->state) & RXDMA)
264*d66571a2SChris Ruehl 		dmaengine_terminate_async(ctlr->dma_rx);
26564e36824Saddy ke }
26664e36824Saddy ke 
26764e36824Saddy ke static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
26864e36824Saddy ke {
26901b59ce5SEmil Renner Berthing 	u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
27001b59ce5SEmil Renner Berthing 	u32 words = min(rs->tx_left, tx_free);
27164e36824Saddy ke 
27201b59ce5SEmil Renner Berthing 	rs->tx_left -= words;
27301b59ce5SEmil Renner Berthing 	for (; words; words--) {
27401b59ce5SEmil Renner Berthing 		u32 txw;
27501b59ce5SEmil Renner Berthing 
27664e36824Saddy ke 		if (rs->n_bytes == 1)
27701b59ce5SEmil Renner Berthing 			txw = *(u8 *)rs->tx;
27864e36824Saddy ke 		else
27901b59ce5SEmil Renner Berthing 			txw = *(u16 *)rs->tx;
28064e36824Saddy ke 
28164e36824Saddy ke 		writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
28264e36824Saddy ke 		rs->tx += rs->n_bytes;
28364e36824Saddy ke 	}
28464e36824Saddy ke }
28564e36824Saddy ke 
28664e36824Saddy ke static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
28764e36824Saddy ke {
28801b59ce5SEmil Renner Berthing 	u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
28901b59ce5SEmil Renner Berthing 	u32 rx_left = rs->rx_left - words;
29064e36824Saddy ke 
29101b59ce5SEmil Renner Berthing 	/* the hardware doesn't allow us to change fifo threshold
29201b59ce5SEmil Renner Berthing 	 * level while spi is enabled, so instead make sure to leave
29301b59ce5SEmil Renner Berthing 	 * enough words in the rx fifo to get the last interrupt
29401b59ce5SEmil Renner Berthing 	 * exactly when all words have been received
29501b59ce5SEmil Renner Berthing 	 */
29601b59ce5SEmil Renner Berthing 	if (rx_left) {
29701b59ce5SEmil Renner Berthing 		u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1;
29801b59ce5SEmil Renner Berthing 
29901b59ce5SEmil Renner Berthing 		if (rx_left < ftl) {
30001b59ce5SEmil Renner Berthing 			rx_left = ftl;
30101b59ce5SEmil Renner Berthing 			words = rs->rx_left - rx_left;
30201b59ce5SEmil Renner Berthing 		}
30301b59ce5SEmil Renner Berthing 	}
30401b59ce5SEmil Renner Berthing 
30501b59ce5SEmil Renner Berthing 	rs->rx_left = rx_left;
30601b59ce5SEmil Renner Berthing 	for (; words; words--) {
30701b59ce5SEmil Renner Berthing 		u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
30801b59ce5SEmil Renner Berthing 
30901b59ce5SEmil Renner Berthing 		if (!rs->rx)
31001b59ce5SEmil Renner Berthing 			continue;
31101b59ce5SEmil Renner Berthing 
31264e36824Saddy ke 		if (rs->n_bytes == 1)
31301b59ce5SEmil Renner Berthing 			*(u8 *)rs->rx = (u8)rxw;
31464e36824Saddy ke 		else
31501b59ce5SEmil Renner Berthing 			*(u16 *)rs->rx = (u16)rxw;
31664e36824Saddy ke 		rs->rx += rs->n_bytes;
3175dcc44edSAddy Ke 	}
31864e36824Saddy ke }
31964e36824Saddy ke 
32001b59ce5SEmil Renner Berthing static irqreturn_t rockchip_spi_isr(int irq, void *dev_id)
32164e36824Saddy ke {
322*d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_id;
323*d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
32464e36824Saddy ke 
32501b59ce5SEmil Renner Berthing 	if (rs->tx_left)
32601b59ce5SEmil Renner Berthing 		rockchip_spi_pio_writer(rs);
32701b59ce5SEmil Renner Berthing 
32801b59ce5SEmil Renner Berthing 	rockchip_spi_pio_reader(rs);
32901b59ce5SEmil Renner Berthing 	if (!rs->rx_left) {
33001b59ce5SEmil Renner Berthing 		spi_enable_chip(rs, false);
33101b59ce5SEmil Renner Berthing 		writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
332*d66571a2SChris Ruehl 		spi_finalize_current_transfer(ctlr);
33301b59ce5SEmil Renner Berthing 	}
33401b59ce5SEmil Renner Berthing 
33501b59ce5SEmil Renner Berthing 	return IRQ_HANDLED;
33601b59ce5SEmil Renner Berthing }
33701b59ce5SEmil Renner Berthing 
33801b59ce5SEmil Renner Berthing static int rockchip_spi_prepare_irq(struct rockchip_spi *rs,
33901b59ce5SEmil Renner Berthing 		struct spi_transfer *xfer)
34001b59ce5SEmil Renner Berthing {
34101b59ce5SEmil Renner Berthing 	rs->tx = xfer->tx_buf;
34201b59ce5SEmil Renner Berthing 	rs->rx = xfer->rx_buf;
34301b59ce5SEmil Renner Berthing 	rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0;
34401b59ce5SEmil Renner Berthing 	rs->rx_left = xfer->len / rs->n_bytes;
34501b59ce5SEmil Renner Berthing 
34601b59ce5SEmil Renner Berthing 	writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR);
34730688e4eSEmil Renner Berthing 	spi_enable_chip(rs, true);
348a3c17402SEmil Renner Berthing 
34901b59ce5SEmil Renner Berthing 	if (rs->tx_left)
35064e36824Saddy ke 		rockchip_spi_pio_writer(rs);
35164e36824Saddy ke 
35201b59ce5SEmil Renner Berthing 	/* 1 means the transfer is in progress */
35301b59ce5SEmil Renner Berthing 	return 1;
35464e36824Saddy ke }
35564e36824Saddy ke 
35664e36824Saddy ke static void rockchip_spi_dma_rxcb(void *data)
35764e36824Saddy ke {
358*d66571a2SChris Ruehl 	struct spi_controller *ctlr = data;
359*d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
360fab3e487SEmil Renner Berthing 	int state = atomic_fetch_andnot(RXDMA, &rs->state);
36164e36824Saddy ke 
362fab3e487SEmil Renner Berthing 	if (state & TXDMA)
363fab3e487SEmil Renner Berthing 		return;
36464e36824Saddy ke 
36530688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
366*d66571a2SChris Ruehl 	spi_finalize_current_transfer(ctlr);
367c28be31bSAddy Ke }
36864e36824Saddy ke 
36964e36824Saddy ke static void rockchip_spi_dma_txcb(void *data)
37064e36824Saddy ke {
371*d66571a2SChris Ruehl 	struct spi_controller *ctlr = data;
372*d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
373fab3e487SEmil Renner Berthing 	int state = atomic_fetch_andnot(TXDMA, &rs->state);
374fab3e487SEmil Renner Berthing 
375fab3e487SEmil Renner Berthing 	if (state & RXDMA)
376fab3e487SEmil Renner Berthing 		return;
37764e36824Saddy ke 
3782df08e78SAddy Ke 	/* Wait until the FIFO data completely. */
3792df08e78SAddy Ke 	wait_for_idle(rs);
3802df08e78SAddy Ke 
38130688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
382*d66571a2SChris Ruehl 	spi_finalize_current_transfer(ctlr);
3832c2bc748SAddy Ke }
38464e36824Saddy ke 
385fc1ad8eeSEmil Renner Berthing static int rockchip_spi_prepare_dma(struct rockchip_spi *rs,
386*d66571a2SChris Ruehl 		struct spi_controller *ctlr, struct spi_transfer *xfer)
38764e36824Saddy ke {
38864e36824Saddy ke 	struct dma_async_tx_descriptor *rxdesc, *txdesc;
38964e36824Saddy ke 
390fab3e487SEmil Renner Berthing 	atomic_set(&rs->state, 0);
39164e36824Saddy ke 
39297cf5669SArnd Bergmann 	rxdesc = NULL;
393fc1ad8eeSEmil Renner Berthing 	if (xfer->rx_buf) {
39431bcb57bSEmil Renner Berthing 		struct dma_slave_config rxconf = {
39531bcb57bSEmil Renner Berthing 			.direction = DMA_DEV_TO_MEM,
396eee06a9eSEmil Renner Berthing 			.src_addr = rs->dma_addr_rx,
39731bcb57bSEmil Renner Berthing 			.src_addr_width = rs->n_bytes,
39831bcb57bSEmil Renner Berthing 			.src_maxburst = 1,
39931bcb57bSEmil Renner Berthing 		};
40031bcb57bSEmil Renner Berthing 
401*d66571a2SChris Ruehl 		dmaengine_slave_config(ctlr->dma_rx, &rxconf);
40264e36824Saddy ke 
4035dcc44edSAddy Ke 		rxdesc = dmaengine_prep_slave_sg(
404*d66571a2SChris Ruehl 				ctlr->dma_rx,
405fc1ad8eeSEmil Renner Berthing 				xfer->rx_sg.sgl, xfer->rx_sg.nents,
406d9071b7eSEmil Renner Berthing 				DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
407ea984911SShawn Lin 		if (!rxdesc)
408ea984911SShawn Lin 			return -EINVAL;
40964e36824Saddy ke 
41064e36824Saddy ke 		rxdesc->callback = rockchip_spi_dma_rxcb;
411*d66571a2SChris Ruehl 		rxdesc->callback_param = ctlr;
41264e36824Saddy ke 	}
41364e36824Saddy ke 
41497cf5669SArnd Bergmann 	txdesc = NULL;
415fc1ad8eeSEmil Renner Berthing 	if (xfer->tx_buf) {
41631bcb57bSEmil Renner Berthing 		struct dma_slave_config txconf = {
41731bcb57bSEmil Renner Berthing 			.direction = DMA_MEM_TO_DEV,
418eee06a9eSEmil Renner Berthing 			.dst_addr = rs->dma_addr_tx,
41931bcb57bSEmil Renner Berthing 			.dst_addr_width = rs->n_bytes,
42047300728SEmil Renner Berthing 			.dst_maxburst = rs->fifo_len / 4,
42131bcb57bSEmil Renner Berthing 		};
42231bcb57bSEmil Renner Berthing 
423*d66571a2SChris Ruehl 		dmaengine_slave_config(ctlr->dma_tx, &txconf);
42464e36824Saddy ke 
4255dcc44edSAddy Ke 		txdesc = dmaengine_prep_slave_sg(
426*d66571a2SChris Ruehl 				ctlr->dma_tx,
427fc1ad8eeSEmil Renner Berthing 				xfer->tx_sg.sgl, xfer->tx_sg.nents,
428d9071b7eSEmil Renner Berthing 				DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
429ea984911SShawn Lin 		if (!txdesc) {
430ea984911SShawn Lin 			if (rxdesc)
431*d66571a2SChris Ruehl 				dmaengine_terminate_sync(ctlr->dma_rx);
432ea984911SShawn Lin 			return -EINVAL;
433ea984911SShawn Lin 		}
43464e36824Saddy ke 
43564e36824Saddy ke 		txdesc->callback = rockchip_spi_dma_txcb;
436*d66571a2SChris Ruehl 		txdesc->callback_param = ctlr;
43764e36824Saddy ke 	}
43864e36824Saddy ke 
43964e36824Saddy ke 	/* rx must be started before tx due to spi instinct */
44097cf5669SArnd Bergmann 	if (rxdesc) {
441fab3e487SEmil Renner Berthing 		atomic_or(RXDMA, &rs->state);
44264e36824Saddy ke 		dmaengine_submit(rxdesc);
443*d66571a2SChris Ruehl 		dma_async_issue_pending(ctlr->dma_rx);
44464e36824Saddy ke 	}
44564e36824Saddy ke 
44630688e4eSEmil Renner Berthing 	spi_enable_chip(rs, true);
447a3c17402SEmil Renner Berthing 
44897cf5669SArnd Bergmann 	if (txdesc) {
449fab3e487SEmil Renner Berthing 		atomic_or(TXDMA, &rs->state);
45064e36824Saddy ke 		dmaengine_submit(txdesc);
451*d66571a2SChris Ruehl 		dma_async_issue_pending(ctlr->dma_tx);
45264e36824Saddy ke 	}
453ea984911SShawn Lin 
454a3c17402SEmil Renner Berthing 	/* 1 means the transfer is in progress */
455a3c17402SEmil Renner Berthing 	return 1;
45664e36824Saddy ke }
45764e36824Saddy ke 
458fc1ad8eeSEmil Renner Berthing static void rockchip_spi_config(struct rockchip_spi *rs,
459eff0275eSEmil Renner Berthing 		struct spi_device *spi, struct spi_transfer *xfer,
460eff0275eSEmil Renner Berthing 		bool use_dma)
46164e36824Saddy ke {
4622410d6a3SEmil Renner Berthing 	u32 cr0 = CR0_FRF_SPI  << CR0_FRF_OFFSET
4632410d6a3SEmil Renner Berthing 	        | CR0_BHT_8BIT << CR0_BHT_OFFSET
4642410d6a3SEmil Renner Berthing 	        | CR0_SSD_ONE  << CR0_SSD_OFFSET
4652410d6a3SEmil Renner Berthing 	        | CR0_EM_BIG   << CR0_EM_OFFSET;
46665498c6aSEmil Renner Berthing 	u32 cr1;
46765498c6aSEmil Renner Berthing 	u32 dmacr = 0;
46864e36824Saddy ke 
46974b7efa8SEmil Renner Berthing 	cr0 |= rs->rsd << CR0_RSD_OFFSET;
470fc1ad8eeSEmil Renner Berthing 	cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET;
47104290192SEmil Renner Berthing 	if (spi->mode & SPI_LSB_FIRST)
47204290192SEmil Renner Berthing 		cr0 |= CR0_FBM_LSB << CR0_FBM_OFFSET;
473fc1ad8eeSEmil Renner Berthing 
474fc1ad8eeSEmil Renner Berthing 	if (xfer->rx_buf && xfer->tx_buf)
475fc1ad8eeSEmil Renner Berthing 		cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET;
476fc1ad8eeSEmil Renner Berthing 	else if (xfer->rx_buf)
477fc1ad8eeSEmil Renner Berthing 		cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET;
47801b59ce5SEmil Renner Berthing 	else if (use_dma)
479fc1ad8eeSEmil Renner Berthing 		cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET;
48064e36824Saddy ke 
48165498c6aSEmil Renner Berthing 	switch (xfer->bits_per_word) {
48265498c6aSEmil Renner Berthing 	case 4:
48365498c6aSEmil Renner Berthing 		cr0 |= CR0_DFS_4BIT << CR0_DFS_OFFSET;
48465498c6aSEmil Renner Berthing 		cr1 = xfer->len - 1;
48565498c6aSEmil Renner Berthing 		break;
48665498c6aSEmil Renner Berthing 	case 8:
48765498c6aSEmil Renner Berthing 		cr0 |= CR0_DFS_8BIT << CR0_DFS_OFFSET;
48865498c6aSEmil Renner Berthing 		cr1 = xfer->len - 1;
48965498c6aSEmil Renner Berthing 		break;
49065498c6aSEmil Renner Berthing 	case 16:
49165498c6aSEmil Renner Berthing 		cr0 |= CR0_DFS_16BIT << CR0_DFS_OFFSET;
49265498c6aSEmil Renner Berthing 		cr1 = xfer->len / 2 - 1;
49365498c6aSEmil Renner Berthing 		break;
49465498c6aSEmil Renner Berthing 	default:
49565498c6aSEmil Renner Berthing 		/* we only whitelist 4, 8 and 16 bit words in
496*d66571a2SChris Ruehl 		 * ctlr->bits_per_word_mask, so this shouldn't
49765498c6aSEmil Renner Berthing 		 * happen
49865498c6aSEmil Renner Berthing 		 */
49965498c6aSEmil Renner Berthing 		unreachable();
50065498c6aSEmil Renner Berthing 	}
50165498c6aSEmil Renner Berthing 
502eff0275eSEmil Renner Berthing 	if (use_dma) {
503fc1ad8eeSEmil Renner Berthing 		if (xfer->tx_buf)
50464e36824Saddy ke 			dmacr |= TF_DMA_EN;
505fc1ad8eeSEmil Renner Berthing 		if (xfer->rx_buf)
50664e36824Saddy ke 			dmacr |= RF_DMA_EN;
50764e36824Saddy ke 	}
50864e36824Saddy ke 
50964e36824Saddy ke 	writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
51065498c6aSEmil Renner Berthing 	writel_relaxed(cr1, rs->regs + ROCKCHIP_SPI_CTRLR1);
51104b37d2dSHuibin Hong 
51201b59ce5SEmil Renner Berthing 	/* unfortunately setting the fifo threshold level to generate an
51301b59ce5SEmil Renner Berthing 	 * interrupt exactly when the fifo is full doesn't seem to work,
51401b59ce5SEmil Renner Berthing 	 * so we need the strict inequality here
51501b59ce5SEmil Renner Berthing 	 */
51601b59ce5SEmil Renner Berthing 	if (xfer->len < rs->fifo_len)
51701b59ce5SEmil Renner Berthing 		writel_relaxed(xfer->len - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
51801b59ce5SEmil Renner Berthing 	else
51964e36824Saddy ke 		writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
52064e36824Saddy ke 
52147300728SEmil Renner Berthing 	writel_relaxed(rs->fifo_len / 2, rs->regs + ROCKCHIP_SPI_DMATDLR);
52264e36824Saddy ke 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR);
52364e36824Saddy ke 	writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
52464e36824Saddy ke 
525420b82f8SEmil Renner Berthing 	/* the hardware only supports an even clock divisor, so
526420b82f8SEmil Renner Berthing 	 * round divisor = spiclk / speed up to nearest even number
527420b82f8SEmil Renner Berthing 	 * so that the resulting speed is <= the requested speed
528420b82f8SEmil Renner Berthing 	 */
529420b82f8SEmil Renner Berthing 	writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz),
530420b82f8SEmil Renner Berthing 			rs->regs + ROCKCHIP_SPI_BAUDR);
53164e36824Saddy ke }
53264e36824Saddy ke 
5335185a81cSBrian Norris static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
5345185a81cSBrian Norris {
5355185a81cSBrian Norris 	return ROCKCHIP_SPI_MAX_TRANLEN;
5365185a81cSBrian Norris }
5375185a81cSBrian Norris 
5385dcc44edSAddy Ke static int rockchip_spi_transfer_one(
539*d66571a2SChris Ruehl 		struct spi_controller *ctlr,
54064e36824Saddy ke 		struct spi_device *spi,
54164e36824Saddy ke 		struct spi_transfer *xfer)
54264e36824Saddy ke {
543*d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
544eff0275eSEmil Renner Berthing 	bool use_dma;
54564e36824Saddy ke 
54662946172SDoug Anderson 	WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
54762946172SDoug Anderson 		(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
54864e36824Saddy ke 
54964e36824Saddy ke 	if (!xfer->tx_buf && !xfer->rx_buf) {
55064e36824Saddy ke 		dev_err(rs->dev, "No buffer for transfer\n");
55164e36824Saddy ke 		return -EINVAL;
55264e36824Saddy ke 	}
55364e36824Saddy ke 
5545185a81cSBrian Norris 	if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
5555185a81cSBrian Norris 		dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
5565185a81cSBrian Norris 		return -EINVAL;
5575185a81cSBrian Norris 	}
5585185a81cSBrian Norris 
55965498c6aSEmil Renner Berthing 	rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2;
56064e36824Saddy ke 
561*d66571a2SChris Ruehl 	use_dma = ctlr->can_dma ? ctlr->can_dma(ctlr, spi, xfer) : false;
56264e36824Saddy ke 
563eff0275eSEmil Renner Berthing 	rockchip_spi_config(rs, spi, xfer, use_dma);
56464e36824Saddy ke 
565eff0275eSEmil Renner Berthing 	if (use_dma)
566*d66571a2SChris Ruehl 		return rockchip_spi_prepare_dma(rs, ctlr, xfer);
56764e36824Saddy ke 
56801b59ce5SEmil Renner Berthing 	return rockchip_spi_prepare_irq(rs, xfer);
56964e36824Saddy ke }
57064e36824Saddy ke 
571*d66571a2SChris Ruehl static bool rockchip_spi_can_dma(struct spi_controller *ctlr,
57264e36824Saddy ke 				 struct spi_device *spi,
57364e36824Saddy ke 				 struct spi_transfer *xfer)
57464e36824Saddy ke {
575*d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
57601b59ce5SEmil Renner Berthing 	unsigned int bytes_per_word = xfer->bits_per_word <= 8 ? 1 : 2;
57764e36824Saddy ke 
57801b59ce5SEmil Renner Berthing 	/* if the numbor of spi words to transfer is less than the fifo
57901b59ce5SEmil Renner Berthing 	 * length we can just fill the fifo and wait for a single irq,
58001b59ce5SEmil Renner Berthing 	 * so don't bother setting up dma
58101b59ce5SEmil Renner Berthing 	 */
58201b59ce5SEmil Renner Berthing 	return xfer->len / bytes_per_word >= rs->fifo_len;
58364e36824Saddy ke }
58464e36824Saddy ke 
58564e36824Saddy ke static int rockchip_spi_probe(struct platform_device *pdev)
58664e36824Saddy ke {
58743de979dSJeffy Chen 	int ret;
58864e36824Saddy ke 	struct rockchip_spi *rs;
589*d66571a2SChris Ruehl 	struct spi_controller *ctlr;
59064e36824Saddy ke 	struct resource *mem;
59176b17e6eSJulius Werner 	u32 rsd_nsecs;
59264e36824Saddy ke 
593*d66571a2SChris Ruehl 	ctlr = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi));
594*d66571a2SChris Ruehl 	if (!ctlr)
59564e36824Saddy ke 		return -ENOMEM;
5965dcc44edSAddy Ke 
597*d66571a2SChris Ruehl 	platform_set_drvdata(pdev, ctlr);
59864e36824Saddy ke 
599*d66571a2SChris Ruehl 	rs = spi_controller_get_devdata(ctlr);
60064e36824Saddy ke 
60164e36824Saddy ke 	/* Get basic io resource and map it */
60264e36824Saddy ke 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
60364e36824Saddy ke 	rs->regs = devm_ioremap_resource(&pdev->dev, mem);
60464e36824Saddy ke 	if (IS_ERR(rs->regs)) {
60564e36824Saddy ke 		ret =  PTR_ERR(rs->regs);
606*d66571a2SChris Ruehl 		goto err_put_ctlr;
60764e36824Saddy ke 	}
60864e36824Saddy ke 
60964e36824Saddy ke 	rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
61064e36824Saddy ke 	if (IS_ERR(rs->apb_pclk)) {
61164e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get apb_pclk\n");
61264e36824Saddy ke 		ret = PTR_ERR(rs->apb_pclk);
613*d66571a2SChris Ruehl 		goto err_put_ctlr;
61464e36824Saddy ke 	}
61564e36824Saddy ke 
61664e36824Saddy ke 	rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
61764e36824Saddy ke 	if (IS_ERR(rs->spiclk)) {
61864e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get spi_pclk\n");
61964e36824Saddy ke 		ret = PTR_ERR(rs->spiclk);
620*d66571a2SChris Ruehl 		goto err_put_ctlr;
62164e36824Saddy ke 	}
62264e36824Saddy ke 
62364e36824Saddy ke 	ret = clk_prepare_enable(rs->apb_pclk);
62443de979dSJeffy Chen 	if (ret < 0) {
62564e36824Saddy ke 		dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
626*d66571a2SChris Ruehl 		goto err_put_ctlr;
62764e36824Saddy ke 	}
62864e36824Saddy ke 
62964e36824Saddy ke 	ret = clk_prepare_enable(rs->spiclk);
63043de979dSJeffy Chen 	if (ret < 0) {
63164e36824Saddy ke 		dev_err(&pdev->dev, "Failed to enable spi_clk\n");
632c351587eSJeffy Chen 		goto err_disable_apbclk;
63364e36824Saddy ke 	}
63464e36824Saddy ke 
63530688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
63664e36824Saddy ke 
63701b59ce5SEmil Renner Berthing 	ret = platform_get_irq(pdev, 0);
63801b59ce5SEmil Renner Berthing 	if (ret < 0)
63901b59ce5SEmil Renner Berthing 		goto err_disable_spiclk;
64001b59ce5SEmil Renner Berthing 
64101b59ce5SEmil Renner Berthing 	ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL,
642*d66571a2SChris Ruehl 			IRQF_ONESHOT, dev_name(&pdev->dev), ctlr);
64301b59ce5SEmil Renner Berthing 	if (ret)
64401b59ce5SEmil Renner Berthing 		goto err_disable_spiclk;
64501b59ce5SEmil Renner Berthing 
64664e36824Saddy ke 	rs->dev = &pdev->dev;
647420b82f8SEmil Renner Berthing 	rs->freq = clk_get_rate(rs->spiclk);
64864e36824Saddy ke 
64976b17e6eSJulius Werner 	if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
65074b7efa8SEmil Renner Berthing 				  &rsd_nsecs)) {
65174b7efa8SEmil Renner Berthing 		/* rx sample delay is expressed in parent clock cycles (max 3) */
65274b7efa8SEmil Renner Berthing 		u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8),
65374b7efa8SEmil Renner Berthing 				1000000000 >> 8);
65474b7efa8SEmil Renner Berthing 		if (!rsd) {
65574b7efa8SEmil Renner Berthing 			dev_warn(rs->dev, "%u Hz are too slow to express %u ns delay\n",
65674b7efa8SEmil Renner Berthing 					rs->freq, rsd_nsecs);
65774b7efa8SEmil Renner Berthing 		} else if (rsd > CR0_RSD_MAX) {
65874b7efa8SEmil Renner Berthing 			rsd = CR0_RSD_MAX;
65974b7efa8SEmil Renner Berthing 			dev_warn(rs->dev, "%u Hz are too fast to express %u ns delay, clamping at %u ns\n",
66074b7efa8SEmil Renner Berthing 					rs->freq, rsd_nsecs,
66174b7efa8SEmil Renner Berthing 					CR0_RSD_MAX * 1000000000U / rs->freq);
66274b7efa8SEmil Renner Berthing 		}
66374b7efa8SEmil Renner Berthing 		rs->rsd = rsd;
66474b7efa8SEmil Renner Berthing 	}
66576b17e6eSJulius Werner 
66664e36824Saddy ke 	rs->fifo_len = get_fifo_len(rs);
66764e36824Saddy ke 	if (!rs->fifo_len) {
66864e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get fifo length\n");
669db7e8d90SWei Yongjun 		ret = -EINVAL;
670c351587eSJeffy Chen 		goto err_disable_spiclk;
67164e36824Saddy ke 	}
67264e36824Saddy ke 
67364e36824Saddy ke 	pm_runtime_set_active(&pdev->dev);
67464e36824Saddy ke 	pm_runtime_enable(&pdev->dev);
67564e36824Saddy ke 
676*d66571a2SChris Ruehl 	ctlr->auto_runtime_pm = true;
677*d66571a2SChris Ruehl 	ctlr->bus_num = pdev->id;
678*d66571a2SChris Ruehl 	ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST;
679*d66571a2SChris Ruehl 	ctlr->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM;
680*d66571a2SChris Ruehl 	ctlr->dev.of_node = pdev->dev.of_node;
681*d66571a2SChris Ruehl 	ctlr->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4);
682*d66571a2SChris Ruehl 	ctlr->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX;
683*d66571a2SChris Ruehl 	ctlr->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT);
68464e36824Saddy ke 
685*d66571a2SChris Ruehl 	ctlr->set_cs = rockchip_spi_set_cs;
686*d66571a2SChris Ruehl 	ctlr->transfer_one = rockchip_spi_transfer_one;
687*d66571a2SChris Ruehl 	ctlr->max_transfer_size = rockchip_spi_max_transfer_size;
688*d66571a2SChris Ruehl 	ctlr->handle_err = rockchip_spi_handle_err;
689*d66571a2SChris Ruehl 	ctlr->flags = SPI_MASTER_GPIO_SS;
69064e36824Saddy ke 
691*d66571a2SChris Ruehl 	ctlr->dma_tx = dma_request_chan(rs->dev, "tx");
692*d66571a2SChris Ruehl 	if (IS_ERR(ctlr->dma_tx)) {
69361cadcf4SShawn Lin 		/* Check tx to see if we need defer probing driver */
694*d66571a2SChris Ruehl 		if (PTR_ERR(ctlr->dma_tx) == -EPROBE_DEFER) {
69561cadcf4SShawn Lin 			ret = -EPROBE_DEFER;
696c351587eSJeffy Chen 			goto err_disable_pm_runtime;
69761cadcf4SShawn Lin 		}
69864e36824Saddy ke 		dev_warn(rs->dev, "Failed to request TX DMA channel\n");
699*d66571a2SChris Ruehl 		ctlr->dma_tx = NULL;
70064e36824Saddy ke 	}
701e4c0e06fSShawn Lin 
702*d66571a2SChris Ruehl 	ctlr->dma_rx = dma_request_chan(rs->dev, "rx");
703*d66571a2SChris Ruehl 	if (IS_ERR(ctlr->dma_rx)) {
704*d66571a2SChris Ruehl 		if (PTR_ERR(ctlr->dma_rx) == -EPROBE_DEFER) {
705e4c0e06fSShawn Lin 			ret = -EPROBE_DEFER;
7065de7ed0cSDan Carpenter 			goto err_free_dma_tx;
707e4c0e06fSShawn Lin 		}
70864e36824Saddy ke 		dev_warn(rs->dev, "Failed to request RX DMA channel\n");
709*d66571a2SChris Ruehl 		ctlr->dma_rx = NULL;
71064e36824Saddy ke 	}
71164e36824Saddy ke 
712*d66571a2SChris Ruehl 	if (ctlr->dma_tx && ctlr->dma_rx) {
713eee06a9eSEmil Renner Berthing 		rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR;
714eee06a9eSEmil Renner Berthing 		rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR;
715*d66571a2SChris Ruehl 		ctlr->can_dma = rockchip_spi_can_dma;
71664e36824Saddy ke 	}
71764e36824Saddy ke 
718*d66571a2SChris Ruehl 	ret = devm_spi_register_controller(&pdev->dev, ctlr);
71943de979dSJeffy Chen 	if (ret < 0) {
720*d66571a2SChris Ruehl 		dev_err(&pdev->dev, "Failed to register controller\n");
721c351587eSJeffy Chen 		goto err_free_dma_rx;
72264e36824Saddy ke 	}
72364e36824Saddy ke 
72464e36824Saddy ke 	return 0;
72564e36824Saddy ke 
726c351587eSJeffy Chen err_free_dma_rx:
727*d66571a2SChris Ruehl 	if (ctlr->dma_rx)
728*d66571a2SChris Ruehl 		dma_release_channel(ctlr->dma_rx);
7295de7ed0cSDan Carpenter err_free_dma_tx:
730*d66571a2SChris Ruehl 	if (ctlr->dma_tx)
731*d66571a2SChris Ruehl 		dma_release_channel(ctlr->dma_tx);
732c351587eSJeffy Chen err_disable_pm_runtime:
733c351587eSJeffy Chen 	pm_runtime_disable(&pdev->dev);
734c351587eSJeffy Chen err_disable_spiclk:
73564e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
736c351587eSJeffy Chen err_disable_apbclk:
73764e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
738*d66571a2SChris Ruehl err_put_ctlr:
739*d66571a2SChris Ruehl 	spi_controller_put(ctlr);
74064e36824Saddy ke 
74164e36824Saddy ke 	return ret;
74264e36824Saddy ke }
74364e36824Saddy ke 
74464e36824Saddy ke static int rockchip_spi_remove(struct platform_device *pdev)
74564e36824Saddy ke {
746*d66571a2SChris Ruehl 	struct spi_controller *ctlr = spi_controller_get(platform_get_drvdata(pdev));
747*d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
74864e36824Saddy ke 
7496a06e895SJeffy Chen 	pm_runtime_get_sync(&pdev->dev);
75064e36824Saddy ke 
75164e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
75264e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
75364e36824Saddy ke 
7546a06e895SJeffy Chen 	pm_runtime_put_noidle(&pdev->dev);
7556a06e895SJeffy Chen 	pm_runtime_disable(&pdev->dev);
7566a06e895SJeffy Chen 	pm_runtime_set_suspended(&pdev->dev);
7576a06e895SJeffy Chen 
758*d66571a2SChris Ruehl 	if (ctlr->dma_tx)
759*d66571a2SChris Ruehl 		dma_release_channel(ctlr->dma_tx);
760*d66571a2SChris Ruehl 	if (ctlr->dma_rx)
761*d66571a2SChris Ruehl 		dma_release_channel(ctlr->dma_rx);
76264e36824Saddy ke 
763*d66571a2SChris Ruehl 	spi_controller_put(ctlr);
764844c9f47SShawn Lin 
76564e36824Saddy ke 	return 0;
76664e36824Saddy ke }
76764e36824Saddy ke 
76864e36824Saddy ke #ifdef CONFIG_PM_SLEEP
76964e36824Saddy ke static int rockchip_spi_suspend(struct device *dev)
77064e36824Saddy ke {
77143de979dSJeffy Chen 	int ret;
772*d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_get_drvdata(dev);
77364e36824Saddy ke 
774*d66571a2SChris Ruehl 	ret = spi_controller_suspend(ctlr);
77543de979dSJeffy Chen 	if (ret < 0)
77664e36824Saddy ke 		return ret;
77764e36824Saddy ke 
778d38c4ae1SJeffy Chen 	ret = pm_runtime_force_suspend(dev);
779d38c4ae1SJeffy Chen 	if (ret < 0)
780d38c4ae1SJeffy Chen 		return ret;
78164e36824Saddy ke 
78223e291c2SBrian Norris 	pinctrl_pm_select_sleep_state(dev);
78323e291c2SBrian Norris 
78443de979dSJeffy Chen 	return 0;
78564e36824Saddy ke }
78664e36824Saddy ke 
78764e36824Saddy ke static int rockchip_spi_resume(struct device *dev)
78864e36824Saddy ke {
78943de979dSJeffy Chen 	int ret;
790*d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_get_drvdata(dev);
791*d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
79264e36824Saddy ke 
79323e291c2SBrian Norris 	pinctrl_pm_select_default_state(dev);
79423e291c2SBrian Norris 
795d38c4ae1SJeffy Chen 	ret = pm_runtime_force_resume(dev);
79664e36824Saddy ke 	if (ret < 0)
79764e36824Saddy ke 		return ret;
79864e36824Saddy ke 
799*d66571a2SChris Ruehl 	ret = spi_controller_resume(ctlr);
80064e36824Saddy ke 	if (ret < 0) {
80164e36824Saddy ke 		clk_disable_unprepare(rs->spiclk);
80264e36824Saddy ke 		clk_disable_unprepare(rs->apb_pclk);
80364e36824Saddy ke 	}
80464e36824Saddy ke 
80543de979dSJeffy Chen 	return 0;
80664e36824Saddy ke }
80764e36824Saddy ke #endif /* CONFIG_PM_SLEEP */
80864e36824Saddy ke 
809ec833050SRafael J. Wysocki #ifdef CONFIG_PM
81064e36824Saddy ke static int rockchip_spi_runtime_suspend(struct device *dev)
81164e36824Saddy ke {
812*d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_get_drvdata(dev);
813*d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
81464e36824Saddy ke 
81564e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
81664e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
81764e36824Saddy ke 
81864e36824Saddy ke 	return 0;
81964e36824Saddy ke }
82064e36824Saddy ke 
82164e36824Saddy ke static int rockchip_spi_runtime_resume(struct device *dev)
82264e36824Saddy ke {
82364e36824Saddy ke 	int ret;
824*d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_get_drvdata(dev);
825*d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
82664e36824Saddy ke 
82764e36824Saddy ke 	ret = clk_prepare_enable(rs->apb_pclk);
82843de979dSJeffy Chen 	if (ret < 0)
82964e36824Saddy ke 		return ret;
83064e36824Saddy ke 
83164e36824Saddy ke 	ret = clk_prepare_enable(rs->spiclk);
83243de979dSJeffy Chen 	if (ret < 0)
83364e36824Saddy ke 		clk_disable_unprepare(rs->apb_pclk);
83464e36824Saddy ke 
83543de979dSJeffy Chen 	return 0;
83664e36824Saddy ke }
837ec833050SRafael J. Wysocki #endif /* CONFIG_PM */
83864e36824Saddy ke 
83964e36824Saddy ke static const struct dev_pm_ops rockchip_spi_pm = {
84064e36824Saddy ke 	SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
84164e36824Saddy ke 	SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
84264e36824Saddy ke 			   rockchip_spi_runtime_resume, NULL)
84364e36824Saddy ke };
84464e36824Saddy ke 
84564e36824Saddy ke static const struct of_device_id rockchip_spi_dt_match[] = {
846c6486eadSJohan Jonker 	{ .compatible = "rockchip,px30-spi", },
847aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3036-spi", },
84864e36824Saddy ke 	{ .compatible = "rockchip,rk3066-spi", },
849b839b785SAddy Ke 	{ .compatible = "rockchip,rk3188-spi", },
850aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3228-spi", },
851b839b785SAddy Ke 	{ .compatible = "rockchip,rk3288-spi", },
852c6486eadSJohan Jonker 	{ .compatible = "rockchip,rk3308-spi", },
853c6486eadSJohan Jonker 	{ .compatible = "rockchip,rk3328-spi", },
854aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3368-spi", },
8559b7a5622SXu Jianqun 	{ .compatible = "rockchip,rk3399-spi", },
856c6486eadSJohan Jonker 	{ .compatible = "rockchip,rv1108-spi", },
85764e36824Saddy ke 	{ },
85864e36824Saddy ke };
85964e36824Saddy ke MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
86064e36824Saddy ke 
86164e36824Saddy ke static struct platform_driver rockchip_spi_driver = {
86264e36824Saddy ke 	.driver = {
86364e36824Saddy ke 		.name	= DRIVER_NAME,
86464e36824Saddy ke 		.pm = &rockchip_spi_pm,
86564e36824Saddy ke 		.of_match_table = of_match_ptr(rockchip_spi_dt_match),
86664e36824Saddy ke 	},
86764e36824Saddy ke 	.probe = rockchip_spi_probe,
86864e36824Saddy ke 	.remove = rockchip_spi_remove,
86964e36824Saddy ke };
87064e36824Saddy ke 
87164e36824Saddy ke module_platform_driver(rockchip_spi_driver);
87264e36824Saddy ke 
8735dcc44edSAddy Ke MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
87464e36824Saddy ke MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
87564e36824Saddy ke MODULE_LICENSE("GPL v2");
876