xref: /openbmc/linux/drivers/spi/spi-rockchip.c (revision ca2478a7d974f38d29d27acb42a952c7f168916e)
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
1071a3ccff3SYang Yingliang #define CR0_OPM_HOST				0x0
1081a3ccff3SYang Yingliang #define CR0_OPM_TARGET				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)
1281a3ccff3SYang Yingliang #define SR_TARGET_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 
1541a3ccff3SYang Yingliang /* sclk_out: spi host 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 
1971a3ccff3SYang Yingliang 	bool target_abort;
1981a3ccff3SYang Yingliang 	bool cs_inactive; /* spi target 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 
spi_enable_chip(struct rockchip_spi * rs,bool enable)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 
wait_for_tx_idle(struct rockchip_spi * rs,bool target_mode)2091a3ccff3SYang Yingliang static inline void wait_for_tx_idle(struct rockchip_spi *rs, bool target_mode)
2102df08e78SAddy Ke {
2112df08e78SAddy Ke 	unsigned long timeout = jiffies + msecs_to_jiffies(5);
2122df08e78SAddy Ke 
2132df08e78SAddy Ke 	do {
2141a3ccff3SYang Yingliang 		if (target_mode) {
2151a3ccff3SYang Yingliang 			if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_TARGET_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 
get_fifo_len(struct rockchip_spi * rs)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 
rockchip_spi_set_cs(struct spi_device * spi,bool enable)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 */
2499e264f3fSAmit Kumar Mahapatra via Alsa-devel 	if (cs_asserted == rs->cs_asserted[spi_get_chipselect(spi, 0)])
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 
2569e264f3fSAmit Kumar Mahapatra via Alsa-devel 		if (spi_get_csgpiod(spi, 0))
257b8d42371SJon Lin 			ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, 1);
258b8d42371SJon Lin 		else
2599e264f3fSAmit Kumar Mahapatra via Alsa-devel 			ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER,
2609e264f3fSAmit Kumar Mahapatra via Alsa-devel 					      BIT(spi_get_chipselect(spi, 0)));
261aa099382SJeffy Chen 	} else {
2629e264f3fSAmit Kumar Mahapatra via Alsa-devel 		if (spi_get_csgpiod(spi, 0))
263b8d42371SJon Lin 			ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, 1);
264b8d42371SJon Lin 		else
2659e264f3fSAmit Kumar Mahapatra via Alsa-devel 			ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER,
2669e264f3fSAmit Kumar Mahapatra via Alsa-devel 					      BIT(spi_get_chipselect(spi, 0)));
26764e36824Saddy ke 
268aa099382SJeffy Chen 		/* Drop reference from when we first asserted CS */
269aa099382SJeffy Chen 		pm_runtime_put(rs->dev);
270aa099382SJeffy Chen 	}
27164e36824Saddy ke 
2729e264f3fSAmit Kumar Mahapatra via Alsa-devel 	rs->cs_asserted[spi_get_chipselect(spi, 0)] = cs_asserted;
27364e36824Saddy ke }
27464e36824Saddy ke 
rockchip_spi_handle_err(struct spi_controller * ctlr,struct spi_message * msg)275d66571a2SChris Ruehl static void rockchip_spi_handle_err(struct spi_controller *ctlr,
27664e36824Saddy ke 				    struct spi_message *msg)
27764e36824Saddy ke {
278d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
27964e36824Saddy ke 
280ce386100SEmil Renner Berthing 	/* stop running spi transfer
281ce386100SEmil Renner Berthing 	 * this also flushes both rx and tx fifos
2825dcc44edSAddy Ke 	 */
283ce386100SEmil Renner Berthing 	spi_enable_chip(rs, false);
284ce386100SEmil Renner Berthing 
2852fcdde56SJon Lin 	/* make sure all interrupts are masked and status cleared */
28601b59ce5SEmil Renner Berthing 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
2872fcdde56SJon Lin 	writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR);
28801b59ce5SEmil Renner Berthing 
289fab3e487SEmil Renner Berthing 	if (atomic_read(&rs->state) & TXDMA)
290d66571a2SChris Ruehl 		dmaengine_terminate_async(ctlr->dma_tx);
291fab3e487SEmil Renner Berthing 
292ce386100SEmil Renner Berthing 	if (atomic_read(&rs->state) & RXDMA)
293d66571a2SChris Ruehl 		dmaengine_terminate_async(ctlr->dma_rx);
29464e36824Saddy ke }
29564e36824Saddy ke 
rockchip_spi_pio_writer(struct rockchip_spi * rs)29664e36824Saddy ke static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
29764e36824Saddy ke {
29801b59ce5SEmil Renner Berthing 	u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
29901b59ce5SEmil Renner Berthing 	u32 words = min(rs->tx_left, tx_free);
30064e36824Saddy ke 
30101b59ce5SEmil Renner Berthing 	rs->tx_left -= words;
30201b59ce5SEmil Renner Berthing 	for (; words; words--) {
30301b59ce5SEmil Renner Berthing 		u32 txw;
30401b59ce5SEmil Renner Berthing 
30564e36824Saddy ke 		if (rs->n_bytes == 1)
30601b59ce5SEmil Renner Berthing 			txw = *(u8 *)rs->tx;
30764e36824Saddy ke 		else
30801b59ce5SEmil Renner Berthing 			txw = *(u16 *)rs->tx;
30964e36824Saddy ke 
31064e36824Saddy ke 		writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
31164e36824Saddy ke 		rs->tx += rs->n_bytes;
31264e36824Saddy ke 	}
31364e36824Saddy ke }
31464e36824Saddy ke 
rockchip_spi_pio_reader(struct rockchip_spi * rs)31564e36824Saddy ke static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
31664e36824Saddy ke {
31701b59ce5SEmil Renner Berthing 	u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
3184294e4acSJon Lin 	u32 rx_left = (rs->rx_left > words) ? rs->rx_left - words : 0;
31964e36824Saddy ke 
32001b59ce5SEmil Renner Berthing 	/* the hardware doesn't allow us to change fifo threshold
32101b59ce5SEmil Renner Berthing 	 * level while spi is enabled, so instead make sure to leave
32201b59ce5SEmil Renner Berthing 	 * enough words in the rx fifo to get the last interrupt
32301b59ce5SEmil Renner Berthing 	 * exactly when all words have been received
32401b59ce5SEmil Renner Berthing 	 */
32501b59ce5SEmil Renner Berthing 	if (rx_left) {
32601b59ce5SEmil Renner Berthing 		u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1;
32701b59ce5SEmil Renner Berthing 
32801b59ce5SEmil Renner Berthing 		if (rx_left < ftl) {
32901b59ce5SEmil Renner Berthing 			rx_left = ftl;
33001b59ce5SEmil Renner Berthing 			words = rs->rx_left - rx_left;
33101b59ce5SEmil Renner Berthing 		}
33201b59ce5SEmil Renner Berthing 	}
33301b59ce5SEmil Renner Berthing 
33401b59ce5SEmil Renner Berthing 	rs->rx_left = rx_left;
33501b59ce5SEmil Renner Berthing 	for (; words; words--) {
33601b59ce5SEmil Renner Berthing 		u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
33701b59ce5SEmil Renner Berthing 
33801b59ce5SEmil Renner Berthing 		if (!rs->rx)
33901b59ce5SEmil Renner Berthing 			continue;
34001b59ce5SEmil Renner Berthing 
34164e36824Saddy ke 		if (rs->n_bytes == 1)
34201b59ce5SEmil Renner Berthing 			*(u8 *)rs->rx = (u8)rxw;
34364e36824Saddy ke 		else
34401b59ce5SEmil Renner Berthing 			*(u16 *)rs->rx = (u16)rxw;
34564e36824Saddy ke 		rs->rx += rs->n_bytes;
3465dcc44edSAddy Ke 	}
34764e36824Saddy ke }
34864e36824Saddy ke 
rockchip_spi_isr(int irq,void * dev_id)34901b59ce5SEmil Renner Berthing static irqreturn_t rockchip_spi_isr(int irq, void *dev_id)
35064e36824Saddy ke {
351d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_id;
352d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
35364e36824Saddy ke 
3541a3ccff3SYang Yingliang 	/* When int_cs_inactive comes, spi target abort */
355869f2c94SJon Lin 	if (rs->cs_inactive && readl_relaxed(rs->regs + ROCKCHIP_SPI_IMR) & INT_CS_INACTIVE) {
3561a3ccff3SYang Yingliang 		ctlr->target_abort(ctlr);
357869f2c94SJon Lin 		writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
358869f2c94SJon Lin 		writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR);
359869f2c94SJon Lin 
360869f2c94SJon Lin 		return IRQ_HANDLED;
361869f2c94SJon Lin 	}
362869f2c94SJon Lin 
36301b59ce5SEmil Renner Berthing 	if (rs->tx_left)
36401b59ce5SEmil Renner Berthing 		rockchip_spi_pio_writer(rs);
36501b59ce5SEmil Renner Berthing 
36601b59ce5SEmil Renner Berthing 	rockchip_spi_pio_reader(rs);
36701b59ce5SEmil Renner Berthing 	if (!rs->rx_left) {
36801b59ce5SEmil Renner Berthing 		spi_enable_chip(rs, false);
36901b59ce5SEmil Renner Berthing 		writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
370869f2c94SJon Lin 		writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR);
371d66571a2SChris Ruehl 		spi_finalize_current_transfer(ctlr);
37201b59ce5SEmil Renner Berthing 	}
37301b59ce5SEmil Renner Berthing 
37401b59ce5SEmil Renner Berthing 	return IRQ_HANDLED;
37501b59ce5SEmil Renner Berthing }
37601b59ce5SEmil Renner Berthing 
rockchip_spi_prepare_irq(struct rockchip_spi * rs,struct spi_controller * ctlr,struct spi_transfer * xfer)37701b59ce5SEmil Renner Berthing static int rockchip_spi_prepare_irq(struct rockchip_spi *rs,
378869f2c94SJon Lin 				    struct spi_controller *ctlr,
37901b59ce5SEmil Renner Berthing 				    struct spi_transfer *xfer)
38001b59ce5SEmil Renner Berthing {
38101b59ce5SEmil Renner Berthing 	rs->tx = xfer->tx_buf;
38201b59ce5SEmil Renner Berthing 	rs->rx = xfer->rx_buf;
38301b59ce5SEmil Renner Berthing 	rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0;
38401b59ce5SEmil Renner Berthing 	rs->rx_left = xfer->len / rs->n_bytes;
38501b59ce5SEmil Renner Berthing 
386419bc8f6SJon Lin 	writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR);
387419bc8f6SJon Lin 
38830688e4eSEmil Renner Berthing 	spi_enable_chip(rs, true);
389a3c17402SEmil Renner Berthing 
39001b59ce5SEmil Renner Berthing 	if (rs->tx_left)
39164e36824Saddy ke 		rockchip_spi_pio_writer(rs);
39264e36824Saddy ke 
393419bc8f6SJon Lin 	if (rs->cs_inactive)
394419bc8f6SJon Lin 		writel_relaxed(INT_RF_FULL | INT_CS_INACTIVE, rs->regs + ROCKCHIP_SPI_IMR);
395419bc8f6SJon Lin 	else
396419bc8f6SJon Lin 		writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR);
397419bc8f6SJon Lin 
39801b59ce5SEmil Renner Berthing 	/* 1 means the transfer is in progress */
39901b59ce5SEmil Renner Berthing 	return 1;
40064e36824Saddy ke }
40164e36824Saddy ke 
rockchip_spi_dma_rxcb(void * data)40264e36824Saddy ke static void rockchip_spi_dma_rxcb(void *data)
40364e36824Saddy ke {
404d66571a2SChris Ruehl 	struct spi_controller *ctlr = data;
405d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
406fab3e487SEmil Renner Berthing 	int state = atomic_fetch_andnot(RXDMA, &rs->state);
40764e36824Saddy ke 
4081a3ccff3SYang Yingliang 	if (state & TXDMA && !rs->target_abort)
409fab3e487SEmil Renner Berthing 		return;
41064e36824Saddy ke 
411869f2c94SJon Lin 	if (rs->cs_inactive)
412869f2c94SJon Lin 		writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
413869f2c94SJon Lin 
41430688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
415d66571a2SChris Ruehl 	spi_finalize_current_transfer(ctlr);
416c28be31bSAddy Ke }
41764e36824Saddy ke 
rockchip_spi_dma_txcb(void * data)41864e36824Saddy ke static void rockchip_spi_dma_txcb(void *data)
41964e36824Saddy ke {
420d66571a2SChris Ruehl 	struct spi_controller *ctlr = data;
421d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
422fab3e487SEmil Renner Berthing 	int state = atomic_fetch_andnot(TXDMA, &rs->state);
423fab3e487SEmil Renner Berthing 
4241a3ccff3SYang Yingliang 	if (state & RXDMA && !rs->target_abort)
425fab3e487SEmil Renner Berthing 		return;
42664e36824Saddy ke 
4272df08e78SAddy Ke 	/* Wait until the FIFO data completely. */
4281a3ccff3SYang Yingliang 	wait_for_tx_idle(rs, ctlr->target);
4292df08e78SAddy Ke 
43030688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
431d66571a2SChris Ruehl 	spi_finalize_current_transfer(ctlr);
4322c2bc748SAddy Ke }
43364e36824Saddy ke 
rockchip_spi_calc_burst_size(u32 data_len)4344d9ca632SJon Lin static u32 rockchip_spi_calc_burst_size(u32 data_len)
4354d9ca632SJon Lin {
4364d9ca632SJon Lin 	u32 i;
4374d9ca632SJon Lin 
4384d9ca632SJon Lin 	/* burst size: 1, 2, 4, 8 */
4394d9ca632SJon Lin 	for (i = 1; i < 8; i <<= 1) {
4404d9ca632SJon Lin 		if (data_len & i)
4414d9ca632SJon Lin 			break;
4424d9ca632SJon Lin 	}
4434d9ca632SJon Lin 
4444d9ca632SJon Lin 	return i;
4454d9ca632SJon Lin }
4464d9ca632SJon Lin 
rockchip_spi_prepare_dma(struct rockchip_spi * rs,struct spi_controller * ctlr,struct spi_transfer * xfer)447fc1ad8eeSEmil Renner Berthing static int rockchip_spi_prepare_dma(struct rockchip_spi *rs,
448d66571a2SChris Ruehl 		struct spi_controller *ctlr, struct spi_transfer *xfer)
44964e36824Saddy ke {
45064e36824Saddy ke 	struct dma_async_tx_descriptor *rxdesc, *txdesc;
45164e36824Saddy ke 
452fab3e487SEmil Renner Berthing 	atomic_set(&rs->state, 0);
45364e36824Saddy ke 
454869f2c94SJon Lin 	rs->tx = xfer->tx_buf;
455869f2c94SJon Lin 	rs->rx = xfer->rx_buf;
456869f2c94SJon Lin 
45797cf5669SArnd Bergmann 	rxdesc = NULL;
458fc1ad8eeSEmil Renner Berthing 	if (xfer->rx_buf) {
45931bcb57bSEmil Renner Berthing 		struct dma_slave_config rxconf = {
46031bcb57bSEmil Renner Berthing 			.direction = DMA_DEV_TO_MEM,
461eee06a9eSEmil Renner Berthing 			.src_addr = rs->dma_addr_rx,
46231bcb57bSEmil Renner Berthing 			.src_addr_width = rs->n_bytes,
463869f2c94SJon Lin 			.src_maxburst = rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes),
46431bcb57bSEmil Renner Berthing 		};
46531bcb57bSEmil Renner Berthing 
466d66571a2SChris Ruehl 		dmaengine_slave_config(ctlr->dma_rx, &rxconf);
46764e36824Saddy ke 
4685dcc44edSAddy Ke 		rxdesc = dmaengine_prep_slave_sg(
469d66571a2SChris Ruehl 				ctlr->dma_rx,
470fc1ad8eeSEmil Renner Berthing 				xfer->rx_sg.sgl, xfer->rx_sg.nents,
471d9071b7eSEmil Renner Berthing 				DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
472ea984911SShawn Lin 		if (!rxdesc)
473ea984911SShawn Lin 			return -EINVAL;
47464e36824Saddy ke 
47564e36824Saddy ke 		rxdesc->callback = rockchip_spi_dma_rxcb;
476d66571a2SChris Ruehl 		rxdesc->callback_param = ctlr;
47764e36824Saddy ke 	}
47864e36824Saddy ke 
47997cf5669SArnd Bergmann 	txdesc = NULL;
480fc1ad8eeSEmil Renner Berthing 	if (xfer->tx_buf) {
48131bcb57bSEmil Renner Berthing 		struct dma_slave_config txconf = {
48231bcb57bSEmil Renner Berthing 			.direction = DMA_MEM_TO_DEV,
483eee06a9eSEmil Renner Berthing 			.dst_addr = rs->dma_addr_tx,
48431bcb57bSEmil Renner Berthing 			.dst_addr_width = rs->n_bytes,
48547300728SEmil Renner Berthing 			.dst_maxburst = rs->fifo_len / 4,
48631bcb57bSEmil Renner Berthing 		};
48731bcb57bSEmil Renner Berthing 
488d66571a2SChris Ruehl 		dmaengine_slave_config(ctlr->dma_tx, &txconf);
48964e36824Saddy ke 
4905dcc44edSAddy Ke 		txdesc = dmaengine_prep_slave_sg(
491d66571a2SChris Ruehl 				ctlr->dma_tx,
492fc1ad8eeSEmil Renner Berthing 				xfer->tx_sg.sgl, xfer->tx_sg.nents,
493d9071b7eSEmil Renner Berthing 				DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
494ea984911SShawn Lin 		if (!txdesc) {
495ea984911SShawn Lin 			if (rxdesc)
496d66571a2SChris Ruehl 				dmaengine_terminate_sync(ctlr->dma_rx);
497ea984911SShawn Lin 			return -EINVAL;
498ea984911SShawn Lin 		}
49964e36824Saddy ke 
50064e36824Saddy ke 		txdesc->callback = rockchip_spi_dma_txcb;
501d66571a2SChris Ruehl 		txdesc->callback_param = ctlr;
50264e36824Saddy ke 	}
50364e36824Saddy ke 
50464e36824Saddy ke 	/* rx must be started before tx due to spi instinct */
50597cf5669SArnd Bergmann 	if (rxdesc) {
506fab3e487SEmil Renner Berthing 		atomic_or(RXDMA, &rs->state);
507869f2c94SJon Lin 		ctlr->dma_rx->cookie = dmaengine_submit(rxdesc);
508d66571a2SChris Ruehl 		dma_async_issue_pending(ctlr->dma_rx);
50964e36824Saddy ke 	}
51064e36824Saddy ke 
511869f2c94SJon Lin 	if (rs->cs_inactive)
512869f2c94SJon Lin 		writel_relaxed(INT_CS_INACTIVE, rs->regs + ROCKCHIP_SPI_IMR);
513869f2c94SJon Lin 
51430688e4eSEmil Renner Berthing 	spi_enable_chip(rs, true);
515a3c17402SEmil Renner Berthing 
51697cf5669SArnd Bergmann 	if (txdesc) {
517fab3e487SEmil Renner Berthing 		atomic_or(TXDMA, &rs->state);
51864e36824Saddy ke 		dmaengine_submit(txdesc);
519d66571a2SChris Ruehl 		dma_async_issue_pending(ctlr->dma_tx);
52064e36824Saddy ke 	}
521ea984911SShawn Lin 
522a3c17402SEmil Renner Berthing 	/* 1 means the transfer is in progress */
523a3c17402SEmil Renner Berthing 	return 1;
52464e36824Saddy ke }
52564e36824Saddy ke 
rockchip_spi_config(struct rockchip_spi * rs,struct spi_device * spi,struct spi_transfer * xfer,bool use_dma,bool target_mode)526e5098952SArnd Bergmann static int rockchip_spi_config(struct rockchip_spi *rs,
527eff0275eSEmil Renner Berthing 		struct spi_device *spi, struct spi_transfer *xfer,
5281a3ccff3SYang Yingliang 		bool use_dma, bool target_mode)
52964e36824Saddy ke {
5302410d6a3SEmil Renner Berthing 	u32 cr0 = CR0_FRF_SPI  << CR0_FRF_OFFSET
5312410d6a3SEmil Renner Berthing 		| CR0_BHT_8BIT << CR0_BHT_OFFSET
5322410d6a3SEmil Renner Berthing 		| CR0_SSD_ONE  << CR0_SSD_OFFSET
5332410d6a3SEmil Renner Berthing 		| CR0_EM_BIG   << CR0_EM_OFFSET;
53465498c6aSEmil Renner Berthing 	u32 cr1;
53565498c6aSEmil Renner Berthing 	u32 dmacr = 0;
53664e36824Saddy ke 
5371a3ccff3SYang Yingliang 	if (target_mode)
5381a3ccff3SYang Yingliang 		cr0 |= CR0_OPM_TARGET << CR0_OPM_OFFSET;
5391a3ccff3SYang Yingliang 	rs->target_abort = false;
540d065f41aSChris Ruehl 
54174b7efa8SEmil Renner Berthing 	cr0 |= rs->rsd << CR0_RSD_OFFSET;
542fc1ad8eeSEmil Renner Berthing 	cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET;
54304290192SEmil Renner Berthing 	if (spi->mode & SPI_LSB_FIRST)
54404290192SEmil Renner Berthing 		cr0 |= CR0_FBM_LSB << CR0_FBM_OFFSET;
545736b81e0SJon Lin 	if (spi->mode & SPI_CS_HIGH)
5469e264f3fSAmit Kumar Mahapatra via Alsa-devel 		cr0 |= BIT(spi_get_chipselect(spi, 0)) << CR0_SOI_OFFSET;
547fc1ad8eeSEmil Renner Berthing 
548fc1ad8eeSEmil Renner Berthing 	if (xfer->rx_buf && xfer->tx_buf)
549fc1ad8eeSEmil Renner Berthing 		cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET;
550fc1ad8eeSEmil Renner Berthing 	else if (xfer->rx_buf)
551fc1ad8eeSEmil Renner Berthing 		cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET;
55201b59ce5SEmil Renner Berthing 	else if (use_dma)
553fc1ad8eeSEmil Renner Berthing 		cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET;
55464e36824Saddy ke 
55565498c6aSEmil Renner Berthing 	switch (xfer->bits_per_word) {
55665498c6aSEmil Renner Berthing 	case 4:
55765498c6aSEmil Renner Berthing 		cr0 |= CR0_DFS_4BIT << CR0_DFS_OFFSET;
55865498c6aSEmil Renner Berthing 		cr1 = xfer->len - 1;
55965498c6aSEmil Renner Berthing 		break;
56065498c6aSEmil Renner Berthing 	case 8:
56165498c6aSEmil Renner Berthing 		cr0 |= CR0_DFS_8BIT << CR0_DFS_OFFSET;
56265498c6aSEmil Renner Berthing 		cr1 = xfer->len - 1;
56365498c6aSEmil Renner Berthing 		break;
56465498c6aSEmil Renner Berthing 	case 16:
56565498c6aSEmil Renner Berthing 		cr0 |= CR0_DFS_16BIT << CR0_DFS_OFFSET;
56665498c6aSEmil Renner Berthing 		cr1 = xfer->len / 2 - 1;
56765498c6aSEmil Renner Berthing 		break;
56865498c6aSEmil Renner Berthing 	default:
56965498c6aSEmil Renner Berthing 		/* we only whitelist 4, 8 and 16 bit words in
570d66571a2SChris Ruehl 		 * ctlr->bits_per_word_mask, so this shouldn't
57165498c6aSEmil Renner Berthing 		 * happen
57265498c6aSEmil Renner Berthing 		 */
573e5098952SArnd Bergmann 		dev_err(rs->dev, "unknown bits per word: %d\n",
574e5098952SArnd Bergmann 			xfer->bits_per_word);
575e5098952SArnd Bergmann 		return -EINVAL;
57665498c6aSEmil Renner Berthing 	}
57765498c6aSEmil Renner Berthing 
578eff0275eSEmil Renner Berthing 	if (use_dma) {
579fc1ad8eeSEmil Renner Berthing 		if (xfer->tx_buf)
58064e36824Saddy ke 			dmacr |= TF_DMA_EN;
581fc1ad8eeSEmil Renner Berthing 		if (xfer->rx_buf)
58264e36824Saddy ke 			dmacr |= RF_DMA_EN;
58364e36824Saddy ke 	}
58464e36824Saddy ke 
58564e36824Saddy ke 	writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
58665498c6aSEmil Renner Berthing 	writel_relaxed(cr1, rs->regs + ROCKCHIP_SPI_CTRLR1);
58704b37d2dSHuibin Hong 
58801b59ce5SEmil Renner Berthing 	/* unfortunately setting the fifo threshold level to generate an
58901b59ce5SEmil Renner Berthing 	 * interrupt exactly when the fifo is full doesn't seem to work,
59001b59ce5SEmil Renner Berthing 	 * so we need the strict inequality here
59101b59ce5SEmil Renner Berthing 	 */
5924a47fcdbSJon Lin 	if ((xfer->len / rs->n_bytes) < rs->fifo_len)
5934a47fcdbSJon Lin 		writel_relaxed(xfer->len / rs->n_bytes - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
59401b59ce5SEmil Renner Berthing 	else
59564e36824Saddy ke 		writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
59664e36824Saddy ke 
5972758bd09SJon Lin 	writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_DMATDLR);
5984d9ca632SJon Lin 	writel_relaxed(rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes) - 1,
5994d9ca632SJon Lin 		       rs->regs + ROCKCHIP_SPI_DMARDLR);
60064e36824Saddy ke 	writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
60164e36824Saddy ke 
602420b82f8SEmil Renner Berthing 	/* the hardware only supports an even clock divisor, so
603420b82f8SEmil Renner Berthing 	 * round divisor = spiclk / speed up to nearest even number
604420b82f8SEmil Renner Berthing 	 * so that the resulting speed is <= the requested speed
605420b82f8SEmil Renner Berthing 	 */
606420b82f8SEmil Renner Berthing 	writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz),
607420b82f8SEmil Renner Berthing 			rs->regs + ROCKCHIP_SPI_BAUDR);
608e5098952SArnd Bergmann 
609e5098952SArnd Bergmann 	return 0;
61064e36824Saddy ke }
61164e36824Saddy ke 
rockchip_spi_max_transfer_size(struct spi_device * spi)6125185a81cSBrian Norris static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
6135185a81cSBrian Norris {
6145185a81cSBrian Norris 	return ROCKCHIP_SPI_MAX_TRANLEN;
6155185a81cSBrian Norris }
6165185a81cSBrian Norris 
rockchip_spi_target_abort(struct spi_controller * ctlr)6171a3ccff3SYang Yingliang static int rockchip_spi_target_abort(struct spi_controller *ctlr)
618d065f41aSChris Ruehl {
619d065f41aSChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
620869f2c94SJon Lin 	u32 rx_fifo_left;
621869f2c94SJon Lin 	struct dma_tx_state state;
622869f2c94SJon Lin 	enum dma_status status;
623d065f41aSChris Ruehl 
624869f2c94SJon Lin 	/* Get current dma rx point */
625869f2c94SJon Lin 	if (atomic_read(&rs->state) & RXDMA) {
626869f2c94SJon Lin 		dmaengine_pause(ctlr->dma_rx);
627869f2c94SJon Lin 		status = dmaengine_tx_status(ctlr->dma_rx, ctlr->dma_rx->cookie, &state);
628869f2c94SJon Lin 		if (status == DMA_ERROR) {
629869f2c94SJon Lin 			rs->rx = rs->xfer->rx_buf;
630869f2c94SJon Lin 			rs->xfer->len = 0;
631869f2c94SJon Lin 			rx_fifo_left = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
632869f2c94SJon Lin 			for (; rx_fifo_left; rx_fifo_left--)
633869f2c94SJon Lin 				readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
634869f2c94SJon Lin 			goto out;
635869f2c94SJon Lin 		} else {
636869f2c94SJon Lin 			rs->rx += rs->xfer->len - rs->n_bytes * state.residue;
637869f2c94SJon Lin 		}
638869f2c94SJon Lin 	}
639869f2c94SJon Lin 
640869f2c94SJon Lin 	/* Get the valid data left in rx fifo and set rs->xfer->len real rx size */
641869f2c94SJon Lin 	if (rs->rx) {
642869f2c94SJon Lin 		rx_fifo_left = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
643869f2c94SJon Lin 		for (; rx_fifo_left; rx_fifo_left--) {
644869f2c94SJon Lin 			u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
645869f2c94SJon Lin 
646869f2c94SJon Lin 			if (rs->n_bytes == 1)
647869f2c94SJon Lin 				*(u8 *)rs->rx = (u8)rxw;
648869f2c94SJon Lin 			else
649869f2c94SJon Lin 				*(u16 *)rs->rx = (u16)rxw;
650869f2c94SJon Lin 			rs->rx += rs->n_bytes;
651869f2c94SJon Lin 		}
652869f2c94SJon Lin 		rs->xfer->len = (unsigned int)(rs->rx - rs->xfer->rx_buf);
653869f2c94SJon Lin 	}
654869f2c94SJon Lin 
655869f2c94SJon Lin out:
65680808768SJon Lin 	if (atomic_read(&rs->state) & RXDMA)
65780808768SJon Lin 		dmaengine_terminate_sync(ctlr->dma_rx);
65880808768SJon Lin 	if (atomic_read(&rs->state) & TXDMA)
65980808768SJon Lin 		dmaengine_terminate_sync(ctlr->dma_tx);
66080808768SJon Lin 	atomic_set(&rs->state, 0);
66180808768SJon Lin 	spi_enable_chip(rs, false);
6621a3ccff3SYang Yingliang 	rs->target_abort = true;
6636bd2c867SVincent Pelletier 	spi_finalize_current_transfer(ctlr);
664d065f41aSChris Ruehl 
665d065f41aSChris Ruehl 	return 0;
666d065f41aSChris Ruehl }
667d065f41aSChris Ruehl 
rockchip_spi_transfer_one(struct spi_controller * ctlr,struct spi_device * spi,struct spi_transfer * xfer)6685dcc44edSAddy Ke static int rockchip_spi_transfer_one(
669d66571a2SChris Ruehl 		struct spi_controller *ctlr,
67064e36824Saddy ke 		struct spi_device *spi,
67164e36824Saddy ke 		struct spi_transfer *xfer)
67264e36824Saddy ke {
673d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
674e5098952SArnd Bergmann 	int ret;
675eff0275eSEmil Renner Berthing 	bool use_dma;
67664e36824Saddy ke 
6775457773eSTobias Schramm 	/* Zero length transfers won't trigger an interrupt on completion */
6785457773eSTobias Schramm 	if (!xfer->len) {
6795457773eSTobias Schramm 		spi_finalize_current_transfer(ctlr);
6805457773eSTobias Schramm 		return 1;
6815457773eSTobias Schramm 	}
6825457773eSTobias Schramm 
68362946172SDoug Anderson 	WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
68462946172SDoug Anderson 		(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
68564e36824Saddy ke 
68664e36824Saddy ke 	if (!xfer->tx_buf && !xfer->rx_buf) {
68764e36824Saddy ke 		dev_err(rs->dev, "No buffer for transfer\n");
68864e36824Saddy ke 		return -EINVAL;
68964e36824Saddy ke 	}
69064e36824Saddy ke 
6915185a81cSBrian Norris 	if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
6925185a81cSBrian Norris 		dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
6935185a81cSBrian Norris 		return -EINVAL;
6945185a81cSBrian Norris 	}
6955185a81cSBrian Norris 
69665498c6aSEmil Renner Berthing 	rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2;
697869f2c94SJon Lin 	rs->xfer = xfer;
698d66571a2SChris Ruehl 	use_dma = ctlr->can_dma ? ctlr->can_dma(ctlr, spi, xfer) : false;
69964e36824Saddy ke 
7001a3ccff3SYang Yingliang 	ret = rockchip_spi_config(rs, spi, xfer, use_dma, ctlr->target);
701e5098952SArnd Bergmann 	if (ret)
702e5098952SArnd Bergmann 		return ret;
70364e36824Saddy ke 
704eff0275eSEmil Renner Berthing 	if (use_dma)
705d66571a2SChris Ruehl 		return rockchip_spi_prepare_dma(rs, ctlr, xfer);
70664e36824Saddy ke 
707869f2c94SJon Lin 	return rockchip_spi_prepare_irq(rs, ctlr, xfer);
70864e36824Saddy ke }
70964e36824Saddy ke 
rockchip_spi_can_dma(struct spi_controller * ctlr,struct spi_device * spi,struct spi_transfer * xfer)710d66571a2SChris Ruehl static bool rockchip_spi_can_dma(struct spi_controller *ctlr,
71164e36824Saddy ke 				 struct spi_device *spi,
71264e36824Saddy ke 				 struct spi_transfer *xfer)
71364e36824Saddy ke {
714d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
71501b59ce5SEmil Renner Berthing 	unsigned int bytes_per_word = xfer->bits_per_word <= 8 ? 1 : 2;
71664e36824Saddy ke 
71701b59ce5SEmil Renner Berthing 	/* if the numbor of spi words to transfer is less than the fifo
71801b59ce5SEmil Renner Berthing 	 * length we can just fill the fifo and wait for a single irq,
71901b59ce5SEmil Renner Berthing 	 * so don't bother setting up dma
72001b59ce5SEmil Renner Berthing 	 */
72101b59ce5SEmil Renner Berthing 	return xfer->len / bytes_per_word >= rs->fifo_len;
72264e36824Saddy ke }
72364e36824Saddy ke 
rockchip_spi_setup(struct spi_device * spi)7243a4bf922SJon Lin static int rockchip_spi_setup(struct spi_device *spi)
7253a4bf922SJon Lin {
7263a4bf922SJon Lin 	struct rockchip_spi *rs = spi_controller_get_devdata(spi->controller);
7273a4bf922SJon Lin 	u32 cr0;
7283a4bf922SJon Lin 
7299e264f3fSAmit Kumar Mahapatra via Alsa-devel 	if (!spi_get_csgpiod(spi, 0) && (spi->mode & SPI_CS_HIGH) && !rs->cs_high_supported) {
730d5d933f0SLuca Ceresoli 		dev_warn(&spi->dev, "setup: non GPIO CS can't be active-high\n");
731d5d933f0SLuca Ceresoli 		return -EINVAL;
732d5d933f0SLuca Ceresoli 	}
733d5d933f0SLuca Ceresoli 
7343a4bf922SJon Lin 	pm_runtime_get_sync(rs->dev);
7353a4bf922SJon Lin 
7363a4bf922SJon Lin 	cr0 = readl_relaxed(rs->regs + ROCKCHIP_SPI_CTRLR0);
7373a4bf922SJon Lin 
7383a4bf922SJon Lin 	cr0 &= ~(0x3 << CR0_SCPH_OFFSET);
7393a4bf922SJon Lin 	cr0 |= ((spi->mode & 0x3) << CR0_SCPH_OFFSET);
7409e264f3fSAmit Kumar Mahapatra via Alsa-devel 	if (spi->mode & SPI_CS_HIGH && spi_get_chipselect(spi, 0) <= 1)
7419e264f3fSAmit Kumar Mahapatra via Alsa-devel 		cr0 |= BIT(spi_get_chipselect(spi, 0)) << CR0_SOI_OFFSET;
7429e264f3fSAmit Kumar Mahapatra via Alsa-devel 	else if (spi_get_chipselect(spi, 0) <= 1)
7439e264f3fSAmit Kumar Mahapatra via Alsa-devel 		cr0 &= ~(BIT(spi_get_chipselect(spi, 0)) << CR0_SOI_OFFSET);
7443a4bf922SJon Lin 
7453a4bf922SJon Lin 	writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
7463a4bf922SJon Lin 
7473a4bf922SJon Lin 	pm_runtime_put(rs->dev);
7483a4bf922SJon Lin 
7493a4bf922SJon Lin 	return 0;
7503a4bf922SJon Lin }
7513a4bf922SJon Lin 
rockchip_spi_probe(struct platform_device * pdev)75264e36824Saddy ke static int rockchip_spi_probe(struct platform_device *pdev)
75364e36824Saddy ke {
75443de979dSJeffy Chen 	int ret;
75564e36824Saddy ke 	struct rockchip_spi *rs;
756d66571a2SChris Ruehl 	struct spi_controller *ctlr;
75764e36824Saddy ke 	struct resource *mem;
758d065f41aSChris Ruehl 	struct device_node *np = pdev->dev.of_node;
7599382df0aSJon Lin 	u32 rsd_nsecs, num_cs;
7601a3ccff3SYang Yingliang 	bool target_mode;
76164e36824Saddy ke 
7621a3ccff3SYang Yingliang 	target_mode = of_property_read_bool(np, "spi-slave");
763d065f41aSChris Ruehl 
7641a3ccff3SYang Yingliang 	if (target_mode)
7651a3ccff3SYang Yingliang 		ctlr = spi_alloc_target(&pdev->dev,
766d065f41aSChris Ruehl 				sizeof(struct rockchip_spi));
767d065f41aSChris Ruehl 	else
7681a3ccff3SYang Yingliang 		ctlr = spi_alloc_host(&pdev->dev,
769d065f41aSChris Ruehl 				sizeof(struct rockchip_spi));
770d065f41aSChris Ruehl 
771d66571a2SChris Ruehl 	if (!ctlr)
77264e36824Saddy ke 		return -ENOMEM;
7735dcc44edSAddy Ke 
774d66571a2SChris Ruehl 	platform_set_drvdata(pdev, ctlr);
77564e36824Saddy ke 
776d66571a2SChris Ruehl 	rs = spi_controller_get_devdata(ctlr);
77764e36824Saddy ke 
77864e36824Saddy ke 	/* Get basic io resource and map it */
779d447fa65SLizhe 	rs->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &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;
8571a3ccff3SYang Yingliang 	if (target_mode) {
858d065f41aSChris Ruehl 		ctlr->mode_bits |= SPI_NO_CS;
8591a3ccff3SYang Yingliang 		ctlr->target_abort = rockchip_spi_target_abort;
860d065f41aSChris Ruehl 	} else {
86182238d2cSAndy Shevchenko 		ctlr->flags = SPI_CONTROLLER_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;
9141a3ccff3SYang Yingliang 		if (ctlr->can_dma && target_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 
rockchip_spi_remove(struct platform_device * pdev)9505ff5e676SUwe Kleine-König static void 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);
97064e36824Saddy ke }
97164e36824Saddy ke 
97264e36824Saddy ke #ifdef CONFIG_PM_SLEEP
rockchip_spi_suspend(struct device * dev)97364e36824Saddy ke static int rockchip_spi_suspend(struct device *dev)
97464e36824Saddy ke {
97543de979dSJeffy Chen 	int ret;
976d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_get_drvdata(dev);
97764e36824Saddy ke 
978d66571a2SChris Ruehl 	ret = spi_controller_suspend(ctlr);
97943de979dSJeffy Chen 	if (ret < 0)
98064e36824Saddy ke 		return ret;
98164e36824Saddy ke 
982*d034bff6SBrian Norris 	ret = pm_runtime_force_suspend(dev);
983*d034bff6SBrian Norris 	if (ret < 0) {
984*d034bff6SBrian Norris 		spi_controller_resume(ctlr);
985*d034bff6SBrian Norris 		return ret;
986*d034bff6SBrian Norris 	}
98764e36824Saddy ke 
98823e291c2SBrian Norris 	pinctrl_pm_select_sleep_state(dev);
98923e291c2SBrian Norris 
99043de979dSJeffy Chen 	return 0;
99164e36824Saddy ke }
99264e36824Saddy ke 
rockchip_spi_resume(struct device * dev)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);
99764e36824Saddy ke 
99823e291c2SBrian Norris 	pinctrl_pm_select_default_state(dev);
99923e291c2SBrian Norris 
1000*d034bff6SBrian Norris 	ret = pm_runtime_force_resume(dev);
100164e36824Saddy ke 	if (ret < 0)
100264e36824Saddy ke 		return ret;
100364e36824Saddy ke 
1004*d034bff6SBrian Norris 	return spi_controller_resume(ctlr);
100564e36824Saddy ke }
100664e36824Saddy ke #endif /* CONFIG_PM_SLEEP */
100764e36824Saddy ke 
1008ec833050SRafael J. Wysocki #ifdef CONFIG_PM
rockchip_spi_runtime_suspend(struct device * dev)100964e36824Saddy ke static int rockchip_spi_runtime_suspend(struct device *dev)
101064e36824Saddy ke {
1011d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_get_drvdata(dev);
1012d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
101364e36824Saddy ke 
101464e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
101564e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
101664e36824Saddy ke 
101764e36824Saddy ke 	return 0;
101864e36824Saddy ke }
101964e36824Saddy ke 
rockchip_spi_runtime_resume(struct device * dev)102064e36824Saddy ke static int rockchip_spi_runtime_resume(struct device *dev)
102164e36824Saddy ke {
102264e36824Saddy ke 	int ret;
1023d66571a2SChris Ruehl 	struct spi_controller *ctlr = dev_get_drvdata(dev);
1024d66571a2SChris Ruehl 	struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
102564e36824Saddy ke 
102664e36824Saddy ke 	ret = clk_prepare_enable(rs->apb_pclk);
102743de979dSJeffy Chen 	if (ret < 0)
102864e36824Saddy ke 		return ret;
102964e36824Saddy ke 
103064e36824Saddy ke 	ret = clk_prepare_enable(rs->spiclk);
103143de979dSJeffy Chen 	if (ret < 0)
103264e36824Saddy ke 		clk_disable_unprepare(rs->apb_pclk);
103364e36824Saddy ke 
103443de979dSJeffy Chen 	return 0;
103564e36824Saddy ke }
1036ec833050SRafael J. Wysocki #endif /* CONFIG_PM */
103764e36824Saddy ke 
103864e36824Saddy ke static const struct dev_pm_ops rockchip_spi_pm = {
1039e882575eSshengfei Xu 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
104064e36824Saddy ke 	SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
104164e36824Saddy ke 			   rockchip_spi_runtime_resume, NULL)
104264e36824Saddy ke };
104364e36824Saddy ke 
104464e36824Saddy ke static const struct of_device_id rockchip_spi_dt_match[] = {
1045c6486eadSJohan Jonker 	{ .compatible = "rockchip,px30-spi", },
1046aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3036-spi", },
104764e36824Saddy ke 	{ .compatible = "rockchip,rk3066-spi", },
1048b839b785SAddy Ke 	{ .compatible = "rockchip,rk3188-spi", },
1049aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3228-spi", },
1050b839b785SAddy Ke 	{ .compatible = "rockchip,rk3288-spi", },
1051c6486eadSJohan Jonker 	{ .compatible = "rockchip,rk3308-spi", },
1052c6486eadSJohan Jonker 	{ .compatible = "rockchip,rk3328-spi", },
1053aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3368-spi", },
10549b7a5622SXu Jianqun 	{ .compatible = "rockchip,rk3399-spi", },
1055c6486eadSJohan Jonker 	{ .compatible = "rockchip,rv1108-spi", },
10560f4f58b8SJon Lin 	{ .compatible = "rockchip,rv1126-spi", },
105764e36824Saddy ke 	{ },
105864e36824Saddy ke };
105964e36824Saddy ke MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
106064e36824Saddy ke 
106164e36824Saddy ke static struct platform_driver rockchip_spi_driver = {
106264e36824Saddy ke 	.driver = {
106364e36824Saddy ke 		.name	= DRIVER_NAME,
106464e36824Saddy ke 		.pm = &rockchip_spi_pm,
106564e36824Saddy ke 		.of_match_table = of_match_ptr(rockchip_spi_dt_match),
106664e36824Saddy ke 	},
106764e36824Saddy ke 	.probe = rockchip_spi_probe,
10685ff5e676SUwe Kleine-König 	.remove_new = rockchip_spi_remove,
106964e36824Saddy ke };
107064e36824Saddy ke 
107164e36824Saddy ke module_platform_driver(rockchip_spi_driver);
107264e36824Saddy ke 
10735dcc44edSAddy Ke MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
107464e36824Saddy ke MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
107564e36824Saddy ke MODULE_LICENSE("GPL v2");
1076