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. |
Pankaj Kumar | 679671e | 2012-03-14 19:32:21 +0530 | [diff] [blame] | 4 | * Copyright (c) 2009-2012 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> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 19 | #include <linux/module.h> |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 20 | #include <linux/device.h> |
| 21 | #include <linux/init.h> |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/mutex.h> |
| 25 | #include <linux/atomic.h> |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 26 | #include <linux/debugfs.h> |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 27 | #include <linux/regulator/consumer.h> |
Matt Wilson | 0e44106 | 2009-06-29 14:13:04 -0500 | [diff] [blame] | 28 | #include <linux/string.h> |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 29 | #include <mach/vreg.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 30 | #include <mach/proc_comm.h> |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 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 |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 39 | |
| 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); |
Pankaj Kumar | 679671e | 2012-03-14 19:32:21 +0530 | [diff] [blame] | 135 | list_del(&vreg->list); |
| 136 | kfree(vreg); |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | int vreg_enable(struct vreg *vreg) |
| 140 | { |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 141 | int rc = 0; |
| 142 | if (!vreg) |
| 143 | return -ENODEV; |
Steve Muckle | 4783de9 | 2008-12-12 19:33:55 -0500 | [diff] [blame] | 144 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 145 | mutex_lock(&vreg->lock); |
| 146 | if (vreg->refcnt == 0) { |
| 147 | rc = regulator_enable(vreg->reg); |
| 148 | if (!rc) |
| 149 | vreg->refcnt++; |
| 150 | } else { |
| 151 | rc = 0; |
| 152 | if (vreg->refcnt < UINT_MAX) |
| 153 | vreg->refcnt++; |
| 154 | } |
| 155 | mutex_unlock(&vreg->lock); |
Matt Wilson | 0e44106 | 2009-06-29 14:13:04 -0500 | [diff] [blame] | 156 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 157 | return rc; |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 158 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 159 | EXPORT_SYMBOL(vreg_enable); |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 160 | |
Steve Muckle | 4783de9 | 2008-12-12 19:33:55 -0500 | [diff] [blame] | 161 | int vreg_disable(struct vreg *vreg) |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 162 | { |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 163 | int rc = 0; |
| 164 | if (!vreg) |
| 165 | return -ENODEV; |
Steve Muckle | 4783de9 | 2008-12-12 19:33:55 -0500 | [diff] [blame] | 166 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 167 | mutex_lock(&vreg->lock); |
| 168 | if (vreg->refcnt == 0) { |
| 169 | pr_warn("%s: unbalanced disables for vreg %s\n", |
| 170 | __func__, vreg->name); |
| 171 | rc = -EINVAL; |
| 172 | } else if (vreg->refcnt == 1) { |
| 173 | rc = regulator_disable(vreg->reg); |
| 174 | if (!rc) |
| 175 | vreg->refcnt--; |
| 176 | } else { |
| 177 | rc = 0; |
Matt Wilson | 0e44106 | 2009-06-29 14:13:04 -0500 | [diff] [blame] | 178 | vreg->refcnt--; |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 179 | } |
| 180 | mutex_unlock(&vreg->lock); |
Matt Wilson | 0e44106 | 2009-06-29 14:13:04 -0500 | [diff] [blame] | 181 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 182 | return rc; |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 183 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 184 | EXPORT_SYMBOL(vreg_disable); |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 185 | |
| 186 | int vreg_set_level(struct vreg *vreg, unsigned mv) |
| 187 | { |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 188 | unsigned uv; |
| 189 | int rc; |
Steve Muckle | 4783de9 | 2008-12-12 19:33:55 -0500 | [diff] [blame] | 190 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 191 | if (!vreg) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | if (mv > (UINT_MAX / 1000)) |
| 195 | return -ERANGE; |
| 196 | |
| 197 | uv = mv * 1000; |
| 198 | |
| 199 | mutex_lock(&vreg->lock); |
| 200 | rc = regulator_set_voltage(vreg->reg, uv, uv); |
| 201 | if (!rc) |
| 202 | vreg->mv = mv; |
| 203 | mutex_unlock(&vreg->lock); |
| 204 | |
| 205 | return rc; |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 206 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 207 | EXPORT_SYMBOL(vreg_set_level); |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 208 | |
| 209 | #if defined(CONFIG_DEBUG_FS) |
| 210 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 211 | static int vreg_debug_enabled_set(void *data, u64 val) |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 212 | { |
Steve Muckle | 4783de9 | 2008-12-12 19:33:55 -0500 | [diff] [blame] | 213 | struct vreg *vreg = data; |
| 214 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 215 | if (val == 0) |
| 216 | return vreg_disable(vreg); |
| 217 | else if (val == 1) |
| 218 | return vreg_enable(vreg); |
Steve Muckle | 4783de9 | 2008-12-12 19:33:55 -0500 | [diff] [blame] | 219 | else |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 220 | return -EINVAL; |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 223 | static int vreg_debug_enabled_get(void *data, u64 *val) |
Matt Wilson | 0e44106 | 2009-06-29 14:13:04 -0500 | [diff] [blame] | 224 | { |
| 225 | struct vreg *vreg = data; |
| 226 | |
| 227 | *val = vreg->refcnt; |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 232 | static int vreg_debug_voltage_set(void *data, u64 val) |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 233 | { |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 234 | struct vreg *vreg = data; |
| 235 | return vreg_set_level(vreg, val); |
| 236 | } |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 237 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 238 | static int vreg_debug_voltage_get(void *data, u64 *val) |
| 239 | { |
| 240 | struct vreg *vreg = data; |
| 241 | *val = vreg->mv; |
| 242 | return 0; |
| 243 | } |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 244 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 245 | DEFINE_SIMPLE_ATTRIBUTE(vreg_debug_enabled, vreg_debug_enabled_get, |
| 246 | vreg_debug_enabled_set, "%llu"); |
| 247 | DEFINE_SIMPLE_ATTRIBUTE(vreg_debug_voltage, vreg_debug_voltage_get, |
| 248 | vreg_debug_voltage_set, "%llu"); |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 249 | |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 250 | static struct dentry *root; |
| 251 | |
| 252 | static void vreg_add_debugfs(struct vreg *vreg) |
| 253 | { |
| 254 | struct dentry *dir; |
| 255 | |
| 256 | if (!root) |
| 257 | return; |
| 258 | |
| 259 | dir = debugfs_create_dir(vreg->name, root); |
| 260 | |
| 261 | if (IS_ERR_OR_NULL(dir)) |
| 262 | goto err; |
| 263 | |
| 264 | if (IS_ERR_OR_NULL(debugfs_create_file("enabled", 0644, dir, vreg, |
| 265 | &vreg_debug_enabled))) |
| 266 | goto destroy; |
| 267 | |
| 268 | if (IS_ERR_OR_NULL(debugfs_create_file("voltage", 0644, dir, vreg, |
| 269 | &vreg_debug_voltage))) |
| 270 | goto destroy; |
| 271 | |
| 272 | return; |
| 273 | |
| 274 | destroy: |
| 275 | debugfs_remove_recursive(dir); |
| 276 | err: |
| 277 | pr_warn("%s: could not create debugfs for vreg %s\n", |
| 278 | __func__, vreg->name); |
| 279 | } |
| 280 | |
| 281 | static int __devinit vreg_debug_init(void) |
| 282 | { |
| 283 | root = debugfs_create_dir("vreg", NULL); |
| 284 | |
| 285 | if (IS_ERR_OR_NULL(root)) { |
| 286 | pr_debug("%s: error initializing debugfs: %ld - " |
| 287 | "disabling debugfs\n", |
| 288 | __func__, root ? PTR_ERR(root) : 0); |
| 289 | root = NULL; |
Matt Wilson | 0e44106 | 2009-06-29 14:13:04 -0500 | [diff] [blame] | 290 | } |
| 291 | |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 292 | return 0; |
| 293 | } |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 294 | static void __devexit vreg_debug_exit(void) |
| 295 | { |
| 296 | if (root) |
| 297 | debugfs_remove_recursive(root); |
| 298 | root = NULL; |
| 299 | } |
| 300 | #else |
| 301 | static inline int __init vreg_debug_init(void) { return 0; } |
| 302 | static inline void __exit vreg_debug_exit(void) { return 0; } |
Brian Swetland | f030d7b | 2008-09-29 14:07:14 -0700 | [diff] [blame] | 303 | #endif |
Justin Paupore | 0f1f2ac | 2011-08-25 18:13:50 -0700 | [diff] [blame] | 304 | |
| 305 | static int __init vreg_init(void) |
| 306 | { |
| 307 | return vreg_debug_init(); |
| 308 | } |
| 309 | module_init(vreg_init); |
| 310 | |
| 311 | static void __exit vreg_exit(void) |
| 312 | { |
| 313 | struct vreg *vreg, *next; |
| 314 | vreg_debug_exit(); |
| 315 | |
| 316 | mutex_lock(&vreg_lock); |
| 317 | list_for_each_entry_safe(vreg, next, &vreg_list, list) |
| 318 | vreg_destroy(vreg); |
| 319 | mutex_unlock(&vreg_lock); |
| 320 | } |
| 321 | module_exit(vreg_exit); |
| 322 | |
| 323 | MODULE_LICENSE("GPL v2"); |
| 324 | MODULE_DESCRIPTION("vreg.c regulator shim"); |
| 325 | MODULE_VERSION("1.0"); |