eeprom: at24: improve the device_id table readability
[linux/fpc-iii.git] / drivers / spi / spi-rockchip.c
blobcd89682065b98d7a06a7d7064ec30aff2a5e7fef
1 /*
2 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
3 * Author: Addy Ke <addy.ke@rock-chips.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
16 #include <linux/clk.h>
17 #include <linux/dmaengine.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/spi/spi.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/scatterlist.h>
25 #define DRIVER_NAME "rockchip-spi"
27 /* SPI register offsets */
28 #define ROCKCHIP_SPI_CTRLR0 0x0000
29 #define ROCKCHIP_SPI_CTRLR1 0x0004
30 #define ROCKCHIP_SPI_SSIENR 0x0008
31 #define ROCKCHIP_SPI_SER 0x000c
32 #define ROCKCHIP_SPI_BAUDR 0x0010
33 #define ROCKCHIP_SPI_TXFTLR 0x0014
34 #define ROCKCHIP_SPI_RXFTLR 0x0018
35 #define ROCKCHIP_SPI_TXFLR 0x001c
36 #define ROCKCHIP_SPI_RXFLR 0x0020
37 #define ROCKCHIP_SPI_SR 0x0024
38 #define ROCKCHIP_SPI_IPR 0x0028
39 #define ROCKCHIP_SPI_IMR 0x002c
40 #define ROCKCHIP_SPI_ISR 0x0030
41 #define ROCKCHIP_SPI_RISR 0x0034
42 #define ROCKCHIP_SPI_ICR 0x0038
43 #define ROCKCHIP_SPI_DMACR 0x003c
44 #define ROCKCHIP_SPI_DMATDLR 0x0040
45 #define ROCKCHIP_SPI_DMARDLR 0x0044
46 #define ROCKCHIP_SPI_TXDR 0x0400
47 #define ROCKCHIP_SPI_RXDR 0x0800
49 /* Bit fields in CTRLR0 */
50 #define CR0_DFS_OFFSET 0
52 #define CR0_CFS_OFFSET 2
54 #define CR0_SCPH_OFFSET 6
56 #define CR0_SCPOL_OFFSET 7
58 #define CR0_CSM_OFFSET 8
59 #define CR0_CSM_KEEP 0x0
60 /* ss_n be high for half sclk_out cycles */
61 #define CR0_CSM_HALF 0X1
62 /* ss_n be high for one sclk_out cycle */
63 #define CR0_CSM_ONE 0x2
65 /* ss_n to sclk_out delay */
66 #define CR0_SSD_OFFSET 10
68 * The period between ss_n active and
69 * sclk_out active is half sclk_out cycles
71 #define CR0_SSD_HALF 0x0
73 * The period between ss_n active and
74 * sclk_out active is one sclk_out cycle
76 #define CR0_SSD_ONE 0x1
78 #define CR0_EM_OFFSET 11
79 #define CR0_EM_LITTLE 0x0
80 #define CR0_EM_BIG 0x1
82 #define CR0_FBM_OFFSET 12
83 #define CR0_FBM_MSB 0x0
84 #define CR0_FBM_LSB 0x1
86 #define CR0_BHT_OFFSET 13
87 #define CR0_BHT_16BIT 0x0
88 #define CR0_BHT_8BIT 0x1
90 #define CR0_RSD_OFFSET 14
92 #define CR0_FRF_OFFSET 16
93 #define CR0_FRF_SPI 0x0
94 #define CR0_FRF_SSP 0x1
95 #define CR0_FRF_MICROWIRE 0x2
97 #define CR0_XFM_OFFSET 18
98 #define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET)
99 #define CR0_XFM_TR 0x0
100 #define CR0_XFM_TO 0x1
101 #define CR0_XFM_RO 0x2
103 #define CR0_OPM_OFFSET 20
104 #define CR0_OPM_MASTER 0x0
105 #define CR0_OPM_SLAVE 0x1
107 #define CR0_MTM_OFFSET 0x21
109 /* Bit fields in SER, 2bit */
110 #define SER_MASK 0x3
112 /* Bit fields in SR, 5bit */
113 #define SR_MASK 0x1f
114 #define SR_BUSY (1 << 0)
115 #define SR_TF_FULL (1 << 1)
116 #define SR_TF_EMPTY (1 << 2)
117 #define SR_RF_EMPTY (1 << 3)
118 #define SR_RF_FULL (1 << 4)
120 /* Bit fields in ISR, IMR, ISR, RISR, 5bit */
121 #define INT_MASK 0x1f
122 #define INT_TF_EMPTY (1 << 0)
123 #define INT_TF_OVERFLOW (1 << 1)
124 #define INT_RF_UNDERFLOW (1 << 2)
125 #define INT_RF_OVERFLOW (1 << 3)
126 #define INT_RF_FULL (1 << 4)
128 /* Bit fields in ICR, 4bit */
129 #define ICR_MASK 0x0f
130 #define ICR_ALL (1 << 0)
131 #define ICR_RF_UNDERFLOW (1 << 1)
132 #define ICR_RF_OVERFLOW (1 << 2)
133 #define ICR_TF_OVERFLOW (1 << 3)
135 /* Bit fields in DMACR */
136 #define RF_DMA_EN (1 << 0)
137 #define TF_DMA_EN (1 << 1)
139 #define RXBUSY (1 << 0)
140 #define TXBUSY (1 << 1)
142 /* sclk_out: spi master internal logic in rk3x can support 50Mhz */
143 #define MAX_SCLK_OUT 50000000
145 enum rockchip_ssi_type {
146 SSI_MOTO_SPI = 0,
147 SSI_TI_SSP,
148 SSI_NS_MICROWIRE,
151 struct rockchip_spi_dma_data {
152 struct dma_chan *ch;
153 enum dma_transfer_direction direction;
154 dma_addr_t addr;
157 struct rockchip_spi {
158 struct device *dev;
159 struct spi_master *master;
161 struct clk *spiclk;
162 struct clk *apb_pclk;
164 void __iomem *regs;
165 /*depth of the FIFO buffer */
166 u32 fifo_len;
167 /* max bus freq supported */
168 u32 max_freq;
169 /* supported slave numbers */
170 enum rockchip_ssi_type type;
172 u16 mode;
173 u8 tmode;
174 u8 bpw;
175 u8 n_bytes;
176 u32 rsd_nsecs;
177 unsigned len;
178 u32 speed;
180 const void *tx;
181 const void *tx_end;
182 void *rx;
183 void *rx_end;
185 u32 state;
186 /* protect state */
187 spinlock_t lock;
189 u32 use_dma;
190 struct sg_table tx_sg;
191 struct sg_table rx_sg;
192 struct rockchip_spi_dma_data dma_rx;
193 struct rockchip_spi_dma_data dma_tx;
194 struct dma_slave_caps dma_caps;
197 static inline void spi_enable_chip(struct rockchip_spi *rs, int enable)
199 writel_relaxed((enable ? 1 : 0), rs->regs + ROCKCHIP_SPI_SSIENR);
202 static inline void spi_set_clk(struct rockchip_spi *rs, u16 div)
204 writel_relaxed(div, rs->regs + ROCKCHIP_SPI_BAUDR);
207 static inline void flush_fifo(struct rockchip_spi *rs)
209 while (readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR))
210 readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
213 static inline void wait_for_idle(struct rockchip_spi *rs)
215 unsigned long timeout = jiffies + msecs_to_jiffies(5);
217 do {
218 if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
219 return;
220 } while (!time_after(jiffies, timeout));
222 dev_warn(rs->dev, "spi controller is in busy state!\n");
225 static u32 get_fifo_len(struct rockchip_spi *rs)
227 u32 fifo;
229 for (fifo = 2; fifo < 32; fifo++) {
230 writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR);
231 if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR))
232 break;
235 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR);
237 return (fifo == 31) ? 0 : fifo;
240 static inline u32 tx_max(struct rockchip_spi *rs)
242 u32 tx_left, tx_room;
244 tx_left = (rs->tx_end - rs->tx) / rs->n_bytes;
245 tx_room = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
247 return min(tx_left, tx_room);
250 static inline u32 rx_max(struct rockchip_spi *rs)
252 u32 rx_left = (rs->rx_end - rs->rx) / rs->n_bytes;
253 u32 rx_room = (u32)readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
255 return min(rx_left, rx_room);
258 static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
260 u32 ser;
261 struct spi_master *master = spi->master;
262 struct rockchip_spi *rs = spi_master_get_devdata(master);
264 pm_runtime_get_sync(rs->dev);
266 ser = readl_relaxed(rs->regs + ROCKCHIP_SPI_SER) & SER_MASK;
269 * drivers/spi/spi.c:
270 * static void spi_set_cs(struct spi_device *spi, bool enable)
272 * if (spi->mode & SPI_CS_HIGH)
273 * enable = !enable;
275 * if (spi->cs_gpio >= 0)
276 * gpio_set_value(spi->cs_gpio, !enable);
277 * else if (spi->master->set_cs)
278 * spi->master->set_cs(spi, !enable);
281 * Note: enable(rockchip_spi_set_cs) = !enable(spi_set_cs)
283 if (!enable)
284 ser |= 1 << spi->chip_select;
285 else
286 ser &= ~(1 << spi->chip_select);
288 writel_relaxed(ser, rs->regs + ROCKCHIP_SPI_SER);
290 pm_runtime_put_sync(rs->dev);
293 static int rockchip_spi_prepare_message(struct spi_master *master,
294 struct spi_message *msg)
296 struct rockchip_spi *rs = spi_master_get_devdata(master);
297 struct spi_device *spi = msg->spi;
299 rs->mode = spi->mode;
301 return 0;
304 static void rockchip_spi_handle_err(struct spi_master *master,
305 struct spi_message *msg)
307 unsigned long flags;
308 struct rockchip_spi *rs = spi_master_get_devdata(master);
310 spin_lock_irqsave(&rs->lock, flags);
313 * For DMA mode, we need terminate DMA channel and flush
314 * fifo for the next transfer if DMA thansfer timeout.
315 * handle_err() was called by core if transfer failed.
316 * Maybe it is reasonable for error handling here.
318 if (rs->use_dma) {
319 if (rs->state & RXBUSY) {
320 dmaengine_terminate_async(rs->dma_rx.ch);
321 flush_fifo(rs);
324 if (rs->state & TXBUSY)
325 dmaengine_terminate_async(rs->dma_tx.ch);
328 spin_unlock_irqrestore(&rs->lock, flags);
331 static int rockchip_spi_unprepare_message(struct spi_master *master,
332 struct spi_message *msg)
334 struct rockchip_spi *rs = spi_master_get_devdata(master);
336 spi_enable_chip(rs, 0);
338 return 0;
341 static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
343 u32 max = tx_max(rs);
344 u32 txw = 0;
346 while (max--) {
347 if (rs->n_bytes == 1)
348 txw = *(u8 *)(rs->tx);
349 else
350 txw = *(u16 *)(rs->tx);
352 writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
353 rs->tx += rs->n_bytes;
357 static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
359 u32 max = rx_max(rs);
360 u32 rxw;
362 while (max--) {
363 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
364 if (rs->n_bytes == 1)
365 *(u8 *)(rs->rx) = (u8)rxw;
366 else
367 *(u16 *)(rs->rx) = (u16)rxw;
368 rs->rx += rs->n_bytes;
372 static int rockchip_spi_pio_transfer(struct rockchip_spi *rs)
374 int remain = 0;
376 do {
377 if (rs->tx) {
378 remain = rs->tx_end - rs->tx;
379 rockchip_spi_pio_writer(rs);
382 if (rs->rx) {
383 remain = rs->rx_end - rs->rx;
384 rockchip_spi_pio_reader(rs);
387 cpu_relax();
388 } while (remain);
390 /* If tx, wait until the FIFO data completely. */
391 if (rs->tx)
392 wait_for_idle(rs);
394 spi_enable_chip(rs, 0);
396 return 0;
399 static void rockchip_spi_dma_rxcb(void *data)
401 unsigned long flags;
402 struct rockchip_spi *rs = data;
404 spin_lock_irqsave(&rs->lock, flags);
406 rs->state &= ~RXBUSY;
407 if (!(rs->state & TXBUSY)) {
408 spi_enable_chip(rs, 0);
409 spi_finalize_current_transfer(rs->master);
412 spin_unlock_irqrestore(&rs->lock, flags);
415 static void rockchip_spi_dma_txcb(void *data)
417 unsigned long flags;
418 struct rockchip_spi *rs = data;
420 /* Wait until the FIFO data completely. */
421 wait_for_idle(rs);
423 spin_lock_irqsave(&rs->lock, flags);
425 rs->state &= ~TXBUSY;
426 if (!(rs->state & RXBUSY)) {
427 spi_enable_chip(rs, 0);
428 spi_finalize_current_transfer(rs->master);
431 spin_unlock_irqrestore(&rs->lock, flags);
434 static int rockchip_spi_prepare_dma(struct rockchip_spi *rs)
436 unsigned long flags;
437 struct dma_slave_config rxconf, txconf;
438 struct dma_async_tx_descriptor *rxdesc, *txdesc;
440 spin_lock_irqsave(&rs->lock, flags);
441 rs->state &= ~RXBUSY;
442 rs->state &= ~TXBUSY;
443 spin_unlock_irqrestore(&rs->lock, flags);
445 rxdesc = NULL;
446 if (rs->rx) {
447 rxconf.direction = rs->dma_rx.direction;
448 rxconf.src_addr = rs->dma_rx.addr;
449 rxconf.src_addr_width = rs->n_bytes;
450 if (rs->dma_caps.max_burst > 4)
451 rxconf.src_maxburst = 4;
452 else
453 rxconf.src_maxburst = 1;
454 dmaengine_slave_config(rs->dma_rx.ch, &rxconf);
456 rxdesc = dmaengine_prep_slave_sg(
457 rs->dma_rx.ch,
458 rs->rx_sg.sgl, rs->rx_sg.nents,
459 rs->dma_rx.direction, DMA_PREP_INTERRUPT);
460 if (!rxdesc)
461 return -EINVAL;
463 rxdesc->callback = rockchip_spi_dma_rxcb;
464 rxdesc->callback_param = rs;
467 txdesc = NULL;
468 if (rs->tx) {
469 txconf.direction = rs->dma_tx.direction;
470 txconf.dst_addr = rs->dma_tx.addr;
471 txconf.dst_addr_width = rs->n_bytes;
472 if (rs->dma_caps.max_burst > 4)
473 txconf.dst_maxburst = 4;
474 else
475 txconf.dst_maxburst = 1;
476 dmaengine_slave_config(rs->dma_tx.ch, &txconf);
478 txdesc = dmaengine_prep_slave_sg(
479 rs->dma_tx.ch,
480 rs->tx_sg.sgl, rs->tx_sg.nents,
481 rs->dma_tx.direction, DMA_PREP_INTERRUPT);
482 if (!txdesc) {
483 if (rxdesc)
484 dmaengine_terminate_sync(rs->dma_rx.ch);
485 return -EINVAL;
488 txdesc->callback = rockchip_spi_dma_txcb;
489 txdesc->callback_param = rs;
492 /* rx must be started before tx due to spi instinct */
493 if (rxdesc) {
494 spin_lock_irqsave(&rs->lock, flags);
495 rs->state |= RXBUSY;
496 spin_unlock_irqrestore(&rs->lock, flags);
497 dmaengine_submit(rxdesc);
498 dma_async_issue_pending(rs->dma_rx.ch);
501 if (txdesc) {
502 spin_lock_irqsave(&rs->lock, flags);
503 rs->state |= TXBUSY;
504 spin_unlock_irqrestore(&rs->lock, flags);
505 dmaengine_submit(txdesc);
506 dma_async_issue_pending(rs->dma_tx.ch);
509 return 0;
512 static void rockchip_spi_config(struct rockchip_spi *rs)
514 u32 div = 0;
515 u32 dmacr = 0;
516 int rsd = 0;
518 u32 cr0 = (CR0_BHT_8BIT << CR0_BHT_OFFSET)
519 | (CR0_SSD_ONE << CR0_SSD_OFFSET)
520 | (CR0_EM_BIG << CR0_EM_OFFSET);
522 cr0 |= (rs->n_bytes << CR0_DFS_OFFSET);
523 cr0 |= ((rs->mode & 0x3) << CR0_SCPH_OFFSET);
524 cr0 |= (rs->tmode << CR0_XFM_OFFSET);
525 cr0 |= (rs->type << CR0_FRF_OFFSET);
527 if (rs->use_dma) {
528 if (rs->tx)
529 dmacr |= TF_DMA_EN;
530 if (rs->rx)
531 dmacr |= RF_DMA_EN;
534 if (WARN_ON(rs->speed > MAX_SCLK_OUT))
535 rs->speed = MAX_SCLK_OUT;
537 /* the minimum divisor is 2 */
538 if (rs->max_freq < 2 * rs->speed) {
539 clk_set_rate(rs->spiclk, 2 * rs->speed);
540 rs->max_freq = clk_get_rate(rs->spiclk);
543 /* div doesn't support odd number */
544 div = DIV_ROUND_UP(rs->max_freq, rs->speed);
545 div = (div + 1) & 0xfffe;
547 /* Rx sample delay is expressed in parent clock cycles (max 3) */
548 rsd = DIV_ROUND_CLOSEST(rs->rsd_nsecs * (rs->max_freq >> 8),
549 1000000000 >> 8);
550 if (!rsd && rs->rsd_nsecs) {
551 pr_warn_once("rockchip-spi: %u Hz are too slow to express %u ns delay\n",
552 rs->max_freq, rs->rsd_nsecs);
553 } else if (rsd > 3) {
554 rsd = 3;
555 pr_warn_once("rockchip-spi: %u Hz are too fast to express %u ns delay, clamping at %u ns\n",
556 rs->max_freq, rs->rsd_nsecs,
557 rsd * 1000000000U / rs->max_freq);
559 cr0 |= rsd << CR0_RSD_OFFSET;
561 writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
563 writel_relaxed(rs->len - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);
564 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_TXFTLR);
565 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
567 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMATDLR);
568 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR);
569 writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
571 spi_set_clk(rs, div);
573 dev_dbg(rs->dev, "cr0 0x%x, div %d\n", cr0, div);
576 static int rockchip_spi_transfer_one(
577 struct spi_master *master,
578 struct spi_device *spi,
579 struct spi_transfer *xfer)
581 int ret = 1;
582 struct rockchip_spi *rs = spi_master_get_devdata(master);
584 WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
585 (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
587 if (!xfer->tx_buf && !xfer->rx_buf) {
588 dev_err(rs->dev, "No buffer for transfer\n");
589 return -EINVAL;
592 rs->speed = xfer->speed_hz;
593 rs->bpw = xfer->bits_per_word;
594 rs->n_bytes = rs->bpw >> 3;
596 rs->tx = xfer->tx_buf;
597 rs->tx_end = rs->tx + xfer->len;
598 rs->rx = xfer->rx_buf;
599 rs->rx_end = rs->rx + xfer->len;
600 rs->len = xfer->len;
602 rs->tx_sg = xfer->tx_sg;
603 rs->rx_sg = xfer->rx_sg;
605 if (rs->tx && rs->rx)
606 rs->tmode = CR0_XFM_TR;
607 else if (rs->tx)
608 rs->tmode = CR0_XFM_TO;
609 else if (rs->rx)
610 rs->tmode = CR0_XFM_RO;
612 /* we need prepare dma before spi was enabled */
613 if (master->can_dma && master->can_dma(master, spi, xfer))
614 rs->use_dma = 1;
615 else
616 rs->use_dma = 0;
618 rockchip_spi_config(rs);
620 if (rs->use_dma) {
621 if (rs->tmode == CR0_XFM_RO) {
622 /* rx: dma must be prepared first */
623 ret = rockchip_spi_prepare_dma(rs);
624 spi_enable_chip(rs, 1);
625 } else {
626 /* tx or tr: spi must be enabled first */
627 spi_enable_chip(rs, 1);
628 ret = rockchip_spi_prepare_dma(rs);
630 } else {
631 spi_enable_chip(rs, 1);
632 ret = rockchip_spi_pio_transfer(rs);
635 return ret;
638 static bool rockchip_spi_can_dma(struct spi_master *master,
639 struct spi_device *spi,
640 struct spi_transfer *xfer)
642 struct rockchip_spi *rs = spi_master_get_devdata(master);
644 return (xfer->len > rs->fifo_len);
647 static int rockchip_spi_probe(struct platform_device *pdev)
649 int ret = 0;
650 struct rockchip_spi *rs;
651 struct spi_master *master;
652 struct resource *mem;
653 u32 rsd_nsecs;
655 master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi));
656 if (!master)
657 return -ENOMEM;
659 platform_set_drvdata(pdev, master);
661 rs = spi_master_get_devdata(master);
663 /* Get basic io resource and map it */
664 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
665 rs->regs = devm_ioremap_resource(&pdev->dev, mem);
666 if (IS_ERR(rs->regs)) {
667 ret = PTR_ERR(rs->regs);
668 goto err_ioremap_resource;
671 rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
672 if (IS_ERR(rs->apb_pclk)) {
673 dev_err(&pdev->dev, "Failed to get apb_pclk\n");
674 ret = PTR_ERR(rs->apb_pclk);
675 goto err_ioremap_resource;
678 rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
679 if (IS_ERR(rs->spiclk)) {
680 dev_err(&pdev->dev, "Failed to get spi_pclk\n");
681 ret = PTR_ERR(rs->spiclk);
682 goto err_ioremap_resource;
685 ret = clk_prepare_enable(rs->apb_pclk);
686 if (ret) {
687 dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
688 goto err_ioremap_resource;
691 ret = clk_prepare_enable(rs->spiclk);
692 if (ret) {
693 dev_err(&pdev->dev, "Failed to enable spi_clk\n");
694 goto err_spiclk_enable;
697 spi_enable_chip(rs, 0);
699 rs->type = SSI_MOTO_SPI;
700 rs->master = master;
701 rs->dev = &pdev->dev;
702 rs->max_freq = clk_get_rate(rs->spiclk);
704 if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
705 &rsd_nsecs))
706 rs->rsd_nsecs = rsd_nsecs;
708 rs->fifo_len = get_fifo_len(rs);
709 if (!rs->fifo_len) {
710 dev_err(&pdev->dev, "Failed to get fifo length\n");
711 ret = -EINVAL;
712 goto err_get_fifo_len;
715 spin_lock_init(&rs->lock);
717 pm_runtime_set_active(&pdev->dev);
718 pm_runtime_enable(&pdev->dev);
720 master->auto_runtime_pm = true;
721 master->bus_num = pdev->id;
722 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
723 master->num_chipselect = 2;
724 master->dev.of_node = pdev->dev.of_node;
725 master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8);
727 master->set_cs = rockchip_spi_set_cs;
728 master->prepare_message = rockchip_spi_prepare_message;
729 master->unprepare_message = rockchip_spi_unprepare_message;
730 master->transfer_one = rockchip_spi_transfer_one;
731 master->handle_err = rockchip_spi_handle_err;
733 rs->dma_tx.ch = dma_request_chan(rs->dev, "tx");
734 if (IS_ERR(rs->dma_tx.ch)) {
735 /* Check tx to see if we need defer probing driver */
736 if (PTR_ERR(rs->dma_tx.ch) == -EPROBE_DEFER) {
737 ret = -EPROBE_DEFER;
738 goto err_get_fifo_len;
740 dev_warn(rs->dev, "Failed to request TX DMA channel\n");
741 rs->dma_tx.ch = NULL;
744 rs->dma_rx.ch = dma_request_chan(rs->dev, "rx");
745 if (IS_ERR(rs->dma_rx.ch)) {
746 if (PTR_ERR(rs->dma_rx.ch) == -EPROBE_DEFER) {
747 ret = -EPROBE_DEFER;
748 goto err_free_dma_tx;
750 dev_warn(rs->dev, "Failed to request RX DMA channel\n");
751 rs->dma_rx.ch = NULL;
754 if (rs->dma_tx.ch && rs->dma_rx.ch) {
755 dma_get_slave_caps(rs->dma_rx.ch, &(rs->dma_caps));
756 rs->dma_tx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_TXDR);
757 rs->dma_rx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_RXDR);
758 rs->dma_tx.direction = DMA_MEM_TO_DEV;
759 rs->dma_rx.direction = DMA_DEV_TO_MEM;
761 master->can_dma = rockchip_spi_can_dma;
762 master->dma_tx = rs->dma_tx.ch;
763 master->dma_rx = rs->dma_rx.ch;
766 ret = devm_spi_register_master(&pdev->dev, master);
767 if (ret) {
768 dev_err(&pdev->dev, "Failed to register master\n");
769 goto err_register_master;
772 return 0;
774 err_register_master:
775 pm_runtime_disable(&pdev->dev);
776 if (rs->dma_rx.ch)
777 dma_release_channel(rs->dma_rx.ch);
778 err_free_dma_tx:
779 if (rs->dma_tx.ch)
780 dma_release_channel(rs->dma_tx.ch);
781 err_get_fifo_len:
782 clk_disable_unprepare(rs->spiclk);
783 err_spiclk_enable:
784 clk_disable_unprepare(rs->apb_pclk);
785 err_ioremap_resource:
786 spi_master_put(master);
788 return ret;
791 static int rockchip_spi_remove(struct platform_device *pdev)
793 struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
794 struct rockchip_spi *rs = spi_master_get_devdata(master);
796 pm_runtime_disable(&pdev->dev);
798 clk_disable_unprepare(rs->spiclk);
799 clk_disable_unprepare(rs->apb_pclk);
801 if (rs->dma_tx.ch)
802 dma_release_channel(rs->dma_tx.ch);
803 if (rs->dma_rx.ch)
804 dma_release_channel(rs->dma_rx.ch);
806 spi_master_put(master);
808 return 0;
811 #ifdef CONFIG_PM_SLEEP
812 static int rockchip_spi_suspend(struct device *dev)
814 int ret = 0;
815 struct spi_master *master = dev_get_drvdata(dev);
816 struct rockchip_spi *rs = spi_master_get_devdata(master);
818 ret = spi_master_suspend(rs->master);
819 if (ret)
820 return ret;
822 if (!pm_runtime_suspended(dev)) {
823 clk_disable_unprepare(rs->spiclk);
824 clk_disable_unprepare(rs->apb_pclk);
827 return ret;
830 static int rockchip_spi_resume(struct device *dev)
832 int ret = 0;
833 struct spi_master *master = dev_get_drvdata(dev);
834 struct rockchip_spi *rs = spi_master_get_devdata(master);
836 if (!pm_runtime_suspended(dev)) {
837 ret = clk_prepare_enable(rs->apb_pclk);
838 if (ret < 0)
839 return ret;
841 ret = clk_prepare_enable(rs->spiclk);
842 if (ret < 0) {
843 clk_disable_unprepare(rs->apb_pclk);
844 return ret;
848 ret = spi_master_resume(rs->master);
849 if (ret < 0) {
850 clk_disable_unprepare(rs->spiclk);
851 clk_disable_unprepare(rs->apb_pclk);
854 return ret;
856 #endif /* CONFIG_PM_SLEEP */
858 #ifdef CONFIG_PM
859 static int rockchip_spi_runtime_suspend(struct device *dev)
861 struct spi_master *master = dev_get_drvdata(dev);
862 struct rockchip_spi *rs = spi_master_get_devdata(master);
864 clk_disable_unprepare(rs->spiclk);
865 clk_disable_unprepare(rs->apb_pclk);
867 return 0;
870 static int rockchip_spi_runtime_resume(struct device *dev)
872 int ret;
873 struct spi_master *master = dev_get_drvdata(dev);
874 struct rockchip_spi *rs = spi_master_get_devdata(master);
876 ret = clk_prepare_enable(rs->apb_pclk);
877 if (ret)
878 return ret;
880 ret = clk_prepare_enable(rs->spiclk);
881 if (ret)
882 clk_disable_unprepare(rs->apb_pclk);
884 return ret;
886 #endif /* CONFIG_PM */
888 static const struct dev_pm_ops rockchip_spi_pm = {
889 SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
890 SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
891 rockchip_spi_runtime_resume, NULL)
894 static const struct of_device_id rockchip_spi_dt_match[] = {
895 { .compatible = "rockchip,rk3066-spi", },
896 { .compatible = "rockchip,rk3188-spi", },
897 { .compatible = "rockchip,rk3288-spi", },
898 { .compatible = "rockchip,rk3399-spi", },
899 { },
901 MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
903 static struct platform_driver rockchip_spi_driver = {
904 .driver = {
905 .name = DRIVER_NAME,
906 .pm = &rockchip_spi_pm,
907 .of_match_table = of_match_ptr(rockchip_spi_dt_match),
909 .probe = rockchip_spi_probe,
910 .remove = rockchip_spi_remove,
913 module_platform_driver(rockchip_spi_driver);
915 MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
916 MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
917 MODULE_LICENSE("GPL v2");