blob: df2e5eeaeb05570757ec7872c2022484c1824c87 [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
Rafael J. Wysockief27bed2011-08-25 15:34:01 +02009#include <linux/kernel.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050010#include <linux/device.h>
Paul Gortmakeraaf19542011-09-28 18:23:03 -040011#include <linux/export.h>
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020012#include <linux/slab.h>
Rafael J. Wysockib5e8d262011-08-25 15:34:19 +020013#include <linux/pm_clock.h>
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020014
15/**
16 * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.
17 * @dev: Device to handle.
18 *
19 * If power.subsys_data is NULL, point it to a new object, otherwise increment
20 * its reference counter. Return 1 if a new object has been created, otherwise
21 * return 0 or error code.
22 */
23int dev_pm_get_subsys_data(struct device *dev)
24{
25 struct pm_subsys_data *psd;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020026
27 psd = kzalloc(sizeof(*psd), GFP_KERNEL);
28 if (!psd)
29 return -ENOMEM;
30
31 spin_lock_irq(&dev->power.lock);
32
33 if (dev->power.subsys_data) {
34 dev->power.subsys_data->refcount++;
35 } else {
36 spin_lock_init(&psd->lock);
37 psd->refcount = 1;
38 dev->power.subsys_data = psd;
39 pm_clk_init(dev);
40 psd = NULL;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020041 }
42
43 spin_unlock_irq(&dev->power.lock);
44
45 /* kfree() verifies that its argument is nonzero. */
46 kfree(psd);
47
Rafael J. Wysocki77254952012-08-07 13:50:14 +020048 return 0;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020049}
50EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data);
51
52/**
53 * dev_pm_put_subsys_data - Drop reference to power.subsys_data.
54 * @dev: Device to handle.
55 *
56 * If the reference counter of power.subsys_data is zero after dropping the
57 * reference, power.subsys_data is removed. Return 1 if that happens or 0
58 * otherwise.
59 */
60int dev_pm_put_subsys_data(struct device *dev)
61{
62 struct pm_subsys_data *psd;
Shuah Khand5e16702013-05-08 01:14:32 +020063 int ret = 1;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020064
65 spin_lock_irq(&dev->power.lock);
66
67 psd = dev_to_psd(dev);
Shuah Khand5e16702013-05-08 01:14:32 +020068 if (!psd)
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020069 goto out;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020070
71 if (--psd->refcount == 0) {
72 dev->power.subsys_data = NULL;
Shuah Khand5e16702013-05-08 01:14:32 +020073 } else {
74 psd = NULL;
75 ret = 0;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020076 }
77
78 out:
79 spin_unlock_irq(&dev->power.lock);
Shuah Khand5e16702013-05-08 01:14:32 +020080 kfree(psd);
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020081
82 return ret;
83}
84EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);