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/delay.h>
16 #include <linux/kernel.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_domain.h>
22 #include <linux/regmap.h>
23 #include <linux/soc/mediatek/infracfg.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_VEN2_PWR_CON 0x0298
32 #define SPM_AUDIO_PWR_CON 0x029c
33 #define SPM_MFG_2D_PWR_CON 0x02c0
34 #define SPM_MFG_ASYNC_PWR_CON 0x02c4
35 #define SPM_USB_PWR_CON 0x02cc
36 #define SPM_PWR_STATUS 0x060c
37 #define SPM_PWR_STATUS_2ND 0x0610
39 #define PWR_RST_B_BIT BIT(0)
40 #define PWR_ISO_BIT BIT(1)
41 #define PWR_ON_BIT BIT(2)
42 #define PWR_ON_2ND_BIT BIT(3)
43 #define PWR_CLK_DIS_BIT BIT(4)
45 #define PWR_STATUS_DISP BIT(3)
46 #define PWR_STATUS_MFG BIT(4)
47 #define PWR_STATUS_ISP BIT(5)
48 #define PWR_STATUS_VDEC BIT(7)
49 #define PWR_STATUS_VENC_LT BIT(20)
50 #define PWR_STATUS_VENC BIT(21)
51 #define PWR_STATUS_MFG_2D BIT(22)
52 #define PWR_STATUS_MFG_ASYNC BIT(23)
53 #define PWR_STATUS_AUDIO BIT(24)
54 #define PWR_STATUS_USB BIT(25)
67 struct scp_domain_data
{
72 u32 sram_pdn_ack_bits
;
74 enum clk_id clk_id
[MAX_CLKS
];
78 static const struct scp_domain_data scp_domain_data
[] __initconst
= {
79 [MT8173_POWER_DOMAIN_VDEC
] = {
81 .sta_mask
= PWR_STATUS_VDEC
,
82 .ctl_offs
= SPM_VDE_PWR_CON
,
83 .sram_pdn_bits
= GENMASK(11, 8),
84 .sram_pdn_ack_bits
= GENMASK(12, 12),
85 .clk_id
= {MT8173_CLK_MM
},
87 [MT8173_POWER_DOMAIN_VENC
] = {
89 .sta_mask
= PWR_STATUS_VENC
,
90 .ctl_offs
= SPM_VEN_PWR_CON
,
91 .sram_pdn_bits
= GENMASK(11, 8),
92 .sram_pdn_ack_bits
= GENMASK(15, 12),
93 .clk_id
= {MT8173_CLK_MM
, MT8173_CLK_VENC
},
95 [MT8173_POWER_DOMAIN_ISP
] = {
97 .sta_mask
= PWR_STATUS_ISP
,
98 .ctl_offs
= SPM_ISP_PWR_CON
,
99 .sram_pdn_bits
= GENMASK(11, 8),
100 .sram_pdn_ack_bits
= GENMASK(13, 12),
101 .clk_id
= {MT8173_CLK_MM
},
103 [MT8173_POWER_DOMAIN_MM
] = {
105 .sta_mask
= PWR_STATUS_DISP
,
106 .ctl_offs
= SPM_DIS_PWR_CON
,
107 .sram_pdn_bits
= GENMASK(11, 8),
108 .sram_pdn_ack_bits
= GENMASK(12, 12),
109 .clk_id
= {MT8173_CLK_MM
},
110 .bus_prot_mask
= MT8173_TOP_AXI_PROT_EN_MM_M0
|
111 MT8173_TOP_AXI_PROT_EN_MM_M1
,
113 [MT8173_POWER_DOMAIN_VENC_LT
] = {
115 .sta_mask
= PWR_STATUS_VENC_LT
,
116 .ctl_offs
= SPM_VEN2_PWR_CON
,
117 .sram_pdn_bits
= GENMASK(11, 8),
118 .sram_pdn_ack_bits
= GENMASK(15, 12),
119 .clk_id
= {MT8173_CLK_MM
, MT8173_CLK_VENC_LT
},
121 [MT8173_POWER_DOMAIN_AUDIO
] = {
123 .sta_mask
= PWR_STATUS_AUDIO
,
124 .ctl_offs
= SPM_AUDIO_PWR_CON
,
125 .sram_pdn_bits
= GENMASK(11, 8),
126 .sram_pdn_ack_bits
= GENMASK(15, 12),
127 .clk_id
= {MT8173_CLK_NONE
},
129 [MT8173_POWER_DOMAIN_USB
] = {
131 .sta_mask
= PWR_STATUS_USB
,
132 .ctl_offs
= SPM_USB_PWR_CON
,
133 .sram_pdn_bits
= GENMASK(11, 8),
134 .sram_pdn_ack_bits
= GENMASK(15, 12),
135 .clk_id
= {MT8173_CLK_NONE
},
136 .active_wakeup
= true,
138 [MT8173_POWER_DOMAIN_MFG_ASYNC
] = {
140 .sta_mask
= PWR_STATUS_MFG_ASYNC
,
141 .ctl_offs
= SPM_MFG_ASYNC_PWR_CON
,
142 .sram_pdn_bits
= GENMASK(11, 8),
143 .sram_pdn_ack_bits
= 0,
144 .clk_id
= {MT8173_CLK_MFG
},
146 [MT8173_POWER_DOMAIN_MFG_2D
] = {
148 .sta_mask
= PWR_STATUS_MFG_2D
,
149 .ctl_offs
= SPM_MFG_2D_PWR_CON
,
150 .sram_pdn_bits
= GENMASK(11, 8),
151 .sram_pdn_ack_bits
= GENMASK(13, 12),
152 .clk_id
= {MT8173_CLK_NONE
},
154 [MT8173_POWER_DOMAIN_MFG
] = {
156 .sta_mask
= PWR_STATUS_MFG
,
157 .ctl_offs
= SPM_MFG_PWR_CON
,
158 .sram_pdn_bits
= GENMASK(13, 8),
159 .sram_pdn_ack_bits
= GENMASK(21, 16),
160 .clk_id
= {MT8173_CLK_NONE
},
161 .bus_prot_mask
= MT8173_TOP_AXI_PROT_EN_MFG_S
|
162 MT8173_TOP_AXI_PROT_EN_MFG_M0
|
163 MT8173_TOP_AXI_PROT_EN_MFG_M1
|
164 MT8173_TOP_AXI_PROT_EN_MFG_SNOOP_OUT
,
168 #define NUM_DOMAINS ARRAY_SIZE(scp_domain_data)
173 struct generic_pm_domain genpd
;
175 struct clk
*clk
[MAX_CLKS
];
177 void __iomem
*ctl_addr
;
179 u32 sram_pdn_ack_bits
;
185 struct scp_domain domains
[NUM_DOMAINS
];
186 struct genpd_onecell_data pd_data
;
189 struct regmap
*infracfg
;
192 static int scpsys_domain_is_on(struct scp_domain
*scpd
)
194 struct scp
*scp
= scpd
->scp
;
196 u32 status
= readl(scp
->base
+ SPM_PWR_STATUS
) & scpd
->sta_mask
;
197 u32 status2
= readl(scp
->base
+ SPM_PWR_STATUS_2ND
) & scpd
->sta_mask
;
200 * A domain is on when both status bits are set. If only one is set
201 * return an error. This happens while powering up a domain
204 if (status
&& status2
)
206 if (!status
&& !status2
)
212 static int scpsys_power_on(struct generic_pm_domain
*genpd
)
214 struct scp_domain
*scpd
= container_of(genpd
, struct scp_domain
, genpd
);
215 struct scp
*scp
= scpd
->scp
;
216 unsigned long timeout
;
218 void __iomem
*ctl_addr
= scpd
->ctl_addr
;
219 u32 sram_pdn_ack
= scpd
->sram_pdn_ack_bits
;
224 for (i
= 0; i
< MAX_CLKS
&& scpd
->clk
[i
]; i
++) {
225 ret
= clk_prepare_enable(scpd
->clk
[i
]);
227 for (--i
; i
>= 0; i
--)
228 clk_disable_unprepare(scpd
->clk
[i
]);
234 val
= readl(ctl_addr
);
236 writel(val
, ctl_addr
);
237 val
|= PWR_ON_2ND_BIT
;
238 writel(val
, ctl_addr
);
240 /* wait until PWR_ACK = 1 */
241 timeout
= jiffies
+ HZ
;
244 ret
= scpsys_domain_is_on(scpd
);
255 if (time_after(jiffies
, timeout
))
259 val
&= ~PWR_CLK_DIS_BIT
;
260 writel(val
, ctl_addr
);
263 writel(val
, ctl_addr
);
265 val
|= PWR_RST_B_BIT
;
266 writel(val
, ctl_addr
);
268 val
&= ~scpd
->sram_pdn_bits
;
269 writel(val
, ctl_addr
);
271 /* wait until SRAM_PDN_ACK all 0 */
272 timeout
= jiffies
+ HZ
;
274 while (sram_pdn_ack
&& (readl(ctl_addr
) & sram_pdn_ack
)) {
283 if (time_after(jiffies
, timeout
))
287 if (scpd
->bus_prot_mask
) {
288 ret
= mtk_infracfg_clear_bus_protection(scp
->infracfg
,
289 scpd
->bus_prot_mask
);
297 for (i
= MAX_CLKS
- 1; i
>= 0; i
--) {
299 clk_disable_unprepare(scpd
->clk
[i
]);
302 dev_err(scp
->dev
, "Failed to power on domain %s\n", genpd
->name
);
307 static int scpsys_power_off(struct generic_pm_domain
*genpd
)
309 struct scp_domain
*scpd
= container_of(genpd
, struct scp_domain
, genpd
);
310 struct scp
*scp
= scpd
->scp
;
311 unsigned long timeout
;
313 void __iomem
*ctl_addr
= scpd
->ctl_addr
;
314 u32 pdn_ack
= scpd
->sram_pdn_ack_bits
;
319 if (scpd
->bus_prot_mask
) {
320 ret
= mtk_infracfg_set_bus_protection(scp
->infracfg
,
321 scpd
->bus_prot_mask
);
326 val
= readl(ctl_addr
);
327 val
|= scpd
->sram_pdn_bits
;
328 writel(val
, ctl_addr
);
330 /* wait until SRAM_PDN_ACK all 1 */
331 timeout
= jiffies
+ HZ
;
333 while (pdn_ack
&& (readl(ctl_addr
) & pdn_ack
) != pdn_ack
) {
341 if (time_after(jiffies
, timeout
))
346 writel(val
, ctl_addr
);
348 val
&= ~PWR_RST_B_BIT
;
349 writel(val
, ctl_addr
);
351 val
|= PWR_CLK_DIS_BIT
;
352 writel(val
, ctl_addr
);
355 writel(val
, ctl_addr
);
357 val
&= ~PWR_ON_2ND_BIT
;
358 writel(val
, ctl_addr
);
360 /* wait until PWR_ACK = 0 */
361 timeout
= jiffies
+ HZ
;
364 ret
= scpsys_domain_is_on(scpd
);
375 if (time_after(jiffies
, timeout
))
379 for (i
= 0; i
< MAX_CLKS
&& scpd
->clk
[i
]; i
++)
380 clk_disable_unprepare(scpd
->clk
[i
]);
385 dev_err(scp
->dev
, "Failed to power off domain %s\n", genpd
->name
);
390 static bool scpsys_active_wakeup(struct device
*dev
)
392 struct generic_pm_domain
*genpd
;
393 struct scp_domain
*scpd
;
395 genpd
= pd_to_genpd(dev
->pm_domain
);
396 scpd
= container_of(genpd
, struct scp_domain
, genpd
);
398 return scpd
->active_wakeup
;
401 static int __init
scpsys_probe(struct platform_device
*pdev
)
403 struct genpd_onecell_data
*pd_data
;
404 struct resource
*res
;
407 struct clk
*clk
[MT8173_CLK_MAX
];
409 scp
= devm_kzalloc(&pdev
->dev
, sizeof(*scp
), GFP_KERNEL
);
413 scp
->dev
= &pdev
->dev
;
415 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
416 scp
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
417 if (IS_ERR(scp
->base
))
418 return PTR_ERR(scp
->base
);
420 pd_data
= &scp
->pd_data
;
422 pd_data
->domains
= devm_kzalloc(&pdev
->dev
,
423 sizeof(*pd_data
->domains
) * NUM_DOMAINS
, GFP_KERNEL
);
424 if (!pd_data
->domains
)
427 clk
[MT8173_CLK_MM
] = devm_clk_get(&pdev
->dev
, "mm");
428 if (IS_ERR(clk
[MT8173_CLK_MM
]))
429 return PTR_ERR(clk
[MT8173_CLK_MM
]);
431 clk
[MT8173_CLK_MFG
] = devm_clk_get(&pdev
->dev
, "mfg");
432 if (IS_ERR(clk
[MT8173_CLK_MFG
]))
433 return PTR_ERR(clk
[MT8173_CLK_MFG
]);
435 clk
[MT8173_CLK_VENC
] = devm_clk_get(&pdev
->dev
, "venc");
436 if (IS_ERR(clk
[MT8173_CLK_VENC
]))
437 return PTR_ERR(clk
[MT8173_CLK_VENC
]);
439 clk
[MT8173_CLK_VENC_LT
] = devm_clk_get(&pdev
->dev
, "venc_lt");
440 if (IS_ERR(clk
[MT8173_CLK_VENC_LT
]))
441 return PTR_ERR(clk
[MT8173_CLK_VENC_LT
]);
443 scp
->infracfg
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
445 if (IS_ERR(scp
->infracfg
)) {
446 dev_err(&pdev
->dev
, "Cannot find infracfg controller: %ld\n",
447 PTR_ERR(scp
->infracfg
));
448 return PTR_ERR(scp
->infracfg
);
451 pd_data
->num_domains
= NUM_DOMAINS
;
453 for (i
= 0; i
< NUM_DOMAINS
; i
++) {
454 struct scp_domain
*scpd
= &scp
->domains
[i
];
455 struct generic_pm_domain
*genpd
= &scpd
->genpd
;
456 const struct scp_domain_data
*data
= &scp_domain_data
[i
];
458 pd_data
->domains
[i
] = genpd
;
461 scpd
->sta_mask
= data
->sta_mask
;
462 scpd
->ctl_addr
= scp
->base
+ data
->ctl_offs
;
463 scpd
->sram_pdn_bits
= data
->sram_pdn_bits
;
464 scpd
->sram_pdn_ack_bits
= data
->sram_pdn_ack_bits
;
465 scpd
->bus_prot_mask
= data
->bus_prot_mask
;
466 scpd
->active_wakeup
= data
->active_wakeup
;
467 for (j
= 0; j
< MAX_CLKS
&& data
->clk_id
[j
]; j
++)
468 scpd
->clk
[j
] = clk
[data
->clk_id
[j
]];
470 genpd
->name
= data
->name
;
471 genpd
->power_off
= scpsys_power_off
;
472 genpd
->power_on
= scpsys_power_on
;
473 genpd
->dev_ops
.active_wakeup
= scpsys_active_wakeup
;
476 * Initially turn on all domains to make the domains usable
477 * with !CONFIG_PM and to get the hardware in sync with the
478 * software. The unused domains will be switched off during
481 genpd
->power_on(genpd
);
483 pm_genpd_init(genpd
, NULL
, false);
487 * We are not allowed to fail here since there is no way to unregister
488 * a power domain. Once registered above we have to keep the domains
492 ret
= pm_genpd_add_subdomain(pd_data
->domains
[MT8173_POWER_DOMAIN_MFG_ASYNC
],
493 pd_data
->domains
[MT8173_POWER_DOMAIN_MFG_2D
]);
494 if (ret
&& IS_ENABLED(CONFIG_PM
))
495 dev_err(&pdev
->dev
, "Failed to add subdomain: %d\n", ret
);
497 ret
= pm_genpd_add_subdomain(pd_data
->domains
[MT8173_POWER_DOMAIN_MFG_2D
],
498 pd_data
->domains
[MT8173_POWER_DOMAIN_MFG
]);
499 if (ret
&& IS_ENABLED(CONFIG_PM
))
500 dev_err(&pdev
->dev
, "Failed to add subdomain: %d\n", ret
);
502 ret
= of_genpd_add_provider_onecell(pdev
->dev
.of_node
, pd_data
);
504 dev_err(&pdev
->dev
, "Failed to add OF provider: %d\n", ret
);
509 static const struct of_device_id of_scpsys_match_tbl
[] = {
511 .compatible
= "mediatek,mt8173-scpsys",
517 static struct platform_driver scpsys_drv
= {
519 .name
= "mtk-scpsys",
520 .owner
= THIS_MODULE
,
521 .of_match_table
= of_match_ptr(of_scpsys_match_tbl
),
525 module_platform_driver_probe(scpsys_drv
, scpsys_probe
);