From: Stefan Popa <stefan.p...@analog.com>

This patch adds the option to activate/deactivate the charging voltage
limit. If activated, the charger prevents charging until the battery
voltage drops below the VCHG_VLIM threshold.

This option is not configurable via the power_supply properties,
therefore, access via sysfs was provided to examine and modify this
attribute on the fly.

Signed-off-by: Stefan Popa <stefan.p...@analog.com>
Signed-off-by: Alexandru Ardelean <alexandru.ardel...@analog.com>
---

I could not find any traces about this patch being denied and why.
So this is a RESEND to [re]trigger a discussion if needed.

 .../ABI/testing/sysfs-class-power-adp5061     | 13 +++++
 drivers/power/supply/adp5061.c                | 47 +++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-class-power-adp5061 
b/Documentation/ABI/testing/sysfs-class-power-adp5061
index 0d056aa103b5..25064c13ea7b 100644
--- a/Documentation/ABI/testing/sysfs-class-power-adp5061
+++ b/Documentation/ABI/testing/sysfs-class-power-adp5061
@@ -8,3 +8,16 @@ Description:
        Valid values:
                - 1: enabled
                - 0: disabled
+
+What: /sys/class/power_supply/adp5061/charging_vlim_enabled
+Description:
+       Enable/disable charging voltage limit
+
+       The ADP5061 charging voltage limit can be enabled by setting
+       this attribute to 1. When enabled, the charger prevents charging
+       until the battery voltage drops bellow the VCHG_VLIM threshold.
+       See device datasheet for details.
+
+       Valid values:
+               - 1: enabled
+               - 0: disabled
diff --git a/drivers/power/supply/adp5061.c b/drivers/power/supply/adp5061.c
index 6e09a6b710e8..41b24cadd2c9 100644
--- a/drivers/power/supply/adp5061.c
+++ b/drivers/power/supply/adp5061.c
@@ -78,6 +78,10 @@
 #define ADP5061_FUNC_SET_1_EN_CHG_MSK          BIT(0)
 #define ADP5061_FUNC_SET_1_EN_CHG_MODE(x)      (((x) & 0x01) << 0)
 
+/* ADP5061_FUNC_SET_2 */
+#define ADP5061_FUNC_SET_2_EN_CHG_VLIM_MSK     BIT(5)
+#define ADP5061_FUNC_SET_2_EN_CHG_VLIM_MODE(x) (((x) & 0x01) << 5)
+
 /* ADP5061_IEND */
 #define ADP5061_IEND_IEND_MSK                  GENMASK(7, 5)
 #define ADP5061_IEND_IEND_MODE(x)              (((x) & 0x07) << 5)
@@ -735,11 +739,54 @@ static int adp5061_set_charging_enabled(struct device 
*dev,
        return count;
 }
 
+static int adp5061_get_chg_vlim_enabled(struct device *dev,
+                                       struct device_attribute *attr,
+                                       char *buf)
+{
+       struct power_supply *psy = dev_get_drvdata(dev);
+       struct adp5061_state *st = power_supply_get_drvdata(psy);
+       unsigned int regval;
+       int ret;
+
+       ret = regmap_read(st->regmap, ADP5061_FUNC_SET_2, &regval);
+       if (ret < 0)
+               return ret;
+
+       regval = (regval & ADP5061_FUNC_SET_2_EN_CHG_VLIM_MSK) >> 5;
+       return sprintf(buf, "%d\n", regval);
+}
+
+static int adp5061_set_chg_vlim_enabled(struct device *dev,
+                                       struct device_attribute *attr,
+                                       const char *buf, size_t count)
+{
+       struct power_supply *psy = dev_get_drvdata(dev);
+       struct adp5061_state *st = power_supply_get_drvdata(psy);
+       u8 chg_vlim_en;
+       int ret;
+
+       ret = kstrtou8(buf, 0, &chg_vlim_en);
+       if (ret < 0)
+               return ret;
+
+       ret = regmap_update_bits(st->regmap, ADP5061_FUNC_SET_2,
+                       ADP5061_FUNC_SET_2_EN_CHG_VLIM_MSK,
+                       ADP5061_FUNC_SET_2_EN_CHG_VLIM_MODE(!!chg_vlim_en));
+
+       if (ret < 0)
+               return ret;
+
+       return count;
+}
+
 static DEVICE_ATTR(charging_enabled, 0644, adp5061_get_charging_enabled,
                   adp5061_set_charging_enabled);
+static DEVICE_ATTR(charging_vlim_enabled, 0644, adp5061_get_chg_vlim_enabled,
+                  adp5061_set_chg_vlim_enabled);
 
 static struct attribute *adp5061_attributes[] = {
        &dev_attr_charging_enabled.attr,
+       &dev_attr_charging_vlim_enabled.attr,
        NULL
 };
 
-- 
2.20.1

Reply via email to