1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <power-domain-uclass.h>
9 #include <asm/io.h>
10 
11 #define MAX_DOMAINS	32
12 
13 struct bcm6328_power_domain {
14 	void __iomem *regs;
15 };
16 
bcm6328_power_domain_request(struct power_domain * power_domain)17 static int bcm6328_power_domain_request(struct power_domain *power_domain)
18 {
19 	if (power_domain->id >= MAX_DOMAINS)
20 		return -EINVAL;
21 
22 	return 0;
23 }
24 
bcm6328_power_domain_free(struct power_domain * power_domain)25 static int bcm6328_power_domain_free(struct power_domain *power_domain)
26 {
27 	return 0;
28 }
29 
bcm6328_power_domain_on(struct power_domain * power_domain)30 static int bcm6328_power_domain_on(struct power_domain *power_domain)
31 {
32 	struct bcm6328_power_domain *priv = dev_get_priv(power_domain->dev);
33 
34 	clrbits_be32(priv->regs, BIT(power_domain->id));
35 
36 	return 0;
37 }
38 
bcm6328_power_domain_off(struct power_domain * power_domain)39 static int bcm6328_power_domain_off(struct power_domain *power_domain)
40 {
41 	struct bcm6328_power_domain *priv = dev_get_priv(power_domain->dev);
42 
43 	setbits_be32(priv->regs, BIT(power_domain->id));
44 
45 	return 0;
46 }
47 
bcm6328_power_domain_probe(struct udevice * dev)48 static int bcm6328_power_domain_probe(struct udevice *dev)
49 {
50 	struct bcm6328_power_domain *priv = dev_get_priv(dev);
51 
52 	priv->regs = dev_remap_addr(dev);
53 	if (!priv->regs)
54 		return -EINVAL;
55 
56 	return 0;
57 }
58 
59 static const struct udevice_id bcm6328_power_domain_ids[] = {
60 	{ .compatible = "brcm,bcm6328-power-domain" },
61 	{ /* sentinel */ }
62 };
63 
64 struct power_domain_ops bcm6328_power_domain_ops = {
65 	.free = bcm6328_power_domain_free,
66 	.off = bcm6328_power_domain_off,
67 	.on = bcm6328_power_domain_on,
68 	.request = bcm6328_power_domain_request,
69 };
70 
71 U_BOOT_DRIVER(bcm6328_power_domain) = {
72 	.name = "bcm6328_power_domain",
73 	.id = UCLASS_POWER_DOMAIN,
74 	.of_match = bcm6328_power_domain_ids,
75 	.ops = &bcm6328_power_domain_ops,
76 	.priv_auto_alloc_size = sizeof(struct bcm6328_power_domain),
77 	.probe = bcm6328_power_domain_probe,
78 };
79