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 int __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
);
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
;
69 ce
->status
= PCE_STATUS_ACQUIRED
;
70 dev_dbg(dev
, "Clock %s managed by runtime PM.\n", ce
->con_id
);
74 static 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
);
88 ce
->con_id
= kstrdup(con_id
, GFP_KERNEL
);
91 "Not enough memory for clock connection ID.\n");
96 if (IS_ERR(ce
->clk
) || !__clk_get(clk
)) {
103 pm_clk_acquire(dev
, ce
);
105 spin_lock_irq(&psd
->lock
);
106 list_add_tail(&ce
->node
, &psd
->clock_list
);
107 spin_unlock_irq(&psd
->lock
);
112 * pm_clk_add - Start using a device clock for power management.
113 * @dev: Device whose clock is going to be used for power management.
114 * @con_id: Connection ID of the clock.
116 * Add the clock represented by @con_id to the list of clocks used for
117 * the power management of @dev.
119 int pm_clk_add(struct device
*dev
, const char *con_id
)
121 return __pm_clk_add(dev
, con_id
, NULL
);
125 * pm_clk_add_clk - Start using a device clock for power management.
126 * @dev: Device whose clock is going to be used for power management.
127 * @clk: Clock pointer
129 * Add the clock to the list of clocks used for the power management of @dev.
130 * It will increment refcount on clock pointer, use clk_put() on it when done.
132 int pm_clk_add_clk(struct device
*dev
, struct clk
*clk
)
134 return __pm_clk_add(dev
, NULL
, clk
);
138 * __pm_clk_remove - Destroy PM clock entry.
139 * @ce: PM clock entry to destroy.
141 static void __pm_clk_remove(struct pm_clock_entry
*ce
)
146 if (ce
->status
< PCE_STATUS_ERROR
) {
147 if (ce
->status
== PCE_STATUS_ENABLED
)
148 clk_disable(ce
->clk
);
150 if (ce
->status
>= PCE_STATUS_ACQUIRED
) {
151 clk_unprepare(ce
->clk
);
161 * pm_clk_remove - Stop using a device clock for power management.
162 * @dev: Device whose clock should not be used for PM any more.
163 * @con_id: Connection ID of the clock.
165 * Remove the clock represented by @con_id from the list of clocks used for
166 * the power management of @dev.
168 void pm_clk_remove(struct device
*dev
, const char *con_id
)
170 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
171 struct pm_clock_entry
*ce
;
176 spin_lock_irq(&psd
->lock
);
178 list_for_each_entry(ce
, &psd
->clock_list
, node
) {
179 if (!con_id
&& !ce
->con_id
)
181 else if (!con_id
|| !ce
->con_id
)
183 else if (!strcmp(con_id
, ce
->con_id
))
187 spin_unlock_irq(&psd
->lock
);
192 spin_unlock_irq(&psd
->lock
);
198 * pm_clk_init - Initialize a device's list of power management clocks.
199 * @dev: Device to initialize the list of PM clocks for.
201 * Initialize the lock and clock_list members of the device's pm_subsys_data
204 void pm_clk_init(struct device
*dev
)
206 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
208 INIT_LIST_HEAD(&psd
->clock_list
);
212 * pm_clk_create - Create and initialize a device's list of PM clocks.
213 * @dev: Device to create and initialize the list of PM clocks for.
215 * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
216 * members and make the @dev's power.subsys_data field point to it.
218 int pm_clk_create(struct device
*dev
)
220 return dev_pm_get_subsys_data(dev
);
224 * pm_clk_destroy - Destroy a device's list of power management clocks.
225 * @dev: Device to destroy the list of PM clocks for.
227 * Clear the @dev's power.subsys_data field, remove the list of clock entries
228 * from the struct pm_subsys_data object pointed to by it before and free
231 void pm_clk_destroy(struct device
*dev
)
233 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
234 struct pm_clock_entry
*ce
, *c
;
235 struct list_head list
;
240 INIT_LIST_HEAD(&list
);
242 spin_lock_irq(&psd
->lock
);
244 list_for_each_entry_safe_reverse(ce
, c
, &psd
->clock_list
, node
)
245 list_move(&ce
->node
, &list
);
247 spin_unlock_irq(&psd
->lock
);
249 dev_pm_put_subsys_data(dev
);
251 list_for_each_entry_safe_reverse(ce
, c
, &list
, node
) {
258 * pm_clk_suspend - Disable clocks in a device's PM clock list.
259 * @dev: Device to disable the clocks for.
261 int pm_clk_suspend(struct device
*dev
)
263 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
264 struct pm_clock_entry
*ce
;
267 dev_dbg(dev
, "%s()\n", __func__
);
272 spin_lock_irqsave(&psd
->lock
, flags
);
274 list_for_each_entry_reverse(ce
, &psd
->clock_list
, node
) {
275 if (ce
->status
< PCE_STATUS_ERROR
) {
276 if (ce
->status
== PCE_STATUS_ENABLED
)
277 clk_disable(ce
->clk
);
278 ce
->status
= PCE_STATUS_ACQUIRED
;
282 spin_unlock_irqrestore(&psd
->lock
, flags
);
288 * pm_clk_resume - Enable clocks in a device's PM clock list.
289 * @dev: Device to enable the clocks for.
291 int pm_clk_resume(struct device
*dev
)
293 struct pm_subsys_data
*psd
= dev_to_psd(dev
);
294 struct pm_clock_entry
*ce
;
297 dev_dbg(dev
, "%s()\n", __func__
);
302 spin_lock_irqsave(&psd
->lock
, flags
);
304 list_for_each_entry(ce
, &psd
->clock_list
, node
)
305 __pm_clk_enable(dev
, ce
);
307 spin_unlock_irqrestore(&psd
->lock
, flags
);
313 * pm_clk_notify - Notify routine for device addition and removal.
314 * @nb: Notifier block object this function is a member of.
315 * @action: Operation being carried out by the caller.
316 * @data: Device the routine is being run for.
318 * For this function to work, @nb must be a member of an object of type
319 * struct pm_clk_notifier_block containing all of the requisite data.
320 * Specifically, the pm_domain member of that object is copied to the device's
321 * pm_domain field and its con_ids member is used to populate the device's list
322 * of PM clocks, depending on @action.
324 * If the device's pm_domain field is already populated with a value different
325 * from the one stored in the struct pm_clk_notifier_block object, the function
328 static int pm_clk_notify(struct notifier_block
*nb
,
329 unsigned long action
, void *data
)
331 struct pm_clk_notifier_block
*clknb
;
332 struct device
*dev
= data
;
336 dev_dbg(dev
, "%s() %ld\n", __func__
, action
);
338 clknb
= container_of(nb
, struct pm_clk_notifier_block
, nb
);
341 case BUS_NOTIFY_ADD_DEVICE
:
345 error
= pm_clk_create(dev
);
349 dev
->pm_domain
= clknb
->pm_domain
;
350 if (clknb
->con_ids
[0]) {
351 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
352 pm_clk_add(dev
, *con_id
);
354 pm_clk_add(dev
, NULL
);
358 case BUS_NOTIFY_DEL_DEVICE
:
359 if (dev
->pm_domain
!= clknb
->pm_domain
)
362 dev
->pm_domain
= NULL
;
370 #else /* !CONFIG_PM */
373 * enable_clock - Enable a device clock.
374 * @dev: Device whose clock is to be enabled.
375 * @con_id: Connection ID of the clock.
377 static void enable_clock(struct device
*dev
, const char *con_id
)
381 clk
= clk_get(dev
, con_id
);
383 clk_prepare_enable(clk
);
385 dev_info(dev
, "Runtime PM disabled, clock forced on.\n");
390 * disable_clock - Disable a device clock.
391 * @dev: Device whose clock is to be disabled.
392 * @con_id: Connection ID of the clock.
394 static void disable_clock(struct device
*dev
, const char *con_id
)
398 clk
= clk_get(dev
, con_id
);
400 clk_disable_unprepare(clk
);
402 dev_info(dev
, "Runtime PM disabled, clock forced off.\n");
407 * pm_clk_notify - Notify routine for device addition and removal.
408 * @nb: Notifier block object this function is a member of.
409 * @action: Operation being carried out by the caller.
410 * @data: Device the routine is being run for.
412 * For this function to work, @nb must be a member of an object of type
413 * struct pm_clk_notifier_block containing all of the requisite data.
414 * Specifically, the con_ids member of that object is used to enable or disable
415 * the device's clocks, depending on @action.
417 static int pm_clk_notify(struct notifier_block
*nb
,
418 unsigned long action
, void *data
)
420 struct pm_clk_notifier_block
*clknb
;
421 struct device
*dev
= data
;
424 dev_dbg(dev
, "%s() %ld\n", __func__
, action
);
426 clknb
= container_of(nb
, struct pm_clk_notifier_block
, nb
);
429 case BUS_NOTIFY_BIND_DRIVER
:
430 if (clknb
->con_ids
[0]) {
431 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
432 enable_clock(dev
, *con_id
);
434 enable_clock(dev
, NULL
);
437 case BUS_NOTIFY_UNBOUND_DRIVER
:
438 if (clknb
->con_ids
[0]) {
439 for (con_id
= clknb
->con_ids
; *con_id
; con_id
++)
440 disable_clock(dev
, *con_id
);
442 disable_clock(dev
, NULL
);
450 #endif /* !CONFIG_PM */
453 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
454 * @bus: Bus type to add the notifier to.
455 * @clknb: Notifier to be added to the given bus type.
457 * The nb member of @clknb is not expected to be initialized and its
458 * notifier_call member will be replaced with pm_clk_notify(). However,
459 * the remaining members of @clknb should be populated prior to calling this
462 void pm_clk_add_notifier(struct bus_type
*bus
,
463 struct pm_clk_notifier_block
*clknb
)
468 clknb
->nb
.notifier_call
= pm_clk_notify
;
469 bus_register_notifier(bus
, &clknb
->nb
);