1e2ad626fSUlf Hansson // SPDX-License-Identifier: GPL-2.0
2e2ad626fSUlf Hansson /*
3e2ad626fSUlf Hansson * Copyright 2022 NXP
4e2ad626fSUlf Hansson */
5e2ad626fSUlf Hansson
6e2ad626fSUlf Hansson #include <linux/clk.h>
7e2ad626fSUlf Hansson #include <linux/delay.h>
8e2ad626fSUlf Hansson #include <linux/iopoll.h>
9e2ad626fSUlf Hansson #include <linux/mod_devicetable.h>
10e2ad626fSUlf Hansson #include <linux/module.h>
11e2ad626fSUlf Hansson #include <linux/platform_device.h>
12e2ad626fSUlf Hansson #include <linux/pm_domain.h>
13e2ad626fSUlf Hansson
14e2ad626fSUlf Hansson #define MIX_SLICE_SW_CTRL_OFF 0x20
15e2ad626fSUlf Hansson #define SLICE_SW_CTRL_PSW_CTRL_OFF_MASK BIT(4)
16e2ad626fSUlf Hansson #define SLICE_SW_CTRL_PDN_SOFT_MASK BIT(31)
17e2ad626fSUlf Hansson
18e2ad626fSUlf Hansson #define MIX_FUNC_STAT_OFF 0xB4
19e2ad626fSUlf Hansson
20e2ad626fSUlf Hansson #define FUNC_STAT_PSW_STAT_MASK BIT(0)
21e2ad626fSUlf Hansson #define FUNC_STAT_RST_STAT_MASK BIT(2)
22e2ad626fSUlf Hansson #define FUNC_STAT_ISO_STAT_MASK BIT(4)
23*52dd070cSPeng Fan #define FUNC_STAT_SSAR_STAT_MASK BIT(8)
24e2ad626fSUlf Hansson
25e2ad626fSUlf Hansson struct imx93_power_domain {
26e2ad626fSUlf Hansson struct generic_pm_domain genpd;
27e2ad626fSUlf Hansson struct device *dev;
28e2ad626fSUlf Hansson void __iomem *addr;
29e2ad626fSUlf Hansson struct clk_bulk_data *clks;
30e2ad626fSUlf Hansson int num_clks;
31e2ad626fSUlf Hansson };
32e2ad626fSUlf Hansson
33e2ad626fSUlf Hansson #define to_imx93_pd(_genpd) container_of(_genpd, struct imx93_power_domain, genpd)
34e2ad626fSUlf Hansson
imx93_pd_on(struct generic_pm_domain * genpd)35e2ad626fSUlf Hansson static int imx93_pd_on(struct generic_pm_domain *genpd)
36e2ad626fSUlf Hansson {
37e2ad626fSUlf Hansson struct imx93_power_domain *domain = to_imx93_pd(genpd);
38e2ad626fSUlf Hansson void __iomem *addr = domain->addr;
39e2ad626fSUlf Hansson u32 val;
40e2ad626fSUlf Hansson int ret;
41e2ad626fSUlf Hansson
42e2ad626fSUlf Hansson ret = clk_bulk_prepare_enable(domain->num_clks, domain->clks);
43e2ad626fSUlf Hansson if (ret) {
44e2ad626fSUlf Hansson dev_err(domain->dev, "failed to enable clocks for domain: %s\n", genpd->name);
45e2ad626fSUlf Hansson return ret;
46e2ad626fSUlf Hansson }
47e2ad626fSUlf Hansson
48e2ad626fSUlf Hansson val = readl(addr + MIX_SLICE_SW_CTRL_OFF);
49e2ad626fSUlf Hansson val &= ~SLICE_SW_CTRL_PDN_SOFT_MASK;
50e2ad626fSUlf Hansson writel(val, addr + MIX_SLICE_SW_CTRL_OFF);
51e2ad626fSUlf Hansson
52e2ad626fSUlf Hansson ret = readl_poll_timeout(addr + MIX_FUNC_STAT_OFF, val,
53e2ad626fSUlf Hansson !(val & FUNC_STAT_SSAR_STAT_MASK), 1, 10000);
54*52dd070cSPeng Fan if (ret) {
55e2ad626fSUlf Hansson dev_err(domain->dev, "pd_on timeout: name: %s, stat: %x\n", genpd->name, val);
56e2ad626fSUlf Hansson return ret;
57e2ad626fSUlf Hansson }
58e2ad626fSUlf Hansson
59e2ad626fSUlf Hansson return 0;
60e2ad626fSUlf Hansson }
61e2ad626fSUlf Hansson
imx93_pd_off(struct generic_pm_domain * genpd)62e2ad626fSUlf Hansson static int imx93_pd_off(struct generic_pm_domain *genpd)
63e2ad626fSUlf Hansson {
64e2ad626fSUlf Hansson struct imx93_power_domain *domain = to_imx93_pd(genpd);
65e2ad626fSUlf Hansson void __iomem *addr = domain->addr;
66e2ad626fSUlf Hansson int ret;
67e2ad626fSUlf Hansson u32 val;
68e2ad626fSUlf Hansson
69e2ad626fSUlf Hansson /* Power off MIX */
70e2ad626fSUlf Hansson val = readl(addr + MIX_SLICE_SW_CTRL_OFF);
71e2ad626fSUlf Hansson val |= SLICE_SW_CTRL_PDN_SOFT_MASK;
72e2ad626fSUlf Hansson writel(val, addr + MIX_SLICE_SW_CTRL_OFF);
73e2ad626fSUlf Hansson
74e2ad626fSUlf Hansson ret = readl_poll_timeout(addr + MIX_FUNC_STAT_OFF, val,
75e2ad626fSUlf Hansson val & FUNC_STAT_PSW_STAT_MASK, 1, 10000);
76*52dd070cSPeng Fan if (ret) {
77e2ad626fSUlf Hansson dev_err(domain->dev, "pd_off timeout: name: %s, stat: %x\n", genpd->name, val);
78e2ad626fSUlf Hansson return ret;
79e2ad626fSUlf Hansson }
80e2ad626fSUlf Hansson
81e2ad626fSUlf Hansson clk_bulk_disable_unprepare(domain->num_clks, domain->clks);
82e2ad626fSUlf Hansson
83e2ad626fSUlf Hansson return 0;
84e2ad626fSUlf Hansson };
85e2ad626fSUlf Hansson
imx93_pd_remove(struct platform_device * pdev)86e2ad626fSUlf Hansson static void imx93_pd_remove(struct platform_device *pdev)
87673c09bcSUwe Kleine-König {
88e2ad626fSUlf Hansson struct imx93_power_domain *domain = platform_get_drvdata(pdev);
89e2ad626fSUlf Hansson struct device *dev = &pdev->dev;
90e2ad626fSUlf Hansson struct device_node *np = dev->of_node;
91e2ad626fSUlf Hansson
92e2ad626fSUlf Hansson of_genpd_del_provider(np);
93e2ad626fSUlf Hansson pm_genpd_remove(&domain->genpd);
94e2ad626fSUlf Hansson }
95e2ad626fSUlf Hansson
imx93_pd_probe(struct platform_device * pdev)96e2ad626fSUlf Hansson static int imx93_pd_probe(struct platform_device *pdev)
97e2ad626fSUlf Hansson {
98e2ad626fSUlf Hansson struct device *dev = &pdev->dev;
99e2ad626fSUlf Hansson struct device_node *np = dev->of_node;
100e2ad626fSUlf Hansson struct imx93_power_domain *domain;
101e2ad626fSUlf Hansson bool init_off;
102e2ad626fSUlf Hansson int ret;
103e2ad626fSUlf Hansson
104e2ad626fSUlf Hansson domain = devm_kzalloc(dev, sizeof(*domain), GFP_KERNEL);
105e2ad626fSUlf Hansson if (!domain)
106e2ad626fSUlf Hansson return -ENOMEM;
107e2ad626fSUlf Hansson
108e2ad626fSUlf Hansson domain->addr = devm_platform_ioremap_resource(pdev, 0);
109e2ad626fSUlf Hansson if (IS_ERR(domain->addr))
110e2ad626fSUlf Hansson return PTR_ERR(domain->addr);
111e2ad626fSUlf Hansson
112e2ad626fSUlf Hansson domain->num_clks = devm_clk_bulk_get_all(dev, &domain->clks);
113e2ad626fSUlf Hansson if (domain->num_clks < 0)
114e2ad626fSUlf Hansson return dev_err_probe(dev, domain->num_clks, "Failed to get domain's clocks\n");
115e2ad626fSUlf Hansson
116e2ad626fSUlf Hansson domain->genpd.name = dev_name(dev);
117e2ad626fSUlf Hansson domain->genpd.power_off = imx93_pd_off;
118e2ad626fSUlf Hansson domain->genpd.power_on = imx93_pd_on;
119e2ad626fSUlf Hansson domain->dev = dev;
120e2ad626fSUlf Hansson
121e2ad626fSUlf Hansson init_off = readl(domain->addr + MIX_FUNC_STAT_OFF) & FUNC_STAT_ISO_STAT_MASK;
122e2ad626fSUlf Hansson /* Just to sync the status of hardware */
123e2ad626fSUlf Hansson if (!init_off) {
124e2ad626fSUlf Hansson ret = clk_bulk_prepare_enable(domain->num_clks, domain->clks);
125e2ad626fSUlf Hansson if (ret)
126e2ad626fSUlf Hansson return dev_err_probe(domain->dev, ret,
127e2ad626fSUlf Hansson "failed to enable clocks for domain: %s\n",
128e2ad626fSUlf Hansson domain->genpd.name);
129e2ad626fSUlf Hansson }
130e2ad626fSUlf Hansson
131e2ad626fSUlf Hansson ret = pm_genpd_init(&domain->genpd, NULL, init_off);
132e2ad626fSUlf Hansson if (ret)
133e2ad626fSUlf Hansson goto err_clk_unprepare;
134e2ad626fSUlf Hansson
135e2ad626fSUlf Hansson platform_set_drvdata(pdev, domain);
136e2ad626fSUlf Hansson
137e2ad626fSUlf Hansson ret = of_genpd_add_provider_simple(np, &domain->genpd);
138e2ad626fSUlf Hansson if (ret)
139e2ad626fSUlf Hansson goto err_genpd_remove;
140e2ad626fSUlf Hansson
141e2ad626fSUlf Hansson return 0;
142e2ad626fSUlf Hansson
143e2ad626fSUlf Hansson err_genpd_remove:
144e2ad626fSUlf Hansson pm_genpd_remove(&domain->genpd);
145e2ad626fSUlf Hansson
146e2ad626fSUlf Hansson err_clk_unprepare:
147e2ad626fSUlf Hansson if (!init_off)
148e2ad626fSUlf Hansson clk_bulk_disable_unprepare(domain->num_clks, domain->clks);
149e2ad626fSUlf Hansson
150e2ad626fSUlf Hansson return ret;
151e2ad626fSUlf Hansson }
152e2ad626fSUlf Hansson
153e2ad626fSUlf Hansson static const struct of_device_id imx93_pd_ids[] = {
154e2ad626fSUlf Hansson { .compatible = "fsl,imx93-src-slice" },
155e2ad626fSUlf Hansson { }
156e2ad626fSUlf Hansson };
157e2ad626fSUlf Hansson MODULE_DEVICE_TABLE(of, imx93_pd_ids);
158e2ad626fSUlf Hansson
159e2ad626fSUlf Hansson static struct platform_driver imx93_power_domain_driver = {
160e2ad626fSUlf Hansson .driver = {
161e2ad626fSUlf Hansson .name = "imx93_power_domain",
162e2ad626fSUlf Hansson .of_match_table = imx93_pd_ids,
163e2ad626fSUlf Hansson },
164e2ad626fSUlf Hansson .probe = imx93_pd_probe,
165e2ad626fSUlf Hansson .remove_new = imx93_pd_remove,
166e2ad626fSUlf Hansson };
167e2ad626fSUlf Hansson module_platform_driver(imx93_power_domain_driver);
168e2ad626fSUlf Hansson
169673c09bcSUwe Kleine-König MODULE_AUTHOR("Peng Fan <peng.fan@nxp.com>");
170e2ad626fSUlf Hansson MODULE_DESCRIPTION("NXP i.MX93 power domain driver");
171e2ad626fSUlf Hansson MODULE_LICENSE("GPL");
172e2ad626fSUlf Hansson