All of lore.kernel.org
 help / color / mirror / Atom feed
From: florian.vaussard@epfl.ch (Florian Vaussard)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 3/3] leds: leds-pwm: Defer led_pwm_set() if PWM can sleep
Date: Fri, 25 Jan 2013 14:04:09 +0100	[thread overview]
Message-ID: <510282C9.5020509@epfl.ch> (raw)
In-Reply-To: <51028041.2060504@ti.com>

Le 25/01/2013 13:53, Peter Ujfalusi a ?crit :
> On 01/25/2013 11:01 AM, Florian Vaussard wrote:
>> Call to led_pwm_set() can happen inside atomic context, like triggers.
>> If the PWM call can sleep, defer using a worker.
>>
>> Signed-off-by: Florian Vaussard <florian.vaussard@epfl.ch>
>> ---
>>   drivers/leds/leds-pwm.c |   45 +++++++++++++++++++++++++++++++++++++++------
>>   1 files changed, 39 insertions(+), 6 deletions(-)
>>
>> diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
>> index a1ea5f6..1ffb6ba 100644
>> --- a/drivers/leds/leds-pwm.c
>> +++ b/drivers/leds/leds-pwm.c
>> @@ -23,12 +23,15 @@
>>   #include <linux/pwm.h>
>>   #include <linux/leds_pwm.h>
>>   #include <linux/slab.h>
>> +#include <linux/workqueue.h>
>>
>>   struct led_pwm_data {
>>   	struct led_classdev	cdev;
>>   	struct pwm_device	*pwm;
>> +	struct work_struct	work;
>>   	unsigned int		active_low;
>>   	unsigned int		period;
>> +	int			duty;
>
> Would it be better if we also store the can_sleep in led_pwm_data struct and
> initialize it when we probe the LEDs? It is not going to change runtime and we
> will save one API call every time the LED has been changed.
>

Indeed, we will save a call each time. I will do it.

>>   };
>>
>>   struct led_pwm_priv {
>> @@ -36,6 +39,20 @@ struct led_pwm_priv {
>>   	struct led_pwm_data leds[0];
>>   };
>>
>> +static void led_pwm_work(struct work_struct *work)
>> +{
>> +	struct led_pwm_data *led_dat =
>> +		container_of(work, struct led_pwm_data, work);
>> +	int new_duty = led_dat->duty;
>> +
>> +	pwm_config(led_dat->pwm, new_duty, led_dat->period);
>> +
>> +	if (new_duty == 0)
>> +		pwm_disable(led_dat->pwm);
>> +	else
>> +		pwm_enable(led_dat->pwm);
>> +}
>
> I think we can remove the duplicated code by doing something like this:
>
> static void __led_pwm_set(struct led_pwm_data *led_dat)
> {
> 	int new_duty = led_dat->duty;
>
> 	pwm_config(led_dat->pwm, new_duty, led_dat->period);
>
> 	if (new_duty == 0)
> 		pwm_disable(led_dat->pwm);
> 	else
> 		pwm_enable(led_dat->pwm);
> }
>
> static void led_pwm_work(struct work_struct *work)
> {
> 	struct led_pwm_data *led_dat =
> 		container_of(work, struct led_pwm_data, work);
>
> 	__led_pwm_set(led_dat);
> }
>
> static void led_pwm_set(struct led_classdev *led_cdev,
> 	enum led_brightness brightness)
> {
> 	struct led_pwm_data *led_dat =
> 		container_of(led_cdev, struct led_pwm_data, cdev);
>   	unsigned int max = led_dat->cdev.max_brightness;
>
> 	led_dat->duty = brightness * led_dat->period / max;
>
> 	if (led_dat->can_sleep)
> 		schedule_work(&led_dat->work);
> 	else
> 		__led_pwm_set(led_dat);
> }
>
> What do you think?
>

It is much better :)

