1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/debugfs.h>
#include <linux/list.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/string.h>
#include <linux/clk.h>
#include <mach/msm_xo.h>
#include <mach/rpm.h>
#include <mach/socinfo.h>
#include "rpm_resources.h"
static DEFINE_MUTEX(msm_xo_lock);
struct msm_xo {
unsigned votes[NUM_MSM_XO_MODES];
unsigned mode;
struct list_head voters;
};
struct msm_xo_voter {
const char *name;
unsigned mode;
struct msm_xo *xo;
struct list_head list;
};
static struct msm_xo msm_xo_sources[NUM_MSM_XO_IDS];
#ifdef CONFIG_DEBUG_FS
static const char *msm_xo_to_str[NUM_MSM_XO_IDS] = {
[MSM_XO_TCXO_D0] = "D0",
[MSM_XO_TCXO_D1] = "D1",
[MSM_XO_TCXO_A0] = "A0",
[MSM_XO_TCXO_A1] = "A1",
[MSM_XO_TCXO_A2] = "A2",
[MSM_XO_CORE] = "CORE",
};
static const char *msm_xo_mode_to_str[NUM_MSM_XO_MODES] = {
[MSM_XO_MODE_ON] = "ON",
[MSM_XO_MODE_PIN_CTRL] = "PIN",
[MSM_XO_MODE_OFF] = "OFF",
};
static int msm_xo_debugfs_open(struct inode *inode, struct file *filp)
{
filp->private_data = inode->i_private;
return 0;
}
static ssize_t msm_xo_debugfs_read(struct file *filp, char __user *ubuf,
size_t cnt, loff_t *ppos)
{
int r;
char buf[10];
struct msm_xo_voter *xo = filp->private_data;
r = snprintf(buf, sizeof(buf), "%s\n", msm_xo_mode_to_str[xo->mode]);
return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
}
static ssize_t msm_xo_debugfs_write(struct file *filp,
const char __user *ubuf, size_t cnt, loff_t *ppos)
{
struct msm_xo_voter *xo = filp->private_data;
char buf[10], *b;
int i, ret;
if (cnt > sizeof(buf) - 1)
return -EINVAL;
if (copy_from_user(&buf, ubuf, cnt))
return -EFAULT;
buf[cnt] = '\0';
b = strstrip(buf);
for (i = 0; i < ARRAY_SIZE(msm_xo_mode_to_str); i++)
if (!strncasecmp(b, msm_xo_mode_to_str[i], sizeof(buf))) {
ret = msm_xo_mode_vote(xo, i);
return ret ? : cnt;
}
return -EINVAL;
}
static const struct file_operations msm_xo_debugfs_fops = {
.open = msm_xo_debugfs_open,
.read = msm_xo_debugfs_read,
.write = msm_xo_debugfs_write,
};
static struct dentry *xo_debugfs_root;
static struct msm_xo_voter *xo_debugfs_voters[NUM_MSM_XO_IDS];
static int __init msm_xo_init_debugfs_voters(void)
{
int i;
xo_debugfs_root = debugfs_create_dir("msm_xo", NULL);
if (!xo_debugfs_root)
return -ENOMEM;
for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++) {
xo_debugfs_voters[i] = msm_xo_get(i, "debugfs");
if (IS_ERR(xo_debugfs_voters[i]))
goto err;
debugfs_create_file(msm_xo_to_str[i], S_IRUGO | S_IWUSR,
xo_debugfs_root, xo_debugfs_voters[i],
&msm_xo_debugfs_fops);
}
return 0;
err:
while (--i >= 0)
msm_xo_put(xo_debugfs_voters[i]);
debugfs_remove_recursive(xo_debugfs_root);
return -ENOMEM;
}
static void msm_xo_dump_xo(struct seq_file *m, struct msm_xo *xo,
const char *name)
{
struct msm_xo_voter *voter;
seq_printf(m, "CXO %-16s%s\n", name, msm_xo_mode_to_str[xo->mode]);
list_for_each_entry(voter, &xo->voters, list)
seq_printf(m, " %s %-16s %s\n",
xo->mode == voter->mode ? "*" : " ",
voter->name,
msm_xo_mode_to_str[voter->mode]);
}
static int msm_xo_show_voters(struct seq_file *m, void *v)
{
int i;
mutex_lock(&msm_xo_lock);
for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++)
msm_xo_dump_xo(m, &msm_xo_sources[i], msm_xo_to_str[i]);
mutex_unlock(&msm_xo_lock);
return 0;
}
static int msm_xo_voters_open(struct inode *inode, struct file *file)
{
return single_open(file, msm_xo_show_voters, inode->i_private);
}
static const struct file_operations msm_xo_voters_ops = {
.open = msm_xo_voters_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
static int __init msm_xo_debugfs_init(void)
{
msm_xo_init_debugfs_voters();
if (!debugfs_create_file("xo_voters", S_IRUGO, NULL, NULL,
&msm_xo_voters_ops))
return -ENOMEM;
return 0;
}
#else
static int __init msm_xo_debugfs_init(void) { return 0; }
#endif
static int msm_xo_update_vote(struct msm_xo *xo)
{
int ret;
unsigned vote, prev_vote = xo->mode;
struct msm_rpm_iv_pair cmd;
if (xo->votes[MSM_XO_MODE_ON])
vote = MSM_XO_MODE_ON;
else if (xo->votes[MSM_XO_MODE_PIN_CTRL])
vote = MSM_XO_MODE_PIN_CTRL;
else
vote = MSM_XO_MODE_OFF;
if (vote == prev_vote)
return 0;
/*
* Change the vote here to simplify the TCXO logic. If the RPM
* command fails we'll rollback.
*/
xo->mode = vote;
cmd.id = MSM_RPM_ID_CXO_BUFFERS;
cmd.value = (msm_xo_sources[MSM_XO_TCXO_D0].mode << 0) |
(msm_xo_sources[MSM_XO_TCXO_D1].mode << 8) |
(msm_xo_sources[MSM_XO_TCXO_A0].mode << 16) |
(msm_xo_sources[MSM_XO_TCXO_A1].mode << 24) |
(msm_xo_sources[MSM_XO_TCXO_A2].mode << 28) |
/*
* 8660 RPM has XO_CORE at bit 18 and 8960 RPM has
* XO_CORE at bit 20. Since the opposite bit is
* reserved in both cases, just set both and be
* done with it.
*/
((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 20) |
((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 18);
ret = msm_rpm_set(MSM_RPM_CTX_SET_0, &cmd, 1);
if (ret)
xo->mode = prev_vote;
return ret;
}
static int __msm_xo_mode_vote(struct msm_xo_voter *xo_voter, unsigned mode)
{
int ret;
struct msm_xo *xo = xo_voter->xo;
int is_d0 = xo == &msm_xo_sources[MSM_XO_TCXO_D0];
int needs_workaround = soc_class_is_msm8960() ||
soc_class_is_apq8064() ||
soc_class_is_msm8930() || cpu_is_msm9615();
if (xo_voter->mode == mode)
return 0;
xo->votes[mode]++;
xo->votes[xo_voter->mode]--;
ret = msm_xo_update_vote(xo);
if (ret) {
xo->votes[xo_voter->mode]++;
xo->votes[mode]--;
goto out;
}
/* TODO: Remove once RPM separates the concept of D0 and CXO */
if (is_d0 && needs_workaround) {
static struct clk *xo_clk;
if (!xo_clk) {
xo_clk = clk_get_sys("msm_xo", "xo");
BUG_ON(IS_ERR(xo_clk));
}
/* Ignore transitions from pin to on or vice versa */
if (mode && xo_voter->mode == MSM_XO_MODE_OFF)
clk_prepare_enable(xo_clk);
else if (!mode)
clk_disable_unprepare(xo_clk);
}
xo_voter->mode = mode;
out:
return ret;
}
/**
* msm_xo_mode_vote() - Vote for an XO to be ON, OFF, or under PIN_CTRL
* @xo_voter - Valid handle returned from msm_xo_get()
* @mode - Mode to vote for (ON, OFF, PIN_CTRL)
*
* Vote for an XO to be either ON, OFF, or under PIN_CTRL. Votes are
* aggregated with ON taking precedence over PIN_CTRL taking precedence
* over OFF.
*
* This function returns 0 on success or a negative error code on failure.
*/
int msm_xo_mode_vote(struct msm_xo_voter *xo_voter, enum msm_xo_modes mode)
{
int ret;
if (!xo_voter)
return 0;
if (mode >= NUM_MSM_XO_MODES || IS_ERR(xo_voter))
return -EINVAL;
mutex_lock(&msm_xo_lock);
ret = __msm_xo_mode_vote(xo_voter, mode);
mutex_unlock(&msm_xo_lock);
return ret;
}
EXPORT_SYMBOL(msm_xo_mode_vote);
/**
* msm_xo_get() - Get a voting handle for an XO
* @xo_id - XO identifier
* @voter - Debug string to identify users
*
* XO voters vote for OFF by default. This function returns a pointer
* indicating success. An ERR_PTR is returned on failure.
*
* If XO voting is disabled, %NULL is returned.
*/
struct msm_xo_voter *msm_xo_get(enum msm_xo_ids xo_id, const char *voter)
{
int ret;
struct msm_xo_voter *xo_voter;
if (xo_id >= NUM_MSM_XO_IDS) {
ret = -EINVAL;
goto err;
}
xo_voter = kzalloc(sizeof(*xo_voter), GFP_KERNEL);
if (!xo_voter) {
ret = -ENOMEM;
goto err;
}
xo_voter->name = kstrdup(voter, GFP_KERNEL);
if (!xo_voter->name) {
ret = -ENOMEM;
goto err_name;
}
xo_voter->xo = &msm_xo_sources[xo_id];
/* Voters vote for OFF by default */
mutex_lock(&msm_xo_lock);
xo_voter->xo->votes[MSM_XO_MODE_OFF]++;
list_add(&xo_voter->list, &xo_voter->xo->voters);
mutex_unlock(&msm_xo_lock);
return xo_voter;
err_name:
kfree(xo_voter);
err:
return ERR_PTR(ret);
}
EXPORT_SYMBOL(msm_xo_get);
/**
* msm_xo_put() - Release a voting handle
* @xo_voter - Valid handle returned from msm_xo_get()
*
* Release a reference to an XO voting handle. This also removes the voter's
* vote, therefore calling msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF)
* beforehand is unnecessary.
*/
void msm_xo_put(struct msm_xo_voter *xo_voter)
{
if (!xo_voter || IS_ERR(xo_voter))
return;
mutex_lock(&msm_xo_lock);
__msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF);
xo_voter->xo->votes[MSM_XO_MODE_OFF]--;
list_del(&xo_voter->list);
mutex_unlock(&msm_xo_lock);
kfree(xo_voter->name);
kfree(xo_voter);
}
EXPORT_SYMBOL(msm_xo_put);
int __init msm_xo_init(void)
{
int i, ret;
struct msm_rpm_iv_pair cmd[1];
for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++)
INIT_LIST_HEAD(&msm_xo_sources[i].voters);
cmd[0].id = MSM_RPM_ID_CXO_BUFFERS;
cmd[0].value = 0;
ret = msm_rpmrs_set(MSM_RPM_CTX_SET_0, cmd, ARRAY_SIZE(cmd));
if (ret)
return ret;
msm_xo_debugfs_init();
return 0;
}
|