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