2 * Rockchip Generic power domain support.
4 * Copyright (c) 2015 ROCKCHIP, Co. Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/err.h>
13 #include <linux/pm_clock.h>
14 #include <linux/pm_domain.h>
15 #include <linux/of_address.h>
16 #include <linux/of_platform.h>
17 #include <linux/clk.h>
18 #include <linux/regmap.h>
19 #include <linux/mfd/syscon.h>
20 #include <dt-bindings/power/rk3288-power.h>
21 #include <dt-bindings/power/rk3368-power.h>
22 #include <dt-bindings/power/rk3399-power.h>
24 struct rockchip_domain_info
{
33 struct rockchip_pmu_info
{
40 u32 core_pwrcnt_offset
;
41 u32 gpu_pwrcnt_offset
;
43 unsigned int core_power_transition_time
;
44 unsigned int gpu_power_transition_time
;
47 const struct rockchip_domain_info
*domain_info
;
50 #define MAX_QOS_REGS_NUM 5
51 #define QOS_PRIORITY 0x08
53 #define QOS_BANDWIDTH 0x10
54 #define QOS_SATURATION 0x14
55 #define QOS_EXTCONTROL 0x18
57 struct rockchip_pm_domain
{
58 struct generic_pm_domain genpd
;
59 const struct rockchip_domain_info
*info
;
60 struct rockchip_pmu
*pmu
;
62 struct regmap
**qos_regmap
;
63 u32
*qos_save_regs
[MAX_QOS_REGS_NUM
];
70 struct regmap
*regmap
;
71 const struct rockchip_pmu_info
*info
;
72 struct mutex mutex
; /* mutex lock for pmu */
73 struct genpd_onecell_data genpd_data
;
74 struct generic_pm_domain
*domains
[];
77 #define to_rockchip_pd(gpd) container_of(gpd, struct rockchip_pm_domain, genpd)
79 #define DOMAIN(pwr, status, req, idle, ack, wakeup) \
81 .pwr_mask = (pwr >= 0) ? BIT(pwr) : 0, \
82 .status_mask = (status >= 0) ? BIT(status) : 0, \
83 .req_mask = (req >= 0) ? BIT(req) : 0, \
84 .idle_mask = (idle >= 0) ? BIT(idle) : 0, \
85 .ack_mask = (ack >= 0) ? BIT(ack) : 0, \
86 .active_wakeup = wakeup, \
89 #define DOMAIN_RK3288(pwr, status, req, wakeup) \
90 DOMAIN(pwr, status, req, req, (req) + 16, wakeup)
92 #define DOMAIN_RK3368(pwr, status, req, wakeup) \
93 DOMAIN(pwr, status, req, (req) + 16, req, wakeup)
95 #define DOMAIN_RK3399(pwr, status, req, wakeup) \
96 DOMAIN(pwr, status, req, req, req, wakeup)
98 static bool rockchip_pmu_domain_is_idle(struct rockchip_pm_domain
*pd
)
100 struct rockchip_pmu
*pmu
= pd
->pmu
;
101 const struct rockchip_domain_info
*pd_info
= pd
->info
;
104 regmap_read(pmu
->regmap
, pmu
->info
->idle_offset
, &val
);
105 return (val
& pd_info
->idle_mask
) == pd_info
->idle_mask
;
108 static int rockchip_pmu_set_idle_request(struct rockchip_pm_domain
*pd
,
111 const struct rockchip_domain_info
*pd_info
= pd
->info
;
112 struct rockchip_pmu
*pmu
= pd
->pmu
;
115 if (pd_info
->req_mask
== 0)
118 regmap_update_bits(pmu
->regmap
, pmu
->info
->req_offset
,
119 pd_info
->req_mask
, idle
? -1U : 0);
124 regmap_read(pmu
->regmap
, pmu
->info
->ack_offset
, &val
);
125 } while ((val
& pd_info
->ack_mask
) != (idle
? pd_info
->ack_mask
: 0));
127 while (rockchip_pmu_domain_is_idle(pd
) != idle
)
133 static int rockchip_pmu_save_qos(struct rockchip_pm_domain
*pd
)
137 for (i
= 0; i
< pd
->num_qos
; i
++) {
138 regmap_read(pd
->qos_regmap
[i
],
140 &pd
->qos_save_regs
[0][i
]);
141 regmap_read(pd
->qos_regmap
[i
],
143 &pd
->qos_save_regs
[1][i
]);
144 regmap_read(pd
->qos_regmap
[i
],
146 &pd
->qos_save_regs
[2][i
]);
147 regmap_read(pd
->qos_regmap
[i
],
149 &pd
->qos_save_regs
[3][i
]);
150 regmap_read(pd
->qos_regmap
[i
],
152 &pd
->qos_save_regs
[4][i
]);
157 static int rockchip_pmu_restore_qos(struct rockchip_pm_domain
*pd
)
161 for (i
= 0; i
< pd
->num_qos
; i
++) {
162 regmap_write(pd
->qos_regmap
[i
],
164 pd
->qos_save_regs
[0][i
]);
165 regmap_write(pd
->qos_regmap
[i
],
167 pd
->qos_save_regs
[1][i
]);
168 regmap_write(pd
->qos_regmap
[i
],
170 pd
->qos_save_regs
[2][i
]);
171 regmap_write(pd
->qos_regmap
[i
],
173 pd
->qos_save_regs
[3][i
]);
174 regmap_write(pd
->qos_regmap
[i
],
176 pd
->qos_save_regs
[4][i
]);
182 static bool rockchip_pmu_domain_is_on(struct rockchip_pm_domain
*pd
)
184 struct rockchip_pmu
*pmu
= pd
->pmu
;
187 /* check idle status for idle-only domains */
188 if (pd
->info
->status_mask
== 0)
189 return !rockchip_pmu_domain_is_idle(pd
);
191 regmap_read(pmu
->regmap
, pmu
->info
->status_offset
, &val
);
193 /* 1'b0: power on, 1'b1: power off */
194 return !(val
& pd
->info
->status_mask
);
197 static void rockchip_do_pmu_set_power_domain(struct rockchip_pm_domain
*pd
,
200 struct rockchip_pmu
*pmu
= pd
->pmu
;
202 if (pd
->info
->pwr_mask
== 0)
205 regmap_update_bits(pmu
->regmap
, pmu
->info
->pwr_offset
,
206 pd
->info
->pwr_mask
, on
? 0 : -1U);
210 while (rockchip_pmu_domain_is_on(pd
) != on
)
214 static int rockchip_pd_power(struct rockchip_pm_domain
*pd
, bool power_on
)
218 mutex_lock(&pd
->pmu
->mutex
);
220 if (rockchip_pmu_domain_is_on(pd
) != power_on
) {
221 for (i
= 0; i
< pd
->num_clks
; i
++)
222 clk_enable(pd
->clks
[i
]);
225 rockchip_pmu_save_qos(pd
);
227 /* if powering down, idle request to NIU first */
228 rockchip_pmu_set_idle_request(pd
, true);
231 rockchip_do_pmu_set_power_domain(pd
, power_on
);
234 /* if powering up, leave idle mode */
235 rockchip_pmu_set_idle_request(pd
, false);
237 rockchip_pmu_restore_qos(pd
);
240 for (i
= pd
->num_clks
- 1; i
>= 0; i
--)
241 clk_disable(pd
->clks
[i
]);
244 mutex_unlock(&pd
->pmu
->mutex
);
248 static int rockchip_pd_power_on(struct generic_pm_domain
*domain
)
250 struct rockchip_pm_domain
*pd
= to_rockchip_pd(domain
);
252 return rockchip_pd_power(pd
, true);
255 static int rockchip_pd_power_off(struct generic_pm_domain
*domain
)
257 struct rockchip_pm_domain
*pd
= to_rockchip_pd(domain
);
259 return rockchip_pd_power(pd
, false);
262 static int rockchip_pd_attach_dev(struct generic_pm_domain
*genpd
,
269 dev_dbg(dev
, "attaching to power domain '%s'\n", genpd
->name
);
271 error
= pm_clk_create(dev
);
273 dev_err(dev
, "pm_clk_create failed %d\n", error
);
278 while ((clk
= of_clk_get(dev
->of_node
, i
++)) && !IS_ERR(clk
)) {
279 dev_dbg(dev
, "adding clock '%pC' to list of PM clocks\n", clk
);
280 error
= pm_clk_add_clk(dev
, clk
);
282 dev_err(dev
, "pm_clk_add_clk failed %d\n", error
);
292 static void rockchip_pd_detach_dev(struct generic_pm_domain
*genpd
,
295 dev_dbg(dev
, "detaching from power domain '%s'\n", genpd
->name
);
300 static bool rockchip_active_wakeup(struct device
*dev
)
302 struct generic_pm_domain
*genpd
;
303 struct rockchip_pm_domain
*pd
;
305 genpd
= pd_to_genpd(dev
->pm_domain
);
306 pd
= container_of(genpd
, struct rockchip_pm_domain
, genpd
);
308 return pd
->info
->active_wakeup
;
311 static int rockchip_pm_add_one_domain(struct rockchip_pmu
*pmu
,
312 struct device_node
*node
)
314 const struct rockchip_domain_info
*pd_info
;
315 struct rockchip_pm_domain
*pd
;
316 struct device_node
*qos_node
;
323 error
= of_property_read_u32(node
, "reg", &id
);
326 "%s: failed to retrieve domain id (reg): %d\n",
331 if (id
>= pmu
->info
->num_domains
) {
332 dev_err(pmu
->dev
, "%s: invalid domain id %d\n",
337 pd_info
= &pmu
->info
->domain_info
[id
];
339 dev_err(pmu
->dev
, "%s: undefined domain id %d\n",
344 clk_cnt
= of_count_phandle_with_args(node
, "clocks", "#clock-cells");
345 pd
= devm_kzalloc(pmu
->dev
,
346 sizeof(*pd
) + clk_cnt
* sizeof(pd
->clks
[0]),
354 for (i
= 0; i
< clk_cnt
; i
++) {
355 clk
= of_clk_get(node
, i
);
357 error
= PTR_ERR(clk
);
359 "%s: failed to get clk at index %d: %d\n",
360 node
->name
, i
, error
);
364 error
= clk_prepare(clk
);
367 "%s: failed to prepare clk %pC (index %d): %d\n",
368 node
->name
, clk
, i
, error
);
373 pd
->clks
[pd
->num_clks
++] = clk
;
375 dev_dbg(pmu
->dev
, "added clock '%pC' to domain '%s'\n",
379 pd
->num_qos
= of_count_phandle_with_args(node
, "pm_qos",
382 if (pd
->num_qos
> 0) {
383 pd
->qos_regmap
= devm_kcalloc(pmu
->dev
, pd
->num_qos
,
384 sizeof(*pd
->qos_regmap
),
386 if (!pd
->qos_regmap
) {
391 for (j
= 0; j
< MAX_QOS_REGS_NUM
; j
++) {
392 pd
->qos_save_regs
[j
] = devm_kcalloc(pmu
->dev
,
396 if (!pd
->qos_save_regs
[j
]) {
402 for (j
= 0; j
< pd
->num_qos
; j
++) {
403 qos_node
= of_parse_phandle(node
, "pm_qos", j
);
408 pd
->qos_regmap
[j
] = syscon_node_to_regmap(qos_node
);
409 if (IS_ERR(pd
->qos_regmap
[j
])) {
411 of_node_put(qos_node
);
414 of_node_put(qos_node
);
418 error
= rockchip_pd_power(pd
, true);
421 "failed to power on domain '%s': %d\n",
426 pd
->genpd
.name
= node
->name
;
427 pd
->genpd
.power_off
= rockchip_pd_power_off
;
428 pd
->genpd
.power_on
= rockchip_pd_power_on
;
429 pd
->genpd
.attach_dev
= rockchip_pd_attach_dev
;
430 pd
->genpd
.detach_dev
= rockchip_pd_detach_dev
;
431 pd
->genpd
.dev_ops
.active_wakeup
= rockchip_active_wakeup
;
432 pd
->genpd
.flags
= GENPD_FLAG_PM_CLK
;
433 pm_genpd_init(&pd
->genpd
, NULL
, false);
435 pmu
->genpd_data
.domains
[id
] = &pd
->genpd
;
440 clk_unprepare(pd
->clks
[i
]);
441 clk_put(pd
->clks
[i
]);
446 static void rockchip_pm_remove_one_domain(struct rockchip_pm_domain
*pd
)
450 for (i
= 0; i
< pd
->num_clks
; i
++) {
451 clk_unprepare(pd
->clks
[i
]);
452 clk_put(pd
->clks
[i
]);
455 /* protect the zeroing of pm->num_clks */
456 mutex_lock(&pd
->pmu
->mutex
);
458 mutex_unlock(&pd
->pmu
->mutex
);
460 /* devm will free our memory */
463 static void rockchip_pm_domain_cleanup(struct rockchip_pmu
*pmu
)
465 struct generic_pm_domain
*genpd
;
466 struct rockchip_pm_domain
*pd
;
469 for (i
= 0; i
< pmu
->genpd_data
.num_domains
; i
++) {
470 genpd
= pmu
->genpd_data
.domains
[i
];
472 pd
= to_rockchip_pd(genpd
);
473 rockchip_pm_remove_one_domain(pd
);
477 /* devm will free our memory */
480 static void rockchip_configure_pd_cnt(struct rockchip_pmu
*pmu
,
481 u32 domain_reg_offset
,
484 /* First configure domain power down transition count ... */
485 regmap_write(pmu
->regmap
, domain_reg_offset
, count
);
486 /* ... and then power up count. */
487 regmap_write(pmu
->regmap
, domain_reg_offset
+ 4, count
);
490 static int rockchip_pm_add_subdomain(struct rockchip_pmu
*pmu
,
491 struct device_node
*parent
)
493 struct device_node
*np
;
494 struct generic_pm_domain
*child_domain
, *parent_domain
;
497 for_each_child_of_node(parent
, np
) {
500 error
= of_property_read_u32(parent
, "reg", &idx
);
503 "%s: failed to retrieve domain id (reg): %d\n",
504 parent
->name
, error
);
507 parent_domain
= pmu
->genpd_data
.domains
[idx
];
509 error
= rockchip_pm_add_one_domain(pmu
, np
);
511 dev_err(pmu
->dev
, "failed to handle node %s: %d\n",
516 error
= of_property_read_u32(np
, "reg", &idx
);
519 "%s: failed to retrieve domain id (reg): %d\n",
523 child_domain
= pmu
->genpd_data
.domains
[idx
];
525 error
= pm_genpd_add_subdomain(parent_domain
, child_domain
);
527 dev_err(pmu
->dev
, "%s failed to add subdomain %s: %d\n",
528 parent_domain
->name
, child_domain
->name
, error
);
531 dev_dbg(pmu
->dev
, "%s add subdomain: %s\n",
532 parent_domain
->name
, child_domain
->name
);
535 rockchip_pm_add_subdomain(pmu
, np
);
545 static int rockchip_pm_domain_probe(struct platform_device
*pdev
)
547 struct device
*dev
= &pdev
->dev
;
548 struct device_node
*np
= dev
->of_node
;
549 struct device_node
*node
;
550 struct device
*parent
;
551 struct rockchip_pmu
*pmu
;
552 const struct of_device_id
*match
;
553 const struct rockchip_pmu_info
*pmu_info
;
557 dev_err(dev
, "device tree node not found\n");
561 match
= of_match_device(dev
->driver
->of_match_table
, dev
);
562 if (!match
|| !match
->data
) {
563 dev_err(dev
, "missing pmu data\n");
567 pmu_info
= match
->data
;
569 pmu
= devm_kzalloc(dev
,
571 pmu_info
->num_domains
* sizeof(pmu
->domains
[0]),
576 pmu
->dev
= &pdev
->dev
;
577 mutex_init(&pmu
->mutex
);
579 pmu
->info
= pmu_info
;
581 pmu
->genpd_data
.domains
= pmu
->domains
;
582 pmu
->genpd_data
.num_domains
= pmu_info
->num_domains
;
584 parent
= dev
->parent
;
586 dev_err(dev
, "no parent for syscon devices\n");
590 pmu
->regmap
= syscon_node_to_regmap(parent
->of_node
);
591 if (IS_ERR(pmu
->regmap
)) {
592 dev_err(dev
, "no regmap available\n");
593 return PTR_ERR(pmu
->regmap
);
597 * Configure power up and down transition delays for CORE
600 rockchip_configure_pd_cnt(pmu
, pmu_info
->core_pwrcnt_offset
,
601 pmu_info
->core_power_transition_time
);
602 rockchip_configure_pd_cnt(pmu
, pmu_info
->gpu_pwrcnt_offset
,
603 pmu_info
->gpu_power_transition_time
);
607 for_each_available_child_of_node(np
, node
) {
608 error
= rockchip_pm_add_one_domain(pmu
, node
);
610 dev_err(dev
, "failed to handle node %s: %d\n",
616 error
= rockchip_pm_add_subdomain(pmu
, node
);
618 dev_err(dev
, "failed to handle subdomain node %s: %d\n",
626 dev_dbg(dev
, "no power domains defined\n");
630 of_genpd_add_provider_onecell(np
, &pmu
->genpd_data
);
635 rockchip_pm_domain_cleanup(pmu
);
639 static const struct rockchip_domain_info rk3288_pm_domains
[] = {
640 [RK3288_PD_VIO
] = DOMAIN_RK3288(7, 7, 4, false),
641 [RK3288_PD_HEVC
] = DOMAIN_RK3288(14, 10, 9, false),
642 [RK3288_PD_VIDEO
] = DOMAIN_RK3288(8, 8, 3, false),
643 [RK3288_PD_GPU
] = DOMAIN_RK3288(9, 9, 2, false),
646 static const struct rockchip_domain_info rk3368_pm_domains
[] = {
647 [RK3368_PD_PERI
] = DOMAIN_RK3368(13, 12, 6, true),
648 [RK3368_PD_VIO
] = DOMAIN_RK3368(15, 14, 8, false),
649 [RK3368_PD_VIDEO
] = DOMAIN_RK3368(14, 13, 7, false),
650 [RK3368_PD_GPU_0
] = DOMAIN_RK3368(16, 15, 2, false),
651 [RK3368_PD_GPU_1
] = DOMAIN_RK3368(17, 16, 2, false),
654 static const struct rockchip_domain_info rk3399_pm_domains
[] = {
655 [RK3399_PD_TCPD0
] = DOMAIN_RK3399(8, 8, -1, false),
656 [RK3399_PD_TCPD1
] = DOMAIN_RK3399(9, 9, -1, false),
657 [RK3399_PD_CCI
] = DOMAIN_RK3399(10, 10, -1, true),
658 [RK3399_PD_CCI0
] = DOMAIN_RK3399(-1, -1, 15, true),
659 [RK3399_PD_CCI1
] = DOMAIN_RK3399(-1, -1, 16, true),
660 [RK3399_PD_PERILP
] = DOMAIN_RK3399(11, 11, 1, true),
661 [RK3399_PD_PERIHP
] = DOMAIN_RK3399(12, 12, 2, true),
662 [RK3399_PD_CENTER
] = DOMAIN_RK3399(13, 13, 14, true),
663 [RK3399_PD_VIO
] = DOMAIN_RK3399(14, 14, 17, false),
664 [RK3399_PD_GPU
] = DOMAIN_RK3399(15, 15, 0, false),
665 [RK3399_PD_VCODEC
] = DOMAIN_RK3399(16, 16, 3, false),
666 [RK3399_PD_VDU
] = DOMAIN_RK3399(17, 17, 4, false),
667 [RK3399_PD_RGA
] = DOMAIN_RK3399(18, 18, 5, false),
668 [RK3399_PD_IEP
] = DOMAIN_RK3399(19, 19, 6, false),
669 [RK3399_PD_VO
] = DOMAIN_RK3399(20, 20, -1, false),
670 [RK3399_PD_VOPB
] = DOMAIN_RK3399(-1, -1, 7, false),
671 [RK3399_PD_VOPL
] = DOMAIN_RK3399(-1, -1, 8, false),
672 [RK3399_PD_ISP0
] = DOMAIN_RK3399(22, 22, 9, false),
673 [RK3399_PD_ISP1
] = DOMAIN_RK3399(23, 23, 10, false),
674 [RK3399_PD_HDCP
] = DOMAIN_RK3399(24, 24, 11, false),
675 [RK3399_PD_GMAC
] = DOMAIN_RK3399(25, 25, 23, true),
676 [RK3399_PD_EMMC
] = DOMAIN_RK3399(26, 26, 24, true),
677 [RK3399_PD_USB3
] = DOMAIN_RK3399(27, 27, 12, true),
678 [RK3399_PD_EDP
] = DOMAIN_RK3399(28, 28, 22, false),
679 [RK3399_PD_GIC
] = DOMAIN_RK3399(29, 29, 27, true),
680 [RK3399_PD_SD
] = DOMAIN_RK3399(30, 30, 28, true),
681 [RK3399_PD_SDIOAUDIO
] = DOMAIN_RK3399(31, 31, 29, true),
684 static const struct rockchip_pmu_info rk3288_pmu
= {
686 .status_offset
= 0x0c,
691 .core_pwrcnt_offset
= 0x34,
692 .gpu_pwrcnt_offset
= 0x3c,
694 .core_power_transition_time
= 24, /* 1us */
695 .gpu_power_transition_time
= 24, /* 1us */
697 .num_domains
= ARRAY_SIZE(rk3288_pm_domains
),
698 .domain_info
= rk3288_pm_domains
,
701 static const struct rockchip_pmu_info rk3368_pmu
= {
703 .status_offset
= 0x10,
708 .core_pwrcnt_offset
= 0x48,
709 .gpu_pwrcnt_offset
= 0x50,
711 .core_power_transition_time
= 24,
712 .gpu_power_transition_time
= 24,
714 .num_domains
= ARRAY_SIZE(rk3368_pm_domains
),
715 .domain_info
= rk3368_pm_domains
,
718 static const struct rockchip_pmu_info rk3399_pmu
= {
720 .status_offset
= 0x18,
725 .core_pwrcnt_offset
= 0x9c,
726 .gpu_pwrcnt_offset
= 0xa4,
728 .core_power_transition_time
= 24,
729 .gpu_power_transition_time
= 24,
731 .num_domains
= ARRAY_SIZE(rk3399_pm_domains
),
732 .domain_info
= rk3399_pm_domains
,
735 static const struct of_device_id rockchip_pm_domain_dt_match
[] = {
737 .compatible
= "rockchip,rk3288-power-controller",
738 .data
= (void *)&rk3288_pmu
,
741 .compatible
= "rockchip,rk3368-power-controller",
742 .data
= (void *)&rk3368_pmu
,
745 .compatible
= "rockchip,rk3399-power-controller",
746 .data
= (void *)&rk3399_pmu
,
751 static struct platform_driver rockchip_pm_domain_driver
= {
752 .probe
= rockchip_pm_domain_probe
,
754 .name
= "rockchip-pm-domain",
755 .of_match_table
= rockchip_pm_domain_dt_match
,
757 * We can't forcibly eject devices form power domain,
758 * so we can't really remove power domains once they
761 .suppress_bind_attrs
= true,
765 static int __init
rockchip_pm_domain_drv_register(void)
767 return platform_driver_register(&rockchip_pm_domain_driver
);
769 postcore_initcall(rockchip_pm_domain_drv_register
);