1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016, NVIDIA CORPORATION.
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <power-domain.h>
9 #include <power-domain-uclass.h>
10 
11 static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev)
12 {
13 	return (struct power_domain_ops *)dev->driver->ops;
14 }
15 
16 static int power_domain_of_xlate_default(struct power_domain *power_domain,
17 					 struct ofnode_phandle_args *args)
18 {
19 	debug("%s(power_domain=%p)\n", __func__, power_domain);
20 
21 	if (args->args_count != 1) {
22 		debug("Invalid args_count: %d\n", args->args_count);
23 		return -EINVAL;
24 	}
25 
26 	power_domain->id = args->args[0];
27 
28 	return 0;
29 }
30 
31 int power_domain_get(struct udevice *dev, struct power_domain *power_domain)
32 {
33 	struct ofnode_phandle_args args;
34 	int ret;
35 	struct udevice *dev_power_domain;
36 	struct power_domain_ops *ops;
37 
38 	debug("%s(dev=%p, power_domain=%p)\n", __func__, dev, power_domain);
39 
40 	ret = dev_read_phandle_with_args(dev, "power-domains",
41 					 "#power-domain-cells", 0, 0, &args);
42 	if (ret) {
43 		debug("%s: dev_read_phandle_with_args failed: %d\n",
44 		      __func__, ret);
45 		return ret;
46 	}
47 
48 	ret = uclass_get_device_by_ofnode(UCLASS_POWER_DOMAIN, args.node,
49 					  &dev_power_domain);
50 	if (ret) {
51 		debug("%s: uclass_get_device_by_ofnode failed: %d\n",
52 		      __func__, ret);
53 		return ret;
54 	}
55 	ops = power_domain_dev_ops(dev_power_domain);
56 
57 	power_domain->dev = dev_power_domain;
58 	if (ops->of_xlate)
59 		ret = ops->of_xlate(power_domain, &args);
60 	else
61 		ret = power_domain_of_xlate_default(power_domain, &args);
62 	if (ret) {
63 		debug("of_xlate() failed: %d\n", ret);
64 		return ret;
65 	}
66 
67 	ret = ops->request(power_domain);
68 	if (ret) {
69 		debug("ops->request() failed: %d\n", ret);
70 		return ret;
71 	}
72 
73 	return 0;
74 }
75 
76 int power_domain_free(struct power_domain *power_domain)
77 {
78 	struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
79 
80 	debug("%s(power_domain=%p)\n", __func__, power_domain);
81 
82 	return ops->free(power_domain);
83 }
84 
85 int power_domain_on(struct power_domain *power_domain)
86 {
87 	struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
88 
89 	debug("%s(power_domain=%p)\n", __func__, power_domain);
90 
91 	return ops->on(power_domain);
92 }
93 
94 int power_domain_off(struct power_domain *power_domain)
95 {
96 	struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
97 
98 	debug("%s(power_domain=%p)\n", __func__, power_domain);
99 
100 	return ops->off(power_domain);
101 }
102 
103 UCLASS_DRIVER(power_domain) = {
104 	.id		= UCLASS_POWER_DOMAIN,
105 	.name		= "power_domain",
106 };
107