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 9764e36824Saddy ke 9864e36824Saddy ke #define CR0_FRF_OFFSET 16 9964e36824Saddy ke #define CR0_FRF_SPI 0x0 10064e36824Saddy ke #define CR0_FRF_SSP 0x1 10164e36824Saddy ke #define CR0_FRF_MICROWIRE 0x2 10264e36824Saddy ke 10364e36824Saddy ke #define CR0_XFM_OFFSET 18 10464e36824Saddy ke #define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET) 10564e36824Saddy ke #define CR0_XFM_TR 0x0 10664e36824Saddy ke #define CR0_XFM_TO 0x1 10764e36824Saddy ke #define CR0_XFM_RO 0x2 10864e36824Saddy ke 10964e36824Saddy ke #define CR0_OPM_OFFSET 20 11064e36824Saddy ke #define CR0_OPM_MASTER 0x0 11164e36824Saddy ke #define CR0_OPM_SLAVE 0x1 11264e36824Saddy ke 11364e36824Saddy ke #define CR0_MTM_OFFSET 0x21 11464e36824Saddy ke 11564e36824Saddy ke /* Bit fields in SER, 2bit */ 11664e36824Saddy ke #define SER_MASK 0x3 11764e36824Saddy ke 11864e36824Saddy ke /* Bit fields in SR, 5bit */ 11964e36824Saddy ke #define SR_MASK 0x1f 12064e36824Saddy ke #define SR_BUSY (1 << 0) 12164e36824Saddy ke #define SR_TF_FULL (1 << 1) 12264e36824Saddy ke #define SR_TF_EMPTY (1 << 2) 12364e36824Saddy ke #define SR_RF_EMPTY (1 << 3) 12464e36824Saddy ke #define SR_RF_FULL (1 << 4) 12564e36824Saddy ke 12664e36824Saddy ke /* Bit fields in ISR, IMR, ISR, RISR, 5bit */ 12764e36824Saddy ke #define INT_MASK 0x1f 12864e36824Saddy ke #define INT_TF_EMPTY (1 << 0) 12964e36824Saddy ke #define INT_TF_OVERFLOW (1 << 1) 13064e36824Saddy ke #define INT_RF_UNDERFLOW (1 << 2) 13164e36824Saddy ke #define INT_RF_OVERFLOW (1 << 3) 13264e36824Saddy ke #define INT_RF_FULL (1 << 4) 13364e36824Saddy ke 13464e36824Saddy ke /* Bit fields in ICR, 4bit */ 13564e36824Saddy ke #define ICR_MASK 0x0f 13664e36824Saddy ke #define ICR_ALL (1 << 0) 13764e36824Saddy ke #define ICR_RF_UNDERFLOW (1 << 1) 13864e36824Saddy ke #define ICR_RF_OVERFLOW (1 << 2) 13964e36824Saddy ke #define ICR_TF_OVERFLOW (1 << 3) 14064e36824Saddy ke 14164e36824Saddy ke /* Bit fields in DMACR */ 14264e36824Saddy ke #define RF_DMA_EN (1 << 0) 14364e36824Saddy ke #define TF_DMA_EN (1 << 1) 14464e36824Saddy ke 145fab3e487SEmil Renner Berthing /* Driver state flags */ 146fab3e487SEmil Renner Berthing #define RXDMA (1 << 0) 147fab3e487SEmil Renner Berthing #define TXDMA (1 << 1) 14864e36824Saddy ke 149f9cfd522SAddy Ke /* sclk_out: spi master internal logic in rk3x can support 50Mhz */ 150f9cfd522SAddy Ke #define MAX_SCLK_OUT 50000000 151f9cfd522SAddy Ke 1525185a81cSBrian Norris /* 1535185a81cSBrian Norris * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However, 1545185a81cSBrian Norris * the controller seems to hang when given 0x10000, so stick with this for now. 1555185a81cSBrian Norris */ 1565185a81cSBrian Norris #define ROCKCHIP_SPI_MAX_TRANLEN 0xffff 1575185a81cSBrian Norris 158aa099382SJeffy Chen #define ROCKCHIP_SPI_MAX_CS_NUM 2 159aa099382SJeffy Chen 16064e36824Saddy ke struct rockchip_spi { 16164e36824Saddy ke struct device *dev; 16264e36824Saddy ke struct spi_master *master; 16364e36824Saddy ke 16464e36824Saddy ke struct clk *spiclk; 16564e36824Saddy ke struct clk *apb_pclk; 16664e36824Saddy ke 16764e36824Saddy ke void __iomem *regs; 168*eee06a9eSEmil Renner Berthing dma_addr_t dma_addr_rx; 169*eee06a9eSEmil Renner Berthing dma_addr_t dma_addr_tx; 170fab3e487SEmil Renner Berthing 171fab3e487SEmil Renner Berthing atomic_t state; 172fab3e487SEmil Renner Berthing 17364e36824Saddy ke /*depth of the FIFO buffer */ 17464e36824Saddy ke u32 fifo_len; 17564e36824Saddy ke /* max bus freq supported */ 17664e36824Saddy ke u32 max_freq; 17764e36824Saddy ke 17864e36824Saddy ke u8 n_bytes; 179108b5c8bSShawn Lin u32 rsd_nsecs; 18064e36824Saddy ke u32 speed; 18164e36824Saddy ke 18264e36824Saddy ke const void *tx; 18364e36824Saddy ke const void *tx_end; 18464e36824Saddy ke void *rx; 18564e36824Saddy ke void *rx_end; 18664e36824Saddy ke 187aa099382SJeffy Chen bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM]; 188aa099382SJeffy Chen 189f340b920SEmil Renner Berthing bool use_dma; 19064e36824Saddy ke }; 19164e36824Saddy ke 19230688e4eSEmil Renner Berthing static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable) 19364e36824Saddy ke { 19430688e4eSEmil Renner Berthing writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR); 19564e36824Saddy ke } 19664e36824Saddy ke 19764e36824Saddy ke static inline void spi_set_clk(struct rockchip_spi *rs, u16 div) 19864e36824Saddy ke { 19964e36824Saddy ke writel_relaxed(div, rs->regs + ROCKCHIP_SPI_BAUDR); 20064e36824Saddy ke } 20164e36824Saddy ke 2022df08e78SAddy Ke static inline void wait_for_idle(struct rockchip_spi *rs) 2032df08e78SAddy Ke { 2042df08e78SAddy Ke unsigned long timeout = jiffies + msecs_to_jiffies(5); 2052df08e78SAddy Ke 2062df08e78SAddy Ke do { 2072df08e78SAddy Ke if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)) 2082df08e78SAddy Ke return; 20964bc0110SDoug Anderson } while (!time_after(jiffies, timeout)); 2102df08e78SAddy Ke 2112df08e78SAddy Ke dev_warn(rs->dev, "spi controller is in busy state!\n"); 2122df08e78SAddy Ke } 2132df08e78SAddy Ke 21464e36824Saddy ke static u32 get_fifo_len(struct rockchip_spi *rs) 21564e36824Saddy ke { 21664e36824Saddy ke u32 fifo; 21764e36824Saddy ke 21864e36824Saddy ke for (fifo = 2; fifo < 32; fifo++) { 21964e36824Saddy ke writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR); 22064e36824Saddy ke if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR)) 22164e36824Saddy ke break; 22264e36824Saddy ke } 22364e36824Saddy ke 22464e36824Saddy ke writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR); 22564e36824Saddy ke 22664e36824Saddy ke return (fifo == 31) ? 0 : fifo; 22764e36824Saddy ke } 22864e36824Saddy ke 22964e36824Saddy ke static inline u32 tx_max(struct rockchip_spi *rs) 23064e36824Saddy ke { 23164e36824Saddy ke u32 tx_left, tx_room; 23264e36824Saddy ke 23364e36824Saddy ke tx_left = (rs->tx_end - rs->tx) / rs->n_bytes; 23464e36824Saddy ke tx_room = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR); 23564e36824Saddy ke 23664e36824Saddy ke return min(tx_left, tx_room); 23764e36824Saddy ke } 23864e36824Saddy ke 23964e36824Saddy ke static inline u32 rx_max(struct rockchip_spi *rs) 24064e36824Saddy ke { 24164e36824Saddy ke u32 rx_left = (rs->rx_end - rs->rx) / rs->n_bytes; 24264e36824Saddy ke u32 rx_room = (u32)readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR); 24364e36824Saddy ke 24464e36824Saddy ke return min(rx_left, rx_room); 24564e36824Saddy ke } 24664e36824Saddy ke 24764e36824Saddy ke static void rockchip_spi_set_cs(struct spi_device *spi, bool enable) 24864e36824Saddy ke { 249b920cc31SHuibin Hong struct spi_master *master = spi->master; 250b920cc31SHuibin Hong struct rockchip_spi *rs = spi_master_get_devdata(master); 251aa099382SJeffy Chen bool cs_asserted = !enable; 252b920cc31SHuibin Hong 253aa099382SJeffy Chen /* Return immediately for no-op */ 254aa099382SJeffy Chen if (cs_asserted == rs->cs_asserted[spi->chip_select]) 255aa099382SJeffy Chen return; 256aa099382SJeffy Chen 257aa099382SJeffy Chen if (cs_asserted) { 258aa099382SJeffy Chen /* Keep things powered as long as CS is asserted */ 259b920cc31SHuibin Hong pm_runtime_get_sync(rs->dev); 26064e36824Saddy ke 261aa099382SJeffy Chen ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, 262aa099382SJeffy Chen BIT(spi->chip_select)); 263aa099382SJeffy Chen } else { 264aa099382SJeffy Chen ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, 265aa099382SJeffy Chen BIT(spi->chip_select)); 26664e36824Saddy ke 267aa099382SJeffy Chen /* Drop reference from when we first asserted CS */ 268aa099382SJeffy Chen pm_runtime_put(rs->dev); 269aa099382SJeffy Chen } 27064e36824Saddy ke 271aa099382SJeffy Chen rs->cs_asserted[spi->chip_select] = cs_asserted; 27264e36824Saddy ke } 27364e36824Saddy ke 2742291793cSAndy Shevchenko static void rockchip_spi_handle_err(struct spi_master *master, 27564e36824Saddy ke struct spi_message *msg) 27664e36824Saddy ke { 27764e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 27864e36824Saddy ke 279ce386100SEmil Renner Berthing /* stop running spi transfer 280ce386100SEmil Renner Berthing * this also flushes both rx and tx fifos 2815dcc44edSAddy Ke */ 282ce386100SEmil Renner Berthing spi_enable_chip(rs, false); 283ce386100SEmil Renner Berthing 284fab3e487SEmil Renner Berthing if (atomic_read(&rs->state) & TXDMA) 285*eee06a9eSEmil Renner Berthing dmaengine_terminate_async(master->dma_tx); 286fab3e487SEmil Renner Berthing 287ce386100SEmil Renner Berthing if (atomic_read(&rs->state) & RXDMA) 288*eee06a9eSEmil Renner Berthing dmaengine_terminate_async(master->dma_rx); 28964e36824Saddy ke } 29064e36824Saddy ke 29164e36824Saddy ke static void rockchip_spi_pio_writer(struct rockchip_spi *rs) 29264e36824Saddy ke { 29364e36824Saddy ke u32 max = tx_max(rs); 29464e36824Saddy ke u32 txw = 0; 29564e36824Saddy ke 29664e36824Saddy ke while (max--) { 29764e36824Saddy ke if (rs->n_bytes == 1) 29864e36824Saddy ke txw = *(u8 *)(rs->tx); 29964e36824Saddy ke else 30064e36824Saddy ke txw = *(u16 *)(rs->tx); 30164e36824Saddy ke 30264e36824Saddy ke writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR); 30364e36824Saddy ke rs->tx += rs->n_bytes; 30464e36824Saddy ke } 30564e36824Saddy ke } 30664e36824Saddy ke 30764e36824Saddy ke static void rockchip_spi_pio_reader(struct rockchip_spi *rs) 30864e36824Saddy ke { 30964e36824Saddy ke u32 max = rx_max(rs); 31064e36824Saddy ke u32 rxw; 31164e36824Saddy ke 31264e36824Saddy ke while (max--) { 31364e36824Saddy ke rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR); 31464e36824Saddy ke if (rs->n_bytes == 1) 31564e36824Saddy ke *(u8 *)(rs->rx) = (u8)rxw; 31664e36824Saddy ke else 31764e36824Saddy ke *(u16 *)(rs->rx) = (u16)rxw; 31864e36824Saddy ke rs->rx += rs->n_bytes; 3195dcc44edSAddy Ke } 32064e36824Saddy ke } 32164e36824Saddy ke 32264e36824Saddy ke static int rockchip_spi_pio_transfer(struct rockchip_spi *rs) 32364e36824Saddy ke { 32464e36824Saddy ke int remain = 0; 32564e36824Saddy ke 32630688e4eSEmil Renner Berthing spi_enable_chip(rs, true); 327a3c17402SEmil Renner Berthing 32864e36824Saddy ke do { 32964e36824Saddy ke if (rs->tx) { 33064e36824Saddy ke remain = rs->tx_end - rs->tx; 33164e36824Saddy ke rockchip_spi_pio_writer(rs); 33264e36824Saddy ke } 33364e36824Saddy ke 33464e36824Saddy ke if (rs->rx) { 33564e36824Saddy ke remain = rs->rx_end - rs->rx; 33664e36824Saddy ke rockchip_spi_pio_reader(rs); 33764e36824Saddy ke } 33864e36824Saddy ke 33964e36824Saddy ke cpu_relax(); 34064e36824Saddy ke } while (remain); 34164e36824Saddy ke 3422df08e78SAddy Ke /* If tx, wait until the FIFO data completely. */ 3432df08e78SAddy Ke if (rs->tx) 3442df08e78SAddy Ke wait_for_idle(rs); 3452df08e78SAddy Ke 34630688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 347c28be31bSAddy Ke 34864e36824Saddy ke return 0; 34964e36824Saddy ke } 35064e36824Saddy ke 35164e36824Saddy ke static void rockchip_spi_dma_rxcb(void *data) 35264e36824Saddy ke { 35364e36824Saddy ke struct rockchip_spi *rs = data; 354fab3e487SEmil Renner Berthing int state = atomic_fetch_andnot(RXDMA, &rs->state); 35564e36824Saddy ke 356fab3e487SEmil Renner Berthing if (state & TXDMA) 357fab3e487SEmil Renner Berthing return; 35864e36824Saddy ke 35930688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 36064e36824Saddy ke spi_finalize_current_transfer(rs->master); 361c28be31bSAddy Ke } 36264e36824Saddy ke 36364e36824Saddy ke static void rockchip_spi_dma_txcb(void *data) 36464e36824Saddy ke { 36564e36824Saddy ke struct rockchip_spi *rs = data; 366fab3e487SEmil Renner Berthing int state = atomic_fetch_andnot(TXDMA, &rs->state); 367fab3e487SEmil Renner Berthing 368fab3e487SEmil Renner Berthing if (state & RXDMA) 369fab3e487SEmil Renner Berthing return; 37064e36824Saddy ke 3712df08e78SAddy Ke /* Wait until the FIFO data completely. */ 3722df08e78SAddy Ke wait_for_idle(rs); 3732df08e78SAddy Ke 37430688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 37564e36824Saddy ke spi_finalize_current_transfer(rs->master); 3762c2bc748SAddy Ke } 37764e36824Saddy ke 378fc1ad8eeSEmil Renner Berthing static int rockchip_spi_prepare_dma(struct rockchip_spi *rs, 379*eee06a9eSEmil Renner Berthing struct spi_master *master, struct spi_transfer *xfer) 38064e36824Saddy ke { 38164e36824Saddy ke struct dma_async_tx_descriptor *rxdesc, *txdesc; 38264e36824Saddy ke 383fab3e487SEmil Renner Berthing atomic_set(&rs->state, 0); 38464e36824Saddy ke 38597cf5669SArnd Bergmann rxdesc = NULL; 386fc1ad8eeSEmil Renner Berthing if (xfer->rx_buf) { 38731bcb57bSEmil Renner Berthing struct dma_slave_config rxconf = { 38831bcb57bSEmil Renner Berthing .direction = DMA_DEV_TO_MEM, 389*eee06a9eSEmil Renner Berthing .src_addr = rs->dma_addr_rx, 39031bcb57bSEmil Renner Berthing .src_addr_width = rs->n_bytes, 39131bcb57bSEmil Renner Berthing .src_maxburst = 1, 39231bcb57bSEmil Renner Berthing }; 39331bcb57bSEmil Renner Berthing 394*eee06a9eSEmil Renner Berthing dmaengine_slave_config(master->dma_rx, &rxconf); 39564e36824Saddy ke 3965dcc44edSAddy Ke rxdesc = dmaengine_prep_slave_sg( 397*eee06a9eSEmil Renner Berthing master->dma_rx, 398fc1ad8eeSEmil Renner Berthing xfer->rx_sg.sgl, xfer->rx_sg.nents, 399d9071b7eSEmil Renner Berthing DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT); 400ea984911SShawn Lin if (!rxdesc) 401ea984911SShawn Lin return -EINVAL; 40264e36824Saddy ke 40364e36824Saddy ke rxdesc->callback = rockchip_spi_dma_rxcb; 40464e36824Saddy ke rxdesc->callback_param = rs; 40564e36824Saddy ke } 40664e36824Saddy ke 40797cf5669SArnd Bergmann txdesc = NULL; 408fc1ad8eeSEmil Renner Berthing if (xfer->tx_buf) { 40931bcb57bSEmil Renner Berthing struct dma_slave_config txconf = { 41031bcb57bSEmil Renner Berthing .direction = DMA_MEM_TO_DEV, 411*eee06a9eSEmil Renner Berthing .dst_addr = rs->dma_addr_tx, 41231bcb57bSEmil Renner Berthing .dst_addr_width = rs->n_bytes, 41331bcb57bSEmil Renner Berthing .dst_maxburst = rs->fifo_len / 2, 41431bcb57bSEmil Renner Berthing }; 41531bcb57bSEmil Renner Berthing 416*eee06a9eSEmil Renner Berthing dmaengine_slave_config(master->dma_tx, &txconf); 41764e36824Saddy ke 4185dcc44edSAddy Ke txdesc = dmaengine_prep_slave_sg( 419*eee06a9eSEmil Renner Berthing master->dma_tx, 420fc1ad8eeSEmil Renner Berthing xfer->tx_sg.sgl, xfer->tx_sg.nents, 421d9071b7eSEmil Renner Berthing DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT); 422ea984911SShawn Lin if (!txdesc) { 423ea984911SShawn Lin if (rxdesc) 424*eee06a9eSEmil Renner Berthing dmaengine_terminate_sync(master->dma_rx); 425ea984911SShawn Lin return -EINVAL; 426ea984911SShawn Lin } 42764e36824Saddy ke 42864e36824Saddy ke txdesc->callback = rockchip_spi_dma_txcb; 42964e36824Saddy ke txdesc->callback_param = rs; 43064e36824Saddy ke } 43164e36824Saddy ke 43264e36824Saddy ke /* rx must be started before tx due to spi instinct */ 43397cf5669SArnd Bergmann if (rxdesc) { 434fab3e487SEmil Renner Berthing atomic_or(RXDMA, &rs->state); 43564e36824Saddy ke dmaengine_submit(rxdesc); 436*eee06a9eSEmil Renner Berthing dma_async_issue_pending(master->dma_rx); 43764e36824Saddy ke } 43864e36824Saddy ke 43930688e4eSEmil Renner Berthing spi_enable_chip(rs, true); 440a3c17402SEmil Renner Berthing 44197cf5669SArnd Bergmann if (txdesc) { 442fab3e487SEmil Renner Berthing atomic_or(TXDMA, &rs->state); 44364e36824Saddy ke dmaengine_submit(txdesc); 444*eee06a9eSEmil Renner Berthing dma_async_issue_pending(master->dma_tx); 44564e36824Saddy ke } 446ea984911SShawn Lin 447a3c17402SEmil Renner Berthing /* 1 means the transfer is in progress */ 448a3c17402SEmil Renner Berthing return 1; 44964e36824Saddy ke } 45064e36824Saddy ke 451fc1ad8eeSEmil Renner Berthing static void rockchip_spi_config(struct rockchip_spi *rs, 452fc1ad8eeSEmil Renner Berthing struct spi_device *spi, struct spi_transfer *xfer) 45364e36824Saddy ke { 45464e36824Saddy ke u32 div = 0; 45564e36824Saddy ke u32 dmacr = 0; 45676b17e6eSJulius Werner int rsd = 0; 45764e36824Saddy ke 4582410d6a3SEmil Renner Berthing u32 cr0 = CR0_FRF_SPI << CR0_FRF_OFFSET 4592410d6a3SEmil Renner Berthing | CR0_BHT_8BIT << CR0_BHT_OFFSET 4602410d6a3SEmil Renner Berthing | CR0_SSD_ONE << CR0_SSD_OFFSET 4612410d6a3SEmil Renner Berthing | CR0_EM_BIG << CR0_EM_OFFSET; 46264e36824Saddy ke 46364e36824Saddy ke cr0 |= (rs->n_bytes << CR0_DFS_OFFSET); 464fc1ad8eeSEmil Renner Berthing cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET; 465fc1ad8eeSEmil Renner Berthing 466fc1ad8eeSEmil Renner Berthing if (xfer->rx_buf && xfer->tx_buf) 467fc1ad8eeSEmil Renner Berthing cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET; 468fc1ad8eeSEmil Renner Berthing else if (xfer->rx_buf) 469fc1ad8eeSEmil Renner Berthing cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET; 470fc1ad8eeSEmil Renner Berthing else 471fc1ad8eeSEmil Renner Berthing cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET; 47264e36824Saddy ke 47364e36824Saddy ke if (rs->use_dma) { 474fc1ad8eeSEmil Renner Berthing if (xfer->tx_buf) 47564e36824Saddy ke dmacr |= TF_DMA_EN; 476fc1ad8eeSEmil Renner Berthing if (xfer->rx_buf) 47764e36824Saddy ke dmacr |= RF_DMA_EN; 47864e36824Saddy ke } 47964e36824Saddy ke 480f9cfd522SAddy Ke if (WARN_ON(rs->speed > MAX_SCLK_OUT)) 481f9cfd522SAddy Ke rs->speed = MAX_SCLK_OUT; 482f9cfd522SAddy Ke 483bb51537aSGeert Uytterhoeven /* the minimum divisor is 2 */ 484f9cfd522SAddy Ke if (rs->max_freq < 2 * rs->speed) { 485f9cfd522SAddy Ke clk_set_rate(rs->spiclk, 2 * rs->speed); 486f9cfd522SAddy Ke rs->max_freq = clk_get_rate(rs->spiclk); 487f9cfd522SAddy Ke } 488f9cfd522SAddy Ke 48964e36824Saddy ke /* div doesn't support odd number */ 490754ec43cSJulius Werner div = DIV_ROUND_UP(rs->max_freq, rs->speed); 49164e36824Saddy ke div = (div + 1) & 0xfffe; 49264e36824Saddy ke 49376b17e6eSJulius Werner /* Rx sample delay is expressed in parent clock cycles (max 3) */ 49476b17e6eSJulius Werner rsd = DIV_ROUND_CLOSEST(rs->rsd_nsecs * (rs->max_freq >> 8), 49576b17e6eSJulius Werner 1000000000 >> 8); 49676b17e6eSJulius Werner if (!rsd && rs->rsd_nsecs) { 49776b17e6eSJulius Werner pr_warn_once("rockchip-spi: %u Hz are too slow to express %u ns delay\n", 49876b17e6eSJulius Werner rs->max_freq, rs->rsd_nsecs); 49976b17e6eSJulius Werner } else if (rsd > 3) { 50076b17e6eSJulius Werner rsd = 3; 50176b17e6eSJulius Werner pr_warn_once("rockchip-spi: %u Hz are too fast to express %u ns delay, clamping at %u ns\n", 50276b17e6eSJulius Werner rs->max_freq, rs->rsd_nsecs, 50376b17e6eSJulius Werner rsd * 1000000000U / rs->max_freq); 50476b17e6eSJulius Werner } 50576b17e6eSJulius Werner cr0 |= rsd << CR0_RSD_OFFSET; 50676b17e6eSJulius Werner 50764e36824Saddy ke writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0); 50864e36824Saddy ke 50904b37d2dSHuibin Hong if (rs->n_bytes == 1) 510fc1ad8eeSEmil Renner Berthing writel_relaxed(xfer->len - 1, rs->regs + ROCKCHIP_SPI_CTRLR1); 51104b37d2dSHuibin Hong else if (rs->n_bytes == 2) 512fc1ad8eeSEmil Renner Berthing writel_relaxed((xfer->len / 2) - 1, rs->regs + ROCKCHIP_SPI_CTRLR1); 51304b37d2dSHuibin Hong else 514fc1ad8eeSEmil Renner Berthing writel_relaxed((xfer->len * 2) - 1, rs->regs + ROCKCHIP_SPI_CTRLR1); 51504b37d2dSHuibin Hong 51664e36824Saddy ke writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_TXFTLR); 51764e36824Saddy ke writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR); 51864e36824Saddy ke 519dcfc861dSHuibin Hong writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_DMATDLR); 52064e36824Saddy ke writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR); 52164e36824Saddy ke writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR); 52264e36824Saddy ke 52364e36824Saddy ke spi_set_clk(rs, div); 52464e36824Saddy ke 5255dcc44edSAddy Ke dev_dbg(rs->dev, "cr0 0x%x, div %d\n", cr0, div); 52664e36824Saddy ke } 52764e36824Saddy ke 5285185a81cSBrian Norris static size_t rockchip_spi_max_transfer_size(struct spi_device *spi) 5295185a81cSBrian Norris { 5305185a81cSBrian Norris return ROCKCHIP_SPI_MAX_TRANLEN; 5315185a81cSBrian Norris } 5325185a81cSBrian Norris 5335dcc44edSAddy Ke static int rockchip_spi_transfer_one( 5345dcc44edSAddy Ke struct spi_master *master, 53564e36824Saddy ke struct spi_device *spi, 53664e36824Saddy ke struct spi_transfer *xfer) 53764e36824Saddy ke { 53864e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 53964e36824Saddy ke 54062946172SDoug Anderson WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) && 54162946172SDoug Anderson (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)); 54264e36824Saddy ke 54364e36824Saddy ke if (!xfer->tx_buf && !xfer->rx_buf) { 54464e36824Saddy ke dev_err(rs->dev, "No buffer for transfer\n"); 54564e36824Saddy ke return -EINVAL; 54664e36824Saddy ke } 54764e36824Saddy ke 5485185a81cSBrian Norris if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) { 5495185a81cSBrian Norris dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len); 5505185a81cSBrian Norris return -EINVAL; 5515185a81cSBrian Norris } 5525185a81cSBrian Norris 55364e36824Saddy ke rs->speed = xfer->speed_hz; 554fc1ad8eeSEmil Renner Berthing rs->n_bytes = xfer->bits_per_word >> 3; 55564e36824Saddy ke 55664e36824Saddy ke rs->tx = xfer->tx_buf; 55764e36824Saddy ke rs->tx_end = rs->tx + xfer->len; 55864e36824Saddy ke rs->rx = xfer->rx_buf; 55964e36824Saddy ke rs->rx_end = rs->rx + xfer->len; 56064e36824Saddy ke 561a24e70c0SAddy Ke /* we need prepare dma before spi was enabled */ 562c28be31bSAddy Ke if (master->can_dma && master->can_dma(master, spi, xfer)) 563f340b920SEmil Renner Berthing rs->use_dma = true; 564c28be31bSAddy Ke else 565f340b920SEmil Renner Berthing rs->use_dma = false; 56664e36824Saddy ke 567fc1ad8eeSEmil Renner Berthing rockchip_spi_config(rs, spi, xfer); 56864e36824Saddy ke 569a3c17402SEmil Renner Berthing if (rs->use_dma) 570*eee06a9eSEmil Renner Berthing return rockchip_spi_prepare_dma(rs, master, xfer); 57164e36824Saddy ke 572a3c17402SEmil Renner Berthing return rockchip_spi_pio_transfer(rs); 57364e36824Saddy ke } 57464e36824Saddy ke 57564e36824Saddy ke static bool rockchip_spi_can_dma(struct spi_master *master, 57664e36824Saddy ke struct spi_device *spi, 57764e36824Saddy ke struct spi_transfer *xfer) 57864e36824Saddy ke { 57964e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 58064e36824Saddy ke 58164e36824Saddy ke return (xfer->len > rs->fifo_len); 58264e36824Saddy ke } 58364e36824Saddy ke 58464e36824Saddy ke static int rockchip_spi_probe(struct platform_device *pdev) 58564e36824Saddy ke { 58643de979dSJeffy Chen int ret; 58764e36824Saddy ke struct rockchip_spi *rs; 58864e36824Saddy ke struct spi_master *master; 58964e36824Saddy ke struct resource *mem; 59076b17e6eSJulius Werner u32 rsd_nsecs; 59164e36824Saddy ke 59264e36824Saddy ke master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi)); 5935dcc44edSAddy Ke if (!master) 59464e36824Saddy ke return -ENOMEM; 5955dcc44edSAddy Ke 59664e36824Saddy ke platform_set_drvdata(pdev, master); 59764e36824Saddy ke 59864e36824Saddy ke rs = spi_master_get_devdata(master); 59964e36824Saddy ke 60064e36824Saddy ke /* Get basic io resource and map it */ 60164e36824Saddy ke mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 60264e36824Saddy ke rs->regs = devm_ioremap_resource(&pdev->dev, mem); 60364e36824Saddy ke if (IS_ERR(rs->regs)) { 60464e36824Saddy ke ret = PTR_ERR(rs->regs); 605c351587eSJeffy Chen goto err_put_master; 60664e36824Saddy ke } 60764e36824Saddy ke 60864e36824Saddy ke rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk"); 60964e36824Saddy ke if (IS_ERR(rs->apb_pclk)) { 61064e36824Saddy ke dev_err(&pdev->dev, "Failed to get apb_pclk\n"); 61164e36824Saddy ke ret = PTR_ERR(rs->apb_pclk); 612c351587eSJeffy Chen goto err_put_master; 61364e36824Saddy ke } 61464e36824Saddy ke 61564e36824Saddy ke rs->spiclk = devm_clk_get(&pdev->dev, "spiclk"); 61664e36824Saddy ke if (IS_ERR(rs->spiclk)) { 61764e36824Saddy ke dev_err(&pdev->dev, "Failed to get spi_pclk\n"); 61864e36824Saddy ke ret = PTR_ERR(rs->spiclk); 619c351587eSJeffy Chen goto err_put_master; 62064e36824Saddy ke } 62164e36824Saddy ke 62264e36824Saddy ke ret = clk_prepare_enable(rs->apb_pclk); 62343de979dSJeffy Chen if (ret < 0) { 62464e36824Saddy ke dev_err(&pdev->dev, "Failed to enable apb_pclk\n"); 625c351587eSJeffy Chen goto err_put_master; 62664e36824Saddy ke } 62764e36824Saddy ke 62864e36824Saddy ke ret = clk_prepare_enable(rs->spiclk); 62943de979dSJeffy Chen if (ret < 0) { 63064e36824Saddy ke dev_err(&pdev->dev, "Failed to enable spi_clk\n"); 631c351587eSJeffy Chen goto err_disable_apbclk; 63264e36824Saddy ke } 63364e36824Saddy ke 63430688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 63564e36824Saddy ke 63664e36824Saddy ke rs->master = master; 63764e36824Saddy ke rs->dev = &pdev->dev; 63864e36824Saddy ke rs->max_freq = clk_get_rate(rs->spiclk); 63964e36824Saddy ke 64076b17e6eSJulius Werner if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns", 64176b17e6eSJulius Werner &rsd_nsecs)) 64276b17e6eSJulius Werner rs->rsd_nsecs = rsd_nsecs; 64376b17e6eSJulius Werner 64464e36824Saddy ke rs->fifo_len = get_fifo_len(rs); 64564e36824Saddy ke if (!rs->fifo_len) { 64664e36824Saddy ke dev_err(&pdev->dev, "Failed to get fifo length\n"); 647db7e8d90SWei Yongjun ret = -EINVAL; 648c351587eSJeffy Chen goto err_disable_spiclk; 64964e36824Saddy ke } 65064e36824Saddy ke 65164e36824Saddy ke pm_runtime_set_active(&pdev->dev); 65264e36824Saddy ke pm_runtime_enable(&pdev->dev); 65364e36824Saddy ke 65464e36824Saddy ke master->auto_runtime_pm = true; 65564e36824Saddy ke master->bus_num = pdev->id; 656ee780997SAddy Ke master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP; 657aa099382SJeffy Chen master->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM; 65864e36824Saddy ke master->dev.of_node = pdev->dev.of_node; 65964e36824Saddy ke master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8); 66064e36824Saddy ke 66164e36824Saddy ke master->set_cs = rockchip_spi_set_cs; 66264e36824Saddy ke master->transfer_one = rockchip_spi_transfer_one; 6635185a81cSBrian Norris master->max_transfer_size = rockchip_spi_max_transfer_size; 6642291793cSAndy Shevchenko master->handle_err = rockchip_spi_handle_err; 665c863795cSJeffy Chen master->flags = SPI_MASTER_GPIO_SS; 66664e36824Saddy ke 667*eee06a9eSEmil Renner Berthing master->dma_tx = dma_request_chan(rs->dev, "tx"); 668*eee06a9eSEmil Renner Berthing if (IS_ERR(master->dma_tx)) { 66961cadcf4SShawn Lin /* Check tx to see if we need defer probing driver */ 670*eee06a9eSEmil Renner Berthing if (PTR_ERR(master->dma_tx) == -EPROBE_DEFER) { 67161cadcf4SShawn Lin ret = -EPROBE_DEFER; 672c351587eSJeffy Chen goto err_disable_pm_runtime; 67361cadcf4SShawn Lin } 67464e36824Saddy ke dev_warn(rs->dev, "Failed to request TX DMA channel\n"); 675*eee06a9eSEmil Renner Berthing master->dma_tx = NULL; 67664e36824Saddy ke } 677e4c0e06fSShawn Lin 678*eee06a9eSEmil Renner Berthing master->dma_rx = dma_request_chan(rs->dev, "rx"); 679*eee06a9eSEmil Renner Berthing if (IS_ERR(master->dma_rx)) { 680*eee06a9eSEmil Renner Berthing if (PTR_ERR(master->dma_rx) == -EPROBE_DEFER) { 681e4c0e06fSShawn Lin ret = -EPROBE_DEFER; 6825de7ed0cSDan Carpenter goto err_free_dma_tx; 683e4c0e06fSShawn Lin } 68464e36824Saddy ke dev_warn(rs->dev, "Failed to request RX DMA channel\n"); 685*eee06a9eSEmil Renner Berthing master->dma_rx = NULL; 68664e36824Saddy ke } 68764e36824Saddy ke 688*eee06a9eSEmil Renner Berthing if (master->dma_tx && master->dma_rx) { 689*eee06a9eSEmil Renner Berthing rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR; 690*eee06a9eSEmil Renner Berthing rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR; 69164e36824Saddy ke master->can_dma = rockchip_spi_can_dma; 69264e36824Saddy ke } 69364e36824Saddy ke 69464e36824Saddy ke ret = devm_spi_register_master(&pdev->dev, master); 69543de979dSJeffy Chen if (ret < 0) { 69664e36824Saddy ke dev_err(&pdev->dev, "Failed to register master\n"); 697c351587eSJeffy Chen goto err_free_dma_rx; 69864e36824Saddy ke } 69964e36824Saddy ke 70064e36824Saddy ke return 0; 70164e36824Saddy ke 702c351587eSJeffy Chen err_free_dma_rx: 703*eee06a9eSEmil Renner Berthing if (master->dma_rx) 704*eee06a9eSEmil Renner Berthing dma_release_channel(master->dma_rx); 7055de7ed0cSDan Carpenter err_free_dma_tx: 706*eee06a9eSEmil Renner Berthing if (master->dma_tx) 707*eee06a9eSEmil Renner Berthing dma_release_channel(master->dma_tx); 708c351587eSJeffy Chen err_disable_pm_runtime: 709c351587eSJeffy Chen pm_runtime_disable(&pdev->dev); 710c351587eSJeffy Chen err_disable_spiclk: 71164e36824Saddy ke clk_disable_unprepare(rs->spiclk); 712c351587eSJeffy Chen err_disable_apbclk: 71364e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 714c351587eSJeffy Chen err_put_master: 71564e36824Saddy ke spi_master_put(master); 71664e36824Saddy ke 71764e36824Saddy ke return ret; 71864e36824Saddy ke } 71964e36824Saddy ke 72064e36824Saddy ke static int rockchip_spi_remove(struct platform_device *pdev) 72164e36824Saddy ke { 72264e36824Saddy ke struct spi_master *master = spi_master_get(platform_get_drvdata(pdev)); 72364e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 72464e36824Saddy ke 7256a06e895SJeffy Chen pm_runtime_get_sync(&pdev->dev); 72664e36824Saddy ke 72764e36824Saddy ke clk_disable_unprepare(rs->spiclk); 72864e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 72964e36824Saddy ke 7306a06e895SJeffy Chen pm_runtime_put_noidle(&pdev->dev); 7316a06e895SJeffy Chen pm_runtime_disable(&pdev->dev); 7326a06e895SJeffy Chen pm_runtime_set_suspended(&pdev->dev); 7336a06e895SJeffy Chen 734*eee06a9eSEmil Renner Berthing if (master->dma_tx) 735*eee06a9eSEmil Renner Berthing dma_release_channel(master->dma_tx); 736*eee06a9eSEmil Renner Berthing if (master->dma_rx) 737*eee06a9eSEmil Renner Berthing dma_release_channel(master->dma_rx); 73864e36824Saddy ke 739844c9f47SShawn Lin spi_master_put(master); 740844c9f47SShawn Lin 74164e36824Saddy ke return 0; 74264e36824Saddy ke } 74364e36824Saddy ke 74464e36824Saddy ke #ifdef CONFIG_PM_SLEEP 74564e36824Saddy ke static int rockchip_spi_suspend(struct device *dev) 74664e36824Saddy ke { 74743de979dSJeffy Chen int ret; 74864e36824Saddy ke struct spi_master *master = dev_get_drvdata(dev); 74964e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 75064e36824Saddy ke 75164e36824Saddy ke ret = spi_master_suspend(rs->master); 75243de979dSJeffy Chen if (ret < 0) 75364e36824Saddy ke return ret; 75464e36824Saddy ke 755d38c4ae1SJeffy Chen ret = pm_runtime_force_suspend(dev); 756d38c4ae1SJeffy Chen if (ret < 0) 757d38c4ae1SJeffy Chen return ret; 75864e36824Saddy ke 75923e291c2SBrian Norris pinctrl_pm_select_sleep_state(dev); 76023e291c2SBrian Norris 76143de979dSJeffy Chen return 0; 76264e36824Saddy ke } 76364e36824Saddy ke 76464e36824Saddy ke static int rockchip_spi_resume(struct device *dev) 76564e36824Saddy ke { 76643de979dSJeffy Chen int ret; 76764e36824Saddy ke struct spi_master *master = dev_get_drvdata(dev); 76864e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 76964e36824Saddy ke 77023e291c2SBrian Norris pinctrl_pm_select_default_state(dev); 77123e291c2SBrian Norris 772d38c4ae1SJeffy Chen ret = pm_runtime_force_resume(dev); 77364e36824Saddy ke if (ret < 0) 77464e36824Saddy ke return ret; 77564e36824Saddy ke 77664e36824Saddy ke ret = spi_master_resume(rs->master); 77764e36824Saddy ke if (ret < 0) { 77864e36824Saddy ke clk_disable_unprepare(rs->spiclk); 77964e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 78064e36824Saddy ke } 78164e36824Saddy ke 78243de979dSJeffy Chen return 0; 78364e36824Saddy ke } 78464e36824Saddy ke #endif /* CONFIG_PM_SLEEP */ 78564e36824Saddy ke 786ec833050SRafael J. Wysocki #ifdef CONFIG_PM 78764e36824Saddy ke static int rockchip_spi_runtime_suspend(struct device *dev) 78864e36824Saddy ke { 78964e36824Saddy ke struct spi_master *master = dev_get_drvdata(dev); 79064e36824Saddy ke struct rockchip_spi *rs = spi_master_get_devdata(master); 79164e36824Saddy ke 79264e36824Saddy ke clk_disable_unprepare(rs->spiclk); 79364e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 79464e36824Saddy ke 79564e36824Saddy ke return 0; 79664e36824Saddy ke } 79764e36824Saddy ke 79864e36824Saddy ke static int rockchip_spi_runtime_resume(struct device *dev) 79964e36824Saddy ke { 80064e36824Saddy ke int ret; 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 ret = clk_prepare_enable(rs->apb_pclk); 80543de979dSJeffy Chen if (ret < 0) 80664e36824Saddy ke return ret; 80764e36824Saddy ke 80864e36824Saddy ke ret = clk_prepare_enable(rs->spiclk); 80943de979dSJeffy Chen if (ret < 0) 81064e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 81164e36824Saddy ke 81243de979dSJeffy Chen return 0; 81364e36824Saddy ke } 814ec833050SRafael J. Wysocki #endif /* CONFIG_PM */ 81564e36824Saddy ke 81664e36824Saddy ke static const struct dev_pm_ops rockchip_spi_pm = { 81764e36824Saddy ke SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume) 81864e36824Saddy ke SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend, 81964e36824Saddy ke rockchip_spi_runtime_resume, NULL) 82064e36824Saddy ke }; 82164e36824Saddy ke 82264e36824Saddy ke static const struct of_device_id rockchip_spi_dt_match[] = { 8236b860e69SAndy Yan { .compatible = "rockchip,rv1108-spi", }, 824aa29ea3dSCaesar Wang { .compatible = "rockchip,rk3036-spi", }, 82564e36824Saddy ke { .compatible = "rockchip,rk3066-spi", }, 826b839b785SAddy Ke { .compatible = "rockchip,rk3188-spi", }, 827aa29ea3dSCaesar Wang { .compatible = "rockchip,rk3228-spi", }, 828b839b785SAddy Ke { .compatible = "rockchip,rk3288-spi", }, 829aa29ea3dSCaesar Wang { .compatible = "rockchip,rk3368-spi", }, 8309b7a5622SXu Jianqun { .compatible = "rockchip,rk3399-spi", }, 83164e36824Saddy ke { }, 83264e36824Saddy ke }; 83364e36824Saddy ke MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match); 83464e36824Saddy ke 83564e36824Saddy ke static struct platform_driver rockchip_spi_driver = { 83664e36824Saddy ke .driver = { 83764e36824Saddy ke .name = DRIVER_NAME, 83864e36824Saddy ke .pm = &rockchip_spi_pm, 83964e36824Saddy ke .of_match_table = of_match_ptr(rockchip_spi_dt_match), 84064e36824Saddy ke }, 84164e36824Saddy ke .probe = rockchip_spi_probe, 84264e36824Saddy ke .remove = rockchip_spi_remove, 84364e36824Saddy ke }; 84464e36824Saddy ke 84564e36824Saddy ke module_platform_driver(rockchip_spi_driver); 84664e36824Saddy ke 8475dcc44edSAddy Ke MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>"); 84864e36824Saddy ke MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver"); 84964e36824Saddy ke MODULE_LICENSE("GPL v2"); 850