blob: af829db9da1f3b48abca16748d01c5119e437956 [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 Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Stephen Warren92c67fa2016-07-13 13:45:31 -060010#include <power-domain.h>
11#include <power-domain-uclass.h>
Lokesh Vutla589eeb82019-09-27 13:48:14 +053012#include <dm/device-internal.h>
Stephen Warren92c67fa2016-07-13 13:45:31 -060013
Stephen Warren92c67fa2016-07-13 13:45:31 -060014static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev)
15{
16 return (struct power_domain_ops *)dev->driver->ops;
17}
18
19static int power_domain_of_xlate_default(struct power_domain *power_domain,
Simon Glass085ba472017-05-18 20:09:49 -060020 struct ofnode_phandle_args *args)
Stephen Warren92c67fa2016-07-13 13:45:31 -060021{
22 debug("%s(power_domain=%p)\n", __func__, power_domain);
23
24 if (args->args_count != 1) {
25 debug("Invalid args_count: %d\n", args->args_count);
26 return -EINVAL;
27 }
28
29 power_domain->id = args->args[0];
30
31 return 0;
32}
33
Lokesh Vutlac411c672018-08-27 15:57:44 +053034int power_domain_get_by_index(struct udevice *dev,
35 struct power_domain *power_domain, int index)
Stephen Warren92c67fa2016-07-13 13:45:31 -060036{
Simon Glass085ba472017-05-18 20:09:49 -060037 struct ofnode_phandle_args args;
Stephen Warren92c67fa2016-07-13 13:45:31 -060038 int ret;
39 struct udevice *dev_power_domain;
40 struct power_domain_ops *ops;
41
42 debug("%s(dev=%p, power_domain=%p)\n", __func__, dev, power_domain);
43
Simon Glass085ba472017-05-18 20:09:49 -060044 ret = dev_read_phandle_with_args(dev, "power-domains",
Lokesh Vutlac411c672018-08-27 15:57:44 +053045 "#power-domain-cells", 0, index,
46 &args);
Stephen Warren92c67fa2016-07-13 13:45:31 -060047 if (ret) {
Simon Glass085ba472017-05-18 20:09:49 -060048 debug("%s: dev_read_phandle_with_args failed: %d\n",
Stephen Warren92c67fa2016-07-13 13:45:31 -060049 __func__, ret);
50 return ret;
51 }
52
Simon Glass085ba472017-05-18 20:09:49 -060053 ret = uclass_get_device_by_ofnode(UCLASS_POWER_DOMAIN, args.node,
54 &dev_power_domain);
Stephen Warren92c67fa2016-07-13 13:45:31 -060055 if (ret) {
Simon Glass085ba472017-05-18 20:09:49 -060056 debug("%s: uclass_get_device_by_ofnode failed: %d\n",
Stephen Warren92c67fa2016-07-13 13:45:31 -060057 __func__, ret);
58 return ret;
59 }
60 ops = power_domain_dev_ops(dev_power_domain);
61
62 power_domain->dev = dev_power_domain;
63 if (ops->of_xlate)
64 ret = ops->of_xlate(power_domain, &args);
65 else
66 ret = power_domain_of_xlate_default(power_domain, &args);
67 if (ret) {
68 debug("of_xlate() failed: %d\n", ret);
69 return ret;
70 }
71
72 ret = ops->request(power_domain);
73 if (ret) {
74 debug("ops->request() failed: %d\n", ret);
75 return ret;
76 }
77
78 return 0;
79}
80
Lokesh Vutlac411c672018-08-27 15:57:44 +053081int power_domain_get(struct udevice *dev, struct power_domain *power_domain)
82{
83 return power_domain_get_by_index(dev, power_domain, 0);
84}
85
Stephen Warren92c67fa2016-07-13 13:45:31 -060086int power_domain_free(struct power_domain *power_domain)
87{
88 struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
89
90 debug("%s(power_domain=%p)\n", __func__, power_domain);
91
Simon Glass92ed3a32020-02-03 07:35:51 -070092 return ops->rfree(power_domain);
Stephen Warren92c67fa2016-07-13 13:45:31 -060093}
94
95int power_domain_on(struct power_domain *power_domain)
96{
97 struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
98
99 debug("%s(power_domain=%p)\n", __func__, power_domain);
100
101 return ops->on(power_domain);
102}
103
104int power_domain_off(struct power_domain *power_domain)
105{
106 struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
107
108 debug("%s(power_domain=%p)\n", __func__, power_domain);
109
110 return ops->off(power_domain);
111}
112
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530113#if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
114static int dev_power_domain_ctrl(struct udevice *dev, bool on)
Peng Fandb513e92019-09-17 09:29:19 +0000115{
116 struct power_domain pd;
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530117 int i, count, ret = 0;
Peng Fandb513e92019-09-17 09:29:19 +0000118
119 count = dev_count_phandle_with_args(dev, "power-domains",
Patrick Delaunayd776a842020-09-25 09:41:14 +0200120 "#power-domain-cells", 0);
Peng Fandb513e92019-09-17 09:29:19 +0000121 for (i = 0; i < count; i++) {
122 ret = power_domain_get_by_index(dev, &pd, i);
123 if (ret)
124 return ret;
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530125 if (on)
126 ret = power_domain_on(&pd);
127 else
128 ret = power_domain_off(&pd);
Peng Fandb513e92019-09-17 09:29:19 +0000129 }
130
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530131 /*
Anatolij Gustschin0e3ce202020-02-17 09:42:11 +0100132 * For platforms with parent and child power-domain devices
133 * we may not run device_remove() on the power-domain parent
134 * because it will result in removing its children and switching
135 * off their power-domain parent. So we will get here again and
136 * again and will be stuck in an endless loop.
137 */
138 if (!on && dev_get_parent(dev) == pd.dev &&
139 device_get_uclass_id(dev) == UCLASS_POWER_DOMAIN)
140 return ret;
141
142 /*
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530143 * power_domain_get() bound the device, thus
144 * we must remove it again to prevent unbinding
145 * active devices (which would result in unbind
146 * error).
147 */
148 if (count > 0 && !on)
149 device_remove(pd.dev, DM_REMOVE_NORMAL);
150
151 return ret;
152}
153
154int dev_power_domain_on(struct udevice *dev)
155{
156 return dev_power_domain_ctrl(dev, true);
157}
158
159int dev_power_domain_off(struct udevice *dev)
160{
161 return dev_power_domain_ctrl(dev, false);
Peng Fandb513e92019-09-17 09:29:19 +0000162}
163#endif
164
Stephen Warren92c67fa2016-07-13 13:45:31 -0600165UCLASS_DRIVER(power_domain) = {
166 .id = UCLASS_POWER_DOMAIN,
167 .name = "power_domain",
168};