blob: 825450215a0535b910e52f6a8115ceae7203b33c [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>
Stephen Boydc8357a92012-04-04 13:54:38 -070018#include <linux/mutex.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070019#include <linux/debugfs.h>
20#include <linux/list.h>
21#include <linux/seq_file.h>
Stephen Boydb60f3862012-02-22 15:18:45 -080022#include <linux/uaccess.h>
23#include <linux/string.h>
Stephen Boyd5a190a82012-03-01 14:45:15 -080024#include <linux/clk.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025
26#include <mach/msm_xo.h>
27#include <mach/rpm.h>
Tianyi Gou41515e22011-09-01 19:37:43 -070028#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029
30#include "rpm_resources.h"
31
Stephen Boydc8357a92012-04-04 13:54:38 -070032static DEFINE_MUTEX(msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
34struct msm_xo {
35 unsigned votes[NUM_MSM_XO_MODES];
36 unsigned mode;
37 struct list_head voters;
38};
39
40struct msm_xo_voter {
41 const char *name;
42 unsigned mode;
43 struct msm_xo *xo;
44 struct list_head list;
45};
46
47static struct msm_xo msm_xo_sources[NUM_MSM_XO_IDS];
48
49#ifdef CONFIG_DEBUG_FS
Stephen Boydb60f3862012-02-22 15:18:45 -080050static const char *msm_xo_to_str[NUM_MSM_XO_IDS] = {
51 [MSM_XO_TCXO_D0] = "D0",
52 [MSM_XO_TCXO_D1] = "D1",
53 [MSM_XO_TCXO_A0] = "A0",
54 [MSM_XO_TCXO_A1] = "A1",
55 [MSM_XO_TCXO_A2] = "A2",
56 [MSM_XO_CORE] = "CORE",
57};
58
59static const char *msm_xo_mode_to_str[NUM_MSM_XO_MODES] = {
60 [MSM_XO_MODE_ON] = "ON",
61 [MSM_XO_MODE_PIN_CTRL] = "PIN",
62 [MSM_XO_MODE_OFF] = "OFF",
63};
64
65static int msm_xo_debugfs_open(struct inode *inode, struct file *filp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066{
Stephen Boydb60f3862012-02-22 15:18:45 -080067 filp->private_data = inode->i_private;
68 return 0;
69}
70
71static ssize_t msm_xo_debugfs_read(struct file *filp, char __user *ubuf,
72 size_t cnt, loff_t *ppos)
73{
74 int r;
75 char buf[10];
76 struct msm_xo_voter *xo = filp->private_data;
77
78 r = snprintf(buf, sizeof(buf), "%s\n", msm_xo_mode_to_str[xo->mode]);
79 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
80}
81
82static ssize_t msm_xo_debugfs_write(struct file *filp,
83 const char __user *ubuf, size_t cnt, loff_t *ppos)
84{
85 struct msm_xo_voter *xo = filp->private_data;
86 char buf[10], *b;
87 int i, ret;
88
89 if (cnt > sizeof(buf) - 1)
90 return -EINVAL;
91
92 if (copy_from_user(&buf, ubuf, cnt))
93 return -EFAULT;
94 buf[cnt] = '\0';
95 b = strstrip(buf);
96
97 for (i = 0; i < ARRAY_SIZE(msm_xo_mode_to_str); i++)
98 if (!strncasecmp(b, msm_xo_mode_to_str[i], sizeof(buf))) {
99 ret = msm_xo_mode_vote(xo, i);
100 return ret ? : cnt;
101 }
102
103 return -EINVAL;
104}
105
106static const struct file_operations msm_xo_debugfs_fops = {
107 .open = msm_xo_debugfs_open,
108 .read = msm_xo_debugfs_read,
109 .write = msm_xo_debugfs_write,
110};
111
112static struct dentry *xo_debugfs_root;
113static struct msm_xo_voter *xo_debugfs_voters[NUM_MSM_XO_IDS];
114
115static int __init msm_xo_init_debugfs_voters(void)
116{
117 int i;
118
119 xo_debugfs_root = debugfs_create_dir("msm_xo", NULL);
120 if (!xo_debugfs_root)
121 return -ENOMEM;
122
123 for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++) {
124 xo_debugfs_voters[i] = msm_xo_get(i, "debugfs");
125 if (IS_ERR(xo_debugfs_voters[i]))
126 goto err;
127 debugfs_create_file(msm_xo_to_str[i], S_IRUGO | S_IWUSR,
128 xo_debugfs_root, xo_debugfs_voters[i],
129 &msm_xo_debugfs_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130 }
Stephen Boydb60f3862012-02-22 15:18:45 -0800131 return 0;
132err:
133 while (--i >= 0)
134 msm_xo_put(xo_debugfs_voters[i]);
135 debugfs_remove_recursive(xo_debugfs_root);
136 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137}
138
139static void msm_xo_dump_xo(struct seq_file *m, struct msm_xo *xo,
140 const char *name)
141{
142 struct msm_xo_voter *voter;
143
Stephen Boydb60f3862012-02-22 15:18:45 -0800144 seq_printf(m, "CXO %-16s%s\n", name, msm_xo_mode_to_str[xo->mode]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 list_for_each_entry(voter, &xo->voters, list)
146 seq_printf(m, " %s %-16s %s\n",
147 xo->mode == voter->mode ? "*" : " ",
148 voter->name,
Stephen Boydb60f3862012-02-22 15:18:45 -0800149 msm_xo_mode_to_str[voter->mode]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150}
151
152static int msm_xo_show_voters(struct seq_file *m, void *v)
153{
Stephen Boydb60f3862012-02-22 15:18:45 -0800154 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700155
Stephen Boydc8357a92012-04-04 13:54:38 -0700156 mutex_lock(&msm_xo_lock);
Stephen Boydb60f3862012-02-22 15:18:45 -0800157 for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++)
158 msm_xo_dump_xo(m, &msm_xo_sources[i], msm_xo_to_str[i]);
Stephen Boydc8357a92012-04-04 13:54:38 -0700159 mutex_unlock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160
161 return 0;
162}
163
164static int msm_xo_voters_open(struct inode *inode, struct file *file)
165{
166 return single_open(file, msm_xo_show_voters, inode->i_private);
167}
168
169static const struct file_operations msm_xo_voters_ops = {
170 .open = msm_xo_voters_open,
171 .read = seq_read,
172 .llseek = seq_lseek,
173 .release = seq_release,
174};
175
176static int __init msm_xo_debugfs_init(void)
177{
Stephen Boydb60f3862012-02-22 15:18:45 -0800178 msm_xo_init_debugfs_voters();
179 if (!debugfs_create_file("xo_voters", S_IRUGO, NULL, NULL,
180 &msm_xo_voters_ops))
181 return -ENOMEM;
182 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700183}
Stephen Boyda70c5432012-02-10 14:31:45 -0800184#else
185static int __init msm_xo_debugfs_init(void) { return 0; }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186#endif
187
188static int msm_xo_update_vote(struct msm_xo *xo)
189{
190 int ret;
Stephen Boyd47239d52012-01-26 14:38:40 -0800191 unsigned vote, prev_vote = xo->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192 struct msm_rpm_iv_pair cmd;
193
194 if (xo->votes[MSM_XO_MODE_ON])
195 vote = MSM_XO_MODE_ON;
196 else if (xo->votes[MSM_XO_MODE_PIN_CTRL])
197 vote = MSM_XO_MODE_PIN_CTRL;
198 else
199 vote = MSM_XO_MODE_OFF;
200
201 if (vote == prev_vote)
202 return 0;
203
204 /*
205 * Change the vote here to simplify the TCXO logic. If the RPM
206 * command fails we'll rollback.
207 */
208 xo->mode = vote;
Stephen Boyd47239d52012-01-26 14:38:40 -0800209 cmd.id = MSM_RPM_ID_CXO_BUFFERS;
210 cmd.value = (msm_xo_sources[MSM_XO_TCXO_D0].mode << 0) |
211 (msm_xo_sources[MSM_XO_TCXO_D1].mode << 8) |
212 (msm_xo_sources[MSM_XO_TCXO_A0].mode << 16) |
213 (msm_xo_sources[MSM_XO_TCXO_A1].mode << 24) |
214 (msm_xo_sources[MSM_XO_TCXO_A2].mode << 28) |
215 /*
216 * 8660 RPM has XO_CORE at bit 18 and 8960 RPM has
217 * XO_CORE at bit 20. Since the opposite bit is
218 * reserved in both cases, just set both and be
219 * done with it.
220 */
221 ((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 20) |
222 ((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 18);
Stephen Boydc8357a92012-04-04 13:54:38 -0700223 ret = msm_rpm_set(MSM_RPM_CTX_SET_0, &cmd, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224
225 if (ret)
226 xo->mode = prev_vote;
227
228 return ret;
229}
230
231static int __msm_xo_mode_vote(struct msm_xo_voter *xo_voter, unsigned mode)
232{
233 int ret;
234 struct msm_xo *xo = xo_voter->xo;
Stephen Boyd5a190a82012-03-01 14:45:15 -0800235 int is_d0 = xo == &msm_xo_sources[MSM_XO_TCXO_D0];
236 int needs_workaround = cpu_is_msm8960() || cpu_is_apq8064() ||
Stepan Moskovchenko0df9bb22012-07-06 18:19:15 -0700237 cpu_is_msm8930() || cpu_is_msm8930aa() ||
Stepan Moskovchenko9c749262012-07-09 19:30:44 -0700238 cpu_is_msm9615() || cpu_is_msm8627() ||
239 cpu_is_msm8960ab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240
241 if (xo_voter->mode == mode)
242 return 0;
243
244 xo->votes[mode]++;
245 xo->votes[xo_voter->mode]--;
246 ret = msm_xo_update_vote(xo);
247 if (ret) {
248 xo->votes[xo_voter->mode]++;
249 xo->votes[mode]--;
250 goto out;
251 }
Stephen Boyd5a190a82012-03-01 14:45:15 -0800252 /* TODO: Remove once RPM separates the concept of D0 and CXO */
253 if (is_d0 && needs_workaround) {
254 static struct clk *xo_clk;
255
256 if (!xo_clk) {
257 xo_clk = clk_get_sys("msm_xo", "xo");
258 BUG_ON(IS_ERR(xo_clk));
259 }
260 /* Ignore transitions from pin to on or vice versa */
261 if (mode && xo_voter->mode == MSM_XO_MODE_OFF)
262 clk_enable(xo_clk);
263 else if (!mode)
264 clk_disable(xo_clk);
265 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 xo_voter->mode = mode;
267out:
268 return ret;
269}
270
271/**
272 * msm_xo_mode_vote() - Vote for an XO to be ON, OFF, or under PIN_CTRL
273 * @xo_voter - Valid handle returned from msm_xo_get()
274 * @mode - Mode to vote for (ON, OFF, PIN_CTRL)
275 *
276 * Vote for an XO to be either ON, OFF, or under PIN_CTRL. Votes are
277 * aggregated with ON taking precedence over PIN_CTRL taking precedence
278 * over OFF.
279 *
280 * This function returns 0 on success or a negative error code on failure.
281 */
282int msm_xo_mode_vote(struct msm_xo_voter *xo_voter, enum msm_xo_modes mode)
283{
284 int ret;
Tianyi Gouc5314912011-10-25 16:44:54 -0700285
286 if (!xo_voter)
Tianyi Gou41515e22011-09-01 19:37:43 -0700287 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288
Tianyi Gouc5314912011-10-25 16:44:54 -0700289 if (mode >= NUM_MSM_XO_MODES || IS_ERR(xo_voter))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290 return -EINVAL;
291
Stephen Boydc8357a92012-04-04 13:54:38 -0700292 mutex_lock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 ret = __msm_xo_mode_vote(xo_voter, mode);
Stephen Boydc8357a92012-04-04 13:54:38 -0700294 mutex_unlock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295
296 return ret;
297}
298EXPORT_SYMBOL(msm_xo_mode_vote);
299
300/**
301 * msm_xo_get() - Get a voting handle for an XO
302 * @xo_id - XO identifier
303 * @voter - Debug string to identify users
304 *
305 * XO voters vote for OFF by default. This function returns a pointer
306 * indicating success. An ERR_PTR is returned on failure.
307 *
308 * If XO voting is disabled, %NULL is returned.
309 */
310struct msm_xo_voter *msm_xo_get(enum msm_xo_ids xo_id, const char *voter)
311{
312 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313 struct msm_xo_voter *xo_voter;
314
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700315 if (xo_id >= NUM_MSM_XO_IDS) {
316 ret = -EINVAL;
317 goto err;
318 }
319
320 xo_voter = kzalloc(sizeof(*xo_voter), GFP_KERNEL);
321 if (!xo_voter) {
322 ret = -ENOMEM;
323 goto err;
324 }
325
326 xo_voter->name = kstrdup(voter, GFP_KERNEL);
327 if (!xo_voter->name) {
328 ret = -ENOMEM;
329 goto err_name;
330 }
331
332 xo_voter->xo = &msm_xo_sources[xo_id];
333
334 /* Voters vote for OFF by default */
Stephen Boydc8357a92012-04-04 13:54:38 -0700335 mutex_lock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700336 xo_voter->xo->votes[MSM_XO_MODE_OFF]++;
337 list_add(&xo_voter->list, &xo_voter->xo->voters);
Stephen Boydc8357a92012-04-04 13:54:38 -0700338 mutex_unlock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339
340 return xo_voter;
341
342err_name:
343 kfree(xo_voter);
344err:
345 return ERR_PTR(ret);
346}
347EXPORT_SYMBOL(msm_xo_get);
348
349/**
350 * msm_xo_put() - Release a voting handle
351 * @xo_voter - Valid handle returned from msm_xo_get()
352 *
353 * Release a reference to an XO voting handle. This also removes the voter's
354 * vote, therefore calling msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF)
355 * beforehand is unnecessary.
356 */
357void msm_xo_put(struct msm_xo_voter *xo_voter)
358{
Tianyi Gouc5314912011-10-25 16:44:54 -0700359 if (!xo_voter || IS_ERR(xo_voter))
360 return;
361
Stephen Boydc8357a92012-04-04 13:54:38 -0700362 mutex_lock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363 __msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF);
364 xo_voter->xo->votes[MSM_XO_MODE_OFF]--;
365 list_del(&xo_voter->list);
Stephen Boydc8357a92012-04-04 13:54:38 -0700366 mutex_unlock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367
368 kfree(xo_voter->name);
369 kfree(xo_voter);
370}
371EXPORT_SYMBOL(msm_xo_put);
372
373int __init msm_xo_init(void)
374{
Stephen Boyd47239d52012-01-26 14:38:40 -0800375 int i, ret;
376 struct msm_rpm_iv_pair cmd[1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377
378 for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++)
379 INIT_LIST_HEAD(&msm_xo_sources[i].voters);
380
Stephen Boyd47239d52012-01-26 14:38:40 -0800381 cmd[0].id = MSM_RPM_ID_CXO_BUFFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382 cmd[0].value = 0;
Stephen Boyd47239d52012-01-26 14:38:40 -0800383 ret = msm_rpmrs_set(MSM_RPM_CTX_SET_0, cmd, ARRAY_SIZE(cmd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384 if (ret)
Stephen Boyd47239d52012-01-26 14:38:40 -0800385 return ret;
Stephen Boyda70c5432012-02-10 14:31:45 -0800386 msm_xo_debugfs_init();
Stephen Boyd47239d52012-01-26 14:38:40 -0800387 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388}