1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
4 * Author: Leilk Liu <leilk.liu@mediatek.com>
8 #include <linux/device.h>
10 #include <linux/interrupt.h>
12 #include <linux/ioport.h>
13 #include <linux/module.h>
15 #include <linux/of_gpio.h>
16 #include <linux/platform_device.h>
17 #include <linux/platform_data/spi-mt65xx.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/spi/spi.h>
20 #include <linux/dma-mapping.h>
22 #define SPI_CFG0_REG 0x0000
23 #define SPI_CFG1_REG 0x0004
24 #define SPI_TX_SRC_REG 0x0008
25 #define SPI_RX_DST_REG 0x000c
26 #define SPI_TX_DATA_REG 0x0010
27 #define SPI_RX_DATA_REG 0x0014
28 #define SPI_CMD_REG 0x0018
29 #define SPI_STATUS0_REG 0x001c
30 #define SPI_PAD_SEL_REG 0x0024
31 #define SPI_CFG2_REG 0x0028
32 #define SPI_TX_SRC_REG_64 0x002c
33 #define SPI_RX_DST_REG_64 0x0030
35 #define SPI_CFG0_SCK_HIGH_OFFSET 0
36 #define SPI_CFG0_SCK_LOW_OFFSET 8
37 #define SPI_CFG0_CS_HOLD_OFFSET 16
38 #define SPI_CFG0_CS_SETUP_OFFSET 24
39 #define SPI_ADJUST_CFG0_CS_HOLD_OFFSET 0
40 #define SPI_ADJUST_CFG0_CS_SETUP_OFFSET 16
42 #define SPI_CFG1_CS_IDLE_OFFSET 0
43 #define SPI_CFG1_PACKET_LOOP_OFFSET 8
44 #define SPI_CFG1_PACKET_LENGTH_OFFSET 16
45 #define SPI_CFG1_GET_TICK_DLY_OFFSET 30
47 #define SPI_CFG1_CS_IDLE_MASK 0xff
48 #define SPI_CFG1_PACKET_LOOP_MASK 0xff00
49 #define SPI_CFG1_PACKET_LENGTH_MASK 0x3ff0000
50 #define SPI_CFG2_SCK_HIGH_OFFSET 0
51 #define SPI_CFG2_SCK_LOW_OFFSET 16
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_SAMPLE_SEL BIT(6)
59 #define SPI_CMD_CS_POL BIT(7)
60 #define SPI_CMD_CPHA BIT(8)
61 #define SPI_CMD_CPOL BIT(9)
62 #define SPI_CMD_RX_DMA BIT(10)
63 #define SPI_CMD_TX_DMA BIT(11)
64 #define SPI_CMD_TXMSBF BIT(12)
65 #define SPI_CMD_RXMSBF BIT(13)
66 #define SPI_CMD_RX_ENDIAN BIT(14)
67 #define SPI_CMD_TX_ENDIAN BIT(15)
68 #define SPI_CMD_FINISH_IE BIT(16)
69 #define SPI_CMD_PAUSE_IE BIT(17)
71 #define MT8173_SPI_MAX_PAD_SEL 3
73 #define MTK_SPI_PAUSE_INT_STATUS 0x2
75 #define MTK_SPI_IDLE 0
76 #define MTK_SPI_PAUSED 1
78 #define MTK_SPI_MAX_FIFO_SIZE 32U
79 #define MTK_SPI_PACKET_SIZE 1024
80 #define MTK_SPI_32BITS_MASK (0xffffffff)
82 #define DMA_ADDR_EXT_BITS (36)
83 #define DMA_ADDR_DEF_BITS (32)
85 struct mtk_spi_compatible
{
87 /* Must explicitly send dummy Tx bytes to do Rx only transfer */
89 /* some IC design adjust cfg register to enhance time accuracy */
91 /* some IC support DMA addr extension */
100 struct clk
*parent_clk
, *sel_clk
, *spi_clk
;
101 struct spi_transfer
*cur_transfer
;
104 struct scatterlist
*tx_sgl
, *rx_sgl
;
105 u32 tx_sgl_len
, rx_sgl_len
;
106 const struct mtk_spi_compatible
*dev_comp
;
109 static const struct mtk_spi_compatible mtk_common_compat
;
111 static const struct mtk_spi_compatible mt2712_compat
= {
115 static const struct mtk_spi_compatible mt6765_compat
= {
116 .need_pad_sel
= true,
118 .enhance_timing
= true,
122 static const struct mtk_spi_compatible mt7622_compat
= {
124 .enhance_timing
= true,
127 static const struct mtk_spi_compatible mt8173_compat
= {
128 .need_pad_sel
= true,
132 static const struct mtk_spi_compatible mt8183_compat
= {
133 .need_pad_sel
= true,
135 .enhance_timing
= true,
139 * A piece of default chip info unless the platform
142 static const struct mtk_chip_config mtk_default_chip_info
= {
146 static const struct of_device_id mtk_spi_of_match
[] = {
147 { .compatible
= "mediatek,mt2701-spi",
148 .data
= (void *)&mtk_common_compat
,
150 { .compatible
= "mediatek,mt2712-spi",
151 .data
= (void *)&mt2712_compat
,
153 { .compatible
= "mediatek,mt6589-spi",
154 .data
= (void *)&mtk_common_compat
,
156 { .compatible
= "mediatek,mt6765-spi",
157 .data
= (void *)&mt6765_compat
,
159 { .compatible
= "mediatek,mt7622-spi",
160 .data
= (void *)&mt7622_compat
,
162 { .compatible
= "mediatek,mt7629-spi",
163 .data
= (void *)&mt7622_compat
,
165 { .compatible
= "mediatek,mt8135-spi",
166 .data
= (void *)&mtk_common_compat
,
168 { .compatible
= "mediatek,mt8173-spi",
169 .data
= (void *)&mt8173_compat
,
171 { .compatible
= "mediatek,mt8183-spi",
172 .data
= (void *)&mt8183_compat
,
174 { .compatible
= "mediatek,mt8192-spi",
175 .data
= (void *)&mt6765_compat
,
179 MODULE_DEVICE_TABLE(of
, mtk_spi_of_match
);
181 static void mtk_spi_reset(struct mtk_spi
*mdata
)
185 /* set the software reset bit in SPI_CMD_REG. */
186 reg_val
= readl(mdata
->base
+ SPI_CMD_REG
);
187 reg_val
|= SPI_CMD_RST
;
188 writel(reg_val
, mdata
->base
+ SPI_CMD_REG
);
190 reg_val
= readl(mdata
->base
+ SPI_CMD_REG
);
191 reg_val
&= ~SPI_CMD_RST
;
192 writel(reg_val
, mdata
->base
+ SPI_CMD_REG
);
195 static int mtk_spi_prepare_message(struct spi_master
*master
,
196 struct spi_message
*msg
)
200 struct spi_device
*spi
= msg
->spi
;
201 struct mtk_chip_config
*chip_config
= spi
->controller_data
;
202 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
204 cpha
= spi
->mode
& SPI_CPHA
? 1 : 0;
205 cpol
= spi
->mode
& SPI_CPOL
? 1 : 0;
207 reg_val
= readl(mdata
->base
+ SPI_CMD_REG
);
209 reg_val
|= SPI_CMD_CPHA
;
211 reg_val
&= ~SPI_CMD_CPHA
;
213 reg_val
|= SPI_CMD_CPOL
;
215 reg_val
&= ~SPI_CMD_CPOL
;
217 /* set the mlsbx and mlsbtx */
218 if (spi
->mode
& SPI_LSB_FIRST
) {
219 reg_val
&= ~SPI_CMD_TXMSBF
;
220 reg_val
&= ~SPI_CMD_RXMSBF
;
222 reg_val
|= SPI_CMD_TXMSBF
;
223 reg_val
|= SPI_CMD_RXMSBF
;
226 /* set the tx/rx endian */
227 #ifdef __LITTLE_ENDIAN
228 reg_val
&= ~SPI_CMD_TX_ENDIAN
;
229 reg_val
&= ~SPI_CMD_RX_ENDIAN
;
231 reg_val
|= SPI_CMD_TX_ENDIAN
;
232 reg_val
|= SPI_CMD_RX_ENDIAN
;
235 if (mdata
->dev_comp
->enhance_timing
) {
236 /* set CS polarity */
237 if (spi
->mode
& SPI_CS_HIGH
)
238 reg_val
|= SPI_CMD_CS_POL
;
240 reg_val
&= ~SPI_CMD_CS_POL
;
242 if (chip_config
->sample_sel
)
243 reg_val
|= SPI_CMD_SAMPLE_SEL
;
245 reg_val
&= ~SPI_CMD_SAMPLE_SEL
;
248 /* set finish and pause interrupt always enable */
249 reg_val
|= SPI_CMD_FINISH_IE
| SPI_CMD_PAUSE_IE
;
251 /* disable dma mode */
252 reg_val
&= ~(SPI_CMD_TX_DMA
| SPI_CMD_RX_DMA
);
254 /* disable deassert mode */
255 reg_val
&= ~SPI_CMD_DEASSERT
;
257 writel(reg_val
, mdata
->base
+ SPI_CMD_REG
);
260 if (mdata
->dev_comp
->need_pad_sel
)
261 writel(mdata
->pad_sel
[spi
->chip_select
],
262 mdata
->base
+ SPI_PAD_SEL_REG
);
267 static void mtk_spi_set_cs(struct spi_device
*spi
, bool enable
)
270 struct mtk_spi
*mdata
= spi_master_get_devdata(spi
->master
);
272 if (spi
->mode
& SPI_CS_HIGH
)
275 reg_val
= readl(mdata
->base
+ SPI_CMD_REG
);
277 reg_val
|= SPI_CMD_PAUSE_EN
;
278 writel(reg_val
, mdata
->base
+ SPI_CMD_REG
);
280 reg_val
&= ~SPI_CMD_PAUSE_EN
;
281 writel(reg_val
, mdata
->base
+ SPI_CMD_REG
);
282 mdata
->state
= MTK_SPI_IDLE
;
283 mtk_spi_reset(mdata
);
287 static void mtk_spi_prepare_transfer(struct spi_master
*master
,
288 struct spi_transfer
*xfer
)
290 u32 spi_clk_hz
, div
, sck_time
, cs_time
, reg_val
;
291 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
293 spi_clk_hz
= clk_get_rate(mdata
->spi_clk
);
294 if (xfer
->speed_hz
< spi_clk_hz
/ 2)
295 div
= DIV_ROUND_UP(spi_clk_hz
, xfer
->speed_hz
);
299 sck_time
= (div
+ 1) / 2;
300 cs_time
= sck_time
* 2;
302 if (mdata
->dev_comp
->enhance_timing
) {
303 reg_val
= (((sck_time
- 1) & 0xffff)
304 << SPI_CFG2_SCK_HIGH_OFFSET
);
305 reg_val
|= (((sck_time
- 1) & 0xffff)
306 << SPI_CFG2_SCK_LOW_OFFSET
);
307 writel(reg_val
, mdata
->base
+ SPI_CFG2_REG
);
308 reg_val
= (((cs_time
- 1) & 0xffff)
309 << SPI_ADJUST_CFG0_CS_HOLD_OFFSET
);
310 reg_val
|= (((cs_time
- 1) & 0xffff)
311 << SPI_ADJUST_CFG0_CS_SETUP_OFFSET
);
312 writel(reg_val
, mdata
->base
+ SPI_CFG0_REG
);
314 reg_val
= (((sck_time
- 1) & 0xff)
315 << SPI_CFG0_SCK_HIGH_OFFSET
);
316 reg_val
|= (((sck_time
- 1) & 0xff) << SPI_CFG0_SCK_LOW_OFFSET
);
317 reg_val
|= (((cs_time
- 1) & 0xff) << SPI_CFG0_CS_HOLD_OFFSET
);
318 reg_val
|= (((cs_time
- 1) & 0xff) << SPI_CFG0_CS_SETUP_OFFSET
);
319 writel(reg_val
, mdata
->base
+ SPI_CFG0_REG
);
322 reg_val
= readl(mdata
->base
+ SPI_CFG1_REG
);
323 reg_val
&= ~SPI_CFG1_CS_IDLE_MASK
;
324 reg_val
|= (((cs_time
- 1) & 0xff) << SPI_CFG1_CS_IDLE_OFFSET
);
325 writel(reg_val
, mdata
->base
+ SPI_CFG1_REG
);
328 static void mtk_spi_setup_packet(struct spi_master
*master
)
330 u32 packet_size
, packet_loop
, reg_val
;
331 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
333 packet_size
= min_t(u32
, mdata
->xfer_len
, MTK_SPI_PACKET_SIZE
);
334 packet_loop
= mdata
->xfer_len
/ packet_size
;
336 reg_val
= readl(mdata
->base
+ SPI_CFG1_REG
);
337 reg_val
&= ~(SPI_CFG1_PACKET_LENGTH_MASK
| SPI_CFG1_PACKET_LOOP_MASK
);
338 reg_val
|= (packet_size
- 1) << SPI_CFG1_PACKET_LENGTH_OFFSET
;
339 reg_val
|= (packet_loop
- 1) << SPI_CFG1_PACKET_LOOP_OFFSET
;
340 writel(reg_val
, mdata
->base
+ SPI_CFG1_REG
);
343 static void mtk_spi_enable_transfer(struct spi_master
*master
)
346 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
348 cmd
= readl(mdata
->base
+ SPI_CMD_REG
);
349 if (mdata
->state
== MTK_SPI_IDLE
)
352 cmd
|= SPI_CMD_RESUME
;
353 writel(cmd
, mdata
->base
+ SPI_CMD_REG
);
356 static int mtk_spi_get_mult_delta(u32 xfer_len
)
360 if (xfer_len
> MTK_SPI_PACKET_SIZE
)
361 mult_delta
= xfer_len
% MTK_SPI_PACKET_SIZE
;
368 static void mtk_spi_update_mdata_len(struct spi_master
*master
)
371 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
373 if (mdata
->tx_sgl_len
&& mdata
->rx_sgl_len
) {
374 if (mdata
->tx_sgl_len
> mdata
->rx_sgl_len
) {
375 mult_delta
= mtk_spi_get_mult_delta(mdata
->rx_sgl_len
);
376 mdata
->xfer_len
= mdata
->rx_sgl_len
- mult_delta
;
377 mdata
->rx_sgl_len
= mult_delta
;
378 mdata
->tx_sgl_len
-= mdata
->xfer_len
;
380 mult_delta
= mtk_spi_get_mult_delta(mdata
->tx_sgl_len
);
381 mdata
->xfer_len
= mdata
->tx_sgl_len
- mult_delta
;
382 mdata
->tx_sgl_len
= mult_delta
;
383 mdata
->rx_sgl_len
-= mdata
->xfer_len
;
385 } else if (mdata
->tx_sgl_len
) {
386 mult_delta
= mtk_spi_get_mult_delta(mdata
->tx_sgl_len
);
387 mdata
->xfer_len
= mdata
->tx_sgl_len
- mult_delta
;
388 mdata
->tx_sgl_len
= mult_delta
;
389 } else if (mdata
->rx_sgl_len
) {
390 mult_delta
= mtk_spi_get_mult_delta(mdata
->rx_sgl_len
);
391 mdata
->xfer_len
= mdata
->rx_sgl_len
- mult_delta
;
392 mdata
->rx_sgl_len
= mult_delta
;
396 static void mtk_spi_setup_dma_addr(struct spi_master
*master
,
397 struct spi_transfer
*xfer
)
399 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
402 writel((u32
)(xfer
->tx_dma
& MTK_SPI_32BITS_MASK
),
403 mdata
->base
+ SPI_TX_SRC_REG
);
404 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
405 if (mdata
->dev_comp
->dma_ext
)
406 writel((u32
)(xfer
->tx_dma
>> 32),
407 mdata
->base
+ SPI_TX_SRC_REG_64
);
412 writel((u32
)(xfer
->rx_dma
& MTK_SPI_32BITS_MASK
),
413 mdata
->base
+ SPI_RX_DST_REG
);
414 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
415 if (mdata
->dev_comp
->dma_ext
)
416 writel((u32
)(xfer
->rx_dma
>> 32),
417 mdata
->base
+ SPI_RX_DST_REG_64
);
422 static int mtk_spi_fifo_transfer(struct spi_master
*master
,
423 struct spi_device
*spi
,
424 struct spi_transfer
*xfer
)
428 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
430 mdata
->cur_transfer
= xfer
;
431 mdata
->xfer_len
= min(MTK_SPI_MAX_FIFO_SIZE
, xfer
->len
);
432 mdata
->num_xfered
= 0;
433 mtk_spi_prepare_transfer(master
, xfer
);
434 mtk_spi_setup_packet(master
);
437 iowrite32_rep(mdata
->base
+ SPI_TX_DATA_REG
, xfer
->tx_buf
, cnt
);
439 remainder
= xfer
->len
% 4;
442 memcpy(®_val
, xfer
->tx_buf
+ (cnt
* 4), remainder
);
443 writel(reg_val
, mdata
->base
+ SPI_TX_DATA_REG
);
446 mtk_spi_enable_transfer(master
);
451 static int mtk_spi_dma_transfer(struct spi_master
*master
,
452 struct spi_device
*spi
,
453 struct spi_transfer
*xfer
)
456 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
458 mdata
->tx_sgl
= NULL
;
459 mdata
->rx_sgl
= NULL
;
460 mdata
->tx_sgl_len
= 0;
461 mdata
->rx_sgl_len
= 0;
462 mdata
->cur_transfer
= xfer
;
463 mdata
->num_xfered
= 0;
465 mtk_spi_prepare_transfer(master
, xfer
);
467 cmd
= readl(mdata
->base
+ SPI_CMD_REG
);
469 cmd
|= SPI_CMD_TX_DMA
;
471 cmd
|= SPI_CMD_RX_DMA
;
472 writel(cmd
, mdata
->base
+ SPI_CMD_REG
);
475 mdata
->tx_sgl
= xfer
->tx_sg
.sgl
;
477 mdata
->rx_sgl
= xfer
->rx_sg
.sgl
;
480 xfer
->tx_dma
= sg_dma_address(mdata
->tx_sgl
);
481 mdata
->tx_sgl_len
= sg_dma_len(mdata
->tx_sgl
);
484 xfer
->rx_dma
= sg_dma_address(mdata
->rx_sgl
);
485 mdata
->rx_sgl_len
= sg_dma_len(mdata
->rx_sgl
);
488 mtk_spi_update_mdata_len(master
);
489 mtk_spi_setup_packet(master
);
490 mtk_spi_setup_dma_addr(master
, xfer
);
491 mtk_spi_enable_transfer(master
);
496 static int mtk_spi_transfer_one(struct spi_master
*master
,
497 struct spi_device
*spi
,
498 struct spi_transfer
*xfer
)
500 if (master
->can_dma(master
, spi
, xfer
))
501 return mtk_spi_dma_transfer(master
, spi
, xfer
);
503 return mtk_spi_fifo_transfer(master
, spi
, xfer
);
506 static bool mtk_spi_can_dma(struct spi_master
*master
,
507 struct spi_device
*spi
,
508 struct spi_transfer
*xfer
)
510 /* Buffers for DMA transactions must be 4-byte aligned */
511 return (xfer
->len
> MTK_SPI_MAX_FIFO_SIZE
&&
512 (unsigned long)xfer
->tx_buf
% 4 == 0 &&
513 (unsigned long)xfer
->rx_buf
% 4 == 0);
516 static int mtk_spi_setup(struct spi_device
*spi
)
518 struct mtk_spi
*mdata
= spi_master_get_devdata(spi
->master
);
520 if (!spi
->controller_data
)
521 spi
->controller_data
= (void *)&mtk_default_chip_info
;
523 if (mdata
->dev_comp
->need_pad_sel
&& gpio_is_valid(spi
->cs_gpio
))
524 gpio_direction_output(spi
->cs_gpio
, !(spi
->mode
& SPI_CS_HIGH
));
529 static irqreturn_t
mtk_spi_interrupt(int irq
, void *dev_id
)
531 u32 cmd
, reg_val
, cnt
, remainder
, len
;
532 struct spi_master
*master
= dev_id
;
533 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
534 struct spi_transfer
*trans
= mdata
->cur_transfer
;
536 reg_val
= readl(mdata
->base
+ SPI_STATUS0_REG
);
537 if (reg_val
& MTK_SPI_PAUSE_INT_STATUS
)
538 mdata
->state
= MTK_SPI_PAUSED
;
540 mdata
->state
= MTK_SPI_IDLE
;
542 if (!master
->can_dma(master
, master
->cur_msg
->spi
, trans
)) {
544 cnt
= mdata
->xfer_len
/ 4;
545 ioread32_rep(mdata
->base
+ SPI_RX_DATA_REG
,
546 trans
->rx_buf
+ mdata
->num_xfered
, cnt
);
547 remainder
= mdata
->xfer_len
% 4;
549 reg_val
= readl(mdata
->base
+ SPI_RX_DATA_REG
);
550 memcpy(trans
->rx_buf
+
558 mdata
->num_xfered
+= mdata
->xfer_len
;
559 if (mdata
->num_xfered
== trans
->len
) {
560 spi_finalize_current_transfer(master
);
564 len
= trans
->len
- mdata
->num_xfered
;
565 mdata
->xfer_len
= min(MTK_SPI_MAX_FIFO_SIZE
, len
);
566 mtk_spi_setup_packet(master
);
568 cnt
= mdata
->xfer_len
/ 4;
569 iowrite32_rep(mdata
->base
+ SPI_TX_DATA_REG
,
570 trans
->tx_buf
+ mdata
->num_xfered
, cnt
);
572 remainder
= mdata
->xfer_len
% 4;
576 trans
->tx_buf
+ (cnt
* 4) + mdata
->num_xfered
,
578 writel(reg_val
, mdata
->base
+ SPI_TX_DATA_REG
);
581 mtk_spi_enable_transfer(master
);
587 trans
->tx_dma
+= mdata
->xfer_len
;
589 trans
->rx_dma
+= mdata
->xfer_len
;
591 if (mdata
->tx_sgl
&& (mdata
->tx_sgl_len
== 0)) {
592 mdata
->tx_sgl
= sg_next(mdata
->tx_sgl
);
594 trans
->tx_dma
= sg_dma_address(mdata
->tx_sgl
);
595 mdata
->tx_sgl_len
= sg_dma_len(mdata
->tx_sgl
);
598 if (mdata
->rx_sgl
&& (mdata
->rx_sgl_len
== 0)) {
599 mdata
->rx_sgl
= sg_next(mdata
->rx_sgl
);
601 trans
->rx_dma
= sg_dma_address(mdata
->rx_sgl
);
602 mdata
->rx_sgl_len
= sg_dma_len(mdata
->rx_sgl
);
606 if (!mdata
->tx_sgl
&& !mdata
->rx_sgl
) {
607 /* spi disable dma */
608 cmd
= readl(mdata
->base
+ SPI_CMD_REG
);
609 cmd
&= ~SPI_CMD_TX_DMA
;
610 cmd
&= ~SPI_CMD_RX_DMA
;
611 writel(cmd
, mdata
->base
+ SPI_CMD_REG
);
613 spi_finalize_current_transfer(master
);
617 mtk_spi_update_mdata_len(master
);
618 mtk_spi_setup_packet(master
);
619 mtk_spi_setup_dma_addr(master
, trans
);
620 mtk_spi_enable_transfer(master
);
625 static int mtk_spi_probe(struct platform_device
*pdev
)
627 struct spi_master
*master
;
628 struct mtk_spi
*mdata
;
629 const struct of_device_id
*of_id
;
630 int i
, irq
, ret
, addr_bits
;
632 master
= spi_alloc_master(&pdev
->dev
, sizeof(*mdata
));
634 dev_err(&pdev
->dev
, "failed to alloc spi master\n");
638 master
->auto_runtime_pm
= true;
639 master
->dev
.of_node
= pdev
->dev
.of_node
;
640 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
;
642 master
->set_cs
= mtk_spi_set_cs
;
643 master
->prepare_message
= mtk_spi_prepare_message
;
644 master
->transfer_one
= mtk_spi_transfer_one
;
645 master
->can_dma
= mtk_spi_can_dma
;
646 master
->setup
= mtk_spi_setup
;
648 of_id
= of_match_node(mtk_spi_of_match
, pdev
->dev
.of_node
);
650 dev_err(&pdev
->dev
, "failed to probe of_node\n");
655 mdata
= spi_master_get_devdata(master
);
656 mdata
->dev_comp
= of_id
->data
;
658 if (mdata
->dev_comp
->enhance_timing
)
659 master
->mode_bits
|= SPI_CS_HIGH
;
661 if (mdata
->dev_comp
->must_tx
)
662 master
->flags
= SPI_MASTER_MUST_TX
;
664 if (mdata
->dev_comp
->need_pad_sel
) {
665 mdata
->pad_num
= of_property_count_u32_elems(
667 "mediatek,pad-select");
668 if (mdata
->pad_num
< 0) {
670 "No 'mediatek,pad-select' property\n");
675 mdata
->pad_sel
= devm_kmalloc_array(&pdev
->dev
, mdata
->pad_num
,
676 sizeof(u32
), GFP_KERNEL
);
677 if (!mdata
->pad_sel
) {
682 for (i
= 0; i
< mdata
->pad_num
; i
++) {
683 of_property_read_u32_index(pdev
->dev
.of_node
,
684 "mediatek,pad-select",
685 i
, &mdata
->pad_sel
[i
]);
686 if (mdata
->pad_sel
[i
] > MT8173_SPI_MAX_PAD_SEL
) {
687 dev_err(&pdev
->dev
, "wrong pad-sel[%d]: %u\n",
688 i
, mdata
->pad_sel
[i
]);
695 platform_set_drvdata(pdev
, master
);
696 mdata
->base
= devm_platform_ioremap_resource(pdev
, 0);
697 if (IS_ERR(mdata
->base
)) {
698 ret
= PTR_ERR(mdata
->base
);
702 irq
= platform_get_irq(pdev
, 0);
708 if (!pdev
->dev
.dma_mask
)
709 pdev
->dev
.dma_mask
= &pdev
->dev
.coherent_dma_mask
;
711 ret
= devm_request_irq(&pdev
->dev
, irq
, mtk_spi_interrupt
,
712 IRQF_TRIGGER_NONE
, dev_name(&pdev
->dev
), master
);
714 dev_err(&pdev
->dev
, "failed to register irq (%d)\n", ret
);
718 mdata
->parent_clk
= devm_clk_get(&pdev
->dev
, "parent-clk");
719 if (IS_ERR(mdata
->parent_clk
)) {
720 ret
= PTR_ERR(mdata
->parent_clk
);
721 dev_err(&pdev
->dev
, "failed to get parent-clk: %d\n", ret
);
725 mdata
->sel_clk
= devm_clk_get(&pdev
->dev
, "sel-clk");
726 if (IS_ERR(mdata
->sel_clk
)) {
727 ret
= PTR_ERR(mdata
->sel_clk
);
728 dev_err(&pdev
->dev
, "failed to get sel-clk: %d\n", ret
);
732 mdata
->spi_clk
= devm_clk_get(&pdev
->dev
, "spi-clk");
733 if (IS_ERR(mdata
->spi_clk
)) {
734 ret
= PTR_ERR(mdata
->spi_clk
);
735 dev_err(&pdev
->dev
, "failed to get spi-clk: %d\n", ret
);
739 ret
= clk_prepare_enable(mdata
->spi_clk
);
741 dev_err(&pdev
->dev
, "failed to enable spi_clk (%d)\n", ret
);
745 ret
= clk_set_parent(mdata
->sel_clk
, mdata
->parent_clk
);
747 dev_err(&pdev
->dev
, "failed to clk_set_parent (%d)\n", ret
);
748 clk_disable_unprepare(mdata
->spi_clk
);
752 clk_disable_unprepare(mdata
->spi_clk
);
754 pm_runtime_enable(&pdev
->dev
);
756 ret
= devm_spi_register_master(&pdev
->dev
, master
);
758 dev_err(&pdev
->dev
, "failed to register master (%d)\n", ret
);
759 goto err_disable_runtime_pm
;
762 if (mdata
->dev_comp
->need_pad_sel
) {
763 if (mdata
->pad_num
!= master
->num_chipselect
) {
765 "pad_num does not match num_chipselect(%d != %d)\n",
766 mdata
->pad_num
, master
->num_chipselect
);
768 goto err_disable_runtime_pm
;
771 if (!master
->cs_gpios
&& master
->num_chipselect
> 1) {
773 "cs_gpios not specified and num_chipselect > 1\n");
775 goto err_disable_runtime_pm
;
778 if (master
->cs_gpios
) {
779 for (i
= 0; i
< master
->num_chipselect
; i
++) {
780 ret
= devm_gpio_request(&pdev
->dev
,
782 dev_name(&pdev
->dev
));
785 "can't get CS GPIO %i\n", i
);
786 goto err_disable_runtime_pm
;
792 if (mdata
->dev_comp
->dma_ext
)
793 addr_bits
= DMA_ADDR_EXT_BITS
;
795 addr_bits
= DMA_ADDR_DEF_BITS
;
796 ret
= dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(addr_bits
));
798 dev_notice(&pdev
->dev
, "SPI dma_set_mask(%d) failed, ret:%d\n",
803 err_disable_runtime_pm
:
804 pm_runtime_disable(&pdev
->dev
);
806 spi_master_put(master
);
811 static int mtk_spi_remove(struct platform_device
*pdev
)
813 struct spi_master
*master
= platform_get_drvdata(pdev
);
814 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
816 pm_runtime_disable(&pdev
->dev
);
818 mtk_spi_reset(mdata
);
823 #ifdef CONFIG_PM_SLEEP
824 static int mtk_spi_suspend(struct device
*dev
)
827 struct spi_master
*master
= dev_get_drvdata(dev
);
828 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
830 ret
= spi_master_suspend(master
);
834 if (!pm_runtime_suspended(dev
))
835 clk_disable_unprepare(mdata
->spi_clk
);
840 static int mtk_spi_resume(struct device
*dev
)
843 struct spi_master
*master
= dev_get_drvdata(dev
);
844 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
846 if (!pm_runtime_suspended(dev
)) {
847 ret
= clk_prepare_enable(mdata
->spi_clk
);
849 dev_err(dev
, "failed to enable spi_clk (%d)\n", ret
);
854 ret
= spi_master_resume(master
);
856 clk_disable_unprepare(mdata
->spi_clk
);
860 #endif /* CONFIG_PM_SLEEP */
863 static int mtk_spi_runtime_suspend(struct device
*dev
)
865 struct spi_master
*master
= dev_get_drvdata(dev
);
866 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
868 clk_disable_unprepare(mdata
->spi_clk
);
873 static int mtk_spi_runtime_resume(struct device
*dev
)
875 struct spi_master
*master
= dev_get_drvdata(dev
);
876 struct mtk_spi
*mdata
= spi_master_get_devdata(master
);
879 ret
= clk_prepare_enable(mdata
->spi_clk
);
881 dev_err(dev
, "failed to enable spi_clk (%d)\n", ret
);
887 #endif /* CONFIG_PM */
889 static const struct dev_pm_ops mtk_spi_pm
= {
890 SET_SYSTEM_SLEEP_PM_OPS(mtk_spi_suspend
, mtk_spi_resume
)
891 SET_RUNTIME_PM_OPS(mtk_spi_runtime_suspend
,
892 mtk_spi_runtime_resume
, NULL
)
895 static struct platform_driver mtk_spi_driver
= {
899 .of_match_table
= mtk_spi_of_match
,
901 .probe
= mtk_spi_probe
,
902 .remove
= mtk_spi_remove
,
905 module_platform_driver(mtk_spi_driver
);
907 MODULE_DESCRIPTION("MTK SPI Controller driver");
908 MODULE_AUTHOR("Leilk Liu <leilk.liu@mediatek.com>");
909 MODULE_LICENSE("GPL v2");
910 MODULE_ALIAS("platform:mtk-spi");