164e36824Saddy ke /* 264e36824Saddy ke * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd 35dcc44edSAddy Ke * Author: Addy Ke <addy.ke@rock-chips.com> 464e36824Saddy ke * 564e36824Saddy ke * This program is free software; you can redistribute it and/or modify it 664e36824Saddy ke * under the terms and conditions of the GNU General Public License, 764e36824Saddy ke * version 2, as published by the Free Software Foundation. 864e36824Saddy ke * 964e36824Saddy ke * This program is distributed in the hope it will be useful, but WITHOUT 1064e36824Saddy ke * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1164e36824Saddy ke * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 1264e36824Saddy ke * more details. 1364e36824Saddy ke * 1464e36824Saddy ke */ 1564e36824Saddy ke 1664e36824Saddy ke #include <linux/clk.h> 1764e36824Saddy ke #include <linux/dmaengine.h> 18ec5c5d8aSShawn Lin #include <linux/module.h> 19ec5c5d8aSShawn Lin #include <linux/of.h> 20ec5c5d8aSShawn Lin #include <linux/platform_device.h> 21ec5c5d8aSShawn Lin #include <linux/spi/spi.h> 22ec5c5d8aSShawn Lin #include <linux/pm_runtime.h> 23ec5c5d8aSShawn Lin #include <linux/scatterlist.h> 2464e36824Saddy ke 2564e36824Saddy ke #define DRIVER_NAME "rockchip-spi" 2664e36824Saddy ke 2764e36824Saddy ke /* SPI register offsets */ 2864e36824Saddy ke #define ROCKCHIP_SPI_CTRLR0 0x0000 2964e36824Saddy ke #define ROCKCHIP_SPI_CTRLR1 0x0004 3064e36824Saddy ke #define ROCKCHIP_SPI_SSIENR 0x0008 3164e36824Saddy ke #define ROCKCHIP_SPI_SER 0x000c 3264e36824Saddy ke #define ROCKCHIP_SPI_BAUDR 0x0010 3364e36824Saddy ke #define ROCKCHIP_SPI_TXFTLR 0x0014 3464e36824Saddy ke #define ROCKCHIP_SPI_RXFTLR 0x0018 3564e36824Saddy ke #define ROCKCHIP_SPI_TXFLR 0x001c 3664e36824Saddy ke #define ROCKCHIP_SPI_RXFLR 0x0020 3764e36824Saddy ke #define ROCKCHIP_SPI_SR 0x0024 3864e36824Saddy ke #define ROCKCHIP_SPI_IPR 0x0028 3964e36824Saddy ke #define ROCKCHIP_SPI_IMR 0x002c 4064e36824Saddy ke #define ROCKCHIP_SPI_ISR 0x0030 4164e36824Saddy ke #define ROCKCHIP_SPI_RISR 0x0034 4264e36824Saddy ke #define ROCKCHIP_SPI_ICR 0x0038 4364e36824Saddy ke #define ROCKCHIP_SPI_DMACR 0x003c 4464e36824Saddy ke #define ROCKCHIP_SPI_DMATDLR 0x0040 4564e36824Saddy ke #define ROCKCHIP_SPI_DMARDLR 0x0044 4664e36824Saddy ke #define ROCKCHIP_SPI_TXDR 0x0400 4764e36824Saddy ke #define ROCKCHIP_SPI_RXDR 0x0800 4864e36824Saddy ke 4964e36824Saddy ke /* Bit fields in CTRLR0 */ 5064e36824Saddy ke #define CR0_DFS_OFFSET 0 5164e36824Saddy ke 5264e36824Saddy ke #define CR0_CFS_OFFSET 2 5364e36824Saddy ke 5464e36824Saddy ke #define CR0_SCPH_OFFSET 6 5564e36824Saddy ke 5664e36824Saddy ke #define CR0_SCPOL_OFFSET 7 5764e36824Saddy ke 5864e36824Saddy ke #define CR0_CSM_OFFSET 8 5964e36824Saddy ke #define CR0_CSM_KEEP 0x0 6064e36824Saddy ke /* ss_n be high for half sclk_out cycles */ 6164e36824Saddy ke #define CR0_CSM_HALF 0X1 6264e36824Saddy ke /* ss_n be high for one sclk_out cycle */ 6364e36824Saddy ke #define CR0_CSM_ONE 0x2 6464e36824Saddy ke 6564e36824Saddy ke /* ss_n to sclk_out delay */ 6664e36824Saddy ke #define CR0_SSD_OFFSET 10 6764e36824Saddy ke /* 6864e36824Saddy ke * The period between ss_n active and 6964e36824Saddy ke * sclk_out active is half sclk_out cycles 7064e36824Saddy ke */ 7164e36824Saddy ke #define CR0_SSD_HALF 0x0 7264e36824Saddy ke /* 7364e36824Saddy ke * The period between ss_n active and 7464e36824Saddy ke * sclk_out active is one sclk_out cycle 7564e36824Saddy ke */ 7664e36824Saddy ke #define CR0_SSD_ONE 0x1 7764e36824Saddy ke 7864e36824Saddy ke #define CR0_EM_OFFSET 11 7964e36824Saddy ke #define CR0_EM_LITTLE 0x0 8064e36824Saddy ke #define CR0_EM_BIG 0x1 8164e36824Saddy ke 8264e36824Saddy ke #define CR0_FBM_OFFSET 12 8364e36824Saddy ke #define CR0_FBM_MSB 0x0 8464e36824Saddy ke #define CR0_FBM_LSB 0x1 8564e36824Saddy ke 8664e36824Saddy ke #define CR0_BHT_OFFSET 13 8764e36824Saddy ke #define CR0_BHT_16BIT 0x0 8864e36824Saddy ke #define CR0_BHT_8BIT 0x1 8964e36824Saddy ke 9064e36824Saddy ke #define CR0_RSD_OFFSET 14 9164e36824Saddy ke 9264e36824Saddy ke #define CR0_FRF_OFFSET 16 9364e36824Saddy ke #define CR0_FRF_SPI 0x0 9464e36824Saddy ke #define CR0_FRF_SSP 0x1 9564e36824Saddy ke #define CR0_FRF_MICROWIRE 0x2 9664e36824Saddy ke 9764e36824Saddy ke #define CR0_XFM_OFFSET 18 9864e36824Saddy ke #define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET) 9964e36824Saddy ke #define CR0_XFM_TR 0x0 10064e36824Saddy ke #define CR0_XFM_TO 0x1 10164e36824Saddy ke #define CR0_XFM_RO 0x2 10264e36824Saddy ke 10364e36824Saddy ke #define CR0_OPM_OFFSET 20 10464e36824Saddy ke #define CR0_OPM_MASTER 0x0 10564e36824Saddy ke #define CR0_OPM_SLAVE 0x1 10664e36824Saddy ke 10764e36824Saddy ke #define CR0_MTM_OFFSET 0x21 10864e36824Saddy ke 10964e36824Saddy ke /* Bit fields in SER, 2bit */ 11064e36824Saddy ke #define SER_MASK 0x3 11164e36824Saddy ke 11264e36824Saddy ke /* Bit fields in SR, 5bit */ 11364e36824Saddy ke #define SR_MASK 0x1f 11464e36824Saddy ke #define SR_BUSY (1 << 0) 11564e36824Saddy ke #define SR_TF_FULL (1 << 1) 11664e36824Saddy ke #define SR_TF_EMPTY (1 << 2) 11764e36824Saddy ke #define SR_RF_EMPTY (1 << 3) 11864e36824Saddy ke #define SR_RF_FULL (1 << 4) 11964e36824Saddy ke 12064e36824Saddy ke /* Bit fields in ISR, IMR, ISR, RISR, 5bit */ 12164e36824Saddy ke #define INT_MASK 0x1f 12264e36824Saddy ke #define INT_TF_EMPTY (1 << 0) 12364e36824Saddy ke #define INT_TF_OVERFLOW (1 << 1) 12464e36824Saddy ke #define INT_RF_UNDERFLOW (1 << 2) 12564e36824Saddy ke #define INT_RF_OVERFLOW (1 << 3) 12664e36824Saddy ke #define INT_RF_FULL (1 << 4) 12764e36824Saddy ke 12864e36824Saddy ke /* Bit fields in ICR, 4bit */ 12964e36824Saddy ke #define ICR_MASK 0x0f 13064e36824Saddy ke #define ICR_ALL (1 << 0) 13164e36824Saddy ke #define ICR_RF_UNDERFLOW (1 << 1) 13264e36824Saddy ke #define ICR_RF_OVERFLOW (1 << 2) 13364e36824Saddy ke #define ICR_TF_OVERFLOW (1 << 3) 13464e36824Saddy ke 13564e36824Saddy ke /* Bit fields in DMACR */ 13664e36824Saddy ke #define RF_DMA_EN (1 << 0) 13764e36824Saddy ke #define TF_DMA_EN (1 << 1) 13864e36824Saddy ke 13964e36824Saddy ke #define RXBUSY (1 << 0) 14064e36824Saddy ke #define TXBUSY (1 << 1) 14164e36824Saddy ke 142f9cfd522SAddy Ke /* sclk_out: spi master internal logic in rk3x can support 50Mhz */ 143f9cfd522SAddy Ke #define MAX_SCLK_OUT 50000000 144f9cfd522SAddy Ke 14564e36824Saddy ke enum rockchip_ssi_type { 14664e36824Saddy ke SSI_MOTO_SPI = 0, 14764e36824Saddy ke SSI_TI_SSP, 14864e36824Saddy ke SSI_NS_MICROWIRE, 14964e36824Saddy ke }; 15064e36824Saddy ke 15164e36824Saddy ke struct rockchip_spi_dma_data { 15264e36824Saddy ke struct dma_chan *ch; 15364e36824Saddy ke enum dma_transfer_direction direction; 15464e36824Saddy ke dma_addr_t addr; 15564e36824Saddy ke }; 15664e36824Saddy ke 15764e36824Saddy ke struct rockchip_spi { 15864e36824Saddy ke struct device *dev; 15964e36824Saddy ke struct spi_master *master; 16064e36824Saddy ke 16164e36824Saddy ke struct clk *spiclk; 16264e36824Saddy ke struct clk *apb_pclk; 16364e36824Saddy ke 16464e36824Saddy ke void __iomem *regs; 16564e36824Saddy ke /*depth of the FIFO buffer */ 16664e36824Saddy ke u32 fifo_len; 16764e36824Saddy ke /* max bus freq supported */ 16864e36824Saddy ke u32 max_freq; 16964e36824Saddy ke /* supported slave numbers */ 17064e36824Saddy ke enum rockchip_ssi_type type; 17164e36824Saddy ke 17264e36824Saddy ke u16 mode; 17364e36824Saddy ke u8 tmode; 17464e36824Saddy ke u8 bpw; 17564e36824Saddy ke u8 n_bytes; 176108b5c8bSShawn Lin u32 rsd_nsecs; 17764e36824Saddy ke unsigned len; 17864e36824Saddy ke u32 speed; 17964e36824Saddy ke 18064e36824Saddy ke const void *tx; 18164e36824Saddy ke const void *tx_end; 18264e36824Saddy ke void *rx; 18364e36824Saddy ke void *rx_end; 18464e36824Saddy ke 18564e36824Saddy ke u32 state; 1865dcc44edSAddy Ke /* protect state */ 18764e36824Saddy ke spinlock_t lock; 18864e36824Saddy ke 18964e36824Saddy ke u32 use_dma; 19064e36824Saddy ke struct sg_table tx_sg; 19164e36824Saddy ke struct sg_table rx_sg; 19264e36824Saddy ke struct rockchip_spi_dma_data dma_rx; 19364e36824Saddy ke struct rockchip_spi_dma_data dma_tx; 19480abf888SAddy Ke struct dma_slave_caps dma_caps; 19564e36824Saddy ke }; 19664e36824Saddy ke 19764e36824Saddy ke static inline void spi_enable_chip(struct rockchip_spi *rs, int enable) 19864e36824Saddy ke { 19964e36824Saddy ke writel_relaxed((enable ? 1 : 0), rs->regs + ROCKCHIP_SPI_SSIENR); 20064e36824Saddy ke } 20164e36824Saddy ke 20264e36824Saddy ke static inline void spi_set_clk(struct rockchip_spi *rs, u16 div) 20364e36824Saddy ke { 20464e36824Saddy ke writel_relaxed(div, rs->regs + ROCKCHIP_SPI_BAUDR); 20564e36824Saddy ke } 20664e36824Saddy ke 20764e36824Saddy ke static inline void flush_fifo(struct rockchip_spi *rs) 20864e36824Saddy ke { 20964e36824Saddy ke while (readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR)) 21064e36824Saddy ke readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR); 21164e36824Saddy ke } 21264e36824Saddy ke 2132df08e78SAddy Ke static inline void wait_for_idle(struct rockchip_spi *rs) 2142df08e78SAddy Ke { 2152df08e78SAddy Ke unsigned long timeout = jiffies + msecs_to_jiffies(5); 2162df08e78SAddy Ke 2172df08e78SAddy Ke do { 2182df08e78SAddy Ke if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)) 2192df08e78SAddy Ke return; 22064bc0110SDoug Anderson } while (!time_after(jiffies, timeout)); 2212df08e78SAddy Ke 2222df08e78SAddy Ke dev_warn(rs->dev, "spi controller is in busy state!\n"); 2232df08e78SAddy Ke } 2242df08e78SAddy Ke 22564e36824Saddy ke static u32 get_fifo_len(struct rockchip_spi *rs) 22664e36824Saddy ke { 22764e36824Saddy ke u32 fifo; 22864e36824Saddy ke 22964e36824Saddy ke for (fifo = 2; fifo < 32; fifo++) { 23064e36824Saddy ke writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR); 23164e36824Saddy ke if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR)) 23264e36824Saddy ke break; 23364e36824Saddy ke } 23464e36824Saddy ke 23564e36824Saddy ke writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR); 23664e36824Saddy ke 23764e36824Saddy ke return (fifo == 31) ? 0 : fifo; 23864e36824Saddy ke } 23964e36824Saddy ke 24064e36824Saddy ke static inline u32 tx_max(struct rockchip_spi *rs) 24164e36824Saddy ke { 24264e36824Saddy ke u32 tx_left, tx_room; 24364e36824Saddy ke 24464e36824Saddy ke tx_left = (rs->tx_end - rs->tx) / rs->n_bytes; 24564e36824Saddy ke tx_room = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR); 24664e36824Saddy ke 24764e36824Saddy ke return min(tx_left, tx_room); 24864e36824Saddy ke } 24964e36824Saddy ke 25064e36824Saddy ke static inline u32 rx_max(struct rockchip_spi *rs) 25164e36824Saddy ke { 25264e36824Saddy ke u32 rx_left = (rs->rx_end - rs->rx) / rs->n_bytes; 25364e36824Saddy ke u32 rx_room = (u32)readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR); 25464e36824Saddy ke 25564e36824Saddy ke return min(rx_left, rx_room); 25664e36824Saddy ke } 25764e36824Saddy ke 25864e36824Saddy ke static void rockchip_spi_set_cs(struct spi_device *spi, bool enable) 25964e36824Saddy ke { 26064e36824Saddy ke u32 ser; 261b920cc31SHuibin Hong struct spi_master *master = spi->master; 262b920cc31SHuibin Hong struct rockchip_spi *rs = spi_master_get_devdata(master); 263b920cc31SHuibin Hong 264b920cc31SHuibin Hong pm_runtime_get_sync(rs->dev); 26564e36824Saddy ke 26664e36824Saddy ke ser = readl_relaxed(rs->regs + ROCKCHIP_SPI_SER) & SER_MASK; 26764e36824Saddy ke 26864e36824Saddy ke /* 26964e36824Saddy ke * drivers/spi/spi.c: 27064e36824Saddy ke * static void spi_set_cs(struct spi_device *spi, bool enable) 27164e36824Saddy ke * { 27264e36824Saddy ke * if (spi->mode & SPI_CS_HIGH) 27364e36824Saddy ke * enable = !enable; 27464e36824Saddy ke * 27564e36824Saddy ke * if (spi->cs_gpio >= 0) 27664e36824Saddy ke * gpio_set_value(spi->cs_gpio, !enable); 27764e36824Saddy ke * else if (spi->master->set_cs) 27864e36824Saddy ke * spi->master->set_cs(spi, !enable); 27964e36824Saddy ke * } 28064e36824Saddy ke * 28164e36824Saddy ke * Note: enable(rockchip_spi_set_cs) = !enable(spi_set_cs) 28264e36824Saddy ke */ 28364e36824Saddy ke if (!enable) 28464e36824Saddy ke ser |= 1 << spi->chip_select; 28564e36824Saddy ke else 28664e36824Saddy ke ser &= ~(1 << spi->chip_select); 28764e36824Saddy ke 28864e36824Saddy ke writel_relaxed(ser, rs->regs + ROCKCHIP_SPI_SER); 289b920cc31SHuibin Hong 290b920cc31SHuibin Hong pm_runtime_put_sync(rs->dev); 29164e36824Saddy ke } 29264e36824Saddy ke 29364e36824Saddy ke static int rockchip_spi_prepare_message(struct spi_master *master, 29464e36824Saddy ke struct spi_message *msg) 29564e36824Saddy ke { 29664e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 29764e36824Saddy ke struct spi_device *spi = msg->spi; 29864e36824Saddy ke 29964e36824Saddy ke rs->mode = spi->mode; 30064e36824Saddy ke 30164e36824Saddy ke return 0; 30264e36824Saddy ke } 30364e36824Saddy ke 3042291793cSAndy Shevchenko static void rockchip_spi_handle_err(struct spi_master *master, 30564e36824Saddy ke struct spi_message *msg) 30664e36824Saddy ke { 30764e36824Saddy ke unsigned long flags; 30864e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 30964e36824Saddy ke 31064e36824Saddy ke spin_lock_irqsave(&rs->lock, flags); 31164e36824Saddy ke 3125dcc44edSAddy Ke /* 3135dcc44edSAddy Ke * For DMA mode, we need terminate DMA channel and flush 3145dcc44edSAddy Ke * fifo for the next transfer if DMA thansfer timeout. 3152291793cSAndy Shevchenko * handle_err() was called by core if transfer failed. 3162291793cSAndy Shevchenko * Maybe it is reasonable for error handling here. 3175dcc44edSAddy Ke */ 31864e36824Saddy ke if (rs->use_dma) { 31964e36824Saddy ke if (rs->state & RXBUSY) { 320557b7ea3SShawn Lin dmaengine_terminate_async(rs->dma_rx.ch); 32164e36824Saddy ke flush_fifo(rs); 32264e36824Saddy ke } 32364e36824Saddy ke 32464e36824Saddy ke if (rs->state & TXBUSY) 325557b7ea3SShawn Lin dmaengine_terminate_async(rs->dma_tx.ch); 32664e36824Saddy ke } 32764e36824Saddy ke 32864e36824Saddy ke spin_unlock_irqrestore(&rs->lock, flags); 3292291793cSAndy Shevchenko } 3302291793cSAndy Shevchenko 3312291793cSAndy Shevchenko static int rockchip_spi_unprepare_message(struct spi_master *master, 3322291793cSAndy Shevchenko struct spi_message *msg) 3332291793cSAndy Shevchenko { 3342291793cSAndy Shevchenko struct rockchip_spi *rs = spi_master_get_devdata(master); 33564e36824Saddy ke 336c28be31bSAddy Ke spi_enable_chip(rs, 0); 337c28be31bSAddy Ke 33864e36824Saddy ke return 0; 33964e36824Saddy ke } 34064e36824Saddy ke 34164e36824Saddy ke static void rockchip_spi_pio_writer(struct rockchip_spi *rs) 34264e36824Saddy ke { 34364e36824Saddy ke u32 max = tx_max(rs); 34464e36824Saddy ke u32 txw = 0; 34564e36824Saddy ke 34664e36824Saddy ke while (max--) { 34764e36824Saddy ke if (rs->n_bytes == 1) 34864e36824Saddy ke txw = *(u8 *)(rs->tx); 34964e36824Saddy ke else 35064e36824Saddy ke txw = *(u16 *)(rs->tx); 35164e36824Saddy ke 35264e36824Saddy ke writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR); 35364e36824Saddy ke rs->tx += rs->n_bytes; 35464e36824Saddy ke } 35564e36824Saddy ke } 35664e36824Saddy ke 35764e36824Saddy ke static void rockchip_spi_pio_reader(struct rockchip_spi *rs) 35864e36824Saddy ke { 35964e36824Saddy ke u32 max = rx_max(rs); 36064e36824Saddy ke u32 rxw; 36164e36824Saddy ke 36264e36824Saddy ke while (max--) { 36364e36824Saddy ke rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR); 36464e36824Saddy ke if (rs->n_bytes == 1) 36564e36824Saddy ke *(u8 *)(rs->rx) = (u8)rxw; 36664e36824Saddy ke else 36764e36824Saddy ke *(u16 *)(rs->rx) = (u16)rxw; 36864e36824Saddy ke rs->rx += rs->n_bytes; 3695dcc44edSAddy Ke } 37064e36824Saddy ke } 37164e36824Saddy ke 37264e36824Saddy ke static int rockchip_spi_pio_transfer(struct rockchip_spi *rs) 37364e36824Saddy ke { 37464e36824Saddy ke int remain = 0; 37564e36824Saddy ke 37664e36824Saddy ke do { 37764e36824Saddy ke if (rs->tx) { 37864e36824Saddy ke remain = rs->tx_end - rs->tx; 37964e36824Saddy ke rockchip_spi_pio_writer(rs); 38064e36824Saddy ke } 38164e36824Saddy ke 38264e36824Saddy ke if (rs->rx) { 38364e36824Saddy ke remain = rs->rx_end - rs->rx; 38464e36824Saddy ke rockchip_spi_pio_reader(rs); 38564e36824Saddy ke } 38664e36824Saddy ke 38764e36824Saddy ke cpu_relax(); 38864e36824Saddy ke } while (remain); 38964e36824Saddy ke 3902df08e78SAddy Ke /* If tx, wait until the FIFO data completely. */ 3912df08e78SAddy Ke if (rs->tx) 3922df08e78SAddy Ke wait_for_idle(rs); 3932df08e78SAddy Ke 394c28be31bSAddy Ke spi_enable_chip(rs, 0); 395c28be31bSAddy Ke 39664e36824Saddy ke return 0; 39764e36824Saddy ke } 39864e36824Saddy ke 39964e36824Saddy ke static void rockchip_spi_dma_rxcb(void *data) 40064e36824Saddy ke { 40164e36824Saddy ke unsigned long flags; 40264e36824Saddy ke struct rockchip_spi *rs = data; 40364e36824Saddy ke 40464e36824Saddy ke spin_lock_irqsave(&rs->lock, flags); 40564e36824Saddy ke 40664e36824Saddy ke rs->state &= ~RXBUSY; 407c28be31bSAddy Ke if (!(rs->state & TXBUSY)) { 408c28be31bSAddy Ke spi_enable_chip(rs, 0); 40964e36824Saddy ke spi_finalize_current_transfer(rs->master); 410c28be31bSAddy Ke } 41164e36824Saddy ke 41264e36824Saddy ke spin_unlock_irqrestore(&rs->lock, flags); 41364e36824Saddy ke } 41464e36824Saddy ke 41564e36824Saddy ke static void rockchip_spi_dma_txcb(void *data) 41664e36824Saddy ke { 41764e36824Saddy ke unsigned long flags; 41864e36824Saddy ke struct rockchip_spi *rs = data; 41964e36824Saddy ke 4202df08e78SAddy Ke /* Wait until the FIFO data completely. */ 4212df08e78SAddy Ke wait_for_idle(rs); 4222df08e78SAddy Ke 42364e36824Saddy ke spin_lock_irqsave(&rs->lock, flags); 42464e36824Saddy ke 42564e36824Saddy ke rs->state &= ~TXBUSY; 4262c2bc748SAddy Ke if (!(rs->state & RXBUSY)) { 4272c2bc748SAddy Ke spi_enable_chip(rs, 0); 42864e36824Saddy ke spi_finalize_current_transfer(rs->master); 4292c2bc748SAddy Ke } 43064e36824Saddy ke 43164e36824Saddy ke spin_unlock_irqrestore(&rs->lock, flags); 43264e36824Saddy ke } 43364e36824Saddy ke 434ea984911SShawn Lin static int rockchip_spi_prepare_dma(struct rockchip_spi *rs) 43564e36824Saddy ke { 43664e36824Saddy ke unsigned long flags; 43764e36824Saddy ke struct dma_slave_config rxconf, txconf; 43864e36824Saddy ke struct dma_async_tx_descriptor *rxdesc, *txdesc; 43964e36824Saddy ke 44064e36824Saddy ke spin_lock_irqsave(&rs->lock, flags); 44164e36824Saddy ke rs->state &= ~RXBUSY; 44264e36824Saddy ke rs->state &= ~TXBUSY; 44364e36824Saddy ke spin_unlock_irqrestore(&rs->lock, flags); 44464e36824Saddy ke 44597cf5669SArnd Bergmann rxdesc = NULL; 44664e36824Saddy ke if (rs->rx) { 44764e36824Saddy ke rxconf.direction = rs->dma_rx.direction; 44864e36824Saddy ke rxconf.src_addr = rs->dma_rx.addr; 44964e36824Saddy ke rxconf.src_addr_width = rs->n_bytes; 45080abf888SAddy Ke if (rs->dma_caps.max_burst > 4) 45180abf888SAddy Ke rxconf.src_maxburst = 4; 45280abf888SAddy Ke else 45380abf888SAddy Ke rxconf.src_maxburst = 1; 45464e36824Saddy ke dmaengine_slave_config(rs->dma_rx.ch, &rxconf); 45564e36824Saddy ke 4565dcc44edSAddy Ke rxdesc = dmaengine_prep_slave_sg( 4575dcc44edSAddy Ke rs->dma_rx.ch, 45864e36824Saddy ke rs->rx_sg.sgl, rs->rx_sg.nents, 45964e36824Saddy ke rs->dma_rx.direction, DMA_PREP_INTERRUPT); 460ea984911SShawn Lin if (!rxdesc) 461ea984911SShawn Lin return -EINVAL; 46264e36824Saddy ke 46364e36824Saddy ke rxdesc->callback = rockchip_spi_dma_rxcb; 46464e36824Saddy ke rxdesc->callback_param = rs; 46564e36824Saddy ke } 46664e36824Saddy ke 46797cf5669SArnd Bergmann txdesc = NULL; 46864e36824Saddy ke if (rs->tx) { 46964e36824Saddy ke txconf.direction = rs->dma_tx.direction; 47064e36824Saddy ke txconf.dst_addr = rs->dma_tx.addr; 47164e36824Saddy ke txconf.dst_addr_width = rs->n_bytes; 47280abf888SAddy Ke if (rs->dma_caps.max_burst > 4) 47380abf888SAddy Ke txconf.dst_maxburst = 4; 47480abf888SAddy Ke else 47580abf888SAddy Ke txconf.dst_maxburst = 1; 47664e36824Saddy ke dmaengine_slave_config(rs->dma_tx.ch, &txconf); 47764e36824Saddy ke 4785dcc44edSAddy Ke txdesc = dmaengine_prep_slave_sg( 4795dcc44edSAddy Ke rs->dma_tx.ch, 48064e36824Saddy ke rs->tx_sg.sgl, rs->tx_sg.nents, 48164e36824Saddy ke rs->dma_tx.direction, DMA_PREP_INTERRUPT); 482ea984911SShawn Lin if (!txdesc) { 483ea984911SShawn Lin if (rxdesc) 484ea984911SShawn Lin dmaengine_terminate_sync(rs->dma_rx.ch); 485ea984911SShawn Lin return -EINVAL; 486ea984911SShawn Lin } 48764e36824Saddy ke 48864e36824Saddy ke txdesc->callback = rockchip_spi_dma_txcb; 48964e36824Saddy ke txdesc->callback_param = rs; 49064e36824Saddy ke } 49164e36824Saddy ke 49264e36824Saddy ke /* rx must be started before tx due to spi instinct */ 49397cf5669SArnd Bergmann if (rxdesc) { 49464e36824Saddy ke spin_lock_irqsave(&rs->lock, flags); 49564e36824Saddy ke rs->state |= RXBUSY; 49664e36824Saddy ke spin_unlock_irqrestore(&rs->lock, flags); 49764e36824Saddy ke dmaengine_submit(rxdesc); 49864e36824Saddy ke dma_async_issue_pending(rs->dma_rx.ch); 49964e36824Saddy ke } 50064e36824Saddy ke 50197cf5669SArnd Bergmann if (txdesc) { 50264e36824Saddy ke spin_lock_irqsave(&rs->lock, flags); 50364e36824Saddy ke rs->state |= TXBUSY; 50464e36824Saddy ke spin_unlock_irqrestore(&rs->lock, flags); 50564e36824Saddy ke dmaengine_submit(txdesc); 50664e36824Saddy ke dma_async_issue_pending(rs->dma_tx.ch); 50764e36824Saddy ke } 508ea984911SShawn Lin 509ea984911SShawn Lin return 0; 51064e36824Saddy ke } 51164e36824Saddy ke 51264e36824Saddy ke static void rockchip_spi_config(struct rockchip_spi *rs) 51364e36824Saddy ke { 51464e36824Saddy ke u32 div = 0; 51564e36824Saddy ke u32 dmacr = 0; 51676b17e6eSJulius Werner int rsd = 0; 51764e36824Saddy ke 51864e36824Saddy ke u32 cr0 = (CR0_BHT_8BIT << CR0_BHT_OFFSET) 5190277e01aSAlexander Kochetkov | (CR0_SSD_ONE << CR0_SSD_OFFSET) 5200277e01aSAlexander Kochetkov | (CR0_EM_BIG << CR0_EM_OFFSET); 52164e36824Saddy ke 52264e36824Saddy ke cr0 |= (rs->n_bytes << CR0_DFS_OFFSET); 52364e36824Saddy ke cr0 |= ((rs->mode & 0x3) << CR0_SCPH_OFFSET); 52464e36824Saddy ke cr0 |= (rs->tmode << CR0_XFM_OFFSET); 52564e36824Saddy ke cr0 |= (rs->type << CR0_FRF_OFFSET); 52664e36824Saddy ke 52764e36824Saddy ke if (rs->use_dma) { 52864e36824Saddy ke if (rs->tx) 52964e36824Saddy ke dmacr |= TF_DMA_EN; 53064e36824Saddy ke if (rs->rx) 53164e36824Saddy ke dmacr |= RF_DMA_EN; 53264e36824Saddy ke } 53364e36824Saddy ke 534f9cfd522SAddy Ke if (WARN_ON(rs->speed > MAX_SCLK_OUT)) 535f9cfd522SAddy Ke rs->speed = MAX_SCLK_OUT; 536f9cfd522SAddy Ke 537bb51537aSGeert Uytterhoeven /* the minimum divisor is 2 */ 538f9cfd522SAddy Ke if (rs->max_freq < 2 * rs->speed) { 539f9cfd522SAddy Ke clk_set_rate(rs->spiclk, 2 * rs->speed); 540f9cfd522SAddy Ke rs->max_freq = clk_get_rate(rs->spiclk); 541f9cfd522SAddy Ke } 542f9cfd522SAddy Ke 54364e36824Saddy ke /* div doesn't support odd number */ 544754ec43cSJulius Werner div = DIV_ROUND_UP(rs->max_freq, rs->speed); 54564e36824Saddy ke div = (div + 1) & 0xfffe; 54664e36824Saddy ke 54776b17e6eSJulius Werner /* Rx sample delay is expressed in parent clock cycles (max 3) */ 54876b17e6eSJulius Werner rsd = DIV_ROUND_CLOSEST(rs->rsd_nsecs * (rs->max_freq >> 8), 54976b17e6eSJulius Werner 1000000000 >> 8); 55076b17e6eSJulius Werner if (!rsd && rs->rsd_nsecs) { 55176b17e6eSJulius Werner pr_warn_once("rockchip-spi: %u Hz are too slow to express %u ns delay\n", 55276b17e6eSJulius Werner rs->max_freq, rs->rsd_nsecs); 55376b17e6eSJulius Werner } else if (rsd > 3) { 55476b17e6eSJulius Werner rsd = 3; 55576b17e6eSJulius Werner pr_warn_once("rockchip-spi: %u Hz are too fast to express %u ns delay, clamping at %u ns\n", 55676b17e6eSJulius Werner rs->max_freq, rs->rsd_nsecs, 55776b17e6eSJulius Werner rsd * 1000000000U / rs->max_freq); 55876b17e6eSJulius Werner } 55976b17e6eSJulius Werner cr0 |= rsd << CR0_RSD_OFFSET; 56076b17e6eSJulius Werner 56164e36824Saddy ke writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0); 56264e36824Saddy ke 56364e36824Saddy ke writel_relaxed(rs->len - 1, rs->regs + ROCKCHIP_SPI_CTRLR1); 56464e36824Saddy ke writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_TXFTLR); 56564e36824Saddy ke writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR); 56664e36824Saddy ke 56764e36824Saddy ke writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMATDLR); 56864e36824Saddy ke writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR); 56964e36824Saddy ke writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR); 57064e36824Saddy ke 57164e36824Saddy ke spi_set_clk(rs, div); 57264e36824Saddy ke 5735dcc44edSAddy Ke dev_dbg(rs->dev, "cr0 0x%x, div %d\n", cr0, div); 57464e36824Saddy ke } 57564e36824Saddy ke 5765dcc44edSAddy Ke static int rockchip_spi_transfer_one( 5775dcc44edSAddy Ke struct spi_master *master, 57864e36824Saddy ke struct spi_device *spi, 57964e36824Saddy ke struct spi_transfer *xfer) 58064e36824Saddy ke { 581c28be31bSAddy Ke int ret = 1; 58264e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 58364e36824Saddy ke 58462946172SDoug Anderson WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) && 58562946172SDoug Anderson (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)); 58664e36824Saddy ke 58764e36824Saddy ke if (!xfer->tx_buf && !xfer->rx_buf) { 58864e36824Saddy ke dev_err(rs->dev, "No buffer for transfer\n"); 58964e36824Saddy ke return -EINVAL; 59064e36824Saddy ke } 59164e36824Saddy ke 59264e36824Saddy ke rs->speed = xfer->speed_hz; 59364e36824Saddy ke rs->bpw = xfer->bits_per_word; 59464e36824Saddy ke rs->n_bytes = rs->bpw >> 3; 59564e36824Saddy ke 59664e36824Saddy ke rs->tx = xfer->tx_buf; 59764e36824Saddy ke rs->tx_end = rs->tx + xfer->len; 59864e36824Saddy ke rs->rx = xfer->rx_buf; 59964e36824Saddy ke rs->rx_end = rs->rx + xfer->len; 60064e36824Saddy ke rs->len = xfer->len; 60164e36824Saddy ke 60264e36824Saddy ke rs->tx_sg = xfer->tx_sg; 60364e36824Saddy ke rs->rx_sg = xfer->rx_sg; 60464e36824Saddy ke 60564e36824Saddy ke if (rs->tx && rs->rx) 60664e36824Saddy ke rs->tmode = CR0_XFM_TR; 60764e36824Saddy ke else if (rs->tx) 60864e36824Saddy ke rs->tmode = CR0_XFM_TO; 60964e36824Saddy ke else if (rs->rx) 61064e36824Saddy ke rs->tmode = CR0_XFM_RO; 61164e36824Saddy ke 612a24e70c0SAddy Ke /* we need prepare dma before spi was enabled */ 613c28be31bSAddy Ke if (master->can_dma && master->can_dma(master, spi, xfer)) 61464e36824Saddy ke rs->use_dma = 1; 615c28be31bSAddy Ke else 61664e36824Saddy ke rs->use_dma = 0; 61764e36824Saddy ke 61864e36824Saddy ke rockchip_spi_config(rs); 61964e36824Saddy ke 620c28be31bSAddy Ke if (rs->use_dma) { 621c28be31bSAddy Ke if (rs->tmode == CR0_XFM_RO) { 622c28be31bSAddy Ke /* rx: dma must be prepared first */ 623ea984911SShawn Lin ret = rockchip_spi_prepare_dma(rs); 624c28be31bSAddy Ke spi_enable_chip(rs, 1); 625c28be31bSAddy Ke } else { 626c28be31bSAddy Ke /* tx or tr: spi must be enabled first */ 627c28be31bSAddy Ke spi_enable_chip(rs, 1); 628ea984911SShawn Lin ret = rockchip_spi_prepare_dma(rs); 629c28be31bSAddy Ke } 630c28be31bSAddy Ke } else { 631c28be31bSAddy Ke spi_enable_chip(rs, 1); 63264e36824Saddy ke ret = rockchip_spi_pio_transfer(rs); 633c28be31bSAddy Ke } 63464e36824Saddy ke 63564e36824Saddy ke return ret; 63664e36824Saddy ke } 63764e36824Saddy ke 63864e36824Saddy ke static bool rockchip_spi_can_dma(struct spi_master *master, 63964e36824Saddy ke struct spi_device *spi, 64064e36824Saddy ke struct spi_transfer *xfer) 64164e36824Saddy ke { 64264e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 64364e36824Saddy ke 64464e36824Saddy ke return (xfer->len > rs->fifo_len); 64564e36824Saddy ke } 64664e36824Saddy ke 64764e36824Saddy ke static int rockchip_spi_probe(struct platform_device *pdev) 64864e36824Saddy ke { 64964e36824Saddy ke int ret = 0; 65064e36824Saddy ke struct rockchip_spi *rs; 65164e36824Saddy ke struct spi_master *master; 65264e36824Saddy ke struct resource *mem; 65376b17e6eSJulius Werner u32 rsd_nsecs; 65464e36824Saddy ke 65564e36824Saddy ke master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi)); 6565dcc44edSAddy Ke if (!master) 65764e36824Saddy ke return -ENOMEM; 6585dcc44edSAddy Ke 65964e36824Saddy ke platform_set_drvdata(pdev, master); 66064e36824Saddy ke 66164e36824Saddy ke rs = spi_master_get_devdata(master); 66264e36824Saddy ke 66364e36824Saddy ke /* Get basic io resource and map it */ 66464e36824Saddy ke mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 66564e36824Saddy ke rs->regs = devm_ioremap_resource(&pdev->dev, mem); 66664e36824Saddy ke if (IS_ERR(rs->regs)) { 66764e36824Saddy ke ret = PTR_ERR(rs->regs); 66864e36824Saddy ke goto err_ioremap_resource; 66964e36824Saddy ke } 67064e36824Saddy ke 67164e36824Saddy ke rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk"); 67264e36824Saddy ke if (IS_ERR(rs->apb_pclk)) { 67364e36824Saddy ke dev_err(&pdev->dev, "Failed to get apb_pclk\n"); 67464e36824Saddy ke ret = PTR_ERR(rs->apb_pclk); 67564e36824Saddy ke goto err_ioremap_resource; 67664e36824Saddy ke } 67764e36824Saddy ke 67864e36824Saddy ke rs->spiclk = devm_clk_get(&pdev->dev, "spiclk"); 67964e36824Saddy ke if (IS_ERR(rs->spiclk)) { 68064e36824Saddy ke dev_err(&pdev->dev, "Failed to get spi_pclk\n"); 68164e36824Saddy ke ret = PTR_ERR(rs->spiclk); 68264e36824Saddy ke goto err_ioremap_resource; 68364e36824Saddy ke } 68464e36824Saddy ke 68564e36824Saddy ke ret = clk_prepare_enable(rs->apb_pclk); 68664e36824Saddy ke if (ret) { 68764e36824Saddy ke dev_err(&pdev->dev, "Failed to enable apb_pclk\n"); 68864e36824Saddy ke goto err_ioremap_resource; 68964e36824Saddy ke } 69064e36824Saddy ke 69164e36824Saddy ke ret = clk_prepare_enable(rs->spiclk); 69264e36824Saddy ke if (ret) { 69364e36824Saddy ke dev_err(&pdev->dev, "Failed to enable spi_clk\n"); 69464e36824Saddy ke goto err_spiclk_enable; 69564e36824Saddy ke } 69664e36824Saddy ke 69764e36824Saddy ke spi_enable_chip(rs, 0); 69864e36824Saddy ke 69964e36824Saddy ke rs->type = SSI_MOTO_SPI; 70064e36824Saddy ke rs->master = master; 70164e36824Saddy ke rs->dev = &pdev->dev; 70264e36824Saddy ke rs->max_freq = clk_get_rate(rs->spiclk); 70364e36824Saddy ke 70476b17e6eSJulius Werner if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns", 70576b17e6eSJulius Werner &rsd_nsecs)) 70676b17e6eSJulius Werner rs->rsd_nsecs = rsd_nsecs; 70776b17e6eSJulius Werner 70864e36824Saddy ke rs->fifo_len = get_fifo_len(rs); 70964e36824Saddy ke if (!rs->fifo_len) { 71064e36824Saddy ke dev_err(&pdev->dev, "Failed to get fifo length\n"); 711db7e8d90SWei Yongjun ret = -EINVAL; 71264e36824Saddy ke goto err_get_fifo_len; 71364e36824Saddy ke } 71464e36824Saddy ke 71564e36824Saddy ke spin_lock_init(&rs->lock); 71664e36824Saddy ke 71764e36824Saddy ke pm_runtime_set_active(&pdev->dev); 71864e36824Saddy ke pm_runtime_enable(&pdev->dev); 71964e36824Saddy ke 72064e36824Saddy ke master->auto_runtime_pm = true; 72164e36824Saddy ke master->bus_num = pdev->id; 722ee780997SAddy Ke master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP; 72364e36824Saddy ke master->num_chipselect = 2; 72464e36824Saddy ke master->dev.of_node = pdev->dev.of_node; 72564e36824Saddy ke master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8); 72664e36824Saddy ke 72764e36824Saddy ke master->set_cs = rockchip_spi_set_cs; 72864e36824Saddy ke master->prepare_message = rockchip_spi_prepare_message; 72964e36824Saddy ke master->unprepare_message = rockchip_spi_unprepare_message; 73064e36824Saddy ke master->transfer_one = rockchip_spi_transfer_one; 7312291793cSAndy Shevchenko master->handle_err = rockchip_spi_handle_err; 73264e36824Saddy ke 733e4c0e06fSShawn Lin rs->dma_tx.ch = dma_request_chan(rs->dev, "tx"); 734e4c0e06fSShawn Lin if (IS_ERR(rs->dma_tx.ch)) { 73561cadcf4SShawn Lin /* Check tx to see if we need defer probing driver */ 73661cadcf4SShawn Lin if (PTR_ERR(rs->dma_tx.ch) == -EPROBE_DEFER) { 73761cadcf4SShawn Lin ret = -EPROBE_DEFER; 73861cadcf4SShawn Lin goto err_get_fifo_len; 73961cadcf4SShawn Lin } 74064e36824Saddy ke dev_warn(rs->dev, "Failed to request TX DMA channel\n"); 74164e36824Saddy ke rs->dma_tx.ch = NULL; 74264e36824Saddy ke } 743e4c0e06fSShawn Lin 744e4c0e06fSShawn Lin rs->dma_rx.ch = dma_request_chan(rs->dev, "rx"); 745e4c0e06fSShawn Lin if (IS_ERR(rs->dma_rx.ch)) { 746e4c0e06fSShawn Lin if (PTR_ERR(rs->dma_rx.ch) == -EPROBE_DEFER) { 747e4c0e06fSShawn Lin ret = -EPROBE_DEFER; 748*5de7ed0cSDan Carpenter goto err_free_dma_tx; 749e4c0e06fSShawn Lin } 75064e36824Saddy ke dev_warn(rs->dev, "Failed to request RX DMA channel\n"); 751e4c0e06fSShawn Lin rs->dma_rx.ch = NULL; 75264e36824Saddy ke } 75364e36824Saddy ke 75464e36824Saddy ke if (rs->dma_tx.ch && rs->dma_rx.ch) { 75580abf888SAddy Ke dma_get_slave_caps(rs->dma_rx.ch, &(rs->dma_caps)); 75664e36824Saddy ke rs->dma_tx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_TXDR); 75764e36824Saddy ke rs->dma_rx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_RXDR); 75864e36824Saddy ke rs->dma_tx.direction = DMA_MEM_TO_DEV; 7590ac7a490SAddy Ke rs->dma_rx.direction = DMA_DEV_TO_MEM; 76064e36824Saddy ke 76164e36824Saddy ke master->can_dma = rockchip_spi_can_dma; 76264e36824Saddy ke master->dma_tx = rs->dma_tx.ch; 76364e36824Saddy ke master->dma_rx = rs->dma_rx.ch; 76464e36824Saddy ke } 76564e36824Saddy ke 76664e36824Saddy ke ret = devm_spi_register_master(&pdev->dev, master); 76764e36824Saddy ke if (ret) { 76864e36824Saddy ke dev_err(&pdev->dev, "Failed to register master\n"); 76964e36824Saddy ke goto err_register_master; 77064e36824Saddy ke } 77164e36824Saddy ke 77264e36824Saddy ke return 0; 77364e36824Saddy ke 77464e36824Saddy ke err_register_master: 775b8659addSShawn Lin pm_runtime_disable(&pdev->dev); 77664e36824Saddy ke if (rs->dma_rx.ch) 77764e36824Saddy ke dma_release_channel(rs->dma_rx.ch); 778*5de7ed0cSDan Carpenter err_free_dma_tx: 779*5de7ed0cSDan Carpenter if (rs->dma_tx.ch) 780*5de7ed0cSDan Carpenter dma_release_channel(rs->dma_tx.ch); 78164e36824Saddy ke err_get_fifo_len: 78264e36824Saddy ke clk_disable_unprepare(rs->spiclk); 78364e36824Saddy ke err_spiclk_enable: 78464e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 78564e36824Saddy ke err_ioremap_resource: 78664e36824Saddy ke spi_master_put(master); 78764e36824Saddy ke 78864e36824Saddy ke return ret; 78964e36824Saddy ke } 79064e36824Saddy ke 79164e36824Saddy ke static int rockchip_spi_remove(struct platform_device *pdev) 79264e36824Saddy ke { 79364e36824Saddy ke struct spi_master *master = spi_master_get(platform_get_drvdata(pdev)); 79464e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 79564e36824Saddy ke 79664e36824Saddy ke pm_runtime_disable(&pdev->dev); 79764e36824Saddy ke 79864e36824Saddy ke clk_disable_unprepare(rs->spiclk); 79964e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 80064e36824Saddy ke 80164e36824Saddy ke if (rs->dma_tx.ch) 80264e36824Saddy ke dma_release_channel(rs->dma_tx.ch); 80364e36824Saddy ke if (rs->dma_rx.ch) 80464e36824Saddy ke dma_release_channel(rs->dma_rx.ch); 80564e36824Saddy ke 806844c9f47SShawn Lin spi_master_put(master); 807844c9f47SShawn Lin 80864e36824Saddy ke return 0; 80964e36824Saddy ke } 81064e36824Saddy ke 81164e36824Saddy ke #ifdef CONFIG_PM_SLEEP 81264e36824Saddy ke static int rockchip_spi_suspend(struct device *dev) 81364e36824Saddy ke { 81464e36824Saddy ke int ret = 0; 81564e36824Saddy ke struct spi_master *master = dev_get_drvdata(dev); 81664e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 81764e36824Saddy ke 81864e36824Saddy ke ret = spi_master_suspend(rs->master); 81964e36824Saddy ke if (ret) 82064e36824Saddy ke return ret; 82164e36824Saddy ke 82264e36824Saddy ke if (!pm_runtime_suspended(dev)) { 82364e36824Saddy ke clk_disable_unprepare(rs->spiclk); 82464e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 82564e36824Saddy ke } 82664e36824Saddy ke 82764e36824Saddy ke return ret; 82864e36824Saddy ke } 82964e36824Saddy ke 83064e36824Saddy ke static int rockchip_spi_resume(struct device *dev) 83164e36824Saddy ke { 83264e36824Saddy ke int ret = 0; 83364e36824Saddy ke struct spi_master *master = dev_get_drvdata(dev); 83464e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 83564e36824Saddy ke 83664e36824Saddy ke if (!pm_runtime_suspended(dev)) { 83764e36824Saddy ke ret = clk_prepare_enable(rs->apb_pclk); 83864e36824Saddy ke if (ret < 0) 83964e36824Saddy ke return ret; 84064e36824Saddy ke 84164e36824Saddy ke ret = clk_prepare_enable(rs->spiclk); 84264e36824Saddy ke if (ret < 0) { 84364e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 84464e36824Saddy ke return ret; 84564e36824Saddy ke } 84664e36824Saddy ke } 84764e36824Saddy ke 84864e36824Saddy ke ret = spi_master_resume(rs->master); 84964e36824Saddy ke if (ret < 0) { 85064e36824Saddy ke clk_disable_unprepare(rs->spiclk); 85164e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 85264e36824Saddy ke } 85364e36824Saddy ke 85464e36824Saddy ke return ret; 85564e36824Saddy ke } 85664e36824Saddy ke #endif /* CONFIG_PM_SLEEP */ 85764e36824Saddy ke 858ec833050SRafael J. Wysocki #ifdef CONFIG_PM 85964e36824Saddy ke static int rockchip_spi_runtime_suspend(struct device *dev) 86064e36824Saddy ke { 86164e36824Saddy ke struct spi_master *master = dev_get_drvdata(dev); 86264e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 86364e36824Saddy ke 86464e36824Saddy ke clk_disable_unprepare(rs->spiclk); 86564e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 86664e36824Saddy ke 86764e36824Saddy ke return 0; 86864e36824Saddy ke } 86964e36824Saddy ke 87064e36824Saddy ke static int rockchip_spi_runtime_resume(struct device *dev) 87164e36824Saddy ke { 87264e36824Saddy ke int ret; 87364e36824Saddy ke struct spi_master *master = dev_get_drvdata(dev); 87464e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 87564e36824Saddy ke 87664e36824Saddy ke ret = clk_prepare_enable(rs->apb_pclk); 87764e36824Saddy ke if (ret) 87864e36824Saddy ke return ret; 87964e36824Saddy ke 88064e36824Saddy ke ret = clk_prepare_enable(rs->spiclk); 88164e36824Saddy ke if (ret) 88264e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 88364e36824Saddy ke 88464e36824Saddy ke return ret; 88564e36824Saddy ke } 886ec833050SRafael J. Wysocki #endif /* CONFIG_PM */ 88764e36824Saddy ke 88864e36824Saddy ke static const struct dev_pm_ops rockchip_spi_pm = { 88964e36824Saddy ke SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume) 89064e36824Saddy ke SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend, 89164e36824Saddy ke rockchip_spi_runtime_resume, NULL) 89264e36824Saddy ke }; 89364e36824Saddy ke 89464e36824Saddy ke static const struct of_device_id rockchip_spi_dt_match[] = { 89564e36824Saddy ke { .compatible = "rockchip,rk3066-spi", }, 896b839b785SAddy Ke { .compatible = "rockchip,rk3188-spi", }, 897b839b785SAddy Ke { .compatible = "rockchip,rk3288-spi", }, 8989b7a5622SXu Jianqun { .compatible = "rockchip,rk3399-spi", }, 89964e36824Saddy ke { }, 90064e36824Saddy ke }; 90164e36824Saddy ke MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match); 90264e36824Saddy ke 90364e36824Saddy ke static struct platform_driver rockchip_spi_driver = { 90464e36824Saddy ke .driver = { 90564e36824Saddy ke .name = DRIVER_NAME, 90664e36824Saddy ke .pm = &rockchip_spi_pm, 90764e36824Saddy ke .of_match_table = of_match_ptr(rockchip_spi_dt_match), 90864e36824Saddy ke }, 90964e36824Saddy ke .probe = rockchip_spi_probe, 91064e36824Saddy ke .remove = rockchip_spi_remove, 91164e36824Saddy ke }; 91264e36824Saddy ke 91364e36824Saddy ke module_platform_driver(rockchip_spi_driver); 91464e36824Saddy ke 9155dcc44edSAddy Ke MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>"); 91664e36824Saddy ke MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver"); 91764e36824Saddy ke MODULE_LICENSE("GPL v2"); 918