regulator: Optimise out noop voltage changes
[pandora-kernel.git] / drivers / regulator / core.c
index 23c5f7c..ab419f8 100644 (file)
@@ -1637,10 +1637,40 @@ static int _regulator_do_set_voltage(struct regulator_dev *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;
@@ -1667,12 +1697,20 @@ static int _regulator_do_set_voltage(struct regulator_dev *rdev,
 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
 {
        struct regulator_dev *rdev = regulator->rdev;
-       int ret;
+       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;
        }
@@ -1691,12 +1729,58 @@ int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
        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);
 
+/**
+ * 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;
+
+       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:
+       mutex_unlock(&rdev->mutex);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(regulator_sync_voltage);
+
 static int _regulator_get_voltage(struct regulator_dev *rdev)
 {
        int sel;
@@ -2256,7 +2340,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;
@@ -2354,12 +2438,18 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
        /* 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)