2011-08-25 21:34:01 +08:00
|
|
|
/*
|
|
|
|
* drivers/base/power/common.c - Common device power management code.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
|
|
|
|
*
|
|
|
|
* This file is released under the GPLv2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kernel.h>
|
2012-01-23 00:23:42 +08:00
|
|
|
#include <linux/device.h>
|
2011-09-29 06:23:03 +08:00
|
|
|
#include <linux/export.h>
|
2011-08-25 21:34:01 +08:00
|
|
|
#include <linux/slab.h>
|
2011-08-25 21:34:19 +08:00
|
|
|
#include <linux/pm_clock.h>
|
2011-08-25 21:34:01 +08: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
|
|
|
|
* its reference counter. Return 1 if a new object has been created, otherwise
|
|
|
|
* return 0 or error code.
|
|
|
|
*/
|
|
|
|
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 19:50:14 +08:00
|
|
|
return 0;
|
2011-08-25 21:34:01 +08: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
|
|
|
|
* reference, power.subsys_data is removed. Return 1 if that happens or 0
|
|
|
|
* otherwise.
|
|
|
|
*/
|
|
|
|
int dev_pm_put_subsys_data(struct device *dev)
|
|
|
|
{
|
|
|
|
struct pm_subsys_data *psd;
|
2013-05-08 07:14:32 +08:00
|
|
|
int ret = 1;
|
2011-08-25 21:34:01 +08:00
|
|
|
|
|
|
|
spin_lock_irq(&dev->power.lock);
|
|
|
|
|
|
|
|
psd = dev_to_psd(dev);
|
2013-05-08 07:14:32 +08:00
|
|
|
if (!psd)
|
2011-08-25 21:34:01 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (--psd->refcount == 0) {
|
|
|
|
dev->power.subsys_data = NULL;
|
2013-05-08 07:14:32 +08:00
|
|
|
} else {
|
|
|
|
psd = NULL;
|
|
|
|
ret = 0;
|
2011-08-25 21:34:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
spin_unlock_irq(&dev->power.lock);
|
2013-05-08 07:14:32 +08:00
|
|
|
kfree(psd);
|
2011-08-25 21:34:01 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);
|