linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] power: supply: charger-manager: Tidy function
@ 2020-09-02 16:58 Alex Dewar
  2020-09-19 19:32 ` Alex Dewar
  2020-10-08 22:35 ` Sebastian Reichel
  0 siblings, 2 replies; 3+ messages in thread
From: Alex Dewar @ 2020-09-02 16:58 UTC (permalink / raw)
  Cc: Alex Dewar, Sebastian Reichel, linux-pm, linux-kernel

check_charging_duration() contains some copy-pasted code, which makes it
less readable. Refactor the function to be a bit tidier.

I've also fixed a couple of typos.

Signed-off-by: Alex Dewar <alex.dewar90@gmail.com>
---
 drivers/power/supply/charger-manager.c | 39 +++++++++-----------------
 1 file changed, 14 insertions(+), 25 deletions(-)

diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c
index 07992821e252..67c7b1fb6601 100644
--- a/drivers/power/supply/charger-manager.c
+++ b/drivers/power/supply/charger-manager.c
@@ -443,42 +443,31 @@ static int try_charger_enable(struct charger_manager *cm, bool enable)
  * check_charging_duration - Monitor charging/discharging duration
  * @cm: the Charger Manager representing the battery.
  *
- * If whole charging duration exceed 'charging_max_duration_ms',
+ * If whole charging duration exceeds 'charging_max_duration_ms',
  * cm stop charging to prevent overcharge/overheat. If discharging
