Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_POWER_DOMAIN |
| 7 | |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 11 | #include <power-domain.h> |
| 12 | #include <power-domain-uclass.h> |
Lokesh Vutla | 589eeb8 | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 14 | |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 15 | static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev) |
| 16 | { |
| 17 | return (struct power_domain_ops *)dev->driver->ops; |
| 18 | } |
| 19 | |
| 20 | static int power_domain_of_xlate_default(struct power_domain *power_domain, |
Simon Glass | 085ba47 | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 21 | struct ofnode_phandle_args *args) |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 22 | { |
| 23 | debug("%s(power_domain=%p)\n", __func__, power_domain); |
| 24 | |
| 25 | if (args->args_count != 1) { |
| 26 | debug("Invalid args_count: %d\n", args->args_count); |
| 27 | return -EINVAL; |
| 28 | } |
| 29 | |
| 30 | power_domain->id = args->args[0]; |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
Lokesh Vutla | c411c67 | 2018-08-27 15:57:44 +0530 | [diff] [blame] | 35 | int power_domain_get_by_index(struct udevice *dev, |
| 36 | struct power_domain *power_domain, int index) |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 37 | { |
Simon Glass | 085ba47 | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 38 | struct ofnode_phandle_args args; |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 39 | int ret; |
| 40 | struct udevice *dev_power_domain; |
| 41 | struct power_domain_ops *ops; |
| 42 | |
| 43 | debug("%s(dev=%p, power_domain=%p)\n", __func__, dev, power_domain); |
| 44 | |
Simon Glass | 085ba47 | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 45 | ret = dev_read_phandle_with_args(dev, "power-domains", |
Lokesh Vutla | c411c67 | 2018-08-27 15:57:44 +0530 | [diff] [blame] | 46 | "#power-domain-cells", 0, index, |
| 47 | &args); |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 48 | if (ret) { |
Simon Glass | 085ba47 | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 49 | debug("%s: dev_read_phandle_with_args failed: %d\n", |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 50 | __func__, ret); |
| 51 | return ret; |
| 52 | } |
| 53 | |
Simon Glass | 085ba47 | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 54 | ret = uclass_get_device_by_ofnode(UCLASS_POWER_DOMAIN, args.node, |
| 55 | &dev_power_domain); |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 56 | if (ret) { |
Simon Glass | 085ba47 | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 57 | debug("%s: uclass_get_device_by_ofnode failed: %d\n", |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 58 | __func__, ret); |
| 59 | return ret; |
| 60 | } |
| 61 | ops = power_domain_dev_ops(dev_power_domain); |
| 62 | |
| 63 | power_domain->dev = dev_power_domain; |
| 64 | if (ops->of_xlate) |
| 65 | ret = ops->of_xlate(power_domain, &args); |
| 66 | else |
| 67 | ret = power_domain_of_xlate_default(power_domain, &args); |
| 68 | if (ret) { |
| 69 | debug("of_xlate() failed: %d\n", ret); |
| 70 | return ret; |
| 71 | } |
| 72 | |
Marek Vasut | 4befe91 | 2022-04-13 00:42:48 +0200 | [diff] [blame] | 73 | ret = ops->request ? ops->request(power_domain) : 0; |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 74 | if (ret) { |
| 75 | debug("ops->request() failed: %d\n", ret); |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Marek Vasut | fb4e8e9 | 2022-04-13 00:42:52 +0200 | [diff] [blame] | 82 | int power_domain_get_by_name(struct udevice *dev, |
| 83 | struct power_domain *power_domain, const char *name) |
| 84 | { |
| 85 | int index; |
| 86 | |
| 87 | index = dev_read_stringlist_search(dev, "power-domain-names", name); |
| 88 | if (index < 0) { |
| 89 | debug("fdt_stringlist_search() failed: %d\n", index); |
| 90 | return index; |
| 91 | } |
| 92 | |
| 93 | return power_domain_get_by_index(dev, power_domain, index); |
| 94 | } |
| 95 | |
Lokesh Vutla | c411c67 | 2018-08-27 15:57:44 +0530 | [diff] [blame] | 96 | int power_domain_get(struct udevice *dev, struct power_domain *power_domain) |
| 97 | { |
| 98 | return power_domain_get_by_index(dev, power_domain, 0); |
| 99 | } |
| 100 | |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 101 | int power_domain_free(struct power_domain *power_domain) |
| 102 | { |
| 103 | struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev); |
| 104 | |
| 105 | debug("%s(power_domain=%p)\n", __func__, power_domain); |
| 106 | |
Marek Vasut | 4befe91 | 2022-04-13 00:42:48 +0200 | [diff] [blame] | 107 | return ops->rfree ? ops->rfree(power_domain) : 0; |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int power_domain_on(struct power_domain *power_domain) |
| 111 | { |
| 112 | struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev); |
| 113 | |
| 114 | debug("%s(power_domain=%p)\n", __func__, power_domain); |
| 115 | |
Marek Vasut | 4befe91 | 2022-04-13 00:42:48 +0200 | [diff] [blame] | 116 | return ops->on ? ops->on(power_domain) : 0; |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | int power_domain_off(struct power_domain *power_domain) |
| 120 | { |
| 121 | struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev); |
| 122 | |
| 123 | debug("%s(power_domain=%p)\n", __func__, power_domain); |
| 124 | |
Marek Vasut | 4befe91 | 2022-04-13 00:42:48 +0200 | [diff] [blame] | 125 | return ops->off ? ops->off(power_domain) : 0; |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 126 | } |
| 127 | |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 128 | #if CONFIG_IS_ENABLED(OF_REAL) |
Lokesh Vutla | 589eeb8 | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 129 | static int dev_power_domain_ctrl(struct udevice *dev, bool on) |
Peng Fan | db513e9 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 130 | { |
| 131 | struct power_domain pd; |
Lokesh Vutla | 589eeb8 | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 132 | int i, count, ret = 0; |
Peng Fan | db513e9 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 133 | |
| 134 | count = dev_count_phandle_with_args(dev, "power-domains", |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 135 | "#power-domain-cells", 0); |
Peng Fan | db513e9 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 136 | for (i = 0; i < count; i++) { |
| 137 | ret = power_domain_get_by_index(dev, &pd, i); |
| 138 | if (ret) |
| 139 | return ret; |
Lokesh Vutla | 589eeb8 | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 140 | if (on) |
| 141 | ret = power_domain_on(&pd); |
| 142 | else |
| 143 | ret = power_domain_off(&pd); |
Peng Fan | db513e9 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Lokesh Vutla | 589eeb8 | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 146 | /* |
Anatolij Gustschin | 0e3ce20 | 2020-02-17 09:42:11 +0100 | [diff] [blame] | 147 | * For platforms with parent and child power-domain devices |
| 148 | * we may not run device_remove() on the power-domain parent |
| 149 | * because it will result in removing its children and switching |
| 150 | * off their power-domain parent. So we will get here again and |
| 151 | * again and will be stuck in an endless loop. |
| 152 | */ |
Sean Anderson | e91b003 | 2022-03-23 14:26:09 -0400 | [diff] [blame] | 153 | if (count > 0 && !on && dev_get_parent(dev) == pd.dev && |
Anatolij Gustschin | 0e3ce20 | 2020-02-17 09:42:11 +0100 | [diff] [blame] | 154 | device_get_uclass_id(dev) == UCLASS_POWER_DOMAIN) |
| 155 | return ret; |
| 156 | |
| 157 | /* |
Lokesh Vutla | 589eeb8 | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 158 | * power_domain_get() bound the device, thus |
| 159 | * we must remove it again to prevent unbinding |
| 160 | * active devices (which would result in unbind |
| 161 | * error). |
| 162 | */ |
| 163 | if (count > 0 && !on) |
| 164 | device_remove(pd.dev, DM_REMOVE_NORMAL); |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | int dev_power_domain_on(struct udevice *dev) |
| 170 | { |
| 171 | return dev_power_domain_ctrl(dev, true); |
| 172 | } |
| 173 | |
| 174 | int dev_power_domain_off(struct udevice *dev) |
| 175 | { |
| 176 | return dev_power_domain_ctrl(dev, false); |
Peng Fan | db513e9 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 177 | } |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 178 | #endif /* OF_REAL */ |
Peng Fan | db513e9 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 179 | |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 180 | UCLASS_DRIVER(power_domain) = { |
| 181 | .id = UCLASS_POWER_DOMAIN, |
| 182 | .name = "power_domain", |
| 183 | }; |