xref: /openbmc/linux/drivers/spi/spi-sun6i.c (revision 794912cff6dba8b5e93948243299bb0b2cb11277)
13558fe90SMaxime Ripard /*
23558fe90SMaxime Ripard  * Copyright (C) 2012 - 2014 Allwinner Tech
33558fe90SMaxime Ripard  * Pan Nan <pannan@allwinnertech.com>
43558fe90SMaxime Ripard  *
53558fe90SMaxime Ripard  * Copyright (C) 2014 Maxime Ripard
63558fe90SMaxime Ripard  * Maxime Ripard <maxime.ripard@free-electrons.com>
73558fe90SMaxime Ripard  *
83558fe90SMaxime Ripard  * This program is free software; you can redistribute it and/or
93558fe90SMaxime Ripard  * modify it under the terms of the GNU General Public License as
103558fe90SMaxime Ripard  * published by the Free Software Foundation; either version 2 of
113558fe90SMaxime Ripard  * the License, or (at your option) any later version.
123558fe90SMaxime Ripard  */
133558fe90SMaxime Ripard 
143558fe90SMaxime Ripard #include <linux/clk.h>
153558fe90SMaxime Ripard #include <linux/delay.h>
163558fe90SMaxime Ripard #include <linux/device.h>
173558fe90SMaxime Ripard #include <linux/interrupt.h>
183558fe90SMaxime Ripard #include <linux/io.h>
193558fe90SMaxime Ripard #include <linux/module.h>
203558fe90SMaxime Ripard #include <linux/platform_device.h>
213558fe90SMaxime Ripard #include <linux/pm_runtime.h>
223558fe90SMaxime Ripard #include <linux/reset.h>
233558fe90SMaxime Ripard 
243558fe90SMaxime Ripard #include <linux/spi/spi.h>
253558fe90SMaxime Ripard 
263558fe90SMaxime Ripard #define SUN6I_FIFO_DEPTH		128
273558fe90SMaxime Ripard 
283558fe90SMaxime Ripard #define SUN6I_GBL_CTL_REG		0x04
293558fe90SMaxime Ripard #define SUN6I_GBL_CTL_BUS_ENABLE		BIT(0)
303558fe90SMaxime Ripard #define SUN6I_GBL_CTL_MASTER			BIT(1)
313558fe90SMaxime Ripard #define SUN6I_GBL_CTL_TP			BIT(7)
323558fe90SMaxime Ripard #define SUN6I_GBL_CTL_RST			BIT(31)
333558fe90SMaxime Ripard 
343558fe90SMaxime Ripard #define SUN6I_TFR_CTL_REG		0x08
353558fe90SMaxime Ripard #define SUN6I_TFR_CTL_CPHA			BIT(0)
363558fe90SMaxime Ripard #define SUN6I_TFR_CTL_CPOL			BIT(1)
373558fe90SMaxime Ripard #define SUN6I_TFR_CTL_SPOL			BIT(2)
38d31ad46fSAxel Lin #define SUN6I_TFR_CTL_CS_MASK			0x30
39d31ad46fSAxel Lin #define SUN6I_TFR_CTL_CS(cs)			(((cs) << 4) & SUN6I_TFR_CTL_CS_MASK)
403558fe90SMaxime Ripard #define SUN6I_TFR_CTL_CS_MANUAL			BIT(6)
413558fe90SMaxime Ripard #define SUN6I_TFR_CTL_CS_LEVEL			BIT(7)
423558fe90SMaxime Ripard #define SUN6I_TFR_CTL_DHB			BIT(8)
433558fe90SMaxime Ripard #define SUN6I_TFR_CTL_FBS			BIT(12)
443558fe90SMaxime Ripard #define SUN6I_TFR_CTL_XCH			BIT(31)
453558fe90SMaxime Ripard 
463558fe90SMaxime Ripard #define SUN6I_INT_CTL_REG		0x10
473558fe90SMaxime Ripard #define SUN6I_INT_CTL_RF_OVF			BIT(8)
483558fe90SMaxime Ripard #define SUN6I_INT_CTL_TC			BIT(12)
493558fe90SMaxime Ripard 
503558fe90SMaxime Ripard #define SUN6I_INT_STA_REG		0x14
513558fe90SMaxime Ripard 
523558fe90SMaxime Ripard #define SUN6I_FIFO_CTL_REG		0x18
533558fe90SMaxime Ripard #define SUN6I_FIFO_CTL_RF_RST			BIT(15)
543558fe90SMaxime Ripard #define SUN6I_FIFO_CTL_TF_RST			BIT(31)
553558fe90SMaxime Ripard 
563558fe90SMaxime Ripard #define SUN6I_FIFO_STA_REG		0x1c
573558fe90SMaxime Ripard #define SUN6I_FIFO_STA_RF_CNT_MASK		0x7f
583558fe90SMaxime Ripard #define SUN6I_FIFO_STA_RF_CNT_BITS		0
593558fe90SMaxime Ripard #define SUN6I_FIFO_STA_TF_CNT_MASK		0x7f
603558fe90SMaxime Ripard #define SUN6I_FIFO_STA_TF_CNT_BITS		16
613558fe90SMaxime Ripard 
623558fe90SMaxime Ripard #define SUN6I_CLK_CTL_REG		0x24
633558fe90SMaxime Ripard #define SUN6I_CLK_CTL_CDR2_MASK			0xff
643558fe90SMaxime Ripard #define SUN6I_CLK_CTL_CDR2(div)			(((div) & SUN6I_CLK_CTL_CDR2_MASK) << 0)
653558fe90SMaxime Ripard #define SUN6I_CLK_CTL_CDR1_MASK			0xf
663558fe90SMaxime Ripard #define SUN6I_CLK_CTL_CDR1(div)			(((div) & SUN6I_CLK_CTL_CDR1_MASK) << 8)
673558fe90SMaxime Ripard #define SUN6I_CLK_CTL_DRS			BIT(12)
683558fe90SMaxime Ripard 
693558fe90SMaxime Ripard #define SUN6I_BURST_CNT_REG		0x30
703558fe90SMaxime Ripard #define SUN6I_BURST_CNT(cnt)			((cnt) & 0xffffff)
713558fe90SMaxime Ripard 
723558fe90SMaxime Ripard #define SUN6I_XMIT_CNT_REG		0x34
733558fe90SMaxime Ripard #define SUN6I_XMIT_CNT(cnt)			((cnt) & 0xffffff)
743558fe90SMaxime Ripard 
753558fe90SMaxime Ripard #define SUN6I_BURST_CTL_CNT_REG		0x38
763558fe90SMaxime Ripard #define SUN6I_BURST_CTL_CNT_STC(cnt)		((cnt) & 0xffffff)
773558fe90SMaxime Ripard 
783558fe90SMaxime Ripard #define SUN6I_TXDATA_REG		0x200
793558fe90SMaxime Ripard #define SUN6I_RXDATA_REG		0x300
803558fe90SMaxime Ripard 
813558fe90SMaxime Ripard struct sun6i_spi {
823558fe90SMaxime Ripard 	struct spi_master	*master;
833558fe90SMaxime Ripard 	void __iomem		*base_addr;
843558fe90SMaxime Ripard 	struct clk		*hclk;
853558fe90SMaxime Ripard 	struct clk		*mclk;
863558fe90SMaxime Ripard 	struct reset_control	*rstc;
873558fe90SMaxime Ripard 
883558fe90SMaxime Ripard 	struct completion	done;
893558fe90SMaxime Ripard 
903558fe90SMaxime Ripard 	const u8		*tx_buf;
913558fe90SMaxime Ripard 	u8			*rx_buf;
923558fe90SMaxime Ripard 	int			len;
933558fe90SMaxime Ripard };
943558fe90SMaxime Ripard 
953558fe90SMaxime Ripard static inline u32 sun6i_spi_read(struct sun6i_spi *sspi, u32 reg)
963558fe90SMaxime Ripard {
973558fe90SMaxime Ripard 	return readl(sspi->base_addr + reg);
983558fe90SMaxime Ripard }
993558fe90SMaxime Ripard 
1003558fe90SMaxime Ripard static inline void sun6i_spi_write(struct sun6i_spi *sspi, u32 reg, u32 value)
1013558fe90SMaxime Ripard {
1023558fe90SMaxime Ripard 	writel(value, sspi->base_addr + reg);
1033558fe90SMaxime Ripard }
1043558fe90SMaxime Ripard 
1053558fe90SMaxime Ripard static inline void sun6i_spi_drain_fifo(struct sun6i_spi *sspi, int len)
1063558fe90SMaxime Ripard {
1073558fe90SMaxime Ripard 	u32 reg, cnt;
1083558fe90SMaxime Ripard 	u8 byte;
1093558fe90SMaxime Ripard 
1103558fe90SMaxime Ripard 	/* See how much data is available */
1113558fe90SMaxime Ripard 	reg = sun6i_spi_read(sspi, SUN6I_FIFO_STA_REG);
1123558fe90SMaxime Ripard 	reg &= SUN6I_FIFO_STA_RF_CNT_MASK;
1133558fe90SMaxime Ripard 	cnt = reg >> SUN6I_FIFO_STA_RF_CNT_BITS;
1143558fe90SMaxime Ripard 
1153558fe90SMaxime Ripard 	if (len > cnt)
1163558fe90SMaxime Ripard 		len = cnt;
1173558fe90SMaxime Ripard 
1183558fe90SMaxime Ripard 	while (len--) {
1193558fe90SMaxime Ripard 		byte = readb(sspi->base_addr + SUN6I_RXDATA_REG);
1203558fe90SMaxime Ripard 		if (sspi->rx_buf)
1213558fe90SMaxime Ripard 			*sspi->rx_buf++ = byte;
1223558fe90SMaxime Ripard 	}
1233558fe90SMaxime Ripard }
1243558fe90SMaxime Ripard 
1253558fe90SMaxime Ripard static inline void sun6i_spi_fill_fifo(struct sun6i_spi *sspi, int len)
1263558fe90SMaxime Ripard {
1273558fe90SMaxime Ripard 	u8 byte;
1283558fe90SMaxime Ripard 
1293558fe90SMaxime Ripard 	if (len > sspi->len)
1303558fe90SMaxime Ripard 		len = sspi->len;
1313558fe90SMaxime Ripard 
1323558fe90SMaxime Ripard 	while (len--) {
1333558fe90SMaxime Ripard 		byte = sspi->tx_buf ? *sspi->tx_buf++ : 0;
1343558fe90SMaxime Ripard 		writeb(byte, sspi->base_addr + SUN6I_TXDATA_REG);
1353558fe90SMaxime Ripard 		sspi->len--;
1363558fe90SMaxime Ripard 	}
1373558fe90SMaxime Ripard }
1383558fe90SMaxime Ripard 
1393558fe90SMaxime Ripard static void sun6i_spi_set_cs(struct spi_device *spi, bool enable)
1403558fe90SMaxime Ripard {
1413558fe90SMaxime Ripard 	struct sun6i_spi *sspi = spi_master_get_devdata(spi->master);
1423558fe90SMaxime Ripard 	u32 reg;
1433558fe90SMaxime Ripard 
1443558fe90SMaxime Ripard 	reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
1453558fe90SMaxime Ripard 	reg &= ~SUN6I_TFR_CTL_CS_MASK;
1463558fe90SMaxime Ripard 	reg |= SUN6I_TFR_CTL_CS(spi->chip_select);
1473558fe90SMaxime Ripard 
1483558fe90SMaxime Ripard 	if (enable)
1493558fe90SMaxime Ripard 		reg |= SUN6I_TFR_CTL_CS_LEVEL;
1503558fe90SMaxime Ripard 	else
1513558fe90SMaxime Ripard 		reg &= ~SUN6I_TFR_CTL_CS_LEVEL;
1523558fe90SMaxime Ripard 
1533558fe90SMaxime Ripard 	sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
1543558fe90SMaxime Ripard }
1553558fe90SMaxime Ripard 
156*794912cfSMichal Suchanek static size_t sun6i_spi_max_transfer_size(struct spi_device *spi)
157*794912cfSMichal Suchanek {
158*794912cfSMichal Suchanek 	return SUN6I_FIFO_DEPTH - 1;
159*794912cfSMichal Suchanek }
1603558fe90SMaxime Ripard 
1613558fe90SMaxime Ripard static int sun6i_spi_transfer_one(struct spi_master *master,
1623558fe90SMaxime Ripard 				  struct spi_device *spi,
1633558fe90SMaxime Ripard 				  struct spi_transfer *tfr)
1643558fe90SMaxime Ripard {
1653558fe90SMaxime Ripard 	struct sun6i_spi *sspi = spi_master_get_devdata(master);
1663558fe90SMaxime Ripard 	unsigned int mclk_rate, div, timeout;
1673558fe90SMaxime Ripard 	unsigned int tx_len = 0;
1683558fe90SMaxime Ripard 	int ret = 0;
1693558fe90SMaxime Ripard 	u32 reg;
1703558fe90SMaxime Ripard 
1713558fe90SMaxime Ripard 	/* We don't support transfer larger than the FIFO */
1723558fe90SMaxime Ripard 	if (tfr->len > SUN6I_FIFO_DEPTH)
1733558fe90SMaxime Ripard 		return -EINVAL;
1743558fe90SMaxime Ripard 
1753558fe90SMaxime Ripard 	reinit_completion(&sspi->done);
1763558fe90SMaxime Ripard 	sspi->tx_buf = tfr->tx_buf;
1773558fe90SMaxime Ripard 	sspi->rx_buf = tfr->rx_buf;
1783558fe90SMaxime Ripard 	sspi->len = tfr->len;
1793558fe90SMaxime Ripard 
1803558fe90SMaxime Ripard 	/* Clear pending interrupts */
1813558fe90SMaxime Ripard 	sun6i_spi_write(sspi, SUN6I_INT_STA_REG, ~0);
1823558fe90SMaxime Ripard 
1833558fe90SMaxime Ripard 	/* Reset FIFO */
1843558fe90SMaxime Ripard 	sun6i_spi_write(sspi, SUN6I_FIFO_CTL_REG,
1853558fe90SMaxime Ripard 			SUN6I_FIFO_CTL_RF_RST | SUN6I_FIFO_CTL_TF_RST);
1863558fe90SMaxime Ripard 
1873558fe90SMaxime Ripard 	/*
1883558fe90SMaxime Ripard 	 * Setup the transfer control register: Chip Select,
1893558fe90SMaxime Ripard 	 * polarities, etc.
1903558fe90SMaxime Ripard 	 */
1913558fe90SMaxime Ripard 	reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
1923558fe90SMaxime Ripard 
1933558fe90SMaxime Ripard 	if (spi->mode & SPI_CPOL)
1943558fe90SMaxime Ripard 		reg |= SUN6I_TFR_CTL_CPOL;
1953558fe90SMaxime Ripard 	else
1963558fe90SMaxime Ripard 		reg &= ~SUN6I_TFR_CTL_CPOL;
1973558fe90SMaxime Ripard 
1983558fe90SMaxime Ripard 	if (spi->mode & SPI_CPHA)
1993558fe90SMaxime Ripard 		reg |= SUN6I_TFR_CTL_CPHA;
2003558fe90SMaxime Ripard 	else
2013558fe90SMaxime Ripard 		reg &= ~SUN6I_TFR_CTL_CPHA;
2023558fe90SMaxime Ripard 
2033558fe90SMaxime Ripard 	if (spi->mode & SPI_LSB_FIRST)
2043558fe90SMaxime Ripard 		reg |= SUN6I_TFR_CTL_FBS;
2053558fe90SMaxime Ripard 	else
2063558fe90SMaxime Ripard 		reg &= ~SUN6I_TFR_CTL_FBS;
2073558fe90SMaxime Ripard 
2083558fe90SMaxime Ripard 	/*
2093558fe90SMaxime Ripard 	 * If it's a TX only transfer, we don't want to fill the RX
2103558fe90SMaxime Ripard 	 * FIFO with bogus data
2113558fe90SMaxime Ripard 	 */
2123558fe90SMaxime Ripard 	if (sspi->rx_buf)
2133558fe90SMaxime Ripard 		reg &= ~SUN6I_TFR_CTL_DHB;
2143558fe90SMaxime Ripard 	else
2153558fe90SMaxime Ripard 		reg |= SUN6I_TFR_CTL_DHB;
2163558fe90SMaxime Ripard 
2173558fe90SMaxime Ripard 	/* We want to control the chip select manually */
2183558fe90SMaxime Ripard 	reg |= SUN6I_TFR_CTL_CS_MANUAL;
2193558fe90SMaxime Ripard 
2203558fe90SMaxime Ripard 	sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
2213558fe90SMaxime Ripard 
2223558fe90SMaxime Ripard 	/* Ensure that we have a parent clock fast enough */
2233558fe90SMaxime Ripard 	mclk_rate = clk_get_rate(sspi->mclk);
22447284e3eSMarcus Weseloh 	if (mclk_rate < (2 * tfr->speed_hz)) {
22547284e3eSMarcus Weseloh 		clk_set_rate(sspi->mclk, 2 * tfr->speed_hz);
2263558fe90SMaxime Ripard 		mclk_rate = clk_get_rate(sspi->mclk);
2273558fe90SMaxime Ripard 	}
2283558fe90SMaxime Ripard 
2293558fe90SMaxime Ripard 	/*
2303558fe90SMaxime Ripard 	 * Setup clock divider.
2313558fe90SMaxime Ripard 	 *
2323558fe90SMaxime Ripard 	 * We have two choices there. Either we can use the clock
2333558fe90SMaxime Ripard 	 * divide rate 1, which is calculated thanks to this formula:
2343558fe90SMaxime Ripard 	 * SPI_CLK = MOD_CLK / (2 ^ cdr)
2353558fe90SMaxime Ripard 	 * Or we can use CDR2, which is calculated with the formula:
2363558fe90SMaxime Ripard 	 * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
2373558fe90SMaxime Ripard 	 * Wether we use the former or the latter is set through the
2383558fe90SMaxime Ripard 	 * DRS bit.
2393558fe90SMaxime Ripard 	 *
2403558fe90SMaxime Ripard 	 * First try CDR2, and if we can't reach the expected
2413558fe90SMaxime Ripard 	 * frequency, fall back to CDR1.
2423558fe90SMaxime Ripard 	 */
24347284e3eSMarcus Weseloh 	div = mclk_rate / (2 * tfr->speed_hz);
2443558fe90SMaxime Ripard 	if (div <= (SUN6I_CLK_CTL_CDR2_MASK + 1)) {
2453558fe90SMaxime Ripard 		if (div > 0)
2463558fe90SMaxime Ripard 			div--;
2473558fe90SMaxime Ripard 
2483558fe90SMaxime Ripard 		reg = SUN6I_CLK_CTL_CDR2(div) | SUN6I_CLK_CTL_DRS;
2493558fe90SMaxime Ripard 	} else {
25047284e3eSMarcus Weseloh 		div = ilog2(mclk_rate) - ilog2(tfr->speed_hz);
2513558fe90SMaxime Ripard 		reg = SUN6I_CLK_CTL_CDR1(div);
2523558fe90SMaxime Ripard 	}
2533558fe90SMaxime Ripard 
2543558fe90SMaxime Ripard 	sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
2553558fe90SMaxime Ripard 
2563558fe90SMaxime Ripard 	/* Setup the transfer now... */
2573558fe90SMaxime Ripard 	if (sspi->tx_buf)
2583558fe90SMaxime Ripard 		tx_len = tfr->len;
2593558fe90SMaxime Ripard 
2603558fe90SMaxime Ripard 	/* Setup the counters */
2613558fe90SMaxime Ripard 	sun6i_spi_write(sspi, SUN6I_BURST_CNT_REG, SUN6I_BURST_CNT(tfr->len));
2623558fe90SMaxime Ripard 	sun6i_spi_write(sspi, SUN6I_XMIT_CNT_REG, SUN6I_XMIT_CNT(tx_len));
2633558fe90SMaxime Ripard 	sun6i_spi_write(sspi, SUN6I_BURST_CTL_CNT_REG,
2643558fe90SMaxime Ripard 			SUN6I_BURST_CTL_CNT_STC(tx_len));
2653558fe90SMaxime Ripard 
2663558fe90SMaxime Ripard 	/* Fill the TX FIFO */
2673558fe90SMaxime Ripard 	sun6i_spi_fill_fifo(sspi, SUN6I_FIFO_DEPTH);
2683558fe90SMaxime Ripard 
2693558fe90SMaxime Ripard 	/* Enable the interrupts */
2703558fe90SMaxime Ripard 	sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, SUN6I_INT_CTL_TC);
2713558fe90SMaxime Ripard 
2723558fe90SMaxime Ripard 	/* Start the transfer */
2733558fe90SMaxime Ripard 	reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
2743558fe90SMaxime Ripard 	sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg | SUN6I_TFR_CTL_XCH);
2753558fe90SMaxime Ripard 
2763558fe90SMaxime Ripard 	timeout = wait_for_completion_timeout(&sspi->done,
2773558fe90SMaxime Ripard 					      msecs_to_jiffies(1000));
2783558fe90SMaxime Ripard 	if (!timeout) {
2793558fe90SMaxime Ripard 		ret = -ETIMEDOUT;
2803558fe90SMaxime Ripard 		goto out;
2813558fe90SMaxime Ripard 	}
2823558fe90SMaxime Ripard 
2833558fe90SMaxime Ripard 	sun6i_spi_drain_fifo(sspi, SUN6I_FIFO_DEPTH);
2843558fe90SMaxime Ripard 
2853558fe90SMaxime Ripard out:
2863558fe90SMaxime Ripard 	sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, 0);
2873558fe90SMaxime Ripard 
2883558fe90SMaxime Ripard 	return ret;
2893558fe90SMaxime Ripard }
2903558fe90SMaxime Ripard 
2913558fe90SMaxime Ripard static irqreturn_t sun6i_spi_handler(int irq, void *dev_id)
2923558fe90SMaxime Ripard {
2933558fe90SMaxime Ripard 	struct sun6i_spi *sspi = dev_id;
2943558fe90SMaxime Ripard 	u32 status = sun6i_spi_read(sspi, SUN6I_INT_STA_REG);
2953558fe90SMaxime Ripard 
2963558fe90SMaxime Ripard 	/* Transfer complete */
2973558fe90SMaxime Ripard 	if (status & SUN6I_INT_CTL_TC) {
2983558fe90SMaxime Ripard 		sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_TC);
2993558fe90SMaxime Ripard 		complete(&sspi->done);
3003558fe90SMaxime Ripard 		return IRQ_HANDLED;
3013558fe90SMaxime Ripard 	}
3023558fe90SMaxime Ripard 
3033558fe90SMaxime Ripard 	return IRQ_NONE;
3043558fe90SMaxime Ripard }
3053558fe90SMaxime Ripard 
3063558fe90SMaxime Ripard static int sun6i_spi_runtime_resume(struct device *dev)
3073558fe90SMaxime Ripard {
3083558fe90SMaxime Ripard 	struct spi_master *master = dev_get_drvdata(dev);
3093558fe90SMaxime Ripard 	struct sun6i_spi *sspi = spi_master_get_devdata(master);
3103558fe90SMaxime Ripard 	int ret;
3113558fe90SMaxime Ripard 
3123558fe90SMaxime Ripard 	ret = clk_prepare_enable(sspi->hclk);
3133558fe90SMaxime Ripard 	if (ret) {
3143558fe90SMaxime Ripard 		dev_err(dev, "Couldn't enable AHB clock\n");
3153558fe90SMaxime Ripard 		goto out;
3163558fe90SMaxime Ripard 	}
3173558fe90SMaxime Ripard 
3183558fe90SMaxime Ripard 	ret = clk_prepare_enable(sspi->mclk);
3193558fe90SMaxime Ripard 	if (ret) {
3203558fe90SMaxime Ripard 		dev_err(dev, "Couldn't enable module clock\n");
3213558fe90SMaxime Ripard 		goto err;
3223558fe90SMaxime Ripard 	}
3233558fe90SMaxime Ripard 
3243558fe90SMaxime Ripard 	ret = reset_control_deassert(sspi->rstc);
3253558fe90SMaxime Ripard 	if (ret) {
3263558fe90SMaxime Ripard 		dev_err(dev, "Couldn't deassert the device from reset\n");
3273558fe90SMaxime Ripard 		goto err2;
3283558fe90SMaxime Ripard 	}
3293558fe90SMaxime Ripard 
3303558fe90SMaxime Ripard 	sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
3313558fe90SMaxime Ripard 			SUN6I_GBL_CTL_BUS_ENABLE | SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
3323558fe90SMaxime Ripard 
3333558fe90SMaxime Ripard 	return 0;
3343558fe90SMaxime Ripard 
3353558fe90SMaxime Ripard err2:
3363558fe90SMaxime Ripard 	clk_disable_unprepare(sspi->mclk);
3373558fe90SMaxime Ripard err:
3383558fe90SMaxime Ripard 	clk_disable_unprepare(sspi->hclk);
3393558fe90SMaxime Ripard out:
3403558fe90SMaxime Ripard 	return ret;
3413558fe90SMaxime Ripard }
3423558fe90SMaxime Ripard 
3433558fe90SMaxime Ripard static int sun6i_spi_runtime_suspend(struct device *dev)
3443558fe90SMaxime Ripard {
3453558fe90SMaxime Ripard 	struct spi_master *master = dev_get_drvdata(dev);
3463558fe90SMaxime Ripard 	struct sun6i_spi *sspi = spi_master_get_devdata(master);
3473558fe90SMaxime Ripard 
3483558fe90SMaxime Ripard 	reset_control_assert(sspi->rstc);
3493558fe90SMaxime Ripard 	clk_disable_unprepare(sspi->mclk);
3503558fe90SMaxime Ripard 	clk_disable_unprepare(sspi->hclk);
3513558fe90SMaxime Ripard 
3523558fe90SMaxime Ripard 	return 0;
3533558fe90SMaxime Ripard }
3543558fe90SMaxime Ripard 
3553558fe90SMaxime Ripard static int sun6i_spi_probe(struct platform_device *pdev)
3563558fe90SMaxime Ripard {
3573558fe90SMaxime Ripard 	struct spi_master *master;
3583558fe90SMaxime Ripard 	struct sun6i_spi *sspi;
3593558fe90SMaxime Ripard 	struct resource	*res;
3603558fe90SMaxime Ripard 	int ret = 0, irq;
3613558fe90SMaxime Ripard 
3623558fe90SMaxime Ripard 	master = spi_alloc_master(&pdev->dev, sizeof(struct sun6i_spi));
3633558fe90SMaxime Ripard 	if (!master) {
3643558fe90SMaxime Ripard 		dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
3653558fe90SMaxime Ripard 		return -ENOMEM;
3663558fe90SMaxime Ripard 	}
3673558fe90SMaxime Ripard 
3683558fe90SMaxime Ripard 	platform_set_drvdata(pdev, master);
3693558fe90SMaxime Ripard 	sspi = spi_master_get_devdata(master);
3703558fe90SMaxime Ripard 
3713558fe90SMaxime Ripard 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3723558fe90SMaxime Ripard 	sspi->base_addr = devm_ioremap_resource(&pdev->dev, res);
3733558fe90SMaxime Ripard 	if (IS_ERR(sspi->base_addr)) {
3743558fe90SMaxime Ripard 		ret = PTR_ERR(sspi->base_addr);
3753558fe90SMaxime Ripard 		goto err_free_master;
3763558fe90SMaxime Ripard 	}
3773558fe90SMaxime Ripard 
3783558fe90SMaxime Ripard 	irq = platform_get_irq(pdev, 0);
3793558fe90SMaxime Ripard 	if (irq < 0) {
3803558fe90SMaxime Ripard 		dev_err(&pdev->dev, "No spi IRQ specified\n");
3813558fe90SMaxime Ripard 		ret = -ENXIO;
3823558fe90SMaxime Ripard 		goto err_free_master;
3833558fe90SMaxime Ripard 	}
3843558fe90SMaxime Ripard 
3853558fe90SMaxime Ripard 	ret = devm_request_irq(&pdev->dev, irq, sun6i_spi_handler,
3863558fe90SMaxime Ripard 			       0, "sun6i-spi", sspi);
3873558fe90SMaxime Ripard 	if (ret) {
3883558fe90SMaxime Ripard 		dev_err(&pdev->dev, "Cannot request IRQ\n");
3893558fe90SMaxime Ripard 		goto err_free_master;
3903558fe90SMaxime Ripard 	}
3913558fe90SMaxime Ripard 
3923558fe90SMaxime Ripard 	sspi->master = master;
3933558fe90SMaxime Ripard 	master->set_cs = sun6i_spi_set_cs;
3943558fe90SMaxime Ripard 	master->transfer_one = sun6i_spi_transfer_one;
3953558fe90SMaxime Ripard 	master->num_chipselect = 4;
3963558fe90SMaxime Ripard 	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
397743a46b8SAxel Lin 	master->bits_per_word_mask = SPI_BPW_MASK(8);
3983558fe90SMaxime Ripard 	master->dev.of_node = pdev->dev.of_node;
3993558fe90SMaxime Ripard 	master->auto_runtime_pm = true;
400*794912cfSMichal Suchanek 	master->max_transfer_size = sun6i_spi_max_transfer_size;
4013558fe90SMaxime Ripard 
4023558fe90SMaxime Ripard 	sspi->hclk = devm_clk_get(&pdev->dev, "ahb");
4033558fe90SMaxime Ripard 	if (IS_ERR(sspi->hclk)) {
4043558fe90SMaxime Ripard 		dev_err(&pdev->dev, "Unable to acquire AHB clock\n");
4053558fe90SMaxime Ripard 		ret = PTR_ERR(sspi->hclk);
4063558fe90SMaxime Ripard 		goto err_free_master;
4073558fe90SMaxime Ripard 	}
4083558fe90SMaxime Ripard 
4093558fe90SMaxime Ripard 	sspi->mclk = devm_clk_get(&pdev->dev, "mod");
4103558fe90SMaxime Ripard 	if (IS_ERR(sspi->mclk)) {
4113558fe90SMaxime Ripard 		dev_err(&pdev->dev, "Unable to acquire module clock\n");
4123558fe90SMaxime Ripard 		ret = PTR_ERR(sspi->mclk);
4133558fe90SMaxime Ripard 		goto err_free_master;
4143558fe90SMaxime Ripard 	}
4153558fe90SMaxime Ripard 
4163558fe90SMaxime Ripard 	init_completion(&sspi->done);
4173558fe90SMaxime Ripard 
4183558fe90SMaxime Ripard 	sspi->rstc = devm_reset_control_get(&pdev->dev, NULL);
4193558fe90SMaxime Ripard 	if (IS_ERR(sspi->rstc)) {
4203558fe90SMaxime Ripard 		dev_err(&pdev->dev, "Couldn't get reset controller\n");
4213558fe90SMaxime Ripard 		ret = PTR_ERR(sspi->rstc);
4223558fe90SMaxime Ripard 		goto err_free_master;
4233558fe90SMaxime Ripard 	}
4243558fe90SMaxime Ripard 
4253558fe90SMaxime Ripard 	/*
4263558fe90SMaxime Ripard 	 * This wake-up/shutdown pattern is to be able to have the
4273558fe90SMaxime Ripard 	 * device woken up, even if runtime_pm is disabled
4283558fe90SMaxime Ripard 	 */
4293558fe90SMaxime Ripard 	ret = sun6i_spi_runtime_resume(&pdev->dev);
4303558fe90SMaxime Ripard 	if (ret) {
4313558fe90SMaxime Ripard 		dev_err(&pdev->dev, "Couldn't resume the device\n");
4323558fe90SMaxime Ripard 		goto err_free_master;
4333558fe90SMaxime Ripard 	}
4343558fe90SMaxime Ripard 
4353558fe90SMaxime Ripard 	pm_runtime_set_active(&pdev->dev);
4363558fe90SMaxime Ripard 	pm_runtime_enable(&pdev->dev);
4373558fe90SMaxime Ripard 	pm_runtime_idle(&pdev->dev);
4383558fe90SMaxime Ripard 
4393558fe90SMaxime Ripard 	ret = devm_spi_register_master(&pdev->dev, master);
4403558fe90SMaxime Ripard 	if (ret) {
4413558fe90SMaxime Ripard 		dev_err(&pdev->dev, "cannot register SPI master\n");
4423558fe90SMaxime Ripard 		goto err_pm_disable;
4433558fe90SMaxime Ripard 	}
4443558fe90SMaxime Ripard 
4453558fe90SMaxime Ripard 	return 0;
4463558fe90SMaxime Ripard 
4473558fe90SMaxime Ripard err_pm_disable:
4483558fe90SMaxime Ripard 	pm_runtime_disable(&pdev->dev);
4493558fe90SMaxime Ripard 	sun6i_spi_runtime_suspend(&pdev->dev);
4503558fe90SMaxime Ripard err_free_master:
4513558fe90SMaxime Ripard 	spi_master_put(master);
4523558fe90SMaxime Ripard 	return ret;
4533558fe90SMaxime Ripard }
4543558fe90SMaxime Ripard 
4553558fe90SMaxime Ripard static int sun6i_spi_remove(struct platform_device *pdev)
4563558fe90SMaxime Ripard {
4573558fe90SMaxime Ripard 	pm_runtime_disable(&pdev->dev);
4583558fe90SMaxime Ripard 
4593558fe90SMaxime Ripard 	return 0;
4603558fe90SMaxime Ripard }
4613558fe90SMaxime Ripard 
4623558fe90SMaxime Ripard static const struct of_device_id sun6i_spi_match[] = {
4633558fe90SMaxime Ripard 	{ .compatible = "allwinner,sun6i-a31-spi", },
4643558fe90SMaxime Ripard 	{}
4653558fe90SMaxime Ripard };
4663558fe90SMaxime Ripard MODULE_DEVICE_TABLE(of, sun6i_spi_match);
4673558fe90SMaxime Ripard 
4683558fe90SMaxime Ripard static const struct dev_pm_ops sun6i_spi_pm_ops = {
4693558fe90SMaxime Ripard 	.runtime_resume		= sun6i_spi_runtime_resume,
4703558fe90SMaxime Ripard 	.runtime_suspend	= sun6i_spi_runtime_suspend,
4713558fe90SMaxime Ripard };
4723558fe90SMaxime Ripard 
4733558fe90SMaxime Ripard static struct platform_driver sun6i_spi_driver = {
4743558fe90SMaxime Ripard 	.probe	= sun6i_spi_probe,
4753558fe90SMaxime Ripard 	.remove	= sun6i_spi_remove,
4763558fe90SMaxime Ripard 	.driver	= {
4773558fe90SMaxime Ripard 		.name		= "sun6i-spi",
4783558fe90SMaxime Ripard 		.of_match_table	= sun6i_spi_match,
4793558fe90SMaxime Ripard 		.pm		= &sun6i_spi_pm_ops,
4803558fe90SMaxime Ripard 	},
4813558fe90SMaxime Ripard };
4823558fe90SMaxime Ripard module_platform_driver(sun6i_spi_driver);
4833558fe90SMaxime Ripard 
4843558fe90SMaxime Ripard MODULE_AUTHOR("Pan Nan <pannan@allwinnertech.com>");
4853558fe90SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
4863558fe90SMaxime Ripard MODULE_DESCRIPTION("Allwinner A31 SPI controller driver");
4873558fe90SMaxime Ripard MODULE_LICENSE("GPL");
488