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