blob: 2ea0ff24c7ae4651a4dfd0e11099da90449e4c6f [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
Lokesh Vutla2618cf32018-08-27 15:57:44 +053031int power_domain_get_by_index(struct udevice *dev,
32 struct power_domain *power_domain, int index)
Stephen Warren61f5ddc2016-07-13 13:45:31 -060033{
Simon Glass424b2fe2017-05-18 20:09:49 -060034 struct ofnode_phandle_args args;
Stephen Warren61f5ddc2016-07-13 13:45:31 -060035 int ret;
36 struct udevice *dev_power_domain;
37 struct power_domain_ops *ops;
38
39 debug("%s(dev=%p, power_domain=%p)\n", __func__, dev, power_domain);
40
Simon Glass424b2fe2017-05-18 20:09:49 -060041 ret = dev_read_phandle_with_args(dev, "power-domains",
Lokesh Vutla2618cf32018-08-27 15:57:44 +053042 "#power-domain-cells", 0, index,
43 &args);
Stephen Warren61f5ddc2016-07-13 13:45:31 -060044 if (ret) {
Simon Glass424b2fe2017-05-18 20:09:49 -060045 debug("%s: dev_read_phandle_with_args failed: %d\n",
Stephen Warren61f5ddc2016-07-13 13:45:31 -060046 __func__, ret);
47 return ret;
48 }
49
Simon Glass424b2fe2017-05-18 20:09:49 -060050 ret = uclass_get_device_by_ofnode(UCLASS_POWER_DOMAIN, args.node,
51 &dev_power_domain);
Stephen Warren61f5ddc2016-07-13 13:45:31 -060052 if (ret) {
Simon Glass424b2fe2017-05-18 20:09:49 -060053 debug("%s: uclass_get_device_by_ofnode failed: %d\n",
Stephen Warren61f5ddc2016-07-13 13:45:31 -060054 __func__, ret);
55 return ret;
56 }
57 ops = power_domain_dev_ops(dev_power_domain);
58
59 power_domain->dev = dev_power_domain;
60 if (ops->of_xlate)
61 ret = ops->of_xlate(power_domain, &args);
62 else
63 ret = power_domain_of_xlate_default(power_domain, &args);
64 if (ret) {
65 debug("of_xlate() failed: %d\n", ret);
66 return ret;
67 }
68
69 ret = ops->request(power_domain);
70 if (ret) {
71 debug("ops->request() failed: %d\n", ret);
72 return ret;
73 }
74
75 return 0;
76}
77
Lokesh Vutla2618cf32018-08-27 15:57:44 +053078int power_domain_get(struct udevice *dev, struct power_domain *power_domain)
79{
80 return power_domain_get_by_index(dev, power_domain, 0);
81}
82
Stephen Warren61f5ddc2016-07-13 13:45:31 -060083int power_domain_free(struct power_domain *power_domain)
84{
85 struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
86
87 debug("%s(power_domain=%p)\n", __func__, power_domain);
88
89 return ops->free(power_domain);
90}
91
92int power_domain_on(struct power_domain *power_domain)
93{
94 struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
95
96 debug("%s(power_domain=%p)\n", __func__, power_domain);
97
98 return ops->on(power_domain);
99}
100
101int power_domain_off(struct power_domain *power_domain)
102{
103 struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
104
105 debug("%s(power_domain=%p)\n", __func__, power_domain);
106
107 return ops->off(power_domain);
108}
109
110UCLASS_DRIVER(power_domain) = {
111 .id = UCLASS_POWER_DOMAIN,
112 .name = "power_domain",
113};