blob: 78fd3999e1722c8808d19c832bcfa78717a0685f [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");
85 msm_xo_dump_xo(m, &msm_xo_sources[MSM_XO_PXO], "PXO during sleep");
Matt Wagantalled90b002011-12-12 21:22:43 -080086 msm_xo_dump_xo(m, &msm_xo_sources[MSM_XO_CXO], "CXO");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087 spin_unlock_irqrestore(&msm_xo_lock, flags);
88
89 return 0;
90}
91
92static int msm_xo_voters_open(struct inode *inode, struct file *file)
93{
94 return single_open(file, msm_xo_show_voters, inode->i_private);
95}
96
97static const struct file_operations msm_xo_voters_ops = {
98 .open = msm_xo_voters_open,
99 .read = seq_read,
100 .llseek = seq_lseek,
101 .release = seq_release,
102};
103
104static int __init msm_xo_debugfs_init(void)
105{
106 struct dentry *entry;
107
108 entry = debugfs_create_file("xo_voters", S_IRUGO, NULL, NULL,
109 &msm_xo_voters_ops);
110 return IS_ERR(entry) ? PTR_ERR(entry) : 0;
111}
Stephen Boyda70c5432012-02-10 14:31:45 -0800112#else
113static int __init msm_xo_debugfs_init(void) { return 0; }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114#endif
115
116static int msm_xo_update_vote(struct msm_xo *xo)
117{
118 int ret;
Vikram Mulukutla2021c002011-12-16 12:32:59 -0800119 unsigned vote, prev_vote = xo->mode, ctx_set;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120 struct msm_rpm_iv_pair cmd;
121
122 if (xo->votes[MSM_XO_MODE_ON])
123 vote = MSM_XO_MODE_ON;
124 else if (xo->votes[MSM_XO_MODE_PIN_CTRL])
125 vote = MSM_XO_MODE_PIN_CTRL;
126 else
127 vote = MSM_XO_MODE_OFF;
128
129 if (vote == prev_vote)
130 return 0;
131
132 /*
133 * Change the vote here to simplify the TCXO logic. If the RPM
134 * command fails we'll rollback.
135 */
136 xo->mode = vote;
137
138 if (xo == &msm_xo_sources[MSM_XO_PXO]) {
139 cmd.id = MSM_RPM_ID_PXO_CLK;
140 cmd.value = msm_xo_sources[MSM_XO_PXO].mode ? 1 : 0;
141 ret = msm_rpmrs_set_noirq(MSM_RPM_CTX_SET_SLEEP, &cmd, 1);
Matt Wagantalled90b002011-12-12 21:22:43 -0800142 } else if (xo == &msm_xo_sources[MSM_XO_CXO]) {
143 cmd.id = MSM_RPM_ID_CXO_CLK;
144 cmd.value = msm_xo_sources[MSM_XO_CXO].mode ? 1 : 0;
Vikram Mulukutla2021c002011-12-16 12:32:59 -0800145 if (cpu_is_msm9615())
146 ctx_set = MSM_RPM_CTX_SET_SLEEP;
147 else
148 ctx_set = MSM_RPM_CTX_SET_0;
149 ret = msm_rpmrs_set_noirq(ctx_set, &cmd, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150 } else {
151 cmd.id = MSM_RPM_ID_CXO_BUFFERS;
152 cmd.value = (msm_xo_sources[MSM_XO_TCXO_D0].mode << 0) |
153 (msm_xo_sources[MSM_XO_TCXO_D1].mode << 8) |
154 (msm_xo_sources[MSM_XO_TCXO_A0].mode << 16) |
155 (msm_xo_sources[MSM_XO_TCXO_A1].mode << 24) |
156 (msm_xo_sources[MSM_XO_TCXO_A2].mode << 28) |
Stephen Boyd0275f932012-01-23 18:42:35 -0800157 /*
158 * 8660 RPM has XO_CORE at bit 18 and 8960 RPM has
159 * XO_CORE at bit 20. Since the opposite bit is
160 * reserved in both cases, just set both and be
161 * done with it.
162 */
163 ((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 20) |
164 ((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 18);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165 ret = msm_rpm_set_noirq(MSM_RPM_CTX_SET_0, &cmd, 1);
166 }
167
168 if (ret)
169 xo->mode = prev_vote;
170
171 return ret;
172}
173
174static int __msm_xo_mode_vote(struct msm_xo_voter *xo_voter, unsigned mode)
175{
176 int ret;
177 struct msm_xo *xo = xo_voter->xo;
178
179 if (xo_voter->mode == mode)
180 return 0;
181
182 xo->votes[mode]++;
183 xo->votes[xo_voter->mode]--;
184 ret = msm_xo_update_vote(xo);
185 if (ret) {
186 xo->votes[xo_voter->mode]++;
187 xo->votes[mode]--;
188 goto out;
189 }
190 xo_voter->mode = mode;
191out:
192 return ret;
193}
194
195/**
196 * msm_xo_mode_vote() - Vote for an XO to be ON, OFF, or under PIN_CTRL
197 * @xo_voter - Valid handle returned from msm_xo_get()
198 * @mode - Mode to vote for (ON, OFF, PIN_CTRL)
199 *
200 * Vote for an XO to be either ON, OFF, or under PIN_CTRL. Votes are
201 * aggregated with ON taking precedence over PIN_CTRL taking precedence
202 * over OFF.
203 *
204 * This function returns 0 on success or a negative error code on failure.
205 */
206int msm_xo_mode_vote(struct msm_xo_voter *xo_voter, enum msm_xo_modes mode)
207{
208 int ret;
209 unsigned long flags;
Tianyi Gouc5314912011-10-25 16:44:54 -0700210
211 if (!xo_voter)
Tianyi Gou41515e22011-09-01 19:37:43 -0700212 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213
Tianyi Gouc5314912011-10-25 16:44:54 -0700214 if (mode >= NUM_MSM_XO_MODES || IS_ERR(xo_voter))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 return -EINVAL;
216
217 spin_lock_irqsave(&msm_xo_lock, flags);
218 ret = __msm_xo_mode_vote(xo_voter, mode);
219 spin_unlock_irqrestore(&msm_xo_lock, flags);
220
221 return ret;
222}
223EXPORT_SYMBOL(msm_xo_mode_vote);
224
225/**
226 * msm_xo_get() - Get a voting handle for an XO
227 * @xo_id - XO identifier
228 * @voter - Debug string to identify users
229 *
230 * XO voters vote for OFF by default. This function returns a pointer
231 * indicating success. An ERR_PTR is returned on failure.
232 *
233 * If XO voting is disabled, %NULL is returned.
234 */
235struct msm_xo_voter *msm_xo_get(enum msm_xo_ids xo_id, const char *voter)
236{
237 int ret;
238 unsigned long flags;
239 struct msm_xo_voter *xo_voter;
240
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241 if (xo_id >= NUM_MSM_XO_IDS) {
242 ret = -EINVAL;
243 goto err;
244 }
245
246 xo_voter = kzalloc(sizeof(*xo_voter), GFP_KERNEL);
247 if (!xo_voter) {
248 ret = -ENOMEM;
249 goto err;
250 }
251
252 xo_voter->name = kstrdup(voter, GFP_KERNEL);
253 if (!xo_voter->name) {
254 ret = -ENOMEM;
255 goto err_name;
256 }
257
258 xo_voter->xo = &msm_xo_sources[xo_id];
259
260 /* Voters vote for OFF by default */
261 spin_lock_irqsave(&msm_xo_lock, flags);
262 xo_voter->xo->votes[MSM_XO_MODE_OFF]++;
263 list_add(&xo_voter->list, &xo_voter->xo->voters);
264 spin_unlock_irqrestore(&msm_xo_lock, flags);
265
266 return xo_voter;
267
268err_name:
269 kfree(xo_voter);
270err:
271 return ERR_PTR(ret);
272}
273EXPORT_SYMBOL(msm_xo_get);
274
275/**
276 * msm_xo_put() - Release a voting handle
277 * @xo_voter - Valid handle returned from msm_xo_get()
278 *
279 * Release a reference to an XO voting handle. This also removes the voter's
280 * vote, therefore calling msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF)
281 * beforehand is unnecessary.
282 */
283void msm_xo_put(struct msm_xo_voter *xo_voter)
284{
285 unsigned long flags;
286
Tianyi Gouc5314912011-10-25 16:44:54 -0700287 if (!xo_voter || IS_ERR(xo_voter))
288 return;
289
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290 spin_lock_irqsave(&msm_xo_lock, flags);
291 __msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF);
292 xo_voter->xo->votes[MSM_XO_MODE_OFF]--;
293 list_del(&xo_voter->list);
294 spin_unlock_irqrestore(&msm_xo_lock, flags);
295
296 kfree(xo_voter->name);
297 kfree(xo_voter);
298}
299EXPORT_SYMBOL(msm_xo_put);
300
301int __init msm_xo_init(void)
302{
303 int i;
Vikram Mulukutla2021c002011-12-16 12:32:59 -0800304 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 struct msm_rpm_iv_pair cmd[2];
306
307 for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++)
308 INIT_LIST_HEAD(&msm_xo_sources[i].voters);
309
Vikram Mulukutla2021c002011-12-16 12:32:59 -0800310 if (cpu_is_msm9615()) {
311 cmd[0].id = MSM_RPM_ID_CXO_CLK;
312 cmd[0].value = 1;
313 ret = msm_rpmrs_set(MSM_RPM_CTX_SET_0, cmd, 1);
314 if (ret)
315 goto out;
316
317 cmd[0].id = MSM_RPM_ID_CXO_CLK;
318 cmd[0].value = 0;
319 ret = msm_rpmrs_set(MSM_RPM_CTX_SET_SLEEP, cmd, 1);
320 goto out;
321 }
322
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323 cmd[0].id = MSM_RPM_ID_PXO_CLK;
324 cmd[0].value = 1;
325 cmd[1].id = MSM_RPM_ID_CXO_BUFFERS;
326 cmd[1].value = 0;
327 ret = msm_rpmrs_set(MSM_RPM_CTX_SET_0, cmd, 2);
328 if (ret)
329 goto out;
330
331 cmd[0].id = MSM_RPM_ID_PXO_CLK;
332 cmd[0].value = 0;
333 ret = msm_rpmrs_set(MSM_RPM_CTX_SET_SLEEP, cmd, 1);
334 if (ret)
335 goto out;
Stephen Boyda70c5432012-02-10 14:31:45 -0800336 msm_xo_debugfs_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337out:
338 return ret;
339}