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);
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");
98 if (IS_ERR(ce->clk) || !__clk_get(clk)) {
105 pm_clk_acquire(dev, ce);
107 spin_lock_irq(&psd->lock);
108 list_add_tail(&ce->node, &psd->clock_list);
109 spin_unlock_irq(&psd->lock);
114 * pm_clk_add - Start using a device clock for power management.
115 * @dev: Device whose clock is going to be used for power management.
116 * @con_id: Connection ID of the clock.
118 * Add the clock represented by @con_id to the list of clocks used for
119 * the power management of @dev.
121 int pm_clk_add(struct device *dev, const char *con_id)
123 return __pm_clk_add(dev, con_id, NULL);
127 * pm_clk_add_clk - Start using a device clock for power management.
128 * @dev: Device whose clock is going to be used for power management.
129 * @clk: Clock pointer
131 * Add the clock to the list of clocks used for the power management of @dev.
132 * It will increment refcount on clock pointer, use clk_put() on it when done.
134 int pm_clk_add_clk(struct device *dev, struct clk *clk)
136 return __pm_clk_add(dev, NULL, clk);
140 * __pm_clk_remove - Destroy PM clock entry.
141 * @ce: PM clock entry to destroy.
143 static void __pm_clk_remove(struct pm_clock_entry *ce)
148 if (ce->status < PCE_STATUS_ERROR) {
149 if (ce->status == PCE_STATUS_ENABLED)
150 clk_disable(ce->clk);
152 if (ce->status >= PCE_STATUS_ACQUIRED) {
153 clk_unprepare(ce->clk);
163 * pm_clk_remove - Stop using a device clock for power management.
164 * @dev: Device whose clock should not be used for PM any more.
165 * @con_id: Connection ID of the clock.
167 * Remove the clock represented by @con_id from the list of clocks used for
168 * the power management of @dev.
170 void pm_clk_remove(struct device *dev, const char *con_id)
172 struct pm_subsys_data *psd = dev_to_psd(dev);
173 struct pm_clock_entry *ce;
178 spin_lock_irq(&psd->lock);
180 list_for_each_entry(ce, &psd->clock_list, node) {
181 if (!con_id && !ce->con_id)
183 else if (!con_id || !ce->con_id)
185 else if (!strcmp(con_id, ce->con_id))
189 spin_unlock_irq(&psd->lock);
194 spin_unlock_irq(&psd->lock);
200 * pm_clk_init - Initialize a device's list of power management clocks.
201 * @dev: Device to initialize the list of PM clocks for.
203 * Initialize the lock and clock_list members of the device's pm_subsys_data
206 void pm_clk_init(struct device *dev)
208 struct pm_subsys_data *psd = dev_to_psd(dev);
210 INIT_LIST_HEAD(&psd->clock_list);
214 * pm_clk_create - Create and initialize a device's list of PM clocks.
215 * @dev: Device to create and initialize the list of PM clocks for.
217 * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
218 * members and make the @dev's power.subsys_data field point to it.
220 int pm_clk_create(struct device *dev)
222 return dev_pm_get_subsys_data(dev);
226 * pm_clk_destroy - Destroy a device's list of power management clocks.
227 * @dev: Device to destroy the list of PM clocks for.
229 * Clear the @dev's power.subsys_data field, remove the list of clock entries
230 * from the struct pm_subsys_data object pointed to by it before and free
233 void pm_clk_destroy(struct device *dev)
235 struct pm_subsys_data *psd = dev_to_psd(dev);
236 struct pm_clock_entry *ce, *c;
237 struct list_head list;
242 INIT_LIST_HEAD(&list);
244 spin_lock_irq(&psd->lock);
246 list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
247 list_move(&ce->node, &list);
249 spin_unlock_irq(&psd->lock);
251 dev_pm_put_subsys_data(dev);
253 list_for_each_entry_safe_reverse(ce, c, &list, node) {
260 * pm_clk_suspend - Disable clocks in a device's PM clock list.
261 * @dev: Device to disable the clocks for.
263 int pm_clk_suspend(struct device *dev)
265 struct pm_subsys_data *psd = dev_to_psd(dev);
266 struct pm_clock_entry *ce;
269 dev_dbg(dev, "%s()\n", __func__);
274 spin_lock_irqsave(&psd->lock, flags);
276 list_for_each_entry_reverse(ce, &psd->clock_list, node) {
277 if (ce->status < PCE_STATUS_ERROR) {
278 if (ce->status == PCE_STATUS_ENABLED)
279 clk_disable(ce->clk);
280 ce->status = PCE_STATUS_ACQUIRED;
284 spin_unlock_irqrestore(&psd->lock, flags);
290 * pm_clk_resume - Enable clocks in a device's PM clock list.
291 * @dev: Device to enable the clocks for.
293 int pm_clk_resume(struct device *dev)
295 struct pm_subsys_data *psd = dev_to_psd(dev);
296 struct pm_clock_entry *ce;
299 dev_dbg(dev, "%s()\n", __func__);
304 spin_lock_irqsave(&psd->lock, flags);
306 list_for_each_entry(ce, &psd->clock_list, node)
307 __pm_clk_enable(dev, ce);
309 spin_unlock_irqrestore(&psd->lock, flags);
315 * pm_clk_notify - Notify routine for device addition and removal.
316 * @nb: Notifier block object this function is a member of.
317 * @action: Operation being carried out by the caller.
318 * @data: Device the routine is being run for.
320 * For this function to work, @nb must be a member of an object of type
321 * struct pm_clk_notifier_block containing all of the requisite data.
322 * Specifically, the pm_domain member of that object is copied to the device's
323 * pm_domain field and its con_ids member is used to populate the device's list
324 * of PM clocks, depending on @action.
326 * If the device's pm_domain field is already populated with a value different
327 * from the one stored in the struct pm_clk_notifier_block object, the function
330 static int pm_clk_notify(struct notifier_block *nb,
331 unsigned long action, void *data)
333 struct pm_clk_notifier_block *clknb;
334 struct device *dev = data;
338 dev_dbg(dev, "%s() %ld\n", __func__, action);
340 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
343 case BUS_NOTIFY_ADD_DEVICE:
347 error = pm_clk_create(dev);
351 dev->pm_domain = clknb->pm_domain;
352 if (clknb->con_ids[0]) {
353 for (con_id = clknb->con_ids; *con_id; con_id++)
354 pm_clk_add(dev, *con_id);
356 pm_clk_add(dev, NULL);
360 case BUS_NOTIFY_DEL_DEVICE:
361 if (dev->pm_domain != clknb->pm_domain)
364 dev->pm_domain = NULL;
372 #else /* !CONFIG_PM */
375 * enable_clock - Enable a device clock.
376 * @dev: Device whose clock is to be enabled.
377 * @con_id: Connection ID of the clock.
379 static void enable_clock(struct device *dev, const char *con_id)
383 clk = clk_get(dev, con_id);
385 clk_prepare_enable(clk);
387 dev_info(dev, "Runtime PM disabled, clock forced on.\n");
392 * disable_clock - Disable a device clock.
393 * @dev: Device whose clock is to be disabled.
394 * @con_id: Connection ID of the clock.
396 static void disable_clock(struct device *dev, const char *con_id)
400 clk = clk_get(dev, con_id);
402 clk_disable_unprepare(clk);
404 dev_info(dev, "Runtime PM disabled, clock forced off.\n");
409 * pm_clk_notify - Notify routine for device addition and removal.
410 * @nb: Notifier block object this function is a member of.
411 * @action: Operation being carried out by the caller.
412 * @data: Device the routine is being run for.
414 * For this function to work, @nb must be a member of an object of type
415 * struct pm_clk_notifier_block containing all of the requisite data.
416 * Specifically, the con_ids member of that object is used to enable or disable
417 * the device's clocks, depending on @action.
419 static int pm_clk_notify(struct notifier_block *nb,
420 unsigned long action, void *data)
422 struct pm_clk_notifier_block *clknb;
423 struct device *dev = data;
426 dev_dbg(dev, "%s() %ld\n", __func__, action);
428 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
431 case BUS_NOTIFY_BIND_DRIVER:
432 if (clknb->con_ids[0]) {
433 for (con_id = clknb->con_ids; *con_id; con_id++)
434 enable_clock(dev, *con_id);
436 enable_clock(dev, NULL);
439 case BUS_NOTIFY_UNBOUND_DRIVER:
440 if (clknb->con_ids[0]) {
441 for (con_id = clknb->con_ids; *con_id; con_id++)
442 disable_clock(dev, *con_id);
444 disable_clock(dev, NULL);
452 #endif /* !CONFIG_PM */
455 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
456 * @bus: Bus type to add the notifier to.
457 * @clknb: Notifier to be added to the given bus type.
459 * The nb member of @clknb is not expected to be initialized and its
460 * notifier_call member will be replaced with pm_clk_notify(). However,
461 * the remaining members of @clknb should be populated prior to calling this
464 void pm_clk_add_notifier(struct bus_type *bus,
465 struct pm_clk_notifier_block *clknb)
470 clknb->nb.notifier_call = pm_clk_notify;
471 bus_register_notifier(bus, &clknb->nb);