diff mbox series

[v2,5/9] power: supply: max77693: Set charge current limits during init

Message ID 20240715-max77693-charger-extcon-v2-5-0838ffbb18c3@gmail.com (mailing list archive)
State New
Headers show
Series power: supply: max77693: Toggle charging/OTG based on extcon status | expand

Commit Message

Artur Weber July 15, 2024, 12:55 p.m. UTC
There are two charger current limit registers:

- Fast charge current limit (which controls current going from the
  charger to the battery);
- CHGIN input current limit (which controls current going into the
  charger through the cable).

Add a function for setting both of the values, and set them to a
safe default value of 500mA at initialization.

The default value for the fast charge current limit can be modified
by setting the constant-charge-current-max-ua DT property of the
battery node specified in the monitored-battery charger DT property.

Signed-off-by: Artur Weber <aweber.kernel@gmail.com>
---
Changes in v2:
- Squashed mfd include register additions into this commit
- Changed from custom fast charge current property to monitored-battery
  (devm_power_supply_register call has been moved up as it is needed by
  the DT init function now)
- Changed to adapt to both current limit values being managed by the
  CHARGER regulator
---
 drivers/power/supply/max77693_charger.c | 43 ++++++++++++++++++++++++---------
 include/linux/mfd/max77693-private.h    |  6 +++++
 2 files changed, 38 insertions(+), 11 deletions(-)

Comments

Lee Jones July 25, 2024, 3:37 p.m. UTC | #1
On Mon, 15 Jul 2024, Artur Weber wrote:

> There are two charger current limit registers:
> 
> - Fast charge current limit (which controls current going from the
>   charger to the battery);
> - CHGIN input current limit (which controls current going into the
>   charger through the cable).
> 
> Add a function for setting both of the values, and set them to a
> safe default value of 500mA at initialization.
> 
> The default value for the fast charge current limit can be modified
> by setting the constant-charge-current-max-ua DT property of the
> battery node specified in the monitored-battery charger DT property.
> 
> Signed-off-by: Artur Weber <aweber.kernel@gmail.com>
> ---
> Changes in v2:
> - Squashed mfd include register additions into this commit
> - Changed from custom fast charge current property to monitored-battery
>   (devm_power_supply_register call has been moved up as it is needed by
>   the DT init function now)
> - Changed to adapt to both current limit values being managed by the
>   CHARGER regulator
> ---
>  drivers/power/supply/max77693_charger.c | 43 ++++++++++++++++++++++++---------

>  include/linux/mfd/max77693-private.h    |  6 +++++

Acked-by: Lee Jones <lee@kernel.org>

