linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Ondřej Jirman" <megous@megous.com>
To: linux-kernel@vger.kernel.org
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>,
	Pavel Machek <pavel@ucw.cz>, Dan Murphy <dmurphy@ti.com>,
	"open list:LED SUBSYSTEM" <linux-leds@vger.kernel.org>
Subject: Re: [PATCH RFC] leds: Add support for per-LED device triggers
Date: Thu, 2 Jul 2020 16:51:29 +0200	[thread overview]
Message-ID: <20200702145129.7k5sgvexmze2sktx@core.my.home> (raw)
In-Reply-To: <20200702144712.1994685-1-megous@megous.com>

Hi,

On Thu, Jul 02, 2020 at 04:47:11PM +0200, megous hlavni wrote:
> Some LED controllers may come with an internal HW triggering mechanism
> for the LED and an ability to switch between user control of the LED,
> or the internal control. One such example is AXP20X PMIC, that allows
> wither for user control of the LED, or for internal control based on
> the state of the battery charger.
> 
> Add support for registering per-LED device trigger.
> 
> Names of private triggers need to be globally unique, but may clash
> with other private triggers. This is enforced during trigger
> registration. Developers can register private triggers just like
> the normal triggers, by setting private_led to a classdev
> of the LED the trigger is associated with.

I've prepared this patch based on the note here:

 https://www.kernel.org/doc/html/latest/leds/leds-class.html

  Future Development:
  
  At the moment, a trigger can’t be created specifically for a single LED. There
  are a number of cases where a trigger might only be mappable to a particular
  LED (ACPI?). The addition of triggers provided by the LED driver should cover
  this option and be possible to add without breaking the current interface.

Does it look good?

I plan to use this interface in the axp20x driver I've sent previously,
if this patch is acceptable.

thank you and regards,
	o.

> Signed-off-by: Ondrej Jirman <megous@megous.com>
> ---
>  drivers/leds/led-triggers.c | 12 +++++++++---
>  include/linux/leds.h        |  3 +++
>  2 files changed, 12 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/leds/led-triggers.c b/drivers/leds/led-triggers.c
> index 79e30d2cb7a5..3011e2658404 100644
> --- a/drivers/leds/led-triggers.c
> +++ b/drivers/leds/led-triggers.c
> @@ -50,7 +50,8 @@ ssize_t led_trigger_write(struct file *filp, struct kobject *kobj,
>  
>  	down_read(&triggers_list_lock);
>  	list_for_each_entry(trig, &trigger_list, next_trig) {
> -		if (sysfs_streq(buf, trig->name)) {
> +		if (sysfs_streq(buf, trig->name) &&
> +		    (!trig->private_led || trig->private_led == led_cdev)) {
>  			down_write(&led_cdev->trigger_lock);
>  			led_trigger_set(led_cdev, trig);
>  			up_write(&led_cdev->trigger_lock);
> @@ -96,6 +97,9 @@ static int led_trigger_format(char *buf, size_t size,
>  		bool hit = led_cdev->trigger &&
>  			!strcmp(led_cdev->trigger->name, trig->name);
>  
> +		if (trig->private_led && trig->private_led != led_cdev)
> +			continue;
> +
>  		len += led_trigger_snprintf(buf + len, size - len,
>  					    " %s%s%s", hit ? "[" : "",
>  					    trig->name, hit ? "]" : "");
> @@ -243,7 +247,8 @@ void led_trigger_set_default(struct led_classdev *led_cdev)
>  	down_read(&triggers_list_lock);
>  	down_write(&led_cdev->trigger_lock);
>  	list_for_each_entry(trig, &trigger_list, next_trig) {
> -		if (!strcmp(led_cdev->default_trigger, trig->name)) {
> +		if (!strcmp(led_cdev->default_trigger, trig->name) &&
> +		    (!trig->private_led || trig->private_led == led_cdev)) {
>  			led_cdev->flags |= LED_INIT_DEFAULT_TRIGGER;
>  			led_trigger_set(led_cdev, trig);
>  			break;
> @@ -280,7 +285,8 @@ int led_trigger_register(struct led_trigger *trig)
>  	down_write(&triggers_list_lock);
>  	/* Make sure the trigger's name isn't already in use */
>  	list_for_each_entry(_trig, &trigger_list, next_trig) {
> -		if (!strcmp(_trig->name, trig->name)) {
> +		if (!strcmp(_trig->name, trig->name) &&
> +		    (!_trig->private_led || _trig->private_led == trig->private_led)) {
>  			up_write(&triggers_list_lock);
>  			return -EEXIST;
>  		}
> diff --git a/include/linux/leds.h b/include/linux/leds.h
> index 2451962d1ec5..0d7577c752ad 100644
> --- a/include/linux/leds.h
> +++ b/include/linux/leds.h
> @@ -345,6 +345,9 @@ struct led_trigger {
>  	int		(*activate)(struct led_classdev *led_cdev);
>  	void		(*deactivate)(struct led_classdev *led_cdev);
>  
> +	/* LED-private triggers have the LED device set here. */
> +	struct led_classdev *private_led;

I'm not sure what the best descriptive name is here. ^

regards,
	o.

>  	/* LEDs under control by this trigger (for simple triggers) */
>  	rwlock_t	  leddev_list_lock;
>  	struct list_head  led_cdevs;
> -- 
> 2.27.0
> 

  reply	other threads:[~2020-07-02 14:51 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-02 14:47 [PATCH RFC] leds: Add support for per-LED device triggers Ondrej Jirman
2020-07-02 14:51 ` Ondřej Jirman [this message]
2020-07-03 10:06 ` Marek Behún
2020-07-03 13:08   ` Ondřej Jirman
2020-07-08 14:55     ` Marek Behún
2020-07-04 12:59   ` Pavel Machek
2020-07-08 14:56     ` Marek Behún
2020-07-04 12:04 ` Pavel Machek
2020-07-04 12:17   ` Ondřej Jirman
2020-07-04 20:07     ` Pavel Machek
2020-07-11 10:04 ` Pavel Machek
2020-07-11 21:01   ` Ondřej Jirman
2020-07-12  7:25     ` Pavel Machek
2020-07-12 13:49       ` Ondřej Jirman
2020-07-12 19:11         ` Pavel Machek
2020-07-12 21:10           ` Marek Behun
2020-07-12 22:12           ` Ondřej Jirman
2020-07-12 22:38           ` Ondřej Jirman
2020-07-12 23:15             ` Marek Behun
2020-07-12 23:18               ` Marek Behun
2020-07-13  7:12                 ` Pavel Machek
2020-07-12 23:20               ` Ondřej Jirman
2020-07-13  1:56           ` Ondřej Jirman
2020-07-15 17:07   ` Marek Behún
2020-07-15 17:55     ` Marek Behún

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=20200702145129.7k5sgvexmze2sktx@core.my.home \
    --to=megous@megous.com \
    --cc=dmurphy@ti.com \
    --cc=jacek.anaszewski@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=pavel@ucw.cz \
    /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 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).