1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/power/clock_ops.c - Generic clock manipulation PM callbacks
5 * Copyright (c) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
8 #include <linux/kernel.h>
9 #include <linux/device.h>
12 #include <linux/pm_clock.h>
13 #include <linux/clk.h>
14 #include <linux/clkdev.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/pm_domain.h>
18 #include <linux/pm_runtime.h>
29 struct pm_clock_entry
{
30 struct list_head node
;
33 enum pce_status status
;
37 * pm_clk_enable - Enable a clock, reporting any errors
38 * @dev: The device for the given clock
39 * @ce: PM clock entry corresponding to the clock.
41 static inline void __pm_clk_enable(struct device
*dev
, struct pm_clock_entry
*ce
)
45 if (ce
->status
< PCE_STATUS_ERROR
) {
46 ret
= clk_enable(ce
->clk
);
48 ce
->status
= PCE_STATUS_ENABLED
;
50 dev_err(dev
, "%s: failed to enable clk %p, error %d\n",
51 __func__
, ce
->clk
, ret
);
56 * pm_clk_acquire - Acquire a device clock.
57 * @dev: Device whose clock is to be acquired.
58 * @ce: PM clock entry corresponding to the clock.
60 static void pm_clk_acquire(struct device
*dev
, struct pm_clock_entry
*ce
)
63 ce
->clk
= clk_get(dev
, ce
->con_id
);
64 if (IS_ERR(ce
->clk
)) {
65 ce
->status
= PCE_STATUS_ERROR
;
67 if (clk_prepare(ce
->clk
)) {
68 ce
->status
= PCE_STATUS_ERROR
;
69 dev_err(dev
, "clk_prepare() failed\n");
71 ce
->status
= PCE_STATUS_ACQUIRED
;
73 "Clock %pC con_id %s managed by runtime PM.\n",
79 static int __pm_clk_add(struct device
*dev
, const char *con_id
,
82 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
83 struct pm_clock_entry
*ce
;
88 ce
= kzalloc(sizeof(*ce
), GFP_KERNEL
);
93 ce
->con_id
= kstrdup(con_id
, GFP_KERNEL
);
96 "Not enough memory for clock connection ID.\n");
108 pm_clk_acquire(dev
, ce
);
110 spin_lock_irq(&psd
->lock
);
111 list_add_tail(&ce
->node
, &psd
->clock_list
);
112 spin_unlock_irq(&psd
->lock
);
117 * pm_clk_add - Start using a device clock for power management.
118 * @dev: Device whose clock is going to be used for power management.
119 * @con_id: Connection ID of the clock.
121 * Add the clock represented by @con_id to the list of clocks used for
122 * the power management of @dev.
124 int pm_clk_add(struct device
*dev
, const char *con_id
)
126 return __pm_clk_add(dev
, con_id
, NULL
);
128 EXPORT_SYMBOL_GPL(pm_clk_add
);
131 * pm_clk_add_clk - Start using a device clock for power management.
132 * @dev: Device whose clock is going to be used for power management.
133 * @clk: Clock pointer
135 * Add the clock to the list of clocks used for the power management of @dev.
136 * The power-management code will take control of the clock reference, so
137 * callers should not call clk_put() on @clk after this function sucessfully
140 int pm_clk_add_clk(struct device
*dev
, struct clk
*clk
)
142 return __pm_clk_add(dev
, NULL
, clk
);
144 EXPORT_SYMBOL_GPL(pm_clk_add_clk
);
148 * of_pm_clk_add_clk - Start using a device clock for power management.
149 * @dev: Device whose clock is going to be used for power management.
150 * @name: Name of clock that is going to be used for power management.
152 * Add the clock described in the 'clocks' device-tree node that matches
153 * with the 'name' provided, to the list of clocks used for the power
154 * management of @dev. On success, returns 0. Returns a negative error
155 * code if the clock is not found or cannot be added.
157 int of_pm_clk_add_clk(struct device
*dev
, const char *name
)
162 if (!dev
|| !dev
->of_node
|| !name
)
165 clk
= of_clk_get_by_name(dev
->of_node
, name
);
169 ret
= pm_clk_add_clk(dev
, clk
);
177 EXPORT_SYMBOL_GPL(of_pm_clk_add_clk
);
180 * of_pm_clk_add_clks - Start using device clock(s) for power management.
181 * @dev: Device whose clock(s) is going to be used for power management.
183 * Add a series of clocks described in the 'clocks' device-tree node for
184 * a device to the list of clocks used for the power management of @dev.
185 * On success, returns the number of clocks added. Returns a negative
186 * error code if there are no clocks in the device node for the device
187 * or if adding a clock fails.
189 int of_pm_clk_add_clks(struct device
*dev
)
195 if (!dev
|| !dev
->of_node
)
198 count
= of_count_phandle_with_args(dev
->of_node
, "clocks",
203 clks
= kcalloc(count
, sizeof(*clks
), GFP_KERNEL
);
207 for (i
= 0; i
< count
; i
++) {
208 clks
[i
] = of_clk_get(dev
->of_node
, i
);
209 if (IS_ERR(clks
[i
])) {
210 ret
= PTR_ERR(clks
[i
]);
214 ret
= pm_clk_add_clk(dev
, clks
[i
]);
227 pm_clk_remove_clk(dev
, clks
[i
]);
233 EXPORT_SYMBOL_GPL(of_pm_clk_add_clks
);
236 * __pm_clk_remove - Destroy PM clock entry.
237 * @ce: PM clock entry to destroy.
239 static void __pm_clk_remove(struct pm_clock_entry
*ce
)
244 if (ce
->status
< PCE_STATUS_ERROR
) {
245 if (ce
->status
== PCE_STATUS_ENABLED
)
246 clk_disable(ce
->clk
);
248 if (ce
->status
>= PCE_STATUS_ACQUIRED
) {
249 clk_unprepare(ce
->clk
);
259 * pm_clk_remove - Stop using a device clock for power management.
260 * @dev: Device whose clock should not be used for PM any more.
261 * @con_id: Connection ID of the clock.
263 * Remove the clock represented by @con_id from the list of clocks used for
264 * the power management of @dev.
266 void pm_clk_remove(struct device
*dev
, const char *con_id
)
268 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
269 struct pm_clock_entry
*ce
;
274 spin_lock_irq(&psd
->lock
);
276 list_for_each_entry(ce
, &psd
->clock_list
, node
) {
277 if (!con_id
&& !ce
->con_id
)
279 else if (!con_id
|| !ce
->con_id
)
281 else if (!strcmp(con_id
, ce
->con_id
))
285 spin_unlock_irq(&psd
->lock
);
290 spin_unlock_irq(&psd
->lock
);
294 EXPORT_SYMBOL_GPL(pm_clk_remove
);
297 * pm_clk_remove_clk - Stop using a device clock for power management.
298 * @dev: Device whose clock should not be used for PM any more.
299 * @clk: Clock pointer
301 * Remove the clock pointed to by @clk from the list of clocks used for
302 * the power management of @dev.
304 void pm_clk_remove_clk(struct device
*dev
, struct clk
*clk
)
306 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
307 struct pm_clock_entry
*ce
;
312 spin_lock_irq(&psd
->lock
);
314 list_for_each_entry(ce
, &psd
->clock_list
, node
) {
319 spin_unlock_irq(&psd
->lock
);
324 spin_unlock_irq(&psd
->lock
);
328 EXPORT_SYMBOL_GPL(pm_clk_remove_clk
);
331 * pm_clk_init - Initialize a device's list of power management clocks.
332 * @dev: Device to initialize the list of PM clocks for.
334 * Initialize the lock and clock_list members of the device's pm_subsys_data
337 void pm_clk_init(struct device
*dev
)
339 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
341 INIT_LIST_HEAD(&psd
->clock_list
);
343 EXPORT_SYMBOL_GPL(pm_clk_init
);
346 * pm_clk_create - Create and initialize a device's list of PM clocks.
347 * @dev: Device to create and initialize the list of PM clocks for.
349 * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
350 * members and make the @dev's power.subsys_data field point to it.
352 int pm_clk_create(struct device
*dev
)
354 return dev_pm_get_subsys_data(dev
);
356 EXPORT_SYMBOL_GPL(pm_clk_create
);
359 * pm_clk_destroy - Destroy a device's list of power management clocks.
360 * @dev: Device to destroy the list of PM clocks for.
362 * Clear the @dev's power.subsys_data field, remove the list of clock entries
363 * from the struct pm_subsys_data object pointed to by it before and free
366 void pm_clk_destroy(struct device
*dev
)
368 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
369 struct pm_clock_entry
*ce
, *c
;
370 struct list_head list
;
375 INIT_LIST_HEAD(&list
);
377 spin_lock_irq(&psd
->lock
);
379 list_for_each_entry_safe_reverse(ce
, c
, &psd
->clock_list
, node
)
380 list_move(&ce
->node
, &list
);
382 spin_unlock_irq(&psd
->lock
);
384 dev_pm_put_subsys_data(dev
);
386 list_for_each_entry_safe_reverse(ce
, c
, &list
, node
) {
391 EXPORT_SYMBOL_GPL(pm_clk_destroy
);
394 * pm_clk_suspend - Disable clocks in a device's PM clock list.
395 * @dev: Device to disable the clocks for.
397 int pm_clk_suspend(struct device
*dev
)
399 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
400 struct pm_clock_entry
*ce
;
403 dev_dbg(dev
, "%s()\n", __func__
);
408 spin_lock_irqsave(&psd
->lock
, flags
);
410 list_for_each_entry_reverse(ce
, &psd
->clock_list
, node
) {
411 if (ce
->status
< PCE_STATUS_ERROR
) {
412 if (ce
->status
== PCE_STATUS_ENABLED
)
413 clk_disable(ce
->clk
);
414 ce
->status
= PCE_STATUS_ACQUIRED
;
418 spin_unlock_irqrestore(&psd
->lock
, flags
);
422 EXPORT_SYMBOL_GPL(pm_clk_suspend
);
425 * pm_clk_resume - Enable clocks in a device's PM clock list.
426 * @dev: Device to enable the clocks for.
428 int pm_clk_resume(struct device
*dev
)
430 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
431 struct pm_clock_entry
*ce
;
434 dev_dbg(dev
, "%s()\n", __func__
);
439 spin_lock_irqsave(&psd
->lock
, flags
);
441 list_for_each_entry(ce
, &psd
->clock_list
, node
)
442 __pm_clk_enable(dev
, ce
);
444 spin_unlock_irqrestore(&psd
->lock
, flags
);
448 EXPORT_SYMBOL_GPL(pm_clk_resume
);
451 * pm_clk_notify - Notify routine for device addition and removal.
452 * @nb: Notifier block object this function is a member of.
453 * @action: Operation being carried out by the caller.
454 * @data: Device the routine is being run for.
456 * For this function to work, @nb must be a member of an object of type
457 * struct pm_clk_notifier_block containing all of the requisite data.
458 * Specifically, the pm_domain member of that object is copied to the device's
459 * pm_domain field and its con_ids member is used to populate the device's list
460 * of PM clocks, depending on @action.
462 * If the device's pm_domain field is already populated with a value different
463 * from the one stored in the struct pm_clk_notifier_block object, the function
466 static int pm_clk_notify(struct notifier_block
*nb
,
467 unsigned long action
, void *data
)
469 struct pm_clk_notifier_block
*clknb
;
470 struct device
*dev
= data
;
474 dev_dbg(dev
, "%s() %ld\n", __func__
, action
);
476 clknb
= container_of(nb
, struct pm_clk_notifier_block
, nb
);
479 case BUS_NOTIFY_ADD_DEVICE
:
483 error
= pm_clk_create(dev
);
487 dev_pm_domain_set(dev
, clknb
->pm_domain
);
488 if (clknb
->con_ids
[0]) {
489 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
490 pm_clk_add(dev
, *con_id
);
492 pm_clk_add(dev
, NULL
);
496 case BUS_NOTIFY_DEL_DEVICE
:
497 if (dev
->pm_domain
!= clknb
->pm_domain
)
500 dev_pm_domain_set(dev
, NULL
);
508 int pm_clk_runtime_suspend(struct device
*dev
)
512 dev_dbg(dev
, "%s\n", __func__
);
514 ret
= pm_generic_runtime_suspend(dev
);
516 dev_err(dev
, "failed to suspend device\n");
520 ret
= pm_clk_suspend(dev
);
522 dev_err(dev
, "failed to suspend clock\n");
523 pm_generic_runtime_resume(dev
);
529 EXPORT_SYMBOL_GPL(pm_clk_runtime_suspend
);
531 int pm_clk_runtime_resume(struct device
*dev
)
535 dev_dbg(dev
, "%s\n", __func__
);
537 ret
= pm_clk_resume(dev
);
539 dev_err(dev
, "failed to resume clock\n");
543 return pm_generic_runtime_resume(dev
);
545 EXPORT_SYMBOL_GPL(pm_clk_runtime_resume
);
547 #else /* !CONFIG_PM_CLK */
550 * enable_clock - Enable a device clock.
551 * @dev: Device whose clock is to be enabled.
552 * @con_id: Connection ID of the clock.
554 static void enable_clock(struct device
*dev
, const char *con_id
)
558 clk
= clk_get(dev
, con_id
);
560 clk_prepare_enable(clk
);
562 dev_info(dev
, "Runtime PM disabled, clock forced on.\n");
567 * disable_clock - Disable a device clock.
568 * @dev: Device whose clock is to be disabled.
569 * @con_id: Connection ID of the clock.
571 static void disable_clock(struct device
*dev
, const char *con_id
)
575 clk
= clk_get(dev
, con_id
);
577 clk_disable_unprepare(clk
);
579 dev_info(dev
, "Runtime PM disabled, clock forced off.\n");
584 * pm_clk_notify - Notify routine for device addition and removal.
585 * @nb: Notifier block object this function is a member of.
586 * @action: Operation being carried out by the caller.
587 * @data: Device the routine is being run for.
589 * For this function to work, @nb must be a member of an object of type
590 * struct pm_clk_notifier_block containing all of the requisite data.
591 * Specifically, the con_ids member of that object is used to enable or disable
592 * the device's clocks, depending on @action.
594 static int pm_clk_notify(struct notifier_block
*nb
,
595 unsigned long action
, void *data
)
597 struct pm_clk_notifier_block
*clknb
;
598 struct device
*dev
= data
;
601 dev_dbg(dev
, "%s() %ld\n", __func__
, action
);
603 clknb
= container_of(nb
, struct pm_clk_notifier_block
, nb
);
606 case BUS_NOTIFY_BIND_DRIVER
:
607 if (clknb
->con_ids
[0]) {
608 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
609 enable_clock(dev
, *con_id
);
611 enable_clock(dev
, NULL
);
614 case BUS_NOTIFY_DRIVER_NOT_BOUND
:
615 case BUS_NOTIFY_UNBOUND_DRIVER
:
616 if (clknb
->con_ids
[0]) {
617 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
618 disable_clock(dev
, *con_id
);
620 disable_clock(dev
, NULL
);
628 #endif /* !CONFIG_PM_CLK */
631 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
632 * @bus: Bus type to add the notifier to.
633 * @clknb: Notifier to be added to the given bus type.
635 * The nb member of @clknb is not expected to be initialized and its
636 * notifier_call member will be replaced with pm_clk_notify(). However,
637 * the remaining members of @clknb should be populated prior to calling this
640 void pm_clk_add_notifier(struct bus_type
*bus
,
641 struct pm_clk_notifier_block
*clknb
)
646 clknb
->nb
.notifier_call
= pm_clk_notify
;
647 bus_register_notifier(bus
, &clknb
->nb
);
649 EXPORT_SYMBOL_GPL(pm_clk_add_notifier
);