locking/refcounts: Include fewer headers in <linux/refcount.h>
[linux/fpc-iii.git] / drivers / soc / imx / gpcv2.c
blobf4e3bd40c72e60c0448c98456f7b53f6be7936bd
1 /*
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 #define PGC_MIPI 4
44 #define PGC_PCIE 5
45 #define PGC_USB_HSIC 8
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 imx7_pgc_domain {
52 struct generic_pm_domain genpd;
53 struct regmap *regmap;
54 struct regulator *regulator;
56 unsigned int pgc;
58 const struct {
59 u32 pxx;
60 u32 map;
61 } bits;
63 const int voltage;
64 struct device *dev;
67 static int imx7_gpc_pu_pgc_sw_pxx_req(struct generic_pm_domain *genpd,
68 bool on)
70 struct imx7_pgc_domain *domain = container_of(genpd,
71 struct imx7_pgc_domain,
72 genpd);
73 unsigned int offset = on ?
74 GPC_PU_PGC_SW_PUP_REQ : GPC_PU_PGC_SW_PDN_REQ;
75 const bool enable_power_control = !on;
76 const bool has_regulator = !IS_ERR(domain->regulator);
77 unsigned long deadline;
78 int ret = 0;
80 regmap_update_bits(domain->regmap, GPC_PGC_CPU_MAPPING,
81 domain->bits.map, domain->bits.map);
83 if (has_regulator && on) {
84 ret = regulator_enable(domain->regulator);
85 if (ret) {
86 dev_err(domain->dev, "failed to enable regulator\n");
87 goto unmap;
91 if (enable_power_control)
92 regmap_update_bits(domain->regmap, GPC_PGC_CTRL(domain->pgc),
93 GPC_PGC_CTRL_PCR, GPC_PGC_CTRL_PCR);
95 regmap_update_bits(domain->regmap, offset,
96 domain->bits.pxx, domain->bits.pxx);
99 * As per "5.5.9.4 Example Code 4" in IMX7DRM.pdf wait
100 * for PUP_REQ/PDN_REQ bit to be cleared
102 deadline = jiffies + msecs_to_jiffies(1);
103 while (true) {
104 u32 pxx_req;
106 regmap_read(domain->regmap, offset, &pxx_req);
108 if (!(pxx_req & domain->bits.pxx))
109 break;
111 if (time_after(jiffies, deadline)) {
112 dev_err(domain->dev, "falied to command PGC\n");
113 ret = -ETIMEDOUT;
115 * If we were in a process of enabling a
116 * domain and failed we might as well disable
117 * the regulator we just enabled. And if it
118 * was the opposite situation and we failed to
119 * power down -- keep the regulator on
121 on = !on;
122 break;
125 cpu_relax();
128 if (enable_power_control)
129 regmap_update_bits(domain->regmap, GPC_PGC_CTRL(domain->pgc),
130 GPC_PGC_CTRL_PCR, 0);
132 if (has_regulator && !on) {
133 int err;
135 err = regulator_disable(domain->regulator);
136 if (err)
137 dev_err(domain->dev,
138 "failed to disable regulator: %d\n", ret);
139 /* Preserve earlier error code */
140 ret = ret ?: err;
142 unmap:
143 regmap_update_bits(domain->regmap, GPC_PGC_CPU_MAPPING,
144 domain->bits.map, 0);
145 return ret;
148 static int imx7_gpc_pu_pgc_sw_pup_req(struct generic_pm_domain *genpd)
150 return imx7_gpc_pu_pgc_sw_pxx_req(genpd, true);
153 static int imx7_gpc_pu_pgc_sw_pdn_req(struct generic_pm_domain *genpd)
155 return imx7_gpc_pu_pgc_sw_pxx_req(genpd, false);
158 static const struct imx7_pgc_domain imx7_pgc_domains[] = {
159 [IMX7_POWER_DOMAIN_MIPI_PHY] = {
160 .genpd = {
161 .name = "mipi-phy",
163 .bits = {
164 .pxx = MIPI_PHY_SW_Pxx_REQ,
165 .map = MIPI_PHY_A7_DOMAIN,
167 .voltage = 1000000,
168 .pgc = PGC_MIPI,
171 [IMX7_POWER_DOMAIN_PCIE_PHY] = {
172 .genpd = {
173 .name = "pcie-phy",
175 .bits = {
176 .pxx = PCIE_PHY_SW_Pxx_REQ,
177 .map = PCIE_PHY_A7_DOMAIN,
179 .voltage = 1000000,
180 .pgc = PGC_PCIE,
183 [IMX7_POWER_DOMAIN_USB_HSIC_PHY] = {
184 .genpd = {
185 .name = "usb-hsic-phy",
187 .bits = {
188 .pxx = USB_HSIC_PHY_SW_Pxx_REQ,
189 .map = USB_HSIC_PHY_A7_DOMAIN,
191 .voltage = 1200000,
192 .pgc = PGC_USB_HSIC,
196 static int imx7_pgc_domain_probe(struct platform_device *pdev)
198 struct imx7_pgc_domain *domain = pdev->dev.platform_data;
199 int ret;
201 domain->dev = &pdev->dev;
203 domain->regulator = devm_regulator_get_optional(domain->dev, "power");
204 if (IS_ERR(domain->regulator)) {
205 if (PTR_ERR(domain->regulator) != -ENODEV) {
206 if (PTR_ERR(domain->regulator) != -EPROBE_DEFER)
207 dev_err(domain->dev, "Failed to get domain's regulator\n");
208 return PTR_ERR(domain->regulator);
210 } else {
211 regulator_set_voltage(domain->regulator,
212 domain->voltage, domain->voltage);
215 ret = pm_genpd_init(&domain->genpd, NULL, true);
216 if (ret) {
217 dev_err(domain->dev, "Failed to init power domain\n");
218 return ret;
221 ret = of_genpd_add_provider_simple(domain->dev->of_node,
222 &domain->genpd);
223 if (ret) {
224 dev_err(domain->dev, "Failed to add genpd provider\n");
225 pm_genpd_remove(&domain->genpd);
228 return ret;
231 static int imx7_pgc_domain_remove(struct platform_device *pdev)
233 struct imx7_pgc_domain *domain = pdev->dev.platform_data;
235 of_genpd_del_provider(domain->dev->of_node);
236 pm_genpd_remove(&domain->genpd);
238 return 0;
241 static const struct platform_device_id imx7_pgc_domain_id[] = {
242 { "imx7-pgc-domain", },
243 { },
246 static struct platform_driver imx7_pgc_domain_driver = {
247 .driver = {
248 .name = "imx7-pgc",
250 .probe = imx7_pgc_domain_probe,
251 .remove = imx7_pgc_domain_remove,
252 .id_table = imx7_pgc_domain_id,
254 builtin_platform_driver(imx7_pgc_domain_driver)
256 static int imx_gpcv2_probe(struct platform_device *pdev)
258 static const struct regmap_range yes_ranges[] = {
259 regmap_reg_range(GPC_LPCR_A7_BSC,
260 GPC_M4_PU_PDN_FLG),
261 regmap_reg_range(GPC_PGC_CTRL(PGC_MIPI),
262 GPC_PGC_SR(PGC_MIPI)),
263 regmap_reg_range(GPC_PGC_CTRL(PGC_PCIE),
264 GPC_PGC_SR(PGC_PCIE)),
265 regmap_reg_range(GPC_PGC_CTRL(PGC_USB_HSIC),
266 GPC_PGC_SR(PGC_USB_HSIC)),
268 static const struct regmap_access_table access_table = {
269 .yes_ranges = yes_ranges,
270 .n_yes_ranges = ARRAY_SIZE(yes_ranges),
272 static const struct regmap_config regmap_config = {
273 .reg_bits = 32,
274 .val_bits = 32,
275 .reg_stride = 4,
276 .rd_table = &access_table,
277 .wr_table = &access_table,
278 .max_register = SZ_4K,
280 struct device *dev = &pdev->dev;
281 struct device_node *pgc_np, *np;
282 struct regmap *regmap;
283 struct resource *res;
284 void __iomem *base;
285 int ret;
287 pgc_np = of_get_child_by_name(dev->of_node, "pgc");
288 if (!pgc_np) {
289 dev_err(dev, "No power domains specified in DT\n");
290 return -EINVAL;
293 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
294 base = devm_ioremap_resource(dev, res);
295 if (IS_ERR(base))
296 return PTR_ERR(base);
298 regmap = devm_regmap_init_mmio(dev, base, &regmap_config);
299 if (IS_ERR(regmap)) {
300 ret = PTR_ERR(regmap);
301 dev_err(dev, "failed to init regmap (%d)\n", ret);
302 return ret;
305 for_each_child_of_node(pgc_np, np) {
306 struct platform_device *pd_pdev;
307 struct imx7_pgc_domain *domain;
308 u32 domain_index;
310 ret = of_property_read_u32(np, "reg", &domain_index);
311 if (ret) {
312 dev_err(dev, "Failed to read 'reg' property\n");
313 of_node_put(np);
314 return ret;
317 if (domain_index >= ARRAY_SIZE(imx7_pgc_domains)) {
318 dev_warn(dev,
319 "Domain index %d is out of bounds\n",
320 domain_index);
321 continue;
324 pd_pdev = platform_device_alloc("imx7-pgc-domain",
325 domain_index);
326 if (!pd_pdev) {
327 dev_err(dev, "Failed to allocate platform device\n");
328 of_node_put(np);
329 return -ENOMEM;
332 ret = platform_device_add_data(pd_pdev,
333 &imx7_pgc_domains[domain_index],
334 sizeof(imx7_pgc_domains[domain_index]));
335 if (ret) {
336 platform_device_put(pd_pdev);
337 of_node_put(np);
338 return ret;
341 domain = pd_pdev->dev.platform_data;
342 domain->regmap = regmap;
343 domain->genpd.power_on = imx7_gpc_pu_pgc_sw_pup_req;
344 domain->genpd.power_off = imx7_gpc_pu_pgc_sw_pdn_req;
346 pd_pdev->dev.parent = dev;
347 pd_pdev->dev.of_node = np;
349 ret = platform_device_add(pd_pdev);
350 if (ret) {
351 platform_device_put(pd_pdev);
352 of_node_put(np);
353 return ret;
357 return 0;
360 static const struct of_device_id imx_gpcv2_dt_ids[] = {
361 { .compatible = "fsl,imx7d-gpc" },
365 static struct platform_driver imx_gpc_driver = {
366 .driver = {
367 .name = "imx-gpcv2",
368 .of_match_table = imx_gpcv2_dt_ids,
370 .probe = imx_gpcv2_probe,
372 builtin_platform_driver(imx_gpc_driver)