blob: 802ee2a9fc4f3faa532be64034a3526db3eb5bb9 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
2 *
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) |
152 ((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 20);
153 ret = msm_rpm_set_noirq(MSM_RPM_CTX_SET_0, &cmd, 1);
154 }
155
156 if (ret)
157 xo->mode = prev_vote;
158
159 return ret;
160}
161
162static int __msm_xo_mode_vote(struct msm_xo_voter *xo_voter, unsigned mode)
163{
164 int ret;
165 struct msm_xo *xo = xo_voter->xo;
166
167 if (xo_voter->mode == mode)
168 return 0;
169
170 xo->votes[mode]++;
171 xo->votes[xo_voter->mode]--;
172 ret = msm_xo_update_vote(xo);
173 if (ret) {
174 xo->votes[xo_voter->mode]++;
175 xo->votes[mode]--;
176 goto out;
177 }
178 xo_voter->mode = mode;
179out:
180 return ret;
181}
182
183/**
184 * msm_xo_mode_vote() - Vote for an XO to be ON, OFF, or under PIN_CTRL
185 * @xo_voter - Valid handle returned from msm_xo_get()
186 * @mode - Mode to vote for (ON, OFF, PIN_CTRL)
187 *
188 * Vote for an XO to be either ON, OFF, or under PIN_CTRL. Votes are
189 * aggregated with ON taking precedence over PIN_CTRL taking precedence
190 * over OFF.
191 *
192 * This function returns 0 on success or a negative error code on failure.
193 */
194int msm_xo_mode_vote(struct msm_xo_voter *xo_voter, enum msm_xo_modes mode)
195{
196 int ret;
197 unsigned long flags;
Tianyi Gouc5314912011-10-25 16:44:54 -0700198
199 if (!xo_voter)
Tianyi Gou41515e22011-09-01 19:37:43 -0700200 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201
Tianyi Gouc5314912011-10-25 16:44:54 -0700202 if (mode >= NUM_MSM_XO_MODES || IS_ERR(xo_voter))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203 return -EINVAL;
204
205 spin_lock_irqsave(&msm_xo_lock, flags);
206 ret = __msm_xo_mode_vote(xo_voter, mode);
207 spin_unlock_irqrestore(&msm_xo_lock, flags);
208
209 return ret;
210}
211EXPORT_SYMBOL(msm_xo_mode_vote);
212
213/**
214 * msm_xo_get() - Get a voting handle for an XO
215 * @xo_id - XO identifier
216 * @voter - Debug string to identify users
217 *
218 * XO voters vote for OFF by default. This function returns a pointer
219 * indicating success. An ERR_PTR is returned on failure.
220 *
221 * If XO voting is disabled, %NULL is returned.
222 */
223struct msm_xo_voter *msm_xo_get(enum msm_xo_ids xo_id, const char *voter)
224{
225 int ret;
226 unsigned long flags;
227 struct msm_xo_voter *xo_voter;
228
Tianyi Gouc5314912011-10-25 16:44:54 -0700229 /*
230 * TODO: Remove early return for 8064 once RPM XO voting support
Matt Wagantalled90b002011-12-12 21:22:43 -0800231 * is available. Remove early return for 8960 CXO once all voters
232 * for it are in place.
Tianyi Gouc5314912011-10-25 16:44:54 -0700233 */
Matt Wagantalled90b002011-12-12 21:22:43 -0800234 if (cpu_is_apq8064() || (cpu_is_msm8960() && xo_id == MSM_XO_CXO))
Tianyi Gou41515e22011-09-01 19:37:43 -0700235 return NULL;
236
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237 if (xo_id >= NUM_MSM_XO_IDS) {
238 ret = -EINVAL;
239 goto err;
240 }
241
242 xo_voter = kzalloc(sizeof(*xo_voter), GFP_KERNEL);
243 if (!xo_voter) {
244 ret = -ENOMEM;
245 goto err;
246 }
247
248 xo_voter->name = kstrdup(voter, GFP_KERNEL);
249 if (!xo_voter->name) {
250 ret = -ENOMEM;
251 goto err_name;
252 }
253
254 xo_voter->xo = &msm_xo_sources[xo_id];
255
256 /* Voters vote for OFF by default */
257 spin_lock_irqsave(&msm_xo_lock, flags);
258 xo_voter->xo->votes[MSM_XO_MODE_OFF]++;
259 list_add(&xo_voter->list, &xo_voter->xo->voters);
260 spin_unlock_irqrestore(&msm_xo_lock, flags);
261
262 return xo_voter;
263
264err_name:
265 kfree(xo_voter);
266err:
267 return ERR_PTR(ret);
268}
269EXPORT_SYMBOL(msm_xo_get);
270
271/**
272 * msm_xo_put() - Release a voting handle
273 * @xo_voter - Valid handle returned from msm_xo_get()
274 *
275 * Release a reference to an XO voting handle. This also removes the voter's
276 * vote, therefore calling msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF)
277 * beforehand is unnecessary.
278 */
279void msm_xo_put(struct msm_xo_voter *xo_voter)
280{
281 unsigned long flags;
282
Tianyi Gouc5314912011-10-25 16:44:54 -0700283 if (!xo_voter || IS_ERR(xo_voter))
284 return;
285
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 spin_lock_irqsave(&msm_xo_lock, flags);
287 __msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF);
288 xo_voter->xo->votes[MSM_XO_MODE_OFF]--;
289 list_del(&xo_voter->list);
290 spin_unlock_irqrestore(&msm_xo_lock, flags);
291
292 kfree(xo_voter->name);
293 kfree(xo_voter);
294}
295EXPORT_SYMBOL(msm_xo_put);
296
297int __init msm_xo_init(void)
298{
299 int i;
300 int ret;
301 struct msm_rpm_iv_pair cmd[2];
302
303 for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++)
304 INIT_LIST_HEAD(&msm_xo_sources[i].voters);
305
306 cmd[0].id = MSM_RPM_ID_PXO_CLK;
307 cmd[0].value = 1;
308 cmd[1].id = MSM_RPM_ID_CXO_BUFFERS;
309 cmd[1].value = 0;
310 ret = msm_rpmrs_set(MSM_RPM_CTX_SET_0, cmd, 2);
311 if (ret)
312 goto out;
313
314 cmd[0].id = MSM_RPM_ID_PXO_CLK;
315 cmd[0].value = 0;
316 ret = msm_rpmrs_set(MSM_RPM_CTX_SET_SLEEP, cmd, 1);
317 if (ret)
318 goto out;
319out:
320 return ret;
321}