- * duration exceed 'discharging _max_duration_ms', charger cable is
+ * duration exceeds 'discharging _max_duration_ms', charger cable is
  * attached, after full-batt, cm start charging to maintain fully
  * charged state for battery.
  */
 static int check_charging_duration(struct charger_manager *cm)
 {
 	struct charger_desc *desc = cm->desc;
-	u64 curr = ktime_to_ms(ktime_get());
 	u64 duration;
-	int ret = false;
 
-	if (!desc->charging_max_duration_ms &&
-			!desc->discharging_max_duration_ms)
-		return ret;
-
-	if (cm->charger_enabled) {
-		duration = curr - cm->charging_start_time;
-
-		if (duration > desc->charging_max_duration_ms) {
-			dev_info(cm->dev, "Charging duration exceed %ums\n",
-				 desc->charging_max_duration_ms);
-			ret = true;
-		}
-	} else if (cm->battery_status == POWER_SUPPLY_STATUS_NOT_CHARGING) {
-		duration = curr - cm->charging_end_time;
+	if ((desc->charging_max_duration_ms == 0 &&
+			desc->discharging_max_duration_ms == 0))
+		return false;
+	if (!cm->charger_enabled &&
+			cm->battery_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
+		return false;
 
-		if (duration > desc->charging_max_duration_ms) {
-			dev_info(cm->dev, "Discharging duration exceed %ums\n",
-				 desc->discharging_max_duration_ms);
-			ret = true;
-		}
+	duration = ktime_to_ms(ktime_get()) - cm->charging_start_time;
+	if (duration > desc->charging_max_duration_ms) {
+		dev_info(cm->dev, "Charging duration exceeds %ums\n",
+				desc->charging_max_duration_ms);
+		return true;
 	}
-
-	return ret;
+	return false;
 }
 
 static int cm_get_battery_temperature_by_psy(struct charger_manager *cm,
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] power: supply: charger-manager: Tidy function
  2020-09-02 16:58 [PATCH] power: supply: charger-manager: Tidy function Alex Dewar
@ 2020-09-19 19:32 ` Alex Dewar
  2020-10-08 22:35 ` Sebastian Reichel
  1 sibling, 0 replies; 3+ messages in thread
From: Alex Dewar @ 2020-09-19 19:32 UTC (permalink / raw)
  Cc: Sebastian Reichel, linux-pm, linux-kernel

On 2020-09-02 17:58, Alex Dewar wrote:
> check_charging_duration() contains some copy-pasted code, which makes it
> less readable. Refactor the function to be a bit tidier.
Ping?
>
> I've also fixed a couple of typos.
>
> Signed-off-by: Alex Dewar <alex.dewar90@gmail.com>
> ---
>   drivers/power/supply/charger-manager.c | 39 +++++++++-----------------
>   1 file changed, 14 insertions(+), 25 deletions(-)
>
> diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c
> index 07992821e252..67c7b1fb6601 100644
> --- a/drivers/power/supply/charger-manager.c
> +++ b/drivers/power/supply/charger-manager.c
> @@ -443,42 +443,31 @@ static int try_charger_enable(struct charger_manager *cm, bool enable)
>    * check_charging_duration - Monitor charging/discharging duration
>    * @cm: the Charger Manager representing the battery.
>    *
> - * If whole charging duration exceed 'charging_max_duration_ms',
> + * If whole charging duration exceeds 'charging_max_duration_ms',
>    * cm stop charging to prevent overcharge/overheat. If discharging
> - * duration exceed 'discharging _max_duration_ms', charger cable is
> + * duration exceeds 'discharging _max_duration_ms', charger cable is
>    * attached, after full-batt, cm start charging to maintain fully
>    * charged state for battery.
>    */
>   static int check_charging_duration(struct charger_manager *cm)
>   {
>   	struct charger_desc *desc = cm->desc;
> -	u64 curr = ktime_to_ms(ktime_get());
>   	u64 duration;
> -	int ret = false;
>   
> -	if (!desc->charging_max_duration_ms &&
> -			!desc->discharging_max_duration_ms)
> -		return ret;
> -
> -	if (cm->charger_enabled) {
> -		duration = curr - cm->charging_start_time;
> -
> -		if (duration > desc->charging_max_duration_ms) {
> -			dev_info(cm->dev, "Charging duration exceed %ums\n",
> -				 desc->charging_max_duration_ms);
> -			ret = true;
> -		}
> -	} else if (cm->battery_status == POWER_SUPPLY_STATUS_NOT_CHARGING) {
> -		duration = curr - cm->charging_end_time;
> +	if ((desc->charging_max_duration_ms == 0 &&
> +			desc->discharging_max_duration_ms == 0))
> +		return false;
> +	if (!cm->charger_enabled &&
> +			cm->battery_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
> +		return false;
>   
> -		if (duration > desc->charging_max_duration_ms) {
> -			dev_info(cm->dev, "Discharging duration exceed %ums\n",
> -				 desc->discharging_max_duration_ms);
> -			ret = true;
> -		}
> +	duration = ktime_to_ms(ktime_get()) - cm->charging_start_time;
> +	if (duration > desc->charging_max_duration_ms) {
> +		dev_info(cm->dev, "Charging duration exceeds %ums\n",
> +				desc->charging_max_duration_ms);
> +		return true;
>   	}
> -
> -	return ret;
> +	return false;
>   }
>   
>   static int cm_get_battery_temperature_by_psy(struct charger_manager *cm,


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] power: supply: charger-manager: Tidy function
  2020-09-02 16:58 [PATCH] power: supply: charger-manager: Tidy function Alex Dewar
  2020-09-19 19:32 ` Alex Dewar
@ 2020-10-08 22:35 ` Sebastian Reichel
  1 sibling, 0 replies; 3+ messages in thread
From: Sebastian Reichel @ 2020-10-08 22:35 UTC (permalink / raw)
  To: Alex Dewar; +Cc: linux-pm, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 3084 bytes --]

Hi,

On Wed, Sep 02, 2020 at 05:58:16PM +0100, Alex Dewar wrote:
> check_charging_duration() contains some copy-pasted code, which makes it
> less readable. Refactor the function to be a bit tidier.
> 
> I've also fixed a couple of typos.
> 
> Signed-off-by: Alex Dewar <alex.dewar90@gmail.com>
> ---
>  drivers/power/supply/charger-manager.c | 39 +++++++++-----------------
>  1 file changed, 14 insertions(+), 25 deletions(-)
> 
> diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c
> index 07992821e252..67c7b1fb6601 100644
> --- a/drivers/power/supply/charger-manager.c
> +++ b/drivers/power/supply/charger-manager.c
> @@ -443,42 +443,31 @@ static int try_charger_enable(struct charger_manager *cm, bool enable)
>   * check_charging_duration - Monitor charging/discharging duration
>   * @cm: the Charger Manager representing the battery.
>   *
> - * If whole charging duration exceed 'charging_max_duration_ms',
> + * If whole charging duration exceeds 'charging_max_duration_ms',
>   * cm stop charging to prevent overcharge/overheat. If discharging
> - * duration exceed 'discharging _max_duration_ms', charger cable is
> + * duration exceeds 'discharging _max_duration_ms', charger cable is
>   * attached, after full-batt, cm start charging to maintain fully
>   * charged state for battery.
>   */
>  static int check_charging_duration(struct charger_manager *cm)
>  {
>  	struct charger_desc *desc = cm->desc;
> -	u64 curr = ktime_to_ms(ktime_get());
>  	u64 duration;
> -	int ret = false;
>  
> -	if (!desc->charging_max_duration_ms &&
> -			!desc->discharging_max_duration_ms)
> -		return ret;
> -
> -	if (cm->charger_enabled) {
> -		duration = curr - cm->charging_start_time;
> -
> -		if (duration > desc->charging_max_duration_ms) {
> -			dev_info(cm->dev, "Charging duration exceed %ums\n",
> -				 desc->charging_max_duration_ms);
> -			ret = true;
> -		}
> -	} else if (cm->battery_status == POWER_SUPPLY_STATUS_NOT_CHARGING) {
> -		duration = curr - cm->charging_end_time;

^^^ this is charging_end_time, not charging_start_time

> +	if ((desc->charging_max_duration_ms == 0 &&
> +			desc->discharging_max_duration_ms == 0))
> +		return false;
> +	if (!cm->charger_enabled &&
> +			cm->battery_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
> +		return false;
>  
> -		if (duration > desc->charging_max_duration_ms) {

^^^ this is discharging_max_duration_ms after I applied a fix.

> -			dev_info(cm->dev, "Discharging duration exceed %ums\n",
> -				 desc->discharging_max_duration_ms);
> -			ret = true;
> -		}
> +	duration = ktime_to_ms(ktime_get()) - cm->charging_start_time;
> +	if (duration > desc->charging_max_duration_ms) {
> +		dev_info(cm->dev, "Charging duration exceeds %ums\n",
> +				desc->charging_max_duration_ms);
> +		return true;
>  	}
> -
> -	return ret;
> +	return false;
>  }
>  
>  static int cm_get_battery_temperature_by_psy(struct charger_manager *cm,

So basically code is similar, but not the same.

-- Sebastian

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-10-08 22:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-02 16:58 [PATCH] power: supply: charger-manager: Tidy function Alex Dewar
2020-09-19 19:32 ` Alex Dewar
2020-10-08 22:35 ` Sebastian Reichel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).