blob: 93ed14cc22524ccdd04301dbe460e0958bb69acf [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
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +010017#include "power.h"
18
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020019/**
20 * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.
21 * @dev: Device to handle.
22 *
23 * If power.subsys_data is NULL, point it to a new object, otherwise increment
Ulf Hansson766bb532015-01-29 18:39:04 +010024 * its reference counter. Return 0 if new object has been created or refcount
25 * increased, otherwise negative error code.
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020026 */
27int dev_pm_get_subsys_data(struct device *dev)
28{
29 struct pm_subsys_data *psd;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020030
31 psd = kzalloc(sizeof(*psd), GFP_KERNEL);
32 if (!psd)
33 return -ENOMEM;
34
35 spin_lock_irq(&dev->power.lock);
36
37 if (dev->power.subsys_data) {
38 dev->power.subsys_data->refcount++;
39 } else {
40 spin_lock_init(&psd->lock);
41 psd->refcount = 1;
42 dev->power.subsys_data = psd;
43 pm_clk_init(dev);
44 psd = NULL;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020045 }
46
47 spin_unlock_irq(&dev->power.lock);
48
49 /* kfree() verifies that its argument is nonzero. */
50 kfree(psd);
51
Rafael J. Wysocki77254952012-08-07 13:50:14 +020052 return 0;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020053}
54EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data);
55
56/**
57 * dev_pm_put_subsys_data - Drop reference to power.subsys_data.
58 * @dev: Device to handle.
59 *
60 * If the reference counter of power.subsys_data is zero after dropping the
Ulf Hansson1e95e3b2015-01-29 18:39:05 +010061 * reference, power.subsys_data is removed.
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020062 */
Ulf Hansson1e95e3b2015-01-29 18:39:05 +010063void dev_pm_put_subsys_data(struct device *dev)
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020064{
65 struct pm_subsys_data *psd;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020066
67 spin_lock_irq(&dev->power.lock);
68
69 psd = dev_to_psd(dev);
Shuah Khand5e16702013-05-08 01:14:32 +020070 if (!psd)
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020071 goto out;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020072
Ulf Hansson1e95e3b2015-01-29 18:39:05 +010073 if (--psd->refcount == 0)
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020074 dev->power.subsys_data = NULL;
Ulf Hansson1e95e3b2015-01-29 18:39:05 +010075 else
Shuah Khand5e16702013-05-08 01:14:32 +020076 psd = NULL;
Rafael J. Wysockief27bed2011-08-25 15:34:01 +020077
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}
82EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);
Ulf Hansson46420dd2014-09-19 20:27:37 +020083
84/**
85 * dev_pm_domain_attach - Attach a device to its PM domain.
86 * @dev: Device to attach.
87 * @power_on: Used to indicate whether we should power on the device.
88 *
89 * The @dev may only be attached to a single PM domain. By iterating through
90 * the available alternatives we try to find a valid PM domain for the device.
91 * As attachment succeeds, the ->detach() callback in the struct dev_pm_domain
92 * should be assigned by the corresponding attach function.
93 *
94 * This function should typically be invoked from subsystem level code during
95 * the probe phase. Especially for those that holds devices which requires
96 * power management through PM domains.
97 *
98 * Callers must ensure proper synchronization of this function with power
99 * management callbacks.
100 *
101 * Returns 0 on successfully attached PM domain or negative error code.
102 */
103int dev_pm_domain_attach(struct device *dev, bool power_on)
104{
105 int ret;
106
107 ret = acpi_dev_pm_attach(dev, power_on);
108 if (ret)
109 ret = genpd_dev_pm_attach(dev);
110
111 return ret;
112}
113EXPORT_SYMBOL_GPL(dev_pm_domain_attach);
114
115/**
116 * dev_pm_domain_detach - Detach a device from its PM domain.
Manuel Pégourié-Gonnard42957332015-12-29 11:03:21 +0100117 * @dev: Device to detach.
Ulf Hansson46420dd2014-09-19 20:27:37 +0200118 * @power_off: Used to indicate whether we should power off the device.
119 *
120 * This functions will reverse the actions from dev_pm_domain_attach() and thus
121 * try to detach the @dev from its PM domain. Typically it should be invoked
122 * from subsystem level code during the remove phase.
123 *
124 * Callers must ensure proper synchronization of this function with power
125 * management callbacks.
126 */
127void dev_pm_domain_detach(struct device *dev, bool power_off)
128{
129 if (dev->pm_domain && dev->pm_domain->detach)
130 dev->pm_domain->detach(dev, power_off);
131}
132EXPORT_SYMBOL_GPL(dev_pm_domain_detach);
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100133
134/**
135 * dev_pm_domain_set - Set PM domain of a device.
136 * @dev: Device whose PM domain is to be set.
137 * @pd: PM domain to be set, or NULL.
138 *
139 * Sets the PM domain the device belongs to. The PM domain of a device needs
140 * to be set before its probe finishes (it's bound to a driver).
141 *
142 * This function must be called with the device lock held.
143 */
144void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd)
145{
146 if (dev->pm_domain == pd)
147 return;
148
149 WARN(device_is_bound(dev),
150 "PM domains can only be changed for unbound devices\n");
151 dev->pm_domain = pd;
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +0100152 device_pm_check_callbacks(dev);
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100153}
154EXPORT_SYMBOL_GPL(dev_pm_domain_set);