blob: 5da914041305907d30f26289f12643b5c581be07 [file] [log] [blame]
Rafael J. Wysockief27bed2011-08-25 15:34:01 +02001/*
2 * drivers/base/power/common.c - Common device power management code.
3 *
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
5 *
6 * This file is released under the GPLv2.
7 */
8
9#include <linux/init.h>
10#include <linux/kernel.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050011#include <linux/device.h>
Paul Gortmakeraaf19542011-09-28 18:23:03 -040012#include <linux/export.h>
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020013#include <linux/slab.h>
Rafael J. Wysockib5e8d262011-08-25 15:34:19 +020014#include <linux/pm_clock.h>
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020015
16/**
17 * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.
18 * @dev: Device to handle.
19 *
20 * If power.subsys_data is NULL, point it to a new object, otherwise increment
21 * its reference counter. Return 1 if a new object has been created, otherwise
22 * return 0 or error code.
23 */
24int dev_pm_get_subsys_data(struct device *dev)
25{
26 struct pm_subsys_data *psd;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020027
28 psd = kzalloc(sizeof(*psd), GFP_KERNEL);
29 if (!psd)
30 return -ENOMEM;
31
32 spin_lock_irq(&dev->power.lock);
33
34 if (dev->power.subsys_data) {
35 dev->power.subsys_data->refcount++;
36 } else {
37 spin_lock_init(&psd->lock);
38 psd->refcount = 1;
39 dev->power.subsys_data = psd;
40 pm_clk_init(dev);
41 psd = NULL;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020042 }
43
44 spin_unlock_irq(&dev->power.lock);
45
46 /* kfree() verifies that its argument is nonzero. */
47 kfree(psd);
48
Rafael J. Wysocki77254952012-08-07 13:50:14 +020049 return 0;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020050}
51EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data);
52
53/**
54 * dev_pm_put_subsys_data - Drop reference to power.subsys_data.
55 * @dev: Device to handle.
56 *
57 * If the reference counter of power.subsys_data is zero after dropping the
58 * reference, power.subsys_data is removed. Return 1 if that happens or 0
59 * otherwise.
60 */
61int dev_pm_put_subsys_data(struct device *dev)
62{
63 struct pm_subsys_data *psd;
Shuah Khand5e16702013-05-08 01:14:32 +020064 int ret = 1;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020065
66 spin_lock_irq(&dev->power.lock);
67
68 psd = dev_to_psd(dev);
Shuah Khand5e16702013-05-08 01:14:32 +020069 if (!psd)
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020070 goto out;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020071
72 if (--psd->refcount == 0) {
73 dev->power.subsys_data = NULL;
Shuah Khand5e16702013-05-08 01:14:32 +020074 } else {
75 psd = NULL;
76 ret = 0;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020077 }
78
79 out:
80 spin_unlock_irq(&dev->power.lock);
Shuah Khand5e16702013-05-08 01:14:32 +020081 kfree(psd);
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020082
83 return ret;
84}
85EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);