>  2 files changed, 38 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/power/supply/max77693_charger.c b/drivers/power/supply/max77693_charger.c
> index 0d53f61d58ba..0ddaa03669a2 100644
> --- a/drivers/power/supply/max77693_charger.c
> +++ b/drivers/power/supply/max77693_charger.c
> @@ -28,6 +28,7 @@ struct max77693_charger {
>  	u32 min_system_volt;
>  	u32 thermal_regulation_temp;
>  	u32 batttery_overcurrent;
> +	u32 fast_charge_current;
>  	u32 charge_input_threshold_volt;
>  };
>  
> @@ -570,6 +571,14 @@ static int max77693_set_batttery_overcurrent(struct max77693_charger *chg,
>  			CHG_CNFG_12_B2SOVRC_MASK, data);
>  }
>  
> +static int max77693_set_current_limit(struct max77693_charger *chg,
> +		unsigned int uamp)
> +{
> +	dev_dbg(chg->dev, "Current limit: %u\n", uamp);
> +
> +	return regulator_set_current_limit(chg->regu, (int)uamp, (int)uamp);
> +}
> +
>  static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg,
>  		unsigned int uvolt)
>  {
> @@ -647,6 +656,10 @@ static int max77693_reg_init(struct max77693_charger *chg)
>  	if (ret)
>  		return ret;
>  
> +	ret = max77693_set_current_limit(chg, DEFAULT_FAST_CHARGE_CURRENT);
> +	if (ret)
> +		return ret;
> +
>  	return max77693_set_charge_input_threshold_volt(chg,
>  			chg->charge_input_threshold_volt);
>  }
> @@ -654,6 +667,7 @@ static int max77693_reg_init(struct max77693_charger *chg)
>  #ifdef CONFIG_OF
>  static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
>  {
> +	struct power_supply_battery_info *battery_info;
>  	struct device_node *np = dev->of_node;
>  
>  	if (!np) {
> @@ -682,11 +696,20 @@ static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
>  		chg->charge_input_threshold_volt =
>  			DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT;
>  
> +	if (power_supply_get_battery_info(chg->charger, &battery_info) ||
> +				!battery_info->constant_charge_current_max_ua)
> +		chg->fast_charge_current = DEFAULT_FAST_CHARGE_CURRENT;
> +	else
> +		chg->fast_charge_current = \
> +				battery_info->constant_charge_current_max_ua;
> +
>  	return 0;
>  }
>  #else /* CONFIG_OF */
>  static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
>  {
> +	chg->fast_charge_current = DEFAULT_FAST_CHARGE_CURRENT;
> +
>  	return 0;
>  }
>  #endif /* CONFIG_OF */
> @@ -712,6 +735,15 @@ static int max77693_charger_probe(struct platform_device *pdev)
>  		return dev_err_probe(&pdev->dev, PTR_ERR(chg->regu),
>  				     "failed to get charger regulator\n");
>  
> +	psy_cfg.drv_data = chg;
> +
> +	chg->charger = devm_power_supply_register(&pdev->dev,
> +						  &max77693_charger_desc,
> +						  &psy_cfg);
> +	if (IS_ERR(chg->charger))
> +		return dev_err_probe(&pdev->dev, PTR_ERR(chg->charger),
> +				     "failed: power supply register\n");
> +
>  	ret = max77693_dt_init(&pdev->dev, chg);
>  	if (ret)
>  		return ret;
> @@ -720,8 +752,6 @@ static int max77693_charger_probe(struct platform_device *pdev)
>  	if (ret)
>  		return ret;
>  
> -	psy_cfg.drv_data = chg;
> -
>  	ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
>  	if (ret) {
>  		dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n");
> @@ -741,15 +771,6 @@ static int max77693_charger_probe(struct platform_device *pdev)
>  		goto err;
>  	}
>  
> -	chg->charger = devm_power_supply_register(&pdev->dev,
> -						  &max77693_charger_desc,
> -						  &psy_cfg);
> -	if (IS_ERR(chg->charger)) {
> -		dev_err(&pdev->dev, "failed: power supply register\n");
> -		ret = PTR_ERR(chg->charger);
> -		goto err;
> -	}
> -
>  	return 0;
>  
>  err:
> diff --git a/include/linux/mfd/max77693-private.h b/include/linux/mfd/max77693-private.h
> index 54444ff2a5de..4570646e2f33 100644
> --- a/include/linux/mfd/max77693-private.h
> +++ b/include/linux/mfd/max77693-private.h
> @@ -145,6 +145,8 @@ enum max77693_pmic_reg {
>  #define DEFAULT_THERMAL_REGULATION_TEMP		100
>  /* microamps */
>  #define DEFAULT_BATTERY_OVERCURRENT		3500000
> +/* microamps */
> +#define DEFAULT_FAST_CHARGE_CURRENT		500000
>  /* microvolts */
>  #define DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT	4300000
>  
> @@ -217,6 +219,10 @@ enum max77693_charger_battery_state {
>  #define CHG_CNFG_01_CHGRSTRT_MASK	(0x3 << CHG_CNFG_01_CHGRSTRT_SHIFT)
>  #define CHG_CNFG_01_PQEN_MAKS		BIT(CHG_CNFG_01_PQEN_SHIFT)
>  
> +/* MAX77693_CHG_REG_CHG_CNFG_02 register */
> +#define CHG_CNFG_02_CC_SHIFT		0
> +#define CHG_CNFG_02_CC_MASK		0x3F
> +
>  /* MAX77693_CHG_REG_CHG_CNFG_03 register */
>  #define CHG_CNFG_03_TOITH_SHIFT		0
>  #define CHG_CNFG_03_TOTIME_SHIFT	3
> 
> -- 
> 2.45.2
>
diff mbox series

Patch

diff --git a/drivers/power/supply/max77693_charger.c b/drivers/power/supply/max77693_charger.c
index 0d53f61d58ba..0ddaa03669a2 100644
--- a/drivers/power/supply/max77693_charger.c
+++ b/drivers/power/supply/max77693_charger.c
@@ -28,6 +28,7 @@  struct max77693_charger {
 	u32 min_system_volt;
 	u32 thermal_regulation_temp;
 	u32 batttery_overcurrent;
+	u32 fast_charge_current;
 	u32 charge_input_threshold_volt;
 };
 
@@ -570,6 +571,14 @@  static int max77693_set_batttery_overcurrent(struct max77693_charger *chg,
 			CHG_CNFG_12_B2SOVRC_MASK, data);
 }
 
+static int max77693_set_current_limit(struct max77693_charger *chg,
+		unsigned int uamp)
+{
+	dev_dbg(chg->dev, "Current limit: %u\n", uamp);
+
+	return regulator_set_current_limit(chg->regu, (int)uamp, (int)uamp);
+}
+
 static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg,
 		unsigned int uvolt)
 {
@@ -647,6 +656,10 @@  static int max77693_reg_init(struct max77693_charger *chg)
 	if (ret)
 		return ret;
 
+	ret = max77693_set_current_limit(chg, DEFAULT_FAST_CHARGE_CURRENT);
+	if (ret)
+		return ret;
+
 	return max77693_set_charge_input_threshold_volt(chg,
 			chg->charge_input_threshold_volt);
 }
@@ -654,6 +667,7 @@  static int max77693_reg_init(struct max77693_charger *chg)
 #ifdef CONFIG_OF
 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
 {
+	struct power_supply_battery_info *battery_info;
 	struct device_node *np = dev->of_node;
 
 	if (!np) {
@@ -682,11 +696,20 @@  static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
 		chg->charge_input_threshold_volt =
 			DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT;
 
+	if (power_supply_get_battery_info(chg->charger, &battery_info) ||
+				!battery_info->constant_charge_current_max_ua)
+		chg->fast_charge_current = DEFAULT_FAST_CHARGE_CURRENT;
+	else
+		chg->fast_charge_current = \
+				battery_info->constant_charge_current_max_ua;
+
 	return 0;
 }
 #else /* CONFIG_OF */
 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
 {
+	chg->fast_charge_current = DEFAULT_FAST_CHARGE_CURRENT;
+
 	return 0;
 }
 #endif /* CONFIG_OF */
@@ -712,6 +735,15 @@  static int max77693_charger_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, PTR_ERR(chg->regu),
 				     "failed to get charger regulator\n");
 
+	psy_cfg.drv_data = chg;
+
+	chg->charger = devm_power_supply_register(&pdev->dev,
+						  &max77693_charger_desc,
+						  &psy_cfg);
+	if (IS_ERR(chg->charger))
+		return dev_err_probe(&pdev->dev, PTR_ERR(chg->charger),
+				     "failed: power supply register\n");
+
 	ret = max77693_dt_init(&pdev->dev, chg);
 	if (ret)
 		return ret;
@@ -720,8 +752,6 @@  static int max77693_charger_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	psy_cfg.drv_data = chg;
-
 	ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
 	if (ret) {
 		dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n");
@@ -741,15 +771,6 @@  static int max77693_charger_probe(struct platform_device *pdev)
 		goto err;
 	}
 
-	chg->charger = devm_power_supply_register(&pdev->dev,
-						  &max77693_charger_desc,
-						  &psy_cfg);
-	if (IS_ERR(chg->charger)) {
-		dev_err(&pdev->dev, "failed: power supply register\n");
-		ret = PTR_ERR(chg->charger);
-		goto err;
-	}
-
 	return 0;
 
 err:
diff --git a/include/linux/mfd/max77693-private.h b/include/linux/mfd/max77693-private.h
index 54444ff2a5de..4570646e2f33 100644
--- a/include/linux/mfd/max77693-private.h
+++ b/include/linux/mfd/max77693-private.h
@@ -145,6 +145,8 @@  enum max77693_pmic_reg {
 #define DEFAULT_THERMAL_REGULATION_TEMP		100
 /* microamps */
 #define DEFAULT_BATTERY_OVERCURRENT		3500000
+/* microamps */
+#define DEFAULT_FAST_CHARGE_CURRENT		500000
 /* microvolts */
 #define DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT	4300000
 
@@ -217,6 +219,10 @@  enum max77693_charger_battery_state {
 #define CHG_CNFG_01_CHGRSTRT_MASK	(0x3 << CHG_CNFG_01_CHGRSTRT_SHIFT)
 #define CHG_CNFG_01_PQEN_MAKS		BIT(CHG_CNFG_01_PQEN_SHIFT)
 
+/* MAX77693_CHG_REG_CHG_CNFG_02 register */
+#define CHG_CNFG_02_CC_SHIFT		0
+#define CHG_CNFG_02_CC_MASK		0x3F
+
 /* MAX77693_CHG_REG_CHG_CNFG_03 register */
 #define CHG_CNFG_03_TOITH_SHIFT		0
 #define CHG_CNFG_03_TOTIME_SHIFT	3