Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 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 | |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 9 | #include <linux/kernel.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 10 | #include <linux/device.h> |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 11 | #include <linux/io.h> |
| 12 | #include <linux/pm.h> |
Rafael J. Wysocki | b5e8d26 | 2011-08-25 15:34:19 +0200 | [diff] [blame] | 13 | #include <linux/pm_clock.h> |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 14 | #include <linux/clk.h> |
Geert Uytterhoeven | 245bd6f | 2014-11-06 15:51:00 +0200 | [diff] [blame] | 15 | #include <linux/clkdev.h> |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/err.h> |
Rajendra Nayak | 75f5040 | 2015-04-23 14:03:09 +0530 | [diff] [blame] | 18 | #include <linux/pm_runtime.h> |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 19 | |
Rafael J. Wysocki | b7b9592 | 2011-07-01 22:13:37 +0200 | [diff] [blame] | 20 | #ifdef CONFIG_PM |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 21 | |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 22 | enum pce_status { |
| 23 | PCE_STATUS_NONE = 0, |
| 24 | PCE_STATUS_ACQUIRED, |
| 25 | PCE_STATUS_ENABLED, |
| 26 | PCE_STATUS_ERROR, |
| 27 | }; |
| 28 | |
| 29 | struct 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. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 36 | /** |
Ben Dooks | 5cda3fb | 2014-01-14 12:23:42 +0000 | [diff] [blame] | 37 | * pm_clk_enable - Enable a clock, reporting any errors |
| 38 | * @dev: The device for the given clock |
Strashko, Grygorii | 471f770 | 2014-11-06 15:51:01 +0200 | [diff] [blame] | 39 | * @ce: PM clock entry corresponding to the clock. |
Ben Dooks | 5cda3fb | 2014-01-14 12:23:42 +0000 | [diff] [blame] | 40 | */ |
Colin Ian King | f4745a9 | 2015-06-29 22:13:38 +0100 | [diff] [blame] | 41 | static inline void __pm_clk_enable(struct device *dev, struct pm_clock_entry *ce) |
Ben Dooks | 5cda3fb | 2014-01-14 12:23:42 +0000 | [diff] [blame] | 42 | { |
Strashko, Grygorii | 471f770 | 2014-11-06 15:51:01 +0200 | [diff] [blame] | 43 | 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 Dooks | 5cda3fb | 2014-01-14 12:23:42 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 56 | * 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 | */ |
| 60 | static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce) |
| 61 | { |
Geert Uytterhoeven | 245bd6f | 2014-11-06 15:51:00 +0200 | [diff] [blame] | 62 | if (!ce->clk) |
| 63 | ce->clk = clk_get(dev, ce->con_id); |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 64 | if (IS_ERR(ce->clk)) { |
| 65 | ce->status = PCE_STATUS_ERROR; |
| 66 | } else { |
Ben Dooks | 8a6720e | 2014-01-14 12:23:40 +0000 | [diff] [blame] | 67 | clk_prepare(ce->clk); |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 68 | ce->status = PCE_STATUS_ACQUIRED; |
Geert Uytterhoeven | f17f4ad | 2015-05-29 18:35:31 +0200 | [diff] [blame] | 69 | dev_dbg(dev, "Clock %pC con_id %s managed by runtime PM.\n", |
| 70 | ce->clk, ce->con_id); |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Geert Uytterhoeven | 245bd6f | 2014-11-06 15:51:00 +0200 | [diff] [blame] | 74 | static int __pm_clk_add(struct device *dev, const char *con_id, |
| 75 | struct clk *clk) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 76 | { |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 77 | struct pm_subsys_data *psd = dev_to_psd(dev); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 78 | struct pm_clock_entry *ce; |
| 79 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 80 | if (!psd) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 81 | return -EINVAL; |
| 82 | |
| 83 | ce = kzalloc(sizeof(*ce), GFP_KERNEL); |
Quentin Lambert | 59d84ca | 2015-02-09 10:45:32 +0100 | [diff] [blame] | 84 | if (!ce) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 85 | return -ENOMEM; |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 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 | } |
Geert Uytterhoeven | 245bd6f | 2014-11-06 15:51:00 +0200 | [diff] [blame] | 95 | } else { |
Geert Uytterhoeven | 3fc3a0b | 2015-05-08 10:47:43 +0200 | [diff] [blame] | 96 | if (IS_ERR(clk) || !__clk_get(clk)) { |
Geert Uytterhoeven | 245bd6f | 2014-11-06 15:51:00 +0200 | [diff] [blame] | 97 | kfree(ce); |
| 98 | return -ENOENT; |
| 99 | } |
| 100 | ce->clk = clk; |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 103 | pm_clk_acquire(dev, ce); |
| 104 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 105 | spin_lock_irq(&psd->lock); |
| 106 | list_add_tail(&ce->node, &psd->clock_list); |
| 107 | spin_unlock_irq(&psd->lock); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /** |
Geert Uytterhoeven | 245bd6f | 2014-11-06 15:51:00 +0200 | [diff] [blame] | 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 | |
| 137 | /** |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 138 | * __pm_clk_remove - Destroy PM clock entry. |
| 139 | * @ce: PM clock entry to destroy. |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 140 | */ |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 141 | static void __pm_clk_remove(struct pm_clock_entry *ce) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 142 | { |
| 143 | if (!ce) |
| 144 | return; |
| 145 | |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 146 | if (ce->status < PCE_STATUS_ERROR) { |
| 147 | if (ce->status == PCE_STATUS_ENABLED) |
Ben Dooks | 8a6720e | 2014-01-14 12:23:40 +0000 | [diff] [blame] | 148 | clk_disable(ce->clk); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 149 | |
Ben Dooks | 8a6720e | 2014-01-14 12:23:40 +0000 | [diff] [blame] | 150 | if (ce->status >= PCE_STATUS_ACQUIRED) { |
| 151 | clk_unprepare(ce->clk); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 152 | clk_put(ce->clk); |
Ben Dooks | 8a6720e | 2014-01-14 12:23:40 +0000 | [diff] [blame] | 153 | } |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Jonghwan Choi | 0ab1e79 | 2011-10-22 00:22:54 +0200 | [diff] [blame] | 156 | kfree(ce->con_id); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 157 | kfree(ce); |
| 158 | } |
| 159 | |
| 160 | /** |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 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. |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 163 | * @con_id: Connection ID of the clock. |
| 164 | * |
| 165 | * Remove the clock represented by @con_id from the list of clocks used for |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 166 | * the power management of @dev. |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 167 | */ |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 168 | void pm_clk_remove(struct device *dev, const char *con_id) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 169 | { |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 170 | struct pm_subsys_data *psd = dev_to_psd(dev); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 171 | struct pm_clock_entry *ce; |
| 172 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 173 | if (!psd) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 174 | return; |
| 175 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 176 | spin_lock_irq(&psd->lock); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 177 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 178 | list_for_each_entry(ce, &psd->clock_list, node) { |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 179 | if (!con_id && !ce->con_id) |
| 180 | goto remove; |
| 181 | else if (!con_id || !ce->con_id) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 182 | continue; |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 183 | else if (!strcmp(con_id, ce->con_id)) |
| 184 | goto remove; |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 187 | spin_unlock_irq(&psd->lock); |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 188 | return; |
| 189 | |
| 190 | remove: |
| 191 | list_del(&ce->node); |
Rafael J. Wysocki | 0d41da2 | 2011-09-26 20:12:45 +0200 | [diff] [blame] | 192 | spin_unlock_irq(&psd->lock); |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 193 | |
| 194 | __pm_clk_remove(ce); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 198 | * pm_clk_init - Initialize a device's list of power management clocks. |
| 199 | * @dev: Device to initialize the list of PM clocks for. |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 200 | * |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 201 | * Initialize the lock and clock_list members of the device's pm_subsys_data |
| 202 | * object. |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 203 | */ |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 204 | void pm_clk_init(struct device *dev) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 205 | { |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 206 | struct pm_subsys_data *psd = dev_to_psd(dev); |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 207 | if (psd) |
| 208 | INIT_LIST_HEAD(&psd->clock_list); |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 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 | { |
Rafael J. Wysocki | 7725495 | 2012-08-07 13:50:14 +0200 | [diff] [blame] | 220 | return dev_pm_get_subsys_data(dev); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 224 | * pm_clk_destroy - Destroy a device's list of power management clocks. |
| 225 | * @dev: Device to destroy the list of PM clocks for. |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 226 | * |
| 227 | * Clear the @dev's power.subsys_data field, remove the list of clock entries |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 228 | * from the struct pm_subsys_data object pointed to by it before and free |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 229 | * that object. |
| 230 | */ |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 231 | void pm_clk_destroy(struct device *dev) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 232 | { |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 233 | struct pm_subsys_data *psd = dev_to_psd(dev); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 234 | struct pm_clock_entry *ce, *c; |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 235 | struct list_head list; |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 236 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 237 | if (!psd) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 238 | return; |
| 239 | |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 240 | INIT_LIST_HEAD(&list); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 241 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 242 | spin_lock_irq(&psd->lock); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 243 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 244 | list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node) |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 245 | list_move(&ce->node, &list); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 246 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 247 | spin_unlock_irq(&psd->lock); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 248 | |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 249 | dev_pm_put_subsys_data(dev); |
Rafael J. Wysocki | e8b364b | 2011-09-26 19:40:23 +0200 | [diff] [blame] | 250 | |
| 251 | list_for_each_entry_safe_reverse(ce, c, &list, node) { |
| 252 | list_del(&ce->node); |
| 253 | __pm_clk_remove(ce); |
| 254 | } |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 258 | * pm_clk_suspend - Disable clocks in a device's PM clock list. |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 259 | * @dev: Device to disable the clocks for. |
| 260 | */ |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 261 | int pm_clk_suspend(struct device *dev) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 262 | { |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 263 | struct pm_subsys_data *psd = dev_to_psd(dev); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 264 | struct pm_clock_entry *ce; |
Rafael J. Wysocki | b7ab83e | 2011-08-24 21:40:56 +0200 | [diff] [blame] | 265 | unsigned long flags; |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 266 | |
| 267 | dev_dbg(dev, "%s()\n", __func__); |
| 268 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 269 | if (!psd) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 270 | return 0; |
| 271 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 272 | spin_lock_irqsave(&psd->lock, flags); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 273 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 274 | list_for_each_entry_reverse(ce, &psd->clock_list, node) { |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 275 | if (ce->status < PCE_STATUS_ERROR) { |
Magnus Damm | 2405095 | 2011-11-10 00:44:10 +0100 | [diff] [blame] | 276 | if (ce->status == PCE_STATUS_ENABLED) |
| 277 | clk_disable(ce->clk); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 278 | ce->status = PCE_STATUS_ACQUIRED; |
| 279 | } |
| 280 | } |
| 281 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 282 | spin_unlock_irqrestore(&psd->lock, flags); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /** |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 288 | * pm_clk_resume - Enable clocks in a device's PM clock list. |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 289 | * @dev: Device to enable the clocks for. |
| 290 | */ |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 291 | int pm_clk_resume(struct device *dev) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 292 | { |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 293 | struct pm_subsys_data *psd = dev_to_psd(dev); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 294 | struct pm_clock_entry *ce; |
Rafael J. Wysocki | b7ab83e | 2011-08-24 21:40:56 +0200 | [diff] [blame] | 295 | unsigned long flags; |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 296 | |
| 297 | dev_dbg(dev, "%s()\n", __func__); |
| 298 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 299 | if (!psd) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 300 | return 0; |
| 301 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 302 | spin_lock_irqsave(&psd->lock, flags); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 303 | |
Strashko, Grygorii | 471f770 | 2014-11-06 15:51:01 +0200 | [diff] [blame] | 304 | list_for_each_entry(ce, &psd->clock_list, node) |
| 305 | __pm_clk_enable(dev, ce); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 306 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 307 | spin_unlock_irqrestore(&psd->lock, flags); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /** |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 313 | * pm_clk_notify - Notify routine for device addition and removal. |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 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. |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 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 |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 322 | * of PM clocks, depending on @action. |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 323 | * |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 324 | * If the device's pm_domain field is already populated with a value different |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 325 | * from the one stored in the struct pm_clk_notifier_block object, the function |
| 326 | * does nothing. |
| 327 | */ |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 328 | static int pm_clk_notify(struct notifier_block *nb, |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 329 | unsigned long action, void *data) |
| 330 | { |
| 331 | struct pm_clk_notifier_block *clknb; |
| 332 | struct device *dev = data; |
Rafael J. Wysocki | 3b3eca3 | 2011-06-07 23:34:58 +0200 | [diff] [blame] | 333 | char **con_id; |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 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: |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 342 | if (dev->pm_domain) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 343 | break; |
| 344 | |
Rafael J. Wysocki | 5c095a0 | 2011-08-25 15:33:50 +0200 | [diff] [blame] | 345 | error = pm_clk_create(dev); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 346 | if (error) |
| 347 | break; |
| 348 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 349 | dev->pm_domain = clknb->pm_domain; |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 350 | if (clknb->con_ids[0]) { |
Rafael J. Wysocki | 3b3eca3 | 2011-06-07 23:34:58 +0200 | [diff] [blame] | 351 | for (con_id = clknb->con_ids; *con_id; con_id++) |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 352 | pm_clk_add(dev, *con_id); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 353 | } else { |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 354 | pm_clk_add(dev, NULL); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | break; |
| 358 | case BUS_NOTIFY_DEL_DEVICE: |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 359 | if (dev->pm_domain != clknb->pm_domain) |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 360 | break; |
| 361 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 362 | dev->pm_domain = NULL; |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 363 | pm_clk_destroy(dev); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
Rajendra Nayak | 75f5040 | 2015-04-23 14:03:09 +0530 | [diff] [blame] | 370 | int pm_clk_runtime_suspend(struct device *dev) |
| 371 | { |
| 372 | int ret; |
| 373 | |
| 374 | dev_dbg(dev, "%s\n", __func__); |
| 375 | |
| 376 | ret = pm_generic_runtime_suspend(dev); |
| 377 | if (ret) { |
| 378 | dev_err(dev, "failed to suspend device\n"); |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | ret = pm_clk_suspend(dev); |
| 383 | if (ret) { |
| 384 | dev_err(dev, "failed to suspend clock\n"); |
| 385 | pm_generic_runtime_resume(dev); |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | int pm_clk_runtime_resume(struct device *dev) |
| 393 | { |
| 394 | int ret; |
| 395 | |
| 396 | dev_dbg(dev, "%s\n", __func__); |
| 397 | |
| 398 | ret = pm_clk_resume(dev); |
| 399 | if (ret) { |
| 400 | dev_err(dev, "failed to resume clock\n"); |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | return pm_generic_runtime_resume(dev); |
| 405 | } |
| 406 | |
Rafael J. Wysocki | d30d819 | 2014-11-27 22:38:05 +0100 | [diff] [blame] | 407 | #else /* !CONFIG_PM */ |
Rafael J. Wysocki | b7b9592 | 2011-07-01 22:13:37 +0200 | [diff] [blame] | 408 | |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 409 | /** |
| 410 | * enable_clock - Enable a device clock. |
| 411 | * @dev: Device whose clock is to be enabled. |
| 412 | * @con_id: Connection ID of the clock. |
| 413 | */ |
| 414 | static void enable_clock(struct device *dev, const char *con_id) |
| 415 | { |
| 416 | struct clk *clk; |
| 417 | |
| 418 | clk = clk_get(dev, con_id); |
| 419 | if (!IS_ERR(clk)) { |
Murali Karicheri | c122f27 | 2012-10-23 01:18:40 +0200 | [diff] [blame] | 420 | clk_prepare_enable(clk); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 421 | clk_put(clk); |
| 422 | dev_info(dev, "Runtime PM disabled, clock forced on.\n"); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * disable_clock - Disable a device clock. |
| 428 | * @dev: Device whose clock is to be disabled. |
| 429 | * @con_id: Connection ID of the clock. |
| 430 | */ |
| 431 | static void disable_clock(struct device *dev, const char *con_id) |
| 432 | { |
| 433 | struct clk *clk; |
| 434 | |
| 435 | clk = clk_get(dev, con_id); |
| 436 | if (!IS_ERR(clk)) { |
Murali Karicheri | c122f27 | 2012-10-23 01:18:40 +0200 | [diff] [blame] | 437 | clk_disable_unprepare(clk); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 438 | clk_put(clk); |
| 439 | dev_info(dev, "Runtime PM disabled, clock forced off.\n"); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /** |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 444 | * pm_clk_notify - Notify routine for device addition and removal. |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 445 | * @nb: Notifier block object this function is a member of. |
| 446 | * @action: Operation being carried out by the caller. |
| 447 | * @data: Device the routine is being run for. |
| 448 | * |
| 449 | * For this function to work, @nb must be a member of an object of type |
| 450 | * struct pm_clk_notifier_block containing all of the requisite data. |
| 451 | * Specifically, the con_ids member of that object is used to enable or disable |
| 452 | * the device's clocks, depending on @action. |
| 453 | */ |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 454 | static int pm_clk_notify(struct notifier_block *nb, |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 455 | unsigned long action, void *data) |
| 456 | { |
| 457 | struct pm_clk_notifier_block *clknb; |
| 458 | struct device *dev = data; |
Rafael J. Wysocki | 3b3eca3 | 2011-06-07 23:34:58 +0200 | [diff] [blame] | 459 | char **con_id; |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 460 | |
| 461 | dev_dbg(dev, "%s() %ld\n", __func__, action); |
| 462 | |
| 463 | clknb = container_of(nb, struct pm_clk_notifier_block, nb); |
| 464 | |
| 465 | switch (action) { |
Rafael J. Wysocki | 4d1518f | 2011-06-21 23:24:33 +0200 | [diff] [blame] | 466 | case BUS_NOTIFY_BIND_DRIVER: |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 467 | if (clknb->con_ids[0]) { |
Rafael J. Wysocki | 3b3eca3 | 2011-06-07 23:34:58 +0200 | [diff] [blame] | 468 | for (con_id = clknb->con_ids; *con_id; con_id++) |
| 469 | enable_clock(dev, *con_id); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 470 | } else { |
| 471 | enable_clock(dev, NULL); |
| 472 | } |
| 473 | break; |
Rafael J. Wysocki | 4d1518f | 2011-06-21 23:24:33 +0200 | [diff] [blame] | 474 | case BUS_NOTIFY_UNBOUND_DRIVER: |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 475 | if (clknb->con_ids[0]) { |
Rafael J. Wysocki | 3b3eca3 | 2011-06-07 23:34:58 +0200 | [diff] [blame] | 476 | for (con_id = clknb->con_ids; *con_id; con_id++) |
| 477 | disable_clock(dev, *con_id); |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 478 | } else { |
| 479 | disable_clock(dev, NULL); |
| 480 | } |
| 481 | break; |
| 482 | } |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
Rafael J. Wysocki | d30d819 | 2014-11-27 22:38:05 +0100 | [diff] [blame] | 487 | #endif /* !CONFIG_PM */ |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 488 | |
| 489 | /** |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 490 | * pm_clk_add_notifier - Add bus type notifier for power management clocks. |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 491 | * @bus: Bus type to add the notifier to. |
| 492 | * @clknb: Notifier to be added to the given bus type. |
| 493 | * |
| 494 | * The nb member of @clknb is not expected to be initialized and its |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 495 | * notifier_call member will be replaced with pm_clk_notify(). However, |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 496 | * the remaining members of @clknb should be populated prior to calling this |
| 497 | * routine. |
| 498 | */ |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 499 | void pm_clk_add_notifier(struct bus_type *bus, |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 500 | struct pm_clk_notifier_block *clknb) |
| 501 | { |
| 502 | if (!bus || !clknb) |
| 503 | return; |
| 504 | |
Rafael J. Wysocki | 3d5c303 | 2011-07-01 22:13:44 +0200 | [diff] [blame] | 505 | clknb->nb.notifier_call = pm_clk_notify; |
Rafael J. Wysocki | 85eb8c8 | 2011-04-30 00:25:44 +0200 | [diff] [blame] | 506 | bus_register_notifier(bus, &clknb->nb); |
| 507 | } |