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>
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 * @ce: PM clock entry corresponding to the clock.
40 static inline void __pm_clk_enable(struct device
*dev
, struct pm_clock_entry
*ce
)
44 if (ce
->status
< PCE_STATUS_ERROR
) {
45 ret
= clk_enable(ce
->clk
);
47 ce
->status
= PCE_STATUS_ENABLED
;
49 dev_err(dev
, "%s: failed to enable clk %p, error %d\n",
50 __func__
, ce
->clk
, ret
);
55 * pm_clk_acquire - Acquire a device clock.
56 * @dev: Device whose clock is to be acquired.
57 * @ce: PM clock entry corresponding to the clock.
59 static void pm_clk_acquire(struct device
*dev
, struct pm_clock_entry
*ce
)
62 ce
->clk
= clk_get(dev
, ce
->con_id
);
63 if (IS_ERR(ce
->clk
)) {
64 ce
->status
= PCE_STATUS_ERROR
;
67 ce
->status
= PCE_STATUS_ACQUIRED
;
68 dev_dbg(dev
, "Clock %s managed by runtime PM.\n", ce
->con_id
);
72 static int __pm_clk_add(struct device
*dev
, const char *con_id
,
75 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
76 struct pm_clock_entry
*ce
;
81 ce
= kzalloc(sizeof(*ce
), GFP_KERNEL
);
86 ce
->con_id
= kstrdup(con_id
, GFP_KERNEL
);
89 "Not enough memory for clock connection ID.\n");
94 if (IS_ERR(clk
) || !__clk_get(clk
)) {
101 pm_clk_acquire(dev
, ce
);
103 spin_lock_irq(&psd
->lock
);
104 list_add_tail(&ce
->node
, &psd
->clock_list
);
105 spin_unlock_irq(&psd
->lock
);
110 * pm_clk_add - Start using a device clock for power management.
111 * @dev: Device whose clock is going to be used for power management.
112 * @con_id: Connection ID of the clock.
114 * Add the clock represented by @con_id to the list of clocks used for
115 * the power management of @dev.
117 int pm_clk_add(struct device
*dev
, const char *con_id
)
119 return __pm_clk_add(dev
, con_id
, NULL
);
123 * pm_clk_add_clk - Start using a device clock for power management.
124 * @dev: Device whose clock is going to be used for power management.
125 * @clk: Clock pointer
127 * Add the clock to the list of clocks used for the power management of @dev.
128 * It will increment refcount on clock pointer, use clk_put() on it when done.
130 int pm_clk_add_clk(struct device
*dev
, struct clk
*clk
)
132 return __pm_clk_add(dev
, NULL
, clk
);
136 * __pm_clk_remove - Destroy PM clock entry.
137 * @ce: PM clock entry to destroy.
139 static void __pm_clk_remove(struct pm_clock_entry
*ce
)
144 if (ce
->status
< PCE_STATUS_ERROR
) {
145 if (ce
->status
== PCE_STATUS_ENABLED
)
146 clk_disable(ce
->clk
);
148 if (ce
->status
>= PCE_STATUS_ACQUIRED
) {
149 clk_unprepare(ce
->clk
);
159 * pm_clk_remove - Stop using a device clock for power management.
160 * @dev: Device whose clock should not be used for PM any more.
161 * @con_id: Connection ID of the clock.
163 * Remove the clock represented by @con_id from the list of clocks used for
164 * the power management of @dev.
166 void pm_clk_remove(struct device
*dev
, const char *con_id
)
168 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
169 struct pm_clock_entry
*ce
;
174 spin_lock_irq(&psd
->lock
);
176 list_for_each_entry(ce
, &psd
->clock_list
, node
) {
177 if (!con_id
&& !ce
->con_id
)
179 else if (!con_id
|| !ce
->con_id
)
181 else if (!strcmp(con_id
, ce
->con_id
))
185 spin_unlock_irq(&psd
->lock
);
190 spin_unlock_irq(&psd
->lock
);
196 * pm_clk_init - Initialize a device's list of power management clocks.
197 * @dev: Device to initialize the list of PM clocks for.
199 * Initialize the lock and clock_list members of the device's pm_subsys_data
202 void pm_clk_init(struct device
*dev
)
204 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
206 INIT_LIST_HEAD(&psd
->clock_list
);
210 * pm_clk_create - Create and initialize a device's list of PM clocks.
211 * @dev: Device to create and initialize the list of PM clocks for.
213 * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
214 * members and make the @dev's power.subsys_data field point to it.
216 int pm_clk_create(struct device
*dev
)
218 return dev_pm_get_subsys_data(dev
);
222 * pm_clk_destroy - Destroy a device's list of power management clocks.
223 * @dev: Device to destroy the list of PM clocks for.
225 * Clear the @dev's power.subsys_data field, remove the list of clock entries
226 * from the struct pm_subsys_data object pointed to by it before and free
229 void pm_clk_destroy(struct device
*dev
)
231 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
232 struct pm_clock_entry
*ce
, *c
;
233 struct list_head list
;
238 INIT_LIST_HEAD(&list
);
240 spin_lock_irq(&psd
->lock
);
242 list_for_each_entry_safe_reverse(ce
, c
, &psd
->clock_list
, node
)
243 list_move(&ce
->node
, &list
);
245 spin_unlock_irq(&psd
->lock
);
247 dev_pm_put_subsys_data(dev
);
249 list_for_each_entry_safe_reverse(ce
, c
, &list
, node
) {
256 * pm_clk_suspend - Disable clocks in a device's PM clock list.
257 * @dev: Device to disable the clocks for.
259 int pm_clk_suspend(struct device
*dev
)
261 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
262 struct pm_clock_entry
*ce
;
265 dev_dbg(dev
, "%s()\n", __func__
);
270 spin_lock_irqsave(&psd
->lock
, flags
);
272 list_for_each_entry_reverse(ce
, &psd
->clock_list
, node
) {
273 if (ce
->status
< PCE_STATUS_ERROR
) {
274 if (ce
->status
== PCE_STATUS_ENABLED
)
275 clk_disable(ce
->clk
);
276 ce
->status
= PCE_STATUS_ACQUIRED
;
280 spin_unlock_irqrestore(&psd
->lock
, flags
);
286 * pm_clk_resume - Enable clocks in a device's PM clock list.
287 * @dev: Device to enable the clocks for.
289 int pm_clk_resume(struct device
*dev
)
291 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
292 struct pm_clock_entry
*ce
;
295 dev_dbg(dev
, "%s()\n", __func__
);
300 spin_lock_irqsave(&psd
->lock
, flags
);
302 list_for_each_entry(ce
, &psd
->clock_list
, node
)
303 __pm_clk_enable(dev
, ce
);
305 spin_unlock_irqrestore(&psd
->lock
, flags
);
311 * pm_clk_notify - Notify routine for device addition and removal.
312 * @nb: Notifier block object this function is a member of.
313 * @action: Operation being carried out by the caller.
314 * @data: Device the routine is being run for.
316 * For this function to work, @nb must be a member of an object of type
317 * struct pm_clk_notifier_block containing all of the requisite data.
318 * Specifically, the pm_domain member of that object is copied to the device's
319 * pm_domain field and its con_ids member is used to populate the device's list
320 * of PM clocks, depending on @action.
322 * If the device's pm_domain field is already populated with a value different
323 * from the one stored in the struct pm_clk_notifier_block object, the function
326 static int pm_clk_notify(struct notifier_block
*nb
,
327 unsigned long action
, void *data
)
329 struct pm_clk_notifier_block
*clknb
;
330 struct device
*dev
= data
;
334 dev_dbg(dev
, "%s() %ld\n", __func__
, action
);
336 clknb
= container_of(nb
, struct pm_clk_notifier_block
, nb
);
339 case BUS_NOTIFY_ADD_DEVICE
:
343 error
= pm_clk_create(dev
);
347 dev
->pm_domain
= clknb
->pm_domain
;
348 if (clknb
->con_ids
[0]) {
349 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
350 pm_clk_add(dev
, *con_id
);
352 pm_clk_add(dev
, NULL
);
356 case BUS_NOTIFY_DEL_DEVICE
:
357 if (dev
->pm_domain
!= clknb
->pm_domain
)
360 dev
->pm_domain
= NULL
;
368 #else /* !CONFIG_PM */
371 * enable_clock - Enable a device clock.
372 * @dev: Device whose clock is to be enabled.
373 * @con_id: Connection ID of the clock.
375 static void enable_clock(struct device
*dev
, const char *con_id
)
379 clk
= clk_get(dev
, con_id
);
381 clk_prepare_enable(clk
);
383 dev_info(dev
, "Runtime PM disabled, clock forced on.\n");
388 * disable_clock - Disable a device clock.
389 * @dev: Device whose clock is to be disabled.
390 * @con_id: Connection ID of the clock.
392 static void disable_clock(struct device
*dev
, const char *con_id
)
396 clk
= clk_get(dev
, con_id
);
398 clk_disable_unprepare(clk
);
400 dev_info(dev
, "Runtime PM disabled, clock forced off.\n");
405 * pm_clk_notify - Notify routine for device addition and removal.
406 * @nb: Notifier block object this function is a member of.
407 * @action: Operation being carried out by the caller.
408 * @data: Device the routine is being run for.
410 * For this function to work, @nb must be a member of an object of type
411 * struct pm_clk_notifier_block containing all of the requisite data.
412 * Specifically, the con_ids member of that object is used to enable or disable
413 * the device's clocks, depending on @action.
415 static int pm_clk_notify(struct notifier_block
*nb
,
416 unsigned long action
, void *data
)
418 struct pm_clk_notifier_block
*clknb
;
419 struct device
*dev
= data
;
422 dev_dbg(dev
, "%s() %ld\n", __func__
, action
);
424 clknb
= container_of(nb
, struct pm_clk_notifier_block
, nb
);
427 case BUS_NOTIFY_BIND_DRIVER
:
428 if (clknb
->con_ids
[0]) {
429 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
430 enable_clock(dev
, *con_id
);
432 enable_clock(dev
, NULL
);
435 case BUS_NOTIFY_UNBOUND_DRIVER
:
436 if (clknb
->con_ids
[0]) {
437 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
438 disable_clock(dev
, *con_id
);
440 disable_clock(dev
, NULL
);
448 #endif /* !CONFIG_PM */
451 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
452 * @bus: Bus type to add the notifier to.
453 * @clknb: Notifier to be added to the given bus type.
455 * The nb member of @clknb is not expected to be initialized and its
456 * notifier_call member will be replaced with pm_clk_notify(). However,
457 * the remaining members of @clknb should be populated prior to calling this
460 void pm_clk_add_notifier(struct bus_type
*bus
,
461 struct pm_clk_notifier_block
*clknb
)
466 clknb
->nb
.notifier_call
= pm_clk_notify
;
467 bus_register_notifier(bus
, &clknb
->nb
);