Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* 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 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"); |
| 85 | msm_xo_dump_xo(m, &msm_xo_sources[MSM_XO_PXO], "PXO during sleep"); |
| 86 | spin_unlock_irqrestore(&msm_xo_lock, flags); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int msm_xo_voters_open(struct inode *inode, struct file *file) |
| 92 | { |
| 93 | return single_open(file, msm_xo_show_voters, inode->i_private); |
| 94 | } |
| 95 | |
| 96 | static const struct file_operations msm_xo_voters_ops = { |
| 97 | .open = msm_xo_voters_open, |
| 98 | .read = seq_read, |
| 99 | .llseek = seq_lseek, |
| 100 | .release = seq_release, |
| 101 | }; |
| 102 | |
| 103 | static int __init msm_xo_debugfs_init(void) |
| 104 | { |
| 105 | struct dentry *entry; |
| 106 | |
| 107 | entry = debugfs_create_file("xo_voters", S_IRUGO, NULL, NULL, |
| 108 | &msm_xo_voters_ops); |
| 109 | return IS_ERR(entry) ? PTR_ERR(entry) : 0; |
| 110 | } |
| 111 | late_initcall(msm_xo_debugfs_init); |
| 112 | #endif |
| 113 | |
| 114 | static int msm_xo_update_vote(struct msm_xo *xo) |
| 115 | { |
| 116 | int ret; |
| 117 | unsigned vote, prev_vote = xo->mode; |
| 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; |
| 135 | |
| 136 | if (xo == &msm_xo_sources[MSM_XO_PXO]) { |
| 137 | cmd.id = MSM_RPM_ID_PXO_CLK; |
| 138 | cmd.value = msm_xo_sources[MSM_XO_PXO].mode ? 1 : 0; |
| 139 | ret = msm_rpmrs_set_noirq(MSM_RPM_CTX_SET_SLEEP, &cmd, 1); |
| 140 | } else { |
| 141 | cmd.id = MSM_RPM_ID_CXO_BUFFERS; |
| 142 | cmd.value = (msm_xo_sources[MSM_XO_TCXO_D0].mode << 0) | |
| 143 | (msm_xo_sources[MSM_XO_TCXO_D1].mode << 8) | |
| 144 | (msm_xo_sources[MSM_XO_TCXO_A0].mode << 16) | |
| 145 | (msm_xo_sources[MSM_XO_TCXO_A1].mode << 24) | |
| 146 | (msm_xo_sources[MSM_XO_TCXO_A2].mode << 28) | |
| 147 | ((msm_xo_sources[MSM_XO_CORE].mode ? 1 : 0) << 20); |
| 148 | ret = msm_rpm_set_noirq(MSM_RPM_CTX_SET_0, &cmd, 1); |
| 149 | } |
| 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 | |
Tianyi Gou | c531491 | 2011-10-25 16:44:54 -0700 | [diff] [blame] | 224 | /* |
| 225 | * TODO: Remove early return for 8064 once RPM XO voting support |
| 226 | * is available. |
| 227 | */ |
Tianyi Gou | 41515e2 | 2011-09-01 19:37:43 -0700 | [diff] [blame] | 228 | if (cpu_is_apq8064()) |
| 229 | return NULL; |
| 230 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 231 | if (xo_id >= NUM_MSM_XO_IDS) { |
| 232 | ret = -EINVAL; |
| 233 | goto err; |
| 234 | } |
| 235 | |
| 236 | xo_voter = kzalloc(sizeof(*xo_voter), GFP_KERNEL); |
| 237 | if (!xo_voter) { |
| 238 | ret = -ENOMEM; |
| 239 | goto err; |
| 240 | } |
| 241 | |
| 242 | xo_voter->name = kstrdup(voter, GFP_KERNEL); |
| 243 | if (!xo_voter->name) { |
| 244 | ret = -ENOMEM; |
| 245 | goto err_name; |
| 246 | } |
| 247 | |
| 248 | xo_voter->xo = &msm_xo_sources[xo_id]; |
| 249 | |
| 250 | /* Voters vote for OFF by default */ |
| 251 | spin_lock_irqsave(&msm_xo_lock, flags); |
| 252 | xo_voter->xo->votes[MSM_XO_MODE_OFF]++; |
| 253 | list_add(&xo_voter->list, &xo_voter->xo->voters); |
| 254 | spin_unlock_irqrestore(&msm_xo_lock, flags); |
| 255 | |
| 256 | return xo_voter; |
| 257 | |
| 258 | err_name: |
| 259 | kfree(xo_voter); |
| 260 | err: |
| 261 | return ERR_PTR(ret); |
| 262 | } |
| 263 | EXPORT_SYMBOL(msm_xo_get); |
| 264 | |
| 265 | /** |
| 266 | * msm_xo_put() - Release a voting handle |
| 267 | * @xo_voter - Valid handle returned from msm_xo_get() |
| 268 | * |
| 269 | * Release a reference to an XO voting handle. This also removes the voter's |
| 270 | * vote, therefore calling msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF) |
| 271 | * beforehand is unnecessary. |
| 272 | */ |
| 273 | void msm_xo_put(struct msm_xo_voter *xo_voter) |
| 274 | { |
| 275 | unsigned long flags; |
| 276 | |
Tianyi Gou | c531491 | 2011-10-25 16:44:54 -0700 | [diff] [blame] | 277 | if (!xo_voter || IS_ERR(xo_voter)) |
| 278 | return; |
| 279 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 280 | spin_lock_irqsave(&msm_xo_lock, flags); |
| 281 | __msm_xo_mode_vote(xo_voter, MSM_XO_MODE_OFF); |
| 282 | xo_voter->xo->votes[MSM_XO_MODE_OFF]--; |
| 283 | list_del(&xo_voter->list); |
| 284 | spin_unlock_irqrestore(&msm_xo_lock, flags); |
| 285 | |
| 286 | kfree(xo_voter->name); |
| 287 | kfree(xo_voter); |
| 288 | } |
| 289 | EXPORT_SYMBOL(msm_xo_put); |
| 290 | |
| 291 | int __init msm_xo_init(void) |
| 292 | { |
| 293 | int i; |
| 294 | int ret; |
| 295 | struct msm_rpm_iv_pair cmd[2]; |
| 296 | |
| 297 | for (i = 0; i < ARRAY_SIZE(msm_xo_sources); i++) |
| 298 | INIT_LIST_HEAD(&msm_xo_sources[i].voters); |
| 299 | |
| 300 | cmd[0].id = MSM_RPM_ID_PXO_CLK; |
| 301 | cmd[0].value = 1; |
| 302 | cmd[1].id = MSM_RPM_ID_CXO_BUFFERS; |
| 303 | cmd[1].value = 0; |
| 304 | ret = msm_rpmrs_set(MSM_RPM_CTX_SET_0, cmd, 2); |
| 305 | if (ret) |
| 306 | goto out; |
| 307 | |
| 308 | cmd[0].id = MSM_RPM_ID_PXO_CLK; |
| 309 | cmd[0].value = 0; |
| 310 | ret = msm_rpmrs_set(MSM_RPM_CTX_SET_SLEEP, cmd, 1); |
| 311 | if (ret) |
| 312 | goto out; |
| 313 | out: |
| 314 | return ret; |
| 315 | } |