2 * drivers/base/power/clock_ops.c - Generic clock manipulation PM callbacks
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>
13 #include <linux/pm_clock.h>
14 #include <linux/clk.h>
15 #include <linux/clkdev.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/pm_domain.h>
19 #include <linux/pm_runtime.h>
30 struct pm_clock_entry
{
31 struct list_head node
;
34 enum pce_status status
;
38 * pm_clk_enable - Enable a clock, reporting any errors
39 * @dev: The device for the given clock
40 * @ce: PM clock entry corresponding to the clock.
42 static inline void __pm_clk_enable(struct device
*dev
, struct pm_clock_entry
*ce
)
46 if (ce
->status
< PCE_STATUS_ERROR
) {
47 ret
= clk_enable(ce
->clk
);
49 ce
->status
= PCE_STATUS_ENABLED
;
51 dev_err(dev
, "%s: failed to enable clk %p, error %d\n",
52 __func__
, ce
->clk
, ret
);
57 * pm_clk_acquire - Acquire a device clock.
58 * @dev: Device whose clock is to be acquired.
59 * @ce: PM clock entry corresponding to the clock.
61 static void pm_clk_acquire(struct device
*dev
, struct pm_clock_entry
*ce
)
64 ce
->clk
= clk_get(dev
, ce
->con_id
);
65 if (IS_ERR(ce
->clk
)) {
66 ce
->status
= PCE_STATUS_ERROR
;
68 if (clk_prepare(ce
->clk
)) {
69 ce
->status
= PCE_STATUS_ERROR
;
70 dev_err(dev
, "clk_prepare() failed\n");
72 ce
->status
= PCE_STATUS_ACQUIRED
;
74 "Clock %pC con_id %s managed by runtime PM.\n",
80 static int __pm_clk_add(struct device
*dev
, const char *con_id
,
83 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
84 struct pm_clock_entry
*ce
;
89 ce
= kzalloc(sizeof(*ce
), GFP_KERNEL
);
94 ce
->con_id
= kstrdup(con_id
, GFP_KERNEL
);
97 "Not enough memory for clock connection ID.\n");
109 pm_clk_acquire(dev
, ce
);
111 spin_lock_irq(&psd
->lock
);
112 list_add_tail(&ce
->node
, &psd
->clock_list
);
113 spin_unlock_irq(&psd
->lock
);
118 * pm_clk_add - Start using a device clock for power management.
119 * @dev: Device whose clock is going to be used for power management.
120 * @con_id: Connection ID of the clock.
122 * Add the clock represented by @con_id to the list of clocks used for
123 * the power management of @dev.
125 int pm_clk_add(struct device
*dev
, const char *con_id
)
127 return __pm_clk_add(dev
, con_id
, NULL
);
129 EXPORT_SYMBOL_GPL(pm_clk_add
);
132 * pm_clk_add_clk - Start using a device clock for power management.
133 * @dev: Device whose clock is going to be used for power management.
134 * @clk: Clock pointer
136 * Add the clock to the list of clocks used for the power management of @dev.
137 * The power-management code will take control of the clock reference, so
138 * callers should not call clk_put() on @clk after this function sucessfully
141 int pm_clk_add_clk(struct device
*dev
, struct clk
*clk
)
143 return __pm_clk_add(dev
, NULL
, clk
);
145 EXPORT_SYMBOL_GPL(pm_clk_add_clk
);
149 * of_pm_clk_add_clk - Start using a device clock for power management.
150 * @dev: Device whose clock is going to be used for power management.
151 * @name: Name of clock that is going to be used for power management.
153 * Add the clock described in the 'clocks' device-tree node that matches
154 * with the 'name' provided, to the list of clocks used for the power
155 * management of @dev. On success, returns 0. Returns a negative error
156 * code if the clock is not found or cannot be added.
158 int of_pm_clk_add_clk(struct device
*dev
, const char *name
)
163 if (!dev
|| !dev
->of_node
|| !name
)
166 clk
= of_clk_get_by_name(dev
->of_node
, name
);
170 ret
= pm_clk_add_clk(dev
, clk
);
178 EXPORT_SYMBOL_GPL(of_pm_clk_add_clk
);
181 * of_pm_clk_add_clks - Start using device clock(s) for power management.
182 * @dev: Device whose clock(s) is going to be used for power management.
184 * Add a series of clocks described in the 'clocks' device-tree node for
185 * a device to the list of clocks used for the power management of @dev.
186 * On success, returns the number of clocks added. Returns a negative
187 * error code if there are no clocks in the device node for the device
188 * or if adding a clock fails.
190 int of_pm_clk_add_clks(struct device
*dev
)
196 if (!dev
|| !dev
->of_node
)
199 count
= of_count_phandle_with_args(dev
->of_node
, "clocks",
204 clks
= kcalloc(count
, sizeof(*clks
), GFP_KERNEL
);
208 for (i
= 0; i
< count
; i
++) {
209 clks
[i
] = of_clk_get(dev
->of_node
, i
);
210 if (IS_ERR(clks
[i
])) {
211 ret
= PTR_ERR(clks
[i
]);
215 ret
= pm_clk_add_clk(dev
, clks
[i
]);
228 pm_clk_remove_clk(dev
, clks
[i
]);
234 EXPORT_SYMBOL_GPL(of_pm_clk_add_clks
);
237 * __pm_clk_remove - Destroy PM clock entry.
238 * @ce: PM clock entry to destroy.
240 static void __pm_clk_remove(struct pm_clock_entry
*ce
)
245 if (ce
->status
< PCE_STATUS_ERROR
) {
246 if (ce
->status
== PCE_STATUS_ENABLED
)
247 clk_disable(ce
->clk
);
249 if (ce
->status
>= PCE_STATUS_ACQUIRED
) {
250 clk_unprepare(ce
->clk
);
260 * pm_clk_remove - Stop using a device clock for power management.
261 * @dev: Device whose clock should not be used for PM any more.
262 * @con_id: Connection ID of the clock.
264 * Remove the clock represented by @con_id from the list of clocks used for
265 * the power management of @dev.
267 void pm_clk_remove(struct device
*dev
, const char *con_id
)
269 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
270 struct pm_clock_entry
*ce
;
275 spin_lock_irq(&psd
->lock
);
277 list_for_each_entry(ce
, &psd
->clock_list
, node
) {
278 if (!con_id
&& !ce
->con_id
)
280 else if (!con_id
|| !ce
->con_id
)
282 else if (!strcmp(con_id
, ce
->con_id
))
286 spin_unlock_irq(&psd
->lock
);
291 spin_unlock_irq(&psd
->lock
);
295 EXPORT_SYMBOL_GPL(pm_clk_remove
);
298 * pm_clk_remove_clk - Stop using a device clock for power management.
299 * @dev: Device whose clock should not be used for PM any more.
300 * @clk: Clock pointer
302 * Remove the clock pointed to by @clk from the list of clocks used for
303 * the power management of @dev.
305 void pm_clk_remove_clk(struct device
*dev
, struct clk
*clk
)
307 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
308 struct pm_clock_entry
*ce
;
313 spin_lock_irq(&psd
->lock
);
315 list_for_each_entry(ce
, &psd
->clock_list
, node
) {
320 spin_unlock_irq(&psd
->lock
);
325 spin_unlock_irq(&psd
->lock
);
329 EXPORT_SYMBOL_GPL(pm_clk_remove_clk
);
332 * pm_clk_init - Initialize a device's list of power management clocks.
333 * @dev: Device to initialize the list of PM clocks for.
335 * Initialize the lock and clock_list members of the device's pm_subsys_data
338 void pm_clk_init(struct device
*dev
)
340 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
342 INIT_LIST_HEAD(&psd
->clock_list
);
344 EXPORT_SYMBOL_GPL(pm_clk_init
);
347 * pm_clk_create - Create and initialize a device's list of PM clocks.
348 * @dev: Device to create and initialize the list of PM clocks for.
350 * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
351 * members and make the @dev's power.subsys_data field point to it.
353 int pm_clk_create(struct device
*dev
)
355 return dev_pm_get_subsys_data(dev
);
357 EXPORT_SYMBOL_GPL(pm_clk_create
);
360 * pm_clk_destroy - Destroy a device's list of power management clocks.
361 * @dev: Device to destroy the list of PM clocks for.
363 * Clear the @dev's power.subsys_data field, remove the list of clock entries
364 * from the struct pm_subsys_data object pointed to by it before and free
367 void pm_clk_destroy(struct device
*dev
)
369 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
370 struct pm_clock_entry
*ce
, *c
;
371 struct list_head list
;
376 INIT_LIST_HEAD(&list
);
378 spin_lock_irq(&psd
->lock
);
380 list_for_each_entry_safe_reverse(ce
, c
, &psd
->clock_list
, node
)
381 list_move(&ce
->node
, &list
);
383 spin_unlock_irq(&psd
->lock
);
385 dev_pm_put_subsys_data(dev
);
387 list_for_each_entry_safe_reverse(ce
, c
, &list
, node
) {
392 EXPORT_SYMBOL_GPL(pm_clk_destroy
);
395 * pm_clk_suspend - Disable clocks in a device's PM clock list.
396 * @dev: Device to disable the clocks for.
398 int pm_clk_suspend(struct device
*dev
)
400 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
401 struct pm_clock_entry
*ce
;
404 dev_dbg(dev
, "%s()\n", __func__
);
409 spin_lock_irqsave(&psd
->lock
, flags
);
411 list_for_each_entry_reverse(ce
, &psd
->clock_list
, node
) {
412 if (ce
->status
< PCE_STATUS_ERROR
) {
413 if (ce
->status
== PCE_STATUS_ENABLED
)
414 clk_disable(ce
->clk
);
415 ce
->status
= PCE_STATUS_ACQUIRED
;
419 spin_unlock_irqrestore(&psd
->lock
, flags
);
423 EXPORT_SYMBOL_GPL(pm_clk_suspend
);
426 * pm_clk_resume - Enable clocks in a device's PM clock list.
427 * @dev: Device to enable the clocks for.
429 int pm_clk_resume(struct device
*dev
)
431 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
432 struct pm_clock_entry
*ce
;
435 dev_dbg(dev
, "%s()\n", __func__
);
440 spin_lock_irqsave(&psd
->lock
, flags
);
442 list_for_each_entry(ce
, &psd
->clock_list
, node
)
443 __pm_clk_enable(dev
, ce
);
445 spin_unlock_irqrestore(&psd
->lock
, flags
);
449 EXPORT_SYMBOL_GPL(pm_clk_resume
);
452 * pm_clk_notify - Notify routine for device addition and removal.
453 * @nb: Notifier block object this function is a member of.
454 * @action: Operation being carried out by the caller.
455 * @data: Device the routine is being run for.
457 * For this function to work, @nb must be a member of an object of type
458 * struct pm_clk_notifier_block containing all of the requisite data.
459 * Specifically, the pm_domain member of that object is copied to the device's
460 * pm_domain field and its con_ids member is used to populate the device's list
461 * of PM clocks, depending on @action.
463 * If the device's pm_domain field is already populated with a value different
464 * from the one stored in the struct pm_clk_notifier_block object, the function
467 static int pm_clk_notify(struct notifier_block
*nb
,
468 unsigned long action
, void *data
)
470 struct pm_clk_notifier_block
*clknb
;
471 struct device
*dev
= data
;
475 dev_dbg(dev
, "%s() %ld\n", __func__
, action
);
477 clknb
= container_of(nb
, struct pm_clk_notifier_block
, nb
);
480 case BUS_NOTIFY_ADD_DEVICE
:
484 error
= pm_clk_create(dev
);
488 dev_pm_domain_set(dev
, clknb
->pm_domain
);
489 if (clknb
->con_ids
[0]) {
490 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
491 pm_clk_add(dev
, *con_id
);
493 pm_clk_add(dev
, NULL
);
497 case BUS_NOTIFY_DEL_DEVICE
:
498 if (dev
->pm_domain
!= clknb
->pm_domain
)
501 dev_pm_domain_set(dev
, NULL
);
509 int pm_clk_runtime_suspend(struct device
*dev
)
513 dev_dbg(dev
, "%s\n", __func__
);
515 ret
= pm_generic_runtime_suspend(dev
);
517 dev_err(dev
, "failed to suspend device\n");
521 ret
= pm_clk_suspend(dev
);
523 dev_err(dev
, "failed to suspend clock\n");
524 pm_generic_runtime_resume(dev
);
530 EXPORT_SYMBOL_GPL(pm_clk_runtime_suspend
);
532 int pm_clk_runtime_resume(struct device
*dev
)
536 dev_dbg(dev
, "%s\n", __func__
);
538 ret
= pm_clk_resume(dev
);
540 dev_err(dev
, "failed to resume clock\n");
544 return pm_generic_runtime_resume(dev
);
546 EXPORT_SYMBOL_GPL(pm_clk_runtime_resume
);
548 #else /* !CONFIG_PM_CLK */
551 * enable_clock - Enable a device clock.
552 * @dev: Device whose clock is to be enabled.
553 * @con_id: Connection ID of the clock.
555 static void enable_clock(struct device
*dev
, const char *con_id
)
559 clk
= clk_get(dev
, con_id
);
561 clk_prepare_enable(clk
);
563 dev_info(dev
, "Runtime PM disabled, clock forced on.\n");
568 * disable_clock - Disable a device clock.
569 * @dev: Device whose clock is to be disabled.
570 * @con_id: Connection ID of the clock.
572 static void disable_clock(struct device
*dev
, const char *con_id
)
576 clk
= clk_get(dev
, con_id
);
578 clk_disable_unprepare(clk
);
580 dev_info(dev
, "Runtime PM disabled, clock forced off.\n");
585 * pm_clk_notify - Notify routine for device addition and removal.
586 * @nb: Notifier block object this function is a member of.
587 * @action: Operation being carried out by the caller.
588 * @data: Device the routine is being run for.
590 * For this function to work, @nb must be a member of an object of type
591 * struct pm_clk_notifier_block containing all of the requisite data.
592 * Specifically, the con_ids member of that object is used to enable or disable
593 * the device's clocks, depending on @action.
595 static int pm_clk_notify(struct notifier_block
*nb
,
596 unsigned long action
, void *data
)
598 struct pm_clk_notifier_block
*clknb
;
599 struct device
*dev
= data
;
602 dev_dbg(dev
, "%s() %ld\n", __func__
, action
);
604 clknb
= container_of(nb
, struct pm_clk_notifier_block
, nb
);
607 case BUS_NOTIFY_BIND_DRIVER
:
608 if (clknb
->con_ids
[0]) {
609 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
610 enable_clock(dev
, *con_id
);
612 enable_clock(dev
, NULL
);
615 case BUS_NOTIFY_DRIVER_NOT_BOUND
:
616 case BUS_NOTIFY_UNBOUND_DRIVER
:
617 if (clknb
->con_ids
[0]) {
618 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
619 disable_clock(dev
, *con_id
);
621 disable_clock(dev
, NULL
);
629 #endif /* !CONFIG_PM_CLK */
632 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
633 * @bus: Bus type to add the notifier to.
634 * @clknb: Notifier to be added to the given bus type.
636 * The nb member of @clknb is not expected to be initialized and its
637 * notifier_call member will be replaced with pm_clk_notify(). However,
638 * the remaining members of @clknb should be populated prior to calling this
641 void pm_clk_add_notifier(struct bus_type
*bus
,
642 struct pm_clk_notifier_block
*clknb
)
647 clknb
->nb
.notifier_call
= pm_clk_notify
;
648 bus_register_notifier(bus
, &clknb
->nb
);
650 EXPORT_SYMBOL_GPL(pm_clk_add_notifier
);