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 | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 10 | #include <power-domain.h> |
| 11 | #include <power-domain-uclass.h> |
Lokesh Vutla | 589eeb8 | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 12 | #include <dm/device-internal.h> |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 13 | |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 14 | static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev) |
| 15 | { |
| 16 | return (struct power_domain_ops *)dev->driver->ops; |
| 17 | } |
| 18 | |
| 19 | static int power_domain_of_xlate_default(struct power_domain *power_domain, |
Simon Glass | 085ba47 | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 20 | struct ofnode_phandle_args *args) |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 21 | { |
| 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 Vutla | c411c67 | 2018-08-27 15:57:44 +0530 | [diff] [blame] | 34 | int power_domain_get_by_index(struct udevice *dev, |
| 35 | struct power_domain *power_domain, int index) |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 36 | { |
Simon Glass | 085ba47 | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 37 | struct ofnode_phandle_args args; |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 38 | 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 Glass | 085ba47 | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 44 | ret = dev_read_phandle_with_args(dev, "power-domains", |
Lokesh Vutla | c411c67 | 2018-08-27 15:57:44 +0530 | [diff] [blame] | 45 | "#power-domain-cells", 0, index, |
| 46 | &args); |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 47 | if (ret) { |
Simon Glass | 085ba47 | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 48 | debug("%s: dev_read_phandle_with_args failed: %d\n", |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 49 | __func__, ret); |
| 50 | return ret; |
| 51 | } |
| 52 | |
Simon Glass | 085ba47 | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 53 | ret = uclass_get_device_by_ofnode(UCLASS_POWER_DOMAIN, args.node, |
| 54 | &dev_power_domain); |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 55 | if (ret) { |
Simon Glass | 085ba47 | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 56 | debug("%s: uclass_get_device_by_ofnode failed: %d\n", |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 57 | __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 Vutla | c411c67 | 2018-08-27 15:57:44 +0530 | [diff] [blame] | 81 | int 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 Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 86 | int 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 Glass | 92ed3a3 | 2020-02-03 07:35:51 -0700 | [diff] [blame] | 92 | return ops->rfree(power_domain); |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int 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 | |
| 104 | int 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 Vutla | 589eeb8 | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 113 | #if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) |
| 114 | static int dev_power_domain_ctrl(struct udevice *dev, bool on) |
Peng Fan | db513e9 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 115 | { |
| 116 | struct power_domain pd; |
Lokesh Vutla | 589eeb8 | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 117 | int i, count, ret = 0; |
Peng Fan | db513e9 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 118 | |
| 119 | count = dev_count_phandle_with_args(dev, "power-domains", |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 120 | "#power-domain-cells", 0); |
Peng Fan | db513e9 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 121 | for (i = 0; i < count; i++) { |
| 122 | ret = power_domain_get_by_index(dev, &pd, i); |
| 123 | if (ret) |
| 124 | return ret; |
Lokesh Vutla | 589eeb8 | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 125 | if (on) |
| 126 | ret = power_domain_on(&pd); |
| 127 | else |
| 128 | ret = power_domain_off(&pd); |
Peng Fan | db513e9 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Lokesh Vutla | 589eeb8 | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 131 | /* |
Anatolij Gustschin | 0e3ce20 | 2020-02-17 09:42:11 +0100 | [diff] [blame] | 132 | * 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 Vutla | 589eeb8 | 2019-09-27 13:48:14 +0530 | [diff] [blame] | 143 | * 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 | |
| 154 | int dev_power_domain_on(struct udevice *dev) |
| 155 | { |
| 156 | return dev_power_domain_ctrl(dev, true); |
| 157 | } |
| 158 | |
| 159 | int dev_power_domain_off(struct udevice *dev) |
| 160 | { |
| 161 | return dev_power_domain_ctrl(dev, false); |
Peng Fan | db513e9 | 2019-09-17 09:29:19 +0000 | [diff] [blame] | 162 | } |
| 163 | #endif |
| 164 | |
Stephen Warren | 92c67fa | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 165 | UCLASS_DRIVER(power_domain) = { |
| 166 | .id = UCLASS_POWER_DOMAIN, |
| 167 | .name = "power_domain", |
| 168 | }; |