blob: f6286c70c1d796173c3fda61c0e71a91526787f0 [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
Patrick Delaunay81313352021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_POWER_DOMAIN
7
Stephen Warren92c67fa2016-07-13 13:45:31 -06008#include <common.h>
9#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Stephen Warren92c67fa2016-07-13 13:45:31 -060012#include <power-domain.h>
13#include <power-domain-uclass.h>
Lokesh Vutla589eeb82019-09-27 13:48:14 +053014#include <dm/device-internal.h>
Stephen Warren92c67fa2016-07-13 13:45:31 -060015
Stephen Warren92c67fa2016-07-13 13:45:31 -060016static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev)
17{
18 return (struct power_domain_ops *)dev->driver->ops;
19}
20
21static int power_domain_of_xlate_default(struct power_domain *power_domain,
Simon Glass085ba472017-05-18 20:09:49 -060022 struct ofnode_phandle_args *args)
Stephen Warren92c67fa2016-07-13 13:45:31 -060023{
24 debug("%s(power_domain=%p)\n", __func__, power_domain);
25
26 if (args->args_count != 1) {
27 debug("Invalid args_count: %d\n", args->args_count);
28 return -EINVAL;
29 }
30
31 power_domain->id = args->args[0];
32
33 return 0;
34}
35
Lokesh Vutlac411c672018-08-27 15:57:44 +053036int power_domain_get_by_index(struct udevice *dev,
37 struct power_domain *power_domain, int index)
Stephen Warren92c67fa2016-07-13 13:45:31 -060038{
Simon Glass085ba472017-05-18 20:09:49 -060039 struct ofnode_phandle_args args;
Stephen Warren92c67fa2016-07-13 13:45:31 -060040 int ret;
41 struct udevice *dev_power_domain;
42 struct power_domain_ops *ops;
43
44 debug("%s(dev=%p, power_domain=%p)\n", __func__, dev, power_domain);
45
Simon Glass085ba472017-05-18 20:09:49 -060046 ret = dev_read_phandle_with_args(dev, "power-domains",
Lokesh Vutlac411c672018-08-27 15:57:44 +053047 "#power-domain-cells", 0, index,
48 &args);
Stephen Warren92c67fa2016-07-13 13:45:31 -060049 if (ret) {
Simon Glass085ba472017-05-18 20:09:49 -060050 debug("%s: dev_read_phandle_with_args failed: %d\n",
Stephen Warren92c67fa2016-07-13 13:45:31 -060051 __func__, ret);
52 return ret;
53 }
54
Simon Glass085ba472017-05-18 20:09:49 -060055 ret = uclass_get_device_by_ofnode(UCLASS_POWER_DOMAIN, args.node,
56 &dev_power_domain);
Stephen Warren92c67fa2016-07-13 13:45:31 -060057 if (ret) {
Simon Glass085ba472017-05-18 20:09:49 -060058 debug("%s: uclass_get_device_by_ofnode failed: %d\n",
Stephen Warren92c67fa2016-07-13 13:45:31 -060059 __func__, ret);
60 return ret;
61 }
62 ops = power_domain_dev_ops(dev_power_domain);
63
64 power_domain->dev = dev_power_domain;
65 if (ops->of_xlate)
66 ret = ops->of_xlate(power_domain, &args);
67 else
68 ret = power_domain_of_xlate_default(power_domain, &args);
69 if (ret) {
70 debug("of_xlate() failed: %d\n", ret);
71 return ret;
72 }
73
Marek Vasut4befe912022-04-13 00:42:48 +020074 ret = ops->request ? ops->request(power_domain) : 0;
Stephen Warren92c67fa2016-07-13 13:45:31 -060075 if (ret) {
76 debug("ops->request() failed: %d\n", ret);
77 return ret;
78 }
79
80 return 0;
81}
82
Marek Vasutfb4e8e92022-04-13 00:42:52 +020083int power_domain_get_by_name(struct udevice *dev,
84 struct power_domain *power_domain, const char *name)
85{
86 int index;
87
88 index = dev_read_stringlist_search(dev, "power-domain-names", name);
89 if (index < 0) {
90 debug("fdt_stringlist_search() failed: %d\n", index);
91 return index;
92 }
93
94 return power_domain_get_by_index(dev, power_domain, index);
95}
96
Lokesh Vutlac411c672018-08-27 15:57:44 +053097int power_domain_get(struct udevice *dev, struct power_domain *power_domain)
98{
99 return power_domain_get_by_index(dev, power_domain, 0);
100}
101
Stephen Warren92c67fa2016-07-13 13:45:31 -0600102int power_domain_free(struct power_domain *power_domain)
103{
104 struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
105
106 debug("%s(power_domain=%p)\n", __func__, power_domain);
107
Marek Vasut4befe912022-04-13 00:42:48 +0200108 return ops->rfree ? ops->rfree(power_domain) : 0;
Stephen Warren92c67fa2016-07-13 13:45:31 -0600109}
110
111int power_domain_on(struct power_domain *power_domain)
112{
113 struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
114
115 debug("%s(power_domain=%p)\n", __func__, power_domain);
116
Marek Vasut4befe912022-04-13 00:42:48 +0200117 return ops->on ? ops->on(power_domain) : 0;
Stephen Warren92c67fa2016-07-13 13:45:31 -0600118}
119
120int power_domain_off(struct power_domain *power_domain)
121{
122 struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
123
124 debug("%s(power_domain=%p)\n", __func__, power_domain);
125
Marek Vasut4befe912022-04-13 00:42:48 +0200126 return ops->off ? ops->off(power_domain) : 0;
Stephen Warren92c67fa2016-07-13 13:45:31 -0600127}
128
Simon Glass3580f6d2021-08-07 07:24:03 -0600129#if CONFIG_IS_ENABLED(OF_REAL)
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530130static int dev_power_domain_ctrl(struct udevice *dev, bool on)
Peng Fandb513e92019-09-17 09:29:19 +0000131{
132 struct power_domain pd;
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530133 int i, count, ret = 0;
Peng Fandb513e92019-09-17 09:29:19 +0000134
135 count = dev_count_phandle_with_args(dev, "power-domains",
Patrick Delaunayd776a842020-09-25 09:41:14 +0200136 "#power-domain-cells", 0);
Peng Fandb513e92019-09-17 09:29:19 +0000137 for (i = 0; i < count; i++) {
138 ret = power_domain_get_by_index(dev, &pd, i);
139 if (ret)
140 return ret;
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530141 if (on)
142 ret = power_domain_on(&pd);
143 else
144 ret = power_domain_off(&pd);
Peng Fandb513e92019-09-17 09:29:19 +0000145 }
146
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530147 /*
Anatolij Gustschin0e3ce202020-02-17 09:42:11 +0100148 * For platforms with parent and child power-domain devices
149 * we may not run device_remove() on the power-domain parent
150 * because it will result in removing its children and switching
151 * off their power-domain parent. So we will get here again and
152 * again and will be stuck in an endless loop.
153 */
Sean Andersone91b0032022-03-23 14:26:09 -0400154 if (count > 0 && !on && dev_get_parent(dev) == pd.dev &&
Anatolij Gustschin0e3ce202020-02-17 09:42:11 +0100155 device_get_uclass_id(dev) == UCLASS_POWER_DOMAIN)
156 return ret;
157
158 /*
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530159 * power_domain_get() bound the device, thus
160 * we must remove it again to prevent unbinding
161 * active devices (which would result in unbind
162 * error).
163 */
164 if (count > 0 && !on)
165 device_remove(pd.dev, DM_REMOVE_NORMAL);
166
167 return ret;
168}
169
170int dev_power_domain_on(struct udevice *dev)
171{
172 return dev_power_domain_ctrl(dev, true);
173}
174
175int dev_power_domain_off(struct udevice *dev)
176{
177 return dev_power_domain_ctrl(dev, false);
Peng Fandb513e92019-09-17 09:29:19 +0000178}
Simon Glass3580f6d2021-08-07 07:24:03 -0600179#endif /* OF_REAL */
Peng Fandb513e92019-09-17 09:29:19 +0000180
Stephen Warren92c67fa2016-07-13 13:45:31 -0600181UCLASS_DRIVER(power_domain) = {
182 .id = UCLASS_POWER_DOMAIN,
183 .name = "power_domain",
184};