1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2017 Impinj, Inc
6 * Based on the code of analogus driver:
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_domain.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/consumer.h>
16 #include <dt-bindings/power/imx7-power.h>
18 #define GPC_LPCR_A_CORE_BSC 0x000
20 #define GPC_PGC_CPU_MAPPING 0x0ec
21 #define USB_HSIC_PHY_A_CORE_DOMAIN BIT(6)
22 #define USB_OTG2_PHY_A_CORE_DOMAIN BIT(5)
23 #define USB_OTG1_PHY_A_CORE_DOMAIN BIT(4)
24 #define PCIE_PHY_A_CORE_DOMAIN BIT(3)
25 #define MIPI_PHY_A_CORE_DOMAIN BIT(2)
27 #define GPC_PU_PGC_SW_PUP_REQ 0x0f8
28 #define GPC_PU_PGC_SW_PDN_REQ 0x104
29 #define USB_HSIC_PHY_SW_Pxx_REQ BIT(4)
30 #define USB_OTG2_PHY_SW_Pxx_REQ BIT(3)
31 #define USB_OTG1_PHY_SW_Pxx_REQ BIT(2)
32 #define PCIE_PHY_SW_Pxx_REQ BIT(1)
33 #define MIPI_PHY_SW_Pxx_REQ BIT(0)
35 #define GPC_M4_PU_PDN_FLG 0x1bc
38 * The PGC offset values in Reference Manual
39 * (Rev. 1, 01/2018 and the older ones) GPC chapter's
40 * GPC_PGC memory map are incorrect, below offset
41 * values are from design RTL.
45 #define PGC_USB_HSIC 20
46 #define GPC_PGC_CTRL(n) (0x800 + (n) * 0x40)
47 #define GPC_PGC_SR(n) (GPC_PGC_CTRL(n) + 0xc)
49 #define GPC_PGC_CTRL_PCR BIT(0)
51 struct imx_pgc_domain {
52 struct generic_pm_domain genpd;
53 struct regmap *regmap;
54 struct regulator *regulator;
67 struct imx_pgc_domain_data {
68 const struct imx_pgc_domain *domains;
72 static int imx_gpc_pu_pgc_sw_pxx_req(struct generic_pm_domain *genpd,
75 struct imx_pgc_domain *domain = container_of(genpd,
76 struct imx_pgc_domain,
78 unsigned int offset = on ?
79 GPC_PU_PGC_SW_PUP_REQ : GPC_PU_PGC_SW_PDN_REQ;
80 const bool enable_power_control = !on;
81 const bool has_regulator = !IS_ERR(domain->regulator);
82 unsigned long deadline;
85 regmap_update_bits(domain->regmap, GPC_PGC_CPU_MAPPING,
86 domain->bits.map, domain->bits.map);
88 if (has_regulator && on) {
89 ret = regulator_enable(domain->regulator);
91 dev_err(domain->dev, "failed to enable regulator\n");
96 if (enable_power_control)
97 regmap_update_bits(domain->regmap, GPC_PGC_CTRL(domain->pgc),
98 GPC_PGC_CTRL_PCR, GPC_PGC_CTRL_PCR);
100 regmap_update_bits(domain->regmap, offset,
101 domain->bits.pxx, domain->bits.pxx);
104 * As per "5.5.9.4 Example Code 4" in IMX7DRM.pdf wait
105 * for PUP_REQ/PDN_REQ bit to be cleared
107 deadline = jiffies + msecs_to_jiffies(1);
111 regmap_read(domain->regmap, offset, &pxx_req);
113 if (!(pxx_req & domain->bits.pxx))
116 if (time_after(jiffies, deadline)) {
117 dev_err(domain->dev, "falied to command PGC\n");
120 * If we were in a process of enabling a
121 * domain and failed we might as well disable
122 * the regulator we just enabled. And if it
123 * was the opposite situation and we failed to
124 * power down -- keep the regulator on
133 if (enable_power_control)
134 regmap_update_bits(domain->regmap, GPC_PGC_CTRL(domain->pgc),
135 GPC_PGC_CTRL_PCR, 0);
137 if (has_regulator && !on) {
140 err = regulator_disable(domain->regulator);
143 "failed to disable regulator: %d\n", ret);
144 /* Preserve earlier error code */
148 regmap_update_bits(domain->regmap, GPC_PGC_CPU_MAPPING,
149 domain->bits.map, 0);
153 static int imx_gpc_pu_pgc_sw_pup_req(struct generic_pm_domain *genpd)
155 return imx_gpc_pu_pgc_sw_pxx_req(genpd, true);
158 static int imx_gpc_pu_pgc_sw_pdn_req(struct generic_pm_domain *genpd)
160 return imx_gpc_pu_pgc_sw_pxx_req(genpd, false);
163 static const struct imx_pgc_domain imx7_pgc_domains[] = {
164 [IMX7_POWER_DOMAIN_MIPI_PHY] = {
169 .pxx = MIPI_PHY_SW_Pxx_REQ,
170 .map = MIPI_PHY_A_CORE_DOMAIN,
176 [IMX7_POWER_DOMAIN_PCIE_PHY] = {
181 .pxx = PCIE_PHY_SW_Pxx_REQ,
182 .map = PCIE_PHY_A_CORE_DOMAIN,
188 [IMX7_POWER_DOMAIN_USB_HSIC_PHY] = {
190 .name = "usb-hsic-phy",
193 .pxx = USB_HSIC_PHY_SW_Pxx_REQ,
194 .map = USB_HSIC_PHY_A_CORE_DOMAIN,
201 static const struct imx_pgc_domain_data imx7_pgc_domain_data = {
202 .domains = imx7_pgc_domains,
203 .domains_num = ARRAY_SIZE(imx7_pgc_domains),
206 static int imx_pgc_domain_probe(struct platform_device *pdev)
208 struct imx_pgc_domain *domain = pdev->dev.platform_data;
211 domain->dev = &pdev->dev;
213 domain->regulator = devm_regulator_get_optional(domain->dev, "power");
214 if (IS_ERR(domain->regulator)) {
215 if (PTR_ERR(domain->regulator) != -ENODEV) {
216 if (PTR_ERR(domain->regulator) != -EPROBE_DEFER)
217 dev_err(domain->dev, "Failed to get domain's regulator\n");
218 return PTR_ERR(domain->regulator);
221 regulator_set_voltage(domain->regulator,
222 domain->voltage, domain->voltage);
225 ret = pm_genpd_init(&domain->genpd, NULL, true);
227 dev_err(domain->dev, "Failed to init power domain\n");
231 ret = of_genpd_add_provider_simple(domain->dev->of_node,
234 dev_err(domain->dev, "Failed to add genpd provider\n");
235 pm_genpd_remove(&domain->genpd);
241 static int imx_pgc_domain_remove(struct platform_device *pdev)
243 struct imx_pgc_domain *domain = pdev->dev.platform_data;
245 of_genpd_del_provider(domain->dev->of_node);
246 pm_genpd_remove(&domain->genpd);
251 static const struct platform_device_id imx_pgc_domain_id[] = {
252 { "imx-pgc-domain", },
256 static struct platform_driver imx_pgc_domain_driver = {
260 .probe = imx_pgc_domain_probe,
261 .remove = imx_pgc_domain_remove,
262 .id_table = imx_pgc_domain_id,
264 builtin_platform_driver(imx_pgc_domain_driver)
266 static int imx_gpcv2_probe(struct platform_device *pdev)
268 static const struct imx_pgc_domain_data *domain_data;
269 static const struct regmap_range yes_ranges[] = {
270 regmap_reg_range(GPC_LPCR_A_CORE_BSC,
272 regmap_reg_range(GPC_PGC_CTRL(PGC_MIPI),
273 GPC_PGC_SR(PGC_MIPI)),
274 regmap_reg_range(GPC_PGC_CTRL(PGC_PCIE),
275 GPC_PGC_SR(PGC_PCIE)),
276 regmap_reg_range(GPC_PGC_CTRL(PGC_USB_HSIC),
277 GPC_PGC_SR(PGC_USB_HSIC)),
279 static const struct regmap_access_table access_table = {
280 .yes_ranges = yes_ranges,
281 .n_yes_ranges = ARRAY_SIZE(yes_ranges),
283 static const struct regmap_config regmap_config = {
287 .rd_table = &access_table,
288 .wr_table = &access_table,
289 .max_register = SZ_4K,
291 struct device *dev = &pdev->dev;
292 struct device_node *pgc_np, *np;
293 struct regmap *regmap;
294 struct resource *res;
298 pgc_np = of_get_child_by_name(dev->of_node, "pgc");
300 dev_err(dev, "No power domains specified in DT\n");
304 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
305 base = devm_ioremap_resource(dev, res);
307 return PTR_ERR(base);
309 regmap = devm_regmap_init_mmio(dev, base, ®map_config);
310 if (IS_ERR(regmap)) {
311 ret = PTR_ERR(regmap);
312 dev_err(dev, "failed to init regmap (%d)\n", ret);
316 domain_data = of_device_get_match_data(&pdev->dev);
318 for_each_child_of_node(pgc_np, np) {
319 struct platform_device *pd_pdev;
320 struct imx_pgc_domain *domain;
323 ret = of_property_read_u32(np, "reg", &domain_index);
325 dev_err(dev, "Failed to read 'reg' property\n");
330 if (domain_index >= domain_data->domains_num) {
332 "Domain index %d is out of bounds\n",
337 pd_pdev = platform_device_alloc("imx-pgc-domain",
340 dev_err(dev, "Failed to allocate platform device\n");
345 ret = platform_device_add_data(pd_pdev,
346 &domain_data->domains[domain_index],
347 sizeof(domain_data->domains[domain_index]));
349 platform_device_put(pd_pdev);
354 domain = pd_pdev->dev.platform_data;
355 domain->regmap = regmap;
356 domain->genpd.power_on = imx_gpc_pu_pgc_sw_pup_req;
357 domain->genpd.power_off = imx_gpc_pu_pgc_sw_pdn_req;
359 pd_pdev->dev.parent = dev;
360 pd_pdev->dev.of_node = np;
362 ret = platform_device_add(pd_pdev);
364 platform_device_put(pd_pdev);
373 static const struct of_device_id imx_gpcv2_dt_ids[] = {
374 { .compatible = "fsl,imx7d-gpc", .data = &imx7_pgc_domain_data, },
378 static struct platform_driver imx_gpc_driver = {
381 .of_match_table = imx_gpcv2_dt_ids,
383 .probe = imx_gpcv2_probe,
385 builtin_platform_driver(imx_gpc_driver)