1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/power/common.c - Common device power management code.
5 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
7 #include <linux/kernel.h>
8 #include <linux/device.h>
9 #include <linux/export.h>
10 #include <linux/slab.h>
11 #include <linux/pm_clock.h>
12 #include <linux/acpi.h>
13 #include <linux/pm_domain.h>
14 #include <linux/pm_opp.h>
19 * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.
20 * @dev: Device to handle.
22 * If power.subsys_data is NULL, point it to a new object, otherwise increment
23 * its reference counter. Return 0 if new object has been created or refcount
24 * increased, otherwise negative error code.
26 int dev_pm_get_subsys_data(struct device
*dev
)
28 struct pm_subsys_data
*psd
;
30 psd
= kzalloc(sizeof(*psd
), GFP_KERNEL
);
34 spin_lock_irq(&dev
->power
.lock
);
36 if (dev
->power
.subsys_data
) {
37 dev
->power
.subsys_data
->refcount
++;
39 spin_lock_init(&psd
->lock
);
41 dev
->power
.subsys_data
= psd
;
46 spin_unlock_irq(&dev
->power
.lock
);
48 /* kfree() verifies that its argument is nonzero. */
53 EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data
);
56 * dev_pm_put_subsys_data - Drop reference to power.subsys_data.
57 * @dev: Device to handle.
59 * If the reference counter of power.subsys_data is zero after dropping the
60 * reference, power.subsys_data is removed.
62 void dev_pm_put_subsys_data(struct device
*dev
)
64 struct pm_subsys_data
*psd
;
66 spin_lock_irq(&dev
->power
.lock
);
68 psd
= dev_to_psd(dev
);
72 if (--psd
->refcount
== 0)
73 dev
->power
.subsys_data
= NULL
;
78 spin_unlock_irq(&dev
->power
.lock
);
81 EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data
);
84 * dev_pm_domain_attach - Attach a device to its PM domain.
85 * @dev: Device to attach.
86 * @power_on: Used to indicate whether we should power on the device.
88 * The @dev may only be attached to a single PM domain. By iterating through
89 * the available alternatives we try to find a valid PM domain for the device.
90 * As attachment succeeds, the ->detach() callback in the struct dev_pm_domain
91 * should be assigned by the corresponding attach function.
93 * This function should typically be invoked from subsystem level code during
94 * the probe phase. Especially for those that holds devices which requires
95 * power management through PM domains.
97 * Callers must ensure proper synchronization of this function with power
98 * management callbacks.
100 * Returns 0 on successfully attached PM domain, or when it is found that the
101 * device doesn't need a PM domain, else a negative error code.
103 int dev_pm_domain_attach(struct device
*dev
, bool power_on
)
110 ret
= acpi_dev_pm_attach(dev
, power_on
);
112 ret
= genpd_dev_pm_attach(dev
);
114 return ret
< 0 ? ret
: 0;
116 EXPORT_SYMBOL_GPL(dev_pm_domain_attach
);
119 * dev_pm_domain_attach_by_id - Associate a device with one of its PM domains.
120 * @dev: The device used to lookup the PM domain.
121 * @index: The index of the PM domain.
123 * As @dev may only be attached to a single PM domain, the backend PM domain
124 * provider creates a virtual device to attach instead. If attachment succeeds,
125 * the ->detach() callback in the struct dev_pm_domain are assigned by the
126 * corresponding backend attach function, as to deal with detaching of the
127 * created virtual device.
129 * This function should typically be invoked by a driver during the probe phase,
130 * in case its device requires power management through multiple PM domains. The
131 * driver may benefit from using the received device, to configure device-links
132 * towards its original device. Depending on the use-case and if needed, the
133 * links may be dynamically changed by the driver, which allows it to control
134 * the power to the PM domains independently from each other.
136 * Callers must ensure proper synchronization of this function with power
137 * management callbacks.
139 * Returns the virtual created device when successfully attached to its PM
140 * domain, NULL in case @dev don't need a PM domain, else an ERR_PTR().
141 * Note that, to detach the returned virtual device, the driver shall call
142 * dev_pm_domain_detach() on it, typically during the remove phase.
144 struct device
*dev_pm_domain_attach_by_id(struct device
*dev
,
148 return ERR_PTR(-EEXIST
);
150 return genpd_dev_pm_attach_by_id(dev
, index
);
152 EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_id
);
155 * dev_pm_domain_attach_by_name - Associate a device with one of its PM domains.
156 * @dev: The device used to lookup the PM domain.
157 * @name: The name of the PM domain.
159 * For a detailed function description, see dev_pm_domain_attach_by_id().
161 struct device
*dev_pm_domain_attach_by_name(struct device
*dev
,
165 return ERR_PTR(-EEXIST
);
167 return genpd_dev_pm_attach_by_name(dev
, name
);
169 EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_name
);
172 * dev_pm_domain_attach_list - Associate a device with its PM domains.
173 * @dev: The device used to lookup the PM domains for.
174 * @data: The data used for attaching to the PM domains.
175 * @list: An out-parameter with an allocated list of attached PM domains.
177 * This function helps to attach a device to its multiple PM domains. The
178 * caller, which is typically a driver's probe function, may provide a list of
179 * names for the PM domains that we should try to attach the device to, but it
180 * may also provide an empty list, in case the attach should be done for all of
181 * the available PM domains.
183 * Callers must ensure proper synchronization of this function with power
184 * management callbacks.
186 * Returns the number of attached PM domains or a negative error code in case of
187 * a failure. Note that, to detach the list of PM domains, the driver shall call
188 * dev_pm_domain_detach_list(), typically during the remove phase.
190 int dev_pm_domain_attach_list(struct device
*dev
,
191 const struct dev_pm_domain_attach_data
*data
,
192 struct dev_pm_domain_list
**list
)
194 struct device_node
*np
= dev
->of_node
;
195 struct dev_pm_domain_list
*pds
;
196 struct device
*pd_dev
= NULL
;
197 int ret
, i
, num_pds
= 0;
200 u32 pd_flags
= data
? data
->pd_flags
: 0;
201 u32 link_flags
= pd_flags
& PD_FLAG_NO_DEV_LINK
? 0 :
202 DL_FLAG_STATELESS
| DL_FLAG_PM_RUNTIME
;
207 /* For now this is limited to OF based platforms. */
211 if (data
&& data
->pd_names
) {
212 num_pds
= data
->num_pd_names
;
215 num_pds
= of_count_phandle_with_args(np
, "power-domains",
216 "#power-domain-cells");
222 pds
= kzalloc(sizeof(*pds
), GFP_KERNEL
);
226 size
= sizeof(*pds
->pd_devs
) + sizeof(*pds
->pd_links
) +
227 sizeof(*pds
->opp_tokens
);
228 pds
->pd_devs
= kcalloc(num_pds
, size
, GFP_KERNEL
);
233 pds
->pd_links
= (void *)(pds
->pd_devs
+ num_pds
);
234 pds
->opp_tokens
= (void *)(pds
->pd_links
+ num_pds
);
236 if (link_flags
&& pd_flags
& PD_FLAG_DEV_LINK_ON
)
237 link_flags
|= DL_FLAG_RPM_ACTIVE
;
239 for (i
= 0; i
< num_pds
; i
++) {
241 pd_dev
= dev_pm_domain_attach_by_id(dev
, i
);
243 pd_dev
= dev_pm_domain_attach_by_name(dev
,
245 if (IS_ERR_OR_NULL(pd_dev
)) {
246 ret
= pd_dev
? PTR_ERR(pd_dev
) : -ENODEV
;
250 if (pd_flags
& PD_FLAG_REQUIRED_OPP
) {
251 struct dev_pm_opp_config config
= {
252 .required_dev
= pd_dev
,
253 .required_dev_index
= i
,
256 ret
= dev_pm_opp_set_config(dev
, &config
);
260 pds
->opp_tokens
[i
] = ret
;
264 struct device_link
*link
;
266 link
= device_link_add(dev
, pd_dev
, link_flags
);
272 pds
->pd_links
[i
] = link
;
275 pds
->pd_devs
[i
] = pd_dev
;
278 pds
->num_pds
= num_pds
;
283 dev_pm_opp_clear_config(pds
->opp_tokens
[i
]);
284 dev_pm_domain_detach(pd_dev
, true);
287 dev_pm_opp_clear_config(pds
->opp_tokens
[i
]);
288 if (pds
->pd_links
[i
])
289 device_link_del(pds
->pd_links
[i
]);
290 dev_pm_domain_detach(pds
->pd_devs
[i
], true);
297 EXPORT_SYMBOL_GPL(dev_pm_domain_attach_list
);
300 * devm_pm_domain_detach_list - devres-enabled version of dev_pm_domain_detach_list.
301 * @_list: The list of PM domains to detach.
303 * This function reverse the actions from devm_pm_domain_attach_list().
304 * it will be invoked during the remove phase from drivers implicitly if driver
305 * uses devm_pm_domain_attach_list() to attach the PM domains.
307 static void devm_pm_domain_detach_list(void *_list
)
309 struct dev_pm_domain_list
*list
= _list
;
311 dev_pm_domain_detach_list(list
);
315 * devm_pm_domain_attach_list - devres-enabled version of dev_pm_domain_attach_list
316 * @dev: The device used to lookup the PM domains for.
317 * @data: The data used for attaching to the PM domains.
318 * @list: An out-parameter with an allocated list of attached PM domains.
320 * NOTE: this will also handle calling devm_pm_domain_detach_list() for
321 * you during remove phase.
323 * Returns the number of attached PM domains or a negative error code in case of
326 int devm_pm_domain_attach_list(struct device
*dev
,
327 const struct dev_pm_domain_attach_data
*data
,
328 struct dev_pm_domain_list
**list
)
332 num_pds
= dev_pm_domain_attach_list(dev
, data
, list
);
336 ret
= devm_add_action_or_reset(dev
, devm_pm_domain_detach_list
, *list
);
342 EXPORT_SYMBOL_GPL(devm_pm_domain_attach_list
);
345 * dev_pm_domain_detach - Detach a device from its PM domain.
346 * @dev: Device to detach.
347 * @power_off: Used to indicate whether we should power off the device.
349 * This functions will reverse the actions from dev_pm_domain_attach(),
350 * dev_pm_domain_attach_by_id() and dev_pm_domain_attach_by_name(), thus it
351 * detaches @dev from its PM domain. Typically it should be invoked during the
352 * remove phase, either from subsystem level code or from drivers.
354 * Callers must ensure proper synchronization of this function with power
355 * management callbacks.
357 void dev_pm_domain_detach(struct device
*dev
, bool power_off
)
359 if (dev
->pm_domain
&& dev
->pm_domain
->detach
)
360 dev
->pm_domain
->detach(dev
, power_off
);
362 EXPORT_SYMBOL_GPL(dev_pm_domain_detach
);
365 * dev_pm_domain_detach_list - Detach a list of PM domains.
366 * @list: The list of PM domains to detach.
368 * This function reverse the actions from dev_pm_domain_attach_list().
369 * Typically it should be invoked during the remove phase from drivers.
371 * Callers must ensure proper synchronization of this function with power
372 * management callbacks.
374 void dev_pm_domain_detach_list(struct dev_pm_domain_list
*list
)
381 for (i
= 0; i
< list
->num_pds
; i
++) {
382 dev_pm_opp_clear_config(list
->opp_tokens
[i
]);
383 if (list
->pd_links
[i
])
384 device_link_del(list
->pd_links
[i
]);
385 dev_pm_domain_detach(list
->pd_devs
[i
], true);
388 kfree(list
->pd_devs
);
391 EXPORT_SYMBOL_GPL(dev_pm_domain_detach_list
);
394 * dev_pm_domain_start - Start the device through its PM domain.
395 * @dev: Device to start.
397 * This function should typically be called during probe by a subsystem/driver,
398 * when it needs to start its device from the PM domain's perspective. Note
399 * that, it's assumed that the PM domain is already powered on when this
400 * function is called.
402 * Returns 0 on success and negative error values on failures.
404 int dev_pm_domain_start(struct device
*dev
)
406 if (dev
->pm_domain
&& dev
->pm_domain
->start
)
407 return dev
->pm_domain
->start(dev
);
411 EXPORT_SYMBOL_GPL(dev_pm_domain_start
);
414 * dev_pm_domain_set - Set PM domain of a device.
415 * @dev: Device whose PM domain is to be set.
416 * @pd: PM domain to be set, or NULL.
418 * Sets the PM domain the device belongs to. The PM domain of a device needs
419 * to be set before its probe finishes (it's bound to a driver).
421 * This function must be called with the device lock held.
423 void dev_pm_domain_set(struct device
*dev
, struct dev_pm_domain
*pd
)
425 if (dev
->pm_domain
== pd
)
428 WARN(pd
&& device_is_bound(dev
),
429 "PM domains can only be changed for unbound devices\n");
431 device_pm_check_callbacks(dev
);
433 EXPORT_SYMBOL_GPL(dev_pm_domain_set
);
436 * dev_pm_domain_set_performance_state - Request a new performance state.
437 * @dev: The device to make the request for.
438 * @state: Target performance state for the device.
440 * This function should be called when a new performance state needs to be
441 * requested for a device that is attached to a PM domain. Note that, the
442 * support for performance scaling for PM domains is optional.
444 * Returns 0 on success and when performance scaling isn't supported, negative
445 * error code on failure.
447 int dev_pm_domain_set_performance_state(struct device
*dev
, unsigned int state
)
449 if (dev
->pm_domain
&& dev
->pm_domain
->set_performance_state
)
450 return dev
->pm_domain
->set_performance_state(dev
, state
);
454 EXPORT_SYMBOL_GPL(dev_pm_domain_set_performance_state
);