xref: /linux/drivers/spi/spi-rockchip.c (revision 419bc8f681a0dc63588cee693b6d45e7caa6006c)
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
4413a96935SJon Lin #define ROCKCHIP_SPI_VERSION			0x0048
4564e36824Saddy ke #define ROCKCHIP_SPI_TXDR			0x0400
4664e36824Saddy ke #define ROCKCHIP_SPI_RXDR			0x0800
4764e36824Saddy ke 
4864e36824Saddy ke /* Bit fields in CTRLR0 */
4964e36824Saddy ke #define CR0_DFS_OFFSET				0
5065498c6aSEmil Renner Berthing #define CR0_DFS_4BIT				0x0
5165498c6aSEmil Renner Berthing #define CR0_DFS_8BIT				0x1
5265498c6aSEmil Renner Berthing #define CR0_DFS_16BIT				0x2
5364e36824Saddy ke 
5464e36824Saddy ke #define CR0_CFS_OFFSET				2
5564e36824Saddy ke 
5664e36824Saddy ke #define CR0_SCPH_OFFSET				6
5764e36824Saddy ke 
5864e36824Saddy ke #define CR0_SCPOL_OFFSET			7
5964e36824Saddy ke 
6064e36824Saddy ke #define CR0_CSM_OFFSET				8
6164e36824Saddy ke #define CR0_CSM_KEEP				0x0
6264e36824Saddy ke /* ss_n be high for half sclk_out cycles */
6364e36824Saddy ke #define CR0_CSM_HALF				0X1
6464e36824Saddy ke /* ss_n be high for one sclk_out cycle */
6564e36824Saddy ke #define CR0_CSM_ONE					0x2
6664e36824Saddy ke 
6764e36824Saddy ke /* ss_n to sclk_out delay */
6864e36824Saddy ke #define CR0_SSD_OFFSET				10
6964e36824Saddy ke /*
7064e36824Saddy ke  * The period between ss_n active and
7164e36824Saddy ke  * sclk_out active is half sclk_out cycles
7264e36824Saddy ke  */
7364e36824Saddy ke #define CR0_SSD_HALF				0x0
7464e36824Saddy ke /*
7564e36824Saddy ke  * The period between ss_n active and
7664e36824Saddy ke  * sclk_out active is one sclk_out cycle
7764e36824Saddy ke  */
7864e36824Saddy ke #define CR0_SSD_ONE					0x1
7964e36824Saddy ke 
8064e36824Saddy ke #define CR0_EM_OFFSET				11
8164e36824Saddy ke #define CR0_EM_LITTLE				0x0
8264e36824Saddy ke #define CR0_EM_BIG					0x1
8364e36824Saddy ke 
8464e36824Saddy ke #define CR0_FBM_OFFSET				12
8564e36824Saddy ke #define CR0_FBM_MSB					0x0
8664e36824Saddy ke #define CR0_FBM_LSB					0x1
8764e36824Saddy ke 
8864e36824Saddy ke #define CR0_BHT_OFFSET				13
8964e36824Saddy ke #define CR0_BHT_16BIT				0x0
9064e36824Saddy ke #define CR0_BHT_8BIT				0x1
9164e36824Saddy ke 
9264e36824Saddy ke #define CR0_RSD_OFFSET				14
9374b7efa8SEmil Renner Berthing #define CR0_RSD_MAX				0x3
9464e36824Saddy ke 
9564e36824Saddy ke #define CR0_FRF_OFFSET				16
9664e36824Saddy ke #define CR0_FRF_SPI					0x0
9764e36824Saddy ke #define CR0_FRF_SSP					0x1
9864e36824Saddy ke #define CR0_FRF_MICROWIRE			0x2
9964e36824Saddy ke 
10064e36824Saddy ke #define CR0_XFM_OFFSET				18
10164e36824Saddy ke #define CR0_XFM_MASK				(0x03 << SPI_XFM_OFFSET)
10264e36824Saddy ke #define CR0_XFM_TR					0x0
10364e36824Saddy ke #define CR0_XFM_TO					0x1
10464e36824Saddy ke #define CR0_XFM_RO					0x2
10564e36824Saddy ke 
10664e36824Saddy ke #define CR0_OPM_OFFSET				20
10764e36824Saddy ke #define CR0_OPM_MASTER				0x0
10864e36824Saddy ke #define CR0_OPM_SLAVE				0x1
10964e36824Saddy ke 
110736b81e0SJon Lin #define CR0_SOI_OFFSET				23
111736b81e0SJon Lin 
11264e36824Saddy ke #define CR0_MTM_OFFSET				0x21
11364e36824Saddy ke 
11464e36824Saddy ke /* Bit fields in SER, 2bit */
11564e36824Saddy ke #define SER_MASK					0x3
11664e36824Saddy ke 
117420b82f8SEmil Renner Berthing /* Bit fields in BAUDR */
118420b82f8SEmil Renner Berthing #define BAUDR_SCKDV_MIN				2
119420b82f8SEmil Renner Berthing #define BAUDR_SCKDV_MAX				65534
120420b82f8SEmil Renner Berthing 
1212758bd09SJon Lin /* Bit fields in SR, 6bit */
1222758bd09SJon Lin #define SR_MASK						0x3f
12364e36824Saddy ke #define SR_BUSY						(1 << 0)
12464e36824Saddy ke #define SR_TF_FULL					(1 << 1)
12564e36824Saddy ke #define SR_TF_EMPTY					(1 << 2)
12664e36824Saddy ke #define SR_RF_EMPTY					(1 << 3)
12764e36824Saddy ke #define SR_RF_FULL					(1 << 4)
1282758bd09SJon Lin #define SR_SLAVE_TX_BUSY				(1 << 5)
12964e36824Saddy ke 
13064e36824Saddy ke /* Bit fields in ISR, IMR, ISR, RISR, 5bit */
13164e36824Saddy ke #define INT_MASK					0x1f
13264e36824Saddy ke #define INT_TF_EMPTY				(1 << 0)
13364e36824Saddy ke #define INT_TF_OVERFLOW				(1 << 1)
13464e36824Saddy ke #define INT_RF_UNDERFLOW			(1 << 2)
13564e36824Saddy ke #define INT_RF_OVERFLOW				(1 << 3)
13664e36824Saddy ke #define INT_RF_FULL				(1 << 4)
137869f2c94SJon Lin #define INT_CS_INACTIVE				(1 << 6)
13864e36824Saddy ke 
13964e36824Saddy ke /* Bit fields in ICR, 4bit */
14064e36824Saddy ke #define ICR_MASK					0x0f
14164e36824Saddy ke #define ICR_ALL						(1 << 0)
14264e36824Saddy ke #define ICR_RF_UNDERFLOW			(1 << 1)
14364e36824Saddy ke #define ICR_RF_OVERFLOW				(1 << 2)
14464e36824Saddy ke #define ICR_TF_OVERFLOW				(1 << 3)
14564e36824Saddy ke 
14664e36824Saddy ke /* Bit fields in DMACR */
14764e36824Saddy ke #define RF_DMA_EN					(1 << 0)
14864e36824Saddy ke #define TF_DMA_EN					(1 << 1)
14964e36824Saddy ke 
150fab3e487SEmil Renner Berthing /* Driver state flags */
151fab3e487SEmil Renner Berthing #define RXDMA					(1 << 0)
152fab3e487SEmil Renner Berthing #define TXDMA					(1 << 1)
15364e36824Saddy ke 
154f9cfd522SAddy Ke /* sclk_out: spi master internal logic in rk3x can support 50Mhz */
155420b82f8SEmil Renner Berthing #define MAX_SCLK_OUT				50000000U
156f9cfd522SAddy Ke 
1575185a81cSBrian Norris /*
1585185a81cSBrian Norris  * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However,
1595185a81cSBrian Norris  * the controller seems to hang when given 0x10000, so stick with this for now.
1605185a81cSBrian Norris  */
1615185a81cSBrian Norris #define ROCKCHIP_SPI_MAX_TRANLEN		0xffff
1625185a81cSBrian Norris 
163b8d42371SJon Lin /* 2 for native cs, 2 for cs-gpio */
164b8d42371SJon Lin #define ROCKCHIP_SPI_MAX_CS_NUM			4
16513a96935SJon Lin #define ROCKCHIP_SPI_VER2_TYPE1			0x05EC0002
16613a96935SJon Lin #define ROCKCHIP_SPI_VER2_TYPE2			0x00110002
167aa099382SJeffy Chen 
168940f3bbfSAlexander Kochetkov #define ROCKCHIP_AUTOSUSPEND_TIMEOUT		2000
169940f3bbfSAlexander Kochetkov 
17064e36824Saddy ke struct rockchip_spi {
17164e36824Saddy ke 	struct device *dev;
17264e36824Saddy ke 
17364e36824Saddy ke 	struct clk *spiclk;
17464e36824Saddy ke 	struct clk *apb_pclk;
17564e36824Saddy ke 
17664e36824Saddy ke 	void __iomem *regs;
177eee06a9eSEmil Renner Berthing 	dma_addr_t dma_addr_rx;
178eee06a9eSEmil Renner Berthing 	dma_addr_t dma_addr_tx;
179fab3e487SEmil Renner Berthing 
18001b59ce5SEmil Renner Berthing 	const void *tx;
18101b59ce5SEmil Renner Berthing 	void *rx;
18201b59ce5SEmil Renner Berthing 	unsigned int tx_left;
18301b59ce5SEmil Renner Berthing 	unsigned int rx_left;
18401b59ce5SEmil Renner Berthing 
185fab3e487SEmil Renner Berthing 	atomic_t state;
186fab3e487SEmil Renner Berthing 
18764e36824Saddy ke 	/*depth of the FIFO buffer */
18864e36824Saddy ke 	u32 fifo_len;
189420b82f8SEmil Renner Berthing 	/* frequency of spiclk */
190420b82f8SEmil Renner Berthing 	u32 freq;
19164e36824Saddy ke 
19264e36824Saddy ke 	u8 n_bytes;
19374b7efa8SEmil Renner Berthing 	u8 rsd;
19464e36824Saddy ke 
195aa099382SJeffy Chen 	bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
196d065f41aSChris Ruehl 
197d065f41aSChris Ruehl 	bool slave_abort;
198869f2c94SJon Lin 	bool cs_inactive; /* spi slave tansmition stop when cs inactive */
199d5d933f0SLuca Ceresoli 	bool cs_high_supported; /* native CS supports active-high polarity */
200d5d933f0SLuca Ceresoli 
201869f2c94SJon Lin 	struct spi_transfer *xfer; /* Store xfer temporarily */
20264e36824Saddy ke };
20364e36824Saddy ke 
20430688e4eSEmil Renner Berthing static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
20564e36824Saddy ke {
20630688e4eSEmil Renner Berthing 	writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR);
20764e36824Saddy ke }
20864e36824Saddy ke 
2092758bd09SJon Lin static inline void wait_for_tx_idle(struct rockchip_spi *rs, bool slave_mode)
2102df08e78SAddy Ke {
2112df08e78SAddy Ke 	unsigned long timeout = jiffies + msecs_to_jiffies(5);
2122df08e78SAddy Ke 
2132df08e78SAddy Ke 	do {
2142758bd09SJon Lin 		if (slave_mode) {
2152758bd09SJon Lin 			if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_SLAVE_TX_BUSY) &&
2162758bd09SJon Lin 			    !((readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)))
2172758bd09SJon Lin 				return;
2182758bd09SJon Lin 		} else {
2192df08e78SAddy Ke 			if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
2202df08e78SAddy Ke 				return;
2212758bd09SJon Lin 		}
22264bc0110SDoug Anderson 	} while (!time_after(jiffies, timeout));
2232df08e78SAddy Ke 
2242df08e78SAddy Ke 	dev_warn(rs->dev, "spi controller is in busy state!\n");
2252df08e78SAddy Ke }
2262df08e78SAddy Ke 
22764e36824Saddy ke static u32 get_fifo_len(struct rockchip_spi *rs)
22864e36824Saddy ke {
22913a96935SJon Lin 	u32 ver;
23064e36824Saddy ke 
23113a96935SJon Lin 	ver = readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION);
23213a96935SJon Lin 
23313a96935SJon Lin 	switch (ver) {
23413a96935SJon Lin 	case ROCKCHIP_SPI_VER2_TYPE1:
23513a96935SJon Lin 	case ROCKCHIP_SPI_VER2_TYPE2:
23613a96935SJon Lin 		return 64;
23713a96935SJon Lin 	default:
23813a96935SJon Lin 		return 32;
23964e36824Saddy ke 	}
24064e36824Saddy ke }
24164e36824Saddy ke 
24264e36824Saddy ke static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
24364e36824Saddy ke {
244d66571a2SChris Ruehl 	struct spi_controller *ctlr = spi->controller;
245d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
246736b81e0SJon Lin 	bool cs_asserted = spi->mode & SPI_CS_HIGH ? enable : !enable;
247b920cc31SHuibin Hong 
248aa099382SJeffy Chen 	/* Return immediately for no-op */
249aa099382SJeffy Chen 	if (cs_asserted == rs->cs_asserted[spi->chip_select])
250aa099382SJeffy Chen 		return;
251aa099382SJeffy Chen 
252aa099382SJeffy Chen 	if (cs_asserted) {
253aa099382SJeffy Chen 		/* Keep things powered as long as CS is asserted */
254b920cc31SHuibin Hong 		pm_runtime_get_sync(rs->dev);
25564e36824Saddy ke 
256b8d42371SJon Lin 		if (spi->cs_gpiod)
257b8d42371SJon Lin 			ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, 1);
258b8d42371SJon Lin 		else
259b8d42371SJon Lin 			ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select));
260aa099382SJeffy Chen 	} else {
261b8d42371SJon Lin 		if (spi->cs_gpiod)
262b8d42371SJon Lin 			ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, 1);
263b8d42371SJon Lin 		else
264b8d42371SJon Lin 			ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select));
26564e36824Saddy ke 
266aa099382SJeffy Chen 		/* Drop reference from when we first asserted CS */
267aa099382SJeffy Chen 		pm_runtime_put(rs->dev);
268aa099382SJeffy Chen 	}
26964e36824Saddy ke 
270aa099382SJeffy Chen 	rs->cs_asserted[spi->chip_select] = cs_asserted;
27164e36824Saddy ke }
27264e36824Saddy ke 
273d66571a2SChris Ruehl static void rockchip_spi_handle_err(struct spi_controller *ctlr,
27464e36824Saddy ke 				    struct spi_message *msg)
27564e36824Saddy ke {
276d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
27764e36824Saddy ke 
278ce386100SEmil Renner Berthing 	/* stop running spi transfer
279ce386100SEmil Renner Berthing 	 * this also flushes both rx and tx fifos
2805dcc44edSAddy Ke 	 */
281ce386100SEmil Renner Berthing 	spi_enable_chip(rs, false);
282ce386100SEmil Renner Berthing 
2832fcdde56SJon Lin 	/* make sure all interrupts are masked and status cleared */
28401b59ce5SEmil Renner Berthing 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
2852fcdde56SJon Lin 	writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR);
28601b59ce5SEmil Renner Berthing 
287fab3e487SEmil Renner Berthing 	if (atomic_read(&rs->state) & TXDMA)
288d66571a2SChris Ruehl 		dmaengine_terminate_async(ctlr->dma_tx);
289fab3e487SEmil Renner Berthing 
290ce386100SEmil Renner Berthing 	if (atomic_read(&rs->state) & RXDMA)
291d66571a2SChris Ruehl 		dmaengine_terminate_async(ctlr->dma_rx);
29264e36824Saddy ke }
29364e36824Saddy ke 
29464e36824Saddy ke static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
29564e36824Saddy ke {
29601b59ce5SEmil Renner Berthing 	u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
29701b59ce5SEmil Renner Berthing 	u32 words = min(rs->tx_left, tx_free);
29864e36824Saddy ke 
29901b59ce5SEmil Renner Berthing 	rs->tx_left -= words;
30001b59ce5SEmil Renner Berthing 	for (; words; words--) {
30101b59ce5SEmil Renner Berthing 		u32 txw;
30201b59ce5SEmil Renner Berthing 
30364e36824Saddy ke 		if (rs->n_bytes == 1)
30401b59ce5SEmil Renner Berthing 			txw = *(u8 *)rs->tx;
30564e36824Saddy ke 		else
30601b59ce5SEmil Renner Berthing 			txw = *(u16 *)rs->tx;
30764e36824Saddy ke 
30864e36824Saddy ke 		writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
30964e36824Saddy ke 		rs->tx += rs->n_bytes;
31064e36824Saddy ke 	}
31164e36824Saddy ke }
31264e36824Saddy ke 
31364e36824Saddy ke static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
31464e36824Saddy ke {
31501b59ce5SEmil Renner Berthing 	u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
3164294e4acSJon Lin 	u32 rx_left = (rs->rx_left > words) ? rs->rx_left - words : 0;
31764e36824Saddy ke 
31801b59ce5SEmil Renner Berthing 	/* the hardware doesn't allow us to change fifo threshold
31901b59ce5SEmil Renner Berthing 	 * level while spi is enabled, so instead make sure to leave
32001b59ce5SEmil Renner Berthing 	 * enough words in the rx fifo to get the last interrupt
32101b59ce5SEmil Renner Berthing 	 * exactly when all words have been received
32201b59ce5SEmil Renner Berthing 	 */
32301b59ce5SEmil Renner Berthing 	if (rx_left) {
32401b59ce5SEmil Renner Berthing 		u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1;
32501b59ce5SEmil Renner Berthing 
32601b59ce5SEmil Renner Berthing 		if (rx_left < ftl) {
32701b59ce5SEmil Renner Berthing 			rx_left = ftl;
32801b59ce5SEmil Renner Berthing 			words = rs->rx_left - rx_left;
32901b59ce5SEmil Renner Berthing 		}
33001b59ce5SEmil Renner Berthing 	}
33101b59ce5SEmil Renner Berthing 
33201b59ce5SEmil Renner Berthing 	rs->rx_left = rx_left;
33301b59ce5SEmil Renner Berthing 	for (; words; words--) {
33401b59ce5SEmil Renner Berthing 		u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
33501b59ce5SEmil Renner Berthing 
33601b59ce5SEmil Renner Berthing 		if (!rs->rx)
33701b59ce5SEmil Renner Berthing 			continue;
33801b59ce5SEmil Renner Berthing 
33964e36824Saddy ke 		if (rs->n_bytes == 1)
34001b59ce5SEmil Renner Berthing 			*(u8 *)rs->rx = (u8)rxw;
34164e36824Saddy ke 		else
34201b59ce5SEmil Renner Berthing 			*(u16 *)rs->rx = (u16)rxw;
34364e36824Saddy ke 		rs->rx += rs->n_bytes;
3445dcc44edSAddy Ke 	}
34564e36824Saddy ke }
34664e36824Saddy ke 
34701b59ce5SEmil Renner Berthing static irqreturn_t rockchip_spi_isr(int irq, void *dev_id)
34864e36824Saddy ke {
349d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_id;
350d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
35164e36824Saddy ke 
352869f2c94SJon Lin 	/* When int_cs_inactive comes, spi slave abort */
353869f2c94SJon Lin 	if (rs->cs_inactive && readl_relaxed(rs->regs + ROCKCHIP_SPI_IMR) & INT_CS_INACTIVE) {
354869f2c94SJon Lin 		ctlr->slave_abort(ctlr);
355869f2c94SJon Lin 		writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
356869f2c94SJon Lin 		writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR);
357869f2c94SJon Lin 
358869f2c94SJon Lin 		return IRQ_HANDLED;
359869f2c94SJon Lin 	}
360869f2c94SJon Lin 
36101b59ce5SEmil Renner Berthing 	if (rs->tx_left)
36201b59ce5SEmil Renner Berthing 		rockchip_spi_pio_writer(rs);
36301b59ce5SEmil Renner Berthing 
36401b59ce5SEmil Renner Berthing 	rockchip_spi_pio_reader(rs);
36501b59ce5SEmil Renner Berthing 	if (!rs->rx_left) {
36601b59ce5SEmil Renner Berthing 		spi_enable_chip(rs, false);
36701b59ce5SEmil Renner Berthing 		writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
368869f2c94SJon Lin 		writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR);
369d66571a2SChris Ruehl 		spi_finalize_current_transfer(ctlr);
37001b59ce5SEmil Renner Berthing 	}
37101b59ce5SEmil Renner Berthing 
37201b59ce5SEmil Renner Berthing 	return IRQ_HANDLED;
37301b59ce5SEmil Renner Berthing }
37401b59ce5SEmil Renner Berthing 
37501b59ce5SEmil Renner Berthing static int rockchip_spi_prepare_irq(struct rockchip_spi *rs,
376869f2c94SJon Lin 				    struct spi_controller *ctlr,
37701b59ce5SEmil Renner Berthing 				    struct spi_transfer *xfer)
37801b59ce5SEmil Renner Berthing {
37901b59ce5SEmil Renner Berthing 	rs->tx = xfer->tx_buf;
38001b59ce5SEmil Renner Berthing 	rs->rx = xfer->rx_buf;
38101b59ce5SEmil Renner Berthing 	rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0;
38201b59ce5SEmil Renner Berthing 	rs->rx_left = xfer->len / rs->n_bytes;
38301b59ce5SEmil Renner Berthing 
384*419bc8f6SJon Lin 	writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR);
385*419bc8f6SJon Lin 
38630688e4eSEmil Renner Berthing 	spi_enable_chip(rs, true);
387a3c17402SEmil Renner Berthing 
38801b59ce5SEmil Renner Berthing 	if (rs->tx_left)
38964e36824Saddy ke 		rockchip_spi_pio_writer(rs);
39064e36824Saddy ke 
391*419bc8f6SJon Lin 	if (rs->cs_inactive)
392*419bc8f6SJon Lin 		writel_relaxed(INT_RF_FULL | INT_CS_INACTIVE, rs->regs + ROCKCHIP_SPI_IMR);
393*419bc8f6SJon Lin 	else
394*419bc8f6SJon Lin 		writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR);
395*419bc8f6SJon Lin 
39601b59ce5SEmil Renner Berthing 	/* 1 means the transfer is in progress */
39701b59ce5SEmil Renner Berthing 	return 1;
39864e36824Saddy ke }
39964e36824Saddy ke 
40064e36824Saddy ke static void rockchip_spi_dma_rxcb(void *data)
40164e36824Saddy ke {
402d66571a2SChris Ruehl 	struct spi_controller *ctlr = data;
403d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
404fab3e487SEmil Renner Berthing 	int state = atomic_fetch_andnot(RXDMA, &rs->state);
40564e36824Saddy ke 
406d065f41aSChris Ruehl 	if (state & TXDMA && !rs->slave_abort)
407fab3e487SEmil Renner Berthing 		return;
40864e36824Saddy ke 
409869f2c94SJon Lin 	if (rs->cs_inactive)
410869f2c94SJon Lin 		writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
411869f2c94SJon Lin 
41230688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
413d66571a2SChris Ruehl 	spi_finalize_current_transfer(ctlr);
414c28be31bSAddy Ke }
41564e36824Saddy ke 
41664e36824Saddy ke static void rockchip_spi_dma_txcb(void *data)
41764e36824Saddy ke {
418d66571a2SChris Ruehl 	struct spi_controller *ctlr = data;
419d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
420fab3e487SEmil Renner Berthing 	int state = atomic_fetch_andnot(TXDMA, &rs->state);
421fab3e487SEmil Renner Berthing 
422d065f41aSChris Ruehl 	if (state & RXDMA && !rs->slave_abort)
423fab3e487SEmil Renner Berthing 		return;
42464e36824Saddy ke 
4252df08e78SAddy Ke 	/* Wait until the FIFO data completely. */
4262758bd09SJon Lin 	wait_for_tx_idle(rs, ctlr->slave);
4272df08e78SAddy Ke 
42830688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
429d66571a2SChris Ruehl 	spi_finalize_current_transfer(ctlr);
4302c2bc748SAddy Ke }
43164e36824Saddy ke 
4324d9ca632SJon Lin static u32 rockchip_spi_calc_burst_size(u32 data_len)
4334d9ca632SJon Lin {
4344d9ca632SJon Lin 	u32 i;
4354d9ca632SJon Lin 
4364d9ca632SJon Lin 	/* burst size: 1, 2, 4, 8 */
4374d9ca632SJon Lin 	for (i = 1; i < 8; i <<= 1) {
4384d9ca632SJon Lin 		if (data_len & i)
4394d9ca632SJon Lin 			break;
4404d9ca632SJon Lin 	}
4414d9ca632SJon Lin 
4424d9ca632SJon Lin 	return i;
4434d9ca632SJon Lin }
4444d9ca632SJon Lin 
445fc1ad8eeSEmil Renner Berthing static int rockchip_spi_prepare_dma(struct rockchip_spi *rs,
446d66571a2SChris Ruehl 		struct spi_controller *ctlr, struct spi_transfer *xfer)
44764e36824Saddy ke {
44864e36824Saddy ke 	struct dma_async_tx_descriptor *rxdesc, *txdesc;
44964e36824Saddy ke 
450fab3e487SEmil Renner Berthing 	atomic_set(&rs->state, 0);
45164e36824Saddy ke 
452869f2c94SJon Lin 	rs->tx = xfer->tx_buf;
453869f2c94SJon Lin 	rs->rx = xfer->rx_buf;
454869f2c94SJon Lin 
45597cf5669SArnd Bergmann 	rxdesc = NULL;
456fc1ad8eeSEmil Renner Berthing 	if (xfer->rx_buf) {
45731bcb57bSEmil Renner Berthing 		struct dma_slave_config rxconf = {
45831bcb57bSEmil Renner Berthing 			.direction = DMA_DEV_TO_MEM,
459eee06a9eSEmil Renner Berthing 			.src_addr = rs->dma_addr_rx,
46031bcb57bSEmil Renner Berthing 			.src_addr_width = rs->n_bytes,
461869f2c94SJon Lin 			.src_maxburst = rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes),
46231bcb57bSEmil Renner Berthing 		};
46331bcb57bSEmil Renner Berthing 
464d66571a2SChris Ruehl 		dmaengine_slave_config(ctlr->dma_rx, &rxconf);
46564e36824Saddy ke 
4665dcc44edSAddy Ke 		rxdesc = dmaengine_prep_slave_sg(
467d66571a2SChris Ruehl 				ctlr->dma_rx,
468fc1ad8eeSEmil Renner Berthing 				xfer->rx_sg.sgl, xfer->rx_sg.nents,
469d9071b7eSEmil Renner Berthing 				DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
470ea984911SShawn Lin 		if (!rxdesc)
471ea984911SShawn Lin 			return -EINVAL;
47264e36824Saddy ke 
47364e36824Saddy ke 		rxdesc->callback = rockchip_spi_dma_rxcb;
474d66571a2SChris Ruehl 		rxdesc->callback_param = ctlr;
47564e36824Saddy ke 	}
47664e36824Saddy ke 
47797cf5669SArnd Bergmann 	txdesc = NULL;
478fc1ad8eeSEmil Renner Berthing 	if (xfer->tx_buf) {
47931bcb57bSEmil Renner Berthing 		struct dma_slave_config txconf = {
48031bcb57bSEmil Renner Berthing 			.direction = DMA_MEM_TO_DEV,
481eee06a9eSEmil Renner Berthing 			.dst_addr = rs->dma_addr_tx,
48231bcb57bSEmil Renner Berthing 			.dst_addr_width = rs->n_bytes,
48347300728SEmil Renner Berthing 			.dst_maxburst = rs->fifo_len / 4,
48431bcb57bSEmil Renner Berthing 		};
48531bcb57bSEmil Renner Berthing 
486d66571a2SChris Ruehl 		dmaengine_slave_config(ctlr->dma_tx, &txconf);
48764e36824Saddy ke 
4885dcc44edSAddy Ke 		txdesc = dmaengine_prep_slave_sg(
489d66571a2SChris Ruehl 				ctlr->dma_tx,
490fc1ad8eeSEmil Renner Berthing 				xfer->tx_sg.sgl, xfer->tx_sg.nents,
491d9071b7eSEmil Renner Berthing 				DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
492ea984911SShawn Lin 		if (!txdesc) {
493ea984911SShawn Lin 			if (rxdesc)
494d66571a2SChris Ruehl 				dmaengine_terminate_sync(ctlr->dma_rx);
495ea984911SShawn Lin 			return -EINVAL;
496ea984911SShawn Lin 		}
49764e36824Saddy ke 
49864e36824Saddy ke 		txdesc->callback = rockchip_spi_dma_txcb;
499d66571a2SChris Ruehl 		txdesc->callback_param = ctlr;
50064e36824Saddy ke 	}
50164e36824Saddy ke 
50264e36824Saddy ke 	/* rx must be started before tx due to spi instinct */
50397cf5669SArnd Bergmann 	if (rxdesc) {
504fab3e487SEmil Renner Berthing 		atomic_or(RXDMA, &rs->state);
505869f2c94SJon Lin 		ctlr->dma_rx->cookie = dmaengine_submit(rxdesc);
506d66571a2SChris Ruehl 		dma_async_issue_pending(ctlr->dma_rx);
50764e36824Saddy ke 	}
50864e36824Saddy ke 
509869f2c94SJon Lin 	if (rs->cs_inactive)
510869f2c94SJon Lin 		writel_relaxed(INT_CS_INACTIVE, rs->regs + ROCKCHIP_SPI_IMR);
511869f2c94SJon Lin 
51230688e4eSEmil Renner Berthing 	spi_enable_chip(rs, true);
513a3c17402SEmil Renner Berthing 
51497cf5669SArnd Bergmann 	if (txdesc) {
515fab3e487SEmil Renner Berthing 		atomic_or(TXDMA, &rs->state);
51664e36824Saddy ke 		dmaengine_submit(txdesc);
517d66571a2SChris Ruehl 		dma_async_issue_pending(ctlr->dma_tx);
51864e36824Saddy ke 	}
519ea984911SShawn Lin 
520a3c17402SEmil Renner Berthing 	/* 1 means the transfer is in progress */
521a3c17402SEmil Renner Berthing 	return 1;
52264e36824Saddy ke }
52364e36824Saddy ke 
524e5098952SArnd Bergmann static int rockchip_spi_config(struct rockchip_spi *rs,
525eff0275eSEmil Renner Berthing 		struct spi_device *spi, struct spi_transfer *xfer,
526d065f41aSChris Ruehl 		bool use_dma, bool slave_mode)
52764e36824Saddy ke {
5282410d6a3SEmil Renner Berthing 	u32 cr0 = CR0_FRF_SPI  << CR0_FRF_OFFSET
5292410d6a3SEmil Renner Berthing 		| CR0_BHT_8BIT << CR0_BHT_OFFSET
5302410d6a3SEmil Renner Berthing 		| CR0_SSD_ONE  << CR0_SSD_OFFSET
5312410d6a3SEmil Renner Berthing 		| CR0_EM_BIG   << CR0_EM_OFFSET;
53265498c6aSEmil Renner Berthing 	u32 cr1;
53365498c6aSEmil Renner Berthing 	u32 dmacr = 0;
53464e36824Saddy ke 
535d065f41aSChris Ruehl 	if (slave_mode)
536d065f41aSChris Ruehl 		cr0 |= CR0_OPM_SLAVE << CR0_OPM_OFFSET;
537d065f41aSChris Ruehl 	rs->slave_abort = false;
538d065f41aSChris Ruehl 
53974b7efa8SEmil Renner Berthing 	cr0 |= rs->rsd << CR0_RSD_OFFSET;
540fc1ad8eeSEmil Renner Berthing 	cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET;
54104290192SEmil Renner Berthing 	if (spi->mode & SPI_LSB_FIRST)
54204290192SEmil Renner Berthing 		cr0 |= CR0_FBM_LSB << CR0_FBM_OFFSET;
543736b81e0SJon Lin 	if (spi->mode & SPI_CS_HIGH)
544736b81e0SJon Lin 		cr0 |= BIT(spi->chip_select) << CR0_SOI_OFFSET;
545fc1ad8eeSEmil Renner Berthing 
546fc1ad8eeSEmil Renner Berthing 	if (xfer->rx_buf && xfer->tx_buf)
547fc1ad8eeSEmil Renner Berthing 		cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET;
548fc1ad8eeSEmil Renner Berthing 	else if (xfer->rx_buf)
549fc1ad8eeSEmil Renner Berthing 		cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET;
55001b59ce5SEmil Renner Berthing 	else if (use_dma)
551fc1ad8eeSEmil Renner Berthing 		cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET;
55264e36824Saddy ke 
55365498c6aSEmil Renner Berthing 	switch (xfer->bits_per_word) {
55465498c6aSEmil Renner Berthing 	case 4:
55565498c6aSEmil Renner Berthing 		cr0 |= CR0_DFS_4BIT << CR0_DFS_OFFSET;
55665498c6aSEmil Renner Berthing 		cr1 = xfer->len - 1;
55765498c6aSEmil Renner Berthing 		break;
55865498c6aSEmil Renner Berthing 	case 8:
55965498c6aSEmil Renner Berthing 		cr0 |= CR0_DFS_8BIT << CR0_DFS_OFFSET;
56065498c6aSEmil Renner Berthing 		cr1 = xfer->len - 1;
56165498c6aSEmil Renner Berthing 		break;
56265498c6aSEmil Renner Berthing 	case 16:
56365498c6aSEmil Renner Berthing 		cr0 |= CR0_DFS_16BIT << CR0_DFS_OFFSET;
56465498c6aSEmil Renner Berthing 		cr1 = xfer->len / 2 - 1;
56565498c6aSEmil Renner Berthing 		break;
56665498c6aSEmil Renner Berthing 	default:
56765498c6aSEmil Renner Berthing 		/* we only whitelist 4, 8 and 16 bit words in
568d66571a2SChris Ruehl 		 * ctlr->bits_per_word_mask, so this shouldn't
56965498c6aSEmil Renner Berthing 		 * happen
57065498c6aSEmil Renner Berthing 		 */
571e5098952SArnd Bergmann 		dev_err(rs->dev, "unknown bits per word: %d\n",
572e5098952SArnd Bergmann 			xfer->bits_per_word);
573e5098952SArnd Bergmann 		return -EINVAL;
57465498c6aSEmil Renner Berthing 	}
57565498c6aSEmil Renner Berthing 
576eff0275eSEmil Renner Berthing 	if (use_dma) {
577fc1ad8eeSEmil Renner Berthing 		if (xfer->tx_buf)
57864e36824Saddy ke 			dmacr |= TF_DMA_EN;
579fc1ad8eeSEmil Renner Berthing 		if (xfer->rx_buf)
58064e36824Saddy ke 			dmacr |= RF_DMA_EN;
58164e36824Saddy ke 	}
58264e36824Saddy ke 
58364e36824Saddy ke 	writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
58465498c6aSEmil Renner Berthing 	writel_relaxed(cr1, rs->regs + ROCKCHIP_SPI_CTRLR1);
58504b37d2dSHuibin Hong 
58601b59ce5SEmil Renner Berthing 	/* unfortunately setting the fifo threshold level to generate an
58701b59ce5SEmil Renner Berthing 	 * interrupt exactly when the fifo is full doesn't seem to work,
58801b59ce5SEmil Renner Berthing 	 * so we need the strict inequality here
58901b59ce5SEmil Renner Berthing 	 */
5904a47fcdbSJon Lin 	if ((xfer->len / rs->n_bytes) < rs->fifo_len)
5914a47fcdbSJon Lin 		writel_relaxed(xfer->len / rs->n_bytes - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
59201b59ce5SEmil Renner Berthing 	else
59364e36824Saddy ke 		writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
59464e36824Saddy ke 
5952758bd09SJon Lin 	writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_DMATDLR);
5964d9ca632SJon Lin 	writel_relaxed(rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes) - 1,
5974d9ca632SJon Lin 		       rs->regs + ROCKCHIP_SPI_DMARDLR);
59864e36824Saddy ke 	writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
59964e36824Saddy ke 
600420b82f8SEmil Renner Berthing 	/* the hardware only supports an even clock divisor, so
601420b82f8SEmil Renner Berthing 	 * round divisor = spiclk / speed up to nearest even number
602420b82f8SEmil Renner Berthing 	 * so that the resulting speed is <= the requested speed
603420b82f8SEmil Renner Berthing 	 */
604420b82f8SEmil Renner Berthing 	writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz),
605420b82f8SEmil Renner Berthing 			rs->regs + ROCKCHIP_SPI_BAUDR);
606e5098952SArnd Bergmann 
607e5098952SArnd Bergmann 	return 0;
60864e36824Saddy ke }
60964e36824Saddy ke 
6105185a81cSBrian Norris static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
6115185a81cSBrian Norris {
6125185a81cSBrian Norris 	return ROCKCHIP_SPI_MAX_TRANLEN;
6135185a81cSBrian Norris }
6145185a81cSBrian Norris 
615d065f41aSChris Ruehl static int rockchip_spi_slave_abort(struct spi_controller *ctlr)
616d065f41aSChris Ruehl {
617d065f41aSChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
618869f2c94SJon Lin 	u32 rx_fifo_left;
619869f2c94SJon Lin 	struct dma_tx_state state;
620869f2c94SJon Lin 	enum dma_status status;
621d065f41aSChris Ruehl 
622869f2c94SJon Lin 	/* Get current dma rx point */
623869f2c94SJon Lin 	if (atomic_read(&rs->state) & RXDMA) {
624869f2c94SJon Lin 		dmaengine_pause(ctlr->dma_rx);
625869f2c94SJon Lin 		status = dmaengine_tx_status(ctlr->dma_rx, ctlr->dma_rx->cookie, &state);
626869f2c94SJon Lin 		if (status == DMA_ERROR) {
627869f2c94SJon Lin 			rs->rx = rs->xfer->rx_buf;
628869f2c94SJon Lin 			rs->xfer->len = 0;
629869f2c94SJon Lin 			rx_fifo_left = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
630869f2c94SJon Lin 			for (; rx_fifo_left; rx_fifo_left--)
631869f2c94SJon Lin 				readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
632869f2c94SJon Lin 			goto out;
633869f2c94SJon Lin 		} else {
634869f2c94SJon Lin 			rs->rx += rs->xfer->len - rs->n_bytes * state.residue;
635869f2c94SJon Lin 		}
636869f2c94SJon Lin 	}
637869f2c94SJon Lin 
638869f2c94SJon Lin 	/* Get the valid data left in rx fifo and set rs->xfer->len real rx size */
639869f2c94SJon Lin 	if (rs->rx) {
640869f2c94SJon Lin 		rx_fifo_left = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
641869f2c94SJon Lin 		for (; rx_fifo_left; rx_fifo_left--) {
642869f2c94SJon Lin 			u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
643869f2c94SJon Lin 
644869f2c94SJon Lin 			if (rs->n_bytes == 1)
645869f2c94SJon Lin 				*(u8 *)rs->rx = (u8)rxw;
646869f2c94SJon Lin 			else
647869f2c94SJon Lin 				*(u16 *)rs->rx = (u16)rxw;
648869f2c94SJon Lin 			rs->rx += rs->n_bytes;
649869f2c94SJon Lin 		}
650869f2c94SJon Lin 		rs->xfer->len = (unsigned int)(rs->rx - rs->xfer->rx_buf);
651869f2c94SJon Lin 	}
652869f2c94SJon Lin 
653869f2c94SJon Lin out:
65480808768SJon Lin 	if (atomic_read(&rs->state) & RXDMA)
65580808768SJon Lin 		dmaengine_terminate_sync(ctlr->dma_rx);
65680808768SJon Lin 	if (atomic_read(&rs->state) & TXDMA)
65780808768SJon Lin 		dmaengine_terminate_sync(ctlr->dma_tx);
65880808768SJon Lin 	atomic_set(&rs->state, 0);
65980808768SJon Lin 	spi_enable_chip(rs, false);
660d065f41aSChris Ruehl 	rs->slave_abort = true;
6616bd2c867SVincent Pelletier 	spi_finalize_current_transfer(ctlr);
662d065f41aSChris Ruehl 
663d065f41aSChris Ruehl 	return 0;
664d065f41aSChris Ruehl }
665d065f41aSChris Ruehl 
6665dcc44edSAddy Ke static int rockchip_spi_transfer_one(
667d66571a2SChris Ruehl 		struct spi_controller *ctlr,
66864e36824Saddy ke 		struct spi_device *spi,
66964e36824Saddy ke 		struct spi_transfer *xfer)
67064e36824Saddy ke {
671d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
672e5098952SArnd Bergmann 	int ret;
673eff0275eSEmil Renner Berthing 	bool use_dma;
67464e36824Saddy ke 
6755457773eSTobias Schramm 	/* Zero length transfers won't trigger an interrupt on completion */
6765457773eSTobias Schramm 	if (!xfer->len) {
6775457773eSTobias Schramm 		spi_finalize_current_transfer(ctlr);
6785457773eSTobias Schramm 		return 1;
6795457773eSTobias Schramm 	}
6805457773eSTobias Schramm 
68162946172SDoug Anderson 	WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
68262946172SDoug Anderson 		(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
68364e36824Saddy ke 
68464e36824Saddy ke 	if (!xfer->tx_buf && !xfer->rx_buf) {
68564e36824Saddy ke 		dev_err(rs->dev, "No buffer for transfer\n");
68664e36824Saddy ke 		return -EINVAL;
68764e36824Saddy ke 	}
68864e36824Saddy ke 
6895185a81cSBrian Norris 	if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
6905185a81cSBrian Norris 		dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
6915185a81cSBrian Norris 		return -EINVAL;
6925185a81cSBrian Norris 	}
6935185a81cSBrian Norris 
69465498c6aSEmil Renner Berthing 	rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2;
695869f2c94SJon Lin 	rs->xfer = xfer;
696d66571a2SChris Ruehl 	use_dma = ctlr->can_dma ? ctlr->can_dma(ctlr, spi, xfer) : false;
69764e36824Saddy ke 
698e5098952SArnd Bergmann 	ret = rockchip_spi_config(rs, spi, xfer, use_dma, ctlr->slave);
699e5098952SArnd Bergmann 	if (ret)
700e5098952SArnd Bergmann 		return ret;
70164e36824Saddy ke 
702eff0275eSEmil Renner Berthing 	if (use_dma)
703d66571a2SChris Ruehl 		return rockchip_spi_prepare_dma(rs, ctlr, xfer);
70464e36824Saddy ke 
705869f2c94SJon Lin 	return rockchip_spi_prepare_irq(rs, ctlr, xfer);
70664e36824Saddy ke }
70764e36824Saddy ke 
708d66571a2SChris Ruehl static bool rockchip_spi_can_dma(struct spi_controller *ctlr,
70964e36824Saddy ke 				 struct spi_device *spi,
71064e36824Saddy ke 				 struct spi_transfer *xfer)
71164e36824Saddy ke {
712d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
71301b59ce5SEmil Renner Berthing 	unsigned int bytes_per_word = xfer->bits_per_word <= 8 ? 1 : 2;
71464e36824Saddy ke 
71501b59ce5SEmil Renner Berthing 	/* if the numbor of spi words to transfer is less than the fifo
71601b59ce5SEmil Renner Berthing 	 * length we can just fill the fifo and wait for a single irq,
71701b59ce5SEmil Renner Berthing 	 * so don't bother setting up dma
71801b59ce5SEmil Renner Berthing 	 */
71901b59ce5SEmil Renner Berthing 	return xfer->len / bytes_per_word >= rs->fifo_len;
72064e36824Saddy ke }
72164e36824Saddy ke 
7223a4bf922SJon Lin static int rockchip_spi_setup(struct spi_device *spi)
7233a4bf922SJon Lin {
7243a4bf922SJon Lin 	struct rockchip_spi *rs = spi_controller_get_devdata(spi->controller);
7253a4bf922SJon Lin 	u32 cr0;
7263a4bf922SJon Lin 
727d5d933f0SLuca Ceresoli 	if (!spi->cs_gpiod && (spi->mode & SPI_CS_HIGH) && !rs->cs_high_supported) {
728d5d933f0SLuca Ceresoli 		dev_warn(&spi->dev, "setup: non GPIO CS can't be active-high\n");
729d5d933f0SLuca Ceresoli 		return -EINVAL;
730d5d933f0SLuca Ceresoli 	}
731d5d933f0SLuca Ceresoli 
7323a4bf922SJon Lin 	pm_runtime_get_sync(rs->dev);
7333a4bf922SJon Lin 
7343a4bf922SJon Lin 	cr0 = readl_relaxed(rs->regs + ROCKCHIP_SPI_CTRLR0);
7353a4bf922SJon Lin 
7363a4bf922SJon Lin 	cr0 &= ~(0x3 << CR0_SCPH_OFFSET);
7373a4bf922SJon Lin 	cr0 |= ((spi->mode & 0x3) << CR0_SCPH_OFFSET);
7383a4bf922SJon Lin 	if (spi->mode & SPI_CS_HIGH && spi->chip_select <= 1)
7393a4bf922SJon Lin 		cr0 |= BIT(spi->chip_select) << CR0_SOI_OFFSET;
7403a4bf922SJon Lin 	else if (spi->chip_select <= 1)
7413a4bf922SJon Lin 		cr0 &= ~(BIT(spi->chip_select) << CR0_SOI_OFFSET);
7423a4bf922SJon Lin 
7433a4bf922SJon Lin 	writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
7443a4bf922SJon Lin 
7453a4bf922SJon Lin 	pm_runtime_put(rs->dev);
7463a4bf922SJon Lin 
7473a4bf922SJon Lin 	return 0;
7483a4bf922SJon Lin }
7493a4bf922SJon Lin 
75064e36824Saddy ke static int rockchip_spi_probe(struct platform_device *pdev)
75164e36824Saddy ke {
75243de979dSJeffy Chen 	int ret;
75364e36824Saddy ke 	struct rockchip_spi *rs;
754d66571a2SChris Ruehl 	struct spi_controller *ctlr;
75564e36824Saddy ke 	struct resource *mem;
756d065f41aSChris Ruehl 	struct device_node *np = pdev->dev.of_node;
7579382df0aSJon Lin 	u32 rsd_nsecs, num_cs;
758d065f41aSChris Ruehl 	bool slave_mode;
75964e36824Saddy ke 
760d065f41aSChris Ruehl 	slave_mode = of_property_read_bool(np, "spi-slave");
761d065f41aSChris Ruehl 
762d065f41aSChris Ruehl 	if (slave_mode)
763d065f41aSChris Ruehl 		ctlr = spi_alloc_slave(&pdev->dev,
764d065f41aSChris Ruehl 				sizeof(struct rockchip_spi));
765d065f41aSChris Ruehl 	else
766d065f41aSChris Ruehl 		ctlr = spi_alloc_master(&pdev->dev,
767d065f41aSChris Ruehl 				sizeof(struct rockchip_spi));
768d065f41aSChris Ruehl 
769d66571a2SChris Ruehl 	if (!ctlr)
77064e36824Saddy ke 		return -ENOMEM;
7715dcc44edSAddy Ke 
772d66571a2SChris Ruehl 	platform_set_drvdata(pdev, ctlr);
77364e36824Saddy ke 
774d66571a2SChris Ruehl 	rs = spi_controller_get_devdata(ctlr);
775d065f41aSChris Ruehl 	ctlr->slave = slave_mode;
77664e36824Saddy ke 
77764e36824Saddy ke 	/* Get basic io resource and map it */
77864e36824Saddy ke 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
77964e36824Saddy ke 	rs->regs = devm_ioremap_resource(&pdev->dev, mem);
78064e36824Saddy ke 	if (IS_ERR(rs->regs)) {
78164e36824Saddy ke 		ret =  PTR_ERR(rs->regs);
782d66571a2SChris Ruehl 		goto err_put_ctlr;
78364e36824Saddy ke 	}
78464e36824Saddy ke 
78564e36824Saddy ke 	rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
78664e36824Saddy ke 	if (IS_ERR(rs->apb_pclk)) {
78764e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get apb_pclk\n");
78864e36824Saddy ke 		ret = PTR_ERR(rs->apb_pclk);
789d66571a2SChris Ruehl 		goto err_put_ctlr;
79064e36824Saddy ke 	}
79164e36824Saddy ke 
79264e36824Saddy ke 	rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
79364e36824Saddy ke 	if (IS_ERR(rs->spiclk)) {
79464e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get spi_pclk\n");
79564e36824Saddy ke 		ret = PTR_ERR(rs->spiclk);
796d66571a2SChris Ruehl 		goto err_put_ctlr;
79764e36824Saddy ke 	}
79864e36824Saddy ke 
79964e36824Saddy ke 	ret = clk_prepare_enable(rs->apb_pclk);
80043de979dSJeffy Chen 	if (ret < 0) {
80164e36824Saddy ke 		dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
802d66571a2SChris Ruehl 		goto err_put_ctlr;
80364e36824Saddy ke 	}
80464e36824Saddy ke 
80564e36824Saddy ke 	ret = clk_prepare_enable(rs->spiclk);
80643de979dSJeffy Chen 	if (ret < 0) {
80764e36824Saddy ke 		dev_err(&pdev->dev, "Failed to enable spi_clk\n");
808c351587eSJeffy Chen 		goto err_disable_apbclk;
80964e36824Saddy ke 	}
81064e36824Saddy ke 
81130688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
81264e36824Saddy ke 
81301b59ce5SEmil Renner Berthing 	ret = platform_get_irq(pdev, 0);
81401b59ce5SEmil Renner Berthing 	if (ret < 0)
81501b59ce5SEmil Renner Berthing 		goto err_disable_spiclk;
81601b59ce5SEmil Renner Berthing 
81701b59ce5SEmil Renner Berthing 	ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL,
818d66571a2SChris Ruehl 			IRQF_ONESHOT, dev_name(&pdev->dev), ctlr);
81901b59ce5SEmil Renner Berthing 	if (ret)
82001b59ce5SEmil Renner Berthing 		goto err_disable_spiclk;
82101b59ce5SEmil Renner Berthing 
82264e36824Saddy ke 	rs->dev = &pdev->dev;
823420b82f8SEmil Renner Berthing 	rs->freq = clk_get_rate(rs->spiclk);
82464e36824Saddy ke 
82576b17e6eSJulius Werner 	if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
82674b7efa8SEmil Renner Berthing 				  &rsd_nsecs)) {
82774b7efa8SEmil Renner Berthing 		/* rx sample delay is expressed in parent clock cycles (max 3) */
82874b7efa8SEmil Renner Berthing 		u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8),
82974b7efa8SEmil Renner Berthing 				1000000000 >> 8);
83074b7efa8SEmil Renner Berthing 		if (!rsd) {
83174b7efa8SEmil Renner Berthing 			dev_warn(rs->dev, "%u Hz are too slow to express %u ns delay\n",
83274b7efa8SEmil Renner Berthing 					rs->freq, rsd_nsecs);
83374b7efa8SEmil Renner Berthing 		} else if (rsd > CR0_RSD_MAX) {
83474b7efa8SEmil Renner Berthing 			rsd = CR0_RSD_MAX;
83574b7efa8SEmil Renner Berthing 			dev_warn(rs->dev, "%u Hz are too fast to express %u ns delay, clamping at %u ns\n",
83674b7efa8SEmil Renner Berthing 					rs->freq, rsd_nsecs,
83774b7efa8SEmil Renner Berthing 					CR0_RSD_MAX * 1000000000U / rs->freq);
83874b7efa8SEmil Renner Berthing 		}
83974b7efa8SEmil Renner Berthing 		rs->rsd = rsd;
84074b7efa8SEmil Renner Berthing 	}
84176b17e6eSJulius Werner 
84264e36824Saddy ke 	rs->fifo_len = get_fifo_len(rs);
84364e36824Saddy ke 	if (!rs->fifo_len) {
84464e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get fifo length\n");
845db7e8d90SWei Yongjun 		ret = -EINVAL;
846c351587eSJeffy Chen 		goto err_disable_spiclk;
84764e36824Saddy ke 	}
84864e36824Saddy ke 
849940f3bbfSAlexander Kochetkov 	pm_runtime_set_autosuspend_delay(&pdev->dev, ROCKCHIP_AUTOSUSPEND_TIMEOUT);
850940f3bbfSAlexander Kochetkov 	pm_runtime_use_autosuspend(&pdev->dev);
85164e36824Saddy ke 	pm_runtime_set_active(&pdev->dev);
85264e36824Saddy ke 	pm_runtime_enable(&pdev->dev);
85364e36824Saddy ke 
854d66571a2SChris Ruehl 	ctlr->auto_runtime_pm = true;
855d66571a2SChris Ruehl 	ctlr->bus_num = pdev->id;
856d66571a2SChris Ruehl 	ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST;
857d065f41aSChris Ruehl 	if (slave_mode) {
858d065f41aSChris Ruehl 		ctlr->mode_bits |= SPI_NO_CS;
859d065f41aSChris Ruehl 		ctlr->slave_abort = rockchip_spi_slave_abort;
860d065f41aSChris Ruehl 	} else {
861d065f41aSChris Ruehl 		ctlr->flags = SPI_MASTER_GPIO_SS;
862eb1262e3SChris Ruehl 		ctlr->max_native_cs = ROCKCHIP_SPI_MAX_CS_NUM;
863eb1262e3SChris Ruehl 		/*
864eb1262e3SChris Ruehl 		 * rk spi0 has two native cs, spi1..5 one cs only
865eb1262e3SChris Ruehl 		 * if num-cs is missing in the dts, default to 1
866eb1262e3SChris Ruehl 		 */
8679382df0aSJon Lin 		if (of_property_read_u32(np, "num-cs", &num_cs))
8689382df0aSJon Lin 			num_cs = 1;
8699382df0aSJon Lin 		ctlr->num_chipselect = num_cs;
870eb1262e3SChris Ruehl 		ctlr->use_gpio_descriptors = true;
871d065f41aSChris Ruehl 	}
872d66571a2SChris Ruehl 	ctlr->dev.of_node = pdev->dev.of_node;
873d66571a2SChris Ruehl 	ctlr->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4);
874d66571a2SChris Ruehl 	ctlr->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX;
875d66571a2SChris Ruehl 	ctlr->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT);
87664e36824Saddy ke 
8773a4bf922SJon Lin 	ctlr->setup = rockchip_spi_setup;
878d66571a2SChris Ruehl 	ctlr->set_cs = rockchip_spi_set_cs;
879d66571a2SChris Ruehl 	ctlr->transfer_one = rockchip_spi_transfer_one;
880d66571a2SChris Ruehl 	ctlr->max_transfer_size = rockchip_spi_max_transfer_size;
881d66571a2SChris Ruehl 	ctlr->handle_err = rockchip_spi_handle_err;
88264e36824Saddy ke 
883d66571a2SChris Ruehl 	ctlr->dma_tx = dma_request_chan(rs->dev, "tx");
884d66571a2SChris Ruehl 	if (IS_ERR(ctlr->dma_tx)) {
88561cadcf4SShawn Lin 		/* Check tx to see if we need defer probing driver */
886d66571a2SChris Ruehl 		if (PTR_ERR(ctlr->dma_tx) == -EPROBE_DEFER) {
88761cadcf4SShawn Lin 			ret = -EPROBE_DEFER;
888c351587eSJeffy Chen 			goto err_disable_pm_runtime;
88961cadcf4SShawn Lin 		}
89064e36824Saddy ke 		dev_warn(rs->dev, "Failed to request TX DMA channel\n");
891d66571a2SChris Ruehl 		ctlr->dma_tx = NULL;
89264e36824Saddy ke 	}
893e4c0e06fSShawn Lin 
894d66571a2SChris Ruehl 	ctlr->dma_rx = dma_request_chan(rs->dev, "rx");
895d66571a2SChris Ruehl 	if (IS_ERR(ctlr->dma_rx)) {
896d66571a2SChris Ruehl 		if (PTR_ERR(ctlr->dma_rx) == -EPROBE_DEFER) {
897e4c0e06fSShawn Lin 			ret = -EPROBE_DEFER;
8985de7ed0cSDan Carpenter 			goto err_free_dma_tx;
899e4c0e06fSShawn Lin 		}
90064e36824Saddy ke 		dev_warn(rs->dev, "Failed to request RX DMA channel\n");
901d66571a2SChris Ruehl 		ctlr->dma_rx = NULL;
90264e36824Saddy ke 	}
90364e36824Saddy ke 
904d66571a2SChris Ruehl 	if (ctlr->dma_tx && ctlr->dma_rx) {
905eee06a9eSEmil Renner Berthing 		rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR;
906eee06a9eSEmil Renner Berthing 		rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR;
907d66571a2SChris Ruehl 		ctlr->can_dma = rockchip_spi_can_dma;
90864e36824Saddy ke 	}
90964e36824Saddy ke 
910736b81e0SJon Lin 	switch (readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION)) {
911736b81e0SJon Lin 	case ROCKCHIP_SPI_VER2_TYPE2:
912d5d933f0SLuca Ceresoli 		rs->cs_high_supported = true;
913736b81e0SJon Lin 		ctlr->mode_bits |= SPI_CS_HIGH;
914869f2c94SJon Lin 		if (ctlr->can_dma && slave_mode)
915869f2c94SJon Lin 			rs->cs_inactive = true;
916869f2c94SJon Lin 		else
917869f2c94SJon Lin 			rs->cs_inactive = false;
918736b81e0SJon Lin 		break;
919736b81e0SJon Lin 	default:
920869f2c94SJon Lin 		rs->cs_inactive = false;
921736b81e0SJon Lin 		break;
922736b81e0SJon Lin 	}
923736b81e0SJon Lin 
924d66571a2SChris Ruehl 	ret = devm_spi_register_controller(&pdev->dev, ctlr);
92543de979dSJeffy Chen 	if (ret < 0) {
926d66571a2SChris Ruehl 		dev_err(&pdev->dev, "Failed to register controller\n");
927c351587eSJeffy Chen 		goto err_free_dma_rx;
92864e36824Saddy ke 	}
92964e36824Saddy ke 
93064e36824Saddy ke 	return 0;
93164e36824Saddy ke 
932c351587eSJeffy Chen err_free_dma_rx:
933d66571a2SChris Ruehl 	if (ctlr->dma_rx)
934d66571a2SChris Ruehl 		dma_release_channel(ctlr->dma_rx);
9355de7ed0cSDan Carpenter err_free_dma_tx:
936d66571a2SChris Ruehl 	if (ctlr->dma_tx)
937d66571a2SChris Ruehl 		dma_release_channel(ctlr->dma_tx);
938c351587eSJeffy Chen err_disable_pm_runtime:
939c351587eSJeffy Chen 	pm_runtime_disable(&pdev->dev);
940c351587eSJeffy Chen err_disable_spiclk:
94164e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
942c351587eSJeffy Chen err_disable_apbclk:
94364e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
944d66571a2SChris Ruehl err_put_ctlr:
945d66571a2SChris Ruehl 	spi_controller_put(ctlr);
94664e36824Saddy ke 
94764e36824Saddy ke 	return ret;
94864e36824Saddy ke }
94964e36824Saddy ke 
95064e36824Saddy ke static int rockchip_spi_remove(struct platform_device *pdev)
95164e36824Saddy ke {
952d66571a2SChris Ruehl 	struct spi_controller *ctlr = spi_controller_get(platform_get_drvdata(pdev));
953d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
95464e36824Saddy ke 
9556a06e895SJeffy Chen 	pm_runtime_get_sync(&pdev->dev);
95664e36824Saddy ke 
95764e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
95864e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
95964e36824Saddy ke 
9606a06e895SJeffy Chen 	pm_runtime_put_noidle(&pdev->dev);
9616a06e895SJeffy Chen 	pm_runtime_disable(&pdev->dev);
9626a06e895SJeffy Chen 	pm_runtime_set_suspended(&pdev->dev);
9636a06e895SJeffy Chen 
964d66571a2SChris Ruehl 	if (ctlr->dma_tx)
965d66571a2SChris Ruehl 		dma_release_channel(ctlr->dma_tx);
966d66571a2SChris Ruehl 	if (ctlr->dma_rx)
967d66571a2SChris Ruehl 		dma_release_channel(ctlr->dma_rx);
96864e36824Saddy ke 
969d66571a2SChris Ruehl 	spi_controller_put(ctlr);
970844c9f47SShawn Lin 
97164e36824Saddy ke 	return 0;
97264e36824Saddy ke }
97364e36824Saddy ke 
97464e36824Saddy ke #ifdef CONFIG_PM_SLEEP
97564e36824Saddy ke static int rockchip_spi_suspend(struct device *dev)
97664e36824Saddy ke {
97743de979dSJeffy Chen 	int ret;
978d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_get_drvdata(dev);
979e882575eSshengfei Xu 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
98064e36824Saddy ke 
981d66571a2SChris Ruehl 	ret = spi_controller_suspend(ctlr);
98243de979dSJeffy Chen 	if (ret < 0)
98364e36824Saddy ke 		return ret;
98464e36824Saddy ke 
985e882575eSshengfei Xu 	clk_disable_unprepare(rs->spiclk);
986e882575eSshengfei Xu 	clk_disable_unprepare(rs->apb_pclk);
98764e36824Saddy ke 
98823e291c2SBrian Norris 	pinctrl_pm_select_sleep_state(dev);
98923e291c2SBrian Norris 
99043de979dSJeffy Chen 	return 0;
99164e36824Saddy ke }
99264e36824Saddy ke 
99364e36824Saddy ke static int rockchip_spi_resume(struct device *dev)
99464e36824Saddy ke {
99543de979dSJeffy Chen 	int ret;
996d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_get_drvdata(dev);
997d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
99864e36824Saddy ke 
99923e291c2SBrian Norris 	pinctrl_pm_select_default_state(dev);
100023e291c2SBrian Norris 
1001e882575eSshengfei Xu 	ret = clk_prepare_enable(rs->apb_pclk);
100264e36824Saddy ke 	if (ret < 0)
100364e36824Saddy ke 		return ret;
100464e36824Saddy ke 
1005e882575eSshengfei Xu 	ret = clk_prepare_enable(rs->spiclk);
1006e882575eSshengfei Xu 	if (ret < 0)
1007e882575eSshengfei Xu 		clk_disable_unprepare(rs->apb_pclk);
1008e882575eSshengfei Xu 
1009d66571a2SChris Ruehl 	ret = spi_controller_resume(ctlr);
101064e36824Saddy ke 	if (ret < 0) {
101164e36824Saddy ke 		clk_disable_unprepare(rs->spiclk);
101264e36824Saddy ke 		clk_disable_unprepare(rs->apb_pclk);
101364e36824Saddy ke 	}
101464e36824Saddy ke 
101543de979dSJeffy Chen 	return 0;
101664e36824Saddy ke }
101764e36824Saddy ke #endif /* CONFIG_PM_SLEEP */
101864e36824Saddy ke 
1019ec833050SRafael J. Wysocki #ifdef CONFIG_PM
102064e36824Saddy ke static int rockchip_spi_runtime_suspend(struct device *dev)
102164e36824Saddy ke {
1022d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_get_drvdata(dev);
1023d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
102464e36824Saddy ke 
102564e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
102664e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
102764e36824Saddy ke 
102864e36824Saddy ke 	return 0;
102964e36824Saddy ke }
103064e36824Saddy ke 
103164e36824Saddy ke static int rockchip_spi_runtime_resume(struct device *dev)
103264e36824Saddy ke {
103364e36824Saddy ke 	int ret;
1034d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_get_drvdata(dev);
1035d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
103664e36824Saddy ke 
103764e36824Saddy ke 	ret = clk_prepare_enable(rs->apb_pclk);
103843de979dSJeffy Chen 	if (ret < 0)
103964e36824Saddy ke 		return ret;
104064e36824Saddy ke 
104164e36824Saddy ke 	ret = clk_prepare_enable(rs->spiclk);
104243de979dSJeffy Chen 	if (ret < 0)
104364e36824Saddy ke 		clk_disable_unprepare(rs->apb_pclk);
104464e36824Saddy ke 
104543de979dSJeffy Chen 	return 0;
104664e36824Saddy ke }
1047ec833050SRafael J. Wysocki #endif /* CONFIG_PM */
104864e36824Saddy ke 
104964e36824Saddy ke static const struct dev_pm_ops rockchip_spi_pm = {
1050e882575eSshengfei Xu 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
105164e36824Saddy ke 	SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
105264e36824Saddy ke 			   rockchip_spi_runtime_resume, NULL)
105364e36824Saddy ke };
105464e36824Saddy ke 
105564e36824Saddy ke static const struct of_device_id rockchip_spi_dt_match[] = {
1056c6486eadSJohan Jonker 	{ .compatible = "rockchip,px30-spi", },
1057aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3036-spi", },
105864e36824Saddy ke 	{ .compatible = "rockchip,rk3066-spi", },
1059b839b785SAddy Ke 	{ .compatible = "rockchip,rk3188-spi", },
1060aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3228-spi", },
1061b839b785SAddy Ke 	{ .compatible = "rockchip,rk3288-spi", },
1062c6486eadSJohan Jonker 	{ .compatible = "rockchip,rk3308-spi", },
1063c6486eadSJohan Jonker 	{ .compatible = "rockchip,rk3328-spi", },
1064aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3368-spi", },
10659b7a5622SXu Jianqun 	{ .compatible = "rockchip,rk3399-spi", },
1066c6486eadSJohan Jonker 	{ .compatible = "rockchip,rv1108-spi", },
10670f4f58b8SJon Lin 	{ .compatible = "rockchip,rv1126-spi", },
106864e36824Saddy ke 	{ },
106964e36824Saddy ke };
107064e36824Saddy ke MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
107164e36824Saddy ke 
107264e36824Saddy ke static struct platform_driver rockchip_spi_driver = {
107364e36824Saddy ke 	.driver = {
107464e36824Saddy ke 		.name	= DRIVER_NAME,
107564e36824Saddy ke 		.pm = &rockchip_spi_pm,
107664e36824Saddy ke 		.of_match_table = of_match_ptr(rockchip_spi_dt_match),
107764e36824Saddy ke 	},
107864e36824Saddy ke 	.probe = rockchip_spi_probe,
107964e36824Saddy ke 	.remove = rockchip_spi_remove,
108064e36824Saddy ke };
108164e36824Saddy ke 
108264e36824Saddy ke module_platform_driver(rockchip_spi_driver);
108364e36824Saddy ke 
10845dcc44edSAddy Ke MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
108564e36824Saddy ke MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
108664e36824Saddy ke MODULE_LICENSE("GPL v2");
1087