blob: d72dd3770298ea5dfb0da525c96cf344328739b0 [file] [log] [blame]
Brian Swetland600f7cf2008-09-09 11:04:14 -07001/* arch/arm/mach-msm/clock.c
2 *
3 * Copyright (C) 2007 Google, Inc.
Daniel Walker5e96da52010-05-12 13:43:28 -07004 * Copyright (c) 2007-2010, Code Aurora Forum. All rights reserved.
Brian Swetland600f7cf2008-09-09 11:04:14 -07005 *
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 Swetland600f7cf2008-09-09 11:04:14 -070017#include <linux/kernel.h>
Brian Swetland600f7cf2008-09-09 11:04:14 -070018#include <linux/list.h>
19#include <linux/err.h>
Brian Swetland600f7cf2008-09-09 11:04:14 -070020#include <linux/spinlock.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020021#include <linux/pm_qos.h>
Stephen Boydbd323442011-02-23 09:37:42 -080022#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 Swetland600f7cf2008-09-09 11:04:14 -070027
28#include "clock.h"
Brian Swetland600f7cf2008-09-09 11:04:14 -070029
30static DEFINE_MUTEX(clocks_mutex);
31static DEFINE_SPINLOCK(clocks_lock);
32static LIST_HEAD(clocks);
33
34/*
Brian Swetland600f7cf2008-09-09 11:04:14 -070035 * Standard clock functions defined in include/linux/clk.h
36 */
Brian Swetland600f7cf2008-09-09 11:04:14 -070037int clk_enable(struct clk *clk)
38{
39 unsigned long flags;
40 spin_lock_irqsave(&clocks_lock, flags);
41 clk->count++;
Stephen Boyd437f6292011-01-26 16:20:52 -080042 if (clk->count == 1)
Daniel Walker5e96da52010-05-12 13:43:28 -070043 clk->ops->enable(clk->id);
Brian Swetland600f7cf2008-09-09 11:04:14 -070044 spin_unlock_irqrestore(&clocks_lock, flags);
45 return 0;
46}
47EXPORT_SYMBOL(clk_enable);
48
49void 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 Boyd437f6292011-01-26 16:20:52 -080055 if (clk->count == 0)
Daniel Walker5e96da52010-05-12 13:43:28 -070056 clk->ops->disable(clk->id);
Brian Swetland600f7cf2008-09-09 11:04:14 -070057 spin_unlock_irqrestore(&clocks_lock, flags);
58}
59EXPORT_SYMBOL(clk_disable);
60
Daniel Walker5e96da52010-05-12 13:43:28 -070061int clk_reset(struct clk *clk, enum clk_reset_action action)
62{
Daniel Walker5e96da52010-05-12 13:43:28 -070063 return clk->ops->reset(clk->remote_id, action);
64}
65EXPORT_SYMBOL(clk_reset);
66
Brian Swetland600f7cf2008-09-09 11:04:14 -070067unsigned long clk_get_rate(struct clk *clk)
68{
Daniel Walker5e96da52010-05-12 13:43:28 -070069 return clk->ops->get_rate(clk->id);
Brian Swetland600f7cf2008-09-09 11:04:14 -070070}
71EXPORT_SYMBOL(clk_get_rate);
72
73int clk_set_rate(struct clk *clk, unsigned long rate)
74{
Daniel Walker3a790bb2010-12-13 14:35:10 -080075 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 Walker5e96da52010-05-12 13:43:28 -070090 return clk->ops->set_rate(clk->id, rate);
Brian Swetland600f7cf2008-09-09 11:04:14 -070091}
92EXPORT_SYMBOL(clk_set_rate);
93
Daniel Walker5e96da52010-05-12 13:43:28 -070094long clk_round_rate(struct clk *clk, unsigned long rate)
95{
96 return clk->ops->round_rate(clk->id, rate);
97}
98EXPORT_SYMBOL(clk_round_rate);
99
Brian Swetland600f7cf2008-09-09 11:04:14 -0700100int clk_set_parent(struct clk *clk, struct clk *parent)
101{
102 return -ENOSYS;
103}
104EXPORT_SYMBOL(clk_set_parent);
105
106struct clk *clk_get_parent(struct clk *clk)
107{
108 return ERR_PTR(-ENOSYS);
109}
110EXPORT_SYMBOL(clk_get_parent);
111
Daniel Walker5e96da52010-05-12 13:43:28 -0700112/* EBI1 is the only shared clock that several clients want to vote on as of
113 * this commit. If this changes in the future, then it might be better to
114 * make clk_min_rate handle the voting or make ebi1_clk_set_min_rate more
115 * generic to support different clocks.
116 */
117static struct clk *ebi1_clk;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700118
Stephen Boyd421faca2013-06-17 10:43:18 -0700119void msm_clock_init(struct clk_lookup *clock_tbl, size_t num_clocks)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700120{
121 unsigned n;
122
Brian Swetland600f7cf2008-09-09 11:04:14 -0700123 mutex_lock(&clocks_mutex);
Stephen Boydbd323442011-02-23 09:37:42 -0800124 for (n = 0; n < num_clocks; n++) {
125 clkdev_add(&clock_tbl[n]);
126 list_add_tail(&clock_tbl[n].clk->list, &clocks);
127 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700128 mutex_unlock(&clocks_mutex);
Daniel Walker5e96da52010-05-12 13:43:28 -0700129
130 ebi1_clk = clk_get(NULL, "ebi1_clk");
131 BUG_ON(ebi1_clk == NULL);
132
Brian Swetland600f7cf2008-09-09 11:04:14 -0700133}
134
135/* The bootloader and/or AMSS may have left various clocks enabled.
136 * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have
137 * not been explicitly enabled by a clk_enable() call.
138 */
139static int __init clock_late_init(void)
140{
141 unsigned long flags;
142 struct clk *clk;
143 unsigned count = 0;
144
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800145 clock_debug_init();
Brian Swetland600f7cf2008-09-09 11:04:14 -0700146 mutex_lock(&clocks_mutex);
147 list_for_each_entry(clk, &clocks, list) {
Matt Wagantalld64560fe2011-01-26 16:20:54 -0800148 clock_debug_add(clk);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700149 if (clk->flags & CLKFLAG_AUTO_OFF) {
150 spin_lock_irqsave(&clocks_lock, flags);
151 if (!clk->count) {
152 count++;
Daniel Walker5e96da52010-05-12 13:43:28 -0700153 clk->ops->auto_off(clk->id);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700154 }
155 spin_unlock_irqrestore(&clocks_lock, flags);
156 }
157 }
158 mutex_unlock(&clocks_mutex);
159 pr_info("clock_late_init() disabled %d unused clocks\n", count);
160 return 0;
161}
162
163late_initcall(clock_late_init);
Daniel Walker5e96da52010-05-12 13:43:28 -0700164