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