2 * Copyright 2017 Impinj, Inc
3 * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
5 * Based on the code of analogus driver:
7 * Copyright 2015-2017 Pengutronix, Lucas Stach <kernel@pengutronix.de>
9 * The code contained herein is licensed under the GNU General Public
10 * License. You may obtain a copy of the GNU General Public License
11 * Version 2 or later at the following locations:
13 * http://www.opensource.org/licenses/gpl-license.html
14 * http://www.gnu.org/copyleft/gpl.html
17 #include <linux/platform_device.h>
18 #include <linux/pm_domain.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/consumer.h>
21 #include <dt-bindings/power/imx7-power.h>
23 #define GPC_LPCR_A7_BSC 0x000
25 #define GPC_PGC_CPU_MAPPING 0x0ec
26 #define USB_HSIC_PHY_A7_DOMAIN BIT(6)
27 #define USB_OTG2_PHY_A7_DOMAIN BIT(5)
28 #define USB_OTG1_PHY_A7_DOMAIN BIT(4)
29 #define PCIE_PHY_A7_DOMAIN BIT(3)
30 #define MIPI_PHY_A7_DOMAIN BIT(2)
32 #define GPC_PU_PGC_SW_PUP_REQ 0x0f8
33 #define GPC_PU_PGC_SW_PDN_REQ 0x104
34 #define USB_HSIC_PHY_SW_Pxx_REQ BIT(4)
35 #define USB_OTG2_PHY_SW_Pxx_REQ BIT(3)
36 #define USB_OTG1_PHY_SW_Pxx_REQ BIT(2)
37 #define PCIE_PHY_SW_Pxx_REQ BIT(1)
38 #define MIPI_PHY_SW_Pxx_REQ BIT(0)
40 #define GPC_M4_PU_PDN_FLG 0x1bc
43 * The PGC offset values in Reference Manual
44 * (Rev. 1, 01/2018 and the older ones) GPC chapter's
45 * GPC_PGC memory map are incorrect, below offset
46 * values are from design RTL.
50 #define PGC_USB_HSIC 20
51 #define GPC_PGC_CTRL(n) (0x800 + (n) * 0x40)
52 #define GPC_PGC_SR(n) (GPC_PGC_CTRL(n) + 0xc)
54 #define GPC_PGC_CTRL_PCR BIT(0)
56 struct imx7_pgc_domain
{
57 struct generic_pm_domain genpd
;
58 struct regmap
*regmap
;
59 struct regulator
*regulator
;
72 static int imx7_gpc_pu_pgc_sw_pxx_req(struct generic_pm_domain
*genpd
,
75 struct imx7_pgc_domain
*domain
= container_of(genpd
,
76 struct imx7_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 imx7_gpc_pu_pgc_sw_pup_req(struct generic_pm_domain
*genpd
)
155 return imx7_gpc_pu_pgc_sw_pxx_req(genpd
, true);
158 static int imx7_gpc_pu_pgc_sw_pdn_req(struct generic_pm_domain
*genpd
)
160 return imx7_gpc_pu_pgc_sw_pxx_req(genpd
, false);
163 static const struct imx7_pgc_domain imx7_pgc_domains
[] = {
164 [IMX7_POWER_DOMAIN_MIPI_PHY
] = {
169 .pxx
= MIPI_PHY_SW_Pxx_REQ
,
170 .map
= MIPI_PHY_A7_DOMAIN
,
176 [IMX7_POWER_DOMAIN_PCIE_PHY
] = {
181 .pxx
= PCIE_PHY_SW_Pxx_REQ
,
182 .map
= PCIE_PHY_A7_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_A7_DOMAIN
,
201 static int imx7_pgc_domain_probe(struct platform_device
*pdev
)
203 struct imx7_pgc_domain
*domain
= pdev
->dev
.platform_data
;
206 domain
->dev
= &pdev
->dev
;
208 domain
->regulator
= devm_regulator_get_optional(domain
->dev
, "power");
209 if (IS_ERR(domain
->regulator
)) {
210 if (PTR_ERR(domain
->regulator
) != -ENODEV
) {
211 if (PTR_ERR(domain
->regulator
) != -EPROBE_DEFER
)
212 dev_err(domain
->dev
, "Failed to get domain's regulator\n");
213 return PTR_ERR(domain
->regulator
);
216 regulator_set_voltage(domain
->regulator
,
217 domain
->voltage
, domain
->voltage
);
220 ret
= pm_genpd_init(&domain
->genpd
, NULL
, true);
222 dev_err(domain
->dev
, "Failed to init power domain\n");
226 ret
= of_genpd_add_provider_simple(domain
->dev
->of_node
,
229 dev_err(domain
->dev
, "Failed to add genpd provider\n");
230 pm_genpd_remove(&domain
->genpd
);
236 static int imx7_pgc_domain_remove(struct platform_device
*pdev
)
238 struct imx7_pgc_domain
*domain
= pdev
->dev
.platform_data
;
240 of_genpd_del_provider(domain
->dev
->of_node
);
241 pm_genpd_remove(&domain
->genpd
);
246 static const struct platform_device_id imx7_pgc_domain_id
[] = {
247 { "imx7-pgc-domain", },
251 static struct platform_driver imx7_pgc_domain_driver
= {
255 .probe
= imx7_pgc_domain_probe
,
256 .remove
= imx7_pgc_domain_remove
,
257 .id_table
= imx7_pgc_domain_id
,
259 builtin_platform_driver(imx7_pgc_domain_driver
)
261 static int imx_gpcv2_probe(struct platform_device
*pdev
)
263 static const struct regmap_range yes_ranges
[] = {
264 regmap_reg_range(GPC_LPCR_A7_BSC
,
266 regmap_reg_range(GPC_PGC_CTRL(PGC_MIPI
),
267 GPC_PGC_SR(PGC_MIPI
)),
268 regmap_reg_range(GPC_PGC_CTRL(PGC_PCIE
),
269 GPC_PGC_SR(PGC_PCIE
)),
270 regmap_reg_range(GPC_PGC_CTRL(PGC_USB_HSIC
),
271 GPC_PGC_SR(PGC_USB_HSIC
)),
273 static const struct regmap_access_table access_table
= {
274 .yes_ranges
= yes_ranges
,
275 .n_yes_ranges
= ARRAY_SIZE(yes_ranges
),
277 static const struct regmap_config regmap_config
= {
281 .rd_table
= &access_table
,
282 .wr_table
= &access_table
,
283 .max_register
= SZ_4K
,
285 struct device
*dev
= &pdev
->dev
;
286 struct device_node
*pgc_np
, *np
;
287 struct regmap
*regmap
;
288 struct resource
*res
;
292 pgc_np
= of_get_child_by_name(dev
->of_node
, "pgc");
294 dev_err(dev
, "No power domains specified in DT\n");
298 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
299 base
= devm_ioremap_resource(dev
, res
);
301 return PTR_ERR(base
);
303 regmap
= devm_regmap_init_mmio(dev
, base
, ®map_config
);
304 if (IS_ERR(regmap
)) {
305 ret
= PTR_ERR(regmap
);
306 dev_err(dev
, "failed to init regmap (%d)\n", ret
);
310 for_each_child_of_node(pgc_np
, np
) {
311 struct platform_device
*pd_pdev
;
312 struct imx7_pgc_domain
*domain
;
315 ret
= of_property_read_u32(np
, "reg", &domain_index
);
317 dev_err(dev
, "Failed to read 'reg' property\n");
322 if (domain_index
>= ARRAY_SIZE(imx7_pgc_domains
)) {
324 "Domain index %d is out of bounds\n",
329 pd_pdev
= platform_device_alloc("imx7-pgc-domain",
332 dev_err(dev
, "Failed to allocate platform device\n");
337 ret
= platform_device_add_data(pd_pdev
,
338 &imx7_pgc_domains
[domain_index
],
339 sizeof(imx7_pgc_domains
[domain_index
]));
341 platform_device_put(pd_pdev
);
346 domain
= pd_pdev
->dev
.platform_data
;
347 domain
->regmap
= regmap
;
348 domain
->genpd
.power_on
= imx7_gpc_pu_pgc_sw_pup_req
;
349 domain
->genpd
.power_off
= imx7_gpc_pu_pgc_sw_pdn_req
;
351 pd_pdev
->dev
.parent
= dev
;
352 pd_pdev
->dev
.of_node
= np
;
354 ret
= platform_device_add(pd_pdev
);
356 platform_device_put(pd_pdev
);
365 static const struct of_device_id imx_gpcv2_dt_ids
[] = {
366 { .compatible
= "fsl,imx7d-gpc" },
370 static struct platform_driver imx_gpc_driver
= {
373 .of_match_table
= imx_gpcv2_dt_ids
,
375 .probe
= imx_gpcv2_probe
,
377 builtin_platform_driver(imx_gpc_driver
)