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