1 // SPDX-License-Identifier: GPL-2.0-only
3 * IMG SPFI controller driver
5 * Copyright (C) 2007,2008,2013 Imagination Technologies Ltd.
6 * Copyright (C) 2014 Google, Inc.
10 #include <linux/delay.h>
11 #include <linux/dmaengine.h>
12 #include <linux/gpio.h>
13 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/scatterlist.h>
21 #include <linux/slab.h>
22 #include <linux/spi/spi.h>
23 #include <linux/spinlock.h>
25 #define SPFI_DEVICE_PARAMETER(x) (0x00 + 0x4 * (x))
26 #define SPFI_DEVICE_PARAMETER_BITCLK_SHIFT 24
27 #define SPFI_DEVICE_PARAMETER_BITCLK_MASK 0xff
28 #define SPFI_DEVICE_PARAMETER_CSSETUP_SHIFT 16
29 #define SPFI_DEVICE_PARAMETER_CSSETUP_MASK 0xff
30 #define SPFI_DEVICE_PARAMETER_CSHOLD_SHIFT 8
31 #define SPFI_DEVICE_PARAMETER_CSHOLD_MASK 0xff
32 #define SPFI_DEVICE_PARAMETER_CSDELAY_SHIFT 0
33 #define SPFI_DEVICE_PARAMETER_CSDELAY_MASK 0xff
35 #define SPFI_CONTROL 0x14
36 #define SPFI_CONTROL_CONTINUE BIT(12)
37 #define SPFI_CONTROL_SOFT_RESET BIT(11)
38 #define SPFI_CONTROL_SEND_DMA BIT(10)
39 #define SPFI_CONTROL_GET_DMA BIT(9)
40 #define SPFI_CONTROL_SE BIT(8)
41 #define SPFI_CONTROL_TMODE_SHIFT 5
42 #define SPFI_CONTROL_TMODE_MASK 0x7
43 #define SPFI_CONTROL_TMODE_SINGLE 0
44 #define SPFI_CONTROL_TMODE_DUAL 1
45 #define SPFI_CONTROL_TMODE_QUAD 2
46 #define SPFI_CONTROL_SPFI_EN BIT(0)
48 #define SPFI_TRANSACTION 0x18
49 #define SPFI_TRANSACTION_TSIZE_SHIFT 16
50 #define SPFI_TRANSACTION_TSIZE_MASK 0xffff
52 #define SPFI_PORT_STATE 0x1c
53 #define SPFI_PORT_STATE_DEV_SEL_SHIFT 20
54 #define SPFI_PORT_STATE_DEV_SEL_MASK 0x7
55 #define SPFI_PORT_STATE_CK_POL(x) BIT(19 - (x))
56 #define SPFI_PORT_STATE_CK_PHASE(x) BIT(14 - (x))
58 #define SPFI_TX_32BIT_VALID_DATA 0x20
59 #define SPFI_TX_8BIT_VALID_DATA 0x24
60 #define SPFI_RX_32BIT_VALID_DATA 0x28
61 #define SPFI_RX_8BIT_VALID_DATA 0x2c
63 #define SPFI_INTERRUPT_STATUS 0x30
64 #define SPFI_INTERRUPT_ENABLE 0x34
65 #define SPFI_INTERRUPT_CLEAR 0x38
66 #define SPFI_INTERRUPT_IACCESS BIT(12)
67 #define SPFI_INTERRUPT_GDEX8BIT BIT(11)
68 #define SPFI_INTERRUPT_ALLDONETRIG BIT(9)
69 #define SPFI_INTERRUPT_GDFUL BIT(8)
70 #define SPFI_INTERRUPT_GDHF BIT(7)
71 #define SPFI_INTERRUPT_GDEX32BIT BIT(6)
72 #define SPFI_INTERRUPT_GDTRIG BIT(5)
73 #define SPFI_INTERRUPT_SDFUL BIT(3)
74 #define SPFI_INTERRUPT_SDHF BIT(2)
75 #define SPFI_INTERRUPT_SDE BIT(1)
76 #define SPFI_INTERRUPT_SDTRIG BIT(0)
79 * There are four parallel FIFOs of 16 bytes each. The word buffer
80 * (*_32BIT_VALID_DATA) accesses all four FIFOs at once, resulting in an
81 * effective FIFO size of 64 bytes. The byte buffer (*_8BIT_VALID_DATA)
82 * accesses only a single FIFO, resulting in an effective FIFO size of
85 #define SPFI_32BIT_FIFO_SIZE 64
86 #define SPFI_8BIT_FIFO_SIZE 16
90 struct spi_master
*master
;
99 struct dma_chan
*rx_ch
;
100 struct dma_chan
*tx_ch
;
105 struct img_spfi_device_data
{
109 static inline u32
spfi_readl(struct img_spfi
*spfi
, u32 reg
)
111 return readl(spfi
->regs
+ reg
);
114 static inline void spfi_writel(struct img_spfi
*spfi
, u32 val
, u32 reg
)
116 writel(val
, spfi
->regs
+ reg
);
119 static inline void spfi_start(struct img_spfi
*spfi
)
123 val
= spfi_readl(spfi
, SPFI_CONTROL
);
124 val
|= SPFI_CONTROL_SPFI_EN
;
125 spfi_writel(spfi
, val
, SPFI_CONTROL
);
128 static inline void spfi_reset(struct img_spfi
*spfi
)
130 spfi_writel(spfi
, SPFI_CONTROL_SOFT_RESET
, SPFI_CONTROL
);
131 spfi_writel(spfi
, 0, SPFI_CONTROL
);
134 static int spfi_wait_all_done(struct img_spfi
*spfi
)
136 unsigned long timeout
= jiffies
+ msecs_to_jiffies(50);
138 while (time_before(jiffies
, timeout
)) {
139 u32 status
= spfi_readl(spfi
, SPFI_INTERRUPT_STATUS
);
141 if (status
& SPFI_INTERRUPT_ALLDONETRIG
) {
142 spfi_writel(spfi
, SPFI_INTERRUPT_ALLDONETRIG
,
143 SPFI_INTERRUPT_CLEAR
);
149 dev_err(spfi
->dev
, "Timed out waiting for transaction to complete\n");
155 static unsigned int spfi_pio_write32(struct img_spfi
*spfi
, const u32
*buf
,
158 unsigned int count
= 0;
161 while (count
< max
/ 4) {
162 spfi_writel(spfi
, SPFI_INTERRUPT_SDFUL
, SPFI_INTERRUPT_CLEAR
);
163 status
= spfi_readl(spfi
, SPFI_INTERRUPT_STATUS
);
164 if (status
& SPFI_INTERRUPT_SDFUL
)
166 spfi_writel(spfi
, buf
[count
], SPFI_TX_32BIT_VALID_DATA
);
173 static unsigned int spfi_pio_write8(struct img_spfi
*spfi
, const u8
*buf
,
176 unsigned int count
= 0;
179 while (count
< max
) {
180 spfi_writel(spfi
, SPFI_INTERRUPT_SDFUL
, SPFI_INTERRUPT_CLEAR
);
181 status
= spfi_readl(spfi
, SPFI_INTERRUPT_STATUS
);
182 if (status
& SPFI_INTERRUPT_SDFUL
)
184 spfi_writel(spfi
, buf
[count
], SPFI_TX_8BIT_VALID_DATA
);
191 static unsigned int spfi_pio_read32(struct img_spfi
*spfi
, u32
*buf
,
194 unsigned int count
= 0;
197 while (count
< max
/ 4) {
198 spfi_writel(spfi
, SPFI_INTERRUPT_GDEX32BIT
,
199 SPFI_INTERRUPT_CLEAR
);
200 status
= spfi_readl(spfi
, SPFI_INTERRUPT_STATUS
);
201 if (!(status
& SPFI_INTERRUPT_GDEX32BIT
))
203 buf
[count
] = spfi_readl(spfi
, SPFI_RX_32BIT_VALID_DATA
);
210 static unsigned int spfi_pio_read8(struct img_spfi
*spfi
, u8
*buf
,
213 unsigned int count
= 0;
216 while (count
< max
) {
217 spfi_writel(spfi
, SPFI_INTERRUPT_GDEX8BIT
,
218 SPFI_INTERRUPT_CLEAR
);
219 status
= spfi_readl(spfi
, SPFI_INTERRUPT_STATUS
);
220 if (!(status
& SPFI_INTERRUPT_GDEX8BIT
))
222 buf
[count
] = spfi_readl(spfi
, SPFI_RX_8BIT_VALID_DATA
);
229 static int img_spfi_start_pio(struct spi_master
*master
,
230 struct spi_device
*spi
,
231 struct spi_transfer
*xfer
)
233 struct img_spfi
*spfi
= spi_master_get_devdata(spi
->master
);
234 unsigned int tx_bytes
= 0, rx_bytes
= 0;
235 const void *tx_buf
= xfer
->tx_buf
;
236 void *rx_buf
= xfer
->rx_buf
;
237 unsigned long timeout
;
241 tx_bytes
= xfer
->len
;
243 rx_bytes
= xfer
->len
;
248 msecs_to_jiffies(xfer
->len
* 8 * 1000 / xfer
->speed_hz
+ 100);
249 while ((tx_bytes
> 0 || rx_bytes
> 0) &&
250 time_before(jiffies
, timeout
)) {
251 unsigned int tx_count
, rx_count
;
254 tx_count
= spfi_pio_write32(spfi
, tx_buf
, tx_bytes
);
256 tx_count
= spfi_pio_write8(spfi
, tx_buf
, tx_bytes
);
259 rx_count
= spfi_pio_read32(spfi
, rx_buf
, rx_bytes
);
261 rx_count
= spfi_pio_read8(spfi
, rx_buf
, rx_bytes
);
265 tx_bytes
-= tx_count
;
266 rx_bytes
-= rx_count
;
271 if (rx_bytes
> 0 || tx_bytes
> 0) {
272 dev_err(spfi
->dev
, "PIO transfer timed out\n");
276 ret
= spfi_wait_all_done(spfi
);
283 static void img_spfi_dma_rx_cb(void *data
)
285 struct img_spfi
*spfi
= data
;
288 spfi_wait_all_done(spfi
);
290 spin_lock_irqsave(&spfi
->lock
, flags
);
291 spfi
->rx_dma_busy
= false;
292 if (!spfi
->tx_dma_busy
)
293 spi_finalize_current_transfer(spfi
->master
);
294 spin_unlock_irqrestore(&spfi
->lock
, flags
);
297 static void img_spfi_dma_tx_cb(void *data
)
299 struct img_spfi
*spfi
= data
;
302 spfi_wait_all_done(spfi
);
304 spin_lock_irqsave(&spfi
->lock
, flags
);
305 spfi
->tx_dma_busy
= false;
306 if (!spfi
->rx_dma_busy
)
307 spi_finalize_current_transfer(spfi
->master
);
308 spin_unlock_irqrestore(&spfi
->lock
, flags
);
311 static int img_spfi_start_dma(struct spi_master
*master
,
312 struct spi_device
*spi
,
313 struct spi_transfer
*xfer
)
315 struct img_spfi
*spfi
= spi_master_get_devdata(spi
->master
);
316 struct dma_async_tx_descriptor
*rxdesc
= NULL
, *txdesc
= NULL
;
317 struct dma_slave_config rxconf
, txconf
;
319 spfi
->rx_dma_busy
= false;
320 spfi
->tx_dma_busy
= false;
323 rxconf
.direction
= DMA_DEV_TO_MEM
;
324 if (xfer
->len
% 4 == 0) {
325 rxconf
.src_addr
= spfi
->phys
+ SPFI_RX_32BIT_VALID_DATA
;
326 rxconf
.src_addr_width
= 4;
327 rxconf
.src_maxburst
= 4;
329 rxconf
.src_addr
= spfi
->phys
+ SPFI_RX_8BIT_VALID_DATA
;
330 rxconf
.src_addr_width
= 1;
331 rxconf
.src_maxburst
= 4;
333 dmaengine_slave_config(spfi
->rx_ch
, &rxconf
);
335 rxdesc
= dmaengine_prep_slave_sg(spfi
->rx_ch
, xfer
->rx_sg
.sgl
,
342 rxdesc
->callback
= img_spfi_dma_rx_cb
;
343 rxdesc
->callback_param
= spfi
;
347 txconf
.direction
= DMA_MEM_TO_DEV
;
348 if (xfer
->len
% 4 == 0) {
349 txconf
.dst_addr
= spfi
->phys
+ SPFI_TX_32BIT_VALID_DATA
;
350 txconf
.dst_addr_width
= 4;
351 txconf
.dst_maxburst
= 4;
353 txconf
.dst_addr
= spfi
->phys
+ SPFI_TX_8BIT_VALID_DATA
;
354 txconf
.dst_addr_width
= 1;
355 txconf
.dst_maxburst
= 4;
357 dmaengine_slave_config(spfi
->tx_ch
, &txconf
);
359 txdesc
= dmaengine_prep_slave_sg(spfi
->tx_ch
, xfer
->tx_sg
.sgl
,
366 txdesc
->callback
= img_spfi_dma_tx_cb
;
367 txdesc
->callback_param
= spfi
;
371 spfi
->rx_dma_busy
= true;
372 dmaengine_submit(rxdesc
);
373 dma_async_issue_pending(spfi
->rx_ch
);
379 spfi
->tx_dma_busy
= true;
380 dmaengine_submit(txdesc
);
381 dma_async_issue_pending(spfi
->tx_ch
);
387 dmaengine_terminate_all(spfi
->rx_ch
);
388 dmaengine_terminate_all(spfi
->tx_ch
);
392 static void img_spfi_handle_err(struct spi_master
*master
,
393 struct spi_message
*msg
)
395 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
399 * Stop all DMA and reset the controller if the previous transaction
400 * timed-out and never completed it's DMA.
402 spin_lock_irqsave(&spfi
->lock
, flags
);
403 if (spfi
->tx_dma_busy
|| spfi
->rx_dma_busy
) {
404 spfi
->tx_dma_busy
= false;
405 spfi
->rx_dma_busy
= false;
407 dmaengine_terminate_all(spfi
->tx_ch
);
408 dmaengine_terminate_all(spfi
->rx_ch
);
410 spin_unlock_irqrestore(&spfi
->lock
, flags
);
413 static int img_spfi_prepare(struct spi_master
*master
, struct spi_message
*msg
)
415 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
418 val
= spfi_readl(spfi
, SPFI_PORT_STATE
);
419 val
&= ~(SPFI_PORT_STATE_DEV_SEL_MASK
<<
420 SPFI_PORT_STATE_DEV_SEL_SHIFT
);
421 val
|= msg
->spi
->chip_select
<< SPFI_PORT_STATE_DEV_SEL_SHIFT
;
422 if (msg
->spi
->mode
& SPI_CPHA
)
423 val
|= SPFI_PORT_STATE_CK_PHASE(msg
->spi
->chip_select
);
425 val
&= ~SPFI_PORT_STATE_CK_PHASE(msg
->spi
->chip_select
);
426 if (msg
->spi
->mode
& SPI_CPOL
)
427 val
|= SPFI_PORT_STATE_CK_POL(msg
->spi
->chip_select
);
429 val
&= ~SPFI_PORT_STATE_CK_POL(msg
->spi
->chip_select
);
430 spfi_writel(spfi
, val
, SPFI_PORT_STATE
);
435 static int img_spfi_unprepare(struct spi_master
*master
,
436 struct spi_message
*msg
)
438 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
445 static int img_spfi_setup(struct spi_device
*spi
)
448 struct img_spfi_device_data
*spfi_data
= spi_get_ctldata(spi
);
451 spfi_data
= kzalloc(sizeof(*spfi_data
), GFP_KERNEL
);
454 spfi_data
->gpio_requested
= false;
455 spi_set_ctldata(spi
, spfi_data
);
457 if (!spfi_data
->gpio_requested
) {
458 ret
= gpio_request_one(spi
->cs_gpio
,
459 (spi
->mode
& SPI_CS_HIGH
) ?
460 GPIOF_OUT_INIT_LOW
: GPIOF_OUT_INIT_HIGH
,
461 dev_name(&spi
->dev
));
463 dev_err(&spi
->dev
, "can't request chipselect gpio %d\n",
466 spfi_data
->gpio_requested
= true;
468 if (gpio_is_valid(spi
->cs_gpio
)) {
469 int mode
= ((spi
->mode
& SPI_CS_HIGH
) ?
470 GPIOF_OUT_INIT_LOW
: GPIOF_OUT_INIT_HIGH
);
472 ret
= gpio_direction_output(spi
->cs_gpio
, mode
);
474 dev_err(&spi
->dev
, "chipselect gpio %d setup failed (%d)\n",
481 static void img_spfi_cleanup(struct spi_device
*spi
)
483 struct img_spfi_device_data
*spfi_data
= spi_get_ctldata(spi
);
486 if (spfi_data
->gpio_requested
)
487 gpio_free(spi
->cs_gpio
);
489 spi_set_ctldata(spi
, NULL
);
493 static void img_spfi_config(struct spi_master
*master
, struct spi_device
*spi
,
494 struct spi_transfer
*xfer
)
496 struct img_spfi
*spfi
= spi_master_get_devdata(spi
->master
);
500 * output = spfi_clk * (BITCLK / 512), where BITCLK must be a
501 * power of 2 up to 128
503 div
= DIV_ROUND_UP(clk_get_rate(spfi
->spfi_clk
), xfer
->speed_hz
);
504 div
= clamp(512 / (1 << get_count_order(div
)), 1, 128);
506 val
= spfi_readl(spfi
, SPFI_DEVICE_PARAMETER(spi
->chip_select
));
507 val
&= ~(SPFI_DEVICE_PARAMETER_BITCLK_MASK
<<
508 SPFI_DEVICE_PARAMETER_BITCLK_SHIFT
);
509 val
|= div
<< SPFI_DEVICE_PARAMETER_BITCLK_SHIFT
;
510 spfi_writel(spfi
, val
, SPFI_DEVICE_PARAMETER(spi
->chip_select
));
512 spfi_writel(spfi
, xfer
->len
<< SPFI_TRANSACTION_TSIZE_SHIFT
,
515 val
= spfi_readl(spfi
, SPFI_CONTROL
);
516 val
&= ~(SPFI_CONTROL_SEND_DMA
| SPFI_CONTROL_GET_DMA
);
518 val
|= SPFI_CONTROL_SEND_DMA
;
520 val
|= SPFI_CONTROL_GET_DMA
;
521 val
&= ~(SPFI_CONTROL_TMODE_MASK
<< SPFI_CONTROL_TMODE_SHIFT
);
522 if (xfer
->tx_nbits
== SPI_NBITS_DUAL
&&
523 xfer
->rx_nbits
== SPI_NBITS_DUAL
)
524 val
|= SPFI_CONTROL_TMODE_DUAL
<< SPFI_CONTROL_TMODE_SHIFT
;
525 else if (xfer
->tx_nbits
== SPI_NBITS_QUAD
&&
526 xfer
->rx_nbits
== SPI_NBITS_QUAD
)
527 val
|= SPFI_CONTROL_TMODE_QUAD
<< SPFI_CONTROL_TMODE_SHIFT
;
528 val
|= SPFI_CONTROL_SE
;
529 spfi_writel(spfi
, val
, SPFI_CONTROL
);
532 static int img_spfi_transfer_one(struct spi_master
*master
,
533 struct spi_device
*spi
,
534 struct spi_transfer
*xfer
)
536 struct img_spfi
*spfi
= spi_master_get_devdata(spi
->master
);
539 if (xfer
->len
> SPFI_TRANSACTION_TSIZE_MASK
) {
541 "Transfer length (%d) is greater than the max supported (%d)",
542 xfer
->len
, SPFI_TRANSACTION_TSIZE_MASK
);
546 img_spfi_config(master
, spi
, xfer
);
547 if (master
->can_dma
&& master
->can_dma(master
, spi
, xfer
))
548 ret
= img_spfi_start_dma(master
, spi
, xfer
);
550 ret
= img_spfi_start_pio(master
, spi
, xfer
);
555 static bool img_spfi_can_dma(struct spi_master
*master
, struct spi_device
*spi
,
556 struct spi_transfer
*xfer
)
558 if (xfer
->len
> SPFI_32BIT_FIFO_SIZE
)
563 static irqreturn_t
img_spfi_irq(int irq
, void *dev_id
)
565 struct img_spfi
*spfi
= (struct img_spfi
*)dev_id
;
568 status
= spfi_readl(spfi
, SPFI_INTERRUPT_STATUS
);
569 if (status
& SPFI_INTERRUPT_IACCESS
) {
570 spfi_writel(spfi
, SPFI_INTERRUPT_IACCESS
, SPFI_INTERRUPT_CLEAR
);
571 dev_err(spfi
->dev
, "Illegal access interrupt");
578 static int img_spfi_probe(struct platform_device
*pdev
)
580 struct spi_master
*master
;
581 struct img_spfi
*spfi
;
582 struct resource
*res
;
586 master
= spi_alloc_master(&pdev
->dev
, sizeof(*spfi
));
589 platform_set_drvdata(pdev
, master
);
591 spfi
= spi_master_get_devdata(master
);
592 spfi
->dev
= &pdev
->dev
;
593 spfi
->master
= master
;
594 spin_lock_init(&spfi
->lock
);
596 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
597 spfi
->regs
= devm_ioremap_resource(spfi
->dev
, res
);
598 if (IS_ERR(spfi
->regs
)) {
599 ret
= PTR_ERR(spfi
->regs
);
602 spfi
->phys
= res
->start
;
604 spfi
->irq
= platform_get_irq(pdev
, 0);
609 ret
= devm_request_irq(spfi
->dev
, spfi
->irq
, img_spfi_irq
,
610 IRQ_TYPE_LEVEL_HIGH
, dev_name(spfi
->dev
), spfi
);
614 spfi
->sys_clk
= devm_clk_get(spfi
->dev
, "sys");
615 if (IS_ERR(spfi
->sys_clk
)) {
616 ret
= PTR_ERR(spfi
->sys_clk
);
619 spfi
->spfi_clk
= devm_clk_get(spfi
->dev
, "spfi");
620 if (IS_ERR(spfi
->spfi_clk
)) {
621 ret
= PTR_ERR(spfi
->spfi_clk
);
625 ret
= clk_prepare_enable(spfi
->sys_clk
);
628 ret
= clk_prepare_enable(spfi
->spfi_clk
);
634 * Only enable the error (IACCESS) interrupt. In PIO mode we'll
635 * poll the status of the FIFOs.
637 spfi_writel(spfi
, SPFI_INTERRUPT_IACCESS
, SPFI_INTERRUPT_ENABLE
);
639 master
->auto_runtime_pm
= true;
640 master
->bus_num
= pdev
->id
;
641 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_TX_DUAL
| SPI_RX_DUAL
;
642 if (of_property_read_bool(spfi
->dev
->of_node
, "img,supports-quad-mode"))
643 master
->mode_bits
|= SPI_TX_QUAD
| SPI_RX_QUAD
;
644 master
->dev
.of_node
= pdev
->dev
.of_node
;
645 master
->bits_per_word_mask
= SPI_BPW_MASK(32) | SPI_BPW_MASK(8);
646 master
->max_speed_hz
= clk_get_rate(spfi
->spfi_clk
) / 4;
647 master
->min_speed_hz
= clk_get_rate(spfi
->spfi_clk
) / 512;
650 * Maximum speed supported by spfi is limited to the lower value
651 * between 1/4 of the SPFI clock or to "spfi-max-frequency"
652 * defined in the device tree.
653 * If no value is defined in the device tree assume the maximum
654 * speed supported to be 1/4 of the SPFI clock.
656 if (!of_property_read_u32(spfi
->dev
->of_node
, "spfi-max-frequency",
658 if (master
->max_speed_hz
> max_speed_hz
)
659 master
->max_speed_hz
= max_speed_hz
;
662 master
->setup
= img_spfi_setup
;
663 master
->cleanup
= img_spfi_cleanup
;
664 master
->transfer_one
= img_spfi_transfer_one
;
665 master
->prepare_message
= img_spfi_prepare
;
666 master
->unprepare_message
= img_spfi_unprepare
;
667 master
->handle_err
= img_spfi_handle_err
;
669 spfi
->tx_ch
= dma_request_chan(spfi
->dev
, "tx");
670 if (IS_ERR(spfi
->tx_ch
)) {
671 ret
= PTR_ERR(spfi
->tx_ch
);
673 if (ret
== -EPROBE_DEFER
)
677 spfi
->rx_ch
= dma_request_chan(spfi
->dev
, "rx");
678 if (IS_ERR(spfi
->rx_ch
)) {
679 ret
= PTR_ERR(spfi
->rx_ch
);
681 if (ret
== -EPROBE_DEFER
)
685 if (!spfi
->tx_ch
|| !spfi
->rx_ch
) {
687 dma_release_channel(spfi
->tx_ch
);
689 dma_release_channel(spfi
->rx_ch
);
692 dev_warn(spfi
->dev
, "Failed to get DMA channels, falling back to PIO mode\n");
694 master
->dma_tx
= spfi
->tx_ch
;
695 master
->dma_rx
= spfi
->rx_ch
;
696 master
->can_dma
= img_spfi_can_dma
;
699 pm_runtime_set_active(spfi
->dev
);
700 pm_runtime_enable(spfi
->dev
);
702 ret
= devm_spi_register_master(spfi
->dev
, master
);
709 pm_runtime_disable(spfi
->dev
);
711 dma_release_channel(spfi
->rx_ch
);
713 dma_release_channel(spfi
->tx_ch
);
714 clk_disable_unprepare(spfi
->spfi_clk
);
716 clk_disable_unprepare(spfi
->sys_clk
);
718 spi_master_put(master
);
723 static int img_spfi_remove(struct platform_device
*pdev
)
725 struct spi_master
*master
= platform_get_drvdata(pdev
);
726 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
729 dma_release_channel(spfi
->tx_ch
);
731 dma_release_channel(spfi
->rx_ch
);
733 pm_runtime_disable(spfi
->dev
);
734 if (!pm_runtime_status_suspended(spfi
->dev
)) {
735 clk_disable_unprepare(spfi
->spfi_clk
);
736 clk_disable_unprepare(spfi
->sys_clk
);
743 static int img_spfi_runtime_suspend(struct device
*dev
)
745 struct spi_master
*master
= dev_get_drvdata(dev
);
746 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
748 clk_disable_unprepare(spfi
->spfi_clk
);
749 clk_disable_unprepare(spfi
->sys_clk
);
754 static int img_spfi_runtime_resume(struct device
*dev
)
756 struct spi_master
*master
= dev_get_drvdata(dev
);
757 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
760 ret
= clk_prepare_enable(spfi
->sys_clk
);
763 ret
= clk_prepare_enable(spfi
->spfi_clk
);
765 clk_disable_unprepare(spfi
->sys_clk
);
771 #endif /* CONFIG_PM */
773 #ifdef CONFIG_PM_SLEEP
774 static int img_spfi_suspend(struct device
*dev
)
776 struct spi_master
*master
= dev_get_drvdata(dev
);
778 return spi_master_suspend(master
);
781 static int img_spfi_resume(struct device
*dev
)
783 struct spi_master
*master
= dev_get_drvdata(dev
);
784 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
787 ret
= pm_runtime_get_sync(dev
);
793 return spi_master_resume(master
);
795 #endif /* CONFIG_PM_SLEEP */
797 static const struct dev_pm_ops img_spfi_pm_ops
= {
798 SET_RUNTIME_PM_OPS(img_spfi_runtime_suspend
, img_spfi_runtime_resume
,
800 SET_SYSTEM_SLEEP_PM_OPS(img_spfi_suspend
, img_spfi_resume
)
803 static const struct of_device_id img_spfi_of_match
[] = {
804 { .compatible
= "img,spfi", },
807 MODULE_DEVICE_TABLE(of
, img_spfi_of_match
);
809 static struct platform_driver img_spfi_driver
= {
812 .pm
= &img_spfi_pm_ops
,
813 .of_match_table
= of_match_ptr(img_spfi_of_match
),
815 .probe
= img_spfi_probe
,
816 .remove
= img_spfi_remove
,
818 module_platform_driver(img_spfi_driver
);
820 MODULE_DESCRIPTION("IMG SPFI controller driver");
821 MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
822 MODULE_LICENSE("GPL v2");