>> +
>>   static void led_pwm_set(struct led_classdev *led_cdev,
>>   	enum led_brightness brightness)
>>   {
>> @@ -43,13 +60,23 @@ static void led_pwm_set(struct led_classdev *led_cdev,
>>   		container_of(led_cdev, struct led_pwm_data, cdev);
>>   	unsigned int max = led_dat->cdev.max_brightness;
>>   	unsigned int period =  led_dat->period;
>> +	int duty;
>>
>> -	if (brightness == 0) {
>> -		pwm_config(led_dat->pwm, 0, period);
>> -		pwm_disable(led_dat->pwm);
>> +	if (brightness == 0)
>> +		duty = 0;
>> +	else
>> +		duty = brightness * period / max;
>> +
>> +	if (pwm_can_sleep(led_dat->pwm)) {
>> +		led_dat->duty = duty;
>> +		schedule_work(&led_dat->work);
>>   	} else {
>> -		pwm_config(led_dat->pwm, brightness * period / max, period);
>> -		pwm_enable(led_dat->pwm);
>> +		pwm_config(led_dat->pwm, duty, period);
>> +
>> +		if (duty == 0)
>> +			pwm_disable(led_dat->pwm);
>> +		else
>> +			pwm_enable(led_dat->pwm);
>>   	}
>>   }
>
>
>
>>
>> @@ -100,6 +127,8 @@ static struct led_pwm_priv *led_pwm_create_of(struct platform_device *pdev)
>>   		led_dat->cdev.brightness = LED_OFF;
>>   		led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
>>
>> +		INIT_WORK(&led_dat->work, led_pwm_work);
>> +
>>   		ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
>>   		if (ret < 0) {
>>   			dev_err(&pdev->dev, "failed to register for %s\n",
>> @@ -153,6 +182,8 @@ static int led_pwm_probe(struct platform_device *pdev)
>>   			led_dat->cdev.max_brightness = cur_led->max_brightness;
>>   			led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
>>
>> +			INIT_WORK(&led_dat->work, led_pwm_work);
>
> If we query the can_sleep before we can skip the INIT_WORK() when it is not set.
>

I will do like this.

>> +
>>   			ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
>>   			if (ret < 0)
>>   				goto err;
>> @@ -180,8 +211,10 @@ static int led_pwm_remove(struct platform_device *pdev)
>>   	struct led_pwm_priv *priv = platform_get_drvdata(pdev);
>>   	int i;
>>
>> -	for (i = 0; i < priv->num_leds; i++)
>> +	for (i = 0; i < priv->num_leds; i++) {
>>   		led_classdev_unregister(&priv->leds[i].cdev);
>> +		cancel_work_sync(&priv->leds[i].work);
>> +	}
>>
>>   	return 0;
>>   }
>>
>
>

Thank you,

Florian

WARNING: multiple messages have this Message-ID (diff)
From: Florian Vaussard <florian.vaussard@epfl.ch>
To: Peter Ujfalusi <peter.ujfalusi@ti.com>
Cc: Bryan Wu <cooloney@gmail.com>, Richard Purdie <rpurdie@rpsys.net>,
	Thierry Reding <thierry.reding@avionic-design.de>,
	linux-leds@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 3/3] leds: leds-pwm: Defer led_pwm_set() if PWM can sleep
Date: Fri, 25 Jan 2013 14:04:09 +0100	[thread overview]
Message-ID: <510282C9.5020509@epfl.ch> (raw)
In-Reply-To: <51028041.2060504@ti.com>

Le 25/01/2013 13:53, Peter Ujfalusi a écrit :
> On 01/25/2013 11:01 AM, Florian Vaussard wrote:
>> Call to led_pwm_set() can happen inside atomic context, like triggers.
>> If the PWM call can sleep, defer using a worker.
>>
>> Signed-off-by: Florian Vaussard <florian.vaussard@epfl.ch>
>> ---
>>   drivers/leds/leds-pwm.c |   45 +++++++++++++++++++++++++++++++++++++++------
>>   1 files changed, 39 insertions(+), 6 deletions(-)
>>
>> diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
>> index a1ea5f6..1ffb6ba 100644
>> --- a/drivers/leds/leds-pwm.c
>> +++ b/drivers/leds/leds-pwm.c
>> @@ -23,12 +23,15 @@
>>   #include <linux/pwm.h>
>>   #include <linux/leds_pwm.h>
>>   #include <linux/slab.h>
>> +#include <linux/workqueue.h>
>>
>>   struct led_pwm_data {
>>   	struct led_classdev	cdev;
>>   	struct pwm_device	*pwm;
>> +	struct work_struct	work;
>>   	unsigned int		active_low;
>>   	unsigned int		period;
>> +	int			duty;
>
> Would it be better if we also store the can_sleep in led_pwm_data struct and
> initialize it when we probe the LEDs? It is not going to change runtime and we
> will save one API call every time the LED has been changed.
>

Indeed, we will save a call each time. I will do it.

>>   };
>>
>>   struct led_pwm_priv {
>> @@ -36,6 +39,20 @@ struct led_pwm_priv {
>>   	struct led_pwm_data leds[0];
>>   };
>>
>> +static void led_pwm_work(struct work_struct *work)
>> +{
>> +	struct led_pwm_data *led_dat =
>> +		container_of(work, struct led_pwm_data, work);
>> +	int new_duty = led_dat->duty;
>> +
>> +	pwm_config(led_dat->pwm, new_duty, led_dat->period);
>> +
>> +	if (new_duty == 0)
>> +		pwm_disable(led_dat->pwm);
>> +	else
>> +		pwm_enable(led_dat->pwm);
>> +}
>
> I think we can remove the duplicated code by doing something like this:
>
> static void __led_pwm_set(struct led_pwm_data *led_dat)
> {
> 	int new_duty = led_dat->duty;
>
> 	pwm_config(led_dat->pwm, new_duty, led_dat->period);
>
> 	if (new_duty == 0)
> 		pwm_disable(led_dat->pwm);
> 	else
> 		pwm_enable(led_dat->pwm);
> }
>
> static void led_pwm_work(struct work_struct *work)
> {
> 	struct led_pwm_data *led_dat =
> 		container_of(work, struct led_pwm_data, work);
>
> 	__led_pwm_set(led_dat);
> }
>
> static void led_pwm_set(struct led_classdev *led_cdev,
> 	enum led_brightness brightness)
> {
> 	struct led_pwm_data *led_dat =
> 		container_of(led_cdev, struct led_pwm_data, cdev);
>   	unsigned int max = led_dat->cdev.max_brightness;
>
> 	led_dat->duty = brightness * led_dat->period / max;
>
> 	if (led_dat->can_sleep)
> 		schedule_work(&led_dat->work);
> 	else
> 		__led_pwm_set(led_dat);
> }
>
> What do you think?
>

It is much better :)

>> +
>>   static void led_pwm_set(struct led_classdev *led_cdev,
>>   	enum led_brightness brightness)
>>   {
>> @@ -43,13 +60,23 @@ static void led_pwm_set(struct led_classdev *led_cdev,
>>   		container_of(led_cdev, struct led_pwm_data, cdev);
>>   	unsigned int max = led_dat->cdev.max_brightness;
>>   	unsigned int period =  led_dat->period;
>> +	int duty;
>>
>> -	if (brightness == 0) {
>> -		pwm_config(led_dat->pwm, 0, period);
>> -		pwm_disable(led_dat->pwm);
>> +	if (brightness == 0)
>> +		duty = 0;
>> +	else
>> +		duty = brightness * period / max;
>> +
>> +	if (pwm_can_sleep(led_dat->pwm)) {
>> +		led_dat->duty = duty;
>> +		schedule_work(&led_dat->work);
>>   	} else {
>> -		pwm_config(led_dat->pwm, brightness * period / max, period);
>> -		pwm_enable(led_dat->pwm);
>> +		pwm_config(led_dat->pwm, duty, period);
>> +
>> +		if (duty == 0)
>> +			pwm_disable(led_dat->pwm);
>> +		else
>> +			pwm_enable(led_dat->pwm);
>>   	}
>>   }
>
>
>
>>
>> @@ -100,6 +127,8 @@ static struct led_pwm_priv *led_pwm_create_of(struct platform_device *pdev)
>>   		led_dat->cdev.brightness = LED_OFF;
>>   		led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
>>
>> +		INIT_WORK(&led_dat->work, led_pwm_work);
>> +
>>   		ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
>>   		if (ret < 0) {
>>   			dev_err(&pdev->dev, "failed to register for %s\n",
>> @@ -153,6 +182,8 @@ static int led_pwm_probe(struct platform_device *pdev)
>>   			led_dat->cdev.max_brightness = cur_led->max_brightness;
>>   			led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
>>
>> +			INIT_WORK(&led_dat->work, led_pwm_work);
>
> If we query the can_sleep before we can skip the INIT_WORK() when it is not set.
>

I will do like this.

>> +
>>   			ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
>>   			if (ret < 0)
>>   				goto err;
>> @@ -180,8 +211,10 @@ static int led_pwm_remove(struct platform_device *pdev)
>>   	struct led_pwm_priv *priv = platform_get_drvdata(pdev);
>>   	int i;
>>
>> -	for (i = 0; i < priv->num_leds; i++)
>> +	for (i = 0; i < priv->num_leds; i++) {
>>   		led_classdev_unregister(&priv->leds[i].cdev);
>> +		cancel_work_sync(&priv->leds[i].work);
>> +	}
>>
>>   	return 0;
>>   }
>>
>
>

Thank you,

Florian

  reply	other threads:[~2013-01-25 13:04 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-25 10:01 [PATCH 0/3] leds-pwm: Defer PWM calls if PWM can sleep Florian Vaussard
2013-01-25 10:01 ` Florian Vaussard
2013-01-25 10:01 ` [PATCH 1/3] pwm: Add pwm_cansleep() as exported API to users Florian Vaussard
2013-01-25 10:01   ` Florian Vaussard
2013-01-25 12:32   ` Peter Ujfalusi
2013-01-25 12:32     ` Peter Ujfalusi
2013-01-25 12:34     ` Florian Vaussard
2013-01-25 12:34       ` Florian Vaussard
2013-01-25 10:01 ` [PATCH 2/3] pwm: Add can_sleep property to drivers Florian Vaussard
2013-01-25 10:01   ` Florian Vaussard
2013-01-25 10:01 ` [PATCH 3/3] leds: leds-pwm: Defer led_pwm_set() if PWM can sleep Florian Vaussard
2013-01-25 10:01   ` Florian Vaussard
2013-01-25 12:53   ` Peter Ujfalusi
2013-01-25 12:53     ` Peter Ujfalusi
2013-01-25 13:04     ` Florian Vaussard [this message]
2013-01-25 13:04       ` Florian Vaussard
2013-01-25 13:07       ` Peter Ujfalusi
2013-01-25 13:07         ` Peter Ujfalusi

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=510282C9.5020509@epfl.ch \
    --to=florian.vaussard@epfl.ch \
    --cc=linux-arm-kernel@lists.infradead.org \
    /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.