xref: /linux/drivers/spi/spi-geni-qcom.c (revision 68890e20942b8693d487a4a2ea882e36523e8193)
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/platform_device.h>
101a9e489eSRajendra Nayak #include <linux/pm_opp.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 	int ret;
100561de45fSGirish Mahadevan 
101561de45fSGirish Mahadevan 	ret = geni_se_clk_freq_match(&mas->se,
102561de45fSGirish Mahadevan 				speed_hz * mas->oversampling,
103561de45fSGirish Mahadevan 				clk_idx, &sclk_freq, false);
104561de45fSGirish Mahadevan 	if (ret) {
105561de45fSGirish Mahadevan 		dev_err(mas->dev, "Failed(%d) to find src clk for %dHz\n",
106561de45fSGirish Mahadevan 							ret, speed_hz);
107561de45fSGirish Mahadevan 		return ret;
108561de45fSGirish Mahadevan 	}
109561de45fSGirish Mahadevan 
110561de45fSGirish Mahadevan 	*clk_div = DIV_ROUND_UP(sclk_freq, mas->oversampling * speed_hz);
111561de45fSGirish Mahadevan 	actual_hz = sclk_freq / (mas->oversampling * *clk_div);
112561de45fSGirish Mahadevan 
113561de45fSGirish Mahadevan 	dev_dbg(mas->dev, "req %u=>%u sclk %lu, idx %d, div %d\n", speed_hz,
114561de45fSGirish Mahadevan 				actual_hz, sclk_freq, *clk_idx, *clk_div);
1151a9e489eSRajendra Nayak 	ret = dev_pm_opp_set_rate(mas->dev, sclk_freq);
116561de45fSGirish Mahadevan 	if (ret)
1171a9e489eSRajendra Nayak 		dev_err(mas->dev, "dev_pm_opp_set_rate failed %d\n", ret);
118561de45fSGirish Mahadevan 	return ret;
119561de45fSGirish Mahadevan }
120561de45fSGirish Mahadevan 
121de43affeSStephen Boyd static void handle_fifo_timeout(struct spi_master *spi,
122de43affeSStephen Boyd 				struct spi_message *msg)
123de43affeSStephen Boyd {
124de43affeSStephen Boyd 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
125de43affeSStephen Boyd 	unsigned long time_left, flags;
126de43affeSStephen Boyd 	struct geni_se *se = &mas->se;
127de43affeSStephen Boyd 
128de43affeSStephen Boyd 	spin_lock_irqsave(&mas->lock, flags);
129de43affeSStephen Boyd 	reinit_completion(&mas->xfer_done);
130de43affeSStephen Boyd 	mas->cur_mcmd = CMD_CANCEL;
131de43affeSStephen Boyd 	geni_se_cancel_m_cmd(se);
132de43affeSStephen Boyd 	writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
133de43affeSStephen Boyd 	spin_unlock_irqrestore(&mas->lock, flags);
134de43affeSStephen Boyd 	time_left = wait_for_completion_timeout(&mas->xfer_done, HZ);
135de43affeSStephen Boyd 	if (time_left)
136de43affeSStephen Boyd 		return;
137de43affeSStephen Boyd 
138de43affeSStephen Boyd 	spin_lock_irqsave(&mas->lock, flags);
139de43affeSStephen Boyd 	reinit_completion(&mas->xfer_done);
140de43affeSStephen Boyd 	geni_se_abort_m_cmd(se);
141de43affeSStephen Boyd 	spin_unlock_irqrestore(&mas->lock, flags);
142de43affeSStephen Boyd 	time_left = wait_for_completion_timeout(&mas->xfer_done, HZ);
143de43affeSStephen Boyd 	if (!time_left)
144de43affeSStephen Boyd 		dev_err(mas->dev, "Failed to cancel/abort m_cmd\n");
145de43affeSStephen Boyd }
146de43affeSStephen Boyd 
147561de45fSGirish Mahadevan static void spi_geni_set_cs(struct spi_device *slv, bool set_flag)
148561de45fSGirish Mahadevan {
149561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(slv->master);
150561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(mas->dev);
151561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
1520dccff3cSAlok Chauhan 	unsigned long time_left;
153561de45fSGirish Mahadevan 
154561de45fSGirish Mahadevan 	reinit_completion(&mas->xfer_done);
155561de45fSGirish Mahadevan 	pm_runtime_get_sync(mas->dev);
156561de45fSGirish Mahadevan 	if (!(slv->mode & SPI_CS_HIGH))
157561de45fSGirish Mahadevan 		set_flag = !set_flag;
158561de45fSGirish Mahadevan 
159561de45fSGirish Mahadevan 	mas->cur_mcmd = CMD_CS;
160561de45fSGirish Mahadevan 	if (set_flag)
161561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_ASSERT, 0);
162561de45fSGirish Mahadevan 	else
163561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_DEASSERT, 0);
164561de45fSGirish Mahadevan 
1650dccff3cSAlok Chauhan 	time_left = wait_for_completion_timeout(&mas->xfer_done, HZ);
1660dccff3cSAlok Chauhan 	if (!time_left)
167561de45fSGirish Mahadevan 		handle_fifo_timeout(spi, NULL);
168561de45fSGirish Mahadevan 
169561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
170561de45fSGirish Mahadevan }
171561de45fSGirish Mahadevan 
172561de45fSGirish Mahadevan static void spi_setup_word_len(struct spi_geni_master *mas, u16 mode,
173561de45fSGirish Mahadevan 					unsigned int bits_per_word)
174561de45fSGirish Mahadevan {
175561de45fSGirish Mahadevan 	unsigned int pack_words;
176561de45fSGirish Mahadevan 	bool msb_first = (mode & SPI_LSB_FIRST) ? false : true;
177561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
178561de45fSGirish Mahadevan 	u32 word_len;
179561de45fSGirish Mahadevan 
180561de45fSGirish Mahadevan 	word_len = readl(se->base + SE_SPI_WORD_LEN);
181561de45fSGirish Mahadevan 
182561de45fSGirish Mahadevan 	/*
183561de45fSGirish Mahadevan 	 * If bits_per_word isn't a byte aligned value, set the packing to be
184561de45fSGirish Mahadevan 	 * 1 SPI word per FIFO word.
185561de45fSGirish Mahadevan 	 */
186561de45fSGirish Mahadevan 	if (!(mas->fifo_width_bits % bits_per_word))
187561de45fSGirish Mahadevan 		pack_words = mas->fifo_width_bits / bits_per_word;
188561de45fSGirish Mahadevan 	else
189561de45fSGirish Mahadevan 		pack_words = 1;
190561de45fSGirish Mahadevan 	word_len &= ~WORD_LEN_MSK;
191561de45fSGirish Mahadevan 	word_len |= ((bits_per_word - MIN_WORD_LEN) & WORD_LEN_MSK);
192561de45fSGirish Mahadevan 	geni_se_config_packing(&mas->se, bits_per_word, pack_words, msb_first,
193561de45fSGirish Mahadevan 								true, true);
194561de45fSGirish Mahadevan 	writel(word_len, se->base + SE_SPI_WORD_LEN);
195561de45fSGirish Mahadevan }
196561de45fSGirish Mahadevan 
1970e3b8a81SAkash Asthana static int geni_spi_set_clock_and_bw(struct spi_geni_master *mas,
1980e3b8a81SAkash Asthana 					unsigned long clk_hz)
199e68b6624SDouglas Anderson {
200e68b6624SDouglas Anderson 	u32 clk_sel, m_clk_cfg, idx, div;
201e68b6624SDouglas Anderson 	struct geni_se *se = &mas->se;
202e68b6624SDouglas Anderson 	int ret;
203e68b6624SDouglas Anderson 
204*68890e20SDouglas Anderson 	if (clk_hz == mas->cur_speed_hz)
205*68890e20SDouglas Anderson 		return 0;
206*68890e20SDouglas Anderson 
207e68b6624SDouglas Anderson 	ret = get_spi_clk_cfg(clk_hz, mas, &idx, &div);
208e68b6624SDouglas Anderson 	if (ret) {
209e68b6624SDouglas Anderson 		dev_err(mas->dev, "Err setting clk to %lu: %d\n", clk_hz, ret);
210e68b6624SDouglas Anderson 		return ret;
211e68b6624SDouglas Anderson 	}
212e68b6624SDouglas Anderson 
213e68b6624SDouglas Anderson 	/*
214e68b6624SDouglas Anderson 	 * SPI core clock gets configured with the requested frequency
215e68b6624SDouglas Anderson 	 * or the frequency closer to the requested frequency.
216e68b6624SDouglas Anderson 	 * For that reason requested frequency is stored in the
217e68b6624SDouglas Anderson 	 * cur_speed_hz and referred in the consecutive transfer instead
218e68b6624SDouglas Anderson 	 * of calling clk_get_rate() API.
219e68b6624SDouglas Anderson 	 */
220e68b6624SDouglas Anderson 	mas->cur_speed_hz = clk_hz;
221e68b6624SDouglas Anderson 
222e68b6624SDouglas Anderson 	clk_sel = idx & CLK_SEL_MSK;
223e68b6624SDouglas Anderson 	m_clk_cfg = (div << CLK_DIV_SHFT) | SER_CLK_EN;
224e68b6624SDouglas Anderson 	writel(clk_sel, se->base + SE_GENI_CLK_SEL);
225e68b6624SDouglas Anderson 	writel(m_clk_cfg, se->base + GENI_SER_M_CLK_CFG);
226e68b6624SDouglas Anderson 
2270e3b8a81SAkash Asthana 	/* Set BW quota for CPU as driver supports FIFO mode only. */
2280e3b8a81SAkash Asthana 	se->icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(mas->cur_speed_hz);
2290e3b8a81SAkash Asthana 	ret = geni_icc_set_bw(se);
2300e3b8a81SAkash Asthana 	if (ret)
2310e3b8a81SAkash Asthana 		return ret;
2320e3b8a81SAkash Asthana 
233e68b6624SDouglas Anderson 	return 0;
234e68b6624SDouglas Anderson }
235e68b6624SDouglas Anderson 
236561de45fSGirish Mahadevan static int setup_fifo_params(struct spi_device *spi_slv,
237561de45fSGirish Mahadevan 					struct spi_master *spi)
238561de45fSGirish Mahadevan {
239561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
240561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
241561de45fSGirish Mahadevan 	u32 loopback_cfg, cpol, cpha, demux_output_inv;
242e68b6624SDouglas Anderson 	u32 demux_sel;
243561de45fSGirish Mahadevan 
244561de45fSGirish Mahadevan 	loopback_cfg = readl(se->base + SE_SPI_LOOPBACK);
245561de45fSGirish Mahadevan 	cpol = readl(se->base + SE_SPI_CPOL);
246561de45fSGirish Mahadevan 	cpha = readl(se->base + SE_SPI_CPHA);
247561de45fSGirish Mahadevan 	demux_output_inv = 0;
248561de45fSGirish Mahadevan 	loopback_cfg &= ~LOOPBACK_MSK;
249561de45fSGirish Mahadevan 	cpol &= ~CPOL;
250561de45fSGirish Mahadevan 	cpha &= ~CPHA;
251561de45fSGirish Mahadevan 
252561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_LOOP)
253561de45fSGirish Mahadevan 		loopback_cfg |= LOOPBACK_ENABLE;
254561de45fSGirish Mahadevan 
255561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CPOL)
256561de45fSGirish Mahadevan 		cpol |= CPOL;
257561de45fSGirish Mahadevan 
258561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CPHA)
259561de45fSGirish Mahadevan 		cpha |= CPHA;
260561de45fSGirish Mahadevan 
261561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CS_HIGH)
262561de45fSGirish Mahadevan 		demux_output_inv = BIT(spi_slv->chip_select);
263561de45fSGirish Mahadevan 
264561de45fSGirish Mahadevan 	demux_sel = spi_slv->chip_select;
265561de45fSGirish Mahadevan 	mas->cur_bits_per_word = spi_slv->bits_per_word;
266561de45fSGirish Mahadevan 
267561de45fSGirish Mahadevan 	spi_setup_word_len(mas, spi_slv->mode, spi_slv->bits_per_word);
268561de45fSGirish Mahadevan 	writel(loopback_cfg, se->base + SE_SPI_LOOPBACK);
269561de45fSGirish Mahadevan 	writel(demux_sel, se->base + SE_SPI_DEMUX_SEL);
270561de45fSGirish Mahadevan 	writel(cpha, se->base + SE_SPI_CPHA);
271561de45fSGirish Mahadevan 	writel(cpol, se->base + SE_SPI_CPOL);
272561de45fSGirish Mahadevan 	writel(demux_output_inv, se->base + SE_SPI_DEMUX_OUTPUT_INV);
273e68b6624SDouglas Anderson 
2740e3b8a81SAkash Asthana 	return geni_spi_set_clock_and_bw(mas, spi_slv->max_speed_hz);
275561de45fSGirish Mahadevan }
276561de45fSGirish Mahadevan 
277561de45fSGirish Mahadevan static int spi_geni_prepare_message(struct spi_master *spi,
278561de45fSGirish Mahadevan 					struct spi_message *spi_msg)
279561de45fSGirish Mahadevan {
280561de45fSGirish Mahadevan 	int ret;
281561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
282561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
283561de45fSGirish Mahadevan 
284561de45fSGirish Mahadevan 	geni_se_select_mode(se, GENI_SE_FIFO);
285561de45fSGirish Mahadevan 	ret = setup_fifo_params(spi_msg->spi, spi);
286561de45fSGirish Mahadevan 	if (ret)
287561de45fSGirish Mahadevan 		dev_err(mas->dev, "Couldn't select mode %d\n", ret);
288561de45fSGirish Mahadevan 	return ret;
289561de45fSGirish Mahadevan }
290561de45fSGirish Mahadevan 
291561de45fSGirish Mahadevan static int spi_geni_init(struct spi_geni_master *mas)
292561de45fSGirish Mahadevan {
293561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
294561de45fSGirish Mahadevan 	unsigned int proto, major, minor, ver;
295561de45fSGirish Mahadevan 
296561de45fSGirish Mahadevan 	pm_runtime_get_sync(mas->dev);
297561de45fSGirish Mahadevan 
298561de45fSGirish Mahadevan 	proto = geni_se_read_proto(se);
299561de45fSGirish Mahadevan 	if (proto != GENI_SE_SPI) {
300561de45fSGirish Mahadevan 		dev_err(mas->dev, "Invalid proto %d\n", proto);
301561de45fSGirish Mahadevan 		pm_runtime_put(mas->dev);
302561de45fSGirish Mahadevan 		return -ENXIO;
303561de45fSGirish Mahadevan 	}
304561de45fSGirish Mahadevan 	mas->tx_fifo_depth = geni_se_get_tx_fifo_depth(se);
305561de45fSGirish Mahadevan 
306561de45fSGirish Mahadevan 	/* Width of Tx and Rx FIFO is same */
307561de45fSGirish Mahadevan 	mas->fifo_width_bits = geni_se_get_tx_fifo_width(se);
308561de45fSGirish Mahadevan 
309561de45fSGirish Mahadevan 	/*
310561de45fSGirish Mahadevan 	 * Hardware programming guide suggests to configure
311561de45fSGirish Mahadevan 	 * RX FIFO RFR level to fifo_depth-2.
312561de45fSGirish Mahadevan 	 */
313561de45fSGirish Mahadevan 	geni_se_init(se, 0x0, mas->tx_fifo_depth - 2);
314561de45fSGirish Mahadevan 	/* Transmit an entire FIFO worth of data per IRQ */
315561de45fSGirish Mahadevan 	mas->tx_wm = 1;
316561de45fSGirish Mahadevan 	ver = geni_se_get_qup_hw_version(se);
317561de45fSGirish Mahadevan 	major = GENI_SE_VERSION_MAJOR(ver);
318561de45fSGirish Mahadevan 	minor = GENI_SE_VERSION_MINOR(ver);
319561de45fSGirish Mahadevan 
320561de45fSGirish Mahadevan 	if (major == 1 && minor == 0)
321561de45fSGirish Mahadevan 		mas->oversampling = 2;
322561de45fSGirish Mahadevan 	else
323561de45fSGirish Mahadevan 		mas->oversampling = 1;
324561de45fSGirish Mahadevan 
325561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
326561de45fSGirish Mahadevan 	return 0;
327561de45fSGirish Mahadevan }
328561de45fSGirish Mahadevan 
329561de45fSGirish Mahadevan static void setup_fifo_xfer(struct spi_transfer *xfer,
330561de45fSGirish Mahadevan 				struct spi_geni_master *mas,
331561de45fSGirish Mahadevan 				u16 mode, struct spi_master *spi)
332561de45fSGirish Mahadevan {
333561de45fSGirish Mahadevan 	u32 m_cmd = 0;
334561de45fSGirish Mahadevan 	u32 spi_tx_cfg, len;
335561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
336e68b6624SDouglas Anderson 	int ret;
337561de45fSGirish Mahadevan 
338561de45fSGirish Mahadevan 	spi_tx_cfg = readl(se->base + SE_SPI_TRANS_CFG);
339561de45fSGirish Mahadevan 	if (xfer->bits_per_word != mas->cur_bits_per_word) {
340561de45fSGirish Mahadevan 		spi_setup_word_len(mas, mode, xfer->bits_per_word);
341561de45fSGirish Mahadevan 		mas->cur_bits_per_word = xfer->bits_per_word;
342561de45fSGirish Mahadevan 	}
343561de45fSGirish Mahadevan 
344561de45fSGirish Mahadevan 	/* Speed and bits per word can be overridden per transfer */
3450e3b8a81SAkash Asthana 	ret = geni_spi_set_clock_and_bw(mas, xfer->speed_hz);
346e68b6624SDouglas Anderson 	if (ret)
347561de45fSGirish Mahadevan 		return;
348561de45fSGirish Mahadevan 
349561de45fSGirish Mahadevan 	mas->tx_rem_bytes = 0;
350561de45fSGirish Mahadevan 	mas->rx_rem_bytes = 0;
351561de45fSGirish Mahadevan 	if (xfer->tx_buf && xfer->rx_buf)
352561de45fSGirish Mahadevan 		m_cmd = SPI_FULL_DUPLEX;
353561de45fSGirish Mahadevan 	else if (xfer->tx_buf)
354561de45fSGirish Mahadevan 		m_cmd = SPI_TX_ONLY;
355561de45fSGirish Mahadevan 	else if (xfer->rx_buf)
356561de45fSGirish Mahadevan 		m_cmd = SPI_RX_ONLY;
357561de45fSGirish Mahadevan 
358561de45fSGirish Mahadevan 	spi_tx_cfg &= ~CS_TOGGLE;
359561de45fSGirish Mahadevan 
360561de45fSGirish Mahadevan 	if (!(mas->cur_bits_per_word % MIN_WORD_LEN))
361561de45fSGirish Mahadevan 		len = xfer->len * BITS_PER_BYTE / mas->cur_bits_per_word;
362561de45fSGirish Mahadevan 	else
363561de45fSGirish Mahadevan 		len = xfer->len / (mas->cur_bits_per_word / BITS_PER_BYTE + 1);
364561de45fSGirish Mahadevan 	len &= TRANS_LEN_MSK;
365561de45fSGirish Mahadevan 
366561de45fSGirish Mahadevan 	mas->cur_xfer = xfer;
367561de45fSGirish Mahadevan 	if (m_cmd & SPI_TX_ONLY) {
368561de45fSGirish Mahadevan 		mas->tx_rem_bytes = xfer->len;
369561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_TX_TRANS_LEN);
370561de45fSGirish Mahadevan 	}
371561de45fSGirish Mahadevan 
372561de45fSGirish Mahadevan 	if (m_cmd & SPI_RX_ONLY) {
373561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_RX_TRANS_LEN);
374561de45fSGirish Mahadevan 		mas->rx_rem_bytes = xfer->len;
375561de45fSGirish Mahadevan 	}
376561de45fSGirish Mahadevan 	writel(spi_tx_cfg, se->base + SE_SPI_TRANS_CFG);
377561de45fSGirish Mahadevan 	mas->cur_mcmd = CMD_XFER;
378561de45fSGirish Mahadevan 	geni_se_setup_m_cmd(se, m_cmd, FRAGMENTATION);
379561de45fSGirish Mahadevan 
380561de45fSGirish Mahadevan 	/*
381561de45fSGirish Mahadevan 	 * TX_WATERMARK_REG should be set after SPI configuration and
382561de45fSGirish Mahadevan 	 * setting up GENI SE engine, as driver starts data transfer
383561de45fSGirish Mahadevan 	 * for the watermark interrupt.
384561de45fSGirish Mahadevan 	 */
385561de45fSGirish Mahadevan 	if (m_cmd & SPI_TX_ONLY)
386561de45fSGirish Mahadevan 		writel(mas->tx_wm, se->base + SE_GENI_TX_WATERMARK_REG);
387561de45fSGirish Mahadevan }
388561de45fSGirish Mahadevan 
389561de45fSGirish Mahadevan static int spi_geni_transfer_one(struct spi_master *spi,
390561de45fSGirish Mahadevan 				struct spi_device *slv,
391561de45fSGirish Mahadevan 				struct spi_transfer *xfer)
392561de45fSGirish Mahadevan {
393561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
394561de45fSGirish Mahadevan 
395561de45fSGirish Mahadevan 	/* Terminate and return success for 0 byte length transfer */
396561de45fSGirish Mahadevan 	if (!xfer->len)
397561de45fSGirish Mahadevan 		return 0;
398561de45fSGirish Mahadevan 
399561de45fSGirish Mahadevan 	setup_fifo_xfer(xfer, mas, slv->mode, spi);
400561de45fSGirish Mahadevan 	return 1;
401561de45fSGirish Mahadevan }
402561de45fSGirish Mahadevan 
403561de45fSGirish Mahadevan static unsigned int geni_byte_per_fifo_word(struct spi_geni_master *mas)
404561de45fSGirish Mahadevan {
405561de45fSGirish Mahadevan 	/*
406561de45fSGirish Mahadevan 	 * Calculate how many bytes we'll put in each FIFO word.  If the
407561de45fSGirish Mahadevan 	 * transfer words don't pack cleanly into a FIFO word we'll just put
408561de45fSGirish Mahadevan 	 * one transfer word in each FIFO word.  If they do pack we'll pack 'em.
409561de45fSGirish Mahadevan 	 */
410561de45fSGirish Mahadevan 	if (mas->fifo_width_bits % mas->cur_bits_per_word)
411561de45fSGirish Mahadevan 		return roundup_pow_of_two(DIV_ROUND_UP(mas->cur_bits_per_word,
412561de45fSGirish Mahadevan 						       BITS_PER_BYTE));
413561de45fSGirish Mahadevan 
414561de45fSGirish Mahadevan 	return mas->fifo_width_bits / BITS_PER_BYTE;
415561de45fSGirish Mahadevan }
416561de45fSGirish Mahadevan 
417561de45fSGirish Mahadevan static void geni_spi_handle_tx(struct spi_geni_master *mas)
418561de45fSGirish Mahadevan {
419561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
420561de45fSGirish Mahadevan 	unsigned int max_bytes;
421561de45fSGirish Mahadevan 	const u8 *tx_buf;
422561de45fSGirish Mahadevan 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
423561de45fSGirish Mahadevan 	unsigned int i = 0;
424561de45fSGirish Mahadevan 
425561de45fSGirish Mahadevan 	max_bytes = (mas->tx_fifo_depth - mas->tx_wm) * bytes_per_fifo_word;
426561de45fSGirish Mahadevan 	if (mas->tx_rem_bytes < max_bytes)
427561de45fSGirish Mahadevan 		max_bytes = mas->tx_rem_bytes;
428561de45fSGirish Mahadevan 
429561de45fSGirish Mahadevan 	tx_buf = mas->cur_xfer->tx_buf + mas->cur_xfer->len - mas->tx_rem_bytes;
430561de45fSGirish Mahadevan 	while (i < max_bytes) {
431561de45fSGirish Mahadevan 		unsigned int j;
432561de45fSGirish Mahadevan 		unsigned int bytes_to_write;
433561de45fSGirish Mahadevan 		u32 fifo_word = 0;
434561de45fSGirish Mahadevan 		u8 *fifo_byte = (u8 *)&fifo_word;
435561de45fSGirish Mahadevan 
436561de45fSGirish Mahadevan 		bytes_to_write = min(bytes_per_fifo_word, max_bytes - i);
437561de45fSGirish Mahadevan 		for (j = 0; j < bytes_to_write; j++)
438561de45fSGirish Mahadevan 			fifo_byte[j] = tx_buf[i++];
439561de45fSGirish Mahadevan 		iowrite32_rep(se->base + SE_GENI_TX_FIFOn, &fifo_word, 1);
440561de45fSGirish Mahadevan 	}
441561de45fSGirish Mahadevan 	mas->tx_rem_bytes -= max_bytes;
442561de45fSGirish Mahadevan 	if (!mas->tx_rem_bytes)
443561de45fSGirish Mahadevan 		writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
444561de45fSGirish Mahadevan }
445561de45fSGirish Mahadevan 
446561de45fSGirish Mahadevan static void geni_spi_handle_rx(struct spi_geni_master *mas)
447561de45fSGirish Mahadevan {
448561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
449561de45fSGirish Mahadevan 	u32 rx_fifo_status;
450561de45fSGirish Mahadevan 	unsigned int rx_bytes;
451561de45fSGirish Mahadevan 	unsigned int rx_last_byte_valid;
452561de45fSGirish Mahadevan 	u8 *rx_buf;
453561de45fSGirish Mahadevan 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
454561de45fSGirish Mahadevan 	unsigned int i = 0;
455561de45fSGirish Mahadevan 
456561de45fSGirish Mahadevan 	rx_fifo_status = readl(se->base + SE_GENI_RX_FIFO_STATUS);
457561de45fSGirish Mahadevan 	rx_bytes = (rx_fifo_status & RX_FIFO_WC_MSK) * bytes_per_fifo_word;
458561de45fSGirish Mahadevan 	if (rx_fifo_status & RX_LAST) {
459561de45fSGirish Mahadevan 		rx_last_byte_valid = rx_fifo_status & RX_LAST_BYTE_VALID_MSK;
460561de45fSGirish Mahadevan 		rx_last_byte_valid >>= RX_LAST_BYTE_VALID_SHFT;
461561de45fSGirish Mahadevan 		if (rx_last_byte_valid && rx_last_byte_valid < 4)
462561de45fSGirish Mahadevan 			rx_bytes -= bytes_per_fifo_word - rx_last_byte_valid;
463561de45fSGirish Mahadevan 	}
464561de45fSGirish Mahadevan 	if (mas->rx_rem_bytes < rx_bytes)
465561de45fSGirish Mahadevan 		rx_bytes = mas->rx_rem_bytes;
466561de45fSGirish Mahadevan 
467561de45fSGirish Mahadevan 	rx_buf = mas->cur_xfer->rx_buf + mas->cur_xfer->len - mas->rx_rem_bytes;
468561de45fSGirish Mahadevan 	while (i < rx_bytes) {
469561de45fSGirish Mahadevan 		u32 fifo_word = 0;
470561de45fSGirish Mahadevan 		u8 *fifo_byte = (u8 *)&fifo_word;
471561de45fSGirish Mahadevan 		unsigned int bytes_to_read;
472561de45fSGirish Mahadevan 		unsigned int j;
473561de45fSGirish Mahadevan 
474561de45fSGirish Mahadevan 		bytes_to_read = min(bytes_per_fifo_word, rx_bytes - i);
475561de45fSGirish Mahadevan 		ioread32_rep(se->base + SE_GENI_RX_FIFOn, &fifo_word, 1);
476561de45fSGirish Mahadevan 		for (j = 0; j < bytes_to_read; j++)
477561de45fSGirish Mahadevan 			rx_buf[i++] = fifo_byte[j];
478561de45fSGirish Mahadevan 	}
479561de45fSGirish Mahadevan 	mas->rx_rem_bytes -= rx_bytes;
480561de45fSGirish Mahadevan }
481561de45fSGirish Mahadevan 
482561de45fSGirish Mahadevan static irqreturn_t geni_spi_isr(int irq, void *data)
483561de45fSGirish Mahadevan {
484561de45fSGirish Mahadevan 	struct spi_master *spi = data;
485561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
486561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
487561de45fSGirish Mahadevan 	u32 m_irq;
488561de45fSGirish Mahadevan 	unsigned long flags;
489561de45fSGirish Mahadevan 
490561de45fSGirish Mahadevan 	if (mas->cur_mcmd == CMD_NONE)
491561de45fSGirish Mahadevan 		return IRQ_NONE;
492561de45fSGirish Mahadevan 
493561de45fSGirish Mahadevan 	spin_lock_irqsave(&mas->lock, flags);
494561de45fSGirish Mahadevan 	m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
495561de45fSGirish Mahadevan 
496561de45fSGirish Mahadevan 	if ((m_irq & M_RX_FIFO_WATERMARK_EN) || (m_irq & M_RX_FIFO_LAST_EN))
497561de45fSGirish Mahadevan 		geni_spi_handle_rx(mas);
498561de45fSGirish Mahadevan 
499561de45fSGirish Mahadevan 	if (m_irq & M_TX_FIFO_WATERMARK_EN)
500561de45fSGirish Mahadevan 		geni_spi_handle_tx(mas);
501561de45fSGirish Mahadevan 
502561de45fSGirish Mahadevan 	if (m_irq & M_CMD_DONE_EN) {
503561de45fSGirish Mahadevan 		if (mas->cur_mcmd == CMD_XFER)
504561de45fSGirish Mahadevan 			spi_finalize_current_transfer(spi);
505561de45fSGirish Mahadevan 		else if (mas->cur_mcmd == CMD_CS)
506561de45fSGirish Mahadevan 			complete(&mas->xfer_done);
507561de45fSGirish Mahadevan 		mas->cur_mcmd = CMD_NONE;
508561de45fSGirish Mahadevan 		/*
509561de45fSGirish Mahadevan 		 * If this happens, then a CMD_DONE came before all the Tx
510561de45fSGirish Mahadevan 		 * buffer bytes were sent out. This is unusual, log this
511561de45fSGirish Mahadevan 		 * condition and disable the WM interrupt to prevent the
512561de45fSGirish Mahadevan 		 * system from stalling due an interrupt storm.
513561de45fSGirish Mahadevan 		 * If this happens when all Rx bytes haven't been received, log
514561de45fSGirish Mahadevan 		 * the condition.
515561de45fSGirish Mahadevan 		 * The only known time this can happen is if bits_per_word != 8
516561de45fSGirish Mahadevan 		 * and some registers that expect xfer lengths in num spi_words
517561de45fSGirish Mahadevan 		 * weren't written correctly.
518561de45fSGirish Mahadevan 		 */
519561de45fSGirish Mahadevan 		if (mas->tx_rem_bytes) {
520561de45fSGirish Mahadevan 			writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
521561de45fSGirish Mahadevan 			dev_err(mas->dev, "Premature done. tx_rem = %d bpw%d\n",
522561de45fSGirish Mahadevan 				mas->tx_rem_bytes, mas->cur_bits_per_word);
523561de45fSGirish Mahadevan 		}
524561de45fSGirish Mahadevan 		if (mas->rx_rem_bytes)
525561de45fSGirish Mahadevan 			dev_err(mas->dev, "Premature done. rx_rem = %d bpw%d\n",
526561de45fSGirish Mahadevan 				mas->rx_rem_bytes, mas->cur_bits_per_word);
527561de45fSGirish Mahadevan 	}
528561de45fSGirish Mahadevan 
529561de45fSGirish Mahadevan 	if ((m_irq & M_CMD_CANCEL_EN) || (m_irq & M_CMD_ABORT_EN)) {
530561de45fSGirish Mahadevan 		mas->cur_mcmd = CMD_NONE;
531561de45fSGirish Mahadevan 		complete(&mas->xfer_done);
532561de45fSGirish Mahadevan 	}
533561de45fSGirish Mahadevan 
534561de45fSGirish Mahadevan 	writel(m_irq, se->base + SE_GENI_M_IRQ_CLEAR);
535561de45fSGirish Mahadevan 	spin_unlock_irqrestore(&mas->lock, flags);
5360dccff3cSAlok Chauhan 	return IRQ_HANDLED;
537561de45fSGirish Mahadevan }
538561de45fSGirish Mahadevan 
539561de45fSGirish Mahadevan static int spi_geni_probe(struct platform_device *pdev)
540561de45fSGirish Mahadevan {
5416a34e285SAlok Chauhan 	int ret, irq;
542561de45fSGirish Mahadevan 	struct spi_master *spi;
543561de45fSGirish Mahadevan 	struct spi_geni_master *mas;
5446a34e285SAlok Chauhan 	void __iomem *base;
5456a34e285SAlok Chauhan 	struct clk *clk;
546ea1e5b33SStephen Boyd 	struct device *dev = &pdev->dev;
5476a34e285SAlok Chauhan 
5486a34e285SAlok Chauhan 	irq = platform_get_irq(pdev, 0);
5496b8ac10eSStephen Boyd 	if (irq < 0)
5506a34e285SAlok Chauhan 		return irq;
5516a34e285SAlok Chauhan 
552d8e477abSYueHaibing 	base = devm_platform_ioremap_resource(pdev, 0);
5536a34e285SAlok Chauhan 	if (IS_ERR(base))
5546a34e285SAlok Chauhan 		return PTR_ERR(base);
5556a34e285SAlok Chauhan 
556ea1e5b33SStephen Boyd 	clk = devm_clk_get(dev, "se");
557ea1e5b33SStephen Boyd 	if (IS_ERR(clk))
5586a34e285SAlok Chauhan 		return PTR_ERR(clk);
559561de45fSGirish Mahadevan 
560ea1e5b33SStephen Boyd 	spi = spi_alloc_master(dev, sizeof(*mas));
561561de45fSGirish Mahadevan 	if (!spi)
562561de45fSGirish Mahadevan 		return -ENOMEM;
563561de45fSGirish Mahadevan 
564561de45fSGirish Mahadevan 	platform_set_drvdata(pdev, spi);
565561de45fSGirish Mahadevan 	mas = spi_master_get_devdata(spi);
5666a34e285SAlok Chauhan 	mas->irq = irq;
567ea1e5b33SStephen Boyd 	mas->dev = dev;
568ea1e5b33SStephen Boyd 	mas->se.dev = dev;
569ea1e5b33SStephen Boyd 	mas->se.wrapper = dev_get_drvdata(dev->parent);
5706a34e285SAlok Chauhan 	mas->se.base = base;
5716a34e285SAlok Chauhan 	mas->se.clk = clk;
5721a9e489eSRajendra Nayak 	mas->se.opp_table = dev_pm_opp_set_clkname(&pdev->dev, "se");
5731a9e489eSRajendra Nayak 	if (IS_ERR(mas->se.opp_table))
5741a9e489eSRajendra Nayak 		return PTR_ERR(mas->se.opp_table);
5751a9e489eSRajendra Nayak 	/* OPP table is optional */
5761a9e489eSRajendra Nayak 	ret = dev_pm_opp_of_add_table(&pdev->dev);
5771a9e489eSRajendra Nayak 	if (!ret) {
5781a9e489eSRajendra Nayak 		mas->se.has_opp_table = true;
5791a9e489eSRajendra Nayak 	} else if (ret != -ENODEV) {
5801a9e489eSRajendra Nayak 		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
5811a9e489eSRajendra Nayak 		return ret;
5821a9e489eSRajendra Nayak 	}
583561de45fSGirish Mahadevan 
584561de45fSGirish Mahadevan 	spi->bus_num = -1;
585ea1e5b33SStephen Boyd 	spi->dev.of_node = dev->of_node;
586561de45fSGirish Mahadevan 	spi->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_CS_HIGH;
587561de45fSGirish Mahadevan 	spi->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
588561de45fSGirish Mahadevan 	spi->num_chipselect = 4;
589561de45fSGirish Mahadevan 	spi->max_speed_hz = 50000000;
590561de45fSGirish Mahadevan 	spi->prepare_message = spi_geni_prepare_message;
591561de45fSGirish Mahadevan 	spi->transfer_one = spi_geni_transfer_one;
592561de45fSGirish Mahadevan 	spi->auto_runtime_pm = true;
593561de45fSGirish Mahadevan 	spi->handle_err = handle_fifo_timeout;
594561de45fSGirish Mahadevan 	spi->set_cs = spi_geni_set_cs;
595561de45fSGirish Mahadevan 
596561de45fSGirish Mahadevan 	init_completion(&mas->xfer_done);
597561de45fSGirish Mahadevan 	spin_lock_init(&mas->lock);
598ea1e5b33SStephen Boyd 	pm_runtime_enable(dev);
599561de45fSGirish Mahadevan 
6000e3b8a81SAkash Asthana 	ret = geni_icc_get(&mas->se, NULL);
6010e3b8a81SAkash Asthana 	if (ret)
6020e3b8a81SAkash Asthana 		goto spi_geni_probe_runtime_disable;
6030e3b8a81SAkash Asthana 	/* Set the bus quota to a reasonable value for register access */
6040e3b8a81SAkash Asthana 	mas->se.icc_paths[GENI_TO_CORE].avg_bw = Bps_to_icc(CORE_2X_50_MHZ);
6050e3b8a81SAkash Asthana 	mas->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
6060e3b8a81SAkash Asthana 
6070e3b8a81SAkash Asthana 	ret = geni_icc_set_bw(&mas->se);
6080e3b8a81SAkash Asthana 	if (ret)
6090e3b8a81SAkash Asthana 		goto spi_geni_probe_runtime_disable;
6100e3b8a81SAkash Asthana 
611561de45fSGirish Mahadevan 	ret = spi_geni_init(mas);
612561de45fSGirish Mahadevan 	if (ret)
613561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
614561de45fSGirish Mahadevan 
615ea1e5b33SStephen Boyd 	ret = request_irq(mas->irq, geni_spi_isr, 0, dev_name(dev), spi);
616561de45fSGirish Mahadevan 	if (ret)
617561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
618561de45fSGirish Mahadevan 
619561de45fSGirish Mahadevan 	ret = spi_register_master(spi);
620561de45fSGirish Mahadevan 	if (ret)
621561de45fSGirish Mahadevan 		goto spi_geni_probe_free_irq;
622561de45fSGirish Mahadevan 
623561de45fSGirish Mahadevan 	return 0;
624561de45fSGirish Mahadevan spi_geni_probe_free_irq:
625561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
626561de45fSGirish Mahadevan spi_geni_probe_runtime_disable:
627ea1e5b33SStephen Boyd 	pm_runtime_disable(dev);
628561de45fSGirish Mahadevan 	spi_master_put(spi);
6291a9e489eSRajendra Nayak 	if (mas->se.has_opp_table)
6301a9e489eSRajendra Nayak 		dev_pm_opp_of_remove_table(&pdev->dev);
6311a9e489eSRajendra Nayak 	dev_pm_opp_put_clkname(mas->se.opp_table);
632561de45fSGirish Mahadevan 	return ret;
633561de45fSGirish Mahadevan }
634561de45fSGirish Mahadevan 
635561de45fSGirish Mahadevan static int spi_geni_remove(struct platform_device *pdev)
636561de45fSGirish Mahadevan {
637561de45fSGirish Mahadevan 	struct spi_master *spi = platform_get_drvdata(pdev);
638561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
639561de45fSGirish Mahadevan 
640561de45fSGirish Mahadevan 	/* Unregister _before_ disabling pm_runtime() so we stop transfers */
641561de45fSGirish Mahadevan 	spi_unregister_master(spi);
642561de45fSGirish Mahadevan 
643561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
644561de45fSGirish Mahadevan 	pm_runtime_disable(&pdev->dev);
6451a9e489eSRajendra Nayak 	if (mas->se.has_opp_table)
6461a9e489eSRajendra Nayak 		dev_pm_opp_of_remove_table(&pdev->dev);
6471a9e489eSRajendra Nayak 	dev_pm_opp_put_clkname(mas->se.opp_table);
648561de45fSGirish Mahadevan 	return 0;
649561de45fSGirish Mahadevan }
650561de45fSGirish Mahadevan 
651561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_suspend(struct device *dev)
652561de45fSGirish Mahadevan {
653561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
654561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
6550e3b8a81SAkash Asthana 	int ret;
656561de45fSGirish Mahadevan 
6571a9e489eSRajendra Nayak 	/* Drop the performance state vote */
6581a9e489eSRajendra Nayak 	dev_pm_opp_set_rate(dev, 0);
6591a9e489eSRajendra Nayak 
6600e3b8a81SAkash Asthana 	ret = geni_se_resources_off(&mas->se);
6610e3b8a81SAkash Asthana 	if (ret)
6620e3b8a81SAkash Asthana 		return ret;
6630e3b8a81SAkash Asthana 
6640e3b8a81SAkash Asthana 	return geni_icc_disable(&mas->se);
665561de45fSGirish Mahadevan }
666561de45fSGirish Mahadevan 
667561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_resume(struct device *dev)
668561de45fSGirish Mahadevan {
669561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
670561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
6710e3b8a81SAkash Asthana 	int ret;
6720e3b8a81SAkash Asthana 
6730e3b8a81SAkash Asthana 	ret = geni_icc_enable(&mas->se);
6740e3b8a81SAkash Asthana 	if (ret)
6750e3b8a81SAkash Asthana 		return ret;
676561de45fSGirish Mahadevan 
677561de45fSGirish Mahadevan 	return geni_se_resources_on(&mas->se);
678561de45fSGirish Mahadevan }
679561de45fSGirish Mahadevan 
680561de45fSGirish Mahadevan static int __maybe_unused spi_geni_suspend(struct device *dev)
681561de45fSGirish Mahadevan {
682561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
683561de45fSGirish Mahadevan 	int ret;
684561de45fSGirish Mahadevan 
685561de45fSGirish Mahadevan 	ret = spi_master_suspend(spi);
686561de45fSGirish Mahadevan 	if (ret)
687561de45fSGirish Mahadevan 		return ret;
688561de45fSGirish Mahadevan 
689561de45fSGirish Mahadevan 	ret = pm_runtime_force_suspend(dev);
690561de45fSGirish Mahadevan 	if (ret)
691561de45fSGirish Mahadevan 		spi_master_resume(spi);
692561de45fSGirish Mahadevan 
693561de45fSGirish Mahadevan 	return ret;
694561de45fSGirish Mahadevan }
695561de45fSGirish Mahadevan 
696561de45fSGirish Mahadevan static int __maybe_unused spi_geni_resume(struct device *dev)
697561de45fSGirish Mahadevan {
698561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
699561de45fSGirish Mahadevan 	int ret;
700561de45fSGirish Mahadevan 
701561de45fSGirish Mahadevan 	ret = pm_runtime_force_resume(dev);
702561de45fSGirish Mahadevan 	if (ret)
703561de45fSGirish Mahadevan 		return ret;
704561de45fSGirish Mahadevan 
705561de45fSGirish Mahadevan 	ret = spi_master_resume(spi);
706561de45fSGirish Mahadevan 	if (ret)
707561de45fSGirish Mahadevan 		pm_runtime_force_suspend(dev);
708561de45fSGirish Mahadevan 
709561de45fSGirish Mahadevan 	return ret;
710561de45fSGirish Mahadevan }
711561de45fSGirish Mahadevan 
712561de45fSGirish Mahadevan static const struct dev_pm_ops spi_geni_pm_ops = {
713561de45fSGirish Mahadevan 	SET_RUNTIME_PM_OPS(spi_geni_runtime_suspend,
714561de45fSGirish Mahadevan 					spi_geni_runtime_resume, NULL)
715561de45fSGirish Mahadevan 	SET_SYSTEM_SLEEP_PM_OPS(spi_geni_suspend, spi_geni_resume)
716561de45fSGirish Mahadevan };
717561de45fSGirish Mahadevan 
718561de45fSGirish Mahadevan static const struct of_device_id spi_geni_dt_match[] = {
719561de45fSGirish Mahadevan 	{ .compatible = "qcom,geni-spi" },
720561de45fSGirish Mahadevan 	{}
721561de45fSGirish Mahadevan };
722561de45fSGirish Mahadevan MODULE_DEVICE_TABLE(of, spi_geni_dt_match);
723561de45fSGirish Mahadevan 
724561de45fSGirish Mahadevan static struct platform_driver spi_geni_driver = {
725561de45fSGirish Mahadevan 	.probe  = spi_geni_probe,
726561de45fSGirish Mahadevan 	.remove = spi_geni_remove,
727561de45fSGirish Mahadevan 	.driver = {
728561de45fSGirish Mahadevan 		.name = "geni_spi",
729561de45fSGirish Mahadevan 		.pm = &spi_geni_pm_ops,
730561de45fSGirish Mahadevan 		.of_match_table = spi_geni_dt_match,
731561de45fSGirish Mahadevan 	},
732561de45fSGirish Mahadevan };
733561de45fSGirish Mahadevan module_platform_driver(spi_geni_driver);
734561de45fSGirish Mahadevan 
735561de45fSGirish Mahadevan MODULE_DESCRIPTION("SPI driver for GENI based QUP cores");
736561de45fSGirish Mahadevan MODULE_LICENSE("GPL v2");
737