blob: 2c18d584066d561cc08f35f864a41d2c9471b890 [file] [log] [blame]
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +02001/*
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/io.h>
12#include <linux/pm.h>
13#include <linux/pm_runtime.h>
14#include <linux/clk.h>
15#include <linux/slab.h>
16#include <linux/err.h>
17
Rafael J. Wysockib7b95922011-07-01 22:13:37 +020018#ifdef CONFIG_PM
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020019
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +020020struct pm_clk_data {
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020021 struct list_head clock_list;
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +020022 spinlock_t lock;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020023};
24
25enum pce_status {
26 PCE_STATUS_NONE = 0,
27 PCE_STATUS_ACQUIRED,
28 PCE_STATUS_ENABLED,
29 PCE_STATUS_ERROR,
30};
31
32struct pm_clock_entry {
33 struct list_head node;
34 char *con_id;
35 struct clk *clk;
36 enum pce_status status;
37};
38
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +020039static struct pm_clk_data *__to_pcd(struct device *dev)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020040{
41 return dev ? dev->power.subsys_data : NULL;
42}
43
44/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +020045 * pm_clk_add - Start using a device clock for power management.
46 * @dev: Device whose clock is going to be used for power management.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020047 * @con_id: Connection ID of the clock.
48 *
49 * Add the clock represented by @con_id to the list of clocks used for
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +020050 * the power management of @dev.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020051 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +020052int pm_clk_add(struct device *dev, const char *con_id)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020053{
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +020054 struct pm_clk_data *pcd = __to_pcd(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020055 struct pm_clock_entry *ce;
56
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +020057 if (!pcd)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020058 return -EINVAL;
59
60 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
61 if (!ce) {
62 dev_err(dev, "Not enough memory for clock entry.\n");
63 return -ENOMEM;
64 }
65
66 if (con_id) {
67 ce->con_id = kstrdup(con_id, GFP_KERNEL);
68 if (!ce->con_id) {
69 dev_err(dev,
70 "Not enough memory for clock connection ID.\n");
71 kfree(ce);
72 return -ENOMEM;
73 }
74 }
75
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +020076 spin_lock_irq(&pcd->lock);
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +020077 list_add_tail(&ce->node, &pcd->clock_list);
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +020078 spin_unlock_irq(&pcd->lock);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020079 return 0;
80}
81
82/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +020083 * __pm_clk_remove - Destroy PM clock entry.
84 * @ce: PM clock entry to destroy.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020085 *
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +020086 * This routine must be called under the spinlock protecting the PM list of
87 * clocks corresponding the the @ce's device.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020088 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +020089static void __pm_clk_remove(struct pm_clock_entry *ce)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020090{
91 if (!ce)
92 return;
93
94 list_del(&ce->node);
95
96 if (ce->status < PCE_STATUS_ERROR) {
97 if (ce->status == PCE_STATUS_ENABLED)
98 clk_disable(ce->clk);
99
100 if (ce->status >= PCE_STATUS_ACQUIRED)
101 clk_put(ce->clk);
102 }
103
104 if (ce->con_id)
105 kfree(ce->con_id);
106
107 kfree(ce);
108}
109
110/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200111 * pm_clk_remove - Stop using a device clock for power management.
112 * @dev: Device whose clock should not be used for PM any more.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200113 * @con_id: Connection ID of the clock.
114 *
115 * Remove the clock represented by @con_id from the list of clocks used for
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200116 * the power management of @dev.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200117 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200118void pm_clk_remove(struct device *dev, const char *con_id)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200119{
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200120 struct pm_clk_data *pcd = __to_pcd(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200121 struct pm_clock_entry *ce;
122
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200123 if (!pcd)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200124 return;
125
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200126 spin_lock_irq(&pcd->lock);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200127
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200128 list_for_each_entry(ce, &pcd->clock_list, node) {
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200129 if (!con_id && !ce->con_id) {
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200130 __pm_clk_remove(ce);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200131 break;
132 } else if (!con_id || !ce->con_id) {
133 continue;
134 } else if (!strcmp(con_id, ce->con_id)) {
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200135 __pm_clk_remove(ce);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200136 break;
137 }
138 }
139
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200140 spin_unlock_irq(&pcd->lock);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200141}
142
143/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200144 * pm_clk_init - Initialize a device's list of power management clocks.
145 * @dev: Device to initialize the list of PM clocks for.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200146 *
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200147 * Allocate a struct pm_clk_data object, initialize its lock member and
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200148 * make the @dev's power.subsys_data field point to it.
149 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200150int pm_clk_init(struct device *dev)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200151{
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200152 struct pm_clk_data *pcd;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200153
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200154 pcd = kzalloc(sizeof(*pcd), GFP_KERNEL);
155 if (!pcd) {
156 dev_err(dev, "Not enough memory for PM clock data.\n");
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200157 return -ENOMEM;
158 }
159
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200160 INIT_LIST_HEAD(&pcd->clock_list);
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200161 spin_lock_init(&pcd->lock);
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200162 dev->power.subsys_data = pcd;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200163 return 0;
164}
165
166/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200167 * pm_clk_destroy - Destroy a device's list of power management clocks.
168 * @dev: Device to destroy the list of PM clocks for.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200169 *
170 * Clear the @dev's power.subsys_data field, remove the list of clock entries
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200171 * from the struct pm_clk_data object pointed to by it before and free
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200172 * that object.
173 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200174void pm_clk_destroy(struct device *dev)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200175{
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200176 struct pm_clk_data *pcd = __to_pcd(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200177 struct pm_clock_entry *ce, *c;
178
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200179 if (!pcd)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200180 return;
181
182 dev->power.subsys_data = NULL;
183
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200184 spin_lock_irq(&pcd->lock);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200185
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200186 list_for_each_entry_safe_reverse(ce, c, &pcd->clock_list, node)
187 __pm_clk_remove(ce);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200188
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200189 spin_unlock_irq(&pcd->lock);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200190
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200191 kfree(pcd);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200192}
193
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200194#endif /* CONFIG_PM */
195
196#ifdef CONFIG_PM_RUNTIME
197
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200198/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200199 * pm_clk_acquire - Acquire a device clock.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200200 * @dev: Device whose clock is to be acquired.
201 * @con_id: Connection ID of the clock.
202 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200203static void pm_clk_acquire(struct device *dev,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200204 struct pm_clock_entry *ce)
205{
206 ce->clk = clk_get(dev, ce->con_id);
207 if (IS_ERR(ce->clk)) {
208 ce->status = PCE_STATUS_ERROR;
209 } else {
210 ce->status = PCE_STATUS_ACQUIRED;
211 dev_dbg(dev, "Clock %s managed by runtime PM.\n", ce->con_id);
212 }
213}
214
215/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200216 * pm_clk_suspend - Disable clocks in a device's PM clock list.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200217 * @dev: Device to disable the clocks for.
218 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200219int pm_clk_suspend(struct device *dev)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200220{
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200221 struct pm_clk_data *pcd = __to_pcd(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200222 struct pm_clock_entry *ce;
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200223 unsigned long flags;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200224
225 dev_dbg(dev, "%s()\n", __func__);
226
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200227 if (!pcd)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200228 return 0;
229
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200230 spin_lock_irqsave(&pcd->lock, flags);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200231
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200232 list_for_each_entry_reverse(ce, &pcd->clock_list, node) {
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200233 if (ce->status == PCE_STATUS_NONE)
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200234 pm_clk_acquire(dev, ce);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200235
236 if (ce->status < PCE_STATUS_ERROR) {
237 clk_disable(ce->clk);
238 ce->status = PCE_STATUS_ACQUIRED;
239 }
240 }
241
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200242 spin_unlock_irqrestore(&pcd->lock, flags);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200243
244 return 0;
245}
246
247/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200248 * pm_clk_resume - Enable clocks in a device's PM clock list.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200249 * @dev: Device to enable the clocks for.
250 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200251int pm_clk_resume(struct device *dev)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200252{
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200253 struct pm_clk_data *pcd = __to_pcd(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200254 struct pm_clock_entry *ce;
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200255 unsigned long flags;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200256
257 dev_dbg(dev, "%s()\n", __func__);
258
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200259 if (!pcd)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200260 return 0;
261
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200262 spin_lock_irqsave(&pcd->lock, flags);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200263
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200264 list_for_each_entry(ce, &pcd->clock_list, node) {
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200265 if (ce->status == PCE_STATUS_NONE)
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200266 pm_clk_acquire(dev, ce);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200267
268 if (ce->status < PCE_STATUS_ERROR) {
269 clk_enable(ce->clk);
270 ce->status = PCE_STATUS_ENABLED;
271 }
272 }
273
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200274 spin_unlock_irqrestore(&pcd->lock, flags);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200275
276 return 0;
277}
278
279/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200280 * pm_clk_notify - Notify routine for device addition and removal.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200281 * @nb: Notifier block object this function is a member of.
282 * @action: Operation being carried out by the caller.
283 * @data: Device the routine is being run for.
284 *
285 * For this function to work, @nb must be a member of an object of type
286 * struct pm_clk_notifier_block containing all of the requisite data.
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200287 * Specifically, the pm_domain member of that object is copied to the device's
288 * pm_domain field and its con_ids member is used to populate the device's list
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200289 * of PM clocks, depending on @action.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200290 *
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200291 * If the device's pm_domain field is already populated with a value different
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200292 * from the one stored in the struct pm_clk_notifier_block object, the function
293 * does nothing.
294 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200295static int pm_clk_notify(struct notifier_block *nb,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200296 unsigned long action, void *data)
297{
298 struct pm_clk_notifier_block *clknb;
299 struct device *dev = data;
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200300 char **con_id;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200301 int error;
302
303 dev_dbg(dev, "%s() %ld\n", __func__, action);
304
305 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
306
307 switch (action) {
308 case BUS_NOTIFY_ADD_DEVICE:
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200309 if (dev->pm_domain)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200310 break;
311
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200312 error = pm_clk_init(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200313 if (error)
314 break;
315
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200316 dev->pm_domain = clknb->pm_domain;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200317 if (clknb->con_ids[0]) {
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200318 for (con_id = clknb->con_ids; *con_id; con_id++)
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200319 pm_clk_add(dev, *con_id);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200320 } else {
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200321 pm_clk_add(dev, NULL);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200322 }
323
324 break;
325 case BUS_NOTIFY_DEL_DEVICE:
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200326 if (dev->pm_domain != clknb->pm_domain)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200327 break;
328
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200329 dev->pm_domain = NULL;
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200330 pm_clk_destroy(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200331 break;
332 }
333
334 return 0;
335}
336
337#else /* !CONFIG_PM_RUNTIME */
338
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200339#ifdef CONFIG_PM
340
341/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200342 * pm_clk_suspend - Disable clocks in a device's PM clock list.
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200343 * @dev: Device to disable the clocks for.
344 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200345int pm_clk_suspend(struct device *dev)
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200346{
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200347 struct pm_clk_data *pcd = __to_pcd(dev);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200348 struct pm_clock_entry *ce;
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200349 unsigned long flags;
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200350
351 dev_dbg(dev, "%s()\n", __func__);
352
353 /* If there is no driver, the clocks are already disabled. */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200354 if (!pcd || !dev->driver)
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200355 return 0;
356
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200357 spin_lock_irqsave(&pcd->lock, flags);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200358
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200359 list_for_each_entry_reverse(ce, &pcd->clock_list, node)
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200360 clk_disable(ce->clk);
361
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200362 spin_unlock_irqrestore(&pcd->lock, flags);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200363
364 return 0;
365}
366
367/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200368 * pm_clk_resume - Enable clocks in a device's PM clock list.
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200369 * @dev: Device to enable the clocks for.
370 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200371int pm_clk_resume(struct device *dev)
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200372{
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200373 struct pm_clk_data *pcd = __to_pcd(dev);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200374 struct pm_clock_entry *ce;
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200375 unsigned long flags;
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200376
377 dev_dbg(dev, "%s()\n", __func__);
378
379 /* If there is no driver, the clocks should remain disabled. */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200380 if (!pcd || !dev->driver)
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200381 return 0;
382
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200383 spin_lock_irqsave(&pcd->lock, flags);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200384
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200385 list_for_each_entry(ce, &pcd->clock_list, node)
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200386 clk_enable(ce->clk);
387
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200388 spin_unlock_irqrestore(&pcd->lock, flags);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200389
390 return 0;
391}
392
393#endif /* CONFIG_PM */
394
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200395/**
396 * enable_clock - Enable a device clock.
397 * @dev: Device whose clock is to be enabled.
398 * @con_id: Connection ID of the clock.
399 */
400static void enable_clock(struct device *dev, const char *con_id)
401{
402 struct clk *clk;
403
404 clk = clk_get(dev, con_id);
405 if (!IS_ERR(clk)) {
406 clk_enable(clk);
407 clk_put(clk);
408 dev_info(dev, "Runtime PM disabled, clock forced on.\n");
409 }
410}
411
412/**
413 * disable_clock - Disable a device clock.
414 * @dev: Device whose clock is to be disabled.
415 * @con_id: Connection ID of the clock.
416 */
417static void disable_clock(struct device *dev, const char *con_id)
418{
419 struct clk *clk;
420
421 clk = clk_get(dev, con_id);
422 if (!IS_ERR(clk)) {
423 clk_disable(clk);
424 clk_put(clk);
425 dev_info(dev, "Runtime PM disabled, clock forced off.\n");
426 }
427}
428
429/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200430 * pm_clk_notify - Notify routine for device addition and removal.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200431 * @nb: Notifier block object this function is a member of.
432 * @action: Operation being carried out by the caller.
433 * @data: Device the routine is being run for.
434 *
435 * For this function to work, @nb must be a member of an object of type
436 * struct pm_clk_notifier_block containing all of the requisite data.
437 * Specifically, the con_ids member of that object is used to enable or disable
438 * the device's clocks, depending on @action.
439 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200440static int pm_clk_notify(struct notifier_block *nb,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200441 unsigned long action, void *data)
442{
443 struct pm_clk_notifier_block *clknb;
444 struct device *dev = data;
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200445 char **con_id;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200446
447 dev_dbg(dev, "%s() %ld\n", __func__, action);
448
449 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
450
451 switch (action) {
Rafael J. Wysocki4d1518f2011-06-21 23:24:33 +0200452 case BUS_NOTIFY_BIND_DRIVER:
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200453 if (clknb->con_ids[0]) {
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200454 for (con_id = clknb->con_ids; *con_id; con_id++)
455 enable_clock(dev, *con_id);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200456 } else {
457 enable_clock(dev, NULL);
458 }
459 break;
Rafael J. Wysocki4d1518f2011-06-21 23:24:33 +0200460 case BUS_NOTIFY_UNBOUND_DRIVER:
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200461 if (clknb->con_ids[0]) {
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200462 for (con_id = clknb->con_ids; *con_id; con_id++)
463 disable_clock(dev, *con_id);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200464 } else {
465 disable_clock(dev, NULL);
466 }
467 break;
468 }
469
470 return 0;
471}
472
473#endif /* !CONFIG_PM_RUNTIME */
474
475/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200476 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200477 * @bus: Bus type to add the notifier to.
478 * @clknb: Notifier to be added to the given bus type.
479 *
480 * The nb member of @clknb is not expected to be initialized and its
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200481 * notifier_call member will be replaced with pm_clk_notify(). However,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200482 * the remaining members of @clknb should be populated prior to calling this
483 * routine.
484 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200485void pm_clk_add_notifier(struct bus_type *bus,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200486 struct pm_clk_notifier_block *clknb)
487{
488 if (!bus || !clknb)
489 return;
490
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200491 clknb->nb.notifier_call = pm_clk_notify;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200492 bus_register_notifier(bus, &clknb->nb);
493}