xref: /linux/drivers/spi/spi-geni-qcom.c (revision 5f219524ec6eae3591f9d7cfbf4d30fc457f1eec)
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;
81*5f219524SDouglas Anderson 	unsigned long cur_sclk_hz;
82561de45fSGirish Mahadevan 	unsigned int cur_bits_per_word;
83561de45fSGirish Mahadevan 	unsigned int tx_rem_bytes;
84561de45fSGirish Mahadevan 	unsigned int rx_rem_bytes;
85561de45fSGirish Mahadevan 	const struct spi_transfer *cur_xfer;
86561de45fSGirish Mahadevan 	struct completion xfer_done;
87561de45fSGirish Mahadevan 	unsigned int oversampling;
88561de45fSGirish Mahadevan 	spinlock_t lock;
890dccff3cSAlok Chauhan 	enum spi_m_cmd_opcode cur_mcmd;
90561de45fSGirish Mahadevan 	int irq;
91561de45fSGirish Mahadevan };
92561de45fSGirish Mahadevan 
93561de45fSGirish Mahadevan static int get_spi_clk_cfg(unsigned int speed_hz,
94561de45fSGirish Mahadevan 			struct spi_geni_master *mas,
95561de45fSGirish Mahadevan 			unsigned int *clk_idx,
96561de45fSGirish Mahadevan 			unsigned int *clk_div)
97561de45fSGirish Mahadevan {
98561de45fSGirish Mahadevan 	unsigned long sclk_freq;
99561de45fSGirish Mahadevan 	unsigned int actual_hz;
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);
1161a9e489eSRajendra Nayak 	ret = dev_pm_opp_set_rate(mas->dev, sclk_freq);
117561de45fSGirish Mahadevan 	if (ret)
1181a9e489eSRajendra Nayak 		dev_err(mas->dev, "dev_pm_opp_set_rate failed %d\n", ret);
119*5f219524SDouglas Anderson 	else
120*5f219524SDouglas Anderson 		mas->cur_sclk_hz = sclk_freq;
121*5f219524SDouglas Anderson 
122561de45fSGirish Mahadevan 	return ret;
123561de45fSGirish Mahadevan }
124561de45fSGirish Mahadevan 
125de43affeSStephen Boyd static void handle_fifo_timeout(struct spi_master *spi,
126de43affeSStephen Boyd 				struct spi_message *msg)
127de43affeSStephen Boyd {
128de43affeSStephen Boyd 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
129de43affeSStephen Boyd 	unsigned long time_left, flags;
130de43affeSStephen Boyd 	struct geni_se *se = &mas->se;
131de43affeSStephen Boyd 
132de43affeSStephen Boyd 	spin_lock_irqsave(&mas->lock, flags);
133de43affeSStephen Boyd 	reinit_completion(&mas->xfer_done);
134de43affeSStephen Boyd 	mas->cur_mcmd = CMD_CANCEL;
135de43affeSStephen Boyd 	geni_se_cancel_m_cmd(se);
136de43affeSStephen Boyd 	writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
137de43affeSStephen Boyd 	spin_unlock_irqrestore(&mas->lock, flags);
138de43affeSStephen Boyd 	time_left = wait_for_completion_timeout(&mas->xfer_done, HZ);
139de43affeSStephen Boyd 	if (time_left)
140de43affeSStephen Boyd 		return;
141de43affeSStephen Boyd 
142de43affeSStephen Boyd 	spin_lock_irqsave(&mas->lock, flags);
143de43affeSStephen Boyd 	reinit_completion(&mas->xfer_done);
144de43affeSStephen Boyd 	geni_se_abort_m_cmd(se);
145de43affeSStephen Boyd 	spin_unlock_irqrestore(&mas->lock, flags);
146de43affeSStephen Boyd 	time_left = wait_for_completion_timeout(&mas->xfer_done, HZ);
147de43affeSStephen Boyd 	if (!time_left)
148de43affeSStephen Boyd 		dev_err(mas->dev, "Failed to cancel/abort m_cmd\n");
149de43affeSStephen Boyd }
150de43affeSStephen Boyd 
151561de45fSGirish Mahadevan static void spi_geni_set_cs(struct spi_device *slv, bool set_flag)
152561de45fSGirish Mahadevan {
153561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(slv->master);
154561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(mas->dev);
155561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
1560dccff3cSAlok Chauhan 	unsigned long time_left;
157561de45fSGirish Mahadevan 
158561de45fSGirish Mahadevan 	reinit_completion(&mas->xfer_done);
159561de45fSGirish Mahadevan 	pm_runtime_get_sync(mas->dev);
160561de45fSGirish Mahadevan 	if (!(slv->mode & SPI_CS_HIGH))
161561de45fSGirish Mahadevan 		set_flag = !set_flag;
162561de45fSGirish Mahadevan 
163561de45fSGirish Mahadevan 	mas->cur_mcmd = CMD_CS;
164561de45fSGirish Mahadevan 	if (set_flag)
165561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_ASSERT, 0);
166561de45fSGirish Mahadevan 	else
167561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_DEASSERT, 0);
168561de45fSGirish Mahadevan 
1690dccff3cSAlok Chauhan 	time_left = wait_for_completion_timeout(&mas->xfer_done, HZ);
1700dccff3cSAlok Chauhan 	if (!time_left)
171561de45fSGirish Mahadevan 		handle_fifo_timeout(spi, NULL);
172561de45fSGirish Mahadevan 
173561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
174561de45fSGirish Mahadevan }
175561de45fSGirish Mahadevan 
176561de45fSGirish Mahadevan static void spi_setup_word_len(struct spi_geni_master *mas, u16 mode,
177561de45fSGirish Mahadevan 					unsigned int bits_per_word)
178561de45fSGirish Mahadevan {
179561de45fSGirish Mahadevan 	unsigned int pack_words;
180561de45fSGirish Mahadevan 	bool msb_first = (mode & SPI_LSB_FIRST) ? false : true;
181561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
182561de45fSGirish Mahadevan 	u32 word_len;
183561de45fSGirish Mahadevan 
184561de45fSGirish Mahadevan 	word_len = readl(se->base + SE_SPI_WORD_LEN);
185561de45fSGirish Mahadevan 
186561de45fSGirish Mahadevan 	/*
187561de45fSGirish Mahadevan 	 * If bits_per_word isn't a byte aligned value, set the packing to be
188561de45fSGirish Mahadevan 	 * 1 SPI word per FIFO word.
189561de45fSGirish Mahadevan 	 */
190561de45fSGirish Mahadevan 	if (!(mas->fifo_width_bits % bits_per_word))
191561de45fSGirish Mahadevan 		pack_words = mas->fifo_width_bits / bits_per_word;
192561de45fSGirish Mahadevan 	else
193561de45fSGirish Mahadevan 		pack_words = 1;
194561de45fSGirish Mahadevan 	word_len &= ~WORD_LEN_MSK;
195561de45fSGirish Mahadevan 	word_len |= ((bits_per_word - MIN_WORD_LEN) & WORD_LEN_MSK);
196561de45fSGirish Mahadevan 	geni_se_config_packing(&mas->se, bits_per_word, pack_words, msb_first,
197561de45fSGirish Mahadevan 								true, true);
198561de45fSGirish Mahadevan 	writel(word_len, se->base + SE_SPI_WORD_LEN);
199561de45fSGirish Mahadevan }
200561de45fSGirish Mahadevan 
2010e3b8a81SAkash Asthana static int geni_spi_set_clock_and_bw(struct spi_geni_master *mas,
2020e3b8a81SAkash Asthana 					unsigned long clk_hz)
203e68b6624SDouglas Anderson {
204e68b6624SDouglas Anderson 	u32 clk_sel, m_clk_cfg, idx, div;
205e68b6624SDouglas Anderson 	struct geni_se *se = &mas->se;
206e68b6624SDouglas Anderson 	int ret;
207e68b6624SDouglas Anderson 
20868890e20SDouglas Anderson 	if (clk_hz == mas->cur_speed_hz)
20968890e20SDouglas Anderson 		return 0;
21068890e20SDouglas Anderson 
211e68b6624SDouglas Anderson 	ret = get_spi_clk_cfg(clk_hz, mas, &idx, &div);
212e68b6624SDouglas Anderson 	if (ret) {
213e68b6624SDouglas Anderson 		dev_err(mas->dev, "Err setting clk to %lu: %d\n", clk_hz, ret);
214e68b6624SDouglas Anderson 		return ret;
215e68b6624SDouglas Anderson 	}
216e68b6624SDouglas Anderson 
217e68b6624SDouglas Anderson 	/*
218e68b6624SDouglas Anderson 	 * SPI core clock gets configured with the requested frequency
219e68b6624SDouglas Anderson 	 * or the frequency closer to the requested frequency.
220e68b6624SDouglas Anderson 	 * For that reason requested frequency is stored in the
221e68b6624SDouglas Anderson 	 * cur_speed_hz and referred in the consecutive transfer instead
222e68b6624SDouglas Anderson 	 * of calling clk_get_rate() API.
223e68b6624SDouglas Anderson 	 */
224e68b6624SDouglas Anderson 	mas->cur_speed_hz = clk_hz;
225e68b6624SDouglas Anderson 
226e68b6624SDouglas Anderson 	clk_sel = idx & CLK_SEL_MSK;
227e68b6624SDouglas Anderson 	m_clk_cfg = (div << CLK_DIV_SHFT) | SER_CLK_EN;
228e68b6624SDouglas Anderson 	writel(clk_sel, se->base + SE_GENI_CLK_SEL);
229e68b6624SDouglas Anderson 	writel(m_clk_cfg, se->base + GENI_SER_M_CLK_CFG);
230e68b6624SDouglas Anderson 
2310e3b8a81SAkash Asthana 	/* Set BW quota for CPU as driver supports FIFO mode only. */
2320e3b8a81SAkash Asthana 	se->icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(mas->cur_speed_hz);
2330e3b8a81SAkash Asthana 	ret = geni_icc_set_bw(se);
2340e3b8a81SAkash Asthana 	if (ret)
2350e3b8a81SAkash Asthana 		return ret;
2360e3b8a81SAkash Asthana 
237e68b6624SDouglas Anderson 	return 0;
238e68b6624SDouglas Anderson }
239e68b6624SDouglas Anderson 
240561de45fSGirish Mahadevan static int setup_fifo_params(struct spi_device *spi_slv,
241561de45fSGirish Mahadevan 					struct spi_master *spi)
242561de45fSGirish Mahadevan {
243561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
244561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
245561de45fSGirish Mahadevan 	u32 loopback_cfg, cpol, cpha, demux_output_inv;
246e68b6624SDouglas Anderson 	u32 demux_sel;
247561de45fSGirish Mahadevan 
248561de45fSGirish Mahadevan 	loopback_cfg = readl(se->base + SE_SPI_LOOPBACK);
249561de45fSGirish Mahadevan 	cpol = readl(se->base + SE_SPI_CPOL);
250561de45fSGirish Mahadevan 	cpha = readl(se->base + SE_SPI_CPHA);
251561de45fSGirish Mahadevan 	demux_output_inv = 0;
252561de45fSGirish Mahadevan 	loopback_cfg &= ~LOOPBACK_MSK;
253561de45fSGirish Mahadevan 	cpol &= ~CPOL;
254561de45fSGirish Mahadevan 	cpha &= ~CPHA;
255561de45fSGirish Mahadevan 
256561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_LOOP)
257561de45fSGirish Mahadevan 		loopback_cfg |= LOOPBACK_ENABLE;
258561de45fSGirish Mahadevan 
259561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CPOL)
260561de45fSGirish Mahadevan 		cpol |= CPOL;
261561de45fSGirish Mahadevan 
262561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CPHA)
263561de45fSGirish Mahadevan 		cpha |= CPHA;
264561de45fSGirish Mahadevan 
265561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CS_HIGH)
266561de45fSGirish Mahadevan 		demux_output_inv = BIT(spi_slv->chip_select);
267561de45fSGirish Mahadevan 
268561de45fSGirish Mahadevan 	demux_sel = spi_slv->chip_select;
269561de45fSGirish Mahadevan 	mas->cur_bits_per_word = spi_slv->bits_per_word;
270561de45fSGirish Mahadevan 
271561de45fSGirish Mahadevan 	spi_setup_word_len(mas, spi_slv->mode, spi_slv->bits_per_word);
272561de45fSGirish Mahadevan 	writel(loopback_cfg, se->base + SE_SPI_LOOPBACK);
273561de45fSGirish Mahadevan 	writel(demux_sel, se->base + SE_SPI_DEMUX_SEL);
274561de45fSGirish Mahadevan 	writel(cpha, se->base + SE_SPI_CPHA);
275561de45fSGirish Mahadevan 	writel(cpol, se->base + SE_SPI_CPOL);
276561de45fSGirish Mahadevan 	writel(demux_output_inv, se->base + SE_SPI_DEMUX_OUTPUT_INV);
277e68b6624SDouglas Anderson 
2780e3b8a81SAkash Asthana 	return geni_spi_set_clock_and_bw(mas, spi_slv->max_speed_hz);
279561de45fSGirish Mahadevan }
280561de45fSGirish Mahadevan 
281561de45fSGirish Mahadevan static int spi_geni_prepare_message(struct spi_master *spi,
282561de45fSGirish Mahadevan 					struct spi_message *spi_msg)
283561de45fSGirish Mahadevan {
284561de45fSGirish Mahadevan 	int ret;
285561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
286561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
287561de45fSGirish Mahadevan 
288561de45fSGirish Mahadevan 	geni_se_select_mode(se, GENI_SE_FIFO);
289561de45fSGirish Mahadevan 	ret = setup_fifo_params(spi_msg->spi, spi);
290561de45fSGirish Mahadevan 	if (ret)
291561de45fSGirish Mahadevan 		dev_err(mas->dev, "Couldn't select mode %d\n", ret);
292561de45fSGirish Mahadevan 	return ret;
293561de45fSGirish Mahadevan }
294561de45fSGirish Mahadevan 
295561de45fSGirish Mahadevan static int spi_geni_init(struct spi_geni_master *mas)
296561de45fSGirish Mahadevan {
297561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
298561de45fSGirish Mahadevan 	unsigned int proto, major, minor, ver;
299561de45fSGirish Mahadevan 
300561de45fSGirish Mahadevan 	pm_runtime_get_sync(mas->dev);
301561de45fSGirish Mahadevan 
302561de45fSGirish Mahadevan 	proto = geni_se_read_proto(se);
303561de45fSGirish Mahadevan 	if (proto != GENI_SE_SPI) {
304561de45fSGirish Mahadevan 		dev_err(mas->dev, "Invalid proto %d\n", proto);
305561de45fSGirish Mahadevan 		pm_runtime_put(mas->dev);
306561de45fSGirish Mahadevan 		return -ENXIO;
307561de45fSGirish Mahadevan 	}
308561de45fSGirish Mahadevan 	mas->tx_fifo_depth = geni_se_get_tx_fifo_depth(se);
309561de45fSGirish Mahadevan 
310561de45fSGirish Mahadevan 	/* Width of Tx and Rx FIFO is same */
311561de45fSGirish Mahadevan 	mas->fifo_width_bits = geni_se_get_tx_fifo_width(se);
312561de45fSGirish Mahadevan 
313561de45fSGirish Mahadevan 	/*
314561de45fSGirish Mahadevan 	 * Hardware programming guide suggests to configure
315561de45fSGirish Mahadevan 	 * RX FIFO RFR level to fifo_depth-2.
316561de45fSGirish Mahadevan 	 */
317561de45fSGirish Mahadevan 	geni_se_init(se, 0x0, mas->tx_fifo_depth - 2);
318561de45fSGirish Mahadevan 	/* Transmit an entire FIFO worth of data per IRQ */
319561de45fSGirish Mahadevan 	mas->tx_wm = 1;
320561de45fSGirish Mahadevan 	ver = geni_se_get_qup_hw_version(se);
321561de45fSGirish Mahadevan 	major = GENI_SE_VERSION_MAJOR(ver);
322561de45fSGirish Mahadevan 	minor = GENI_SE_VERSION_MINOR(ver);
323561de45fSGirish Mahadevan 
324561de45fSGirish Mahadevan 	if (major == 1 && minor == 0)
325561de45fSGirish Mahadevan 		mas->oversampling = 2;
326561de45fSGirish Mahadevan 	else
327561de45fSGirish Mahadevan 		mas->oversampling = 1;
328561de45fSGirish Mahadevan 
329561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
330561de45fSGirish Mahadevan 	return 0;
331561de45fSGirish Mahadevan }
332561de45fSGirish Mahadevan 
333561de45fSGirish Mahadevan static void setup_fifo_xfer(struct spi_transfer *xfer,
334561de45fSGirish Mahadevan 				struct spi_geni_master *mas,
335561de45fSGirish Mahadevan 				u16 mode, struct spi_master *spi)
336561de45fSGirish Mahadevan {
337561de45fSGirish Mahadevan 	u32 m_cmd = 0;
338561de45fSGirish Mahadevan 	u32 spi_tx_cfg, len;
339561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
340e68b6624SDouglas Anderson 	int ret;
341561de45fSGirish Mahadevan 
342561de45fSGirish Mahadevan 	spi_tx_cfg = readl(se->base + SE_SPI_TRANS_CFG);
343561de45fSGirish Mahadevan 	if (xfer->bits_per_word != mas->cur_bits_per_word) {
344561de45fSGirish Mahadevan 		spi_setup_word_len(mas, mode, xfer->bits_per_word);
345561de45fSGirish Mahadevan 		mas->cur_bits_per_word = xfer->bits_per_word;
346561de45fSGirish Mahadevan 	}
347561de45fSGirish Mahadevan 
348561de45fSGirish Mahadevan 	/* Speed and bits per word can be overridden per transfer */
3490e3b8a81SAkash Asthana 	ret = geni_spi_set_clock_and_bw(mas, xfer->speed_hz);
350e68b6624SDouglas Anderson 	if (ret)
351561de45fSGirish Mahadevan 		return;
352561de45fSGirish Mahadevan 
353561de45fSGirish Mahadevan 	mas->tx_rem_bytes = 0;
354561de45fSGirish Mahadevan 	mas->rx_rem_bytes = 0;
355561de45fSGirish Mahadevan 	if (xfer->tx_buf && xfer->rx_buf)
356561de45fSGirish Mahadevan 		m_cmd = SPI_FULL_DUPLEX;
357561de45fSGirish Mahadevan 	else if (xfer->tx_buf)
358561de45fSGirish Mahadevan 		m_cmd = SPI_TX_ONLY;
359561de45fSGirish Mahadevan 	else if (xfer->rx_buf)
360561de45fSGirish Mahadevan 		m_cmd = SPI_RX_ONLY;
361561de45fSGirish Mahadevan 
362561de45fSGirish Mahadevan 	spi_tx_cfg &= ~CS_TOGGLE;
363561de45fSGirish Mahadevan 
364561de45fSGirish Mahadevan 	if (!(mas->cur_bits_per_word % MIN_WORD_LEN))
365561de45fSGirish Mahadevan 		len = xfer->len * BITS_PER_BYTE / mas->cur_bits_per_word;
366561de45fSGirish Mahadevan 	else
367561de45fSGirish Mahadevan 		len = xfer->len / (mas->cur_bits_per_word / BITS_PER_BYTE + 1);
368561de45fSGirish Mahadevan 	len &= TRANS_LEN_MSK;
369561de45fSGirish Mahadevan 
370561de45fSGirish Mahadevan 	mas->cur_xfer = xfer;
371561de45fSGirish Mahadevan 	if (m_cmd & SPI_TX_ONLY) {
372561de45fSGirish Mahadevan 		mas->tx_rem_bytes = xfer->len;
373561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_TX_TRANS_LEN);
374561de45fSGirish Mahadevan 	}
375561de45fSGirish Mahadevan 
376561de45fSGirish Mahadevan 	if (m_cmd & SPI_RX_ONLY) {
377561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_RX_TRANS_LEN);
378561de45fSGirish Mahadevan 		mas->rx_rem_bytes = xfer->len;
379561de45fSGirish Mahadevan 	}
380561de45fSGirish Mahadevan 	writel(spi_tx_cfg, se->base + SE_SPI_TRANS_CFG);
381561de45fSGirish Mahadevan 	mas->cur_mcmd = CMD_XFER;
382561de45fSGirish Mahadevan 	geni_se_setup_m_cmd(se, m_cmd, FRAGMENTATION);
383561de45fSGirish Mahadevan 
384561de45fSGirish Mahadevan 	/*
385561de45fSGirish Mahadevan 	 * TX_WATERMARK_REG should be set after SPI configuration and
386561de45fSGirish Mahadevan 	 * setting up GENI SE engine, as driver starts data transfer
387561de45fSGirish Mahadevan 	 * for the watermark interrupt.
388561de45fSGirish Mahadevan 	 */
389561de45fSGirish Mahadevan 	if (m_cmd & SPI_TX_ONLY)
390561de45fSGirish Mahadevan 		writel(mas->tx_wm, se->base + SE_GENI_TX_WATERMARK_REG);
391561de45fSGirish Mahadevan }
392561de45fSGirish Mahadevan 
393561de45fSGirish Mahadevan static int spi_geni_transfer_one(struct spi_master *spi,
394561de45fSGirish Mahadevan 				struct spi_device *slv,
395561de45fSGirish Mahadevan 				struct spi_transfer *xfer)
396561de45fSGirish Mahadevan {
397561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
398561de45fSGirish Mahadevan 
399561de45fSGirish Mahadevan 	/* Terminate and return success for 0 byte length transfer */
400561de45fSGirish Mahadevan 	if (!xfer->len)
401561de45fSGirish Mahadevan 		return 0;
402561de45fSGirish Mahadevan 
403561de45fSGirish Mahadevan 	setup_fifo_xfer(xfer, mas, slv->mode, spi);
404561de45fSGirish Mahadevan 	return 1;
405561de45fSGirish Mahadevan }
406561de45fSGirish Mahadevan 
407561de45fSGirish Mahadevan static unsigned int geni_byte_per_fifo_word(struct spi_geni_master *mas)
408561de45fSGirish Mahadevan {
409561de45fSGirish Mahadevan 	/*
410561de45fSGirish Mahadevan 	 * Calculate how many bytes we'll put in each FIFO word.  If the
411561de45fSGirish Mahadevan 	 * transfer words don't pack cleanly into a FIFO word we'll just put
412561de45fSGirish Mahadevan 	 * one transfer word in each FIFO word.  If they do pack we'll pack 'em.
413561de45fSGirish Mahadevan 	 */
414561de45fSGirish Mahadevan 	if (mas->fifo_width_bits % mas->cur_bits_per_word)
415561de45fSGirish Mahadevan 		return roundup_pow_of_two(DIV_ROUND_UP(mas->cur_bits_per_word,
416561de45fSGirish Mahadevan 						       BITS_PER_BYTE));
417561de45fSGirish Mahadevan 
418561de45fSGirish Mahadevan 	return mas->fifo_width_bits / BITS_PER_BYTE;
419561de45fSGirish Mahadevan }
420561de45fSGirish Mahadevan 
421561de45fSGirish Mahadevan static void geni_spi_handle_tx(struct spi_geni_master *mas)
422561de45fSGirish Mahadevan {
423561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
424561de45fSGirish Mahadevan 	unsigned int max_bytes;
425561de45fSGirish Mahadevan 	const u8 *tx_buf;
426561de45fSGirish Mahadevan 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
427561de45fSGirish Mahadevan 	unsigned int i = 0;
428561de45fSGirish Mahadevan 
429561de45fSGirish Mahadevan 	max_bytes = (mas->tx_fifo_depth - mas->tx_wm) * bytes_per_fifo_word;
430561de45fSGirish Mahadevan 	if (mas->tx_rem_bytes < max_bytes)
431561de45fSGirish Mahadevan 		max_bytes = mas->tx_rem_bytes;
432561de45fSGirish Mahadevan 
433561de45fSGirish Mahadevan 	tx_buf = mas->cur_xfer->tx_buf + mas->cur_xfer->len - mas->tx_rem_bytes;
434561de45fSGirish Mahadevan 	while (i < max_bytes) {
435561de45fSGirish Mahadevan 		unsigned int j;
436561de45fSGirish Mahadevan 		unsigned int bytes_to_write;
437561de45fSGirish Mahadevan 		u32 fifo_word = 0;
438561de45fSGirish Mahadevan 		u8 *fifo_byte = (u8 *)&fifo_word;
439561de45fSGirish Mahadevan 
440561de45fSGirish Mahadevan 		bytes_to_write = min(bytes_per_fifo_word, max_bytes - i);
441561de45fSGirish Mahadevan 		for (j = 0; j < bytes_to_write; j++)
442561de45fSGirish Mahadevan 			fifo_byte[j] = tx_buf[i++];
443561de45fSGirish Mahadevan 		iowrite32_rep(se->base + SE_GENI_TX_FIFOn, &fifo_word, 1);
444561de45fSGirish Mahadevan 	}
445561de45fSGirish Mahadevan 	mas->tx_rem_bytes -= max_bytes;
446561de45fSGirish Mahadevan 	if (!mas->tx_rem_bytes)
447561de45fSGirish Mahadevan 		writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
448561de45fSGirish Mahadevan }
449561de45fSGirish Mahadevan 
450561de45fSGirish Mahadevan static void geni_spi_handle_rx(struct spi_geni_master *mas)
451561de45fSGirish Mahadevan {
452561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
453561de45fSGirish Mahadevan 	u32 rx_fifo_status;
454561de45fSGirish Mahadevan 	unsigned int rx_bytes;
455561de45fSGirish Mahadevan 	unsigned int rx_last_byte_valid;
456561de45fSGirish Mahadevan 	u8 *rx_buf;
457561de45fSGirish Mahadevan 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
458561de45fSGirish Mahadevan 	unsigned int i = 0;
459561de45fSGirish Mahadevan 
460561de45fSGirish Mahadevan 	rx_fifo_status = readl(se->base + SE_GENI_RX_FIFO_STATUS);
461561de45fSGirish Mahadevan 	rx_bytes = (rx_fifo_status & RX_FIFO_WC_MSK) * bytes_per_fifo_word;
462561de45fSGirish Mahadevan 	if (rx_fifo_status & RX_LAST) {
463561de45fSGirish Mahadevan 		rx_last_byte_valid = rx_fifo_status & RX_LAST_BYTE_VALID_MSK;
464561de45fSGirish Mahadevan 		rx_last_byte_valid >>= RX_LAST_BYTE_VALID_SHFT;
465561de45fSGirish Mahadevan 		if (rx_last_byte_valid && rx_last_byte_valid < 4)
466561de45fSGirish Mahadevan 			rx_bytes -= bytes_per_fifo_word - rx_last_byte_valid;
467561de45fSGirish Mahadevan 	}
468561de45fSGirish Mahadevan 	if (mas->rx_rem_bytes < rx_bytes)
469561de45fSGirish Mahadevan 		rx_bytes = mas->rx_rem_bytes;
470561de45fSGirish Mahadevan 
471561de45fSGirish Mahadevan 	rx_buf = mas->cur_xfer->rx_buf + mas->cur_xfer->len - mas->rx_rem_bytes;
472561de45fSGirish Mahadevan 	while (i < rx_bytes) {
473561de45fSGirish Mahadevan 		u32 fifo_word = 0;
474561de45fSGirish Mahadevan 		u8 *fifo_byte = (u8 *)&fifo_word;
475561de45fSGirish Mahadevan 		unsigned int bytes_to_read;
476561de45fSGirish Mahadevan 		unsigned int j;
477561de45fSGirish Mahadevan 
478561de45fSGirish Mahadevan 		bytes_to_read = min(bytes_per_fifo_word, rx_bytes - i);
479561de45fSGirish Mahadevan 		ioread32_rep(se->base + SE_GENI_RX_FIFOn, &fifo_word, 1);
480561de45fSGirish Mahadevan 		for (j = 0; j < bytes_to_read; j++)
481561de45fSGirish Mahadevan 			rx_buf[i++] = fifo_byte[j];
482561de45fSGirish Mahadevan 	}
483561de45fSGirish Mahadevan 	mas->rx_rem_bytes -= rx_bytes;
484561de45fSGirish Mahadevan }
485561de45fSGirish Mahadevan 
486561de45fSGirish Mahadevan static irqreturn_t geni_spi_isr(int irq, void *data)
487561de45fSGirish Mahadevan {
488561de45fSGirish Mahadevan 	struct spi_master *spi = data;
489561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
490561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
491561de45fSGirish Mahadevan 	u32 m_irq;
492561de45fSGirish Mahadevan 	unsigned long flags;
493561de45fSGirish Mahadevan 
494561de45fSGirish Mahadevan 	if (mas->cur_mcmd == CMD_NONE)
495561de45fSGirish Mahadevan 		return IRQ_NONE;
496561de45fSGirish Mahadevan 
497561de45fSGirish Mahadevan 	spin_lock_irqsave(&mas->lock, flags);
498561de45fSGirish Mahadevan 	m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
499561de45fSGirish Mahadevan 
500561de45fSGirish Mahadevan 	if ((m_irq & M_RX_FIFO_WATERMARK_EN) || (m_irq & M_RX_FIFO_LAST_EN))
501561de45fSGirish Mahadevan 		geni_spi_handle_rx(mas);
502561de45fSGirish Mahadevan 
503561de45fSGirish Mahadevan 	if (m_irq & M_TX_FIFO_WATERMARK_EN)
504561de45fSGirish Mahadevan 		geni_spi_handle_tx(mas);
505561de45fSGirish Mahadevan 
506561de45fSGirish Mahadevan 	if (m_irq & M_CMD_DONE_EN) {
507561de45fSGirish Mahadevan 		if (mas->cur_mcmd == CMD_XFER)
508561de45fSGirish Mahadevan 			spi_finalize_current_transfer(spi);
509561de45fSGirish Mahadevan 		else if (mas->cur_mcmd == CMD_CS)
510561de45fSGirish Mahadevan 			complete(&mas->xfer_done);
511561de45fSGirish Mahadevan 		mas->cur_mcmd = CMD_NONE;
512561de45fSGirish Mahadevan 		/*
513561de45fSGirish Mahadevan 		 * If this happens, then a CMD_DONE came before all the Tx
514561de45fSGirish Mahadevan 		 * buffer bytes were sent out. This is unusual, log this
515561de45fSGirish Mahadevan 		 * condition and disable the WM interrupt to prevent the
516561de45fSGirish Mahadevan 		 * system from stalling due an interrupt storm.
517561de45fSGirish Mahadevan 		 * If this happens when all Rx bytes haven't been received, log
518561de45fSGirish Mahadevan 		 * the condition.
519561de45fSGirish Mahadevan 		 * The only known time this can happen is if bits_per_word != 8
520561de45fSGirish Mahadevan 		 * and some registers that expect xfer lengths in num spi_words
521561de45fSGirish Mahadevan 		 * weren't written correctly.
522561de45fSGirish Mahadevan 		 */
523561de45fSGirish Mahadevan 		if (mas->tx_rem_bytes) {
524561de45fSGirish Mahadevan 			writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
525561de45fSGirish Mahadevan 			dev_err(mas->dev, "Premature done. tx_rem = %d bpw%d\n",
526561de45fSGirish Mahadevan 				mas->tx_rem_bytes, mas->cur_bits_per_word);
527561de45fSGirish Mahadevan 		}
528561de45fSGirish Mahadevan 		if (mas->rx_rem_bytes)
529561de45fSGirish Mahadevan 			dev_err(mas->dev, "Premature done. rx_rem = %d bpw%d\n",
530561de45fSGirish Mahadevan 				mas->rx_rem_bytes, mas->cur_bits_per_word);
531561de45fSGirish Mahadevan 	}
532561de45fSGirish Mahadevan 
533561de45fSGirish Mahadevan 	if ((m_irq & M_CMD_CANCEL_EN) || (m_irq & M_CMD_ABORT_EN)) {
534561de45fSGirish Mahadevan 		mas->cur_mcmd = CMD_NONE;
535561de45fSGirish Mahadevan 		complete(&mas->xfer_done);
536561de45fSGirish Mahadevan 	}
537561de45fSGirish Mahadevan 
538561de45fSGirish Mahadevan 	writel(m_irq, se->base + SE_GENI_M_IRQ_CLEAR);
539561de45fSGirish Mahadevan 	spin_unlock_irqrestore(&mas->lock, flags);
5400dccff3cSAlok Chauhan 	return IRQ_HANDLED;
541561de45fSGirish Mahadevan }
542561de45fSGirish Mahadevan 
543561de45fSGirish Mahadevan static int spi_geni_probe(struct platform_device *pdev)
544561de45fSGirish Mahadevan {
5456a34e285SAlok Chauhan 	int ret, irq;
546561de45fSGirish Mahadevan 	struct spi_master *spi;
547561de45fSGirish Mahadevan 	struct spi_geni_master *mas;
5486a34e285SAlok Chauhan 	void __iomem *base;
5496a34e285SAlok Chauhan 	struct clk *clk;
550ea1e5b33SStephen Boyd 	struct device *dev = &pdev->dev;
5516a34e285SAlok Chauhan 
5526a34e285SAlok Chauhan 	irq = platform_get_irq(pdev, 0);
5536b8ac10eSStephen Boyd 	if (irq < 0)
5546a34e285SAlok Chauhan 		return irq;
5556a34e285SAlok Chauhan 
556d8e477abSYueHaibing 	base = devm_platform_ioremap_resource(pdev, 0);
5576a34e285SAlok Chauhan 	if (IS_ERR(base))
5586a34e285SAlok Chauhan 		return PTR_ERR(base);
5596a34e285SAlok Chauhan 
560ea1e5b33SStephen Boyd 	clk = devm_clk_get(dev, "se");
561ea1e5b33SStephen Boyd 	if (IS_ERR(clk))
5626a34e285SAlok Chauhan 		return PTR_ERR(clk);
563561de45fSGirish Mahadevan 
564ea1e5b33SStephen Boyd 	spi = spi_alloc_master(dev, sizeof(*mas));
565561de45fSGirish Mahadevan 	if (!spi)
566561de45fSGirish Mahadevan 		return -ENOMEM;
567561de45fSGirish Mahadevan 
568561de45fSGirish Mahadevan 	platform_set_drvdata(pdev, spi);
569561de45fSGirish Mahadevan 	mas = spi_master_get_devdata(spi);
5706a34e285SAlok Chauhan 	mas->irq = irq;
571ea1e5b33SStephen Boyd 	mas->dev = dev;
572ea1e5b33SStephen Boyd 	mas->se.dev = dev;
573ea1e5b33SStephen Boyd 	mas->se.wrapper = dev_get_drvdata(dev->parent);
5746a34e285SAlok Chauhan 	mas->se.base = base;
5756a34e285SAlok Chauhan 	mas->se.clk = clk;
5761a9e489eSRajendra Nayak 	mas->se.opp_table = dev_pm_opp_set_clkname(&pdev->dev, "se");
5771a9e489eSRajendra Nayak 	if (IS_ERR(mas->se.opp_table))
5781a9e489eSRajendra Nayak 		return PTR_ERR(mas->se.opp_table);
5791a9e489eSRajendra Nayak 	/* OPP table is optional */
5801a9e489eSRajendra Nayak 	ret = dev_pm_opp_of_add_table(&pdev->dev);
5811a9e489eSRajendra Nayak 	if (!ret) {
5821a9e489eSRajendra Nayak 		mas->se.has_opp_table = true;
5831a9e489eSRajendra Nayak 	} else if (ret != -ENODEV) {
5841a9e489eSRajendra Nayak 		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
5851a9e489eSRajendra Nayak 		return ret;
5861a9e489eSRajendra Nayak 	}
587561de45fSGirish Mahadevan 
588561de45fSGirish Mahadevan 	spi->bus_num = -1;
589ea1e5b33SStephen Boyd 	spi->dev.of_node = dev->of_node;
590561de45fSGirish Mahadevan 	spi->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_CS_HIGH;
591561de45fSGirish Mahadevan 	spi->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
592561de45fSGirish Mahadevan 	spi->num_chipselect = 4;
593561de45fSGirish Mahadevan 	spi->max_speed_hz = 50000000;
594561de45fSGirish Mahadevan 	spi->prepare_message = spi_geni_prepare_message;
595561de45fSGirish Mahadevan 	spi->transfer_one = spi_geni_transfer_one;
596561de45fSGirish Mahadevan 	spi->auto_runtime_pm = true;
597561de45fSGirish Mahadevan 	spi->handle_err = handle_fifo_timeout;
598561de45fSGirish Mahadevan 	spi->set_cs = spi_geni_set_cs;
599561de45fSGirish Mahadevan 
600561de45fSGirish Mahadevan 	init_completion(&mas->xfer_done);
601561de45fSGirish Mahadevan 	spin_lock_init(&mas->lock);
602ea1e5b33SStephen Boyd 	pm_runtime_enable(dev);
603561de45fSGirish Mahadevan 
6040e3b8a81SAkash Asthana 	ret = geni_icc_get(&mas->se, NULL);
6050e3b8a81SAkash Asthana 	if (ret)
6060e3b8a81SAkash Asthana 		goto spi_geni_probe_runtime_disable;
6070e3b8a81SAkash Asthana 	/* Set the bus quota to a reasonable value for register access */
6080e3b8a81SAkash Asthana 	mas->se.icc_paths[GENI_TO_CORE].avg_bw = Bps_to_icc(CORE_2X_50_MHZ);
6090e3b8a81SAkash Asthana 	mas->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
6100e3b8a81SAkash Asthana 
6110e3b8a81SAkash Asthana 	ret = geni_icc_set_bw(&mas->se);
6120e3b8a81SAkash Asthana 	if (ret)
6130e3b8a81SAkash Asthana 		goto spi_geni_probe_runtime_disable;
6140e3b8a81SAkash Asthana 
615561de45fSGirish Mahadevan 	ret = spi_geni_init(mas);
616561de45fSGirish Mahadevan 	if (ret)
617561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
618561de45fSGirish Mahadevan 
619ea1e5b33SStephen Boyd 	ret = request_irq(mas->irq, geni_spi_isr, 0, dev_name(dev), spi);
620561de45fSGirish Mahadevan 	if (ret)
621561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
622561de45fSGirish Mahadevan 
623561de45fSGirish Mahadevan 	ret = spi_register_master(spi);
624561de45fSGirish Mahadevan 	if (ret)
625561de45fSGirish Mahadevan 		goto spi_geni_probe_free_irq;
626561de45fSGirish Mahadevan 
627561de45fSGirish Mahadevan 	return 0;
628561de45fSGirish Mahadevan spi_geni_probe_free_irq:
629561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
630561de45fSGirish Mahadevan spi_geni_probe_runtime_disable:
631ea1e5b33SStephen Boyd 	pm_runtime_disable(dev);
632561de45fSGirish Mahadevan 	spi_master_put(spi);
6331a9e489eSRajendra Nayak 	if (mas->se.has_opp_table)
6341a9e489eSRajendra Nayak 		dev_pm_opp_of_remove_table(&pdev->dev);
6351a9e489eSRajendra Nayak 	dev_pm_opp_put_clkname(mas->se.opp_table);
636561de45fSGirish Mahadevan 	return ret;
637561de45fSGirish Mahadevan }
638561de45fSGirish Mahadevan 
639561de45fSGirish Mahadevan static int spi_geni_remove(struct platform_device *pdev)
640561de45fSGirish Mahadevan {
641561de45fSGirish Mahadevan 	struct spi_master *spi = platform_get_drvdata(pdev);
642561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
643561de45fSGirish Mahadevan 
644561de45fSGirish Mahadevan 	/* Unregister _before_ disabling pm_runtime() so we stop transfers */
645561de45fSGirish Mahadevan 	spi_unregister_master(spi);
646561de45fSGirish Mahadevan 
647561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
648561de45fSGirish Mahadevan 	pm_runtime_disable(&pdev->dev);
6491a9e489eSRajendra Nayak 	if (mas->se.has_opp_table)
6501a9e489eSRajendra Nayak 		dev_pm_opp_of_remove_table(&pdev->dev);
6511a9e489eSRajendra Nayak 	dev_pm_opp_put_clkname(mas->se.opp_table);
652561de45fSGirish Mahadevan 	return 0;
653561de45fSGirish Mahadevan }
654561de45fSGirish Mahadevan 
655561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_suspend(struct device *dev)
656561de45fSGirish Mahadevan {
657561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
658561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
6590e3b8a81SAkash Asthana 	int ret;
660561de45fSGirish Mahadevan 
6611a9e489eSRajendra Nayak 	/* Drop the performance state vote */
6621a9e489eSRajendra Nayak 	dev_pm_opp_set_rate(dev, 0);
6631a9e489eSRajendra Nayak 
6640e3b8a81SAkash Asthana 	ret = geni_se_resources_off(&mas->se);
6650e3b8a81SAkash Asthana 	if (ret)
6660e3b8a81SAkash Asthana 		return ret;
6670e3b8a81SAkash Asthana 
6680e3b8a81SAkash Asthana 	return geni_icc_disable(&mas->se);
669561de45fSGirish Mahadevan }
670561de45fSGirish Mahadevan 
671561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_resume(struct device *dev)
672561de45fSGirish Mahadevan {
673561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
674561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
6750e3b8a81SAkash Asthana 	int ret;
6760e3b8a81SAkash Asthana 
6770e3b8a81SAkash Asthana 	ret = geni_icc_enable(&mas->se);
6780e3b8a81SAkash Asthana 	if (ret)
6790e3b8a81SAkash Asthana 		return ret;
680561de45fSGirish Mahadevan 
681*5f219524SDouglas Anderson 	ret = geni_se_resources_on(&mas->se);
682*5f219524SDouglas Anderson 	if (ret)
683*5f219524SDouglas Anderson 		return ret;
684*5f219524SDouglas Anderson 
685*5f219524SDouglas Anderson 	return dev_pm_opp_set_rate(mas->dev, mas->cur_sclk_hz);
686561de45fSGirish Mahadevan }
687561de45fSGirish Mahadevan 
688561de45fSGirish Mahadevan static int __maybe_unused spi_geni_suspend(struct device *dev)
689561de45fSGirish Mahadevan {
690561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
691561de45fSGirish Mahadevan 	int ret;
692561de45fSGirish Mahadevan 
693561de45fSGirish Mahadevan 	ret = spi_master_suspend(spi);
694561de45fSGirish Mahadevan 	if (ret)
695561de45fSGirish Mahadevan 		return ret;
696561de45fSGirish Mahadevan 
697561de45fSGirish Mahadevan 	ret = pm_runtime_force_suspend(dev);
698561de45fSGirish Mahadevan 	if (ret)
699561de45fSGirish Mahadevan 		spi_master_resume(spi);
700561de45fSGirish Mahadevan 
701561de45fSGirish Mahadevan 	return ret;
702561de45fSGirish Mahadevan }
703561de45fSGirish Mahadevan 
704561de45fSGirish Mahadevan static int __maybe_unused spi_geni_resume(struct device *dev)
705561de45fSGirish Mahadevan {
706561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
707561de45fSGirish Mahadevan 	int ret;
708561de45fSGirish Mahadevan 
709561de45fSGirish Mahadevan 	ret = pm_runtime_force_resume(dev);
710561de45fSGirish Mahadevan 	if (ret)
711561de45fSGirish Mahadevan 		return ret;
712561de45fSGirish Mahadevan 
713561de45fSGirish Mahadevan 	ret = spi_master_resume(spi);
714561de45fSGirish Mahadevan 	if (ret)
715561de45fSGirish Mahadevan 		pm_runtime_force_suspend(dev);
716561de45fSGirish Mahadevan 
717561de45fSGirish Mahadevan 	return ret;
718561de45fSGirish Mahadevan }
719561de45fSGirish Mahadevan 
720561de45fSGirish Mahadevan static const struct dev_pm_ops spi_geni_pm_ops = {
721561de45fSGirish Mahadevan 	SET_RUNTIME_PM_OPS(spi_geni_runtime_suspend,
722561de45fSGirish Mahadevan 					spi_geni_runtime_resume, NULL)
723561de45fSGirish Mahadevan 	SET_SYSTEM_SLEEP_PM_OPS(spi_geni_suspend, spi_geni_resume)
724561de45fSGirish Mahadevan };
725561de45fSGirish Mahadevan 
726561de45fSGirish Mahadevan static const struct of_device_id spi_geni_dt_match[] = {
727561de45fSGirish Mahadevan 	{ .compatible = "qcom,geni-spi" },
728561de45fSGirish Mahadevan 	{}
729561de45fSGirish Mahadevan };
730561de45fSGirish Mahadevan MODULE_DEVICE_TABLE(of, spi_geni_dt_match);
731561de45fSGirish Mahadevan 
732561de45fSGirish Mahadevan static struct platform_driver spi_geni_driver = {
733561de45fSGirish Mahadevan 	.probe  = spi_geni_probe,
734561de45fSGirish Mahadevan 	.remove = spi_geni_remove,
735561de45fSGirish Mahadevan 	.driver = {
736561de45fSGirish Mahadevan 		.name = "geni_spi",
737561de45fSGirish Mahadevan 		.pm = &spi_geni_pm_ops,
738561de45fSGirish Mahadevan 		.of_match_table = spi_geni_dt_match,
739561de45fSGirish Mahadevan 	},
740561de45fSGirish Mahadevan };
741561de45fSGirish Mahadevan module_platform_driver(spi_geni_driver);
742561de45fSGirish Mahadevan 
743561de45fSGirish Mahadevan MODULE_DESCRIPTION("SPI driver for GENI based QUP cores");
744561de45fSGirish Mahadevan MODULE_LICENSE("GPL v2");
745