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 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 8 | #include <power-domain.h> |
| 9 | #include <power-domain-uclass.h> |
| 10 | |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 11 | static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev) |
| 12 | { |
| 13 | return (struct power_domain_ops *)dev->driver->ops; |
| 14 | } |
| 15 | |
| 16 | static int power_domain_of_xlate_default(struct power_domain *power_domain, |
Simon Glass | 424b2fe | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 17 | struct ofnode_phandle_args *args) |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 18 | { |
| 19 | debug("%s(power_domain=%p)\n", __func__, power_domain); |
| 20 | |
| 21 | if (args->args_count != 1) { |
| 22 | debug("Invalid args_count: %d\n", args->args_count); |
| 23 | return -EINVAL; |
| 24 | } |
| 25 | |
| 26 | power_domain->id = args->args[0]; |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | int power_domain_get(struct udevice *dev, struct power_domain *power_domain) |
| 32 | { |
Simon Glass | 424b2fe | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 33 | struct ofnode_phandle_args args; |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 34 | int ret; |
| 35 | struct udevice *dev_power_domain; |
| 36 | struct power_domain_ops *ops; |
| 37 | |
| 38 | debug("%s(dev=%p, power_domain=%p)\n", __func__, dev, power_domain); |
| 39 | |
Simon Glass | 424b2fe | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 40 | ret = dev_read_phandle_with_args(dev, "power-domains", |
| 41 | "#power-domain-cells", 0, 0, &args); |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 42 | if (ret) { |
Simon Glass | 424b2fe | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 43 | debug("%s: dev_read_phandle_with_args failed: %d\n", |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 44 | __func__, ret); |
| 45 | return ret; |
| 46 | } |
| 47 | |
Simon Glass | 424b2fe | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 48 | ret = uclass_get_device_by_ofnode(UCLASS_POWER_DOMAIN, args.node, |
| 49 | &dev_power_domain); |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 50 | if (ret) { |
Simon Glass | 424b2fe | 2017-05-18 20:09:49 -0600 | [diff] [blame] | 51 | debug("%s: uclass_get_device_by_ofnode failed: %d\n", |
Stephen Warren | 61f5ddc | 2016-07-13 13:45:31 -0600 | [diff] [blame] | 52 | __func__, ret); |
| 53 | return ret; |
| 54 | } |
| 55 | ops = power_domain_dev_ops(dev_power_domain); |
| 56 | |
| 57 | power_domain->dev = dev_power_domain; |
| 58 | if (ops->of_xlate) |
| 59 | ret = ops->of_xlate(power_domain, &args); |
| 60 | else |
| 61 | ret = power_domain_of_xlate_default(power_domain, &args); |
| 62 | if (ret) { |
| 63 | debug("of_xlate() failed: %d\n", ret); |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | ret = ops->request(power_domain); |
| 68 | if (ret) { |
| 69 | debug("ops->request() failed: %d\n", ret); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | int power_domain_free(struct power_domain *power_domain) |
| 77 | { |
| 78 | struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev); |
| 79 | |
| 80 | debug("%s(power_domain=%p)\n", __func__, power_domain); |
| 81 | |
| 82 | return ops->free(power_domain); |
| 83 | } |
| 84 | |
| 85 | int power_domain_on(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 | |
| 91 | return ops->on(power_domain); |
| 92 | } |
| 93 | |
| 94 | int power_domain_off(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->off(power_domain); |
| 101 | } |
| 102 | |
| 103 | UCLASS_DRIVER(power_domain) = { |
| 104 | .id = UCLASS_POWER_DOMAIN, |
| 105 | .name = "power_domain", |
| 106 | }; |