xref: /linux/drivers/spi/spi-rockchip.c (revision 47300728fb213486a830565d2af49da967c9d16a)
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
5765498c6aSEmil Renner Berthing #define CR0_DFS_4BIT				0x0
5865498c6aSEmil Renner Berthing #define CR0_DFS_8BIT				0x1
5965498c6aSEmil Renner Berthing #define CR0_DFS_16BIT				0x2
6064e36824Saddy ke 
6164e36824Saddy ke #define CR0_CFS_OFFSET				2
6264e36824Saddy ke 
6364e36824Saddy ke #define CR0_SCPH_OFFSET				6
6464e36824Saddy ke 
6564e36824Saddy ke #define CR0_SCPOL_OFFSET			7
6664e36824Saddy ke 
6764e36824Saddy ke #define CR0_CSM_OFFSET				8
6864e36824Saddy ke #define CR0_CSM_KEEP				0x0
6964e36824Saddy ke /* ss_n be high for half sclk_out cycles */
7064e36824Saddy ke #define CR0_CSM_HALF				0X1
7164e36824Saddy ke /* ss_n be high for one sclk_out cycle */
7264e36824Saddy ke #define CR0_CSM_ONE					0x2
7364e36824Saddy ke 
7464e36824Saddy ke /* ss_n to sclk_out delay */
7564e36824Saddy ke #define CR0_SSD_OFFSET				10
7664e36824Saddy ke /*
7764e36824Saddy ke  * The period between ss_n active and
7864e36824Saddy ke  * sclk_out active is half sclk_out cycles
7964e36824Saddy ke  */
8064e36824Saddy ke #define CR0_SSD_HALF				0x0
8164e36824Saddy ke /*
8264e36824Saddy ke  * The period between ss_n active and
8364e36824Saddy ke  * sclk_out active is one sclk_out cycle
8464e36824Saddy ke  */
8564e36824Saddy ke #define CR0_SSD_ONE					0x1
8664e36824Saddy ke 
8764e36824Saddy ke #define CR0_EM_OFFSET				11
8864e36824Saddy ke #define CR0_EM_LITTLE				0x0
8964e36824Saddy ke #define CR0_EM_BIG					0x1
9064e36824Saddy ke 
9164e36824Saddy ke #define CR0_FBM_OFFSET				12
9264e36824Saddy ke #define CR0_FBM_MSB					0x0
9364e36824Saddy ke #define CR0_FBM_LSB					0x1
9464e36824Saddy ke 
9564e36824Saddy ke #define CR0_BHT_OFFSET				13
9664e36824Saddy ke #define CR0_BHT_16BIT				0x0
9764e36824Saddy ke #define CR0_BHT_8BIT				0x1
9864e36824Saddy ke 
9964e36824Saddy ke #define CR0_RSD_OFFSET				14
10074b7efa8SEmil Renner Berthing #define CR0_RSD_MAX				0x3
10164e36824Saddy ke 
10264e36824Saddy ke #define CR0_FRF_OFFSET				16
10364e36824Saddy ke #define CR0_FRF_SPI					0x0
10464e36824Saddy ke #define CR0_FRF_SSP					0x1
10564e36824Saddy ke #define CR0_FRF_MICROWIRE			0x2
10664e36824Saddy ke 
10764e36824Saddy ke #define CR0_XFM_OFFSET				18
10864e36824Saddy ke #define CR0_XFM_MASK				(0x03 << SPI_XFM_OFFSET)
10964e36824Saddy ke #define CR0_XFM_TR					0x0
11064e36824Saddy ke #define CR0_XFM_TO					0x1
11164e36824Saddy ke #define CR0_XFM_RO					0x2
11264e36824Saddy ke 
11364e36824Saddy ke #define CR0_OPM_OFFSET				20
11464e36824Saddy ke #define CR0_OPM_MASTER				0x0
11564e36824Saddy ke #define CR0_OPM_SLAVE				0x1
11664e36824Saddy ke 
11764e36824Saddy ke #define CR0_MTM_OFFSET				0x21
11864e36824Saddy ke 
11964e36824Saddy ke /* Bit fields in SER, 2bit */
12064e36824Saddy ke #define SER_MASK					0x3
12164e36824Saddy ke 
122420b82f8SEmil Renner Berthing /* Bit fields in BAUDR */
123420b82f8SEmil Renner Berthing #define BAUDR_SCKDV_MIN				2
124420b82f8SEmil Renner Berthing #define BAUDR_SCKDV_MAX				65534
125420b82f8SEmil Renner Berthing 
12664e36824Saddy ke /* Bit fields in SR, 5bit */
12764e36824Saddy ke #define SR_MASK						0x1f
12864e36824Saddy ke #define SR_BUSY						(1 << 0)
12964e36824Saddy ke #define SR_TF_FULL					(1 << 1)
13064e36824Saddy ke #define SR_TF_EMPTY					(1 << 2)
13164e36824Saddy ke #define SR_RF_EMPTY					(1 << 3)
13264e36824Saddy ke #define SR_RF_FULL					(1 << 4)
13364e36824Saddy ke 
13464e36824Saddy ke /* Bit fields in ISR, IMR, ISR, RISR, 5bit */
13564e36824Saddy ke #define INT_MASK					0x1f
13664e36824Saddy ke #define INT_TF_EMPTY				(1 << 0)
13764e36824Saddy ke #define INT_TF_OVERFLOW				(1 << 1)
13864e36824Saddy ke #define INT_RF_UNDERFLOW			(1 << 2)
13964e36824Saddy ke #define INT_RF_OVERFLOW				(1 << 3)
14064e36824Saddy ke #define INT_RF_FULL					(1 << 4)
14164e36824Saddy ke 
14264e36824Saddy ke /* Bit fields in ICR, 4bit */
14364e36824Saddy ke #define ICR_MASK					0x0f
14464e36824Saddy ke #define ICR_ALL						(1 << 0)
14564e36824Saddy ke #define ICR_RF_UNDERFLOW			(1 << 1)
14664e36824Saddy ke #define ICR_RF_OVERFLOW				(1 << 2)
14764e36824Saddy ke #define ICR_TF_OVERFLOW				(1 << 3)
14864e36824Saddy ke 
14964e36824Saddy ke /* Bit fields in DMACR */
15064e36824Saddy ke #define RF_DMA_EN					(1 << 0)
15164e36824Saddy ke #define TF_DMA_EN					(1 << 1)
15264e36824Saddy ke 
153fab3e487SEmil Renner Berthing /* Driver state flags */
154fab3e487SEmil Renner Berthing #define RXDMA					(1 << 0)
155fab3e487SEmil Renner Berthing #define TXDMA					(1 << 1)
15664e36824Saddy ke 
157f9cfd522SAddy Ke /* sclk_out: spi master internal logic in rk3x can support 50Mhz */
158420b82f8SEmil Renner Berthing #define MAX_SCLK_OUT				50000000U
159f9cfd522SAddy Ke 
1605185a81cSBrian Norris /*
1615185a81cSBrian Norris  * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However,
1625185a81cSBrian Norris  * the controller seems to hang when given 0x10000, so stick with this for now.
1635185a81cSBrian Norris  */
1645185a81cSBrian Norris #define ROCKCHIP_SPI_MAX_TRANLEN		0xffff
1655185a81cSBrian Norris 
166aa099382SJeffy Chen #define ROCKCHIP_SPI_MAX_CS_NUM			2
167aa099382SJeffy Chen 
16864e36824Saddy ke struct rockchip_spi {
16964e36824Saddy ke 	struct device *dev;
17064e36824Saddy ke 
17164e36824Saddy ke 	struct clk *spiclk;
17264e36824Saddy ke 	struct clk *apb_pclk;
17364e36824Saddy ke 
17464e36824Saddy ke 	void __iomem *regs;
175eee06a9eSEmil Renner Berthing 	dma_addr_t dma_addr_rx;
176eee06a9eSEmil Renner Berthing 	dma_addr_t dma_addr_tx;
177fab3e487SEmil Renner Berthing 
17801b59ce5SEmil Renner Berthing 	const void *tx;
17901b59ce5SEmil Renner Berthing 	void *rx;
18001b59ce5SEmil Renner Berthing 	unsigned int tx_left;
18101b59ce5SEmil Renner Berthing 	unsigned int rx_left;
18201b59ce5SEmil Renner Berthing 
183fab3e487SEmil Renner Berthing 	atomic_t state;
184fab3e487SEmil Renner Berthing 
18564e36824Saddy ke 	/*depth of the FIFO buffer */
18664e36824Saddy ke 	u32 fifo_len;
187420b82f8SEmil Renner Berthing 	/* frequency of spiclk */
188420b82f8SEmil Renner Berthing 	u32 freq;
18964e36824Saddy ke 
19064e36824Saddy ke 	u8 n_bytes;
19174b7efa8SEmil Renner Berthing 	u8 rsd;
19264e36824Saddy ke 
193aa099382SJeffy Chen 	bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
19464e36824Saddy ke };
19564e36824Saddy ke 
19630688e4eSEmil Renner Berthing static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
19764e36824Saddy ke {
19830688e4eSEmil Renner Berthing 	writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR);
19964e36824Saddy ke }
20064e36824Saddy ke 
2012df08e78SAddy Ke static inline void wait_for_idle(struct rockchip_spi *rs)
2022df08e78SAddy Ke {
2032df08e78SAddy Ke 	unsigned long timeout = jiffies + msecs_to_jiffies(5);
2042df08e78SAddy Ke 
2052df08e78SAddy Ke 	do {
2062df08e78SAddy Ke 		if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
2072df08e78SAddy Ke 			return;
20864bc0110SDoug Anderson 	} while (!time_after(jiffies, timeout));
2092df08e78SAddy Ke 
2102df08e78SAddy Ke 	dev_warn(rs->dev, "spi controller is in busy state!\n");
2112df08e78SAddy Ke }
2122df08e78SAddy Ke 
21364e36824Saddy ke static u32 get_fifo_len(struct rockchip_spi *rs)
21464e36824Saddy ke {
21564e36824Saddy ke 	u32 fifo;
21664e36824Saddy ke 
21764e36824Saddy ke 	for (fifo = 2; fifo < 32; fifo++) {
21864e36824Saddy ke 		writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR);
21964e36824Saddy ke 		if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR))
22064e36824Saddy ke 			break;
22164e36824Saddy ke 	}
22264e36824Saddy ke 
22364e36824Saddy ke 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR);
22464e36824Saddy ke 
22564e36824Saddy ke 	return (fifo == 31) ? 0 : fifo;
22664e36824Saddy ke }
22764e36824Saddy ke 
22864e36824Saddy ke static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
22964e36824Saddy ke {
230b920cc31SHuibin Hong 	struct spi_master *master = spi->master;
231b920cc31SHuibin Hong 	struct rockchip_spi *rs = spi_master_get_devdata(master);
232aa099382SJeffy Chen 	bool cs_asserted = !enable;
233b920cc31SHuibin Hong 
234aa099382SJeffy Chen 	/* Return immediately for no-op */
235aa099382SJeffy Chen 	if (cs_asserted == rs->cs_asserted[spi->chip_select])
236aa099382SJeffy Chen 		return;
237aa099382SJeffy Chen 
238aa099382SJeffy Chen 	if (cs_asserted) {
239aa099382SJeffy Chen 		/* Keep things powered as long as CS is asserted */
240b920cc31SHuibin Hong 		pm_runtime_get_sync(rs->dev);
24164e36824Saddy ke 
242aa099382SJeffy Chen 		ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER,
243aa099382SJeffy Chen 				      BIT(spi->chip_select));
244aa099382SJeffy Chen 	} else {
245aa099382SJeffy Chen 		ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER,
246aa099382SJeffy Chen 				      BIT(spi->chip_select));
24764e36824Saddy ke 
248aa099382SJeffy Chen 		/* Drop reference from when we first asserted CS */
249aa099382SJeffy Chen 		pm_runtime_put(rs->dev);
250aa099382SJeffy Chen 	}
25164e36824Saddy ke 
252aa099382SJeffy Chen 	rs->cs_asserted[spi->chip_select] = cs_asserted;
25364e36824Saddy ke }
25464e36824Saddy ke 
2552291793cSAndy Shevchenko static void rockchip_spi_handle_err(struct spi_master *master,
25664e36824Saddy ke 				    struct spi_message *msg)
25764e36824Saddy ke {
25864e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
25964e36824Saddy ke 
260ce386100SEmil Renner Berthing 	/* stop running spi transfer
261ce386100SEmil Renner Berthing 	 * this also flushes both rx and tx fifos
2625dcc44edSAddy Ke 	 */
263ce386100SEmil Renner Berthing 	spi_enable_chip(rs, false);
264ce386100SEmil Renner Berthing 
26501b59ce5SEmil Renner Berthing 	/* make sure all interrupts are masked */
26601b59ce5SEmil Renner Berthing 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
26701b59ce5SEmil Renner Berthing 
268fab3e487SEmil Renner Berthing 	if (atomic_read(&rs->state) & TXDMA)
269eee06a9eSEmil Renner Berthing 		dmaengine_terminate_async(master->dma_tx);
270fab3e487SEmil Renner Berthing 
271ce386100SEmil Renner Berthing 	if (atomic_read(&rs->state) & RXDMA)
272eee06a9eSEmil Renner Berthing 		dmaengine_terminate_async(master->dma_rx);
27364e36824Saddy ke }
27464e36824Saddy ke 
27564e36824Saddy ke static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
27664e36824Saddy ke {
27701b59ce5SEmil Renner Berthing 	u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
27801b59ce5SEmil Renner Berthing 	u32 words = min(rs->tx_left, tx_free);
27964e36824Saddy ke 
28001b59ce5SEmil Renner Berthing 	rs->tx_left -= words;
28101b59ce5SEmil Renner Berthing 	for (; words; words--) {
28201b59ce5SEmil Renner Berthing 		u32 txw;
28301b59ce5SEmil Renner Berthing 
28464e36824Saddy ke 		if (rs->n_bytes == 1)
28501b59ce5SEmil Renner Berthing 			txw = *(u8 *)rs->tx;
28664e36824Saddy ke 		else
28701b59ce5SEmil Renner Berthing 			txw = *(u16 *)rs->tx;
28864e36824Saddy ke 
28964e36824Saddy ke 		writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
29064e36824Saddy ke 		rs->tx += rs->n_bytes;
29164e36824Saddy ke 	}
29264e36824Saddy ke }
29364e36824Saddy ke 
29464e36824Saddy ke static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
29564e36824Saddy ke {
29601b59ce5SEmil Renner Berthing 	u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
29701b59ce5SEmil Renner Berthing 	u32 rx_left = rs->rx_left - words;
29864e36824Saddy ke 
29901b59ce5SEmil Renner Berthing 	/* the hardware doesn't allow us to change fifo threshold
30001b59ce5SEmil Renner Berthing 	 * level while spi is enabled, so instead make sure to leave
30101b59ce5SEmil Renner Berthing 	 * enough words in the rx fifo to get the last interrupt
30201b59ce5SEmil Renner Berthing 	 * exactly when all words have been received
30301b59ce5SEmil Renner Berthing 	 */
30401b59ce5SEmil Renner Berthing 	if (rx_left) {
30501b59ce5SEmil Renner Berthing 		u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1;
30601b59ce5SEmil Renner Berthing 
30701b59ce5SEmil Renner Berthing 		if (rx_left < ftl) {
30801b59ce5SEmil Renner Berthing 			rx_left = ftl;
30901b59ce5SEmil Renner Berthing 			words = rs->rx_left - rx_left;
31001b59ce5SEmil Renner Berthing 		}
31101b59ce5SEmil Renner Berthing 	}
31201b59ce5SEmil Renner Berthing 
31301b59ce5SEmil Renner Berthing 	rs->rx_left = rx_left;
31401b59ce5SEmil Renner Berthing 	for (; words; words--) {
31501b59ce5SEmil Renner Berthing 		u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
31601b59ce5SEmil Renner Berthing 
31701b59ce5SEmil Renner Berthing 		if (!rs->rx)
31801b59ce5SEmil Renner Berthing 			continue;
31901b59ce5SEmil Renner Berthing 
32064e36824Saddy ke 		if (rs->n_bytes == 1)
32101b59ce5SEmil Renner Berthing 			*(u8 *)rs->rx = (u8)rxw;
32264e36824Saddy ke 		else
32301b59ce5SEmil Renner Berthing 			*(u16 *)rs->rx = (u16)rxw;
32464e36824Saddy ke 		rs->rx += rs->n_bytes;
3255dcc44edSAddy Ke 	}
32664e36824Saddy ke }
32764e36824Saddy ke 
32801b59ce5SEmil Renner Berthing static irqreturn_t rockchip_spi_isr(int irq, void *dev_id)
32964e36824Saddy ke {
33001b59ce5SEmil Renner Berthing 	struct spi_master *master = dev_id;
33101b59ce5SEmil Renner Berthing 	struct rockchip_spi *rs = spi_master_get_devdata(master);
33264e36824Saddy ke 
33301b59ce5SEmil Renner Berthing 	if (rs->tx_left)
33401b59ce5SEmil Renner Berthing 		rockchip_spi_pio_writer(rs);
33501b59ce5SEmil Renner Berthing 
33601b59ce5SEmil Renner Berthing 	rockchip_spi_pio_reader(rs);
33701b59ce5SEmil Renner Berthing 	if (!rs->rx_left) {
33801b59ce5SEmil Renner Berthing 		spi_enable_chip(rs, false);
33901b59ce5SEmil Renner Berthing 		writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
34001b59ce5SEmil Renner Berthing 		spi_finalize_current_transfer(master);
34101b59ce5SEmil Renner Berthing 	}
34201b59ce5SEmil Renner Berthing 
34301b59ce5SEmil Renner Berthing 	return IRQ_HANDLED;
34401b59ce5SEmil Renner Berthing }
34501b59ce5SEmil Renner Berthing 
34601b59ce5SEmil Renner Berthing static int rockchip_spi_prepare_irq(struct rockchip_spi *rs,
34701b59ce5SEmil Renner Berthing 		struct spi_transfer *xfer)
34801b59ce5SEmil Renner Berthing {
34901b59ce5SEmil Renner Berthing 	rs->tx = xfer->tx_buf;
35001b59ce5SEmil Renner Berthing 	rs->rx = xfer->rx_buf;
35101b59ce5SEmil Renner Berthing 	rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0;
35201b59ce5SEmil Renner Berthing 	rs->rx_left = xfer->len / rs->n_bytes;
35301b59ce5SEmil Renner Berthing 
35401b59ce5SEmil Renner Berthing 	writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR);
35530688e4eSEmil Renner Berthing 	spi_enable_chip(rs, true);
356a3c17402SEmil Renner Berthing 
35701b59ce5SEmil Renner Berthing 	if (rs->tx_left)
35864e36824Saddy ke 		rockchip_spi_pio_writer(rs);
35964e36824Saddy ke 
36001b59ce5SEmil Renner Berthing 	/* 1 means the transfer is in progress */
36101b59ce5SEmil Renner Berthing 	return 1;
36264e36824Saddy ke }
36364e36824Saddy ke 
36464e36824Saddy ke static void rockchip_spi_dma_rxcb(void *data)
36564e36824Saddy ke {
366d790c342SEmil Renner Berthing 	struct spi_master *master = data;
367d790c342SEmil Renner Berthing 	struct rockchip_spi *rs = spi_master_get_devdata(master);
368fab3e487SEmil Renner Berthing 	int state = atomic_fetch_andnot(RXDMA, &rs->state);
36964e36824Saddy ke 
370fab3e487SEmil Renner Berthing 	if (state & TXDMA)
371fab3e487SEmil Renner Berthing 		return;
37264e36824Saddy ke 
37330688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
374d790c342SEmil Renner Berthing 	spi_finalize_current_transfer(master);
375c28be31bSAddy Ke }
37664e36824Saddy ke 
37764e36824Saddy ke static void rockchip_spi_dma_txcb(void *data)
37864e36824Saddy ke {
379d790c342SEmil Renner Berthing 	struct spi_master *master = data;
380d790c342SEmil Renner Berthing 	struct rockchip_spi *rs = spi_master_get_devdata(master);
381fab3e487SEmil Renner Berthing 	int state = atomic_fetch_andnot(TXDMA, &rs->state);
382fab3e487SEmil Renner Berthing 
383fab3e487SEmil Renner Berthing 	if (state & RXDMA)
384fab3e487SEmil Renner Berthing 		return;
38564e36824Saddy ke 
3862df08e78SAddy Ke 	/* Wait until the FIFO data completely. */
3872df08e78SAddy Ke 	wait_for_idle(rs);
3882df08e78SAddy Ke 
38930688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
390d790c342SEmil Renner Berthing 	spi_finalize_current_transfer(master);
3912c2bc748SAddy Ke }
39264e36824Saddy ke 
393fc1ad8eeSEmil Renner Berthing static int rockchip_spi_prepare_dma(struct rockchip_spi *rs,
394eee06a9eSEmil Renner Berthing 		struct spi_master *master, struct spi_transfer *xfer)
39564e36824Saddy ke {
39664e36824Saddy ke 	struct dma_async_tx_descriptor *rxdesc, *txdesc;
39764e36824Saddy ke 
398fab3e487SEmil Renner Berthing 	atomic_set(&rs->state, 0);
39964e36824Saddy ke 
40097cf5669SArnd Bergmann 	rxdesc = NULL;
401fc1ad8eeSEmil Renner Berthing 	if (xfer->rx_buf) {
40231bcb57bSEmil Renner Berthing 		struct dma_slave_config rxconf = {
40331bcb57bSEmil Renner Berthing 			.direction = DMA_DEV_TO_MEM,
404eee06a9eSEmil Renner Berthing 			.src_addr = rs->dma_addr_rx,
40531bcb57bSEmil Renner Berthing 			.src_addr_width = rs->n_bytes,
40631bcb57bSEmil Renner Berthing 			.src_maxburst = 1,
40731bcb57bSEmil Renner Berthing 		};
40831bcb57bSEmil Renner Berthing 
409eee06a9eSEmil Renner Berthing 		dmaengine_slave_config(master->dma_rx, &rxconf);
41064e36824Saddy ke 
4115dcc44edSAddy Ke 		rxdesc = dmaengine_prep_slave_sg(
412eee06a9eSEmil Renner Berthing 				master->dma_rx,
413fc1ad8eeSEmil Renner Berthing 				xfer->rx_sg.sgl, xfer->rx_sg.nents,
414d9071b7eSEmil Renner Berthing 				DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
415ea984911SShawn Lin 		if (!rxdesc)
416ea984911SShawn Lin 			return -EINVAL;
41764e36824Saddy ke 
41864e36824Saddy ke 		rxdesc->callback = rockchip_spi_dma_rxcb;
419d790c342SEmil Renner Berthing 		rxdesc->callback_param = master;
42064e36824Saddy ke 	}
42164e36824Saddy ke 
42297cf5669SArnd Bergmann 	txdesc = NULL;
423fc1ad8eeSEmil Renner Berthing 	if (xfer->tx_buf) {
42431bcb57bSEmil Renner Berthing 		struct dma_slave_config txconf = {
42531bcb57bSEmil Renner Berthing 			.direction = DMA_MEM_TO_DEV,
426eee06a9eSEmil Renner Berthing 			.dst_addr = rs->dma_addr_tx,
42731bcb57bSEmil Renner Berthing 			.dst_addr_width = rs->n_bytes,
428*47300728SEmil Renner Berthing 			.dst_maxburst = rs->fifo_len / 4,
42931bcb57bSEmil Renner Berthing 		};
43031bcb57bSEmil Renner Berthing 
431eee06a9eSEmil Renner Berthing 		dmaengine_slave_config(master->dma_tx, &txconf);
43264e36824Saddy ke 
4335dcc44edSAddy Ke 		txdesc = dmaengine_prep_slave_sg(
434eee06a9eSEmil Renner Berthing 				master->dma_tx,
435fc1ad8eeSEmil Renner Berthing 				xfer->tx_sg.sgl, xfer->tx_sg.nents,
436d9071b7eSEmil Renner Berthing 				DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
437ea984911SShawn Lin 		if (!txdesc) {
438ea984911SShawn Lin 			if (rxdesc)
439eee06a9eSEmil Renner Berthing 				dmaengine_terminate_sync(master->dma_rx);
440ea984911SShawn Lin 			return -EINVAL;
441ea984911SShawn Lin 		}
44264e36824Saddy ke 
44364e36824Saddy ke 		txdesc->callback = rockchip_spi_dma_txcb;
444d790c342SEmil Renner Berthing 		txdesc->callback_param = master;
44564e36824Saddy ke 	}
44664e36824Saddy ke 
44764e36824Saddy ke 	/* rx must be started before tx due to spi instinct */
44897cf5669SArnd Bergmann 	if (rxdesc) {
449fab3e487SEmil Renner Berthing 		atomic_or(RXDMA, &rs->state);
45064e36824Saddy ke 		dmaengine_submit(rxdesc);
451eee06a9eSEmil Renner Berthing 		dma_async_issue_pending(master->dma_rx);
45264e36824Saddy ke 	}
45364e36824Saddy ke 
45430688e4eSEmil Renner Berthing 	spi_enable_chip(rs, true);
455a3c17402SEmil Renner Berthing 
45697cf5669SArnd Bergmann 	if (txdesc) {
457fab3e487SEmil Renner Berthing 		atomic_or(TXDMA, &rs->state);
45864e36824Saddy ke 		dmaengine_submit(txdesc);
459eee06a9eSEmil Renner Berthing 		dma_async_issue_pending(master->dma_tx);
46064e36824Saddy ke 	}
461ea984911SShawn Lin 
462a3c17402SEmil Renner Berthing 	/* 1 means the transfer is in progress */
463a3c17402SEmil Renner Berthing 	return 1;
46464e36824Saddy ke }
46564e36824Saddy ke 
466fc1ad8eeSEmil Renner Berthing static void rockchip_spi_config(struct rockchip_spi *rs,
467eff0275eSEmil Renner Berthing 		struct spi_device *spi, struct spi_transfer *xfer,
468eff0275eSEmil Renner Berthing 		bool use_dma)
46964e36824Saddy ke {
4702410d6a3SEmil Renner Berthing 	u32 cr0 = CR0_FRF_SPI  << CR0_FRF_OFFSET
4712410d6a3SEmil Renner Berthing 	        | CR0_BHT_8BIT << CR0_BHT_OFFSET
4722410d6a3SEmil Renner Berthing 	        | CR0_SSD_ONE  << CR0_SSD_OFFSET
4732410d6a3SEmil Renner Berthing 	        | CR0_EM_BIG   << CR0_EM_OFFSET;
47465498c6aSEmil Renner Berthing 	u32 cr1;
47565498c6aSEmil Renner Berthing 	u32 dmacr = 0;
47664e36824Saddy ke 
47774b7efa8SEmil Renner Berthing 	cr0 |= rs->rsd << CR0_RSD_OFFSET;
478fc1ad8eeSEmil Renner Berthing 	cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET;
47904290192SEmil Renner Berthing 	if (spi->mode & SPI_LSB_FIRST)
48004290192SEmil Renner Berthing 		cr0 |= CR0_FBM_LSB << CR0_FBM_OFFSET;
481fc1ad8eeSEmil Renner Berthing 
482fc1ad8eeSEmil Renner Berthing 	if (xfer->rx_buf && xfer->tx_buf)
483fc1ad8eeSEmil Renner Berthing 		cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET;
484fc1ad8eeSEmil Renner Berthing 	else if (xfer->rx_buf)
485fc1ad8eeSEmil Renner Berthing 		cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET;
48601b59ce5SEmil Renner Berthing 	else if (use_dma)
487fc1ad8eeSEmil Renner Berthing 		cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET;
48864e36824Saddy ke 
48965498c6aSEmil Renner Berthing 	switch (xfer->bits_per_word) {
49065498c6aSEmil Renner Berthing 	case 4:
49165498c6aSEmil Renner Berthing 		cr0 |= CR0_DFS_4BIT << CR0_DFS_OFFSET;
49265498c6aSEmil Renner Berthing 		cr1 = xfer->len - 1;
49365498c6aSEmil Renner Berthing 		break;
49465498c6aSEmil Renner Berthing 	case 8:
49565498c6aSEmil Renner Berthing 		cr0 |= CR0_DFS_8BIT << CR0_DFS_OFFSET;
49665498c6aSEmil Renner Berthing 		cr1 = xfer->len - 1;
49765498c6aSEmil Renner Berthing 		break;
49865498c6aSEmil Renner Berthing 	case 16:
49965498c6aSEmil Renner Berthing 		cr0 |= CR0_DFS_16BIT << CR0_DFS_OFFSET;
50065498c6aSEmil Renner Berthing 		cr1 = xfer->len / 2 - 1;
50165498c6aSEmil Renner Berthing 		break;
50265498c6aSEmil Renner Berthing 	default:
50365498c6aSEmil Renner Berthing 		/* we only whitelist 4, 8 and 16 bit words in
50465498c6aSEmil Renner Berthing 		 * master->bits_per_word_mask, so this shouldn't
50565498c6aSEmil Renner Berthing 		 * happen
50665498c6aSEmil Renner Berthing 		 */
50765498c6aSEmil Renner Berthing 		unreachable();
50865498c6aSEmil Renner Berthing 	}
50965498c6aSEmil Renner Berthing 
510eff0275eSEmil Renner Berthing 	if (use_dma) {
511fc1ad8eeSEmil Renner Berthing 		if (xfer->tx_buf)
51264e36824Saddy ke 			dmacr |= TF_DMA_EN;
513fc1ad8eeSEmil Renner Berthing 		if (xfer->rx_buf)
51464e36824Saddy ke 			dmacr |= RF_DMA_EN;
51564e36824Saddy ke 	}
51664e36824Saddy ke 
51764e36824Saddy ke 	writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
51865498c6aSEmil Renner Berthing 	writel_relaxed(cr1, rs->regs + ROCKCHIP_SPI_CTRLR1);
51904b37d2dSHuibin Hong 
52001b59ce5SEmil Renner Berthing 	/* unfortunately setting the fifo threshold level to generate an
52101b59ce5SEmil Renner Berthing 	 * interrupt exactly when the fifo is full doesn't seem to work,
52201b59ce5SEmil Renner Berthing 	 * so we need the strict inequality here
52301b59ce5SEmil Renner Berthing 	 */
52401b59ce5SEmil Renner Berthing 	if (xfer->len < rs->fifo_len)
52501b59ce5SEmil Renner Berthing 		writel_relaxed(xfer->len - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
52601b59ce5SEmil Renner Berthing 	else
52764e36824Saddy ke 		writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
52864e36824Saddy ke 
529*47300728SEmil Renner Berthing 	writel_relaxed(rs->fifo_len / 2, rs->regs + ROCKCHIP_SPI_DMATDLR);
53064e36824Saddy ke 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR);
53164e36824Saddy ke 	writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
53264e36824Saddy ke 
533420b82f8SEmil Renner Berthing 	/* the hardware only supports an even clock divisor, so
534420b82f8SEmil Renner Berthing 	 * round divisor = spiclk / speed up to nearest even number
535420b82f8SEmil Renner Berthing 	 * so that the resulting speed is <= the requested speed
536420b82f8SEmil Renner Berthing 	 */
537420b82f8SEmil Renner Berthing 	writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz),
538420b82f8SEmil Renner Berthing 			rs->regs + ROCKCHIP_SPI_BAUDR);
53964e36824Saddy ke }
54064e36824Saddy ke 
5415185a81cSBrian Norris static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
5425185a81cSBrian Norris {
5435185a81cSBrian Norris 	return ROCKCHIP_SPI_MAX_TRANLEN;
5445185a81cSBrian Norris }
5455185a81cSBrian Norris 
5465dcc44edSAddy Ke static int rockchip_spi_transfer_one(
5475dcc44edSAddy Ke 		struct spi_master *master,
54864e36824Saddy ke 		struct spi_device *spi,
54964e36824Saddy ke 		struct spi_transfer *xfer)
55064e36824Saddy ke {
55164e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
552eff0275eSEmil Renner Berthing 	bool use_dma;
55364e36824Saddy ke 
55462946172SDoug Anderson 	WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
55562946172SDoug Anderson 		(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
55664e36824Saddy ke 
55764e36824Saddy ke 	if (!xfer->tx_buf && !xfer->rx_buf) {
55864e36824Saddy ke 		dev_err(rs->dev, "No buffer for transfer\n");
55964e36824Saddy ke 		return -EINVAL;
56064e36824Saddy ke 	}
56164e36824Saddy ke 
5625185a81cSBrian Norris 	if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
5635185a81cSBrian Norris 		dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
5645185a81cSBrian Norris 		return -EINVAL;
5655185a81cSBrian Norris 	}
5665185a81cSBrian Norris 
56765498c6aSEmil Renner Berthing 	rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2;
56864e36824Saddy ke 
569eff0275eSEmil Renner Berthing 	use_dma = master->can_dma ? master->can_dma(master, spi, xfer) : false;
57064e36824Saddy ke 
571eff0275eSEmil Renner Berthing 	rockchip_spi_config(rs, spi, xfer, use_dma);
57264e36824Saddy ke 
573eff0275eSEmil Renner Berthing 	if (use_dma)
574eee06a9eSEmil Renner Berthing 		return rockchip_spi_prepare_dma(rs, master, xfer);
57564e36824Saddy ke 
57601b59ce5SEmil Renner Berthing 	return rockchip_spi_prepare_irq(rs, xfer);
57764e36824Saddy ke }
57864e36824Saddy ke 
57964e36824Saddy ke static bool rockchip_spi_can_dma(struct spi_master *master,
58064e36824Saddy ke 				 struct spi_device *spi,
58164e36824Saddy ke 				 struct spi_transfer *xfer)
58264e36824Saddy ke {
58364e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
58401b59ce5SEmil Renner Berthing 	unsigned int bytes_per_word = xfer->bits_per_word <= 8 ? 1 : 2;
58564e36824Saddy ke 
58601b59ce5SEmil Renner Berthing 	/* if the numbor of spi words to transfer is less than the fifo
58701b59ce5SEmil Renner Berthing 	 * length we can just fill the fifo and wait for a single irq,
58801b59ce5SEmil Renner Berthing 	 * so don't bother setting up dma
58901b59ce5SEmil Renner Berthing 	 */
59001b59ce5SEmil Renner Berthing 	return xfer->len / bytes_per_word >= rs->fifo_len;
59164e36824Saddy ke }
59264e36824Saddy ke 
59364e36824Saddy ke static int rockchip_spi_probe(struct platform_device *pdev)
59464e36824Saddy ke {
59543de979dSJeffy Chen 	int ret;
59664e36824Saddy ke 	struct rockchip_spi *rs;
59764e36824Saddy ke 	struct spi_master *master;
59864e36824Saddy ke 	struct resource *mem;
59976b17e6eSJulius Werner 	u32 rsd_nsecs;
60064e36824Saddy ke 
60164e36824Saddy ke 	master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi));
6025dcc44edSAddy Ke 	if (!master)
60364e36824Saddy ke 		return -ENOMEM;
6045dcc44edSAddy Ke 
60564e36824Saddy ke 	platform_set_drvdata(pdev, master);
60664e36824Saddy ke 
60764e36824Saddy ke 	rs = spi_master_get_devdata(master);
60864e36824Saddy ke 
60964e36824Saddy ke 	/* Get basic io resource and map it */
61064e36824Saddy ke 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
61164e36824Saddy ke 	rs->regs = devm_ioremap_resource(&pdev->dev, mem);
61264e36824Saddy ke 	if (IS_ERR(rs->regs)) {
61364e36824Saddy ke 		ret =  PTR_ERR(rs->regs);
614c351587eSJeffy Chen 		goto err_put_master;
61564e36824Saddy ke 	}
61664e36824Saddy ke 
61764e36824Saddy ke 	rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
61864e36824Saddy ke 	if (IS_ERR(rs->apb_pclk)) {
61964e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get apb_pclk\n");
62064e36824Saddy ke 		ret = PTR_ERR(rs->apb_pclk);
621c351587eSJeffy Chen 		goto err_put_master;
62264e36824Saddy ke 	}
62364e36824Saddy ke 
62464e36824Saddy ke 	rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
62564e36824Saddy ke 	if (IS_ERR(rs->spiclk)) {
62664e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get spi_pclk\n");
62764e36824Saddy ke 		ret = PTR_ERR(rs->spiclk);
628c351587eSJeffy Chen 		goto err_put_master;
62964e36824Saddy ke 	}
63064e36824Saddy ke 
63164e36824Saddy ke 	ret = clk_prepare_enable(rs->apb_pclk);
63243de979dSJeffy Chen 	if (ret < 0) {
63364e36824Saddy ke 		dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
634c351587eSJeffy Chen 		goto err_put_master;
63564e36824Saddy ke 	}
63664e36824Saddy ke 
63764e36824Saddy ke 	ret = clk_prepare_enable(rs->spiclk);
63843de979dSJeffy Chen 	if (ret < 0) {
63964e36824Saddy ke 		dev_err(&pdev->dev, "Failed to enable spi_clk\n");
640c351587eSJeffy Chen 		goto err_disable_apbclk;
64164e36824Saddy ke 	}
64264e36824Saddy ke 
64330688e4eSEmil Renner Berthing 	spi_enable_chip(rs, false);
64464e36824Saddy ke 
64501b59ce5SEmil Renner Berthing 	ret = platform_get_irq(pdev, 0);
64601b59ce5SEmil Renner Berthing 	if (ret < 0)
64701b59ce5SEmil Renner Berthing 		goto err_disable_spiclk;
64801b59ce5SEmil Renner Berthing 
64901b59ce5SEmil Renner Berthing 	ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL,
65001b59ce5SEmil Renner Berthing 			IRQF_ONESHOT, dev_name(&pdev->dev), master);
65101b59ce5SEmil Renner Berthing 	if (ret)
65201b59ce5SEmil Renner Berthing 		goto err_disable_spiclk;
65301b59ce5SEmil Renner Berthing 
65464e36824Saddy ke 	rs->dev = &pdev->dev;
655420b82f8SEmil Renner Berthing 	rs->freq = clk_get_rate(rs->spiclk);
65664e36824Saddy ke 
65776b17e6eSJulius Werner 	if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
65874b7efa8SEmil Renner Berthing 				  &rsd_nsecs)) {
65974b7efa8SEmil Renner Berthing 		/* rx sample delay is expressed in parent clock cycles (max 3) */
66074b7efa8SEmil Renner Berthing 		u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8),
66174b7efa8SEmil Renner Berthing 				1000000000 >> 8);
66274b7efa8SEmil Renner Berthing 		if (!rsd) {
66374b7efa8SEmil Renner Berthing 			dev_warn(rs->dev, "%u Hz are too slow to express %u ns delay\n",
66474b7efa8SEmil Renner Berthing 					rs->freq, rsd_nsecs);
66574b7efa8SEmil Renner Berthing 		} else if (rsd > CR0_RSD_MAX) {
66674b7efa8SEmil Renner Berthing 			rsd = CR0_RSD_MAX;
66774b7efa8SEmil Renner Berthing 			dev_warn(rs->dev, "%u Hz are too fast to express %u ns delay, clamping at %u ns\n",
66874b7efa8SEmil Renner Berthing 					rs->freq, rsd_nsecs,
66974b7efa8SEmil Renner Berthing 					CR0_RSD_MAX * 1000000000U / rs->freq);
67074b7efa8SEmil Renner Berthing 		}
67174b7efa8SEmil Renner Berthing 		rs->rsd = rsd;
67274b7efa8SEmil Renner Berthing 	}
67376b17e6eSJulius Werner 
67464e36824Saddy ke 	rs->fifo_len = get_fifo_len(rs);
67564e36824Saddy ke 	if (!rs->fifo_len) {
67664e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get fifo length\n");
677db7e8d90SWei Yongjun 		ret = -EINVAL;
678c351587eSJeffy Chen 		goto err_disable_spiclk;
67964e36824Saddy ke 	}
68064e36824Saddy ke 
68164e36824Saddy ke 	pm_runtime_set_active(&pdev->dev);
68264e36824Saddy ke 	pm_runtime_enable(&pdev->dev);
68364e36824Saddy ke 
68464e36824Saddy ke 	master->auto_runtime_pm = true;
68564e36824Saddy ke 	master->bus_num = pdev->id;
68604290192SEmil Renner Berthing 	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST;
687aa099382SJeffy Chen 	master->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM;
68864e36824Saddy ke 	master->dev.of_node = pdev->dev.of_node;
68965498c6aSEmil Renner Berthing 	master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4);
690420b82f8SEmil Renner Berthing 	master->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX;
691420b82f8SEmil Renner Berthing 	master->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT);
69264e36824Saddy ke 
69364e36824Saddy ke 	master->set_cs = rockchip_spi_set_cs;
69464e36824Saddy ke 	master->transfer_one = rockchip_spi_transfer_one;
6955185a81cSBrian Norris 	master->max_transfer_size = rockchip_spi_max_transfer_size;
6962291793cSAndy Shevchenko 	master->handle_err = rockchip_spi_handle_err;
697c863795cSJeffy Chen 	master->flags = SPI_MASTER_GPIO_SS;
69864e36824Saddy ke 
699eee06a9eSEmil Renner Berthing 	master->dma_tx = dma_request_chan(rs->dev, "tx");
700eee06a9eSEmil Renner Berthing 	if (IS_ERR(master->dma_tx)) {
70161cadcf4SShawn Lin 		/* Check tx to see if we need defer probing driver */
702eee06a9eSEmil Renner Berthing 		if (PTR_ERR(master->dma_tx) == -EPROBE_DEFER) {
70361cadcf4SShawn Lin 			ret = -EPROBE_DEFER;
704c351587eSJeffy Chen 			goto err_disable_pm_runtime;
70561cadcf4SShawn Lin 		}
70664e36824Saddy ke 		dev_warn(rs->dev, "Failed to request TX DMA channel\n");
707eee06a9eSEmil Renner Berthing 		master->dma_tx = NULL;
70864e36824Saddy ke 	}
709e4c0e06fSShawn Lin 
710eee06a9eSEmil Renner Berthing 	master->dma_rx = dma_request_chan(rs->dev, "rx");
711eee06a9eSEmil Renner Berthing 	if (IS_ERR(master->dma_rx)) {
712eee06a9eSEmil Renner Berthing 		if (PTR_ERR(master->dma_rx) == -EPROBE_DEFER) {
713e4c0e06fSShawn Lin 			ret = -EPROBE_DEFER;
7145de7ed0cSDan Carpenter 			goto err_free_dma_tx;
715e4c0e06fSShawn Lin 		}
71664e36824Saddy ke 		dev_warn(rs->dev, "Failed to request RX DMA channel\n");
717eee06a9eSEmil Renner Berthing 		master->dma_rx = NULL;
71864e36824Saddy ke 	}
71964e36824Saddy ke 
720eee06a9eSEmil Renner Berthing 	if (master->dma_tx && master->dma_rx) {
721eee06a9eSEmil Renner Berthing 		rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR;
722eee06a9eSEmil Renner Berthing 		rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR;
72364e36824Saddy ke 		master->can_dma = rockchip_spi_can_dma;
72464e36824Saddy ke 	}
72564e36824Saddy ke 
72664e36824Saddy ke 	ret = devm_spi_register_master(&pdev->dev, master);
72743de979dSJeffy Chen 	if (ret < 0) {
72864e36824Saddy ke 		dev_err(&pdev->dev, "Failed to register master\n");
729c351587eSJeffy Chen 		goto err_free_dma_rx;
73064e36824Saddy ke 	}
73164e36824Saddy ke 
73264e36824Saddy ke 	return 0;
73364e36824Saddy ke 
734c351587eSJeffy Chen err_free_dma_rx:
735eee06a9eSEmil Renner Berthing 	if (master->dma_rx)
736eee06a9eSEmil Renner Berthing 		dma_release_channel(master->dma_rx);
7375de7ed0cSDan Carpenter err_free_dma_tx:
738eee06a9eSEmil Renner Berthing 	if (master->dma_tx)
739eee06a9eSEmil Renner Berthing 		dma_release_channel(master->dma_tx);
740c351587eSJeffy Chen err_disable_pm_runtime:
741c351587eSJeffy Chen 	pm_runtime_disable(&pdev->dev);
742c351587eSJeffy Chen err_disable_spiclk:
74364e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
744c351587eSJeffy Chen err_disable_apbclk:
74564e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
746c351587eSJeffy Chen err_put_master:
74764e36824Saddy ke 	spi_master_put(master);
74864e36824Saddy ke 
74964e36824Saddy ke 	return ret;
75064e36824Saddy ke }
75164e36824Saddy ke 
75264e36824Saddy ke static int rockchip_spi_remove(struct platform_device *pdev)
75364e36824Saddy ke {
75464e36824Saddy ke 	struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
75564e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
75664e36824Saddy ke 
7576a06e895SJeffy Chen 	pm_runtime_get_sync(&pdev->dev);
75864e36824Saddy ke 
75964e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
76064e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
76164e36824Saddy ke 
7626a06e895SJeffy Chen 	pm_runtime_put_noidle(&pdev->dev);
7636a06e895SJeffy Chen 	pm_runtime_disable(&pdev->dev);
7646a06e895SJeffy Chen 	pm_runtime_set_suspended(&pdev->dev);
7656a06e895SJeffy Chen 
766eee06a9eSEmil Renner Berthing 	if (master->dma_tx)
767eee06a9eSEmil Renner Berthing 		dma_release_channel(master->dma_tx);
768eee06a9eSEmil Renner Berthing 	if (master->dma_rx)
769eee06a9eSEmil Renner Berthing 		dma_release_channel(master->dma_rx);
77064e36824Saddy ke 
771844c9f47SShawn Lin 	spi_master_put(master);
772844c9f47SShawn Lin 
77364e36824Saddy ke 	return 0;
77464e36824Saddy ke }
77564e36824Saddy ke 
77664e36824Saddy ke #ifdef CONFIG_PM_SLEEP
77764e36824Saddy ke static int rockchip_spi_suspend(struct device *dev)
77864e36824Saddy ke {
77943de979dSJeffy Chen 	int ret;
78064e36824Saddy ke 	struct spi_master *master = dev_get_drvdata(dev);
78164e36824Saddy ke 
782d790c342SEmil Renner Berthing 	ret = spi_master_suspend(master);
78343de979dSJeffy Chen 	if (ret < 0)
78464e36824Saddy ke 		return ret;
78564e36824Saddy ke 
786d38c4ae1SJeffy Chen 	ret = pm_runtime_force_suspend(dev);
787d38c4ae1SJeffy Chen 	if (ret < 0)
788d38c4ae1SJeffy Chen 		return ret;
78964e36824Saddy ke 
79023e291c2SBrian Norris 	pinctrl_pm_select_sleep_state(dev);
79123e291c2SBrian Norris 
79243de979dSJeffy Chen 	return 0;
79364e36824Saddy ke }
79464e36824Saddy ke 
79564e36824Saddy ke static int rockchip_spi_resume(struct device *dev)
79664e36824Saddy ke {
79743de979dSJeffy Chen 	int ret;
79864e36824Saddy ke 	struct spi_master *master = dev_get_drvdata(dev);
79964e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
80064e36824Saddy ke 
80123e291c2SBrian Norris 	pinctrl_pm_select_default_state(dev);
80223e291c2SBrian Norris 
803d38c4ae1SJeffy Chen 	ret = pm_runtime_force_resume(dev);
80464e36824Saddy ke 	if (ret < 0)
80564e36824Saddy ke 		return ret;
80664e36824Saddy ke 
807d790c342SEmil Renner Berthing 	ret = spi_master_resume(master);
80864e36824Saddy ke 	if (ret < 0) {
80964e36824Saddy ke 		clk_disable_unprepare(rs->spiclk);
81064e36824Saddy ke 		clk_disable_unprepare(rs->apb_pclk);
81164e36824Saddy ke 	}
81264e36824Saddy ke 
81343de979dSJeffy Chen 	return 0;
81464e36824Saddy ke }
81564e36824Saddy ke #endif /* CONFIG_PM_SLEEP */
81664e36824Saddy ke 
817ec833050SRafael J. Wysocki #ifdef CONFIG_PM
81864e36824Saddy ke static int rockchip_spi_runtime_suspend(struct device *dev)
81964e36824Saddy ke {
82064e36824Saddy ke 	struct spi_master *master = dev_get_drvdata(dev);
82164e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
82264e36824Saddy ke 
82364e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
82464e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
82564e36824Saddy ke 
82664e36824Saddy ke 	return 0;
82764e36824Saddy ke }
82864e36824Saddy ke 
82964e36824Saddy ke static int rockchip_spi_runtime_resume(struct device *dev)
83064e36824Saddy ke {
83164e36824Saddy ke 	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 = clk_prepare_enable(rs->apb_pclk);
83643de979dSJeffy Chen 	if (ret < 0)
83764e36824Saddy ke 		return ret;
83864e36824Saddy ke 
83964e36824Saddy ke 	ret = clk_prepare_enable(rs->spiclk);
84043de979dSJeffy Chen 	if (ret < 0)
84164e36824Saddy ke 		clk_disable_unprepare(rs->apb_pclk);
84264e36824Saddy ke 
84343de979dSJeffy Chen 	return 0;
84464e36824Saddy ke }
845ec833050SRafael J. Wysocki #endif /* CONFIG_PM */
84664e36824Saddy ke 
84764e36824Saddy ke static const struct dev_pm_ops rockchip_spi_pm = {
84864e36824Saddy ke 	SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
84964e36824Saddy ke 	SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
85064e36824Saddy ke 			   rockchip_spi_runtime_resume, NULL)
85164e36824Saddy ke };
85264e36824Saddy ke 
85364e36824Saddy ke static const struct of_device_id rockchip_spi_dt_match[] = {
8546b860e69SAndy Yan 	{ .compatible = "rockchip,rv1108-spi", },
855aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3036-spi", },
85664e36824Saddy ke 	{ .compatible = "rockchip,rk3066-spi", },
857b839b785SAddy Ke 	{ .compatible = "rockchip,rk3188-spi", },
858aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3228-spi", },
859b839b785SAddy Ke 	{ .compatible = "rockchip,rk3288-spi", },
860aa29ea3dSCaesar Wang 	{ .compatible = "rockchip,rk3368-spi", },
8619b7a5622SXu Jianqun 	{ .compatible = "rockchip,rk3399-spi", },
86264e36824Saddy ke 	{ },
86364e36824Saddy ke };
86464e36824Saddy ke MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
86564e36824Saddy ke 
86664e36824Saddy ke static struct platform_driver rockchip_spi_driver = {
86764e36824Saddy ke 	.driver = {
86864e36824Saddy ke 		.name	= DRIVER_NAME,
86964e36824Saddy ke 		.pm = &rockchip_spi_pm,
87064e36824Saddy ke 		.of_match_table = of_match_ptr(rockchip_spi_dt_match),
87164e36824Saddy ke 	},
87264e36824Saddy ke 	.probe = rockchip_spi_probe,
87364e36824Saddy ke 	.remove = rockchip_spi_remove,
87464e36824Saddy ke };
87564e36824Saddy ke 
87664e36824Saddy ke module_platform_driver(rockchip_spi_driver);
87764e36824Saddy ke 
8785dcc44edSAddy Ke MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
87964e36824Saddy ke MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
88064e36824Saddy ke MODULE_LICENSE("GPL v2");
881