power: supply: twl4030-charger: add writable INPUT_CURRENT_LIMIT property
authorH. Nikolaus Schaller <hns@goldelico.com>
Fri, 14 Apr 2017 18:25:56 +0000 (20:25 +0200)
committerSebastian Reichel <sre@kernel.org>
Mon, 1 May 2017 09:37:11 +0000 (11:37 +0200)
Currently, the twl4030 charger defines its own max_current by directly
creating sysfs nodes. It should use the input_current_limit property
which is e.g. used by the bq24257 driver.

This patch adds the input_current_property with the same semantics as
the max_current property. The code to manage the max_current property
is removed by a separate patch.

Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.co.uk>
drivers/power/supply/twl4030_charger.c

index 990ff3d218bcc7572bd1be15da7731fd36894736..0b84f336fe392553735a67ba039928e2fc387e8b 100644 (file)
@@ -922,6 +922,28 @@ static int twl4030_bci_get_property(struct power_supply *psy,
                        twl4030_bci_state_to_status(state) !=
                                POWER_SUPPLY_STATUS_NOT_CHARGING;
                break;
+       case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+               val->intval = -1;
+               if (psy->desc->type != POWER_SUPPLY_TYPE_USB) {
+                       if (!bci->ac_is_active)
+                               val->intval = bci->ac_cur;
+               } else {
+                       if (bci->ac_is_active)
+                               val->intval = bci->usb_cur_target;
+               }
+               if (val->intval < 0) {
+                       u8 bcictl1;
+
+                       val->intval = twl4030bci_read_adc_val(TWL4030_BCIIREF1);
+                       if (val->intval < 0)
+                               return val->intval;
+                       ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
+                       if (ret < 0)
+                               return ret;
+                       val->intval = regval2ua(val->intval, bcictl1 &
+                                                       TWL4030_CGAIN);
+               }
+               break;
        default:
                return -EINVAL;
        }
@@ -929,11 +951,44 @@ static int twl4030_bci_get_property(struct power_supply *psy,
        return 0;
 }
 
+static int twl4030_bci_set_property(struct power_supply *psy,
+                                   enum power_supply_property psp,
+                                   const union power_supply_propval *val)
+{
+       struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent);
+
+       switch (psp) {
+       case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+               if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
+                       bci->usb_cur_target = val->intval;
+               else
+                       bci->ac_cur = val->intval;
+               twl4030_charger_update_current(bci);
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+int twl4030_bci_property_is_writeable(struct power_supply *psy,
+                                     enum power_supply_property psp)
+{
+       switch (psp) {
+       case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+               return true;
+       default:
+               return false;
+       }
+}
+
 static enum power_supply_property twl4030_charger_props[] = {
        POWER_SUPPLY_PROP_STATUS,
        POWER_SUPPLY_PROP_ONLINE,
        POWER_SUPPLY_PROP_VOLTAGE_NOW,
        POWER_SUPPLY_PROP_CURRENT_NOW,
+       POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
 };
 
 #ifdef CONFIG_OF
@@ -970,6 +1025,8 @@ static const struct power_supply_desc twl4030_bci_ac_desc = {
        .properties     = twl4030_charger_props,
        .num_properties = ARRAY_SIZE(twl4030_charger_props),
        .get_property   = twl4030_bci_get_property,
+       .set_property   = twl4030_bci_set_property,
+       .property_is_writeable  = twl4030_bci_property_is_writeable,
 };
 
 static const struct power_supply_desc twl4030_bci_usb_desc = {
@@ -978,6 +1035,8 @@ static const struct power_supply_desc twl4030_bci_usb_desc = {
        .properties     = twl4030_charger_props,
        .num_properties = ARRAY_SIZE(twl4030_charger_props),
        .get_property   = twl4030_bci_get_property,
+       .set_property   = twl4030_bci_set_property,
+       .property_is_writeable  = twl4030_bci_property_is_writeable,
 };
 
 static int twl4030_bci_probe(struct platform_device *pdev)