blob: af272afbd1723f13f6ddda806449a7b9018eb1e9 [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}
112late_initcall(msm_xo_debugfs_init);
113#endif
114
115static int msm_xo_update_vote(struct msm_xo *xo)
116{
117 int ret;
118 unsigned vote, prev_vote = xo->mode;
119 struct msm_rpm_iv_pair cmd;
120
121 if (xo->votes[MSM_XO_MODE_ON])
122 vote = MSM_XO_MODE_ON;
123 else if (xo->votes[MSM_XO_MODE_PIN_CTRL])
124 vote = MSM_XO_MODE_PIN_CTRL;
125 else
126 vote = MSM_XO_MODE_OFF;
127
128 if (vote == prev_vote)
129 return 0;
130
131 /*
132 * Change the vote here to simplify the TCXO logic. If the RPM
133 * command fails we'll rollback.
134 */
135 xo->mode = vote;
136
137 if (xo == &msm_xo_sources[MSM_XO_PXO]) {
138 cmd.id = MSM_RPM_ID_PXO_CLK;
139 cmd.value = msm_xo_sources[MSM_XO_PXO].mode ? 1 : 0;
140 ret = msm_rpmrs_set_noirq(MSM_RPM_CTX_SET_SLEEP, &cmd, 1);
Matt Wagantalled90b002011-12-12 21:22:43 -0800141 } else if (xo == &msm_xo_sources[MSM_XO_CXO]) {
142 cmd.id = MSM_RPM_ID_CXO_CLK;
143 cmd.value = msm_xo_sources[MSM_XO_CXO].mode ? 1 : 0;
144 ret = msm_rpmrs_set_noirq(MSM_RPM_CTX_SET_0, &cmd, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 } else {
146 cmd.id = MSM_RPM_ID_CXO_BUFFERS;
147 cmd.value = (msm_xo_sources[MSM_XO_TCXO_D0].mode << 0) |
148 (msm_xo_sources[MSM_XO_TCXO_D1].mode << 8) |
149 (msm_xo_sources[MSM_XO_TCXO_A0].mode << 16) |
150 (msm_xo_sources[MSM_XO_TCXO_A1].mode << 24) |
151 (msm_xo_sources[MSM_XO_TCXO_A2].mode << 28) |
Stephen Boyd0275f932012-01-23 18:42:35 -0800152 /*
153 * 8660 RPM has XO_CORE at bit 18 and 8960 RPM has
154 * XO_CORE at bit 20. Since the opposite bit is
155 * reserved in both cases, just set both and be
156 * done with it.
157 */
158 ((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 20) |
159 ((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 18);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160 ret = msm_rpm_set_noirq(MSM_RPM_CTX_SET_0, &cmd, 1);
161 }
162
163 if (ret)
164 xo->mode = prev_vote;
165
166 return ret;
167}
168
169static int __msm_xo_mode_vote(struct msm_xo_voter *xo_voter, unsigned mode)
170{
171 int ret;
172 struct msm_xo *xo = xo_voter->xo;
173
174 if (xo_voter->mode == mode)
175 return 0;
176
177 xo->votes[mode]++;
178 xo->votes[xo_voter->mode]--;
179 ret = msm_xo_update_vote(xo);
180 if (ret) {
181 xo->votes[xo_voter->mode]++;
182 xo->votes[mode]--;
183 goto out;
184 }
185 xo_voter->mode = mode;
186out:
187 return ret;
188}
189
190/**
191 * msm_xo_mode_vote() - Vote for an XO to be ON, OFF, or under PIN_CTRL
192 * @xo_voter - Valid handle returned from msm_xo_get()
193 * @mode - Mode to vote for (ON, OFF, PIN_CTRL)
194 *
195 * Vote for an XO to be either ON, OFF, or under PIN_CTRL. Votes are
196 * aggregated with ON taking precedence over PIN_CTRL taking precedence
197 * over OFF.
198 *
199 * This function returns 0 on success or a negative error code on failure.
200 */
201int msm_xo_mode_vote(struct msm_xo_voter *xo_voter, enum msm_xo_modes mode)
202{
203 int ret;
204 unsigned long flags;
Tianyi Gouc5314912011-10-25 16:44:54 -0700205
206 if (!xo_voter)
Tianyi Gou41515e22011-09-01 19:37:43 -0700207 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208
Tianyi Gouc5314912011-10-25 16:44:54 -0700209 if (mode >= NUM_MSM_XO_MODES || IS_ERR(xo_voter))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 return -EINVAL;
211
212 spin_lock_irqsave(&msm_xo_lock, flags);
213 ret = __msm_xo_mode_vote(xo_voter, mode);
214 spin_unlock_irqrestore(&msm_xo_lock, flags);
215
216 return ret;
217}
218EXPORT_SYMBOL(msm_xo_mode_vote);
219
220/**
221 * msm_xo_get() - Get a voting handle for an XO
222 * @xo_id - XO identifier
223 * @voter - Debug string to identify users
224 *
225 * XO voters vote for OFF by default. This function returns a pointer
226 * indicating success. An ERR_PTR is returned on failure.
227 *
228 * If XO voting is disabled, %NULL is returned.
229 */
230struct msm_xo_voter *msm_xo_get(enum msm_xo_ids xo_id, const char *voter)
231{
232 int ret;
233 unsigned long flags;
234 struct msm_xo_voter *xo_voter;
235
Tianyi Gouc5314912011-10-25 16:44:54 -0700236 /*
237 * TODO: Remove early return for 8064 once RPM XO voting support
Matt Wagantalled90b002011-12-12 21:22:43 -0800238 * is available. Remove early return for 8960 CXO once all voters
239 * for it are in place.
Tianyi Gouc5314912011-10-25 16:44:54 -0700240 */
Matt Wagantalled90b002011-12-12 21:22:43 -0800241 if (cpu_is_apq8064() || (cpu_is_msm8960() && xo_id == MSM_XO_CXO))
Tianyi Gou41515e22011-09-01 19:37:43 -0700242 return NULL;
243
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244 if (xo_id >= NUM_MSM_XO_IDS) {
245 ret = -EINVAL;
246 goto err;
247 }
248
249 xo_voter = kzalloc(sizeof(*xo_voter), GFP_KERNEL);
250 if (!xo_voter) {
251 ret = -ENOMEM;
252 goto err;
253 }
254
255 xo_voter->name = kstrdup(voter, GFP_KERNEL);
256 if (!xo_voter->name) {
257 ret = -ENOMEM;
258 goto err_name;
259 }
260
261 xo_voter->xo = &msm_xo_sources[xo_id];
262
263 /* Voters vote for OFF by default */
264 spin_lock_irqsave(&msm_xo_lock, flags);
265 xo_voter->xo->votes[MSM_XO_MODE_OFF]++;
266 list_add(&xo_voter->list, &xo_voter->xo->voters);
267 spin_unlock_irqrestore(&msm_xo_lock, flags);
268
269 return xo_voter;
270
271err_name:
272 kfree(xo_voter);
273err:
274 return ERR_PTR(ret);
275}
276EXPORT_SYMBOL(msm_xo_get);
277
278/**
279 * msm_xo_put() - Release a voting handle
280 * @xo_voter - Valid handle returned from msm_xo_get()
281 *
282 * Release a reference to an XO voting handle. This also removes the voter's
283 * vote, therefore calling msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF)
284 * beforehand is unnecessary.
285 */
286void msm_xo_put(struct msm_xo_voter *xo_voter)
287{
288 unsigned long flags;
289
Tianyi Gouc5314912011-10-25 16:44:54 -0700290 if (!xo_voter || IS_ERR(xo_voter))
291 return;
292
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 spin_lock_irqsave(&msm_xo_lock, flags);
294 __msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF);
295 xo_voter->xo->votes[MSM_XO_MODE_OFF]--;
296 list_del(&xo_voter->list);
297 spin_unlock_irqrestore(&msm_xo_lock, flags);
298
299 kfree(xo_voter->name);
300 kfree(xo_voter);
301}
302EXPORT_SYMBOL(msm_xo_put);
303
304int __init msm_xo_init(void)
305{
306 int i;
307 int ret;
308 struct msm_rpm_iv_pair cmd[2];
309
310 for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++)
311 INIT_LIST_HEAD(&msm_xo_sources[i].voters);
312
313 cmd[0].id = MSM_RPM_ID_PXO_CLK;
314 cmd[0].value = 1;
315 cmd[1].id = MSM_RPM_ID_CXO_BUFFERS;
316 cmd[1].value = 0;
317 ret = msm_rpmrs_set(MSM_RPM_CTX_SET_0, cmd, 2);
318 if (ret)
319 goto out;
320
321 cmd[0].id = MSM_RPM_ID_PXO_CLK;
322 cmd[0].value = 0;
323 ret = msm_rpmrs_set(MSM_RPM_CTX_SET_SLEEP, cmd, 1);
324 if (ret)
325 goto out;
326out:
327 return ret;
328}