xref: /openbmc/linux/drivers/spi/spi-sunplus-sp7021.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1f62ca4e2SLi-hao Kuo // SPDX-License-Identifier: GPL-2.0-only
2f62ca4e2SLi-hao Kuo // Copyright (c) 2021 Sunplus Inc.
3f62ca4e2SLi-hao Kuo // Author: Li-hao Kuo <lhjeff911@gmail.com>
4f62ca4e2SLi-hao Kuo 
5f62ca4e2SLi-hao Kuo #include <linux/bitfield.h>
6f62ca4e2SLi-hao Kuo #include <linux/clk.h>
7f62ca4e2SLi-hao Kuo #include <linux/delay.h>
8f62ca4e2SLi-hao Kuo #include <linux/dma-mapping.h>
9f62ca4e2SLi-hao Kuo #include <linux/interrupt.h>
10f62ca4e2SLi-hao Kuo #include <linux/module.h>
11f62ca4e2SLi-hao Kuo #include <linux/of.h>
12f62ca4e2SLi-hao Kuo #include <linux/platform_device.h>
13f62ca4e2SLi-hao Kuo #include <linux/pm_runtime.h>
14f62ca4e2SLi-hao Kuo #include <linux/reset.h>
15f62ca4e2SLi-hao Kuo #include <linux/spi/spi.h>
16f62ca4e2SLi-hao Kuo 
17f62ca4e2SLi-hao Kuo #define SP7021_DATA_RDY_REG		0x0044
18f62ca4e2SLi-hao Kuo #define SP7021_SLAVE_DMA_CTRL_REG	0x0048
19f62ca4e2SLi-hao Kuo #define SP7021_SLAVE_DMA_LENGTH_REG	0x004c
20f62ca4e2SLi-hao Kuo #define SP7021_SLAVE_DMA_ADDR_REG	0x004c
21f62ca4e2SLi-hao Kuo 
22f62ca4e2SLi-hao Kuo #define SP7021_SLAVE_DATA_RDY		BIT(0)
23f62ca4e2SLi-hao Kuo #define SP7021_SLAVE_SW_RST		BIT(1)
24f62ca4e2SLi-hao Kuo #define SP7021_SLA_DMA_W_INT		BIT(8)
25f62ca4e2SLi-hao Kuo #define SP7021_SLAVE_CLR_INT		BIT(8)
26f62ca4e2SLi-hao Kuo #define SP7021_SLAVE_DMA_EN		BIT(0)
27f62ca4e2SLi-hao Kuo #define SP7021_SLAVE_DMA_RW		BIT(6)
28f62ca4e2SLi-hao Kuo #define SP7021_SLAVE_DMA_CMD		GENMASK(3, 2)
29f62ca4e2SLi-hao Kuo 
30f62ca4e2SLi-hao Kuo #define SP7021_FIFO_REG			0x0034
31f62ca4e2SLi-hao Kuo #define SP7021_SPI_STATUS_REG		0x0038
32f62ca4e2SLi-hao Kuo #define SP7021_SPI_CONFIG_REG		0x003c
33f62ca4e2SLi-hao Kuo #define SP7021_INT_BUSY_REG		0x004c
34f62ca4e2SLi-hao Kuo #define SP7021_DMA_CTRL_REG		0x0050
35f62ca4e2SLi-hao Kuo 
36f62ca4e2SLi-hao Kuo #define SP7021_SPI_START_FD		BIT(0)
37f62ca4e2SLi-hao Kuo #define SP7021_FD_SW_RST		BIT(1)
38f62ca4e2SLi-hao Kuo #define SP7021_TX_EMP_FLAG		BIT(2)
39f62ca4e2SLi-hao Kuo #define SP7021_RX_EMP_FLAG		BIT(4)
40f62ca4e2SLi-hao Kuo #define SP7021_RX_FULL_FLAG		BIT(5)
41f62ca4e2SLi-hao Kuo #define SP7021_FINISH_FLAG		BIT(6)
42f62ca4e2SLi-hao Kuo 
43f62ca4e2SLi-hao Kuo #define SP7021_TX_CNT_MASK		GENMASK(11, 8)
44f62ca4e2SLi-hao Kuo #define SP7021_RX_CNT_MASK		GENMASK(15, 12)
45f62ca4e2SLi-hao Kuo #define SP7021_TX_LEN_MASK		GENMASK(23, 16)
46f62ca4e2SLi-hao Kuo #define SP7021_GET_LEN_MASK		GENMASK(31, 24)
47f62ca4e2SLi-hao Kuo #define SP7021_SET_TX_LEN		GENMASK(23, 16)
48f62ca4e2SLi-hao Kuo #define SP7021_SET_XFER_LEN		GENMASK(31, 24)
49f62ca4e2SLi-hao Kuo 
50f62ca4e2SLi-hao Kuo #define SP7021_CPOL_FD			BIT(0)
51f62ca4e2SLi-hao Kuo #define SP7021_CPHA_R			BIT(1)
52f62ca4e2SLi-hao Kuo #define SP7021_CPHA_W			BIT(2)
53f62ca4e2SLi-hao Kuo #define SP7021_LSB_SEL			BIT(4)
54f62ca4e2SLi-hao Kuo #define SP7021_CS_POR			BIT(5)
55f62ca4e2SLi-hao Kuo #define SP7021_FD_SEL			BIT(6)
56f62ca4e2SLi-hao Kuo 
57f62ca4e2SLi-hao Kuo #define SP7021_RX_UNIT			GENMASK(8, 7)
58f62ca4e2SLi-hao Kuo #define SP7021_TX_UNIT			GENMASK(10, 9)
59f62ca4e2SLi-hao Kuo #define SP7021_TX_EMP_FLAG_MASK		BIT(11)
60f62ca4e2SLi-hao Kuo #define SP7021_RX_FULL_FLAG_MASK	BIT(14)
61f62ca4e2SLi-hao Kuo #define SP7021_FINISH_FLAG_MASK		BIT(15)
62f62ca4e2SLi-hao Kuo #define SP7021_CLEAN_RW_BYTE		GENMASK(10, 7)
63f62ca4e2SLi-hao Kuo #define SP7021_CLEAN_FLUG_MASK		GENMASK(15, 11)
64f62ca4e2SLi-hao Kuo #define SP7021_CLK_MASK			GENMASK(31, 16)
65f62ca4e2SLi-hao Kuo 
66f62ca4e2SLi-hao Kuo #define SP7021_INT_BYPASS		BIT(3)
67f62ca4e2SLi-hao Kuo #define SP7021_CLR_MASTER_INT		BIT(6)
68f62ca4e2SLi-hao Kuo 
69f62ca4e2SLi-hao Kuo #define SP7021_SPI_DATA_SIZE		(255)
70f62ca4e2SLi-hao Kuo #define SP7021_FIFO_DATA_LEN		(16)
71f62ca4e2SLi-hao Kuo 
72f62ca4e2SLi-hao Kuo enum {
73f62ca4e2SLi-hao Kuo 	SP7021_MASTER_MODE = 0,
74f62ca4e2SLi-hao Kuo 	SP7021_SLAVE_MODE = 1,
75f62ca4e2SLi-hao Kuo };
76f62ca4e2SLi-hao Kuo 
77f62ca4e2SLi-hao Kuo struct sp7021_spi_ctlr {
78f62ca4e2SLi-hao Kuo 	struct device *dev;
79f62ca4e2SLi-hao Kuo 	struct spi_controller *ctlr;
80f62ca4e2SLi-hao Kuo 	void __iomem *m_base;
81f62ca4e2SLi-hao Kuo 	void __iomem *s_base;
82f62ca4e2SLi-hao Kuo 	u32 xfer_conf;
83f62ca4e2SLi-hao Kuo 	int mode;
84f62ca4e2SLi-hao Kuo 	int m_irq;
85f62ca4e2SLi-hao Kuo 	int s_irq;
86f62ca4e2SLi-hao Kuo 	struct clk *spi_clk;
87f62ca4e2SLi-hao Kuo 	struct reset_control *rstc;
88f62ca4e2SLi-hao Kuo 	// data xfer lock
89f62ca4e2SLi-hao Kuo 	struct mutex buf_lock;
90f62ca4e2SLi-hao Kuo 	struct completion isr_done;
91f62ca4e2SLi-hao Kuo 	struct completion slave_isr;
92f62ca4e2SLi-hao Kuo 	unsigned int  rx_cur_len;
93f62ca4e2SLi-hao Kuo 	unsigned int  tx_cur_len;
94f62ca4e2SLi-hao Kuo 	unsigned int  data_unit;
95f62ca4e2SLi-hao Kuo 	const u8 *tx_buf;
96f62ca4e2SLi-hao Kuo 	u8 *rx_buf;
97f62ca4e2SLi-hao Kuo };
98f62ca4e2SLi-hao Kuo 
sp7021_spi_slave_irq(int irq,void * dev)99f62ca4e2SLi-hao Kuo static irqreturn_t sp7021_spi_slave_irq(int irq, void *dev)
100f62ca4e2SLi-hao Kuo {
101f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = dev;
102f62ca4e2SLi-hao Kuo 	unsigned int data_status;
103f62ca4e2SLi-hao Kuo 
104f62ca4e2SLi-hao Kuo 	data_status = readl(pspim->s_base + SP7021_DATA_RDY_REG);
10547e8fe57SLi-hao Kuo 	data_status |= SP7021_SLAVE_CLR_INT;
10647e8fe57SLi-hao Kuo 	writel(data_status , pspim->s_base + SP7021_DATA_RDY_REG);
107f62ca4e2SLi-hao Kuo 	complete(&pspim->slave_isr);
108f62ca4e2SLi-hao Kuo 	return IRQ_HANDLED;
109f62ca4e2SLi-hao Kuo }
110f62ca4e2SLi-hao Kuo 
sp7021_spi_slave_abort(struct spi_controller * ctlr)111f62ca4e2SLi-hao Kuo static int sp7021_spi_slave_abort(struct spi_controller *ctlr)
112f62ca4e2SLi-hao Kuo {
113f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
114f62ca4e2SLi-hao Kuo 
115f62ca4e2SLi-hao Kuo 	complete(&pspim->slave_isr);
116f62ca4e2SLi-hao Kuo 	complete(&pspim->isr_done);
117f62ca4e2SLi-hao Kuo 	return 0;
118f62ca4e2SLi-hao Kuo }
119f62ca4e2SLi-hao Kuo 
sp7021_spi_slave_tx(struct spi_device * spi,struct spi_transfer * xfer)1206938e02fS郭力豪 static int sp7021_spi_slave_tx(struct spi_device *spi, struct spi_transfer *xfer)
121f62ca4e2SLi-hao Kuo {
122f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = spi_controller_get_devdata(spi->controller);
12347e8fe57SLi-hao Kuo 	u32 value;
124f62ca4e2SLi-hao Kuo 
125f62ca4e2SLi-hao Kuo 	reinit_completion(&pspim->slave_isr);
12647e8fe57SLi-hao Kuo 	value = SP7021_SLAVE_DMA_EN | SP7021_SLAVE_DMA_RW | FIELD_PREP(SP7021_SLAVE_DMA_CMD, 3);
12747e8fe57SLi-hao Kuo 	writel(value, pspim->s_base + SP7021_SLAVE_DMA_CTRL_REG);
128f62ca4e2SLi-hao Kuo 	writel(xfer->len, pspim->s_base + SP7021_SLAVE_DMA_LENGTH_REG);
129f62ca4e2SLi-hao Kuo 	writel(xfer->tx_dma, pspim->s_base + SP7021_SLAVE_DMA_ADDR_REG);
13047e8fe57SLi-hao Kuo 	value = readl(pspim->s_base + SP7021_DATA_RDY_REG);
13147e8fe57SLi-hao Kuo 	value |= SP7021_SLAVE_DATA_RDY;
13247e8fe57SLi-hao Kuo 	writel(value, pspim->s_base + SP7021_DATA_RDY_REG);
133f62ca4e2SLi-hao Kuo 	if (wait_for_completion_interruptible(&pspim->isr_done)) {
134f62ca4e2SLi-hao Kuo 		dev_err(&spi->dev, "%s() wait_for_completion err\n", __func__);
135f62ca4e2SLi-hao Kuo 		return -EINTR;
136f62ca4e2SLi-hao Kuo 	}
137f62ca4e2SLi-hao Kuo 	return 0;
138f62ca4e2SLi-hao Kuo }
139f62ca4e2SLi-hao Kuo 
sp7021_spi_slave_rx(struct spi_device * spi,struct spi_transfer * xfer)1406938e02fS郭力豪 static int sp7021_spi_slave_rx(struct spi_device *spi, struct spi_transfer *xfer)
141f62ca4e2SLi-hao Kuo {
142f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = spi_controller_get_devdata(spi->controller);
14347e8fe57SLi-hao Kuo 	u32 value;
144f62ca4e2SLi-hao Kuo 
145f62ca4e2SLi-hao Kuo 	reinit_completion(&pspim->isr_done);
14647e8fe57SLi-hao Kuo 	value = SP7021_SLAVE_DMA_EN | FIELD_PREP(SP7021_SLAVE_DMA_CMD, 3);
14747e8fe57SLi-hao Kuo 	writel(value, pspim->s_base + SP7021_SLAVE_DMA_CTRL_REG);
148f62ca4e2SLi-hao Kuo 	writel(xfer->len, pspim->s_base + SP7021_SLAVE_DMA_LENGTH_REG);
149f62ca4e2SLi-hao Kuo 	writel(xfer->rx_dma, pspim->s_base + SP7021_SLAVE_DMA_ADDR_REG);
150f62ca4e2SLi-hao Kuo 	if (wait_for_completion_interruptible(&pspim->isr_done)) {
151f62ca4e2SLi-hao Kuo 		dev_err(&spi->dev, "%s() wait_for_completion err\n", __func__);
152f62ca4e2SLi-hao Kuo 		return -EINTR;
153f62ca4e2SLi-hao Kuo 	}
154f62ca4e2SLi-hao Kuo 	writel(SP7021_SLAVE_SW_RST, pspim->s_base + SP7021_SLAVE_DMA_CTRL_REG);
15547e8fe57SLi-hao Kuo 	return 0;
156f62ca4e2SLi-hao Kuo }
157f62ca4e2SLi-hao Kuo 
sp7021_spi_master_rb(struct sp7021_spi_ctlr * pspim,unsigned int len)1586938e02fS郭力豪 static void sp7021_spi_master_rb(struct sp7021_spi_ctlr *pspim, unsigned int len)
159f62ca4e2SLi-hao Kuo {
160f62ca4e2SLi-hao Kuo 	int i;
161f62ca4e2SLi-hao Kuo 
162f62ca4e2SLi-hao Kuo 	for (i = 0; i < len; i++) {
163f62ca4e2SLi-hao Kuo 		pspim->rx_buf[pspim->rx_cur_len] =
164f62ca4e2SLi-hao Kuo 			readl(pspim->m_base + SP7021_FIFO_REG);
165f62ca4e2SLi-hao Kuo 		pspim->rx_cur_len++;
166f62ca4e2SLi-hao Kuo 	}
167f62ca4e2SLi-hao Kuo }
168f62ca4e2SLi-hao Kuo 
sp7021_spi_master_wb(struct sp7021_spi_ctlr * pspim,unsigned int len)1696938e02fS郭力豪 static void sp7021_spi_master_wb(struct sp7021_spi_ctlr *pspim, unsigned int len)
170f62ca4e2SLi-hao Kuo {
171f62ca4e2SLi-hao Kuo 	int i;
172f62ca4e2SLi-hao Kuo 
173f62ca4e2SLi-hao Kuo 	for (i = 0; i < len; i++) {
174f62ca4e2SLi-hao Kuo 		writel(pspim->tx_buf[pspim->tx_cur_len],
175f62ca4e2SLi-hao Kuo 		       pspim->m_base + SP7021_FIFO_REG);
176f62ca4e2SLi-hao Kuo 		pspim->tx_cur_len++;
177f62ca4e2SLi-hao Kuo 	}
178f62ca4e2SLi-hao Kuo }
179f62ca4e2SLi-hao Kuo 
sp7021_spi_master_irq(int irq,void * dev)180f62ca4e2SLi-hao Kuo static irqreturn_t sp7021_spi_master_irq(int irq, void *dev)
181f62ca4e2SLi-hao Kuo {
182f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = dev;
183f62ca4e2SLi-hao Kuo 	unsigned int tx_cnt, total_len;
184f62ca4e2SLi-hao Kuo 	unsigned int tx_len, rx_cnt;
185f62ca4e2SLi-hao Kuo 	unsigned int fd_status;
186f62ca4e2SLi-hao Kuo 	bool isrdone = false;
187f62ca4e2SLi-hao Kuo 	u32 value;
188f62ca4e2SLi-hao Kuo 
189f62ca4e2SLi-hao Kuo 	fd_status = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
190f62ca4e2SLi-hao Kuo 	tx_cnt = FIELD_GET(SP7021_TX_CNT_MASK, fd_status);
191f62ca4e2SLi-hao Kuo 	tx_len = FIELD_GET(SP7021_TX_LEN_MASK, fd_status);
192f62ca4e2SLi-hao Kuo 	total_len = FIELD_GET(SP7021_GET_LEN_MASK, fd_status);
193f62ca4e2SLi-hao Kuo 
194f62ca4e2SLi-hao Kuo 	if ((fd_status & SP7021_TX_EMP_FLAG) && (fd_status & SP7021_RX_EMP_FLAG) && total_len == 0)
195f62ca4e2SLi-hao Kuo 		return IRQ_NONE;
196f62ca4e2SLi-hao Kuo 
197f62ca4e2SLi-hao Kuo 	if (tx_len == 0 && total_len == 0)
198f62ca4e2SLi-hao Kuo 		return IRQ_NONE;
199f62ca4e2SLi-hao Kuo 
200f62ca4e2SLi-hao Kuo 	rx_cnt = FIELD_GET(SP7021_RX_CNT_MASK, fd_status);
201f62ca4e2SLi-hao Kuo 	if (fd_status & SP7021_RX_FULL_FLAG)
202f62ca4e2SLi-hao Kuo 		rx_cnt = pspim->data_unit;
203f62ca4e2SLi-hao Kuo 
204f62ca4e2SLi-hao Kuo 	tx_cnt = min(tx_len - pspim->tx_cur_len, pspim->data_unit - tx_cnt);
205f62ca4e2SLi-hao Kuo 	dev_dbg(pspim->dev, "fd_st=0x%x rx_c:%d tx_c:%d tx_l:%d",
206f62ca4e2SLi-hao Kuo 		fd_status, rx_cnt, tx_cnt, tx_len);
207f62ca4e2SLi-hao Kuo 
208f62ca4e2SLi-hao Kuo 	if (rx_cnt > 0)
209f62ca4e2SLi-hao Kuo 		sp7021_spi_master_rb(pspim, rx_cnt);
210f62ca4e2SLi-hao Kuo 	if (tx_cnt > 0)
211f62ca4e2SLi-hao Kuo 		sp7021_spi_master_wb(pspim, tx_cnt);
212f62ca4e2SLi-hao Kuo 
213f62ca4e2SLi-hao Kuo 	fd_status = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
214f62ca4e2SLi-hao Kuo 	tx_len = FIELD_GET(SP7021_TX_LEN_MASK, fd_status);
215f62ca4e2SLi-hao Kuo 	total_len = FIELD_GET(SP7021_GET_LEN_MASK, fd_status);
216f62ca4e2SLi-hao Kuo 
217f62ca4e2SLi-hao Kuo 	if (fd_status & SP7021_FINISH_FLAG || tx_len == pspim->tx_cur_len) {
218f62ca4e2SLi-hao Kuo 		while (total_len != pspim->rx_cur_len) {
219f62ca4e2SLi-hao Kuo 			fd_status = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
220f62ca4e2SLi-hao Kuo 			total_len = FIELD_GET(SP7021_GET_LEN_MASK, fd_status);
221f62ca4e2SLi-hao Kuo 			if (fd_status & SP7021_RX_FULL_FLAG)
222f62ca4e2SLi-hao Kuo 				rx_cnt = pspim->data_unit;
223f62ca4e2SLi-hao Kuo 			else
224f62ca4e2SLi-hao Kuo 				rx_cnt = FIELD_GET(SP7021_RX_CNT_MASK, fd_status);
225f62ca4e2SLi-hao Kuo 
226f62ca4e2SLi-hao Kuo 			if (rx_cnt > 0)
227f62ca4e2SLi-hao Kuo 				sp7021_spi_master_rb(pspim, rx_cnt);
228f62ca4e2SLi-hao Kuo 		}
229f62ca4e2SLi-hao Kuo 		value = readl(pspim->m_base + SP7021_INT_BUSY_REG);
230f62ca4e2SLi-hao Kuo 		value |= SP7021_CLR_MASTER_INT;
231f62ca4e2SLi-hao Kuo 		writel(value, pspim->m_base + SP7021_INT_BUSY_REG);
232f62ca4e2SLi-hao Kuo 		writel(SP7021_FINISH_FLAG, pspim->m_base + SP7021_SPI_STATUS_REG);
233f62ca4e2SLi-hao Kuo 		isrdone = true;
234f62ca4e2SLi-hao Kuo 	}
235f62ca4e2SLi-hao Kuo 
236f62ca4e2SLi-hao Kuo 	if (isrdone)
237f62ca4e2SLi-hao Kuo 		complete(&pspim->isr_done);
238f62ca4e2SLi-hao Kuo 	return IRQ_HANDLED;
239f62ca4e2SLi-hao Kuo }
240f62ca4e2SLi-hao Kuo 
sp7021_prep_transfer(struct spi_controller * ctlr,struct spi_device * spi)241f62ca4e2SLi-hao Kuo static void sp7021_prep_transfer(struct spi_controller *ctlr, struct spi_device *spi)
242f62ca4e2SLi-hao Kuo {
243f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
244f62ca4e2SLi-hao Kuo 
245f62ca4e2SLi-hao Kuo 	pspim->tx_cur_len = 0;
246f62ca4e2SLi-hao Kuo 	pspim->rx_cur_len = 0;
247f62ca4e2SLi-hao Kuo 	pspim->data_unit = SP7021_FIFO_DATA_LEN;
248f62ca4e2SLi-hao Kuo }
249f62ca4e2SLi-hao Kuo 
250f62ca4e2SLi-hao Kuo // preliminary set CS, CPOL, CPHA and LSB
sp7021_spi_controller_prepare_message(struct spi_controller * ctlr,struct spi_message * msg)251f62ca4e2SLi-hao Kuo static int sp7021_spi_controller_prepare_message(struct spi_controller *ctlr,
252f62ca4e2SLi-hao Kuo 						 struct spi_message *msg)
253f62ca4e2SLi-hao Kuo {
254f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
255f62ca4e2SLi-hao Kuo 	struct spi_device *s = msg->spi;
256f62ca4e2SLi-hao Kuo 	u32 valus, rs = 0;
257f62ca4e2SLi-hao Kuo 
258f62ca4e2SLi-hao Kuo 	valus = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
259f62ca4e2SLi-hao Kuo 	valus |= SP7021_FD_SW_RST;
260f62ca4e2SLi-hao Kuo 	writel(valus, pspim->m_base + SP7021_SPI_STATUS_REG);
261f62ca4e2SLi-hao Kuo 	rs |= SP7021_FD_SEL;
262f62ca4e2SLi-hao Kuo 	if (s->mode & SPI_CPOL)
263f62ca4e2SLi-hao Kuo 		rs |= SP7021_CPOL_FD;
264f62ca4e2SLi-hao Kuo 
265f62ca4e2SLi-hao Kuo 	if (s->mode & SPI_LSB_FIRST)
266f62ca4e2SLi-hao Kuo 		rs |= SP7021_LSB_SEL;
267f62ca4e2SLi-hao Kuo 
268f62ca4e2SLi-hao Kuo 	if (s->mode & SPI_CS_HIGH)
269f62ca4e2SLi-hao Kuo 		rs |= SP7021_CS_POR;
270f62ca4e2SLi-hao Kuo 
271f62ca4e2SLi-hao Kuo 	if (s->mode & SPI_CPHA)
272f62ca4e2SLi-hao Kuo 		rs |=  SP7021_CPHA_R;
273f62ca4e2SLi-hao Kuo 	else
274f62ca4e2SLi-hao Kuo 		rs |=  SP7021_CPHA_W;
275f62ca4e2SLi-hao Kuo 
276f62ca4e2SLi-hao Kuo 	rs |=  FIELD_PREP(SP7021_TX_UNIT, 0) | FIELD_PREP(SP7021_RX_UNIT, 0);
277f62ca4e2SLi-hao Kuo 	pspim->xfer_conf = rs;
278f62ca4e2SLi-hao Kuo 	if (pspim->xfer_conf & SP7021_CPOL_FD)
279f62ca4e2SLi-hao Kuo 		writel(pspim->xfer_conf, pspim->m_base + SP7021_SPI_CONFIG_REG);
280f62ca4e2SLi-hao Kuo 
281f62ca4e2SLi-hao Kuo 	return 0;
282f62ca4e2SLi-hao Kuo }
283f62ca4e2SLi-hao Kuo 
sp7021_spi_setup_clk(struct spi_controller * ctlr,struct spi_transfer * xfer)284f62ca4e2SLi-hao Kuo static void sp7021_spi_setup_clk(struct spi_controller *ctlr, struct spi_transfer *xfer)
285f62ca4e2SLi-hao Kuo {
286f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
287f62ca4e2SLi-hao Kuo 	u32 clk_rate, clk_sel, div;
288f62ca4e2SLi-hao Kuo 
289f62ca4e2SLi-hao Kuo 	clk_rate = clk_get_rate(pspim->spi_clk);
29047e8fe57SLi-hao Kuo 	div = max(2U, clk_rate / xfer->speed_hz);
29147e8fe57SLi-hao Kuo 
292f62ca4e2SLi-hao Kuo 	clk_sel = (div / 2) - 1;
29347e8fe57SLi-hao Kuo 	pspim->xfer_conf &= ~SP7021_CLK_MASK;
294f62ca4e2SLi-hao Kuo 	pspim->xfer_conf |= FIELD_PREP(SP7021_CLK_MASK, clk_sel);
295f62ca4e2SLi-hao Kuo 	writel(pspim->xfer_conf, pspim->m_base + SP7021_SPI_CONFIG_REG);
296f62ca4e2SLi-hao Kuo }
297f62ca4e2SLi-hao Kuo 
sp7021_spi_master_transfer_one(struct spi_controller * ctlr,struct spi_device * spi,struct spi_transfer * xfer)298f62ca4e2SLi-hao Kuo static int sp7021_spi_master_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
299f62ca4e2SLi-hao Kuo 				       struct spi_transfer *xfer)
300f62ca4e2SLi-hao Kuo {
301f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
302f62ca4e2SLi-hao Kuo 	unsigned long timeout = msecs_to_jiffies(1000);
303f62ca4e2SLi-hao Kuo 	unsigned int xfer_cnt, xfer_len, last_len;
304f62ca4e2SLi-hao Kuo 	unsigned int i, len_temp;
305f62ca4e2SLi-hao Kuo 	u32 reg_temp;
306f62ca4e2SLi-hao Kuo 
307f62ca4e2SLi-hao Kuo 	xfer_cnt = xfer->len / SP7021_SPI_DATA_SIZE;
308f62ca4e2SLi-hao Kuo 	last_len = xfer->len % SP7021_SPI_DATA_SIZE;
309f62ca4e2SLi-hao Kuo 
310f62ca4e2SLi-hao Kuo 	for (i = 0; i <= xfer_cnt; i++) {
311f62ca4e2SLi-hao Kuo 		mutex_lock(&pspim->buf_lock);
312f62ca4e2SLi-hao Kuo 		sp7021_prep_transfer(ctlr, spi);
313f62ca4e2SLi-hao Kuo 		sp7021_spi_setup_clk(ctlr, xfer);
314f62ca4e2SLi-hao Kuo 		reinit_completion(&pspim->isr_done);
315f62ca4e2SLi-hao Kuo 
316f62ca4e2SLi-hao Kuo 		if (i == xfer_cnt)
317f62ca4e2SLi-hao Kuo 			xfer_len = last_len;
318f62ca4e2SLi-hao Kuo 		else
319f62ca4e2SLi-hao Kuo 			xfer_len = SP7021_SPI_DATA_SIZE;
320f62ca4e2SLi-hao Kuo 
321f62ca4e2SLi-hao Kuo 		pspim->tx_buf = xfer->tx_buf + i * SP7021_SPI_DATA_SIZE;
322f62ca4e2SLi-hao Kuo 		pspim->rx_buf = xfer->rx_buf + i * SP7021_SPI_DATA_SIZE;
323f62ca4e2SLi-hao Kuo 
324f62ca4e2SLi-hao Kuo 		if (pspim->tx_cur_len < xfer_len) {
325f62ca4e2SLi-hao Kuo 			len_temp = min(pspim->data_unit, xfer_len);
326f62ca4e2SLi-hao Kuo 			sp7021_spi_master_wb(pspim, len_temp);
327f62ca4e2SLi-hao Kuo 		}
328f62ca4e2SLi-hao Kuo 		reg_temp = readl(pspim->m_base + SP7021_SPI_CONFIG_REG);
329f62ca4e2SLi-hao Kuo 		reg_temp &= ~SP7021_CLEAN_RW_BYTE;
330f62ca4e2SLi-hao Kuo 		reg_temp &= ~SP7021_CLEAN_FLUG_MASK;
331f62ca4e2SLi-hao Kuo 		reg_temp |= SP7021_FD_SEL | SP7021_FINISH_FLAG_MASK |
332f62ca4e2SLi-hao Kuo 			    SP7021_TX_EMP_FLAG_MASK | SP7021_RX_FULL_FLAG_MASK |
333f62ca4e2SLi-hao Kuo 			    FIELD_PREP(SP7021_TX_UNIT, 0) | FIELD_PREP(SP7021_RX_UNIT, 0);
334f62ca4e2SLi-hao Kuo 		writel(reg_temp, pspim->m_base + SP7021_SPI_CONFIG_REG);
335f62ca4e2SLi-hao Kuo 
336f62ca4e2SLi-hao Kuo 		reg_temp = FIELD_PREP(SP7021_SET_TX_LEN, xfer_len) |
337f62ca4e2SLi-hao Kuo 				      FIELD_PREP(SP7021_SET_XFER_LEN, xfer_len) |
338f62ca4e2SLi-hao Kuo 				      SP7021_SPI_START_FD;
339f62ca4e2SLi-hao Kuo 		writel(reg_temp, pspim->m_base + SP7021_SPI_STATUS_REG);
340f62ca4e2SLi-hao Kuo 
341f62ca4e2SLi-hao Kuo 		if (!wait_for_completion_interruptible_timeout(&pspim->isr_done, timeout)) {
342f62ca4e2SLi-hao Kuo 			dev_err(&spi->dev, "wait_for_completion err\n");
34320dc69caSYang Yingliang 			mutex_unlock(&pspim->buf_lock);
344f62ca4e2SLi-hao Kuo 			return -ETIMEDOUT;
345f62ca4e2SLi-hao Kuo 		}
346f62ca4e2SLi-hao Kuo 
347f62ca4e2SLi-hao Kuo 		reg_temp = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
348f62ca4e2SLi-hao Kuo 		if (reg_temp & SP7021_FINISH_FLAG) {
349f62ca4e2SLi-hao Kuo 			writel(SP7021_FINISH_FLAG, pspim->m_base + SP7021_SPI_STATUS_REG);
350f62ca4e2SLi-hao Kuo 			writel(readl(pspim->m_base + SP7021_SPI_CONFIG_REG) &
351f62ca4e2SLi-hao Kuo 				SP7021_CLEAN_FLUG_MASK, pspim->m_base + SP7021_SPI_CONFIG_REG);
352f62ca4e2SLi-hao Kuo 		}
353f62ca4e2SLi-hao Kuo 
354f62ca4e2SLi-hao Kuo 		if (pspim->xfer_conf & SP7021_CPOL_FD)
355f62ca4e2SLi-hao Kuo 			writel(pspim->xfer_conf, pspim->m_base + SP7021_SPI_CONFIG_REG);
356f62ca4e2SLi-hao Kuo 
357f62ca4e2SLi-hao Kuo 		mutex_unlock(&pspim->buf_lock);
358f62ca4e2SLi-hao Kuo 	}
35947e8fe57SLi-hao Kuo 	return 0;
360f62ca4e2SLi-hao Kuo }
361f62ca4e2SLi-hao Kuo 
sp7021_spi_slave_transfer_one(struct spi_controller * ctlr,struct spi_device * spi,struct spi_transfer * xfer)362f62ca4e2SLi-hao Kuo static int sp7021_spi_slave_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
363f62ca4e2SLi-hao Kuo 				       struct spi_transfer *xfer)
364f62ca4e2SLi-hao Kuo {
365f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
366f62ca4e2SLi-hao Kuo 	struct device *dev = pspim->dev;
3675790597dSLi-hao Kuo 	int ret;
368f62ca4e2SLi-hao Kuo 
3695790597dSLi-hao Kuo 	if (xfer->tx_buf && !xfer->rx_buf) {
370f62ca4e2SLi-hao Kuo 		xfer->tx_dma = dma_map_single(dev, (void *)xfer->tx_buf,
371f62ca4e2SLi-hao Kuo 					      xfer->len, DMA_TO_DEVICE);
372f62ca4e2SLi-hao Kuo 		if (dma_mapping_error(dev, xfer->tx_dma))
373f62ca4e2SLi-hao Kuo 			return -ENOMEM;
3745790597dSLi-hao Kuo 		ret = sp7021_spi_slave_tx(spi, xfer);
3755790597dSLi-hao Kuo 		dma_unmap_single(dev, xfer->tx_dma, xfer->len, DMA_TO_DEVICE);
3765790597dSLi-hao Kuo 	} else if (xfer->rx_buf && !xfer->tx_buf) {
377f62ca4e2SLi-hao Kuo 		xfer->rx_dma = dma_map_single(dev, xfer->rx_buf, xfer->len,
378f62ca4e2SLi-hao Kuo 					      DMA_FROM_DEVICE);
379f62ca4e2SLi-hao Kuo 		if (dma_mapping_error(dev, xfer->rx_dma))
380f62ca4e2SLi-hao Kuo 			return -ENOMEM;
381f62ca4e2SLi-hao Kuo 		ret = sp7021_spi_slave_rx(spi, xfer);
382f62ca4e2SLi-hao Kuo 		dma_unmap_single(dev, xfer->rx_dma, xfer->len, DMA_FROM_DEVICE);
3835790597dSLi-hao Kuo 	} else {
3845790597dSLi-hao Kuo 		dev_dbg(&ctlr->dev, "%s() wrong command\n", __func__);
3855790597dSLi-hao Kuo 		return -EINVAL;
3865790597dSLi-hao Kuo 	}
387f62ca4e2SLi-hao Kuo 
388f62ca4e2SLi-hao Kuo 	spi_finalize_current_transfer(ctlr);
389f62ca4e2SLi-hao Kuo 	return ret;
390f62ca4e2SLi-hao Kuo }
391f62ca4e2SLi-hao Kuo 
sp7021_spi_disable_unprepare(void * data)392f62ca4e2SLi-hao Kuo static void sp7021_spi_disable_unprepare(void *data)
393f62ca4e2SLi-hao Kuo {
394f62ca4e2SLi-hao Kuo 	clk_disable_unprepare(data);
395f62ca4e2SLi-hao Kuo }
396f62ca4e2SLi-hao Kuo 
sp7021_spi_reset_control_assert(void * data)397f62ca4e2SLi-hao Kuo static void sp7021_spi_reset_control_assert(void *data)
398f62ca4e2SLi-hao Kuo {
399f62ca4e2SLi-hao Kuo 	reset_control_assert(data);
400f62ca4e2SLi-hao Kuo }
401f62ca4e2SLi-hao Kuo 
sp7021_spi_controller_probe(struct platform_device * pdev)402f62ca4e2SLi-hao Kuo static int sp7021_spi_controller_probe(struct platform_device *pdev)
403f62ca4e2SLi-hao Kuo {
404f62ca4e2SLi-hao Kuo 	struct device *dev = &pdev->dev;
405f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim;
406f62ca4e2SLi-hao Kuo 	struct spi_controller *ctlr;
407f62ca4e2SLi-hao Kuo 	int mode, ret;
408f62ca4e2SLi-hao Kuo 
409f62ca4e2SLi-hao Kuo 	pdev->id = of_alias_get_id(pdev->dev.of_node, "sp_spi");
410f62ca4e2SLi-hao Kuo 
411f62ca4e2SLi-hao Kuo 	if (device_property_read_bool(dev, "spi-slave"))
412f62ca4e2SLi-hao Kuo 		mode = SP7021_SLAVE_MODE;
413f62ca4e2SLi-hao Kuo 	else
414f62ca4e2SLi-hao Kuo 		mode = SP7021_MASTER_MODE;
415f62ca4e2SLi-hao Kuo 
416f62ca4e2SLi-hao Kuo 	if (mode == SP7021_SLAVE_MODE)
417f62ca4e2SLi-hao Kuo 		ctlr = devm_spi_alloc_slave(dev, sizeof(*pspim));
418f62ca4e2SLi-hao Kuo 	else
419f62ca4e2SLi-hao Kuo 		ctlr = devm_spi_alloc_master(dev, sizeof(*pspim));
420f62ca4e2SLi-hao Kuo 	if (!ctlr)
421f62ca4e2SLi-hao Kuo 		return -ENOMEM;
42247e8fe57SLi-hao Kuo 	device_set_node(&ctlr->dev, dev_fwnode(dev));
423f62ca4e2SLi-hao Kuo 	ctlr->bus_num = pdev->id;
424f62ca4e2SLi-hao Kuo 	ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
425f62ca4e2SLi-hao Kuo 	ctlr->auto_runtime_pm = true;
426f62ca4e2SLi-hao Kuo 	ctlr->prepare_message = sp7021_spi_controller_prepare_message;
427f62ca4e2SLi-hao Kuo 	if (mode == SP7021_SLAVE_MODE) {
428f62ca4e2SLi-hao Kuo 		ctlr->transfer_one = sp7021_spi_slave_transfer_one;
429f62ca4e2SLi-hao Kuo 		ctlr->slave_abort = sp7021_spi_slave_abort;
430f62ca4e2SLi-hao Kuo 		ctlr->flags = SPI_CONTROLLER_HALF_DUPLEX;
431f62ca4e2SLi-hao Kuo 	} else {
432f62ca4e2SLi-hao Kuo 		ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
433f62ca4e2SLi-hao Kuo 		ctlr->min_speed_hz = 40000;
434f62ca4e2SLi-hao Kuo 		ctlr->max_speed_hz = 25000000;
435f62ca4e2SLi-hao Kuo 		ctlr->use_gpio_descriptors = true;
436f62ca4e2SLi-hao Kuo 		ctlr->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
437f62ca4e2SLi-hao Kuo 		ctlr->transfer_one = sp7021_spi_master_transfer_one;
438f62ca4e2SLi-hao Kuo 	}
439f62ca4e2SLi-hao Kuo 	platform_set_drvdata(pdev, ctlr);
440f62ca4e2SLi-hao Kuo 	pspim = spi_controller_get_devdata(ctlr);
441f62ca4e2SLi-hao Kuo 	pspim->mode = mode;
442f62ca4e2SLi-hao Kuo 	pspim->ctlr = ctlr;
443f62ca4e2SLi-hao Kuo 	pspim->dev = dev;
444f62ca4e2SLi-hao Kuo 	mutex_init(&pspim->buf_lock);
445f62ca4e2SLi-hao Kuo 	init_completion(&pspim->isr_done);
446f62ca4e2SLi-hao Kuo 	init_completion(&pspim->slave_isr);
447f62ca4e2SLi-hao Kuo 
448f62ca4e2SLi-hao Kuo 	pspim->m_base = devm_platform_ioremap_resource_byname(pdev, "master");
449f62ca4e2SLi-hao Kuo 	if (IS_ERR(pspim->m_base))
450f62ca4e2SLi-hao Kuo 		return dev_err_probe(dev, PTR_ERR(pspim->m_base), "m_base get fail\n");
451f62ca4e2SLi-hao Kuo 
452f62ca4e2SLi-hao Kuo 	pspim->s_base = devm_platform_ioremap_resource_byname(pdev, "slave");
453f62ca4e2SLi-hao Kuo 	if (IS_ERR(pspim->s_base))
454f62ca4e2SLi-hao Kuo 		return dev_err_probe(dev, PTR_ERR(pspim->s_base), "s_base get fail\n");
455f62ca4e2SLi-hao Kuo 
456f62ca4e2SLi-hao Kuo 	pspim->m_irq = platform_get_irq_byname(pdev, "master_risc");
457f62ca4e2SLi-hao Kuo 	if (pspim->m_irq < 0)
458f62ca4e2SLi-hao Kuo 		return pspim->m_irq;
459f62ca4e2SLi-hao Kuo 
460f62ca4e2SLi-hao Kuo 	pspim->s_irq = platform_get_irq_byname(pdev, "slave_risc");
461f62ca4e2SLi-hao Kuo 	if (pspim->s_irq < 0)
462f62ca4e2SLi-hao Kuo 		return pspim->s_irq;
463f62ca4e2SLi-hao Kuo 
464f62ca4e2SLi-hao Kuo 	pspim->spi_clk = devm_clk_get(dev, NULL);
465f62ca4e2SLi-hao Kuo 	if (IS_ERR(pspim->spi_clk))
466f62ca4e2SLi-hao Kuo 		return dev_err_probe(dev, PTR_ERR(pspim->spi_clk), "clk get fail\n");
467f62ca4e2SLi-hao Kuo 
468f62ca4e2SLi-hao Kuo 	pspim->rstc = devm_reset_control_get_exclusive(dev, NULL);
469f62ca4e2SLi-hao Kuo 	if (IS_ERR(pspim->rstc))
470f62ca4e2SLi-hao Kuo 		return dev_err_probe(dev, PTR_ERR(pspim->rstc), "rst get fail\n");
471f62ca4e2SLi-hao Kuo 
472f62ca4e2SLi-hao Kuo 	ret = clk_prepare_enable(pspim->spi_clk);
473f62ca4e2SLi-hao Kuo 	if (ret)
474f62ca4e2SLi-hao Kuo 		return dev_err_probe(dev, ret, "failed to enable clk\n");
475f62ca4e2SLi-hao Kuo 
476f62ca4e2SLi-hao Kuo 	ret = devm_add_action_or_reset(dev, sp7021_spi_disable_unprepare, pspim->spi_clk);
477f62ca4e2SLi-hao Kuo 	if (ret)
478f62ca4e2SLi-hao Kuo 		return ret;
479f62ca4e2SLi-hao Kuo 
480f62ca4e2SLi-hao Kuo 	ret = reset_control_deassert(pspim->rstc);
481f62ca4e2SLi-hao Kuo 	if (ret)
482f62ca4e2SLi-hao Kuo 		return dev_err_probe(dev, ret, "failed to deassert reset\n");
483f62ca4e2SLi-hao Kuo 
484f62ca4e2SLi-hao Kuo 	ret = devm_add_action_or_reset(dev, sp7021_spi_reset_control_assert, pspim->rstc);
485f62ca4e2SLi-hao Kuo 	if (ret)
486f62ca4e2SLi-hao Kuo 		return ret;
487f62ca4e2SLi-hao Kuo 
48847e8fe57SLi-hao Kuo 	ret = devm_request_irq(dev, pspim->m_irq, sp7021_spi_master_irq,
48947e8fe57SLi-hao Kuo 			       IRQF_TRIGGER_RISING, pdev->name, pspim);
49047e8fe57SLi-hao Kuo 	if (ret)
49147e8fe57SLi-hao Kuo 		return ret;
49247e8fe57SLi-hao Kuo 
49347e8fe57SLi-hao Kuo 	ret = devm_request_irq(dev, pspim->s_irq, sp7021_spi_slave_irq,
49447e8fe57SLi-hao Kuo 			       IRQF_TRIGGER_RISING, pdev->name, pspim);
49547e8fe57SLi-hao Kuo 	if (ret)
49647e8fe57SLi-hao Kuo 		return ret;
49747e8fe57SLi-hao Kuo 
498f62ca4e2SLi-hao Kuo 	pm_runtime_enable(dev);
499f62ca4e2SLi-hao Kuo 	ret = spi_register_controller(ctlr);
500f62ca4e2SLi-hao Kuo 	if (ret) {
501f62ca4e2SLi-hao Kuo 		pm_runtime_disable(dev);
502f62ca4e2SLi-hao Kuo 		return dev_err_probe(dev, ret, "spi_register_master fail\n");
503f62ca4e2SLi-hao Kuo 	}
504f62ca4e2SLi-hao Kuo 	return 0;
505f62ca4e2SLi-hao Kuo }
506f62ca4e2SLi-hao Kuo 
sp7021_spi_controller_remove(struct platform_device * pdev)507*18bb7328SUwe Kleine-König static void sp7021_spi_controller_remove(struct platform_device *pdev)
508f62ca4e2SLi-hao Kuo {
509f62ca4e2SLi-hao Kuo 	struct spi_controller *ctlr = dev_get_drvdata(&pdev->dev);
510f62ca4e2SLi-hao Kuo 
511f62ca4e2SLi-hao Kuo 	spi_unregister_controller(ctlr);
512f62ca4e2SLi-hao Kuo 	pm_runtime_disable(&pdev->dev);
513f62ca4e2SLi-hao Kuo 	pm_runtime_set_suspended(&pdev->dev);
514f62ca4e2SLi-hao Kuo }
515f62ca4e2SLi-hao Kuo 
sp7021_spi_controller_suspend(struct device * dev)516f62ca4e2SLi-hao Kuo static int __maybe_unused sp7021_spi_controller_suspend(struct device *dev)
517f62ca4e2SLi-hao Kuo {
518f62ca4e2SLi-hao Kuo 	struct spi_controller *ctlr = dev_get_drvdata(dev);
519f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
520f62ca4e2SLi-hao Kuo 
521f62ca4e2SLi-hao Kuo 	return reset_control_assert(pspim->rstc);
522f62ca4e2SLi-hao Kuo }
523f62ca4e2SLi-hao Kuo 
sp7021_spi_controller_resume(struct device * dev)524f62ca4e2SLi-hao Kuo static int __maybe_unused sp7021_spi_controller_resume(struct device *dev)
525f62ca4e2SLi-hao Kuo {
526f62ca4e2SLi-hao Kuo 	struct spi_controller *ctlr = dev_get_drvdata(dev);
527f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
528f62ca4e2SLi-hao Kuo 
529f62ca4e2SLi-hao Kuo 	reset_control_deassert(pspim->rstc);
530f62ca4e2SLi-hao Kuo 	return clk_prepare_enable(pspim->spi_clk);
531f62ca4e2SLi-hao Kuo }
532f62ca4e2SLi-hao Kuo 
5336938e02fS郭力豪 #ifdef CONFIG_PM
sp7021_spi_runtime_suspend(struct device * dev)534f62ca4e2SLi-hao Kuo static int sp7021_spi_runtime_suspend(struct device *dev)
535f62ca4e2SLi-hao Kuo {
536f62ca4e2SLi-hao Kuo 	struct spi_controller *ctlr = dev_get_drvdata(dev);
537f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
538f62ca4e2SLi-hao Kuo 
539f62ca4e2SLi-hao Kuo 	return reset_control_assert(pspim->rstc);
540f62ca4e2SLi-hao Kuo }
541f62ca4e2SLi-hao Kuo 
sp7021_spi_runtime_resume(struct device * dev)542f62ca4e2SLi-hao Kuo static int sp7021_spi_runtime_resume(struct device *dev)
543f62ca4e2SLi-hao Kuo {
544f62ca4e2SLi-hao Kuo 	struct spi_controller *ctlr = dev_get_drvdata(dev);
545f62ca4e2SLi-hao Kuo 	struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
546f62ca4e2SLi-hao Kuo 
547f62ca4e2SLi-hao Kuo 	return reset_control_deassert(pspim->rstc);
548f62ca4e2SLi-hao Kuo }
5496938e02fS郭力豪 #endif
550f62ca4e2SLi-hao Kuo 
551f62ca4e2SLi-hao Kuo static const struct dev_pm_ops sp7021_spi_pm_ops = {
552f62ca4e2SLi-hao Kuo 	SET_RUNTIME_PM_OPS(sp7021_spi_runtime_suspend,
553f62ca4e2SLi-hao Kuo 			   sp7021_spi_runtime_resume, NULL)
554f62ca4e2SLi-hao Kuo 	SET_SYSTEM_SLEEP_PM_OPS(sp7021_spi_controller_suspend,
555f62ca4e2SLi-hao Kuo 				sp7021_spi_controller_resume)
556f62ca4e2SLi-hao Kuo };
557f62ca4e2SLi-hao Kuo 
558f62ca4e2SLi-hao Kuo static const struct of_device_id sp7021_spi_controller_ids[] = {
559f62ca4e2SLi-hao Kuo 	{ .compatible = "sunplus,sp7021-spi" },
560f62ca4e2SLi-hao Kuo 	{}
561f62ca4e2SLi-hao Kuo };
562f62ca4e2SLi-hao Kuo MODULE_DEVICE_TABLE(of, sp7021_spi_controller_ids);
563f62ca4e2SLi-hao Kuo 
564f62ca4e2SLi-hao Kuo static struct platform_driver sp7021_spi_controller_driver = {
565f62ca4e2SLi-hao Kuo 	.probe = sp7021_spi_controller_probe,
566*18bb7328SUwe Kleine-König 	.remove_new = sp7021_spi_controller_remove,
567f62ca4e2SLi-hao Kuo 	.driver = {
568f62ca4e2SLi-hao Kuo 		.name = "sunplus,sp7021-spi-controller",
569f62ca4e2SLi-hao Kuo 		.of_match_table = sp7021_spi_controller_ids,
570f62ca4e2SLi-hao Kuo 		.pm     = &sp7021_spi_pm_ops,
571f62ca4e2SLi-hao Kuo 	},
572f62ca4e2SLi-hao Kuo };
573f62ca4e2SLi-hao Kuo module_platform_driver(sp7021_spi_controller_driver);
574f62ca4e2SLi-hao Kuo 
575f62ca4e2SLi-hao Kuo MODULE_AUTHOR("Li-hao Kuo <lhjeff911@gmail.com>");
576f62ca4e2SLi-hao Kuo MODULE_DESCRIPTION("Sunplus SPI controller driver");
577f62ca4e2SLi-hao Kuo MODULE_LICENSE("GPL");
578