Merge tag 'pinctrl-v4.14-2' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6-block.git] / drivers / power / supply / power_supply_core.c
index 540d3e0aa0115e27005cc79f46b585d9d5c9fd21..02c6340ae36fe93900662d7971b6af7df4df48ed 100644 (file)
@@ -259,18 +259,14 @@ static int power_supply_check_supplies(struct power_supply *psy)
        /* All supplies found, allocate char ** array for filling */
        psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
                                          GFP_KERNEL);
-       if (!psy->supplied_from) {
-               dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
+       if (!psy->supplied_from)
                return -ENOMEM;
-       }
 
        *psy->supplied_from = devm_kzalloc(&psy->dev,
                                           sizeof(char *) * (cnt - 1),
                                           GFP_KERNEL);
-       if (!*psy->supplied_from) {
-               dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
+       if (!*psy->supplied_from)
                return -ENOMEM;
-       }
 
        return power_supply_populate_supplied_from(psy);
 }
@@ -314,11 +310,12 @@ static int __power_supply_am_i_supplied(struct device *dev, void *_data)
        struct power_supply *epsy = dev_get_drvdata(dev);
        struct psy_am_i_supplied_data *data = _data;
 
-       data->count++;
-       if (__power_supply_is_supplied_by(epsy, data->psy))
+       if (__power_supply_is_supplied_by(epsy, data->psy)) {
+               data->count++;
                if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
                                        &ret))
                        return ret.intval;
+       }
 
        return 0;
 }
@@ -374,6 +371,47 @@ int power_supply_is_system_supplied(void)
 }
 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
 
+static int __power_supply_get_supplier_max_current(struct device *dev,
+                                                  void *data)
+{
+       union power_supply_propval ret = {0,};
+       struct power_supply *epsy = dev_get_drvdata(dev);
+       struct power_supply *psy = data;
+
+       if (__power_supply_is_supplied_by(epsy, psy))
+               if (!epsy->desc->get_property(epsy,
+                                             POWER_SUPPLY_PROP_CURRENT_MAX,
+                                             &ret))
+                       return ret.intval;
+
+       return 0;
+}
+
+int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
+{
+       union power_supply_propval val = {0,};
+       int curr;
+
+       if (!psy->desc->set_property)
+               return -EINVAL;
+
+       /*
+        * This function is not intended for use with a supply with multiple
+        * suppliers, we simply pick the first supply to report a non 0
+        * max-current.
+        */
+       curr = class_for_each_device(power_supply_class, NULL, psy,
+                                     __power_supply_get_supplier_max_current);
+       if (curr <= 0)
+               return (curr == 0) ? -ENODEV : curr;
+
+       val.intval = curr;
+
+       return psy->desc->set_property(psy,
+                               POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
+}
+EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
+
 int power_supply_set_battery_charged(struct power_supply *psy)
 {
        if (atomic_read(&psy->use_cnt) >= 0 &&