Loading...
1/*
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
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
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;
26
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;
41 }
42
43 spin_unlock_irq(&dev->power.lock);
44
45 /* kfree() verifies that its argument is nonzero. */
46 kfree(psd);
47
48 return 0;
49}
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;
63 int ret = 1;
64
65 spin_lock_irq(&dev->power.lock);
66
67 psd = dev_to_psd(dev);
68 if (!psd)
69 goto out;
70
71 if (--psd->refcount == 0) {
72 dev->power.subsys_data = NULL;
73 } else {
74 psd = NULL;
75 ret = 0;
76 }
77
78 out:
79 spin_unlock_irq(&dev->power.lock);
80 kfree(psd);
81
82 return ret;
83}
84EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * drivers/base/power/common.c - Common device power management code.
4 *
5 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 */
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>
15
16#include "power.h"
17
18/**
19 * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.
20 * @dev: Device to handle.
21 *
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.
25 */
26int dev_pm_get_subsys_data(struct device *dev)
27{
28 struct pm_subsys_data *psd;
29
30 psd = kzalloc(sizeof(*psd), GFP_KERNEL);
31 if (!psd)
32 return -ENOMEM;
33
34 spin_lock_irq(&dev->power.lock);
35
36 if (dev->power.subsys_data) {
37 dev->power.subsys_data->refcount++;
38 } else {
39 spin_lock_init(&psd->lock);
40 psd->refcount = 1;
41 dev->power.subsys_data = psd;
42 pm_clk_init(dev);
43 psd = NULL;
44 }
45
46 spin_unlock_irq(&dev->power.lock);
47
48 /* kfree() verifies that its argument is nonzero. */
49 kfree(psd);
50
51 return 0;
52}
53EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data);
54
55/**
56 * dev_pm_put_subsys_data - Drop reference to power.subsys_data.
57 * @dev: Device to handle.
58 *
59 * If the reference counter of power.subsys_data is zero after dropping the
60 * reference, power.subsys_data is removed.
61 */
62void dev_pm_put_subsys_data(struct device *dev)
63{
64 struct pm_subsys_data *psd;
65
66 spin_lock_irq(&dev->power.lock);
67
68 psd = dev_to_psd(dev);
69 if (!psd)
70 goto out;
71
72 if (--psd->refcount == 0)
73 dev->power.subsys_data = NULL;
74 else
75 psd = NULL;
76
77 out:
78 spin_unlock_irq(&dev->power.lock);
79 kfree(psd);
80}
81EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);
82
83/**
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.
87 *
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.
92 *
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.
96 *
97 * Callers must ensure proper synchronization of this function with power
98 * management callbacks.
99 *
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.
102 */
103int dev_pm_domain_attach(struct device *dev, bool power_on)
104{
105 int ret;
106
107 if (dev->pm_domain)
108 return 0;
109
110 ret = acpi_dev_pm_attach(dev, power_on);
111 if (!ret)
112 ret = genpd_dev_pm_attach(dev);
113
114 return ret < 0 ? ret : 0;
115}
116EXPORT_SYMBOL_GPL(dev_pm_domain_attach);
117
118/**
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.
122 *
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.
128 *
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.
135 *
136 * Callers must ensure proper synchronization of this function with power
137 * management callbacks.
138 *
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.
143 */
144struct device *dev_pm_domain_attach_by_id(struct device *dev,
145 unsigned int index)
146{
147 if (dev->pm_domain)
148 return ERR_PTR(-EEXIST);
149
150 return genpd_dev_pm_attach_by_id(dev, index);
151}
152EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_id);
153
154/**
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.
158 *
159 * For a detailed function description, see dev_pm_domain_attach_by_id().
160 */
161struct device *dev_pm_domain_attach_by_name(struct device *dev,
162 const char *name)
163{
164 if (dev->pm_domain)
165 return ERR_PTR(-EEXIST);
166
167 return genpd_dev_pm_attach_by_name(dev, name);
168}
169EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_name);
170
171/**
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.
176 *
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.
182 *
183 * Callers must ensure proper synchronization of this function with power
184 * management callbacks.
185 *
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.
189 */
190int dev_pm_domain_attach_list(struct device *dev,
191 const struct dev_pm_domain_attach_data *data,
192 struct dev_pm_domain_list **list)
193{
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;
198 bool by_id = true;
199 size_t size;
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;
203
204 if (dev->pm_domain)
205 return -EEXIST;
206
207 /* For now this is limited to OF based platforms. */
208 if (!np)
209 return 0;
210
211 if (data && data->pd_names) {
212 num_pds = data->num_pd_names;
213 by_id = false;
214 } else {
215 num_pds = of_count_phandle_with_args(np, "power-domains",
216 "#power-domain-cells");
217 }
218
219 if (num_pds <= 0)
220 return 0;
221
222 pds = kzalloc(sizeof(*pds), GFP_KERNEL);
223 if (!pds)
224 return -ENOMEM;
225
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);
229 if (!pds->pd_devs) {
230 ret = -ENOMEM;
231 goto free_pds;
232 }
233 pds->pd_links = (void *)(pds->pd_devs + num_pds);
234 pds->opp_tokens = (void *)(pds->pd_links + num_pds);
235
236 if (link_flags && pd_flags & PD_FLAG_DEV_LINK_ON)
237 link_flags |= DL_FLAG_RPM_ACTIVE;
238
239 for (i = 0; i < num_pds; i++) {
240 if (by_id)
241 pd_dev = dev_pm_domain_attach_by_id(dev, i);
242 else
243 pd_dev = dev_pm_domain_attach_by_name(dev,
244 data->pd_names[i]);
245 if (IS_ERR_OR_NULL(pd_dev)) {
246 ret = pd_dev ? PTR_ERR(pd_dev) : -ENODEV;
247 goto err_attach;
248 }
249
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,
254 };
255
256 ret = dev_pm_opp_set_config(dev, &config);
257 if (ret < 0)
258 goto err_link;
259
260 pds->opp_tokens[i] = ret;
261 }
262
263 if (link_flags) {
264 struct device_link *link;
265
266 link = device_link_add(dev, pd_dev, link_flags);
267 if (!link) {
268 ret = -ENODEV;
269 goto err_link;
270 }
271
272 pds->pd_links[i] = link;
273 }
274
275 pds->pd_devs[i] = pd_dev;
276 }
277
278 pds->num_pds = num_pds;
279 *list = pds;
280 return num_pds;
281
282err_link:
283 dev_pm_opp_clear_config(pds->opp_tokens[i]);
284 dev_pm_domain_detach(pd_dev, true);
285err_attach:
286 while (--i >= 0) {
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);
291 }
292 kfree(pds->pd_devs);
293free_pds:
294 kfree(pds);
295 return ret;
296}
297EXPORT_SYMBOL_GPL(dev_pm_domain_attach_list);
298
299/**
300 * devm_pm_domain_detach_list - devres-enabled version of dev_pm_domain_detach_list.
301 * @_list: The list of PM domains to detach.
302 *
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.
306 */
307static void devm_pm_domain_detach_list(void *_list)
308{
309 struct dev_pm_domain_list *list = _list;
310
311 dev_pm_domain_detach_list(list);
312}
313
314/**
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.
319 *
320 * NOTE: this will also handle calling devm_pm_domain_detach_list() for
321 * you during remove phase.
322 *
323 * Returns the number of attached PM domains or a negative error code in case of
324 * a failure.
325 */
326int devm_pm_domain_attach_list(struct device *dev,
327 const struct dev_pm_domain_attach_data *data,
328 struct dev_pm_domain_list **list)
329{
330 int ret, num_pds;
331
332 num_pds = dev_pm_domain_attach_list(dev, data, list);
333 if (num_pds <= 0)
334 return num_pds;
335
336 ret = devm_add_action_or_reset(dev, devm_pm_domain_detach_list, *list);
337 if (ret)
338 return ret;
339
340 return num_pds;
341}
342EXPORT_SYMBOL_GPL(devm_pm_domain_attach_list);
343
344/**
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.
348 *
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.
353 *
354 * Callers must ensure proper synchronization of this function with power
355 * management callbacks.
356 */
357void dev_pm_domain_detach(struct device *dev, bool power_off)
358{
359 if (dev->pm_domain && dev->pm_domain->detach)
360 dev->pm_domain->detach(dev, power_off);
361}
362EXPORT_SYMBOL_GPL(dev_pm_domain_detach);
363
364/**
365 * dev_pm_domain_detach_list - Detach a list of PM domains.
366 * @list: The list of PM domains to detach.
367 *
368 * This function reverse the actions from dev_pm_domain_attach_list().
369 * Typically it should be invoked during the remove phase from drivers.
370 *
371 * Callers must ensure proper synchronization of this function with power
372 * management callbacks.
373 */
374void dev_pm_domain_detach_list(struct dev_pm_domain_list *list)
375{
376 int i;
377
378 if (!list)
379 return;
380
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);
386 }
387
388 kfree(list->pd_devs);
389 kfree(list);
390}
391EXPORT_SYMBOL_GPL(dev_pm_domain_detach_list);
392
393/**
394 * dev_pm_domain_start - Start the device through its PM domain.
395 * @dev: Device to start.
396 *
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.
401 *
402 * Returns 0 on success and negative error values on failures.
403 */
404int dev_pm_domain_start(struct device *dev)
405{
406 if (dev->pm_domain && dev->pm_domain->start)
407 return dev->pm_domain->start(dev);
408
409 return 0;
410}
411EXPORT_SYMBOL_GPL(dev_pm_domain_start);
412
413/**
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.
417 *
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).
420 *
421 * This function must be called with the device lock held.
422 */
423void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd)
424{
425 if (dev->pm_domain == pd)
426 return;
427
428 WARN(pd && device_is_bound(dev),
429 "PM domains can only be changed for unbound devices\n");
430 dev->pm_domain = pd;
431 device_pm_check_callbacks(dev);
432}
433EXPORT_SYMBOL_GPL(dev_pm_domain_set);
434
435/**
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.
439 *
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.
443 *
444 * Returns 0 on success and when performance scaling isn't supported, negative
445 * error code on failure.
446 */
447int dev_pm_domain_set_performance_state(struct device *dev, unsigned int state)
448{
449 if (dev->pm_domain && dev->pm_domain->set_performance_state)
450 return dev->pm_domain->set_performance_state(dev, state);
451
452 return 0;
453}
454EXPORT_SYMBOL_GPL(dev_pm_domain_set_performance_state);