blob: 2cc6d9951b52e7c994ce3bb676b4c2a4d68e10b9 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
addy ke64e36822014-07-01 09:03:59 +08002/*
3 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
Addy Ke5dcc44e2014-07-11 10:07:56 +08004 * Author: Addy Ke <addy.ke@rock-chips.com>
addy ke64e36822014-07-01 09:03:59 +08005 */
6
addy ke64e36822014-07-01 09:03:59 +08007#include <linux/clk.h>
addy ke64e36822014-07-01 09:03:59 +08008#include <linux/dmaengine.h>
Suren Baghdasaryan8af0c182019-05-14 15:41:12 -07009#include <linux/interrupt.h>
Shawn Linec5c5d82016-03-10 14:51:48 +080010#include <linux/module.h>
11#include <linux/of.h>
Brian Norris23e291c2016-12-16 16:59:16 -080012#include <linux/pinctrl/consumer.h>
Shawn Linec5c5d82016-03-10 14:51:48 +080013#include <linux/platform_device.h>
14#include <linux/spi/spi.h>
15#include <linux/pm_runtime.h>
16#include <linux/scatterlist.h>
addy ke64e36822014-07-01 09:03:59 +080017
18#define DRIVER_NAME "rockchip-spi"
19
Jeffy Chenaa099382017-06-28 12:38:43 +080020#define ROCKCHIP_SPI_CLR_BITS(reg, bits) \
21 writel_relaxed(readl_relaxed(reg) & ~(bits), reg)
22#define ROCKCHIP_SPI_SET_BITS(reg, bits) \
23 writel_relaxed(readl_relaxed(reg) | (bits), reg)
24
addy ke64e36822014-07-01 09:03:59 +080025/* SPI register offsets */
26#define ROCKCHIP_SPI_CTRLR0 0x0000
27#define ROCKCHIP_SPI_CTRLR1 0x0004
28#define ROCKCHIP_SPI_SSIENR 0x0008
29#define ROCKCHIP_SPI_SER 0x000c
30#define ROCKCHIP_SPI_BAUDR 0x0010
31#define ROCKCHIP_SPI_TXFTLR 0x0014
32#define ROCKCHIP_SPI_RXFTLR 0x0018
33#define ROCKCHIP_SPI_TXFLR 0x001c
34#define ROCKCHIP_SPI_RXFLR 0x0020
35#define ROCKCHIP_SPI_SR 0x0024
36#define ROCKCHIP_SPI_IPR 0x0028
37#define ROCKCHIP_SPI_IMR 0x002c
38#define ROCKCHIP_SPI_ISR 0x0030
39#define ROCKCHIP_SPI_RISR 0x0034
40#define ROCKCHIP_SPI_ICR 0x0038
41#define ROCKCHIP_SPI_DMACR 0x003c
42#define ROCKCHIP_SPI_DMATDLR 0x0040
43#define ROCKCHIP_SPI_DMARDLR 0x0044
44#define ROCKCHIP_SPI_TXDR 0x0400
45#define ROCKCHIP_SPI_RXDR 0x0800
46
47/* Bit fields in CTRLR0 */
48#define CR0_DFS_OFFSET 0
Emil Renner Berthing65498c62018-10-31 11:57:10 +010049#define CR0_DFS_4BIT 0x0
50#define CR0_DFS_8BIT 0x1
51#define CR0_DFS_16BIT 0x2
addy ke64e36822014-07-01 09:03:59 +080052
53#define CR0_CFS_OFFSET 2
54
55#define CR0_SCPH_OFFSET 6
56
57#define CR0_SCPOL_OFFSET 7
58
59#define CR0_CSM_OFFSET 8
60#define CR0_CSM_KEEP 0x0
61/* ss_n be high for half sclk_out cycles */
62#define CR0_CSM_HALF 0X1
63/* ss_n be high for one sclk_out cycle */
64#define CR0_CSM_ONE 0x2
65
66/* ss_n to sclk_out delay */
67#define CR0_SSD_OFFSET 10
68/*
69 * The period between ss_n active and
70 * sclk_out active is half sclk_out cycles
71 */
72#define CR0_SSD_HALF 0x0
73/*
74 * The period between ss_n active and
75 * sclk_out active is one sclk_out cycle
76 */
77#define CR0_SSD_ONE 0x1
78
79#define CR0_EM_OFFSET 11
80#define CR0_EM_LITTLE 0x0
81#define CR0_EM_BIG 0x1
82
83#define CR0_FBM_OFFSET 12
84#define CR0_FBM_MSB 0x0
85#define CR0_FBM_LSB 0x1
86
87#define CR0_BHT_OFFSET 13
88#define CR0_BHT_16BIT 0x0
89#define CR0_BHT_8BIT 0x1
90
91#define CR0_RSD_OFFSET 14
Emil Renner Berthing74b7efa2018-10-31 11:57:08 +010092#define CR0_RSD_MAX 0x3
addy ke64e36822014-07-01 09:03:59 +080093
94#define CR0_FRF_OFFSET 16
95#define CR0_FRF_SPI 0x0
96#define CR0_FRF_SSP 0x1
97#define CR0_FRF_MICROWIRE 0x2
98
99#define CR0_XFM_OFFSET 18
100#define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET)
101#define CR0_XFM_TR 0x0
102#define CR0_XFM_TO 0x1
103#define CR0_XFM_RO 0x2
104
105#define CR0_OPM_OFFSET 20
106#define CR0_OPM_MASTER 0x0
107#define CR0_OPM_SLAVE 0x1
108
109#define CR0_MTM_OFFSET 0x21
110
111/* Bit fields in SER, 2bit */
112#define SER_MASK 0x3
113
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100114/* Bit fields in BAUDR */
115#define BAUDR_SCKDV_MIN 2
116#define BAUDR_SCKDV_MAX 65534
117
addy ke64e36822014-07-01 09:03:59 +0800118/* Bit fields in SR, 5bit */
119#define SR_MASK 0x1f
120#define SR_BUSY (1 << 0)
121#define SR_TF_FULL (1 << 1)
122#define SR_TF_EMPTY (1 << 2)
123#define SR_RF_EMPTY (1 << 3)
124#define SR_RF_FULL (1 << 4)
125
126/* Bit fields in ISR, IMR, ISR, RISR, 5bit */
127#define INT_MASK 0x1f
128#define INT_TF_EMPTY (1 << 0)
129#define INT_TF_OVERFLOW (1 << 1)
130#define INT_RF_UNDERFLOW (1 << 2)
131#define INT_RF_OVERFLOW (1 << 3)
132#define INT_RF_FULL (1 << 4)
133
134/* Bit fields in ICR, 4bit */
135#define ICR_MASK 0x0f
136#define ICR_ALL (1 << 0)
137#define ICR_RF_UNDERFLOW (1 << 1)
138#define ICR_RF_OVERFLOW (1 << 2)
139#define ICR_TF_OVERFLOW (1 << 3)
140
141/* Bit fields in DMACR */
142#define RF_DMA_EN (1 << 0)
143#define TF_DMA_EN (1 << 1)
144
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100145/* Driver state flags */
146#define RXDMA (1 << 0)
147#define TXDMA (1 << 1)
addy ke64e36822014-07-01 09:03:59 +0800148
Addy Kef9cfd522014-10-15 19:25:49 +0800149/* sclk_out: spi master internal logic in rk3x can support 50Mhz */
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100150#define MAX_SCLK_OUT 50000000U
Addy Kef9cfd522014-10-15 19:25:49 +0800151
Brian Norris5185a812016-07-14 18:30:59 -0700152/*
153 * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However,
154 * the controller seems to hang when given 0x10000, so stick with this for now.
155 */
156#define ROCKCHIP_SPI_MAX_TRANLEN 0xffff
157
Jeffy Chenaa099382017-06-28 12:38:43 +0800158#define ROCKCHIP_SPI_MAX_CS_NUM 2
159
addy ke64e36822014-07-01 09:03:59 +0800160struct rockchip_spi {
161 struct device *dev;
addy ke64e36822014-07-01 09:03:59 +0800162
163 struct clk *spiclk;
164 struct clk *apb_pclk;
165
166 void __iomem *regs;
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100167 dma_addr_t dma_addr_rx;
168 dma_addr_t dma_addr_tx;
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100169
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100170 const void *tx;
171 void *rx;
172 unsigned int tx_left;
173 unsigned int rx_left;
174
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100175 atomic_t state;
176
addy ke64e36822014-07-01 09:03:59 +0800177 /*depth of the FIFO buffer */
178 u32 fifo_len;
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100179 /* frequency of spiclk */
180 u32 freq;
addy ke64e36822014-07-01 09:03:59 +0800181
addy ke64e36822014-07-01 09:03:59 +0800182 u8 n_bytes;
Emil Renner Berthing74b7efa2018-10-31 11:57:08 +0100183 u8 rsd;
addy ke64e36822014-07-01 09:03:59 +0800184
Jeffy Chenaa099382017-06-28 12:38:43 +0800185 bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
addy ke64e36822014-07-01 09:03:59 +0800186};
187
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100188static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
addy ke64e36822014-07-01 09:03:59 +0800189{
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100190 writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR);
addy ke64e36822014-07-01 09:03:59 +0800191}
192
Addy Ke2df08e72014-07-11 10:08:24 +0800193static inline void wait_for_idle(struct rockchip_spi *rs)
194{
195 unsigned long timeout = jiffies + msecs_to_jiffies(5);
196
197 do {
198 if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
199 return;
Doug Anderson64bc0112014-09-03 13:44:25 -0700200 } while (!time_after(jiffies, timeout));
Addy Ke2df08e72014-07-11 10:08:24 +0800201
202 dev_warn(rs->dev, "spi controller is in busy state!\n");
203}
204
addy ke64e36822014-07-01 09:03:59 +0800205static u32 get_fifo_len(struct rockchip_spi *rs)
206{
207 u32 fifo;
208
209 for (fifo = 2; fifo < 32; fifo++) {
210 writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR);
211 if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR))
212 break;
213 }
214
215 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR);
216
217 return (fifo == 31) ? 0 : fifo;
218}
219
addy ke64e36822014-07-01 09:03:59 +0800220static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
221{
Huibin Hongb920cc32016-02-24 18:00:04 +0800222 struct spi_master *master = spi->master;
223 struct rockchip_spi *rs = spi_master_get_devdata(master);
Jeffy Chenaa099382017-06-28 12:38:43 +0800224 bool cs_asserted = !enable;
Huibin Hongb920cc32016-02-24 18:00:04 +0800225
Jeffy Chenaa099382017-06-28 12:38:43 +0800226 /* Return immediately for no-op */
227 if (cs_asserted == rs->cs_asserted[spi->chip_select])
228 return;
addy ke64e36822014-07-01 09:03:59 +0800229
Jeffy Chenaa099382017-06-28 12:38:43 +0800230 if (cs_asserted) {
231 /* Keep things powered as long as CS is asserted */
232 pm_runtime_get_sync(rs->dev);
addy ke64e36822014-07-01 09:03:59 +0800233
Jeffy Chenaa099382017-06-28 12:38:43 +0800234 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER,
235 BIT(spi->chip_select));
236 } else {
237 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER,
238 BIT(spi->chip_select));
addy ke64e36822014-07-01 09:03:59 +0800239
Jeffy Chenaa099382017-06-28 12:38:43 +0800240 /* Drop reference from when we first asserted CS */
241 pm_runtime_put(rs->dev);
242 }
Huibin Hongb920cc32016-02-24 18:00:04 +0800243
Jeffy Chenaa099382017-06-28 12:38:43 +0800244 rs->cs_asserted[spi->chip_select] = cs_asserted;
addy ke64e36822014-07-01 09:03:59 +0800245}
246
Andy Shevchenko22917932015-02-27 17:34:16 +0200247static void rockchip_spi_handle_err(struct spi_master *master,
248 struct spi_message *msg)
addy ke64e36822014-07-01 09:03:59 +0800249{
addy ke64e36822014-07-01 09:03:59 +0800250 struct rockchip_spi *rs = spi_master_get_devdata(master);
251
Emil Renner Berthingce386102018-10-31 11:57:02 +0100252 /* stop running spi transfer
253 * this also flushes both rx and tx fifos
Addy Ke5dcc44e2014-07-11 10:07:56 +0800254 */
Emil Renner Berthingce386102018-10-31 11:57:02 +0100255 spi_enable_chip(rs, false);
256
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100257 /* make sure all interrupts are masked */
258 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
259
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100260 if (atomic_read(&rs->state) & TXDMA)
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100261 dmaengine_terminate_async(master->dma_tx);
addy ke64e36822014-07-01 09:03:59 +0800262
Emil Renner Berthingce386102018-10-31 11:57:02 +0100263 if (atomic_read(&rs->state) & RXDMA)
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100264 dmaengine_terminate_async(master->dma_rx);
addy ke64e36822014-07-01 09:03:59 +0800265}
266
267static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
268{
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100269 u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
270 u32 words = min(rs->tx_left, tx_free);
addy ke64e36822014-07-01 09:03:59 +0800271
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100272 rs->tx_left -= words;
273 for (; words; words--) {
274 u32 txw;
275
addy ke64e36822014-07-01 09:03:59 +0800276 if (rs->n_bytes == 1)
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100277 txw = *(u8 *)rs->tx;
addy ke64e36822014-07-01 09:03:59 +0800278 else
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100279 txw = *(u16 *)rs->tx;
addy ke64e36822014-07-01 09:03:59 +0800280
281 writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
282 rs->tx += rs->n_bytes;
283 }
284}
285
286static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
287{
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100288 u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
289 u32 rx_left = rs->rx_left - words;
addy ke64e36822014-07-01 09:03:59 +0800290
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100291 /* the hardware doesn't allow us to change fifo threshold
292 * level while spi is enabled, so instead make sure to leave
293 * enough words in the rx fifo to get the last interrupt
294 * exactly when all words have been received
295 */
296 if (rx_left) {
297 u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1;
298
299 if (rx_left < ftl) {
300 rx_left = ftl;
301 words = rs->rx_left - rx_left;
302 }
303 }
304
305 rs->rx_left = rx_left;
306 for (; words; words--) {
307 u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
308
309 if (!rs->rx)
310 continue;
311
addy ke64e36822014-07-01 09:03:59 +0800312 if (rs->n_bytes == 1)
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100313 *(u8 *)rs->rx = (u8)rxw;
addy ke64e36822014-07-01 09:03:59 +0800314 else
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100315 *(u16 *)rs->rx = (u16)rxw;
addy ke64e36822014-07-01 09:03:59 +0800316 rs->rx += rs->n_bytes;
Addy Ke5dcc44e2014-07-11 10:07:56 +0800317 }
addy ke64e36822014-07-01 09:03:59 +0800318}
319
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100320static irqreturn_t rockchip_spi_isr(int irq, void *dev_id)
addy ke64e36822014-07-01 09:03:59 +0800321{
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100322 struct spi_master *master = dev_id;
323 struct rockchip_spi *rs = spi_master_get_devdata(master);
addy ke64e36822014-07-01 09:03:59 +0800324
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100325 if (rs->tx_left)
326 rockchip_spi_pio_writer(rs);
327
328 rockchip_spi_pio_reader(rs);
329 if (!rs->rx_left) {
330 spi_enable_chip(rs, false);
331 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
332 spi_finalize_current_transfer(master);
333 }
334
335 return IRQ_HANDLED;
336}
337
338static int rockchip_spi_prepare_irq(struct rockchip_spi *rs,
339 struct spi_transfer *xfer)
340{
341 rs->tx = xfer->tx_buf;
342 rs->rx = xfer->rx_buf;
343 rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0;
344 rs->rx_left = xfer->len / rs->n_bytes;
345
346 writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR);
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100347 spi_enable_chip(rs, true);
Emil Renner Berthinga3c17402018-10-10 11:00:38 +0200348
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100349 if (rs->tx_left)
350 rockchip_spi_pio_writer(rs);
addy ke64e36822014-07-01 09:03:59 +0800351
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100352 /* 1 means the transfer is in progress */
353 return 1;
addy ke64e36822014-07-01 09:03:59 +0800354}
355
356static void rockchip_spi_dma_rxcb(void *data)
357{
Emil Renner Berthingd790c342018-10-31 11:57:05 +0100358 struct spi_master *master = data;
359 struct rockchip_spi *rs = spi_master_get_devdata(master);
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100360 int state = atomic_fetch_andnot(RXDMA, &rs->state);
addy ke64e36822014-07-01 09:03:59 +0800361
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100362 if (state & TXDMA)
363 return;
addy ke64e36822014-07-01 09:03:59 +0800364
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100365 spi_enable_chip(rs, false);
Emil Renner Berthingd790c342018-10-31 11:57:05 +0100366 spi_finalize_current_transfer(master);
addy ke64e36822014-07-01 09:03:59 +0800367}
368
369static void rockchip_spi_dma_txcb(void *data)
370{
Emil Renner Berthingd790c342018-10-31 11:57:05 +0100371 struct spi_master *master = data;
372 struct rockchip_spi *rs = spi_master_get_devdata(master);
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100373 int state = atomic_fetch_andnot(TXDMA, &rs->state);
374
375 if (state & RXDMA)
376 return;
addy ke64e36822014-07-01 09:03:59 +0800377
Addy Ke2df08e72014-07-11 10:08:24 +0800378 /* Wait until the FIFO data completely. */
379 wait_for_idle(rs);
380
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100381 spi_enable_chip(rs, false);
Emil Renner Berthingd790c342018-10-31 11:57:05 +0100382 spi_finalize_current_transfer(master);
addy ke64e36822014-07-01 09:03:59 +0800383}
384
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100385static int rockchip_spi_prepare_dma(struct rockchip_spi *rs,
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100386 struct spi_master *master, struct spi_transfer *xfer)
addy ke64e36822014-07-01 09:03:59 +0800387{
addy ke64e36822014-07-01 09:03:59 +0800388 struct dma_async_tx_descriptor *rxdesc, *txdesc;
389
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100390 atomic_set(&rs->state, 0);
addy ke64e36822014-07-01 09:03:59 +0800391
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100392 rxdesc = NULL;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100393 if (xfer->rx_buf) {
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100394 struct dma_slave_config rxconf = {
395 .direction = DMA_DEV_TO_MEM,
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100396 .src_addr = rs->dma_addr_rx,
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100397 .src_addr_width = rs->n_bytes,
398 .src_maxburst = 1,
399 };
400
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100401 dmaengine_slave_config(master->dma_rx, &rxconf);
addy ke64e36822014-07-01 09:03:59 +0800402
Addy Ke5dcc44e2014-07-11 10:07:56 +0800403 rxdesc = dmaengine_prep_slave_sg(
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100404 master->dma_rx,
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100405 xfer->rx_sg.sgl, xfer->rx_sg.nents,
Emil Renner Berthingd9071b72018-10-10 11:00:37 +0200406 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
Shawn Linea984912016-03-09 16:11:15 +0800407 if (!rxdesc)
408 return -EINVAL;
addy ke64e36822014-07-01 09:03:59 +0800409
410 rxdesc->callback = rockchip_spi_dma_rxcb;
Emil Renner Berthingd790c342018-10-31 11:57:05 +0100411 rxdesc->callback_param = master;
addy ke64e36822014-07-01 09:03:59 +0800412 }
413
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100414 txdesc = NULL;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100415 if (xfer->tx_buf) {
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100416 struct dma_slave_config txconf = {
417 .direction = DMA_MEM_TO_DEV,
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100418 .dst_addr = rs->dma_addr_tx,
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100419 .dst_addr_width = rs->n_bytes,
Emil Renner Berthing47300722019-04-12 12:53:20 +0200420 .dst_maxburst = rs->fifo_len / 4,
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100421 };
422
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100423 dmaengine_slave_config(master->dma_tx, &txconf);
addy ke64e36822014-07-01 09:03:59 +0800424
Addy Ke5dcc44e2014-07-11 10:07:56 +0800425 txdesc = dmaengine_prep_slave_sg(
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100426 master->dma_tx,
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100427 xfer->tx_sg.sgl, xfer->tx_sg.nents,
Emil Renner Berthingd9071b72018-10-10 11:00:37 +0200428 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
Shawn Linea984912016-03-09 16:11:15 +0800429 if (!txdesc) {
430 if (rxdesc)
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100431 dmaengine_terminate_sync(master->dma_rx);
Shawn Linea984912016-03-09 16:11:15 +0800432 return -EINVAL;
433 }
addy ke64e36822014-07-01 09:03:59 +0800434
435 txdesc->callback = rockchip_spi_dma_txcb;
Emil Renner Berthingd790c342018-10-31 11:57:05 +0100436 txdesc->callback_param = master;
addy ke64e36822014-07-01 09:03:59 +0800437 }
438
439 /* rx must be started before tx due to spi instinct */
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100440 if (rxdesc) {
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100441 atomic_or(RXDMA, &rs->state);
addy ke64e36822014-07-01 09:03:59 +0800442 dmaengine_submit(rxdesc);
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100443 dma_async_issue_pending(master->dma_rx);
addy ke64e36822014-07-01 09:03:59 +0800444 }
445
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100446 spi_enable_chip(rs, true);
Emil Renner Berthinga3c17402018-10-10 11:00:38 +0200447
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100448 if (txdesc) {
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100449 atomic_or(TXDMA, &rs->state);
addy ke64e36822014-07-01 09:03:59 +0800450 dmaengine_submit(txdesc);
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100451 dma_async_issue_pending(master->dma_tx);
addy ke64e36822014-07-01 09:03:59 +0800452 }
Shawn Linea984912016-03-09 16:11:15 +0800453
Emil Renner Berthinga3c17402018-10-10 11:00:38 +0200454 /* 1 means the transfer is in progress */
455 return 1;
addy ke64e36822014-07-01 09:03:59 +0800456}
457
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100458static void rockchip_spi_config(struct rockchip_spi *rs,
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100459 struct spi_device *spi, struct spi_transfer *xfer,
460 bool use_dma)
addy ke64e36822014-07-01 09:03:59 +0800461{
Emil Renner Berthing2410d6a2018-10-31 11:57:00 +0100462 u32 cr0 = CR0_FRF_SPI << CR0_FRF_OFFSET
463 | CR0_BHT_8BIT << CR0_BHT_OFFSET
464 | CR0_SSD_ONE << CR0_SSD_OFFSET
465 | CR0_EM_BIG << CR0_EM_OFFSET;
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100466 u32 cr1;
467 u32 dmacr = 0;
addy ke64e36822014-07-01 09:03:59 +0800468
Emil Renner Berthing74b7efa2018-10-31 11:57:08 +0100469 cr0 |= rs->rsd << CR0_RSD_OFFSET;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100470 cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET;
Emil Renner Berthing04290192018-10-31 11:57:11 +0100471 if (spi->mode & SPI_LSB_FIRST)
472 cr0 |= CR0_FBM_LSB << CR0_FBM_OFFSET;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100473
474 if (xfer->rx_buf && xfer->tx_buf)
475 cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET;
476 else if (xfer->rx_buf)
477 cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET;
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100478 else if (use_dma)
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100479 cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET;
addy ke64e36822014-07-01 09:03:59 +0800480
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100481 switch (xfer->bits_per_word) {
482 case 4:
483 cr0 |= CR0_DFS_4BIT << CR0_DFS_OFFSET;
484 cr1 = xfer->len - 1;
485 break;
486 case 8:
487 cr0 |= CR0_DFS_8BIT << CR0_DFS_OFFSET;
488 cr1 = xfer->len - 1;
489 break;
490 case 16:
491 cr0 |= CR0_DFS_16BIT << CR0_DFS_OFFSET;
492 cr1 = xfer->len / 2 - 1;
493 break;
494 default:
495 /* we only whitelist 4, 8 and 16 bit words in
496 * master->bits_per_word_mask, so this shouldn't
497 * happen
498 */
499 unreachable();
500 }
501
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100502 if (use_dma) {
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100503 if (xfer->tx_buf)
addy ke64e36822014-07-01 09:03:59 +0800504 dmacr |= TF_DMA_EN;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100505 if (xfer->rx_buf)
addy ke64e36822014-07-01 09:03:59 +0800506 dmacr |= RF_DMA_EN;
507 }
508
addy ke64e36822014-07-01 09:03:59 +0800509 writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100510 writel_relaxed(cr1, rs->regs + ROCKCHIP_SPI_CTRLR1);
Huibin Hong04b37d22017-08-16 10:12:02 +0800511
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100512 /* unfortunately setting the fifo threshold level to generate an
513 * interrupt exactly when the fifo is full doesn't seem to work,
514 * so we need the strict inequality here
515 */
516 if (xfer->len < rs->fifo_len)
517 writel_relaxed(xfer->len - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
518 else
519 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
addy ke64e36822014-07-01 09:03:59 +0800520
Emil Renner Berthing47300722019-04-12 12:53:20 +0200521 writel_relaxed(rs->fifo_len / 2, rs->regs + ROCKCHIP_SPI_DMATDLR);
addy ke64e36822014-07-01 09:03:59 +0800522 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR);
523 writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
524
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100525 /* the hardware only supports an even clock divisor, so
526 * round divisor = spiclk / speed up to nearest even number
527 * so that the resulting speed is <= the requested speed
528 */
529 writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz),
530 rs->regs + ROCKCHIP_SPI_BAUDR);
addy ke64e36822014-07-01 09:03:59 +0800531}
532
Brian Norris5185a812016-07-14 18:30:59 -0700533static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
534{
535 return ROCKCHIP_SPI_MAX_TRANLEN;
536}
537
Addy Ke5dcc44e2014-07-11 10:07:56 +0800538static int rockchip_spi_transfer_one(
539 struct spi_master *master,
addy ke64e36822014-07-01 09:03:59 +0800540 struct spi_device *spi,
541 struct spi_transfer *xfer)
542{
addy ke64e36822014-07-01 09:03:59 +0800543 struct rockchip_spi *rs = spi_master_get_devdata(master);
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100544 bool use_dma;
addy ke64e36822014-07-01 09:03:59 +0800545
Doug Anderson62946172014-09-03 13:44:26 -0700546 WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
547 (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
addy ke64e36822014-07-01 09:03:59 +0800548
549 if (!xfer->tx_buf && !xfer->rx_buf) {
550 dev_err(rs->dev, "No buffer for transfer\n");
551 return -EINVAL;
552 }
553
Brian Norris5185a812016-07-14 18:30:59 -0700554 if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
555 dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
556 return -EINVAL;
557 }
558
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100559 rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2;
addy ke64e36822014-07-01 09:03:59 +0800560
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100561 use_dma = master->can_dma ? master->can_dma(master, spi, xfer) : false;
addy ke64e36822014-07-01 09:03:59 +0800562
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100563 rockchip_spi_config(rs, spi, xfer, use_dma);
addy ke64e36822014-07-01 09:03:59 +0800564
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100565 if (use_dma)
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100566 return rockchip_spi_prepare_dma(rs, master, xfer);
addy ke64e36822014-07-01 09:03:59 +0800567
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100568 return rockchip_spi_prepare_irq(rs, xfer);
addy ke64e36822014-07-01 09:03:59 +0800569}
570
571static bool rockchip_spi_can_dma(struct spi_master *master,
Addy Ke5dcc44e2014-07-11 10:07:56 +0800572 struct spi_device *spi,
573 struct spi_transfer *xfer)
addy ke64e36822014-07-01 09:03:59 +0800574{
575 struct rockchip_spi *rs = spi_master_get_devdata(master);
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100576 unsigned int bytes_per_word = xfer->bits_per_word <= 8 ? 1 : 2;
addy ke64e36822014-07-01 09:03:59 +0800577
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100578 /* if the numbor of spi words to transfer is less than the fifo
579 * length we can just fill the fifo and wait for a single irq,
580 * so don't bother setting up dma
581 */
582 return xfer->len / bytes_per_word >= rs->fifo_len;
addy ke64e36822014-07-01 09:03:59 +0800583}
584
585static int rockchip_spi_probe(struct platform_device *pdev)
586{
Jeffy Chen43de9792017-08-07 20:40:18 +0800587 int ret;
addy ke64e36822014-07-01 09:03:59 +0800588 struct rockchip_spi *rs;
589 struct spi_master *master;
590 struct resource *mem;
Julius Werner76b17e62015-03-26 16:30:25 -0700591 u32 rsd_nsecs;
addy ke64e36822014-07-01 09:03:59 +0800592
593 master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi));
Addy Ke5dcc44e2014-07-11 10:07:56 +0800594 if (!master)
addy ke64e36822014-07-01 09:03:59 +0800595 return -ENOMEM;
Addy Ke5dcc44e2014-07-11 10:07:56 +0800596
addy ke64e36822014-07-01 09:03:59 +0800597 platform_set_drvdata(pdev, master);
598
599 rs = spi_master_get_devdata(master);
addy ke64e36822014-07-01 09:03:59 +0800600
601 /* Get basic io resource and map it */
602 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
603 rs->regs = devm_ioremap_resource(&pdev->dev, mem);
604 if (IS_ERR(rs->regs)) {
addy ke64e36822014-07-01 09:03:59 +0800605 ret = PTR_ERR(rs->regs);
Jeffy Chenc3515872017-06-13 13:25:40 +0800606 goto err_put_master;
addy ke64e36822014-07-01 09:03:59 +0800607 }
608
609 rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
610 if (IS_ERR(rs->apb_pclk)) {
611 dev_err(&pdev->dev, "Failed to get apb_pclk\n");
612 ret = PTR_ERR(rs->apb_pclk);
Jeffy Chenc3515872017-06-13 13:25:40 +0800613 goto err_put_master;
addy ke64e36822014-07-01 09:03:59 +0800614 }
615
616 rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
617 if (IS_ERR(rs->spiclk)) {
618 dev_err(&pdev->dev, "Failed to get spi_pclk\n");
619 ret = PTR_ERR(rs->spiclk);
Jeffy Chenc3515872017-06-13 13:25:40 +0800620 goto err_put_master;
addy ke64e36822014-07-01 09:03:59 +0800621 }
622
623 ret = clk_prepare_enable(rs->apb_pclk);
Jeffy Chen43de9792017-08-07 20:40:18 +0800624 if (ret < 0) {
addy ke64e36822014-07-01 09:03:59 +0800625 dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
Jeffy Chenc3515872017-06-13 13:25:40 +0800626 goto err_put_master;
addy ke64e36822014-07-01 09:03:59 +0800627 }
628
629 ret = clk_prepare_enable(rs->spiclk);
Jeffy Chen43de9792017-08-07 20:40:18 +0800630 if (ret < 0) {
addy ke64e36822014-07-01 09:03:59 +0800631 dev_err(&pdev->dev, "Failed to enable spi_clk\n");
Jeffy Chenc3515872017-06-13 13:25:40 +0800632 goto err_disable_apbclk;
addy ke64e36822014-07-01 09:03:59 +0800633 }
634
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100635 spi_enable_chip(rs, false);
addy ke64e36822014-07-01 09:03:59 +0800636
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100637 ret = platform_get_irq(pdev, 0);
638 if (ret < 0)
639 goto err_disable_spiclk;
640
641 ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL,
642 IRQF_ONESHOT, dev_name(&pdev->dev), master);
643 if (ret)
644 goto err_disable_spiclk;
645
addy ke64e36822014-07-01 09:03:59 +0800646 rs->dev = &pdev->dev;
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100647 rs->freq = clk_get_rate(rs->spiclk);
addy ke64e36822014-07-01 09:03:59 +0800648
Julius Werner76b17e62015-03-26 16:30:25 -0700649 if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
Emil Renner Berthing74b7efa2018-10-31 11:57:08 +0100650 &rsd_nsecs)) {
651 /* rx sample delay is expressed in parent clock cycles (max 3) */
652 u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8),
653 1000000000 >> 8);
654 if (!rsd) {
655 dev_warn(rs->dev, "%u Hz are too slow to express %u ns delay\n",
656 rs->freq, rsd_nsecs);
657 } else if (rsd > CR0_RSD_MAX) {
658 rsd = CR0_RSD_MAX;
659 dev_warn(rs->dev, "%u Hz are too fast to express %u ns delay, clamping at %u ns\n",
660 rs->freq, rsd_nsecs,
661 CR0_RSD_MAX * 1000000000U / rs->freq);
662 }
663 rs->rsd = rsd;
664 }
Julius Werner76b17e62015-03-26 16:30:25 -0700665
addy ke64e36822014-07-01 09:03:59 +0800666 rs->fifo_len = get_fifo_len(rs);
667 if (!rs->fifo_len) {
668 dev_err(&pdev->dev, "Failed to get fifo length\n");
Wei Yongjundb7e8d92014-07-20 22:02:04 +0800669 ret = -EINVAL;
Jeffy Chenc3515872017-06-13 13:25:40 +0800670 goto err_disable_spiclk;
addy ke64e36822014-07-01 09:03:59 +0800671 }
672
addy ke64e36822014-07-01 09:03:59 +0800673 pm_runtime_set_active(&pdev->dev);
674 pm_runtime_enable(&pdev->dev);
675
676 master->auto_runtime_pm = true;
677 master->bus_num = pdev->id;
Emil Renner Berthing04290192018-10-31 11:57:11 +0100678 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST;
Jeffy Chenaa099382017-06-28 12:38:43 +0800679 master->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM;
addy ke64e36822014-07-01 09:03:59 +0800680 master->dev.of_node = pdev->dev.of_node;
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100681 master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4);
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100682 master->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX;
683 master->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT);
addy ke64e36822014-07-01 09:03:59 +0800684
685 master->set_cs = rockchip_spi_set_cs;
addy ke64e36822014-07-01 09:03:59 +0800686 master->transfer_one = rockchip_spi_transfer_one;
Brian Norris5185a812016-07-14 18:30:59 -0700687 master->max_transfer_size = rockchip_spi_max_transfer_size;
Andy Shevchenko22917932015-02-27 17:34:16 +0200688 master->handle_err = rockchip_spi_handle_err;
Jeffy Chenc8637952017-06-28 12:38:42 +0800689 master->flags = SPI_MASTER_GPIO_SS;
addy ke64e36822014-07-01 09:03:59 +0800690
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100691 master->dma_tx = dma_request_chan(rs->dev, "tx");
692 if (IS_ERR(master->dma_tx)) {
Shawn Lin61cadcf2016-03-09 16:11:32 +0800693 /* Check tx to see if we need defer probing driver */
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100694 if (PTR_ERR(master->dma_tx) == -EPROBE_DEFER) {
Shawn Lin61cadcf2016-03-09 16:11:32 +0800695 ret = -EPROBE_DEFER;
Jeffy Chenc3515872017-06-13 13:25:40 +0800696 goto err_disable_pm_runtime;
Shawn Lin61cadcf2016-03-09 16:11:32 +0800697 }
addy ke64e36822014-07-01 09:03:59 +0800698 dev_warn(rs->dev, "Failed to request TX DMA channel\n");
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100699 master->dma_tx = NULL;
Shawn Lin61cadcf2016-03-09 16:11:32 +0800700 }
addy ke64e36822014-07-01 09:03:59 +0800701
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100702 master->dma_rx = dma_request_chan(rs->dev, "rx");
703 if (IS_ERR(master->dma_rx)) {
704 if (PTR_ERR(master->dma_rx) == -EPROBE_DEFER) {
Shawn Line4c0e062016-03-31 11:11:41 +0800705 ret = -EPROBE_DEFER;
Dan Carpenter5de7ed02016-05-04 09:25:46 +0300706 goto err_free_dma_tx;
addy ke64e36822014-07-01 09:03:59 +0800707 }
708 dev_warn(rs->dev, "Failed to request RX DMA channel\n");
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100709 master->dma_rx = NULL;
addy ke64e36822014-07-01 09:03:59 +0800710 }
711
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100712 if (master->dma_tx && master->dma_rx) {
713 rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR;
714 rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR;
addy ke64e36822014-07-01 09:03:59 +0800715 master->can_dma = rockchip_spi_can_dma;
addy ke64e36822014-07-01 09:03:59 +0800716 }
717
718 ret = devm_spi_register_master(&pdev->dev, master);
Jeffy Chen43de9792017-08-07 20:40:18 +0800719 if (ret < 0) {
addy ke64e36822014-07-01 09:03:59 +0800720 dev_err(&pdev->dev, "Failed to register master\n");
Jeffy Chenc3515872017-06-13 13:25:40 +0800721 goto err_free_dma_rx;
addy ke64e36822014-07-01 09:03:59 +0800722 }
723
addy ke64e36822014-07-01 09:03:59 +0800724 return 0;
725
Jeffy Chenc3515872017-06-13 13:25:40 +0800726err_free_dma_rx:
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100727 if (master->dma_rx)
728 dma_release_channel(master->dma_rx);
Dan Carpenter5de7ed02016-05-04 09:25:46 +0300729err_free_dma_tx:
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100730 if (master->dma_tx)
731 dma_release_channel(master->dma_tx);
Jeffy Chenc3515872017-06-13 13:25:40 +0800732err_disable_pm_runtime:
733 pm_runtime_disable(&pdev->dev);
734err_disable_spiclk:
addy ke64e36822014-07-01 09:03:59 +0800735 clk_disable_unprepare(rs->spiclk);
Jeffy Chenc3515872017-06-13 13:25:40 +0800736err_disable_apbclk:
addy ke64e36822014-07-01 09:03:59 +0800737 clk_disable_unprepare(rs->apb_pclk);
Jeffy Chenc3515872017-06-13 13:25:40 +0800738err_put_master:
addy ke64e36822014-07-01 09:03:59 +0800739 spi_master_put(master);
740
741 return ret;
742}
743
744static int rockchip_spi_remove(struct platform_device *pdev)
745{
746 struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
747 struct rockchip_spi *rs = spi_master_get_devdata(master);
748
Jeffy Chen6a06e892017-08-07 20:40:19 +0800749 pm_runtime_get_sync(&pdev->dev);
addy ke64e36822014-07-01 09:03:59 +0800750
751 clk_disable_unprepare(rs->spiclk);
752 clk_disable_unprepare(rs->apb_pclk);
753
Jeffy Chen6a06e892017-08-07 20:40:19 +0800754 pm_runtime_put_noidle(&pdev->dev);
755 pm_runtime_disable(&pdev->dev);
756 pm_runtime_set_suspended(&pdev->dev);
757
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100758 if (master->dma_tx)
759 dma_release_channel(master->dma_tx);
760 if (master->dma_rx)
761 dma_release_channel(master->dma_rx);
addy ke64e36822014-07-01 09:03:59 +0800762
Shawn Lin844c9f42016-02-15 16:28:12 +0800763 spi_master_put(master);
764
addy ke64e36822014-07-01 09:03:59 +0800765 return 0;
766}
767
768#ifdef CONFIG_PM_SLEEP
769static int rockchip_spi_suspend(struct device *dev)
770{
Jeffy Chen43de9792017-08-07 20:40:18 +0800771 int ret;
addy ke64e36822014-07-01 09:03:59 +0800772 struct spi_master *master = dev_get_drvdata(dev);
addy ke64e36822014-07-01 09:03:59 +0800773
Emil Renner Berthingd790c342018-10-31 11:57:05 +0100774 ret = spi_master_suspend(master);
Jeffy Chen43de9792017-08-07 20:40:18 +0800775 if (ret < 0)
addy ke64e36822014-07-01 09:03:59 +0800776 return ret;
777
Jeffy Chend38c4ae12017-08-07 20:40:20 +0800778 ret = pm_runtime_force_suspend(dev);
779 if (ret < 0)
780 return ret;
addy ke64e36822014-07-01 09:03:59 +0800781
Brian Norris23e291c2016-12-16 16:59:16 -0800782 pinctrl_pm_select_sleep_state(dev);
783
Jeffy Chen43de9792017-08-07 20:40:18 +0800784 return 0;
addy ke64e36822014-07-01 09:03:59 +0800785}
786
787static int rockchip_spi_resume(struct device *dev)
788{
Jeffy Chen43de9792017-08-07 20:40:18 +0800789 int ret;
addy ke64e36822014-07-01 09:03:59 +0800790 struct spi_master *master = dev_get_drvdata(dev);
791 struct rockchip_spi *rs = spi_master_get_devdata(master);
792
Brian Norris23e291c2016-12-16 16:59:16 -0800793 pinctrl_pm_select_default_state(dev);
794
Jeffy Chend38c4ae12017-08-07 20:40:20 +0800795 ret = pm_runtime_force_resume(dev);
796 if (ret < 0)
797 return ret;
addy ke64e36822014-07-01 09:03:59 +0800798
Emil Renner Berthingd790c342018-10-31 11:57:05 +0100799 ret = spi_master_resume(master);
addy ke64e36822014-07-01 09:03:59 +0800800 if (ret < 0) {
801 clk_disable_unprepare(rs->spiclk);
802 clk_disable_unprepare(rs->apb_pclk);
803 }
804
Jeffy Chen43de9792017-08-07 20:40:18 +0800805 return 0;
addy ke64e36822014-07-01 09:03:59 +0800806}
807#endif /* CONFIG_PM_SLEEP */
808
Rafael J. Wysockiec833052014-12-13 00:41:15 +0100809#ifdef CONFIG_PM
addy ke64e36822014-07-01 09:03:59 +0800810static int rockchip_spi_runtime_suspend(struct device *dev)
811{
812 struct spi_master *master = dev_get_drvdata(dev);
813 struct rockchip_spi *rs = spi_master_get_devdata(master);
814
815 clk_disable_unprepare(rs->spiclk);
816 clk_disable_unprepare(rs->apb_pclk);
817
818 return 0;
819}
820
821static int rockchip_spi_runtime_resume(struct device *dev)
822{
823 int ret;
824 struct spi_master *master = dev_get_drvdata(dev);
825 struct rockchip_spi *rs = spi_master_get_devdata(master);
826
827 ret = clk_prepare_enable(rs->apb_pclk);
Jeffy Chen43de9792017-08-07 20:40:18 +0800828 if (ret < 0)
addy ke64e36822014-07-01 09:03:59 +0800829 return ret;
830
831 ret = clk_prepare_enable(rs->spiclk);
Jeffy Chen43de9792017-08-07 20:40:18 +0800832 if (ret < 0)
addy ke64e36822014-07-01 09:03:59 +0800833 clk_disable_unprepare(rs->apb_pclk);
834
Jeffy Chen43de9792017-08-07 20:40:18 +0800835 return 0;
addy ke64e36822014-07-01 09:03:59 +0800836}
Rafael J. Wysockiec833052014-12-13 00:41:15 +0100837#endif /* CONFIG_PM */
addy ke64e36822014-07-01 09:03:59 +0800838
839static const struct dev_pm_ops rockchip_spi_pm = {
840 SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
841 SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
842 rockchip_spi_runtime_resume, NULL)
843};
844
845static const struct of_device_id rockchip_spi_dt_match[] = {
Andy Yan6b860e62017-08-14 16:34:22 +0800846 { .compatible = "rockchip,rv1108-spi", },
Caesar Wangaa29ea32016-05-20 07:56:21 +0800847 { .compatible = "rockchip,rk3036-spi", },
addy ke64e36822014-07-01 09:03:59 +0800848 { .compatible = "rockchip,rk3066-spi", },
Addy Keb839b782014-07-11 10:09:19 +0800849 { .compatible = "rockchip,rk3188-spi", },
Caesar Wangaa29ea32016-05-20 07:56:21 +0800850 { .compatible = "rockchip,rk3228-spi", },
Addy Keb839b782014-07-11 10:09:19 +0800851 { .compatible = "rockchip,rk3288-spi", },
Caesar Wangaa29ea32016-05-20 07:56:21 +0800852 { .compatible = "rockchip,rk3368-spi", },
Xu Jianqun9b7a5622016-02-18 19:16:31 +0800853 { .compatible = "rockchip,rk3399-spi", },
addy ke64e36822014-07-01 09:03:59 +0800854 { },
855};
856MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
857
858static struct platform_driver rockchip_spi_driver = {
859 .driver = {
860 .name = DRIVER_NAME,
addy ke64e36822014-07-01 09:03:59 +0800861 .pm = &rockchip_spi_pm,
862 .of_match_table = of_match_ptr(rockchip_spi_dt_match),
863 },
864 .probe = rockchip_spi_probe,
865 .remove = rockchip_spi_remove,
866};
867
868module_platform_driver(rockchip_spi_driver);
869
Addy Ke5dcc44e2014-07-11 10:07:56 +0800870MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
addy ke64e36822014-07-01 09:03:59 +0800871MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
872MODULE_LICENSE("GPL v2");