2 * Copyright 2015-2017 Pengutronix, Lucas Stach <kernel@pengutronix.de>
3 * Copyright 2011-2013 Freescale Semiconductor, Inc.
5 * The code contained herein is licensed under the GNU General Public
6 * License. You may obtain a copy of the GNU General Public License
7 * Version 2 or later at the following locations:
9 * http://www.opensource.org/licenses/gpl-license.html
10 * http://www.gnu.org/copyleft/gpl.html
13 #include <linux/clk.h>
14 #include <linux/delay.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_domain.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/consumer.h>
22 #define GPC_CNTR 0x000
24 #define GPC_PGC_CTRL_OFFS 0x0
25 #define GPC_PGC_PUPSCR_OFFS 0x4
26 #define GPC_PGC_PDNSCR_OFFS 0x8
27 #define GPC_PGC_SW2ISO_SHIFT 0x8
28 #define GPC_PGC_SW_SHIFT 0x0
30 #define GPC_PGC_PCI_PDN 0x200
31 #define GPC_PGC_PCI_SR 0x20c
33 #define GPC_PGC_GPU_PDN 0x260
34 #define GPC_PGC_GPU_PUPSCR 0x264
35 #define GPC_PGC_GPU_PDNSCR 0x268
36 #define GPC_PGC_GPU_SR 0x26c
38 #define GPC_PGC_DISP_PDN 0x240
39 #define GPC_PGC_DISP_SR 0x24c
41 #define GPU_VPU_PUP_REQ BIT(1)
42 #define GPU_VPU_PDN_REQ BIT(0)
46 #define PGC_DOMAIN_FLAG_NO_PD BIT(0)
48 struct imx_pm_domain {
49 struct generic_pm_domain base;
50 struct regmap *regmap;
51 struct regulator *supply;
52 struct clk *clk[GPC_CLK_MAX];
54 unsigned int reg_offs;
55 signed char cntr_pdn_bit;
56 unsigned int ipg_rate_mhz;
59 static inline struct imx_pm_domain *
60 to_imx_pm_domain(struct generic_pm_domain *genpd)
62 return container_of(genpd, struct imx_pm_domain, base);
65 static int imx6_pm_domain_power_off(struct generic_pm_domain *genpd)
67 struct imx_pm_domain *pd = to_imx_pm_domain(genpd);
71 /* Read ISO and ISO2SW power down delays */
72 regmap_read(pd->regmap, pd->reg_offs + GPC_PGC_PUPSCR_OFFS, &val);
74 iso2sw = (val >> 8) & 0x3f;
76 /* Gate off domain when powered down */
77 regmap_update_bits(pd->regmap, pd->reg_offs + GPC_PGC_CTRL_OFFS,
80 /* Request GPC to power down domain */
81 val = BIT(pd->cntr_pdn_bit);
82 regmap_update_bits(pd->regmap, GPC_CNTR, val, val);
84 /* Wait ISO + ISO2SW IPG clock cycles */
85 udelay(DIV_ROUND_UP(iso + iso2sw, pd->ipg_rate_mhz));
88 regulator_disable(pd->supply);
93 static int imx6_pm_domain_power_on(struct generic_pm_domain *genpd)
95 struct imx_pm_domain *pd = to_imx_pm_domain(genpd);
96 int i, ret, sw, sw2iso;
100 ret = regulator_enable(pd->supply);
102 pr_err("%s: failed to enable regulator: %d\n",
108 /* Enable reset clocks for all devices in the domain */
109 for (i = 0; i < pd->num_clks; i++)
110 clk_prepare_enable(pd->clk[i]);
112 /* Gate off domain when powered down */
113 regmap_update_bits(pd->regmap, pd->reg_offs + GPC_PGC_CTRL_OFFS,
116 /* Read ISO and ISO2SW power up delays */
117 regmap_read(pd->regmap, pd->reg_offs + GPC_PGC_PUPSCR_OFFS, &val);
119 sw2iso = (val >> 8) & 0x3f;
121 /* Request GPC to power up domain */
122 val = BIT(pd->cntr_pdn_bit + 1);
123 regmap_update_bits(pd->regmap, GPC_CNTR, val, val);
125 /* Wait ISO + ISO2SW IPG clock cycles */
126 udelay(DIV_ROUND_UP(sw + sw2iso, pd->ipg_rate_mhz));
128 /* Disable reset clocks for all devices in the domain */
129 for (i = 0; i < pd->num_clks; i++)
130 clk_disable_unprepare(pd->clk[i]);
135 static int imx_pgc_get_clocks(struct device *dev, struct imx_pm_domain *domain)
140 struct clk *clk = of_clk_get(dev->of_node, i);
143 if (i >= GPC_CLK_MAX) {
144 dev_err(dev, "more than %d clocks\n", GPC_CLK_MAX);
148 domain->clk[i] = clk;
150 domain->num_clks = i;
156 clk_put(domain->clk[i]);
161 static void imx_pgc_put_clocks(struct imx_pm_domain *domain)
165 for (i = domain->num_clks - 1; i >= 0; i--)
166 clk_put(domain->clk[i]);
169 static int imx_pgc_parse_dt(struct device *dev, struct imx_pm_domain *domain)
171 /* try to get the domain supply regulator */
172 domain->supply = devm_regulator_get_optional(dev, "power");
173 if (IS_ERR(domain->supply)) {
174 if (PTR_ERR(domain->supply) == -ENODEV)
175 domain->supply = NULL;
177 return PTR_ERR(domain->supply);
180 /* try to get all clocks needed for reset propagation */
181 return imx_pgc_get_clocks(dev, domain);
184 static int imx_pgc_power_domain_probe(struct platform_device *pdev)
186 struct imx_pm_domain *domain = pdev->dev.platform_data;
187 struct device *dev = &pdev->dev;
190 /* if this PD is associated with a DT node try to parse it */
192 ret = imx_pgc_parse_dt(dev, domain);
197 /* initially power on the domain */
198 if (domain->base.power_on)
199 domain->base.power_on(&domain->base);
201 if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
202 pm_genpd_init(&domain->base, NULL, false);
203 ret = of_genpd_add_provider_simple(dev->of_node, &domain->base);
208 device_link_add(dev, dev->parent, DL_FLAG_AUTOREMOVE_CONSUMER);
213 pm_genpd_remove(&domain->base);
214 imx_pgc_put_clocks(domain);
219 static int imx_pgc_power_domain_remove(struct platform_device *pdev)
221 struct imx_pm_domain *domain = pdev->dev.platform_data;
223 if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
224 of_genpd_del_provider(pdev->dev.of_node);
225 pm_genpd_remove(&domain->base);
226 imx_pgc_put_clocks(domain);
232 static const struct platform_device_id imx_pgc_power_domain_id[] = {
233 { "imx-pgc-power-domain"},
237 static struct platform_driver imx_pgc_power_domain_driver = {
239 .name = "imx-pgc-pd",
241 .probe = imx_pgc_power_domain_probe,
242 .remove = imx_pgc_power_domain_remove,
243 .id_table = imx_pgc_power_domain_id,
245 builtin_platform_driver(imx_pgc_power_domain_driver)
247 #define GPC_PGC_DOMAIN_ARM 0
248 #define GPC_PGC_DOMAIN_PU 1
249 #define GPC_PGC_DOMAIN_DISPLAY 2
251 static struct genpd_power_state imx6_pm_domain_pu_state = {
252 .power_off_latency_ns = 25000,
253 .power_on_latency_ns = 2000000,
256 static struct imx_pm_domain imx_gpc_domains[] = {
260 .flags = GENPD_FLAG_ALWAYS_ON,
265 .power_off = imx6_pm_domain_power_off,
266 .power_on = imx6_pm_domain_power_on,
267 .states = &imx6_pm_domain_pu_state,
275 .power_off = imx6_pm_domain_power_off,
276 .power_on = imx6_pm_domain_power_on,
283 .power_off = imx6_pm_domain_power_off,
284 .power_on = imx6_pm_domain_power_on,
291 struct imx_gpc_dt_data {
293 bool err009619_present;
294 bool err006287_present;
297 static const struct imx_gpc_dt_data imx6q_dt_data = {
299 .err009619_present = false,
300 .err006287_present = false,
303 static const struct imx_gpc_dt_data imx6qp_dt_data = {
305 .err009619_present = true,
306 .err006287_present = false,
309 static const struct imx_gpc_dt_data imx6sl_dt_data = {
311 .err009619_present = false,
312 .err006287_present = true,
315 static const struct imx_gpc_dt_data imx6sx_dt_data = {
317 .err009619_present = false,
318 .err006287_present = false,
321 static const struct of_device_id imx_gpc_dt_ids[] = {
322 { .compatible = "fsl,imx6q-gpc", .data = &imx6q_dt_data },
323 { .compatible = "fsl,imx6qp-gpc", .data = &imx6qp_dt_data },
324 { .compatible = "fsl,imx6sl-gpc", .data = &imx6sl_dt_data },
325 { .compatible = "fsl,imx6sx-gpc", .data = &imx6sx_dt_data },
329 static const struct regmap_range yes_ranges[] = {
330 regmap_reg_range(GPC_CNTR, GPC_CNTR),
331 regmap_reg_range(GPC_PGC_PCI_PDN, GPC_PGC_PCI_SR),
332 regmap_reg_range(GPC_PGC_GPU_PDN, GPC_PGC_GPU_SR),
333 regmap_reg_range(GPC_PGC_DISP_PDN, GPC_PGC_DISP_SR),
336 static const struct regmap_access_table access_table = {
337 .yes_ranges = yes_ranges,
338 .n_yes_ranges = ARRAY_SIZE(yes_ranges),
341 static const struct regmap_config imx_gpc_regmap_config = {
345 .rd_table = &access_table,
346 .wr_table = &access_table,
347 .max_register = 0x2ac,
350 static struct generic_pm_domain *imx_gpc_onecell_domains[] = {
351 &imx_gpc_domains[0].base,
352 &imx_gpc_domains[1].base,
355 static struct genpd_onecell_data imx_gpc_onecell_data = {
356 .domains = imx_gpc_onecell_domains,
360 static int imx_gpc_old_dt_init(struct device *dev, struct regmap *regmap,
361 unsigned int num_domains)
363 struct imx_pm_domain *domain;
366 for (i = 0; i < num_domains; i++) {
367 domain = &imx_gpc_domains[i];
368 domain->regmap = regmap;
369 domain->ipg_rate_mhz = 66;
372 domain->supply = devm_regulator_get(dev, "pu");
373 if (IS_ERR(domain->supply))
374 return PTR_ERR(domain->supply);
376 ret = imx_pgc_get_clocks(dev, domain);
380 domain->base.power_on(&domain->base);
384 for (i = 0; i < num_domains; i++)
385 pm_genpd_init(&imx_gpc_domains[i].base, NULL, false);
387 if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
388 ret = of_genpd_add_provider_onecell(dev->of_node,
389 &imx_gpc_onecell_data);
397 for (i = 0; i < num_domains; i++)
398 pm_genpd_remove(&imx_gpc_domains[i].base);
399 imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
404 static int imx_gpc_probe(struct platform_device *pdev)
406 const struct of_device_id *of_id =
407 of_match_device(imx_gpc_dt_ids, &pdev->dev);
408 const struct imx_gpc_dt_data *of_id_data = of_id->data;
409 struct device_node *pgc_node;
410 struct regmap *regmap;
411 struct resource *res;
415 pgc_node = of_get_child_by_name(pdev->dev.of_node, "pgc");
417 /* bail out if DT too old and doesn't provide the necessary info */
418 if (!of_property_read_bool(pdev->dev.of_node, "#power-domain-cells") &&
422 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
423 base = devm_ioremap_resource(&pdev->dev, res);
425 return PTR_ERR(base);
427 regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
428 &imx_gpc_regmap_config);
429 if (IS_ERR(regmap)) {
430 ret = PTR_ERR(regmap);
431 dev_err(&pdev->dev, "failed to init regmap: %d\n",
436 /* Disable PU power down in normal operation if ERR009619 is present */
437 if (of_id_data->err009619_present)
438 imx_gpc_domains[GPC_PGC_DOMAIN_PU].base.flags |=
439 GENPD_FLAG_ALWAYS_ON;
441 /* Keep DISP always on if ERR006287 is present */
442 if (of_id_data->err006287_present)
443 imx_gpc_domains[GPC_PGC_DOMAIN_DISPLAY].base.flags |=
444 GENPD_FLAG_ALWAYS_ON;
447 ret = imx_gpc_old_dt_init(&pdev->dev, regmap,
448 of_id_data->num_domains);
452 struct imx_pm_domain *domain;
453 struct platform_device *pd_pdev;
454 struct device_node *np;
456 unsigned int ipg_rate_mhz;
459 ipg_clk = devm_clk_get(&pdev->dev, "ipg");
461 return PTR_ERR(ipg_clk);
462 ipg_rate_mhz = clk_get_rate(ipg_clk) / 1000000;
464 for_each_child_of_node(pgc_node, np) {
465 ret = of_property_read_u32(np, "reg", &domain_index);
470 if (domain_index >= of_id_data->num_domains)
473 pd_pdev = platform_device_alloc("imx-pgc-power-domain",
480 ret = platform_device_add_data(pd_pdev,
481 &imx_gpc_domains[domain_index],
482 sizeof(imx_gpc_domains[domain_index]));
484 platform_device_put(pd_pdev);
488 domain = pd_pdev->dev.platform_data;
489 domain->regmap = regmap;
490 domain->ipg_rate_mhz = ipg_rate_mhz;
492 pd_pdev->dev.parent = &pdev->dev;
493 pd_pdev->dev.of_node = np;
495 ret = platform_device_add(pd_pdev);
497 platform_device_put(pd_pdev);
507 static int imx_gpc_remove(struct platform_device *pdev)
509 struct device_node *pgc_node;
512 pgc_node = of_get_child_by_name(pdev->dev.of_node, "pgc");
514 /* bail out if DT too old and doesn't provide the necessary info */
515 if (!of_property_read_bool(pdev->dev.of_node, "#power-domain-cells") &&
520 * If the old DT binding is used the toplevel driver needs to
521 * de-register the power domains
524 of_genpd_del_provider(pdev->dev.of_node);
526 ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_PU].base);
529 imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
531 ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_ARM].base);
539 static struct platform_driver imx_gpc_driver = {
542 .of_match_table = imx_gpc_dt_ids,
544 .probe = imx_gpc_probe,
545 .remove = imx_gpc_remove,
547 builtin_platform_driver(imx_gpc_driver)