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/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/scatterlist.h>
20 #include <linux/slab.h>
21 #include <linux/spi/spi.h>
22 #include <linux/spinlock.h>
24 #define SPFI_DEVICE_PARAMETER(x) (0x00 + 0x4 * (x))
25 #define SPFI_DEVICE_PARAMETER_BITCLK_SHIFT 24
26 #define SPFI_DEVICE_PARAMETER_BITCLK_MASK 0xff
27 #define SPFI_DEVICE_PARAMETER_CSSETUP_SHIFT 16
28 #define SPFI_DEVICE_PARAMETER_CSSETUP_MASK 0xff
29 #define SPFI_DEVICE_PARAMETER_CSHOLD_SHIFT 8
30 #define SPFI_DEVICE_PARAMETER_CSHOLD_MASK 0xff
31 #define SPFI_DEVICE_PARAMETER_CSDELAY_SHIFT 0
32 #define SPFI_DEVICE_PARAMETER_CSDELAY_MASK 0xff
34 #define SPFI_CONTROL 0x14
35 #define SPFI_CONTROL_CONTINUE BIT(12)
36 #define SPFI_CONTROL_SOFT_RESET BIT(11)
37 #define SPFI_CONTROL_SEND_DMA BIT(10)
38 #define SPFI_CONTROL_GET_DMA BIT(9)
39 #define SPFI_CONTROL_SE BIT(8)
40 #define SPFI_CONTROL_TMODE_SHIFT 5
41 #define SPFI_CONTROL_TMODE_MASK 0x7
42 #define SPFI_CONTROL_TMODE_SINGLE 0
43 #define SPFI_CONTROL_TMODE_DUAL 1
44 #define SPFI_CONTROL_TMODE_QUAD 2
45 #define SPFI_CONTROL_SPFI_EN BIT(0)
47 #define SPFI_TRANSACTION 0x18
48 #define SPFI_TRANSACTION_TSIZE_SHIFT 16
49 #define SPFI_TRANSACTION_TSIZE_MASK 0xffff
51 #define SPFI_PORT_STATE 0x1c
52 #define SPFI_PORT_STATE_DEV_SEL_SHIFT 20
53 #define SPFI_PORT_STATE_DEV_SEL_MASK 0x7
54 #define SPFI_PORT_STATE_CK_POL(x) BIT(19 - (x))
55 #define SPFI_PORT_STATE_CK_PHASE(x) BIT(14 - (x))
57 #define SPFI_TX_32BIT_VALID_DATA 0x20
58 #define SPFI_TX_8BIT_VALID_DATA 0x24
59 #define SPFI_RX_32BIT_VALID_DATA 0x28
60 #define SPFI_RX_8BIT_VALID_DATA 0x2c
62 #define SPFI_INTERRUPT_STATUS 0x30
63 #define SPFI_INTERRUPT_ENABLE 0x34
64 #define SPFI_INTERRUPT_CLEAR 0x38
65 #define SPFI_INTERRUPT_IACCESS BIT(12)
66 #define SPFI_INTERRUPT_GDEX8BIT BIT(11)
67 #define SPFI_INTERRUPT_ALLDONETRIG BIT(9)
68 #define SPFI_INTERRUPT_GDFUL BIT(8)
69 #define SPFI_INTERRUPT_GDHF BIT(7)
70 #define SPFI_INTERRUPT_GDEX32BIT BIT(6)
71 #define SPFI_INTERRUPT_GDTRIG BIT(5)
72 #define SPFI_INTERRUPT_SDFUL BIT(3)
73 #define SPFI_INTERRUPT_SDHF BIT(2)
74 #define SPFI_INTERRUPT_SDE BIT(1)
75 #define SPFI_INTERRUPT_SDTRIG BIT(0)
78 * There are four parallel FIFOs of 16 bytes each. The word buffer
79 * (*_32BIT_VALID_DATA) accesses all four FIFOs at once, resulting in an
80 * effective FIFO size of 64 bytes. The byte buffer (*_8BIT_VALID_DATA)
81 * accesses only a single FIFO, resulting in an effective FIFO size of
84 #define SPFI_32BIT_FIFO_SIZE 64
85 #define SPFI_8BIT_FIFO_SIZE 16
89 struct spi_master
*master
;
98 struct dma_chan
*rx_ch
;
99 struct dma_chan
*tx_ch
;
104 static inline u32
spfi_readl(struct img_spfi
*spfi
, u32 reg
)
106 return readl(spfi
->regs
+ reg
);
109 static inline void spfi_writel(struct img_spfi
*spfi
, u32 val
, u32 reg
)
111 writel(val
, spfi
->regs
+ reg
);
114 static inline void spfi_start(struct img_spfi
*spfi
)
118 val
= spfi_readl(spfi
, SPFI_CONTROL
);
119 val
|= SPFI_CONTROL_SPFI_EN
;
120 spfi_writel(spfi
, val
, SPFI_CONTROL
);
123 static inline void spfi_reset(struct img_spfi
*spfi
)
125 spfi_writel(spfi
, SPFI_CONTROL_SOFT_RESET
, SPFI_CONTROL
);
126 spfi_writel(spfi
, 0, SPFI_CONTROL
);
129 static int spfi_wait_all_done(struct img_spfi
*spfi
)
131 unsigned long timeout
= jiffies
+ msecs_to_jiffies(50);
133 while (time_before(jiffies
, timeout
)) {
134 u32 status
= spfi_readl(spfi
, SPFI_INTERRUPT_STATUS
);
136 if (status
& SPFI_INTERRUPT_ALLDONETRIG
) {
137 spfi_writel(spfi
, SPFI_INTERRUPT_ALLDONETRIG
,
138 SPFI_INTERRUPT_CLEAR
);
144 dev_err(spfi
->dev
, "Timed out waiting for transaction to complete\n");
150 static unsigned int spfi_pio_write32(struct img_spfi
*spfi
, const u32
*buf
,
153 unsigned int count
= 0;
156 while (count
< max
/ 4) {
157 spfi_writel(spfi
, SPFI_INTERRUPT_SDFUL
, SPFI_INTERRUPT_CLEAR
);
158 status
= spfi_readl(spfi
, SPFI_INTERRUPT_STATUS
);
159 if (status
& SPFI_INTERRUPT_SDFUL
)
161 spfi_writel(spfi
, buf
[count
], SPFI_TX_32BIT_VALID_DATA
);
168 static unsigned int spfi_pio_write8(struct img_spfi
*spfi
, const u8
*buf
,
171 unsigned int count
= 0;
174 while (count
< max
) {
175 spfi_writel(spfi
, SPFI_INTERRUPT_SDFUL
, SPFI_INTERRUPT_CLEAR
);
176 status
= spfi_readl(spfi
, SPFI_INTERRUPT_STATUS
);
177 if (status
& SPFI_INTERRUPT_SDFUL
)
179 spfi_writel(spfi
, buf
[count
], SPFI_TX_8BIT_VALID_DATA
);
186 static unsigned int spfi_pio_read32(struct img_spfi
*spfi
, u32
*buf
,
189 unsigned int count
= 0;
192 while (count
< max
/ 4) {
193 spfi_writel(spfi
, SPFI_INTERRUPT_GDEX32BIT
,
194 SPFI_INTERRUPT_CLEAR
);
195 status
= spfi_readl(spfi
, SPFI_INTERRUPT_STATUS
);
196 if (!(status
& SPFI_INTERRUPT_GDEX32BIT
))
198 buf
[count
] = spfi_readl(spfi
, SPFI_RX_32BIT_VALID_DATA
);
205 static unsigned int spfi_pio_read8(struct img_spfi
*spfi
, u8
*buf
,
208 unsigned int count
= 0;
211 while (count
< max
) {
212 spfi_writel(spfi
, SPFI_INTERRUPT_GDEX8BIT
,
213 SPFI_INTERRUPT_CLEAR
);
214 status
= spfi_readl(spfi
, SPFI_INTERRUPT_STATUS
);
215 if (!(status
& SPFI_INTERRUPT_GDEX8BIT
))
217 buf
[count
] = spfi_readl(spfi
, SPFI_RX_8BIT_VALID_DATA
);
224 static int img_spfi_start_pio(struct spi_master
*master
,
225 struct spi_device
*spi
,
226 struct spi_transfer
*xfer
)
228 struct img_spfi
*spfi
= spi_master_get_devdata(spi
->master
);
229 unsigned int tx_bytes
= 0, rx_bytes
= 0;
230 const void *tx_buf
= xfer
->tx_buf
;
231 void *rx_buf
= xfer
->rx_buf
;
232 unsigned long timeout
;
236 tx_bytes
= xfer
->len
;
238 rx_bytes
= xfer
->len
;
243 msecs_to_jiffies(xfer
->len
* 8 * 1000 / xfer
->speed_hz
+ 100);
244 while ((tx_bytes
> 0 || rx_bytes
> 0) &&
245 time_before(jiffies
, timeout
)) {
246 unsigned int tx_count
, rx_count
;
249 tx_count
= spfi_pio_write32(spfi
, tx_buf
, tx_bytes
);
251 tx_count
= spfi_pio_write8(spfi
, tx_buf
, tx_bytes
);
254 rx_count
= spfi_pio_read32(spfi
, rx_buf
, rx_bytes
);
256 rx_count
= spfi_pio_read8(spfi
, rx_buf
, rx_bytes
);
260 tx_bytes
-= tx_count
;
261 rx_bytes
-= rx_count
;
266 if (rx_bytes
> 0 || tx_bytes
> 0) {
267 dev_err(spfi
->dev
, "PIO transfer timed out\n");
271 ret
= spfi_wait_all_done(spfi
);
278 static void img_spfi_dma_rx_cb(void *data
)
280 struct img_spfi
*spfi
= data
;
283 spfi_wait_all_done(spfi
);
285 spin_lock_irqsave(&spfi
->lock
, flags
);
286 spfi
->rx_dma_busy
= false;
287 if (!spfi
->tx_dma_busy
)
288 spi_finalize_current_transfer(spfi
->master
);
289 spin_unlock_irqrestore(&spfi
->lock
, flags
);
292 static void img_spfi_dma_tx_cb(void *data
)
294 struct img_spfi
*spfi
= data
;
297 spfi_wait_all_done(spfi
);
299 spin_lock_irqsave(&spfi
->lock
, flags
);
300 spfi
->tx_dma_busy
= false;
301 if (!spfi
->rx_dma_busy
)
302 spi_finalize_current_transfer(spfi
->master
);
303 spin_unlock_irqrestore(&spfi
->lock
, flags
);
306 static int img_spfi_start_dma(struct spi_master
*master
,
307 struct spi_device
*spi
,
308 struct spi_transfer
*xfer
)
310 struct img_spfi
*spfi
= spi_master_get_devdata(spi
->master
);
311 struct dma_async_tx_descriptor
*rxdesc
= NULL
, *txdesc
= NULL
;
312 struct dma_slave_config rxconf
, txconf
;
314 spfi
->rx_dma_busy
= false;
315 spfi
->tx_dma_busy
= false;
318 rxconf
.direction
= DMA_DEV_TO_MEM
;
319 if (xfer
->len
% 4 == 0) {
320 rxconf
.src_addr
= spfi
->phys
+ SPFI_RX_32BIT_VALID_DATA
;
321 rxconf
.src_addr_width
= 4;
322 rxconf
.src_maxburst
= 4;
324 rxconf
.src_addr
= spfi
->phys
+ SPFI_RX_8BIT_VALID_DATA
;
325 rxconf
.src_addr_width
= 1;
326 rxconf
.src_maxburst
= 4;
328 dmaengine_slave_config(spfi
->rx_ch
, &rxconf
);
330 rxdesc
= dmaengine_prep_slave_sg(spfi
->rx_ch
, xfer
->rx_sg
.sgl
,
337 rxdesc
->callback
= img_spfi_dma_rx_cb
;
338 rxdesc
->callback_param
= spfi
;
342 txconf
.direction
= DMA_MEM_TO_DEV
;
343 if (xfer
->len
% 4 == 0) {
344 txconf
.dst_addr
= spfi
->phys
+ SPFI_TX_32BIT_VALID_DATA
;
345 txconf
.dst_addr_width
= 4;
346 txconf
.dst_maxburst
= 4;
348 txconf
.dst_addr
= spfi
->phys
+ SPFI_TX_8BIT_VALID_DATA
;
349 txconf
.dst_addr_width
= 1;
350 txconf
.dst_maxburst
= 4;
352 dmaengine_slave_config(spfi
->tx_ch
, &txconf
);
354 txdesc
= dmaengine_prep_slave_sg(spfi
->tx_ch
, xfer
->tx_sg
.sgl
,
361 txdesc
->callback
= img_spfi_dma_tx_cb
;
362 txdesc
->callback_param
= spfi
;
366 spfi
->rx_dma_busy
= true;
367 dmaengine_submit(rxdesc
);
368 dma_async_issue_pending(spfi
->rx_ch
);
374 spfi
->tx_dma_busy
= true;
375 dmaengine_submit(txdesc
);
376 dma_async_issue_pending(spfi
->tx_ch
);
382 dmaengine_terminate_all(spfi
->rx_ch
);
383 dmaengine_terminate_all(spfi
->tx_ch
);
387 static void img_spfi_handle_err(struct spi_master
*master
,
388 struct spi_message
*msg
)
390 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
394 * Stop all DMA and reset the controller if the previous transaction
395 * timed-out and never completed it's DMA.
397 spin_lock_irqsave(&spfi
->lock
, flags
);
398 if (spfi
->tx_dma_busy
|| spfi
->rx_dma_busy
) {
399 spfi
->tx_dma_busy
= false;
400 spfi
->rx_dma_busy
= false;
402 dmaengine_terminate_all(spfi
->tx_ch
);
403 dmaengine_terminate_all(spfi
->rx_ch
);
405 spin_unlock_irqrestore(&spfi
->lock
, flags
);
408 static int img_spfi_prepare(struct spi_master
*master
, struct spi_message
*msg
)
410 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
413 val
= spfi_readl(spfi
, SPFI_PORT_STATE
);
414 val
&= ~(SPFI_PORT_STATE_DEV_SEL_MASK
<<
415 SPFI_PORT_STATE_DEV_SEL_SHIFT
);
416 val
|= msg
->spi
->chip_select
<< SPFI_PORT_STATE_DEV_SEL_SHIFT
;
417 if (msg
->spi
->mode
& SPI_CPHA
)
418 val
|= SPFI_PORT_STATE_CK_PHASE(msg
->spi
->chip_select
);
420 val
&= ~SPFI_PORT_STATE_CK_PHASE(msg
->spi
->chip_select
);
421 if (msg
->spi
->mode
& SPI_CPOL
)
422 val
|= SPFI_PORT_STATE_CK_POL(msg
->spi
->chip_select
);
424 val
&= ~SPFI_PORT_STATE_CK_POL(msg
->spi
->chip_select
);
425 spfi_writel(spfi
, val
, SPFI_PORT_STATE
);
430 static int img_spfi_unprepare(struct spi_master
*master
,
431 struct spi_message
*msg
)
433 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
440 static void img_spfi_config(struct spi_master
*master
, struct spi_device
*spi
,
441 struct spi_transfer
*xfer
)
443 struct img_spfi
*spfi
= spi_master_get_devdata(spi
->master
);
447 * output = spfi_clk * (BITCLK / 512), where BITCLK must be a
448 * power of 2 up to 128
450 div
= DIV_ROUND_UP(clk_get_rate(spfi
->spfi_clk
), xfer
->speed_hz
);
451 div
= clamp(512 / (1 << get_count_order(div
)), 1, 128);
453 val
= spfi_readl(spfi
, SPFI_DEVICE_PARAMETER(spi
->chip_select
));
454 val
&= ~(SPFI_DEVICE_PARAMETER_BITCLK_MASK
<<
455 SPFI_DEVICE_PARAMETER_BITCLK_SHIFT
);
456 val
|= div
<< SPFI_DEVICE_PARAMETER_BITCLK_SHIFT
;
457 spfi_writel(spfi
, val
, SPFI_DEVICE_PARAMETER(spi
->chip_select
));
459 spfi_writel(spfi
, xfer
->len
<< SPFI_TRANSACTION_TSIZE_SHIFT
,
462 val
= spfi_readl(spfi
, SPFI_CONTROL
);
463 val
&= ~(SPFI_CONTROL_SEND_DMA
| SPFI_CONTROL_GET_DMA
);
465 val
|= SPFI_CONTROL_SEND_DMA
;
467 val
|= SPFI_CONTROL_GET_DMA
;
468 val
&= ~(SPFI_CONTROL_TMODE_MASK
<< SPFI_CONTROL_TMODE_SHIFT
);
469 if (xfer
->tx_nbits
== SPI_NBITS_DUAL
&&
470 xfer
->rx_nbits
== SPI_NBITS_DUAL
)
471 val
|= SPFI_CONTROL_TMODE_DUAL
<< SPFI_CONTROL_TMODE_SHIFT
;
472 else if (xfer
->tx_nbits
== SPI_NBITS_QUAD
&&
473 xfer
->rx_nbits
== SPI_NBITS_QUAD
)
474 val
|= SPFI_CONTROL_TMODE_QUAD
<< SPFI_CONTROL_TMODE_SHIFT
;
475 val
|= SPFI_CONTROL_SE
;
476 spfi_writel(spfi
, val
, SPFI_CONTROL
);
479 static int img_spfi_transfer_one(struct spi_master
*master
,
480 struct spi_device
*spi
,
481 struct spi_transfer
*xfer
)
483 struct img_spfi
*spfi
= spi_master_get_devdata(spi
->master
);
486 if (xfer
->len
> SPFI_TRANSACTION_TSIZE_MASK
) {
488 "Transfer length (%d) is greater than the max supported (%d)",
489 xfer
->len
, SPFI_TRANSACTION_TSIZE_MASK
);
493 img_spfi_config(master
, spi
, xfer
);
494 if (master
->can_dma
&& master
->can_dma(master
, spi
, xfer
))
495 ret
= img_spfi_start_dma(master
, spi
, xfer
);
497 ret
= img_spfi_start_pio(master
, spi
, xfer
);
502 static bool img_spfi_can_dma(struct spi_master
*master
, struct spi_device
*spi
,
503 struct spi_transfer
*xfer
)
505 if (xfer
->len
> SPFI_32BIT_FIFO_SIZE
)
510 static irqreturn_t
img_spfi_irq(int irq
, void *dev_id
)
512 struct img_spfi
*spfi
= (struct img_spfi
*)dev_id
;
515 status
= spfi_readl(spfi
, SPFI_INTERRUPT_STATUS
);
516 if (status
& SPFI_INTERRUPT_IACCESS
) {
517 spfi_writel(spfi
, SPFI_INTERRUPT_IACCESS
, SPFI_INTERRUPT_CLEAR
);
518 dev_err(spfi
->dev
, "Illegal access interrupt");
525 static int img_spfi_probe(struct platform_device
*pdev
)
527 struct spi_master
*master
;
528 struct img_spfi
*spfi
;
529 struct resource
*res
;
533 master
= spi_alloc_master(&pdev
->dev
, sizeof(*spfi
));
536 platform_set_drvdata(pdev
, master
);
538 spfi
= spi_master_get_devdata(master
);
539 spfi
->dev
= &pdev
->dev
;
540 spfi
->master
= master
;
541 spin_lock_init(&spfi
->lock
);
543 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
544 spfi
->regs
= devm_ioremap_resource(spfi
->dev
, res
);
545 if (IS_ERR(spfi
->regs
)) {
546 ret
= PTR_ERR(spfi
->regs
);
549 spfi
->phys
= res
->start
;
551 spfi
->irq
= platform_get_irq(pdev
, 0);
556 ret
= devm_request_irq(spfi
->dev
, spfi
->irq
, img_spfi_irq
,
557 IRQ_TYPE_LEVEL_HIGH
, dev_name(spfi
->dev
), spfi
);
561 spfi
->sys_clk
= devm_clk_get(spfi
->dev
, "sys");
562 if (IS_ERR(spfi
->sys_clk
)) {
563 ret
= PTR_ERR(spfi
->sys_clk
);
566 spfi
->spfi_clk
= devm_clk_get(spfi
->dev
, "spfi");
567 if (IS_ERR(spfi
->spfi_clk
)) {
568 ret
= PTR_ERR(spfi
->spfi_clk
);
572 ret
= clk_prepare_enable(spfi
->sys_clk
);
575 ret
= clk_prepare_enable(spfi
->spfi_clk
);
581 * Only enable the error (IACCESS) interrupt. In PIO mode we'll
582 * poll the status of the FIFOs.
584 spfi_writel(spfi
, SPFI_INTERRUPT_IACCESS
, SPFI_INTERRUPT_ENABLE
);
586 master
->auto_runtime_pm
= true;
587 master
->bus_num
= pdev
->id
;
588 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_TX_DUAL
| SPI_RX_DUAL
;
589 if (of_property_read_bool(spfi
->dev
->of_node
, "img,supports-quad-mode"))
590 master
->mode_bits
|= SPI_TX_QUAD
| SPI_RX_QUAD
;
591 master
->dev
.of_node
= pdev
->dev
.of_node
;
592 master
->bits_per_word_mask
= SPI_BPW_MASK(32) | SPI_BPW_MASK(8);
593 master
->max_speed_hz
= clk_get_rate(spfi
->spfi_clk
) / 4;
594 master
->min_speed_hz
= clk_get_rate(spfi
->spfi_clk
) / 512;
597 * Maximum speed supported by spfi is limited to the lower value
598 * between 1/4 of the SPFI clock or to "spfi-max-frequency"
599 * defined in the device tree.
600 * If no value is defined in the device tree assume the maximum
601 * speed supported to be 1/4 of the SPFI clock.
603 if (!of_property_read_u32(spfi
->dev
->of_node
, "spfi-max-frequency",
605 if (master
->max_speed_hz
> max_speed_hz
)
606 master
->max_speed_hz
= max_speed_hz
;
609 master
->transfer_one
= img_spfi_transfer_one
;
610 master
->prepare_message
= img_spfi_prepare
;
611 master
->unprepare_message
= img_spfi_unprepare
;
612 master
->handle_err
= img_spfi_handle_err
;
613 master
->use_gpio_descriptors
= true;
615 spfi
->tx_ch
= dma_request_chan(spfi
->dev
, "tx");
616 if (IS_ERR(spfi
->tx_ch
)) {
617 ret
= PTR_ERR(spfi
->tx_ch
);
619 if (ret
== -EPROBE_DEFER
)
623 spfi
->rx_ch
= dma_request_chan(spfi
->dev
, "rx");
624 if (IS_ERR(spfi
->rx_ch
)) {
625 ret
= PTR_ERR(spfi
->rx_ch
);
627 if (ret
== -EPROBE_DEFER
)
631 if (!spfi
->tx_ch
|| !spfi
->rx_ch
) {
633 dma_release_channel(spfi
->tx_ch
);
635 dma_release_channel(spfi
->rx_ch
);
638 dev_warn(spfi
->dev
, "Failed to get DMA channels, falling back to PIO mode\n");
640 master
->dma_tx
= spfi
->tx_ch
;
641 master
->dma_rx
= spfi
->rx_ch
;
642 master
->can_dma
= img_spfi_can_dma
;
645 pm_runtime_set_active(spfi
->dev
);
646 pm_runtime_enable(spfi
->dev
);
648 ret
= devm_spi_register_master(spfi
->dev
, master
);
655 pm_runtime_disable(spfi
->dev
);
657 dma_release_channel(spfi
->rx_ch
);
659 dma_release_channel(spfi
->tx_ch
);
660 clk_disable_unprepare(spfi
->spfi_clk
);
662 clk_disable_unprepare(spfi
->sys_clk
);
664 spi_master_put(master
);
669 static int img_spfi_remove(struct platform_device
*pdev
)
671 struct spi_master
*master
= platform_get_drvdata(pdev
);
672 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
675 dma_release_channel(spfi
->tx_ch
);
677 dma_release_channel(spfi
->rx_ch
);
679 pm_runtime_disable(spfi
->dev
);
680 if (!pm_runtime_status_suspended(spfi
->dev
)) {
681 clk_disable_unprepare(spfi
->spfi_clk
);
682 clk_disable_unprepare(spfi
->sys_clk
);
689 static int img_spfi_runtime_suspend(struct device
*dev
)
691 struct spi_master
*master
= dev_get_drvdata(dev
);
692 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
694 clk_disable_unprepare(spfi
->spfi_clk
);
695 clk_disable_unprepare(spfi
->sys_clk
);
700 static int img_spfi_runtime_resume(struct device
*dev
)
702 struct spi_master
*master
= dev_get_drvdata(dev
);
703 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
706 ret
= clk_prepare_enable(spfi
->sys_clk
);
709 ret
= clk_prepare_enable(spfi
->spfi_clk
);
711 clk_disable_unprepare(spfi
->sys_clk
);
717 #endif /* CONFIG_PM */
719 #ifdef CONFIG_PM_SLEEP
720 static int img_spfi_suspend(struct device
*dev
)
722 struct spi_master
*master
= dev_get_drvdata(dev
);
724 return spi_master_suspend(master
);
727 static int img_spfi_resume(struct device
*dev
)
729 struct spi_master
*master
= dev_get_drvdata(dev
);
730 struct img_spfi
*spfi
= spi_master_get_devdata(master
);
733 ret
= pm_runtime_get_sync(dev
);
735 pm_runtime_put_noidle(dev
);
741 return spi_master_resume(master
);
743 #endif /* CONFIG_PM_SLEEP */
745 static const struct dev_pm_ops img_spfi_pm_ops
= {
746 SET_RUNTIME_PM_OPS(img_spfi_runtime_suspend
, img_spfi_runtime_resume
,
748 SET_SYSTEM_SLEEP_PM_OPS(img_spfi_suspend
, img_spfi_resume
)
751 static const struct of_device_id img_spfi_of_match
[] = {
752 { .compatible
= "img,spfi", },
755 MODULE_DEVICE_TABLE(of
, img_spfi_of_match
);
757 static struct platform_driver img_spfi_driver
= {
760 .pm
= &img_spfi_pm_ops
,
761 .of_match_table
= of_match_ptr(img_spfi_of_match
),
763 .probe
= img_spfi_probe
,
764 .remove
= img_spfi_remove
,
766 module_platform_driver(img_spfi_driver
);
768 MODULE_DESCRIPTION("IMG SPFI controller driver");
769 MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
770 MODULE_LICENSE("GPL v2");