xref: /openbmc/linux/drivers/spi/spi-rockchip.c (revision 754ec43c0184aae48f5a8c917d8282449ab564a9)
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/init.h>
1764e36824Saddy ke #include <linux/module.h>
1864e36824Saddy ke #include <linux/clk.h>
1964e36824Saddy ke #include <linux/err.h>
2064e36824Saddy ke #include <linux/delay.h>
2164e36824Saddy ke #include <linux/interrupt.h>
2264e36824Saddy ke #include <linux/platform_device.h>
2364e36824Saddy ke #include <linux/slab.h>
2464e36824Saddy ke #include <linux/spi/spi.h>
2564e36824Saddy ke #include <linux/scatterlist.h>
2664e36824Saddy ke #include <linux/of.h>
2764e36824Saddy ke #include <linux/pm_runtime.h>
2864e36824Saddy ke #include <linux/io.h>
2964e36824Saddy ke #include <linux/dmaengine.h>
3064e36824Saddy ke 
3164e36824Saddy ke #define DRIVER_NAME "rockchip-spi"
3264e36824Saddy ke 
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 
15164e36824Saddy ke enum rockchip_ssi_type {
15264e36824Saddy ke 	SSI_MOTO_SPI = 0,
15364e36824Saddy ke 	SSI_TI_SSP,
15464e36824Saddy ke 	SSI_NS_MICROWIRE,
15564e36824Saddy ke };
15664e36824Saddy ke 
15764e36824Saddy ke struct rockchip_spi_dma_data {
15864e36824Saddy ke 	struct dma_chan *ch;
15964e36824Saddy ke 	enum dma_transfer_direction direction;
16064e36824Saddy ke 	dma_addr_t addr;
16164e36824Saddy ke };
16264e36824Saddy ke 
16364e36824Saddy ke struct rockchip_spi {
16464e36824Saddy ke 	struct device *dev;
16564e36824Saddy ke 	struct spi_master *master;
16664e36824Saddy ke 
16764e36824Saddy ke 	struct clk *spiclk;
16864e36824Saddy ke 	struct clk *apb_pclk;
16964e36824Saddy ke 
17064e36824Saddy ke 	void __iomem *regs;
17164e36824Saddy ke 	/*depth of the FIFO buffer */
17264e36824Saddy ke 	u32 fifo_len;
17364e36824Saddy ke 	/* max bus freq supported */
17464e36824Saddy ke 	u32 max_freq;
17564e36824Saddy ke 	/* supported slave numbers */
17664e36824Saddy ke 	enum rockchip_ssi_type type;
17764e36824Saddy ke 
17864e36824Saddy ke 	u16 mode;
17964e36824Saddy ke 	u8 tmode;
18064e36824Saddy ke 	u8 bpw;
18164e36824Saddy ke 	u8 n_bytes;
18264e36824Saddy ke 	unsigned len;
18364e36824Saddy ke 	u32 speed;
18464e36824Saddy ke 
18564e36824Saddy ke 	const void *tx;
18664e36824Saddy ke 	const void *tx_end;
18764e36824Saddy ke 	void *rx;
18864e36824Saddy ke 	void *rx_end;
18964e36824Saddy ke 
19064e36824Saddy ke 	u32 state;
1915dcc44edSAddy Ke 	/* protect state */
19264e36824Saddy ke 	spinlock_t lock;
19364e36824Saddy ke 
19464e36824Saddy ke 	struct completion xfer_completion;
19564e36824Saddy ke 
19664e36824Saddy ke 	u32 use_dma;
19764e36824Saddy ke 	struct sg_table tx_sg;
19864e36824Saddy ke 	struct sg_table rx_sg;
19964e36824Saddy ke 	struct rockchip_spi_dma_data dma_rx;
20064e36824Saddy ke 	struct rockchip_spi_dma_data dma_tx;
20164e36824Saddy ke };
20264e36824Saddy ke 
20364e36824Saddy ke static inline void spi_enable_chip(struct rockchip_spi *rs, int enable)
20464e36824Saddy ke {
20564e36824Saddy ke 	writel_relaxed((enable ? 1 : 0), rs->regs + ROCKCHIP_SPI_SSIENR);
20664e36824Saddy ke }
20764e36824Saddy ke 
20864e36824Saddy ke static inline void spi_set_clk(struct rockchip_spi *rs, u16 div)
20964e36824Saddy ke {
21064e36824Saddy ke 	writel_relaxed(div, rs->regs + ROCKCHIP_SPI_BAUDR);
21164e36824Saddy ke }
21264e36824Saddy ke 
21364e36824Saddy ke static inline void flush_fifo(struct rockchip_spi *rs)
21464e36824Saddy ke {
21564e36824Saddy ke 	while (readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR))
21664e36824Saddy ke 		readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
21764e36824Saddy ke }
21864e36824Saddy ke 
2192df08e78SAddy Ke static inline void wait_for_idle(struct rockchip_spi *rs)
2202df08e78SAddy Ke {
2212df08e78SAddy Ke 	unsigned long timeout = jiffies + msecs_to_jiffies(5);
2222df08e78SAddy Ke 
2232df08e78SAddy Ke 	do {
2242df08e78SAddy Ke 		if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
2252df08e78SAddy Ke 			return;
22664bc0110SDoug Anderson 	} while (!time_after(jiffies, timeout));
2272df08e78SAddy Ke 
2282df08e78SAddy Ke 	dev_warn(rs->dev, "spi controller is in busy state!\n");
2292df08e78SAddy Ke }
2302df08e78SAddy Ke 
23164e36824Saddy ke static u32 get_fifo_len(struct rockchip_spi *rs)
23264e36824Saddy ke {
23364e36824Saddy ke 	u32 fifo;
23464e36824Saddy ke 
23564e36824Saddy ke 	for (fifo = 2; fifo < 32; fifo++) {
23664e36824Saddy ke 		writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR);
23764e36824Saddy ke 		if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR))
23864e36824Saddy ke 			break;
23964e36824Saddy ke 	}
24064e36824Saddy ke 
24164e36824Saddy ke 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR);
24264e36824Saddy ke 
24364e36824Saddy ke 	return (fifo == 31) ? 0 : fifo;
24464e36824Saddy ke }
24564e36824Saddy ke 
24664e36824Saddy ke static inline u32 tx_max(struct rockchip_spi *rs)
24764e36824Saddy ke {
24864e36824Saddy ke 	u32 tx_left, tx_room;
24964e36824Saddy ke 
25064e36824Saddy ke 	tx_left = (rs->tx_end - rs->tx) / rs->n_bytes;
25164e36824Saddy ke 	tx_room = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
25264e36824Saddy ke 
25364e36824Saddy ke 	return min(tx_left, tx_room);
25464e36824Saddy ke }
25564e36824Saddy ke 
25664e36824Saddy ke static inline u32 rx_max(struct rockchip_spi *rs)
25764e36824Saddy ke {
25864e36824Saddy ke 	u32 rx_left = (rs->rx_end - rs->rx) / rs->n_bytes;
25964e36824Saddy ke 	u32 rx_room = (u32)readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
26064e36824Saddy ke 
26164e36824Saddy ke 	return min(rx_left, rx_room);
26264e36824Saddy ke }
26364e36824Saddy ke 
26464e36824Saddy ke static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
26564e36824Saddy ke {
26664e36824Saddy ke 	u32 ser;
26764e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(spi->master);
26864e36824Saddy ke 
26964e36824Saddy ke 	ser = readl_relaxed(rs->regs + ROCKCHIP_SPI_SER) & SER_MASK;
27064e36824Saddy ke 
27164e36824Saddy ke 	/*
27264e36824Saddy ke 	 * drivers/spi/spi.c:
27364e36824Saddy ke 	 * static void spi_set_cs(struct spi_device *spi, bool enable)
27464e36824Saddy ke 	 * {
27564e36824Saddy ke 	 *		if (spi->mode & SPI_CS_HIGH)
27664e36824Saddy ke 	 *			enable = !enable;
27764e36824Saddy ke 	 *
27864e36824Saddy ke 	 *		if (spi->cs_gpio >= 0)
27964e36824Saddy ke 	 *			gpio_set_value(spi->cs_gpio, !enable);
28064e36824Saddy ke 	 *		else if (spi->master->set_cs)
28164e36824Saddy ke 	 *		spi->master->set_cs(spi, !enable);
28264e36824Saddy ke 	 * }
28364e36824Saddy ke 	 *
28464e36824Saddy ke 	 * Note: enable(rockchip_spi_set_cs) = !enable(spi_set_cs)
28564e36824Saddy ke 	 */
28664e36824Saddy ke 	if (!enable)
28764e36824Saddy ke 		ser |= 1 << spi->chip_select;
28864e36824Saddy ke 	else
28964e36824Saddy ke 		ser &= ~(1 << spi->chip_select);
29064e36824Saddy ke 
29164e36824Saddy ke 	writel_relaxed(ser, rs->regs + ROCKCHIP_SPI_SER);
29264e36824Saddy ke }
29364e36824Saddy ke 
29464e36824Saddy ke static int rockchip_spi_prepare_message(struct spi_master *master,
29564e36824Saddy ke 					struct spi_message *msg)
29664e36824Saddy ke {
29764e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
29864e36824Saddy ke 	struct spi_device *spi = msg->spi;
29964e36824Saddy ke 
30064e36824Saddy ke 	rs->mode = spi->mode;
30164e36824Saddy ke 
30264e36824Saddy ke 	return 0;
30364e36824Saddy ke }
30464e36824Saddy ke 
30564e36824Saddy ke static int rockchip_spi_unprepare_message(struct spi_master *master,
30664e36824Saddy ke 					  struct spi_message *msg)
30764e36824Saddy ke {
30864e36824Saddy ke 	unsigned long flags;
30964e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
31064e36824Saddy ke 
31164e36824Saddy ke 	spin_lock_irqsave(&rs->lock, flags);
31264e36824Saddy ke 
3135dcc44edSAddy Ke 	/*
3145dcc44edSAddy Ke 	 * For DMA mode, we need terminate DMA channel and flush
3155dcc44edSAddy Ke 	 * fifo for the next transfer if DMA thansfer timeout.
3165dcc44edSAddy Ke 	 * unprepare_message() was called by core if transfer complete
3175dcc44edSAddy Ke 	 * or timeout. Maybe it is reasonable for error handling here.
3185dcc44edSAddy Ke 	 */
31964e36824Saddy ke 	if (rs->use_dma) {
32064e36824Saddy ke 		if (rs->state & RXBUSY) {
32164e36824Saddy ke 			dmaengine_terminate_all(rs->dma_rx.ch);
32264e36824Saddy ke 			flush_fifo(rs);
32364e36824Saddy ke 		}
32464e36824Saddy ke 
32564e36824Saddy ke 		if (rs->state & TXBUSY)
32664e36824Saddy ke 			dmaengine_terminate_all(rs->dma_tx.ch);
32764e36824Saddy ke 	}
32864e36824Saddy ke 
32964e36824Saddy ke 	spin_unlock_irqrestore(&rs->lock, flags);
33064e36824Saddy ke 
331c28be31bSAddy Ke 	spi_enable_chip(rs, 0);
332c28be31bSAddy Ke 
33364e36824Saddy ke 	return 0;
33464e36824Saddy ke }
33564e36824Saddy ke 
33664e36824Saddy ke static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
33764e36824Saddy ke {
33864e36824Saddy ke 	u32 max = tx_max(rs);
33964e36824Saddy ke 	u32 txw = 0;
34064e36824Saddy ke 
34164e36824Saddy ke 	while (max--) {
34264e36824Saddy ke 		if (rs->n_bytes == 1)
34364e36824Saddy ke 			txw = *(u8 *)(rs->tx);
34464e36824Saddy ke 		else
34564e36824Saddy ke 			txw = *(u16 *)(rs->tx);
34664e36824Saddy ke 
34764e36824Saddy ke 		writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
34864e36824Saddy ke 		rs->tx += rs->n_bytes;
34964e36824Saddy ke 	}
35064e36824Saddy ke }
35164e36824Saddy ke 
35264e36824Saddy ke static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
35364e36824Saddy ke {
35464e36824Saddy ke 	u32 max = rx_max(rs);
35564e36824Saddy ke 	u32 rxw;
35664e36824Saddy ke 
35764e36824Saddy ke 	while (max--) {
35864e36824Saddy ke 		rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
35964e36824Saddy ke 		if (rs->n_bytes == 1)
36064e36824Saddy ke 			*(u8 *)(rs->rx) = (u8)rxw;
36164e36824Saddy ke 		else
36264e36824Saddy ke 			*(u16 *)(rs->rx) = (u16)rxw;
36364e36824Saddy ke 		rs->rx += rs->n_bytes;
3645dcc44edSAddy Ke 	}
36564e36824Saddy ke }
36664e36824Saddy ke 
36764e36824Saddy ke static int rockchip_spi_pio_transfer(struct rockchip_spi *rs)
36864e36824Saddy ke {
36964e36824Saddy ke 	int remain = 0;
37064e36824Saddy ke 
37164e36824Saddy ke 	do {
37264e36824Saddy ke 		if (rs->tx) {
37364e36824Saddy ke 			remain = rs->tx_end - rs->tx;
37464e36824Saddy ke 			rockchip_spi_pio_writer(rs);
37564e36824Saddy ke 		}
37664e36824Saddy ke 
37764e36824Saddy ke 		if (rs->rx) {
37864e36824Saddy ke 			remain = rs->rx_end - rs->rx;
37964e36824Saddy ke 			rockchip_spi_pio_reader(rs);
38064e36824Saddy ke 		}
38164e36824Saddy ke 
38264e36824Saddy ke 		cpu_relax();
38364e36824Saddy ke 	} while (remain);
38464e36824Saddy ke 
3852df08e78SAddy Ke 	/* If tx, wait until the FIFO data completely. */
3862df08e78SAddy Ke 	if (rs->tx)
3872df08e78SAddy Ke 		wait_for_idle(rs);
3882df08e78SAddy Ke 
389c28be31bSAddy Ke 	spi_enable_chip(rs, 0);
390c28be31bSAddy Ke 
39164e36824Saddy ke 	return 0;
39264e36824Saddy ke }
39364e36824Saddy ke 
39464e36824Saddy ke static void rockchip_spi_dma_rxcb(void *data)
39564e36824Saddy ke {
39664e36824Saddy ke 	unsigned long flags;
39764e36824Saddy ke 	struct rockchip_spi *rs = data;
39864e36824Saddy ke 
39964e36824Saddy ke 	spin_lock_irqsave(&rs->lock, flags);
40064e36824Saddy ke 
40164e36824Saddy ke 	rs->state &= ~RXBUSY;
402c28be31bSAddy Ke 	if (!(rs->state & TXBUSY)) {
403c28be31bSAddy Ke 		spi_enable_chip(rs, 0);
40464e36824Saddy ke 		spi_finalize_current_transfer(rs->master);
405c28be31bSAddy Ke 	}
40664e36824Saddy ke 
40764e36824Saddy ke 	spin_unlock_irqrestore(&rs->lock, flags);
40864e36824Saddy ke }
40964e36824Saddy ke 
41064e36824Saddy ke static void rockchip_spi_dma_txcb(void *data)
41164e36824Saddy ke {
41264e36824Saddy ke 	unsigned long flags;
41364e36824Saddy ke 	struct rockchip_spi *rs = data;
41464e36824Saddy ke 
4152df08e78SAddy Ke 	/* Wait until the FIFO data completely. */
4162df08e78SAddy Ke 	wait_for_idle(rs);
4172df08e78SAddy Ke 
41864e36824Saddy ke 	spin_lock_irqsave(&rs->lock, flags);
41964e36824Saddy ke 
42064e36824Saddy ke 	rs->state &= ~TXBUSY;
4212c2bc748SAddy Ke 	if (!(rs->state & RXBUSY)) {
4222c2bc748SAddy Ke 		spi_enable_chip(rs, 0);
42364e36824Saddy ke 		spi_finalize_current_transfer(rs->master);
4242c2bc748SAddy Ke 	}
42564e36824Saddy ke 
42664e36824Saddy ke 	spin_unlock_irqrestore(&rs->lock, flags);
42764e36824Saddy ke }
42864e36824Saddy ke 
429a24e70c0SAddy Ke static void rockchip_spi_prepare_dma(struct rockchip_spi *rs)
43064e36824Saddy ke {
43164e36824Saddy ke 	unsigned long flags;
43264e36824Saddy ke 	struct dma_slave_config rxconf, txconf;
43364e36824Saddy ke 	struct dma_async_tx_descriptor *rxdesc, *txdesc;
43464e36824Saddy ke 
43564e36824Saddy ke 	spin_lock_irqsave(&rs->lock, flags);
43664e36824Saddy ke 	rs->state &= ~RXBUSY;
43764e36824Saddy ke 	rs->state &= ~TXBUSY;
43864e36824Saddy ke 	spin_unlock_irqrestore(&rs->lock, flags);
43964e36824Saddy ke 
44097cf5669SArnd Bergmann 	rxdesc = NULL;
44164e36824Saddy ke 	if (rs->rx) {
44264e36824Saddy ke 		rxconf.direction = rs->dma_rx.direction;
44364e36824Saddy ke 		rxconf.src_addr = rs->dma_rx.addr;
44464e36824Saddy ke 		rxconf.src_addr_width = rs->n_bytes;
44564e36824Saddy ke 		rxconf.src_maxburst = rs->n_bytes;
44664e36824Saddy ke 		dmaengine_slave_config(rs->dma_rx.ch, &rxconf);
44764e36824Saddy ke 
4485dcc44edSAddy Ke 		rxdesc = dmaengine_prep_slave_sg(
4495dcc44edSAddy Ke 				rs->dma_rx.ch,
45064e36824Saddy ke 				rs->rx_sg.sgl, rs->rx_sg.nents,
45164e36824Saddy ke 				rs->dma_rx.direction, DMA_PREP_INTERRUPT);
45264e36824Saddy ke 
45364e36824Saddy ke 		rxdesc->callback = rockchip_spi_dma_rxcb;
45464e36824Saddy ke 		rxdesc->callback_param = rs;
45564e36824Saddy ke 	}
45664e36824Saddy ke 
45797cf5669SArnd Bergmann 	txdesc = NULL;
45864e36824Saddy ke 	if (rs->tx) {
45964e36824Saddy ke 		txconf.direction = rs->dma_tx.direction;
46064e36824Saddy ke 		txconf.dst_addr = rs->dma_tx.addr;
46164e36824Saddy ke 		txconf.dst_addr_width = rs->n_bytes;
46264e36824Saddy ke 		txconf.dst_maxburst = rs->n_bytes;
46364e36824Saddy ke 		dmaengine_slave_config(rs->dma_tx.ch, &txconf);
46464e36824Saddy ke 
4655dcc44edSAddy Ke 		txdesc = dmaengine_prep_slave_sg(
4665dcc44edSAddy Ke 				rs->dma_tx.ch,
46764e36824Saddy ke 				rs->tx_sg.sgl, rs->tx_sg.nents,
46864e36824Saddy ke 				rs->dma_tx.direction, DMA_PREP_INTERRUPT);
46964e36824Saddy ke 
47064e36824Saddy ke 		txdesc->callback = rockchip_spi_dma_txcb;
47164e36824Saddy ke 		txdesc->callback_param = rs;
47264e36824Saddy ke 	}
47364e36824Saddy ke 
47464e36824Saddy ke 	/* rx must be started before tx due to spi instinct */
47597cf5669SArnd Bergmann 	if (rxdesc) {
47664e36824Saddy ke 		spin_lock_irqsave(&rs->lock, flags);
47764e36824Saddy ke 		rs->state |= RXBUSY;
47864e36824Saddy ke 		spin_unlock_irqrestore(&rs->lock, flags);
47964e36824Saddy ke 		dmaengine_submit(rxdesc);
48064e36824Saddy ke 		dma_async_issue_pending(rs->dma_rx.ch);
48164e36824Saddy ke 	}
48264e36824Saddy ke 
48397cf5669SArnd Bergmann 	if (txdesc) {
48464e36824Saddy ke 		spin_lock_irqsave(&rs->lock, flags);
48564e36824Saddy ke 		rs->state |= TXBUSY;
48664e36824Saddy ke 		spin_unlock_irqrestore(&rs->lock, flags);
48764e36824Saddy ke 		dmaengine_submit(txdesc);
48864e36824Saddy ke 		dma_async_issue_pending(rs->dma_tx.ch);
48964e36824Saddy ke 	}
49064e36824Saddy ke }
49164e36824Saddy ke 
49264e36824Saddy ke static void rockchip_spi_config(struct rockchip_spi *rs)
49364e36824Saddy ke {
49464e36824Saddy ke 	u32 div = 0;
49564e36824Saddy ke 	u32 dmacr = 0;
49664e36824Saddy ke 
49764e36824Saddy ke 	u32 cr0 = (CR0_BHT_8BIT << CR0_BHT_OFFSET)
49864e36824Saddy ke 		| (CR0_SSD_ONE << CR0_SSD_OFFSET);
49964e36824Saddy ke 
50064e36824Saddy ke 	cr0 |= (rs->n_bytes << CR0_DFS_OFFSET);
50164e36824Saddy ke 	cr0 |= ((rs->mode & 0x3) << CR0_SCPH_OFFSET);
50264e36824Saddy ke 	cr0 |= (rs->tmode << CR0_XFM_OFFSET);
50364e36824Saddy ke 	cr0 |= (rs->type << CR0_FRF_OFFSET);
50464e36824Saddy ke 
50564e36824Saddy ke 	if (rs->use_dma) {
50664e36824Saddy ke 		if (rs->tx)
50764e36824Saddy ke 			dmacr |= TF_DMA_EN;
50864e36824Saddy ke 		if (rs->rx)
50964e36824Saddy ke 			dmacr |= RF_DMA_EN;
51064e36824Saddy ke 	}
51164e36824Saddy ke 
512f9cfd522SAddy Ke 	if (WARN_ON(rs->speed > MAX_SCLK_OUT))
513f9cfd522SAddy Ke 		rs->speed = MAX_SCLK_OUT;
514f9cfd522SAddy Ke 
515f9cfd522SAddy Ke 	/* the minimum divsor is 2 */
516f9cfd522SAddy Ke 	if (rs->max_freq < 2 * rs->speed) {
517f9cfd522SAddy Ke 		clk_set_rate(rs->spiclk, 2 * rs->speed);
518f9cfd522SAddy Ke 		rs->max_freq = clk_get_rate(rs->spiclk);
519f9cfd522SAddy Ke 	}
520f9cfd522SAddy Ke 
52164e36824Saddy ke 	/* div doesn't support odd number */
522*754ec43cSJulius Werner 	div = DIV_ROUND_UP(rs->max_freq, rs->speed);
52364e36824Saddy ke 	div = (div + 1) & 0xfffe;
52464e36824Saddy ke 
52564e36824Saddy ke 	writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
52664e36824Saddy ke 
52764e36824Saddy ke 	writel_relaxed(rs->len - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);
52864e36824Saddy ke 	writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_TXFTLR);
52964e36824Saddy ke 	writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
53064e36824Saddy ke 
53164e36824Saddy ke 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMATDLR);
53264e36824Saddy ke 	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR);
53364e36824Saddy ke 	writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
53464e36824Saddy ke 
53564e36824Saddy ke 	spi_set_clk(rs, div);
53664e36824Saddy ke 
5375dcc44edSAddy Ke 	dev_dbg(rs->dev, "cr0 0x%x, div %d\n", cr0, div);
53864e36824Saddy ke }
53964e36824Saddy ke 
5405dcc44edSAddy Ke static int rockchip_spi_transfer_one(
5415dcc44edSAddy Ke 		struct spi_master *master,
54264e36824Saddy ke 		struct spi_device *spi,
54364e36824Saddy ke 		struct spi_transfer *xfer)
54464e36824Saddy ke {
545c28be31bSAddy Ke 	int ret = 1;
54664e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
54764e36824Saddy ke 
54862946172SDoug Anderson 	WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
54962946172SDoug Anderson 		(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
55064e36824Saddy ke 
55164e36824Saddy ke 	if (!xfer->tx_buf && !xfer->rx_buf) {
55264e36824Saddy ke 		dev_err(rs->dev, "No buffer for transfer\n");
55364e36824Saddy ke 		return -EINVAL;
55464e36824Saddy ke 	}
55564e36824Saddy ke 
55664e36824Saddy ke 	rs->speed = xfer->speed_hz;
55764e36824Saddy ke 	rs->bpw = xfer->bits_per_word;
55864e36824Saddy ke 	rs->n_bytes = rs->bpw >> 3;
55964e36824Saddy ke 
56064e36824Saddy ke 	rs->tx = xfer->tx_buf;
56164e36824Saddy ke 	rs->tx_end = rs->tx + xfer->len;
56264e36824Saddy ke 	rs->rx = xfer->rx_buf;
56364e36824Saddy ke 	rs->rx_end = rs->rx + xfer->len;
56464e36824Saddy ke 	rs->len = xfer->len;
56564e36824Saddy ke 
56664e36824Saddy ke 	rs->tx_sg = xfer->tx_sg;
56764e36824Saddy ke 	rs->rx_sg = xfer->rx_sg;
56864e36824Saddy ke 
56964e36824Saddy ke 	if (rs->tx && rs->rx)
57064e36824Saddy ke 		rs->tmode = CR0_XFM_TR;
57164e36824Saddy ke 	else if (rs->tx)
57264e36824Saddy ke 		rs->tmode = CR0_XFM_TO;
57364e36824Saddy ke 	else if (rs->rx)
57464e36824Saddy ke 		rs->tmode = CR0_XFM_RO;
57564e36824Saddy ke 
576a24e70c0SAddy Ke 	/* we need prepare dma before spi was enabled */
577c28be31bSAddy Ke 	if (master->can_dma && master->can_dma(master, spi, xfer))
57864e36824Saddy ke 		rs->use_dma = 1;
579c28be31bSAddy Ke 	else
58064e36824Saddy ke 		rs->use_dma = 0;
58164e36824Saddy ke 
58264e36824Saddy ke 	rockchip_spi_config(rs);
58364e36824Saddy ke 
584c28be31bSAddy Ke 	if (rs->use_dma) {
585c28be31bSAddy Ke 		if (rs->tmode == CR0_XFM_RO) {
586c28be31bSAddy Ke 			/* rx: dma must be prepared first */
587c28be31bSAddy Ke 			rockchip_spi_prepare_dma(rs);
588c28be31bSAddy Ke 			spi_enable_chip(rs, 1);
589c28be31bSAddy Ke 		} else {
590c28be31bSAddy Ke 			/* tx or tr: spi must be enabled first */
591c28be31bSAddy Ke 			spi_enable_chip(rs, 1);
592c28be31bSAddy Ke 			rockchip_spi_prepare_dma(rs);
593c28be31bSAddy Ke 		}
594c28be31bSAddy Ke 	} else {
595c28be31bSAddy Ke 		spi_enable_chip(rs, 1);
59664e36824Saddy ke 		ret = rockchip_spi_pio_transfer(rs);
597c28be31bSAddy Ke 	}
59864e36824Saddy ke 
59964e36824Saddy ke 	return ret;
60064e36824Saddy ke }
60164e36824Saddy ke 
60264e36824Saddy ke static bool rockchip_spi_can_dma(struct spi_master *master,
60364e36824Saddy ke 				 struct spi_device *spi,
60464e36824Saddy ke 				 struct spi_transfer *xfer)
60564e36824Saddy ke {
60664e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
60764e36824Saddy ke 
60864e36824Saddy ke 	return (xfer->len > rs->fifo_len);
60964e36824Saddy ke }
61064e36824Saddy ke 
61164e36824Saddy ke static int rockchip_spi_probe(struct platform_device *pdev)
61264e36824Saddy ke {
61364e36824Saddy ke 	int ret = 0;
61464e36824Saddy ke 	struct rockchip_spi *rs;
61564e36824Saddy ke 	struct spi_master *master;
61664e36824Saddy ke 	struct resource *mem;
61764e36824Saddy ke 
61864e36824Saddy ke 	master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi));
6195dcc44edSAddy Ke 	if (!master)
62064e36824Saddy ke 		return -ENOMEM;
6215dcc44edSAddy Ke 
62264e36824Saddy ke 	platform_set_drvdata(pdev, master);
62364e36824Saddy ke 
62464e36824Saddy ke 	rs = spi_master_get_devdata(master);
62564e36824Saddy ke 	memset(rs, 0, sizeof(struct rockchip_spi));
62664e36824Saddy ke 
62764e36824Saddy ke 	/* Get basic io resource and map it */
62864e36824Saddy ke 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
62964e36824Saddy ke 	rs->regs = devm_ioremap_resource(&pdev->dev, mem);
63064e36824Saddy ke 	if (IS_ERR(rs->regs)) {
63164e36824Saddy ke 		ret =  PTR_ERR(rs->regs);
63264e36824Saddy ke 		goto err_ioremap_resource;
63364e36824Saddy ke 	}
63464e36824Saddy ke 
63564e36824Saddy ke 	rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
63664e36824Saddy ke 	if (IS_ERR(rs->apb_pclk)) {
63764e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get apb_pclk\n");
63864e36824Saddy ke 		ret = PTR_ERR(rs->apb_pclk);
63964e36824Saddy ke 		goto err_ioremap_resource;
64064e36824Saddy ke 	}
64164e36824Saddy ke 
64264e36824Saddy ke 	rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
64364e36824Saddy ke 	if (IS_ERR(rs->spiclk)) {
64464e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get spi_pclk\n");
64564e36824Saddy ke 		ret = PTR_ERR(rs->spiclk);
64664e36824Saddy ke 		goto err_ioremap_resource;
64764e36824Saddy ke 	}
64864e36824Saddy ke 
64964e36824Saddy ke 	ret = clk_prepare_enable(rs->apb_pclk);
65064e36824Saddy ke 	if (ret) {
65164e36824Saddy ke 		dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
65264e36824Saddy ke 		goto err_ioremap_resource;
65364e36824Saddy ke 	}
65464e36824Saddy ke 
65564e36824Saddy ke 	ret = clk_prepare_enable(rs->spiclk);
65664e36824Saddy ke 	if (ret) {
65764e36824Saddy ke 		dev_err(&pdev->dev, "Failed to enable spi_clk\n");
65864e36824Saddy ke 		goto err_spiclk_enable;
65964e36824Saddy ke 	}
66064e36824Saddy ke 
66164e36824Saddy ke 	spi_enable_chip(rs, 0);
66264e36824Saddy ke 
66364e36824Saddy ke 	rs->type = SSI_MOTO_SPI;
66464e36824Saddy ke 	rs->master = master;
66564e36824Saddy ke 	rs->dev = &pdev->dev;
66664e36824Saddy ke 	rs->max_freq = clk_get_rate(rs->spiclk);
66764e36824Saddy ke 
66864e36824Saddy ke 	rs->fifo_len = get_fifo_len(rs);
66964e36824Saddy ke 	if (!rs->fifo_len) {
67064e36824Saddy ke 		dev_err(&pdev->dev, "Failed to get fifo length\n");
671db7e8d90SWei Yongjun 		ret = -EINVAL;
67264e36824Saddy ke 		goto err_get_fifo_len;
67364e36824Saddy ke 	}
67464e36824Saddy ke 
67564e36824Saddy ke 	spin_lock_init(&rs->lock);
67664e36824Saddy ke 
67764e36824Saddy ke 	pm_runtime_set_active(&pdev->dev);
67864e36824Saddy ke 	pm_runtime_enable(&pdev->dev);
67964e36824Saddy ke 
68064e36824Saddy ke 	master->auto_runtime_pm = true;
68164e36824Saddy ke 	master->bus_num = pdev->id;
682ee780997SAddy Ke 	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
68364e36824Saddy ke 	master->num_chipselect = 2;
68464e36824Saddy ke 	master->dev.of_node = pdev->dev.of_node;
68564e36824Saddy ke 	master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8);
68664e36824Saddy ke 
68764e36824Saddy ke 	master->set_cs = rockchip_spi_set_cs;
68864e36824Saddy ke 	master->prepare_message = rockchip_spi_prepare_message;
68964e36824Saddy ke 	master->unprepare_message = rockchip_spi_unprepare_message;
69064e36824Saddy ke 	master->transfer_one = rockchip_spi_transfer_one;
69164e36824Saddy ke 
69264e36824Saddy ke 	rs->dma_tx.ch = dma_request_slave_channel(rs->dev, "tx");
69364e36824Saddy ke 	if (!rs->dma_tx.ch)
69464e36824Saddy ke 		dev_warn(rs->dev, "Failed to request TX DMA channel\n");
69564e36824Saddy ke 
69664e36824Saddy ke 	rs->dma_rx.ch = dma_request_slave_channel(rs->dev, "rx");
69764e36824Saddy ke 	if (!rs->dma_rx.ch) {
69864e36824Saddy ke 		if (rs->dma_tx.ch) {
69964e36824Saddy ke 			dma_release_channel(rs->dma_tx.ch);
70064e36824Saddy ke 			rs->dma_tx.ch = NULL;
70164e36824Saddy ke 		}
70264e36824Saddy ke 		dev_warn(rs->dev, "Failed to request RX DMA channel\n");
70364e36824Saddy ke 	}
70464e36824Saddy ke 
70564e36824Saddy ke 	if (rs->dma_tx.ch && rs->dma_rx.ch) {
70664e36824Saddy ke 		rs->dma_tx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_TXDR);
70764e36824Saddy ke 		rs->dma_rx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_RXDR);
70864e36824Saddy ke 		rs->dma_tx.direction = DMA_MEM_TO_DEV;
7090ac7a490SAddy Ke 		rs->dma_rx.direction = DMA_DEV_TO_MEM;
71064e36824Saddy ke 
71164e36824Saddy ke 		master->can_dma = rockchip_spi_can_dma;
71264e36824Saddy ke 		master->dma_tx = rs->dma_tx.ch;
71364e36824Saddy ke 		master->dma_rx = rs->dma_rx.ch;
71464e36824Saddy ke 	}
71564e36824Saddy ke 
71664e36824Saddy ke 	ret = devm_spi_register_master(&pdev->dev, master);
71764e36824Saddy ke 	if (ret) {
71864e36824Saddy ke 		dev_err(&pdev->dev, "Failed to register master\n");
71964e36824Saddy ke 		goto err_register_master;
72064e36824Saddy ke 	}
72164e36824Saddy ke 
72264e36824Saddy ke 	return 0;
72364e36824Saddy ke 
72464e36824Saddy ke err_register_master:
72564e36824Saddy ke 	if (rs->dma_tx.ch)
72664e36824Saddy ke 		dma_release_channel(rs->dma_tx.ch);
72764e36824Saddy ke 	if (rs->dma_rx.ch)
72864e36824Saddy ke 		dma_release_channel(rs->dma_rx.ch);
72964e36824Saddy ke err_get_fifo_len:
73064e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
73164e36824Saddy ke err_spiclk_enable:
73264e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
73364e36824Saddy ke err_ioremap_resource:
73464e36824Saddy ke 	spi_master_put(master);
73564e36824Saddy ke 
73664e36824Saddy ke 	return ret;
73764e36824Saddy ke }
73864e36824Saddy ke 
73964e36824Saddy ke static int rockchip_spi_remove(struct platform_device *pdev)
74064e36824Saddy ke {
74164e36824Saddy ke 	struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
74264e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
74364e36824Saddy ke 
74464e36824Saddy ke 	pm_runtime_disable(&pdev->dev);
74564e36824Saddy ke 
74664e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
74764e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
74864e36824Saddy ke 
74964e36824Saddy ke 	if (rs->dma_tx.ch)
75064e36824Saddy ke 		dma_release_channel(rs->dma_tx.ch);
75164e36824Saddy ke 	if (rs->dma_rx.ch)
75264e36824Saddy ke 		dma_release_channel(rs->dma_rx.ch);
75364e36824Saddy ke 
75464e36824Saddy ke 	return 0;
75564e36824Saddy ke }
75664e36824Saddy ke 
75764e36824Saddy ke #ifdef CONFIG_PM_SLEEP
75864e36824Saddy ke static int rockchip_spi_suspend(struct device *dev)
75964e36824Saddy ke {
76064e36824Saddy ke 	int ret = 0;
76164e36824Saddy ke 	struct spi_master *master = dev_get_drvdata(dev);
76264e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
76364e36824Saddy ke 
76464e36824Saddy ke 	ret = spi_master_suspend(rs->master);
76564e36824Saddy ke 	if (ret)
76664e36824Saddy ke 		return ret;
76764e36824Saddy ke 
76864e36824Saddy ke 	if (!pm_runtime_suspended(dev)) {
76964e36824Saddy ke 		clk_disable_unprepare(rs->spiclk);
77064e36824Saddy ke 		clk_disable_unprepare(rs->apb_pclk);
77164e36824Saddy ke 	}
77264e36824Saddy ke 
77364e36824Saddy ke 	return ret;
77464e36824Saddy ke }
77564e36824Saddy ke 
77664e36824Saddy ke static int rockchip_spi_resume(struct device *dev)
77764e36824Saddy ke {
77864e36824Saddy ke 	int ret = 0;
77964e36824Saddy ke 	struct spi_master *master = dev_get_drvdata(dev);
78064e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
78164e36824Saddy ke 
78264e36824Saddy ke 	if (!pm_runtime_suspended(dev)) {
78364e36824Saddy ke 		ret = clk_prepare_enable(rs->apb_pclk);
78464e36824Saddy ke 		if (ret < 0)
78564e36824Saddy ke 			return ret;
78664e36824Saddy ke 
78764e36824Saddy ke 		ret = clk_prepare_enable(rs->spiclk);
78864e36824Saddy ke 		if (ret < 0) {
78964e36824Saddy ke 			clk_disable_unprepare(rs->apb_pclk);
79064e36824Saddy ke 			return ret;
79164e36824Saddy ke 		}
79264e36824Saddy ke 	}
79364e36824Saddy ke 
79464e36824Saddy ke 	ret = spi_master_resume(rs->master);
79564e36824Saddy ke 	if (ret < 0) {
79664e36824Saddy ke 		clk_disable_unprepare(rs->spiclk);
79764e36824Saddy ke 		clk_disable_unprepare(rs->apb_pclk);
79864e36824Saddy ke 	}
79964e36824Saddy ke 
80064e36824Saddy ke 	return ret;
80164e36824Saddy ke }
80264e36824Saddy ke #endif /* CONFIG_PM_SLEEP */
80364e36824Saddy ke 
804ec833050SRafael J. Wysocki #ifdef CONFIG_PM
80564e36824Saddy ke static int rockchip_spi_runtime_suspend(struct device *dev)
80664e36824Saddy ke {
80764e36824Saddy ke 	struct spi_master *master = dev_get_drvdata(dev);
80864e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
80964e36824Saddy ke 
81064e36824Saddy ke 	clk_disable_unprepare(rs->spiclk);
81164e36824Saddy ke 	clk_disable_unprepare(rs->apb_pclk);
81264e36824Saddy ke 
81364e36824Saddy ke 	return 0;
81464e36824Saddy ke }
81564e36824Saddy ke 
81664e36824Saddy ke static int rockchip_spi_runtime_resume(struct device *dev)
81764e36824Saddy ke {
81864e36824Saddy ke 	int ret;
81964e36824Saddy ke 	struct spi_master *master = dev_get_drvdata(dev);
82064e36824Saddy ke 	struct rockchip_spi *rs = spi_master_get_devdata(master);
82164e36824Saddy ke 
82264e36824Saddy ke 	ret = clk_prepare_enable(rs->apb_pclk);
82364e36824Saddy ke 	if (ret)
82464e36824Saddy ke 		return ret;
82564e36824Saddy ke 
82664e36824Saddy ke 	ret = clk_prepare_enable(rs->spiclk);
82764e36824Saddy ke 	if (ret)
82864e36824Saddy ke 		clk_disable_unprepare(rs->apb_pclk);
82964e36824Saddy ke 
83064e36824Saddy ke 	return ret;
83164e36824Saddy ke }
832ec833050SRafael J. Wysocki #endif /* CONFIG_PM */
83364e36824Saddy ke 
83464e36824Saddy ke static const struct dev_pm_ops rockchip_spi_pm = {
83564e36824Saddy ke 	SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
83664e36824Saddy ke 	SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
83764e36824Saddy ke 			   rockchip_spi_runtime_resume, NULL)
83864e36824Saddy ke };
83964e36824Saddy ke 
84064e36824Saddy ke static const struct of_device_id rockchip_spi_dt_match[] = {
84164e36824Saddy ke 	{ .compatible = "rockchip,rk3066-spi", },
842b839b785SAddy Ke 	{ .compatible = "rockchip,rk3188-spi", },
843b839b785SAddy Ke 	{ .compatible = "rockchip,rk3288-spi", },
84464e36824Saddy ke 	{ },
84564e36824Saddy ke };
84664e36824Saddy ke MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
84764e36824Saddy ke 
84864e36824Saddy ke static struct platform_driver rockchip_spi_driver = {
84964e36824Saddy ke 	.driver = {
85064e36824Saddy ke 		.name	= DRIVER_NAME,
85164e36824Saddy ke 		.pm = &rockchip_spi_pm,
85264e36824Saddy ke 		.of_match_table = of_match_ptr(rockchip_spi_dt_match),
85364e36824Saddy ke 	},
85464e36824Saddy ke 	.probe = rockchip_spi_probe,
85564e36824Saddy ke 	.remove = rockchip_spi_remove,
85664e36824Saddy ke };
85764e36824Saddy ke 
85864e36824Saddy ke module_platform_driver(rockchip_spi_driver);
85964e36824Saddy ke 
8605dcc44edSAddy Ke MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
86164e36824Saddy ke MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
86264e36824Saddy ke MODULE_LICENSE("GPL v2");
863