2019-04-02 13:32:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2011-08-25 13:34:01 +00:00
|
|
|
/*
|
|
|
|
* drivers/base/power/common.c - Common device power management code.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
|
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
2012-01-22 16:23:42 +00:00
|
|
|
#include <linux/device.h>
|
2011-09-28 22:23:03 +00:00
|
|
|
#include <linux/export.h>
|
2011-08-25 13:34:01 +00:00
|
|
|
#include <linux/slab.h>
|
2011-08-25 13:34:19 +00:00
|
|
|
#include <linux/pm_clock.h>
|
2014-09-19 18:27:37 +00:00
|
|
|
#include <linux/acpi.h>
|
|
|
|
#include <linux/pm_domain.h>
|
2011-08-25 13:34:01 +00:00
|
|
|
|
2016-01-07 15:46:14 +00:00
|
|
|
#include "power.h"
|
|
|
|
|
2011-08-25 13:34:01 +00:00
|
|
|
/**
|
|
|
|
* dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.
|
|
|
|
* @dev: Device to handle.
|
|
|
|
*
|
|
|
|
* If power.subsys_data is NULL, point it to a new object, otherwise increment
|
2015-01-29 17:39:04 +00:00
|
|
|
* its reference counter. Return 0 if new object has been created or refcount
|
|
|
|
* increased, otherwise negative error code.
|
2011-08-25 13:34:01 +00:00
|
|
|
*/
|
|
|
|
int dev_pm_get_subsys_data(struct device *dev)
|
|
|
|
{
|
|
|
|
struct pm_subsys_data *psd;
|
|
|
|
|
|
|
|
psd = kzalloc(sizeof(*psd), GFP_KERNEL);
|
|
|
|
if (!psd)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
spin_lock_irq(&dev->power.lock);
|
|
|
|
|
|
|
|
if (dev->power.subsys_data) {
|
|
|
|
dev->power.subsys_data->refcount++;
|
|
|
|
} else {
|
|
|
|
spin_lock_init(&psd->lock);
|
|
|
|
psd->refcount = 1;
|
|
|
|
dev->power.subsys_data = psd;
|
|
|
|
pm_clk_init(dev);
|
|
|
|
psd = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irq(&dev->power.lock);
|
|
|
|
|
|
|
|
/* kfree() verifies that its argument is nonzero. */
|
|
|
|
kfree(psd);
|
|
|
|
|
2012-08-07 11:50:14 +00:00
|
|
|
return 0;
|
2011-08-25 13:34:01 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dev_pm_put_subsys_data - Drop reference to power.subsys_data.
|
|
|
|
* @dev: Device to handle.
|
|
|
|
*
|
|
|
|
* If the reference counter of power.subsys_data is zero after dropping the
|
2015-01-29 17:39:05 +00:00
|
|
|
* reference, power.subsys_data is removed.
|
2011-08-25 13:34:01 +00:00
|
|
|
*/
|
2015-01-29 17:39:05 +00:00
|
|
|
void dev_pm_put_subsys_data(struct device *dev)
|
2011-08-25 13:34:01 +00:00
|
|
|
{
|
|
|
|
struct pm_subsys_data *psd;
|
|
|
|
|
|
|
|
spin_lock_irq(&dev->power.lock);
|
|
|
|
|
|
|
|
psd = dev_to_psd(dev);
|
2013-05-07 23:14:32 +00:00
|
|
|
if (!psd)
|
2011-08-25 13:34:01 +00:00
|
|
|
goto out;
|
|
|
|
|
2015-01-29 17:39:05 +00:00
|
|
|
if (--psd->refcount == 0)
|
2011-08-25 13:34:01 +00:00
|
|
|
dev->power.subsys_data = NULL;
|
2015-01-29 17:39:05 +00:00
|
|
|
else
|
2013-05-07 23:14:32 +00:00
|
|
|
psd = NULL;
|
2011-08-25 13:34:01 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
spin_unlock_irq(&dev->power.lock);
|
2013-05-07 23:14:32 +00:00
|
|
|
kfree(psd);
|
2011-08-25 13:34:01 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);
|
2014-09-19 18:27:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* dev_pm_domain_attach - Attach a device to its PM domain.
|
|
|
|
* @dev: Device to attach.
|
|
|
|
* @power_on: Used to indicate whether we should power on the device.
|
|
|
|
*
|
|
|
|
* The @dev may only be attached to a single PM domain. By iterating through
|
|
|
|
* the available alternatives we try to find a valid PM domain for the device.
|
|
|
|
* As attachment succeeds, the ->detach() callback in the struct dev_pm_domain
|
|
|
|
* should be assigned by the corresponding attach function.
|
|
|
|
*
|
|
|
|
* This function should typically be invoked from subsystem level code during
|
|
|
|
* the probe phase. Especially for those that holds devices which requires
|
|
|
|
* power management through PM domains.
|
|
|
|
*
|
|
|
|
* Callers must ensure proper synchronization of this function with power
|
|
|
|
* management callbacks.
|
|
|
|
*
|
2018-05-15 13:21:43 +00:00
|
|
|
* Returns 0 on successfully attached PM domain, or when it is found that the
|
|
|
|
* device doesn't need a PM domain, else a negative error code.
|
2014-09-19 18:27:37 +00:00
|
|
|
*/
|
|
|
|
int dev_pm_domain_attach(struct device *dev, bool power_on)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-04-26 08:53:03 +00:00
|
|
|
if (dev->pm_domain)
|
2018-05-14 14:52:37 +00:00
|
|
|
return 0;
|
2018-04-26 08:53:03 +00:00
|
|
|
|
2014-09-19 18:27:37 +00:00
|
|
|
ret = acpi_dev_pm_attach(dev, power_on);
|
2018-05-09 10:17:52 +00:00
|
|
|
if (!ret)
|
2014-09-19 18:27:37 +00:00
|
|
|
ret = genpd_dev_pm_attach(dev);
|
|
|
|
|
2018-05-09 10:17:52 +00:00
|
|
|
return ret < 0 ? ret : 0;
|
2014-09-19 18:27:37 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dev_pm_domain_attach);
|
|
|
|
|
PM / Domains: Add dev_pm_domain_attach_by_id() to manage multi PM domains
The existing dev_pm_domain_attach() function, allows a single PM domain to
be attached per device. To be able to support devices that are partitioned
across multiple PM domains, let's introduce a new interface,
dev_pm_domain_attach_by_id().
The dev_pm_domain_attach_by_id() returns a new allocated struct device with
the corresponding attached PM domain. This enables for example a driver to
operate on the new device from a power management point of view. The driver
may then also benefit from using the received device, to set up so called
device-links towards its original device. Depending on the situation, these
links may then be dynamically changed.
The new interface is typically called by drivers during their probe phase,
in case they manages devices which uses multiple PM domains. If that is the
case, the driver also becomes responsible of managing the detaching of the
PM domains, which typically should be done at the remove phase. Detaching
is done by calling the existing dev_pm_domain_detach() function and for
each of the received devices from dev_pm_domain_attach_by_id().
Note, currently its only genpd that supports multiple PM domains per
device, but dev_pm_domain_attach_by_id() can easily by extended to cover
other PM domain types, if/when needed.
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
Acked-by: Jon Hunter <jonathanh@nvidia.com>
Tested-by: Jon Hunter <jonathanh@nvidia.com>
Reviewed-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2018-05-31 10:59:59 +00:00
|
|
|
/**
|
|
|
|
* dev_pm_domain_attach_by_id - Associate a device with one of its PM domains.
|
|
|
|
* @dev: The device used to lookup the PM domain.
|
|
|
|
* @index: The index of the PM domain.
|
|
|
|
*
|
|
|
|
* As @dev may only be attached to a single PM domain, the backend PM domain
|
|
|
|
* provider creates a virtual device to attach instead. If attachment succeeds,
|
|
|
|
* the ->detach() callback in the struct dev_pm_domain are assigned by the
|
|
|
|
* corresponding backend attach function, as to deal with detaching of the
|
|
|
|
* created virtual device.
|
|
|
|
*
|
|
|
|
* This function should typically be invoked by a driver during the probe phase,
|
|
|
|
* in case its device requires power management through multiple PM domains. The
|
|
|
|
* driver may benefit from using the received device, to configure device-links
|
|
|
|
* towards its original device. Depending on the use-case and if needed, the
|
|
|
|
* links may be dynamically changed by the driver, which allows it to control
|
|
|
|
* the power to the PM domains independently from each other.
|
|
|
|
*
|
|
|
|
* Callers must ensure proper synchronization of this function with power
|
|
|
|
* management callbacks.
|
|
|
|
*
|
|
|
|
* Returns the virtual created device when successfully attached to its PM
|
|
|
|
* domain, NULL in case @dev don't need a PM domain, else an ERR_PTR().
|
|
|
|
* Note that, to detach the returned virtual device, the driver shall call
|
|
|
|
* dev_pm_domain_detach() on it, typically during the remove phase.
|
|
|
|
*/
|
|
|
|
struct device *dev_pm_domain_attach_by_id(struct device *dev,
|
|
|
|
unsigned int index)
|
|
|
|
{
|
|
|
|
if (dev->pm_domain)
|
|
|
|
return ERR_PTR(-EEXIST);
|
|
|
|
|
|
|
|
return genpd_dev_pm_attach_by_id(dev, index);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_id);
|
|
|
|
|
2018-06-29 11:04:32 +00:00
|
|
|
/**
|
|
|
|
* dev_pm_domain_attach_by_name - Associate a device with one of its PM domains.
|
|
|
|
* @dev: The device used to lookup the PM domain.
|
|
|
|
* @name: The name of the PM domain.
|
|
|
|
*
|
|
|
|
* For a detailed function description, see dev_pm_domain_attach_by_id().
|
|
|
|
*/
|
|
|
|
struct device *dev_pm_domain_attach_by_name(struct device *dev,
|
2019-02-14 18:12:49 +00:00
|
|
|
const char *name)
|
2018-06-29 11:04:32 +00:00
|
|
|
{
|
|
|
|
if (dev->pm_domain)
|
|
|
|
return ERR_PTR(-EEXIST);
|
|
|
|
|
|
|
|
return genpd_dev_pm_attach_by_name(dev, name);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_name);
|
|
|
|
|
2014-09-19 18:27:37 +00:00
|
|
|
/**
|
|
|
|
* dev_pm_domain_detach - Detach a device from its PM domain.
|
2015-12-29 10:03:21 +00:00
|
|
|
* @dev: Device to detach.
|
2014-09-19 18:27:37 +00:00
|
|
|
* @power_off: Used to indicate whether we should power off the device.
|
|
|
|
*
|
PM / Domains: Add dev_pm_domain_attach_by_id() to manage multi PM domains
The existing dev_pm_domain_attach() function, allows a single PM domain to
be attached per device. To be able to support devices that are partitioned
across multiple PM domains, let's introduce a new interface,
dev_pm_domain_attach_by_id().
The dev_pm_domain_attach_by_id() returns a new allocated struct device with
the corresponding attached PM domain. This enables for example a driver to
operate on the new device from a power management point of view. The driver
may then also benefit from using the received device, to set up so called
device-links towards its original device. Depending on the situation, these
links may then be dynamically changed.
The new interface is typically called by drivers during their probe phase,
in case they manages devices which uses multiple PM domains. If that is the
case, the driver also becomes responsible of managing the detaching of the
PM domains, which typically should be done at the remove phase. Detaching
is done by calling the existing dev_pm_domain_detach() function and for
each of the received devices from dev_pm_domain_attach_by_id().
Note, currently its only genpd that supports multiple PM domains per
device, but dev_pm_domain_attach_by_id() can easily by extended to cover
other PM domain types, if/when needed.
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
Acked-by: Jon Hunter <jonathanh@nvidia.com>
Tested-by: Jon Hunter <jonathanh@nvidia.com>
Reviewed-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2018-05-31 10:59:59 +00:00
|
|
|
* This functions will reverse the actions from dev_pm_domain_attach() and
|
|
|
|
* dev_pm_domain_attach_by_id(), thus it detaches @dev from its PM domain.
|
|
|
|
* Typically it should be invoked during the remove phase, either from
|
|
|
|
* subsystem level code or from drivers.
|
2014-09-19 18:27:37 +00:00
|
|
|
*
|
|
|
|
* Callers must ensure proper synchronization of this function with power
|
|
|
|
* management callbacks.
|
|
|
|
*/
|
|
|
|
void dev_pm_domain_detach(struct device *dev, bool power_off)
|
|
|
|
{
|
|
|
|
if (dev->pm_domain && dev->pm_domain->detach)
|
|
|
|
dev->pm_domain->detach(dev, power_off);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dev_pm_domain_detach);
|
2016-01-07 15:46:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* dev_pm_domain_set - Set PM domain of a device.
|
|
|
|
* @dev: Device whose PM domain is to be set.
|
|
|
|
* @pd: PM domain to be set, or NULL.
|
|
|
|
*
|
|
|
|
* Sets the PM domain the device belongs to. The PM domain of a device needs
|
|
|
|
* to be set before its probe finishes (it's bound to a driver).
|
|
|
|
*
|
|
|
|
* This function must be called with the device lock held.
|
|
|
|
*/
|
|
|
|
void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd)
|
|
|
|
{
|
|
|
|
if (dev->pm_domain == pd)
|
|
|
|
return;
|
|
|
|
|
2016-01-30 11:54:29 +00:00
|
|
|
WARN(pd && device_is_bound(dev),
|
2016-01-07 15:46:13 +00:00
|
|
|
"PM domains can only be changed for unbound devices\n");
|
|
|
|
dev->pm_domain = pd;
|
2016-01-07 15:46:14 +00:00
|
|
|
device_pm_check_callbacks(dev);
|
2016-01-07 15:46:13 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dev_pm_domain_set);
|