Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 1 | /* arch/arm/mach-msm/clock.c |
| 2 | * |
| 3 | * Copyright (C) 2007 Google, Inc. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 4 | * Copyright (c) 2007-2011, Code Aurora Forum. All rights reserved. |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 19 | #include <linux/spinlock.h> |
Stephen Boyd | bd32344 | 2011-02-23 09:37:42 -0800 | [diff] [blame] | 20 | #include <linux/string.h> |
| 21 | #include <linux/module.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | #include <linux/clk.h> |
Stephen Boyd | bd32344 | 2011-02-23 09:37:42 -0800 | [diff] [blame] | 23 | #include <linux/clkdev.h> |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 24 | |
| 25 | #include "clock.h" |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 26 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 27 | /* |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 28 | * Standard clock functions defined in include/linux/clk.h |
| 29 | */ |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 30 | int clk_enable(struct clk *clk) |
| 31 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 32 | int ret = 0; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 33 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 34 | struct clk *parent; |
| 35 | |
| 36 | if (!clk) |
| 37 | return 0; |
| 38 | |
| 39 | spin_lock_irqsave(&clk->lock, flags); |
| 40 | if (clk->count == 0) { |
| 41 | parent = clk_get_parent(clk); |
| 42 | ret = clk_enable(parent); |
| 43 | if (ret) |
| 44 | goto out; |
Stephen Boyd | 7fa2674 | 2011-08-11 23:22:29 -0700 | [diff] [blame] | 45 | ret = clk_enable(clk->depends); |
| 46 | if (ret) { |
| 47 | clk_disable(parent); |
| 48 | goto out; |
| 49 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 50 | |
| 51 | if (clk->ops->enable) |
| 52 | ret = clk->ops->enable(clk); |
| 53 | if (ret) { |
Stephen Boyd | 7fa2674 | 2011-08-11 23:22:29 -0700 | [diff] [blame] | 54 | clk_disable(clk->depends); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 55 | clk_disable(parent); |
| 56 | goto out; |
| 57 | } |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 58 | } else if (clk->flags & CLKFLAG_HANDOFF_RATE) { |
| 59 | /* |
| 60 | * The clock was already enabled by handoff code so there is no |
| 61 | * need to enable it again here. Clearing the handoff flag will |
| 62 | * prevent the lateinit handoff code from disabling the clock if |
| 63 | * a client driver still has it enabled. |
| 64 | */ |
| 65 | clk->flags &= ~CLKFLAG_HANDOFF_RATE; |
| 66 | goto out; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 67 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 68 | clk->count++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 69 | out: |
| 70 | spin_unlock_irqrestore(&clk->lock, flags); |
| 71 | |
| 72 | return ret; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 73 | } |
| 74 | EXPORT_SYMBOL(clk_enable); |
| 75 | |
| 76 | void clk_disable(struct clk *clk) |
| 77 | { |
| 78 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 79 | struct clk *parent; |
| 80 | |
| 81 | if (!clk) |
| 82 | return; |
| 83 | |
| 84 | spin_lock_irqsave(&clk->lock, flags); |
Stephen Boyd | d906b52 | 2011-07-26 10:51:41 -0700 | [diff] [blame] | 85 | if (WARN(clk->count == 0, "%s is unbalanced", clk->dbg_name)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 86 | goto out; |
| 87 | if (clk->count == 1) { |
| 88 | if (clk->ops->disable) |
| 89 | clk->ops->disable(clk); |
Stephen Boyd | 7fa2674 | 2011-08-11 23:22:29 -0700 | [diff] [blame] | 90 | clk_disable(clk->depends); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 91 | parent = clk_get_parent(clk); |
| 92 | clk_disable(parent); |
| 93 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 94 | clk->count--; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 95 | out: |
| 96 | spin_unlock_irqrestore(&clk->lock, flags); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 97 | } |
| 98 | EXPORT_SYMBOL(clk_disable); |
| 99 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 100 | int clk_reset(struct clk *clk, enum clk_reset_action action) |
| 101 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 102 | if (!clk->ops->reset) |
| 103 | return -ENOSYS; |
| 104 | |
| 105 | return clk->ops->reset(clk, action); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 106 | } |
| 107 | EXPORT_SYMBOL(clk_reset); |
| 108 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 109 | unsigned long clk_get_rate(struct clk *clk) |
| 110 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 111 | if (!clk->ops->get_rate) |
| 112 | return 0; |
| 113 | |
| 114 | return clk->ops->get_rate(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 115 | } |
| 116 | EXPORT_SYMBOL(clk_get_rate); |
| 117 | |
| 118 | int clk_set_rate(struct clk *clk, unsigned long rate) |
| 119 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 120 | if (!clk->ops->set_rate) |
| 121 | return -ENOSYS; |
Daniel Walker | 3a790bb | 2010-12-13 14:35:10 -0800 | [diff] [blame] | 122 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 123 | return clk->ops->set_rate(clk, rate); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 124 | } |
| 125 | EXPORT_SYMBOL(clk_set_rate); |
| 126 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 127 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 128 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 129 | if (!clk->ops->round_rate) |
| 130 | return -ENOSYS; |
| 131 | |
| 132 | return clk->ops->round_rate(clk, rate); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 133 | } |
| 134 | EXPORT_SYMBOL(clk_round_rate); |
| 135 | |
| 136 | int clk_set_min_rate(struct clk *clk, unsigned long rate) |
| 137 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 138 | if (!clk->ops->set_min_rate) |
| 139 | return -ENOSYS; |
| 140 | |
| 141 | return clk->ops->set_min_rate(clk, rate); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 142 | } |
| 143 | EXPORT_SYMBOL(clk_set_min_rate); |
| 144 | |
| 145 | int clk_set_max_rate(struct clk *clk, unsigned long rate) |
| 146 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 147 | if (!clk->ops->set_max_rate) |
| 148 | return -ENOSYS; |
| 149 | |
| 150 | return clk->ops->set_max_rate(clk, rate); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 151 | } |
| 152 | EXPORT_SYMBOL(clk_set_max_rate); |
| 153 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 154 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 155 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 156 | if (!clk->ops->set_parent) |
| 157 | return 0; |
| 158 | |
| 159 | return clk->ops->set_parent(clk, parent); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 160 | } |
| 161 | EXPORT_SYMBOL(clk_set_parent); |
| 162 | |
| 163 | struct clk *clk_get_parent(struct clk *clk) |
| 164 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 165 | if (!clk->ops->get_parent) |
| 166 | return NULL; |
| 167 | |
| 168 | return clk->ops->get_parent(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 169 | } |
| 170 | EXPORT_SYMBOL(clk_get_parent); |
| 171 | |
| 172 | int clk_set_flags(struct clk *clk, unsigned long flags) |
| 173 | { |
| 174 | if (clk == NULL || IS_ERR(clk)) |
| 175 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 176 | if (!clk->ops->set_flags) |
| 177 | return -ENOSYS; |
| 178 | |
| 179 | return clk->ops->set_flags(clk, flags); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 180 | } |
| 181 | EXPORT_SYMBOL(clk_set_flags); |
| 182 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 183 | static struct clock_init_data __initdata *clk_init_data; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 184 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 185 | void __init msm_clock_init(struct clock_init_data *data) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 186 | { |
| 187 | unsigned n; |
Stephen Boyd | 94625ef | 2011-07-12 17:06:01 -0700 | [diff] [blame^] | 188 | struct clk_lookup *clock_tbl; |
| 189 | size_t num_clocks; |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 190 | |
| 191 | clk_init_data = data; |
| 192 | if (clk_init_data->init) |
| 193 | clk_init_data->init(); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 194 | |
Stephen Boyd | 94625ef | 2011-07-12 17:06:01 -0700 | [diff] [blame^] | 195 | clock_tbl = data->table; |
| 196 | num_clocks = data->size; |
| 197 | |
Stephen Boyd | bd32344 | 2011-02-23 09:37:42 -0800 | [diff] [blame] | 198 | for (n = 0; n < num_clocks; n++) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 199 | struct clk *clk = clock_tbl[n].clk; |
| 200 | struct clk *parent = clk_get_parent(clk); |
| 201 | clk_set_parent(clk, parent); |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 202 | if (clk->ops->handoff) |
| 203 | clk->ops->handoff(clk); |
Stephen Boyd | bd32344 | 2011-02-23 09:37:42 -0800 | [diff] [blame] | 204 | } |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 205 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 206 | clkdev_add_table(clock_tbl, num_clocks); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 209 | /* |
| 210 | * The bootloader and/or AMSS may have left various clocks enabled. |
| 211 | * Disable any clocks that have not been explicitly enabled by a |
| 212 | * clk_enable() call and don't have the CLKFLAG_SKIP_AUTO_OFF flag. |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 213 | */ |
| 214 | static int __init clock_late_init(void) |
| 215 | { |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 216 | unsigned n, count = 0; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 217 | unsigned long flags; |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 218 | int ret = 0; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 219 | |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 220 | clock_debug_init(clk_init_data); |
| 221 | for (n = 0; n < clk_init_data->size; n++) { |
| 222 | struct clk *clk = clk_init_data->table[n].clk; |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 223 | bool handoff = false; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 224 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame] | 225 | clock_debug_add(clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 226 | if (!(clk->flags & CLKFLAG_SKIP_AUTO_OFF)) { |
| 227 | spin_lock_irqsave(&clk->lock, flags); |
| 228 | if (!clk->count && clk->ops->auto_off) { |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 229 | count++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 230 | clk->ops->auto_off(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 231 | } |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 232 | if (clk->flags & CLKFLAG_HANDOFF_RATE) { |
| 233 | clk->flags &= ~CLKFLAG_HANDOFF_RATE; |
| 234 | handoff = true; |
| 235 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 236 | spin_unlock_irqrestore(&clk->lock, flags); |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 237 | /* |
| 238 | * Calling clk_disable() outside the lock is safe since |
| 239 | * it doesn't need to be atomic with the flag change. |
| 240 | */ |
| 241 | if (handoff) |
| 242 | clk_disable(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 245 | pr_info("clock_late_init() disabled %d unused clocks\n", count); |
Stephen Boyd | bb600ae | 2011-08-02 20:11:40 -0700 | [diff] [blame] | 246 | if (clk_init_data->late_init) |
| 247 | ret = clk_init_data->late_init(); |
| 248 | return ret; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 249 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 250 | late_initcall(clock_late_init); |