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