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/iopoll.h>
13 #include <linux/err.h>
14 #include <linux/pm_clock.h>
15 #include <linux/pm_domain.h>
16 #include <linux/of_address.h>
17 #include <linux/of_platform.h>
18 #include <linux/clk.h>
19 #include <linux/regmap.h>
20 #include <linux/mfd/syscon.h>
21 #include <dt-bindings/power/rk3288-power.h>
22 #include <dt-bindings/power/rk3368-power.h>
23 #include <dt-bindings/power/rk3399-power.h>
25 struct rockchip_domain_info
{
34 struct rockchip_pmu_info
{
41 u32 core_pwrcnt_offset
;
42 u32 gpu_pwrcnt_offset
;
44 unsigned int core_power_transition_time
;
45 unsigned int gpu_power_transition_time
;
48 const struct rockchip_domain_info
*domain_info
;
51 #define MAX_QOS_REGS_NUM 5
52 #define QOS_PRIORITY 0x08
54 #define QOS_BANDWIDTH 0x10
55 #define QOS_SATURATION 0x14
56 #define QOS_EXTCONTROL 0x18
58 struct rockchip_pm_domain
{
59 struct generic_pm_domain genpd
;
60 const struct rockchip_domain_info
*info
;
61 struct rockchip_pmu
*pmu
;
63 struct regmap
**qos_regmap
;
64 u32
*qos_save_regs
[MAX_QOS_REGS_NUM
];
71 struct regmap
*regmap
;
72 const struct rockchip_pmu_info
*info
;
73 struct mutex mutex
; /* mutex lock for pmu */
74 struct genpd_onecell_data genpd_data
;
75 struct generic_pm_domain
*domains
[];
78 #define to_rockchip_pd(gpd) container_of(gpd, struct rockchip_pm_domain, genpd)
80 #define DOMAIN(pwr, status, req, idle, ack, wakeup) \
82 .pwr_mask = (pwr >= 0) ? BIT(pwr) : 0, \
83 .status_mask = (status >= 0) ? BIT(status) : 0, \
84 .req_mask = (req >= 0) ? BIT(req) : 0, \
85 .idle_mask = (idle >= 0) ? BIT(idle) : 0, \
86 .ack_mask = (ack >= 0) ? BIT(ack) : 0, \
87 .active_wakeup = wakeup, \
90 #define DOMAIN_RK3288(pwr, status, req, wakeup) \
91 DOMAIN(pwr, status, req, req, (req) + 16, wakeup)
93 #define DOMAIN_RK3368(pwr, status, req, wakeup) \
94 DOMAIN(pwr, status, req, (req) + 16, req, wakeup)
96 #define DOMAIN_RK3399(pwr, status, req, wakeup) \
97 DOMAIN(pwr, status, req, req, req, wakeup)
99 static bool rockchip_pmu_domain_is_idle(struct rockchip_pm_domain
*pd
)
101 struct rockchip_pmu
*pmu
= pd
->pmu
;
102 const struct rockchip_domain_info
*pd_info
= pd
->info
;
105 regmap_read(pmu
->regmap
, pmu
->info
->idle_offset
, &val
);
106 return (val
& pd_info
->idle_mask
) == pd_info
->idle_mask
;
109 static unsigned int rockchip_pmu_read_ack(struct rockchip_pmu
*pmu
)
113 regmap_read(pmu
->regmap
, pmu
->info
->ack_offset
, &val
);
117 static int rockchip_pmu_set_idle_request(struct rockchip_pm_domain
*pd
,
120 const struct rockchip_domain_info
*pd_info
= pd
->info
;
121 struct generic_pm_domain
*genpd
= &pd
->genpd
;
122 struct rockchip_pmu
*pmu
= pd
->pmu
;
123 unsigned int target_ack
;
128 if (pd_info
->req_mask
== 0)
131 regmap_update_bits(pmu
->regmap
, pmu
->info
->req_offset
,
132 pd_info
->req_mask
, idle
? -1U : 0);
136 /* Wait util idle_ack = 1 */
137 target_ack
= idle
? pd_info
->ack_mask
: 0;
138 ret
= readx_poll_timeout_atomic(rockchip_pmu_read_ack
, pmu
, val
,
139 (val
& pd_info
->ack_mask
) == target_ack
,
143 "failed to get ack on domain '%s', val=0x%x\n",
148 ret
= readx_poll_timeout_atomic(rockchip_pmu_domain_is_idle
, pd
,
149 is_idle
, is_idle
== idle
, 0, 10000);
152 "failed to set idle on domain '%s', val=%d\n",
153 genpd
->name
, is_idle
);
160 static int rockchip_pmu_save_qos(struct rockchip_pm_domain
*pd
)
164 for (i
= 0; i
< pd
->num_qos
; i
++) {
165 regmap_read(pd
->qos_regmap
[i
],
167 &pd
->qos_save_regs
[0][i
]);
168 regmap_read(pd
->qos_regmap
[i
],
170 &pd
->qos_save_regs
[1][i
]);
171 regmap_read(pd
->qos_regmap
[i
],
173 &pd
->qos_save_regs
[2][i
]);
174 regmap_read(pd
->qos_regmap
[i
],
176 &pd
->qos_save_regs
[3][i
]);
177 regmap_read(pd
->qos_regmap
[i
],
179 &pd
->qos_save_regs
[4][i
]);
184 static int rockchip_pmu_restore_qos(struct rockchip_pm_domain
*pd
)
188 for (i
= 0; i
< pd
->num_qos
; i
++) {
189 regmap_write(pd
->qos_regmap
[i
],
191 pd
->qos_save_regs
[0][i
]);
192 regmap_write(pd
->qos_regmap
[i
],
194 pd
->qos_save_regs
[1][i
]);
195 regmap_write(pd
->qos_regmap
[i
],
197 pd
->qos_save_regs
[2][i
]);
198 regmap_write(pd
->qos_regmap
[i
],
200 pd
->qos_save_regs
[3][i
]);
201 regmap_write(pd
->qos_regmap
[i
],
203 pd
->qos_save_regs
[4][i
]);
209 static bool rockchip_pmu_domain_is_on(struct rockchip_pm_domain
*pd
)
211 struct rockchip_pmu
*pmu
= pd
->pmu
;
214 /* check idle status for idle-only domains */
215 if (pd
->info
->status_mask
== 0)
216 return !rockchip_pmu_domain_is_idle(pd
);
218 regmap_read(pmu
->regmap
, pmu
->info
->status_offset
, &val
);
220 /* 1'b0: power on, 1'b1: power off */
221 return !(val
& pd
->info
->status_mask
);
224 static void rockchip_do_pmu_set_power_domain(struct rockchip_pm_domain
*pd
,
227 struct rockchip_pmu
*pmu
= pd
->pmu
;
228 struct generic_pm_domain
*genpd
= &pd
->genpd
;
231 if (pd
->info
->pwr_mask
== 0)
234 regmap_update_bits(pmu
->regmap
, pmu
->info
->pwr_offset
,
235 pd
->info
->pwr_mask
, on
? 0 : -1U);
239 if (readx_poll_timeout_atomic(rockchip_pmu_domain_is_on
, pd
, is_on
,
240 is_on
== on
, 0, 10000)) {
242 "failed to set domain '%s', val=%d\n",
248 static int rockchip_pd_power(struct rockchip_pm_domain
*pd
, bool power_on
)
252 mutex_lock(&pd
->pmu
->mutex
);
254 if (rockchip_pmu_domain_is_on(pd
) != power_on
) {
255 for (i
= 0; i
< pd
->num_clks
; i
++)
256 clk_enable(pd
->clks
[i
]);
259 rockchip_pmu_save_qos(pd
);
261 /* if powering down, idle request to NIU first */
262 rockchip_pmu_set_idle_request(pd
, true);
265 rockchip_do_pmu_set_power_domain(pd
, power_on
);
268 /* if powering up, leave idle mode */
269 rockchip_pmu_set_idle_request(pd
, false);
271 rockchip_pmu_restore_qos(pd
);
274 for (i
= pd
->num_clks
- 1; i
>= 0; i
--)
275 clk_disable(pd
->clks
[i
]);
278 mutex_unlock(&pd
->pmu
->mutex
);
282 static int rockchip_pd_power_on(struct generic_pm_domain
*domain
)
284 struct rockchip_pm_domain
*pd
= to_rockchip_pd(domain
);
286 return rockchip_pd_power(pd
, true);
289 static int rockchip_pd_power_off(struct generic_pm_domain
*domain
)
291 struct rockchip_pm_domain
*pd
= to_rockchip_pd(domain
);
293 return rockchip_pd_power(pd
, false);
296 static int rockchip_pd_attach_dev(struct generic_pm_domain
*genpd
,
303 dev_dbg(dev
, "attaching to power domain '%s'\n", genpd
->name
);
305 error
= pm_clk_create(dev
);
307 dev_err(dev
, "pm_clk_create failed %d\n", error
);
312 while ((clk
= of_clk_get(dev
->of_node
, i
++)) && !IS_ERR(clk
)) {
313 dev_dbg(dev
, "adding clock '%pC' to list of PM clocks\n", clk
);
314 error
= pm_clk_add_clk(dev
, clk
);
316 dev_err(dev
, "pm_clk_add_clk failed %d\n", error
);
326 static void rockchip_pd_detach_dev(struct generic_pm_domain
*genpd
,
329 dev_dbg(dev
, "detaching from power domain '%s'\n", genpd
->name
);
334 static bool rockchip_active_wakeup(struct device
*dev
)
336 struct generic_pm_domain
*genpd
;
337 struct rockchip_pm_domain
*pd
;
339 genpd
= pd_to_genpd(dev
->pm_domain
);
340 pd
= container_of(genpd
, struct rockchip_pm_domain
, genpd
);
342 return pd
->info
->active_wakeup
;
345 static int rockchip_pm_add_one_domain(struct rockchip_pmu
*pmu
,
346 struct device_node
*node
)
348 const struct rockchip_domain_info
*pd_info
;
349 struct rockchip_pm_domain
*pd
;
350 struct device_node
*qos_node
;
357 error
= of_property_read_u32(node
, "reg", &id
);
360 "%s: failed to retrieve domain id (reg): %d\n",
365 if (id
>= pmu
->info
->num_domains
) {
366 dev_err(pmu
->dev
, "%s: invalid domain id %d\n",
371 pd_info
= &pmu
->info
->domain_info
[id
];
373 dev_err(pmu
->dev
, "%s: undefined domain id %d\n",
378 clk_cnt
= of_count_phandle_with_args(node
, "clocks", "#clock-cells");
379 pd
= devm_kzalloc(pmu
->dev
,
380 sizeof(*pd
) + clk_cnt
* sizeof(pd
->clks
[0]),
388 for (i
= 0; i
< clk_cnt
; i
++) {
389 clk
= of_clk_get(node
, i
);
391 error
= PTR_ERR(clk
);
393 "%s: failed to get clk at index %d: %d\n",
394 node
->name
, i
, error
);
398 error
= clk_prepare(clk
);
401 "%s: failed to prepare clk %pC (index %d): %d\n",
402 node
->name
, clk
, i
, error
);
407 pd
->clks
[pd
->num_clks
++] = clk
;
409 dev_dbg(pmu
->dev
, "added clock '%pC' to domain '%s'\n",
413 pd
->num_qos
= of_count_phandle_with_args(node
, "pm_qos",
416 if (pd
->num_qos
> 0) {
417 pd
->qos_regmap
= devm_kcalloc(pmu
->dev
, pd
->num_qos
,
418 sizeof(*pd
->qos_regmap
),
420 if (!pd
->qos_regmap
) {
425 for (j
= 0; j
< MAX_QOS_REGS_NUM
; j
++) {
426 pd
->qos_save_regs
[j
] = devm_kcalloc(pmu
->dev
,
430 if (!pd
->qos_save_regs
[j
]) {
436 for (j
= 0; j
< pd
->num_qos
; j
++) {
437 qos_node
= of_parse_phandle(node
, "pm_qos", j
);
442 pd
->qos_regmap
[j
] = syscon_node_to_regmap(qos_node
);
443 if (IS_ERR(pd
->qos_regmap
[j
])) {
445 of_node_put(qos_node
);
448 of_node_put(qos_node
);
452 error
= rockchip_pd_power(pd
, true);
455 "failed to power on domain '%s': %d\n",
460 pd
->genpd
.name
= node
->name
;
461 pd
->genpd
.power_off
= rockchip_pd_power_off
;
462 pd
->genpd
.power_on
= rockchip_pd_power_on
;
463 pd
->genpd
.attach_dev
= rockchip_pd_attach_dev
;
464 pd
->genpd
.detach_dev
= rockchip_pd_detach_dev
;
465 pd
->genpd
.dev_ops
.active_wakeup
= rockchip_active_wakeup
;
466 pd
->genpd
.flags
= GENPD_FLAG_PM_CLK
;
467 pm_genpd_init(&pd
->genpd
, NULL
, false);
469 pmu
->genpd_data
.domains
[id
] = &pd
->genpd
;
474 clk_unprepare(pd
->clks
[i
]);
475 clk_put(pd
->clks
[i
]);
480 static void rockchip_pm_remove_one_domain(struct rockchip_pm_domain
*pd
)
485 * We're in the error cleanup already, so we only complain,
486 * but won't emit another error on top of the original one.
488 ret
= pm_genpd_remove(&pd
->genpd
);
490 dev_err(pd
->pmu
->dev
, "failed to remove domain '%s' : %d - state may be inconsistent\n",
491 pd
->genpd
.name
, ret
);
493 for (i
= 0; i
< pd
->num_clks
; i
++) {
494 clk_unprepare(pd
->clks
[i
]);
495 clk_put(pd
->clks
[i
]);
498 /* protect the zeroing of pm->num_clks */
499 mutex_lock(&pd
->pmu
->mutex
);
501 mutex_unlock(&pd
->pmu
->mutex
);
503 /* devm will free our memory */
506 static void rockchip_pm_domain_cleanup(struct rockchip_pmu
*pmu
)
508 struct generic_pm_domain
*genpd
;
509 struct rockchip_pm_domain
*pd
;
512 for (i
= 0; i
< pmu
->genpd_data
.num_domains
; i
++) {
513 genpd
= pmu
->genpd_data
.domains
[i
];
515 pd
= to_rockchip_pd(genpd
);
516 rockchip_pm_remove_one_domain(pd
);
520 /* devm will free our memory */
523 static void rockchip_configure_pd_cnt(struct rockchip_pmu
*pmu
,
524 u32 domain_reg_offset
,
527 /* First configure domain power down transition count ... */
528 regmap_write(pmu
->regmap
, domain_reg_offset
, count
);
529 /* ... and then power up count. */
530 regmap_write(pmu
->regmap
, domain_reg_offset
+ 4, count
);
533 static int rockchip_pm_add_subdomain(struct rockchip_pmu
*pmu
,
534 struct device_node
*parent
)
536 struct device_node
*np
;
537 struct generic_pm_domain
*child_domain
, *parent_domain
;
540 for_each_child_of_node(parent
, np
) {
543 error
= of_property_read_u32(parent
, "reg", &idx
);
546 "%s: failed to retrieve domain id (reg): %d\n",
547 parent
->name
, error
);
550 parent_domain
= pmu
->genpd_data
.domains
[idx
];
552 error
= rockchip_pm_add_one_domain(pmu
, np
);
554 dev_err(pmu
->dev
, "failed to handle node %s: %d\n",
559 error
= of_property_read_u32(np
, "reg", &idx
);
562 "%s: failed to retrieve domain id (reg): %d\n",
566 child_domain
= pmu
->genpd_data
.domains
[idx
];
568 error
= pm_genpd_add_subdomain(parent_domain
, child_domain
);
570 dev_err(pmu
->dev
, "%s failed to add subdomain %s: %d\n",
571 parent_domain
->name
, child_domain
->name
, error
);
574 dev_dbg(pmu
->dev
, "%s add subdomain: %s\n",
575 parent_domain
->name
, child_domain
->name
);
578 rockchip_pm_add_subdomain(pmu
, np
);
588 static int rockchip_pm_domain_probe(struct platform_device
*pdev
)
590 struct device
*dev
= &pdev
->dev
;
591 struct device_node
*np
= dev
->of_node
;
592 struct device_node
*node
;
593 struct device
*parent
;
594 struct rockchip_pmu
*pmu
;
595 const struct of_device_id
*match
;
596 const struct rockchip_pmu_info
*pmu_info
;
600 dev_err(dev
, "device tree node not found\n");
604 match
= of_match_device(dev
->driver
->of_match_table
, dev
);
605 if (!match
|| !match
->data
) {
606 dev_err(dev
, "missing pmu data\n");
610 pmu_info
= match
->data
;
612 pmu
= devm_kzalloc(dev
,
614 pmu_info
->num_domains
* sizeof(pmu
->domains
[0]),
619 pmu
->dev
= &pdev
->dev
;
620 mutex_init(&pmu
->mutex
);
622 pmu
->info
= pmu_info
;
624 pmu
->genpd_data
.domains
= pmu
->domains
;
625 pmu
->genpd_data
.num_domains
= pmu_info
->num_domains
;
627 parent
= dev
->parent
;
629 dev_err(dev
, "no parent for syscon devices\n");
633 pmu
->regmap
= syscon_node_to_regmap(parent
->of_node
);
634 if (IS_ERR(pmu
->regmap
)) {
635 dev_err(dev
, "no regmap available\n");
636 return PTR_ERR(pmu
->regmap
);
640 * Configure power up and down transition delays for CORE
643 if (pmu_info
->core_power_transition_time
)
644 rockchip_configure_pd_cnt(pmu
, pmu_info
->core_pwrcnt_offset
,
645 pmu_info
->core_power_transition_time
);
646 if (pmu_info
->gpu_pwrcnt_offset
)
647 rockchip_configure_pd_cnt(pmu
, pmu_info
->gpu_pwrcnt_offset
,
648 pmu_info
->gpu_power_transition_time
);
652 for_each_available_child_of_node(np
, node
) {
653 error
= rockchip_pm_add_one_domain(pmu
, node
);
655 dev_err(dev
, "failed to handle node %s: %d\n",
661 error
= rockchip_pm_add_subdomain(pmu
, node
);
663 dev_err(dev
, "failed to handle subdomain node %s: %d\n",
671 dev_dbg(dev
, "no power domains defined\n");
675 error
= of_genpd_add_provider_onecell(np
, &pmu
->genpd_data
);
677 dev_err(dev
, "failed to add provider: %d\n", error
);
684 rockchip_pm_domain_cleanup(pmu
);
688 static const struct rockchip_domain_info rk3288_pm_domains
[] = {
689 [RK3288_PD_VIO
] = DOMAIN_RK3288(7, 7, 4, false),
690 [RK3288_PD_HEVC
] = DOMAIN_RK3288(14, 10, 9, false),
691 [RK3288_PD_VIDEO
] = DOMAIN_RK3288(8, 8, 3, false),
692 [RK3288_PD_GPU
] = DOMAIN_RK3288(9, 9, 2, false),
695 static const struct rockchip_domain_info rk3368_pm_domains
[] = {
696 [RK3368_PD_PERI
] = DOMAIN_RK3368(13, 12, 6, true),
697 [RK3368_PD_VIO
] = DOMAIN_RK3368(15, 14, 8, false),
698 [RK3368_PD_VIDEO
] = DOMAIN_RK3368(14, 13, 7, false),
699 [RK3368_PD_GPU_0
] = DOMAIN_RK3368(16, 15, 2, false),
700 [RK3368_PD_GPU_1
] = DOMAIN_RK3368(17, 16, 2, false),
703 static const struct rockchip_domain_info rk3399_pm_domains
[] = {
704 [RK3399_PD_TCPD0
] = DOMAIN_RK3399(8, 8, -1, false),
705 [RK3399_PD_TCPD1
] = DOMAIN_RK3399(9, 9, -1, false),
706 [RK3399_PD_CCI
] = DOMAIN_RK3399(10, 10, -1, true),
707 [RK3399_PD_CCI0
] = DOMAIN_RK3399(-1, -1, 15, true),
708 [RK3399_PD_CCI1
] = DOMAIN_RK3399(-1, -1, 16, true),
709 [RK3399_PD_PERILP
] = DOMAIN_RK3399(11, 11, 1, true),
710 [RK3399_PD_PERIHP
] = DOMAIN_RK3399(12, 12, 2, true),
711 [RK3399_PD_CENTER
] = DOMAIN_RK3399(13, 13, 14, true),
712 [RK3399_PD_VIO
] = DOMAIN_RK3399(14, 14, 17, false),
713 [RK3399_PD_GPU
] = DOMAIN_RK3399(15, 15, 0, false),
714 [RK3399_PD_VCODEC
] = DOMAIN_RK3399(16, 16, 3, false),
715 [RK3399_PD_VDU
] = DOMAIN_RK3399(17, 17, 4, false),
716 [RK3399_PD_RGA
] = DOMAIN_RK3399(18, 18, 5, false),
717 [RK3399_PD_IEP
] = DOMAIN_RK3399(19, 19, 6, false),
718 [RK3399_PD_VO
] = DOMAIN_RK3399(20, 20, -1, false),
719 [RK3399_PD_VOPB
] = DOMAIN_RK3399(-1, -1, 7, false),
720 [RK3399_PD_VOPL
] = DOMAIN_RK3399(-1, -1, 8, false),
721 [RK3399_PD_ISP0
] = DOMAIN_RK3399(22, 22, 9, false),
722 [RK3399_PD_ISP1
] = DOMAIN_RK3399(23, 23, 10, false),
723 [RK3399_PD_HDCP
] = DOMAIN_RK3399(24, 24, 11, false),
724 [RK3399_PD_GMAC
] = DOMAIN_RK3399(25, 25, 23, true),
725 [RK3399_PD_EMMC
] = DOMAIN_RK3399(26, 26, 24, true),
726 [RK3399_PD_USB3
] = DOMAIN_RK3399(27, 27, 12, true),
727 [RK3399_PD_EDP
] = DOMAIN_RK3399(28, 28, 22, false),
728 [RK3399_PD_GIC
] = DOMAIN_RK3399(29, 29, 27, true),
729 [RK3399_PD_SD
] = DOMAIN_RK3399(30, 30, 28, true),
730 [RK3399_PD_SDIOAUDIO
] = DOMAIN_RK3399(31, 31, 29, true),
733 static const struct rockchip_pmu_info rk3288_pmu
= {
735 .status_offset
= 0x0c,
740 .core_pwrcnt_offset
= 0x34,
741 .gpu_pwrcnt_offset
= 0x3c,
743 .core_power_transition_time
= 24, /* 1us */
744 .gpu_power_transition_time
= 24, /* 1us */
746 .num_domains
= ARRAY_SIZE(rk3288_pm_domains
),
747 .domain_info
= rk3288_pm_domains
,
750 static const struct rockchip_pmu_info rk3368_pmu
= {
752 .status_offset
= 0x10,
757 .core_pwrcnt_offset
= 0x48,
758 .gpu_pwrcnt_offset
= 0x50,
760 .core_power_transition_time
= 24,
761 .gpu_power_transition_time
= 24,
763 .num_domains
= ARRAY_SIZE(rk3368_pm_domains
),
764 .domain_info
= rk3368_pm_domains
,
767 static const struct rockchip_pmu_info rk3399_pmu
= {
769 .status_offset
= 0x18,
774 /* ARM Trusted Firmware manages power transition times */
776 .num_domains
= ARRAY_SIZE(rk3399_pm_domains
),
777 .domain_info
= rk3399_pm_domains
,
780 static const struct of_device_id rockchip_pm_domain_dt_match
[] = {
782 .compatible
= "rockchip,rk3288-power-controller",
783 .data
= (void *)&rk3288_pmu
,
786 .compatible
= "rockchip,rk3368-power-controller",
787 .data
= (void *)&rk3368_pmu
,
790 .compatible
= "rockchip,rk3399-power-controller",
791 .data
= (void *)&rk3399_pmu
,
796 static struct platform_driver rockchip_pm_domain_driver
= {
797 .probe
= rockchip_pm_domain_probe
,
799 .name
= "rockchip-pm-domain",
800 .of_match_table
= rockchip_pm_domain_dt_match
,
802 * We can't forcibly eject devices form power domain,
803 * so we can't really remove power domains once they
806 .suppress_bind_attrs
= true,
810 static int __init
rockchip_pm_domain_drv_register(void)
812 return platform_driver_register(&rockchip_pm_domain_driver
);
814 postcore_initcall(rockchip_pm_domain_drv_register
);