common.c 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * 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>
11
#include <linux/device.h>
12
#include <linux/export.h>
13
#include <linux/slab.h>
14
#include <linux/pm_clock.h>
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

/**
 * 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);

49
	return 0;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
}
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;
	int ret = 0;

	spin_lock_irq(&dev->power.lock);

	psd = dev_to_psd(dev);
	if (!psd) {
		ret = -EINVAL;
		goto out;
	}

	if (--psd->refcount == 0) {
		dev->power.subsys_data = NULL;
		kfree(psd);
		ret = 1;
	}

 out:
	spin_unlock_irq(&dev->power.lock);

	return ret;
}
EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

/**
 * dev_pm_syscore_device - Set/unset the given device's power.syscore flag.
 * @dev: Device whose flag is to be modified.
 * @val: New value of the flag.
 */
void dev_pm_syscore_device(struct device *dev, bool val)
{
	unsigned long flags;

	spin_lock_irqsave(&dev->power.lock, flags);
	dev->power.syscore = val;
	spin_unlock_irqrestore(&dev->power.lock, flags);
}
EXPORT_SYMBOL_GPL(dev_pm_syscore_device);