Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | b953ec2 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_POWER_DOMAIN |
| 7 | |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 12 | #include <power-domain.h> |
| 13 | #include <power-domain-uclass.h> |
Lokesh Vutla | 0cf795a | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 15 | |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 16 | static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev) |
| 17 | { |
| 18 | return (struct power_domain_ops *)dev->driver->ops; |
| 19 | } |
| 20 | |
| 21 | static int power_domain_of_xlate_default(struct power_domain *power_domain, |
Simon Glass | 424b2fe | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 22 | struct ofnode_phandle_args *args) |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 23 | { |
| 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 Vutla | 2618cf3 | 2018-08-27 15:57:44 +0530 | [diff] [blame] | 36 | int power_domain_get_by_index(struct udevice *dev, |
| 37 | struct power_domain *power_domain, int index) |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 38 | { |
Simon Glass | 424b2fe | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 39 | struct ofnode_phandle_args args; |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 40 | 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 Glass | 424b2fe | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 46 | ret = dev_read_phandle_with_args(dev, "power-domains", |
Lokesh Vutla | 2618cf3 | 2018-08-27 15:57:44 +0530 | [diff] [blame] | 47 | "#power-domain-cells", 0, index, |
| 48 | &args); |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 49 | if (ret) { |
Simon Glass | 424b2fe | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 50 | debug("%s: dev_read_phandle_with_args failed: %d\n", |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 51 | __func__, ret); |
| 52 | return ret; |
| 53 | } |
| 54 | |
Simon Glass | 424b2fe | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 55 | ret = uclass_get_device_by_ofnode(UCLASS_POWER_DOMAIN, args.node, |
| 56 | &dev_power_domain); |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 57 | if (ret) { |
Simon Glass | 424b2fe | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 58 | debug("%s: uclass_get_device_by_ofnode failed: %d\n", |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 59 | __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 | |
| 74 | ret = ops->request(power_domain); |
| 75 | if (ret) { |
| 76 | debug("ops->request() failed: %d\n", ret); |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
Lokesh Vutla | 2618cf3 | 2018-08-27 15:57:44 +0530 | [diff] [blame] | 83 | int power_domain_get(struct udevice *dev, struct power_domain *power_domain) |
| 84 | { |
| 85 | return power_domain_get_by_index(dev, power_domain, 0); |
| 86 | } |
| 87 | |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 88 | int power_domain_free(struct power_domain *power_domain) |
| 89 | { |
| 90 | struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev); |
| 91 | |
| 92 | debug("%s(power_domain=%p)\n", __func__, power_domain); |
| 93 | |
Simon Glass | 4f51188 | 2020-02-03 07:35:51 -0700 | [diff] [blame] | 94 | return ops->rfree(power_domain); |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | int power_domain_on(struct power_domain *power_domain) |
| 98 | { |
| 99 | struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev); |
| 100 | |
| 101 | debug("%s(power_domain=%p)\n", __func__, power_domain); |
| 102 | |
| 103 | return ops->on(power_domain); |
| 104 | } |
| 105 | |
| 106 | int power_domain_off(struct power_domain *power_domain) |
| 107 | { |
| 108 | struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev); |
| 109 | |
| 110 | debug("%s(power_domain=%p)\n", __func__, power_domain); |
| 111 | |
| 112 | return ops->off(power_domain); |
| 113 | } |
| 114 | |
Simon Glass | 414cc15 | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 115 | #if CONFIG_IS_ENABLED(OF_REAL) |
Lokesh Vutla | 0cf795a | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 116 | static int dev_power_domain_ctrl(struct udevice *dev, bool on) |
Peng Fan | 9c1e982 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 117 | { |
| 118 | struct power_domain pd; |
Lokesh Vutla | 0cf795a | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 119 | int i, count, ret = 0; |
Peng Fan | 9c1e982 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 120 | |
| 121 | count = dev_count_phandle_with_args(dev, "power-domains", |
Patrick Delaunay | 89f6830 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 122 | "#power-domain-cells", 0); |
Peng Fan | 9c1e982 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 123 | for (i = 0; i < count; i++) { |
| 124 | ret = power_domain_get_by_index(dev, &pd, i); |
| 125 | if (ret) |
| 126 | return ret; |
Lokesh Vutla | 0cf795a | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 127 | if (on) |
| 128 | ret = power_domain_on(&pd); |
| 129 | else |
| 130 | ret = power_domain_off(&pd); |
Peng Fan | 9c1e982 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Lokesh Vutla | 0cf795a | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 133 | /* |
Anatolij Gustschin | 3e4fcfa | 2020-02-17 09:42:11 +0100 | [diff] [blame] | 134 | * For platforms with parent and child power-domain devices |
| 135 | * we may not run device_remove() on the power-domain parent |
| 136 | * because it will result in removing its children and switching |
| 137 | * off their power-domain parent. So we will get here again and |
| 138 | * again and will be stuck in an endless loop. |
| 139 | */ |
| 140 | if (!on && dev_get_parent(dev) == pd.dev && |
| 141 | device_get_uclass_id(dev) == UCLASS_POWER_DOMAIN) |
| 142 | return ret; |
| 143 | |
| 144 | /* |
Lokesh Vutla | 0cf795a | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 145 | * power_domain_get() bound the device, thus |
| 146 | * we must remove it again to prevent unbinding |
| 147 | * active devices (which would result in unbind |
| 148 | * error). |
| 149 | */ |
| 150 | if (count > 0 && !on) |
| 151 | device_remove(pd.dev, DM_REMOVE_NORMAL); |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | int dev_power_domain_on(struct udevice *dev) |
| 157 | { |
| 158 | return dev_power_domain_ctrl(dev, true); |
| 159 | } |
| 160 | |
| 161 | int dev_power_domain_off(struct udevice *dev) |
| 162 | { |
| 163 | return dev_power_domain_ctrl(dev, false); |
Peng Fan | 9c1e982 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 164 | } |
Simon Glass | 414cc15 | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 165 | #endif /* OF_REAL */ |
Peng Fan | 9c1e982 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 166 | |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 167 | UCLASS_DRIVER(power_domain) = { |
| 168 | .id = UCLASS_POWER_DOMAIN, |
| 169 | .name = "power_domain", |
| 170 | }; |