2 * Copyright (c) 2017 BayLibre, SAS
3 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 * SPDX-License-Identifier: GPL-2.0+
8 #include <linux/of_address.h>
9 #include <linux/platform_device.h>
10 #include <linux/pm_domain.h>
11 #include <linux/bitfield.h>
12 #include <linux/regmap.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/of_device.h>
15 #include <linux/reset.h>
16 #include <linux/clk.h>
20 #define AO_RTI_GEN_PWR_SLEEP0 (0x3a << 2)
22 #define GEN_PWR_VPU_HDMI BIT(8)
23 #define GEN_PWR_VPU_HDMI_ISO BIT(9)
27 #define HHI_MEM_PD_REG0 (0x40 << 2)
28 #define HHI_VPU_MEM_PD_REG0 (0x41 << 2)
29 #define HHI_VPU_MEM_PD_REG1 (0x42 << 2)
30 #define HHI_VPU_MEM_PD_REG2 (0x4d << 2)
32 struct meson_gx_pwrc_vpu {
33 struct generic_pm_domain genpd;
34 struct regmap *regmap_ao;
35 struct regmap *regmap_hhi;
36 struct reset_control *rstc;
42 struct meson_gx_pwrc_vpu *genpd_to_pd(struct generic_pm_domain *d)
44 return container_of(d, struct meson_gx_pwrc_vpu, genpd);
47 static int meson_gx_pwrc_vpu_power_off(struct generic_pm_domain *genpd)
49 struct meson_gx_pwrc_vpu *pd = genpd_to_pd(genpd);
52 regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
53 GEN_PWR_VPU_HDMI_ISO, GEN_PWR_VPU_HDMI_ISO);
56 /* Power Down Memories */
57 for (i = 0; i < 32; i += 2) {
58 regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG0,
62 for (i = 0; i < 32; i += 2) {
63 regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG1,
67 for (i = 8; i < 16; i++) {
68 regmap_update_bits(pd->regmap_hhi, HHI_MEM_PD_REG0,
74 regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
75 GEN_PWR_VPU_HDMI, GEN_PWR_VPU_HDMI);
79 clk_disable_unprepare(pd->vpu_clk);
80 clk_disable_unprepare(pd->vapb_clk);
85 static int meson_g12a_pwrc_vpu_power_off(struct generic_pm_domain *genpd)
87 struct meson_gx_pwrc_vpu *pd = genpd_to_pd(genpd);
90 regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
91 GEN_PWR_VPU_HDMI_ISO, GEN_PWR_VPU_HDMI_ISO);
94 /* Power Down Memories */
95 for (i = 0; i < 32; i += 2) {
96 regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG0,
100 for (i = 0; i < 32; i += 2) {
101 regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG1,
105 for (i = 0; i < 32; i += 2) {
106 regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG2,
110 for (i = 8; i < 16; i++) {
111 regmap_update_bits(pd->regmap_hhi, HHI_MEM_PD_REG0,
117 regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
118 GEN_PWR_VPU_HDMI, GEN_PWR_VPU_HDMI);
122 clk_disable_unprepare(pd->vpu_clk);
123 clk_disable_unprepare(pd->vapb_clk);
128 static int meson_gx_pwrc_vpu_setup_clk(struct meson_gx_pwrc_vpu *pd)
132 ret = clk_prepare_enable(pd->vpu_clk);
136 ret = clk_prepare_enable(pd->vapb_clk);
138 clk_disable_unprepare(pd->vpu_clk);
143 static int meson_gx_pwrc_vpu_power_on(struct generic_pm_domain *genpd)
145 struct meson_gx_pwrc_vpu *pd = genpd_to_pd(genpd);
149 regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
150 GEN_PWR_VPU_HDMI, 0);
153 /* Power Up Memories */
154 for (i = 0; i < 32; i += 2) {
155 regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG0,
160 for (i = 0; i < 32; i += 2) {
161 regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG1,
166 for (i = 8; i < 16; i++) {
167 regmap_update_bits(pd->regmap_hhi, HHI_MEM_PD_REG0,
173 ret = reset_control_assert(pd->rstc);
177 regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
178 GEN_PWR_VPU_HDMI_ISO, 0);
180 ret = reset_control_deassert(pd->rstc);
184 ret = meson_gx_pwrc_vpu_setup_clk(pd);
191 static int meson_g12a_pwrc_vpu_power_on(struct generic_pm_domain *genpd)
193 struct meson_gx_pwrc_vpu *pd = genpd_to_pd(genpd);
197 regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
198 GEN_PWR_VPU_HDMI, 0);
201 /* Power Up Memories */
202 for (i = 0; i < 32; i += 2) {
203 regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG0,
208 for (i = 0; i < 32; i += 2) {
209 regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG1,
214 for (i = 0; i < 32; i += 2) {
215 regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG2,
220 for (i = 8; i < 16; i++) {
221 regmap_update_bits(pd->regmap_hhi, HHI_MEM_PD_REG0,
227 ret = reset_control_assert(pd->rstc);
231 regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
232 GEN_PWR_VPU_HDMI_ISO, 0);
234 ret = reset_control_deassert(pd->rstc);
238 ret = meson_gx_pwrc_vpu_setup_clk(pd);
245 static bool meson_gx_pwrc_vpu_get_power(struct meson_gx_pwrc_vpu *pd)
249 regmap_read(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0, ®);
251 return (reg & GEN_PWR_VPU_HDMI);
254 static struct meson_gx_pwrc_vpu vpu_hdmi_pd = {
257 .power_off = meson_gx_pwrc_vpu_power_off,
258 .power_on = meson_gx_pwrc_vpu_power_on,
262 static struct meson_gx_pwrc_vpu vpu_hdmi_pd_g12a = {
265 .power_off = meson_g12a_pwrc_vpu_power_off,
266 .power_on = meson_g12a_pwrc_vpu_power_on,
270 static int meson_gx_pwrc_vpu_probe(struct platform_device *pdev)
272 const struct meson_gx_pwrc_vpu *vpu_pd_match;
273 struct regmap *regmap_ao, *regmap_hhi;
274 struct meson_gx_pwrc_vpu *vpu_pd;
275 struct reset_control *rstc;
277 struct clk *vapb_clk;
281 vpu_pd_match = of_device_get_match_data(&pdev->dev);
283 dev_err(&pdev->dev, "failed to get match data\n");
287 vpu_pd = devm_kzalloc(&pdev->dev, sizeof(*vpu_pd), GFP_KERNEL);
291 memcpy(vpu_pd, vpu_pd_match, sizeof(*vpu_pd));
293 regmap_ao = syscon_node_to_regmap(of_get_parent(pdev->dev.of_node));
294 if (IS_ERR(regmap_ao)) {
295 dev_err(&pdev->dev, "failed to get regmap\n");
296 return PTR_ERR(regmap_ao);
299 regmap_hhi = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
300 "amlogic,hhi-sysctrl");
301 if (IS_ERR(regmap_hhi)) {
302 dev_err(&pdev->dev, "failed to get HHI regmap\n");
303 return PTR_ERR(regmap_hhi);
306 rstc = devm_reset_control_array_get(&pdev->dev, false, false);
308 if (PTR_ERR(rstc) != -EPROBE_DEFER)
309 dev_err(&pdev->dev, "failed to get reset lines\n");
310 return PTR_ERR(rstc);
313 vpu_clk = devm_clk_get(&pdev->dev, "vpu");
314 if (IS_ERR(vpu_clk)) {
315 dev_err(&pdev->dev, "vpu clock request failed\n");
316 return PTR_ERR(vpu_clk);
319 vapb_clk = devm_clk_get(&pdev->dev, "vapb");
320 if (IS_ERR(vapb_clk)) {
321 dev_err(&pdev->dev, "vapb clock request failed\n");
322 return PTR_ERR(vapb_clk);
325 vpu_pd->regmap_ao = regmap_ao;
326 vpu_pd->regmap_hhi = regmap_hhi;
328 vpu_pd->vpu_clk = vpu_clk;
329 vpu_pd->vapb_clk = vapb_clk;
331 platform_set_drvdata(pdev, vpu_pd);
333 powered_off = meson_gx_pwrc_vpu_get_power(vpu_pd);
335 /* If already powered, sync the clock states */
337 ret = meson_gx_pwrc_vpu_setup_clk(vpu_pd);
342 vpu_pd->genpd.flags = GENPD_FLAG_ALWAYS_ON;
343 pm_genpd_init(&vpu_pd->genpd, NULL, powered_off);
345 return of_genpd_add_provider_simple(pdev->dev.of_node,
349 static void meson_gx_pwrc_vpu_shutdown(struct platform_device *pdev)
351 struct meson_gx_pwrc_vpu *vpu_pd = platform_get_drvdata(pdev);
354 powered_off = meson_gx_pwrc_vpu_get_power(vpu_pd);
356 vpu_pd->genpd.power_off(&vpu_pd->genpd);
359 static const struct of_device_id meson_gx_pwrc_vpu_match_table[] = {
360 { .compatible = "amlogic,meson-gx-pwrc-vpu", .data = &vpu_hdmi_pd },
362 .compatible = "amlogic,meson-g12a-pwrc-vpu",
363 .data = &vpu_hdmi_pd_g12a
368 static struct platform_driver meson_gx_pwrc_vpu_driver = {
369 .probe = meson_gx_pwrc_vpu_probe,
370 .shutdown = meson_gx_pwrc_vpu_shutdown,
372 .name = "meson_gx_pwrc_vpu",
373 .of_match_table = meson_gx_pwrc_vpu_match_table,
376 builtin_platform_driver(meson_gx_pwrc_vpu_driver);