blob: f32b802b98f4b56bc0e733fe1efe5b81b182b009 [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>
Ulf Hansson46420dd2014-09-19 20:27:37 +020014#include <linux/acpi.h>
15#include <linux/pm_domain.h>
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020016
17/**
18 * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.
19 * @dev: Device to handle.
20 *
21 * If power.subsys_data is NULL, point it to a new object, otherwise increment
Ulf Hansson766bb532015-01-29 18:39:04 +010022 * its reference counter. Return 0 if new object has been created or refcount
23 * increased, otherwise negative error code.
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020024 */
25int dev_pm_get_subsys_data(struct device *dev)
26{
27 struct pm_subsys_data *psd;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020028
29 psd = kzalloc(sizeof(*psd), GFP_KERNEL);
30 if (!psd)
31 return -ENOMEM;
32
33 spin_lock_irq(&dev->power.lock);
34
35 if (dev->power.subsys_data) {
36 dev->power.subsys_data->refcount++;
37 } else {
38 spin_lock_init(&psd->lock);
39 psd->refcount = 1;
40 dev->power.subsys_data = psd;
41 pm_clk_init(dev);
42 psd = NULL;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020043 }
44
45 spin_unlock_irq(&dev->power.lock);
46
47 /* kfree() verifies that its argument is nonzero. */
48 kfree(psd);
49
Rafael J. Wysocki77254952012-08-07 13:50:14 +020050 return 0;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020051}
52EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data);
53
54/**
55 * dev_pm_put_subsys_data - Drop reference to power.subsys_data.
56 * @dev: Device to handle.
57 *
58 * If the reference counter of power.subsys_data is zero after dropping the
Ulf Hansson1e95e3b2015-01-29 18:39:05 +010059 * reference, power.subsys_data is removed.
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020060 */
Ulf Hansson1e95e3b2015-01-29 18:39:05 +010061void dev_pm_put_subsys_data(struct device *dev)
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020062{
63 struct pm_subsys_data *psd;
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
Ulf Hansson1e95e3b2015-01-29 18:39:05 +010071 if (--psd->refcount == 0)
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020072 dev->power.subsys_data = NULL;
Ulf Hansson1e95e3b2015-01-29 18:39:05 +010073 else
Shuah Khand5e16702013-05-08 01:14:32 +020074 psd = NULL;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020075
76 out:
77 spin_unlock_irq(&dev->power.lock);
Shuah Khand5e16702013-05-08 01:14:32 +020078 kfree(psd);
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020079}
80EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);
Ulf Hansson46420dd2014-09-19 20:27:37 +020081
82/**
83 * dev_pm_domain_attach - Attach a device to its PM domain.
84 * @dev: Device to attach.
85 * @power_on: Used to indicate whether we should power on the device.
86 *
87 * The @dev may only be attached to a single PM domain. By iterating through
88 * the available alternatives we try to find a valid PM domain for the device.
89 * As attachment succeeds, the ->detach() callback in the struct dev_pm_domain
90 * should be assigned by the corresponding attach function.
91 *
92 * This function should typically be invoked from subsystem level code during
93 * the probe phase. Especially for those that holds devices which requires
94 * power management through PM domains.
95 *
96 * Callers must ensure proper synchronization of this function with power
97 * management callbacks.
98 *
99 * Returns 0 on successfully attached PM domain or negative error code.
100 */
101int dev_pm_domain_attach(struct device *dev, bool power_on)
102{
103 int ret;
104
105 ret = acpi_dev_pm_attach(dev, power_on);
106 if (ret)
107 ret = genpd_dev_pm_attach(dev);
108
109 return ret;
110}
111EXPORT_SYMBOL_GPL(dev_pm_domain_attach);
112
113/**
114 * dev_pm_domain_detach - Detach a device from its PM domain.
115 * @dev: Device to attach.
116 * @power_off: Used to indicate whether we should power off the device.
117 *
118 * This functions will reverse the actions from dev_pm_domain_attach() and thus
119 * try to detach the @dev from its PM domain. Typically it should be invoked
120 * from subsystem level code during the remove phase.
121 *
122 * Callers must ensure proper synchronization of this function with power
123 * management callbacks.
124 */
125void dev_pm_domain_detach(struct device *dev, bool power_off)
126{
127 if (dev->pm_domain && dev->pm_domain->detach)
128 dev->pm_domain->detach(dev, power_off);
129}
130EXPORT_SYMBOL_GPL(dev_pm_domain_detach);