IPoIB: Avoid reading an uninitialized member variable
[linux/fpc-iii.git] / drivers / base / power / clock_ops.c
blob60ee5591ee8f0d58d8229bad412d0f5882064e6a
1 /*
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.
7 */
9 #include <linux/kernel.h>
10 #include <linux/device.h>
11 #include <linux/io.h>
12 #include <linux/pm.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_runtime.h>
20 #ifdef CONFIG_PM_CLK
22 enum pce_status {
23 PCE_STATUS_NONE = 0,
24 PCE_STATUS_ACQUIRED,
25 PCE_STATUS_ENABLED,
26 PCE_STATUS_ERROR,
29 struct pm_clock_entry {
30 struct list_head node;
31 char *con_id;
32 struct clk *clk;
33 enum pce_status status;
36 /**
37 * pm_clk_enable - Enable a clock, reporting any errors
38 * @dev: The device for the given clock
39 * @ce: PM clock entry corresponding to the clock.
41 static inline void __pm_clk_enable(struct device *dev, struct pm_clock_entry *ce)
43 int ret;
45 if (ce->status < PCE_STATUS_ERROR) {
46 ret = clk_enable(ce->clk);
47 if (!ret)
48 ce->status = PCE_STATUS_ENABLED;
49 else
50 dev_err(dev, "%s: failed to enable clk %p, error %d\n",
51 __func__, ce->clk, ret);
55 /**
56 * pm_clk_acquire - Acquire a device clock.
57 * @dev: Device whose clock is to be acquired.
58 * @ce: PM clock entry corresponding to the clock.
60 static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
62 if (!ce->clk)
63 ce->clk = clk_get(dev, ce->con_id);
64 if (IS_ERR(ce->clk)) {
65 ce->status = PCE_STATUS_ERROR;
66 } else {
67 clk_prepare(ce->clk);
68 ce->status = PCE_STATUS_ACQUIRED;
69 dev_dbg(dev, "Clock %pC con_id %s managed by runtime PM.\n",
70 ce->clk, ce->con_id);
74 static int __pm_clk_add(struct device *dev, const char *con_id,
75 struct clk *clk)
77 struct pm_subsys_data *psd = dev_to_psd(dev);
78 struct pm_clock_entry *ce;
80 if (!psd)
81 return -EINVAL;
83 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
84 if (!ce)
85 return -ENOMEM;
87 if (con_id) {
88 ce->con_id = kstrdup(con_id, GFP_KERNEL);
89 if (!ce->con_id) {
90 dev_err(dev,
91 "Not enough memory for clock connection ID.\n");
92 kfree(ce);
93 return -ENOMEM;
95 } else {
96 if (IS_ERR(clk)) {
97 kfree(ce);
98 return -ENOENT;
100 ce->clk = 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);
108 return 0;
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 * The power-management code will take control of the clock reference, so
131 * callers should not call clk_put() on @clk after this function sucessfully
132 * returned.
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)
145 if (!ce)
146 return;
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);
154 clk_put(ce->clk);
158 kfree(ce->con_id);
159 kfree(ce);
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;
175 if (!psd)
176 return;
178 spin_lock_irq(&psd->lock);
180 list_for_each_entry(ce, &psd->clock_list, node) {
181 if (!con_id && !ce->con_id)
182 goto remove;
183 else if (!con_id || !ce->con_id)
184 continue;
185 else if (!strcmp(con_id, ce->con_id))
186 goto remove;
189 spin_unlock_irq(&psd->lock);
190 return;
192 remove:
193 list_del(&ce->node);
194 spin_unlock_irq(&psd->lock);
196 __pm_clk_remove(ce);
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
204 * object.
206 void pm_clk_init(struct device *dev)
208 struct pm_subsys_data *psd = dev_to_psd(dev);
209 if (psd)
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
231 * that object.
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;
239 if (!psd)
240 return;
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) {
254 list_del(&ce->node);
255 __pm_clk_remove(ce);
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;
267 unsigned long flags;
269 dev_dbg(dev, "%s()\n", __func__);
271 if (!psd)
272 return 0;
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);
286 return 0;
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;
297 unsigned long flags;
299 dev_dbg(dev, "%s()\n", __func__);
301 if (!psd)
302 return 0;
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);
311 return 0;
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
328 * does nothing.
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;
335 char **con_id;
336 int error;
338 dev_dbg(dev, "%s() %ld\n", __func__, action);
340 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
342 switch (action) {
343 case BUS_NOTIFY_ADD_DEVICE:
344 if (dev->pm_domain)
345 break;
347 error = pm_clk_create(dev);
348 if (error)
349 break;
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);
355 } else {
356 pm_clk_add(dev, NULL);
359 break;
360 case BUS_NOTIFY_DEL_DEVICE:
361 if (dev->pm_domain != clknb->pm_domain)
362 break;
364 dev->pm_domain = NULL;
365 pm_clk_destroy(dev);
366 break;
369 return 0;
372 int pm_clk_runtime_suspend(struct device *dev)
374 int ret;
376 dev_dbg(dev, "%s\n", __func__);
378 ret = pm_generic_runtime_suspend(dev);
379 if (ret) {
380 dev_err(dev, "failed to suspend device\n");
381 return ret;
384 ret = pm_clk_suspend(dev);
385 if (ret) {
386 dev_err(dev, "failed to suspend clock\n");
387 pm_generic_runtime_resume(dev);
388 return ret;
391 return 0;
394 int pm_clk_runtime_resume(struct device *dev)
396 int ret;
398 dev_dbg(dev, "%s\n", __func__);
400 ret = pm_clk_resume(dev);
401 if (ret) {
402 dev_err(dev, "failed to resume clock\n");
403 return ret;
406 return pm_generic_runtime_resume(dev);
409 #else /* !CONFIG_PM_CLK */
412 * enable_clock - Enable a device clock.
413 * @dev: Device whose clock is to be enabled.
414 * @con_id: Connection ID of the clock.
416 static void enable_clock(struct device *dev, const char *con_id)
418 struct clk *clk;
420 clk = clk_get(dev, con_id);
421 if (!IS_ERR(clk)) {
422 clk_prepare_enable(clk);
423 clk_put(clk);
424 dev_info(dev, "Runtime PM disabled, clock forced on.\n");
429 * disable_clock - Disable a device clock.
430 * @dev: Device whose clock is to be disabled.
431 * @con_id: Connection ID of the clock.
433 static void disable_clock(struct device *dev, const char *con_id)
435 struct clk *clk;
437 clk = clk_get(dev, con_id);
438 if (!IS_ERR(clk)) {
439 clk_disable_unprepare(clk);
440 clk_put(clk);
441 dev_info(dev, "Runtime PM disabled, clock forced off.\n");
446 * pm_clk_notify - Notify routine for device addition and removal.
447 * @nb: Notifier block object this function is a member of.
448 * @action: Operation being carried out by the caller.
449 * @data: Device the routine is being run for.
451 * For this function to work, @nb must be a member of an object of type
452 * struct pm_clk_notifier_block containing all of the requisite data.
453 * Specifically, the con_ids member of that object is used to enable or disable
454 * the device's clocks, depending on @action.
456 static int pm_clk_notify(struct notifier_block *nb,
457 unsigned long action, void *data)
459 struct pm_clk_notifier_block *clknb;
460 struct device *dev = data;
461 char **con_id;
463 dev_dbg(dev, "%s() %ld\n", __func__, action);
465 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
467 switch (action) {
468 case BUS_NOTIFY_BIND_DRIVER:
469 if (clknb->con_ids[0]) {
470 for (con_id = clknb->con_ids; *con_id; con_id++)
471 enable_clock(dev, *con_id);
472 } else {
473 enable_clock(dev, NULL);
475 break;
476 case BUS_NOTIFY_UNBOUND_DRIVER:
477 if (clknb->con_ids[0]) {
478 for (con_id = clknb->con_ids; *con_id; con_id++)
479 disable_clock(dev, *con_id);
480 } else {
481 disable_clock(dev, NULL);
483 break;
486 return 0;
489 #endif /* !CONFIG_PM_CLK */
492 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
493 * @bus: Bus type to add the notifier to.
494 * @clknb: Notifier to be added to the given bus type.
496 * The nb member of @clknb is not expected to be initialized and its
497 * notifier_call member will be replaced with pm_clk_notify(). However,
498 * the remaining members of @clknb should be populated prior to calling this
499 * routine.
501 void pm_clk_add_notifier(struct bus_type *bus,
502 struct pm_clk_notifier_block *clknb)
504 if (!bus || !clknb)
505 return;
507 clknb->nb.notifier_call = pm_clk_notify;
508 bus_register_notifier(bus, &clknb->nb);