blob: 46d4a12a7df6e26cccc3c378140779ac21657537 [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];
Stepan Moskovchenko5b9e7762012-09-21 20:32:17 -0700236 int needs_workaround = soc_class_is_msm8960() ||
237 soc_class_is_apq8064() ||
238 soc_class_is_msm8930() || cpu_is_msm9615();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239
240 if (xo_voter->mode == mode)
241 return 0;
242
243 xo->votes[mode]++;
244 xo->votes[xo_voter->mode]--;
245 ret = msm_xo_update_vote(xo);
246 if (ret) {
247 xo->votes[xo_voter->mode]++;
248 xo->votes[mode]--;
249 goto out;
250 }
Stephen Boyd5a190a82012-03-01 14:45:15 -0800251 /* TODO: Remove once RPM separates the concept of D0 and CXO */
252 if (is_d0 && needs_workaround) {
253 static struct clk *xo_clk;
254
255 if (!xo_clk) {
256 xo_clk = clk_get_sys("msm_xo", "xo");
257 BUG_ON(IS_ERR(xo_clk));
258 }
259 /* Ignore transitions from pin to on or vice versa */
260 if (mode && xo_voter->mode == MSM_XO_MODE_OFF)
Stephen Boyd9f70b9b2012-08-03 11:41:27 -0700261 clk_prepare_enable(xo_clk);
Stephen Boyd5a190a82012-03-01 14:45:15 -0800262 else if (!mode)
Stephen Boyd9f70b9b2012-08-03 11:41:27 -0700263 clk_disable_unprepare(xo_clk);
Stephen Boyd5a190a82012-03-01 14:45:15 -0800264 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265 xo_voter->mode = mode;
266out:
267 return ret;
268}
269
270/**
271 * msm_xo_mode_vote() - Vote for an XO to be ON, OFF, or under PIN_CTRL
272 * @xo_voter - Valid handle returned from msm_xo_get()
273 * @mode - Mode to vote for (ON, OFF, PIN_CTRL)
274 *
275 * Vote for an XO to be either ON, OFF, or under PIN_CTRL. Votes are
276 * aggregated with ON taking precedence over PIN_CTRL taking precedence
277 * over OFF.
278 *
279 * This function returns 0 on success or a negative error code on failure.
280 */
281int msm_xo_mode_vote(struct msm_xo_voter *xo_voter, enum msm_xo_modes mode)
282{
283 int ret;
Tianyi Gouc5314912011-10-25 16:44:54 -0700284
285 if (!xo_voter)
Tianyi Gou41515e22011-09-01 19:37:43 -0700286 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287
Tianyi Gouc5314912011-10-25 16:44:54 -0700288 if (mode >= NUM_MSM_XO_MODES || IS_ERR(xo_voter))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 return -EINVAL;
290
Stephen Boydc8357a92012-04-04 13:54:38 -0700291 mutex_lock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 ret = __msm_xo_mode_vote(xo_voter, mode);
Stephen Boydc8357a92012-04-04 13:54:38 -0700293 mutex_unlock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294
295 return ret;
296}
297EXPORT_SYMBOL(msm_xo_mode_vote);
298
299/**
300 * msm_xo_get() - Get a voting handle for an XO
301 * @xo_id - XO identifier
302 * @voter - Debug string to identify users
303 *
304 * XO voters vote for OFF by default. This function returns a pointer
305 * indicating success. An ERR_PTR is returned on failure.
306 *
307 * If XO voting is disabled, %NULL is returned.
308 */
309struct msm_xo_voter *msm_xo_get(enum msm_xo_ids xo_id, const char *voter)
310{
311 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700312 struct msm_xo_voter *xo_voter;
313
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314 if (xo_id >= NUM_MSM_XO_IDS) {
315 ret = -EINVAL;
316 goto err;
317 }
318
319 xo_voter = kzalloc(sizeof(*xo_voter), GFP_KERNEL);
320 if (!xo_voter) {
321 ret = -ENOMEM;
322 goto err;
323 }
324
325 xo_voter->name = kstrdup(voter, GFP_KERNEL);
326 if (!xo_voter->name) {
327 ret = -ENOMEM;
328 goto err_name;
329 }
330
331 xo_voter->xo = &msm_xo_sources[xo_id];
332
333 /* Voters vote for OFF by default */
Stephen Boydc8357a92012-04-04 13:54:38 -0700334 mutex_lock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 xo_voter->xo->votes[MSM_XO_MODE_OFF]++;
336 list_add(&xo_voter->list, &xo_voter->xo->voters);
Stephen Boydc8357a92012-04-04 13:54:38 -0700337 mutex_unlock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338
339 return xo_voter;
340
341err_name:
342 kfree(xo_voter);
343err:
344 return ERR_PTR(ret);
345}
346EXPORT_SYMBOL(msm_xo_get);
347
348/**
349 * msm_xo_put() - Release a voting handle
350 * @xo_voter - Valid handle returned from msm_xo_get()
351 *
352 * Release a reference to an XO voting handle. This also removes the voter's
353 * vote, therefore calling msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF)
354 * beforehand is unnecessary.
355 */
356void msm_xo_put(struct msm_xo_voter *xo_voter)
357{
Tianyi Gouc5314912011-10-25 16:44:54 -0700358 if (!xo_voter || IS_ERR(xo_voter))
359 return;
360
Stephen Boydc8357a92012-04-04 13:54:38 -0700361 mutex_lock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362 __msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF);
363 xo_voter->xo->votes[MSM_XO_MODE_OFF]--;
364 list_del(&xo_voter->list);
Stephen Boydc8357a92012-04-04 13:54:38 -0700365 mutex_unlock(&msm_xo_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366
367 kfree(xo_voter->name);
368 kfree(xo_voter);
369}
370EXPORT_SYMBOL(msm_xo_put);
371
372int __init msm_xo_init(void)
373{
Stephen Boyd47239d52012-01-26 14:38:40 -0800374 int i, ret;
375 struct msm_rpm_iv_pair cmd[1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376
377 for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++)
378 INIT_LIST_HEAD(&msm_xo_sources[i].voters);
379
Stephen Boyd47239d52012-01-26 14:38:40 -0800380 cmd[0].id = MSM_RPM_ID_CXO_BUFFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 cmd[0].value = 0;
Stephen Boyd47239d52012-01-26 14:38:40 -0800382 ret = msm_rpmrs_set(MSM_RPM_CTX_SET_0, cmd, ARRAY_SIZE(cmd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383 if (ret)
Stephen Boyd47239d52012-01-26 14:38:40 -0800384 return ret;
Stephen Boyda70c5432012-02-10 14:31:45 -0800385 msm_xo_debugfs_init();
Stephen Boyd47239d52012-01-26 14:38:40 -0800386 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387}