blob: 9e9ec4f41925426b60535147f85f753e1b2bf45e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren61f5ddc2016-07-13 13:45:31 -06002/*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
Stephen Warren61f5ddc2016-07-13 13:45:31 -06004 */
5
6#include <common.h>
7#include <dm.h>
Stephen Warren61f5ddc2016-07-13 13:45:31 -06008#include <power-domain.h>
9#include <power-domain-uclass.h>
10
Stephen Warren61f5ddc2016-07-13 13:45:31 -060011static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev)
12{
13 return (struct power_domain_ops *)dev->driver->ops;
14}
15
16static int power_domain_of_xlate_default(struct power_domain *power_domain,
Simon Glass424b2fe2017-05-18 20:09:49 -060017 struct ofnode_phandle_args *args)
Stephen Warren61f5ddc2016-07-13 13:45:31 -060018{
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
31int power_domain_get(struct udevice *dev, struct power_domain *power_domain)
32{
Simon Glass424b2fe2017-05-18 20:09:49 -060033 struct ofnode_phandle_args args;
Stephen Warren61f5ddc2016-07-13 13:45:31 -060034 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 Glass424b2fe2017-05-18 20:09:49 -060040 ret = dev_read_phandle_with_args(dev, "power-domains",
41 "#power-domain-cells", 0, 0, &args);
Stephen Warren61f5ddc2016-07-13 13:45:31 -060042 if (ret) {
Simon Glass424b2fe2017-05-18 20:09:49 -060043 debug("%s: dev_read_phandle_with_args failed: %d\n",
Stephen Warren61f5ddc2016-07-13 13:45:31 -060044 __func__, ret);
45 return ret;
46 }
47
Simon Glass424b2fe2017-05-18 20:09:49 -060048 ret = uclass_get_device_by_ofnode(UCLASS_POWER_DOMAIN, args.node,
49 &dev_power_domain);
Stephen Warren61f5ddc2016-07-13 13:45:31 -060050 if (ret) {
Simon Glass424b2fe2017-05-18 20:09:49 -060051 debug("%s: uclass_get_device_by_ofnode failed: %d\n",
Stephen Warren61f5ddc2016-07-13 13:45:31 -060052 __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
76int 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
85int 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
94int 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
103UCLASS_DRIVER(power_domain) = {
104 .id = UCLASS_POWER_DOMAIN,
105 .name = "power_domain",
106};