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>
17 #include <linux/of_device.h>
19 struct bcm63xx_power_dev {
20 struct generic_pm_domain genpd;
21 struct bcm63xx_power *power;
25 struct bcm63xx_power {
28 struct bcm63xx_power_dev *dev;
29 struct genpd_onecell_data genpd_data;
30 struct generic_pm_domain **genpd;
33 struct bcm63xx_power_data {
34 const char * const name;
39 static int bcm63xx_power_get_state(struct bcm63xx_power_dev *pmd, bool *is_on)
41 struct bcm63xx_power *power = pmd->power;
48 *is_on = !(__raw_readl(power->base) & pmd->mask);
53 static int bcm63xx_power_set_state(struct bcm63xx_power_dev *pmd, bool on)
55 struct bcm63xx_power *power = pmd->power;
62 spin_lock_irqsave(&power->lock, flags);
63 val = __raw_readl(power->base);
68 __raw_writel(val, power->base);
69 spin_unlock_irqrestore(&power->lock, flags);
74 static int bcm63xx_power_on(struct generic_pm_domain *genpd)
76 struct bcm63xx_power_dev *pmd = container_of(genpd,
77 struct bcm63xx_power_dev, genpd);
79 return bcm63xx_power_set_state(pmd, true);
82 static int bcm63xx_power_off(struct generic_pm_domain *genpd)
84 struct bcm63xx_power_dev *pmd = container_of(genpd,
85 struct bcm63xx_power_dev, genpd);
87 return bcm63xx_power_set_state(pmd, false);
90 static int bcm63xx_power_probe(struct platform_device *pdev)
92 struct device *dev = &pdev->dev;
93 struct device_node *np = dev->of_node;
95 const struct bcm63xx_power_data *entry, *table;
96 struct bcm63xx_power *power;
101 power = devm_kzalloc(dev, sizeof(*power), GFP_KERNEL);
105 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
106 power->base = devm_ioremap_resource(&pdev->dev, res);
107 if (IS_ERR(power->base))
108 return PTR_ERR(power->base);
110 table = of_device_get_match_data(dev);
114 power->genpd_data.num_domains = 0;
116 for (entry = table; entry->name; entry++) {
117 max_bit = max(max_bit, entry->bit);
124 power->genpd_data.num_domains = max_bit + 1;
126 power->dev = devm_kcalloc(dev, power->genpd_data.num_domains,
127 sizeof(struct bcm63xx_power_dev),
132 power->genpd = devm_kcalloc(dev, power->genpd_data.num_domains,
133 sizeof(struct generic_pm_domain *),
138 power->genpd_data.domains = power->genpd;
141 for (entry = table; entry->name; entry++) {
142 struct bcm63xx_power_dev *pmd = &power->dev[ndom];
146 pmd->mask = BIT(entry->bit);
147 pmd->genpd.name = entry->name;
148 pmd->genpd.flags = entry->flags;
150 ret = bcm63xx_power_get_state(pmd, &is_on);
152 dev_warn(dev, "unable to get current state for %s\n",
155 pmd->genpd.power_on = bcm63xx_power_on;
156 pmd->genpd.power_off = bcm63xx_power_off;
158 pm_genpd_init(&pmd->genpd, NULL, !is_on);
159 power->genpd[entry->bit] = &pmd->genpd;
164 spin_lock_init(&power->lock);
166 ret = of_genpd_add_provider_onecell(np, &power->genpd_data);
168 dev_err(dev, "failed to register genpd driver: %d\n", ret);
172 dev_info(dev, "registered %u power domains\n", ndom);
177 static const struct bcm63xx_power_data bcm6318_power_domains[] = {
180 .bit = BCM6318_POWER_DOMAIN_PCIE,
183 .bit = BCM6318_POWER_DOMAIN_USB,
186 .bit = BCM6318_POWER_DOMAIN_EPHY0,
189 .bit = BCM6318_POWER_DOMAIN_EPHY1,
192 .bit = BCM6318_POWER_DOMAIN_EPHY2,
195 .bit = BCM6318_POWER_DOMAIN_EPHY3,
198 .bit = BCM6318_POWER_DOMAIN_LDO2P5,
199 .flags = GENPD_FLAG_ALWAYS_ON,
202 .bit = BCM6318_POWER_DOMAIN_LDO2P9,
203 .flags = GENPD_FLAG_ALWAYS_ON,
206 .bit = BCM6318_POWER_DOMAIN_SW1P0,
207 .flags = GENPD_FLAG_ALWAYS_ON,
210 .bit = BCM6318_POWER_DOMAIN_PAD,
211 .flags = GENPD_FLAG_ALWAYS_ON,
217 static const struct bcm63xx_power_data bcm6328_power_domains[] = {
219 .name = "adsl2-mips",
220 .bit = BCM6328_POWER_DOMAIN_ADSL2_MIPS,
223 .bit = BCM6328_POWER_DOMAIN_ADSL2_PHY,
226 .bit = BCM6328_POWER_DOMAIN_ADSL2_AFE,
229 .bit = BCM6328_POWER_DOMAIN_SAR,
232 .bit = BCM6328_POWER_DOMAIN_PCM,
235 .bit = BCM6328_POWER_DOMAIN_USBD,
238 .bit = BCM6328_POWER_DOMAIN_USBH,
241 .bit = BCM6328_POWER_DOMAIN_PCIE,
244 .bit = BCM6328_POWER_DOMAIN_ROBOSW,
247 .bit = BCM6328_POWER_DOMAIN_EPHY,
253 static const struct bcm63xx_power_data bcm6362_power_domains[] = {
256 .bit = BCM6362_POWER_DOMAIN_SAR,
259 .bit = BCM6362_POWER_DOMAIN_IPSEC,
262 .bit = BCM6362_POWER_DOMAIN_MIPS,
263 .flags = GENPD_FLAG_ALWAYS_ON,
266 .bit = BCM6362_POWER_DOMAIN_DECT,
269 .bit = BCM6362_POWER_DOMAIN_USBH,
272 .bit = BCM6362_POWER_DOMAIN_USBD,
275 .bit = BCM6362_POWER_DOMAIN_ROBOSW,
278 .bit = BCM6362_POWER_DOMAIN_PCM,
281 .bit = BCM6362_POWER_DOMAIN_PERIPH,
282 .flags = GENPD_FLAG_ALWAYS_ON,
285 .bit = BCM6362_POWER_DOMAIN_ADSL_PHY,
288 .bit = BCM6362_POWER_DOMAIN_GMII_PADS,
291 .bit = BCM6362_POWER_DOMAIN_FAP,
294 .bit = BCM6362_POWER_DOMAIN_PCIE,
297 .bit = BCM6362_POWER_DOMAIN_WLAN_PADS,
303 static const struct bcm63xx_power_data bcm63268_power_domains[] = {
306 .bit = BCM63268_POWER_DOMAIN_SAR,
309 .bit = BCM63268_POWER_DOMAIN_IPSEC,
312 .bit = BCM63268_POWER_DOMAIN_MIPS,
313 .flags = GENPD_FLAG_ALWAYS_ON,
316 .bit = BCM63268_POWER_DOMAIN_DECT,
319 .bit = BCM63268_POWER_DOMAIN_USBH,
322 .bit = BCM63268_POWER_DOMAIN_USBD,
325 .bit = BCM63268_POWER_DOMAIN_ROBOSW,
328 .bit = BCM63268_POWER_DOMAIN_PCM,
331 .bit = BCM63268_POWER_DOMAIN_PERIPH,
332 .flags = GENPD_FLAG_ALWAYS_ON,
335 .bit = BCM63268_POWER_DOMAIN_VDSL_PHY,
338 .bit = BCM63268_POWER_DOMAIN_VDSL_MIPS,
341 .bit = BCM63268_POWER_DOMAIN_FAP,
344 .bit = BCM63268_POWER_DOMAIN_PCIE,
347 .bit = BCM63268_POWER_DOMAIN_WLAN_PADS,
353 static const struct of_device_id bcm63xx_power_of_match[] = {
355 .compatible = "brcm,bcm6318-power-controller",
356 .data = &bcm6318_power_domains,
358 .compatible = "brcm,bcm6328-power-controller",
359 .data = &bcm6328_power_domains,
361 .compatible = "brcm,bcm6362-power-controller",
362 .data = &bcm6362_power_domains,
364 .compatible = "brcm,bcm63268-power-controller",
365 .data = &bcm63268_power_domains,
371 static struct platform_driver bcm63xx_power_driver = {
373 .name = "bcm63xx-power-controller",
374 .of_match_table = bcm63xx_power_of_match,
376 .probe = bcm63xx_power_probe,
378 builtin_platform_driver(bcm63xx_power_driver);