xref: /linux/drivers/spi/spi-geni-qcom.c (revision 6a34e285de9afc4441ee81ce76865acd30ed9263)
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 void handle_fifo_timeout(struct spi_master *spi,
93561de45fSGirish Mahadevan 				struct spi_message *msg);
94561de45fSGirish Mahadevan 
95561de45fSGirish Mahadevan static int get_spi_clk_cfg(unsigned int speed_hz,
96561de45fSGirish Mahadevan 			struct spi_geni_master *mas,
97561de45fSGirish Mahadevan 			unsigned int *clk_idx,
98561de45fSGirish Mahadevan 			unsigned int *clk_div)
99561de45fSGirish Mahadevan {
100561de45fSGirish Mahadevan 	unsigned long sclk_freq;
101561de45fSGirish Mahadevan 	unsigned int actual_hz;
102561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
103561de45fSGirish Mahadevan 	int ret;
104561de45fSGirish Mahadevan 
105561de45fSGirish Mahadevan 	ret = geni_se_clk_freq_match(&mas->se,
106561de45fSGirish Mahadevan 				speed_hz * mas->oversampling,
107561de45fSGirish Mahadevan 				clk_idx, &sclk_freq, false);
108561de45fSGirish Mahadevan 	if (ret) {
109561de45fSGirish Mahadevan 		dev_err(mas->dev, "Failed(%d) to find src clk for %dHz\n",
110561de45fSGirish Mahadevan 							ret, speed_hz);
111561de45fSGirish Mahadevan 		return ret;
112561de45fSGirish Mahadevan 	}
113561de45fSGirish Mahadevan 
114561de45fSGirish Mahadevan 	*clk_div = DIV_ROUND_UP(sclk_freq, mas->oversampling * speed_hz);
115561de45fSGirish Mahadevan 	actual_hz = sclk_freq / (mas->oversampling * *clk_div);
116561de45fSGirish Mahadevan 
117561de45fSGirish Mahadevan 	dev_dbg(mas->dev, "req %u=>%u sclk %lu, idx %d, div %d\n", speed_hz,
118561de45fSGirish Mahadevan 				actual_hz, sclk_freq, *clk_idx, *clk_div);
119561de45fSGirish Mahadevan 	ret = clk_set_rate(se->clk, sclk_freq);
120561de45fSGirish Mahadevan 	if (ret)
121561de45fSGirish Mahadevan 		dev_err(mas->dev, "clk_set_rate failed %d\n", ret);
122561de45fSGirish Mahadevan 	return ret;
123561de45fSGirish Mahadevan }
124561de45fSGirish Mahadevan 
125561de45fSGirish Mahadevan static void spi_geni_set_cs(struct spi_device *slv, bool set_flag)
126561de45fSGirish Mahadevan {
127561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(slv->master);
128561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(mas->dev);
129561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
1300dccff3cSAlok Chauhan 	unsigned long time_left;
131561de45fSGirish Mahadevan 
132561de45fSGirish Mahadevan 	reinit_completion(&mas->xfer_done);
133561de45fSGirish Mahadevan 	pm_runtime_get_sync(mas->dev);
134561de45fSGirish Mahadevan 	if (!(slv->mode & SPI_CS_HIGH))
135561de45fSGirish Mahadevan 		set_flag = !set_flag;
136561de45fSGirish Mahadevan 
137561de45fSGirish Mahadevan 	mas->cur_mcmd = CMD_CS;
138561de45fSGirish Mahadevan 	if (set_flag)
139561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_ASSERT, 0);
140561de45fSGirish Mahadevan 	else
141561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_DEASSERT, 0);
142561de45fSGirish Mahadevan 
1430dccff3cSAlok Chauhan 	time_left = wait_for_completion_timeout(&mas->xfer_done, HZ);
1440dccff3cSAlok Chauhan 	if (!time_left)
145561de45fSGirish Mahadevan 		handle_fifo_timeout(spi, NULL);
146561de45fSGirish Mahadevan 
147561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
148561de45fSGirish Mahadevan }
149561de45fSGirish Mahadevan 
150561de45fSGirish Mahadevan static void spi_setup_word_len(struct spi_geni_master *mas, u16 mode,
151561de45fSGirish Mahadevan 					unsigned int bits_per_word)
152561de45fSGirish Mahadevan {
153561de45fSGirish Mahadevan 	unsigned int pack_words;
154561de45fSGirish Mahadevan 	bool msb_first = (mode & SPI_LSB_FIRST) ? false : true;
155561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
156561de45fSGirish Mahadevan 	u32 word_len;
157561de45fSGirish Mahadevan 
158561de45fSGirish Mahadevan 	word_len = readl(se->base + SE_SPI_WORD_LEN);
159561de45fSGirish Mahadevan 
160561de45fSGirish Mahadevan 	/*
161561de45fSGirish Mahadevan 	 * If bits_per_word isn't a byte aligned value, set the packing to be
162561de45fSGirish Mahadevan 	 * 1 SPI word per FIFO word.
163561de45fSGirish Mahadevan 	 */
164561de45fSGirish Mahadevan 	if (!(mas->fifo_width_bits % bits_per_word))
165561de45fSGirish Mahadevan 		pack_words = mas->fifo_width_bits / bits_per_word;
166561de45fSGirish Mahadevan 	else
167561de45fSGirish Mahadevan 		pack_words = 1;
168561de45fSGirish Mahadevan 	word_len &= ~WORD_LEN_MSK;
169561de45fSGirish Mahadevan 	word_len |= ((bits_per_word - MIN_WORD_LEN) & WORD_LEN_MSK);
170561de45fSGirish Mahadevan 	geni_se_config_packing(&mas->se, bits_per_word, pack_words, msb_first,
171561de45fSGirish Mahadevan 								true, true);
172561de45fSGirish Mahadevan 	writel(word_len, se->base + SE_SPI_WORD_LEN);
173561de45fSGirish Mahadevan }
174561de45fSGirish Mahadevan 
175561de45fSGirish Mahadevan static int setup_fifo_params(struct spi_device *spi_slv,
176561de45fSGirish Mahadevan 					struct spi_master *spi)
177561de45fSGirish Mahadevan {
178561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
179561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
180561de45fSGirish Mahadevan 	u32 loopback_cfg, cpol, cpha, demux_output_inv;
181561de45fSGirish Mahadevan 	u32 demux_sel, clk_sel, m_clk_cfg, idx, div;
182561de45fSGirish Mahadevan 	int ret;
183561de45fSGirish Mahadevan 
184561de45fSGirish Mahadevan 	loopback_cfg = readl(se->base + SE_SPI_LOOPBACK);
185561de45fSGirish Mahadevan 	cpol = readl(se->base + SE_SPI_CPOL);
186561de45fSGirish Mahadevan 	cpha = readl(se->base + SE_SPI_CPHA);
187561de45fSGirish Mahadevan 	demux_output_inv = 0;
188561de45fSGirish Mahadevan 	loopback_cfg &= ~LOOPBACK_MSK;
189561de45fSGirish Mahadevan 	cpol &= ~CPOL;
190561de45fSGirish Mahadevan 	cpha &= ~CPHA;
191561de45fSGirish Mahadevan 
192561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_LOOP)
193561de45fSGirish Mahadevan 		loopback_cfg |= LOOPBACK_ENABLE;
194561de45fSGirish Mahadevan 
195561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CPOL)
196561de45fSGirish Mahadevan 		cpol |= CPOL;
197561de45fSGirish Mahadevan 
198561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CPHA)
199561de45fSGirish Mahadevan 		cpha |= CPHA;
200561de45fSGirish Mahadevan 
201561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CS_HIGH)
202561de45fSGirish Mahadevan 		demux_output_inv = BIT(spi_slv->chip_select);
203561de45fSGirish Mahadevan 
204561de45fSGirish Mahadevan 	demux_sel = spi_slv->chip_select;
205561de45fSGirish Mahadevan 	mas->cur_speed_hz = spi_slv->max_speed_hz;
206561de45fSGirish Mahadevan 	mas->cur_bits_per_word = spi_slv->bits_per_word;
207561de45fSGirish Mahadevan 
208561de45fSGirish Mahadevan 	ret = get_spi_clk_cfg(mas->cur_speed_hz, mas, &idx, &div);
209561de45fSGirish Mahadevan 	if (ret) {
210561de45fSGirish Mahadevan 		dev_err(mas->dev, "Err setting clks ret(%d) for %ld\n",
211561de45fSGirish Mahadevan 							ret, mas->cur_speed_hz);
212561de45fSGirish Mahadevan 		return ret;
213561de45fSGirish Mahadevan 	}
214561de45fSGirish Mahadevan 
215561de45fSGirish Mahadevan 	clk_sel = idx & CLK_SEL_MSK;
216561de45fSGirish Mahadevan 	m_clk_cfg = (div << CLK_DIV_SHFT) | SER_CLK_EN;
217561de45fSGirish Mahadevan 	spi_setup_word_len(mas, spi_slv->mode, spi_slv->bits_per_word);
218561de45fSGirish Mahadevan 	writel(loopback_cfg, se->base + SE_SPI_LOOPBACK);
219561de45fSGirish Mahadevan 	writel(demux_sel, se->base + SE_SPI_DEMUX_SEL);
220561de45fSGirish Mahadevan 	writel(cpha, se->base + SE_SPI_CPHA);
221561de45fSGirish Mahadevan 	writel(cpol, se->base + SE_SPI_CPOL);
222561de45fSGirish Mahadevan 	writel(demux_output_inv, se->base + SE_SPI_DEMUX_OUTPUT_INV);
223561de45fSGirish Mahadevan 	writel(clk_sel, se->base + SE_GENI_CLK_SEL);
224561de45fSGirish Mahadevan 	writel(m_clk_cfg, se->base + GENI_SER_M_CLK_CFG);
225561de45fSGirish Mahadevan 	return 0;
226561de45fSGirish Mahadevan }
227561de45fSGirish Mahadevan 
228561de45fSGirish Mahadevan static int spi_geni_prepare_message(struct spi_master *spi,
229561de45fSGirish Mahadevan 					struct spi_message *spi_msg)
230561de45fSGirish Mahadevan {
231561de45fSGirish Mahadevan 	int ret;
232561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
233561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
234561de45fSGirish Mahadevan 
235561de45fSGirish Mahadevan 	geni_se_select_mode(se, GENI_SE_FIFO);
236561de45fSGirish Mahadevan 	reinit_completion(&mas->xfer_done);
237561de45fSGirish Mahadevan 	ret = setup_fifo_params(spi_msg->spi, spi);
238561de45fSGirish Mahadevan 	if (ret)
239561de45fSGirish Mahadevan 		dev_err(mas->dev, "Couldn't select mode %d\n", ret);
240561de45fSGirish Mahadevan 	return ret;
241561de45fSGirish Mahadevan }
242561de45fSGirish Mahadevan 
243561de45fSGirish Mahadevan static int spi_geni_init(struct spi_geni_master *mas)
244561de45fSGirish Mahadevan {
245561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
246561de45fSGirish Mahadevan 	unsigned int proto, major, minor, ver;
247561de45fSGirish Mahadevan 
248561de45fSGirish Mahadevan 	pm_runtime_get_sync(mas->dev);
249561de45fSGirish Mahadevan 
250561de45fSGirish Mahadevan 	proto = geni_se_read_proto(se);
251561de45fSGirish Mahadevan 	if (proto != GENI_SE_SPI) {
252561de45fSGirish Mahadevan 		dev_err(mas->dev, "Invalid proto %d\n", proto);
253561de45fSGirish Mahadevan 		pm_runtime_put(mas->dev);
254561de45fSGirish Mahadevan 		return -ENXIO;
255561de45fSGirish Mahadevan 	}
256561de45fSGirish Mahadevan 	mas->tx_fifo_depth = geni_se_get_tx_fifo_depth(se);
257561de45fSGirish Mahadevan 
258561de45fSGirish Mahadevan 	/* Width of Tx and Rx FIFO is same */
259561de45fSGirish Mahadevan 	mas->fifo_width_bits = geni_se_get_tx_fifo_width(se);
260561de45fSGirish Mahadevan 
261561de45fSGirish Mahadevan 	/*
262561de45fSGirish Mahadevan 	 * Hardware programming guide suggests to configure
263561de45fSGirish Mahadevan 	 * RX FIFO RFR level to fifo_depth-2.
264561de45fSGirish Mahadevan 	 */
265561de45fSGirish Mahadevan 	geni_se_init(se, 0x0, mas->tx_fifo_depth - 2);
266561de45fSGirish Mahadevan 	/* Transmit an entire FIFO worth of data per IRQ */
267561de45fSGirish Mahadevan 	mas->tx_wm = 1;
268561de45fSGirish Mahadevan 	ver = geni_se_get_qup_hw_version(se);
269561de45fSGirish Mahadevan 	major = GENI_SE_VERSION_MAJOR(ver);
270561de45fSGirish Mahadevan 	minor = GENI_SE_VERSION_MINOR(ver);
271561de45fSGirish Mahadevan 
272561de45fSGirish Mahadevan 	if (major == 1 && minor == 0)
273561de45fSGirish Mahadevan 		mas->oversampling = 2;
274561de45fSGirish Mahadevan 	else
275561de45fSGirish Mahadevan 		mas->oversampling = 1;
276561de45fSGirish Mahadevan 
277561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
278561de45fSGirish Mahadevan 	return 0;
279561de45fSGirish Mahadevan }
280561de45fSGirish Mahadevan 
281561de45fSGirish Mahadevan static void setup_fifo_xfer(struct spi_transfer *xfer,
282561de45fSGirish Mahadevan 				struct spi_geni_master *mas,
283561de45fSGirish Mahadevan 				u16 mode, struct spi_master *spi)
284561de45fSGirish Mahadevan {
285561de45fSGirish Mahadevan 	u32 m_cmd = 0;
286561de45fSGirish Mahadevan 	u32 spi_tx_cfg, len;
287561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
288561de45fSGirish Mahadevan 
289561de45fSGirish Mahadevan 	spi_tx_cfg = readl(se->base + SE_SPI_TRANS_CFG);
290561de45fSGirish Mahadevan 	if (xfer->bits_per_word != mas->cur_bits_per_word) {
291561de45fSGirish Mahadevan 		spi_setup_word_len(mas, mode, xfer->bits_per_word);
292561de45fSGirish Mahadevan 		mas->cur_bits_per_word = xfer->bits_per_word;
293561de45fSGirish Mahadevan 	}
294561de45fSGirish Mahadevan 
295561de45fSGirish Mahadevan 	/* Speed and bits per word can be overridden per transfer */
296561de45fSGirish Mahadevan 	if (xfer->speed_hz != mas->cur_speed_hz) {
297561de45fSGirish Mahadevan 		int ret;
298561de45fSGirish Mahadevan 		u32 clk_sel, m_clk_cfg;
299561de45fSGirish Mahadevan 		unsigned int idx, div;
300561de45fSGirish Mahadevan 
301561de45fSGirish Mahadevan 		ret = get_spi_clk_cfg(xfer->speed_hz, mas, &idx, &div);
302561de45fSGirish Mahadevan 		if (ret) {
303561de45fSGirish Mahadevan 			dev_err(mas->dev, "Err setting clks:%d\n", ret);
304561de45fSGirish Mahadevan 			return;
305561de45fSGirish Mahadevan 		}
306561de45fSGirish Mahadevan 		/*
307561de45fSGirish Mahadevan 		 * SPI core clock gets configured with the requested frequency
308561de45fSGirish Mahadevan 		 * or the frequency closer to the requested frequency.
309561de45fSGirish Mahadevan 		 * For that reason requested frequency is stored in the
310561de45fSGirish Mahadevan 		 * cur_speed_hz and referred in the consecutive transfer instead
311561de45fSGirish Mahadevan 		 * of calling clk_get_rate() API.
312561de45fSGirish Mahadevan 		 */
313561de45fSGirish Mahadevan 		mas->cur_speed_hz = xfer->speed_hz;
314561de45fSGirish Mahadevan 		clk_sel = idx & CLK_SEL_MSK;
315561de45fSGirish Mahadevan 		m_clk_cfg = (div << CLK_DIV_SHFT) | SER_CLK_EN;
316561de45fSGirish Mahadevan 		writel(clk_sel, se->base + SE_GENI_CLK_SEL);
317561de45fSGirish Mahadevan 		writel(m_clk_cfg, se->base + GENI_SER_M_CLK_CFG);
318561de45fSGirish Mahadevan 	}
319561de45fSGirish Mahadevan 
320561de45fSGirish Mahadevan 	mas->tx_rem_bytes = 0;
321561de45fSGirish Mahadevan 	mas->rx_rem_bytes = 0;
322561de45fSGirish Mahadevan 	if (xfer->tx_buf && xfer->rx_buf)
323561de45fSGirish Mahadevan 		m_cmd = SPI_FULL_DUPLEX;
324561de45fSGirish Mahadevan 	else if (xfer->tx_buf)
325561de45fSGirish Mahadevan 		m_cmd = SPI_TX_ONLY;
326561de45fSGirish Mahadevan 	else if (xfer->rx_buf)
327561de45fSGirish Mahadevan 		m_cmd = SPI_RX_ONLY;
328561de45fSGirish Mahadevan 
329561de45fSGirish Mahadevan 	spi_tx_cfg &= ~CS_TOGGLE;
330561de45fSGirish Mahadevan 
331561de45fSGirish Mahadevan 	if (!(mas->cur_bits_per_word % MIN_WORD_LEN))
332561de45fSGirish Mahadevan 		len = xfer->len * BITS_PER_BYTE / mas->cur_bits_per_word;
333561de45fSGirish Mahadevan 	else
334561de45fSGirish Mahadevan 		len = xfer->len / (mas->cur_bits_per_word / BITS_PER_BYTE + 1);
335561de45fSGirish Mahadevan 	len &= TRANS_LEN_MSK;
336561de45fSGirish Mahadevan 
337561de45fSGirish Mahadevan 	mas->cur_xfer = xfer;
338561de45fSGirish Mahadevan 	if (m_cmd & SPI_TX_ONLY) {
339561de45fSGirish Mahadevan 		mas->tx_rem_bytes = xfer->len;
340561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_TX_TRANS_LEN);
341561de45fSGirish Mahadevan 	}
342561de45fSGirish Mahadevan 
343561de45fSGirish Mahadevan 	if (m_cmd & SPI_RX_ONLY) {
344561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_RX_TRANS_LEN);
345561de45fSGirish Mahadevan 		mas->rx_rem_bytes = xfer->len;
346561de45fSGirish Mahadevan 	}
347561de45fSGirish Mahadevan 	writel(spi_tx_cfg, se->base + SE_SPI_TRANS_CFG);
348561de45fSGirish Mahadevan 	mas->cur_mcmd = CMD_XFER;
349561de45fSGirish Mahadevan 	geni_se_setup_m_cmd(se, m_cmd, FRAGMENTATION);
350561de45fSGirish Mahadevan 
351561de45fSGirish Mahadevan 	/*
352561de45fSGirish Mahadevan 	 * TX_WATERMARK_REG should be set after SPI configuration and
353561de45fSGirish Mahadevan 	 * setting up GENI SE engine, as driver starts data transfer
354561de45fSGirish Mahadevan 	 * for the watermark interrupt.
355561de45fSGirish Mahadevan 	 */
356561de45fSGirish Mahadevan 	if (m_cmd & SPI_TX_ONLY)
357561de45fSGirish Mahadevan 		writel(mas->tx_wm, se->base + SE_GENI_TX_WATERMARK_REG);
358561de45fSGirish Mahadevan }
359561de45fSGirish Mahadevan 
360561de45fSGirish Mahadevan static void handle_fifo_timeout(struct spi_master *spi,
361561de45fSGirish Mahadevan 				struct spi_message *msg)
362561de45fSGirish Mahadevan {
363561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
364561de45fSGirish Mahadevan 	unsigned long time_left, flags;
365561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
366561de45fSGirish Mahadevan 
367561de45fSGirish Mahadevan 	spin_lock_irqsave(&mas->lock, flags);
368561de45fSGirish Mahadevan 	reinit_completion(&mas->xfer_done);
369561de45fSGirish Mahadevan 	mas->cur_mcmd = CMD_CANCEL;
370561de45fSGirish Mahadevan 	geni_se_cancel_m_cmd(se);
371561de45fSGirish Mahadevan 	writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
372561de45fSGirish Mahadevan 	spin_unlock_irqrestore(&mas->lock, flags);
373561de45fSGirish Mahadevan 	time_left = wait_for_completion_timeout(&mas->xfer_done, HZ);
374561de45fSGirish Mahadevan 	if (time_left)
375561de45fSGirish Mahadevan 		return;
376561de45fSGirish Mahadevan 
377561de45fSGirish Mahadevan 	spin_lock_irqsave(&mas->lock, flags);
378561de45fSGirish Mahadevan 	reinit_completion(&mas->xfer_done);
379561de45fSGirish Mahadevan 	geni_se_abort_m_cmd(se);
380561de45fSGirish Mahadevan 	spin_unlock_irqrestore(&mas->lock, flags);
381561de45fSGirish Mahadevan 	time_left = wait_for_completion_timeout(&mas->xfer_done, HZ);
382561de45fSGirish Mahadevan 	if (!time_left)
383561de45fSGirish Mahadevan 		dev_err(mas->dev, "Failed to cancel/abort m_cmd\n");
384561de45fSGirish Mahadevan }
385561de45fSGirish Mahadevan 
386561de45fSGirish Mahadevan static int spi_geni_transfer_one(struct spi_master *spi,
387561de45fSGirish Mahadevan 				struct spi_device *slv,
388561de45fSGirish Mahadevan 				struct spi_transfer *xfer)
389561de45fSGirish Mahadevan {
390561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
391561de45fSGirish Mahadevan 
392561de45fSGirish Mahadevan 	/* Terminate and return success for 0 byte length transfer */
393561de45fSGirish Mahadevan 	if (!xfer->len)
394561de45fSGirish Mahadevan 		return 0;
395561de45fSGirish Mahadevan 
396561de45fSGirish Mahadevan 	setup_fifo_xfer(xfer, mas, slv->mode, spi);
397561de45fSGirish Mahadevan 	return 1;
398561de45fSGirish Mahadevan }
399561de45fSGirish Mahadevan 
400561de45fSGirish Mahadevan static unsigned int geni_byte_per_fifo_word(struct spi_geni_master *mas)
401561de45fSGirish Mahadevan {
402561de45fSGirish Mahadevan 	/*
403561de45fSGirish Mahadevan 	 * Calculate how many bytes we'll put in each FIFO word.  If the
404561de45fSGirish Mahadevan 	 * transfer words don't pack cleanly into a FIFO word we'll just put
405561de45fSGirish Mahadevan 	 * one transfer word in each FIFO word.  If they do pack we'll pack 'em.
406561de45fSGirish Mahadevan 	 */
407561de45fSGirish Mahadevan 	if (mas->fifo_width_bits % mas->cur_bits_per_word)
408561de45fSGirish Mahadevan 		return roundup_pow_of_two(DIV_ROUND_UP(mas->cur_bits_per_word,
409561de45fSGirish Mahadevan 						       BITS_PER_BYTE));
410561de45fSGirish Mahadevan 
411561de45fSGirish Mahadevan 	return mas->fifo_width_bits / BITS_PER_BYTE;
412561de45fSGirish Mahadevan }
413561de45fSGirish Mahadevan 
414561de45fSGirish Mahadevan static void geni_spi_handle_tx(struct spi_geni_master *mas)
415561de45fSGirish Mahadevan {
416561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
417561de45fSGirish Mahadevan 	unsigned int max_bytes;
418561de45fSGirish Mahadevan 	const u8 *tx_buf;
419561de45fSGirish Mahadevan 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
420561de45fSGirish Mahadevan 	unsigned int i = 0;
421561de45fSGirish Mahadevan 
422561de45fSGirish Mahadevan 	max_bytes = (mas->tx_fifo_depth - mas->tx_wm) * bytes_per_fifo_word;
423561de45fSGirish Mahadevan 	if (mas->tx_rem_bytes < max_bytes)
424561de45fSGirish Mahadevan 		max_bytes = mas->tx_rem_bytes;
425561de45fSGirish Mahadevan 
426561de45fSGirish Mahadevan 	tx_buf = mas->cur_xfer->tx_buf + mas->cur_xfer->len - mas->tx_rem_bytes;
427561de45fSGirish Mahadevan 	while (i < max_bytes) {
428561de45fSGirish Mahadevan 		unsigned int j;
429561de45fSGirish Mahadevan 		unsigned int bytes_to_write;
430561de45fSGirish Mahadevan 		u32 fifo_word = 0;
431561de45fSGirish Mahadevan 		u8 *fifo_byte = (u8 *)&fifo_word;
432561de45fSGirish Mahadevan 
433561de45fSGirish Mahadevan 		bytes_to_write = min(bytes_per_fifo_word, max_bytes - i);
434561de45fSGirish Mahadevan 		for (j = 0; j < bytes_to_write; j++)
435561de45fSGirish Mahadevan 			fifo_byte[j] = tx_buf[i++];
436561de45fSGirish Mahadevan 		iowrite32_rep(se->base + SE_GENI_TX_FIFOn, &fifo_word, 1);
437561de45fSGirish Mahadevan 	}
438561de45fSGirish Mahadevan 	mas->tx_rem_bytes -= max_bytes;
439561de45fSGirish Mahadevan 	if (!mas->tx_rem_bytes)
440561de45fSGirish Mahadevan 		writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
441561de45fSGirish Mahadevan }
442561de45fSGirish Mahadevan 
443561de45fSGirish Mahadevan static void geni_spi_handle_rx(struct spi_geni_master *mas)
444561de45fSGirish Mahadevan {
445561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
446561de45fSGirish Mahadevan 	u32 rx_fifo_status;
447561de45fSGirish Mahadevan 	unsigned int rx_bytes;
448561de45fSGirish Mahadevan 	unsigned int rx_last_byte_valid;
449561de45fSGirish Mahadevan 	u8 *rx_buf;
450561de45fSGirish Mahadevan 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
451561de45fSGirish Mahadevan 	unsigned int i = 0;
452561de45fSGirish Mahadevan 
453561de45fSGirish Mahadevan 	rx_fifo_status = readl(se->base + SE_GENI_RX_FIFO_STATUS);
454561de45fSGirish Mahadevan 	rx_bytes = (rx_fifo_status & RX_FIFO_WC_MSK) * bytes_per_fifo_word;
455561de45fSGirish Mahadevan 	if (rx_fifo_status & RX_LAST) {
456561de45fSGirish Mahadevan 		rx_last_byte_valid = rx_fifo_status & RX_LAST_BYTE_VALID_MSK;
457561de45fSGirish Mahadevan 		rx_last_byte_valid >>= RX_LAST_BYTE_VALID_SHFT;
458561de45fSGirish Mahadevan 		if (rx_last_byte_valid && rx_last_byte_valid < 4)
459561de45fSGirish Mahadevan 			rx_bytes -= bytes_per_fifo_word - rx_last_byte_valid;
460561de45fSGirish Mahadevan 	}
461561de45fSGirish Mahadevan 	if (mas->rx_rem_bytes < rx_bytes)
462561de45fSGirish Mahadevan 		rx_bytes = mas->rx_rem_bytes;
463561de45fSGirish Mahadevan 
464561de45fSGirish Mahadevan 	rx_buf = mas->cur_xfer->rx_buf + mas->cur_xfer->len - mas->rx_rem_bytes;
465561de45fSGirish Mahadevan 	while (i < rx_bytes) {
466561de45fSGirish Mahadevan 		u32 fifo_word = 0;
467561de45fSGirish Mahadevan 		u8 *fifo_byte = (u8 *)&fifo_word;
468561de45fSGirish Mahadevan 		unsigned int bytes_to_read;
469561de45fSGirish Mahadevan 		unsigned int j;
470561de45fSGirish Mahadevan 
471561de45fSGirish Mahadevan 		bytes_to_read = min(bytes_per_fifo_word, rx_bytes - i);
472561de45fSGirish Mahadevan 		ioread32_rep(se->base + SE_GENI_RX_FIFOn, &fifo_word, 1);
473561de45fSGirish Mahadevan 		for (j = 0; j < bytes_to_read; j++)
474561de45fSGirish Mahadevan 			rx_buf[i++] = fifo_byte[j];
475561de45fSGirish Mahadevan 	}
476561de45fSGirish Mahadevan 	mas->rx_rem_bytes -= rx_bytes;
477561de45fSGirish Mahadevan }
478561de45fSGirish Mahadevan 
479561de45fSGirish Mahadevan static irqreturn_t geni_spi_isr(int irq, void *data)
480561de45fSGirish Mahadevan {
481561de45fSGirish Mahadevan 	struct spi_master *spi = data;
482561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
483561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
484561de45fSGirish Mahadevan 	u32 m_irq;
485561de45fSGirish Mahadevan 	unsigned long flags;
486561de45fSGirish Mahadevan 
487561de45fSGirish Mahadevan 	if (mas->cur_mcmd == CMD_NONE)
488561de45fSGirish Mahadevan 		return IRQ_NONE;
489561de45fSGirish Mahadevan 
490561de45fSGirish Mahadevan 	spin_lock_irqsave(&mas->lock, flags);
491561de45fSGirish Mahadevan 	m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
492561de45fSGirish Mahadevan 
493561de45fSGirish Mahadevan 	if ((m_irq & M_RX_FIFO_WATERMARK_EN) || (m_irq & M_RX_FIFO_LAST_EN))
494561de45fSGirish Mahadevan 		geni_spi_handle_rx(mas);
495561de45fSGirish Mahadevan 
496561de45fSGirish Mahadevan 	if (m_irq & M_TX_FIFO_WATERMARK_EN)
497561de45fSGirish Mahadevan 		geni_spi_handle_tx(mas);
498561de45fSGirish Mahadevan 
499561de45fSGirish Mahadevan 	if (m_irq & M_CMD_DONE_EN) {
500561de45fSGirish Mahadevan 		if (mas->cur_mcmd == CMD_XFER)
501561de45fSGirish Mahadevan 			spi_finalize_current_transfer(spi);
502561de45fSGirish Mahadevan 		else if (mas->cur_mcmd == CMD_CS)
503561de45fSGirish Mahadevan 			complete(&mas->xfer_done);
504561de45fSGirish Mahadevan 		mas->cur_mcmd = CMD_NONE;
505561de45fSGirish Mahadevan 		/*
506561de45fSGirish Mahadevan 		 * If this happens, then a CMD_DONE came before all the Tx
507561de45fSGirish Mahadevan 		 * buffer bytes were sent out. This is unusual, log this
508561de45fSGirish Mahadevan 		 * condition and disable the WM interrupt to prevent the
509561de45fSGirish Mahadevan 		 * system from stalling due an interrupt storm.
510561de45fSGirish Mahadevan 		 * If this happens when all Rx bytes haven't been received, log
511561de45fSGirish Mahadevan 		 * the condition.
512561de45fSGirish Mahadevan 		 * The only known time this can happen is if bits_per_word != 8
513561de45fSGirish Mahadevan 		 * and some registers that expect xfer lengths in num spi_words
514561de45fSGirish Mahadevan 		 * weren't written correctly.
515561de45fSGirish Mahadevan 		 */
516561de45fSGirish Mahadevan 		if (mas->tx_rem_bytes) {
517561de45fSGirish Mahadevan 			writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
518561de45fSGirish Mahadevan 			dev_err(mas->dev, "Premature done. tx_rem = %d bpw%d\n",
519561de45fSGirish Mahadevan 				mas->tx_rem_bytes, mas->cur_bits_per_word);
520561de45fSGirish Mahadevan 		}
521561de45fSGirish Mahadevan 		if (mas->rx_rem_bytes)
522561de45fSGirish Mahadevan 			dev_err(mas->dev, "Premature done. rx_rem = %d bpw%d\n",
523561de45fSGirish Mahadevan 				mas->rx_rem_bytes, mas->cur_bits_per_word);
524561de45fSGirish Mahadevan 	}
525561de45fSGirish Mahadevan 
526561de45fSGirish Mahadevan 	if ((m_irq & M_CMD_CANCEL_EN) || (m_irq & M_CMD_ABORT_EN)) {
527561de45fSGirish Mahadevan 		mas->cur_mcmd = CMD_NONE;
528561de45fSGirish Mahadevan 		complete(&mas->xfer_done);
529561de45fSGirish Mahadevan 	}
530561de45fSGirish Mahadevan 
531561de45fSGirish Mahadevan 	writel(m_irq, se->base + SE_GENI_M_IRQ_CLEAR);
532561de45fSGirish Mahadevan 	spin_unlock_irqrestore(&mas->lock, flags);
5330dccff3cSAlok Chauhan 	return IRQ_HANDLED;
534561de45fSGirish Mahadevan }
535561de45fSGirish Mahadevan 
536561de45fSGirish Mahadevan static int spi_geni_probe(struct platform_device *pdev)
537561de45fSGirish Mahadevan {
538*6a34e285SAlok Chauhan 	int ret, irq;
539561de45fSGirish Mahadevan 	struct spi_master *spi;
540561de45fSGirish Mahadevan 	struct spi_geni_master *mas;
541561de45fSGirish Mahadevan 	struct resource *res;
542*6a34e285SAlok Chauhan 	void __iomem *base;
543*6a34e285SAlok Chauhan 	struct clk *clk;
544*6a34e285SAlok Chauhan 
545*6a34e285SAlok Chauhan 	irq = platform_get_irq(pdev, 0);
546*6a34e285SAlok Chauhan 	if (irq < 0) {
547*6a34e285SAlok Chauhan 		dev_err(&pdev->dev, "Err getting IRQ %d\n", irq);
548*6a34e285SAlok Chauhan 		return irq;
549*6a34e285SAlok Chauhan 	}
550*6a34e285SAlok Chauhan 
551*6a34e285SAlok Chauhan 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
552*6a34e285SAlok Chauhan 	base = devm_ioremap_resource(&pdev->dev, res);
553*6a34e285SAlok Chauhan 	if (IS_ERR(base))
554*6a34e285SAlok Chauhan 		return PTR_ERR(base);
555*6a34e285SAlok Chauhan 
556*6a34e285SAlok Chauhan 	clk = devm_clk_get(&pdev->dev, "se");
557*6a34e285SAlok Chauhan 	if (IS_ERR(clk)) {
558*6a34e285SAlok Chauhan 		dev_err(&pdev->dev, "Err getting SE Core clk %ld\n",
559*6a34e285SAlok Chauhan 						PTR_ERR(clk));
560*6a34e285SAlok Chauhan 		return PTR_ERR(clk);
561*6a34e285SAlok Chauhan 	}
562561de45fSGirish Mahadevan 
563561de45fSGirish Mahadevan 	spi = spi_alloc_master(&pdev->dev, sizeof(*mas));
564561de45fSGirish Mahadevan 	if (!spi)
565561de45fSGirish Mahadevan 		return -ENOMEM;
566561de45fSGirish Mahadevan 
567561de45fSGirish Mahadevan 	platform_set_drvdata(pdev, spi);
568561de45fSGirish Mahadevan 	mas = spi_master_get_devdata(spi);
569*6a34e285SAlok Chauhan 	mas->irq = irq;
570561de45fSGirish Mahadevan 	mas->dev = &pdev->dev;
571561de45fSGirish Mahadevan 	mas->se.dev = &pdev->dev;
572561de45fSGirish Mahadevan 	mas->se.wrapper = dev_get_drvdata(pdev->dev.parent);
573*6a34e285SAlok Chauhan 	mas->se.base = base;
574*6a34e285SAlok Chauhan 	mas->se.clk = clk;
575561de45fSGirish Mahadevan 
576561de45fSGirish Mahadevan 	spi->bus_num = -1;
577561de45fSGirish Mahadevan 	spi->dev.of_node = pdev->dev.of_node;
578561de45fSGirish Mahadevan 	spi->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_CS_HIGH;
579561de45fSGirish Mahadevan 	spi->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
580561de45fSGirish Mahadevan 	spi->num_chipselect = 4;
581561de45fSGirish Mahadevan 	spi->max_speed_hz = 50000000;
582561de45fSGirish Mahadevan 	spi->prepare_message = spi_geni_prepare_message;
583561de45fSGirish Mahadevan 	spi->transfer_one = spi_geni_transfer_one;
584561de45fSGirish Mahadevan 	spi->auto_runtime_pm = true;
585561de45fSGirish Mahadevan 	spi->handle_err = handle_fifo_timeout;
586561de45fSGirish Mahadevan 	spi->set_cs = spi_geni_set_cs;
587561de45fSGirish Mahadevan 
588561de45fSGirish Mahadevan 	init_completion(&mas->xfer_done);
589561de45fSGirish Mahadevan 	spin_lock_init(&mas->lock);
590561de45fSGirish Mahadevan 	pm_runtime_enable(&pdev->dev);
591561de45fSGirish Mahadevan 
592561de45fSGirish Mahadevan 	ret = spi_geni_init(mas);
593561de45fSGirish Mahadevan 	if (ret)
594561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
595561de45fSGirish Mahadevan 
596561de45fSGirish Mahadevan 	ret = request_irq(mas->irq, geni_spi_isr,
597561de45fSGirish Mahadevan 			IRQF_TRIGGER_HIGH, "spi_geni", spi);
598561de45fSGirish Mahadevan 	if (ret)
599561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
600561de45fSGirish Mahadevan 
601561de45fSGirish Mahadevan 	ret = spi_register_master(spi);
602561de45fSGirish Mahadevan 	if (ret)
603561de45fSGirish Mahadevan 		goto spi_geni_probe_free_irq;
604561de45fSGirish Mahadevan 
605561de45fSGirish Mahadevan 	return 0;
606561de45fSGirish Mahadevan spi_geni_probe_free_irq:
607561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
608561de45fSGirish Mahadevan spi_geni_probe_runtime_disable:
609561de45fSGirish Mahadevan 	pm_runtime_disable(&pdev->dev);
610561de45fSGirish Mahadevan 	spi_master_put(spi);
611561de45fSGirish Mahadevan 	return ret;
612561de45fSGirish Mahadevan }
613561de45fSGirish Mahadevan 
614561de45fSGirish Mahadevan static int spi_geni_remove(struct platform_device *pdev)
615561de45fSGirish Mahadevan {
616561de45fSGirish Mahadevan 	struct spi_master *spi = platform_get_drvdata(pdev);
617561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
618561de45fSGirish Mahadevan 
619561de45fSGirish Mahadevan 	/* Unregister _before_ disabling pm_runtime() so we stop transfers */
620561de45fSGirish Mahadevan 	spi_unregister_master(spi);
621561de45fSGirish Mahadevan 
622561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
623561de45fSGirish Mahadevan 	pm_runtime_disable(&pdev->dev);
624561de45fSGirish Mahadevan 	return 0;
625561de45fSGirish Mahadevan }
626561de45fSGirish Mahadevan 
627561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_suspend(struct device *dev)
628561de45fSGirish Mahadevan {
629561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
630561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
631561de45fSGirish Mahadevan 
632561de45fSGirish Mahadevan 	return geni_se_resources_off(&mas->se);
633561de45fSGirish Mahadevan }
634561de45fSGirish Mahadevan 
635561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_resume(struct device *dev)
636561de45fSGirish Mahadevan {
637561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
638561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
639561de45fSGirish Mahadevan 
640561de45fSGirish Mahadevan 	return geni_se_resources_on(&mas->se);
641561de45fSGirish Mahadevan }
642561de45fSGirish Mahadevan 
643561de45fSGirish Mahadevan static int __maybe_unused spi_geni_suspend(struct device *dev)
644561de45fSGirish Mahadevan {
645561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
646561de45fSGirish Mahadevan 	int ret;
647561de45fSGirish Mahadevan 
648561de45fSGirish Mahadevan 	ret = spi_master_suspend(spi);
649561de45fSGirish Mahadevan 	if (ret)
650561de45fSGirish Mahadevan 		return ret;
651561de45fSGirish Mahadevan 
652561de45fSGirish Mahadevan 	ret = pm_runtime_force_suspend(dev);
653561de45fSGirish Mahadevan 	if (ret)
654561de45fSGirish Mahadevan 		spi_master_resume(spi);
655561de45fSGirish Mahadevan 
656561de45fSGirish Mahadevan 	return ret;
657561de45fSGirish Mahadevan }
658561de45fSGirish Mahadevan 
659561de45fSGirish Mahadevan static int __maybe_unused spi_geni_resume(struct device *dev)
660561de45fSGirish Mahadevan {
661561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
662561de45fSGirish Mahadevan 	int ret;
663561de45fSGirish Mahadevan 
664561de45fSGirish Mahadevan 	ret = pm_runtime_force_resume(dev);
665561de45fSGirish Mahadevan 	if (ret)
666561de45fSGirish Mahadevan 		return ret;
667561de45fSGirish Mahadevan 
668561de45fSGirish Mahadevan 	ret = spi_master_resume(spi);
669561de45fSGirish Mahadevan 	if (ret)
670561de45fSGirish Mahadevan 		pm_runtime_force_suspend(dev);
671561de45fSGirish Mahadevan 
672561de45fSGirish Mahadevan 	return ret;
673561de45fSGirish Mahadevan }
674561de45fSGirish Mahadevan 
675561de45fSGirish Mahadevan static const struct dev_pm_ops spi_geni_pm_ops = {
676561de45fSGirish Mahadevan 	SET_RUNTIME_PM_OPS(spi_geni_runtime_suspend,
677561de45fSGirish Mahadevan 					spi_geni_runtime_resume, NULL)
678561de45fSGirish Mahadevan 	SET_SYSTEM_SLEEP_PM_OPS(spi_geni_suspend, spi_geni_resume)
679561de45fSGirish Mahadevan };
680561de45fSGirish Mahadevan 
681561de45fSGirish Mahadevan static const struct of_device_id spi_geni_dt_match[] = {
682561de45fSGirish Mahadevan 	{ .compatible = "qcom,geni-spi" },
683561de45fSGirish Mahadevan 	{}
684561de45fSGirish Mahadevan };
685561de45fSGirish Mahadevan MODULE_DEVICE_TABLE(of, spi_geni_dt_match);
686561de45fSGirish Mahadevan 
687561de45fSGirish Mahadevan static struct platform_driver spi_geni_driver = {
688561de45fSGirish Mahadevan 	.probe  = spi_geni_probe,
689561de45fSGirish Mahadevan 	.remove = spi_geni_remove,
690561de45fSGirish Mahadevan 	.driver = {
691561de45fSGirish Mahadevan 		.name = "geni_spi",
692561de45fSGirish Mahadevan 		.pm = &spi_geni_pm_ops,
693561de45fSGirish Mahadevan 		.of_match_table = spi_geni_dt_match,
694561de45fSGirish Mahadevan 	},
695561de45fSGirish Mahadevan };
696561de45fSGirish Mahadevan module_platform_driver(spi_geni_driver);
697561de45fSGirish Mahadevan 
698561de45fSGirish Mahadevan MODULE_DESCRIPTION("SPI driver for GENI based QUP cores");
699561de45fSGirish Mahadevan MODULE_LICENSE("GPL v2");
700