dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / base / power / clock_ops.c
blob365ad751ce0f34ae2cb447dc7c21acb2cc02740e
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_domain.h>
19 #include <linux/pm_runtime.h>
21 #ifdef CONFIG_PM_CLK
23 enum pce_status {
24 PCE_STATUS_NONE = 0,
25 PCE_STATUS_ACQUIRED,
26 PCE_STATUS_ENABLED,
27 PCE_STATUS_ERROR,
30 struct pm_clock_entry {
31 struct list_head node;
32 char *con_id;
33 struct clk *clk;
34 enum pce_status status;
37 /**
38 * pm_clk_enable - Enable a clock, reporting any errors
39 * @dev: The device for the given clock
40 * @ce: PM clock entry corresponding to the clock.
42 static inline void __pm_clk_enable(struct device *dev, struct pm_clock_entry *ce)
44 int ret;
46 if (ce->status < PCE_STATUS_ERROR) {
47 ret = clk_enable(ce->clk);
48 if (!ret)
49 ce->status = PCE_STATUS_ENABLED;
50 else
51 dev_err(dev, "%s: failed to enable clk %p, error %d\n",
52 __func__, ce->clk, ret);
56 /**
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)
63 if (!ce->clk)
64 ce->clk = clk_get(dev, ce->con_id);
65 if (IS_ERR(ce->clk)) {
66 ce->status = PCE_STATUS_ERROR;
67 } else {
68 if (clk_prepare(ce->clk)) {
69 ce->status = PCE_STATUS_ERROR;
70 dev_err(dev, "clk_prepare() failed\n");
71 } else {
72 ce->status = PCE_STATUS_ACQUIRED;
73 dev_dbg(dev,
74 "Clock %pC con_id %s managed by runtime PM.\n",
75 ce->clk, ce->con_id);
80 static int __pm_clk_add(struct device *dev, const char *con_id,
81 struct clk *clk)
83 struct pm_subsys_data *psd = dev_to_psd(dev);
84 struct pm_clock_entry *ce;
86 if (!psd)
87 return -EINVAL;
89 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
90 if (!ce)
91 return -ENOMEM;
93 if (con_id) {
94 ce->con_id = kstrdup(con_id, GFP_KERNEL);
95 if (!ce->con_id) {
96 dev_err(dev,
97 "Not enough memory for clock connection ID.\n");
98 kfree(ce);
99 return -ENOMEM;
101 } else {
102 if (IS_ERR(clk)) {
103 kfree(ce);
104 return -ENOENT;
106 ce->clk = clk;
109 pm_clk_acquire(dev, ce);
111 spin_lock_irq(&psd->lock);
112 list_add_tail(&ce->node, &psd->clock_list);
113 spin_unlock_irq(&psd->lock);
114 return 0;
118 * pm_clk_add - Start using a device clock for power management.
119 * @dev: Device whose clock is going to be used for power management.
120 * @con_id: Connection ID of the clock.
122 * Add the clock represented by @con_id to the list of clocks used for
123 * the power management of @dev.
125 int pm_clk_add(struct device *dev, const char *con_id)
127 return __pm_clk_add(dev, con_id, NULL);
129 EXPORT_SYMBOL_GPL(pm_clk_add);
132 * pm_clk_add_clk - Start using a device clock for power management.
133 * @dev: Device whose clock is going to be used for power management.
134 * @clk: Clock pointer
136 * Add the clock to the list of clocks used for the power management of @dev.
137 * The power-management code will take control of the clock reference, so
138 * callers should not call clk_put() on @clk after this function sucessfully
139 * returned.
141 int pm_clk_add_clk(struct device *dev, struct clk *clk)
143 return __pm_clk_add(dev, NULL, clk);
145 EXPORT_SYMBOL_GPL(pm_clk_add_clk);
149 * of_pm_clk_add_clk - Start using a device clock for power management.
150 * @dev: Device whose clock is going to be used for power management.
151 * @name: Name of clock that is going to be used for power management.
153 * Add the clock described in the 'clocks' device-tree node that matches
154 * with the 'name' provided, to the list of clocks used for the power
155 * management of @dev. On success, returns 0. Returns a negative error
156 * code if the clock is not found or cannot be added.
158 int of_pm_clk_add_clk(struct device *dev, const char *name)
160 struct clk *clk;
161 int ret;
163 if (!dev || !dev->of_node || !name)
164 return -EINVAL;
166 clk = of_clk_get_by_name(dev->of_node, name);
167 if (IS_ERR(clk))
168 return PTR_ERR(clk);
170 ret = pm_clk_add_clk(dev, clk);
171 if (ret) {
172 clk_put(clk);
173 return ret;
176 return 0;
178 EXPORT_SYMBOL_GPL(of_pm_clk_add_clk);
181 * of_pm_clk_add_clks - Start using device clock(s) for power management.
182 * @dev: Device whose clock(s) is going to be used for power management.
184 * Add a series of clocks described in the 'clocks' device-tree node for
185 * a device to the list of clocks used for the power management of @dev.
186 * On success, returns the number of clocks added. Returns a negative
187 * error code if there are no clocks in the device node for the device
188 * or if adding a clock fails.
190 int of_pm_clk_add_clks(struct device *dev)
192 struct clk **clks;
193 int i, count;
194 int ret;
196 if (!dev || !dev->of_node)
197 return -EINVAL;
199 count = of_count_phandle_with_args(dev->of_node, "clocks",
200 "#clock-cells");
201 if (count <= 0)
202 return -ENODEV;
204 clks = kcalloc(count, sizeof(*clks), GFP_KERNEL);
205 if (!clks)
206 return -ENOMEM;
208 for (i = 0; i < count; i++) {
209 clks[i] = of_clk_get(dev->of_node, i);
210 if (IS_ERR(clks[i])) {
211 ret = PTR_ERR(clks[i]);
212 goto error;
215 ret = pm_clk_add_clk(dev, clks[i]);
216 if (ret) {
217 clk_put(clks[i]);
218 goto error;
222 kfree(clks);
224 return i;
226 error:
227 while (i--)
228 pm_clk_remove_clk(dev, clks[i]);
230 kfree(clks);
232 return ret;
234 EXPORT_SYMBOL_GPL(of_pm_clk_add_clks);
237 * __pm_clk_remove - Destroy PM clock entry.
238 * @ce: PM clock entry to destroy.
240 static void __pm_clk_remove(struct pm_clock_entry *ce)
242 if (!ce)
243 return;
245 if (ce->status < PCE_STATUS_ERROR) {
246 if (ce->status == PCE_STATUS_ENABLED)
247 clk_disable(ce->clk);
249 if (ce->status >= PCE_STATUS_ACQUIRED) {
250 clk_unprepare(ce->clk);
251 clk_put(ce->clk);
255 kfree(ce->con_id);
256 kfree(ce);
260 * pm_clk_remove - Stop using a device clock for power management.
261 * @dev: Device whose clock should not be used for PM any more.
262 * @con_id: Connection ID of the clock.
264 * Remove the clock represented by @con_id from the list of clocks used for
265 * the power management of @dev.
267 void pm_clk_remove(struct device *dev, const char *con_id)
269 struct pm_subsys_data *psd = dev_to_psd(dev);
270 struct pm_clock_entry *ce;
272 if (!psd)
273 return;
275 spin_lock_irq(&psd->lock);
277 list_for_each_entry(ce, &psd->clock_list, node) {
278 if (!con_id && !ce->con_id)
279 goto remove;
280 else if (!con_id || !ce->con_id)
281 continue;
282 else if (!strcmp(con_id, ce->con_id))
283 goto remove;
286 spin_unlock_irq(&psd->lock);
287 return;
289 remove:
290 list_del(&ce->node);
291 spin_unlock_irq(&psd->lock);
293 __pm_clk_remove(ce);
295 EXPORT_SYMBOL_GPL(pm_clk_remove);
298 * pm_clk_remove_clk - Stop using a device clock for power management.
299 * @dev: Device whose clock should not be used for PM any more.
300 * @clk: Clock pointer
302 * Remove the clock pointed to by @clk from the list of clocks used for
303 * the power management of @dev.
305 void pm_clk_remove_clk(struct device *dev, struct clk *clk)
307 struct pm_subsys_data *psd = dev_to_psd(dev);
308 struct pm_clock_entry *ce;
310 if (!psd || !clk)
311 return;
313 spin_lock_irq(&psd->lock);
315 list_for_each_entry(ce, &psd->clock_list, node) {
316 if (clk == ce->clk)
317 goto remove;
320 spin_unlock_irq(&psd->lock);
321 return;
323 remove:
324 list_del(&ce->node);
325 spin_unlock_irq(&psd->lock);
327 __pm_clk_remove(ce);
329 EXPORT_SYMBOL_GPL(pm_clk_remove_clk);
332 * pm_clk_init - Initialize a device's list of power management clocks.
333 * @dev: Device to initialize the list of PM clocks for.
335 * Initialize the lock and clock_list members of the device's pm_subsys_data
336 * object.
338 void pm_clk_init(struct device *dev)
340 struct pm_subsys_data *psd = dev_to_psd(dev);
341 if (psd)
342 INIT_LIST_HEAD(&psd->clock_list);
344 EXPORT_SYMBOL_GPL(pm_clk_init);
347 * pm_clk_create - Create and initialize a device's list of PM clocks.
348 * @dev: Device to create and initialize the list of PM clocks for.
350 * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
351 * members and make the @dev's power.subsys_data field point to it.
353 int pm_clk_create(struct device *dev)
355 return dev_pm_get_subsys_data(dev);
357 EXPORT_SYMBOL_GPL(pm_clk_create);
360 * pm_clk_destroy - Destroy a device's list of power management clocks.
361 * @dev: Device to destroy the list of PM clocks for.
363 * Clear the @dev's power.subsys_data field, remove the list of clock entries
364 * from the struct pm_subsys_data object pointed to by it before and free
365 * that object.
367 void pm_clk_destroy(struct device *dev)
369 struct pm_subsys_data *psd = dev_to_psd(dev);
370 struct pm_clock_entry *ce, *c;
371 struct list_head list;
373 if (!psd)
374 return;
376 INIT_LIST_HEAD(&list);
378 spin_lock_irq(&psd->lock);
380 list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
381 list_move(&ce->node, &list);
383 spin_unlock_irq(&psd->lock);
385 dev_pm_put_subsys_data(dev);
387 list_for_each_entry_safe_reverse(ce, c, &list, node) {
388 list_del(&ce->node);
389 __pm_clk_remove(ce);
392 EXPORT_SYMBOL_GPL(pm_clk_destroy);
395 * pm_clk_suspend - Disable clocks in a device's PM clock list.
396 * @dev: Device to disable the clocks for.
398 int pm_clk_suspend(struct device *dev)
400 struct pm_subsys_data *psd = dev_to_psd(dev);
401 struct pm_clock_entry *ce;
402 unsigned long flags;
404 dev_dbg(dev, "%s()\n", __func__);
406 if (!psd)
407 return 0;
409 spin_lock_irqsave(&psd->lock, flags);
411 list_for_each_entry_reverse(ce, &psd->clock_list, node) {
412 if (ce->status < PCE_STATUS_ERROR) {
413 if (ce->status == PCE_STATUS_ENABLED)
414 clk_disable(ce->clk);
415 ce->status = PCE_STATUS_ACQUIRED;
419 spin_unlock_irqrestore(&psd->lock, flags);
421 return 0;
423 EXPORT_SYMBOL_GPL(pm_clk_suspend);
426 * pm_clk_resume - Enable clocks in a device's PM clock list.
427 * @dev: Device to enable the clocks for.
429 int pm_clk_resume(struct device *dev)
431 struct pm_subsys_data *psd = dev_to_psd(dev);
432 struct pm_clock_entry *ce;
433 unsigned long flags;
435 dev_dbg(dev, "%s()\n", __func__);
437 if (!psd)
438 return 0;
440 spin_lock_irqsave(&psd->lock, flags);
442 list_for_each_entry(ce, &psd->clock_list, node)
443 __pm_clk_enable(dev, ce);
445 spin_unlock_irqrestore(&psd->lock, flags);
447 return 0;
449 EXPORT_SYMBOL_GPL(pm_clk_resume);
452 * pm_clk_notify - Notify routine for device addition and removal.
453 * @nb: Notifier block object this function is a member of.
454 * @action: Operation being carried out by the caller.
455 * @data: Device the routine is being run for.
457 * For this function to work, @nb must be a member of an object of type
458 * struct pm_clk_notifier_block containing all of the requisite data.
459 * Specifically, the pm_domain member of that object is copied to the device's
460 * pm_domain field and its con_ids member is used to populate the device's list
461 * of PM clocks, depending on @action.
463 * If the device's pm_domain field is already populated with a value different
464 * from the one stored in the struct pm_clk_notifier_block object, the function
465 * does nothing.
467 static int pm_clk_notify(struct notifier_block *nb,
468 unsigned long action, void *data)
470 struct pm_clk_notifier_block *clknb;
471 struct device *dev = data;
472 char **con_id;
473 int error;
475 dev_dbg(dev, "%s() %ld\n", __func__, action);
477 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
479 switch (action) {
480 case BUS_NOTIFY_ADD_DEVICE:
481 if (dev->pm_domain)
482 break;
484 error = pm_clk_create(dev);
485 if (error)
486 break;
488 dev_pm_domain_set(dev, clknb->pm_domain);
489 if (clknb->con_ids[0]) {
490 for (con_id = clknb->con_ids; *con_id; con_id++)
491 pm_clk_add(dev, *con_id);
492 } else {
493 pm_clk_add(dev, NULL);
496 break;
497 case BUS_NOTIFY_DEL_DEVICE:
498 if (dev->pm_domain != clknb->pm_domain)
499 break;
501 dev_pm_domain_set(dev, NULL);
502 pm_clk_destroy(dev);
503 break;
506 return 0;
509 int pm_clk_runtime_suspend(struct device *dev)
511 int ret;
513 dev_dbg(dev, "%s\n", __func__);
515 ret = pm_generic_runtime_suspend(dev);
516 if (ret) {
517 dev_err(dev, "failed to suspend device\n");
518 return ret;
521 ret = pm_clk_suspend(dev);
522 if (ret) {
523 dev_err(dev, "failed to suspend clock\n");
524 pm_generic_runtime_resume(dev);
525 return ret;
528 return 0;
530 EXPORT_SYMBOL_GPL(pm_clk_runtime_suspend);
532 int pm_clk_runtime_resume(struct device *dev)
534 int ret;
536 dev_dbg(dev, "%s\n", __func__);
538 ret = pm_clk_resume(dev);
539 if (ret) {
540 dev_err(dev, "failed to resume clock\n");
541 return ret;
544 return pm_generic_runtime_resume(dev);
546 EXPORT_SYMBOL_GPL(pm_clk_runtime_resume);
548 #else /* !CONFIG_PM_CLK */
551 * enable_clock - Enable a device clock.
552 * @dev: Device whose clock is to be enabled.
553 * @con_id: Connection ID of the clock.
555 static void enable_clock(struct device *dev, const char *con_id)
557 struct clk *clk;
559 clk = clk_get(dev, con_id);
560 if (!IS_ERR(clk)) {
561 clk_prepare_enable(clk);
562 clk_put(clk);
563 dev_info(dev, "Runtime PM disabled, clock forced on.\n");
568 * disable_clock - Disable a device clock.
569 * @dev: Device whose clock is to be disabled.
570 * @con_id: Connection ID of the clock.
572 static void disable_clock(struct device *dev, const char *con_id)
574 struct clk *clk;
576 clk = clk_get(dev, con_id);
577 if (!IS_ERR(clk)) {
578 clk_disable_unprepare(clk);
579 clk_put(clk);
580 dev_info(dev, "Runtime PM disabled, clock forced off.\n");
585 * pm_clk_notify - Notify routine for device addition and removal.
586 * @nb: Notifier block object this function is a member of.
587 * @action: Operation being carried out by the caller.
588 * @data: Device the routine is being run for.
590 * For this function to work, @nb must be a member of an object of type
591 * struct pm_clk_notifier_block containing all of the requisite data.
592 * Specifically, the con_ids member of that object is used to enable or disable
593 * the device's clocks, depending on @action.
595 static int pm_clk_notify(struct notifier_block *nb,
596 unsigned long action, void *data)
598 struct pm_clk_notifier_block *clknb;
599 struct device *dev = data;
600 char **con_id;
602 dev_dbg(dev, "%s() %ld\n", __func__, action);
604 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
606 switch (action) {
607 case BUS_NOTIFY_BIND_DRIVER:
608 if (clknb->con_ids[0]) {
609 for (con_id = clknb->con_ids; *con_id; con_id++)
610 enable_clock(dev, *con_id);
611 } else {
612 enable_clock(dev, NULL);
614 break;
615 case BUS_NOTIFY_DRIVER_NOT_BOUND:
616 case BUS_NOTIFY_UNBOUND_DRIVER:
617 if (clknb->con_ids[0]) {
618 for (con_id = clknb->con_ids; *con_id; con_id++)
619 disable_clock(dev, *con_id);
620 } else {
621 disable_clock(dev, NULL);
623 break;
626 return 0;
629 #endif /* !CONFIG_PM_CLK */
632 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
633 * @bus: Bus type to add the notifier to.
634 * @clknb: Notifier to be added to the given bus type.
636 * The nb member of @clknb is not expected to be initialized and its
637 * notifier_call member will be replaced with pm_clk_notify(). However,
638 * the remaining members of @clknb should be populated prior to calling this
639 * routine.
641 void pm_clk_add_notifier(struct bus_type *bus,
642 struct pm_clk_notifier_block *clknb)
644 if (!bus || !clknb)
645 return;
647 clknb->nb.notifier_call = pm_clk_notify;
648 bus_register_notifier(bus, &clknb->nb);
650 EXPORT_SYMBOL_GPL(pm_clk_add_notifier);