blob: 80df5aff5018b1280e5e1c3ffacd1aacd99a4b3f [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>
Stephen Warren92c67fa2016-07-13 13:45:31 -06008#include <power-domain.h>
9#include <power-domain-uclass.h>
Lokesh Vutla589eeb82019-09-27 13:48:14 +053010#include <dm/device-internal.h>
Stephen Warren92c67fa2016-07-13 13:45:31 -060011
Stephen Warren92c67fa2016-07-13 13:45:31 -060012static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev)
13{
14 return (struct power_domain_ops *)dev->driver->ops;
15}
16
17static int power_domain_of_xlate_default(struct power_domain *power_domain,
Simon Glass085ba472017-05-18 20:09:49 -060018 struct ofnode_phandle_args *args)
Stephen Warren92c67fa2016-07-13 13:45:31 -060019{
20 debug("%s(power_domain=%p)\n", __func__, power_domain);
21
22 if (args->args_count != 1) {
23 debug("Invalid args_count: %d\n", args->args_count);
24 return -EINVAL;
25 }
26
27 power_domain->id = args->args[0];
28
29 return 0;
30}
31
Lokesh Vutlac411c672018-08-27 15:57:44 +053032int power_domain_get_by_index(struct udevice *dev,
33 struct power_domain *power_domain, int index)
Stephen Warren92c67fa2016-07-13 13:45:31 -060034{
Simon Glass085ba472017-05-18 20:09:49 -060035 struct ofnode_phandle_args args;
Stephen Warren92c67fa2016-07-13 13:45:31 -060036 int ret;
37 struct udevice *dev_power_domain;
38 struct power_domain_ops *ops;
39
40 debug("%s(dev=%p, power_domain=%p)\n", __func__, dev, power_domain);
41
Simon Glass085ba472017-05-18 20:09:49 -060042 ret = dev_read_phandle_with_args(dev, "power-domains",
Lokesh Vutlac411c672018-08-27 15:57:44 +053043 "#power-domain-cells", 0, index,
44 &args);
Stephen Warren92c67fa2016-07-13 13:45:31 -060045 if (ret) {
Simon Glass085ba472017-05-18 20:09:49 -060046 debug("%s: dev_read_phandle_with_args failed: %d\n",
Stephen Warren92c67fa2016-07-13 13:45:31 -060047 __func__, ret);
48 return ret;
49 }
50
Simon Glass085ba472017-05-18 20:09:49 -060051 ret = uclass_get_device_by_ofnode(UCLASS_POWER_DOMAIN, args.node,
52 &dev_power_domain);
Stephen Warren92c67fa2016-07-13 13:45:31 -060053 if (ret) {
Simon Glass085ba472017-05-18 20:09:49 -060054 debug("%s: uclass_get_device_by_ofnode failed: %d\n",
Stephen Warren92c67fa2016-07-13 13:45:31 -060055 __func__, ret);
56 return ret;
57 }
58 ops = power_domain_dev_ops(dev_power_domain);
59
60 power_domain->dev = dev_power_domain;
61 if (ops->of_xlate)
62 ret = ops->of_xlate(power_domain, &args);
63 else
64 ret = power_domain_of_xlate_default(power_domain, &args);
65 if (ret) {
66 debug("of_xlate() failed: %d\n", ret);
67 return ret;
68 }
69
70 ret = ops->request(power_domain);
71 if (ret) {
72 debug("ops->request() failed: %d\n", ret);
73 return ret;
74 }
75
76 return 0;
77}
78
Lokesh Vutlac411c672018-08-27 15:57:44 +053079int power_domain_get(struct udevice *dev, struct power_domain *power_domain)
80{
81 return power_domain_get_by_index(dev, power_domain, 0);
82}
83
Stephen Warren92c67fa2016-07-13 13:45:31 -060084int power_domain_free(struct power_domain *power_domain)
85{
86 struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
87
88 debug("%s(power_domain=%p)\n", __func__, power_domain);
89
90 return ops->free(power_domain);
91}
92
93int power_domain_on(struct power_domain *power_domain)
94{
95 struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
96
97 debug("%s(power_domain=%p)\n", __func__, power_domain);
98
99 return ops->on(power_domain);
100}
101
102int power_domain_off(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
108 return ops->off(power_domain);
109}
110
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530111#if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
112static int dev_power_domain_ctrl(struct udevice *dev, bool on)
Peng Fandb513e92019-09-17 09:29:19 +0000113{
114 struct power_domain pd;
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530115 int i, count, ret = 0;
Peng Fandb513e92019-09-17 09:29:19 +0000116
117 count = dev_count_phandle_with_args(dev, "power-domains",
118 "#power-domain-cells");
119 for (i = 0; i < count; i++) {
120 ret = power_domain_get_by_index(dev, &pd, i);
121 if (ret)
122 return ret;
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530123 if (on)
124 ret = power_domain_on(&pd);
125 else
126 ret = power_domain_off(&pd);
Peng Fandb513e92019-09-17 09:29:19 +0000127 }
128
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530129 /*
130 * power_domain_get() bound the device, thus
131 * we must remove it again to prevent unbinding
132 * active devices (which would result in unbind
133 * error).
134 */
135 if (count > 0 && !on)
136 device_remove(pd.dev, DM_REMOVE_NORMAL);
137
138 return ret;
139}
140
141int dev_power_domain_on(struct udevice *dev)
142{
143 return dev_power_domain_ctrl(dev, true);
144}
145
146int dev_power_domain_off(struct udevice *dev)
147{
148 return dev_power_domain_ctrl(dev, false);
Peng Fandb513e92019-09-17 09:29:19 +0000149}
150#endif
151
Stephen Warren92c67fa2016-07-13 13:45:31 -0600152UCLASS_DRIVER(power_domain) = {
153 .id = UCLASS_POWER_DOMAIN,
154 .name = "power_domain",
155};