xref: /linux/drivers/spi/spi-rockchip.c (revision 31bcb57be12fd815a9051f07d64334809b8cb472)
164e36824Saddy ke /*
264e36824Saddy ke  * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
35dcc44edSAddy Ke  * Author: Addy Ke <addy.ke@rock-chips.com>
464e36824Saddy ke  *
564e36824Saddy ke  * This program is free software; you can redistribute it and/or modify it
664e36824Saddy ke  * under the terms and conditions of the GNU General Public License,
764e36824Saddy ke  * version 2, as published by the Free Software Foundation.
864e36824Saddy ke  *
964e36824Saddy ke  * This program is distributed in the hope it will be useful, but WITHOUT
1064e36824Saddy ke  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1164e36824Saddy ke  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1264e36824Saddy ke  * more details.
1364e36824Saddy ke  *
1464e36824Saddy ke  */
1564e36824Saddy ke 
1664e36824Saddy ke #include <linux/clk.h>
1764e36824Saddy ke #include <linux/dmaengine.h>
18ec5c5d8aSShawn Lin #include <linux/module.h>
19ec5c5d8aSShawn Lin #include <linux/of.h>
2023e291c2SBrian Norris #include <linux/pinctrl/consumer.h>
21ec5c5d8aSShawn Lin #include <linux/platform_device.h>
22ec5c5d8aSShawn Lin #include <linux/spi/spi.h>
23ec5c5d8aSShawn Lin #include <linux/pm_runtime.h>
24ec5c5d8aSShawn Lin #include <linux/scatterlist.h>
2564e36824Saddy ke 
2664e36824Saddy ke #define DRIVER_NAME "rockchip-spi"
2764e36824Saddy ke 
28aa099382SJeffy Chen #define ROCKCHIP_SPI_CLR_BITS(reg, bits) \
29aa099382SJeffy Chen 		writel_relaxed(readl_relaxed(reg) & ~(bits), reg)
30aa099382SJeffy Chen #define ROCKCHIP_SPI_SET_BITS(reg, bits) \
31aa099382SJeffy Chen 		writel_relaxed(readl_relaxed(reg) | (bits), reg)
32aa099382SJeffy Chen 
3364e36824Saddy ke /* SPI register offsets */
3464e36824Saddy ke #define ROCKCHIP_SPI_CTRLR0			0x0000
3564e36824Saddy ke #define ROCKCHIP_SPI_CTRLR1			0x0004
3664e36824Saddy ke #define ROCKCHIP_SPI_SSIENR			0x0008
3764e36824Saddy ke #define ROCKCHIP_SPI_SER			0x000c
3864e36824Saddy ke #define ROCKCHIP_SPI_BAUDR			0x0010
3964e36824Saddy ke #define ROCKCHIP_SPI_TXFTLR			0x0014
4064e36824Saddy ke #define ROCKCHIP_SPI_RXFTLR			0x0018
4164e36824Saddy ke #define ROCKCHIP_SPI_TXFLR			0x001c
4264e36824Saddy ke #define ROCKCHIP_SPI_RXFLR			0x0020
4364e36824Saddy ke #define ROCKCHIP_SPI_SR				0x0024
4464e36824Saddy ke #define ROCKCHIP_SPI_IPR			0x0028
4564e36824Saddy ke #define ROCKCHIP_SPI_IMR			0x002c
4664e36824Saddy ke #define ROCKCHIP_SPI_ISR			0x0030
4764e36824Saddy ke #define ROCKCHIP_SPI_RISR			0x0034
4864e36824Saddy ke #define ROCKCHIP_SPI_ICR			0x0038
4964e36824Saddy ke #define ROCKCHIP_SPI_DMACR			0x003c
5064e36824Saddy ke #define ROCKCHIP_SPI_DMATDLR		0x0040
5164e36824Saddy ke #define ROCKCHIP_SPI_DMARDLR		0x0044
5264e36824Saddy ke #define ROCKCHIP_SPI_TXDR			0x0400
5364e36824Saddy ke #define ROCKCHIP_SPI_RXDR			0x0800
5464e36824Saddy ke 
5564e36824Saddy ke /* Bit fields in CTRLR0 */
5664e36824Saddy ke #define CR0_DFS_OFFSET				0
5764e36824Saddy ke 
5864e36824Saddy ke #define CR0_CFS_OFFSET				2
5964e36824Saddy ke 
6064e36824Saddy ke #define CR0_SCPH_OFFSET				6
6164e36824Saddy ke 
6264e36824Saddy ke #define CR0_SCPOL_OFFSET			7
6364e36824Saddy ke 
6464e36824Saddy ke #define CR0_CSM_OFFSET				8
6564e36824Saddy ke #define CR0_CSM_KEEP				0x0
6664e36824Saddy ke /* ss_n be high for half sclk_out cycles */
6764e36824Saddy ke #define CR0_CSM_HALF				0X1
6864e36824Saddy ke /* ss_n be high for one sclk_out cycle */
6964e36824Saddy ke #define CR0_CSM_ONE					0x2
7064e36824Saddy ke 
7164e36824Saddy ke /* ss_n to sclk_out delay */
7264e36824Saddy ke #define CR0_SSD_OFFSET				10
7364e36824Saddy ke /*
7464e36824Saddy ke  * The period between ss_n active and
7564e36824Saddy ke  * sclk_out active is half sclk_out cycles
7664e36824Saddy ke  */
7764e36824Saddy ke #define CR0_SSD_HALF				0x0
7864e36824Saddy ke /*
7964e36824Saddy ke  * The period between ss_n active and
8064e36824Saddy ke  * sclk_out active is one sclk_out cycle
8164e36824Saddy ke  */
8264e36824Saddy ke #define CR0_SSD_ONE					0x1
8364e36824Saddy ke 
8464e36824Saddy ke #define CR0_EM_OFFSET				11
8564e36824Saddy ke #define CR0_EM_LITTLE				0x0
8664e36824Saddy ke #define CR0_EM_BIG					0x1
8764e36824Saddy ke 
8864e36824Saddy ke #define CR0_FBM_OFFSET				12
8964e36824Saddy ke #define CR0_FBM_MSB					0x0
9064e36824Saddy ke #define CR0_FBM_LSB					0x1
9164e36824Saddy ke 
9264e36824Saddy ke #define CR0_BHT_OFFSET				13
9364e36824Saddy ke #define CR0_BHT_16BIT				0x0
9464e36824Saddy ke #define CR0_BHT_8BIT				0x1
9564e36824Saddy ke 
9664e36824Saddy ke #define CR0_RSD_OFFSET				14
9764e36824Saddy ke 
9864e36824Saddy ke #define CR0_FRF_OFFSET				16
9964e36824Saddy ke #define CR0_FRF_SPI					0x0
10064e36824Saddy ke #define CR0_FRF_SSP					0x1
10164e36824Saddy ke #define CR0_FRF_MICROWIRE			0x2
10264e36824Saddy ke 
10364e36824Saddy ke #define CR0_XFM_OFFSET				18
10464e36824Saddy ke #define CR0_XFM_MASK				(0x03 << SPI_XFM_OFFSET)
10564e36824Saddy ke #define CR0_XFM_TR					0x0
10664e36824Saddy ke #define CR0_XFM_TO					0x1
10764e36824Saddy ke #define CR0_XFM_RO					0x2
10864e36824Saddy ke 
10964e36824Saddy ke #define CR0_OPM_OFFSET				20
11064e36824Saddy ke #define CR0_OPM_MASTER				0x0
11164e36824Saddy ke #define CR0_OPM_SLAVE				0x1
11264e36824Saddy ke 
11364e36824Saddy ke #define CR0_MTM_OFFSET				0x21
11464e36824Saddy ke 
11564e36824Saddy ke /* Bit fields in SER, 2bit */
11664e36824Saddy ke #define SER_MASK					0x3
11764e36824Saddy ke 
11864e36824Saddy ke /* Bit fields in SR, 5bit */
11964e36824Saddy ke #define SR_MASK						0x1f
12064e36824Saddy ke #define SR_BUSY						(1 << 0)
12164e36824Saddy ke #define SR_TF_FULL					(1 << 1)
12264e36824Saddy ke #define SR_TF_EMPTY					(1 << 2)
12364e36824Saddy ke #define SR_RF_EMPTY					(1 << 3)
12464e36824Saddy ke #define SR_RF_FULL					(1 << 4)
12564e36824Saddy ke 
12664e36824Saddy ke /* Bit fields in ISR, IMR, ISR, RISR, 5bit */
12764e36824Saddy ke #define INT_MASK					0x1f
12864e36824Saddy ke #define INT_TF_EMPTY				(1 << 0)
12964e36824Saddy ke #define INT_TF_OVERFLOW				(1 << 1)
13064e36824Saddy ke #define INT_RF_UNDERFLOW			(1 << 2)
13164e36824Saddy ke #define INT_RF_OVERFLOW				(1 << 3)
13264e36824Saddy ke #define INT_RF_FULL					(1 << 4)
13364e36824Saddy ke 
13464e36824Saddy ke /* Bit fields in ICR, 4bit */
13564e36824Saddy ke #define ICR_MASK					0x0f
13664e36824Saddy ke #define ICR_ALL						(1 << 0)
13764e36824Saddy ke #define ICR_RF_UNDERFLOW			(1 << 1)
13864e36824Saddy ke #define ICR_RF_OVERFLOW				(1 << 2)
13964e36824Saddy ke #define ICR_TF_OVERFLOW				(1 << 3)
14064e36824Saddy ke 
14164e36824Saddy ke /* Bit fields in DMACR */
14264e36824Saddy ke #define RF_DMA_EN					(1 << 0)
14364e36824Saddy ke #define TF_DMA_EN					(1 << 1)
14464e36824Saddy ke 
14564e36824Saddy ke #define RXBUSY						(1 << 0)
14664e36824Saddy ke #define TXBUSY						(1 << 1)
14764e36824Saddy ke 
148f9cfd522SAddy Ke /* sclk_out: spi master internal logic in rk3x can support 50Mhz */
149f9cfd522SAddy Ke #define MAX_SCLK_OUT		50000000
150f9cfd522SAddy Ke 
1515185a81cSBrian Norris /*
1525185a81cSBrian Norris  * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However,
1535185a81cSBrian Norris  * the controller seems to hang when given 0x10000, so stick with this for now.
1545185a81cSBrian Norris  */
1555185a81cSBrian Norris #define ROCKCHIP_SPI_MAX_TRANLEN		0xffff
1565185a81cSBrian Norris 
157aa099382SJeffy Chen #define ROCKCHIP_SPI_MAX_CS_NUM			2
158aa099382SJeffy Chen 
15964e36824Saddy ke enum rockchip_ssi_type {
16064e36824Saddy ke 	SSI_MOTO_SPI = 0,
16164e36824Saddy ke 	SSI_TI_SSP,
16264e36824Saddy ke 	SSI_NS_MICROWIRE,
16364e36824Saddy ke };
16464e36824Saddy ke 
16564e36824Saddy ke struct rockchip_spi_dma_data {
16664e36824Saddy ke 	struct dma_chan *ch;
16764e36824Saddy ke 	dma_addr_t addr;
16864e36824Saddy ke };
16964e36824Saddy ke 
17064e36824Saddy ke struct rockchip_spi {
17164e36824Saddy ke 	struct device *dev;
17264e36824Saddy ke 	struct spi_master *master;
17364e36824Saddy ke 
17464e36824Saddy ke 	struct clk *spiclk;
17564e36824Saddy ke 	struct clk *apb_pclk;
17664e36824Saddy ke 
17764e36824Saddy ke 	void __iomem *regs;
17864e36824Saddy ke 	/*depth of the FIFO buffer */
17964e36824Saddy ke 	u32 fifo_len;
18064e36824Saddy ke 	/* max bus freq supported */
18164e36824Saddy ke 	u32 max_freq;
18264e36824Saddy ke 	/* supported slave numbers */
18364e36824Saddy ke 	enum rockchip_ssi_type type;
18464e36824Saddy ke 
18564e36824Saddy ke 	u16 mode;
18664e36824Saddy ke 	u8 tmode;
18764e36824Saddy ke 	u8 bpw;
18864e36824Saddy ke 	u8 n_bytes;
189108b5c8bSShawn Lin 	u32 rsd_nsecs;
19064e36824Saddy ke 	unsigned len;
19164e36824Saddy ke 	u32 speed;
19264e36824Saddy ke 
19364e36824Saddy ke 	const void *tx;
19464e36824Saddy ke 	const void *tx_end;
19564e36824Saddy ke 	void *rx;
19664e36824Saddy ke 	void *rx_end;
19764e36824Saddy ke 
19864e36824Saddy ke 	u32 state;
1995dcc44edSAddy Ke 	/* protect state */
20064e36824Saddy ke 	spinlock_t lock;
20164e36824Saddy ke 
202aa099382SJeffy Chen 	bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
203aa099382SJeffy Chen 
204f340b920SEmil Renner Berthing 	bool use_dma;
20564e36824Saddy ke 	struct sg_table tx_sg;
20664e36824Saddy ke 	struct sg_table rx_sg;
20764e36824Saddy ke 	struct rockchip_spi_dma_data dma_rx;
20864e36824Saddy ke 	struct rockchip_spi_dma_data dma_tx;
20964e36824Saddy ke };
21064e36824Saddy ke 
21130688e4eSEmil Renner Berthing static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
21264e36824Saddy ke {
21330688e4eSEmil Renner Berthing 	writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR);
21464e36824Saddy ke }
21564e36824Saddy ke 
21664e36824Saddy ke static inline void spi_set_clk(struct rockchip_spi *rs, u16 div)
21764e36824Saddy ke {
21864e36824Saddy ke 	writel_relaxed(div, rs->regs + ROCKCHIP_SPI_BAUDR);
21964e36824Saddy ke }
22064e36824Saddy ke 
22164e36824Saddy ke static inline void flush_fifo(struct rockchip_spi *rs)
22264e36824Saddy ke {
22364e36824Saddy ke 	while (readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR))
22464e36824Saddy ke 		readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
22564e36824Saddy ke }
22664e36824Saddy ke 
2272df08e78SAddy Ke static inline void wait_for_idle(struct rockchip_spi *rs)
2282df08e78SAddy Ke {
2292df08e78SAddy Ke 	unsigned long timeout = jiffies + msecs_to_jiffies(5);
2302df08e78SAddy Ke 
2312df08e78SAddy Ke 	do {
2322df08e78SAddy Ke 		if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
2332df08e78SAddy Ke 			return;
23464bc0110SDoug Anderson 	} while (!time_after(jiffies, timeout));
2352df08e78SAddy Ke 
2362df08e78SAddy Ke 	dev_warn(rs->dev, "spi controller is in busy state!\n");
2372df08e78SAddy Ke }
2382df08e78SAddy Ke 
23964e36824Saddy ke static u32 get_fifo_len(struct rockchip_spi *rs)
24064e36824Saddy ke {
24164e36824Saddy ke 	u32 fifo;
24264e36824Saddy ke 
24364e36824Saddy ke 	for (fifo = 2; fifo < 32; fifo++) {
24464e36824Saddy ke 		writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR);
24564e36824Saddy ke 		if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR))
24664e36824Saddy ke 			break;
24764e36824Saddy ke 	}
24864e36824Saddy ke 
24964e36824Saddy ke 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR);
25064e36824Saddy ke 
25164e36824Saddy ke 	return (fifo == 31) ? 0 : fifo;
25264e36824Saddy ke }
25364e36824Saddy ke 
25464e36824Saddy ke static inline u32 tx_max(struct rockchip_spi *rs)
25564e36824Saddy ke {
25664e36824Saddy ke 	u32 tx_left, tx_room;
25764e36824Saddy ke 
25864e36824Saddy ke 	tx_left = (rs->tx_end - rs->tx) / rs->n_bytes;
25964e36824Saddy ke 	tx_room = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
26064e36824Saddy ke 
26164e36824Saddy ke 	return min(tx_left, tx_room);
26264e36824Saddy ke }
26364e36824Saddy ke 
26464e36824Saddy ke static inline u32 rx_max(struct rockchip_spi *rs)
26564e36824Saddy ke {
26664e36824Saddy ke 	u32 rx_left = (rs->rx_end - rs->rx) / rs->n_bytes;
26764e36824Saddy ke 	u32 rx_room = (u32)readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
26864e36824Saddy ke 
26964e36824Saddy ke 	return min(rx_left, rx_room);
27064e36824Saddy ke }
27164e36824Saddy ke 
27264e36824Saddy ke static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
27364e36824Saddy ke {
274b920cc31SHuibin Hong 	struct spi_master *master = spi->master;
275b920cc31SHuibin Hong 	struct rockchip_spi *rs = spi_master_get_devdata(master);
276aa099382SJeffy Chen 	bool cs_asserted = !enable;
277b920cc31SHuibin Hong 
278aa099382SJeffy Chen 	/* Return immediately for no-op */
279aa099382SJeffy Chen 	if (cs_asserted == rs->cs_asserted[spi->chip_select])
280aa099382SJeffy Chen 		return;
281aa099382SJeffy Chen 
282aa099382SJeffy Chen 	if (cs_asserted) {
283aa099382SJeffy Chen 		/* Keep things powered as long as CS is asserted */
284b920cc31SHuibin Hong 		pm_runtime_get_sync(rs->dev);
28564e36824Saddy ke 
286aa099382SJeffy Chen 		ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER,
287aa099382SJeffy Chen 				      BIT(spi->chip_select));
288aa099382SJeffy Chen 	} else {
289aa099382SJeffy Chen 		ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER,
290aa099382SJeffy Chen 				      BIT(spi->chip_select));
29164e36824Saddy ke 
292aa099382SJeffy Chen 		/* Drop reference from when we first asserted CS */
293aa099382SJeffy Chen 		pm_runtime_put(rs->dev);
294aa099382SJeffy Chen 	}
29564e36824Saddy ke 
296aa099382SJeffy Chen 	rs->cs_asserted[spi->chip_select] = cs_asserted;
29764e36824Saddy ke }
29864e36824Saddy ke 
29964e36824Saddy ke static int rockchip_spi_prepare_message(struct spi_master *master,
30064e36824Saddy ke 					struct spi_message *msg)
30164e36824Saddy ke {
30264e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
30364e36824Saddy ke 	struct spi_device *spi = msg->spi;
30464e36824Saddy ke 
30564e36824Saddy ke 	rs->mode = spi->mode;
30664e36824Saddy ke 
30764e36824Saddy ke 	return 0;
30864e36824Saddy ke }
30964e36824Saddy ke 
3102291793cSAndy Shevchenko static void rockchip_spi_handle_err(struct spi_master *master,
31164e36824Saddy ke 				    struct spi_message *msg)
31264e36824Saddy ke {
31364e36824Saddy ke 	unsigned long flags;
31464e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
31564e36824Saddy ke 
31664e36824Saddy ke 	spin_lock_irqsave(&rs->lock, flags);
31764e36824Saddy ke 
3185dcc44edSAddy Ke 	/*
3195dcc44edSAddy Ke 	 * For DMA mode, we need terminate DMA channel and flush
3205dcc44edSAddy Ke 	 * fifo for the next transfer if DMA thansfer timeout.
3212291793cSAndy Shevchenko 	 * handle_err() was called by core if transfer failed.
3222291793cSAndy Shevchenko 	 * Maybe it is reasonable for error handling here.
3235dcc44edSAddy Ke 	 */
32464e36824Saddy ke 	if (rs->use_dma) {
32564e36824Saddy ke 		if (rs->state & RXBUSY) {
326557b7ea3SShawn Lin 			dmaengine_terminate_async(rs->dma_rx.ch);
32764e36824Saddy ke 			flush_fifo(rs);
32864e36824Saddy ke 		}
32964e36824Saddy ke 
33064e36824Saddy ke 		if (rs->state & TXBUSY)
331557b7ea3SShawn Lin 			dmaengine_terminate_async(rs->dma_tx.ch);
33264e36824Saddy ke 	}
33364e36824Saddy ke 
33464e36824Saddy ke 	spin_unlock_irqrestore(&rs->lock, flags);
3352291793cSAndy Shevchenko }
3362291793cSAndy Shevchenko 
3372291793cSAndy Shevchenko static int rockchip_spi_unprepare_message(struct spi_master *master,
3382291793cSAndy Shevchenko 					  struct spi_message *msg)
3392291793cSAndy Shevchenko {
3402291793cSAndy Shevchenko 	struct rockchip_spi *rs = spi_master_get_devdata(master);
34164e36824Saddy ke 
34230688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
343c28be31bSAddy Ke 
34464e36824Saddy ke 	return 0;
34564e36824Saddy ke }
34664e36824Saddy ke 
34764e36824Saddy ke static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
34864e36824Saddy ke {
34964e36824Saddy ke 	u32 max = tx_max(rs);
35064e36824Saddy ke 	u32 txw = 0;
35164e36824Saddy ke 
35264e36824Saddy ke 	while (max--) {
35364e36824Saddy ke 		if (rs->n_bytes == 1)
35464e36824Saddy ke 			txw = *(u8 *)(rs->tx);
35564e36824Saddy ke 		else
35664e36824Saddy ke 			txw = *(u16 *)(rs->tx);
35764e36824Saddy ke 
35864e36824Saddy ke 		writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
35964e36824Saddy ke 		rs->tx += rs->n_bytes;
36064e36824Saddy ke 	}
36164e36824Saddy ke }
36264e36824Saddy ke 
36364e36824Saddy ke static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
36464e36824Saddy ke {
36564e36824Saddy ke 	u32 max = rx_max(rs);
36664e36824Saddy ke 	u32 rxw;
36764e36824Saddy ke 
36864e36824Saddy ke 	while (max--) {
36964e36824Saddy ke 		rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
37064e36824Saddy ke 		if (rs->n_bytes == 1)
37164e36824Saddy ke 			*(u8 *)(rs->rx) = (u8)rxw;
37264e36824Saddy ke 		else
37364e36824Saddy ke 			*(u16 *)(rs->rx) = (u16)rxw;
37464e36824Saddy ke 		rs->rx += rs->n_bytes;
3755dcc44edSAddy Ke 	}
37664e36824Saddy ke }
37764e36824Saddy ke 
37864e36824Saddy ke static int rockchip_spi_pio_transfer(struct rockchip_spi *rs)
37964e36824Saddy ke {
38064e36824Saddy ke 	int remain = 0;
38164e36824Saddy ke 
38230688e4eSEmil Renner Berthing 	spi_enable_chip(rs, true);
383a3c17402SEmil Renner Berthing 
38464e36824Saddy ke 	do {
38564e36824Saddy ke 		if (rs->tx) {
38664e36824Saddy ke 			remain = rs->tx_end - rs->tx;
38764e36824Saddy ke 			rockchip_spi_pio_writer(rs);
38864e36824Saddy ke 		}
38964e36824Saddy ke 
39064e36824Saddy ke 		if (rs->rx) {
39164e36824Saddy ke 			remain = rs->rx_end - rs->rx;
39264e36824Saddy ke 			rockchip_spi_pio_reader(rs);
39364e36824Saddy ke 		}
39464e36824Saddy ke 
39564e36824Saddy ke 		cpu_relax();
39664e36824Saddy ke 	} while (remain);
39764e36824Saddy ke 
3982df08e78SAddy Ke 	/* If tx, wait until the FIFO data completely. */
3992df08e78SAddy Ke 	if (rs->tx)
4002df08e78SAddy Ke 		wait_for_idle(rs);
4012df08e78SAddy Ke 
40230688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
403c28be31bSAddy Ke 
40464e36824Saddy ke 	return 0;
40564e36824Saddy ke }
40664e36824Saddy ke 
40764e36824Saddy ke static void rockchip_spi_dma_rxcb(void *data)
40864e36824Saddy ke {
40964e36824Saddy ke 	unsigned long flags;
41064e36824Saddy ke 	struct rockchip_spi *rs = data;
41164e36824Saddy ke 
41264e36824Saddy ke 	spin_lock_irqsave(&rs->lock, flags);
41364e36824Saddy ke 
41464e36824Saddy ke 	rs->state &= ~RXBUSY;
415c28be31bSAddy Ke 	if (!(rs->state & TXBUSY)) {
41630688e4eSEmil Renner Berthing 		spi_enable_chip(rs, false);
41764e36824Saddy ke 		spi_finalize_current_transfer(rs->master);
418c28be31bSAddy Ke 	}
41964e36824Saddy ke 
42064e36824Saddy ke 	spin_unlock_irqrestore(&rs->lock, flags);
42164e36824Saddy ke }
42264e36824Saddy ke 
42364e36824Saddy ke static void rockchip_spi_dma_txcb(void *data)
42464e36824Saddy ke {
42564e36824Saddy ke 	unsigned long flags;
42664e36824Saddy ke 	struct rockchip_spi *rs = data;
42764e36824Saddy ke 
4282df08e78SAddy Ke 	/* Wait until the FIFO data completely. */
4292df08e78SAddy Ke 	wait_for_idle(rs);
4302df08e78SAddy Ke 
43164e36824Saddy ke 	spin_lock_irqsave(&rs->lock, flags);
43264e36824Saddy ke 
43364e36824Saddy ke 	rs->state &= ~TXBUSY;
4342c2bc748SAddy Ke 	if (!(rs->state & RXBUSY)) {
43530688e4eSEmil Renner Berthing 		spi_enable_chip(rs, false);
43664e36824Saddy ke 		spi_finalize_current_transfer(rs->master);
4372c2bc748SAddy Ke 	}
43864e36824Saddy ke 
43964e36824Saddy ke 	spin_unlock_irqrestore(&rs->lock, flags);
44064e36824Saddy ke }
44164e36824Saddy ke 
442ea984911SShawn Lin static int rockchip_spi_prepare_dma(struct rockchip_spi *rs)
44364e36824Saddy ke {
44464e36824Saddy ke 	unsigned long flags;
44564e36824Saddy ke 	struct dma_async_tx_descriptor *rxdesc, *txdesc;
44664e36824Saddy ke 
44764e36824Saddy ke 	spin_lock_irqsave(&rs->lock, flags);
44864e36824Saddy ke 	rs->state &= ~RXBUSY;
44964e36824Saddy ke 	rs->state &= ~TXBUSY;
45064e36824Saddy ke 	spin_unlock_irqrestore(&rs->lock, flags);
45164e36824Saddy ke 
45297cf5669SArnd Bergmann 	rxdesc = NULL;
45364e36824Saddy ke 	if (rs->rx) {
454*31bcb57bSEmil Renner Berthing 		struct dma_slave_config rxconf = {
455*31bcb57bSEmil Renner Berthing 			.direction = DMA_DEV_TO_MEM,
456*31bcb57bSEmil Renner Berthing 			.src_addr = rs->dma_rx.addr,
457*31bcb57bSEmil Renner Berthing 			.src_addr_width = rs->n_bytes,
458*31bcb57bSEmil Renner Berthing 			.src_maxburst = 1,
459*31bcb57bSEmil Renner Berthing 		};
460*31bcb57bSEmil Renner Berthing 
46164e36824Saddy ke 		dmaengine_slave_config(rs->dma_rx.ch, &rxconf);
46264e36824Saddy ke 
4635dcc44edSAddy Ke 		rxdesc = dmaengine_prep_slave_sg(
4645dcc44edSAddy Ke 				rs->dma_rx.ch,
46564e36824Saddy ke 				rs->rx_sg.sgl, rs->rx_sg.nents,
466d9071b7eSEmil Renner Berthing 				DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
467ea984911SShawn Lin 		if (!rxdesc)
468ea984911SShawn Lin 			return -EINVAL;
46964e36824Saddy ke 
47064e36824Saddy ke 		rxdesc->callback = rockchip_spi_dma_rxcb;
47164e36824Saddy ke 		rxdesc->callback_param = rs;
47264e36824Saddy ke 	}
47364e36824Saddy ke 
47497cf5669SArnd Bergmann 	txdesc = NULL;
47564e36824Saddy ke 	if (rs->tx) {
476*31bcb57bSEmil Renner Berthing 		struct dma_slave_config txconf = {
477*31bcb57bSEmil Renner Berthing 			.direction = DMA_MEM_TO_DEV,
478*31bcb57bSEmil Renner Berthing 			.dst_addr = rs->dma_tx.addr,
479*31bcb57bSEmil Renner Berthing 			.dst_addr_width = rs->n_bytes,
480*31bcb57bSEmil Renner Berthing 			.dst_maxburst = rs->fifo_len / 2,
481*31bcb57bSEmil Renner Berthing 		};
482*31bcb57bSEmil Renner Berthing 
48364e36824Saddy ke 		dmaengine_slave_config(rs->dma_tx.ch, &txconf);
48464e36824Saddy ke 
4855dcc44edSAddy Ke 		txdesc = dmaengine_prep_slave_sg(
4865dcc44edSAddy Ke 				rs->dma_tx.ch,
48764e36824Saddy ke 				rs->tx_sg.sgl, rs->tx_sg.nents,
488d9071b7eSEmil Renner Berthing 				DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
489ea984911SShawn Lin 		if (!txdesc) {
490ea984911SShawn Lin 			if (rxdesc)
491ea984911SShawn Lin 				dmaengine_terminate_sync(rs->dma_rx.ch);
492ea984911SShawn Lin 			return -EINVAL;
493ea984911SShawn Lin 		}
49464e36824Saddy ke 
49564e36824Saddy ke 		txdesc->callback = rockchip_spi_dma_txcb;
49664e36824Saddy ke 		txdesc->callback_param = rs;
49764e36824Saddy ke 	}
49864e36824Saddy ke 
49964e36824Saddy ke 	/* rx must be started before tx due to spi instinct */
50097cf5669SArnd Bergmann 	if (rxdesc) {
50164e36824Saddy ke 		spin_lock_irqsave(&rs->lock, flags);
50264e36824Saddy ke 		rs->state |= RXBUSY;
50364e36824Saddy ke 		spin_unlock_irqrestore(&rs->lock, flags);
50464e36824Saddy ke 		dmaengine_submit(rxdesc);
50564e36824Saddy ke 		dma_async_issue_pending(rs->dma_rx.ch);
50664e36824Saddy ke 	}
50764e36824Saddy ke 
50830688e4eSEmil Renner Berthing 	spi_enable_chip(rs, true);
509a3c17402SEmil Renner Berthing 
51097cf5669SArnd Bergmann 	if (txdesc) {
51164e36824Saddy ke 		spin_lock_irqsave(&rs->lock, flags);
51264e36824Saddy ke 		rs->state |= TXBUSY;
51364e36824Saddy ke 		spin_unlock_irqrestore(&rs->lock, flags);
51464e36824Saddy ke 		dmaengine_submit(txdesc);
51564e36824Saddy ke 		dma_async_issue_pending(rs->dma_tx.ch);
51664e36824Saddy ke 	}
517ea984911SShawn Lin 
518a3c17402SEmil Renner Berthing 	/* 1 means the transfer is in progress */
519a3c17402SEmil Renner Berthing 	return 1;
52064e36824Saddy ke }
52164e36824Saddy ke 
52264e36824Saddy ke static void rockchip_spi_config(struct rockchip_spi *rs)
52364e36824Saddy ke {
52464e36824Saddy ke 	u32 div = 0;
52564e36824Saddy ke 	u32 dmacr = 0;
52676b17e6eSJulius Werner 	int rsd = 0;
52764e36824Saddy ke 
52864e36824Saddy ke 	u32 cr0 = (CR0_BHT_8BIT << CR0_BHT_OFFSET)
5290277e01aSAlexander Kochetkov 		| (CR0_SSD_ONE << CR0_SSD_OFFSET)
5300277e01aSAlexander Kochetkov 		| (CR0_EM_BIG << CR0_EM_OFFSET);
53164e36824Saddy ke 
53264e36824Saddy ke 	cr0 |= (rs->n_bytes << CR0_DFS_OFFSET);
53364e36824Saddy ke 	cr0 |= ((rs->mode & 0x3) << CR0_SCPH_OFFSET);
53464e36824Saddy ke 	cr0 |= (rs->tmode << CR0_XFM_OFFSET);
53564e36824Saddy ke 	cr0 |= (rs->type << CR0_FRF_OFFSET);
53664e36824Saddy ke 
53764e36824Saddy ke 	if (rs->use_dma) {
53864e36824Saddy ke 		if (rs->tx)
53964e36824Saddy ke 			dmacr |= TF_DMA_EN;
54064e36824Saddy ke 		if (rs->rx)
54164e36824Saddy ke 			dmacr |= RF_DMA_EN;
54264e36824Saddy ke 	}
54364e36824Saddy ke 
544f9cfd522SAddy Ke 	if (WARN_ON(rs->speed > MAX_SCLK_OUT))
545f9cfd522SAddy Ke 		rs->speed = MAX_SCLK_OUT;
546f9cfd522SAddy Ke 
547bb51537aSGeert Uytterhoeven 	/* the minimum divisor is 2 */
548f9cfd522SAddy Ke 	if (rs->max_freq < 2 * rs->speed) {
549f9cfd522SAddy Ke 		clk_set_rate(rs->spiclk, 2 * rs->speed);
550f9cfd522SAddy Ke 		rs->max_freq = clk_get_rate(rs->spiclk);
551f9cfd522SAddy Ke 	}
552f9cfd522SAddy Ke 
55364e36824Saddy ke 	/* div doesn't support odd number */
554754ec43cSJulius Werner 	div = DIV_ROUND_UP(rs->max_freq, rs->speed);
55564e36824Saddy ke 	div = (div + 1) & 0xfffe;
55664e36824Saddy ke 
55776b17e6eSJulius Werner 	/* Rx sample delay is expressed in parent clock cycles (max 3) */
55876b17e6eSJulius Werner 	rsd = DIV_ROUND_CLOSEST(rs->rsd_nsecs * (rs->max_freq >> 8),
55976b17e6eSJulius Werner 				1000000000 >> 8);
56076b17e6eSJulius Werner 	if (!rsd && rs->rsd_nsecs) {
56176b17e6eSJulius Werner 		pr_warn_once("rockchip-spi: %u Hz are too slow to express %u ns delay\n",
56276b17e6eSJulius Werner 			     rs->max_freq, rs->rsd_nsecs);
56376b17e6eSJulius Werner 	} else if (rsd > 3) {
56476b17e6eSJulius Werner 		rsd = 3;
56576b17e6eSJulius Werner 		pr_warn_once("rockchip-spi: %u Hz are too fast to express %u ns delay, clamping at %u ns\n",
56676b17e6eSJulius Werner 			     rs->max_freq, rs->rsd_nsecs,
56776b17e6eSJulius Werner 			     rsd * 1000000000U / rs->max_freq);
56876b17e6eSJulius Werner 	}
56976b17e6eSJulius Werner 	cr0 |= rsd << CR0_RSD_OFFSET;
57076b17e6eSJulius Werner 
57164e36824Saddy ke 	writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
57264e36824Saddy ke 
57304b37d2dSHuibin Hong 	if (rs->n_bytes == 1)
57464e36824Saddy ke 		writel_relaxed(rs->len - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);
57504b37d2dSHuibin Hong 	else if (rs->n_bytes == 2)
57604b37d2dSHuibin Hong 		writel_relaxed((rs->len / 2) - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);
57704b37d2dSHuibin Hong 	else
57804b37d2dSHuibin Hong 		writel_relaxed((rs->len * 2) - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);
57904b37d2dSHuibin Hong 
58064e36824Saddy ke 	writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_TXFTLR);
58164e36824Saddy ke 	writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
58264e36824Saddy ke 
583dcfc861dSHuibin Hong 	writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_DMATDLR);
58464e36824Saddy ke 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR);
58564e36824Saddy ke 	writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
58664e36824Saddy ke 
58764e36824Saddy ke 	spi_set_clk(rs, div);
58864e36824Saddy ke 
5895dcc44edSAddy Ke 	dev_dbg(rs->dev, "cr0 0x%x, div %d\n", cr0, div);
59064e36824Saddy ke }
59164e36824Saddy ke 
5925185a81cSBrian Norris static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
5935185a81cSBrian Norris {
5945185a81cSBrian Norris 	return ROCKCHIP_SPI_MAX_TRANLEN;
5955185a81cSBrian Norris }
5965185a81cSBrian Norris 
5975dcc44edSAddy Ke static int rockchip_spi_transfer_one(
5985dcc44edSAddy Ke 		struct spi_master *master,
59964e36824Saddy ke 		struct spi_device *spi,
60064e36824Saddy ke 		struct spi_transfer *xfer)
60164e36824Saddy ke {
60264e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
60364e36824Saddy ke 
60462946172SDoug Anderson 	WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
60562946172SDoug Anderson 		(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
60664e36824Saddy ke 
60764e36824Saddy ke 	if (!xfer->tx_buf && !xfer->rx_buf) {
60864e36824Saddy ke 		dev_err(rs->dev, "No buffer for transfer\n");
60964e36824Saddy ke 		return -EINVAL;
61064e36824Saddy ke 	}
61164e36824Saddy ke 
6125185a81cSBrian Norris 	if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
6135185a81cSBrian Norris 		dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
6145185a81cSBrian Norris 		return -EINVAL;
6155185a81cSBrian Norris 	}
6165185a81cSBrian Norris 
61764e36824Saddy ke 	rs->speed = xfer->speed_hz;
61864e36824Saddy ke 	rs->bpw = xfer->bits_per_word;
61964e36824Saddy ke 	rs->n_bytes = rs->bpw >> 3;
62064e36824Saddy ke 
62164e36824Saddy ke 	rs->tx = xfer->tx_buf;
62264e36824Saddy ke 	rs->tx_end = rs->tx + xfer->len;
62364e36824Saddy ke 	rs->rx = xfer->rx_buf;
62464e36824Saddy ke 	rs->rx_end = rs->rx + xfer->len;
62564e36824Saddy ke 	rs->len = xfer->len;
62664e36824Saddy ke 
62764e36824Saddy ke 	rs->tx_sg = xfer->tx_sg;
62864e36824Saddy ke 	rs->rx_sg = xfer->rx_sg;
62964e36824Saddy ke 
63064e36824Saddy ke 	if (rs->tx && rs->rx)
63164e36824Saddy ke 		rs->tmode = CR0_XFM_TR;
63264e36824Saddy ke 	else if (rs->tx)
63364e36824Saddy ke 		rs->tmode = CR0_XFM_TO;
63464e36824Saddy ke 	else if (rs->rx)
63564e36824Saddy ke 		rs->tmode = CR0_XFM_RO;
63664e36824Saddy ke 
637a24e70c0SAddy Ke 	/* we need prepare dma before spi was enabled */
638c28be31bSAddy Ke 	if (master->can_dma && master->can_dma(master, spi, xfer))
639f340b920SEmil Renner Berthing 		rs->use_dma = true;
640c28be31bSAddy Ke 	else
641f340b920SEmil Renner Berthing 		rs->use_dma = false;
64264e36824Saddy ke 
64364e36824Saddy ke 	rockchip_spi_config(rs);
64464e36824Saddy ke 
645a3c17402SEmil Renner Berthing 	if (rs->use_dma)
646a3c17402SEmil Renner Berthing 		return rockchip_spi_prepare_dma(rs);
64764e36824Saddy ke 
648a3c17402SEmil Renner Berthing 	return rockchip_spi_pio_transfer(rs);
64964e36824Saddy ke }
65064e36824Saddy ke 
65164e36824Saddy ke static bool rockchip_spi_can_dma(struct spi_master *master,
65264e36824Saddy ke 				 struct spi_device *spi,
65364e36824Saddy ke 				 struct spi_transfer *xfer)
65464e36824Saddy ke {
65564e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
65664e36824Saddy ke 
65764e36824Saddy ke 	return (xfer->len > rs->fifo_len);
65864e36824Saddy ke }
65964e36824Saddy ke 
66064e36824Saddy ke static int rockchip_spi_probe(struct platform_device *pdev)
66164e36824Saddy ke {
66243de979dSJeffy Chen 	int ret;
66364e36824Saddy ke 	struct rockchip_spi *rs;
66464e36824Saddy ke 	struct spi_master *master;
66564e36824Saddy ke 	struct resource *mem;
66676b17e6eSJulius Werner 	u32 rsd_nsecs;
66764e36824Saddy ke 
66864e36824Saddy ke 	master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi));
6695dcc44edSAddy Ke 	if (!master)
67064e36824Saddy ke 		return -ENOMEM;
6715dcc44edSAddy Ke 
67264e36824Saddy ke 	platform_set_drvdata(pdev, master);
67364e36824Saddy ke 
67464e36824Saddy ke 	rs = spi_master_get_devdata(master);
67564e36824Saddy ke 
67664e36824Saddy ke 	/* Get basic io resource and map it */
67764e36824Saddy ke 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
67864e36824Saddy ke 	rs->regs = devm_ioremap_resource(&pdev->dev, mem);
67964e36824Saddy ke 	if (IS_ERR(rs->regs)) {
68064e36824Saddy ke 		ret =  PTR_ERR(rs->regs);
681c351587eSJeffy Chen 		goto err_put_master;
68264e36824Saddy ke 	}
68364e36824Saddy ke 
68464e36824Saddy ke 	rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
68564e36824Saddy ke 	if (IS_ERR(rs->apb_pclk)) {
68664e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get apb_pclk\n");
68764e36824Saddy ke 		ret = PTR_ERR(rs->apb_pclk);
688c351587eSJeffy Chen 		goto err_put_master;
68964e36824Saddy ke 	}
69064e36824Saddy ke 
69164e36824Saddy ke 	rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
69264e36824Saddy ke 	if (IS_ERR(rs->spiclk)) {
69364e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get spi_pclk\n");
69464e36824Saddy ke 		ret = PTR_ERR(rs->spiclk);
695c351587eSJeffy Chen 		goto err_put_master;
69664e36824Saddy ke 	}
69764e36824Saddy ke 
69864e36824Saddy ke 	ret = clk_prepare_enable(rs->apb_pclk);
69943de979dSJeffy Chen 	if (ret < 0) {
70064e36824Saddy ke 		dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
701c351587eSJeffy Chen 		goto err_put_master;
70264e36824Saddy ke 	}
70364e36824Saddy ke 
70464e36824Saddy ke 	ret = clk_prepare_enable(rs->spiclk);
70543de979dSJeffy Chen 	if (ret < 0) {
70664e36824Saddy ke 		dev_err(&pdev->dev, "Failed to enable spi_clk\n");
707c351587eSJeffy Chen 		goto err_disable_apbclk;
70864e36824Saddy ke 	}
70964e36824Saddy ke 
71030688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
71164e36824Saddy ke 
71264e36824Saddy ke 	rs->type = SSI_MOTO_SPI;
71364e36824Saddy ke 	rs->master = master;
71464e36824Saddy ke 	rs->dev = &pdev->dev;
71564e36824Saddy ke 	rs->max_freq = clk_get_rate(rs->spiclk);
71664e36824Saddy ke 
71776b17e6eSJulius Werner 	if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
71876b17e6eSJulius Werner 				  &rsd_nsecs))
71976b17e6eSJulius Werner 		rs->rsd_nsecs = rsd_nsecs;
72076b17e6eSJulius Werner 
72164e36824Saddy ke 	rs->fifo_len = get_fifo_len(rs);
72264e36824Saddy ke 	if (!rs->fifo_len) {
72364e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get fifo length\n");
724db7e8d90SWei Yongjun 		ret = -EINVAL;
725c351587eSJeffy Chen 		goto err_disable_spiclk;
72664e36824Saddy ke 	}
72764e36824Saddy ke 
72864e36824Saddy ke 	spin_lock_init(&rs->lock);
72964e36824Saddy ke 
73064e36824Saddy ke 	pm_runtime_set_active(&pdev->dev);
73164e36824Saddy ke 	pm_runtime_enable(&pdev->dev);
73264e36824Saddy ke 
73364e36824Saddy ke 	master->auto_runtime_pm = true;
73464e36824Saddy ke 	master->bus_num = pdev->id;
735ee780997SAddy Ke 	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
736aa099382SJeffy Chen 	master->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM;
73764e36824Saddy ke 	master->dev.of_node = pdev->dev.of_node;
73864e36824Saddy ke 	master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8);
73964e36824Saddy ke 
74064e36824Saddy ke 	master->set_cs = rockchip_spi_set_cs;
74164e36824Saddy ke 	master->prepare_message = rockchip_spi_prepare_message;
74264e36824Saddy ke 	master->unprepare_message = rockchip_spi_unprepare_message;
74364e36824Saddy ke 	master->transfer_one = rockchip_spi_transfer_one;
7445185a81cSBrian Norris 	master->max_transfer_size = rockchip_spi_max_transfer_size;
7452291793cSAndy Shevchenko 	master->handle_err = rockchip_spi_handle_err;
746c863795cSJeffy Chen 	master->flags = SPI_MASTER_GPIO_SS;
74764e36824Saddy ke 
748e4c0e06fSShawn Lin 	rs->dma_tx.ch = dma_request_chan(rs->dev, "tx");
749e4c0e06fSShawn Lin 	if (IS_ERR(rs->dma_tx.ch)) {
75061cadcf4SShawn Lin 		/* Check tx to see if we need defer probing driver */
75161cadcf4SShawn Lin 		if (PTR_ERR(rs->dma_tx.ch) == -EPROBE_DEFER) {
75261cadcf4SShawn Lin 			ret = -EPROBE_DEFER;
753c351587eSJeffy Chen 			goto err_disable_pm_runtime;
75461cadcf4SShawn Lin 		}
75564e36824Saddy ke 		dev_warn(rs->dev, "Failed to request TX DMA channel\n");
75664e36824Saddy ke 		rs->dma_tx.ch = NULL;
75764e36824Saddy ke 	}
758e4c0e06fSShawn Lin 
759e4c0e06fSShawn Lin 	rs->dma_rx.ch = dma_request_chan(rs->dev, "rx");
760e4c0e06fSShawn Lin 	if (IS_ERR(rs->dma_rx.ch)) {
761e4c0e06fSShawn Lin 		if (PTR_ERR(rs->dma_rx.ch) == -EPROBE_DEFER) {
762e4c0e06fSShawn Lin 			ret = -EPROBE_DEFER;
7635de7ed0cSDan Carpenter 			goto err_free_dma_tx;
764e4c0e06fSShawn Lin 		}
76564e36824Saddy ke 		dev_warn(rs->dev, "Failed to request RX DMA channel\n");
766e4c0e06fSShawn Lin 		rs->dma_rx.ch = NULL;
76764e36824Saddy ke 	}
76864e36824Saddy ke 
76964e36824Saddy ke 	if (rs->dma_tx.ch && rs->dma_rx.ch) {
77064e36824Saddy ke 		rs->dma_tx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_TXDR);
77164e36824Saddy ke 		rs->dma_rx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_RXDR);
77264e36824Saddy ke 
77364e36824Saddy ke 		master->can_dma = rockchip_spi_can_dma;
77464e36824Saddy ke 		master->dma_tx = rs->dma_tx.ch;
77564e36824Saddy ke 		master->dma_rx = rs->dma_rx.ch;
77664e36824Saddy ke 	}
77764e36824Saddy ke 
77864e36824Saddy ke 	ret = devm_spi_register_master(&pdev->dev, master);
77943de979dSJeffy Chen 	if (ret < 0) {
78064e36824Saddy ke 		dev_err(&pdev->dev, "Failed to register master\n");
781c351587eSJeffy Chen 		goto err_free_dma_rx;
78264e36824Saddy ke 	}
78364e36824Saddy ke 
78464e36824Saddy ke 	return 0;
78564e36824Saddy ke 
786c351587eSJeffy Chen err_free_dma_rx:
78764e36824Saddy ke 	if (rs->dma_rx.ch)
78864e36824Saddy ke 		dma_release_channel(rs->dma_rx.ch);
7895de7ed0cSDan Carpenter err_free_dma_tx:
7905de7ed0cSDan Carpenter 	if (rs->dma_tx.ch)
7915de7ed0cSDan Carpenter 		dma_release_channel(rs->dma_tx.ch);
792c351587eSJeffy Chen err_disable_pm_runtime:
793c351587eSJeffy Chen 	pm_runtime_disable(&pdev->dev);
794c351587eSJeffy Chen err_disable_spiclk:
79564e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
796c351587eSJeffy Chen err_disable_apbclk:
79764e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
798c351587eSJeffy Chen err_put_master:
79964e36824Saddy ke 	spi_master_put(master);
80064e36824Saddy ke 
80164e36824Saddy ke 	return ret;
80264e36824Saddy ke }
80364e36824Saddy ke 
80464e36824Saddy ke static int rockchip_spi_remove(struct platform_device *pdev)
80564e36824Saddy ke {
80664e36824Saddy ke 	struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
80764e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
80864e36824Saddy ke 
8096a06e895SJeffy Chen 	pm_runtime_get_sync(&pdev->dev);
81064e36824Saddy ke 
81164e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
81264e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
81364e36824Saddy ke 
8146a06e895SJeffy Chen 	pm_runtime_put_noidle(&pdev->dev);
8156a06e895SJeffy Chen 	pm_runtime_disable(&pdev->dev);
8166a06e895SJeffy Chen 	pm_runtime_set_suspended(&pdev->dev);
8176a06e895SJeffy Chen 
81864e36824Saddy ke 	if (rs->dma_tx.ch)
81964e36824Saddy ke 		dma_release_channel(rs->dma_tx.ch);
82064e36824Saddy ke 	if (rs->dma_rx.ch)
82164e36824Saddy ke 		dma_release_channel(rs->dma_rx.ch);
82264e36824Saddy ke 
823844c9f47SShawn Lin 	spi_master_put(master);
824844c9f47SShawn Lin 
82564e36824Saddy ke 	return 0;
82664e36824Saddy ke }
82764e36824Saddy ke 
82864e36824Saddy ke #ifdef CONFIG_PM_SLEEP
82964e36824Saddy ke static int rockchip_spi_suspend(struct device *dev)
83064e36824Saddy ke {
83143de979dSJeffy Chen 	int ret;
83264e36824Saddy ke 	struct spi_master *master = dev_get_drvdata(dev);
83364e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
83464e36824Saddy ke 
83564e36824Saddy ke 	ret = spi_master_suspend(rs->master);
83643de979dSJeffy Chen 	if (ret < 0)
83764e36824Saddy ke 		return ret;
83864e36824Saddy ke 
839d38c4ae1SJeffy Chen 	ret = pm_runtime_force_suspend(dev);
840d38c4ae1SJeffy Chen 	if (ret < 0)
841d38c4ae1SJeffy Chen 		return ret;
84264e36824Saddy ke 
84323e291c2SBrian Norris 	pinctrl_pm_select_sleep_state(dev);
84423e291c2SBrian Norris 
84543de979dSJeffy Chen 	return 0;
84664e36824Saddy ke }
84764e36824Saddy ke 
84864e36824Saddy ke static int rockchip_spi_resume(struct device *dev)
84964e36824Saddy ke {
85043de979dSJeffy Chen 	int ret;
85164e36824Saddy ke 	struct spi_master *master = dev_get_drvdata(dev);
85264e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
85364e36824Saddy ke 
85423e291c2SBrian Norris 	pinctrl_pm_select_default_state(dev);
85523e291c2SBrian Norris 
856d38c4ae1SJeffy Chen 	ret = pm_runtime_force_resume(dev);
85764e36824Saddy ke 	if (ret < 0)
85864e36824Saddy ke 		return ret;
85964e36824Saddy ke 
86064e36824Saddy ke 	ret = spi_master_resume(rs->master);
86164e36824Saddy ke 	if (ret < 0) {
86264e36824Saddy ke 		clk_disable_unprepare(rs->spiclk);
86364e36824Saddy ke 		clk_disable_unprepare(rs->apb_pclk);
86464e36824Saddy ke 	}
86564e36824Saddy ke 
86643de979dSJeffy Chen 	return 0;
86764e36824Saddy ke }
86864e36824Saddy ke #endif /* CONFIG_PM_SLEEP */
86964e36824Saddy ke 
870ec833050SRafael J. Wysocki #ifdef CONFIG_PM
87164e36824Saddy ke static int rockchip_spi_runtime_suspend(struct device *dev)
87264e36824Saddy ke {
87364e36824Saddy ke 	struct spi_master *master = dev_get_drvdata(dev);
87464e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
87564e36824Saddy ke 
87664e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
87764e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
87864e36824Saddy ke 
87964e36824Saddy ke 	return 0;
88064e36824Saddy ke }
88164e36824Saddy ke 
88264e36824Saddy ke static int rockchip_spi_runtime_resume(struct device *dev)
88364e36824Saddy ke {
88464e36824Saddy ke 	int ret;
88564e36824Saddy ke 	struct spi_master *master = dev_get_drvdata(dev);
88664e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
88764e36824Saddy ke 
88864e36824Saddy ke 	ret = clk_prepare_enable(rs->apb_pclk);
88943de979dSJeffy Chen 	if (ret < 0)
89064e36824Saddy ke 		return ret;
89164e36824Saddy ke 
89264e36824Saddy ke 	ret = clk_prepare_enable(rs->spiclk);
89343de979dSJeffy Chen 	if (ret < 0)
89464e36824Saddy ke 		clk_disable_unprepare(rs->apb_pclk);
89564e36824Saddy ke 
89643de979dSJeffy Chen 	return 0;
89764e36824Saddy ke }
898ec833050SRafael J. Wysocki #endif /* CONFIG_PM */
89964e36824Saddy ke 
90064e36824Saddy ke static const struct dev_pm_ops rockchip_spi_pm = {
90164e36824Saddy ke 	SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
90264e36824Saddy ke 	SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
90364e36824Saddy ke 			   rockchip_spi_runtime_resume, NULL)
90464e36824Saddy ke };
90564e36824Saddy ke 
90664e36824Saddy ke static const struct of_device_id rockchip_spi_dt_match[] = {
9076b860e69SAndy Yan 	{ .compatible = "rockchip,rv1108-spi", },
908aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3036-spi", },
90964e36824Saddy ke 	{ .compatible = "rockchip,rk3066-spi", },
910b839b785SAddy Ke 	{ .compatible = "rockchip,rk3188-spi", },
911aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3228-spi", },
912b839b785SAddy Ke 	{ .compatible = "rockchip,rk3288-spi", },
913aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3368-spi", },
9149b7a5622SXu Jianqun 	{ .compatible = "rockchip,rk3399-spi", },
91564e36824Saddy ke 	{ },
91664e36824Saddy ke };
91764e36824Saddy ke MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
91864e36824Saddy ke 
91964e36824Saddy ke static struct platform_driver rockchip_spi_driver = {
92064e36824Saddy ke 	.driver = {
92164e36824Saddy ke 		.name	= DRIVER_NAME,
92264e36824Saddy ke 		.pm = &rockchip_spi_pm,
92364e36824Saddy ke 		.of_match_table = of_match_ptr(rockchip_spi_dt_match),
92464e36824Saddy ke 	},
92564e36824Saddy ke 	.probe = rockchip_spi_probe,
92664e36824Saddy ke 	.remove = rockchip_spi_remove,
92764e36824Saddy ke };
92864e36824Saddy ke 
92964e36824Saddy ke module_platform_driver(rockchip_spi_driver);
93064e36824Saddy ke 
9315dcc44edSAddy Ke MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
93264e36824Saddy ke MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
93364e36824Saddy ke MODULE_LICENSE("GPL v2");
934