PM / clk: Print acquired clock name in addition to con_id
[deliverable/linux.git] / drivers / base / power / clock_ops.c
CommitLineData
85eb8c8d
RW
1/*
2 * drivers/base/power/clock_ops.c - Generic clock manipulation PM callbacks
3 *
4 * Copyright (c) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
5 *
6 * This file is released under the GPLv2.
7 */
8
85eb8c8d 9#include <linux/kernel.h>
51990e82 10#include <linux/device.h>
85eb8c8d
RW
11#include <linux/io.h>
12#include <linux/pm.h>
b5e8d269 13#include <linux/pm_clock.h>
85eb8c8d 14#include <linux/clk.h>
245bd6f6 15#include <linux/clkdev.h>
85eb8c8d
RW
16#include <linux/slab.h>
17#include <linux/err.h>
75f50400 18#include <linux/pm_runtime.h>
85eb8c8d 19
b7b95920 20#ifdef CONFIG_PM
85eb8c8d 21
85eb8c8d
RW
22enum pce_status {
23 PCE_STATUS_NONE = 0,
24 PCE_STATUS_ACQUIRED,
25 PCE_STATUS_ENABLED,
26 PCE_STATUS_ERROR,
27};
28
29struct pm_clock_entry {
30 struct list_head node;
31 char *con_id;
32 struct clk *clk;
33 enum pce_status status;
34};
35
5cda3fbb
BD
36/**
37 * pm_clk_enable - Enable a clock, reporting any errors
38 * @dev: The device for the given clock
471f7707 39 * @ce: PM clock entry corresponding to the clock.
5cda3fbb 40 */
471f7707 41static inline int __pm_clk_enable(struct device *dev, struct pm_clock_entry *ce)
5cda3fbb 42{
471f7707
SG
43 int ret;
44
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);
52 }
5cda3fbb
BD
53
54 return ret;
55}
56
e8b364b8
RW
57/**
58 * pm_clk_acquire - Acquire a device clock.
59 * @dev: Device whose clock is to be acquired.
60 * @ce: PM clock entry corresponding to the clock.
61 */
62static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
63{
245bd6f6
GU
64 if (!ce->clk)
65 ce->clk = clk_get(dev, ce->con_id);
e8b364b8
RW
66 if (IS_ERR(ce->clk)) {
67 ce->status = PCE_STATUS_ERROR;
68 } else {
8a6720ec 69 clk_prepare(ce->clk);
e8b364b8 70 ce->status = PCE_STATUS_ACQUIRED;
f17f4adf
GU
71 dev_dbg(dev, "Clock %pC con_id %s managed by runtime PM.\n",
72 ce->clk, ce->con_id);
e8b364b8
RW
73 }
74}
75
245bd6f6
GU
76static int __pm_clk_add(struct device *dev, const char *con_id,
77 struct clk *clk)
85eb8c8d 78{
5c095a0e 79 struct pm_subsys_data *psd = dev_to_psd(dev);
85eb8c8d
RW
80 struct pm_clock_entry *ce;
81
5c095a0e 82 if (!psd)
85eb8c8d
RW
83 return -EINVAL;
84
85 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
59d84ca8 86 if (!ce)
85eb8c8d 87 return -ENOMEM;
85eb8c8d
RW
88
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;
96 }
245bd6f6 97 } else {
3fc3a0be 98 if (IS_ERR(clk) || !__clk_get(clk)) {
245bd6f6
GU
99 kfree(ce);
100 return -ENOENT;
101 }
102 ce->clk = clk;
85eb8c8d
RW
103 }
104
e8b364b8
RW
105 pm_clk_acquire(dev, ce);
106
5c095a0e
RW
107 spin_lock_irq(&psd->lock);
108 list_add_tail(&ce->node, &psd->clock_list);
109 spin_unlock_irq(&psd->lock);
85eb8c8d
RW
110 return 0;
111}
112
245bd6f6
GU
113/**
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.
117 *
118 * Add the clock represented by @con_id to the list of clocks used for
119 * the power management of @dev.
120 */
121int pm_clk_add(struct device *dev, const char *con_id)
122{
123 return __pm_clk_add(dev, con_id, NULL);
124}
125
126/**
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
130 *
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.
133 */
134int pm_clk_add_clk(struct device *dev, struct clk *clk)
135{
136 return __pm_clk_add(dev, NULL, clk);
137}
138
85eb8c8d 139/**
3d5c3036
RW
140 * __pm_clk_remove - Destroy PM clock entry.
141 * @ce: PM clock entry to destroy.
85eb8c8d 142 */
3d5c3036 143static void __pm_clk_remove(struct pm_clock_entry *ce)
85eb8c8d
RW
144{
145 if (!ce)
146 return;
147
85eb8c8d
RW
148 if (ce->status < PCE_STATUS_ERROR) {
149 if (ce->status == PCE_STATUS_ENABLED)
8a6720ec 150 clk_disable(ce->clk);
85eb8c8d 151
8a6720ec
BD
152 if (ce->status >= PCE_STATUS_ACQUIRED) {
153 clk_unprepare(ce->clk);
85eb8c8d 154 clk_put(ce->clk);
8a6720ec 155 }
85eb8c8d
RW
156 }
157
0ab1e79b 158 kfree(ce->con_id);
85eb8c8d
RW
159 kfree(ce);
160}
161
162/**
3d5c3036
RW
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.
85eb8c8d
RW
165 * @con_id: Connection ID of the clock.
166 *
167 * Remove the clock represented by @con_id from the list of clocks used for
3d5c3036 168 * the power management of @dev.
85eb8c8d 169 */
3d5c3036 170void pm_clk_remove(struct device *dev, const char *con_id)
85eb8c8d 171{
5c095a0e 172 struct pm_subsys_data *psd = dev_to_psd(dev);
85eb8c8d
RW
173 struct pm_clock_entry *ce;
174
5c095a0e 175 if (!psd)
85eb8c8d
RW
176 return;
177
5c095a0e 178 spin_lock_irq(&psd->lock);
85eb8c8d 179
5c095a0e 180 list_for_each_entry(ce, &psd->clock_list, node) {
e8b364b8
RW
181 if (!con_id && !ce->con_id)
182 goto remove;
183 else if (!con_id || !ce->con_id)
85eb8c8d 184 continue;
e8b364b8
RW
185 else if (!strcmp(con_id, ce->con_id))
186 goto remove;
85eb8c8d
RW
187 }
188
5c095a0e 189 spin_unlock_irq(&psd->lock);
e8b364b8
RW
190 return;
191
192 remove:
193 list_del(&ce->node);
0d41da2e 194 spin_unlock_irq(&psd->lock);
e8b364b8
RW
195
196 __pm_clk_remove(ce);
85eb8c8d
RW
197}
198
199/**
3d5c3036
RW
200 * pm_clk_init - Initialize a device's list of power management clocks.
201 * @dev: Device to initialize the list of PM clocks for.
85eb8c8d 202 *
5c095a0e
RW
203 * Initialize the lock and clock_list members of the device's pm_subsys_data
204 * object.
85eb8c8d 205 */
5c095a0e 206void pm_clk_init(struct device *dev)
85eb8c8d 207{
5c095a0e 208 struct pm_subsys_data *psd = dev_to_psd(dev);
ef27bed1
RW
209 if (psd)
210 INIT_LIST_HEAD(&psd->clock_list);
5c095a0e
RW
211}
212
213/**
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.
216 *
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.
219 */
220int pm_clk_create(struct device *dev)
221{
77254950 222 return dev_pm_get_subsys_data(dev);
85eb8c8d
RW
223}
224
225/**
3d5c3036
RW
226 * pm_clk_destroy - Destroy a device's list of power management clocks.
227 * @dev: Device to destroy the list of PM clocks for.
85eb8c8d
RW
228 *
229 * Clear the @dev's power.subsys_data field, remove the list of clock entries
5c095a0e 230 * from the struct pm_subsys_data object pointed to by it before and free
85eb8c8d
RW
231 * that object.
232 */
3d5c3036 233void pm_clk_destroy(struct device *dev)
85eb8c8d 234{
5c095a0e 235 struct pm_subsys_data *psd = dev_to_psd(dev);
85eb8c8d 236 struct pm_clock_entry *ce, *c;
e8b364b8 237 struct list_head list;
85eb8c8d 238
5c095a0e 239 if (!psd)
85eb8c8d
RW
240 return;
241
e8b364b8 242 INIT_LIST_HEAD(&list);
85eb8c8d 243
5c095a0e 244 spin_lock_irq(&psd->lock);
85eb8c8d 245
5c095a0e 246 list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
e8b364b8 247 list_move(&ce->node, &list);
85eb8c8d 248
5c095a0e 249 spin_unlock_irq(&psd->lock);
85eb8c8d 250
ef27bed1 251 dev_pm_put_subsys_data(dev);
e8b364b8
RW
252
253 list_for_each_entry_safe_reverse(ce, c, &list, node) {
254 list_del(&ce->node);
255 __pm_clk_remove(ce);
256 }
85eb8c8d
RW
257}
258
85eb8c8d 259/**
3d5c3036 260 * pm_clk_suspend - Disable clocks in a device's PM clock list.
85eb8c8d
RW
261 * @dev: Device to disable the clocks for.
262 */
3d5c3036 263int pm_clk_suspend(struct device *dev)
85eb8c8d 264{
5c095a0e 265 struct pm_subsys_data *psd = dev_to_psd(dev);
85eb8c8d 266 struct pm_clock_entry *ce;
b7ab83ed 267 unsigned long flags;
85eb8c8d
RW
268
269 dev_dbg(dev, "%s()\n", __func__);
270
5c095a0e 271 if (!psd)
85eb8c8d
RW
272 return 0;
273
5c095a0e 274 spin_lock_irqsave(&psd->lock, flags);
85eb8c8d 275
5c095a0e 276 list_for_each_entry_reverse(ce, &psd->clock_list, node) {
85eb8c8d 277 if (ce->status < PCE_STATUS_ERROR) {
24050956
MD
278 if (ce->status == PCE_STATUS_ENABLED)
279 clk_disable(ce->clk);
85eb8c8d
RW
280 ce->status = PCE_STATUS_ACQUIRED;
281 }
282 }
283
5c095a0e 284 spin_unlock_irqrestore(&psd->lock, flags);
85eb8c8d
RW
285
286 return 0;
287}
288
289/**
3d5c3036 290 * pm_clk_resume - Enable clocks in a device's PM clock list.
85eb8c8d
RW
291 * @dev: Device to enable the clocks for.
292 */
3d5c3036 293int pm_clk_resume(struct device *dev)
85eb8c8d 294{
5c095a0e 295 struct pm_subsys_data *psd = dev_to_psd(dev);
85eb8c8d 296 struct pm_clock_entry *ce;
b7ab83ed 297 unsigned long flags;
85eb8c8d
RW
298
299 dev_dbg(dev, "%s()\n", __func__);
300
5c095a0e 301 if (!psd)
85eb8c8d
RW
302 return 0;
303
5c095a0e 304 spin_lock_irqsave(&psd->lock, flags);
85eb8c8d 305
471f7707
SG
306 list_for_each_entry(ce, &psd->clock_list, node)
307 __pm_clk_enable(dev, ce);
85eb8c8d 308
5c095a0e 309 spin_unlock_irqrestore(&psd->lock, flags);
85eb8c8d
RW
310
311 return 0;
312}
313
314/**
3d5c3036 315 * pm_clk_notify - Notify routine for device addition and removal.
85eb8c8d
RW
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.
319 *
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.
564b905a
RW
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
3d5c3036 324 * of PM clocks, depending on @action.
85eb8c8d 325 *
564b905a 326 * If the device's pm_domain field is already populated with a value different
85eb8c8d
RW
327 * from the one stored in the struct pm_clk_notifier_block object, the function
328 * does nothing.
329 */
3d5c3036 330static int pm_clk_notify(struct notifier_block *nb,
85eb8c8d
RW
331 unsigned long action, void *data)
332{
333 struct pm_clk_notifier_block *clknb;
334 struct device *dev = data;
3b3eca31 335 char **con_id;
85eb8c8d
RW
336 int error;
337
338 dev_dbg(dev, "%s() %ld\n", __func__, action);
339
340 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
341
342 switch (action) {
343 case BUS_NOTIFY_ADD_DEVICE:
564b905a 344 if (dev->pm_domain)
85eb8c8d
RW
345 break;
346
5c095a0e 347 error = pm_clk_create(dev);
85eb8c8d
RW
348 if (error)
349 break;
350
564b905a 351 dev->pm_domain = clknb->pm_domain;
85eb8c8d 352 if (clknb->con_ids[0]) {
3b3eca31 353 for (con_id = clknb->con_ids; *con_id; con_id++)
3d5c3036 354 pm_clk_add(dev, *con_id);
85eb8c8d 355 } else {
3d5c3036 356 pm_clk_add(dev, NULL);
85eb8c8d
RW
357 }
358
359 break;
360 case BUS_NOTIFY_DEL_DEVICE:
564b905a 361 if (dev->pm_domain != clknb->pm_domain)
85eb8c8d
RW
362 break;
363
564b905a 364 dev->pm_domain = NULL;
3d5c3036 365 pm_clk_destroy(dev);
85eb8c8d
RW
366 break;
367 }
368
369 return 0;
370}
371
75f50400
RN
372int pm_clk_runtime_suspend(struct device *dev)
373{
374 int ret;
375
376 dev_dbg(dev, "%s\n", __func__);
377
378 ret = pm_generic_runtime_suspend(dev);
379 if (ret) {
380 dev_err(dev, "failed to suspend device\n");
381 return ret;
382 }
383
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;
389 }
390
391 return 0;
392}
393
394int pm_clk_runtime_resume(struct device *dev)
395{
396 int ret;
397
398 dev_dbg(dev, "%s\n", __func__);
399
400 ret = pm_clk_resume(dev);
401 if (ret) {
402 dev_err(dev, "failed to resume clock\n");
403 return ret;
404 }
405
406 return pm_generic_runtime_resume(dev);
407}
408
d30d819d 409#else /* !CONFIG_PM */
b7b95920 410
85eb8c8d
RW
411/**
412 * enable_clock - Enable a device clock.
413 * @dev: Device whose clock is to be enabled.
414 * @con_id: Connection ID of the clock.
415 */
416static void enable_clock(struct device *dev, const char *con_id)
417{
418 struct clk *clk;
419
420 clk = clk_get(dev, con_id);
421 if (!IS_ERR(clk)) {
c122f27e 422 clk_prepare_enable(clk);
85eb8c8d
RW
423 clk_put(clk);
424 dev_info(dev, "Runtime PM disabled, clock forced on.\n");
425 }
426}
427
428/**
429 * disable_clock - Disable a device clock.
430 * @dev: Device whose clock is to be disabled.
431 * @con_id: Connection ID of the clock.
432 */
433static void disable_clock(struct device *dev, const char *con_id)
434{
435 struct clk *clk;
436
437 clk = clk_get(dev, con_id);
438 if (!IS_ERR(clk)) {
c122f27e 439 clk_disable_unprepare(clk);
85eb8c8d
RW
440 clk_put(clk);
441 dev_info(dev, "Runtime PM disabled, clock forced off.\n");
442 }
443}
444
445/**
3d5c3036 446 * pm_clk_notify - Notify routine for device addition and removal.
85eb8c8d
RW
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.
450 *
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.
455 */
3d5c3036 456static int pm_clk_notify(struct notifier_block *nb,
85eb8c8d
RW
457 unsigned long action, void *data)
458{
459 struct pm_clk_notifier_block *clknb;
460 struct device *dev = data;
3b3eca31 461 char **con_id;
85eb8c8d
RW
462
463 dev_dbg(dev, "%s() %ld\n", __func__, action);
464
465 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
466
467 switch (action) {
4d1518f5 468 case BUS_NOTIFY_BIND_DRIVER:
85eb8c8d 469 if (clknb->con_ids[0]) {
3b3eca31
RW
470 for (con_id = clknb->con_ids; *con_id; con_id++)
471 enable_clock(dev, *con_id);
85eb8c8d
RW
472 } else {
473 enable_clock(dev, NULL);
474 }
475 break;
4d1518f5 476 case BUS_NOTIFY_UNBOUND_DRIVER:
85eb8c8d 477 if (clknb->con_ids[0]) {
3b3eca31
RW
478 for (con_id = clknb->con_ids; *con_id; con_id++)
479 disable_clock(dev, *con_id);
85eb8c8d
RW
480 } else {
481 disable_clock(dev, NULL);
482 }
483 break;
484 }
485
486 return 0;
487}
488
d30d819d 489#endif /* !CONFIG_PM */
85eb8c8d
RW
490
491/**
3d5c3036 492 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
85eb8c8d
RW
493 * @bus: Bus type to add the notifier to.
494 * @clknb: Notifier to be added to the given bus type.
495 *
496 * The nb member of @clknb is not expected to be initialized and its
3d5c3036 497 * notifier_call member will be replaced with pm_clk_notify(). However,
85eb8c8d
RW
498 * the remaining members of @clknb should be populated prior to calling this
499 * routine.
500 */
3d5c3036 501void pm_clk_add_notifier(struct bus_type *bus,
85eb8c8d
RW
502 struct pm_clk_notifier_block *clknb)
503{
504 if (!bus || !clknb)
505 return;
506
3d5c3036 507 clknb->nb.notifier_call = pm_clk_notify;
85eb8c8d
RW
508 bus_register_notifier(bus, &clknb->nb);
509}
This page took 0.277406 seconds and 5 git commands to generate.