1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * BCM63xx Power Domain Controller Driver
5 * Copyright (C) 2020 Álvaro Fernández Rojas <noltari@gmail.com>
8 #include <dt-bindings/soc/bcm6318-pm.h>
9 #include <dt-bindings/soc/bcm6328-pm.h>
10 #include <dt-bindings/soc/bcm6362-pm.h>
11 #include <dt-bindings/soc/bcm63268-pm.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_domain.h>
18 struct bcm63xx_power_dev {
19 struct generic_pm_domain genpd;
20 struct bcm63xx_power *power;
24 struct bcm63xx_power {
27 struct bcm63xx_power_dev *dev;
28 struct genpd_onecell_data genpd_data;
29 struct generic_pm_domain **genpd;
32 struct bcm63xx_power_data {
33 const char * const name;
38 static int bcm63xx_power_get_state(struct bcm63xx_power_dev *pmd, bool *is_on)
40 struct bcm63xx_power *power = pmd->power;
47 *is_on = !(__raw_readl(power->base) & pmd->mask);
52 static int bcm63xx_power_set_state(struct bcm63xx_power_dev *pmd, bool on)
54 struct bcm63xx_power *power = pmd->power;
61 spin_lock_irqsave(&power->lock, flags);
62 val = __raw_readl(power->base);
67 __raw_writel(val, power->base);
68 spin_unlock_irqrestore(&power->lock, flags);
73 static int bcm63xx_power_on(struct generic_pm_domain *genpd)
75 struct bcm63xx_power_dev *pmd = container_of(genpd,
76 struct bcm63xx_power_dev, genpd);
78 return bcm63xx_power_set_state(pmd, true);
81 static int bcm63xx_power_off(struct generic_pm_domain *genpd)
83 struct bcm63xx_power_dev *pmd = container_of(genpd,
84 struct bcm63xx_power_dev, genpd);
86 return bcm63xx_power_set_state(pmd, false);
89 static int bcm63xx_power_probe(struct platform_device *pdev)
91 struct device *dev = &pdev->dev;
92 struct device_node *np = dev->of_node;
93 const struct bcm63xx_power_data *entry, *table;
94 struct bcm63xx_power *power;
99 power = devm_kzalloc(dev, sizeof(*power), GFP_KERNEL);
103 power->base = devm_platform_ioremap_resource(pdev, 0);
104 if (IS_ERR(power->base))
105 return PTR_ERR(power->base);
107 table = of_device_get_match_data(dev);
111 power->genpd_data.num_domains = 0;
113 for (entry = table; entry->name; entry++) {
114 max_bit = max(max_bit, entry->bit);
121 power->genpd_data.num_domains = max_bit + 1;
123 power->dev = devm_kcalloc(dev, power->genpd_data.num_domains,
124 sizeof(struct bcm63xx_power_dev),
129 power->genpd = devm_kcalloc(dev, power->genpd_data.num_domains,
130 sizeof(struct generic_pm_domain *),
135 power->genpd_data.domains = power->genpd;
138 for (entry = table; entry->name; entry++) {
139 struct bcm63xx_power_dev *pmd = &power->dev[ndom];
143 pmd->mask = BIT(entry->bit);
144 pmd->genpd.name = entry->name;
145 pmd->genpd.flags = entry->flags;
147 ret = bcm63xx_power_get_state(pmd, &is_on);
149 dev_warn(dev, "unable to get current state for %s\n",
152 pmd->genpd.power_on = bcm63xx_power_on;
153 pmd->genpd.power_off = bcm63xx_power_off;
155 pm_genpd_init(&pmd->genpd, NULL, !is_on);
156 power->genpd[entry->bit] = &pmd->genpd;
161 spin_lock_init(&power->lock);
163 ret = of_genpd_add_provider_onecell(np, &power->genpd_data);
165 dev_err(dev, "failed to register genpd driver: %d\n", ret);
169 dev_info(dev, "registered %u power domains\n", ndom);
174 static const struct bcm63xx_power_data bcm6318_power_domains[] = {
177 .bit = BCM6318_POWER_DOMAIN_PCIE,
180 .bit = BCM6318_POWER_DOMAIN_USB,
183 .bit = BCM6318_POWER_DOMAIN_EPHY0,
186 .bit = BCM6318_POWER_DOMAIN_EPHY1,
189 .bit = BCM6318_POWER_DOMAIN_EPHY2,
192 .bit = BCM6318_POWER_DOMAIN_EPHY3,
195 .bit = BCM6318_POWER_DOMAIN_LDO2P5,
196 .flags = GENPD_FLAG_ALWAYS_ON,
199 .bit = BCM6318_POWER_DOMAIN_LDO2P9,
200 .flags = GENPD_FLAG_ALWAYS_ON,
203 .bit = BCM6318_POWER_DOMAIN_SW1P0,
204 .flags = GENPD_FLAG_ALWAYS_ON,
207 .bit = BCM6318_POWER_DOMAIN_PAD,
208 .flags = GENPD_FLAG_ALWAYS_ON,
214 static const struct bcm63xx_power_data bcm6328_power_domains[] = {
216 .name = "adsl2-mips",
217 .bit = BCM6328_POWER_DOMAIN_ADSL2_MIPS,
220 .bit = BCM6328_POWER_DOMAIN_ADSL2_PHY,
223 .bit = BCM6328_POWER_DOMAIN_ADSL2_AFE,
226 .bit = BCM6328_POWER_DOMAIN_SAR,
229 .bit = BCM6328_POWER_DOMAIN_PCM,
232 .bit = BCM6328_POWER_DOMAIN_USBD,
235 .bit = BCM6328_POWER_DOMAIN_USBH,
238 .bit = BCM6328_POWER_DOMAIN_PCIE,
241 .bit = BCM6328_POWER_DOMAIN_ROBOSW,
244 .bit = BCM6328_POWER_DOMAIN_EPHY,
250 static const struct bcm63xx_power_data bcm6362_power_domains[] = {
253 .bit = BCM6362_POWER_DOMAIN_SAR,
256 .bit = BCM6362_POWER_DOMAIN_IPSEC,
259 .bit = BCM6362_POWER_DOMAIN_MIPS,
260 .flags = GENPD_FLAG_ALWAYS_ON,
263 .bit = BCM6362_POWER_DOMAIN_DECT,
266 .bit = BCM6362_POWER_DOMAIN_USBH,
269 .bit = BCM6362_POWER_DOMAIN_USBD,
272 .bit = BCM6362_POWER_DOMAIN_ROBOSW,
275 .bit = BCM6362_POWER_DOMAIN_PCM,
278 .bit = BCM6362_POWER_DOMAIN_PERIPH,
279 .flags = GENPD_FLAG_ALWAYS_ON,
282 .bit = BCM6362_POWER_DOMAIN_ADSL_PHY,
285 .bit = BCM6362_POWER_DOMAIN_GMII_PADS,
288 .bit = BCM6362_POWER_DOMAIN_FAP,
291 .bit = BCM6362_POWER_DOMAIN_PCIE,
294 .bit = BCM6362_POWER_DOMAIN_WLAN_PADS,
300 static const struct bcm63xx_power_data bcm63268_power_domains[] = {
303 .bit = BCM63268_POWER_DOMAIN_SAR,
306 .bit = BCM63268_POWER_DOMAIN_IPSEC,
309 .bit = BCM63268_POWER_DOMAIN_MIPS,
310 .flags = GENPD_FLAG_ALWAYS_ON,
313 .bit = BCM63268_POWER_DOMAIN_DECT,
316 .bit = BCM63268_POWER_DOMAIN_USBH,
319 .bit = BCM63268_POWER_DOMAIN_USBD,
322 .bit = BCM63268_POWER_DOMAIN_ROBOSW,
325 .bit = BCM63268_POWER_DOMAIN_PCM,
328 .bit = BCM63268_POWER_DOMAIN_PERIPH,
329 .flags = GENPD_FLAG_ALWAYS_ON,
332 .bit = BCM63268_POWER_DOMAIN_VDSL_PHY,
335 .bit = BCM63268_POWER_DOMAIN_VDSL_MIPS,
338 .bit = BCM63268_POWER_DOMAIN_FAP,
341 .bit = BCM63268_POWER_DOMAIN_PCIE,
344 .bit = BCM63268_POWER_DOMAIN_WLAN_PADS,
350 static const struct of_device_id bcm63xx_power_of_match[] = {
352 .compatible = "brcm,bcm6318-power-controller",
353 .data = &bcm6318_power_domains,
355 .compatible = "brcm,bcm6328-power-controller",
356 .data = &bcm6328_power_domains,
358 .compatible = "brcm,bcm6362-power-controller",
359 .data = &bcm6362_power_domains,
361 .compatible = "brcm,bcm63268-power-controller",
362 .data = &bcm63268_power_domains,
368 static struct platform_driver bcm63xx_power_driver = {
370 .name = "bcm63xx-power-controller",
371 .of_match_table = bcm63xx_power_of_match,
373 .probe = bcm63xx_power_probe,
375 builtin_platform_driver(bcm63xx_power_driver);