blob: 52bbaeb6b81a87fe7c4cba1f2bf00aa7aa69d5f1 [file] [log] [blame]
Stephen Boyd0275f932012-01-23 18:42:35 -08001/* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/init.h>
16#include <linux/err.h>
17#include <linux/module.h>
18#include <linux/spinlock.h>
19#include <linux/debugfs.h>
20#include <linux/list.h>
21#include <linux/seq_file.h>
22
23#include <mach/msm_xo.h>
24#include <mach/rpm.h>
Tianyi Gou41515e22011-09-01 19:37:43 -070025#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026
27#include "rpm_resources.h"
28
29static DEFINE_SPINLOCK(msm_xo_lock);
30
31struct msm_xo {
32 unsigned votes[NUM_MSM_XO_MODES];
33 unsigned mode;
34 struct list_head voters;
35};
36
37struct msm_xo_voter {
38 const char *name;
39 unsigned mode;
40 struct msm_xo *xo;
41 struct list_head list;
42};
43
44static struct msm_xo msm_xo_sources[NUM_MSM_XO_IDS];
45
46#ifdef CONFIG_DEBUG_FS
47static const char *msm_xo_mode_to_str(unsigned mode)
48{
49 switch (mode) {
50 case MSM_XO_MODE_ON:
51 return "ON";
52 case MSM_XO_MODE_PIN_CTRL:
53 return "PIN";
54 case MSM_XO_MODE_OFF:
55 return "OFF";
56 default:
57 return "ERR";
58 }
59}
60
61static void msm_xo_dump_xo(struct seq_file *m, struct msm_xo *xo,
62 const char *name)
63{
64 struct msm_xo_voter *voter;
65
66 seq_printf(m, "%-20s%s\n", name, msm_xo_mode_to_str(xo->mode));
67 list_for_each_entry(voter, &xo->voters, list)
68 seq_printf(m, " %s %-16s %s\n",
69 xo->mode == voter->mode ? "*" : " ",
70 voter->name,
71 msm_xo_mode_to_str(voter->mode));
72}
73
74static int msm_xo_show_voters(struct seq_file *m, void *v)
75{
76 unsigned long flags;
77
78 spin_lock_irqsave(&msm_xo_lock, flags);
79 msm_xo_dump_xo(m, &msm_xo_sources[MSM_XO_TCXO_D0], "TCXO D0");
80 msm_xo_dump_xo(m, &msm_xo_sources[MSM_XO_TCXO_D1], "TCXO D1");
81 msm_xo_dump_xo(m, &msm_xo_sources[MSM_XO_TCXO_A0], "TCXO A0");
82 msm_xo_dump_xo(m, &msm_xo_sources[MSM_XO_TCXO_A1], "TCXO A1");
83 msm_xo_dump_xo(m, &msm_xo_sources[MSM_XO_TCXO_A2], "TCXO A2");
84 msm_xo_dump_xo(m, &msm_xo_sources[MSM_XO_CORE], "TCXO Core");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070085 spin_unlock_irqrestore(&msm_xo_lock, flags);
86
87 return 0;
88}
89
90static int msm_xo_voters_open(struct inode *inode, struct file *file)
91{
92 return single_open(file, msm_xo_show_voters, inode->i_private);
93}
94
95static const struct file_operations msm_xo_voters_ops = {
96 .open = msm_xo_voters_open,
97 .read = seq_read,
98 .llseek = seq_lseek,
99 .release = seq_release,
100};
101
102static int __init msm_xo_debugfs_init(void)
103{
104 struct dentry *entry;
105
106 entry = debugfs_create_file("xo_voters", S_IRUGO, NULL, NULL,
107 &msm_xo_voters_ops);
108 return IS_ERR(entry) ? PTR_ERR(entry) : 0;
109}
Stephen Boyda70c5432012-02-10 14:31:45 -0800110#else
111static int __init msm_xo_debugfs_init(void) { return 0; }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112#endif
113
114static int msm_xo_update_vote(struct msm_xo *xo)
115{
116 int ret;
Stephen Boyd47239d52012-01-26 14:38:40 -0800117 unsigned vote, prev_vote = xo->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118 struct msm_rpm_iv_pair cmd;
119
120 if (xo->votes[MSM_XO_MODE_ON])
121 vote = MSM_XO_MODE_ON;
122 else if (xo->votes[MSM_XO_MODE_PIN_CTRL])
123 vote = MSM_XO_MODE_PIN_CTRL;
124 else
125 vote = MSM_XO_MODE_OFF;
126
127 if (vote == prev_vote)
128 return 0;
129
130 /*
131 * Change the vote here to simplify the TCXO logic. If the RPM
132 * command fails we'll rollback.
133 */
134 xo->mode = vote;
Stephen Boyd47239d52012-01-26 14:38:40 -0800135 cmd.id = MSM_RPM_ID_CXO_BUFFERS;
136 cmd.value = (msm_xo_sources[MSM_XO_TCXO_D0].mode << 0) |
137 (msm_xo_sources[MSM_XO_TCXO_D1].mode << 8) |
138 (msm_xo_sources[MSM_XO_TCXO_A0].mode << 16) |
139 (msm_xo_sources[MSM_XO_TCXO_A1].mode << 24) |
140 (msm_xo_sources[MSM_XO_TCXO_A2].mode << 28) |
141 /*
142 * 8660 RPM has XO_CORE at bit 18 and 8960 RPM has
143 * XO_CORE at bit 20. Since the opposite bit is
144 * reserved in both cases, just set both and be
145 * done with it.
146 */
147 ((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 20) |
148 ((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 18);
149 ret = msm_rpm_set_noirq(MSM_RPM_CTX_SET_0, &cmd, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150
151 if (ret)
152 xo->mode = prev_vote;
153
154 return ret;
155}
156
157static int __msm_xo_mode_vote(struct msm_xo_voter *xo_voter, unsigned mode)
158{
159 int ret;
160 struct msm_xo *xo = xo_voter->xo;
161
162 if (xo_voter->mode == mode)
163 return 0;
164
165 xo->votes[mode]++;
166 xo->votes[xo_voter->mode]--;
167 ret = msm_xo_update_vote(xo);
168 if (ret) {
169 xo->votes[xo_voter->mode]++;
170 xo->votes[mode]--;
171 goto out;
172 }
173 xo_voter->mode = mode;
174out:
175 return ret;
176}
177
178/**
179 * msm_xo_mode_vote() - Vote for an XO to be ON, OFF, or under PIN_CTRL
180 * @xo_voter - Valid handle returned from msm_xo_get()
181 * @mode - Mode to vote for (ON, OFF, PIN_CTRL)
182 *
183 * Vote for an XO to be either ON, OFF, or under PIN_CTRL. Votes are
184 * aggregated with ON taking precedence over PIN_CTRL taking precedence
185 * over OFF.
186 *
187 * This function returns 0 on success or a negative error code on failure.
188 */
189int msm_xo_mode_vote(struct msm_xo_voter *xo_voter, enum msm_xo_modes mode)
190{
191 int ret;
192 unsigned long flags;
Tianyi Gouc5314912011-10-25 16:44:54 -0700193
194 if (!xo_voter)
Tianyi Gou41515e22011-09-01 19:37:43 -0700195 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196
Tianyi Gouc5314912011-10-25 16:44:54 -0700197 if (mode >= NUM_MSM_XO_MODES || IS_ERR(xo_voter))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 return -EINVAL;
199
200 spin_lock_irqsave(&msm_xo_lock, flags);
201 ret = __msm_xo_mode_vote(xo_voter, mode);
202 spin_unlock_irqrestore(&msm_xo_lock, flags);
203
204 return ret;
205}
206EXPORT_SYMBOL(msm_xo_mode_vote);
207
208/**
209 * msm_xo_get() - Get a voting handle for an XO
210 * @xo_id - XO identifier
211 * @voter - Debug string to identify users
212 *
213 * XO voters vote for OFF by default. This function returns a pointer
214 * indicating success. An ERR_PTR is returned on failure.
215 *
216 * If XO voting is disabled, %NULL is returned.
217 */
218struct msm_xo_voter *msm_xo_get(enum msm_xo_ids xo_id, const char *voter)
219{
220 int ret;
221 unsigned long flags;
222 struct msm_xo_voter *xo_voter;
223
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 if (xo_id >= NUM_MSM_XO_IDS) {
225 ret = -EINVAL;
226 goto err;
227 }
228
229 xo_voter = kzalloc(sizeof(*xo_voter), GFP_KERNEL);
230 if (!xo_voter) {
231 ret = -ENOMEM;
232 goto err;
233 }
234
235 xo_voter->name = kstrdup(voter, GFP_KERNEL);
236 if (!xo_voter->name) {
237 ret = -ENOMEM;
238 goto err_name;
239 }
240
241 xo_voter->xo = &msm_xo_sources[xo_id];
242
243 /* Voters vote for OFF by default */
244 spin_lock_irqsave(&msm_xo_lock, flags);
245 xo_voter->xo->votes[MSM_XO_MODE_OFF]++;
246 list_add(&xo_voter->list, &xo_voter->xo->voters);
247 spin_unlock_irqrestore(&msm_xo_lock, flags);
248
249 return xo_voter;
250
251err_name:
252 kfree(xo_voter);
253err:
254 return ERR_PTR(ret);
255}
256EXPORT_SYMBOL(msm_xo_get);
257
258/**
259 * msm_xo_put() - Release a voting handle
260 * @xo_voter - Valid handle returned from msm_xo_get()
261 *
262 * Release a reference to an XO voting handle. This also removes the voter's
263 * vote, therefore calling msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF)
264 * beforehand is unnecessary.
265 */
266void msm_xo_put(struct msm_xo_voter *xo_voter)
267{
268 unsigned long flags;
269
Tianyi Gouc5314912011-10-25 16:44:54 -0700270 if (!xo_voter || IS_ERR(xo_voter))
271 return;
272
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700273 spin_lock_irqsave(&msm_xo_lock, flags);
274 __msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF);
275 xo_voter->xo->votes[MSM_XO_MODE_OFF]--;
276 list_del(&xo_voter->list);
277 spin_unlock_irqrestore(&msm_xo_lock, flags);
278
279 kfree(xo_voter->name);
280 kfree(xo_voter);
281}
282EXPORT_SYMBOL(msm_xo_put);
283
284int __init msm_xo_init(void)
285{
Stephen Boyd47239d52012-01-26 14:38:40 -0800286 int i, ret;
287 struct msm_rpm_iv_pair cmd[1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288
289 for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++)
290 INIT_LIST_HEAD(&msm_xo_sources[i].voters);
291
Stephen Boyd47239d52012-01-26 14:38:40 -0800292 cmd[0].id = MSM_RPM_ID_CXO_BUFFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 cmd[0].value = 0;
Stephen Boyd47239d52012-01-26 14:38:40 -0800294 ret = msm_rpmrs_set(MSM_RPM_CTX_SET_0, cmd, ARRAY_SIZE(cmd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295 if (ret)
Stephen Boyd47239d52012-01-26 14:38:40 -0800296 return ret;
Stephen Boyda70c5432012-02-10 14:31:45 -0800297 msm_xo_debugfs_init();
Stephen Boyd47239d52012-01-26 14:38:40 -0800298 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299}