xref: /linux/drivers/spi/spi-st-ssc4.c (revision 9e862375c5420a329521c458a3c808c127e9038f)
1*9e862375SLee Jones /*
2*9e862375SLee Jones  *  Copyright (c) 2008-2014 STMicroelectronics Limited
3*9e862375SLee Jones  *
4*9e862375SLee Jones  *  Author: Angus Clark <Angus.Clark@st.com>
5*9e862375SLee Jones  *          Patrice Chotard <patrice.chotard@st.com>
6*9e862375SLee Jones  *          Lee Jones <lee.jones@linaro.org>
7*9e862375SLee Jones  *
8*9e862375SLee Jones  *  SPI master mode controller driver, used in STMicroelectronics devices.
9*9e862375SLee Jones  *
10*9e862375SLee Jones  *  May be copied or modified under the terms of the GNU General Public
11*9e862375SLee Jones  *  License Version 2.0 only.  See linux/COPYING for more information.
12*9e862375SLee Jones  */
13*9e862375SLee Jones 
14*9e862375SLee Jones #include <linux/clk.h>
15*9e862375SLee Jones #include <linux/delay.h>
16*9e862375SLee Jones #include <linux/interrupt.h>
17*9e862375SLee Jones #include <linux/io.h>
18*9e862375SLee Jones #include <linux/module.h>
19*9e862375SLee Jones #include <linux/pinctrl/consumer.h>
20*9e862375SLee Jones #include <linux/platform_device.h>
21*9e862375SLee Jones #include <linux/of.h>
22*9e862375SLee Jones #include <linux/of_gpio.h>
23*9e862375SLee Jones #include <linux/of_irq.h>
24*9e862375SLee Jones #include <linux/pm_runtime.h>
25*9e862375SLee Jones #include <linux/spi/spi.h>
26*9e862375SLee Jones #include <linux/spi/spi_bitbang.h>
27*9e862375SLee Jones 
28*9e862375SLee Jones /* SSC registers */
29*9e862375SLee Jones #define SSC_BRG				0x000
30*9e862375SLee Jones #define SSC_TBUF			0x004
31*9e862375SLee Jones #define SSC_RBUF			0x008
32*9e862375SLee Jones #define SSC_CTL				0x00C
33*9e862375SLee Jones #define SSC_IEN				0x010
34*9e862375SLee Jones #define SSC_I2C				0x018
35*9e862375SLee Jones 
36*9e862375SLee Jones /* SSC Control */
37*9e862375SLee Jones #define SSC_CTL_DATA_WIDTH_9		0x8
38*9e862375SLee Jones #define SSC_CTL_DATA_WIDTH_MSK		0xf
39*9e862375SLee Jones #define SSC_CTL_BM			0xf
40*9e862375SLee Jones #define SSC_CTL_HB			BIT(4)
41*9e862375SLee Jones #define SSC_CTL_PH			BIT(5)
42*9e862375SLee Jones #define SSC_CTL_PO			BIT(6)
43*9e862375SLee Jones #define SSC_CTL_SR			BIT(7)
44*9e862375SLee Jones #define SSC_CTL_MS			BIT(8)
45*9e862375SLee Jones #define SSC_CTL_EN			BIT(9)
46*9e862375SLee Jones #define SSC_CTL_LPB			BIT(10)
47*9e862375SLee Jones #define SSC_CTL_EN_TX_FIFO		BIT(11)
48*9e862375SLee Jones #define SSC_CTL_EN_RX_FIFO		BIT(12)
49*9e862375SLee Jones #define SSC_CTL_EN_CLST_RX		BIT(13)
50*9e862375SLee Jones 
51*9e862375SLee Jones /* SSC Interrupt Enable */
52*9e862375SLee Jones #define SSC_IEN_TEEN			BIT(2)
53*9e862375SLee Jones 
54*9e862375SLee Jones #define FIFO_SIZE			8
55*9e862375SLee Jones 
56*9e862375SLee Jones struct spi_st {
57*9e862375SLee Jones 	/* SSC SPI Controller */
58*9e862375SLee Jones 	void __iomem		*base;
59*9e862375SLee Jones 	struct clk		*clk;
60*9e862375SLee Jones 	struct device		*dev;
61*9e862375SLee Jones 
62*9e862375SLee Jones 	/* SSC SPI current transaction */
63*9e862375SLee Jones 	const u8		*tx_ptr;
64*9e862375SLee Jones 	u8			*rx_ptr;
65*9e862375SLee Jones 	u16			bytes_per_word;
66*9e862375SLee Jones 	unsigned int		words_remaining;
67*9e862375SLee Jones 	unsigned int		baud;
68*9e862375SLee Jones 	struct completion	done;
69*9e862375SLee Jones };
70*9e862375SLee Jones 
71*9e862375SLee Jones static int spi_st_clk_enable(struct spi_st *spi_st)
72*9e862375SLee Jones {
73*9e862375SLee Jones 	/*
74*9e862375SLee Jones 	 * Current platforms use one of the core clocks for SPI and I2C.
75*9e862375SLee Jones 	 * If we attempt to disable the clock, the system will hang.
76*9e862375SLee Jones 	 *
77*9e862375SLee Jones 	 * TODO: Remove this when platform supports power domains.
78*9e862375SLee Jones 	 */
79*9e862375SLee Jones 	return 0;
80*9e862375SLee Jones 
81*9e862375SLee Jones 	return clk_prepare_enable(spi_st->clk);
82*9e862375SLee Jones }
83*9e862375SLee Jones 
84*9e862375SLee Jones static void spi_st_clk_disable(struct spi_st *spi_st)
85*9e862375SLee Jones {
86*9e862375SLee Jones 	/*
87*9e862375SLee Jones 	 * Current platforms use one of the core clocks for SPI and I2C.
88*9e862375SLee Jones 	 * If we attempt to disable the clock, the system will hang.
89*9e862375SLee Jones 	 *
90*9e862375SLee Jones 	 * TODO: Remove this when platform supports power domains.
91*9e862375SLee Jones 	 */
92*9e862375SLee Jones 	return;
93*9e862375SLee Jones 
94*9e862375SLee Jones 	clk_disable_unprepare(spi_st->clk);
95*9e862375SLee Jones }
96*9e862375SLee Jones 
97*9e862375SLee Jones /* Load the TX FIFO */
98*9e862375SLee Jones static void ssc_write_tx_fifo(struct spi_st *spi_st)
99*9e862375SLee Jones {
100*9e862375SLee Jones 	unsigned int count, i;
101*9e862375SLee Jones 	uint32_t word = 0;
102*9e862375SLee Jones 
103*9e862375SLee Jones 	if (spi_st->words_remaining > FIFO_SIZE)
104*9e862375SLee Jones 		count = FIFO_SIZE;
105*9e862375SLee Jones 	else
106*9e862375SLee Jones 		count = spi_st->words_remaining;
107*9e862375SLee Jones 
108*9e862375SLee Jones 	for (i = 0; i < count; i++) {
109*9e862375SLee Jones 		if (spi_st->tx_ptr) {
110*9e862375SLee Jones 			if (spi_st->bytes_per_word == 1) {
111*9e862375SLee Jones 				word = *spi_st->tx_ptr++;
112*9e862375SLee Jones 			} else {
113*9e862375SLee Jones 				word = *spi_st->tx_ptr++;
114*9e862375SLee Jones 				word = *spi_st->tx_ptr++ | (word << 8);
115*9e862375SLee Jones 			}
116*9e862375SLee Jones 		}
117*9e862375SLee Jones 		writel_relaxed(word, spi_st->base + SSC_TBUF);
118*9e862375SLee Jones 	}
119*9e862375SLee Jones }
120*9e862375SLee Jones 
121*9e862375SLee Jones /* Read the RX FIFO */
122*9e862375SLee Jones static void ssc_read_rx_fifo(struct spi_st *spi_st)
123*9e862375SLee Jones {
124*9e862375SLee Jones 	unsigned int count, i;
125*9e862375SLee Jones 	uint32_t word = 0;
126*9e862375SLee Jones 
127*9e862375SLee Jones 	if (spi_st->words_remaining > FIFO_SIZE)
128*9e862375SLee Jones 		count = FIFO_SIZE;
129*9e862375SLee Jones 	else
130*9e862375SLee Jones 		count = spi_st->words_remaining;
131*9e862375SLee Jones 
132*9e862375SLee Jones 	for (i = 0; i < count; i++) {
133*9e862375SLee Jones 		word = readl_relaxed(spi_st->base + SSC_RBUF);
134*9e862375SLee Jones 
135*9e862375SLee Jones 		if (spi_st->rx_ptr) {
136*9e862375SLee Jones 			if (spi_st->bytes_per_word == 1) {
137*9e862375SLee Jones 				*spi_st->rx_ptr++ = (uint8_t)word;
138*9e862375SLee Jones 			} else {
139*9e862375SLee Jones 				*spi_st->rx_ptr++ = (word >> 8);
140*9e862375SLee Jones 				*spi_st->rx_ptr++ = word & 0xff;
141*9e862375SLee Jones 			}
142*9e862375SLee Jones 		}
143*9e862375SLee Jones 	}
144*9e862375SLee Jones 	spi_st->words_remaining -= count;
145*9e862375SLee Jones }
146*9e862375SLee Jones 
147*9e862375SLee Jones static int spi_st_transfer_one(struct spi_master *master,
148*9e862375SLee Jones 			       struct spi_device *spi, struct spi_transfer *t)
149*9e862375SLee Jones {
150*9e862375SLee Jones 	struct spi_st *spi_st = spi_master_get_devdata(master);
151*9e862375SLee Jones 	uint32_t ctl = 0;
152*9e862375SLee Jones 
153*9e862375SLee Jones 	/* Setup transfer */
154*9e862375SLee Jones 	spi_st->tx_ptr = t->tx_buf;
155*9e862375SLee Jones 	spi_st->rx_ptr = t->rx_buf;
156*9e862375SLee Jones 
157*9e862375SLee Jones 	if (spi->bits_per_word > 8) {
158*9e862375SLee Jones 		/*
159*9e862375SLee Jones 		 * Anything greater than 8 bits-per-word requires 2
160*9e862375SLee Jones 		 * bytes-per-word in the RX/TX buffers
161*9e862375SLee Jones 		 */
162*9e862375SLee Jones 		spi_st->bytes_per_word = 2;
163*9e862375SLee Jones 		spi_st->words_remaining = t->len / 2;
164*9e862375SLee Jones 
165*9e862375SLee Jones 	} else if (spi->bits_per_word == 8 && !(t->len & 0x1)) {
166*9e862375SLee Jones 		/*
167*9e862375SLee Jones 		 * If transfer is even-length, and 8 bits-per-word, then
168*9e862375SLee Jones 		 * implement as half-length 16 bits-per-word transfer
169*9e862375SLee Jones 		 */
170*9e862375SLee Jones 		spi_st->bytes_per_word = 2;
171*9e862375SLee Jones 		spi_st->words_remaining = t->len / 2;
172*9e862375SLee Jones 
173*9e862375SLee Jones 		/* Set SSC_CTL to 16 bits-per-word */
174*9e862375SLee Jones 		ctl = readl_relaxed(spi_st->base + SSC_CTL);
175*9e862375SLee Jones 		writel_relaxed((ctl | 0xf), spi_st->base + SSC_CTL);
176*9e862375SLee Jones 
177*9e862375SLee Jones 		readl_relaxed(spi_st->base + SSC_RBUF);
178*9e862375SLee Jones 
179*9e862375SLee Jones 	} else {
180*9e862375SLee Jones 		spi_st->bytes_per_word = 1;
181*9e862375SLee Jones 		spi_st->words_remaining = t->len;
182*9e862375SLee Jones 	}
183*9e862375SLee Jones 
184*9e862375SLee Jones 	reinit_completion(&spi_st->done);
185*9e862375SLee Jones 
186*9e862375SLee Jones 	/* Start transfer by writing to the TX FIFO */
187*9e862375SLee Jones 	ssc_write_tx_fifo(spi_st);
188*9e862375SLee Jones 	writel_relaxed(SSC_IEN_TEEN, spi_st->base + SSC_IEN);
189*9e862375SLee Jones 
190*9e862375SLee Jones 	/* Wait for transfer to complete */
191*9e862375SLee Jones 	wait_for_completion(&spi_st->done);
192*9e862375SLee Jones 
193*9e862375SLee Jones 	/* Restore SSC_CTL if necessary */
194*9e862375SLee Jones 	if (ctl)
195*9e862375SLee Jones 		writel_relaxed(ctl, spi_st->base + SSC_CTL);
196*9e862375SLee Jones 
197*9e862375SLee Jones 	spi_finalize_current_transfer(spi->master);
198*9e862375SLee Jones 
199*9e862375SLee Jones 	return t->len;
200*9e862375SLee Jones }
201*9e862375SLee Jones 
202*9e862375SLee Jones static void spi_st_cleanup(struct spi_device *spi)
203*9e862375SLee Jones {
204*9e862375SLee Jones 	int cs = spi->cs_gpio;
205*9e862375SLee Jones 
206*9e862375SLee Jones 	if (gpio_is_valid(cs))
207*9e862375SLee Jones 		devm_gpio_free(&spi->dev, cs);
208*9e862375SLee Jones }
209*9e862375SLee Jones 
210*9e862375SLee Jones /* the spi->mode bits understood by this driver: */
211*9e862375SLee Jones #define MODEBITS  (SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_LOOP | SPI_CS_HIGH)
212*9e862375SLee Jones static int spi_st_setup(struct spi_device *spi)
213*9e862375SLee Jones {
214*9e862375SLee Jones 	struct spi_st *spi_st = spi_master_get_devdata(spi->master);
215*9e862375SLee Jones 	u32 spi_st_clk, sscbrg, var;
216*9e862375SLee Jones 	u32 hz = spi->max_speed_hz;
217*9e862375SLee Jones 	int cs = spi->cs_gpio;
218*9e862375SLee Jones 	int ret;
219*9e862375SLee Jones 
220*9e862375SLee Jones 	if (spi->mode & ~MODEBITS) {
221*9e862375SLee Jones 		dev_err(&spi->dev, "unsupported mode bits 0x%x\n",
222*9e862375SLee Jones 			spi->mode & ~MODEBITS);
223*9e862375SLee Jones 		return -EINVAL;
224*9e862375SLee Jones 	}
225*9e862375SLee Jones 
226*9e862375SLee Jones 	if (!hz)  {
227*9e862375SLee Jones 		dev_err(&spi->dev, "max_speed_hz unspecified\n");
228*9e862375SLee Jones 		return -EINVAL;
229*9e862375SLee Jones 	}
230*9e862375SLee Jones 
231*9e862375SLee Jones 	if (!gpio_is_valid(cs)) {
232*9e862375SLee Jones 		dev_err(&spi->dev, "%d is not a valid gpio\n", cs);
233*9e862375SLee Jones 		return -EINVAL;
234*9e862375SLee Jones 	}
235*9e862375SLee Jones 
236*9e862375SLee Jones 	if (devm_gpio_request(&spi->dev, cs, dev_name(&spi->dev))) {
237*9e862375SLee Jones 		dev_err(&spi->dev, "could not request gpio:%d\n", cs);
238*9e862375SLee Jones 		return -EINVAL;
239*9e862375SLee Jones 	}
240*9e862375SLee Jones 
241*9e862375SLee Jones 	ret = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH);
242*9e862375SLee Jones 	if (ret)
243*9e862375SLee Jones 		return ret;
244*9e862375SLee Jones 
245*9e862375SLee Jones 	spi_st_clk = clk_get_rate(spi_st->clk);
246*9e862375SLee Jones 
247*9e862375SLee Jones 	/* Set SSC_BRF */
248*9e862375SLee Jones 	sscbrg = spi_st_clk / (2 * hz);
249*9e862375SLee Jones 	if (sscbrg < 0x07 || sscbrg > BIT(16)) {
250*9e862375SLee Jones 		dev_err(&spi->dev,
251*9e862375SLee Jones 			"baudrate %d outside valid range %d\n", sscbrg, hz);
252*9e862375SLee Jones 		return -EINVAL;
253*9e862375SLee Jones 	}
254*9e862375SLee Jones 
255*9e862375SLee Jones 	spi_st->baud = spi_st_clk / (2 * sscbrg);
256*9e862375SLee Jones 	if (sscbrg == BIT(16)) /* 16-bit counter wraps */
257*9e862375SLee Jones 		sscbrg = 0x0;
258*9e862375SLee Jones 
259*9e862375SLee Jones 	writel_relaxed(sscbrg, spi_st->base + SSC_BRG);
260*9e862375SLee Jones 
261*9e862375SLee Jones 	dev_dbg(&spi->dev,
262*9e862375SLee Jones 		"setting baudrate:target= %u hz, actual= %u hz, sscbrg= %u\n",
263*9e862375SLee Jones 		hz, spi_st->baud, sscbrg);
264*9e862375SLee Jones 
265*9e862375SLee Jones 	 /* Set SSC_CTL and enable SSC */
266*9e862375SLee Jones 	 var = readl_relaxed(spi_st->base + SSC_CTL);
267*9e862375SLee Jones 	 var |= SSC_CTL_MS;
268*9e862375SLee Jones 
269*9e862375SLee Jones 	 if (spi->mode & SPI_CPOL)
270*9e862375SLee Jones 		var |= SSC_CTL_PO;
271*9e862375SLee Jones 	 else
272*9e862375SLee Jones 		var &= ~SSC_CTL_PO;
273*9e862375SLee Jones 
274*9e862375SLee Jones 	 if (spi->mode & SPI_CPHA)
275*9e862375SLee Jones 		var |= SSC_CTL_PH;
276*9e862375SLee Jones 	 else
277*9e862375SLee Jones 		var &= ~SSC_CTL_PH;
278*9e862375SLee Jones 
279*9e862375SLee Jones 	 if ((spi->mode & SPI_LSB_FIRST) == 0)
280*9e862375SLee Jones 		var |= SSC_CTL_HB;
281*9e862375SLee Jones 	 else
282*9e862375SLee Jones 		var &= ~SSC_CTL_HB;
283*9e862375SLee Jones 
284*9e862375SLee Jones 	 if (spi->mode & SPI_LOOP)
285*9e862375SLee Jones 		var |= SSC_CTL_LPB;
286*9e862375SLee Jones 	 else
287*9e862375SLee Jones 		var &= ~SSC_CTL_LPB;
288*9e862375SLee Jones 
289*9e862375SLee Jones 	 var &= ~SSC_CTL_DATA_WIDTH_MSK;
290*9e862375SLee Jones 	 var |= (spi->bits_per_word - 1);
291*9e862375SLee Jones 
292*9e862375SLee Jones 	 var |= SSC_CTL_EN_TX_FIFO | SSC_CTL_EN_RX_FIFO;
293*9e862375SLee Jones 	 var |= SSC_CTL_EN;
294*9e862375SLee Jones 
295*9e862375SLee Jones 	 writel_relaxed(var, spi_st->base + SSC_CTL);
296*9e862375SLee Jones 
297*9e862375SLee Jones 	 /* Clear the status register */
298*9e862375SLee Jones 	 readl_relaxed(spi_st->base + SSC_RBUF);
299*9e862375SLee Jones 
300*9e862375SLee Jones 	 return 0;
301*9e862375SLee Jones }
302*9e862375SLee Jones 
303*9e862375SLee Jones /* Interrupt fired when TX shift register becomes empty */
304*9e862375SLee Jones static irqreturn_t spi_st_irq(int irq, void *dev_id)
305*9e862375SLee Jones {
306*9e862375SLee Jones 	struct spi_st *spi_st = (struct spi_st *)dev_id;
307*9e862375SLee Jones 
308*9e862375SLee Jones 	/* Read RX FIFO */
309*9e862375SLee Jones 	ssc_read_rx_fifo(spi_st);
310*9e862375SLee Jones 
311*9e862375SLee Jones 	/* Fill TX FIFO */
312*9e862375SLee Jones 	if (spi_st->words_remaining) {
313*9e862375SLee Jones 		ssc_write_tx_fifo(spi_st);
314*9e862375SLee Jones 	} else {
315*9e862375SLee Jones 		/* TX/RX complete */
316*9e862375SLee Jones 		writel_relaxed(0x0, spi_st->base + SSC_IEN);
317*9e862375SLee Jones 		/*
318*9e862375SLee Jones 		 * read SSC_IEN to ensure that this bit is set
319*9e862375SLee Jones 		 * before re-enabling interrupt
320*9e862375SLee Jones 		 */
321*9e862375SLee Jones 		readl(spi_st->base + SSC_IEN);
322*9e862375SLee Jones 		complete(&spi_st->done);
323*9e862375SLee Jones 	}
324*9e862375SLee Jones 
325*9e862375SLee Jones 	return IRQ_HANDLED;
326*9e862375SLee Jones }
327*9e862375SLee Jones 
328*9e862375SLee Jones static int spi_st_probe(struct platform_device *pdev)
329*9e862375SLee Jones {
330*9e862375SLee Jones 	struct device_node *np = pdev->dev.of_node;
331*9e862375SLee Jones 	struct spi_master *master;
332*9e862375SLee Jones 	struct resource *res;
333*9e862375SLee Jones 	struct spi_st *spi_st;
334*9e862375SLee Jones 	int irq, ret = 0;
335*9e862375SLee Jones 	u32 var;
336*9e862375SLee Jones 
337*9e862375SLee Jones 	master = spi_alloc_master(&pdev->dev, sizeof(*spi_st));
338*9e862375SLee Jones 	if (!master)
339*9e862375SLee Jones 		return -ENOMEM;
340*9e862375SLee Jones 
341*9e862375SLee Jones 	master->dev.of_node		= np;
342*9e862375SLee Jones 	master->mode_bits		= MODEBITS;
343*9e862375SLee Jones 	master->setup			= spi_st_setup;
344*9e862375SLee Jones 	master->cleanup			= spi_st_cleanup;
345*9e862375SLee Jones 	master->transfer_one		= spi_st_transfer_one;
346*9e862375SLee Jones 	master->bits_per_word_mask	= SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
347*9e862375SLee Jones 	master->auto_runtime_pm		= true;
348*9e862375SLee Jones 	master->bus_num			= pdev->id;
349*9e862375SLee Jones 	spi_st				= spi_master_get_devdata(master);
350*9e862375SLee Jones 
351*9e862375SLee Jones 	spi_st->clk = devm_clk_get(&pdev->dev, "ssc");
352*9e862375SLee Jones 	if (IS_ERR(spi_st->clk)) {
353*9e862375SLee Jones 		dev_err(&pdev->dev, "Unable to request clock\n");
354*9e862375SLee Jones 		return PTR_ERR(spi_st->clk);
355*9e862375SLee Jones 	}
356*9e862375SLee Jones 
357*9e862375SLee Jones 	ret = spi_st_clk_enable(spi_st);
358*9e862375SLee Jones 	if (ret)
359*9e862375SLee Jones 		return ret;
360*9e862375SLee Jones 
361*9e862375SLee Jones 	init_completion(&spi_st->done);
362*9e862375SLee Jones 
363*9e862375SLee Jones 	/* Get resources */
364*9e862375SLee Jones 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
365*9e862375SLee Jones 	spi_st->base = devm_ioremap_resource(&pdev->dev, res);
366*9e862375SLee Jones 	if (IS_ERR(spi_st->base)) {
367*9e862375SLee Jones 		ret = PTR_ERR(spi_st->base);
368*9e862375SLee Jones 		goto clk_disable;
369*9e862375SLee Jones 	}
370*9e862375SLee Jones 
371*9e862375SLee Jones 	/* Disable I2C and Reset SSC */
372*9e862375SLee Jones 	writel_relaxed(0x0, spi_st->base + SSC_I2C);
373*9e862375SLee Jones 	var = readw_relaxed(spi_st->base + SSC_CTL);
374*9e862375SLee Jones 	var |= SSC_CTL_SR;
375*9e862375SLee Jones 	writel_relaxed(var, spi_st->base + SSC_CTL);
376*9e862375SLee Jones 
377*9e862375SLee Jones 	udelay(1);
378*9e862375SLee Jones 	var = readl_relaxed(spi_st->base + SSC_CTL);
379*9e862375SLee Jones 	var &= ~SSC_CTL_SR;
380*9e862375SLee Jones 	writel_relaxed(var, spi_st->base + SSC_CTL);
381*9e862375SLee Jones 
382*9e862375SLee Jones 	/* Set SSC into slave mode before reconfiguring PIO pins */
383*9e862375SLee Jones 	var = readl_relaxed(spi_st->base + SSC_CTL);
384*9e862375SLee Jones 	var &= ~SSC_CTL_MS;
385*9e862375SLee Jones 	writel_relaxed(var, spi_st->base + SSC_CTL);
386*9e862375SLee Jones 
387*9e862375SLee Jones 	irq = irq_of_parse_and_map(np, 0);
388*9e862375SLee Jones 	if (!irq) {
389*9e862375SLee Jones 		dev_err(&pdev->dev, "IRQ missing or invalid\n");
390*9e862375SLee Jones 		ret = -EINVAL;
391*9e862375SLee Jones 		goto clk_disable;
392*9e862375SLee Jones 	}
393*9e862375SLee Jones 
394*9e862375SLee Jones 	ret = devm_request_irq(&pdev->dev, irq, spi_st_irq, 0,
395*9e862375SLee Jones 			       pdev->name, spi_st);
396*9e862375SLee Jones 	if (ret) {
397*9e862375SLee Jones 		dev_err(&pdev->dev, "Failed to request irq %d\n", irq);
398*9e862375SLee Jones 		goto clk_disable;
399*9e862375SLee Jones 	}
400*9e862375SLee Jones 
401*9e862375SLee Jones 	/* by default the device is on */
402*9e862375SLee Jones 	pm_runtime_set_active(&pdev->dev);
403*9e862375SLee Jones 	pm_runtime_enable(&pdev->dev);
404*9e862375SLee Jones 
405*9e862375SLee Jones 	platform_set_drvdata(pdev, master);
406*9e862375SLee Jones 
407*9e862375SLee Jones 	ret = devm_spi_register_master(&pdev->dev, master);
408*9e862375SLee Jones 	if (ret) {
409*9e862375SLee Jones 		dev_err(&pdev->dev, "Failed to register master\n");
410*9e862375SLee Jones 		goto clk_disable;
411*9e862375SLee Jones 	}
412*9e862375SLee Jones 
413*9e862375SLee Jones 	return 0;
414*9e862375SLee Jones 
415*9e862375SLee Jones clk_disable:
416*9e862375SLee Jones 	spi_st_clk_disable(spi_st);
417*9e862375SLee Jones 
418*9e862375SLee Jones 	return ret;
419*9e862375SLee Jones }
420*9e862375SLee Jones 
421*9e862375SLee Jones static int spi_st_remove(struct platform_device *pdev)
422*9e862375SLee Jones {
423*9e862375SLee Jones 	struct spi_master *master = platform_get_drvdata(pdev);
424*9e862375SLee Jones 	struct spi_st *spi_st = spi_master_get_devdata(master);
425*9e862375SLee Jones 
426*9e862375SLee Jones 	spi_st_clk_disable(spi_st);
427*9e862375SLee Jones 
428*9e862375SLee Jones 	pinctrl_pm_select_sleep_state(&pdev->dev);
429*9e862375SLee Jones 
430*9e862375SLee Jones 	return 0;
431*9e862375SLee Jones }
432*9e862375SLee Jones 
433*9e862375SLee Jones #ifdef CONFIG_PM
434*9e862375SLee Jones static int spi_st_runtime_suspend(struct device *dev)
435*9e862375SLee Jones {
436*9e862375SLee Jones 	struct spi_master *master = dev_get_drvdata(dev);
437*9e862375SLee Jones 	struct spi_st *spi_st = spi_master_get_devdata(master);
438*9e862375SLee Jones 
439*9e862375SLee Jones 	writel_relaxed(0, spi_st->base + SSC_IEN);
440*9e862375SLee Jones 	pinctrl_pm_select_sleep_state(dev);
441*9e862375SLee Jones 
442*9e862375SLee Jones 	spi_st_clk_disable(spi_st);
443*9e862375SLee Jones 
444*9e862375SLee Jones 	return 0;
445*9e862375SLee Jones }
446*9e862375SLee Jones 
447*9e862375SLee Jones static int spi_st_runtime_resume(struct device *dev)
448*9e862375SLee Jones {
449*9e862375SLee Jones 	struct spi_master *master = dev_get_drvdata(dev);
450*9e862375SLee Jones 	struct spi_st *spi_st = spi_master_get_devdata(master);
451*9e862375SLee Jones 	int ret;
452*9e862375SLee Jones 
453*9e862375SLee Jones 	ret = spi_st_clk_enable(spi_st);
454*9e862375SLee Jones 	pinctrl_pm_select_default_state(dev);
455*9e862375SLee Jones 
456*9e862375SLee Jones 	return ret;
457*9e862375SLee Jones }
458*9e862375SLee Jones #endif
459*9e862375SLee Jones 
460*9e862375SLee Jones #ifdef CONFIG_PM_SLEEP
461*9e862375SLee Jones static int spi_st_suspend(struct device *dev)
462*9e862375SLee Jones {
463*9e862375SLee Jones 	struct spi_master *master = dev_get_drvdata(dev);
464*9e862375SLee Jones 	int ret;
465*9e862375SLee Jones 
466*9e862375SLee Jones 	ret = spi_master_suspend(master);
467*9e862375SLee Jones 	if (ret)
468*9e862375SLee Jones 		return ret;
469*9e862375SLee Jones 
470*9e862375SLee Jones 	return pm_runtime_force_suspend(dev);
471*9e862375SLee Jones }
472*9e862375SLee Jones 
473*9e862375SLee Jones static int spi_st_resume(struct device *dev)
474*9e862375SLee Jones {
475*9e862375SLee Jones 	struct spi_master *master = dev_get_drvdata(dev);
476*9e862375SLee Jones 	int ret;
477*9e862375SLee Jones 
478*9e862375SLee Jones 	ret = spi_master_resume(master);
479*9e862375SLee Jones 	if (ret)
480*9e862375SLee Jones 		return ret;
481*9e862375SLee Jones 
482*9e862375SLee Jones 	return pm_runtime_force_resume(dev);
483*9e862375SLee Jones }
484*9e862375SLee Jones #endif
485*9e862375SLee Jones 
486*9e862375SLee Jones static const struct dev_pm_ops spi_st_pm = {
487*9e862375SLee Jones 	SET_SYSTEM_SLEEP_PM_OPS(spi_st_suspend, spi_st_resume)
488*9e862375SLee Jones 	SET_RUNTIME_PM_OPS(spi_st_runtime_suspend, spi_st_runtime_resume, NULL)
489*9e862375SLee Jones };
490*9e862375SLee Jones 
491*9e862375SLee Jones static struct of_device_id stm_spi_match[] = {
492*9e862375SLee Jones 	{ .compatible = "st,comms-ssc4-spi", },
493*9e862375SLee Jones 	{},
494*9e862375SLee Jones };
495*9e862375SLee Jones MODULE_DEVICE_TABLE(of, stm_spi_match);
496*9e862375SLee Jones 
497*9e862375SLee Jones static struct platform_driver spi_st_driver = {
498*9e862375SLee Jones 	.driver = {
499*9e862375SLee Jones 		.name = "spi-st",
500*9e862375SLee Jones 		.pm = &spi_st_pm,
501*9e862375SLee Jones 		.of_match_table = of_match_ptr(stm_spi_match),
502*9e862375SLee Jones 	},
503*9e862375SLee Jones 	.probe = spi_st_probe,
504*9e862375SLee Jones 	.remove = spi_st_remove,
505*9e862375SLee Jones };
506*9e862375SLee Jones module_platform_driver(spi_st_driver);
507*9e862375SLee Jones 
508*9e862375SLee Jones MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
509*9e862375SLee Jones MODULE_DESCRIPTION("STM SSC SPI driver");
510*9e862375SLee Jones MODULE_LICENSE("GPL v2");
511