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