2 * Copyright (c) 2015 Pengutronix, Sascha Hauer <kernel@pengutronix.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 #include <linux/clk.h>
14 #include <linux/init.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_domain.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/soc/mediatek/infracfg.h>
23 #include <dt-bindings/power/mt2701-power.h>
24 #include <dt-bindings/power/mt8173-power.h>
26 #define SPM_VDE_PWR_CON 0x0210
27 #define SPM_MFG_PWR_CON 0x0214
28 #define SPM_VEN_PWR_CON 0x0230
29 #define SPM_ISP_PWR_CON 0x0238
30 #define SPM_DIS_PWR_CON 0x023c
31 #define SPM_CONN_PWR_CON 0x0280
32 #define SPM_VEN2_PWR_CON 0x0298
33 #define SPM_AUDIO_PWR_CON 0x029c /* MT8173 */
34 #define SPM_BDP_PWR_CON 0x029c /* MT2701 */
35 #define SPM_ETH_PWR_CON 0x02a0
36 #define SPM_HIF_PWR_CON 0x02a4
37 #define SPM_IFR_MSC_PWR_CON 0x02a8
38 #define SPM_MFG_2D_PWR_CON 0x02c0
39 #define SPM_MFG_ASYNC_PWR_CON 0x02c4
40 #define SPM_USB_PWR_CON 0x02cc
42 #define SPM_PWR_STATUS 0x060c
43 #define SPM_PWR_STATUS_2ND 0x0610
45 #define PWR_RST_B_BIT BIT(0)
46 #define PWR_ISO_BIT BIT(1)
47 #define PWR_ON_BIT BIT(2)
48 #define PWR_ON_2ND_BIT BIT(3)
49 #define PWR_CLK_DIS_BIT BIT(4)
51 #define PWR_STATUS_CONN BIT(1)
52 #define PWR_STATUS_DISP BIT(3)
53 #define PWR_STATUS_MFG BIT(4)
54 #define PWR_STATUS_ISP BIT(5)
55 #define PWR_STATUS_VDEC BIT(7)
56 #define PWR_STATUS_BDP BIT(14)
57 #define PWR_STATUS_ETH BIT(15)
58 #define PWR_STATUS_HIF BIT(16)
59 #define PWR_STATUS_IFR_MSC BIT(17)
60 #define PWR_STATUS_VENC_LT BIT(20)
61 #define PWR_STATUS_VENC BIT(21)
62 #define PWR_STATUS_MFG_2D BIT(22)
63 #define PWR_STATUS_MFG_ASYNC BIT(23)
64 #define PWR_STATUS_AUDIO BIT(24)
65 #define PWR_STATUS_USB BIT(25)
77 static const char * const clk_names
[] = {
89 struct scp_domain_data
{
94 u32 sram_pdn_ack_bits
;
96 enum clk_id clk_id
[MAX_CLKS
];
103 struct generic_pm_domain genpd
;
105 struct clk
*clk
[MAX_CLKS
];
106 const struct scp_domain_data
*data
;
107 struct regulator
*supply
;
111 struct scp_domain
*domains
;
112 struct genpd_onecell_data pd_data
;
115 struct regmap
*infracfg
;
118 static int scpsys_domain_is_on(struct scp_domain
*scpd
)
120 struct scp
*scp
= scpd
->scp
;
122 u32 status
= readl(scp
->base
+ SPM_PWR_STATUS
) & scpd
->data
->sta_mask
;
123 u32 status2
= readl(scp
->base
+ SPM_PWR_STATUS_2ND
) &
124 scpd
->data
->sta_mask
;
127 * A domain is on when both status bits are set. If only one is set
128 * return an error. This happens while powering up a domain
131 if (status
&& status2
)
133 if (!status
&& !status2
)
139 static int scpsys_power_on(struct generic_pm_domain
*genpd
)
141 struct scp_domain
*scpd
= container_of(genpd
, struct scp_domain
, genpd
);
142 struct scp
*scp
= scpd
->scp
;
143 unsigned long timeout
;
145 void __iomem
*ctl_addr
= scp
->base
+ scpd
->data
->ctl_offs
;
146 u32 sram_pdn_ack
= scpd
->data
->sram_pdn_ack_bits
;
152 ret
= regulator_enable(scpd
->supply
);
157 for (i
= 0; i
< MAX_CLKS
&& scpd
->clk
[i
]; i
++) {
158 ret
= clk_prepare_enable(scpd
->clk
[i
]);
160 for (--i
; i
>= 0; i
--)
161 clk_disable_unprepare(scpd
->clk
[i
]);
167 val
= readl(ctl_addr
);
169 writel(val
, ctl_addr
);
170 val
|= PWR_ON_2ND_BIT
;
171 writel(val
, ctl_addr
);
173 /* wait until PWR_ACK = 1 */
174 timeout
= jiffies
+ HZ
;
177 ret
= scpsys_domain_is_on(scpd
);
188 if (time_after(jiffies
, timeout
))
192 val
&= ~PWR_CLK_DIS_BIT
;
193 writel(val
, ctl_addr
);
196 writel(val
, ctl_addr
);
198 val
|= PWR_RST_B_BIT
;
199 writel(val
, ctl_addr
);
201 val
&= ~scpd
->data
->sram_pdn_bits
;
202 writel(val
, ctl_addr
);
204 /* wait until SRAM_PDN_ACK all 0 */
205 timeout
= jiffies
+ HZ
;
207 while (sram_pdn_ack
&& (readl(ctl_addr
) & sram_pdn_ack
)) {
216 if (time_after(jiffies
, timeout
))
220 if (scpd
->data
->bus_prot_mask
) {
221 ret
= mtk_infracfg_clear_bus_protection(scp
->infracfg
,
222 scpd
->data
->bus_prot_mask
);
230 for (i
= MAX_CLKS
- 1; i
>= 0; i
--) {
232 clk_disable_unprepare(scpd
->clk
[i
]);
236 regulator_disable(scpd
->supply
);
238 dev_err(scp
->dev
, "Failed to power on domain %s\n", genpd
->name
);
243 static int scpsys_power_off(struct generic_pm_domain
*genpd
)
245 struct scp_domain
*scpd
= container_of(genpd
, struct scp_domain
, genpd
);
246 struct scp
*scp
= scpd
->scp
;
247 unsigned long timeout
;
249 void __iomem
*ctl_addr
= scp
->base
+ scpd
->data
->ctl_offs
;
250 u32 pdn_ack
= scpd
->data
->sram_pdn_ack_bits
;
255 if (scpd
->data
->bus_prot_mask
) {
256 ret
= mtk_infracfg_set_bus_protection(scp
->infracfg
,
257 scpd
->data
->bus_prot_mask
);
262 val
= readl(ctl_addr
);
263 val
|= scpd
->data
->sram_pdn_bits
;
264 writel(val
, ctl_addr
);
266 /* wait until SRAM_PDN_ACK all 1 */
267 timeout
= jiffies
+ HZ
;
269 while (pdn_ack
&& (readl(ctl_addr
) & pdn_ack
) != pdn_ack
) {
277 if (time_after(jiffies
, timeout
))
282 writel(val
, ctl_addr
);
284 val
&= ~PWR_RST_B_BIT
;
285 writel(val
, ctl_addr
);
287 val
|= PWR_CLK_DIS_BIT
;
288 writel(val
, ctl_addr
);
291 writel(val
, ctl_addr
);
293 val
&= ~PWR_ON_2ND_BIT
;
294 writel(val
, ctl_addr
);
296 /* wait until PWR_ACK = 0 */
297 timeout
= jiffies
+ HZ
;
300 ret
= scpsys_domain_is_on(scpd
);
311 if (time_after(jiffies
, timeout
))
315 for (i
= 0; i
< MAX_CLKS
&& scpd
->clk
[i
]; i
++)
316 clk_disable_unprepare(scpd
->clk
[i
]);
319 regulator_disable(scpd
->supply
);
324 dev_err(scp
->dev
, "Failed to power off domain %s\n", genpd
->name
);
329 static bool scpsys_active_wakeup(struct device
*dev
)
331 struct generic_pm_domain
*genpd
;
332 struct scp_domain
*scpd
;
334 genpd
= pd_to_genpd(dev
->pm_domain
);
335 scpd
= container_of(genpd
, struct scp_domain
, genpd
);
337 return scpd
->data
->active_wakeup
;
340 static void init_clks(struct platform_device
*pdev
, struct clk
**clk
)
344 for (i
= CLK_NONE
+ 1; i
< CLK_MAX
; i
++)
345 clk
[i
] = devm_clk_get(&pdev
->dev
, clk_names
[i
]);
348 static struct scp
*init_scp(struct platform_device
*pdev
,
349 const struct scp_domain_data
*scp_domain_data
, int num
)
351 struct genpd_onecell_data
*pd_data
;
352 struct resource
*res
;
355 struct clk
*clk
[CLK_MAX
];
357 scp
= devm_kzalloc(&pdev
->dev
, sizeof(*scp
), GFP_KERNEL
);
359 return ERR_PTR(-ENOMEM
);
361 scp
->dev
= &pdev
->dev
;
363 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
364 scp
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
365 if (IS_ERR(scp
->base
))
366 return ERR_CAST(scp
->base
);
368 scp
->domains
= devm_kzalloc(&pdev
->dev
,
369 sizeof(*scp
->domains
) * num
, GFP_KERNEL
);
371 return ERR_PTR(-ENOMEM
);
373 pd_data
= &scp
->pd_data
;
375 pd_data
->domains
= devm_kzalloc(&pdev
->dev
,
376 sizeof(*pd_data
->domains
) * num
, GFP_KERNEL
);
377 if (!pd_data
->domains
)
378 return ERR_PTR(-ENOMEM
);
380 scp
->infracfg
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
382 if (IS_ERR(scp
->infracfg
)) {
383 dev_err(&pdev
->dev
, "Cannot find infracfg controller: %ld\n",
384 PTR_ERR(scp
->infracfg
));
385 return ERR_CAST(scp
->infracfg
);
388 for (i
= 0; i
< num
; i
++) {
389 struct scp_domain
*scpd
= &scp
->domains
[i
];
390 const struct scp_domain_data
*data
= &scp_domain_data
[i
];
392 scpd
->supply
= devm_regulator_get_optional(&pdev
->dev
, data
->name
);
393 if (IS_ERR(scpd
->supply
)) {
394 if (PTR_ERR(scpd
->supply
) == -ENODEV
)
397 return ERR_CAST(scpd
->supply
);
401 pd_data
->num_domains
= num
;
403 init_clks(pdev
, clk
);
405 for (i
= 0; i
< num
; i
++) {
406 struct scp_domain
*scpd
= &scp
->domains
[i
];
407 struct generic_pm_domain
*genpd
= &scpd
->genpd
;
408 const struct scp_domain_data
*data
= &scp_domain_data
[i
];
410 pd_data
->domains
[i
] = genpd
;
415 for (j
= 0; j
< MAX_CLKS
&& data
->clk_id
[j
]; j
++) {
416 struct clk
*c
= clk
[data
->clk_id
[j
]];
419 dev_err(&pdev
->dev
, "%s: clk unavailable\n",
427 genpd
->name
= data
->name
;
428 genpd
->power_off
= scpsys_power_off
;
429 genpd
->power_on
= scpsys_power_on
;
430 genpd
->dev_ops
.active_wakeup
= scpsys_active_wakeup
;
436 static void mtk_register_power_domains(struct platform_device
*pdev
,
437 struct scp
*scp
, int num
)
439 struct genpd_onecell_data
*pd_data
;
442 for (i
= 0; i
< num
; i
++) {
443 struct scp_domain
*scpd
= &scp
->domains
[i
];
444 struct generic_pm_domain
*genpd
= &scpd
->genpd
;
447 * Initially turn on all domains to make the domains usable
448 * with !CONFIG_PM and to get the hardware in sync with the
449 * software. The unused domains will be switched off during
452 genpd
->power_on(genpd
);
454 pm_genpd_init(genpd
, NULL
, false);
458 * We are not allowed to fail here since there is no way to unregister
459 * a power domain. Once registered above we have to keep the domains
463 pd_data
= &scp
->pd_data
;
465 ret
= of_genpd_add_provider_onecell(pdev
->dev
.of_node
, pd_data
);
467 dev_err(&pdev
->dev
, "Failed to add OF provider: %d\n", ret
);
471 * MT2701 power domain support
474 static const struct scp_domain_data scp_domain_data_mt2701
[] = {
475 [MT2701_POWER_DOMAIN_CONN
] = {
477 .sta_mask
= PWR_STATUS_CONN
,
478 .ctl_offs
= SPM_CONN_PWR_CON
,
479 .bus_prot_mask
= 0x0104,
480 .clk_id
= {CLK_NONE
},
481 .active_wakeup
= true,
483 [MT2701_POWER_DOMAIN_DISP
] = {
485 .sta_mask
= PWR_STATUS_DISP
,
486 .ctl_offs
= SPM_DIS_PWR_CON
,
487 .sram_pdn_bits
= GENMASK(11, 8),
489 .bus_prot_mask
= 0x0002,
490 .active_wakeup
= true,
492 [MT2701_POWER_DOMAIN_MFG
] = {
494 .sta_mask
= PWR_STATUS_MFG
,
495 .ctl_offs
= SPM_MFG_PWR_CON
,
496 .sram_pdn_bits
= GENMASK(11, 8),
497 .sram_pdn_ack_bits
= GENMASK(12, 12),
499 .active_wakeup
= true,
501 [MT2701_POWER_DOMAIN_VDEC
] = {
503 .sta_mask
= PWR_STATUS_VDEC
,
504 .ctl_offs
= SPM_VDE_PWR_CON
,
505 .sram_pdn_bits
= GENMASK(11, 8),
506 .sram_pdn_ack_bits
= GENMASK(12, 12),
508 .active_wakeup
= true,
510 [MT2701_POWER_DOMAIN_ISP
] = {
512 .sta_mask
= PWR_STATUS_ISP
,
513 .ctl_offs
= SPM_ISP_PWR_CON
,
514 .sram_pdn_bits
= GENMASK(11, 8),
515 .sram_pdn_ack_bits
= GENMASK(13, 12),
517 .active_wakeup
= true,
519 [MT2701_POWER_DOMAIN_BDP
] = {
521 .sta_mask
= PWR_STATUS_BDP
,
522 .ctl_offs
= SPM_BDP_PWR_CON
,
523 .sram_pdn_bits
= GENMASK(11, 8),
524 .clk_id
= {CLK_NONE
},
525 .active_wakeup
= true,
527 [MT2701_POWER_DOMAIN_ETH
] = {
529 .sta_mask
= PWR_STATUS_ETH
,
530 .ctl_offs
= SPM_ETH_PWR_CON
,
531 .sram_pdn_bits
= GENMASK(11, 8),
532 .sram_pdn_ack_bits
= GENMASK(15, 12),
533 .clk_id
= {CLK_ETHIF
},
534 .active_wakeup
= true,
536 [MT2701_POWER_DOMAIN_HIF
] = {
538 .sta_mask
= PWR_STATUS_HIF
,
539 .ctl_offs
= SPM_HIF_PWR_CON
,
540 .sram_pdn_bits
= GENMASK(11, 8),
541 .sram_pdn_ack_bits
= GENMASK(15, 12),
542 .clk_id
= {CLK_ETHIF
},
543 .active_wakeup
= true,
545 [MT2701_POWER_DOMAIN_IFR_MSC
] = {
547 .sta_mask
= PWR_STATUS_IFR_MSC
,
548 .ctl_offs
= SPM_IFR_MSC_PWR_CON
,
549 .clk_id
= {CLK_NONE
},
550 .active_wakeup
= true,
554 #define NUM_DOMAINS_MT2701 ARRAY_SIZE(scp_domain_data_mt2701)
556 static int __init
scpsys_probe_mt2701(struct platform_device
*pdev
)
560 scp
= init_scp(pdev
, scp_domain_data_mt2701
, NUM_DOMAINS_MT2701
);
564 mtk_register_power_domains(pdev
, scp
, NUM_DOMAINS_MT2701
);
570 * MT8173 power domain support
573 static const struct scp_domain_data scp_domain_data_mt8173
[] = {
574 [MT8173_POWER_DOMAIN_VDEC
] = {
576 .sta_mask
= PWR_STATUS_VDEC
,
577 .ctl_offs
= SPM_VDE_PWR_CON
,
578 .sram_pdn_bits
= GENMASK(11, 8),
579 .sram_pdn_ack_bits
= GENMASK(12, 12),
582 [MT8173_POWER_DOMAIN_VENC
] = {
584 .sta_mask
= PWR_STATUS_VENC
,
585 .ctl_offs
= SPM_VEN_PWR_CON
,
586 .sram_pdn_bits
= GENMASK(11, 8),
587 .sram_pdn_ack_bits
= GENMASK(15, 12),
588 .clk_id
= {CLK_MM
, CLK_VENC
},
590 [MT8173_POWER_DOMAIN_ISP
] = {
592 .sta_mask
= PWR_STATUS_ISP
,
593 .ctl_offs
= SPM_ISP_PWR_CON
,
594 .sram_pdn_bits
= GENMASK(11, 8),
595 .sram_pdn_ack_bits
= GENMASK(13, 12),
598 [MT8173_POWER_DOMAIN_MM
] = {
600 .sta_mask
= PWR_STATUS_DISP
,
601 .ctl_offs
= SPM_DIS_PWR_CON
,
602 .sram_pdn_bits
= GENMASK(11, 8),
603 .sram_pdn_ack_bits
= GENMASK(12, 12),
605 .bus_prot_mask
= MT8173_TOP_AXI_PROT_EN_MM_M0
|
606 MT8173_TOP_AXI_PROT_EN_MM_M1
,
608 [MT8173_POWER_DOMAIN_VENC_LT
] = {
610 .sta_mask
= PWR_STATUS_VENC_LT
,
611 .ctl_offs
= SPM_VEN2_PWR_CON
,
612 .sram_pdn_bits
= GENMASK(11, 8),
613 .sram_pdn_ack_bits
= GENMASK(15, 12),
614 .clk_id
= {CLK_MM
, CLK_VENC_LT
},
616 [MT8173_POWER_DOMAIN_AUDIO
] = {
618 .sta_mask
= PWR_STATUS_AUDIO
,
619 .ctl_offs
= SPM_AUDIO_PWR_CON
,
620 .sram_pdn_bits
= GENMASK(11, 8),
621 .sram_pdn_ack_bits
= GENMASK(15, 12),
622 .clk_id
= {CLK_NONE
},
624 [MT8173_POWER_DOMAIN_USB
] = {
626 .sta_mask
= PWR_STATUS_USB
,
627 .ctl_offs
= SPM_USB_PWR_CON
,
628 .sram_pdn_bits
= GENMASK(11, 8),
629 .sram_pdn_ack_bits
= GENMASK(15, 12),
630 .clk_id
= {CLK_NONE
},
631 .active_wakeup
= true,
633 [MT8173_POWER_DOMAIN_MFG_ASYNC
] = {
635 .sta_mask
= PWR_STATUS_MFG_ASYNC
,
636 .ctl_offs
= SPM_MFG_ASYNC_PWR_CON
,
637 .sram_pdn_bits
= GENMASK(11, 8),
638 .sram_pdn_ack_bits
= 0,
641 [MT8173_POWER_DOMAIN_MFG_2D
] = {
643 .sta_mask
= PWR_STATUS_MFG_2D
,
644 .ctl_offs
= SPM_MFG_2D_PWR_CON
,
645 .sram_pdn_bits
= GENMASK(11, 8),
646 .sram_pdn_ack_bits
= GENMASK(13, 12),
647 .clk_id
= {CLK_NONE
},
649 [MT8173_POWER_DOMAIN_MFG
] = {
651 .sta_mask
= PWR_STATUS_MFG
,
652 .ctl_offs
= SPM_MFG_PWR_CON
,
653 .sram_pdn_bits
= GENMASK(13, 8),
654 .sram_pdn_ack_bits
= GENMASK(21, 16),
655 .clk_id
= {CLK_NONE
},
656 .bus_prot_mask
= MT8173_TOP_AXI_PROT_EN_MFG_S
|
657 MT8173_TOP_AXI_PROT_EN_MFG_M0
|
658 MT8173_TOP_AXI_PROT_EN_MFG_M1
|
659 MT8173_TOP_AXI_PROT_EN_MFG_SNOOP_OUT
,
663 #define NUM_DOMAINS_MT8173 ARRAY_SIZE(scp_domain_data_mt8173)
665 static int __init
scpsys_probe_mt8173(struct platform_device
*pdev
)
668 struct genpd_onecell_data
*pd_data
;
671 scp
= init_scp(pdev
, scp_domain_data_mt8173
, NUM_DOMAINS_MT8173
);
675 mtk_register_power_domains(pdev
, scp
, NUM_DOMAINS_MT8173
);
677 pd_data
= &scp
->pd_data
;
679 ret
= pm_genpd_add_subdomain(pd_data
->domains
[MT8173_POWER_DOMAIN_MFG_ASYNC
],
680 pd_data
->domains
[MT8173_POWER_DOMAIN_MFG_2D
]);
681 if (ret
&& IS_ENABLED(CONFIG_PM
))
682 dev_err(&pdev
->dev
, "Failed to add subdomain: %d\n", ret
);
684 ret
= pm_genpd_add_subdomain(pd_data
->domains
[MT8173_POWER_DOMAIN_MFG_2D
],
685 pd_data
->domains
[MT8173_POWER_DOMAIN_MFG
]);
686 if (ret
&& IS_ENABLED(CONFIG_PM
))
687 dev_err(&pdev
->dev
, "Failed to add subdomain: %d\n", ret
);
696 static const struct of_device_id of_scpsys_match_tbl
[] = {
698 .compatible
= "mediatek,mt2701-scpsys",
699 .data
= scpsys_probe_mt2701
,
701 .compatible
= "mediatek,mt8173-scpsys",
702 .data
= scpsys_probe_mt8173
,
708 static int scpsys_probe(struct platform_device
*pdev
)
710 int (*probe
)(struct platform_device
*);
711 const struct of_device_id
*of_id
;
713 of_id
= of_match_node(of_scpsys_match_tbl
, pdev
->dev
.of_node
);
714 if (!of_id
|| !of_id
->data
)
722 static struct platform_driver scpsys_drv
= {
723 .probe
= scpsys_probe
,
725 .name
= "mtk-scpsys",
726 .suppress_bind_attrs
= true,
727 .owner
= THIS_MODULE
,
728 .of_match_table
= of_match_ptr(of_scpsys_match_tbl
),
731 builtin_platform_driver(scpsys_drv
);