Merge branch 'devel-stable' of master.kernel.org:/home/rmk/linux-2.6-arm
[pandora-kernel.git] / drivers / regulator / core.c
index 671eb53..9fa2095 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <linux/kernel.h>
 #include <linux/init.h>
+#include <linux/debugfs.h>
 #include <linux/device.h>
 #include <linux/slab.h>
 #include <linux/err.h>
 static DEFINE_MUTEX(regulator_list_mutex);
 static LIST_HEAD(regulator_list);
 static LIST_HEAD(regulator_map_list);
-static int has_full_constraints;
+static bool has_full_constraints;
 static bool board_wants_dummy_regulator;
 
+#ifdef CONFIG_DEBUG_FS
+static struct dentry *debugfs_root;
+#endif
+
 /*
  * struct regulator_map
  *
@@ -83,6 +88,8 @@ static int _regulator_get_current_limit(struct regulator_dev *rdev);
 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
 static void _notifier_call_chain(struct regulator_dev *rdev,
                                  unsigned long event, void *data);
+static int _regulator_do_set_voltage(struct regulator_dev *rdev,
+                                    int min_uV, int max_uV);
 
 static const char *rdev_get_name(struct regulator_dev *rdev)
 {
@@ -577,7 +584,9 @@ static void drms_uA_update(struct regulator_dev *rdev)
 
        err = regulator_check_drms(rdev);
        if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
-           !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
+           (!rdev->desc->ops->get_voltage &&
+            !rdev->desc->ops->get_voltage_sel) ||
+           !rdev->desc->ops->set_mode)
                return;
 
        /* get output voltage */
@@ -735,7 +744,7 @@ static void print_constraints(struct regulator_dev *rdev)
        if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
                count += sprintf(buf + count, "standby");
 
-       rdev_info(rdev, "regulator: %s\n", buf);
+       rdev_info(rdev, "%s\n", buf);
 }
 
 static int machine_constraints_voltage(struct regulator_dev *rdev,
@@ -743,22 +752,19 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,
 {
        struct regulator_ops *ops = rdev->desc->ops;
        int ret;
-       unsigned selector;
 
        /* do we need to apply the constraint voltage */
        if (rdev->constraints->apply_uV &&
-               rdev->constraints->min_uV == rdev->constraints->max_uV &&
-               ops->set_voltage) {
-               ret = ops->set_voltage(rdev,
-                                      rdev->constraints->min_uV,
-                                      rdev->constraints->max_uV,
-                                      &selector);
-                       if (ret < 0) {
-                               rdev_err(rdev, "failed to apply %duV constraint\n",
-                                        rdev->constraints->min_uV);
-                               rdev->constraints = NULL;
-                               return ret;
-                       }
+           rdev->constraints->min_uV == rdev->constraints->max_uV) {
+               ret = _regulator_do_set_voltage(rdev,
+                                               rdev->constraints->min_uV,
+                                               rdev->constraints->max_uV);
+               if (ret < 0) {
+                       rdev_err(rdev, "failed to apply %duV constraint\n",
+                                rdev->constraints->min_uV);
+                       rdev->constraints = NULL;
+                       return ret;
+               }
        }
 
        /* constrain machine-level voltage specs to fit
@@ -1619,6 +1625,62 @@ int regulator_is_supported_voltage(struct regulator *regulator,
        return 0;
 }
 
+static int _regulator_do_set_voltage(struct regulator_dev *rdev,
+                                    int min_uV, int max_uV)
+{
+       int ret;
+       unsigned int selector;
+
+       trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
+
+       if (rdev->desc->ops->set_voltage) {
+               ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
+                                                  &selector);
+
+               if (rdev->desc->ops->list_voltage)
+                       selector = rdev->desc->ops->list_voltage(rdev,
+                                                                selector);
+               else
+                       selector = -1;
+       } else if (rdev->desc->ops->set_voltage_sel) {
+               int best_val = INT_MAX;
+               int i;
+
+               selector = 0;
+
+               /* Find the smallest voltage that falls within the specified
+                * range.
+                */
+               for (i = 0; i < rdev->desc->n_voltages; i++) {
+                       ret = rdev->desc->ops->list_voltage(rdev, i);
+                       if (ret < 0)
+                               continue;
+
+                       if (ret < best_val && ret >= min_uV && ret <= max_uV) {
+                               best_val = ret;
+                               selector = i;
+                       }
+               }
+
+               if (best_val != INT_MAX) {
+                       ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
+                       selector = best_val;
+               } else {
+                       ret = -EINVAL;
+               }
+       } else {
+               ret = -EINVAL;
+       }
+
+       if (ret == 0)
+               _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
+                                    NULL);
+
+       trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
+
+       return ret;
+}
+
 /**
  * regulator_set_voltage - set regulator output voltage
  * @regulator: regulator source
@@ -1640,13 +1702,20 @@ int regulator_is_supported_voltage(struct regulator *regulator,
 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
 {
        struct regulator_dev *rdev = regulator->rdev;
-       int ret;
-       unsigned selector;
+       int ret = 0;
 
        mutex_lock(&rdev->mutex);
 
+       /* If we're setting the same range as last time the change
+        * should be a noop (some cpufreq implementations use the same
+        * voltage for multiple frequencies, for example).
+        */
+       if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
+               goto out;
+
        /* sanity check */
-       if (!rdev->desc->ops->set_voltage) {
+       if (!rdev->desc->ops->set_voltage &&
+           !rdev->desc->ops->set_voltage_sel) {
                ret = -EINVAL;
                goto out;
        }
@@ -1662,27 +1731,71 @@ int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
        if (ret < 0)
                goto out;
 
-       trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
+       ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
 
-       ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, &selector);
+out:
+       mutex_unlock(&rdev->mutex);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(regulator_set_voltage);
 
