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