Stephen Boyd | 0275f93 | 2012-01-23 18:42:35 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 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 Gou | 41515e2 | 2011-09-01 19:37:43 -0700 | [diff] [blame] | 25 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 26 | |
| 27 | #include "rpm_resources.h" |
| 28 | |
| 29 | static DEFINE_SPINLOCK(msm_xo_lock); |
| 30 | |
| 31 | struct msm_xo { |
| 32 | unsigned votes[NUM_MSM_XO_MODES]; |
| 33 | unsigned mode; |
| 34 | struct list_head voters; |
| 35 | }; |
| 36 | |
| 37 | struct msm_xo_voter { |
| 38 | const char *name; |
| 39 | unsigned mode; |
| 40 | struct msm_xo *xo; |
| 41 | struct list_head list; |
| 42 | }; |
| 43 | |
| 44 | static struct msm_xo msm_xo_sources[NUM_MSM_XO_IDS]; |
| 45 | |
| 46 | #ifdef CONFIG_DEBUG_FS |
| 47 | static 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 | |
| 61 | static 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 | |
| 74 | static 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"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 85 | spin_unlock_irqrestore(&msm_xo_lock, flags); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int msm_xo_voters_open(struct inode *inode, struct file *file) |
| 91 | { |
| 92 | return single_open(file, msm_xo_show_voters, inode->i_private); |
| 93 | } |
| 94 | |
| 95 | static const struct file_operations msm_xo_voters_ops = { |
| 96 | .open = msm_xo_voters_open, |
| 97 | .read = seq_read, |
| 98 | .llseek = seq_lseek, |
| 99 | .release = seq_release, |
| 100 | }; |
| 101 | |
| 102 | static int __init msm_xo_debugfs_init(void) |
| 103 | { |
| 104 | struct dentry *entry; |
| 105 | |
| 106 | entry = debugfs_create_file("xo_voters", S_IRUGO, NULL, NULL, |
| 107 | &msm_xo_voters_ops); |
| 108 | return IS_ERR(entry) ? PTR_ERR(entry) : 0; |
| 109 | } |
Stephen Boyd | a70c543 | 2012-02-10 14:31:45 -0800 | [diff] [blame] | 110 | #else |
| 111 | static int __init msm_xo_debugfs_init(void) { return 0; } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 112 | #endif |
| 113 | |
| 114 | static int msm_xo_update_vote(struct msm_xo *xo) |
| 115 | { |
| 116 | int ret; |
Stephen Boyd | 47239d5 | 2012-01-26 14:38:40 -0800 | [diff] [blame^] | 117 | unsigned vote, prev_vote = xo->mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 118 | struct msm_rpm_iv_pair cmd; |
| 119 | |
| 120 | if (xo->votes[MSM_XO_MODE_ON]) |
| 121 | vote = MSM_XO_MODE_ON; |
| 122 | else if (xo->votes[MSM_XO_MODE_PIN_CTRL]) |
| 123 | vote = MSM_XO_MODE_PIN_CTRL; |
| 124 | else |
| 125 | vote = MSM_XO_MODE_OFF; |
| 126 | |
| 127 | if (vote == prev_vote) |
| 128 | return 0; |
| 129 | |
| 130 | /* |
| 131 | * Change the vote here to simplify the TCXO logic. If the RPM |
| 132 | * command fails we'll rollback. |
| 133 | */ |
| 134 | xo->mode = vote; |
Stephen Boyd | 47239d5 | 2012-01-26 14:38:40 -0800 | [diff] [blame^] | 135 | cmd.id = MSM_RPM_ID_CXO_BUFFERS; |
| 136 | cmd.value = (msm_xo_sources[MSM_XO_TCXO_D0].mode << 0) | |
| 137 | (msm_xo_sources[MSM_XO_TCXO_D1].mode << 8) | |
| 138 | (msm_xo_sources[MSM_XO_TCXO_A0].mode << 16) | |
| 139 | (msm_xo_sources[MSM_XO_TCXO_A1].mode << 24) | |
| 140 | (msm_xo_sources[MSM_XO_TCXO_A2].mode << 28) | |
| 141 | /* |
| 142 | * 8660 RPM has XO_CORE at bit 18 and 8960 RPM has |
| 143 | * XO_CORE at bit 20. Since the opposite bit is |
| 144 | * reserved in both cases, just set both and be |
| 145 | * done with it. |
| 146 | */ |
| 147 | ((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 20) | |
| 148 | ((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 18); |
| 149 | ret = msm_rpm_set_noirq(MSM_RPM_CTX_SET_0, &cmd, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 150 | |
| 151 | if (ret) |
| 152 | xo->mode = prev_vote; |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | static int __msm_xo_mode_vote(struct msm_xo_voter *xo_voter, unsigned mode) |
| 158 | { |
| 159 | int ret; |
| 160 | struct msm_xo *xo = xo_voter->xo; |
| 161 | |
| 162 | if (xo_voter->mode == mode) |
| 163 | return 0; |
| 164 | |
| 165 | xo->votes[mode]++; |
| 166 | xo->votes[xo_voter->mode]--; |
| 167 | ret = msm_xo_update_vote(xo); |
| 168 | if (ret) { |
| 169 | xo->votes[xo_voter->mode]++; |
| 170 | xo->votes[mode]--; |
| 171 | goto out; |
| 172 | } |
| 173 | xo_voter->mode = mode; |
| 174 | out: |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * msm_xo_mode_vote() - Vote for an XO to be ON, OFF, or under PIN_CTRL |
| 180 | * @xo_voter - Valid handle returned from msm_xo_get() |
| 181 | * @mode - Mode to vote for (ON, OFF, PIN_CTRL) |
| 182 | * |
| 183 | * Vote for an XO to be either ON, OFF, or under PIN_CTRL. Votes are |
| 184 | * aggregated with ON taking precedence over PIN_CTRL taking precedence |
| 185 | * over OFF. |
| 186 | * |
| 187 | * This function returns 0 on success or a negative error code on failure. |
| 188 | */ |
| 189 | int msm_xo_mode_vote(struct msm_xo_voter *xo_voter, enum msm_xo_modes mode) |
| 190 | { |
| 191 | int ret; |
| 192 | unsigned long flags; |
Tianyi Gou | c531491 | 2011-10-25 16:44:54 -0700 | [diff] [blame] | 193 | |
| 194 | if (!xo_voter) |
Tianyi Gou | 41515e2 | 2011-09-01 19:37:43 -0700 | [diff] [blame] | 195 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 196 | |
Tianyi Gou | c531491 | 2011-10-25 16:44:54 -0700 | [diff] [blame] | 197 | if (mode >= NUM_MSM_XO_MODES || IS_ERR(xo_voter)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 198 | return -EINVAL; |
| 199 | |
| 200 | spin_lock_irqsave(&msm_xo_lock, flags); |
| 201 | ret = __msm_xo_mode_vote(xo_voter, mode); |
| 202 | spin_unlock_irqrestore(&msm_xo_lock, flags); |
| 203 | |
| 204 | return ret; |
| 205 | } |
| 206 | EXPORT_SYMBOL(msm_xo_mode_vote); |
| 207 | |
| 208 | /** |
| 209 | * msm_xo_get() - Get a voting handle for an XO |
| 210 | * @xo_id - XO identifier |
| 211 | * @voter - Debug string to identify users |
| 212 | * |
| 213 | * XO voters vote for OFF by default. This function returns a pointer |
| 214 | * indicating success. An ERR_PTR is returned on failure. |
| 215 | * |
| 216 | * If XO voting is disabled, %NULL is returned. |
| 217 | */ |
| 218 | struct msm_xo_voter *msm_xo_get(enum msm_xo_ids xo_id, const char *voter) |
| 219 | { |
| 220 | int ret; |
| 221 | unsigned long flags; |
| 222 | struct msm_xo_voter *xo_voter; |
| 223 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 224 | if (xo_id >= NUM_MSM_XO_IDS) { |
| 225 | ret = -EINVAL; |
| 226 | goto err; |
| 227 | } |
| 228 | |
| 229 | xo_voter = kzalloc(sizeof(*xo_voter), GFP_KERNEL); |
| 230 | if (!xo_voter) { |
| 231 | ret = -ENOMEM; |
| 232 | goto err; |
| 233 | } |
| 234 | |
| 235 | xo_voter->name = kstrdup(voter, GFP_KERNEL); |
| 236 | if (!xo_voter->name) { |
| 237 | ret = -ENOMEM; |
| 238 | goto err_name; |
| 239 | } |
| 240 | |
| 241 | xo_voter->xo = &msm_xo_sources[xo_id]; |
| 242 | |
| 243 | /* Voters vote for OFF by default */ |
| 244 | spin_lock_irqsave(&msm_xo_lock, flags); |
| 245 | xo_voter->xo->votes[MSM_XO_MODE_OFF]++; |
| 246 | list_add(&xo_voter->list, &xo_voter->xo->voters); |
| 247 | spin_unlock_irqrestore(&msm_xo_lock, flags); |
| 248 | |
| 249 | return xo_voter; |
| 250 | |
| 251 | err_name: |
| 252 | kfree(xo_voter); |
| 253 | err: |
| 254 | return ERR_PTR(ret); |
| 255 | } |
| 256 | EXPORT_SYMBOL(msm_xo_get); |
| 257 | |
| 258 | /** |
| 259 | * msm_xo_put() - Release a voting handle |
| 260 | * @xo_voter - Valid handle returned from msm_xo_get() |
| 261 | * |
| 262 | * Release a reference to an XO voting handle. This also removes the voter's |
| 263 | * vote, therefore calling msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF) |
| 264 | * beforehand is unnecessary. |
| 265 | */ |
| 266 | void msm_xo_put(struct msm_xo_voter *xo_voter) |
| 267 | { |
| 268 | unsigned long flags; |
| 269 | |
Tianyi Gou | c531491 | 2011-10-25 16:44:54 -0700 | [diff] [blame] | 270 | if (!xo_voter || IS_ERR(xo_voter)) |
| 271 | return; |
| 272 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 273 | spin_lock_irqsave(&msm_xo_lock, flags); |
| 274 | __msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF); |
| 275 | xo_voter->xo->votes[MSM_XO_MODE_OFF]--; |
| 276 | list_del(&xo_voter->list); |
| 277 | spin_unlock_irqrestore(&msm_xo_lock, flags); |
| 278 | |
| 279 | kfree(xo_voter->name); |
| 280 | kfree(xo_voter); |
| 281 | } |
| 282 | EXPORT_SYMBOL(msm_xo_put); |
| 283 | |
| 284 | int __init msm_xo_init(void) |
| 285 | { |
Stephen Boyd | 47239d5 | 2012-01-26 14:38:40 -0800 | [diff] [blame^] | 286 | int i, ret; |
| 287 | struct msm_rpm_iv_pair cmd[1]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 288 | |
| 289 | for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++) |
| 290 | INIT_LIST_HEAD(&msm_xo_sources[i].voters); |
| 291 | |
Stephen Boyd | 47239d5 | 2012-01-26 14:38:40 -0800 | [diff] [blame^] | 292 | cmd[0].id = MSM_RPM_ID_CXO_BUFFERS; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 293 | cmd[0].value = 0; |
Stephen Boyd | 47239d5 | 2012-01-26 14:38:40 -0800 | [diff] [blame^] | 294 | ret = msm_rpmrs_set(MSM_RPM_CTX_SET_0, cmd, ARRAY_SIZE(cmd)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 295 | if (ret) |
Stephen Boyd | 47239d5 | 2012-01-26 14:38:40 -0800 | [diff] [blame^] | 296 | return ret; |
Stephen Boyd | a70c543 | 2012-02-10 14:31:45 -0800 | [diff] [blame] | 297 | msm_xo_debugfs_init(); |
Stephen Boyd | 47239d5 | 2012-01-26 14:38:40 -0800 | [diff] [blame^] | 298 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 299 | } |