-       if (rdev->desc->ops->list_voltage)
-               selector = rdev->desc->ops->list_voltage(rdev, selector);
-       else
-               selector = -1;
+/**
+ * regulator_sync_voltage - re-apply last regulator output voltage
+ * @regulator: regulator source
+ *
+ * Re-apply the last configured voltage.  This is intended to be used
+ * where some external control source the consumer is cooperating with
+ * has caused the configured voltage to change.
+ */
+int regulator_sync_voltage(struct regulator *regulator)
+{
+       struct regulator_dev *rdev = regulator->rdev;
+       int ret, min_uV, max_uV;
 
-       trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
+       mutex_lock(&rdev->mutex);
+
+       if (!rdev->desc->ops->set_voltage &&
+           !rdev->desc->ops->set_voltage_sel) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       /* This is only going to work if we've had a voltage configured. */
+       if (!regulator->min_uV && !regulator->max_uV) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       min_uV = regulator->min_uV;
+       max_uV = regulator->max_uV;
+
+       /* This should be a paranoia check... */
+       ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
+       if (ret < 0)
+               goto out;
+
+       ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
+       if (ret < 0)
+               goto out;
+
+       ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
 
 out:
-       _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
        mutex_unlock(&rdev->mutex);
        return ret;
 }
-EXPORT_SYMBOL_GPL(regulator_set_voltage);
+EXPORT_SYMBOL_GPL(regulator_sync_voltage);
 
 static int _regulator_get_voltage(struct regulator_dev *rdev)
 {
-       /* sanity check */
+       int sel;
+
+       if (rdev->desc->ops->get_voltage_sel) {
+               sel = rdev->desc->ops->get_voltage_sel(rdev);
+               if (sel < 0)
+                       return sel;
+               return rdev->desc->ops->list_voltage(rdev, sel);
+       }
        if (rdev->desc->ops->get_voltage)
                return rdev->desc->ops->get_voltage(rdev);
        else
@@ -2191,7 +2304,7 @@ static int add_regulator_attributes(struct regulator_dev *rdev)
        int                     status = 0;
 
        /* some attributes need specific methods to be displayed */
-       if (ops->get_voltage) {
+       if (ops->get_voltage || ops->get_voltage_sel) {
                status = device_create_file(dev, &dev_attr_microvolts);
                if (status < 0)
                        return status;
@@ -2232,7 +2345,7 @@ static int add_regulator_attributes(struct regulator_dev *rdev)
                return status;
 
        /* constraints need specific supporting methods */
-       if (ops->set_voltage) {
+       if (ops->set_voltage || ops->set_voltage_sel) {
                status = device_create_file(dev, &dev_attr_min_microvolts);
                if (status < 0)
                        return status;
@@ -2296,6 +2409,23 @@ static int add_regulator_attributes(struct regulator_dev *rdev)
        return status;
 }
 
+static void rdev_init_debugfs(struct regulator_dev *rdev)
+{
+#ifdef CONFIG_DEBUG_FS
+       rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
+       if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
+               rdev_warn(rdev, "Failed to create debugfs directory\n");
+               rdev->debugfs = NULL;
+               return;
+       }
+
+       debugfs_create_u32("use_count", 0444, rdev->debugfs,
+                          &rdev->use_count);
+       debugfs_create_u32("open_count", 0444, rdev->debugfs,
+                          &rdev->open_count);
+#endif
+}
+
 /**
  * regulator_register - register regulator
  * @regulator_desc: regulator to register
@@ -2327,6 +2457,22 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
        if (!init_data)
                return ERR_PTR(-EINVAL);
 
+       /* Only one of each should be implemented */
+       WARN_ON(regulator_desc->ops->get_voltage &&
+               regulator_desc->ops->get_voltage_sel);
+       WARN_ON(regulator_desc->ops->set_voltage &&
+               regulator_desc->ops->set_voltage_sel);
+
+       /* If we're using selectors we must implement list_voltage. */
+       if (regulator_desc->ops->get_voltage_sel &&
+           !regulator_desc->ops->list_voltage) {
+               return ERR_PTR(-EINVAL);
+       }
+       if (regulator_desc->ops->set_voltage_sel &&
+           !regulator_desc->ops->list_voltage) {
+               return ERR_PTR(-EINVAL);
+       }
+
        rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
        if (rdev == NULL)
                return ERR_PTR(-ENOMEM);
@@ -2424,6 +2570,8 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
        }
 
        list_add(&rdev->list, &regulator_list);
+
+       rdev_init_debugfs(rdev);
 out:
        mutex_unlock(&regulator_list_mutex);
        return rdev;
@@ -2456,6 +2604,9 @@ void regulator_unregister(struct regulator_dev *rdev)
                return;
 
        mutex_lock(&regulator_list_mutex);
+#ifdef CONFIG_DEBUG_FS
+       debugfs_remove_recursive(rdev->debugfs);
+#endif
        WARN_ON(rdev->open_count);
        unset_regulator_supplies(rdev);
        list_del(&rdev->list);
@@ -2599,6 +2750,14 @@ static int __init regulator_init(void)
 
        ret = class_register(&regulator_class);
 
+#ifdef CONFIG_DEBUG_FS
+       debugfs_root = debugfs_create_dir("regulator", NULL);
+       if (IS_ERR(debugfs_root) || !debugfs_root) {
+               pr_warn("regulator: Failed to create debugfs directory\n");
+               debugfs_root = NULL;
+       }
+#endif
+
        regulator_dummy_init();
 
        return ret;