PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / base / power / clock_ops.c
blobe870bbe9ec4e674d91fb3bede6e4105fd25ccd6a
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/init.h>
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/io.h>
13 #include <linux/pm.h>
14 #include <linux/pm_clock.h>
15 #include <linux/clk.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
19 #ifdef CONFIG_PM
21 enum pce_status {
22 PCE_STATUS_NONE = 0,
23 PCE_STATUS_ACQUIRED,
24 PCE_STATUS_ENABLED,
25 PCE_STATUS_ERROR,
28 struct pm_clock_entry {
29 struct list_head node;
30 char *con_id;
31 struct clk *clk;
32 enum pce_status status;
35 /**
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);
43 if (ret)
44 dev_err(dev, "%s: failed to enable clk %p, error %d\n",
45 __func__, clk, ret);
47 return ret;
50 /**
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;
60 } else {
61 clk_prepare(ce->clk);
62 ce->status = PCE_STATUS_ACQUIRED;
63 dev_dbg(dev, "Clock %s managed by runtime PM.\n", ce->con_id);
67 /**
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;
80 if (!psd)
81 return -EINVAL;
83 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
84 if (!ce) {
85 dev_err(dev, "Not enough memory for clock entry.\n");
86 return -ENOMEM;
89 if (con_id) {
90 ce->con_id = kstrdup(con_id, GFP_KERNEL);
91 if (!ce->con_id) {
92 dev_err(dev,
93 "Not enough memory for clock connection ID.\n");
94 kfree(ce);
95 return -ENOMEM;
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);
104 return 0;
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)
113 if (!ce)
114 return;
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);
122 clk_put(ce->clk);
126 kfree(ce->con_id);
127 kfree(ce);
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;
143 if (!psd)
144 return;
146 spin_lock_irq(&psd->lock);
148 list_for_each_entry(ce, &psd->clock_list, node) {
149 if (!con_id && !ce->con_id)
150 goto remove;
151 else if (!con_id || !ce->con_id)
152 continue;
153 else if (!strcmp(con_id, ce->con_id))
154 goto remove;
157 spin_unlock_irq(&psd->lock);
158 return;
160 remove:
161 list_del(&ce->node);
162 spin_unlock_irq(&psd->lock);
164 __pm_clk_remove(ce);
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
172 * object.
174 void pm_clk_init(struct device *dev)
176 struct pm_subsys_data *psd = dev_to_psd(dev);
177 if (psd)
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
199 * that object.
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;
207 if (!psd)
208 return;
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) {
222 list_del(&ce->node);
223 __pm_clk_remove(ce);
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;
239 unsigned long flags;
241 dev_dbg(dev, "%s()\n", __func__);
243 if (!psd)
244 return 0;
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);
258 return 0;
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;
269 unsigned long flags;
270 int ret;
272 dev_dbg(dev, "%s()\n", __func__);
274 if (!psd)
275 return 0;
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);
282 if (!ret)
283 ce->status = PCE_STATUS_ENABLED;
287 spin_unlock_irqrestore(&psd->lock, flags);
289 return 0;
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
306 * does nothing.
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;
313 char **con_id;
314 int error;
316 dev_dbg(dev, "%s() %ld\n", __func__, action);
318 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
320 switch (action) {
321 case BUS_NOTIFY_ADD_DEVICE:
322 if (dev->pm_domain)
323 break;
325 error = pm_clk_create(dev);
326 if (error)
327 break;
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);
333 } else {
334 pm_clk_add(dev, NULL);
337 break;
338 case BUS_NOTIFY_DEL_DEVICE:
339 if (dev->pm_domain != clknb->pm_domain)
340 break;
342 dev->pm_domain = NULL;
343 pm_clk_destroy(dev);
344 break;
347 return 0;
350 #else /* !CONFIG_PM_RUNTIME */
352 #ifdef CONFIG_PM
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;
362 unsigned long flags;
364 dev_dbg(dev, "%s()\n", __func__);
366 /* If there is no driver, the clocks are already disabled. */
367 if (!psd || !dev->driver)
368 return 0;
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);
377 return 0;
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;
388 unsigned long flags;
390 dev_dbg(dev, "%s()\n", __func__);
392 /* If there is no driver, the clocks should remain disabled. */
393 if (!psd || !dev->driver)
394 return 0;
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);
403 return 0;
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)
415 struct clk *clk;
417 clk = clk_get(dev, con_id);
418 if (!IS_ERR(clk)) {
419 clk_prepare_enable(clk);
420 clk_put(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)
432 struct clk *clk;
434 clk = clk_get(dev, con_id);
435 if (!IS_ERR(clk)) {
436 clk_disable_unprepare(clk);
437 clk_put(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;
458 char **con_id;
460 dev_dbg(dev, "%s() %ld\n", __func__, action);
462 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
464 switch (action) {
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);
469 } else {
470 enable_clock(dev, NULL);
472 break;
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);
477 } else {
478 disable_clock(dev, NULL);
480 break;
483 return 0;
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
496 * routine.
498 void pm_clk_add_notifier(struct bus_type *bus,
499 struct pm_clk_notifier_block *clknb)
501 if (!bus || !clknb)
502 return;
504 clknb->nb.notifier_call = pm_clk_notify;
505 bus_register_notifier(bus, &clknb->nb);