aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/regulator/core.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/regulator/core.c')
-rw-r--r--drivers/regulator/core.c200
1 files changed, 143 insertions, 57 deletions
diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index e9a83f84ada..e2f3afa71ef 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -13,8 +13,6 @@
13 * 13 *
14 */ 14 */
15 15
16#define pr_fmt(fmt) "%s: " fmt, __func__
17
18#include <linux/kernel.h> 16#include <linux/kernel.h>
19#include <linux/init.h> 17#include <linux/init.h>
20#include <linux/debugfs.h> 18#include <linux/debugfs.h>
@@ -54,9 +52,7 @@ static LIST_HEAD(regulator_map_list);
54static bool has_full_constraints; 52static bool has_full_constraints;
55static bool board_wants_dummy_regulator; 53static bool board_wants_dummy_regulator;
56 54
57#ifdef CONFIG_DEBUG_FS
58static struct dentry *debugfs_root; 55static struct dentry *debugfs_root;
59#endif
60 56
61/* 57/*
62 * struct regulator_map 58 * struct regulator_map
@@ -84,9 +80,7 @@ struct regulator {
84 char *supply_name; 80 char *supply_name;
85 struct device_attribute dev_attr; 81 struct device_attribute dev_attr;
86 struct regulator_dev *rdev; 82 struct regulator_dev *rdev;
87#ifdef CONFIG_DEBUG_FS
88 struct dentry *debugfs; 83 struct dentry *debugfs;
89#endif
90}; 84};
91 85
92static int _regulator_is_enabled(struct regulator_dev *rdev); 86static int _regulator_is_enabled(struct regulator_dev *rdev);
@@ -154,7 +148,7 @@ static struct device_node *of_get_regulator(struct device *dev, const char *supp
154 regnode = of_parse_phandle(dev->of_node, prop_name, 0); 148 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
155 149
156 if (!regnode) { 150 if (!regnode) {
157 dev_warn(dev, "%s property in node %s references invalid phandle", 151 dev_dbg(dev, "Looking up %s property in node %s failed",
158 prop_name, dev->of_node->full_name); 152 prop_name, dev->of_node->full_name);
159 return NULL; 153 return NULL;
160 } 154 }
@@ -807,6 +801,11 @@ static void print_constraints(struct regulator_dev *rdev)
807 count += sprintf(buf + count, "standby"); 801 count += sprintf(buf + count, "standby");
808 802
809 rdev_info(rdev, "%s\n", buf); 803 rdev_info(rdev, "%s\n", buf);
804
805 if ((constraints->min_uV != constraints->max_uV) &&
806 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
807 rdev_warn(rdev,
808 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
810} 809}
811 810
812static int machine_constraints_voltage(struct regulator_dev *rdev, 811static int machine_constraints_voltage(struct regulator_dev *rdev,
@@ -996,7 +995,6 @@ static int set_supply(struct regulator_dev *rdev,
996/** 995/**
997 * set_consumer_device_supply - Bind a regulator to a symbolic supply 996 * set_consumer_device_supply - Bind a regulator to a symbolic supply
998 * @rdev: regulator source 997 * @rdev: regulator source
999 * @consumer_dev: device the supply applies to
1000 * @consumer_dev_name: dev_name() string for device supply applies to 998 * @consumer_dev_name: dev_name() string for device supply applies to
1001 * @supply: symbolic name for supply 999 * @supply: symbolic name for supply
1002 * 1000 *
@@ -1004,22 +1002,14 @@ static int set_supply(struct regulator_dev *rdev,
1004 * sources to symbolic names for supplies for use by devices. Devices 1002 * sources to symbolic names for supplies for use by devices. Devices
1005 * should use these symbolic names to request regulators, avoiding the 1003 * should use these symbolic names to request regulators, avoiding the
1006 * need to provide board-specific regulator names as platform data. 1004 * need to provide board-specific regulator names as platform data.
1007 *
1008 * Only one of consumer_dev and consumer_dev_name may be specified.
1009 */ 1005 */
1010static int set_consumer_device_supply(struct regulator_dev *rdev, 1006static int set_consumer_device_supply(struct regulator_dev *rdev,
1011 struct device *consumer_dev, const char *consumer_dev_name, 1007 const char *consumer_dev_name,
1012 const char *supply) 1008 const char *supply)
1013{ 1009{
1014 struct regulator_map *node; 1010 struct regulator_map *node;
1015 int has_dev; 1011 int has_dev;
1016 1012
1017 if (consumer_dev && consumer_dev_name)
1018 return -EINVAL;
1019
1020 if (!consumer_dev_name && consumer_dev)
1021 consumer_dev_name = dev_name(consumer_dev);
1022
1023 if (supply == NULL) 1013 if (supply == NULL)
1024 return -EINVAL; 1014 return -EINVAL;
1025 1015
@@ -1039,11 +1029,12 @@ static int set_consumer_device_supply(struct regulator_dev *rdev,
1039 if (strcmp(node->supply, supply) != 0) 1029 if (strcmp(node->supply, supply) != 0)
1040 continue; 1030 continue;
1041 1031
1042 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n", 1032 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1043 dev_name(&node->regulator->dev), 1033 consumer_dev_name,
1044 node->regulator->desc->name, 1034 dev_name(&node->regulator->dev),
1045 supply, 1035 node->regulator->desc->name,
1046 dev_name(&rdev->dev), rdev_get_name(rdev)); 1036 supply,
1037 dev_name(&rdev->dev), rdev_get_name(rdev));
1047 return -EBUSY; 1038 return -EBUSY;
1048 } 1039 }
1049 1040
@@ -1142,12 +1133,10 @@ static struct regulator *create_regulator(struct regulator_dev *rdev,
1142 goto attr_err; 1133 goto attr_err;
1143 } 1134 }
1144 1135
1145#ifdef CONFIG_DEBUG_FS
1146 regulator->debugfs = debugfs_create_dir(regulator->supply_name, 1136 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1147 rdev->debugfs); 1137 rdev->debugfs);
1148 if (IS_ERR_OR_NULL(regulator->debugfs)) { 1138 if (!regulator->debugfs) {
1149 rdev_warn(rdev, "Failed to create debugfs directory\n"); 1139 rdev_warn(rdev, "Failed to create debugfs directory\n");
1150 regulator->debugfs = NULL;
1151 } else { 1140 } else {
1152 debugfs_create_u32("uA_load", 0444, regulator->debugfs, 1141 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1153 &regulator->uA_load); 1142 &regulator->uA_load);
@@ -1156,7 +1145,6 @@ static struct regulator *create_regulator(struct regulator_dev *rdev,
1156 debugfs_create_u32("max_uV", 0444, regulator->debugfs, 1145 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1157 &regulator->max_uV); 1146 &regulator->max_uV);
1158 } 1147 }
1159#endif
1160 1148
1161 mutex_unlock(&rdev->mutex); 1149 mutex_unlock(&rdev->mutex);
1162 return regulator; 1150 return regulator;
@@ -1320,6 +1308,40 @@ struct regulator *regulator_get(struct device *dev, const char *id)
1320} 1308}
1321EXPORT_SYMBOL_GPL(regulator_get); 1309EXPORT_SYMBOL_GPL(regulator_get);
1322 1310
1311static void devm_regulator_release(struct device *dev, void *res)
1312{
1313 regulator_put(*(struct regulator **)res);
1314}
1315
1316/**
1317 * devm_regulator_get - Resource managed regulator_get()
1318 * @dev: device for regulator "consumer"
1319 * @id: Supply name or regulator ID.
1320 *
1321 * Managed regulator_get(). Regulators returned from this function are
1322 * automatically regulator_put() on driver detach. See regulator_get() for more
1323 * information.
1324 */
1325struct regulator *devm_regulator_get(struct device *dev, const char *id)
1326{
1327 struct regulator **ptr, *regulator;
1328
1329 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1330 if (!ptr)
1331 return ERR_PTR(-ENOMEM);
1332
1333 regulator = regulator_get(dev, id);
1334 if (!IS_ERR(regulator)) {
1335 *ptr = regulator;
1336 devres_add(dev, ptr);
1337 } else {
1338 devres_free(ptr);
1339 }
1340
1341 return regulator;
1342}
1343EXPORT_SYMBOL_GPL(devm_regulator_get);
1344
1323/** 1345/**
1324 * regulator_get_exclusive - obtain exclusive access to a regulator. 1346 * regulator_get_exclusive - obtain exclusive access to a regulator.
1325 * @dev: device for regulator "consumer" 1347 * @dev: device for regulator "consumer"
@@ -1365,9 +1387,7 @@ void regulator_put(struct regulator *regulator)
1365 mutex_lock(&regulator_list_mutex); 1387 mutex_lock(&regulator_list_mutex);
1366 rdev = regulator->rdev; 1388 rdev = regulator->rdev;
1367 1389
1368#ifdef CONFIG_DEBUG_FS
1369 debugfs_remove_recursive(regulator->debugfs); 1390 debugfs_remove_recursive(regulator->debugfs);
1370#endif
1371 1391
1372 /* remove any sysfs entries */ 1392 /* remove any sysfs entries */
1373 if (regulator->dev) { 1393 if (regulator->dev) {
@@ -1387,6 +1407,34 @@ void regulator_put(struct regulator *regulator)
1387} 1407}
1388EXPORT_SYMBOL_GPL(regulator_put); 1408EXPORT_SYMBOL_GPL(regulator_put);
1389 1409
1410static int devm_regulator_match(struct device *dev, void *res, void *data)
1411{
1412 struct regulator **r = res;
1413 if (!r || !*r) {
1414 WARN_ON(!r || !*r);
1415 return 0;
1416 }
1417 return *r == data;
1418}
1419
1420/**
1421 * devm_regulator_put - Resource managed regulator_put()
1422 * @regulator: regulator to free
1423 *
1424 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1425 * this function will not need to be called and the resource management
1426 * code will ensure that the resource is freed.
1427 */
1428void devm_regulator_put(struct regulator *regulator)
1429{
1430 int rc;
1431
1432 rc = devres_destroy(regulator->dev, devm_regulator_release,
1433 devm_regulator_match, regulator);
1434 WARN_ON(rc);
1435}
1436EXPORT_SYMBOL_GPL(devm_regulator_put);
1437
1390static int _regulator_can_change_status(struct regulator_dev *rdev) 1438static int _regulator_can_change_status(struct regulator_dev *rdev)
1391{ 1439{
1392 if (!rdev->constraints) 1440 if (!rdev->constraints)
@@ -1842,8 +1890,12 @@ static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1842 if (ret < 0) 1890 if (ret < 0)
1843 return ret; 1891 return ret;
1844 old_selector = ret; 1892 old_selector = ret;
1845 delay = rdev->desc->ops->set_voltage_time_sel(rdev, 1893 ret = rdev->desc->ops->set_voltage_time_sel(rdev,
1846 old_selector, selector); 1894 old_selector, selector);
1895 if (ret < 0)
1896 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
1897 else
1898 delay = ret;
1847 } 1899 }
1848 1900
1849 if (best_val != INT_MAX) { 1901 if (best_val != INT_MAX) {
@@ -2394,13 +2446,59 @@ int regulator_bulk_get(struct device *dev, int num_consumers,
2394 return 0; 2446 return 0;
2395 2447
2396err: 2448err:
2397 for (i = 0; i < num_consumers && consumers[i].consumer; i++) 2449 while (--i >= 0)
2398 regulator_put(consumers[i].consumer); 2450 regulator_put(consumers[i].consumer);
2399 2451
2400 return ret; 2452 return ret;
2401} 2453}
2402EXPORT_SYMBOL_GPL(regulator_bulk_get); 2454EXPORT_SYMBOL_GPL(regulator_bulk_get);
2403 2455
2456/**
2457 * devm_regulator_bulk_get - managed get multiple regulator consumers
2458 *
2459 * @dev: Device to supply
2460 * @num_consumers: Number of consumers to register
2461 * @consumers: Configuration of consumers; clients are stored here.
2462 *
2463 * @return 0 on success, an errno on failure.
2464 *
2465 * This helper function allows drivers to get several regulator
2466 * consumers in one operation with management, the regulators will
2467 * automatically be freed when the device is unbound. If any of the
2468 * regulators cannot be acquired then any regulators that were
2469 * allocated will be freed before returning to the caller.
2470 */
2471int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2472 struct regulator_bulk_data *consumers)
2473{
2474 int i;
2475 int ret;
2476
2477 for (i = 0; i < num_consumers; i++)
2478 consumers[i].consumer = NULL;
2479
2480 for (i = 0; i < num_consumers; i++) {
2481 consumers[i].consumer = devm_regulator_get(dev,
2482 consumers[i].supply);
2483 if (IS_ERR(consumers[i].consumer)) {
2484 ret = PTR_ERR(consumers[i].consumer);
2485 dev_err(dev, "Failed to get supply '%s': %d\n",
2486 consumers[i].supply, ret);
2487 consumers[i].consumer = NULL;
2488 goto err;
2489 }
2490 }
2491
2492 return 0;
2493
2494err:
2495 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2496 devm_regulator_put(consumers[i].consumer);
2497
2498 return ret;
2499}
2500EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2501
2404static void regulator_bulk_enable_async(void *data, async_cookie_t cookie) 2502static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2405{ 2503{
2406 struct regulator_bulk_data *bulk = data; 2504 struct regulator_bulk_data *bulk = data;
@@ -2444,12 +2542,9 @@ int regulator_bulk_enable(int num_consumers,
2444 return 0; 2542 return 0;
2445 2543
2446err: 2544err:
2447 for (i = 0; i < num_consumers; i++) 2545 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2448 if (consumers[i].ret == 0) 2546 while (--i >= 0)
2449 regulator_disable(consumers[i].consumer); 2547 regulator_disable(consumers[i].consumer);
2450 else
2451 pr_err("Failed to enable %s: %d\n",
2452 consumers[i].supply, consumers[i].ret);
2453 2548
2454 return ret; 2549 return ret;
2455} 2550}
@@ -2463,8 +2558,8 @@ EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2463 * @return 0 on success, an errno on failure 2558 * @return 0 on success, an errno on failure
2464 * 2559 *
2465 * This convenience API allows consumers to disable multiple regulator 2560 * This convenience API allows consumers to disable multiple regulator
2466 * clients in a single API call. If any consumers cannot be enabled 2561 * clients in a single API call. If any consumers cannot be disabled
2467 * then any others that were disabled will be disabled again prior to 2562 * then any others that were disabled will be enabled again prior to
2468 * return. 2563 * return.
2469 */ 2564 */
2470int regulator_bulk_disable(int num_consumers, 2565int regulator_bulk_disable(int num_consumers,
@@ -2473,7 +2568,7 @@ int regulator_bulk_disable(int num_consumers,
2473 int i; 2568 int i;
2474 int ret; 2569 int ret;
2475 2570
2476 for (i = 0; i < num_consumers; i++) { 2571 for (i = num_consumers - 1; i >= 0; --i) {
2477 ret = regulator_disable(consumers[i].consumer); 2572 ret = regulator_disable(consumers[i].consumer);
2478 if (ret != 0) 2573 if (ret != 0)
2479 goto err; 2574 goto err;
@@ -2483,7 +2578,7 @@ int regulator_bulk_disable(int num_consumers,
2483 2578
2484err: 2579err:
2485 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret); 2580 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
2486 for (--i; i >= 0; --i) 2581 for (++i; i < num_consumers; ++i)
2487 regulator_enable(consumers[i].consumer); 2582 regulator_enable(consumers[i].consumer);
2488 2583
2489 return ret; 2584 return ret;
@@ -2710,11 +2805,9 @@ static int add_regulator_attributes(struct regulator_dev *rdev)
2710 2805
2711static void rdev_init_debugfs(struct regulator_dev *rdev) 2806static void rdev_init_debugfs(struct regulator_dev *rdev)
2712{ 2807{
2713#ifdef CONFIG_DEBUG_FS
2714 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root); 2808 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2715 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) { 2809 if (!rdev->debugfs) {
2716 rdev_warn(rdev, "Failed to create debugfs directory\n"); 2810 rdev_warn(rdev, "Failed to create debugfs directory\n");
2717 rdev->debugfs = NULL;
2718 return; 2811 return;
2719 } 2812 }
2720 2813
@@ -2722,7 +2815,6 @@ static void rdev_init_debugfs(struct regulator_dev *rdev)
2722 &rdev->use_count); 2815 &rdev->use_count);
2723 debugfs_create_u32("open_count", 0444, rdev->debugfs, 2816 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2724 &rdev->open_count); 2817 &rdev->open_count);
2725#endif
2726} 2818}
2727 2819
2728/** 2820/**
@@ -2855,7 +2947,6 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2855 if (init_data) { 2947 if (init_data) {
2856 for (i = 0; i < init_data->num_consumer_supplies; i++) { 2948 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2857 ret = set_consumer_device_supply(rdev, 2949 ret = set_consumer_device_supply(rdev,
2858 init_data->consumer_supplies[i].dev,
2859 init_data->consumer_supplies[i].dev_name, 2950 init_data->consumer_supplies[i].dev_name,
2860 init_data->consumer_supplies[i].supply); 2951 init_data->consumer_supplies[i].supply);
2861 if (ret < 0) { 2952 if (ret < 0) {
@@ -2902,9 +2993,7 @@ void regulator_unregister(struct regulator_dev *rdev)
2902 return; 2993 return;
2903 2994
2904 mutex_lock(&regulator_list_mutex); 2995 mutex_lock(&regulator_list_mutex);
2905#ifdef CONFIG_DEBUG_FS
2906 debugfs_remove_recursive(rdev->debugfs); 2996 debugfs_remove_recursive(rdev->debugfs);
2907#endif
2908 flush_work_sync(&rdev->disable_work.work); 2997 flush_work_sync(&rdev->disable_work.work);
2909 WARN_ON(rdev->open_count); 2998 WARN_ON(rdev->open_count);
2910 unset_regulator_supplies(rdev); 2999 unset_regulator_supplies(rdev);
@@ -3114,12 +3203,14 @@ static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3114 3203
3115 return ret; 3204 return ret;
3116} 3205}
3206#endif
3117 3207
3118static const struct file_operations supply_map_fops = { 3208static const struct file_operations supply_map_fops = {
3209#ifdef CONFIG_DEBUG_FS
3119 .read = supply_map_read_file, 3210 .read = supply_map_read_file,
3120 .llseek = default_llseek, 3211 .llseek = default_llseek,
3121};
3122#endif 3212#endif
3213};
3123 3214
3124static int __init regulator_init(void) 3215static int __init regulator_init(void)
3125{ 3216{
@@ -3127,17 +3218,12 @@ static int __init regulator_init(void)
3127 3218
3128 ret = class_register(&regulator_class); 3219 ret = class_register(&regulator_class);
3129 3220
3130#ifdef CONFIG_DEBUG_FS
3131 debugfs_root = debugfs_create_dir("regulator", NULL); 3221 debugfs_root = debugfs_create_dir("regulator", NULL);
3132 if (IS_ERR(debugfs_root) || !debugfs_root) { 3222 if (!debugfs_root)
3133 pr_warn("regulator: Failed to create debugfs directory\n"); 3223 pr_warn("regulator: Failed to create debugfs directory\n");
3134 debugfs_root = NULL;
3135 }
3136 3224
3137 if (IS_ERR(debugfs_create_file("supply_map", 0444, debugfs_root, 3225 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3138 NULL, &supply_map_fops))) 3226 &supply_map_fops);
3139 pr_warn("regulator: Failed to create supplies debugfs\n");
3140#endif
3141 3227
3142 regulator_dummy_init(); 3228 regulator_dummy_init();
3143 3229