blob: c39b8617280feff712096e6699ffe6d4f3db13f8 [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>
Rajendra Nayak75f50402015-04-23 14:03:09 +053018#include <linux/pm_runtime.h>
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020019
Geert Uytterhoevena6175612015-10-19 10:16:07 +020020#ifdef CONFIG_PM_CLK
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020021
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020022enum pce_status {
23 PCE_STATUS_NONE = 0,
24 PCE_STATUS_ACQUIRED,
25 PCE_STATUS_ENABLED,
26 PCE_STATUS_ERROR,
27};
28
29struct pm_clock_entry {
30 struct list_head node;
31 char *con_id;
32 struct clk *clk;
33 enum pce_status status;
34};
35
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020036/**
Ben Dooks5cda3fb2014-01-14 12:23:42 +000037 * pm_clk_enable - Enable a clock, reporting any errors
38 * @dev: The device for the given clock
Strashko, Grygorii471f7702014-11-06 15:51:01 +020039 * @ce: PM clock entry corresponding to the clock.
Ben Dooks5cda3fb2014-01-14 12:23:42 +000040 */
Colin Ian Kingf4745a92015-06-29 22:13:38 +010041static inline void __pm_clk_enable(struct device *dev, struct pm_clock_entry *ce)
Ben Dooks5cda3fb2014-01-14 12:23:42 +000042{
Strashko, Grygorii471f7702014-11-06 15:51:01 +020043 int ret;
44
45 if (ce->status < PCE_STATUS_ERROR) {
46 ret = clk_enable(ce->clk);
47 if (!ret)
48 ce->status = PCE_STATUS_ENABLED;
49 else
50 dev_err(dev, "%s: failed to enable clk %p, error %d\n",
51 __func__, ce->clk, ret);
52 }
Ben Dooks5cda3fb2014-01-14 12:23:42 +000053}
54
55/**
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +020056 * pm_clk_acquire - Acquire a device clock.
57 * @dev: Device whose clock is to be acquired.
58 * @ce: PM clock entry corresponding to the clock.
59 */
60static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
61{
Geert Uytterhoeven245bd6f2014-11-06 15:51:00 +020062 if (!ce->clk)
63 ce->clk = clk_get(dev, ce->con_id);
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +020064 if (IS_ERR(ce->clk)) {
65 ce->status = PCE_STATUS_ERROR;
66 } else {
Ben Dooks8a6720e2014-01-14 12:23:40 +000067 clk_prepare(ce->clk);
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +020068 ce->status = PCE_STATUS_ACQUIRED;
Geert Uytterhoevenf17f4ad2015-05-29 18:35:31 +020069 dev_dbg(dev, "Clock %pC con_id %s managed by runtime PM.\n",
70 ce->clk, ce->con_id);
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +020071 }
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);
Quentin Lambert59d84ca2015-02-09 10:45:32 +010084 if (!ce)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020085 return -ENOMEM;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +020086
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 }
Geert Uytterhoeven245bd6f2014-11-06 15:51:00 +020095 } else {
Stephen Boyd772b0502015-10-01 21:05:09 +020096 if (IS_ERR(clk)) {
Geert Uytterhoeven245bd6f2014-11-06 15:51:00 +020097 kfree(ce);
98 return -ENOENT;
99 }
100 ce->clk = clk;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200101 }
102
Rafael J. Wysockie8b364b2011-09-26 19:40:23 +0200103 pm_clk_acquire(dev, ce);
104
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200105 spin_lock_irq(&psd->lock);
106 list_add_tail(&ce->node, &psd->clock_list);
107 spin_unlock_irq(&psd->lock);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200108 return 0;
109}
110
111/**
Geert Uytterhoeven245bd6f2014-11-06 15:51:00 +0200112 * 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 */
119int 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.
Stephen Boyd772b0502015-10-01 21:05:09 +0200130 * The power-management code will take control of the clock reference, so
131 * callers should not call clk_put() on @clk after this function sucessfully
132 * returned.
Geert Uytterhoeven245bd6f2014-11-06 15:51:00 +0200133 */
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. Wysockief27bed2011-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. Wysockief27bed2011-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
259/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200260 * pm_clk_suspend - Disable clocks in a device's PM clock list.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200261 * @dev: Device to disable the clocks for.
262 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200263int pm_clk_suspend(struct device *dev)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200264{
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200265 struct pm_subsys_data *psd = dev_to_psd(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200266 struct pm_clock_entry *ce;
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200267 unsigned long flags;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200268
269 dev_dbg(dev, "%s()\n", __func__);
270
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200271 if (!psd)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200272 return 0;
273
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200274 spin_lock_irqsave(&psd->lock, flags);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200275
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200276 list_for_each_entry_reverse(ce, &psd->clock_list, node) {
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200277 if (ce->status < PCE_STATUS_ERROR) {
Magnus Damm24050952011-11-10 00:44:10 +0100278 if (ce->status == PCE_STATUS_ENABLED)
279 clk_disable(ce->clk);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200280 ce->status = PCE_STATUS_ACQUIRED;
281 }
282 }
283
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200284 spin_unlock_irqrestore(&psd->lock, flags);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200285
286 return 0;
287}
288
289/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200290 * pm_clk_resume - Enable clocks in a device's PM clock list.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200291 * @dev: Device to enable the clocks for.
292 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200293int pm_clk_resume(struct device *dev)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200294{
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200295 struct pm_subsys_data *psd = dev_to_psd(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200296 struct pm_clock_entry *ce;
Rafael J. Wysockib7ab83e2011-08-24 21:40:56 +0200297 unsigned long flags;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200298
299 dev_dbg(dev, "%s()\n", __func__);
300
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200301 if (!psd)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200302 return 0;
303
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200304 spin_lock_irqsave(&psd->lock, flags);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200305
Strashko, Grygorii471f7702014-11-06 15:51:01 +0200306 list_for_each_entry(ce, &psd->clock_list, node)
307 __pm_clk_enable(dev, ce);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200308
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200309 spin_unlock_irqrestore(&psd->lock, flags);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200310
311 return 0;
312}
313
314/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200315 * pm_clk_notify - Notify routine for device addition and removal.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200316 * @nb: Notifier block object this function is a member of.
317 * @action: Operation being carried out by the caller.
318 * @data: Device the routine is being run for.
319 *
320 * For this function to work, @nb must be a member of an object of type
321 * struct pm_clk_notifier_block containing all of the requisite data.
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200322 * Specifically, the pm_domain member of that object is copied to the device's
323 * pm_domain field and its con_ids member is used to populate the device's list
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200324 * of PM clocks, depending on @action.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200325 *
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200326 * If the device's pm_domain field is already populated with a value different
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200327 * from the one stored in the struct pm_clk_notifier_block object, the function
328 * does nothing.
329 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200330static int pm_clk_notify(struct notifier_block *nb,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200331 unsigned long action, void *data)
332{
333 struct pm_clk_notifier_block *clknb;
334 struct device *dev = data;
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200335 char **con_id;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200336 int error;
337
338 dev_dbg(dev, "%s() %ld\n", __func__, action);
339
340 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
341
342 switch (action) {
343 case BUS_NOTIFY_ADD_DEVICE:
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200344 if (dev->pm_domain)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200345 break;
346
Rafael J. Wysocki5c095a02011-08-25 15:33:50 +0200347 error = pm_clk_create(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200348 if (error)
349 break;
350
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200351 dev->pm_domain = clknb->pm_domain;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200352 if (clknb->con_ids[0]) {
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200353 for (con_id = clknb->con_ids; *con_id; con_id++)
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200354 pm_clk_add(dev, *con_id);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200355 } else {
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200356 pm_clk_add(dev, NULL);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200357 }
358
359 break;
360 case BUS_NOTIFY_DEL_DEVICE:
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200361 if (dev->pm_domain != clknb->pm_domain)
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200362 break;
363
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200364 dev->pm_domain = NULL;
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200365 pm_clk_destroy(dev);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200366 break;
367 }
368
369 return 0;
370}
371
Rajendra Nayak75f50402015-04-23 14:03:09 +0530372int pm_clk_runtime_suspend(struct device *dev)
373{
374 int ret;
375
376 dev_dbg(dev, "%s\n", __func__);
377
378 ret = pm_generic_runtime_suspend(dev);
379 if (ret) {
380 dev_err(dev, "failed to suspend device\n");
381 return ret;
382 }
383
384 ret = pm_clk_suspend(dev);
385 if (ret) {
386 dev_err(dev, "failed to suspend clock\n");
387 pm_generic_runtime_resume(dev);
388 return ret;
389 }
390
391 return 0;
392}
393
394int pm_clk_runtime_resume(struct device *dev)
395{
396 int ret;
397
398 dev_dbg(dev, "%s\n", __func__);
399
400 ret = pm_clk_resume(dev);
401 if (ret) {
402 dev_err(dev, "failed to resume clock\n");
403 return ret;
404 }
405
406 return pm_generic_runtime_resume(dev);
407}
408
Geert Uytterhoevena6175612015-10-19 10:16:07 +0200409#else /* !CONFIG_PM_CLK */
Rafael J. Wysockib7b95922011-07-01 22:13:37 +0200410
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200411/**
412 * enable_clock - Enable a device clock.
413 * @dev: Device whose clock is to be enabled.
414 * @con_id: Connection ID of the clock.
415 */
416static void enable_clock(struct device *dev, const char *con_id)
417{
418 struct clk *clk;
419
420 clk = clk_get(dev, con_id);
421 if (!IS_ERR(clk)) {
Murali Karicheric122f272012-10-23 01:18:40 +0200422 clk_prepare_enable(clk);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200423 clk_put(clk);
424 dev_info(dev, "Runtime PM disabled, clock forced on.\n");
425 }
426}
427
428/**
429 * disable_clock - Disable a device clock.
430 * @dev: Device whose clock is to be disabled.
431 * @con_id: Connection ID of the clock.
432 */
433static void disable_clock(struct device *dev, const char *con_id)
434{
435 struct clk *clk;
436
437 clk = clk_get(dev, con_id);
438 if (!IS_ERR(clk)) {
Murali Karicheric122f272012-10-23 01:18:40 +0200439 clk_disable_unprepare(clk);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200440 clk_put(clk);
441 dev_info(dev, "Runtime PM disabled, clock forced off.\n");
442 }
443}
444
445/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200446 * pm_clk_notify - Notify routine for device addition and removal.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200447 * @nb: Notifier block object this function is a member of.
448 * @action: Operation being carried out by the caller.
449 * @data: Device the routine is being run for.
450 *
451 * For this function to work, @nb must be a member of an object of type
452 * struct pm_clk_notifier_block containing all of the requisite data.
453 * Specifically, the con_ids member of that object is used to enable or disable
454 * the device's clocks, depending on @action.
455 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200456static int pm_clk_notify(struct notifier_block *nb,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200457 unsigned long action, void *data)
458{
459 struct pm_clk_notifier_block *clknb;
460 struct device *dev = data;
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200461 char **con_id;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200462
463 dev_dbg(dev, "%s() %ld\n", __func__, action);
464
465 clknb = container_of(nb, struct pm_clk_notifier_block, nb);
466
467 switch (action) {
Rafael J. Wysocki4d1518f2011-06-21 23:24:33 +0200468 case BUS_NOTIFY_BIND_DRIVER:
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200469 if (clknb->con_ids[0]) {
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200470 for (con_id = clknb->con_ids; *con_id; con_id++)
471 enable_clock(dev, *con_id);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200472 } else {
473 enable_clock(dev, NULL);
474 }
475 break;
Andy Shevchenkod35818a2016-01-07 12:49:31 +0200476 case BUS_NOTIFY_DRIVER_NOT_BOUND:
Rafael J. Wysocki4d1518f2011-06-21 23:24:33 +0200477 case BUS_NOTIFY_UNBOUND_DRIVER:
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200478 if (clknb->con_ids[0]) {
Rafael J. Wysocki3b3eca32011-06-07 23:34:58 +0200479 for (con_id = clknb->con_ids; *con_id; con_id++)
480 disable_clock(dev, *con_id);
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200481 } else {
482 disable_clock(dev, NULL);
483 }
484 break;
485 }
486
487 return 0;
488}
489
Geert Uytterhoevena6175612015-10-19 10:16:07 +0200490#endif /* !CONFIG_PM_CLK */
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200491
492/**
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200493 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200494 * @bus: Bus type to add the notifier to.
495 * @clknb: Notifier to be added to the given bus type.
496 *
497 * The nb member of @clknb is not expected to be initialized and its
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200498 * notifier_call member will be replaced with pm_clk_notify(). However,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200499 * the remaining members of @clknb should be populated prior to calling this
500 * routine.
501 */
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200502void pm_clk_add_notifier(struct bus_type *bus,
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200503 struct pm_clk_notifier_block *clknb)
504{
505 if (!bus || !clknb)
506 return;
507
Rafael J. Wysocki3d5c3032011-07-01 22:13:44 +0200508 clknb->nb.notifier_call = pm_clk_notify;
Rafael J. Wysocki85eb8c82011-04-30 00:25:44 +0200509 bus_register_notifier(bus, &clknb->nb);
510}