blob: 2b7a1cb8fe3becceaae0d251ea85984166fdc238 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren92c67fa2016-07-13 13:45:31 -06002/*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
Stephen Warren92c67fa2016-07-13 13:45:31 -06004 */
5
6#include <common.h>
7#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07008#include <malloc.h>
Stephen Warren92c67fa2016-07-13 13:45:31 -06009#include <power-domain.h>
10#include <power-domain-uclass.h>
Lokesh Vutla589eeb82019-09-27 13:48:14 +053011#include <dm/device-internal.h>
Stephen Warren92c67fa2016-07-13 13:45:31 -060012
Stephen Warren92c67fa2016-07-13 13:45:31 -060013static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev)
14{
15 return (struct power_domain_ops *)dev->driver->ops;
16}
17
18static int power_domain_of_xlate_default(struct power_domain *power_domain,
Simon Glass085ba472017-05-18 20:09:49 -060019 struct ofnode_phandle_args *args)
Stephen Warren92c67fa2016-07-13 13:45:31 -060020{
21 debug("%s(power_domain=%p)\n", __func__, power_domain);
22
23 if (args->args_count != 1) {
24 debug("Invalid args_count: %d\n", args->args_count);
25 return -EINVAL;
26 }
27
28 power_domain->id = args->args[0];
29
30 return 0;
31}
32
Lokesh Vutlac411c672018-08-27 15:57:44 +053033int power_domain_get_by_index(struct udevice *dev,
34 struct power_domain *power_domain, int index)
Stephen Warren92c67fa2016-07-13 13:45:31 -060035{
Simon Glass085ba472017-05-18 20:09:49 -060036 struct ofnode_phandle_args args;
Stephen Warren92c67fa2016-07-13 13:45:31 -060037 int ret;
38 struct udevice *dev_power_domain;
39 struct power_domain_ops *ops;
40
41 debug("%s(dev=%p, power_domain=%p)\n", __func__, dev, power_domain);
42
Simon Glass085ba472017-05-18 20:09:49 -060043 ret = dev_read_phandle_with_args(dev, "power-domains",
Lokesh Vutlac411c672018-08-27 15:57:44 +053044 "#power-domain-cells", 0, index,
45 &args);
Stephen Warren92c67fa2016-07-13 13:45:31 -060046 if (ret) {
Simon Glass085ba472017-05-18 20:09:49 -060047 debug("%s: dev_read_phandle_with_args failed: %d\n",
Stephen Warren92c67fa2016-07-13 13:45:31 -060048 __func__, ret);
49 return ret;
50 }
51
Simon Glass085ba472017-05-18 20:09:49 -060052 ret = uclass_get_device_by_ofnode(UCLASS_POWER_DOMAIN, args.node,
53 &dev_power_domain);
Stephen Warren92c67fa2016-07-13 13:45:31 -060054 if (ret) {
Simon Glass085ba472017-05-18 20:09:49 -060055 debug("%s: uclass_get_device_by_ofnode failed: %d\n",
Stephen Warren92c67fa2016-07-13 13:45:31 -060056 __func__, ret);
57 return ret;
58 }
59 ops = power_domain_dev_ops(dev_power_domain);
60
61 power_domain->dev = dev_power_domain;
62 if (ops->of_xlate)
63 ret = ops->of_xlate(power_domain, &args);
64 else
65 ret = power_domain_of_xlate_default(power_domain, &args);
66 if (ret) {
67 debug("of_xlate() failed: %d\n", ret);
68 return ret;
69 }
70
71 ret = ops->request(power_domain);
72 if (ret) {
73 debug("ops->request() failed: %d\n", ret);
74 return ret;
75 }
76
77 return 0;
78}
79
Lokesh Vutlac411c672018-08-27 15:57:44 +053080int power_domain_get(struct udevice *dev, struct power_domain *power_domain)
81{
82 return power_domain_get_by_index(dev, power_domain, 0);
83}
84
Stephen Warren92c67fa2016-07-13 13:45:31 -060085int power_domain_free(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
Simon Glass92ed3a32020-02-03 07:35:51 -070091 return ops->rfree(power_domain);
Stephen Warren92c67fa2016-07-13 13:45:31 -060092}
93
94int power_domain_on(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->on(power_domain);
101}
102
103int power_domain_off(struct power_domain *power_domain)
104{
105 struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
106
107 debug("%s(power_domain=%p)\n", __func__, power_domain);
108
109 return ops->off(power_domain);
110}
111
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530112#if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
113static int dev_power_domain_ctrl(struct udevice *dev, bool on)
Peng Fandb513e92019-09-17 09:29:19 +0000114{
115 struct power_domain pd;
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530116 int i, count, ret = 0;
Peng Fandb513e92019-09-17 09:29:19 +0000117
118 count = dev_count_phandle_with_args(dev, "power-domains",
119 "#power-domain-cells");
120 for (i = 0; i < count; i++) {
121 ret = power_domain_get_by_index(dev, &pd, i);
122 if (ret)
123 return ret;
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530124 if (on)
125 ret = power_domain_on(&pd);
126 else
127 ret = power_domain_off(&pd);
Peng Fandb513e92019-09-17 09:29:19 +0000128 }
129
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530130 /*
Anatolij Gustschin0e3ce202020-02-17 09:42:11 +0100131 * For platforms with parent and child power-domain devices
132 * we may not run device_remove() on the power-domain parent
133 * because it will result in removing its children and switching
134 * off their power-domain parent. So we will get here again and
135 * again and will be stuck in an endless loop.
136 */
137 if (!on && dev_get_parent(dev) == pd.dev &&
138 device_get_uclass_id(dev) == UCLASS_POWER_DOMAIN)
139 return ret;
140
141 /*
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530142 * power_domain_get() bound the device, thus
143 * we must remove it again to prevent unbinding
144 * active devices (which would result in unbind
145 * error).
146 */
147 if (count > 0 && !on)
148 device_remove(pd.dev, DM_REMOVE_NORMAL);
149
150 return ret;
151}
152
153int dev_power_domain_on(struct udevice *dev)
154{
155 return dev_power_domain_ctrl(dev, true);
156}
157
158int dev_power_domain_off(struct udevice *dev)
159{
160 return dev_power_domain_ctrl(dev, false);
Peng Fandb513e92019-09-17 09:29:19 +0000161}
162#endif
163
Stephen Warren92c67fa2016-07-13 13:45:31 -0600164UCLASS_DRIVER(power_domain) = {
165 .id = UCLASS_POWER_DOMAIN,
166 .name = "power_domain",
167};