blob: b32b5d47b3c542cec1a111abdeeba6e9a535c439 [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
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +02009#include <linux/kernel.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050010#include <linux/device.h>
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020011#include <linux/io.h>
12#include <linux/pm.h>
Rafael J. Wysockib5e8d262011-08-25 15:34:19 +020013#include <linux/pm_clock.h>
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020014#include <linux/clk.h>
Geert Uytterhoeven245bd6f2014-11-06 15:51:00 +020015#include <linux/clkdev.h>
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020016#include <linux/slab.h>
17#include <linux/err.h>
18
Rafael J. Wysockib7b95922011-07-01 22:13:37 +020019#ifdef CONFIG_PM
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020020
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020021enum pce_status {
22 PCE_STATUS_NONE = 0,
23 PCE_STATUS_ACQUIRED,
24 PCE_STATUS_ENABLED,
25 PCE_STATUS_ERROR,
26};
27
28struct pm_clock_entry {
29 struct list_head node;
30 char *con_id;
31 struct clk *clk;
32 enum pce_status status;
33};
34
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020035/**
Ben Dooks5cda3fb2014-01-14 12:23:42 +000036 * pm_clk_enable - Enable a clock, reporting any errors
37 * @dev: The device for the given clock
Strashko, Grygorii471f7702014-11-06 15:51:01 +020038 * @ce: PM clock entry corresponding to the clock.
Ben Dooks5cda3fb2014-01-14 12:23:42 +000039 */
Strashko, Grygorii471f7702014-11-06 15:51:01 +020040static inline int __pm_clk_enable(struct device *dev, struct pm_clock_entry *ce)
Ben Dooks5cda3fb2014-01-14 12:23:42 +000041{
Strashko, Grygorii471f7702014-11-06 15:51:01 +020042 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 }
Ben Dooks5cda3fb2014-01-14 12:23:42 +000052
53 return ret;
54}
55
56/**
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +020057 * 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 */
61static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
62{
Geert Uytterhoeven245bd6f2014-11-06 15:51:00 +020063 if (!ce->clk)
64 ce->clk = clk_get(dev, ce->con_id);
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +020065 if (IS_ERR(ce->clk)) {
66 ce->status = PCE_STATUS_ERROR;
67 } else {
Ben Dooks8a6720e2014-01-14 12:23:40 +000068 clk_prepare(ce->clk);
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +020069 ce->status = PCE_STATUS_ACQUIRED;
70 dev_dbg(dev, "Clock %s managed by runtime PM.\n", ce->con_id);
71 }
72}
73
Geert Uytterhoeven245bd6f2014-11-06 15:51:00 +020074static int __pm_clk_add(struct device *dev, const char *con_id,
75 struct clk *clk)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020076{
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +020077 struct pm_subsys_data *psd = dev_to_psd(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020078 struct pm_clock_entry *ce;
79
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +020080 if (!psd)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020081 return -EINVAL;
82
83 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
84 if (!ce) {
85 dev_err(dev, "Not enough memory for clock entry.\n");
86 return -ENOMEM;
87 }
88
89 if (con_id) {
90 ce->con_id = kstrdup(con_id, GFP_KERNEL);
91 if (!ce->con_id) {
92 dev_err(dev,
93 "Not enough memory for clock connection ID.\n");
94 kfree(ce);
95 return -ENOMEM;
96 }
Geert Uytterhoeven245bd6f2014-11-06 15:51:00 +020097 } else {
98 if (IS_ERR(ce->clk) || !__clk_get(clk)) {
99 kfree(ce);
100 return -ENOENT;
101 }
102 ce->clk = clk;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200103 }
104
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +0200105 pm_clk_acquire(dev, ce);
106
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200107 spin_lock_irq(&psd->lock);
108 list_add_tail(&ce->node, &psd->clock_list);
109 spin_unlock_irq(&psd->lock);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200110 return 0;
111}
112
113/**
Geert Uytterhoeven245bd6f2014-11-06 15:51:00 +0200114 * pm_clk_add - Start using a device clock for power management.
115 * @dev: Device whose clock is going to be used for power management.
116 * @con_id: Connection ID of the clock.
117 *
118 * Add the clock represented by @con_id to the list of clocks used for
119 * the power management of @dev.
120 */
121int pm_clk_add(struct device *dev, const char *con_id)
122{
123 return __pm_clk_add(dev, con_id, NULL);
124}
125
126/**
127 * pm_clk_add_clk - Start using a device clock for power management.
128 * @dev: Device whose clock is going to be used for power management.
129 * @clk: Clock pointer
130 *
131 * Add the clock to the list of clocks used for the power management of @dev.
132 * It will increment refcount on clock pointer, use clk_put() on it when done.
133 */
134int pm_clk_add_clk(struct device *dev, struct clk *clk)
135{
136 return __pm_clk_add(dev, NULL, clk);
137}
138
139/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200140 * __pm_clk_remove - Destroy PM clock entry.
141 * @ce: PM clock entry to destroy.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200142 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200143static void __pm_clk_remove(struct pm_clock_entry *ce)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200144{
145 if (!ce)
146 return;
147
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200148 if (ce->status < PCE_STATUS_ERROR) {
149 if (ce->status == PCE_STATUS_ENABLED)
Ben Dooks8a6720e2014-01-14 12:23:40 +0000150 clk_disable(ce->clk);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200151
Ben Dooks8a6720e2014-01-14 12:23:40 +0000152 if (ce->status >= PCE_STATUS_ACQUIRED) {
153 clk_unprepare(ce->clk);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200154 clk_put(ce->clk);
Ben Dooks8a6720e2014-01-14 12:23:40 +0000155 }
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200156 }
157
Jonghwan Choi0ab1e792011-10-22 00:22:54 +0200158 kfree(ce->con_id);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200159 kfree(ce);
160}
161
162/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200163 * pm_clk_remove - Stop using a device clock for power management.
164 * @dev: Device whose clock should not be used for PM any more.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200165 * @con_id: Connection ID of the clock.
166 *
167 * Remove the clock represented by @con_id from the list of clocks used for
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200168 * the power management of @dev.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200169 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200170void pm_clk_remove(struct device *dev, const char *con_id)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200171{
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200172 struct pm_subsys_data *psd = dev_to_psd(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200173 struct pm_clock_entry *ce;
174
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200175 if (!psd)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200176 return;
177
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200178 spin_lock_irq(&psd->lock);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200179
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200180 list_for_each_entry(ce, &psd->clock_list, node) {
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +0200181 if (!con_id && !ce->con_id)
182 goto remove;
183 else if (!con_id || !ce->con_id)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200184 continue;
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +0200185 else if (!strcmp(con_id, ce->con_id))
186 goto remove;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200187 }
188
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200189 spin_unlock_irq(&psd->lock);
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +0200190 return;
191
192 remove:
193 list_del(&ce->node);
Rafael J. Wysocki0d41da22011-09-26 20:12:45 +0200194 spin_unlock_irq(&psd->lock);
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +0200195
196 __pm_clk_remove(ce);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200197}
198
199/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200200 * pm_clk_init - Initialize a device's list of power management clocks.
201 * @dev: Device to initialize the list of PM clocks for.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200202 *
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200203 * Initialize the lock and clock_list members of the device's pm_subsys_data
204 * object.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200205 */
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200206void pm_clk_init(struct device *dev)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200207{
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200208 struct pm_subsys_data *psd = dev_to_psd(dev);
Rafael J. Wysockief27bed12011-08-25 15:34:01 +0200209 if (psd)
210 INIT_LIST_HEAD(&psd->clock_list);
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200211}
212
213/**
214 * pm_clk_create - Create and initialize a device's list of PM clocks.
215 * @dev: Device to create and initialize the list of PM clocks for.
216 *
217 * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
218 * members and make the @dev's power.subsys_data field point to it.
219 */
220int pm_clk_create(struct device *dev)
221{
Rafael J. Wysocki77254952012-08-07 13:50:14 +0200222 return dev_pm_get_subsys_data(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200223}
224
225/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200226 * pm_clk_destroy - Destroy a device's list of power management clocks.
227 * @dev: Device to destroy the list of PM clocks for.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200228 *
229 * Clear the @dev's power.subsys_data field, remove the list of clock entries
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200230 * from the struct pm_subsys_data object pointed to by it before and free
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200231 * that object.
232 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200233void pm_clk_destroy(struct device *dev)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200234{
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200235 struct pm_subsys_data *psd = dev_to_psd(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200236 struct pm_clock_entry *ce, *c;
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +0200237 struct list_head list;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200238
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200239 if (!psd)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200240 return;
241
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +0200242 INIT_LIST_HEAD(&list);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200243
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200244 spin_lock_irq(&psd->lock);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200245
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200246 list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +0200247 list_move(&ce->node, &list);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200248
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200249 spin_unlock_irq(&psd->lock);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200250
Rafael J. Wysockief27bed12011-08-25 15:34:01 +0200251 dev_pm_put_subsys_data(dev);
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +0200252
253 list_for_each_entry_safe_reverse(ce, c, &list, node) {
254 list_del(&ce->node);
255 __pm_clk_remove(ce);
256 }
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200257}
258
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200259#endif /* CONFIG_PM */
260
261#ifdef CONFIG_PM_RUNTIME
262
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200263/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200264 * pm_clk_suspend - Disable clocks in a device's PM clock list.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200265 * @dev: Device to disable the clocks for.
266 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200267int pm_clk_suspend(struct device *dev)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200268{
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200269 struct pm_subsys_data *psd = dev_to_psd(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200270 struct pm_clock_entry *ce;
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200271 unsigned long flags;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200272
273 dev_dbg(dev, "%s()\n", __func__);
274
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200275 if (!psd)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200276 return 0;
277
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200278 spin_lock_irqsave(&psd->lock, flags);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200279
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200280 list_for_each_entry_reverse(ce, &psd->clock_list, node) {
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200281 if (ce->status < PCE_STATUS_ERROR) {
Magnus Damm24050952011-11-10 00:44:10 +0100282 if (ce->status == PCE_STATUS_ENABLED)
283 clk_disable(ce->clk);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200284 ce->status = PCE_STATUS_ACQUIRED;
285 }
286 }
287
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200288 spin_unlock_irqrestore(&psd->lock, flags);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200289
290 return 0;
291}
292
293/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200294 * pm_clk_resume - Enable clocks in a device's PM clock list.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200295 * @dev: Device to enable the clocks for.
296 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200297int pm_clk_resume(struct device *dev)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200298{
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200299 struct pm_subsys_data *psd = dev_to_psd(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200300 struct pm_clock_entry *ce;
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200301 unsigned long flags;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200302
303 dev_dbg(dev, "%s()\n", __func__);
304
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200305 if (!psd)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200306 return 0;
307
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200308 spin_lock_irqsave(&psd->lock, flags);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200309
Strashko, Grygorii471f7702014-11-06 15:51:01 +0200310 list_for_each_entry(ce, &psd->clock_list, node)
311 __pm_clk_enable(dev, ce);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200312
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200313 spin_unlock_irqrestore(&psd->lock, flags);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200314
315 return 0;
316}
317
318/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200319 * pm_clk_notify - Notify routine for device addition and removal.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200320 * @nb: Notifier block object this function is a member of.
321 * @action: Operation being carried out by the caller.
322 * @data: Device the routine is being run for.
323 *
324 * For this function to work, @nb must be a member of an object of type
325 * struct pm_clk_notifier_block containing all of the requisite data.
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200326 * Specifically, the pm_domain member of that object is copied to the device's
327 * pm_domain field and its con_ids member is used to populate the device's list
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200328 * of PM clocks, depending on @action.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200329 *
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200330 * If the device's pm_domain field is already populated with a value different
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200331 * from the one stored in the struct pm_clk_notifier_block object, the function
332 * does nothing.
333 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200334static int pm_clk_notify(struct notifier_block *nb,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200335 unsigned long action, void *data)
336{
337 struct pm_clk_notifier_block *clknb;
338 struct device *dev = data;
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200339 char **con_id;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200340 int error;
341
342 dev_dbg(dev, "%s() %ld\n", __func__, action);
343
344 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
345
346 switch (action) {
347 case BUS_NOTIFY_ADD_DEVICE:
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200348 if (dev->pm_domain)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200349 break;
350
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200351 error = pm_clk_create(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200352 if (error)
353 break;
354
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200355 dev->pm_domain = clknb->pm_domain;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200356 if (clknb->con_ids[0]) {
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200357 for (con_id = clknb->con_ids; *con_id; con_id++)
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200358 pm_clk_add(dev, *con_id);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200359 } else {
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200360 pm_clk_add(dev, NULL);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200361 }
362
363 break;
364 case BUS_NOTIFY_DEL_DEVICE:
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200365 if (dev->pm_domain != clknb->pm_domain)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200366 break;
367
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200368 dev->pm_domain = NULL;
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200369 pm_clk_destroy(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200370 break;
371 }
372
373 return 0;
374}
375
376#else /* !CONFIG_PM_RUNTIME */
377
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200378#ifdef CONFIG_PM
379
380/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200381 * pm_clk_suspend - Disable clocks in a device's PM clock list.
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200382 * @dev: Device to disable the clocks for.
383 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200384int pm_clk_suspend(struct device *dev)
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200385{
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200386 struct pm_subsys_data *psd = dev_to_psd(dev);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200387 struct pm_clock_entry *ce;
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200388 unsigned long flags;
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200389
390 dev_dbg(dev, "%s()\n", __func__);
391
392 /* If there is no driver, the clocks are already disabled. */
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200393 if (!psd || !dev->driver)
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200394 return 0;
395
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200396 spin_lock_irqsave(&psd->lock, flags);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200397
Geert Uytterhoevena968bed2014-10-01 20:38:17 +0200398 list_for_each_entry_reverse(ce, &psd->clock_list, node) {
399 if (ce->status < PCE_STATUS_ERROR) {
400 if (ce->status == PCE_STATUS_ENABLED)
401 clk_disable(ce->clk);
402 ce->status = PCE_STATUS_ACQUIRED;
403 }
404 }
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200405
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200406 spin_unlock_irqrestore(&psd->lock, flags);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200407
408 return 0;
409}
410
411/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200412 * pm_clk_resume - Enable clocks in a device's PM clock list.
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200413 * @dev: Device to enable the clocks for.
414 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200415int pm_clk_resume(struct device *dev)
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200416{
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200417 struct pm_subsys_data *psd = dev_to_psd(dev);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200418 struct pm_clock_entry *ce;
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200419 unsigned long flags;
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200420
421 dev_dbg(dev, "%s()\n", __func__);
422
423 /* If there is no driver, the clocks should remain disabled. */
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200424 if (!psd || !dev->driver)
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200425 return 0;
426
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200427 spin_lock_irqsave(&psd->lock, flags);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200428
Strashko, Grygorii471f7702014-11-06 15:51:01 +0200429 list_for_each_entry(ce, &psd->clock_list, node)
430 __pm_clk_enable(dev, ce);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200431
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200432 spin_unlock_irqrestore(&psd->lock, flags);
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200433
434 return 0;
435}
436
437#endif /* CONFIG_PM */
438
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200439/**
440 * enable_clock - Enable a device clock.
441 * @dev: Device whose clock is to be enabled.
442 * @con_id: Connection ID of the clock.
443 */
444static void enable_clock(struct device *dev, const char *con_id)
445{
446 struct clk *clk;
447
448 clk = clk_get(dev, con_id);
449 if (!IS_ERR(clk)) {
Murali Karicheric122f272012-10-23 01:18:40 +0200450 clk_prepare_enable(clk);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200451 clk_put(clk);
452 dev_info(dev, "Runtime PM disabled, clock forced on.\n");
453 }
454}
455
456/**
457 * disable_clock - Disable a device clock.
458 * @dev: Device whose clock is to be disabled.
459 * @con_id: Connection ID of the clock.
460 */
461static void disable_clock(struct device *dev, const char *con_id)
462{
463 struct clk *clk;
464
465 clk = clk_get(dev, con_id);
466 if (!IS_ERR(clk)) {
Murali Karicheric122f272012-10-23 01:18:40 +0200467 clk_disable_unprepare(clk);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200468 clk_put(clk);
469 dev_info(dev, "Runtime PM disabled, clock forced off.\n");
470 }
471}
472
473/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200474 * pm_clk_notify - Notify routine for device addition and removal.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200475 * @nb: Notifier block object this function is a member of.
476 * @action: Operation being carried out by the caller.
477 * @data: Device the routine is being run for.
478 *
479 * For this function to work, @nb must be a member of an object of type
480 * struct pm_clk_notifier_block containing all of the requisite data.
481 * Specifically, the con_ids member of that object is used to enable or disable
482 * the device's clocks, depending on @action.
483 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200484static int pm_clk_notify(struct notifier_block *nb,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200485 unsigned long action, void *data)
486{
487 struct pm_clk_notifier_block *clknb;
488 struct device *dev = data;
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200489 char **con_id;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200490
491 dev_dbg(dev, "%s() %ld\n", __func__, action);
492
493 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
494
495 switch (action) {
Rafael J. Wysocki4d1518f2011-06-21 23:24:33 +0200496 case BUS_NOTIFY_BIND_DRIVER:
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200497 if (clknb->con_ids[0]) {
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200498 for (con_id = clknb->con_ids; *con_id; con_id++)
499 enable_clock(dev, *con_id);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200500 } else {
501 enable_clock(dev, NULL);
502 }
503 break;
Rafael J. Wysocki4d1518f2011-06-21 23:24:33 +0200504 case BUS_NOTIFY_UNBOUND_DRIVER:
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200505 if (clknb->con_ids[0]) {
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200506 for (con_id = clknb->con_ids; *con_id; con_id++)
507 disable_clock(dev, *con_id);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200508 } else {
509 disable_clock(dev, NULL);
510 }
511 break;
512 }
513
514 return 0;
515}
516
517#endif /* !CONFIG_PM_RUNTIME */
518
519/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200520 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200521 * @bus: Bus type to add the notifier to.
522 * @clknb: Notifier to be added to the given bus type.
523 *
524 * The nb member of @clknb is not expected to be initialized and its
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200525 * notifier_call member will be replaced with pm_clk_notify(). However,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200526 * the remaining members of @clknb should be populated prior to calling this
527 * routine.
528 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200529void pm_clk_add_notifier(struct bus_type *bus,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200530 struct pm_clk_notifier_block *clknb)
531{
532 if (!bus || !clknb)
533 return;
534
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200535 clknb->nb.notifier_call = pm_clk_notify;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200536 bus_register_notifier(bus, &clknb->nb);
537}