2 * Copyright (c) 2015 MediaTek Inc.
3 * Author: Leilk Liu <leilk.liu@mediatek.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/interrupt.h>
20 #include <linux/ioport.h>
21 #include <linux/module.h>
23 #include <linux/of_gpio.h>
24 #include <linux/platform_device.h>
25 #include <linux/platform_data/spi-mt65xx.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/spi/spi.h>
29 #define SPI_CFG0_REG 0x0000
30 #define SPI_CFG1_REG 0x0004
31 #define SPI_TX_SRC_REG 0x0008
32 #define SPI_RX_DST_REG 0x000c
33 #define SPI_TX_DATA_REG 0x0010
34 #define SPI_RX_DATA_REG 0x0014
35 #define SPI_CMD_REG 0x0018
36 #define SPI_STATUS0_REG 0x001c
37 #define SPI_PAD_SEL_REG 0x0024
39 #define SPI_CFG0_SCK_HIGH_OFFSET 0
40 #define SPI_CFG0_SCK_LOW_OFFSET 8
41 #define SPI_CFG0_CS_HOLD_OFFSET 16
42 #define SPI_CFG0_CS_SETUP_OFFSET 24
44 #define SPI_CFG1_CS_IDLE_OFFSET 0
45 #define SPI_CFG1_PACKET_LOOP_OFFSET 8
46 #define SPI_CFG1_PACKET_LENGTH_OFFSET 16
47 #define SPI_CFG1_GET_TICK_DLY_OFFSET 30
49 #define SPI_CFG1_CS_IDLE_MASK 0xff
50 #define SPI_CFG1_PACKET_LOOP_MASK 0xff00
51 #define SPI_CFG1_PACKET_LENGTH_MASK 0x3ff0000
53 #define SPI_CMD_ACT BIT(0)
54 #define SPI_CMD_RESUME BIT(1)
55 #define SPI_CMD_RST BIT(2)
56 #define SPI_CMD_PAUSE_EN BIT(4)
57 #define SPI_CMD_DEASSERT BIT(5)
58 #define SPI_CMD_CPHA BIT(8)
59 #define SPI_CMD_CPOL BIT(9)
60 #define SPI_CMD_RX_DMA BIT(10)
61 #define SPI_CMD_TX_DMA BIT(11)
62 #define SPI_CMD_TXMSBF BIT(12)
63 #define SPI_CMD_RXMSBF BIT(13)
64 #define SPI_CMD_RX_ENDIAN BIT(14)
65 #define SPI_CMD_TX_ENDIAN BIT(15)
66 #define SPI_CMD_FINISH_IE BIT(16)
67 #define SPI_CMD_PAUSE_IE BIT(17)
69 #define MT8173_SPI_MAX_PAD_SEL 3
71 #define MTK_SPI_PAUSE_INT_STATUS 0x2
73 #define MTK_SPI_IDLE 0
74 #define MTK_SPI_PAUSED 1
76 #define MTK_SPI_MAX_FIFO_SIZE 32
77 #define MTK_SPI_PACKET_SIZE 1024
79 struct mtk_spi_compatible
{
81 /* Must explicitly send dummy Tx bytes to do Rx only transfer */
90 struct clk
*parent_clk
, *sel_clk
, *spi_clk
;
91 struct spi_transfer
*cur_transfer
;
93 struct scatterlist
*tx_sgl
, *rx_sgl
;
94 u32 tx_sgl_len
, rx_sgl_len
;
95 const struct mtk_spi_compatible
*dev_comp
;
98 static const struct mtk_spi_compatible mt6589_compat
;
99 static const struct mtk_spi_compatible mt8135_compat
;
100 static const struct mtk_spi_compatible mt8173_compat
= {
101 .need_pad_sel
= true,
106 * A piece of default chip info unless the platform
109 static const struct mtk_chip_config mtk_default_chip_info
= {
114 static const struct of_device_id mtk_spi_of_match
[] = {
115 { .compatible
= "mediatek,mt6589-spi", .data
= (void *)&mt6589_compat
},
116 { .compatible
= "mediatek,mt8135-spi", .data
= (void *)&mt8135_compat
},
117 { .compatible
= "mediatek,mt8173-spi", .data
= (void *)&mt8173_compat
},
120 MODULE_DEVICE_TABLE(of
, mtk_spi_of_match
);
122 static void mtk_spi_reset(struct mtk_spi
*mdata
)
126 /* set the software reset bit in SPI_CMD_REG. */
127 reg_val
= readl(mdata
->base
+ SPI_CMD_REG
);
128 reg_val
|= SPI_CMD_RST
;
129 writel(reg_val
, mdata
->base
+ SPI_CMD_REG
);
131 reg_val
= readl(mdata
->base
+ SPI_CMD_REG
);
132 reg_val
&= ~SPI_CMD_RST
;
133 writel(reg_val
, mdata
->base
+ SPI_CMD_REG
);
136 static int mtk_spi_prepare_message(struct spi_master
*master
,
137 struct spi_message
*msg
)
141 struct spi_device
*spi
= msg
->spi
;
142 struct mtk_chip_config
*chip_config
= spi
->controller_data
;
143 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
145 cpha
= spi
->mode
& SPI_CPHA
? 1 : 0;
146 cpol
= spi
->mode
& SPI_CPOL
? 1 : 0;
148 reg_val
= readl(mdata
->base
+ SPI_CMD_REG
);
150 reg_val
|= SPI_CMD_CPHA
;
152 reg_val
&= ~SPI_CMD_CPHA
;
154 reg_val
|= SPI_CMD_CPOL
;
156 reg_val
&= ~SPI_CMD_CPOL
;
157 writel(reg_val
, mdata
->base
+ SPI_CMD_REG
);
159 reg_val
= readl(mdata
->base
+ SPI_CMD_REG
);
161 /* set the mlsbx and mlsbtx */
162 if (chip_config
->tx_mlsb
)
163 reg_val
|= SPI_CMD_TXMSBF
;
165 reg_val
&= ~SPI_CMD_TXMSBF
;
166 if (chip_config
->rx_mlsb
)
167 reg_val
|= SPI_CMD_RXMSBF
;
169 reg_val
&= ~SPI_CMD_RXMSBF
;
171 /* set the tx/rx endian */
172 #ifdef __LITTLE_ENDIAN
173 reg_val
&= ~SPI_CMD_TX_ENDIAN
;
174 reg_val
&= ~SPI_CMD_RX_ENDIAN
;
176 reg_val
|= SPI_CMD_TX_ENDIAN
;
177 reg_val
|= SPI_CMD_RX_ENDIAN
;
180 /* set finish and pause interrupt always enable */
181 reg_val
|= SPI_CMD_FINISH_IE
| SPI_CMD_PAUSE_IE
;
183 /* disable dma mode */
184 reg_val
&= ~(SPI_CMD_TX_DMA
| SPI_CMD_RX_DMA
);
186 /* disable deassert mode */
187 reg_val
&= ~SPI_CMD_DEASSERT
;
189 writel(reg_val
, mdata
->base
+ SPI_CMD_REG
);
192 if (mdata
->dev_comp
->need_pad_sel
)
193 writel(mdata
->pad_sel
[spi
->chip_select
],
194 mdata
->base
+ SPI_PAD_SEL_REG
);
199 static void mtk_spi_set_cs(struct spi_device
*spi
, bool enable
)
202 struct mtk_spi
*mdata
= spi_master_get_devdata(spi
->master
);
204 reg_val
= readl(mdata
->base
+ SPI_CMD_REG
);
206 reg_val
|= SPI_CMD_PAUSE_EN
;
207 writel(reg_val
, mdata
->base
+ SPI_CMD_REG
);
209 reg_val
&= ~SPI_CMD_PAUSE_EN
;
210 writel(reg_val
, mdata
->base
+ SPI_CMD_REG
);
211 mdata
->state
= MTK_SPI_IDLE
;
212 mtk_spi_reset(mdata
);
216 static void mtk_spi_prepare_transfer(struct spi_master
*master
,
217 struct spi_transfer
*xfer
)
219 u32 spi_clk_hz
, div
, sck_time
, cs_time
, reg_val
= 0;
220 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
222 spi_clk_hz
= clk_get_rate(mdata
->spi_clk
);
223 if (xfer
->speed_hz
< spi_clk_hz
/ 2)
224 div
= DIV_ROUND_UP(spi_clk_hz
, xfer
->speed_hz
);
228 sck_time
= (div
+ 1) / 2;
229 cs_time
= sck_time
* 2;
231 reg_val
|= (((sck_time
- 1) & 0xff) << SPI_CFG0_SCK_HIGH_OFFSET
);
232 reg_val
|= (((sck_time
- 1) & 0xff) << SPI_CFG0_SCK_LOW_OFFSET
);
233 reg_val
|= (((cs_time
- 1) & 0xff) << SPI_CFG0_CS_HOLD_OFFSET
);
234 reg_val
|= (((cs_time
- 1) & 0xff) << SPI_CFG0_CS_SETUP_OFFSET
);
235 writel(reg_val
, mdata
->base
+ SPI_CFG0_REG
);
237 reg_val
= readl(mdata
->base
+ SPI_CFG1_REG
);
238 reg_val
&= ~SPI_CFG1_CS_IDLE_MASK
;
239 reg_val
|= (((cs_time
- 1) & 0xff) << SPI_CFG1_CS_IDLE_OFFSET
);
240 writel(reg_val
, mdata
->base
+ SPI_CFG1_REG
);
243 static void mtk_spi_setup_packet(struct spi_master
*master
)
245 u32 packet_size
, packet_loop
, reg_val
;
246 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
248 packet_size
= min_t(u32
, mdata
->xfer_len
, MTK_SPI_PACKET_SIZE
);
249 packet_loop
= mdata
->xfer_len
/ packet_size
;
251 reg_val
= readl(mdata
->base
+ SPI_CFG1_REG
);
252 reg_val
&= ~(SPI_CFG1_PACKET_LENGTH_MASK
| SPI_CFG1_PACKET_LOOP_MASK
);
253 reg_val
|= (packet_size
- 1) << SPI_CFG1_PACKET_LENGTH_OFFSET
;
254 reg_val
|= (packet_loop
- 1) << SPI_CFG1_PACKET_LOOP_OFFSET
;
255 writel(reg_val
, mdata
->base
+ SPI_CFG1_REG
);
258 static void mtk_spi_enable_transfer(struct spi_master
*master
)
261 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
263 cmd
= readl(mdata
->base
+ SPI_CMD_REG
);
264 if (mdata
->state
== MTK_SPI_IDLE
)
267 cmd
|= SPI_CMD_RESUME
;
268 writel(cmd
, mdata
->base
+ SPI_CMD_REG
);
271 static int mtk_spi_get_mult_delta(u32 xfer_len
)
275 if (xfer_len
> MTK_SPI_PACKET_SIZE
)
276 mult_delta
= xfer_len
% MTK_SPI_PACKET_SIZE
;
283 static void mtk_spi_update_mdata_len(struct spi_master
*master
)
286 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
288 if (mdata
->tx_sgl_len
&& mdata
->rx_sgl_len
) {
289 if (mdata
->tx_sgl_len
> mdata
->rx_sgl_len
) {
290 mult_delta
= mtk_spi_get_mult_delta(mdata
->rx_sgl_len
);
291 mdata
->xfer_len
= mdata
->rx_sgl_len
- mult_delta
;
292 mdata
->rx_sgl_len
= mult_delta
;
293 mdata
->tx_sgl_len
-= mdata
->xfer_len
;
295 mult_delta
= mtk_spi_get_mult_delta(mdata
->tx_sgl_len
);
296 mdata
->xfer_len
= mdata
->tx_sgl_len
- mult_delta
;
297 mdata
->tx_sgl_len
= mult_delta
;
298 mdata
->rx_sgl_len
-= mdata
->xfer_len
;
300 } else if (mdata
->tx_sgl_len
) {
301 mult_delta
= mtk_spi_get_mult_delta(mdata
->tx_sgl_len
);
302 mdata
->xfer_len
= mdata
->tx_sgl_len
- mult_delta
;
303 mdata
->tx_sgl_len
= mult_delta
;
304 } else if (mdata
->rx_sgl_len
) {
305 mult_delta
= mtk_spi_get_mult_delta(mdata
->rx_sgl_len
);
306 mdata
->xfer_len
= mdata
->rx_sgl_len
- mult_delta
;
307 mdata
->rx_sgl_len
= mult_delta
;
311 static void mtk_spi_setup_dma_addr(struct spi_master
*master
,
312 struct spi_transfer
*xfer
)
314 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
317 writel(xfer
->tx_dma
, mdata
->base
+ SPI_TX_SRC_REG
);
319 writel(xfer
->rx_dma
, mdata
->base
+ SPI_RX_DST_REG
);
322 static int mtk_spi_fifo_transfer(struct spi_master
*master
,
323 struct spi_device
*spi
,
324 struct spi_transfer
*xfer
)
327 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
329 mdata
->cur_transfer
= xfer
;
330 mdata
->xfer_len
= xfer
->len
;
331 mtk_spi_prepare_transfer(master
, xfer
);
332 mtk_spi_setup_packet(master
);
335 cnt
= xfer
->len
/ 4 + 1;
338 iowrite32_rep(mdata
->base
+ SPI_TX_DATA_REG
, xfer
->tx_buf
, cnt
);
340 mtk_spi_enable_transfer(master
);
345 static int mtk_spi_dma_transfer(struct spi_master
*master
,
346 struct spi_device
*spi
,
347 struct spi_transfer
*xfer
)
350 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
352 mdata
->tx_sgl
= NULL
;
353 mdata
->rx_sgl
= NULL
;
354 mdata
->tx_sgl_len
= 0;
355 mdata
->rx_sgl_len
= 0;
356 mdata
->cur_transfer
= xfer
;
358 mtk_spi_prepare_transfer(master
, xfer
);
360 cmd
= readl(mdata
->base
+ SPI_CMD_REG
);
362 cmd
|= SPI_CMD_TX_DMA
;
364 cmd
|= SPI_CMD_RX_DMA
;
365 writel(cmd
, mdata
->base
+ SPI_CMD_REG
);
368 mdata
->tx_sgl
= xfer
->tx_sg
.sgl
;
370 mdata
->rx_sgl
= xfer
->rx_sg
.sgl
;
373 xfer
->tx_dma
= sg_dma_address(mdata
->tx_sgl
);
374 mdata
->tx_sgl_len
= sg_dma_len(mdata
->tx_sgl
);
377 xfer
->rx_dma
= sg_dma_address(mdata
->rx_sgl
);
378 mdata
->rx_sgl_len
= sg_dma_len(mdata
->rx_sgl
);
381 mtk_spi_update_mdata_len(master
);
382 mtk_spi_setup_packet(master
);
383 mtk_spi_setup_dma_addr(master
, xfer
);
384 mtk_spi_enable_transfer(master
);
389 static int mtk_spi_transfer_one(struct spi_master
*master
,
390 struct spi_device
*spi
,
391 struct spi_transfer
*xfer
)
393 if (master
->can_dma(master
, spi
, xfer
))
394 return mtk_spi_dma_transfer(master
, spi
, xfer
);
396 return mtk_spi_fifo_transfer(master
, spi
, xfer
);
399 static bool mtk_spi_can_dma(struct spi_master
*master
,
400 struct spi_device
*spi
,
401 struct spi_transfer
*xfer
)
403 return xfer
->len
> MTK_SPI_MAX_FIFO_SIZE
;
406 static int mtk_spi_setup(struct spi_device
*spi
)
408 struct mtk_spi
*mdata
= spi_master_get_devdata(spi
->master
);
410 if (!spi
->controller_data
)
411 spi
->controller_data
= (void *)&mtk_default_chip_info
;
413 if (mdata
->dev_comp
->need_pad_sel
&& gpio_is_valid(spi
->cs_gpio
))
414 gpio_direction_output(spi
->cs_gpio
, !(spi
->mode
& SPI_CS_HIGH
));
419 static irqreturn_t
mtk_spi_interrupt(int irq
, void *dev_id
)
421 u32 cmd
, reg_val
, cnt
;
422 struct spi_master
*master
= dev_id
;
423 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
424 struct spi_transfer
*trans
= mdata
->cur_transfer
;
426 reg_val
= readl(mdata
->base
+ SPI_STATUS0_REG
);
427 if (reg_val
& MTK_SPI_PAUSE_INT_STATUS
)
428 mdata
->state
= MTK_SPI_PAUSED
;
430 mdata
->state
= MTK_SPI_IDLE
;
432 if (!master
->can_dma(master
, master
->cur_msg
->spi
, trans
)) {
434 if (mdata
->xfer_len
% 4)
435 cnt
= mdata
->xfer_len
/ 4 + 1;
437 cnt
= mdata
->xfer_len
/ 4;
438 ioread32_rep(mdata
->base
+ SPI_RX_DATA_REG
,
441 spi_finalize_current_transfer(master
);
446 trans
->tx_dma
+= mdata
->xfer_len
;
448 trans
->rx_dma
+= mdata
->xfer_len
;
450 if (mdata
->tx_sgl
&& (mdata
->tx_sgl_len
== 0)) {
451 mdata
->tx_sgl
= sg_next(mdata
->tx_sgl
);
453 trans
->tx_dma
= sg_dma_address(mdata
->tx_sgl
);
454 mdata
->tx_sgl_len
= sg_dma_len(mdata
->tx_sgl
);
457 if (mdata
->rx_sgl
&& (mdata
->rx_sgl_len
== 0)) {
458 mdata
->rx_sgl
= sg_next(mdata
->rx_sgl
);
460 trans
->rx_dma
= sg_dma_address(mdata
->rx_sgl
);
461 mdata
->rx_sgl_len
= sg_dma_len(mdata
->rx_sgl
);
465 if (!mdata
->tx_sgl
&& !mdata
->rx_sgl
) {
466 /* spi disable dma */
467 cmd
= readl(mdata
->base
+ SPI_CMD_REG
);
468 cmd
&= ~SPI_CMD_TX_DMA
;
469 cmd
&= ~SPI_CMD_RX_DMA
;
470 writel(cmd
, mdata
->base
+ SPI_CMD_REG
);
472 spi_finalize_current_transfer(master
);
476 mtk_spi_update_mdata_len(master
);
477 mtk_spi_setup_packet(master
);
478 mtk_spi_setup_dma_addr(master
, trans
);
479 mtk_spi_enable_transfer(master
);
484 static int mtk_spi_probe(struct platform_device
*pdev
)
486 struct spi_master
*master
;
487 struct mtk_spi
*mdata
;
488 const struct of_device_id
*of_id
;
489 struct resource
*res
;
492 master
= spi_alloc_master(&pdev
->dev
, sizeof(*mdata
));
494 dev_err(&pdev
->dev
, "failed to alloc spi master\n");
498 master
->auto_runtime_pm
= true;
499 master
->dev
.of_node
= pdev
->dev
.of_node
;
500 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
;
502 master
->set_cs
= mtk_spi_set_cs
;
503 master
->prepare_message
= mtk_spi_prepare_message
;
504 master
->transfer_one
= mtk_spi_transfer_one
;
505 master
->can_dma
= mtk_spi_can_dma
;
506 master
->setup
= mtk_spi_setup
;
508 of_id
= of_match_node(mtk_spi_of_match
, pdev
->dev
.of_node
);
510 dev_err(&pdev
->dev
, "failed to probe of_node\n");
515 mdata
= spi_master_get_devdata(master
);
516 mdata
->dev_comp
= of_id
->data
;
517 if (mdata
->dev_comp
->must_tx
)
518 master
->flags
= SPI_MASTER_MUST_TX
;
520 if (mdata
->dev_comp
->need_pad_sel
) {
521 mdata
->pad_num
= of_property_count_u32_elems(
523 "mediatek,pad-select");
524 if (mdata
->pad_num
< 0) {
526 "No 'mediatek,pad-select' property\n");
531 mdata
->pad_sel
= devm_kmalloc_array(&pdev
->dev
, mdata
->pad_num
,
532 sizeof(u32
), GFP_KERNEL
);
533 if (!mdata
->pad_sel
) {
538 for (i
= 0; i
< mdata
->pad_num
; i
++) {
539 of_property_read_u32_index(pdev
->dev
.of_node
,
540 "mediatek,pad-select",
541 i
, &mdata
->pad_sel
[i
]);
542 if (mdata
->pad_sel
[i
] > MT8173_SPI_MAX_PAD_SEL
) {
543 dev_err(&pdev
->dev
, "wrong pad-sel[%d]: %u\n",
544 i
, mdata
->pad_sel
[i
]);
551 platform_set_drvdata(pdev
, master
);
553 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
556 dev_err(&pdev
->dev
, "failed to determine base address\n");
560 mdata
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
561 if (IS_ERR(mdata
->base
)) {
562 ret
= PTR_ERR(mdata
->base
);
566 irq
= platform_get_irq(pdev
, 0);
568 dev_err(&pdev
->dev
, "failed to get irq (%d)\n", irq
);
573 if (!pdev
->dev
.dma_mask
)
574 pdev
->dev
.dma_mask
= &pdev
->dev
.coherent_dma_mask
;
576 ret
= devm_request_irq(&pdev
->dev
, irq
, mtk_spi_interrupt
,
577 IRQF_TRIGGER_NONE
, dev_name(&pdev
->dev
), master
);
579 dev_err(&pdev
->dev
, "failed to register irq (%d)\n", ret
);
583 mdata
->parent_clk
= devm_clk_get(&pdev
->dev
, "parent-clk");
584 if (IS_ERR(mdata
->parent_clk
)) {
585 ret
= PTR_ERR(mdata
->parent_clk
);
586 dev_err(&pdev
->dev
, "failed to get parent-clk: %d\n", ret
);
590 mdata
->sel_clk
= devm_clk_get(&pdev
->dev
, "sel-clk");
591 if (IS_ERR(mdata
->sel_clk
)) {
592 ret
= PTR_ERR(mdata
->sel_clk
);
593 dev_err(&pdev
->dev
, "failed to get sel-clk: %d\n", ret
);
597 mdata
->spi_clk
= devm_clk_get(&pdev
->dev
, "spi-clk");
598 if (IS_ERR(mdata
->spi_clk
)) {
599 ret
= PTR_ERR(mdata
->spi_clk
);
600 dev_err(&pdev
->dev
, "failed to get spi-clk: %d\n", ret
);
604 ret
= clk_prepare_enable(mdata
->spi_clk
);
606 dev_err(&pdev
->dev
, "failed to enable spi_clk (%d)\n", ret
);
610 ret
= clk_set_parent(mdata
->sel_clk
, mdata
->parent_clk
);
612 dev_err(&pdev
->dev
, "failed to clk_set_parent (%d)\n", ret
);
613 goto err_disable_clk
;
616 clk_disable_unprepare(mdata
->spi_clk
);
618 pm_runtime_enable(&pdev
->dev
);
620 ret
= devm_spi_register_master(&pdev
->dev
, master
);
622 dev_err(&pdev
->dev
, "failed to register master (%d)\n", ret
);
626 if (mdata
->dev_comp
->need_pad_sel
) {
627 if (mdata
->pad_num
!= master
->num_chipselect
) {
629 "pad_num does not match num_chipselect(%d != %d)\n",
630 mdata
->pad_num
, master
->num_chipselect
);
635 if (!master
->cs_gpios
&& master
->num_chipselect
> 1) {
637 "cs_gpios not specified and num_chipselect > 1\n");
642 if (master
->cs_gpios
) {
643 for (i
= 0; i
< master
->num_chipselect
; i
++) {
644 ret
= devm_gpio_request(&pdev
->dev
,
646 dev_name(&pdev
->dev
));
649 "can't get CS GPIO %i\n", i
);
659 clk_disable_unprepare(mdata
->spi_clk
);
661 spi_master_put(master
);
666 static int mtk_spi_remove(struct platform_device
*pdev
)
668 struct spi_master
*master
= platform_get_drvdata(pdev
);
669 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
671 pm_runtime_disable(&pdev
->dev
);
673 mtk_spi_reset(mdata
);
674 spi_master_put(master
);
679 #ifdef CONFIG_PM_SLEEP
680 static int mtk_spi_suspend(struct device
*dev
)
683 struct spi_master
*master
= dev_get_drvdata(dev
);
684 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
686 ret
= spi_master_suspend(master
);
690 if (!pm_runtime_suspended(dev
))
691 clk_disable_unprepare(mdata
->spi_clk
);
696 static int mtk_spi_resume(struct device
*dev
)
699 struct spi_master
*master
= dev_get_drvdata(dev
);
700 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
702 if (!pm_runtime_suspended(dev
)) {
703 ret
= clk_prepare_enable(mdata
->spi_clk
);
705 dev_err(dev
, "failed to enable spi_clk (%d)\n", ret
);
710 ret
= spi_master_resume(master
);
712 clk_disable_unprepare(mdata
->spi_clk
);
716 #endif /* CONFIG_PM_SLEEP */
719 static int mtk_spi_runtime_suspend(struct device
*dev
)
721 struct spi_master
*master
= dev_get_drvdata(dev
);
722 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
724 clk_disable_unprepare(mdata
->spi_clk
);
729 static int mtk_spi_runtime_resume(struct device
*dev
)
731 struct spi_master
*master
= dev_get_drvdata(dev
);
732 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
735 ret
= clk_prepare_enable(mdata
->spi_clk
);
737 dev_err(dev
, "failed to enable spi_clk (%d)\n", ret
);
743 #endif /* CONFIG_PM */
745 static const struct dev_pm_ops mtk_spi_pm
= {
746 SET_SYSTEM_SLEEP_PM_OPS(mtk_spi_suspend
, mtk_spi_resume
)
747 SET_RUNTIME_PM_OPS(mtk_spi_runtime_suspend
,
748 mtk_spi_runtime_resume
, NULL
)
751 static struct platform_driver mtk_spi_driver
= {
755 .of_match_table
= mtk_spi_of_match
,
757 .probe
= mtk_spi_probe
,
758 .remove
= mtk_spi_remove
,
761 module_platform_driver(mtk_spi_driver
);
763 MODULE_DESCRIPTION("MTK SPI Controller driver");
764 MODULE_AUTHOR("Leilk Liu <leilk.liu@mediatek.com>");
765 MODULE_LICENSE("GPL v2");
766 MODULE_ALIAS("platform:mtk-spi");