Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / base / power / clock_ops.c
blob59d19dd649289287af0a34f3ed5d9653974e3a8a
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * drivers/base/power/clock_ops.c - Generic clock manipulation PM callbacks
5 * Copyright (c) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 */
8 #include <linux/kernel.h>
9 #include <linux/device.h>
10 #include <linux/io.h>
11 #include <linux/pm.h>
12 #include <linux/pm_clock.h>
13 #include <linux/clk.h>
14 #include <linux/clkdev.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/pm_domain.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 if (clk_prepare(ce->clk)) {
68 ce->status = PCE_STATUS_ERROR;
69 dev_err(dev, "clk_prepare() failed\n");
70 } else {
71 ce->status = PCE_STATUS_ACQUIRED;
72 dev_dbg(dev,
73 "Clock %pC con_id %s managed by runtime PM.\n",
74 ce->clk, ce->con_id);
79 static int __pm_clk_add(struct device *dev, const char *con_id,
80 struct clk *clk)
82 struct pm_subsys_data *psd = dev_to_psd(dev);
83 struct pm_clock_entry *ce;
85 if (!psd)
86 return -EINVAL;
88 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
89 if (!ce)
90 return -ENOMEM;
92 if (con_id) {
93 ce->con_id = kstrdup(con_id, GFP_KERNEL);
94 if (!ce->con_id) {
95 dev_err(dev,
96 "Not enough memory for clock connection ID.\n");
97 kfree(ce);
98 return -ENOMEM;
100 } else {
101 if (IS_ERR(clk)) {
102 kfree(ce);
103 return -ENOENT;
105 ce->clk = clk;
108 pm_clk_acquire(dev, ce);
110 spin_lock_irq(&psd->lock);
111 list_add_tail(&ce->node, &psd->clock_list);
112 spin_unlock_irq(&psd->lock);
113 return 0;
117 * pm_clk_add - Start using a device clock for power management.
118 * @dev: Device whose clock is going to be used for power management.
119 * @con_id: Connection ID of the clock.
121 * Add the clock represented by @con_id to the list of clocks used for
122 * the power management of @dev.
124 int pm_clk_add(struct device *dev, const char *con_id)
126 return __pm_clk_add(dev, con_id, NULL);
128 EXPORT_SYMBOL_GPL(pm_clk_add);
131 * pm_clk_add_clk - Start using a device clock for power management.
132 * @dev: Device whose clock is going to be used for power management.
133 * @clk: Clock pointer
135 * Add the clock to the list of clocks used for the power management of @dev.
136 * The power-management code will take control of the clock reference, so
137 * callers should not call clk_put() on @clk after this function sucessfully
138 * returned.
140 int pm_clk_add_clk(struct device *dev, struct clk *clk)
142 return __pm_clk_add(dev, NULL, clk);
144 EXPORT_SYMBOL_GPL(pm_clk_add_clk);
148 * of_pm_clk_add_clk - Start using a device clock for power management.
149 * @dev: Device whose clock is going to be used for power management.
150 * @name: Name of clock that is going to be used for power management.
152 * Add the clock described in the 'clocks' device-tree node that matches
153 * with the 'name' provided, to the list of clocks used for the power
154 * management of @dev. On success, returns 0. Returns a negative error
155 * code if the clock is not found or cannot be added.
157 int of_pm_clk_add_clk(struct device *dev, const char *name)
159 struct clk *clk;
160 int ret;
162 if (!dev || !dev->of_node || !name)
163 return -EINVAL;
165 clk = of_clk_get_by_name(dev->of_node, name);
166 if (IS_ERR(clk))
167 return PTR_ERR(clk);
169 ret = pm_clk_add_clk(dev, clk);
170 if (ret) {
171 clk_put(clk);
172 return ret;
175 return 0;
177 EXPORT_SYMBOL_GPL(of_pm_clk_add_clk);
180 * of_pm_clk_add_clks - Start using device clock(s) for power management.
181 * @dev: Device whose clock(s) is going to be used for power management.
183 * Add a series of clocks described in the 'clocks' device-tree node for
184 * a device to the list of clocks used for the power management of @dev.
185 * On success, returns the number of clocks added. Returns a negative
186 * error code if there are no clocks in the device node for the device
187 * or if adding a clock fails.
189 int of_pm_clk_add_clks(struct device *dev)
191 struct clk **clks;
192 int i, count;
193 int ret;
195 if (!dev || !dev->of_node)
196 return -EINVAL;
198 count = of_count_phandle_with_args(dev->of_node, "clocks",
199 "#clock-cells");
200 if (count <= 0)
201 return -ENODEV;
203 clks = kcalloc(count, sizeof(*clks), GFP_KERNEL);
204 if (!clks)
205 return -ENOMEM;
207 for (i = 0; i < count; i++) {
208 clks[i] = of_clk_get(dev->of_node, i);
209 if (IS_ERR(clks[i])) {
210 ret = PTR_ERR(clks[i]);
211 goto error;
214 ret = pm_clk_add_clk(dev, clks[i]);
215 if (ret) {
216 clk_put(clks[i]);
217 goto error;
221 kfree(clks);
223 return i;
225 error:
226 while (i--)
227 pm_clk_remove_clk(dev, clks[i]);
229 kfree(clks);
231 return ret;
233 EXPORT_SYMBOL_GPL(of_pm_clk_add_clks);
236 * __pm_clk_remove - Destroy PM clock entry.
237 * @ce: PM clock entry to destroy.
239 static void __pm_clk_remove(struct pm_clock_entry *ce)
241 if (!ce)
242 return;
244 if (ce->status < PCE_STATUS_ERROR) {
245 if (ce->status == PCE_STATUS_ENABLED)
246 clk_disable(ce->clk);
248 if (ce->status >= PCE_STATUS_ACQUIRED) {
249 clk_unprepare(ce->clk);
250 clk_put(ce->clk);
254 kfree(ce->con_id);
255 kfree(ce);
259 * pm_clk_remove - Stop using a device clock for power management.
260 * @dev: Device whose clock should not be used for PM any more.
261 * @con_id: Connection ID of the clock.
263 * Remove the clock represented by @con_id from the list of clocks used for
264 * the power management of @dev.
266 void pm_clk_remove(struct device *dev, const char *con_id)
268 struct pm_subsys_data *psd = dev_to_psd(dev);
269 struct pm_clock_entry *ce;
271 if (!psd)
272 return;
274 spin_lock_irq(&psd->lock);
276 list_for_each_entry(ce, &psd->clock_list, node) {
277 if (!con_id && !ce->con_id)
278 goto remove;
279 else if (!con_id || !ce->con_id)
280 continue;
281 else if (!strcmp(con_id, ce->con_id))
282 goto remove;
285 spin_unlock_irq(&psd->lock);
286 return;
288 remove:
289 list_del(&ce->node);
290 spin_unlock_irq(&psd->lock);
292 __pm_clk_remove(ce);
294 EXPORT_SYMBOL_GPL(pm_clk_remove);
297 * pm_clk_remove_clk - Stop using a device clock for power management.
298 * @dev: Device whose clock should not be used for PM any more.
299 * @clk: Clock pointer
301 * Remove the clock pointed to by @clk from the list of clocks used for
302 * the power management of @dev.
304 void pm_clk_remove_clk(struct device *dev, struct clk *clk)
306 struct pm_subsys_data *psd = dev_to_psd(dev);
307 struct pm_clock_entry *ce;
309 if (!psd || !clk)
310 return;
312 spin_lock_irq(&psd->lock);
314 list_for_each_entry(ce, &psd->clock_list, node) {
315 if (clk == ce->clk)
316 goto remove;
319 spin_unlock_irq(&psd->lock);
320 return;
322 remove:
323 list_del(&ce->node);
324 spin_unlock_irq(&psd->lock);
326 __pm_clk_remove(ce);
328 EXPORT_SYMBOL_GPL(pm_clk_remove_clk);
331 * pm_clk_init - Initialize a device's list of power management clocks.
332 * @dev: Device to initialize the list of PM clocks for.
334 * Initialize the lock and clock_list members of the device's pm_subsys_data
335 * object.
337 void pm_clk_init(struct device *dev)
339 struct pm_subsys_data *psd = dev_to_psd(dev);
340 if (psd)
341 INIT_LIST_HEAD(&psd->clock_list);
343 EXPORT_SYMBOL_GPL(pm_clk_init);
346 * pm_clk_create - Create and initialize a device's list of PM clocks.
347 * @dev: Device to create and initialize the list of PM clocks for.
349 * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
350 * members and make the @dev's power.subsys_data field point to it.
352 int pm_clk_create(struct device *dev)
354 return dev_pm_get_subsys_data(dev);
356 EXPORT_SYMBOL_GPL(pm_clk_create);
359 * pm_clk_destroy - Destroy a device's list of power management clocks.
360 * @dev: Device to destroy the list of PM clocks for.
362 * Clear the @dev's power.subsys_data field, remove the list of clock entries
363 * from the struct pm_subsys_data object pointed to by it before and free
364 * that object.
366 void pm_clk_destroy(struct device *dev)
368 struct pm_subsys_data *psd = dev_to_psd(dev);
369 struct pm_clock_entry *ce, *c;
370 struct list_head list;
372 if (!psd)
373 return;
375 INIT_LIST_HEAD(&list);
377 spin_lock_irq(&psd->lock);
379 list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
380 list_move(&ce->node, &list);
382 spin_unlock_irq(&psd->lock);
384 dev_pm_put_subsys_data(dev);
386 list_for_each_entry_safe_reverse(ce, c, &list, node) {
387 list_del(&ce->node);
388 __pm_clk_remove(ce);
391 EXPORT_SYMBOL_GPL(pm_clk_destroy);
394 * pm_clk_suspend - Disable clocks in a device's PM clock list.
395 * @dev: Device to disable the clocks for.
397 int pm_clk_suspend(struct device *dev)
399 struct pm_subsys_data *psd = dev_to_psd(dev);
400 struct pm_clock_entry *ce;
401 unsigned long flags;
403 dev_dbg(dev, "%s()\n", __func__);
405 if (!psd)
406 return 0;
408 spin_lock_irqsave(&psd->lock, flags);
410 list_for_each_entry_reverse(ce, &psd->clock_list, node) {
411 if (ce->status < PCE_STATUS_ERROR) {
412 if (ce->status == PCE_STATUS_ENABLED)
413 clk_disable(ce->clk);
414 ce->status = PCE_STATUS_ACQUIRED;
418 spin_unlock_irqrestore(&psd->lock, flags);
420 return 0;
422 EXPORT_SYMBOL_GPL(pm_clk_suspend);
425 * pm_clk_resume - Enable clocks in a device's PM clock list.
426 * @dev: Device to enable the clocks for.
428 int pm_clk_resume(struct device *dev)
430 struct pm_subsys_data *psd = dev_to_psd(dev);
431 struct pm_clock_entry *ce;
432 unsigned long flags;
434 dev_dbg(dev, "%s()\n", __func__);
436 if (!psd)
437 return 0;
439 spin_lock_irqsave(&psd->lock, flags);
441 list_for_each_entry(ce, &psd->clock_list, node)
442 __pm_clk_enable(dev, ce);
444 spin_unlock_irqrestore(&psd->lock, flags);
446 return 0;
448 EXPORT_SYMBOL_GPL(pm_clk_resume);
451 * pm_clk_notify - Notify routine for device addition and removal.
452 * @nb: Notifier block object this function is a member of.
453 * @action: Operation being carried out by the caller.
454 * @data: Device the routine is being run for.
456 * For this function to work, @nb must be a member of an object of type
457 * struct pm_clk_notifier_block containing all of the requisite data.
458 * Specifically, the pm_domain member of that object is copied to the device's
459 * pm_domain field and its con_ids member is used to populate the device's list
460 * of PM clocks, depending on @action.
462 * If the device's pm_domain field is already populated with a value different
463 * from the one stored in the struct pm_clk_notifier_block object, the function
464 * does nothing.
466 static int pm_clk_notify(struct notifier_block *nb,
467 unsigned long action, void *data)
469 struct pm_clk_notifier_block *clknb;
470 struct device *dev = data;
471 char **con_id;
472 int error;
474 dev_dbg(dev, "%s() %ld\n", __func__, action);
476 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
478 switch (action) {
479 case BUS_NOTIFY_ADD_DEVICE:
480 if (dev->pm_domain)
481 break;
483 error = pm_clk_create(dev);
484 if (error)
485 break;
487 dev_pm_domain_set(dev, clknb->pm_domain);
488 if (clknb->con_ids[0]) {
489 for (con_id = clknb->con_ids; *con_id; con_id++)
490 pm_clk_add(dev, *con_id);
491 } else {
492 pm_clk_add(dev, NULL);
495 break;
496 case BUS_NOTIFY_DEL_DEVICE:
497 if (dev->pm_domain != clknb->pm_domain)
498 break;
500 dev_pm_domain_set(dev, NULL);
501 pm_clk_destroy(dev);
502 break;
505 return 0;
508 int pm_clk_runtime_suspend(struct device *dev)
510 int ret;
512 dev_dbg(dev, "%s\n", __func__);
514 ret = pm_generic_runtime_suspend(dev);
515 if (ret) {
516 dev_err(dev, "failed to suspend device\n");
517 return ret;
520 ret = pm_clk_suspend(dev);
521 if (ret) {
522 dev_err(dev, "failed to suspend clock\n");
523 pm_generic_runtime_resume(dev);
524 return ret;
527 return 0;
529 EXPORT_SYMBOL_GPL(pm_clk_runtime_suspend);
531 int pm_clk_runtime_resume(struct device *dev)
533 int ret;
535 dev_dbg(dev, "%s\n", __func__);
537 ret = pm_clk_resume(dev);
538 if (ret) {
539 dev_err(dev, "failed to resume clock\n");
540 return ret;
543 return pm_generic_runtime_resume(dev);
545 EXPORT_SYMBOL_GPL(pm_clk_runtime_resume);
547 #else /* !CONFIG_PM_CLK */
550 * enable_clock - Enable a device clock.
551 * @dev: Device whose clock is to be enabled.
552 * @con_id: Connection ID of the clock.
554 static void enable_clock(struct device *dev, const char *con_id)
556 struct clk *clk;
558 clk = clk_get(dev, con_id);
559 if (!IS_ERR(clk)) {
560 clk_prepare_enable(clk);
561 clk_put(clk);
562 dev_info(dev, "Runtime PM disabled, clock forced on.\n");
567 * disable_clock - Disable a device clock.
568 * @dev: Device whose clock is to be disabled.
569 * @con_id: Connection ID of the clock.
571 static void disable_clock(struct device *dev, const char *con_id)
573 struct clk *clk;
575 clk = clk_get(dev, con_id);
576 if (!IS_ERR(clk)) {
577 clk_disable_unprepare(clk);
578 clk_put(clk);
579 dev_info(dev, "Runtime PM disabled, clock forced off.\n");
584 * pm_clk_notify - Notify routine for device addition and removal.
585 * @nb: Notifier block object this function is a member of.
586 * @action: Operation being carried out by the caller.
587 * @data: Device the routine is being run for.
589 * For this function to work, @nb must be a member of an object of type
590 * struct pm_clk_notifier_block containing all of the requisite data.
591 * Specifically, the con_ids member of that object is used to enable or disable
592 * the device's clocks, depending on @action.
594 static int pm_clk_notify(struct notifier_block *nb,
595 unsigned long action, void *data)
597 struct pm_clk_notifier_block *clknb;
598 struct device *dev = data;
599 char **con_id;
601 dev_dbg(dev, "%s() %ld\n", __func__, action);
603 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
605 switch (action) {
606 case BUS_NOTIFY_BIND_DRIVER:
607 if (clknb->con_ids[0]) {
608 for (con_id = clknb->con_ids; *con_id; con_id++)
609 enable_clock(dev, *con_id);
610 } else {
611 enable_clock(dev, NULL);
613 break;
614 case BUS_NOTIFY_DRIVER_NOT_BOUND:
615 case BUS_NOTIFY_UNBOUND_DRIVER:
616 if (clknb->con_ids[0]) {
617 for (con_id = clknb->con_ids; *con_id; con_id++)
618 disable_clock(dev, *con_id);
619 } else {
620 disable_clock(dev, NULL);
622 break;
625 return 0;
628 #endif /* !CONFIG_PM_CLK */
631 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
632 * @bus: Bus type to add the notifier to.
633 * @clknb: Notifier to be added to the given bus type.
635 * The nb member of @clknb is not expected to be initialized and its
636 * notifier_call member will be replaced with pm_clk_notify(). However,
637 * the remaining members of @clknb should be populated prior to calling this
638 * routine.
640 void pm_clk_add_notifier(struct bus_type *bus,
641 struct pm_clk_notifier_block *clknb)
643 if (!bus || !clknb)
644 return;
646 clknb->nb.notifier_call = pm_clk_notify;
647 bus_register_notifier(bus, &clknb->nb);
649 EXPORT_SYMBOL_GPL(pm_clk_add_notifier);