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