All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sebastian Reichel <sebastian.reichel@collabora.com>
To: Ivaylo Dimitrov <ivo.g.dimitrov.75@gmail.com>
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	tony@atomide.com, philipp@uvos.xyz
Subject: Re: [PATCH 1/3] power: cpcap-battery: Do not issue low signal too frequently
Date: Thu, 10 Nov 2022 16:55:10 +0100	[thread overview]
Message-ID: <20221110155510.tobdbaabjoe7ugvl@mercury.elektranox.org> (raw)
In-Reply-To: <1667647544-12945-2-git-send-email-ivo.g.dimitrov.75@gmail.com>

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

Hi,

On Sat, Nov 05, 2022 at 01:25:42PM +0200, Ivaylo Dimitrov wrote:
> It seems that low battery irq may be generated tens of times per second,
> leading to userspace being flooded with unnecessary events.
> 
> Fix that by preventing such events being generated more than once every 30
> seconds.
> 
> Signed-off-by: Ivaylo Dimitrov <ivo.g.dimitrov.75@gmail.com>
> ---

Concept looks ok to me, but the code is slightly racy, since the
thread is flushed before the IRQ is disabled in the remove routine.

>  drivers/power/supply/cpcap-battery.c | 27 ++++++++++++++++++++++++++-
>  1 file changed, 26 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
> index 4676560..8869067 100644
> --- a/drivers/power/supply/cpcap-battery.c
> +++ b/drivers/power/supply/cpcap-battery.c
> @@ -137,6 +137,7 @@ struct cpcap_battery_ddata {
>  	struct power_supply *psy;
>  	struct cpcap_battery_config config;
>  	struct cpcap_battery_state_data state[CPCAP_BATTERY_STATE_NR];
> +	struct delayed_work low_irq_work;
>  	u32 cc_lsb;		/* μAms per LSB */
>  	atomic_t active;
>  	int charge_full;
> @@ -914,9 +915,13 @@ static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
>  		dev_info(ddata->dev, "Coulomb counter calibration done\n");
>  		break;
>  	case CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW:
> -		if (latest->current_ua >= 0)
> +		if (latest->current_ua >= 0 &&
> +		    !delayed_work_pending((&ddata->low_irq_work))) {
>  			dev_warn(ddata->dev, "Battery low at %imV!\n",
>  				latest->voltage / 1000);
> +			schedule_delayed_work(&ddata->low_irq_work, 30 * HZ);
> +			disable_irq_nosync(d->irq);
> +		}
>  		break;
>  	case CPCAP_BATTERY_IRQ_ACTION_POWEROFF:
>  		if (latest->current_ua >= 0 && latest->voltage <= 3200000) {
> @@ -1087,6 +1092,21 @@ static int cpcap_battery_calibrate(struct cpcap_battery_ddata *ddata)
>  	return error;
>  }
>  
> +static void cpcap_battery_lowbph_enable(struct work_struct *work)
> +{
> +	struct delayed_work *d_work = to_delayed_work(work);
> +	struct cpcap_battery_ddata *ddata = container_of(d_work,
> +			struct cpcap_battery_ddata, low_irq_work);
> +	struct cpcap_interrupt_desc *d;
> +
> +	list_for_each_entry(d, &ddata->irq_list, node) {
> +		if (d->action == CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW)
> +			break;
> +	}
> +
> +	enable_irq(d->irq);
> +}
> +
>  #ifdef CONFIG_OF
>  static const struct of_device_id cpcap_battery_id_table[] = {
>  	{
> @@ -1118,6 +1138,8 @@ static int cpcap_battery_probe(struct platform_device *pdev)
>  	if (!ddata)
>  		return -ENOMEM;
>  
> +	INIT_DELAYED_WORK(&ddata->low_irq_work, cpcap_battery_lowbph_enable);

use devm_delayed_work_autocancel() and put it directly before
cpcap_battery_init_interrupts().

>  	cpcap_battery_detect_battery_type(ddata);
>  
>  	INIT_LIST_HEAD(&ddata->irq_list);
> @@ -1185,6 +1207,9 @@ static int cpcap_battery_remove(struct platform_device *pdev)
>  	if (error)
>  		dev_err(&pdev->dev, "could not disable: %i\n", error);
>  
> +	/* make sure to call enable_irq() if needed */
> +	flush_delayed_work(&ddata->low_irq_work);

and this can be dropped afterwards.

> +
>  	return 0;
>  }

Thanks,

-- Sebastian

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

  reply	other threads:[~2022-11-10 15:55 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-05 11:25 [PATCH v2 0/3] power: supply: cpcap-battery improvements Ivaylo Dimitrov
2022-11-05 11:25 ` [PATCH 1/3] power: cpcap-battery: Do not issue low signal too frequently Ivaylo Dimitrov
2022-11-10 15:55   ` Sebastian Reichel [this message]
2022-11-10 18:13     ` Ivaylo Dimitrov
2022-11-05 11:25 ` [PATCH 2/3] power: supply: cpcap-battery: Fix battery identification Ivaylo Dimitrov
2022-11-10 16:05   ` Sebastian Reichel
2022-11-10 16:50     ` Ivaylo Dimitrov
2022-11-15 13:49       ` Tony Lindgren
2022-11-15 15:41         ` Ivaylo Dimitrov
2022-11-17  4:53           ` Tony Lindgren
2022-11-17  8:15             ` Carl Klemm
2022-11-22  7:15               ` Tony Lindgren
2022-11-05 11:25 ` [PATCH 3/3] power: supply: cpcap_battery: Read battery parameters from nvram Ivaylo Dimitrov
2022-12-05 20:28   ` Ivaylo Dimitrov
  -- strict thread matches above, loose matches on Subject: below --
2022-11-15  2:04 [PATCH 1/3] power: cpcap-battery: Do not issue low signal too frequently kernel test robot
2022-11-11  4:45 kernel test robot
2022-11-11  6:15 ` Dan Carpenter
2022-11-01 19:53 [PATCH 0/3] power: supply: cpcap-battery improvements Ivaylo Dimitrov
2022-11-01 19:53 ` [PATCH 1/3] power: cpcap-battery: Do not issue low signal too frequently Ivaylo Dimitrov
2022-11-04  6:09   ` Ivaylo Dimitrov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221110155510.tobdbaabjoe7ugvl@mercury.elektranox.org \
    --to=sebastian.reichel@collabora.com \
    --cc=ivo.g.dimitrov.75@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=philipp@uvos.xyz \
    --cc=tony@atomide.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.