2 * drivers/base/power/clock_ops.c - Generic clock manipulation PM callbacks
4 * Copyright (c) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 * This file is released under the GPLv2.
9 #include <linux/init.h>
10 #include <linux/kernel.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/clk.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
18 #ifdef CONFIG_PM_RUNTIME
20 struct pm_runtime_clk_data {
21 struct list_head clock_list;
32 struct pm_clock_entry {
33 struct list_head node;
36 enum pce_status status;
39 static struct pm_runtime_clk_data *__to_prd(struct device *dev)
41 return dev ? dev->power.subsys_data : NULL;
45 * pm_runtime_clk_add - Start using a device clock for runtime PM.
46 * @dev: Device whose clock is going to be used for runtime PM.
47 * @con_id: Connection ID of the clock.
49 * Add the clock represented by @con_id to the list of clocks used for
50 * the runtime PM of @dev.
52 int pm_runtime_clk_add(struct device *dev, const char *con_id)
54 struct pm_runtime_clk_data *prd = __to_prd(dev);
55 struct pm_clock_entry *ce;
60 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
62 dev_err(dev, "Not enough memory for clock entry.\n");
67 ce->con_id = kstrdup(con_id, GFP_KERNEL);
70 "Not enough memory for clock connection ID.\n");
76 mutex_lock(&prd->lock);
77 list_add_tail(&ce->node, &prd->clock_list);
78 mutex_unlock(&prd->lock);
83 * __pm_runtime_clk_remove - Destroy runtime PM clock entry.
84 * @ce: Runtime PM clock entry to destroy.
86 * This routine must be called under the mutex protecting the runtime PM list
87 * of clocks corresponding the the @ce's device.
89 static void __pm_runtime_clk_remove(struct pm_clock_entry *ce)
96 if (ce->status < PCE_STATUS_ERROR) {
97 if (ce->status == PCE_STATUS_ENABLED)
100 if (ce->status >= PCE_STATUS_ACQUIRED)
111 * pm_runtime_clk_remove - Stop using a device clock for runtime PM.
112 * @dev: Device whose clock should not be used for runtime PM any more.
113 * @con_id: Connection ID of the clock.
115 * Remove the clock represented by @con_id from the list of clocks used for
116 * the runtime PM of @dev.
118 void pm_runtime_clk_remove(struct device *dev, const char *con_id)
120 struct pm_runtime_clk_data *prd = __to_prd(dev);
121 struct pm_clock_entry *ce;
126 mutex_lock(&prd->lock);
128 list_for_each_entry(ce, &prd->clock_list, node) {
129 if (!con_id && !ce->con_id) {
130 __pm_runtime_clk_remove(ce);
132 } else if (!con_id || !ce->con_id) {
134 } else if (!strcmp(con_id, ce->con_id)) {
135 __pm_runtime_clk_remove(ce);
140 mutex_unlock(&prd->lock);
144 * pm_runtime_clk_init - Initialize a device's list of runtime PM clocks.
145 * @dev: Device to initialize the list of runtime PM clocks for.
147 * Allocate a struct pm_runtime_clk_data object, initialize its lock member and
148 * make the @dev's power.subsys_data field point to it.
150 int pm_runtime_clk_init(struct device *dev)
152 struct pm_runtime_clk_data *prd;
154 prd = kzalloc(sizeof(*prd), GFP_KERNEL);
156 dev_err(dev, "Not enough memory fo runtime PM data.\n");
160 INIT_LIST_HEAD(&prd->clock_list);
161 mutex_init(&prd->lock);
162 dev->power.subsys_data = prd;
167 * pm_runtime_clk_destroy - Destroy a device's list of runtime PM clocks.
168 * @dev: Device to destroy the list of runtime PM clocks for.
170 * Clear the @dev's power.subsys_data field, remove the list of clock entries
171 * from the struct pm_runtime_clk_data object pointed to by it before and free
174 void pm_runtime_clk_destroy(struct device *dev)
176 struct pm_runtime_clk_data *prd = __to_prd(dev);
177 struct pm_clock_entry *ce, *c;
182 dev->power.subsys_data = NULL;
184 mutex_lock(&prd->lock);
186 list_for_each_entry_safe_reverse(ce, c, &prd->clock_list, node)
187 __pm_runtime_clk_remove(ce);
189 mutex_unlock(&prd->lock);
195 * pm_runtime_clk_acquire - Acquire a device clock.
196 * @dev: Device whose clock is to be acquired.
197 * @con_id: Connection ID of the clock.
199 static void pm_runtime_clk_acquire(struct device *dev,
200 struct pm_clock_entry *ce)
202 ce->clk = clk_get(dev, ce->con_id);
203 if (IS_ERR(ce->clk)) {
204 ce->status = PCE_STATUS_ERROR;
206 ce->status = PCE_STATUS_ACQUIRED;
207 dev_dbg(dev, "Clock %s managed by runtime PM.\n", ce->con_id);
212 * pm_runtime_clk_suspend - Disable clocks in a device's runtime PM clock list.
213 * @dev: Device to disable the clocks for.
215 int pm_runtime_clk_suspend(struct device *dev)
217 struct pm_runtime_clk_data *prd = __to_prd(dev);
218 struct pm_clock_entry *ce;
220 dev_dbg(dev, "%s()\n", __func__);
225 mutex_lock(&prd->lock);
227 list_for_each_entry_reverse(ce, &prd->clock_list, node) {
228 if (ce->status == PCE_STATUS_NONE)
229 pm_runtime_clk_acquire(dev, ce);
231 if (ce->status < PCE_STATUS_ERROR) {
232 clk_disable(ce->clk);
233 ce->status = PCE_STATUS_ACQUIRED;
237 mutex_unlock(&prd->lock);
243 * pm_runtime_clk_resume - Enable clocks in a device's runtime PM clock list.
244 * @dev: Device to enable the clocks for.
246 int pm_runtime_clk_resume(struct device *dev)
248 struct pm_runtime_clk_data *prd = __to_prd(dev);
249 struct pm_clock_entry *ce;
251 dev_dbg(dev, "%s()\n", __func__);
256 mutex_lock(&prd->lock);
258 list_for_each_entry(ce, &prd->clock_list, node) {
259 if (ce->status == PCE_STATUS_NONE)
260 pm_runtime_clk_acquire(dev, ce);
262 if (ce->status < PCE_STATUS_ERROR) {
264 ce->status = PCE_STATUS_ENABLED;
268 mutex_unlock(&prd->lock);
274 * pm_runtime_clk_notify - Notify routine for device addition and removal.
275 * @nb: Notifier block object this function is a member of.
276 * @action: Operation being carried out by the caller.
277 * @data: Device the routine is being run for.
279 * For this function to work, @nb must be a member of an object of type
280 * struct pm_clk_notifier_block containing all of the requisite data.
281 * Specifically, the pwr_domain member of that object is copied to the device's
282 * pwr_domain field and its con_ids member is used to populate the device's list
283 * of runtime PM clocks, depending on @action.
285 * If the device's pwr_domain field is already populated with a value different
286 * from the one stored in the struct pm_clk_notifier_block object, the function
289 static int pm_runtime_clk_notify(struct notifier_block *nb,
290 unsigned long action, void *data)
292 struct pm_clk_notifier_block *clknb;
293 struct device *dev = data;
297 dev_dbg(dev, "%s() %ld\n", __func__, action);
299 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
302 case BUS_NOTIFY_ADD_DEVICE:
306 error = pm_runtime_clk_init(dev);
310 dev->pwr_domain = clknb->pwr_domain;
311 if (clknb->con_ids[0]) {
312 for (con_id = clknb->con_ids; *con_id; con_id++)
313 pm_runtime_clk_add(dev, *con_id);
315 pm_runtime_clk_add(dev, NULL);
319 case BUS_NOTIFY_DEL_DEVICE:
320 if (dev->pwr_domain != clknb->pwr_domain)
323 dev->pwr_domain = NULL;
324 pm_runtime_clk_destroy(dev);
331 #else /* !CONFIG_PM_RUNTIME */
334 * enable_clock - Enable a device clock.
335 * @dev: Device whose clock is to be enabled.
336 * @con_id: Connection ID of the clock.
338 static void enable_clock(struct device *dev, const char *con_id)
342 clk = clk_get(dev, con_id);
346 dev_info(dev, "Runtime PM disabled, clock forced on.\n");
351 * disable_clock - Disable a device clock.
352 * @dev: Device whose clock is to be disabled.
353 * @con_id: Connection ID of the clock.
355 static void disable_clock(struct device *dev, const char *con_id)
359 clk = clk_get(dev, con_id);
363 dev_info(dev, "Runtime PM disabled, clock forced off.\n");
368 * pm_runtime_clk_notify - Notify routine for device addition and removal.
369 * @nb: Notifier block object this function is a member of.
370 * @action: Operation being carried out by the caller.
371 * @data: Device the routine is being run for.
373 * For this function to work, @nb must be a member of an object of type
374 * struct pm_clk_notifier_block containing all of the requisite data.
375 * Specifically, the con_ids member of that object is used to enable or disable
376 * the device's clocks, depending on @action.
378 static int pm_runtime_clk_notify(struct notifier_block *nb,
379 unsigned long action, void *data)
381 struct pm_clk_notifier_block *clknb;
382 struct device *dev = data;
385 dev_dbg(dev, "%s() %ld\n", __func__, action);
387 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
390 case BUS_NOTIFY_BIND_DRIVER:
391 if (clknb->con_ids[0]) {
392 for (con_id = clknb->con_ids; *con_id; con_id++)
393 enable_clock(dev, *con_id);
395 enable_clock(dev, NULL);
398 case BUS_NOTIFY_UNBOUND_DRIVER:
399 if (clknb->con_ids[0]) {
400 for (con_id = clknb->con_ids; *con_id; con_id++)
401 disable_clock(dev, *con_id);
403 disable_clock(dev, NULL);
411 #endif /* !CONFIG_PM_RUNTIME */
414 * pm_runtime_clk_add_notifier - Add bus type notifier for runtime PM clocks.
415 * @bus: Bus type to add the notifier to.
416 * @clknb: Notifier to be added to the given bus type.
418 * The nb member of @clknb is not expected to be initialized and its
419 * notifier_call member will be replaced with pm_runtime_clk_notify(). However,
420 * the remaining members of @clknb should be populated prior to calling this
423 void pm_runtime_clk_add_notifier(struct bus_type *bus,
424 struct pm_clk_notifier_block *clknb)
429 clknb->nb.notifier_call = pm_runtime_clk_notify;
430 bus_register_notifier(bus, &clknb->nb);