Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 1 | /* arch/arm/mach-msm/vreg.c |
| 2 | * |
| 3 | * Copyright (C) 2008 Google, Inc. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 4 | * Copyright (c) 2009-2011 Code Aurora Forum. All rights reserved. |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 5 | * Author: Brian Swetland <swetland@google.com> |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/init.h> |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | #include <linux/list.h> |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/atomic.h> |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 25 | #include <linux/debugfs.h> |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 26 | #include <linux/regulator/consumer.h> |
Matt Wilson | 0e44106 | 2009-06-29 14:13:04 -0500 | [diff] [blame] | 27 | #include <linux/string.h> |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 28 | #include <mach/vreg.h> |
| 29 | |
| 30 | #include "proc_comm.h" |
| 31 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 32 | #if defined(CONFIG_MSM_VREG_SWITCH_INVERTED) |
| 33 | #define VREG_SWITCH_ENABLE 0 |
| 34 | #define VREG_SWITCH_DISABLE 1 |
| 35 | #else |
| 36 | #define VREG_SWITCH_ENABLE 1 |
| 37 | #define VREG_SWITCH_DISABLE 0 |
| 38 | #endif |
| 39 | |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 40 | struct vreg { |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 41 | struct list_head list; |
| 42 | struct mutex lock; |
| 43 | const char *name; |
| 44 | u64 refcnt; |
| 45 | unsigned mv; |
| 46 | struct regulator *reg; |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 49 | static LIST_HEAD(vreg_list); |
| 50 | static DEFINE_MUTEX(vreg_lock); |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 51 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 52 | #ifdef CONFIG_DEBUG_FS |
| 53 | static void vreg_add_debugfs(struct vreg *vreg); |
| 54 | #else |
| 55 | static inline void vreg_add_debugfs(struct vreg *vreg) { } |
| 56 | #endif |
| 57 | |
| 58 | static struct vreg *vreg_create(const char *id) |
| 59 | { |
| 60 | int rc; |
| 61 | struct vreg *vreg; |
| 62 | |
| 63 | vreg = kzalloc(sizeof(*vreg), GFP_KERNEL); |
| 64 | if (!vreg) { |
| 65 | rc = -ENOMEM; |
| 66 | goto error; |
| 67 | } |
| 68 | |
| 69 | INIT_LIST_HEAD(&vreg->list); |
| 70 | mutex_init(&vreg->lock); |
| 71 | |
| 72 | vreg->reg = regulator_get(NULL, id); |
| 73 | if (IS_ERR(vreg->reg)) { |
| 74 | rc = PTR_ERR(vreg->reg); |
| 75 | goto free_vreg; |
| 76 | } |
| 77 | |
| 78 | vreg->name = kstrdup(id, GFP_KERNEL); |
| 79 | if (!vreg->name) { |
| 80 | rc = -ENOMEM; |
| 81 | goto put_reg; |
| 82 | } |
| 83 | |
| 84 | list_add_tail(&vreg->list, &vreg_list); |
| 85 | vreg_add_debugfs(vreg); |
| 86 | |
| 87 | return vreg; |
| 88 | |
| 89 | put_reg: |
| 90 | regulator_put(vreg->reg); |
| 91 | free_vreg: |
| 92 | kfree(vreg); |
| 93 | error: |
| 94 | return ERR_PTR(rc); |
| 95 | } |
| 96 | |
| 97 | static void vreg_destroy(struct vreg *vreg) |
| 98 | { |
| 99 | if (!vreg) |
| 100 | return; |
| 101 | |
| 102 | if (vreg->refcnt) |
| 103 | regulator_disable(vreg->reg); |
| 104 | |
| 105 | kfree(vreg->name); |
| 106 | regulator_put(vreg->reg); |
| 107 | kfree(vreg); |
| 108 | } |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 109 | |
| 110 | struct vreg *vreg_get(struct device *dev, const char *id) |
| 111 | { |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 112 | struct vreg *vreg = NULL; |
| 113 | |
| 114 | if (!id) |
| 115 | return ERR_PTR(-EINVAL); |
| 116 | |
| 117 | mutex_lock(&vreg_lock); |
| 118 | list_for_each_entry(vreg, &vreg_list, list) { |
| 119 | if (!strncmp(vreg->name, id, 10)) |
| 120 | goto ret; |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 121 | } |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 122 | |
| 123 | vreg = vreg_create(id); |
| 124 | |
| 125 | ret: |
| 126 | mutex_unlock(&vreg_lock); |
| 127 | return vreg; |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 128 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 129 | EXPORT_SYMBOL(vreg_get); |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 130 | |
| 131 | void vreg_put(struct vreg *vreg) |
| 132 | { |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 133 | kfree(vreg->name); |
| 134 | regulator_put(vreg->reg); |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int vreg_enable(struct vreg *vreg) |
| 138 | { |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 139 | int rc = 0; |
| 140 | if (!vreg) |
| 141 | return -ENODEV; |
Steve Muckle | 4783de9 | 2008-12-12 19:33:55 -0500 | [diff] [blame] | 142 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 143 | mutex_lock(&vreg->lock); |
| 144 | if (vreg->refcnt == 0) { |
| 145 | rc = regulator_enable(vreg->reg); |
| 146 | if (!rc) |
| 147 | vreg->refcnt++; |
| 148 | } else { |
| 149 | rc = 0; |
| 150 | if (vreg->refcnt < UINT_MAX) |
| 151 | vreg->refcnt++; |
| 152 | } |
| 153 | mutex_unlock(&vreg->lock); |
Matt Wilson | 0e44106 | 2009-06-29 14:13:04 -0500 | [diff] [blame] | 154 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 155 | return rc; |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 156 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 157 | EXPORT_SYMBOL(vreg_enable); |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 158 | |
Steve Muckle | 4783de9 | 2008-12-12 19:33:55 -0500 | [diff] [blame] | 159 | int vreg_disable(struct vreg *vreg) |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 160 | { |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 161 | int rc = 0; |
| 162 | if (!vreg) |
| 163 | return -ENODEV; |
Steve Muckle | 4783de9 | 2008-12-12 19:33:55 -0500 | [diff] [blame] | 164 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 165 | mutex_lock(&vreg->lock); |
| 166 | if (vreg->refcnt == 0) { |
| 167 | pr_warn("%s: unbalanced disables for vreg %s\n", |
| 168 | __func__, vreg->name); |
| 169 | rc = -EINVAL; |
| 170 | } else if (vreg->refcnt == 1) { |
| 171 | rc = regulator_disable(vreg->reg); |
| 172 | if (!rc) |
| 173 | vreg->refcnt--; |
| 174 | } else { |
| 175 | rc = 0; |
Matt Wilson | 0e44106 | 2009-06-29 14:13:04 -0500 | [diff] [blame] | 176 | vreg->refcnt--; |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 177 | } |
| 178 | mutex_unlock(&vreg->lock); |
Matt Wilson | 0e44106 | 2009-06-29 14:13:04 -0500 | [diff] [blame] | 179 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 180 | return rc; |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 181 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 182 | EXPORT_SYMBOL(vreg_disable); |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 183 | |
| 184 | int vreg_set_level(struct vreg *vreg, unsigned mv) |
| 185 | { |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 186 | unsigned uv; |
| 187 | int rc; |
Steve Muckle | 4783de9 | 2008-12-12 19:33:55 -0500 | [diff] [blame] | 188 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 189 | if (!vreg) |
| 190 | return -EINVAL; |
| 191 | |
| 192 | if (mv > (UINT_MAX / 1000)) |
| 193 | return -ERANGE; |
| 194 | |
| 195 | uv = mv * 1000; |
| 196 | |
| 197 | mutex_lock(&vreg->lock); |
| 198 | rc = regulator_set_voltage(vreg->reg, uv, uv); |
| 199 | if (!rc) |
| 200 | vreg->mv = mv; |
| 201 | mutex_unlock(&vreg->lock); |
| 202 | |
| 203 | return rc; |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 204 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 205 | EXPORT_SYMBOL(vreg_set_level); |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 206 | |
| 207 | #if defined(CONFIG_DEBUG_FS) |
| 208 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 209 | static int vreg_debug_enabled_set(void *data, u64 val) |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 210 | { |
Steve Muckle | 4783de9 | 2008-12-12 19:33:55 -0500 | [diff] [blame] | 211 | struct vreg *vreg = data; |
| 212 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 213 | if (val == 0) |
| 214 | return vreg_disable(vreg); |
| 215 | else if (val == 1) |
| 216 | return vreg_enable(vreg); |
Steve Muckle | 4783de9 | 2008-12-12 19:33:55 -0500 | [diff] [blame] | 217 | else |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 218 | return -EINVAL; |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 221 | static int vreg_debug_enabled_get(void *data, u64 *val) |
Matt Wilson | 0e44106 | 2009-06-29 14:13:04 -0500 | [diff] [blame] | 222 | { |
| 223 | struct vreg *vreg = data; |
| 224 | |
| 225 | *val = vreg->refcnt; |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 230 | static int vreg_debug_voltage_set(void *data, u64 val) |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 231 | { |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 232 | struct vreg *vreg = data; |
| 233 | return vreg_set_level(vreg, val); |
| 234 | } |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 235 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 236 | static int vreg_debug_voltage_get(void *data, u64 *val) |
| 237 | { |
| 238 | struct vreg *vreg = data; |
| 239 | *val = vreg->mv; |
| 240 | return 0; |
| 241 | } |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 242 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 243 | DEFINE_SIMPLE_ATTRIBUTE(vreg_debug_enabled, vreg_debug_enabled_get, |
| 244 | vreg_debug_enabled_set, "%llu"); |
| 245 | DEFINE_SIMPLE_ATTRIBUTE(vreg_debug_voltage, vreg_debug_voltage_get, |
| 246 | vreg_debug_voltage_set, "%llu"); |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 247 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 248 | static struct dentry *root; |
| 249 | |
| 250 | static void vreg_add_debugfs(struct vreg *vreg) |
| 251 | { |
| 252 | struct dentry *dir; |
| 253 | |
| 254 | if (!root) |
| 255 | return; |
| 256 | |
| 257 | dir = debugfs_create_dir(vreg->name, root); |
| 258 | |
| 259 | if (IS_ERR_OR_NULL(dir)) |
| 260 | goto err; |
| 261 | |
| 262 | if (IS_ERR_OR_NULL(debugfs_create_file("enabled", 0644, dir, vreg, |
| 263 | &vreg_debug_enabled))) |
| 264 | goto destroy; |
| 265 | |
| 266 | if (IS_ERR_OR_NULL(debugfs_create_file("voltage", 0644, dir, vreg, |
| 267 | &vreg_debug_voltage))) |
| 268 | goto destroy; |
| 269 | |
| 270 | return; |
| 271 | |
| 272 | destroy: |
| 273 | debugfs_remove_recursive(dir); |
| 274 | err: |
| 275 | pr_warn("%s: could not create debugfs for vreg %s\n", |
| 276 | __func__, vreg->name); |
| 277 | } |
| 278 | |
| 279 | static int __devinit vreg_debug_init(void) |
| 280 | { |
| 281 | root = debugfs_create_dir("vreg", NULL); |
| 282 | |
| 283 | if (IS_ERR_OR_NULL(root)) { |
| 284 | pr_debug("%s: error initializing debugfs: %ld - " |
| 285 | "disabling debugfs\n", |
| 286 | __func__, root ? PTR_ERR(root) : 0); |
| 287 | root = NULL; |
Matt Wilson | 0e44106 | 2009-06-29 14:13:04 -0500 | [diff] [blame] | 288 | } |
| 289 | |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 290 | return 0; |
| 291 | } |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 292 | static void __devexit vreg_debug_exit(void) |
| 293 | { |
| 294 | if (root) |
| 295 | debugfs_remove_recursive(root); |
| 296 | root = NULL; |
| 297 | } |
| 298 | #else |
| 299 | static inline int __init vreg_debug_init(void) { return 0; } |
| 300 | static inline void __exit vreg_debug_exit(void) { return 0; } |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 301 | #endif |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 302 | |
| 303 | static int __init vreg_init(void) |
| 304 | { |
| 305 | return vreg_debug_init(); |
| 306 | } |
| 307 | module_init(vreg_init); |
| 308 | |
| 309 | static void __exit vreg_exit(void) |
| 310 | { |
| 311 | struct vreg *vreg, *next; |
| 312 | vreg_debug_exit(); |
| 313 | |
| 314 | mutex_lock(&vreg_lock); |
| 315 | list_for_each_entry_safe(vreg, next, &vreg_list, list) |
| 316 | vreg_destroy(vreg); |
| 317 | mutex_unlock(&vreg_lock); |
| 318 | } |
| 319 | module_exit(vreg_exit); |
| 320 | |
| 321 | MODULE_LICENSE("GPL v2"); |
| 322 | MODULE_DESCRIPTION("vreg.c regulator shim"); |
| 323 | MODULE_VERSION("1.0"); |