2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-07-13 19:45:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016, NVIDIA CORPORATION.
|
|
|
|
*/
|
|
|
|
|
2021-04-27 09:02:19 +00:00
|
|
|
#define LOG_CATEGORY UCLASS_POWER_DOMAIN
|
|
|
|
|
2016-07-13 19:45:31 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.h>
|
2016-07-13 19:45:31 +00:00
|
|
|
#include <power-domain.h>
|
|
|
|
#include <power-domain-uclass.h>
|
2019-09-27 08:18:14 +00:00
|
|
|
#include <dm/device-internal.h>
|
2016-07-13 19:45:31 +00:00
|
|
|
|
|
|
|
static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return (struct power_domain_ops *)dev->driver->ops;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int power_domain_of_xlate_default(struct power_domain *power_domain,
|
2017-05-19 02:09:49 +00:00
|
|
|
struct ofnode_phandle_args *args)
|
2016-07-13 19:45:31 +00:00
|
|
|
{
|
|
|
|
debug("%s(power_domain=%p)\n", __func__, power_domain);
|
|
|
|
|
|
|
|
if (args->args_count != 1) {
|
|
|
|
debug("Invalid args_count: %d\n", args->args_count);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
power_domain->id = args->args[0];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-27 10:27:44 +00:00
|
|
|
int power_domain_get_by_index(struct udevice *dev,
|
|
|
|
struct power_domain *power_domain, int index)
|
2016-07-13 19:45:31 +00:00
|
|
|
{
|
2017-05-19 02:09:49 +00:00
|
|
|
struct ofnode_phandle_args args;
|
2016-07-13 19:45:31 +00:00
|
|
|
int ret;
|
|
|
|
struct udevice *dev_power_domain;
|
|
|
|
struct power_domain_ops *ops;
|
|
|
|
|
|
|
|
debug("%s(dev=%p, power_domain=%p)\n", __func__, dev, power_domain);
|
|
|
|
|
2017-05-19 02:09:49 +00:00
|
|
|
ret = dev_read_phandle_with_args(dev, "power-domains",
|
2018-08-27 10:27:44 +00:00
|
|
|
"#power-domain-cells", 0, index,
|
|
|
|
&args);
|
2016-07-13 19:45:31 +00:00
|
|
|
if (ret) {
|
2017-05-19 02:09:49 +00:00
|
|
|
debug("%s: dev_read_phandle_with_args failed: %d\n",
|
2016-07-13 19:45:31 +00:00
|
|
|
__func__, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:09:49 +00:00
|
|
|
ret = uclass_get_device_by_ofnode(UCLASS_POWER_DOMAIN, args.node,
|
|
|
|
&dev_power_domain);
|
2016-07-13 19:45:31 +00:00
|
|
|
if (ret) {
|
2017-05-19 02:09:49 +00:00
|
|
|
debug("%s: uclass_get_device_by_ofnode failed: %d\n",
|
2016-07-13 19:45:31 +00:00
|
|
|
__func__, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ops = power_domain_dev_ops(dev_power_domain);
|
|
|
|
|
|
|
|
power_domain->dev = dev_power_domain;
|
|
|
|
if (ops->of_xlate)
|
|
|
|
ret = ops->of_xlate(power_domain, &args);
|
|
|
|
else
|
|
|
|
ret = power_domain_of_xlate_default(power_domain, &args);
|
|
|
|
if (ret) {
|
|
|
|
debug("of_xlate() failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ops->request(power_domain);
|
|
|
|
if (ret) {
|
|
|
|
debug("ops->request() failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-27 10:27:44 +00:00
|
|
|
int power_domain_get(struct udevice *dev, struct power_domain *power_domain)
|
|
|
|
{
|
|
|
|
return power_domain_get_by_index(dev, power_domain, 0);
|
|
|
|
}
|
|
|
|
|
2016-07-13 19:45:31 +00:00
|
|
|
int power_domain_free(struct power_domain *power_domain)
|
|
|
|
{
|
|
|
|
struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
|
|
|
|
|
|
|
|
debug("%s(power_domain=%p)\n", __func__, power_domain);
|
|
|
|
|
2020-02-03 14:35:51 +00:00
|
|
|
return ops->rfree(power_domain);
|
2016-07-13 19:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int power_domain_on(struct power_domain *power_domain)
|
|
|
|
{
|
|
|
|
struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
|
|
|
|
|
|
|
|
debug("%s(power_domain=%p)\n", __func__, power_domain);
|
|
|
|
|
|
|
|
return ops->on(power_domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
int power_domain_off(struct power_domain *power_domain)
|
|
|
|
{
|
|
|
|
struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
|
|
|
|
|
|
|
|
debug("%s(power_domain=%p)\n", __func__, power_domain);
|
|
|
|
|
|
|
|
return ops->off(power_domain);
|
|
|
|
}
|
|
|
|
|
2019-09-27 08:18:14 +00:00
|
|
|
#if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
|
|
|
|
static int dev_power_domain_ctrl(struct udevice *dev, bool on)
|
2019-09-17 09:29:19 +00:00
|
|
|
{
|
|
|
|
struct power_domain pd;
|
2019-09-27 08:18:14 +00:00
|
|
|
int i, count, ret = 0;
|
2019-09-17 09:29:19 +00:00
|
|
|
|
|
|
|
count = dev_count_phandle_with_args(dev, "power-domains",
|
2020-09-25 07:41:14 +00:00
|
|
|
"#power-domain-cells", 0);
|
2019-09-17 09:29:19 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
ret = power_domain_get_by_index(dev, &pd, i);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-09-27 08:18:14 +00:00
|
|
|
if (on)
|
|
|
|
ret = power_domain_on(&pd);
|
|
|
|
else
|
|
|
|
ret = power_domain_off(&pd);
|
2019-09-17 09:29:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 08:42:11 +00:00
|
|
|
/*
|
|
|
|
* For platforms with parent and child power-domain devices
|
|
|
|
* we may not run device_remove() on the power-domain parent
|
|
|
|
* because it will result in removing its children and switching
|
|
|
|
* off their power-domain parent. So we will get here again and
|
|
|
|
* again and will be stuck in an endless loop.
|
|
|
|
*/
|
|
|
|
if (!on && dev_get_parent(dev) == pd.dev &&
|
|
|
|
device_get_uclass_id(dev) == UCLASS_POWER_DOMAIN)
|
|
|
|
return ret;
|
|
|
|
|
2019-09-27 08:18:14 +00:00
|
|
|
/*
|
|
|
|
* power_domain_get() bound the device, thus
|
|
|
|
* we must remove it again to prevent unbinding
|
|
|
|
* active devices (which would result in unbind
|
|
|
|
* error).
|
|
|
|
*/
|
|
|
|
if (count > 0 && !on)
|
|
|
|
device_remove(pd.dev, DM_REMOVE_NORMAL);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dev_power_domain_on(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return dev_power_domain_ctrl(dev, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
int dev_power_domain_off(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return dev_power_domain_ctrl(dev, false);
|
2019-09-17 09:29:19 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-07-13 19:45:31 +00:00
|
|
|
UCLASS_DRIVER(power_domain) = {
|
|
|
|
.id = UCLASS_POWER_DOMAIN,
|
|
|
|
.name = "power_domain",
|
|
|
|
};
|