xref: /linux/drivers/spi/spi-geni-qcom.c (revision de43affed77b1bd9f246235dbd94eb23b07ab657)
1561de45fSGirish Mahadevan // SPDX-License-Identifier: GPL-2.0
2561de45fSGirish Mahadevan // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
3561de45fSGirish Mahadevan 
4561de45fSGirish Mahadevan #include <linux/clk.h>
5561de45fSGirish Mahadevan #include <linux/interrupt.h>
6561de45fSGirish Mahadevan #include <linux/io.h>
7561de45fSGirish Mahadevan #include <linux/log2.h>
8561de45fSGirish Mahadevan #include <linux/module.h>
9561de45fSGirish Mahadevan #include <linux/of.h>
10561de45fSGirish Mahadevan #include <linux/platform_device.h>
11561de45fSGirish Mahadevan #include <linux/pm_runtime.h>
12561de45fSGirish Mahadevan #include <linux/qcom-geni-se.h>
13561de45fSGirish Mahadevan #include <linux/spi/spi.h>
14561de45fSGirish Mahadevan #include <linux/spinlock.h>
15561de45fSGirish Mahadevan 
16561de45fSGirish Mahadevan /* SPI SE specific registers and respective register fields */
17561de45fSGirish Mahadevan #define SE_SPI_CPHA		0x224
18561de45fSGirish Mahadevan #define CPHA			BIT(0)
19561de45fSGirish Mahadevan 
20561de45fSGirish Mahadevan #define SE_SPI_LOOPBACK		0x22c
21561de45fSGirish Mahadevan #define LOOPBACK_ENABLE		0x1
22561de45fSGirish Mahadevan #define NORMAL_MODE		0x0
23561de45fSGirish Mahadevan #define LOOPBACK_MSK		GENMASK(1, 0)
24561de45fSGirish Mahadevan 
25561de45fSGirish Mahadevan #define SE_SPI_CPOL		0x230
26561de45fSGirish Mahadevan #define CPOL			BIT(2)
27561de45fSGirish Mahadevan 
28561de45fSGirish Mahadevan #define SE_SPI_DEMUX_OUTPUT_INV	0x24c
29561de45fSGirish Mahadevan #define CS_DEMUX_OUTPUT_INV_MSK	GENMASK(3, 0)
30561de45fSGirish Mahadevan 
31561de45fSGirish Mahadevan #define SE_SPI_DEMUX_SEL	0x250
32561de45fSGirish Mahadevan #define CS_DEMUX_OUTPUT_SEL	GENMASK(3, 0)
33561de45fSGirish Mahadevan 
34561de45fSGirish Mahadevan #define SE_SPI_TRANS_CFG	0x25c
35561de45fSGirish Mahadevan #define CS_TOGGLE		BIT(0)
36561de45fSGirish Mahadevan 
37561de45fSGirish Mahadevan #define SE_SPI_WORD_LEN		0x268
38561de45fSGirish Mahadevan #define WORD_LEN_MSK		GENMASK(9, 0)
39561de45fSGirish Mahadevan #define MIN_WORD_LEN		4
40561de45fSGirish Mahadevan 
41561de45fSGirish Mahadevan #define SE_SPI_TX_TRANS_LEN	0x26c
42561de45fSGirish Mahadevan #define SE_SPI_RX_TRANS_LEN	0x270
43561de45fSGirish Mahadevan #define TRANS_LEN_MSK		GENMASK(23, 0)
44561de45fSGirish Mahadevan 
45561de45fSGirish Mahadevan #define SE_SPI_PRE_POST_CMD_DLY	0x274
46561de45fSGirish Mahadevan 
47561de45fSGirish Mahadevan #define SE_SPI_DELAY_COUNTERS	0x278
48561de45fSGirish Mahadevan #define SPI_INTER_WORDS_DELAY_MSK	GENMASK(9, 0)
49561de45fSGirish Mahadevan #define SPI_CS_CLK_DELAY_MSK		GENMASK(19, 10)
50561de45fSGirish Mahadevan #define SPI_CS_CLK_DELAY_SHFT		10
51561de45fSGirish Mahadevan 
52561de45fSGirish Mahadevan /* M_CMD OP codes for SPI */
53561de45fSGirish Mahadevan #define SPI_TX_ONLY		1
54561de45fSGirish Mahadevan #define SPI_RX_ONLY		2
55561de45fSGirish Mahadevan #define SPI_FULL_DUPLEX		3
56561de45fSGirish Mahadevan #define SPI_TX_RX		7
57561de45fSGirish Mahadevan #define SPI_CS_ASSERT		8
58561de45fSGirish Mahadevan #define SPI_CS_DEASSERT		9
59561de45fSGirish Mahadevan #define SPI_SCK_ONLY		10
60561de45fSGirish Mahadevan /* M_CMD params for SPI */
61561de45fSGirish Mahadevan #define SPI_PRE_CMD_DELAY	BIT(0)
62561de45fSGirish Mahadevan #define TIMESTAMP_BEFORE	BIT(1)
63561de45fSGirish Mahadevan #define FRAGMENTATION		BIT(2)
64561de45fSGirish Mahadevan #define TIMESTAMP_AFTER		BIT(3)
65561de45fSGirish Mahadevan #define POST_CMD_DELAY		BIT(4)
66561de45fSGirish Mahadevan 
670dccff3cSAlok Chauhan enum spi_m_cmd_opcode {
68561de45fSGirish Mahadevan 	CMD_NONE,
69561de45fSGirish Mahadevan 	CMD_XFER,
70561de45fSGirish Mahadevan 	CMD_CS,
71561de45fSGirish Mahadevan 	CMD_CANCEL,
72561de45fSGirish Mahadevan };
73561de45fSGirish Mahadevan 
74561de45fSGirish Mahadevan struct spi_geni_master {
75561de45fSGirish Mahadevan 	struct geni_se se;
76561de45fSGirish Mahadevan 	struct device *dev;
77561de45fSGirish Mahadevan 	u32 tx_fifo_depth;
78561de45fSGirish Mahadevan 	u32 fifo_width_bits;
79561de45fSGirish Mahadevan 	u32 tx_wm;
80561de45fSGirish Mahadevan 	unsigned long cur_speed_hz;
81561de45fSGirish Mahadevan 	unsigned int cur_bits_per_word;
82561de45fSGirish Mahadevan 	unsigned int tx_rem_bytes;
83561de45fSGirish Mahadevan 	unsigned int rx_rem_bytes;
84561de45fSGirish Mahadevan 	const struct spi_transfer *cur_xfer;
85561de45fSGirish Mahadevan 	struct completion xfer_done;
86561de45fSGirish Mahadevan 	unsigned int oversampling;
87561de45fSGirish Mahadevan 	spinlock_t lock;
880dccff3cSAlok Chauhan 	enum spi_m_cmd_opcode cur_mcmd;
89561de45fSGirish Mahadevan 	int irq;
90561de45fSGirish Mahadevan };
91561de45fSGirish Mahadevan 
92561de45fSGirish Mahadevan static int get_spi_clk_cfg(unsigned int speed_hz,
93561de45fSGirish Mahadevan 			struct spi_geni_master *mas,
94561de45fSGirish Mahadevan 			unsigned int *clk_idx,
95561de45fSGirish Mahadevan 			unsigned int *clk_div)
96561de45fSGirish Mahadevan {
97561de45fSGirish Mahadevan 	unsigned long sclk_freq;
98561de45fSGirish Mahadevan 	unsigned int actual_hz;
99561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
100561de45fSGirish Mahadevan 	int ret;
101561de45fSGirish Mahadevan 
102561de45fSGirish Mahadevan 	ret = geni_se_clk_freq_match(&mas->se,
103561de45fSGirish Mahadevan 				speed_hz * mas->oversampling,
104561de45fSGirish Mahadevan 				clk_idx, &sclk_freq, false);
105561de45fSGirish Mahadevan 	if (ret) {
106561de45fSGirish Mahadevan 		dev_err(mas->dev, "Failed(%d) to find src clk for %dHz\n",
107561de45fSGirish Mahadevan 							ret, speed_hz);
108561de45fSGirish Mahadevan 		return ret;
109561de45fSGirish Mahadevan 	}
110561de45fSGirish Mahadevan 
111561de45fSGirish Mahadevan 	*clk_div = DIV_ROUND_UP(sclk_freq, mas->oversampling * speed_hz);
112561de45fSGirish Mahadevan 	actual_hz = sclk_freq / (mas->oversampling * *clk_div);
113561de45fSGirish Mahadevan 
114561de45fSGirish Mahadevan 	dev_dbg(mas->dev, "req %u=>%u sclk %lu, idx %d, div %d\n", speed_hz,
115561de45fSGirish Mahadevan 				actual_hz, sclk_freq, *clk_idx, *clk_div);
116561de45fSGirish Mahadevan 	ret = clk_set_rate(se->clk, sclk_freq);
117561de45fSGirish Mahadevan 	if (ret)
118561de45fSGirish Mahadevan 		dev_err(mas->dev, "clk_set_rate failed %d\n", ret);
119561de45fSGirish Mahadevan 	return ret;
120561de45fSGirish Mahadevan }
121561de45fSGirish Mahadevan 
122*de43affeSStephen Boyd static void handle_fifo_timeout(struct spi_master *spi,
123*de43affeSStephen Boyd 				struct spi_message *msg)
124*de43affeSStephen Boyd {
125*de43affeSStephen Boyd 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
126*de43affeSStephen Boyd 	unsigned long time_left, flags;
127*de43affeSStephen Boyd 	struct geni_se *se = &mas->se;
128*de43affeSStephen Boyd 
129*de43affeSStephen Boyd 	spin_lock_irqsave(&mas->lock, flags);
130*de43affeSStephen Boyd 	reinit_completion(&mas->xfer_done);
131*de43affeSStephen Boyd 	mas->cur_mcmd = CMD_CANCEL;
132*de43affeSStephen Boyd 	geni_se_cancel_m_cmd(se);
133*de43affeSStephen Boyd 	writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
134*de43affeSStephen Boyd 	spin_unlock_irqrestore(&mas->lock, flags);
135*de43affeSStephen Boyd 	time_left = wait_for_completion_timeout(&mas->xfer_done, HZ);
136*de43affeSStephen Boyd 	if (time_left)
137*de43affeSStephen Boyd 		return;
138*de43affeSStephen Boyd 
139*de43affeSStephen Boyd 	spin_lock_irqsave(&mas->lock, flags);
140*de43affeSStephen Boyd 	reinit_completion(&mas->xfer_done);
141*de43affeSStephen Boyd 	geni_se_abort_m_cmd(se);
142*de43affeSStephen Boyd 	spin_unlock_irqrestore(&mas->lock, flags);
143*de43affeSStephen Boyd 	time_left = wait_for_completion_timeout(&mas->xfer_done, HZ);
144*de43affeSStephen Boyd 	if (!time_left)
145*de43affeSStephen Boyd 		dev_err(mas->dev, "Failed to cancel/abort m_cmd\n");
146*de43affeSStephen Boyd }
147*de43affeSStephen Boyd 
148561de45fSGirish Mahadevan static void spi_geni_set_cs(struct spi_device *slv, bool set_flag)
149561de45fSGirish Mahadevan {
150561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(slv->master);
151561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(mas->dev);
152561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
1530dccff3cSAlok Chauhan 	unsigned long time_left;
154561de45fSGirish Mahadevan 
155561de45fSGirish Mahadevan 	reinit_completion(&mas->xfer_done);
156561de45fSGirish Mahadevan 	pm_runtime_get_sync(mas->dev);
157561de45fSGirish Mahadevan 	if (!(slv->mode & SPI_CS_HIGH))
158561de45fSGirish Mahadevan 		set_flag = !set_flag;
159561de45fSGirish Mahadevan 
160561de45fSGirish Mahadevan 	mas->cur_mcmd = CMD_CS;
161561de45fSGirish Mahadevan 	if (set_flag)
162561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_ASSERT, 0);
163561de45fSGirish Mahadevan 	else
164561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_DEASSERT, 0);
165561de45fSGirish Mahadevan 
1660dccff3cSAlok Chauhan 	time_left = wait_for_completion_timeout(&mas->xfer_done, HZ);
1670dccff3cSAlok Chauhan 	if (!time_left)
168561de45fSGirish Mahadevan 		handle_fifo_timeout(spi, NULL);
169561de45fSGirish Mahadevan 
170561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
171561de45fSGirish Mahadevan }
172561de45fSGirish Mahadevan 
173561de45fSGirish Mahadevan static void spi_setup_word_len(struct spi_geni_master *mas, u16 mode,
174561de45fSGirish Mahadevan 					unsigned int bits_per_word)
175561de45fSGirish Mahadevan {
176561de45fSGirish Mahadevan 	unsigned int pack_words;
177561de45fSGirish Mahadevan 	bool msb_first = (mode & SPI_LSB_FIRST) ? false : true;
178561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
179561de45fSGirish Mahadevan 	u32 word_len;
180561de45fSGirish Mahadevan 
181561de45fSGirish Mahadevan 	word_len = readl(se->base + SE_SPI_WORD_LEN);
182561de45fSGirish Mahadevan 
183561de45fSGirish Mahadevan 	/*
184561de45fSGirish Mahadevan 	 * If bits_per_word isn't a byte aligned value, set the packing to be
185561de45fSGirish Mahadevan 	 * 1 SPI word per FIFO word.
186561de45fSGirish Mahadevan 	 */
187561de45fSGirish Mahadevan 	if (!(mas->fifo_width_bits % bits_per_word))
188561de45fSGirish Mahadevan 		pack_words = mas->fifo_width_bits / bits_per_word;
189561de45fSGirish Mahadevan 	else
190561de45fSGirish Mahadevan 		pack_words = 1;
191561de45fSGirish Mahadevan 	word_len &= ~WORD_LEN_MSK;
192561de45fSGirish Mahadevan 	word_len |= ((bits_per_word - MIN_WORD_LEN) & WORD_LEN_MSK);
193561de45fSGirish Mahadevan 	geni_se_config_packing(&mas->se, bits_per_word, pack_words, msb_first,
194561de45fSGirish Mahadevan 								true, true);
195561de45fSGirish Mahadevan 	writel(word_len, se->base + SE_SPI_WORD_LEN);
196561de45fSGirish Mahadevan }
197561de45fSGirish Mahadevan 
198561de45fSGirish Mahadevan static int setup_fifo_params(struct spi_device *spi_slv,
199561de45fSGirish Mahadevan 					struct spi_master *spi)
200561de45fSGirish Mahadevan {
201561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
202561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
203561de45fSGirish Mahadevan 	u32 loopback_cfg, cpol, cpha, demux_output_inv;
204561de45fSGirish Mahadevan 	u32 demux_sel, clk_sel, m_clk_cfg, idx, div;
205561de45fSGirish Mahadevan 	int ret;
206561de45fSGirish Mahadevan 
207561de45fSGirish Mahadevan 	loopback_cfg = readl(se->base + SE_SPI_LOOPBACK);
208561de45fSGirish Mahadevan 	cpol = readl(se->base + SE_SPI_CPOL);
209561de45fSGirish Mahadevan 	cpha = readl(se->base + SE_SPI_CPHA);
210561de45fSGirish Mahadevan 	demux_output_inv = 0;
211561de45fSGirish Mahadevan 	loopback_cfg &= ~LOOPBACK_MSK;
212561de45fSGirish Mahadevan 	cpol &= ~CPOL;
213561de45fSGirish Mahadevan 	cpha &= ~CPHA;
214561de45fSGirish Mahadevan 
215561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_LOOP)
216561de45fSGirish Mahadevan 		loopback_cfg |= LOOPBACK_ENABLE;
217561de45fSGirish Mahadevan 
218561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CPOL)
219561de45fSGirish Mahadevan 		cpol |= CPOL;
220561de45fSGirish Mahadevan 
221561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CPHA)
222561de45fSGirish Mahadevan 		cpha |= CPHA;
223561de45fSGirish Mahadevan 
224561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CS_HIGH)
225561de45fSGirish Mahadevan 		demux_output_inv = BIT(spi_slv->chip_select);
226561de45fSGirish Mahadevan 
227561de45fSGirish Mahadevan 	demux_sel = spi_slv->chip_select;
228561de45fSGirish Mahadevan 	mas->cur_speed_hz = spi_slv->max_speed_hz;
229561de45fSGirish Mahadevan 	mas->cur_bits_per_word = spi_slv->bits_per_word;
230561de45fSGirish Mahadevan 
231561de45fSGirish Mahadevan 	ret = get_spi_clk_cfg(mas->cur_speed_hz, mas, &idx, &div);
232561de45fSGirish Mahadevan 	if (ret) {
233561de45fSGirish Mahadevan 		dev_err(mas->dev, "Err setting clks ret(%d) for %ld\n",
234561de45fSGirish Mahadevan 							ret, mas->cur_speed_hz);
235561de45fSGirish Mahadevan 		return ret;
236561de45fSGirish Mahadevan 	}
237561de45fSGirish Mahadevan 
238561de45fSGirish Mahadevan 	clk_sel = idx & CLK_SEL_MSK;
239561de45fSGirish Mahadevan 	m_clk_cfg = (div << CLK_DIV_SHFT) | SER_CLK_EN;
240561de45fSGirish Mahadevan 	spi_setup_word_len(mas, spi_slv->mode, spi_slv->bits_per_word);
241561de45fSGirish Mahadevan 	writel(loopback_cfg, se->base + SE_SPI_LOOPBACK);
242561de45fSGirish Mahadevan 	writel(demux_sel, se->base + SE_SPI_DEMUX_SEL);
243561de45fSGirish Mahadevan 	writel(cpha, se->base + SE_SPI_CPHA);
244561de45fSGirish Mahadevan 	writel(cpol, se->base + SE_SPI_CPOL);
245561de45fSGirish Mahadevan 	writel(demux_output_inv, se->base + SE_SPI_DEMUX_OUTPUT_INV);
246561de45fSGirish Mahadevan 	writel(clk_sel, se->base + SE_GENI_CLK_SEL);
247561de45fSGirish Mahadevan 	writel(m_clk_cfg, se->base + GENI_SER_M_CLK_CFG);
248561de45fSGirish Mahadevan 	return 0;
249561de45fSGirish Mahadevan }
250561de45fSGirish Mahadevan 
251561de45fSGirish Mahadevan static int spi_geni_prepare_message(struct spi_master *spi,
252561de45fSGirish Mahadevan 					struct spi_message *spi_msg)
253561de45fSGirish Mahadevan {
254561de45fSGirish Mahadevan 	int ret;
255561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
256561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
257561de45fSGirish Mahadevan 
258561de45fSGirish Mahadevan 	geni_se_select_mode(se, GENI_SE_FIFO);
259561de45fSGirish Mahadevan 	ret = setup_fifo_params(spi_msg->spi, spi);
260561de45fSGirish Mahadevan 	if (ret)
261561de45fSGirish Mahadevan 		dev_err(mas->dev, "Couldn't select mode %d\n", ret);
262561de45fSGirish Mahadevan 	return ret;
263561de45fSGirish Mahadevan }
264561de45fSGirish Mahadevan 
265561de45fSGirish Mahadevan static int spi_geni_init(struct spi_geni_master *mas)
266561de45fSGirish Mahadevan {
267561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
268561de45fSGirish Mahadevan 	unsigned int proto, major, minor, ver;
269561de45fSGirish Mahadevan 
270561de45fSGirish Mahadevan 	pm_runtime_get_sync(mas->dev);
271561de45fSGirish Mahadevan 
272561de45fSGirish Mahadevan 	proto = geni_se_read_proto(se);
273561de45fSGirish Mahadevan 	if (proto != GENI_SE_SPI) {
274561de45fSGirish Mahadevan 		dev_err(mas->dev, "Invalid proto %d\n", proto);
275561de45fSGirish Mahadevan 		pm_runtime_put(mas->dev);
276561de45fSGirish Mahadevan 		return -ENXIO;
277561de45fSGirish Mahadevan 	}
278561de45fSGirish Mahadevan 	mas->tx_fifo_depth = geni_se_get_tx_fifo_depth(se);
279561de45fSGirish Mahadevan 
280561de45fSGirish Mahadevan 	/* Width of Tx and Rx FIFO is same */
281561de45fSGirish Mahadevan 	mas->fifo_width_bits = geni_se_get_tx_fifo_width(se);
282561de45fSGirish Mahadevan 
283561de45fSGirish Mahadevan 	/*
284561de45fSGirish Mahadevan 	 * Hardware programming guide suggests to configure
285561de45fSGirish Mahadevan 	 * RX FIFO RFR level to fifo_depth-2.
286561de45fSGirish Mahadevan 	 */
287561de45fSGirish Mahadevan 	geni_se_init(se, 0x0, mas->tx_fifo_depth - 2);
288561de45fSGirish Mahadevan 	/* Transmit an entire FIFO worth of data per IRQ */
289561de45fSGirish Mahadevan 	mas->tx_wm = 1;
290561de45fSGirish Mahadevan 	ver = geni_se_get_qup_hw_version(se);
291561de45fSGirish Mahadevan 	major = GENI_SE_VERSION_MAJOR(ver);
292561de45fSGirish Mahadevan 	minor = GENI_SE_VERSION_MINOR(ver);
293561de45fSGirish Mahadevan 
294561de45fSGirish Mahadevan 	if (major == 1 && minor == 0)
295561de45fSGirish Mahadevan 		mas->oversampling = 2;
296561de45fSGirish Mahadevan 	else
297561de45fSGirish Mahadevan 		mas->oversampling = 1;
298561de45fSGirish Mahadevan 
299561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
300561de45fSGirish Mahadevan 	return 0;
301561de45fSGirish Mahadevan }
302561de45fSGirish Mahadevan 
303561de45fSGirish Mahadevan static void setup_fifo_xfer(struct spi_transfer *xfer,
304561de45fSGirish Mahadevan 				struct spi_geni_master *mas,
305561de45fSGirish Mahadevan 				u16 mode, struct spi_master *spi)
306561de45fSGirish Mahadevan {
307561de45fSGirish Mahadevan 	u32 m_cmd = 0;
308561de45fSGirish Mahadevan 	u32 spi_tx_cfg, len;
309561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
310561de45fSGirish Mahadevan 
311561de45fSGirish Mahadevan 	spi_tx_cfg = readl(se->base + SE_SPI_TRANS_CFG);
312561de45fSGirish Mahadevan 	if (xfer->bits_per_word != mas->cur_bits_per_word) {
313561de45fSGirish Mahadevan 		spi_setup_word_len(mas, mode, xfer->bits_per_word);
314561de45fSGirish Mahadevan 		mas->cur_bits_per_word = xfer->bits_per_word;
315561de45fSGirish Mahadevan 	}
316561de45fSGirish Mahadevan 
317561de45fSGirish Mahadevan 	/* Speed and bits per word can be overridden per transfer */
318561de45fSGirish Mahadevan 	if (xfer->speed_hz != mas->cur_speed_hz) {
319561de45fSGirish Mahadevan 		int ret;
320561de45fSGirish Mahadevan 		u32 clk_sel, m_clk_cfg;
321561de45fSGirish Mahadevan 		unsigned int idx, div;
322561de45fSGirish Mahadevan 
323561de45fSGirish Mahadevan 		ret = get_spi_clk_cfg(xfer->speed_hz, mas, &idx, &div);
324561de45fSGirish Mahadevan 		if (ret) {
325561de45fSGirish Mahadevan 			dev_err(mas->dev, "Err setting clks:%d\n", ret);
326561de45fSGirish Mahadevan 			return;
327561de45fSGirish Mahadevan 		}
328561de45fSGirish Mahadevan 		/*
329561de45fSGirish Mahadevan 		 * SPI core clock gets configured with the requested frequency
330561de45fSGirish Mahadevan 		 * or the frequency closer to the requested frequency.
331561de45fSGirish Mahadevan 		 * For that reason requested frequency is stored in the
332561de45fSGirish Mahadevan 		 * cur_speed_hz and referred in the consecutive transfer instead
333561de45fSGirish Mahadevan 		 * of calling clk_get_rate() API.
334561de45fSGirish Mahadevan 		 */
335561de45fSGirish Mahadevan 		mas->cur_speed_hz = xfer->speed_hz;
336561de45fSGirish Mahadevan 		clk_sel = idx & CLK_SEL_MSK;
337561de45fSGirish Mahadevan 		m_clk_cfg = (div << CLK_DIV_SHFT) | SER_CLK_EN;
338561de45fSGirish Mahadevan 		writel(clk_sel, se->base + SE_GENI_CLK_SEL);
339561de45fSGirish Mahadevan 		writel(m_clk_cfg, se->base + GENI_SER_M_CLK_CFG);
340561de45fSGirish Mahadevan 	}
341561de45fSGirish Mahadevan 
342561de45fSGirish Mahadevan 	mas->tx_rem_bytes = 0;
343561de45fSGirish Mahadevan 	mas->rx_rem_bytes = 0;
344561de45fSGirish Mahadevan 	if (xfer->tx_buf && xfer->rx_buf)
345561de45fSGirish Mahadevan 		m_cmd = SPI_FULL_DUPLEX;
346561de45fSGirish Mahadevan 	else if (xfer->tx_buf)
347561de45fSGirish Mahadevan 		m_cmd = SPI_TX_ONLY;
348561de45fSGirish Mahadevan 	else if (xfer->rx_buf)
349561de45fSGirish Mahadevan 		m_cmd = SPI_RX_ONLY;
350561de45fSGirish Mahadevan 
351561de45fSGirish Mahadevan 	spi_tx_cfg &= ~CS_TOGGLE;
352561de45fSGirish Mahadevan 
353561de45fSGirish Mahadevan 	if (!(mas->cur_bits_per_word % MIN_WORD_LEN))
354561de45fSGirish Mahadevan 		len = xfer->len * BITS_PER_BYTE / mas->cur_bits_per_word;
355561de45fSGirish Mahadevan 	else
356561de45fSGirish Mahadevan 		len = xfer->len / (mas->cur_bits_per_word / BITS_PER_BYTE + 1);
357561de45fSGirish Mahadevan 	len &= TRANS_LEN_MSK;
358561de45fSGirish Mahadevan 
359561de45fSGirish Mahadevan 	mas->cur_xfer = xfer;
360561de45fSGirish Mahadevan 	if (m_cmd & SPI_TX_ONLY) {
361561de45fSGirish Mahadevan 		mas->tx_rem_bytes = xfer->len;
362561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_TX_TRANS_LEN);
363561de45fSGirish Mahadevan 	}
364561de45fSGirish Mahadevan 
365561de45fSGirish Mahadevan 	if (m_cmd & SPI_RX_ONLY) {
366561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_RX_TRANS_LEN);
367561de45fSGirish Mahadevan 		mas->rx_rem_bytes = xfer->len;
368561de45fSGirish Mahadevan 	}
369561de45fSGirish Mahadevan 	writel(spi_tx_cfg, se->base + SE_SPI_TRANS_CFG);
370561de45fSGirish Mahadevan 	mas->cur_mcmd = CMD_XFER;
371561de45fSGirish Mahadevan 	geni_se_setup_m_cmd(se, m_cmd, FRAGMENTATION);
372561de45fSGirish Mahadevan 
373561de45fSGirish Mahadevan 	/*
374561de45fSGirish Mahadevan 	 * TX_WATERMARK_REG should be set after SPI configuration and
375561de45fSGirish Mahadevan 	 * setting up GENI SE engine, as driver starts data transfer
376561de45fSGirish Mahadevan 	 * for the watermark interrupt.
377561de45fSGirish Mahadevan 	 */
378561de45fSGirish Mahadevan 	if (m_cmd & SPI_TX_ONLY)
379561de45fSGirish Mahadevan 		writel(mas->tx_wm, se->base + SE_GENI_TX_WATERMARK_REG);
380561de45fSGirish Mahadevan }
381561de45fSGirish Mahadevan 
382561de45fSGirish Mahadevan static int spi_geni_transfer_one(struct spi_master *spi,
383561de45fSGirish Mahadevan 				struct spi_device *slv,
384561de45fSGirish Mahadevan 				struct spi_transfer *xfer)
385561de45fSGirish Mahadevan {
386561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
387561de45fSGirish Mahadevan 
388561de45fSGirish Mahadevan 	/* Terminate and return success for 0 byte length transfer */
389561de45fSGirish Mahadevan 	if (!xfer->len)
390561de45fSGirish Mahadevan 		return 0;
391561de45fSGirish Mahadevan 
392561de45fSGirish Mahadevan 	setup_fifo_xfer(xfer, mas, slv->mode, spi);
393561de45fSGirish Mahadevan 	return 1;
394561de45fSGirish Mahadevan }
395561de45fSGirish Mahadevan 
396561de45fSGirish Mahadevan static unsigned int geni_byte_per_fifo_word(struct spi_geni_master *mas)
397561de45fSGirish Mahadevan {
398561de45fSGirish Mahadevan 	/*
399561de45fSGirish Mahadevan 	 * Calculate how many bytes we'll put in each FIFO word.  If the
400561de45fSGirish Mahadevan 	 * transfer words don't pack cleanly into a FIFO word we'll just put
401561de45fSGirish Mahadevan 	 * one transfer word in each FIFO word.  If they do pack we'll pack 'em.
402561de45fSGirish Mahadevan 	 */
403561de45fSGirish Mahadevan 	if (mas->fifo_width_bits % mas->cur_bits_per_word)
404561de45fSGirish Mahadevan 		return roundup_pow_of_two(DIV_ROUND_UP(mas->cur_bits_per_word,
405561de45fSGirish Mahadevan 						       BITS_PER_BYTE));
406561de45fSGirish Mahadevan 
407561de45fSGirish Mahadevan 	return mas->fifo_width_bits / BITS_PER_BYTE;
408561de45fSGirish Mahadevan }
409561de45fSGirish Mahadevan 
410561de45fSGirish Mahadevan static void geni_spi_handle_tx(struct spi_geni_master *mas)
411561de45fSGirish Mahadevan {
412561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
413561de45fSGirish Mahadevan 	unsigned int max_bytes;
414561de45fSGirish Mahadevan 	const u8 *tx_buf;
415561de45fSGirish Mahadevan 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
416561de45fSGirish Mahadevan 	unsigned int i = 0;
417561de45fSGirish Mahadevan 
418561de45fSGirish Mahadevan 	max_bytes = (mas->tx_fifo_depth - mas->tx_wm) * bytes_per_fifo_word;
419561de45fSGirish Mahadevan 	if (mas->tx_rem_bytes < max_bytes)
420561de45fSGirish Mahadevan 		max_bytes = mas->tx_rem_bytes;
421561de45fSGirish Mahadevan 
422561de45fSGirish Mahadevan 	tx_buf = mas->cur_xfer->tx_buf + mas->cur_xfer->len - mas->tx_rem_bytes;
423561de45fSGirish Mahadevan 	while (i < max_bytes) {
424561de45fSGirish Mahadevan 		unsigned int j;
425561de45fSGirish Mahadevan 		unsigned int bytes_to_write;
426561de45fSGirish Mahadevan 		u32 fifo_word = 0;
427561de45fSGirish Mahadevan 		u8 *fifo_byte = (u8 *)&fifo_word;
428561de45fSGirish Mahadevan 
429561de45fSGirish Mahadevan 		bytes_to_write = min(bytes_per_fifo_word, max_bytes - i);
430561de45fSGirish Mahadevan 		for (j = 0; j < bytes_to_write; j++)
431561de45fSGirish Mahadevan 			fifo_byte[j] = tx_buf[i++];
432561de45fSGirish Mahadevan 		iowrite32_rep(se->base + SE_GENI_TX_FIFOn, &fifo_word, 1);
433561de45fSGirish Mahadevan 	}
434561de45fSGirish Mahadevan 	mas->tx_rem_bytes -= max_bytes;
435561de45fSGirish Mahadevan 	if (!mas->tx_rem_bytes)
436561de45fSGirish Mahadevan 		writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
437561de45fSGirish Mahadevan }
438561de45fSGirish Mahadevan 
439561de45fSGirish Mahadevan static void geni_spi_handle_rx(struct spi_geni_master *mas)
440561de45fSGirish Mahadevan {
441561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
442561de45fSGirish Mahadevan 	u32 rx_fifo_status;
443561de45fSGirish Mahadevan 	unsigned int rx_bytes;
444561de45fSGirish Mahadevan 	unsigned int rx_last_byte_valid;
445561de45fSGirish Mahadevan 	u8 *rx_buf;
446561de45fSGirish Mahadevan 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
447561de45fSGirish Mahadevan 	unsigned int i = 0;
448561de45fSGirish Mahadevan 
449561de45fSGirish Mahadevan 	rx_fifo_status = readl(se->base + SE_GENI_RX_FIFO_STATUS);
450561de45fSGirish Mahadevan 	rx_bytes = (rx_fifo_status & RX_FIFO_WC_MSK) * bytes_per_fifo_word;
451561de45fSGirish Mahadevan 	if (rx_fifo_status & RX_LAST) {
452561de45fSGirish Mahadevan 		rx_last_byte_valid = rx_fifo_status & RX_LAST_BYTE_VALID_MSK;
453561de45fSGirish Mahadevan 		rx_last_byte_valid >>= RX_LAST_BYTE_VALID_SHFT;
454561de45fSGirish Mahadevan 		if (rx_last_byte_valid && rx_last_byte_valid < 4)
455561de45fSGirish Mahadevan 			rx_bytes -= bytes_per_fifo_word - rx_last_byte_valid;
456561de45fSGirish Mahadevan 	}
457561de45fSGirish Mahadevan 	if (mas->rx_rem_bytes < rx_bytes)
458561de45fSGirish Mahadevan 		rx_bytes = mas->rx_rem_bytes;
459561de45fSGirish Mahadevan 
460561de45fSGirish Mahadevan 	rx_buf = mas->cur_xfer->rx_buf + mas->cur_xfer->len - mas->rx_rem_bytes;
461561de45fSGirish Mahadevan 	while (i < rx_bytes) {
462561de45fSGirish Mahadevan 		u32 fifo_word = 0;
463561de45fSGirish Mahadevan 		u8 *fifo_byte = (u8 *)&fifo_word;
464561de45fSGirish Mahadevan 		unsigned int bytes_to_read;
465561de45fSGirish Mahadevan 		unsigned int j;
466561de45fSGirish Mahadevan 
467561de45fSGirish Mahadevan 		bytes_to_read = min(bytes_per_fifo_word, rx_bytes - i);
468561de45fSGirish Mahadevan 		ioread32_rep(se->base + SE_GENI_RX_FIFOn, &fifo_word, 1);
469561de45fSGirish Mahadevan 		for (j = 0; j < bytes_to_read; j++)
470561de45fSGirish Mahadevan 			rx_buf[i++] = fifo_byte[j];
471561de45fSGirish Mahadevan 	}
472561de45fSGirish Mahadevan 	mas->rx_rem_bytes -= rx_bytes;
473561de45fSGirish Mahadevan }
474561de45fSGirish Mahadevan 
475561de45fSGirish Mahadevan static irqreturn_t geni_spi_isr(int irq, void *data)
476561de45fSGirish Mahadevan {
477561de45fSGirish Mahadevan 	struct spi_master *spi = data;
478561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
479561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
480561de45fSGirish Mahadevan 	u32 m_irq;
481561de45fSGirish Mahadevan 	unsigned long flags;
482561de45fSGirish Mahadevan 
483561de45fSGirish Mahadevan 	if (mas->cur_mcmd == CMD_NONE)
484561de45fSGirish Mahadevan 		return IRQ_NONE;
485561de45fSGirish Mahadevan 
486561de45fSGirish Mahadevan 	spin_lock_irqsave(&mas->lock, flags);
487561de45fSGirish Mahadevan 	m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
488561de45fSGirish Mahadevan 
489561de45fSGirish Mahadevan 	if ((m_irq & M_RX_FIFO_WATERMARK_EN) || (m_irq & M_RX_FIFO_LAST_EN))
490561de45fSGirish Mahadevan 		geni_spi_handle_rx(mas);
491561de45fSGirish Mahadevan 
492561de45fSGirish Mahadevan 	if (m_irq & M_TX_FIFO_WATERMARK_EN)
493561de45fSGirish Mahadevan 		geni_spi_handle_tx(mas);
494561de45fSGirish Mahadevan 
495561de45fSGirish Mahadevan 	if (m_irq & M_CMD_DONE_EN) {
496561de45fSGirish Mahadevan 		if (mas->cur_mcmd == CMD_XFER)
497561de45fSGirish Mahadevan 			spi_finalize_current_transfer(spi);
498561de45fSGirish Mahadevan 		else if (mas->cur_mcmd == CMD_CS)
499561de45fSGirish Mahadevan 			complete(&mas->xfer_done);
500561de45fSGirish Mahadevan 		mas->cur_mcmd = CMD_NONE;
501561de45fSGirish Mahadevan 		/*
502561de45fSGirish Mahadevan 		 * If this happens, then a CMD_DONE came before all the Tx
503561de45fSGirish Mahadevan 		 * buffer bytes were sent out. This is unusual, log this
504561de45fSGirish Mahadevan 		 * condition and disable the WM interrupt to prevent the
505561de45fSGirish Mahadevan 		 * system from stalling due an interrupt storm.
506561de45fSGirish Mahadevan 		 * If this happens when all Rx bytes haven't been received, log
507561de45fSGirish Mahadevan 		 * the condition.
508561de45fSGirish Mahadevan 		 * The only known time this can happen is if bits_per_word != 8
509561de45fSGirish Mahadevan 		 * and some registers that expect xfer lengths in num spi_words
510561de45fSGirish Mahadevan 		 * weren't written correctly.
511561de45fSGirish Mahadevan 		 */
512561de45fSGirish Mahadevan 		if (mas->tx_rem_bytes) {
513561de45fSGirish Mahadevan 			writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
514561de45fSGirish Mahadevan 			dev_err(mas->dev, "Premature done. tx_rem = %d bpw%d\n",
515561de45fSGirish Mahadevan 				mas->tx_rem_bytes, mas->cur_bits_per_word);
516561de45fSGirish Mahadevan 		}
517561de45fSGirish Mahadevan 		if (mas->rx_rem_bytes)
518561de45fSGirish Mahadevan 			dev_err(mas->dev, "Premature done. rx_rem = %d bpw%d\n",
519561de45fSGirish Mahadevan 				mas->rx_rem_bytes, mas->cur_bits_per_word);
520561de45fSGirish Mahadevan 	}
521561de45fSGirish Mahadevan 
522561de45fSGirish Mahadevan 	if ((m_irq & M_CMD_CANCEL_EN) || (m_irq & M_CMD_ABORT_EN)) {
523561de45fSGirish Mahadevan 		mas->cur_mcmd = CMD_NONE;
524561de45fSGirish Mahadevan 		complete(&mas->xfer_done);
525561de45fSGirish Mahadevan 	}
526561de45fSGirish Mahadevan 
527561de45fSGirish Mahadevan 	writel(m_irq, se->base + SE_GENI_M_IRQ_CLEAR);
528561de45fSGirish Mahadevan 	spin_unlock_irqrestore(&mas->lock, flags);
5290dccff3cSAlok Chauhan 	return IRQ_HANDLED;
530561de45fSGirish Mahadevan }
531561de45fSGirish Mahadevan 
532561de45fSGirish Mahadevan static int spi_geni_probe(struct platform_device *pdev)
533561de45fSGirish Mahadevan {
5346a34e285SAlok Chauhan 	int ret, irq;
535561de45fSGirish Mahadevan 	struct spi_master *spi;
536561de45fSGirish Mahadevan 	struct spi_geni_master *mas;
537561de45fSGirish Mahadevan 	struct resource *res;
5386a34e285SAlok Chauhan 	void __iomem *base;
5396a34e285SAlok Chauhan 	struct clk *clk;
5406a34e285SAlok Chauhan 
5416a34e285SAlok Chauhan 	irq = platform_get_irq(pdev, 0);
5426a34e285SAlok Chauhan 	if (irq < 0) {
5436a34e285SAlok Chauhan 		dev_err(&pdev->dev, "Err getting IRQ %d\n", irq);
5446a34e285SAlok Chauhan 		return irq;
5456a34e285SAlok Chauhan 	}
5466a34e285SAlok Chauhan 
5476a34e285SAlok Chauhan 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5486a34e285SAlok Chauhan 	base = devm_ioremap_resource(&pdev->dev, res);
5496a34e285SAlok Chauhan 	if (IS_ERR(base))
5506a34e285SAlok Chauhan 		return PTR_ERR(base);
5516a34e285SAlok Chauhan 
5526a34e285SAlok Chauhan 	clk = devm_clk_get(&pdev->dev, "se");
5536a34e285SAlok Chauhan 	if (IS_ERR(clk)) {
5546a34e285SAlok Chauhan 		dev_err(&pdev->dev, "Err getting SE Core clk %ld\n",
5556a34e285SAlok Chauhan 						PTR_ERR(clk));
5566a34e285SAlok Chauhan 		return PTR_ERR(clk);
5576a34e285SAlok Chauhan 	}
558561de45fSGirish Mahadevan 
559561de45fSGirish Mahadevan 	spi = spi_alloc_master(&pdev->dev, sizeof(*mas));
560561de45fSGirish Mahadevan 	if (!spi)
561561de45fSGirish Mahadevan 		return -ENOMEM;
562561de45fSGirish Mahadevan 
563561de45fSGirish Mahadevan 	platform_set_drvdata(pdev, spi);
564561de45fSGirish Mahadevan 	mas = spi_master_get_devdata(spi);
5656a34e285SAlok Chauhan 	mas->irq = irq;
566561de45fSGirish Mahadevan 	mas->dev = &pdev->dev;
567561de45fSGirish Mahadevan 	mas->se.dev = &pdev->dev;
568561de45fSGirish Mahadevan 	mas->se.wrapper = dev_get_drvdata(pdev->dev.parent);
5696a34e285SAlok Chauhan 	mas->se.base = base;
5706a34e285SAlok Chauhan 	mas->se.clk = clk;
571561de45fSGirish Mahadevan 
572561de45fSGirish Mahadevan 	spi->bus_num = -1;
573561de45fSGirish Mahadevan 	spi->dev.of_node = pdev->dev.of_node;
574561de45fSGirish Mahadevan 	spi->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_CS_HIGH;
575561de45fSGirish Mahadevan 	spi->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
576561de45fSGirish Mahadevan 	spi->num_chipselect = 4;
577561de45fSGirish Mahadevan 	spi->max_speed_hz = 50000000;
578561de45fSGirish Mahadevan 	spi->prepare_message = spi_geni_prepare_message;
579561de45fSGirish Mahadevan 	spi->transfer_one = spi_geni_transfer_one;
580561de45fSGirish Mahadevan 	spi->auto_runtime_pm = true;
581561de45fSGirish Mahadevan 	spi->handle_err = handle_fifo_timeout;
582561de45fSGirish Mahadevan 	spi->set_cs = spi_geni_set_cs;
583561de45fSGirish Mahadevan 
584561de45fSGirish Mahadevan 	init_completion(&mas->xfer_done);
585561de45fSGirish Mahadevan 	spin_lock_init(&mas->lock);
586561de45fSGirish Mahadevan 	pm_runtime_enable(&pdev->dev);
587561de45fSGirish Mahadevan 
588561de45fSGirish Mahadevan 	ret = spi_geni_init(mas);
589561de45fSGirish Mahadevan 	if (ret)
590561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
591561de45fSGirish Mahadevan 
592561de45fSGirish Mahadevan 	ret = request_irq(mas->irq, geni_spi_isr,
593561de45fSGirish Mahadevan 			IRQF_TRIGGER_HIGH, "spi_geni", spi);
594561de45fSGirish Mahadevan 	if (ret)
595561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
596561de45fSGirish Mahadevan 
597561de45fSGirish Mahadevan 	ret = spi_register_master(spi);
598561de45fSGirish Mahadevan 	if (ret)
599561de45fSGirish Mahadevan 		goto spi_geni_probe_free_irq;
600561de45fSGirish Mahadevan 
601561de45fSGirish Mahadevan 	return 0;
602561de45fSGirish Mahadevan spi_geni_probe_free_irq:
603561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
604561de45fSGirish Mahadevan spi_geni_probe_runtime_disable:
605561de45fSGirish Mahadevan 	pm_runtime_disable(&pdev->dev);
606561de45fSGirish Mahadevan 	spi_master_put(spi);
607561de45fSGirish Mahadevan 	return ret;
608561de45fSGirish Mahadevan }
609561de45fSGirish Mahadevan 
610561de45fSGirish Mahadevan static int spi_geni_remove(struct platform_device *pdev)
611561de45fSGirish Mahadevan {
612561de45fSGirish Mahadevan 	struct spi_master *spi = platform_get_drvdata(pdev);
613561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
614561de45fSGirish Mahadevan 
615561de45fSGirish Mahadevan 	/* Unregister _before_ disabling pm_runtime() so we stop transfers */
616561de45fSGirish Mahadevan 	spi_unregister_master(spi);
617561de45fSGirish Mahadevan 
618561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
619561de45fSGirish Mahadevan 	pm_runtime_disable(&pdev->dev);
620561de45fSGirish Mahadevan 	return 0;
621561de45fSGirish Mahadevan }
622561de45fSGirish Mahadevan 
623561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_suspend(struct device *dev)
624561de45fSGirish Mahadevan {
625561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
626561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
627561de45fSGirish Mahadevan 
628561de45fSGirish Mahadevan 	return geni_se_resources_off(&mas->se);
629561de45fSGirish Mahadevan }
630561de45fSGirish Mahadevan 
631561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_resume(struct device *dev)
632561de45fSGirish Mahadevan {
633561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
634561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
635561de45fSGirish Mahadevan 
636561de45fSGirish Mahadevan 	return geni_se_resources_on(&mas->se);
637561de45fSGirish Mahadevan }
638561de45fSGirish Mahadevan 
639561de45fSGirish Mahadevan static int __maybe_unused spi_geni_suspend(struct device *dev)
640561de45fSGirish Mahadevan {
641561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
642561de45fSGirish Mahadevan 	int ret;
643561de45fSGirish Mahadevan 
644561de45fSGirish Mahadevan 	ret = spi_master_suspend(spi);
645561de45fSGirish Mahadevan 	if (ret)
646561de45fSGirish Mahadevan 		return ret;
647561de45fSGirish Mahadevan 
648561de45fSGirish Mahadevan 	ret = pm_runtime_force_suspend(dev);
649561de45fSGirish Mahadevan 	if (ret)
650561de45fSGirish Mahadevan 		spi_master_resume(spi);
651561de45fSGirish Mahadevan 
652561de45fSGirish Mahadevan 	return ret;
653561de45fSGirish Mahadevan }
654561de45fSGirish Mahadevan 
655561de45fSGirish Mahadevan static int __maybe_unused spi_geni_resume(struct device *dev)
656561de45fSGirish Mahadevan {
657561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
658561de45fSGirish Mahadevan 	int ret;
659561de45fSGirish Mahadevan 
660561de45fSGirish Mahadevan 	ret = pm_runtime_force_resume(dev);
661561de45fSGirish Mahadevan 	if (ret)
662561de45fSGirish Mahadevan 		return ret;
663561de45fSGirish Mahadevan 
664561de45fSGirish Mahadevan 	ret = spi_master_resume(spi);
665561de45fSGirish Mahadevan 	if (ret)
666561de45fSGirish Mahadevan 		pm_runtime_force_suspend(dev);
667561de45fSGirish Mahadevan 
668561de45fSGirish Mahadevan 	return ret;
669561de45fSGirish Mahadevan }
670561de45fSGirish Mahadevan 
671561de45fSGirish Mahadevan static const struct dev_pm_ops spi_geni_pm_ops = {
672561de45fSGirish Mahadevan 	SET_RUNTIME_PM_OPS(spi_geni_runtime_suspend,
673561de45fSGirish Mahadevan 					spi_geni_runtime_resume, NULL)
674561de45fSGirish Mahadevan 	SET_SYSTEM_SLEEP_PM_OPS(spi_geni_suspend, spi_geni_resume)
675561de45fSGirish Mahadevan };
676561de45fSGirish Mahadevan 
677561de45fSGirish Mahadevan static const struct of_device_id spi_geni_dt_match[] = {
678561de45fSGirish Mahadevan 	{ .compatible = "qcom,geni-spi" },
679561de45fSGirish Mahadevan 	{}
680561de45fSGirish Mahadevan };
681561de45fSGirish Mahadevan MODULE_DEVICE_TABLE(of, spi_geni_dt_match);
682561de45fSGirish Mahadevan 
683561de45fSGirish Mahadevan static struct platform_driver spi_geni_driver = {
684561de45fSGirish Mahadevan 	.probe  = spi_geni_probe,
685561de45fSGirish Mahadevan 	.remove = spi_geni_remove,
686561de45fSGirish Mahadevan 	.driver = {
687561de45fSGirish Mahadevan 		.name = "geni_spi",
688561de45fSGirish Mahadevan 		.pm = &spi_geni_pm_ops,
689561de45fSGirish Mahadevan 		.of_match_table = spi_geni_dt_match,
690561de45fSGirish Mahadevan 	},
691561de45fSGirish Mahadevan };
692561de45fSGirish Mahadevan module_platform_driver(spi_geni_driver);
693561de45fSGirish Mahadevan 
694561de45fSGirish Mahadevan MODULE_DESCRIPTION("SPI driver for GENI based QUP cores");
695561de45fSGirish Mahadevan MODULE_LICENSE("GPL v2");
696