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/init.h>
10 #include <linux/kernel.h>
11 #include <linux/device.h>
14 #include <linux/pm_clock.h>
15 #include <linux/clk.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
28 struct pm_clock_entry
{
29 struct list_head node
;
32 enum pce_status status
;
36 * pm_clk_enable - Enable a clock, reporting any errors
37 * @dev: The device for the given clock
38 * @clk: The clock being enabled.
40 static inline int __pm_clk_enable(struct device
*dev
, struct clk
*clk
)
42 int ret
= clk_enable(clk
);
44 dev_err(dev
, "%s: failed to enable clk %p, error %d\n",
51 * pm_clk_acquire - Acquire a device clock.
52 * @dev: Device whose clock is to be acquired.
53 * @ce: PM clock entry corresponding to the clock.
55 static void pm_clk_acquire(struct device
*dev
, struct pm_clock_entry
*ce
)
57 ce
->clk
= clk_get(dev
, ce
->con_id
);
58 if (IS_ERR(ce
->clk
)) {
59 ce
->status
= PCE_STATUS_ERROR
;
62 ce
->status
= PCE_STATUS_ACQUIRED
;
63 dev_dbg(dev
, "Clock %s managed by runtime PM.\n", ce
->con_id
);
68 * pm_clk_add - Start using a device clock for power management.
69 * @dev: Device whose clock is going to be used for power management.
70 * @con_id: Connection ID of the clock.
72 * Add the clock represented by @con_id to the list of clocks used for
73 * the power management of @dev.
75 int pm_clk_add(struct device
*dev
, const char *con_id
)
77 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
78 struct pm_clock_entry
*ce
;
83 ce
= kzalloc(sizeof(*ce
), GFP_KERNEL
);
85 dev_err(dev
, "Not enough memory for clock entry.\n");
90 ce
->con_id
= kstrdup(con_id
, GFP_KERNEL
);
93 "Not enough memory for clock connection ID.\n");
99 pm_clk_acquire(dev
, ce
);
101 spin_lock_irq(&psd
->lock
);
102 list_add_tail(&ce
->node
, &psd
->clock_list
);
103 spin_unlock_irq(&psd
->lock
);
108 * __pm_clk_remove - Destroy PM clock entry.
109 * @ce: PM clock entry to destroy.
111 static void __pm_clk_remove(struct pm_clock_entry
*ce
)
116 if (ce
->status
< PCE_STATUS_ERROR
) {
117 if (ce
->status
== PCE_STATUS_ENABLED
)
118 clk_disable(ce
->clk
);
120 if (ce
->status
>= PCE_STATUS_ACQUIRED
) {
121 clk_unprepare(ce
->clk
);
131 * pm_clk_remove - Stop using a device clock for power management.
132 * @dev: Device whose clock should not be used for PM any more.
133 * @con_id: Connection ID of the clock.
135 * Remove the clock represented by @con_id from the list of clocks used for
136 * the power management of @dev.
138 void pm_clk_remove(struct device
*dev
, const char *con_id
)
140 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
141 struct pm_clock_entry
*ce
;
146 spin_lock_irq(&psd
->lock
);
148 list_for_each_entry(ce
, &psd
->clock_list
, node
) {
149 if (!con_id
&& !ce
->con_id
)
151 else if (!con_id
|| !ce
->con_id
)
153 else if (!strcmp(con_id
, ce
->con_id
))
157 spin_unlock_irq(&psd
->lock
);
162 spin_unlock_irq(&psd
->lock
);
168 * pm_clk_init - Initialize a device's list of power management clocks.
169 * @dev: Device to initialize the list of PM clocks for.
171 * Initialize the lock and clock_list members of the device's pm_subsys_data
174 void pm_clk_init(struct device
*dev
)
176 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
178 INIT_LIST_HEAD(&psd
->clock_list
);
182 * pm_clk_create - Create and initialize a device's list of PM clocks.
183 * @dev: Device to create and initialize the list of PM clocks for.
185 * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
186 * members and make the @dev's power.subsys_data field point to it.
188 int pm_clk_create(struct device
*dev
)
190 return dev_pm_get_subsys_data(dev
);
194 * pm_clk_destroy - Destroy a device's list of power management clocks.
195 * @dev: Device to destroy the list of PM clocks for.
197 * Clear the @dev's power.subsys_data field, remove the list of clock entries
198 * from the struct pm_subsys_data object pointed to by it before and free
201 void pm_clk_destroy(struct device
*dev
)
203 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
204 struct pm_clock_entry
*ce
, *c
;
205 struct list_head list
;
210 INIT_LIST_HEAD(&list
);
212 spin_lock_irq(&psd
->lock
);
214 list_for_each_entry_safe_reverse(ce
, c
, &psd
->clock_list
, node
)
215 list_move(&ce
->node
, &list
);
217 spin_unlock_irq(&psd
->lock
);
219 dev_pm_put_subsys_data(dev
);
221 list_for_each_entry_safe_reverse(ce
, c
, &list
, node
) {
227 #endif /* CONFIG_PM */
229 #ifdef CONFIG_PM_RUNTIME
232 * pm_clk_suspend - Disable clocks in a device's PM clock list.
233 * @dev: Device to disable the clocks for.
235 int pm_clk_suspend(struct device
*dev
)
237 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
238 struct pm_clock_entry
*ce
;
241 dev_dbg(dev
, "%s()\n", __func__
);
246 spin_lock_irqsave(&psd
->lock
, flags
);
248 list_for_each_entry_reverse(ce
, &psd
->clock_list
, node
) {
249 if (ce
->status
< PCE_STATUS_ERROR
) {
250 if (ce
->status
== PCE_STATUS_ENABLED
)
251 clk_disable(ce
->clk
);
252 ce
->status
= PCE_STATUS_ACQUIRED
;
256 spin_unlock_irqrestore(&psd
->lock
, flags
);
262 * pm_clk_resume - Enable clocks in a device's PM clock list.
263 * @dev: Device to enable the clocks for.
265 int pm_clk_resume(struct device
*dev
)
267 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
268 struct pm_clock_entry
*ce
;
272 dev_dbg(dev
, "%s()\n", __func__
);
277 spin_lock_irqsave(&psd
->lock
, flags
);
279 list_for_each_entry(ce
, &psd
->clock_list
, node
) {
280 if (ce
->status
< PCE_STATUS_ERROR
) {
281 ret
= __pm_clk_enable(dev
, ce
->clk
);
283 ce
->status
= PCE_STATUS_ENABLED
;
287 spin_unlock_irqrestore(&psd
->lock
, flags
);
293 * pm_clk_notify - Notify routine for device addition and removal.
294 * @nb: Notifier block object this function is a member of.
295 * @action: Operation being carried out by the caller.
296 * @data: Device the routine is being run for.
298 * For this function to work, @nb must be a member of an object of type
299 * struct pm_clk_notifier_block containing all of the requisite data.
300 * Specifically, the pm_domain member of that object is copied to the device's
301 * pm_domain field and its con_ids member is used to populate the device's list
302 * of PM clocks, depending on @action.
304 * If the device's pm_domain field is already populated with a value different
305 * from the one stored in the struct pm_clk_notifier_block object, the function
308 static int pm_clk_notify(struct notifier_block
*nb
,
309 unsigned long action
, void *data
)
311 struct pm_clk_notifier_block
*clknb
;
312 struct device
*dev
= data
;
316 dev_dbg(dev
, "%s() %ld\n", __func__
, action
);
318 clknb
= container_of(nb
, struct pm_clk_notifier_block
, nb
);
321 case BUS_NOTIFY_ADD_DEVICE
:
325 error
= pm_clk_create(dev
);
329 dev
->pm_domain
= clknb
->pm_domain
;
330 if (clknb
->con_ids
[0]) {
331 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
332 pm_clk_add(dev
, *con_id
);
334 pm_clk_add(dev
, NULL
);
338 case BUS_NOTIFY_DEL_DEVICE
:
339 if (dev
->pm_domain
!= clknb
->pm_domain
)
342 dev
->pm_domain
= NULL
;
350 #else /* !CONFIG_PM_RUNTIME */
355 * pm_clk_suspend - Disable clocks in a device's PM clock list.
356 * @dev: Device to disable the clocks for.
358 int pm_clk_suspend(struct device
*dev
)
360 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
361 struct pm_clock_entry
*ce
;
364 dev_dbg(dev
, "%s()\n", __func__
);
366 /* If there is no driver, the clocks are already disabled. */
367 if (!psd
|| !dev
->driver
)
370 spin_lock_irqsave(&psd
->lock
, flags
);
372 list_for_each_entry_reverse(ce
, &psd
->clock_list
, node
)
373 clk_disable(ce
->clk
);
375 spin_unlock_irqrestore(&psd
->lock
, flags
);
381 * pm_clk_resume - Enable clocks in a device's PM clock list.
382 * @dev: Device to enable the clocks for.
384 int pm_clk_resume(struct device
*dev
)
386 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
387 struct pm_clock_entry
*ce
;
390 dev_dbg(dev
, "%s()\n", __func__
);
392 /* If there is no driver, the clocks should remain disabled. */
393 if (!psd
|| !dev
->driver
)
396 spin_lock_irqsave(&psd
->lock
, flags
);
398 list_for_each_entry(ce
, &psd
->clock_list
, node
)
399 __pm_clk_enable(dev
, ce
->clk
);
401 spin_unlock_irqrestore(&psd
->lock
, flags
);
406 #endif /* CONFIG_PM */
409 * enable_clock - Enable a device clock.
410 * @dev: Device whose clock is to be enabled.
411 * @con_id: Connection ID of the clock.
413 static void enable_clock(struct device
*dev
, const char *con_id
)
417 clk
= clk_get(dev
, con_id
);
419 clk_prepare_enable(clk
);
421 dev_info(dev
, "Runtime PM disabled, clock forced on.\n");
426 * disable_clock - Disable a device clock.
427 * @dev: Device whose clock is to be disabled.
428 * @con_id: Connection ID of the clock.
430 static void disable_clock(struct device
*dev
, const char *con_id
)
434 clk
= clk_get(dev
, con_id
);
436 clk_disable_unprepare(clk
);
438 dev_info(dev
, "Runtime PM disabled, clock forced off.\n");
443 * pm_clk_notify - Notify routine for device addition and removal.
444 * @nb: Notifier block object this function is a member of.
445 * @action: Operation being carried out by the caller.
446 * @data: Device the routine is being run for.
448 * For this function to work, @nb must be a member of an object of type
449 * struct pm_clk_notifier_block containing all of the requisite data.
450 * Specifically, the con_ids member of that object is used to enable or disable
451 * the device's clocks, depending on @action.
453 static int pm_clk_notify(struct notifier_block
*nb
,
454 unsigned long action
, void *data
)
456 struct pm_clk_notifier_block
*clknb
;
457 struct device
*dev
= data
;
460 dev_dbg(dev
, "%s() %ld\n", __func__
, action
);
462 clknb
= container_of(nb
, struct pm_clk_notifier_block
, nb
);
465 case BUS_NOTIFY_BIND_DRIVER
:
466 if (clknb
->con_ids
[0]) {
467 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
468 enable_clock(dev
, *con_id
);
470 enable_clock(dev
, NULL
);
473 case BUS_NOTIFY_UNBOUND_DRIVER
:
474 if (clknb
->con_ids
[0]) {
475 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
476 disable_clock(dev
, *con_id
);
478 disable_clock(dev
, NULL
);
486 #endif /* !CONFIG_PM_RUNTIME */
489 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
490 * @bus: Bus type to add the notifier to.
491 * @clknb: Notifier to be added to the given bus type.
493 * The nb member of @clknb is not expected to be initialized and its
494 * notifier_call member will be replaced with pm_clk_notify(). However,
495 * the remaining members of @clknb should be populated prior to calling this
498 void pm_clk_add_notifier(struct bus_type
*bus
,
499 struct pm_clk_notifier_block
*clknb
)
504 clknb
->nb
.notifier_call
= pm_clk_notify
;
505 bus_register_notifier(bus
, &clknb
->nb
);