Commit | Line | Data |
---|---|---|
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 |
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 | ||
e8b364b8 RW |
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 { | |
8a6720ec | 46 | clk_prepare(ce->clk); |
e8b364b8 RW |
47 | ce->status = PCE_STATUS_ACQUIRED; |
48 | dev_dbg(dev, "Clock %s managed by runtime PM.\n", ce->con_id); | |
49 | } | |
50 | } | |
51 | ||
85eb8c8d | 52 | /** |
3d5c3036 RW |
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. | |
85eb8c8d RW |
55 | * @con_id: Connection ID of the clock. |
56 | * | |
57 | * Add the clock represented by @con_id to the list of clocks used for | |
3d5c3036 | 58 | * the power management of @dev. |
85eb8c8d | 59 | */ |
3d5c3036 | 60 | int pm_clk_add(struct device *dev, const char *con_id) |
85eb8c8d | 61 | { |
5c095a0e | 62 | struct pm_subsys_data *psd = dev_to_psd(dev); |
85eb8c8d RW |
63 | struct pm_clock_entry *ce; |
64 | ||
5c095a0e | 65 | if (!psd) |
85eb8c8d RW |
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 | ||
e8b364b8 RW |
84 | pm_clk_acquire(dev, ce); |
85 | ||
5c095a0e RW |
86 | spin_lock_irq(&psd->lock); |
87 | list_add_tail(&ce->node, &psd->clock_list); | |
88 | spin_unlock_irq(&psd->lock); | |
85eb8c8d RW |
89 | return 0; |
90 | } | |
91 | ||
92 | /** | |
3d5c3036 RW |
93 | * __pm_clk_remove - Destroy PM clock entry. |
94 | * @ce: PM clock entry to destroy. | |
85eb8c8d | 95 | */ |
3d5c3036 | 96 | static void __pm_clk_remove(struct pm_clock_entry *ce) |
85eb8c8d RW |
97 | { |
98 | if (!ce) | |
99 | return; | |
100 | ||
85eb8c8d RW |
101 | if (ce->status < PCE_STATUS_ERROR) { |
102 | if (ce->status == PCE_STATUS_ENABLED) | |
8a6720ec | 103 | clk_disable(ce->clk); |
85eb8c8d | 104 | |
8a6720ec BD |
105 | if (ce->status >= PCE_STATUS_ACQUIRED) { |
106 | clk_unprepare(ce->clk); | |
85eb8c8d | 107 | clk_put(ce->clk); |
8a6720ec | 108 | } |
85eb8c8d RW |
109 | } |
110 | ||
0ab1e79b | 111 | kfree(ce->con_id); |
85eb8c8d RW |
112 | kfree(ce); |
113 | } | |
114 | ||
115 | /** | |
3d5c3036 RW |
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. | |
85eb8c8d RW |
118 | * @con_id: Connection ID of the clock. |
119 | * | |
120 | * Remove the clock represented by @con_id from the list of clocks used for | |
3d5c3036 | 121 | * the power management of @dev. |
85eb8c8d | 122 | */ |
3d5c3036 | 123 | void pm_clk_remove(struct device *dev, const char *con_id) |
85eb8c8d | 124 | { |
5c095a0e | 125 | struct pm_subsys_data *psd = dev_to_psd(dev); |
85eb8c8d RW |
126 | struct pm_clock_entry *ce; |
127 | ||
5c095a0e | 128 | if (!psd) |
85eb8c8d RW |
129 | return; |
130 | ||
5c095a0e | 131 | spin_lock_irq(&psd->lock); |
85eb8c8d | 132 | |
5c095a0e | 133 | list_for_each_entry(ce, &psd->clock_list, node) { |
e8b364b8 RW |
134 | if (!con_id && !ce->con_id) |
135 | goto remove; | |
136 | else if (!con_id || !ce->con_id) | |
85eb8c8d | 137 | continue; |
e8b364b8 RW |
138 | else if (!strcmp(con_id, ce->con_id)) |
139 | goto remove; | |
85eb8c8d RW |
140 | } |
141 | ||
5c095a0e | 142 | spin_unlock_irq(&psd->lock); |
e8b364b8 RW |
143 | return; |
144 | ||
145 | remove: | |
146 | list_del(&ce->node); | |
0d41da2e | 147 | spin_unlock_irq(&psd->lock); |
e8b364b8 RW |
148 | |
149 | __pm_clk_remove(ce); | |
85eb8c8d RW |
150 | } |
151 | ||
152 | /** | |
3d5c3036 RW |
153 | * pm_clk_init - Initialize a device's list of power management clocks. |
154 | * @dev: Device to initialize the list of PM clocks for. | |
85eb8c8d | 155 | * |
5c095a0e RW |
156 | * Initialize the lock and clock_list members of the device's pm_subsys_data |
157 | * object. | |
85eb8c8d | 158 | */ |
5c095a0e | 159 | void pm_clk_init(struct device *dev) |
85eb8c8d | 160 | { |
5c095a0e | 161 | struct pm_subsys_data *psd = dev_to_psd(dev); |
ef27bed1 RW |
162 | if (psd) |
163 | INIT_LIST_HEAD(&psd->clock_list); | |
5c095a0e RW |
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 | { | |
77254950 | 175 | return dev_pm_get_subsys_data(dev); |
85eb8c8d RW |
176 | } |
177 | ||
178 | /** | |
3d5c3036 RW |
179 | * pm_clk_destroy - Destroy a device's list of power management clocks. |
180 | * @dev: Device to destroy the list of PM clocks for. | |
85eb8c8d RW |
181 | * |
182 | * Clear the @dev's power.subsys_data field, remove the list of clock entries | |
5c095a0e | 183 | * from the struct pm_subsys_data object pointed to by it before and free |
85eb8c8d RW |
184 | * that object. |
185 | */ | |
3d5c3036 | 186 | void pm_clk_destroy(struct device *dev) |
85eb8c8d | 187 | { |
5c095a0e | 188 | struct pm_subsys_data *psd = dev_to_psd(dev); |
85eb8c8d | 189 | struct pm_clock_entry *ce, *c; |
e8b364b8 | 190 | struct list_head list; |
85eb8c8d | 191 | |
5c095a0e | 192 | if (!psd) |
85eb8c8d RW |
193 | return; |
194 | ||
e8b364b8 | 195 | INIT_LIST_HEAD(&list); |
85eb8c8d | 196 | |
5c095a0e | 197 | spin_lock_irq(&psd->lock); |
85eb8c8d | 198 | |
5c095a0e | 199 | list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node) |
e8b364b8 | 200 | list_move(&ce->node, &list); |
85eb8c8d | 201 | |
5c095a0e | 202 | spin_unlock_irq(&psd->lock); |
85eb8c8d | 203 | |
ef27bed1 | 204 | dev_pm_put_subsys_data(dev); |
e8b364b8 RW |
205 | |
206 | list_for_each_entry_safe_reverse(ce, c, &list, node) { | |
207 | list_del(&ce->node); | |
208 | __pm_clk_remove(ce); | |
209 | } | |
85eb8c8d RW |
210 | } |
211 | ||
b7b95920 RW |
212 | #endif /* CONFIG_PM */ |
213 | ||
214 | #ifdef CONFIG_PM_RUNTIME | |
215 | ||
85eb8c8d | 216 | /** |
3d5c3036 | 217 | * pm_clk_suspend - Disable clocks in a device's PM clock list. |
85eb8c8d RW |
218 | * @dev: Device to disable the clocks for. |
219 | */ | |
3d5c3036 | 220 | int pm_clk_suspend(struct device *dev) |
85eb8c8d | 221 | { |
5c095a0e | 222 | struct pm_subsys_data *psd = dev_to_psd(dev); |
85eb8c8d | 223 | struct pm_clock_entry *ce; |
b7ab83ed | 224 | unsigned long flags; |
85eb8c8d RW |
225 | |
226 | dev_dbg(dev, "%s()\n", __func__); | |
227 | ||
5c095a0e | 228 | if (!psd) |
85eb8c8d RW |
229 | return 0; |
230 | ||
5c095a0e | 231 | spin_lock_irqsave(&psd->lock, flags); |
85eb8c8d | 232 | |
5c095a0e | 233 | list_for_each_entry_reverse(ce, &psd->clock_list, node) { |
85eb8c8d | 234 | if (ce->status < PCE_STATUS_ERROR) { |
24050956 MD |
235 | if (ce->status == PCE_STATUS_ENABLED) |
236 | clk_disable(ce->clk); | |
85eb8c8d RW |
237 | ce->status = PCE_STATUS_ACQUIRED; |
238 | } | |
239 | } | |
240 | ||
5c095a0e | 241 | spin_unlock_irqrestore(&psd->lock, flags); |
85eb8c8d RW |
242 | |
243 | return 0; | |
244 | } | |
245 | ||
246 | /** | |
3d5c3036 | 247 | * pm_clk_resume - Enable clocks in a device's PM clock list. |
85eb8c8d RW |
248 | * @dev: Device to enable the clocks for. |
249 | */ | |
3d5c3036 | 250 | int pm_clk_resume(struct device *dev) |
85eb8c8d | 251 | { |
5c095a0e | 252 | struct pm_subsys_data *psd = dev_to_psd(dev); |
85eb8c8d | 253 | struct pm_clock_entry *ce; |
b7ab83ed | 254 | unsigned long flags; |
afdd3ab3 | 255 | int ret; |
85eb8c8d RW |
256 | |
257 | dev_dbg(dev, "%s()\n", __func__); | |
258 | ||
5c095a0e | 259 | if (!psd) |
85eb8c8d RW |
260 | return 0; |
261 | ||
5c095a0e | 262 | spin_lock_irqsave(&psd->lock, flags); |
85eb8c8d | 263 | |
5c095a0e | 264 | list_for_each_entry(ce, &psd->clock_list, node) { |
85eb8c8d | 265 | if (ce->status < PCE_STATUS_ERROR) { |
afdd3ab3 BD |
266 | ret = clk_enable(ce->clk); |
267 | if (!ret) | |
268 | ce->status = PCE_STATUS_ENABLED; | |
85eb8c8d RW |
269 | } |
270 | } | |
271 | ||
5c095a0e | 272 | spin_unlock_irqrestore(&psd->lock, flags); |
85eb8c8d RW |
273 | |
274 | return 0; | |
275 | } | |
276 | ||
277 | /** | |
3d5c3036 | 278 | * pm_clk_notify - Notify routine for device addition and removal. |
85eb8c8d RW |
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. | |
564b905a RW |
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 | |
3d5c3036 | 287 | * of PM clocks, depending on @action. |
85eb8c8d | 288 | * |
564b905a | 289 | * If the device's pm_domain field is already populated with a value different |
85eb8c8d RW |
290 | * from the one stored in the struct pm_clk_notifier_block object, the function |
291 | * does nothing. | |
292 | */ | |
3d5c3036 | 293 | static int pm_clk_notify(struct notifier_block *nb, |
85eb8c8d RW |
294 | unsigned long action, void *data) |
295 | { | |
296 | struct pm_clk_notifier_block *clknb; | |
297 | struct device *dev = data; | |
3b3eca31 | 298 | char **con_id; |
85eb8c8d RW |
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: | |
564b905a | 307 | if (dev->pm_domain) |
85eb8c8d RW |
308 | break; |
309 | ||
5c095a0e | 310 | error = pm_clk_create(dev); |
85eb8c8d RW |
311 | if (error) |
312 | break; | |
313 | ||
564b905a | 314 | dev->pm_domain = clknb->pm_domain; |
85eb8c8d | 315 | if (clknb->con_ids[0]) { |
3b3eca31 | 316 | for (con_id = clknb->con_ids; *con_id; con_id++) |
3d5c3036 | 317 | pm_clk_add(dev, *con_id); |
85eb8c8d | 318 | } else { |
3d5c3036 | 319 | pm_clk_add(dev, NULL); |
85eb8c8d RW |
320 | } |
321 | ||
322 | break; | |
323 | case BUS_NOTIFY_DEL_DEVICE: | |
564b905a | 324 | if (dev->pm_domain != clknb->pm_domain) |
85eb8c8d RW |
325 | break; |
326 | ||
564b905a | 327 | dev->pm_domain = NULL; |
3d5c3036 | 328 | pm_clk_destroy(dev); |
85eb8c8d RW |
329 | break; |
330 | } | |
331 | ||
332 | return 0; | |
333 | } | |
334 | ||
335 | #else /* !CONFIG_PM_RUNTIME */ | |
336 | ||
b7b95920 RW |
337 | #ifdef CONFIG_PM |
338 | ||
339 | /** | |
3d5c3036 | 340 | * pm_clk_suspend - Disable clocks in a device's PM clock list. |
b7b95920 RW |
341 | * @dev: Device to disable the clocks for. |
342 | */ | |
3d5c3036 | 343 | int pm_clk_suspend(struct device *dev) |
b7b95920 | 344 | { |
5c095a0e | 345 | struct pm_subsys_data *psd = dev_to_psd(dev); |
b7b95920 | 346 | struct pm_clock_entry *ce; |
b7ab83ed | 347 | unsigned long flags; |
b7b95920 RW |
348 | |
349 | dev_dbg(dev, "%s()\n", __func__); | |
350 | ||
351 | /* If there is no driver, the clocks are already disabled. */ | |
5c095a0e | 352 | if (!psd || !dev->driver) |
b7b95920 RW |
353 | return 0; |
354 | ||
5c095a0e | 355 | spin_lock_irqsave(&psd->lock, flags); |
b7b95920 | 356 | |
5c095a0e | 357 | list_for_each_entry_reverse(ce, &psd->clock_list, node) |
b7b95920 RW |
358 | clk_disable(ce->clk); |
359 | ||
5c095a0e | 360 | spin_unlock_irqrestore(&psd->lock, flags); |
b7b95920 RW |
361 | |
362 | return 0; | |
363 | } | |
364 | ||
365 | /** | |
3d5c3036 | 366 | * pm_clk_resume - Enable clocks in a device's PM clock list. |
b7b95920 RW |
367 | * @dev: Device to enable the clocks for. |
368 | */ | |
3d5c3036 | 369 | int pm_clk_resume(struct device *dev) |
b7b95920 | 370 | { |
5c095a0e | 371 | struct pm_subsys_data *psd = dev_to_psd(dev); |
b7b95920 | 372 | struct pm_clock_entry *ce; |
b7ab83ed | 373 | unsigned long flags; |
b7b95920 RW |
374 | |
375 | dev_dbg(dev, "%s()\n", __func__); | |
376 | ||
377 | /* If there is no driver, the clocks should remain disabled. */ | |
5c095a0e | 378 | if (!psd || !dev->driver) |
b7b95920 RW |
379 | return 0; |
380 | ||
5c095a0e | 381 | spin_lock_irqsave(&psd->lock, flags); |
b7b95920 | 382 | |
5c095a0e | 383 | list_for_each_entry(ce, &psd->clock_list, node) |
b7b95920 RW |
384 | clk_enable(ce->clk); |
385 | ||
5c095a0e | 386 | spin_unlock_irqrestore(&psd->lock, flags); |
b7b95920 RW |
387 | |
388 | return 0; | |
389 | } | |
390 | ||
391 | #endif /* CONFIG_PM */ | |
392 | ||
85eb8c8d RW |
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)) { | |
c122f27e | 404 | clk_prepare_enable(clk); |
85eb8c8d RW |
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)) { | |
c122f27e | 421 | clk_disable_unprepare(clk); |
85eb8c8d RW |
422 | clk_put(clk); |
423 | dev_info(dev, "Runtime PM disabled, clock forced off.\n"); | |
424 | } | |
425 | } | |
426 | ||
427 | /** | |
3d5c3036 | 428 | * pm_clk_notify - Notify routine for device addition and removal. |
85eb8c8d RW |
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 | */ | |
3d5c3036 | 438 | static int pm_clk_notify(struct notifier_block *nb, |
85eb8c8d RW |
439 | unsigned long action, void *data) |
440 | { | |
441 | struct pm_clk_notifier_block *clknb; | |
442 | struct device *dev = data; | |
3b3eca31 | 443 | char **con_id; |
85eb8c8d RW |
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) { | |
4d1518f5 | 450 | case BUS_NOTIFY_BIND_DRIVER: |
85eb8c8d | 451 | if (clknb->con_ids[0]) { |
3b3eca31 RW |
452 | for (con_id = clknb->con_ids; *con_id; con_id++) |
453 | enable_clock(dev, *con_id); | |
85eb8c8d RW |
454 | } else { |
455 | enable_clock(dev, NULL); | |
456 | } | |
457 | break; | |
4d1518f5 | 458 | case BUS_NOTIFY_UNBOUND_DRIVER: |
85eb8c8d | 459 | if (clknb->con_ids[0]) { |
3b3eca31 RW |
460 | for (con_id = clknb->con_ids; *con_id; con_id++) |
461 | disable_clock(dev, *con_id); | |
85eb8c8d RW |
462 | } else { |
463 | disable_clock(dev, NULL); | |
464 | } | |
465 | break; | |
466 | } | |
467 | ||
468 | return 0; | |
469 | } | |
470 | ||
471 | #endif /* !CONFIG_PM_RUNTIME */ | |
472 | ||
473 | /** | |
3d5c3036 | 474 | * pm_clk_add_notifier - Add bus type notifier for power management clocks. |
85eb8c8d RW |
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 | |
3d5c3036 | 479 | * notifier_call member will be replaced with pm_clk_notify(). However, |
85eb8c8d RW |
480 | * the remaining members of @clknb should be populated prior to calling this |
481 | * routine. | |
482 | */ | |
3d5c3036 | 483 | void pm_clk_add_notifier(struct bus_type *bus, |
85eb8c8d RW |
484 | struct pm_clk_notifier_block *clknb) |
485 | { | |
486 | if (!bus || !clknb) | |
487 | return; | |
488 | ||
3d5c3036 | 489 | clknb->nb.notifier_call = pm_clk_notify; |
85eb8c8d RW |
490 | bus_register_notifier(bus, &clknb->nb); |
491 | } |