2 * drivers/base/power/common.c - Common device power management code.
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 * This file is released under the GPLv2.
9 #include <linux/kernel.h>
10 #include <linux/device.h>
11 #include <linux/export.h>
12 #include <linux/slab.h>
13 #include <linux/pm_clock.h>
14 #include <linux/acpi.h>
15 #include <linux/pm_domain.h>
18 * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.
19 * @dev: Device to handle.
21 * If power.subsys_data is NULL, point it to a new object, otherwise increment
22 * its reference counter. Return 0 if new object has been created or refcount
23 * increased, otherwise negative error code.
25 int dev_pm_get_subsys_data(struct device
*dev
)
27 struct pm_subsys_data
*psd
;
29 psd
= kzalloc(sizeof(*psd
), GFP_KERNEL
);
33 spin_lock_irq(&dev
->power
.lock
);
35 if (dev
->power
.subsys_data
) {
36 dev
->power
.subsys_data
->refcount
++;
38 spin_lock_init(&psd
->lock
);
40 dev
->power
.subsys_data
= psd
;
45 spin_unlock_irq(&dev
->power
.lock
);
47 /* kfree() verifies that its argument is nonzero. */
52 EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data
);
55 * dev_pm_put_subsys_data - Drop reference to power.subsys_data.
56 * @dev: Device to handle.
58 * If the reference counter of power.subsys_data is zero after dropping the
59 * reference, power.subsys_data is removed.
61 void dev_pm_put_subsys_data(struct device
*dev
)
63 struct pm_subsys_data
*psd
;
65 spin_lock_irq(&dev
->power
.lock
);
67 psd
= dev_to_psd(dev
);
71 if (--psd
->refcount
== 0)
72 dev
->power
.subsys_data
= NULL
;
77 spin_unlock_irq(&dev
->power
.lock
);
80 EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data
);
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.
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.
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.
96 * Callers must ensure proper synchronization of this function with power
97 * management callbacks.
99 * Returns 0 on successfully attached PM domain or negative error code.
101 int dev_pm_domain_attach(struct device
*dev
, bool power_on
)
105 ret
= acpi_dev_pm_attach(dev
, power_on
);
107 ret
= genpd_dev_pm_attach(dev
);
111 EXPORT_SYMBOL_GPL(dev_pm_domain_attach
);
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.
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.
122 * Callers must ensure proper synchronization of this function with power
123 * management callbacks.
125 void dev_pm_domain_detach(struct device
*dev
, bool power_off
)
127 if (dev
->pm_domain
&& dev
->pm_domain
->detach
)
128 dev
->pm_domain
->detach(dev
, power_off
);
130 EXPORT_SYMBOL_GPL(dev_pm_domain_detach
);