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> 2023e291c2SBrian Norris #include <linux/pinctrl/consumer.h> 21ec5c5d8aSShawn Lin #include <linux/platform_device.h> 22ec5c5d8aSShawn Lin #include <linux/spi/spi.h> 23ec5c5d8aSShawn Lin #include <linux/pm_runtime.h> 24ec5c5d8aSShawn Lin #include <linux/scatterlist.h> 2564e36824Saddy ke 2664e36824Saddy ke #define DRIVER_NAME "rockchip-spi" 2764e36824Saddy ke 28aa099382SJeffy Chen #define ROCKCHIP_SPI_CLR_BITS(reg, bits) \ 29aa099382SJeffy Chen writel_relaxed(readl_relaxed(reg) & ~(bits), reg) 30aa099382SJeffy Chen #define ROCKCHIP_SPI_SET_BITS(reg, bits) \ 31aa099382SJeffy Chen writel_relaxed(readl_relaxed(reg) | (bits), reg) 32aa099382SJeffy Chen 3364e36824Saddy ke /* SPI register offsets */ 3464e36824Saddy ke #define ROCKCHIP_SPI_CTRLR0 0x0000 3564e36824Saddy ke #define ROCKCHIP_SPI_CTRLR1 0x0004 3664e36824Saddy ke #define ROCKCHIP_SPI_SSIENR 0x0008 3764e36824Saddy ke #define ROCKCHIP_SPI_SER 0x000c 3864e36824Saddy ke #define ROCKCHIP_SPI_BAUDR 0x0010 3964e36824Saddy ke #define ROCKCHIP_SPI_TXFTLR 0x0014 4064e36824Saddy ke #define ROCKCHIP_SPI_RXFTLR 0x0018 4164e36824Saddy ke #define ROCKCHIP_SPI_TXFLR 0x001c 4264e36824Saddy ke #define ROCKCHIP_SPI_RXFLR 0x0020 4364e36824Saddy ke #define ROCKCHIP_SPI_SR 0x0024 4464e36824Saddy ke #define ROCKCHIP_SPI_IPR 0x0028 4564e36824Saddy ke #define ROCKCHIP_SPI_IMR 0x002c 4664e36824Saddy ke #define ROCKCHIP_SPI_ISR 0x0030 4764e36824Saddy ke #define ROCKCHIP_SPI_RISR 0x0034 4864e36824Saddy ke #define ROCKCHIP_SPI_ICR 0x0038 4964e36824Saddy ke #define ROCKCHIP_SPI_DMACR 0x003c 5064e36824Saddy ke #define ROCKCHIP_SPI_DMATDLR 0x0040 5164e36824Saddy ke #define ROCKCHIP_SPI_DMARDLR 0x0044 5264e36824Saddy ke #define ROCKCHIP_SPI_TXDR 0x0400 5364e36824Saddy ke #define ROCKCHIP_SPI_RXDR 0x0800 5464e36824Saddy ke 5564e36824Saddy ke /* Bit fields in CTRLR0 */ 5664e36824Saddy ke #define CR0_DFS_OFFSET 0 5764e36824Saddy ke 5864e36824Saddy ke #define CR0_CFS_OFFSET 2 5964e36824Saddy ke 6064e36824Saddy ke #define CR0_SCPH_OFFSET 6 6164e36824Saddy ke 6264e36824Saddy ke #define CR0_SCPOL_OFFSET 7 6364e36824Saddy ke 6464e36824Saddy ke #define CR0_CSM_OFFSET 8 6564e36824Saddy ke #define CR0_CSM_KEEP 0x0 6664e36824Saddy ke /* ss_n be high for half sclk_out cycles */ 6764e36824Saddy ke #define CR0_CSM_HALF 0X1 6864e36824Saddy ke /* ss_n be high for one sclk_out cycle */ 6964e36824Saddy ke #define CR0_CSM_ONE 0x2 7064e36824Saddy ke 7164e36824Saddy ke /* ss_n to sclk_out delay */ 7264e36824Saddy ke #define CR0_SSD_OFFSET 10 7364e36824Saddy ke /* 7464e36824Saddy ke * The period between ss_n active and 7564e36824Saddy ke * sclk_out active is half sclk_out cycles 7664e36824Saddy ke */ 7764e36824Saddy ke #define CR0_SSD_HALF 0x0 7864e36824Saddy ke /* 7964e36824Saddy ke * The period between ss_n active and 8064e36824Saddy ke * sclk_out active is one sclk_out cycle 8164e36824Saddy ke */ 8264e36824Saddy ke #define CR0_SSD_ONE 0x1 8364e36824Saddy ke 8464e36824Saddy ke #define CR0_EM_OFFSET 11 8564e36824Saddy ke #define CR0_EM_LITTLE 0x0 8664e36824Saddy ke #define CR0_EM_BIG 0x1 8764e36824Saddy ke 8864e36824Saddy ke #define CR0_FBM_OFFSET 12 8964e36824Saddy ke #define CR0_FBM_MSB 0x0 9064e36824Saddy ke #define CR0_FBM_LSB 0x1 9164e36824Saddy ke 9264e36824Saddy ke #define CR0_BHT_OFFSET 13 9364e36824Saddy ke #define CR0_BHT_16BIT 0x0 9464e36824Saddy ke #define CR0_BHT_8BIT 0x1 9564e36824Saddy ke 9664e36824Saddy ke #define CR0_RSD_OFFSET 14 9774b7efa8SEmil Renner Berthing #define CR0_RSD_MAX 0x3 9864e36824Saddy ke 9964e36824Saddy ke #define CR0_FRF_OFFSET 16 10064e36824Saddy ke #define CR0_FRF_SPI 0x0 10164e36824Saddy ke #define CR0_FRF_SSP 0x1 10264e36824Saddy ke #define CR0_FRF_MICROWIRE 0x2 10364e36824Saddy ke 10464e36824Saddy ke #define CR0_XFM_OFFSET 18 10564e36824Saddy ke #define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET) 10664e36824Saddy ke #define CR0_XFM_TR 0x0 10764e36824Saddy ke #define CR0_XFM_TO 0x1 10864e36824Saddy ke #define CR0_XFM_RO 0x2 10964e36824Saddy ke 11064e36824Saddy ke #define CR0_OPM_OFFSET 20 11164e36824Saddy ke #define CR0_OPM_MASTER 0x0 11264e36824Saddy ke #define CR0_OPM_SLAVE 0x1 11364e36824Saddy ke 11464e36824Saddy ke #define CR0_MTM_OFFSET 0x21 11564e36824Saddy ke 11664e36824Saddy ke /* Bit fields in SER, 2bit */ 11764e36824Saddy ke #define SER_MASK 0x3 11864e36824Saddy ke 119420b82f8SEmil Renner Berthing /* Bit fields in BAUDR */ 120420b82f8SEmil Renner Berthing #define BAUDR_SCKDV_MIN 2 121420b82f8SEmil Renner Berthing #define BAUDR_SCKDV_MAX 65534 122420b82f8SEmil Renner Berthing 12364e36824Saddy ke /* Bit fields in SR, 5bit */ 12464e36824Saddy ke #define SR_MASK 0x1f 12564e36824Saddy ke #define SR_BUSY (1 << 0) 12664e36824Saddy ke #define SR_TF_FULL (1 << 1) 12764e36824Saddy ke #define SR_TF_EMPTY (1 << 2) 12864e36824Saddy ke #define SR_RF_EMPTY (1 << 3) 12964e36824Saddy ke #define SR_RF_FULL (1 << 4) 13064e36824Saddy ke 13164e36824Saddy ke /* Bit fields in ISR, IMR, ISR, RISR, 5bit */ 13264e36824Saddy ke #define INT_MASK 0x1f 13364e36824Saddy ke #define INT_TF_EMPTY (1 << 0) 13464e36824Saddy ke #define INT_TF_OVERFLOW (1 << 1) 13564e36824Saddy ke #define INT_RF_UNDERFLOW (1 << 2) 13664e36824Saddy ke #define INT_RF_OVERFLOW (1 << 3) 13764e36824Saddy ke #define INT_RF_FULL (1 << 4) 13864e36824Saddy ke 13964e36824Saddy ke /* Bit fields in ICR, 4bit */ 14064e36824Saddy ke #define ICR_MASK 0x0f 14164e36824Saddy ke #define ICR_ALL (1 << 0) 14264e36824Saddy ke #define ICR_RF_UNDERFLOW (1 << 1) 14364e36824Saddy ke #define ICR_RF_OVERFLOW (1 << 2) 14464e36824Saddy ke #define ICR_TF_OVERFLOW (1 << 3) 14564e36824Saddy ke 14664e36824Saddy ke /* Bit fields in DMACR */ 14764e36824Saddy ke #define RF_DMA_EN (1 << 0) 14864e36824Saddy ke #define TF_DMA_EN (1 << 1) 14964e36824Saddy ke 150fab3e487SEmil Renner Berthing /* Driver state flags */ 151fab3e487SEmil Renner Berthing #define RXDMA (1 << 0) 152fab3e487SEmil Renner Berthing #define TXDMA (1 << 1) 15364e36824Saddy ke 154f9cfd522SAddy Ke /* sclk_out: spi master internal logic in rk3x can support 50Mhz */ 155420b82f8SEmil Renner Berthing #define MAX_SCLK_OUT 50000000U 156f9cfd522SAddy Ke 1575185a81cSBrian Norris /* 1585185a81cSBrian Norris * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However, 1595185a81cSBrian Norris * the controller seems to hang when given 0x10000, so stick with this for now. 1605185a81cSBrian Norris */ 1615185a81cSBrian Norris #define ROCKCHIP_SPI_MAX_TRANLEN 0xffff 1625185a81cSBrian Norris 163aa099382SJeffy Chen #define ROCKCHIP_SPI_MAX_CS_NUM 2 164aa099382SJeffy Chen 16564e36824Saddy ke struct rockchip_spi { 16664e36824Saddy ke struct device *dev; 16764e36824Saddy ke 16864e36824Saddy ke struct clk *spiclk; 16964e36824Saddy ke struct clk *apb_pclk; 17064e36824Saddy ke 17164e36824Saddy ke void __iomem *regs; 172eee06a9eSEmil Renner Berthing dma_addr_t dma_addr_rx; 173eee06a9eSEmil Renner Berthing dma_addr_t dma_addr_tx; 174fab3e487SEmil Renner Berthing 175*01b59ce5SEmil Renner Berthing const void *tx; 176*01b59ce5SEmil Renner Berthing void *rx; 177*01b59ce5SEmil Renner Berthing unsigned int tx_left; 178*01b59ce5SEmil Renner Berthing unsigned int rx_left; 179*01b59ce5SEmil Renner Berthing 180fab3e487SEmil Renner Berthing atomic_t state; 181fab3e487SEmil Renner Berthing 18264e36824Saddy ke /*depth of the FIFO buffer */ 18364e36824Saddy ke u32 fifo_len; 184420b82f8SEmil Renner Berthing /* frequency of spiclk */ 185420b82f8SEmil Renner Berthing u32 freq; 18664e36824Saddy ke 18764e36824Saddy ke u8 n_bytes; 18874b7efa8SEmil Renner Berthing u8 rsd; 18964e36824Saddy ke 190aa099382SJeffy Chen bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM]; 19164e36824Saddy ke }; 19264e36824Saddy ke 19330688e4eSEmil Renner Berthing static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable) 19464e36824Saddy ke { 19530688e4eSEmil Renner Berthing writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR); 19664e36824Saddy ke } 19764e36824Saddy ke 1982df08e78SAddy Ke static inline void wait_for_idle(struct rockchip_spi *rs) 1992df08e78SAddy Ke { 2002df08e78SAddy Ke unsigned long timeout = jiffies + msecs_to_jiffies(5); 2012df08e78SAddy Ke 2022df08e78SAddy Ke do { 2032df08e78SAddy Ke if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)) 2042df08e78SAddy Ke return; 20564bc0110SDoug Anderson } while (!time_after(jiffies, timeout)); 2062df08e78SAddy Ke 2072df08e78SAddy Ke dev_warn(rs->dev, "spi controller is in busy state!\n"); 2082df08e78SAddy Ke } 2092df08e78SAddy Ke 21064e36824Saddy ke static u32 get_fifo_len(struct rockchip_spi *rs) 21164e36824Saddy ke { 21264e36824Saddy ke u32 fifo; 21364e36824Saddy ke 21464e36824Saddy ke for (fifo = 2; fifo < 32; fifo++) { 21564e36824Saddy ke writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR); 21664e36824Saddy ke if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR)) 21764e36824Saddy ke break; 21864e36824Saddy ke } 21964e36824Saddy ke 22064e36824Saddy ke writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR); 22164e36824Saddy ke 22264e36824Saddy ke return (fifo == 31) ? 0 : fifo; 22364e36824Saddy ke } 22464e36824Saddy ke 22564e36824Saddy ke static void rockchip_spi_set_cs(struct spi_device *spi, bool enable) 22664e36824Saddy ke { 227b920cc31SHuibin Hong struct spi_master *master = spi->master; 228b920cc31SHuibin Hong struct rockchip_spi *rs = spi_master_get_devdata(master); 229aa099382SJeffy Chen bool cs_asserted = !enable; 230b920cc31SHuibin Hong 231aa099382SJeffy Chen /* Return immediately for no-op */ 232aa099382SJeffy Chen if (cs_asserted == rs->cs_asserted[spi->chip_select]) 233aa099382SJeffy Chen return; 234aa099382SJeffy Chen 235aa099382SJeffy Chen if (cs_asserted) { 236aa099382SJeffy Chen /* Keep things powered as long as CS is asserted */ 237b920cc31SHuibin Hong pm_runtime_get_sync(rs->dev); 23864e36824Saddy ke 239aa099382SJeffy Chen ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, 240aa099382SJeffy Chen BIT(spi->chip_select)); 241aa099382SJeffy Chen } else { 242aa099382SJeffy Chen ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, 243aa099382SJeffy Chen BIT(spi->chip_select)); 24464e36824Saddy ke 245aa099382SJeffy Chen /* Drop reference from when we first asserted CS */ 246aa099382SJeffy Chen pm_runtime_put(rs->dev); 247aa099382SJeffy Chen } 24864e36824Saddy ke 249aa099382SJeffy Chen rs->cs_asserted[spi->chip_select] = cs_asserted; 25064e36824Saddy ke } 25164e36824Saddy ke 2522291793cSAndy Shevchenko static void rockchip_spi_handle_err(struct spi_master *master, 25364e36824Saddy ke struct spi_message *msg) 25464e36824Saddy ke { 25564e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 25664e36824Saddy ke 257ce386100SEmil Renner Berthing /* stop running spi transfer 258ce386100SEmil Renner Berthing * this also flushes both rx and tx fifos 2595dcc44edSAddy Ke */ 260ce386100SEmil Renner Berthing spi_enable_chip(rs, false); 261ce386100SEmil Renner Berthing 262*01b59ce5SEmil Renner Berthing /* make sure all interrupts are masked */ 263*01b59ce5SEmil Renner Berthing writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR); 264*01b59ce5SEmil Renner Berthing 265fab3e487SEmil Renner Berthing if (atomic_read(&rs->state) & TXDMA) 266eee06a9eSEmil Renner Berthing dmaengine_terminate_async(master->dma_tx); 267fab3e487SEmil Renner Berthing 268ce386100SEmil Renner Berthing if (atomic_read(&rs->state) & RXDMA) 269eee06a9eSEmil Renner Berthing dmaengine_terminate_async(master->dma_rx); 27064e36824Saddy ke } 27164e36824Saddy ke 27264e36824Saddy ke static void rockchip_spi_pio_writer(struct rockchip_spi *rs) 27364e36824Saddy ke { 274*01b59ce5SEmil Renner Berthing u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR); 275*01b59ce5SEmil Renner Berthing u32 words = min(rs->tx_left, tx_free); 27664e36824Saddy ke 277*01b59ce5SEmil Renner Berthing rs->tx_left -= words; 278*01b59ce5SEmil Renner Berthing for (; words; words--) { 279*01b59ce5SEmil Renner Berthing u32 txw; 280*01b59ce5SEmil Renner Berthing 28164e36824Saddy ke if (rs->n_bytes == 1) 282*01b59ce5SEmil Renner Berthing txw = *(u8 *)rs->tx; 28364e36824Saddy ke else 284*01b59ce5SEmil Renner Berthing txw = *(u16 *)rs->tx; 28564e36824Saddy ke 28664e36824Saddy ke writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR); 28764e36824Saddy ke rs->tx += rs->n_bytes; 28864e36824Saddy ke } 28964e36824Saddy ke } 29064e36824Saddy ke 29164e36824Saddy ke static void rockchip_spi_pio_reader(struct rockchip_spi *rs) 29264e36824Saddy ke { 293*01b59ce5SEmil Renner Berthing u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR); 294*01b59ce5SEmil Renner Berthing u32 rx_left = rs->rx_left - words; 29564e36824Saddy ke 296*01b59ce5SEmil Renner Berthing /* the hardware doesn't allow us to change fifo threshold 297*01b59ce5SEmil Renner Berthing * level while spi is enabled, so instead make sure to leave 298*01b59ce5SEmil Renner Berthing * enough words in the rx fifo to get the last interrupt 299*01b59ce5SEmil Renner Berthing * exactly when all words have been received 300*01b59ce5SEmil Renner Berthing */ 301*01b59ce5SEmil Renner Berthing if (rx_left) { 302*01b59ce5SEmil Renner Berthing u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1; 303*01b59ce5SEmil Renner Berthing 304*01b59ce5SEmil Renner Berthing if (rx_left < ftl) { 305*01b59ce5SEmil Renner Berthing rx_left = ftl; 306*01b59ce5SEmil Renner Berthing words = rs->rx_left - rx_left; 307*01b59ce5SEmil Renner Berthing } 308*01b59ce5SEmil Renner Berthing } 309*01b59ce5SEmil Renner Berthing 310*01b59ce5SEmil Renner Berthing rs->rx_left = rx_left; 311*01b59ce5SEmil Renner Berthing for (; words; words--) { 312*01b59ce5SEmil Renner Berthing u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR); 313*01b59ce5SEmil Renner Berthing 314*01b59ce5SEmil Renner Berthing if (!rs->rx) 315*01b59ce5SEmil Renner Berthing continue; 316*01b59ce5SEmil Renner Berthing 31764e36824Saddy ke if (rs->n_bytes == 1) 318*01b59ce5SEmil Renner Berthing *(u8 *)rs->rx = (u8)rxw; 31964e36824Saddy ke else 320*01b59ce5SEmil Renner Berthing *(u16 *)rs->rx = (u16)rxw; 32164e36824Saddy ke rs->rx += rs->n_bytes; 3225dcc44edSAddy Ke } 32364e36824Saddy ke } 32464e36824Saddy ke 325*01b59ce5SEmil Renner Berthing static irqreturn_t rockchip_spi_isr(int irq, void *dev_id) 32664e36824Saddy ke { 327*01b59ce5SEmil Renner Berthing struct spi_master *master = dev_id; 328*01b59ce5SEmil Renner Berthing struct rockchip_spi *rs = spi_master_get_devdata(master); 32964e36824Saddy ke 330*01b59ce5SEmil Renner Berthing if (rs->tx_left) 331*01b59ce5SEmil Renner Berthing rockchip_spi_pio_writer(rs); 332*01b59ce5SEmil Renner Berthing 333*01b59ce5SEmil Renner Berthing rockchip_spi_pio_reader(rs); 334*01b59ce5SEmil Renner Berthing if (!rs->rx_left) { 335*01b59ce5SEmil Renner Berthing spi_enable_chip(rs, false); 336*01b59ce5SEmil Renner Berthing writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR); 337*01b59ce5SEmil Renner Berthing spi_finalize_current_transfer(master); 338*01b59ce5SEmil Renner Berthing } 339*01b59ce5SEmil Renner Berthing 340*01b59ce5SEmil Renner Berthing return IRQ_HANDLED; 341*01b59ce5SEmil Renner Berthing } 342*01b59ce5SEmil Renner Berthing 343*01b59ce5SEmil Renner Berthing static int rockchip_spi_prepare_irq(struct rockchip_spi *rs, 344*01b59ce5SEmil Renner Berthing struct spi_transfer *xfer) 345*01b59ce5SEmil Renner Berthing { 346*01b59ce5SEmil Renner Berthing rs->tx = xfer->tx_buf; 347*01b59ce5SEmil Renner Berthing rs->rx = xfer->rx_buf; 348*01b59ce5SEmil Renner Berthing rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0; 349*01b59ce5SEmil Renner Berthing rs->rx_left = xfer->len / rs->n_bytes; 350*01b59ce5SEmil Renner Berthing 351*01b59ce5SEmil Renner Berthing writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR); 35230688e4eSEmil Renner Berthing spi_enable_chip(rs, true); 353a3c17402SEmil Renner Berthing 354*01b59ce5SEmil Renner Berthing if (rs->tx_left) 35564e36824Saddy ke rockchip_spi_pio_writer(rs); 35664e36824Saddy ke 357*01b59ce5SEmil Renner Berthing /* 1 means the transfer is in progress */ 358*01b59ce5SEmil Renner Berthing return 1; 35964e36824Saddy ke } 36064e36824Saddy ke 36164e36824Saddy ke static void rockchip_spi_dma_rxcb(void *data) 36264e36824Saddy ke { 363d790c342SEmil Renner Berthing struct spi_master *master = data; 364d790c342SEmil Renner Berthing struct rockchip_spi *rs = spi_master_get_devdata(master); 365fab3e487SEmil Renner Berthing int state = atomic_fetch_andnot(RXDMA, &rs->state); 36664e36824Saddy ke 367fab3e487SEmil Renner Berthing if (state & TXDMA) 368fab3e487SEmil Renner Berthing return; 36964e36824Saddy ke 37030688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 371d790c342SEmil Renner Berthing spi_finalize_current_transfer(master); 372c28be31bSAddy Ke } 37364e36824Saddy ke 37464e36824Saddy ke static void rockchip_spi_dma_txcb(void *data) 37564e36824Saddy ke { 376d790c342SEmil Renner Berthing struct spi_master *master = data; 377d790c342SEmil Renner Berthing struct rockchip_spi *rs = spi_master_get_devdata(master); 378fab3e487SEmil Renner Berthing int state = atomic_fetch_andnot(TXDMA, &rs->state); 379fab3e487SEmil Renner Berthing 380fab3e487SEmil Renner Berthing if (state & RXDMA) 381fab3e487SEmil Renner Berthing return; 38264e36824Saddy ke 3832df08e78SAddy Ke /* Wait until the FIFO data completely. */ 3842df08e78SAddy Ke wait_for_idle(rs); 3852df08e78SAddy Ke 38630688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 387d790c342SEmil Renner Berthing spi_finalize_current_transfer(master); 3882c2bc748SAddy Ke } 38964e36824Saddy ke 390fc1ad8eeSEmil Renner Berthing static int rockchip_spi_prepare_dma(struct rockchip_spi *rs, 391eee06a9eSEmil Renner Berthing struct spi_master *master, struct spi_transfer *xfer) 39264e36824Saddy ke { 39364e36824Saddy ke struct dma_async_tx_descriptor *rxdesc, *txdesc; 39464e36824Saddy ke 395fab3e487SEmil Renner Berthing atomic_set(&rs->state, 0); 39664e36824Saddy ke 39797cf5669SArnd Bergmann rxdesc = NULL; 398fc1ad8eeSEmil Renner Berthing if (xfer->rx_buf) { 39931bcb57bSEmil Renner Berthing struct dma_slave_config rxconf = { 40031bcb57bSEmil Renner Berthing .direction = DMA_DEV_TO_MEM, 401eee06a9eSEmil Renner Berthing .src_addr = rs->dma_addr_rx, 40231bcb57bSEmil Renner Berthing .src_addr_width = rs->n_bytes, 40331bcb57bSEmil Renner Berthing .src_maxburst = 1, 40431bcb57bSEmil Renner Berthing }; 40531bcb57bSEmil Renner Berthing 406eee06a9eSEmil Renner Berthing dmaengine_slave_config(master->dma_rx, &rxconf); 40764e36824Saddy ke 4085dcc44edSAddy Ke rxdesc = dmaengine_prep_slave_sg( 409eee06a9eSEmil Renner Berthing master->dma_rx, 410fc1ad8eeSEmil Renner Berthing xfer->rx_sg.sgl, xfer->rx_sg.nents, 411d9071b7eSEmil Renner Berthing DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT); 412ea984911SShawn Lin if (!rxdesc) 413ea984911SShawn Lin return -EINVAL; 41464e36824Saddy ke 41564e36824Saddy ke rxdesc->callback = rockchip_spi_dma_rxcb; 416d790c342SEmil Renner Berthing rxdesc->callback_param = master; 41764e36824Saddy ke } 41864e36824Saddy ke 41997cf5669SArnd Bergmann txdesc = NULL; 420fc1ad8eeSEmil Renner Berthing if (xfer->tx_buf) { 42131bcb57bSEmil Renner Berthing struct dma_slave_config txconf = { 42231bcb57bSEmil Renner Berthing .direction = DMA_MEM_TO_DEV, 423eee06a9eSEmil Renner Berthing .dst_addr = rs->dma_addr_tx, 42431bcb57bSEmil Renner Berthing .dst_addr_width = rs->n_bytes, 42531bcb57bSEmil Renner Berthing .dst_maxburst = rs->fifo_len / 2, 42631bcb57bSEmil Renner Berthing }; 42731bcb57bSEmil Renner Berthing 428eee06a9eSEmil Renner Berthing dmaengine_slave_config(master->dma_tx, &txconf); 42964e36824Saddy ke 4305dcc44edSAddy Ke txdesc = dmaengine_prep_slave_sg( 431eee06a9eSEmil Renner Berthing master->dma_tx, 432fc1ad8eeSEmil Renner Berthing xfer->tx_sg.sgl, xfer->tx_sg.nents, 433d9071b7eSEmil Renner Berthing DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT); 434ea984911SShawn Lin if (!txdesc) { 435ea984911SShawn Lin if (rxdesc) 436eee06a9eSEmil Renner Berthing dmaengine_terminate_sync(master->dma_rx); 437ea984911SShawn Lin return -EINVAL; 438ea984911SShawn Lin } 43964e36824Saddy ke 44064e36824Saddy ke txdesc->callback = rockchip_spi_dma_txcb; 441d790c342SEmil Renner Berthing txdesc->callback_param = master; 44264e36824Saddy ke } 44364e36824Saddy ke 44464e36824Saddy ke /* rx must be started before tx due to spi instinct */ 44597cf5669SArnd Bergmann if (rxdesc) { 446fab3e487SEmil Renner Berthing atomic_or(RXDMA, &rs->state); 44764e36824Saddy ke dmaengine_submit(rxdesc); 448eee06a9eSEmil Renner Berthing dma_async_issue_pending(master->dma_rx); 44964e36824Saddy ke } 45064e36824Saddy ke 45130688e4eSEmil Renner Berthing spi_enable_chip(rs, true); 452a3c17402SEmil Renner Berthing 45397cf5669SArnd Bergmann if (txdesc) { 454fab3e487SEmil Renner Berthing atomic_or(TXDMA, &rs->state); 45564e36824Saddy ke dmaengine_submit(txdesc); 456eee06a9eSEmil Renner Berthing dma_async_issue_pending(master->dma_tx); 45764e36824Saddy ke } 458ea984911SShawn Lin 459a3c17402SEmil Renner Berthing /* 1 means the transfer is in progress */ 460a3c17402SEmil Renner Berthing return 1; 46164e36824Saddy ke } 46264e36824Saddy ke 463fc1ad8eeSEmil Renner Berthing static void rockchip_spi_config(struct rockchip_spi *rs, 464eff0275eSEmil Renner Berthing struct spi_device *spi, struct spi_transfer *xfer, 465eff0275eSEmil Renner Berthing bool use_dma) 46664e36824Saddy ke { 46764e36824Saddy ke u32 dmacr = 0; 46864e36824Saddy ke 4692410d6a3SEmil Renner Berthing u32 cr0 = CR0_FRF_SPI << CR0_FRF_OFFSET 4702410d6a3SEmil Renner Berthing | CR0_BHT_8BIT << CR0_BHT_OFFSET 4712410d6a3SEmil Renner Berthing | CR0_SSD_ONE << CR0_SSD_OFFSET 4722410d6a3SEmil Renner Berthing | CR0_EM_BIG << CR0_EM_OFFSET; 47364e36824Saddy ke 47474b7efa8SEmil Renner Berthing cr0 |= rs->rsd << CR0_RSD_OFFSET; 47564e36824Saddy ke cr0 |= (rs->n_bytes << CR0_DFS_OFFSET); 476fc1ad8eeSEmil Renner Berthing cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET; 477fc1ad8eeSEmil Renner Berthing 478fc1ad8eeSEmil Renner Berthing if (xfer->rx_buf && xfer->tx_buf) 479fc1ad8eeSEmil Renner Berthing cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET; 480fc1ad8eeSEmil Renner Berthing else if (xfer->rx_buf) 481fc1ad8eeSEmil Renner Berthing cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET; 482*01b59ce5SEmil Renner Berthing else if (use_dma) 483fc1ad8eeSEmil Renner Berthing cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET; 48464e36824Saddy ke 485eff0275eSEmil Renner Berthing if (use_dma) { 486fc1ad8eeSEmil Renner Berthing if (xfer->tx_buf) 48764e36824Saddy ke dmacr |= TF_DMA_EN; 488fc1ad8eeSEmil Renner Berthing if (xfer->rx_buf) 48964e36824Saddy ke dmacr |= RF_DMA_EN; 49064e36824Saddy ke } 49164e36824Saddy ke 49264e36824Saddy ke writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0); 49364e36824Saddy ke 49404b37d2dSHuibin Hong if (rs->n_bytes == 1) 495fc1ad8eeSEmil Renner Berthing writel_relaxed(xfer->len - 1, rs->regs + ROCKCHIP_SPI_CTRLR1); 49604b37d2dSHuibin Hong else if (rs->n_bytes == 2) 497fc1ad8eeSEmil Renner Berthing writel_relaxed((xfer->len / 2) - 1, rs->regs + ROCKCHIP_SPI_CTRLR1); 49804b37d2dSHuibin Hong else 499fc1ad8eeSEmil Renner Berthing writel_relaxed((xfer->len * 2) - 1, rs->regs + ROCKCHIP_SPI_CTRLR1); 50004b37d2dSHuibin Hong 501*01b59ce5SEmil Renner Berthing /* unfortunately setting the fifo threshold level to generate an 502*01b59ce5SEmil Renner Berthing * interrupt exactly when the fifo is full doesn't seem to work, 503*01b59ce5SEmil Renner Berthing * so we need the strict inequality here 504*01b59ce5SEmil Renner Berthing */ 505*01b59ce5SEmil Renner Berthing if (xfer->len < rs->fifo_len) 506*01b59ce5SEmil Renner Berthing writel_relaxed(xfer->len - 1, rs->regs + ROCKCHIP_SPI_RXFTLR); 507*01b59ce5SEmil Renner Berthing else 50864e36824Saddy ke writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR); 50964e36824Saddy ke 510dcfc861dSHuibin Hong writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_DMATDLR); 51164e36824Saddy ke writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR); 51264e36824Saddy ke writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR); 51364e36824Saddy ke 514420b82f8SEmil Renner Berthing /* the hardware only supports an even clock divisor, so 515420b82f8SEmil Renner Berthing * round divisor = spiclk / speed up to nearest even number 516420b82f8SEmil Renner Berthing * so that the resulting speed is <= the requested speed 517420b82f8SEmil Renner Berthing */ 518420b82f8SEmil Renner Berthing writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz), 519420b82f8SEmil Renner Berthing rs->regs + ROCKCHIP_SPI_BAUDR); 52064e36824Saddy ke } 52164e36824Saddy ke 5225185a81cSBrian Norris static size_t rockchip_spi_max_transfer_size(struct spi_device *spi) 5235185a81cSBrian Norris { 5245185a81cSBrian Norris return ROCKCHIP_SPI_MAX_TRANLEN; 5255185a81cSBrian Norris } 5265185a81cSBrian Norris 5275dcc44edSAddy Ke static int rockchip_spi_transfer_one( 5285dcc44edSAddy Ke struct spi_master *master, 52964e36824Saddy ke struct spi_device *spi, 53064e36824Saddy ke struct spi_transfer *xfer) 53164e36824Saddy ke { 53264e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 533eff0275eSEmil Renner Berthing bool use_dma; 53464e36824Saddy ke 53562946172SDoug Anderson WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) && 53662946172SDoug Anderson (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)); 53764e36824Saddy ke 53864e36824Saddy ke if (!xfer->tx_buf && !xfer->rx_buf) { 53964e36824Saddy ke dev_err(rs->dev, "No buffer for transfer\n"); 54064e36824Saddy ke return -EINVAL; 54164e36824Saddy ke } 54264e36824Saddy ke 5435185a81cSBrian Norris if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) { 5445185a81cSBrian Norris dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len); 5455185a81cSBrian Norris return -EINVAL; 5465185a81cSBrian Norris } 5475185a81cSBrian Norris 548fc1ad8eeSEmil Renner Berthing rs->n_bytes = xfer->bits_per_word >> 3; 54964e36824Saddy ke 550eff0275eSEmil Renner Berthing use_dma = master->can_dma ? master->can_dma(master, spi, xfer) : false; 55164e36824Saddy ke 552eff0275eSEmil Renner Berthing rockchip_spi_config(rs, spi, xfer, use_dma); 55364e36824Saddy ke 554eff0275eSEmil Renner Berthing if (use_dma) 555eee06a9eSEmil Renner Berthing return rockchip_spi_prepare_dma(rs, master, xfer); 55664e36824Saddy ke 557*01b59ce5SEmil Renner Berthing return rockchip_spi_prepare_irq(rs, xfer); 55864e36824Saddy ke } 55964e36824Saddy ke 56064e36824Saddy ke static bool rockchip_spi_can_dma(struct spi_master *master, 56164e36824Saddy ke struct spi_device *spi, 56264e36824Saddy ke struct spi_transfer *xfer) 56364e36824Saddy ke { 56464e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 565*01b59ce5SEmil Renner Berthing unsigned int bytes_per_word = xfer->bits_per_word <= 8 ? 1 : 2; 56664e36824Saddy ke 567*01b59ce5SEmil Renner Berthing /* if the numbor of spi words to transfer is less than the fifo 568*01b59ce5SEmil Renner Berthing * length we can just fill the fifo and wait for a single irq, 569*01b59ce5SEmil Renner Berthing * so don't bother setting up dma 570*01b59ce5SEmil Renner Berthing */ 571*01b59ce5SEmil Renner Berthing return xfer->len / bytes_per_word >= rs->fifo_len; 57264e36824Saddy ke } 57364e36824Saddy ke 57464e36824Saddy ke static int rockchip_spi_probe(struct platform_device *pdev) 57564e36824Saddy ke { 57643de979dSJeffy Chen int ret; 57764e36824Saddy ke struct rockchip_spi *rs; 57864e36824Saddy ke struct spi_master *master; 57964e36824Saddy ke struct resource *mem; 58076b17e6eSJulius Werner u32 rsd_nsecs; 58164e36824Saddy ke 58264e36824Saddy ke master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi)); 5835dcc44edSAddy Ke if (!master) 58464e36824Saddy ke return -ENOMEM; 5855dcc44edSAddy Ke 58664e36824Saddy ke platform_set_drvdata(pdev, master); 58764e36824Saddy ke 58864e36824Saddy ke rs = spi_master_get_devdata(master); 58964e36824Saddy ke 59064e36824Saddy ke /* Get basic io resource and map it */ 59164e36824Saddy ke mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 59264e36824Saddy ke rs->regs = devm_ioremap_resource(&pdev->dev, mem); 59364e36824Saddy ke if (IS_ERR(rs->regs)) { 59464e36824Saddy ke ret = PTR_ERR(rs->regs); 595c351587eSJeffy Chen goto err_put_master; 59664e36824Saddy ke } 59764e36824Saddy ke 59864e36824Saddy ke rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk"); 59964e36824Saddy ke if (IS_ERR(rs->apb_pclk)) { 60064e36824Saddy ke dev_err(&pdev->dev, "Failed to get apb_pclk\n"); 60164e36824Saddy ke ret = PTR_ERR(rs->apb_pclk); 602c351587eSJeffy Chen goto err_put_master; 60364e36824Saddy ke } 60464e36824Saddy ke 60564e36824Saddy ke rs->spiclk = devm_clk_get(&pdev->dev, "spiclk"); 60664e36824Saddy ke if (IS_ERR(rs->spiclk)) { 60764e36824Saddy ke dev_err(&pdev->dev, "Failed to get spi_pclk\n"); 60864e36824Saddy ke ret = PTR_ERR(rs->spiclk); 609c351587eSJeffy Chen goto err_put_master; 61064e36824Saddy ke } 61164e36824Saddy ke 61264e36824Saddy ke ret = clk_prepare_enable(rs->apb_pclk); 61343de979dSJeffy Chen if (ret < 0) { 61464e36824Saddy ke dev_err(&pdev->dev, "Failed to enable apb_pclk\n"); 615c351587eSJeffy Chen goto err_put_master; 61664e36824Saddy ke } 61764e36824Saddy ke 61864e36824Saddy ke ret = clk_prepare_enable(rs->spiclk); 61943de979dSJeffy Chen if (ret < 0) { 62064e36824Saddy ke dev_err(&pdev->dev, "Failed to enable spi_clk\n"); 621c351587eSJeffy Chen goto err_disable_apbclk; 62264e36824Saddy ke } 62364e36824Saddy ke 62430688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 62564e36824Saddy ke 626*01b59ce5SEmil Renner Berthing ret = platform_get_irq(pdev, 0); 627*01b59ce5SEmil Renner Berthing if (ret < 0) 628*01b59ce5SEmil Renner Berthing goto err_disable_spiclk; 629*01b59ce5SEmil Renner Berthing 630*01b59ce5SEmil Renner Berthing ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL, 631*01b59ce5SEmil Renner Berthing IRQF_ONESHOT, dev_name(&pdev->dev), master); 632*01b59ce5SEmil Renner Berthing if (ret) 633*01b59ce5SEmil Renner Berthing goto err_disable_spiclk; 634*01b59ce5SEmil Renner Berthing 63564e36824Saddy ke rs->dev = &pdev->dev; 636420b82f8SEmil Renner Berthing rs->freq = clk_get_rate(rs->spiclk); 63764e36824Saddy ke 63876b17e6eSJulius Werner if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns", 63974b7efa8SEmil Renner Berthing &rsd_nsecs)) { 64074b7efa8SEmil Renner Berthing /* rx sample delay is expressed in parent clock cycles (max 3) */ 64174b7efa8SEmil Renner Berthing u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8), 64274b7efa8SEmil Renner Berthing 1000000000 >> 8); 64374b7efa8SEmil Renner Berthing if (!rsd) { 64474b7efa8SEmil Renner Berthing dev_warn(rs->dev, "%u Hz are too slow to express %u ns delay\n", 64574b7efa8SEmil Renner Berthing rs->freq, rsd_nsecs); 64674b7efa8SEmil Renner Berthing } else if (rsd > CR0_RSD_MAX) { 64774b7efa8SEmil Renner Berthing rsd = CR0_RSD_MAX; 64874b7efa8SEmil Renner Berthing dev_warn(rs->dev, "%u Hz are too fast to express %u ns delay, clamping at %u ns\n", 64974b7efa8SEmil Renner Berthing rs->freq, rsd_nsecs, 65074b7efa8SEmil Renner Berthing CR0_RSD_MAX * 1000000000U / rs->freq); 65174b7efa8SEmil Renner Berthing } 65274b7efa8SEmil Renner Berthing rs->rsd = rsd; 65374b7efa8SEmil Renner Berthing } 65476b17e6eSJulius Werner 65564e36824Saddy ke rs->fifo_len = get_fifo_len(rs); 65664e36824Saddy ke if (!rs->fifo_len) { 65764e36824Saddy ke dev_err(&pdev->dev, "Failed to get fifo length\n"); 658db7e8d90SWei Yongjun ret = -EINVAL; 659c351587eSJeffy Chen goto err_disable_spiclk; 66064e36824Saddy ke } 66164e36824Saddy ke 66264e36824Saddy ke pm_runtime_set_active(&pdev->dev); 66364e36824Saddy ke pm_runtime_enable(&pdev->dev); 66464e36824Saddy ke 66564e36824Saddy ke master->auto_runtime_pm = true; 66664e36824Saddy ke master->bus_num = pdev->id; 667ee780997SAddy Ke master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP; 668aa099382SJeffy Chen master->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM; 66964e36824Saddy ke master->dev.of_node = pdev->dev.of_node; 67064e36824Saddy ke master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8); 671420b82f8SEmil Renner Berthing master->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX; 672420b82f8SEmil Renner Berthing master->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT); 67364e36824Saddy ke 67464e36824Saddy ke master->set_cs = rockchip_spi_set_cs; 67564e36824Saddy ke master->transfer_one = rockchip_spi_transfer_one; 6765185a81cSBrian Norris master->max_transfer_size = rockchip_spi_max_transfer_size; 6772291793cSAndy Shevchenko master->handle_err = rockchip_spi_handle_err; 678c863795cSJeffy Chen master->flags = SPI_MASTER_GPIO_SS; 67964e36824Saddy ke 680eee06a9eSEmil Renner Berthing master->dma_tx = dma_request_chan(rs->dev, "tx"); 681eee06a9eSEmil Renner Berthing if (IS_ERR(master->dma_tx)) { 68261cadcf4SShawn Lin /* Check tx to see if we need defer probing driver */ 683eee06a9eSEmil Renner Berthing if (PTR_ERR(master->dma_tx) == -EPROBE_DEFER) { 68461cadcf4SShawn Lin ret = -EPROBE_DEFER; 685c351587eSJeffy Chen goto err_disable_pm_runtime; 68661cadcf4SShawn Lin } 68764e36824Saddy ke dev_warn(rs->dev, "Failed to request TX DMA channel\n"); 688eee06a9eSEmil Renner Berthing master->dma_tx = NULL; 68964e36824Saddy ke } 690e4c0e06fSShawn Lin 691eee06a9eSEmil Renner Berthing master->dma_rx = dma_request_chan(rs->dev, "rx"); 692eee06a9eSEmil Renner Berthing if (IS_ERR(master->dma_rx)) { 693eee06a9eSEmil Renner Berthing if (PTR_ERR(master->dma_rx) == -EPROBE_DEFER) { 694e4c0e06fSShawn Lin ret = -EPROBE_DEFER; 6955de7ed0cSDan Carpenter goto err_free_dma_tx; 696e4c0e06fSShawn Lin } 69764e36824Saddy ke dev_warn(rs->dev, "Failed to request RX DMA channel\n"); 698eee06a9eSEmil Renner Berthing master->dma_rx = NULL; 69964e36824Saddy ke } 70064e36824Saddy ke 701eee06a9eSEmil Renner Berthing if (master->dma_tx && master->dma_rx) { 702eee06a9eSEmil Renner Berthing rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR; 703eee06a9eSEmil Renner Berthing rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR; 70464e36824Saddy ke master->can_dma = rockchip_spi_can_dma; 70564e36824Saddy ke } 70664e36824Saddy ke 70764e36824Saddy ke ret = devm_spi_register_master(&pdev->dev, master); 70843de979dSJeffy Chen if (ret < 0) { 70964e36824Saddy ke dev_err(&pdev->dev, "Failed to register master\n"); 710c351587eSJeffy Chen goto err_free_dma_rx; 71164e36824Saddy ke } 71264e36824Saddy ke 71364e36824Saddy ke return 0; 71464e36824Saddy ke 715c351587eSJeffy Chen err_free_dma_rx: 716eee06a9eSEmil Renner Berthing if (master->dma_rx) 717eee06a9eSEmil Renner Berthing dma_release_channel(master->dma_rx); 7185de7ed0cSDan Carpenter err_free_dma_tx: 719eee06a9eSEmil Renner Berthing if (master->dma_tx) 720eee06a9eSEmil Renner Berthing dma_release_channel(master->dma_tx); 721c351587eSJeffy Chen err_disable_pm_runtime: 722c351587eSJeffy Chen pm_runtime_disable(&pdev->dev); 723c351587eSJeffy Chen err_disable_spiclk: 72464e36824Saddy ke clk_disable_unprepare(rs->spiclk); 725c351587eSJeffy Chen err_disable_apbclk: 72664e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 727c351587eSJeffy Chen err_put_master: 72864e36824Saddy ke spi_master_put(master); 72964e36824Saddy ke 73064e36824Saddy ke return ret; 73164e36824Saddy ke } 73264e36824Saddy ke 73364e36824Saddy ke static int rockchip_spi_remove(struct platform_device *pdev) 73464e36824Saddy ke { 73564e36824Saddy ke struct spi_master *master = spi_master_get(platform_get_drvdata(pdev)); 73664e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 73764e36824Saddy ke 7386a06e895SJeffy Chen pm_runtime_get_sync(&pdev->dev); 73964e36824Saddy ke 74064e36824Saddy ke clk_disable_unprepare(rs->spiclk); 74164e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 74264e36824Saddy ke 7436a06e895SJeffy Chen pm_runtime_put_noidle(&pdev->dev); 7446a06e895SJeffy Chen pm_runtime_disable(&pdev->dev); 7456a06e895SJeffy Chen pm_runtime_set_suspended(&pdev->dev); 7466a06e895SJeffy Chen 747eee06a9eSEmil Renner Berthing if (master->dma_tx) 748eee06a9eSEmil Renner Berthing dma_release_channel(master->dma_tx); 749eee06a9eSEmil Renner Berthing if (master->dma_rx) 750eee06a9eSEmil Renner Berthing dma_release_channel(master->dma_rx); 75164e36824Saddy ke 752844c9f47SShawn Lin spi_master_put(master); 753844c9f47SShawn Lin 75464e36824Saddy ke return 0; 75564e36824Saddy ke } 75664e36824Saddy ke 75764e36824Saddy ke #ifdef CONFIG_PM_SLEEP 75864e36824Saddy ke static int rockchip_spi_suspend(struct device *dev) 75964e36824Saddy ke { 76043de979dSJeffy Chen int ret; 76164e36824Saddy ke struct spi_master *master = dev_get_drvdata(dev); 76264e36824Saddy ke 763d790c342SEmil Renner Berthing ret = spi_master_suspend(master); 76443de979dSJeffy Chen if (ret < 0) 76564e36824Saddy ke return ret; 76664e36824Saddy ke 767d38c4ae1SJeffy Chen ret = pm_runtime_force_suspend(dev); 768d38c4ae1SJeffy Chen if (ret < 0) 769d38c4ae1SJeffy Chen return ret; 77064e36824Saddy ke 77123e291c2SBrian Norris pinctrl_pm_select_sleep_state(dev); 77223e291c2SBrian Norris 77343de979dSJeffy Chen return 0; 77464e36824Saddy ke } 77564e36824Saddy ke 77664e36824Saddy ke static int rockchip_spi_resume(struct device *dev) 77764e36824Saddy ke { 77843de979dSJeffy Chen int ret; 77964e36824Saddy ke struct spi_master *master = dev_get_drvdata(dev); 78064e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 78164e36824Saddy ke 78223e291c2SBrian Norris pinctrl_pm_select_default_state(dev); 78323e291c2SBrian Norris 784d38c4ae1SJeffy Chen ret = pm_runtime_force_resume(dev); 78564e36824Saddy ke if (ret < 0) 78664e36824Saddy ke return ret; 78764e36824Saddy ke 788d790c342SEmil Renner Berthing ret = spi_master_resume(master); 78964e36824Saddy ke if (ret < 0) { 79064e36824Saddy ke clk_disable_unprepare(rs->spiclk); 79164e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 79264e36824Saddy ke } 79364e36824Saddy ke 79443de979dSJeffy Chen return 0; 79564e36824Saddy ke } 79664e36824Saddy ke #endif /* CONFIG_PM_SLEEP */ 79764e36824Saddy ke 798ec833050SRafael J. Wysocki #ifdef CONFIG_PM 79964e36824Saddy ke static int rockchip_spi_runtime_suspend(struct device *dev) 80064e36824Saddy ke { 80164e36824Saddy ke struct spi_master *master = dev_get_drvdata(dev); 80264e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 80364e36824Saddy ke 80464e36824Saddy ke clk_disable_unprepare(rs->spiclk); 80564e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 80664e36824Saddy ke 80764e36824Saddy ke return 0; 80864e36824Saddy ke } 80964e36824Saddy ke 81064e36824Saddy ke static int rockchip_spi_runtime_resume(struct device *dev) 81164e36824Saddy ke { 81264e36824Saddy ke int ret; 81364e36824Saddy ke struct spi_master *master = dev_get_drvdata(dev); 81464e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 81564e36824Saddy ke 81664e36824Saddy ke ret = clk_prepare_enable(rs->apb_pclk); 81743de979dSJeffy Chen if (ret < 0) 81864e36824Saddy ke return ret; 81964e36824Saddy ke 82064e36824Saddy ke ret = clk_prepare_enable(rs->spiclk); 82143de979dSJeffy Chen if (ret < 0) 82264e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 82364e36824Saddy ke 82443de979dSJeffy Chen return 0; 82564e36824Saddy ke } 826ec833050SRafael J. Wysocki #endif /* CONFIG_PM */ 82764e36824Saddy ke 82864e36824Saddy ke static const struct dev_pm_ops rockchip_spi_pm = { 82964e36824Saddy ke SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume) 83064e36824Saddy ke SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend, 83164e36824Saddy ke rockchip_spi_runtime_resume, NULL) 83264e36824Saddy ke }; 83364e36824Saddy ke 83464e36824Saddy ke static const struct of_device_id rockchip_spi_dt_match[] = { 8356b860e69SAndy Yan { .compatible = "rockchip,rv1108-spi", }, 836aa29ea3dSCaesar Wang { .compatible = "rockchip,rk3036-spi", }, 83764e36824Saddy ke { .compatible = "rockchip,rk3066-spi", }, 838b839b785SAddy Ke { .compatible = "rockchip,rk3188-spi", }, 839aa29ea3dSCaesar Wang { .compatible = "rockchip,rk3228-spi", }, 840b839b785SAddy Ke { .compatible = "rockchip,rk3288-spi", }, 841aa29ea3dSCaesar Wang { .compatible = "rockchip,rk3368-spi", }, 8429b7a5622SXu Jianqun { .compatible = "rockchip,rk3399-spi", }, 84364e36824Saddy ke { }, 84464e36824Saddy ke }; 84564e36824Saddy ke MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match); 84664e36824Saddy ke 84764e36824Saddy ke static struct platform_driver rockchip_spi_driver = { 84864e36824Saddy ke .driver = { 84964e36824Saddy ke .name = DRIVER_NAME, 85064e36824Saddy ke .pm = &rockchip_spi_pm, 85164e36824Saddy ke .of_match_table = of_match_ptr(rockchip_spi_dt_match), 85264e36824Saddy ke }, 85364e36824Saddy ke .probe = rockchip_spi_probe, 85464e36824Saddy ke .remove = rockchip_spi_remove, 85564e36824Saddy ke }; 85664e36824Saddy ke 85764e36824Saddy ke module_platform_driver(rockchip_spi_driver); 85864e36824Saddy ke 8595dcc44edSAddy Ke MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>"); 86064e36824Saddy ke MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver"); 86164e36824Saddy ke MODULE_LICENSE("GPL v2"); 862