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. |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 4 | * Copyright (c) 2007-2010, 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/list.h> |
| 19 | #include <linux/err.h> |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 20 | #include <linux/spinlock.h> |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 21 | #include <linux/pm_qos_params.h> |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 22 | |
| 23 | #include "clock.h" |
| 24 | #include "proc_comm.h" |
Gregory Bean | 37a298f | 2010-04-30 22:22:07 -0700 | [diff] [blame] | 25 | #include "clock-7x30.h" |
Stephen Boyd | ce1c80f | 2011-01-26 16:20:53 -0800 | [diff] [blame] | 26 | #include "clock-pcom.h" |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 27 | |
| 28 | static DEFINE_MUTEX(clocks_mutex); |
| 29 | static DEFINE_SPINLOCK(clocks_lock); |
| 30 | static LIST_HEAD(clocks); |
| 31 | |
| 32 | /* |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 33 | * Standard clock functions defined in include/linux/clk.h |
| 34 | */ |
| 35 | struct clk *clk_get(struct device *dev, const char *id) |
| 36 | { |
| 37 | struct clk *clk; |
| 38 | |
| 39 | mutex_lock(&clocks_mutex); |
| 40 | |
| 41 | list_for_each_entry(clk, &clocks, list) |
| 42 | if (!strcmp(id, clk->name) && clk->dev == dev) |
| 43 | goto found_it; |
| 44 | |
| 45 | list_for_each_entry(clk, &clocks, list) |
| 46 | if (!strcmp(id, clk->name) && clk->dev == NULL) |
| 47 | goto found_it; |
| 48 | |
| 49 | clk = ERR_PTR(-ENOENT); |
| 50 | found_it: |
| 51 | mutex_unlock(&clocks_mutex); |
| 52 | return clk; |
| 53 | } |
| 54 | EXPORT_SYMBOL(clk_get); |
| 55 | |
| 56 | void clk_put(struct clk *clk) |
| 57 | { |
| 58 | } |
| 59 | EXPORT_SYMBOL(clk_put); |
| 60 | |
| 61 | int clk_enable(struct clk *clk) |
| 62 | { |
| 63 | unsigned long flags; |
| 64 | spin_lock_irqsave(&clocks_lock, flags); |
| 65 | clk->count++; |
Stephen Boyd | 437f629 | 2011-01-26 16:20:52 -0800 | [diff] [blame] | 66 | if (clk->count == 1) |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 67 | clk->ops->enable(clk->id); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 68 | spin_unlock_irqrestore(&clocks_lock, flags); |
| 69 | return 0; |
| 70 | } |
| 71 | EXPORT_SYMBOL(clk_enable); |
| 72 | |
| 73 | void clk_disable(struct clk *clk) |
| 74 | { |
| 75 | unsigned long flags; |
| 76 | spin_lock_irqsave(&clocks_lock, flags); |
| 77 | BUG_ON(clk->count == 0); |
| 78 | clk->count--; |
Stephen Boyd | 437f629 | 2011-01-26 16:20:52 -0800 | [diff] [blame] | 79 | if (clk->count == 0) |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 80 | clk->ops->disable(clk->id); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 81 | spin_unlock_irqrestore(&clocks_lock, flags); |
| 82 | } |
| 83 | EXPORT_SYMBOL(clk_disable); |
| 84 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 85 | int clk_reset(struct clk *clk, enum clk_reset_action action) |
| 86 | { |
| 87 | if (!clk->ops->reset) |
| 88 | clk->ops->reset = &pc_clk_reset; |
| 89 | return clk->ops->reset(clk->remote_id, action); |
| 90 | } |
| 91 | EXPORT_SYMBOL(clk_reset); |
| 92 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 93 | unsigned long clk_get_rate(struct clk *clk) |
| 94 | { |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 95 | return clk->ops->get_rate(clk->id); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 96 | } |
| 97 | EXPORT_SYMBOL(clk_get_rate); |
| 98 | |
| 99 | int clk_set_rate(struct clk *clk, unsigned long rate) |
| 100 | { |
Daniel Walker | 3a790bb | 2010-12-13 14:35:10 -0800 | [diff] [blame] | 101 | int ret; |
| 102 | if (clk->flags & CLKFLAG_MAX) { |
| 103 | ret = clk->ops->set_max_rate(clk->id, rate); |
| 104 | if (ret) |
| 105 | return ret; |
| 106 | } |
| 107 | if (clk->flags & CLKFLAG_MIN) { |
| 108 | ret = clk->ops->set_min_rate(clk->id, rate); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | if (clk->flags & CLKFLAG_MAX || clk->flags & CLKFLAG_MIN) |
| 114 | return ret; |
| 115 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 116 | return clk->ops->set_rate(clk->id, rate); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 117 | } |
| 118 | EXPORT_SYMBOL(clk_set_rate); |
| 119 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 120 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 121 | { |
| 122 | return clk->ops->round_rate(clk->id, rate); |
| 123 | } |
| 124 | EXPORT_SYMBOL(clk_round_rate); |
| 125 | |
| 126 | int clk_set_min_rate(struct clk *clk, unsigned long rate) |
| 127 | { |
| 128 | return clk->ops->set_min_rate(clk->id, rate); |
| 129 | } |
| 130 | EXPORT_SYMBOL(clk_set_min_rate); |
| 131 | |
| 132 | int clk_set_max_rate(struct clk *clk, unsigned long rate) |
| 133 | { |
| 134 | return clk->ops->set_max_rate(clk->id, rate); |
| 135 | } |
| 136 | EXPORT_SYMBOL(clk_set_max_rate); |
| 137 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 138 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 139 | { |
| 140 | return -ENOSYS; |
| 141 | } |
| 142 | EXPORT_SYMBOL(clk_set_parent); |
| 143 | |
| 144 | struct clk *clk_get_parent(struct clk *clk) |
| 145 | { |
| 146 | return ERR_PTR(-ENOSYS); |
| 147 | } |
| 148 | EXPORT_SYMBOL(clk_get_parent); |
| 149 | |
| 150 | int clk_set_flags(struct clk *clk, unsigned long flags) |
| 151 | { |
| 152 | if (clk == NULL || IS_ERR(clk)) |
| 153 | return -EINVAL; |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 154 | return clk->ops->set_flags(clk->id, flags); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 155 | } |
| 156 | EXPORT_SYMBOL(clk_set_flags); |
| 157 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 158 | /* EBI1 is the only shared clock that several clients want to vote on as of |
| 159 | * this commit. If this changes in the future, then it might be better to |
| 160 | * make clk_min_rate handle the voting or make ebi1_clk_set_min_rate more |
| 161 | * generic to support different clocks. |
| 162 | */ |
| 163 | static struct clk *ebi1_clk; |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 164 | |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 165 | static void __init set_clock_ops(struct clk *clk) |
| 166 | { |
| 167 | if (!clk->ops) { |
| 168 | clk->ops = &clk_ops_pcom; |
| 169 | clk->id = clk->remote_id; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void __init msm_clock_init(struct clk *clock_tbl, unsigned num_clocks) |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 174 | { |
| 175 | unsigned n; |
| 176 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 177 | mutex_lock(&clocks_mutex); |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame^] | 178 | for (n = 0; n < num_clocks; n++) { |
| 179 | set_clock_ops(&clock_tbl[n]); |
| 180 | list_add_tail(&clock_tbl[n].list, &clocks); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 181 | } |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 182 | mutex_unlock(&clocks_mutex); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 183 | |
| 184 | ebi1_clk = clk_get(NULL, "ebi1_clk"); |
| 185 | BUG_ON(ebi1_clk == NULL); |
| 186 | |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* The bootloader and/or AMSS may have left various clocks enabled. |
| 190 | * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have |
| 191 | * not been explicitly enabled by a clk_enable() call. |
| 192 | */ |
| 193 | static int __init clock_late_init(void) |
| 194 | { |
| 195 | unsigned long flags; |
| 196 | struct clk *clk; |
| 197 | unsigned count = 0; |
| 198 | |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame^] | 199 | clock_debug_init(); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 200 | mutex_lock(&clocks_mutex); |
| 201 | list_for_each_entry(clk, &clocks, list) { |
Matt Wagantall | d64560fe | 2011-01-26 16:20:54 -0800 | [diff] [blame^] | 202 | clock_debug_add(clk); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 203 | if (clk->flags & CLKFLAG_AUTO_OFF) { |
| 204 | spin_lock_irqsave(&clocks_lock, flags); |
| 205 | if (!clk->count) { |
| 206 | count++; |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 207 | clk->ops->auto_off(clk->id); |
Brian Swetland | 600f7cf | 2008-09-09 11:04:14 -0700 | [diff] [blame] | 208 | } |
| 209 | spin_unlock_irqrestore(&clocks_lock, flags); |
| 210 | } |
| 211 | } |
| 212 | mutex_unlock(&clocks_mutex); |
| 213 | pr_info("clock_late_init() disabled %d unused clocks\n", count); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | late_initcall(clock_late_init); |
Daniel Walker | 5e96da5 | 2010-05-12 13:43:28 -0700 | [diff] [blame] | 218 | |