xref: /linux/drivers/spi/spi-geni-qcom.c (revision cfdab2cd85ecd3f98837e5cc59dd3319cd9b6fff)
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>
10561de45fSGirish Mahadevan #include <linux/pm_runtime.h>
11561de45fSGirish Mahadevan #include <linux/qcom-geni-se.h>
12561de45fSGirish Mahadevan #include <linux/spi/spi.h>
13561de45fSGirish Mahadevan #include <linux/spinlock.h>
14561de45fSGirish Mahadevan 
15561de45fSGirish Mahadevan /* SPI SE specific registers and respective register fields */
16561de45fSGirish Mahadevan #define SE_SPI_CPHA		0x224
17561de45fSGirish Mahadevan #define CPHA			BIT(0)
18561de45fSGirish Mahadevan 
19561de45fSGirish Mahadevan #define SE_SPI_LOOPBACK		0x22c
20561de45fSGirish Mahadevan #define LOOPBACK_ENABLE		0x1
21561de45fSGirish Mahadevan #define NORMAL_MODE		0x0
22561de45fSGirish Mahadevan #define LOOPBACK_MSK		GENMASK(1, 0)
23561de45fSGirish Mahadevan 
24561de45fSGirish Mahadevan #define SE_SPI_CPOL		0x230
25561de45fSGirish Mahadevan #define CPOL			BIT(2)
26561de45fSGirish Mahadevan 
27561de45fSGirish Mahadevan #define SE_SPI_DEMUX_OUTPUT_INV	0x24c
28561de45fSGirish Mahadevan #define CS_DEMUX_OUTPUT_INV_MSK	GENMASK(3, 0)
29561de45fSGirish Mahadevan 
30561de45fSGirish Mahadevan #define SE_SPI_DEMUX_SEL	0x250
31561de45fSGirish Mahadevan #define CS_DEMUX_OUTPUT_SEL	GENMASK(3, 0)
32561de45fSGirish Mahadevan 
33561de45fSGirish Mahadevan #define SE_SPI_TRANS_CFG	0x25c
34561de45fSGirish Mahadevan #define CS_TOGGLE		BIT(0)
35561de45fSGirish Mahadevan 
36561de45fSGirish Mahadevan #define SE_SPI_WORD_LEN		0x268
37561de45fSGirish Mahadevan #define WORD_LEN_MSK		GENMASK(9, 0)
38561de45fSGirish Mahadevan #define MIN_WORD_LEN		4
39561de45fSGirish Mahadevan 
40561de45fSGirish Mahadevan #define SE_SPI_TX_TRANS_LEN	0x26c
41561de45fSGirish Mahadevan #define SE_SPI_RX_TRANS_LEN	0x270
42561de45fSGirish Mahadevan #define TRANS_LEN_MSK		GENMASK(23, 0)
43561de45fSGirish Mahadevan 
44561de45fSGirish Mahadevan #define SE_SPI_PRE_POST_CMD_DLY	0x274
45561de45fSGirish Mahadevan 
46561de45fSGirish Mahadevan #define SE_SPI_DELAY_COUNTERS	0x278
47561de45fSGirish Mahadevan #define SPI_INTER_WORDS_DELAY_MSK	GENMASK(9, 0)
48561de45fSGirish Mahadevan #define SPI_CS_CLK_DELAY_MSK		GENMASK(19, 10)
49561de45fSGirish Mahadevan #define SPI_CS_CLK_DELAY_SHFT		10
50561de45fSGirish Mahadevan 
51561de45fSGirish Mahadevan /* M_CMD OP codes for SPI */
52561de45fSGirish Mahadevan #define SPI_TX_ONLY		1
53561de45fSGirish Mahadevan #define SPI_RX_ONLY		2
54561de45fSGirish Mahadevan #define SPI_TX_RX		7
55561de45fSGirish Mahadevan #define SPI_CS_ASSERT		8
56561de45fSGirish Mahadevan #define SPI_CS_DEASSERT		9
57561de45fSGirish Mahadevan #define SPI_SCK_ONLY		10
58561de45fSGirish Mahadevan /* M_CMD params for SPI */
59561de45fSGirish Mahadevan #define SPI_PRE_CMD_DELAY	BIT(0)
60561de45fSGirish Mahadevan #define TIMESTAMP_BEFORE	BIT(1)
61561de45fSGirish Mahadevan #define FRAGMENTATION		BIT(2)
62561de45fSGirish Mahadevan #define TIMESTAMP_AFTER		BIT(3)
63561de45fSGirish Mahadevan #define POST_CMD_DELAY		BIT(4)
64561de45fSGirish Mahadevan 
65561de45fSGirish Mahadevan struct spi_geni_master {
66561de45fSGirish Mahadevan 	struct geni_se se;
67561de45fSGirish Mahadevan 	struct device *dev;
68561de45fSGirish Mahadevan 	u32 tx_fifo_depth;
69561de45fSGirish Mahadevan 	u32 fifo_width_bits;
70561de45fSGirish Mahadevan 	u32 tx_wm;
71561de45fSGirish Mahadevan 	unsigned long cur_speed_hz;
72561de45fSGirish Mahadevan 	unsigned int cur_bits_per_word;
73561de45fSGirish Mahadevan 	unsigned int tx_rem_bytes;
74561de45fSGirish Mahadevan 	unsigned int rx_rem_bytes;
75561de45fSGirish Mahadevan 	const struct spi_transfer *cur_xfer;
767ba9bdcbSDouglas Anderson 	struct completion cs_done;
777ba9bdcbSDouglas Anderson 	struct completion cancel_done;
787ba9bdcbSDouglas Anderson 	struct completion abort_done;
79561de45fSGirish Mahadevan 	unsigned int oversampling;
80561de45fSGirish Mahadevan 	spinlock_t lock;
81561de45fSGirish Mahadevan 	int irq;
82638d8488SDouglas Anderson 	bool cs_flag;
83561de45fSGirish Mahadevan };
84561de45fSGirish Mahadevan 
85561de45fSGirish Mahadevan static int get_spi_clk_cfg(unsigned int speed_hz,
86561de45fSGirish Mahadevan 			struct spi_geni_master *mas,
87561de45fSGirish Mahadevan 			unsigned int *clk_idx,
88561de45fSGirish Mahadevan 			unsigned int *clk_div)
89561de45fSGirish Mahadevan {
90561de45fSGirish Mahadevan 	unsigned long sclk_freq;
91561de45fSGirish Mahadevan 	unsigned int actual_hz;
92561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
93561de45fSGirish Mahadevan 	int ret;
94561de45fSGirish Mahadevan 
95561de45fSGirish Mahadevan 	ret = geni_se_clk_freq_match(&mas->se,
96561de45fSGirish Mahadevan 				speed_hz * mas->oversampling,
97561de45fSGirish Mahadevan 				clk_idx, &sclk_freq, false);
98561de45fSGirish Mahadevan 	if (ret) {
99561de45fSGirish Mahadevan 		dev_err(mas->dev, "Failed(%d) to find src clk for %dHz\n",
100561de45fSGirish Mahadevan 							ret, speed_hz);
101561de45fSGirish Mahadevan 		return ret;
102561de45fSGirish Mahadevan 	}
103561de45fSGirish Mahadevan 
104561de45fSGirish Mahadevan 	*clk_div = DIV_ROUND_UP(sclk_freq, mas->oversampling * speed_hz);
105561de45fSGirish Mahadevan 	actual_hz = sclk_freq / (mas->oversampling * *clk_div);
106561de45fSGirish Mahadevan 
107561de45fSGirish Mahadevan 	dev_dbg(mas->dev, "req %u=>%u sclk %lu, idx %d, div %d\n", speed_hz,
108561de45fSGirish Mahadevan 				actual_hz, sclk_freq, *clk_idx, *clk_div);
109561de45fSGirish Mahadevan 	ret = clk_set_rate(se->clk, sclk_freq);
110561de45fSGirish Mahadevan 	if (ret)
111561de45fSGirish Mahadevan 		dev_err(mas->dev, "clk_set_rate failed %d\n", ret);
112561de45fSGirish Mahadevan 	return ret;
113561de45fSGirish Mahadevan }
114561de45fSGirish Mahadevan 
115de43affeSStephen Boyd static void handle_fifo_timeout(struct spi_master *spi,
116de43affeSStephen Boyd 				struct spi_message *msg)
117de43affeSStephen Boyd {
118de43affeSStephen Boyd 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
119539afdf9SDouglas Anderson 	unsigned long time_left;
120de43affeSStephen Boyd 	struct geni_se *se = &mas->se;
121de43affeSStephen Boyd 
122539afdf9SDouglas Anderson 	spin_lock_irq(&mas->lock);
1237ba9bdcbSDouglas Anderson 	reinit_completion(&mas->cancel_done);
124de43affeSStephen Boyd 	writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
1257ba9bdcbSDouglas Anderson 	mas->cur_xfer = NULL;
1267ba9bdcbSDouglas Anderson 	geni_se_cancel_m_cmd(se);
127539afdf9SDouglas Anderson 	spin_unlock_irq(&mas->lock);
1287ba9bdcbSDouglas Anderson 
1297ba9bdcbSDouglas Anderson 	time_left = wait_for_completion_timeout(&mas->cancel_done, HZ);
130de43affeSStephen Boyd 	if (time_left)
131de43affeSStephen Boyd 		return;
132de43affeSStephen Boyd 
133539afdf9SDouglas Anderson 	spin_lock_irq(&mas->lock);
1347ba9bdcbSDouglas Anderson 	reinit_completion(&mas->abort_done);
135de43affeSStephen Boyd 	geni_se_abort_m_cmd(se);
136539afdf9SDouglas Anderson 	spin_unlock_irq(&mas->lock);
1377ba9bdcbSDouglas Anderson 
1387ba9bdcbSDouglas Anderson 	time_left = wait_for_completion_timeout(&mas->abort_done, HZ);
139de43affeSStephen Boyd 	if (!time_left)
140de43affeSStephen Boyd 		dev_err(mas->dev, "Failed to cancel/abort m_cmd\n");
141de43affeSStephen Boyd }
142de43affeSStephen Boyd 
143561de45fSGirish Mahadevan static void spi_geni_set_cs(struct spi_device *slv, bool set_flag)
144561de45fSGirish Mahadevan {
145561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(slv->master);
146561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(mas->dev);
147561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
1480dccff3cSAlok Chauhan 	unsigned long time_left;
149561de45fSGirish Mahadevan 
150561de45fSGirish Mahadevan 	if (!(slv->mode & SPI_CS_HIGH))
151561de45fSGirish Mahadevan 		set_flag = !set_flag;
152561de45fSGirish Mahadevan 
153638d8488SDouglas Anderson 	if (set_flag == mas->cs_flag)
154638d8488SDouglas Anderson 		return;
155638d8488SDouglas Anderson 
156638d8488SDouglas Anderson 	mas->cs_flag = set_flag;
157638d8488SDouglas Anderson 
158638d8488SDouglas Anderson 	pm_runtime_get_sync(mas->dev);
1592ee471a1SDouglas Anderson 	spin_lock_irq(&mas->lock);
1607ba9bdcbSDouglas Anderson 	reinit_completion(&mas->cs_done);
161561de45fSGirish Mahadevan 	if (set_flag)
162561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_ASSERT, 0);
163561de45fSGirish Mahadevan 	else
164561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_DEASSERT, 0);
1652ee471a1SDouglas Anderson 	spin_unlock_irq(&mas->lock);
166561de45fSGirish Mahadevan 
1677ba9bdcbSDouglas Anderson 	time_left = wait_for_completion_timeout(&mas->cs_done, HZ);
1680dccff3cSAlok Chauhan 	if (!time_left)
169561de45fSGirish Mahadevan 		handle_fifo_timeout(spi, NULL);
170561de45fSGirish Mahadevan 
171561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
172561de45fSGirish Mahadevan }
173561de45fSGirish Mahadevan 
174561de45fSGirish Mahadevan static void spi_setup_word_len(struct spi_geni_master *mas, u16 mode,
175561de45fSGirish Mahadevan 					unsigned int bits_per_word)
176561de45fSGirish Mahadevan {
177561de45fSGirish Mahadevan 	unsigned int pack_words;
178561de45fSGirish Mahadevan 	bool msb_first = (mode & SPI_LSB_FIRST) ? false : true;
179561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
180561de45fSGirish Mahadevan 	u32 word_len;
181561de45fSGirish Mahadevan 
182561de45fSGirish Mahadevan 	word_len = readl(se->base + SE_SPI_WORD_LEN);
183561de45fSGirish Mahadevan 
184561de45fSGirish Mahadevan 	/*
185561de45fSGirish Mahadevan 	 * If bits_per_word isn't a byte aligned value, set the packing to be
186561de45fSGirish Mahadevan 	 * 1 SPI word per FIFO word.
187561de45fSGirish Mahadevan 	 */
188561de45fSGirish Mahadevan 	if (!(mas->fifo_width_bits % bits_per_word))
189561de45fSGirish Mahadevan 		pack_words = mas->fifo_width_bits / bits_per_word;
190561de45fSGirish Mahadevan 	else
191561de45fSGirish Mahadevan 		pack_words = 1;
192561de45fSGirish Mahadevan 	word_len &= ~WORD_LEN_MSK;
193561de45fSGirish Mahadevan 	word_len |= ((bits_per_word - MIN_WORD_LEN) & WORD_LEN_MSK);
194561de45fSGirish Mahadevan 	geni_se_config_packing(&mas->se, bits_per_word, pack_words, msb_first,
195561de45fSGirish Mahadevan 								true, true);
196561de45fSGirish Mahadevan 	writel(word_len, se->base + SE_SPI_WORD_LEN);
197561de45fSGirish Mahadevan }
198561de45fSGirish Mahadevan 
199561de45fSGirish Mahadevan static int setup_fifo_params(struct spi_device *spi_slv,
200561de45fSGirish Mahadevan 					struct spi_master *spi)
201561de45fSGirish Mahadevan {
202561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
203561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
204561de45fSGirish Mahadevan 	u32 loopback_cfg, cpol, cpha, demux_output_inv;
205561de45fSGirish Mahadevan 	u32 demux_sel, clk_sel, m_clk_cfg, idx, div;
206561de45fSGirish Mahadevan 	int ret;
207561de45fSGirish Mahadevan 
208561de45fSGirish Mahadevan 	loopback_cfg = readl(se->base + SE_SPI_LOOPBACK);
209561de45fSGirish Mahadevan 	cpol = readl(se->base + SE_SPI_CPOL);
210561de45fSGirish Mahadevan 	cpha = readl(se->base + SE_SPI_CPHA);
211561de45fSGirish Mahadevan 	demux_output_inv = 0;
212561de45fSGirish Mahadevan 	loopback_cfg &= ~LOOPBACK_MSK;
213561de45fSGirish Mahadevan 	cpol &= ~CPOL;
214561de45fSGirish Mahadevan 	cpha &= ~CPHA;
215561de45fSGirish Mahadevan 
216561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_LOOP)
217561de45fSGirish Mahadevan 		loopback_cfg |= LOOPBACK_ENABLE;
218561de45fSGirish Mahadevan 
219561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CPOL)
220561de45fSGirish Mahadevan 		cpol |= CPOL;
221561de45fSGirish Mahadevan 
222561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CPHA)
223561de45fSGirish Mahadevan 		cpha |= CPHA;
224561de45fSGirish Mahadevan 
225561de45fSGirish Mahadevan 	if (spi_slv->mode & SPI_CS_HIGH)
226561de45fSGirish Mahadevan 		demux_output_inv = BIT(spi_slv->chip_select);
227561de45fSGirish Mahadevan 
228561de45fSGirish Mahadevan 	demux_sel = spi_slv->chip_select;
229561de45fSGirish Mahadevan 	mas->cur_speed_hz = spi_slv->max_speed_hz;
230561de45fSGirish Mahadevan 	mas->cur_bits_per_word = spi_slv->bits_per_word;
231561de45fSGirish Mahadevan 
232561de45fSGirish Mahadevan 	ret = get_spi_clk_cfg(mas->cur_speed_hz, mas, &idx, &div);
233561de45fSGirish Mahadevan 	if (ret) {
234561de45fSGirish Mahadevan 		dev_err(mas->dev, "Err setting clks ret(%d) for %ld\n",
235561de45fSGirish Mahadevan 							ret, mas->cur_speed_hz);
236561de45fSGirish Mahadevan 		return ret;
237561de45fSGirish Mahadevan 	}
238561de45fSGirish Mahadevan 
239561de45fSGirish Mahadevan 	clk_sel = idx & CLK_SEL_MSK;
240561de45fSGirish Mahadevan 	m_clk_cfg = (div << CLK_DIV_SHFT) | SER_CLK_EN;
241561de45fSGirish Mahadevan 	spi_setup_word_len(mas, spi_slv->mode, spi_slv->bits_per_word);
242561de45fSGirish Mahadevan 	writel(loopback_cfg, se->base + SE_SPI_LOOPBACK);
243561de45fSGirish Mahadevan 	writel(demux_sel, se->base + SE_SPI_DEMUX_SEL);
244561de45fSGirish Mahadevan 	writel(cpha, se->base + SE_SPI_CPHA);
245561de45fSGirish Mahadevan 	writel(cpol, se->base + SE_SPI_CPOL);
246561de45fSGirish Mahadevan 	writel(demux_output_inv, se->base + SE_SPI_DEMUX_OUTPUT_INV);
247561de45fSGirish Mahadevan 	writel(clk_sel, se->base + SE_GENI_CLK_SEL);
248561de45fSGirish Mahadevan 	writel(m_clk_cfg, se->base + GENI_SER_M_CLK_CFG);
249561de45fSGirish Mahadevan 	return 0;
250561de45fSGirish Mahadevan }
251561de45fSGirish Mahadevan 
252561de45fSGirish Mahadevan static int spi_geni_prepare_message(struct spi_master *spi,
253561de45fSGirish Mahadevan 					struct spi_message *spi_msg)
254561de45fSGirish Mahadevan {
255561de45fSGirish Mahadevan 	int ret;
256561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
257561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
258561de45fSGirish Mahadevan 
259561de45fSGirish Mahadevan 	geni_se_select_mode(se, GENI_SE_FIFO);
260561de45fSGirish Mahadevan 	ret = setup_fifo_params(spi_msg->spi, spi);
261561de45fSGirish Mahadevan 	if (ret)
262561de45fSGirish Mahadevan 		dev_err(mas->dev, "Couldn't select mode %d\n", ret);
263561de45fSGirish Mahadevan 	return ret;
264561de45fSGirish Mahadevan }
265561de45fSGirish Mahadevan 
266561de45fSGirish Mahadevan static int spi_geni_init(struct spi_geni_master *mas)
267561de45fSGirish Mahadevan {
268561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
269561de45fSGirish Mahadevan 	unsigned int proto, major, minor, ver;
270561de45fSGirish Mahadevan 
271561de45fSGirish Mahadevan 	pm_runtime_get_sync(mas->dev);
272561de45fSGirish Mahadevan 
273561de45fSGirish Mahadevan 	proto = geni_se_read_proto(se);
274561de45fSGirish Mahadevan 	if (proto != GENI_SE_SPI) {
275561de45fSGirish Mahadevan 		dev_err(mas->dev, "Invalid proto %d\n", proto);
276561de45fSGirish Mahadevan 		pm_runtime_put(mas->dev);
277561de45fSGirish Mahadevan 		return -ENXIO;
278561de45fSGirish Mahadevan 	}
279561de45fSGirish Mahadevan 	mas->tx_fifo_depth = geni_se_get_tx_fifo_depth(se);
280561de45fSGirish Mahadevan 
281561de45fSGirish Mahadevan 	/* Width of Tx and Rx FIFO is same */
282561de45fSGirish Mahadevan 	mas->fifo_width_bits = geni_se_get_tx_fifo_width(se);
283561de45fSGirish Mahadevan 
284561de45fSGirish Mahadevan 	/*
285561de45fSGirish Mahadevan 	 * Hardware programming guide suggests to configure
286561de45fSGirish Mahadevan 	 * RX FIFO RFR level to fifo_depth-2.
287561de45fSGirish Mahadevan 	 */
288902481a7SDouglas Anderson 	geni_se_init(se, mas->tx_fifo_depth / 2, mas->tx_fifo_depth - 2);
289561de45fSGirish Mahadevan 	/* Transmit an entire FIFO worth of data per IRQ */
290561de45fSGirish Mahadevan 	mas->tx_wm = 1;
291561de45fSGirish Mahadevan 	ver = geni_se_get_qup_hw_version(se);
292561de45fSGirish Mahadevan 	major = GENI_SE_VERSION_MAJOR(ver);
293561de45fSGirish Mahadevan 	minor = GENI_SE_VERSION_MINOR(ver);
294561de45fSGirish Mahadevan 
295561de45fSGirish Mahadevan 	if (major == 1 && minor == 0)
296561de45fSGirish Mahadevan 		mas->oversampling = 2;
297561de45fSGirish Mahadevan 	else
298561de45fSGirish Mahadevan 		mas->oversampling = 1;
299561de45fSGirish Mahadevan 
300561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
301561de45fSGirish Mahadevan 	return 0;
302561de45fSGirish Mahadevan }
303561de45fSGirish Mahadevan 
304561de45fSGirish Mahadevan static void setup_fifo_xfer(struct spi_transfer *xfer,
305561de45fSGirish Mahadevan 				struct spi_geni_master *mas,
306561de45fSGirish Mahadevan 				u16 mode, struct spi_master *spi)
307561de45fSGirish Mahadevan {
308561de45fSGirish Mahadevan 	u32 m_cmd = 0;
309561de45fSGirish Mahadevan 	u32 spi_tx_cfg, len;
310561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
311561de45fSGirish Mahadevan 
3122ee471a1SDouglas Anderson 	/*
3132ee471a1SDouglas Anderson 	 * Ensure that our interrupt handler isn't still running from some
3142ee471a1SDouglas Anderson 	 * prior command before we start messing with the hardware behind
3152ee471a1SDouglas Anderson 	 * its back.  We don't need to _keep_ the lock here since we're only
3162ee471a1SDouglas Anderson 	 * worried about racing with out interrupt handler.  The SPI core
3172ee471a1SDouglas Anderson 	 * already handles making sure that we're not trying to do two
3182ee471a1SDouglas Anderson 	 * transfers at once or setting a chip select and doing a transfer
3192ee471a1SDouglas Anderson 	 * concurrently.
3202ee471a1SDouglas Anderson 	 *
3212ee471a1SDouglas Anderson 	 * NOTE: we actually _can't_ hold the lock here because possibly we
3222ee471a1SDouglas Anderson 	 * might call clk_set_rate() which needs to be able to sleep.
3232ee471a1SDouglas Anderson 	 */
3242ee471a1SDouglas Anderson 	spin_lock_irq(&mas->lock);
3252ee471a1SDouglas Anderson 	spin_unlock_irq(&mas->lock);
3262ee471a1SDouglas Anderson 
327561de45fSGirish Mahadevan 	spi_tx_cfg = readl(se->base + SE_SPI_TRANS_CFG);
328561de45fSGirish Mahadevan 	if (xfer->bits_per_word != mas->cur_bits_per_word) {
329561de45fSGirish Mahadevan 		spi_setup_word_len(mas, mode, xfer->bits_per_word);
330561de45fSGirish Mahadevan 		mas->cur_bits_per_word = xfer->bits_per_word;
331561de45fSGirish Mahadevan 	}
332561de45fSGirish Mahadevan 
333561de45fSGirish Mahadevan 	/* Speed and bits per word can be overridden per transfer */
334561de45fSGirish Mahadevan 	if (xfer->speed_hz != mas->cur_speed_hz) {
335561de45fSGirish Mahadevan 		int ret;
336561de45fSGirish Mahadevan 		u32 clk_sel, m_clk_cfg;
337561de45fSGirish Mahadevan 		unsigned int idx, div;
338561de45fSGirish Mahadevan 
339561de45fSGirish Mahadevan 		ret = get_spi_clk_cfg(xfer->speed_hz, mas, &idx, &div);
340561de45fSGirish Mahadevan 		if (ret) {
341561de45fSGirish Mahadevan 			dev_err(mas->dev, "Err setting clks:%d\n", ret);
342561de45fSGirish Mahadevan 			return;
343561de45fSGirish Mahadevan 		}
344561de45fSGirish Mahadevan 		/*
345561de45fSGirish Mahadevan 		 * SPI core clock gets configured with the requested frequency
346561de45fSGirish Mahadevan 		 * or the frequency closer to the requested frequency.
347561de45fSGirish Mahadevan 		 * For that reason requested frequency is stored in the
348561de45fSGirish Mahadevan 		 * cur_speed_hz and referred in the consecutive transfer instead
349561de45fSGirish Mahadevan 		 * of calling clk_get_rate() API.
350561de45fSGirish Mahadevan 		 */
351561de45fSGirish Mahadevan 		mas->cur_speed_hz = xfer->speed_hz;
352561de45fSGirish Mahadevan 		clk_sel = idx & CLK_SEL_MSK;
353561de45fSGirish Mahadevan 		m_clk_cfg = (div << CLK_DIV_SHFT) | SER_CLK_EN;
354561de45fSGirish Mahadevan 		writel(clk_sel, se->base + SE_GENI_CLK_SEL);
355561de45fSGirish Mahadevan 		writel(m_clk_cfg, se->base + GENI_SER_M_CLK_CFG);
356561de45fSGirish Mahadevan 	}
357561de45fSGirish Mahadevan 
358561de45fSGirish Mahadevan 	mas->tx_rem_bytes = 0;
359561de45fSGirish Mahadevan 	mas->rx_rem_bytes = 0;
360561de45fSGirish Mahadevan 
361561de45fSGirish Mahadevan 	spi_tx_cfg &= ~CS_TOGGLE;
362561de45fSGirish Mahadevan 
363561de45fSGirish Mahadevan 	if (!(mas->cur_bits_per_word % MIN_WORD_LEN))
364561de45fSGirish Mahadevan 		len = xfer->len * BITS_PER_BYTE / mas->cur_bits_per_word;
365561de45fSGirish Mahadevan 	else
366561de45fSGirish Mahadevan 		len = xfer->len / (mas->cur_bits_per_word / BITS_PER_BYTE + 1);
367561de45fSGirish Mahadevan 	len &= TRANS_LEN_MSK;
368561de45fSGirish Mahadevan 
369561de45fSGirish Mahadevan 	mas->cur_xfer = xfer;
37019ea3275SStephen Boyd 	if (xfer->tx_buf) {
37119ea3275SStephen Boyd 		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 
37619ea3275SStephen Boyd 	if (xfer->rx_buf) {
37719ea3275SStephen Boyd 		m_cmd |= SPI_RX_ONLY;
378561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_RX_TRANS_LEN);
379561de45fSGirish Mahadevan 		mas->rx_rem_bytes = xfer->len;
380561de45fSGirish Mahadevan 	}
381561de45fSGirish Mahadevan 	writel(spi_tx_cfg, se->base + SE_SPI_TRANS_CFG);
3822ee471a1SDouglas Anderson 
3832ee471a1SDouglas Anderson 	/*
3842ee471a1SDouglas Anderson 	 * Lock around right before we start the transfer since our
3852ee471a1SDouglas Anderson 	 * interrupt could come in at any time now.
3862ee471a1SDouglas Anderson 	 */
3872ee471a1SDouglas Anderson 	spin_lock_irq(&mas->lock);
388561de45fSGirish Mahadevan 	geni_se_setup_m_cmd(se, m_cmd, FRAGMENTATION);
389561de45fSGirish Mahadevan 
390561de45fSGirish Mahadevan 	/*
391561de45fSGirish Mahadevan 	 * TX_WATERMARK_REG should be set after SPI configuration and
392561de45fSGirish Mahadevan 	 * setting up GENI SE engine, as driver starts data transfer
393561de45fSGirish Mahadevan 	 * for the watermark interrupt.
394561de45fSGirish Mahadevan 	 */
395561de45fSGirish Mahadevan 	if (m_cmd & SPI_TX_ONLY)
396561de45fSGirish Mahadevan 		writel(mas->tx_wm, se->base + SE_GENI_TX_WATERMARK_REG);
3972ee471a1SDouglas Anderson 	spin_unlock_irq(&mas->lock);
398561de45fSGirish Mahadevan }
399561de45fSGirish Mahadevan 
400561de45fSGirish Mahadevan static int spi_geni_transfer_one(struct spi_master *spi,
401561de45fSGirish Mahadevan 				struct spi_device *slv,
402561de45fSGirish Mahadevan 				struct spi_transfer *xfer)
403561de45fSGirish Mahadevan {
404561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
405561de45fSGirish Mahadevan 
406561de45fSGirish Mahadevan 	/* Terminate and return success for 0 byte length transfer */
407561de45fSGirish Mahadevan 	if (!xfer->len)
408561de45fSGirish Mahadevan 		return 0;
409561de45fSGirish Mahadevan 
410561de45fSGirish Mahadevan 	setup_fifo_xfer(xfer, mas, slv->mode, spi);
411561de45fSGirish Mahadevan 	return 1;
412561de45fSGirish Mahadevan }
413561de45fSGirish Mahadevan 
414561de45fSGirish Mahadevan static unsigned int geni_byte_per_fifo_word(struct spi_geni_master *mas)
415561de45fSGirish Mahadevan {
416561de45fSGirish Mahadevan 	/*
417561de45fSGirish Mahadevan 	 * Calculate how many bytes we'll put in each FIFO word.  If the
418561de45fSGirish Mahadevan 	 * transfer words don't pack cleanly into a FIFO word we'll just put
419561de45fSGirish Mahadevan 	 * one transfer word in each FIFO word.  If they do pack we'll pack 'em.
420561de45fSGirish Mahadevan 	 */
421561de45fSGirish Mahadevan 	if (mas->fifo_width_bits % mas->cur_bits_per_word)
422561de45fSGirish Mahadevan 		return roundup_pow_of_two(DIV_ROUND_UP(mas->cur_bits_per_word,
423561de45fSGirish Mahadevan 						       BITS_PER_BYTE));
424561de45fSGirish Mahadevan 
425561de45fSGirish Mahadevan 	return mas->fifo_width_bits / BITS_PER_BYTE;
426561de45fSGirish Mahadevan }
427561de45fSGirish Mahadevan 
428561de45fSGirish Mahadevan static void geni_spi_handle_tx(struct spi_geni_master *mas)
429561de45fSGirish Mahadevan {
430561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
431561de45fSGirish Mahadevan 	unsigned int max_bytes;
432561de45fSGirish Mahadevan 	const u8 *tx_buf;
433561de45fSGirish Mahadevan 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
434561de45fSGirish Mahadevan 	unsigned int i = 0;
435561de45fSGirish Mahadevan 
436561de45fSGirish Mahadevan 	max_bytes = (mas->tx_fifo_depth - mas->tx_wm) * bytes_per_fifo_word;
437561de45fSGirish Mahadevan 	if (mas->tx_rem_bytes < max_bytes)
438561de45fSGirish Mahadevan 		max_bytes = mas->tx_rem_bytes;
439561de45fSGirish Mahadevan 
440561de45fSGirish Mahadevan 	tx_buf = mas->cur_xfer->tx_buf + mas->cur_xfer->len - mas->tx_rem_bytes;
441561de45fSGirish Mahadevan 	while (i < max_bytes) {
442561de45fSGirish Mahadevan 		unsigned int j;
443561de45fSGirish Mahadevan 		unsigned int bytes_to_write;
444561de45fSGirish Mahadevan 		u32 fifo_word = 0;
445561de45fSGirish Mahadevan 		u8 *fifo_byte = (u8 *)&fifo_word;
446561de45fSGirish Mahadevan 
447561de45fSGirish Mahadevan 		bytes_to_write = min(bytes_per_fifo_word, max_bytes - i);
448561de45fSGirish Mahadevan 		for (j = 0; j < bytes_to_write; j++)
449561de45fSGirish Mahadevan 			fifo_byte[j] = tx_buf[i++];
450561de45fSGirish Mahadevan 		iowrite32_rep(se->base + SE_GENI_TX_FIFOn, &fifo_word, 1);
451561de45fSGirish Mahadevan 	}
452561de45fSGirish Mahadevan 	mas->tx_rem_bytes -= max_bytes;
453561de45fSGirish Mahadevan 	if (!mas->tx_rem_bytes)
454561de45fSGirish Mahadevan 		writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
455561de45fSGirish Mahadevan }
456561de45fSGirish Mahadevan 
457561de45fSGirish Mahadevan static void geni_spi_handle_rx(struct spi_geni_master *mas)
458561de45fSGirish Mahadevan {
459561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
460561de45fSGirish Mahadevan 	u32 rx_fifo_status;
461561de45fSGirish Mahadevan 	unsigned int rx_bytes;
462561de45fSGirish Mahadevan 	unsigned int rx_last_byte_valid;
463561de45fSGirish Mahadevan 	u8 *rx_buf;
464561de45fSGirish Mahadevan 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
465561de45fSGirish Mahadevan 	unsigned int i = 0;
466561de45fSGirish Mahadevan 
467561de45fSGirish Mahadevan 	rx_fifo_status = readl(se->base + SE_GENI_RX_FIFO_STATUS);
468561de45fSGirish Mahadevan 	rx_bytes = (rx_fifo_status & RX_FIFO_WC_MSK) * bytes_per_fifo_word;
469561de45fSGirish Mahadevan 	if (rx_fifo_status & RX_LAST) {
470561de45fSGirish Mahadevan 		rx_last_byte_valid = rx_fifo_status & RX_LAST_BYTE_VALID_MSK;
471561de45fSGirish Mahadevan 		rx_last_byte_valid >>= RX_LAST_BYTE_VALID_SHFT;
472561de45fSGirish Mahadevan 		if (rx_last_byte_valid && rx_last_byte_valid < 4)
473561de45fSGirish Mahadevan 			rx_bytes -= bytes_per_fifo_word - rx_last_byte_valid;
474561de45fSGirish Mahadevan 	}
475561de45fSGirish Mahadevan 	if (mas->rx_rem_bytes < rx_bytes)
476561de45fSGirish Mahadevan 		rx_bytes = mas->rx_rem_bytes;
477561de45fSGirish Mahadevan 
478561de45fSGirish Mahadevan 	rx_buf = mas->cur_xfer->rx_buf + mas->cur_xfer->len - mas->rx_rem_bytes;
479561de45fSGirish Mahadevan 	while (i < rx_bytes) {
480561de45fSGirish Mahadevan 		u32 fifo_word = 0;
481561de45fSGirish Mahadevan 		u8 *fifo_byte = (u8 *)&fifo_word;
482561de45fSGirish Mahadevan 		unsigned int bytes_to_read;
483561de45fSGirish Mahadevan 		unsigned int j;
484561de45fSGirish Mahadevan 
485561de45fSGirish Mahadevan 		bytes_to_read = min(bytes_per_fifo_word, rx_bytes - i);
486561de45fSGirish Mahadevan 		ioread32_rep(se->base + SE_GENI_RX_FIFOn, &fifo_word, 1);
487561de45fSGirish Mahadevan 		for (j = 0; j < bytes_to_read; j++)
488561de45fSGirish Mahadevan 			rx_buf[i++] = fifo_byte[j];
489561de45fSGirish Mahadevan 	}
490561de45fSGirish Mahadevan 	mas->rx_rem_bytes -= rx_bytes;
491561de45fSGirish Mahadevan }
492561de45fSGirish Mahadevan 
493561de45fSGirish Mahadevan static irqreturn_t geni_spi_isr(int irq, void *data)
494561de45fSGirish Mahadevan {
495561de45fSGirish Mahadevan 	struct spi_master *spi = data;
496561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
497561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
498561de45fSGirish Mahadevan 	u32 m_irq;
499561de45fSGirish Mahadevan 
5002ee471a1SDouglas Anderson 	m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
5012ee471a1SDouglas Anderson 	if (!m_irq)
502561de45fSGirish Mahadevan 		return IRQ_NONE;
503561de45fSGirish Mahadevan 
504e191a082SDouglas Anderson 	if (m_irq & (M_CMD_OVERRUN_EN | M_ILLEGAL_CMD_EN | M_CMD_FAILURE_EN |
505e191a082SDouglas Anderson 		     M_RX_FIFO_RD_ERR_EN | M_RX_FIFO_WR_ERR_EN |
506e191a082SDouglas Anderson 		     M_TX_FIFO_RD_ERR_EN | M_TX_FIFO_WR_ERR_EN))
507e191a082SDouglas Anderson 		dev_warn(mas->dev, "Unexpected IRQ err status %#010x\n", m_irq);
508e191a082SDouglas Anderson 
509539afdf9SDouglas Anderson 	spin_lock(&mas->lock);
510561de45fSGirish Mahadevan 
511561de45fSGirish Mahadevan 	if ((m_irq & M_RX_FIFO_WATERMARK_EN) || (m_irq & M_RX_FIFO_LAST_EN))
512561de45fSGirish Mahadevan 		geni_spi_handle_rx(mas);
513561de45fSGirish Mahadevan 
514561de45fSGirish Mahadevan 	if (m_irq & M_TX_FIFO_WATERMARK_EN)
515561de45fSGirish Mahadevan 		geni_spi_handle_tx(mas);
516561de45fSGirish Mahadevan 
517561de45fSGirish Mahadevan 	if (m_irq & M_CMD_DONE_EN) {
5187ba9bdcbSDouglas Anderson 		if (mas->cur_xfer) {
519561de45fSGirish Mahadevan 			spi_finalize_current_transfer(spi);
5207ba9bdcbSDouglas Anderson 			mas->cur_xfer = NULL;
521561de45fSGirish Mahadevan 			/*
52259ab0fa0SStephen Boyd 			 * If this happens, then a CMD_DONE came before all the
52359ab0fa0SStephen Boyd 			 * Tx buffer bytes were sent out. This is unusual, log
52459ab0fa0SStephen Boyd 			 * this condition and disable the WM interrupt to
52559ab0fa0SStephen Boyd 			 * prevent the system from stalling due an interrupt
52659ab0fa0SStephen Boyd 			 * storm.
52759ab0fa0SStephen Boyd 			 *
52859ab0fa0SStephen Boyd 			 * If this happens when all Rx bytes haven't been
52959ab0fa0SStephen Boyd 			 * received, log the condition. The only known time
53059ab0fa0SStephen Boyd 			 * this can happen is if bits_per_word != 8 and some
53159ab0fa0SStephen Boyd 			 * registers that expect xfer lengths in num spi_words
532561de45fSGirish Mahadevan 			 * weren't written correctly.
533561de45fSGirish Mahadevan 			 */
534561de45fSGirish Mahadevan 			if (mas->tx_rem_bytes) {
535561de45fSGirish Mahadevan 				writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
536561de45fSGirish Mahadevan 				dev_err(mas->dev, "Premature done. tx_rem = %d bpw%d\n",
537561de45fSGirish Mahadevan 					mas->tx_rem_bytes, mas->cur_bits_per_word);
538561de45fSGirish Mahadevan 			}
539561de45fSGirish Mahadevan 			if (mas->rx_rem_bytes)
540561de45fSGirish Mahadevan 				dev_err(mas->dev, "Premature done. rx_rem = %d bpw%d\n",
541561de45fSGirish Mahadevan 					mas->rx_rem_bytes, mas->cur_bits_per_word);
54259ab0fa0SStephen Boyd 		} else {
54359ab0fa0SStephen Boyd 			complete(&mas->cs_done);
54459ab0fa0SStephen Boyd 		}
545561de45fSGirish Mahadevan 	}
546561de45fSGirish Mahadevan 
5477ba9bdcbSDouglas Anderson 	if (m_irq & M_CMD_CANCEL_EN)
5487ba9bdcbSDouglas Anderson 		complete(&mas->cancel_done);
5497ba9bdcbSDouglas Anderson 	if (m_irq & M_CMD_ABORT_EN)
5507ba9bdcbSDouglas Anderson 		complete(&mas->abort_done);
551561de45fSGirish Mahadevan 
5522ee471a1SDouglas Anderson 	/*
5532ee471a1SDouglas Anderson 	 * It's safe or a good idea to Ack all of our our interrupts at the
5542ee471a1SDouglas Anderson 	 * end of the function. Specifically:
5552ee471a1SDouglas Anderson 	 * - M_CMD_DONE_EN / M_RX_FIFO_LAST_EN: Edge triggered interrupts and
5562ee471a1SDouglas Anderson 	 *   clearing Acks. Clearing at the end relies on nobody else having
5572ee471a1SDouglas Anderson 	 *   started a new transfer yet or else we could be clearing _their_
5582ee471a1SDouglas Anderson 	 *   done bit, but everyone grabs the spinlock before starting a new
5592ee471a1SDouglas Anderson 	 *   transfer.
5602ee471a1SDouglas Anderson 	 * - M_RX_FIFO_WATERMARK_EN / M_TX_FIFO_WATERMARK_EN: These appear
5612ee471a1SDouglas Anderson 	 *   to be "latched level" interrupts so it's important to clear them
5622ee471a1SDouglas Anderson 	 *   _after_ you've handled the condition and always safe to do so
5632ee471a1SDouglas Anderson 	 *   since they'll re-assert if they're still happening.
5642ee471a1SDouglas Anderson 	 */
565561de45fSGirish Mahadevan 	writel(m_irq, se->base + SE_GENI_M_IRQ_CLEAR);
5662ee471a1SDouglas Anderson 
567539afdf9SDouglas Anderson 	spin_unlock(&mas->lock);
5682ee471a1SDouglas Anderson 
5690dccff3cSAlok Chauhan 	return IRQ_HANDLED;
570561de45fSGirish Mahadevan }
571561de45fSGirish Mahadevan 
572561de45fSGirish Mahadevan static int spi_geni_probe(struct platform_device *pdev)
573561de45fSGirish Mahadevan {
5746a34e285SAlok Chauhan 	int ret, irq;
575561de45fSGirish Mahadevan 	struct spi_master *spi;
576561de45fSGirish Mahadevan 	struct spi_geni_master *mas;
5776a34e285SAlok Chauhan 	void __iomem *base;
5786a34e285SAlok Chauhan 	struct clk *clk;
579ea1e5b33SStephen Boyd 	struct device *dev = &pdev->dev;
5806a34e285SAlok Chauhan 
5816a34e285SAlok Chauhan 	irq = platform_get_irq(pdev, 0);
5826b8ac10eSStephen Boyd 	if (irq < 0)
5836a34e285SAlok Chauhan 		return irq;
5846a34e285SAlok Chauhan 
585d8e477abSYueHaibing 	base = devm_platform_ioremap_resource(pdev, 0);
5866a34e285SAlok Chauhan 	if (IS_ERR(base))
5876a34e285SAlok Chauhan 		return PTR_ERR(base);
5886a34e285SAlok Chauhan 
589ea1e5b33SStephen Boyd 	clk = devm_clk_get(dev, "se");
590ea1e5b33SStephen Boyd 	if (IS_ERR(clk))
5916a34e285SAlok Chauhan 		return PTR_ERR(clk);
592561de45fSGirish Mahadevan 
593ea1e5b33SStephen Boyd 	spi = spi_alloc_master(dev, sizeof(*mas));
594561de45fSGirish Mahadevan 	if (!spi)
595561de45fSGirish Mahadevan 		return -ENOMEM;
596561de45fSGirish Mahadevan 
597561de45fSGirish Mahadevan 	platform_set_drvdata(pdev, spi);
598561de45fSGirish Mahadevan 	mas = spi_master_get_devdata(spi);
5996a34e285SAlok Chauhan 	mas->irq = irq;
600ea1e5b33SStephen Boyd 	mas->dev = dev;
601ea1e5b33SStephen Boyd 	mas->se.dev = dev;
602ea1e5b33SStephen Boyd 	mas->se.wrapper = dev_get_drvdata(dev->parent);
6036a34e285SAlok Chauhan 	mas->se.base = base;
6046a34e285SAlok Chauhan 	mas->se.clk = clk;
605561de45fSGirish Mahadevan 
606561de45fSGirish Mahadevan 	spi->bus_num = -1;
607ea1e5b33SStephen Boyd 	spi->dev.of_node = dev->of_node;
608561de45fSGirish Mahadevan 	spi->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_CS_HIGH;
609561de45fSGirish Mahadevan 	spi->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
610561de45fSGirish Mahadevan 	spi->num_chipselect = 4;
611561de45fSGirish Mahadevan 	spi->max_speed_hz = 50000000;
612561de45fSGirish Mahadevan 	spi->prepare_message = spi_geni_prepare_message;
613561de45fSGirish Mahadevan 	spi->transfer_one = spi_geni_transfer_one;
614561de45fSGirish Mahadevan 	spi->auto_runtime_pm = true;
615561de45fSGirish Mahadevan 	spi->handle_err = handle_fifo_timeout;
616561de45fSGirish Mahadevan 	spi->set_cs = spi_geni_set_cs;
617561de45fSGirish Mahadevan 
6187ba9bdcbSDouglas Anderson 	init_completion(&mas->cs_done);
6197ba9bdcbSDouglas Anderson 	init_completion(&mas->cancel_done);
6207ba9bdcbSDouglas Anderson 	init_completion(&mas->abort_done);
621561de45fSGirish Mahadevan 	spin_lock_init(&mas->lock);
622*cfdab2cdSDouglas Anderson 	pm_runtime_use_autosuspend(&pdev->dev);
623*cfdab2cdSDouglas Anderson 	pm_runtime_set_autosuspend_delay(&pdev->dev, 250);
624ea1e5b33SStephen Boyd 	pm_runtime_enable(dev);
625561de45fSGirish Mahadevan 
626561de45fSGirish Mahadevan 	ret = spi_geni_init(mas);
627561de45fSGirish Mahadevan 	if (ret)
628561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
629561de45fSGirish Mahadevan 
630ea1e5b33SStephen Boyd 	ret = request_irq(mas->irq, geni_spi_isr, 0, dev_name(dev), spi);
631561de45fSGirish Mahadevan 	if (ret)
632561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
633561de45fSGirish Mahadevan 
634561de45fSGirish Mahadevan 	ret = spi_register_master(spi);
635561de45fSGirish Mahadevan 	if (ret)
636561de45fSGirish Mahadevan 		goto spi_geni_probe_free_irq;
637561de45fSGirish Mahadevan 
638561de45fSGirish Mahadevan 	return 0;
639561de45fSGirish Mahadevan spi_geni_probe_free_irq:
640561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
641561de45fSGirish Mahadevan spi_geni_probe_runtime_disable:
642ea1e5b33SStephen Boyd 	pm_runtime_disable(dev);
643561de45fSGirish Mahadevan 	spi_master_put(spi);
644561de45fSGirish Mahadevan 	return ret;
645561de45fSGirish Mahadevan }
646561de45fSGirish Mahadevan 
647561de45fSGirish Mahadevan static int spi_geni_remove(struct platform_device *pdev)
648561de45fSGirish Mahadevan {
649561de45fSGirish Mahadevan 	struct spi_master *spi = platform_get_drvdata(pdev);
650561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
651561de45fSGirish Mahadevan 
652561de45fSGirish Mahadevan 	/* Unregister _before_ disabling pm_runtime() so we stop transfers */
653561de45fSGirish Mahadevan 	spi_unregister_master(spi);
654561de45fSGirish Mahadevan 
655561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
656561de45fSGirish Mahadevan 	pm_runtime_disable(&pdev->dev);
657561de45fSGirish Mahadevan 	return 0;
658561de45fSGirish Mahadevan }
659561de45fSGirish Mahadevan 
660561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_suspend(struct device *dev)
661561de45fSGirish Mahadevan {
662561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
663561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
664561de45fSGirish Mahadevan 
665561de45fSGirish Mahadevan 	return geni_se_resources_off(&mas->se);
666561de45fSGirish Mahadevan }
667561de45fSGirish Mahadevan 
668561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_resume(struct device *dev)
669561de45fSGirish Mahadevan {
670561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
671561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
672561de45fSGirish Mahadevan 
673561de45fSGirish Mahadevan 	return geni_se_resources_on(&mas->se);
674561de45fSGirish Mahadevan }
675561de45fSGirish Mahadevan 
676561de45fSGirish Mahadevan static int __maybe_unused spi_geni_suspend(struct device *dev)
677561de45fSGirish Mahadevan {
678561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
679561de45fSGirish Mahadevan 	int ret;
680561de45fSGirish Mahadevan 
681561de45fSGirish Mahadevan 	ret = spi_master_suspend(spi);
682561de45fSGirish Mahadevan 	if (ret)
683561de45fSGirish Mahadevan 		return ret;
684561de45fSGirish Mahadevan 
685561de45fSGirish Mahadevan 	ret = pm_runtime_force_suspend(dev);
686561de45fSGirish Mahadevan 	if (ret)
687561de45fSGirish Mahadevan 		spi_master_resume(spi);
688561de45fSGirish Mahadevan 
689561de45fSGirish Mahadevan 	return ret;
690561de45fSGirish Mahadevan }
691561de45fSGirish Mahadevan 
692561de45fSGirish Mahadevan static int __maybe_unused spi_geni_resume(struct device *dev)
693561de45fSGirish Mahadevan {
694561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
695561de45fSGirish Mahadevan 	int ret;
696561de45fSGirish Mahadevan 
697561de45fSGirish Mahadevan 	ret = pm_runtime_force_resume(dev);
698561de45fSGirish Mahadevan 	if (ret)
699561de45fSGirish Mahadevan 		return ret;
700561de45fSGirish Mahadevan 
701561de45fSGirish Mahadevan 	ret = spi_master_resume(spi);
702561de45fSGirish Mahadevan 	if (ret)
703561de45fSGirish Mahadevan 		pm_runtime_force_suspend(dev);
704561de45fSGirish Mahadevan 
705561de45fSGirish Mahadevan 	return ret;
706561de45fSGirish Mahadevan }
707561de45fSGirish Mahadevan 
708561de45fSGirish Mahadevan static const struct dev_pm_ops spi_geni_pm_ops = {
709561de45fSGirish Mahadevan 	SET_RUNTIME_PM_OPS(spi_geni_runtime_suspend,
710561de45fSGirish Mahadevan 					spi_geni_runtime_resume, NULL)
711561de45fSGirish Mahadevan 	SET_SYSTEM_SLEEP_PM_OPS(spi_geni_suspend, spi_geni_resume)
712561de45fSGirish Mahadevan };
713561de45fSGirish Mahadevan 
714561de45fSGirish Mahadevan static const struct of_device_id spi_geni_dt_match[] = {
715561de45fSGirish Mahadevan 	{ .compatible = "qcom,geni-spi" },
716561de45fSGirish Mahadevan 	{}
717561de45fSGirish Mahadevan };
718561de45fSGirish Mahadevan MODULE_DEVICE_TABLE(of, spi_geni_dt_match);
719561de45fSGirish Mahadevan 
720561de45fSGirish Mahadevan static struct platform_driver spi_geni_driver = {
721561de45fSGirish Mahadevan 	.probe  = spi_geni_probe,
722561de45fSGirish Mahadevan 	.remove = spi_geni_remove,
723561de45fSGirish Mahadevan 	.driver = {
724561de45fSGirish Mahadevan 		.name = "geni_spi",
725561de45fSGirish Mahadevan 		.pm = &spi_geni_pm_ops,
726561de45fSGirish Mahadevan 		.of_match_table = spi_geni_dt_match,
727561de45fSGirish Mahadevan 	},
728561de45fSGirish Mahadevan };
729561de45fSGirish Mahadevan module_platform_driver(spi_geni_driver);
730561de45fSGirish Mahadevan 
731561de45fSGirish Mahadevan MODULE_DESCRIPTION("SPI driver for GENI based QUP cores");
732561de45fSGirish Mahadevan MODULE_LICENSE("GPL v2");
733