diff mbox series

[v5,1/2] leds: core: Introduce LED pattern trigger

Message ID 1dc5d394324b2bf1ffe229b8e42691fab6d749e0.1533556992.git.baolin.wang@linaro.org
State Superseded
Headers show
Series [v5,1/2] leds: core: Introduce LED pattern trigger | expand

Commit Message

(Exiting) Baolin Wang Aug. 6, 2018, 12:05 p.m. UTC
Some LED controllers have support for autonomously controlling
brightness over time, according to some preprogrammed pattern or
function.

This patch adds pattern trigger that LED device can configure the
pattern and trigger it.

Signed-off-by: Raphael Teysseyre <rteysseyre@gmail.com>

Signed-off-by: Baolin Wang <baolin.wang@linaro.org>

---
Changes from v4:
 - Change the repeat file to return the originally written number.
 - Improve comments.
 - Fix some build warnings.

Changes from v3:
 - Reset pattern number to 0 if user provides incorrect pattern string.
 - Support one pattern.

Changes from v2:
 - Remove hardware_pattern boolen.
 - Chnage the pattern string format.

Changes from v1:
 - Use ATTRIBUTE_GROUPS() to define attributes.
 - Introduce hardware_pattern flag to determine if software pattern
 or hardware pattern.
 - Re-implement pattern_trig_store_pattern() function.
 - Remove pattern_get() interface.
 - Improve comments.
 - Other small optimization.
---
 .../ABI/testing/sysfs-class-led-trigger-pattern    |   24 ++
 drivers/leds/trigger/Kconfig                       |    7 +
 drivers/leds/trigger/Makefile                      |    1 +
 drivers/leds/trigger/ledtrig-pattern.c             |  266 ++++++++++++++++++++
 include/linux/leds.h                               |   16 ++
 5 files changed, 314 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-led-trigger-pattern
 create mode 100644 drivers/leds/trigger/ledtrig-pattern.c

-- 
1.7.9.5

Comments

Jacek Anaszewski Aug. 7, 2018, 9:54 p.m. UTC | #1
Hi Baolin,

Thank you for addressing the review remarks.
Since the patch set is targeted for 4.19, then we have three weeks
before it will be merged to the for-next anyway. That said, I propose
one more modification, please take a look below.

On 08/06/2018 02:05 PM, Baolin Wang wrote:
> Some LED controllers have support for autonomously controlling

> brightness over time, according to some preprogrammed pattern or

> function.

> 

> This patch adds pattern trigger that LED device can configure the

> pattern and trigger it.

> 

> Signed-off-by: Raphael Teysseyre <rteysseyre@gmail.com>

> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>

> ---

> Changes from v4:

>  - Change the repeat file to return the originally written number.

>  - Improve comments.

>  - Fix some build warnings.

> 

> Changes from v3:

>  - Reset pattern number to 0 if user provides incorrect pattern string.

>  - Support one pattern.

> 

> Changes from v2:

>  - Remove hardware_pattern boolen.

>  - Chnage the pattern string format.

> 

> Changes from v1:

>  - Use ATTRIBUTE_GROUPS() to define attributes.

>  - Introduce hardware_pattern flag to determine if software pattern

>  or hardware pattern.

>  - Re-implement pattern_trig_store_pattern() function.

>  - Remove pattern_get() interface.

>  - Improve comments.

>  - Other small optimization.

> ---

>  .../ABI/testing/sysfs-class-led-trigger-pattern    |   24 ++

>  drivers/leds/trigger/Kconfig                       |    7 +

>  drivers/leds/trigger/Makefile                      |    1 +

>  drivers/leds/trigger/ledtrig-pattern.c             |  266 ++++++++++++++++++++

>  include/linux/leds.h                               |   16 ++

>  5 files changed, 314 insertions(+)

>  create mode 100644 Documentation/ABI/testing/sysfs-class-led-trigger-pattern

>  create mode 100644 drivers/leds/trigger/ledtrig-pattern.c

> 

> diff --git a/Documentation/ABI/testing/sysfs-class-led-trigger-pattern b/Documentation/ABI/testing/sysfs-class-led-trigger-pattern

> new file mode 100644

> index 0000000..bc7475f

> --- /dev/null

> +++ b/Documentation/ABI/testing/sysfs-class-led-trigger-pattern

> @@ -0,0 +1,24 @@

> +What:		/sys/class/leds/<led>/pattern

> +Date:		August 2018

> +KernelVersion:	4.19

> +Description:

> +		Specify a pattern for the LED, for LED hardware that support

> +		altering the brightness as a function of time.

> +

> +		The pattern is given by a series of tuples, of brightness and

> +		duration (ms). The LED is expected to traverse the series and

> +		each brightness value for the specified duration. Duration of

> +		0 means brightness should immediately change to new value.

> +

> +		The format of the pattern values should be:

> +		"brightness_1 duration_1 brightness_2 duration_2 brightness_3

> +		duration_3 ...".

> +

> +What:		/sys/class/leds/<led>/repeat

> +Date:		August 2018

> +KernelVersion:	4.19

> +Description:

> +		Specify a pattern repeat number. 0 means repeat indefinitely.

> +

> +		This file will always return the originally written repeat

> +		number.

> diff --git a/drivers/leds/trigger/Kconfig b/drivers/leds/trigger/Kconfig

> index 4018af7..b76fc3c 100644

> --- a/drivers/leds/trigger/Kconfig

> +++ b/drivers/leds/trigger/Kconfig

> @@ -129,4 +129,11 @@ config LEDS_TRIGGER_NETDEV

>  	  This allows LEDs to be controlled by network device activity.

>  	  If unsure, say Y.

>  

> +config LEDS_TRIGGER_PATTERN

> +	tristate "LED Pattern Trigger"

> +	help

> +	  This allows LEDs to be controlled by a software or hardware pattern

> +	  which is a series of tuples, of brightness and duration (ms).

> +	  If unsure, say N

> +

>  endif # LEDS_TRIGGERS

> diff --git a/drivers/leds/trigger/Makefile b/drivers/leds/trigger/Makefile

> index f3cfe19..9bcb64e 100644

> --- a/drivers/leds/trigger/Makefile

> +++ b/drivers/leds/trigger/Makefile

> @@ -13,3 +13,4 @@ obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT)	+= ledtrig-transient.o

>  obj-$(CONFIG_LEDS_TRIGGER_CAMERA)	+= ledtrig-camera.o

>  obj-$(CONFIG_LEDS_TRIGGER_PANIC)	+= ledtrig-panic.o

>  obj-$(CONFIG_LEDS_TRIGGER_NETDEV)	+= ledtrig-netdev.o

> +obj-$(CONFIG_LEDS_TRIGGER_PATTERN)	+= ledtrig-pattern.o

> diff --git a/drivers/leds/trigger/ledtrig-pattern.c b/drivers/leds/trigger/ledtrig-pattern.c

> new file mode 100644

> index 0000000..63b94a2

> --- /dev/null

> +++ b/drivers/leds/trigger/ledtrig-pattern.c

> @@ -0,0 +1,266 @@

> +// SPDX-License-Identifier: GPL-2.0

> +

> +/*

> + * LED pattern trigger

> + *

> + * Idea discussed with Pavel Machek. Raphael Teysseyre implemented

> + * the first version, Baolin Wang simplified and improved the approach.

> + */

> +

> +#include <linux/kernel.h>

> +#include <linux/leds.h>

> +#include <linux/module.h>

> +#include <linux/mutex.h>

> +#include <linux/slab.h>

> +#include <linux/timer.h>

> +

> +#define MAX_PATTERNS		1024

> +

> +struct pattern_trig_data {

> +	struct led_classdev *led_cdev;

> +	struct led_pattern patterns[MAX_PATTERNS];

> +	struct led_pattern *curr;

> +	struct led_pattern *next;

> +	struct mutex lock;

> +	u32 npatterns;

> +	u32 repeat;

> +	u32 last_repeat;

> +	bool is_indefinite;

> +	struct timer_list timer;

> +};

> +

> +static void pattern_trig_update_patterns(struct pattern_trig_data *data)

> +{

> +	data->curr = data->next;

> +	if (!data->is_indefinite && data->curr == data->patterns)

> +		data->repeat--;

> +

> +	if (data->next == data->patterns + data->npatterns - 1)

> +		data->next = data->patterns;

> +	else

> +		data->next++;

> +}

> +

> +static void pattern_trig_timer_function(struct timer_list *t)

> +{

> +	struct pattern_trig_data *data = from_timer(data, t, timer);

> +

> +	mutex_lock(&data->lock);

> +

> +	if (!data->is_indefinite && !data->repeat) {

> +		mutex_unlock(&data->lock);

> +		return;

> +	}

> +

> +	led_set_brightness(data->led_cdev, data->curr->brightness);

> +	mod_timer(&data->timer, jiffies + msecs_to_jiffies(data->curr->delta_t));

> +	pattern_trig_update_patterns(data);

> +

> +	mutex_unlock(&data->lock);

> +}

> +

> +static int pattern_trig_start_pattern(struct pattern_trig_data *data,

> +				      struct led_classdev *led_cdev)

> +{

> +	if (!data->npatterns)

> +		return 0;

> +

> +	if (led_cdev->pattern_set) {

> +		return led_cdev->pattern_set(led_cdev, data->patterns,

> +					     data->npatterns, data->repeat);


I think that it would be more flexible if software pattern fallback
was applied in case of pattern_set failure. Otherwise, it would
lead to the situation where LED class devices that support hardware
blinking couldn't be applied the same set of patterns as LED class
devices that don't implement pattern_set. The latter will always have to
resort to using software pattern engine which will accept far greater
amount of pattern combinations.

In this case we need to discuss on what basis the decision will be
made on whether hardware or software engine will be used.

Possible options coming to mind:
- an interface will be provided to determine max difference between
  the settings supported by the hardware and the settings requested by
  the user, that will result in aligning user's setting to the hardware
  capabilities
- the above alignment rate will be predefined instead
- hardware engine will be used only if user requests supported settings
  on the whole span of the requested pattern
- in each of the above cases it would be worth to think of the
  interface to show the scope of the settings supported by hardware

The same issue applies to the already available timer trigger.
So far the policy implemented by the drivers implementing blink_set
op varies.

-- 
Best regards,
Jacek Anaszewski
(Exiting) Baolin Wang Aug. 8, 2018, 6:01 a.m. UTC | #2
Hi Jacek,

On 8 August 2018 at 05:54, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:
> Hi Baolin,

>

> Thank you for addressing the review remarks.

> Since the patch set is targeted for 4.19, then we have three weeks

> before it will be merged to the for-next anyway. That said, I propose

> one more modification, please take a look below.


Sure.

>

> On 08/06/2018 02:05 PM, Baolin Wang wrote:

>> Some LED controllers have support for autonomously controlling

>> brightness over time, according to some preprogrammed pattern or

>> function.

>>

>> This patch adds pattern trigger that LED device can configure the

>> pattern and trigger it.

>>

>> Signed-off-by: Raphael Teysseyre <rteysseyre@gmail.com>

>> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>

>> ---

>> Changes from v4:

>>  - Change the repeat file to return the originally written number.

>>  - Improve comments.

>>  - Fix some build warnings.

>>

>> Changes from v3:

>>  - Reset pattern number to 0 if user provides incorrect pattern string.

>>  - Support one pattern.

>>

>> Changes from v2:

>>  - Remove hardware_pattern boolen.

>>  - Chnage the pattern string format.

>>

>> Changes from v1:

>>  - Use ATTRIBUTE_GROUPS() to define attributes.

>>  - Introduce hardware_pattern flag to determine if software pattern

>>  or hardware pattern.

>>  - Re-implement pattern_trig_store_pattern() function.

>>  - Remove pattern_get() interface.

>>  - Improve comments.

>>  - Other small optimization.

>> ---

>>  .../ABI/testing/sysfs-class-led-trigger-pattern    |   24 ++

>>  drivers/leds/trigger/Kconfig                       |    7 +

>>  drivers/leds/trigger/Makefile                      |    1 +

>>  drivers/leds/trigger/ledtrig-pattern.c             |  266 ++++++++++++++++++++

>>  include/linux/leds.h                               |   16 ++

>>  5 files changed, 314 insertions(+)

>>  create mode 100644 Documentation/ABI/testing/sysfs-class-led-trigger-pattern

>>  create mode 100644 drivers/leds/trigger/ledtrig-pattern.c

>>

>> diff --git a/Documentation/ABI/testing/sysfs-class-led-trigger-pattern b/Documentation/ABI/testing/sysfs-class-led-trigger-pattern

>> new file mode 100644

>> index 0000000..bc7475f

>> --- /dev/null

>> +++ b/Documentation/ABI/testing/sysfs-class-led-trigger-pattern

>> @@ -0,0 +1,24 @@

>> +What:                /sys/class/leds/<led>/pattern

>> +Date:                August 2018

>> +KernelVersion:       4.19

>> +Description:

>> +             Specify a pattern for the LED, for LED hardware that support

>> +             altering the brightness as a function of time.

>> +

>> +             The pattern is given by a series of tuples, of brightness and

>> +             duration (ms). The LED is expected to traverse the series and

>> +             each brightness value for the specified duration. Duration of

>> +             0 means brightness should immediately change to new value.

>> +

>> +             The format of the pattern values should be:

>> +             "brightness_1 duration_1 brightness_2 duration_2 brightness_3

>> +             duration_3 ...".

>> +

>> +What:                /sys/class/leds/<led>/repeat

>> +Date:                August 2018

>> +KernelVersion:       4.19

>> +Description:

>> +             Specify a pattern repeat number. 0 means repeat indefinitely.

>> +

>> +             This file will always return the originally written repeat

>> +             number.

>> diff --git a/drivers/leds/trigger/Kconfig b/drivers/leds/trigger/Kconfig

>> index 4018af7..b76fc3c 100644

>> --- a/drivers/leds/trigger/Kconfig

>> +++ b/drivers/leds/trigger/Kconfig

>> @@ -129,4 +129,11 @@ config LEDS_TRIGGER_NETDEV

>>         This allows LEDs to be controlled by network device activity.

>>         If unsure, say Y.

>>

>> +config LEDS_TRIGGER_PATTERN

>> +     tristate "LED Pattern Trigger"

>> +     help

>> +       This allows LEDs to be controlled by a software or hardware pattern

>> +       which is a series of tuples, of brightness and duration (ms).

>> +       If unsure, say N

>> +

>>  endif # LEDS_TRIGGERS

>> diff --git a/drivers/leds/trigger/Makefile b/drivers/leds/trigger/Makefile

>> index f3cfe19..9bcb64e 100644

>> --- a/drivers/leds/trigger/Makefile

>> +++ b/drivers/leds/trigger/Makefile

>> @@ -13,3 +13,4 @@ obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT)        += ledtrig-transient.o

>>  obj-$(CONFIG_LEDS_TRIGGER_CAMERA)    += ledtrig-camera.o

>>  obj-$(CONFIG_LEDS_TRIGGER_PANIC)     += ledtrig-panic.o

>>  obj-$(CONFIG_LEDS_TRIGGER_NETDEV)    += ledtrig-netdev.o

>> +obj-$(CONFIG_LEDS_TRIGGER_PATTERN)   += ledtrig-pattern.o

>> diff --git a/drivers/leds/trigger/ledtrig-pattern.c b/drivers/leds/trigger/ledtrig-pattern.c

>> new file mode 100644

>> index 0000000..63b94a2

>> --- /dev/null

>> +++ b/drivers/leds/trigger/ledtrig-pattern.c

>> @@ -0,0 +1,266 @@

>> +// SPDX-License-Identifier: GPL-2.0

>> +

>> +/*

>> + * LED pattern trigger

>> + *

>> + * Idea discussed with Pavel Machek. Raphael Teysseyre implemented

>> + * the first version, Baolin Wang simplified and improved the approach.

>> + */

>> +

>> +#include <linux/kernel.h>

>> +#include <linux/leds.h>

>> +#include <linux/module.h>

>> +#include <linux/mutex.h>

>> +#include <linux/slab.h>

>> +#include <linux/timer.h>

>> +

>> +#define MAX_PATTERNS         1024

>> +

>> +struct pattern_trig_data {

>> +     struct led_classdev *led_cdev;

>> +     struct led_pattern patterns[MAX_PATTERNS];

>> +     struct led_pattern *curr;

>> +     struct led_pattern *next;

>> +     struct mutex lock;

>> +     u32 npatterns;

>> +     u32 repeat;

>> +     u32 last_repeat;

>> +     bool is_indefinite;

>> +     struct timer_list timer;

>> +};

>> +

>> +static void pattern_trig_update_patterns(struct pattern_trig_data *data)

>> +{

>> +     data->curr = data->next;

>> +     if (!data->is_indefinite && data->curr == data->patterns)

>> +             data->repeat--;

>> +

>> +     if (data->next == data->patterns + data->npatterns - 1)

>> +             data->next = data->patterns;

>> +     else

>> +             data->next++;

>> +}

>> +

>> +static void pattern_trig_timer_function(struct timer_list *t)

>> +{

>> +     struct pattern_trig_data *data = from_timer(data, t, timer);

>> +

>> +     mutex_lock(&data->lock);

>> +

>> +     if (!data->is_indefinite && !data->repeat) {

>> +             mutex_unlock(&data->lock);

>> +             return;

>> +     }

>> +

>> +     led_set_brightness(data->led_cdev, data->curr->brightness);

>> +     mod_timer(&data->timer, jiffies + msecs_to_jiffies(data->curr->delta_t));

>> +     pattern_trig_update_patterns(data);

>> +

>> +     mutex_unlock(&data->lock);

>> +}

>> +

>> +static int pattern_trig_start_pattern(struct pattern_trig_data *data,

>> +                                   struct led_classdev *led_cdev)

>> +{

>> +     if (!data->npatterns)

>> +             return 0;

>> +

>> +     if (led_cdev->pattern_set) {

>> +             return led_cdev->pattern_set(led_cdev, data->patterns,

>> +                                          data->npatterns, data->repeat);

>

> I think that it would be more flexible if software pattern fallback

> was applied in case of pattern_set failure. Otherwise, it would

> lead to the situation where LED class devices that support hardware

> blinking couldn't be applied the same set of patterns as LED class

> devices that don't implement pattern_set. The latter will always have to

> resort to using software pattern engine which will accept far greater

> amount of pattern combinations.


Hmmm, I am not sure this is useful for hardware pattern, since the LED
hardware can be diverse which is hard to be compatible with software
pattern.

For example, for our SC27XX LED, it only supports 4 hardware patterns
setting (low time, rising time, high time and falling time) to make it
work at breathing mode. If user provides 3 or 5 patterns values, it
will failed to issue pattern_set(). But at this time, we don't want to
use software pattern instead since it will be not the breathing mode
expected by user. I don't know if there are other special LED
hardware.

So I think we should let LED driver to handle this case, if failed to
issue pattern_set(), the LED driver can set one group default hardware
patterns, or turn off the LED hardware pattern or other error handling
method supplied by LED driver. That will not combine software pattern
and hardware pattern together to make things complicated.

So what do you think?

> In this case we need to discuss on what basis the decision will be

> made on whether hardware or software engine will be used.

>

> Possible options coming to mind:

> - an interface will be provided to determine max difference between

>   the settings supported by the hardware and the settings requested by

>   the user, that will result in aligning user's setting to the hardware

>   capabilities

> - the above alignment rate will be predefined instead

> - hardware engine will be used only if user requests supported settings

>   on the whole span of the requested pattern

> - in each of the above cases it would be worth to think of the

>   interface to show the scope of the settings supported by hardware

>

> The same issue applies to the already available timer trigger.

> So far the policy implemented by the drivers implementing blink_set

> op varies.

>

> --

> Best regards,

> Jacek Anaszewski




-- 
Baolin Wang
Best Regards
Jacek Anaszewski Aug. 9, 2018, 1:21 p.m. UTC | #3
Hi Baolin,

On 08/09/2018 07:48 AM, Baolin Wang wrote:
[...]
>>>>> +static int pattern_trig_start_pattern(struct pattern_trig_data *data,

>>>>> +                                   struct led_classdev *led_cdev)

>>>>> +{

>>>>> +     if (!data->npatterns)

>>>>> +             return 0;

>>>>> +

>>>>> +     if (led_cdev->pattern_set) {

>>>>> +             return led_cdev->pattern_set(led_cdev, data->patterns,

>>>>> +                                          data->npatterns, data->repeat);

>>>>

>>>> I think that it would be more flexible if software pattern fallback

>>>> was applied in case of pattern_set failure. Otherwise, it would

>>>> lead to the situation where LED class devices that support hardware

>>>> blinking couldn't be applied the same set of patterns as LED class

>>>> devices that don't implement pattern_set. The latter will always have to

>>>> resort to using software pattern engine which will accept far greater

>>>> amount of pattern combinations.

>>>

>>> Hmmm, I am not sure this is useful for hardware pattern, since the LED

>>> hardware can be diverse which is hard to be compatible with software

>>> pattern.

>>>

>>> For example, for our SC27XX LED, it only supports 4 hardware patterns

>>> setting (low time, rising time, high time and falling time) to make it

>>> work at breathing mode. If user provides 3 or 5 patterns values, it

>>> will failed to issue pattern_set(). But at this time, we don't want to

>>> use software pattern instead since it will be not the breathing mode

>>> expected by user. I don't know if there are other special LED

>>> hardware.

>>

>> Good point. So this is the issue we should dwell on, since the

>> requested pattern effect should look similar on all devices.

>> Of course in case of software pattern it will be often impossible

>> to achieve the same fluency. Similarly as in case of rendering graphics

>> with and without acceleration.

>>

>> In case of your device, I'd say that we'd need more complex description

>> of breathing mode pattern. More complex than just four intervals.

>> It should reflect all the intervals the hardware engine needs to perform

>> to achieve the breathing effect. Can this information be inferred from

>> the docs?

> 

>>From our docs, it only provides registers to set the low time, rising

> time, high time and falling time (value unit is 0.125s and max value

> is 63 = 7.875s) to enable breathing mode. So each interval value can

> be 1 ~ 63. But that is still hard for software pattern to simulate the

> breathing mode performed by hardware pattern.


Software fallback is not expected to show similar performance to the
hardware engine on the whole span of the supported time range.

Having min rise time value at 125ms, and given that max_brightness
is 255, then we'd have 255 / 125 = 2.04 of brightness level rise per
1ms. So, the pattern for rising edge could look like (assuming we
stop at 254):

0 1 2 1 4 1 6 1 8 1 10 1 ... 254 1

Now, I'm starting to wonder if we shouldn't have specialized trigger
for breathing patterns, that would accept brightness level change per
time period. Pattern trigger needs more flexibility and inferring if the
hardware can handle given series of pattern intervals would entail
unnecessary code complexity.

Such breathing trigger would require triplets comprised of
start brightness, end brightness and a duration of the brightness
transition.

-- 
Best regards,
Jacek Anaszewski
(Exiting) Baolin Wang Aug. 10, 2018, 3:26 p.m. UTC | #4
Hi Jacek,

On 9 August 2018 at 21:21, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:
> Hi Baolin,

>

> On 08/09/2018 07:48 AM, Baolin Wang wrote:

> [...]

>>>>>> +static int pattern_trig_start_pattern(struct pattern_trig_data *data,

>>>>>> +                                   struct led_classdev *led_cdev)

>>>>>> +{

>>>>>> +     if (!data->npatterns)

>>>>>> +             return 0;

>>>>>> +

>>>>>> +     if (led_cdev->pattern_set) {

>>>>>> +             return led_cdev->pattern_set(led_cdev, data->patterns,

>>>>>> +                                          data->npatterns, data->repeat);

>>>>>

>>>>> I think that it would be more flexible if software pattern fallback

>>>>> was applied in case of pattern_set failure. Otherwise, it would

>>>>> lead to the situation where LED class devices that support hardware

>>>>> blinking couldn't be applied the same set of patterns as LED class

>>>>> devices that don't implement pattern_set. The latter will always have to

>>>>> resort to using software pattern engine which will accept far greater

>>>>> amount of pattern combinations.

>>>>

>>>> Hmmm, I am not sure this is useful for hardware pattern, since the LED

>>>> hardware can be diverse which is hard to be compatible with software

>>>> pattern.

>>>>

>>>> For example, for our SC27XX LED, it only supports 4 hardware patterns

>>>> setting (low time, rising time, high time and falling time) to make it

>>>> work at breathing mode. If user provides 3 or 5 patterns values, it

>>>> will failed to issue pattern_set(). But at this time, we don't want to

>>>> use software pattern instead since it will be not the breathing mode

>>>> expected by user. I don't know if there are other special LED

>>>> hardware.

>>>

>>> Good point. So this is the issue we should dwell on, since the

>>> requested pattern effect should look similar on all devices.

>>> Of course in case of software pattern it will be often impossible

>>> to achieve the same fluency. Similarly as in case of rendering graphics

>>> with and without acceleration.

>>>

>>> In case of your device, I'd say that we'd need more complex description

>>> of breathing mode pattern. More complex than just four intervals.

>>> It should reflect all the intervals the hardware engine needs to perform

>>> to achieve the breathing effect. Can this information be inferred from

>>> the docs?

>>

>>>From our docs, it only provides registers to set the low time, rising

>> time, high time and falling time (value unit is 0.125s and max value

>> is 63 = 7.875s) to enable breathing mode. So each interval value can

>> be 1 ~ 63. But that is still hard for software pattern to simulate the

>> breathing mode performed by hardware pattern.

>

> Software fallback is not expected to show similar performance to the

> hardware engine on the whole span of the supported time range.

>

> Having min rise time value at 125ms, and given that max_brightness

> is 255, then we'd have 255 / 125 = 2.04 of brightness level rise per

> 1ms. So, the pattern for rising edge could look like (assuming we

> stop at 254):

>

> 0 1 2 1 4 1 6 1 8 1 10 1 ... 254 1


Right, maybe this can work, maybe not. Since we can met different
cases when failed to issue pattern_set(). Maybe the LED hardware
occurs some errors, in this case we should shutdown the LED, not
enable the software pattern instead, which still can not work. Maybe
driver can set NULL for pattern_set/clear interfaces to disable
hardware pattern, then next time user will perform the patterns with
software pattern mode.

For our SC27XX LED, like I said if user provides only 3 pattern values
which will failed to issue pattern_set(). But I still do not need use
software patterns to show similar performance, instead it will still
keep the last hardware pattern performance ( It did not overlap the
previous hardware pattern setting). Maybe different drivers have
different error handling, that's why I think we can leave driver to
choose the proper way to handle.

Honestly, can we keep this pattern trigger simple and easy at first?
If some drivers want to use software patterns to perform again once
their hardware patterns performed failed (IMHO our SC27XX LED do not
need), then we can add this feature, at that time we will have users
to test and optimize the feature. Maybe I am wrong:)

> Now, I'm starting to wonder if we shouldn't have specialized trigger

> for breathing patterns, that would accept brightness level change per

> time period. Pattern trigger needs more flexibility and inferring if the

> hardware can handle given series of pattern intervals would entail

> unnecessary code complexity.

>

> Such breathing trigger would require triplets comprised of

> start brightness, end brightness and a duration of the brightness

> transition.


But this is the only place we can set the hardware patterns according
to our previous discussion. Thanks.

-- 
Baolin Wang
Best Regards
(Exiting) Baolin Wang Aug. 11, 2018, 2:17 a.m. UTC | #5
Hi Jacek,

On 11 August 2018 at 02:10, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:
> Hi Baolin,

>

> On 08/10/2018 05:26 PM, Baolin Wang wrote:

>> Hi Jacek,

>>

>> On 9 August 2018 at 21:21, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:

>>> Hi Baolin,

>>>

>>> On 08/09/2018 07:48 AM, Baolin Wang wrote:

>>> [...]

>>>>>>>> +static int pattern_trig_start_pattern(struct pattern_trig_data *data,

>>>>>>>> +                                   struct led_classdev *led_cdev)

>>>>>>>> +{

>>>>>>>> +     if (!data->npatterns)

>>>>>>>> +             return 0;

>>>>>>>> +

>>>>>>>> +     if (led_cdev->pattern_set) {

>>>>>>>> +             return led_cdev->pattern_set(led_cdev, data->patterns,

>>>>>>>> +                                          data->npatterns, data->repeat);

>>>>>>>

>>>>>>> I think that it would be more flexible if software pattern fallback

>>>>>>> was applied in case of pattern_set failure. Otherwise, it would

>>>>>>> lead to the situation where LED class devices that support hardware

>>>>>>> blinking couldn't be applied the same set of patterns as LED class

>>>>>>> devices that don't implement pattern_set. The latter will always have to

>>>>>>> resort to using software pattern engine which will accept far greater

>>>>>>> amount of pattern combinations.

>>>>>>

>>>>>> Hmmm, I am not sure this is useful for hardware pattern, since the LED

>>>>>> hardware can be diverse which is hard to be compatible with software

>>>>>> pattern.

>>>>>>

>>>>>> For example, for our SC27XX LED, it only supports 4 hardware patterns

>>>>>> setting (low time, rising time, high time and falling time) to make it

>>>>>> work at breathing mode. If user provides 3 or 5 patterns values, it

>>>>>> will failed to issue pattern_set(). But at this time, we don't want to

>>>>>> use software pattern instead since it will be not the breathing mode

>>>>>> expected by user. I don't know if there are other special LED

>>>>>> hardware.

>>>>>

>>>>> Good point. So this is the issue we should dwell on, since the

>>>>> requested pattern effect should look similar on all devices.

>>>>> Of course in case of software pattern it will be often impossible

>>>>> to achieve the same fluency. Similarly as in case of rendering graphics

>>>>> with and without acceleration.

>>>>>

>>>>> In case of your device, I'd say that we'd need more complex description

>>>>> of breathing mode pattern. More complex than just four intervals.

>>>>> It should reflect all the intervals the hardware engine needs to perform

>>>>> to achieve the breathing effect. Can this information be inferred from

>>>>> the docs?

>>>>

>>>> >From our docs, it only provides registers to set the low time, rising

>>>> time, high time and falling time (value unit is 0.125s and max value

>>>> is 63 = 7.875s) to enable breathing mode. So each interval value can

>>>> be 1 ~ 63. But that is still hard for software pattern to simulate the

>>>> breathing mode performed by hardware pattern.

>>>

>>> Software fallback is not expected to show similar performance to the

>>> hardware engine on the whole span of the supported time range.

>>>

>>> Having min rise time value at 125ms, and given that max_brightness

>>> is 255, then we'd have 255 / 125 = 2.04 of brightness level rise per

>>> 1ms. So, the pattern for rising edge could look like (assuming we

>>> stop at 254):

>>>

>>> 0 1 2 1 4 1 6 1 8 1 10 1 ... 254 1

>>

>> Right, maybe this can work, maybe not. Since we can met different

>> cases when failed to issue pattern_set(). Maybe the LED hardware

>> occurs some errors, in this case we should shutdown the LED, not

>> enable the software pattern instead, which still can not work.

>

> This is arguable. Timer trigger always resorts to using software

> fallback if blink_set() fails.


Timer trigger is simple which only need delay_on and delay_off, that
means software can be easy to show the same performance. But hardware
pattern can be diverse, we need to convert hardware patterns to
software patterns.

>> Maybe

>> driver can set NULL for pattern_set/clear interfaces to disable

>> hardware pattern, then next time user will perform the patterns with

>> software pattern mode.

>

> Resetting any ops after LED class driver is probed should be

> deemed forbidden, since LED core can make some decisions basing on

> whether given ops are initialized or not.


OK.

>

>> For our SC27XX LED, like I said if user provides only 3 pattern values

>> which will failed to issue pattern_set(). But I still do not need use

>> software patterns to show similar performance, instead it will still

>> keep the last hardware pattern performance ( It did not overlap the

>> previous hardware pattern setting). Maybe different drivers have

>> different error handling, that's why I think we can leave driver to

>> choose the proper way to handle.

>

> ABI semantics is generic, i.e. common for all drivers. Driver can

> always log any problems occurred while setting pattern, but it shouldn't

> necessarily need to prevent pattern trigger from using software

> fallback.


Fine.

>> Honestly, can we keep this pattern trigger simple and easy at first?

>> If some drivers want to use software patterns to perform again once

>> their hardware patterns performed failed (IMHO our SC27XX LED do not

>> need), then we can add this feature, at that time we will have users

>> to test and optimize the feature. Maybe I am wrong:)

>

> In other words you want to prevent users from exploiting the flexibility

> of pattern trigger, and limit them to using always only breathing

> scheme for SC27XX LED. What if someone would like to employ pattern

> trigger for encoding morse message to report system errors?


I think we can change to use timer trigger or other triggers.

>

> And secondly, we cannot keep the interface simple at first and then

> change it, because this is against Linux rule, which says that

> interface cannot break existing users.


Make sense.

>

>>> Now, I'm starting to wonder if we shouldn't have specialized trigger

>>> for breathing patterns, that would accept brightness level change per

>>> time period. Pattern trigger needs more flexibility and inferring if the

>>> hardware can handle given series of pattern intervals would entail

>>> unnecessary code complexity.

>>>

>>> Such breathing trigger would require triplets comprised of

>>> start brightness, end brightness and a duration of the brightness

>>> transition.

>>

>> But this is the only place we can set the hardware patterns according

>> to our previous discussion. Thanks.

>

> Thinking more about it, introducing another trigger for something being

> also a pattern is a bad idea.


Yes.

> Perhaps, as an alternative, we could allow for providing mathematical

> formula to define the edge of brightness change.

>

> I wonder what Pavel thinks.


-- 
Baolin Wang
Best Regards
Pavel Machek Aug. 24, 2018, 10:11 a.m. UTC | #6
Hi!

> I think that it would be more flexible if software pattern fallback

> was applied in case of pattern_set failure. Otherwise, it would

> lead to the situation where LED class devices that support hardware

> blinking couldn't be applied the same set of patterns as LED class

> devices that don't implement pattern_set. The latter will always have to

> resort to using software pattern engine which will accept far greater

> amount of pattern combinations.

> 

> In this case we need to discuss on what basis the decision will be

> made on whether hardware or software engine will be used.

> 

> Possible options coming to mind:

> - an interface will be provided to determine max difference between

>   the settings supported by the hardware and the settings requested by

>   the user, that will result in aligning user's setting to the hardware

>   capabilities

> - the above alignment rate will be predefined instead

> - hardware engine will be used only if user requests supported settings

>   on the whole span of the requested pattern

> - in each of the above cases it would be worth to think of the

>   interface to show the scope of the settings supported by hardware


I'd recommend keeping it simple. We use hardware engine if driver
author thinks pattern is "close enough".

If human can not tell the difference, it probably is.

We may want to do something more formal later.
									Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Jacek Anaszewski Aug. 24, 2018, 7:49 p.m. UTC | #7
Hi Pavel,

On 08/24/2018 12:11 PM, Pavel Machek wrote:
> Hi!

> 

>> I think that it would be more flexible if software pattern fallback

>> was applied in case of pattern_set failure. Otherwise, it would

>> lead to the situation where LED class devices that support hardware

>> blinking couldn't be applied the same set of patterns as LED class

>> devices that don't implement pattern_set. The latter will always have to

>> resort to using software pattern engine which will accept far greater

>> amount of pattern combinations.

>>

>> In this case we need to discuss on what basis the decision will be

>> made on whether hardware or software engine will be used.

>>

>> Possible options coming to mind:

>> - an interface will be provided to determine max difference between

>>   the settings supported by the hardware and the settings requested by

>>   the user, that will result in aligning user's setting to the hardware

>>   capabilities

>> - the above alignment rate will be predefined instead

>> - hardware engine will be used only if user requests supported settings

>>   on the whole span of the requested pattern

>> - in each of the above cases it would be worth to think of the

>>   interface to show the scope of the settings supported by hardware

> 

> I'd recommend keeping it simple. We use hardware engine if driver

> author thinks pattern is "close enough".


The thing is that in the ledtrig-pattern v5 implementation there
is no option of using software fallback if pattern_set op
is initialized:

+	if (led_cdev->pattern_set) {
+		return led_cdev->pattern_set(led_cdev, data->patterns,
+					     data->npatterns, data->repeat);
+	}

> If human can not tell the difference, it probably is.

> 

> We may want to do something more formal later.


-- 
Best regards,
Jacek Anaszewski
Pavel Machek Aug. 24, 2018, 8:12 p.m. UTC | #8
On Fri 2018-08-24 21:49:50, Jacek Anaszewski wrote:
> Hi Pavel,

> 

> On 08/24/2018 12:11 PM, Pavel Machek wrote:

> > Hi!

> > 

> >> I think that it would be more flexible if software pattern fallback

> >> was applied in case of pattern_set failure. Otherwise, it would

> >> lead to the situation where LED class devices that support hardware

> >> blinking couldn't be applied the same set of patterns as LED class

> >> devices that don't implement pattern_set. The latter will always have to

> >> resort to using software pattern engine which will accept far greater

> >> amount of pattern combinations.

> >>

> >> In this case we need to discuss on what basis the decision will be

> >> made on whether hardware or software engine will be used.

> >>

> >> Possible options coming to mind:

> >> - an interface will be provided to determine max difference between

> >>   the settings supported by the hardware and the settings requested by

> >>   the user, that will result in aligning user's setting to the hardware

> >>   capabilities

> >> - the above alignment rate will be predefined instead

> >> - hardware engine will be used only if user requests supported settings

> >>   on the whole span of the requested pattern

> >> - in each of the above cases it would be worth to think of the

> >>   interface to show the scope of the settings supported by hardware

> > 

> > I'd recommend keeping it simple. We use hardware engine if driver

> > author thinks pattern is "close enough".

> 

> The thing is that in the ledtrig-pattern v5 implementation there

> is no option of using software fallback if pattern_set op

> is initialized:

> 

> +	if (led_cdev->pattern_set) {

> +		return led_cdev->pattern_set(led_cdev, data->patterns,

> +					     data->npatterns, data->repeat);

> +	}


Yeah, that sounds wrong. (Sorry I did not pay enough attention).

It pattern_set() returns special error code, it should just continue
and use software pattern fallback.
									Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Jacek Anaszewski Aug. 24, 2018, 8:44 p.m. UTC | #9
On 08/24/2018 10:12 PM, Pavel Machek wrote:
> On Fri 2018-08-24 21:49:50, Jacek Anaszewski wrote:

>> Hi Pavel,

>>

>> On 08/24/2018 12:11 PM, Pavel Machek wrote:

>>> Hi!

>>>

>>>> I think that it would be more flexible if software pattern fallback

>>>> was applied in case of pattern_set failure. Otherwise, it would

>>>> lead to the situation where LED class devices that support hardware

>>>> blinking couldn't be applied the same set of patterns as LED class

>>>> devices that don't implement pattern_set. The latter will always have to

>>>> resort to using software pattern engine which will accept far greater

>>>> amount of pattern combinations.

>>>>

>>>> In this case we need to discuss on what basis the decision will be

>>>> made on whether hardware or software engine will be used.

>>>>

>>>> Possible options coming to mind:

>>>> - an interface will be provided to determine max difference between

>>>>   the settings supported by the hardware and the settings requested by

>>>>   the user, that will result in aligning user's setting to the hardware

>>>>   capabilities

>>>> - the above alignment rate will be predefined instead

>>>> - hardware engine will be used only if user requests supported settings

>>>>   on the whole span of the requested pattern

>>>> - in each of the above cases it would be worth to think of the

>>>>   interface to show the scope of the settings supported by hardware

>>>

>>> I'd recommend keeping it simple. We use hardware engine if driver

>>> author thinks pattern is "close enough".

>>

>> The thing is that in the ledtrig-pattern v5 implementation there

>> is no option of using software fallback if pattern_set op

>> is initialized:

>>

>> +	if (led_cdev->pattern_set) {

>> +		return led_cdev->pattern_set(led_cdev, data->patterns,

>> +					     data->npatterns, data->repeat);

>> +	}

> 

> Yeah, that sounds wrong. (Sorry I did not pay enough attention).

> 

> It pattern_set() returns special error code, it should just continue

> and use software pattern fallback.


And now we can get back to the issue I was concerned about in the
email you replied to, i.e. what series of [brightness delta_t] tuples
should be written to the pattern file to enable hardware breathing
engine.

-- 
Best regards,
Jacek Anaszewski
(Exiting) Baolin Wang Aug. 25, 2018, 7:51 a.m. UTC | #10
On 25 August 2018 at 04:44, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:
> On 08/24/2018 10:12 PM, Pavel Machek wrote:

>> On Fri 2018-08-24 21:49:50, Jacek Anaszewski wrote:

>>> Hi Pavel,

>>>

>>> On 08/24/2018 12:11 PM, Pavel Machek wrote:

>>>> Hi!

>>>>

>>>>> I think that it would be more flexible if software pattern fallback

>>>>> was applied in case of pattern_set failure. Otherwise, it would

>>>>> lead to the situation where LED class devices that support hardware

>>>>> blinking couldn't be applied the same set of patterns as LED class

>>>>> devices that don't implement pattern_set. The latter will always have to

>>>>> resort to using software pattern engine which will accept far greater

>>>>> amount of pattern combinations.

>>>>>

>>>>> In this case we need to discuss on what basis the decision will be

>>>>> made on whether hardware or software engine will be used.

>>>>>

>>>>> Possible options coming to mind:

>>>>> - an interface will be provided to determine max difference between

>>>>>   the settings supported by the hardware and the settings requested by

>>>>>   the user, that will result in aligning user's setting to the hardware

>>>>>   capabilities

>>>>> - the above alignment rate will be predefined instead

>>>>> - hardware engine will be used only if user requests supported settings

>>>>>   on the whole span of the requested pattern

>>>>> - in each of the above cases it would be worth to think of the

>>>>>   interface to show the scope of the settings supported by hardware

>>>>

>>>> I'd recommend keeping it simple. We use hardware engine if driver

>>>> author thinks pattern is "close enough".

>>>

>>> The thing is that in the ledtrig-pattern v5 implementation there

>>> is no option of using software fallback if pattern_set op

>>> is initialized:

>>>

>>> +    if (led_cdev->pattern_set) {

>>> +            return led_cdev->pattern_set(led_cdev, data->patterns,

>>> +                                         data->npatterns, data->repeat);

>>> +    }

>>

>> Yeah, that sounds wrong. (Sorry I did not pay enough attention).

>>

>> It pattern_set() returns special error code, it should just continue

>> and use software pattern fallback.

>

> And now we can get back to the issue I was concerned about in the

> email you replied to, i.e. what series of [brightness delta_t] tuples

> should be written to the pattern file to enable hardware breathing

> engine.


OK. So now we've made a consensus to use the software pattern fallback
if failed to set hardware pattern. How about introduce one interface
to convert hardware patterns to software patterns if necessary?

diff --git a/drivers/leds/trigger/ledtrig-pattern.c
b/drivers/leds/trigger/ledtrig-pattern.c
index 63b94a2..d46a641 100644
--- a/drivers/leds/trigger/ledtrig-pattern.c
+++ b/drivers/leds/trigger/ledtrig-pattern.c
@@ -66,8 +66,15 @@ static int pattern_trig_start_pattern(struct
pattern_trig_data *data,
                return 0;

        if (led_cdev->pattern_set) {
-               return led_cdev->pattern_set(led_cdev, data->patterns,
-                                            data->npatterns, data->repeat);
+               ret = led_cdev->pattern_set(led_cdev, data->patterns,
+                                           data->npatterns, data->repeat);
+               if (!ret)
+                       return 0;
+
+               dev_warn(led_cdev->dev, "Failed to set hardware pattern\n");
+
+               if (led_cdev->pattern_convert)
+                       led_cdev->pattern_convert(led_cdev,
data->patterns, &data->npatterns);
        }

        data->curr = data->patterns;
@@ -106,8 +113,7 @@ static ssize_t repeat_store(struct device *dev,
struct device_attribute *attr,
        if (err)
                return err;

-       if (!led_cdev->pattern_set)
-               del_timer_sync(&data->timer);
+       del_timer_sync(&data->timer);

        mutex_lock(&data->lock);

@@ -161,8 +167,7 @@ static ssize_t pattern_store(struct device *dev,
struct device_attribute *attr,
        struct pattern_trig_data *data = led_cdev->trigger_data;
        int ccount, cr, offset = 0, err = 0;

-       if (!led_cdev->pattern_set)
-               del_timer_sync(&data->timer);
+       del_timer_sync(&data->timer);

        mutex_lock(&data->lock);

@@ -232,9 +237,8 @@ static void pattern_trig_deactivate(struct
led_classdev *led_cdev)

        if (led_cdev->pattern_clear)
                led_cdev->pattern_clear(led_cdev);
-       else
-               del_timer_sync(&data->timer);

+       del_timer_sync(&data->timer);
        led_set_brightness(led_cdev, LED_OFF);
        kfree(data);
        led_cdev->activated = false;
diff --git a/include/linux/leds.h b/include/linux/leds.h
index 74fc2c6..04f3eaf 100644
--- a/include/linux/leds.h
+++ b/include/linux/leds.h
@@ -93,6 +93,8 @@ struct led_classdev {
                           struct led_pattern *pattern, int len,
                           unsigned int repeat);
        int (*pattern_clear)(struct led_classdev *led_cdev);
+       int (*pattern_convert)(struct led_classdev *led_cdev,
+                              struct led_pattern *pattern, int *len);

        struct device           *dev;
        const struct attribute_group    **groups;
-- 
Baolin Wang
Best Regards
Jacek Anaszewski Aug. 28, 2018, 8:25 p.m. UTC | #11
On 08/25/2018 09:51 AM, Baolin Wang wrote:
> On 25 August 2018 at 04:44, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:

>> On 08/24/2018 10:12 PM, Pavel Machek wrote:

>>> On Fri 2018-08-24 21:49:50, Jacek Anaszewski wrote:

>>>> Hi Pavel,

>>>>

>>>> On 08/24/2018 12:11 PM, Pavel Machek wrote:

>>>>> Hi!

>>>>>

>>>>>> I think that it would be more flexible if software pattern fallback

>>>>>> was applied in case of pattern_set failure. Otherwise, it would

>>>>>> lead to the situation where LED class devices that support hardware

>>>>>> blinking couldn't be applied the same set of patterns as LED class

>>>>>> devices that don't implement pattern_set. The latter will always have to

>>>>>> resort to using software pattern engine which will accept far greater

>>>>>> amount of pattern combinations.

>>>>>>

>>>>>> In this case we need to discuss on what basis the decision will be

>>>>>> made on whether hardware or software engine will be used.

>>>>>>

>>>>>> Possible options coming to mind:

>>>>>> - an interface will be provided to determine max difference between

>>>>>>   the settings supported by the hardware and the settings requested by

>>>>>>   the user, that will result in aligning user's setting to the hardware

>>>>>>   capabilities

>>>>>> - the above alignment rate will be predefined instead

>>>>>> - hardware engine will be used only if user requests supported settings

>>>>>>   on the whole span of the requested pattern

>>>>>> - in each of the above cases it would be worth to think of the

>>>>>>   interface to show the scope of the settings supported by hardware

>>>>>

>>>>> I'd recommend keeping it simple. We use hardware engine if driver

>>>>> author thinks pattern is "close enough".

>>>>

>>>> The thing is that in the ledtrig-pattern v5 implementation there

>>>> is no option of using software fallback if pattern_set op

>>>> is initialized:

>>>>

>>>> +    if (led_cdev->pattern_set) {

>>>> +            return led_cdev->pattern_set(led_cdev, data->patterns,

>>>> +                                         data->npatterns, data->repeat);

>>>> +    }

>>>

>>> Yeah, that sounds wrong. (Sorry I did not pay enough attention).

>>>

>>> It pattern_set() returns special error code, it should just continue

>>> and use software pattern fallback.

>>

>> And now we can get back to the issue I was concerned about in the

>> email you replied to, i.e. what series of [brightness delta_t] tuples

>> should be written to the pattern file to enable hardware breathing

>> engine.

> 

> OK. So now we've made a consensus to use the software pattern fallback

> if failed to set hardware pattern. How about introduce one interface

> to convert hardware patterns to software patterns if necessary?

> 

> diff --git a/drivers/leds/trigger/ledtrig-pattern.c

> b/drivers/leds/trigger/ledtrig-pattern.c

> index 63b94a2..d46a641 100644

> --- a/drivers/leds/trigger/ledtrig-pattern.c

> +++ b/drivers/leds/trigger/ledtrig-pattern.c

> @@ -66,8 +66,15 @@ static int pattern_trig_start_pattern(struct

> pattern_trig_data *data,

>                 return 0;

> 

>         if (led_cdev->pattern_set) {

> -               return led_cdev->pattern_set(led_cdev, data->patterns,

> -                                            data->npatterns, data->repeat);

> +               ret = led_cdev->pattern_set(led_cdev, data->patterns,

> +                                           data->npatterns, data->repeat);

> +               if (!ret)

> +                       return 0;

> +

> +               dev_warn(led_cdev->dev, "Failed to set hardware pattern\n");

> +

> +               if (led_cdev->pattern_convert)

> +                       led_cdev->pattern_convert(led_cdev,


I can't see how it could help to assess if hw pattern
engine can launch given pattern, and with what accuracy.

Instead, I propose to add a means for defining whether the pattern
to be set is intended for hardware pattern engine or for software
fallback. It could be either separate sysfs file e.g. hw_pattern,
or a modifier to the pattern written to the pattern file, e,g,

echo "hw 100 2 200 3 100 2" > pattern

hw format expected by given driver would have to be described
in the per-driver ABI documentation. All patterns without
"hw" prefix would enable software pattern engine.

> data->patterns, &data->npatterns);

>         }

> 

>         data->curr = data->patterns;

> @@ -106,8 +113,7 @@ static ssize_t repeat_store(struct device *dev,

> struct device_attribute *attr,

>         if (err)

>                 return err;

> 

> -       if (!led_cdev->pattern_set)

> -               del_timer_sync(&data->timer);

> +       del_timer_sync(&data->timer);

> 

>         mutex_lock(&data->lock);

> 

> @@ -161,8 +167,7 @@ static ssize_t pattern_store(struct device *dev,

> struct device_attribute *attr,

>         struct pattern_trig_data *data = led_cdev->trigger_data;

>         int ccount, cr, offset = 0, err = 0;

> 

> -       if (!led_cdev->pattern_set)

> -               del_timer_sync(&data->timer);

> +       del_timer_sync(&data->timer);

> 

>         mutex_lock(&data->lock);

> 

> @@ -232,9 +237,8 @@ static void pattern_trig_deactivate(struct

> led_classdev *led_cdev)

> 

>         if (led_cdev->pattern_clear)

>                 led_cdev->pattern_clear(led_cdev);

> -       else

> -               del_timer_sync(&data->timer);

> 

> +       del_timer_sync(&data->timer);

>         led_set_brightness(led_cdev, LED_OFF);

>         kfree(data);

>         led_cdev->activated = false;

> diff --git a/include/linux/leds.h b/include/linux/leds.h

> index 74fc2c6..04f3eaf 100644

> --- a/include/linux/leds.h

> +++ b/include/linux/leds.h

> @@ -93,6 +93,8 @@ struct led_classdev {

>                            struct led_pattern *pattern, int len,

>                            unsigned int repeat);

>         int (*pattern_clear)(struct led_classdev *led_cdev);

> +       int (*pattern_convert)(struct led_classdev *led_cdev,

> +                              struct led_pattern *pattern, int *len);

> 

>         struct device           *dev;

>         const struct attribute_group    **groups;

> 


-- 
Best regards,
Jacek Anaszewski
Bjorn Andersson Aug. 28, 2018, 8:47 p.m. UTC | #12
On Sat 25 Aug 00:51 PDT 2018, Baolin Wang wrote:

> On 25 August 2018 at 04:44, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:

> > On 08/24/2018 10:12 PM, Pavel Machek wrote:

> >> On Fri 2018-08-24 21:49:50, Jacek Anaszewski wrote:

> >>> Hi Pavel,

> >>>

> >>> On 08/24/2018 12:11 PM, Pavel Machek wrote:

[..]
> >>> +    if (led_cdev->pattern_set) {

> >>> +            return led_cdev->pattern_set(led_cdev, data->patterns,

> >>> +                                         data->npatterns, data->repeat);

> >>> +    }

> >>

> >> Yeah, that sounds wrong. (Sorry I did not pay enough attention).

> >>

> >> It pattern_set() returns special error code, it should just continue

> >> and use software pattern fallback.

> >

> > And now we can get back to the issue I was concerned about in the

> > email you replied to, i.e. what series of [brightness delta_t] tuples

> > should be written to the pattern file to enable hardware breathing

> > engine.

> 

> OK. So now we've made a consensus to use the software pattern fallback

> if failed to set hardware pattern. How about introduce one interface

> to convert hardware patterns to software patterns if necessary?

> 


I do not agree that a silent fallback mechanism is the way to go here.
There is a enormous difference in power consumption between letting a
dedicated piece of hardware drive a pulse on a LED or using a general
purpose ARM core running at hundreds of MHz.


Note that the most common use case for the driver I'm trying to upstream
is to pulse the LED when you have a notification pending on your Android
device, in which case you want all the CPUs are completely powered down.

Regards,
Bjorn
Bjorn Andersson Aug. 28, 2018, 9:13 p.m. UTC | #13
On Tue 28 Aug 13:25 PDT 2018, Jacek Anaszewski wrote:

> On 08/25/2018 09:51 AM, Baolin Wang wrote:

> > On 25 August 2018 at 04:44, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:

> >> On 08/24/2018 10:12 PM, Pavel Machek wrote:

> >>> On Fri 2018-08-24 21:49:50, Jacek Anaszewski wrote:

> >>>> Hi Pavel,

> >>>>

> >>>> On 08/24/2018 12:11 PM, Pavel Machek wrote:

> >>>>> Hi!

> >>>>>

> >>>>>> I think that it would be more flexible if software pattern fallback

> >>>>>> was applied in case of pattern_set failure. Otherwise, it would

> >>>>>> lead to the situation where LED class devices that support hardware

> >>>>>> blinking couldn't be applied the same set of patterns as LED class

> >>>>>> devices that don't implement pattern_set. The latter will always have to

> >>>>>> resort to using software pattern engine which will accept far greater

> >>>>>> amount of pattern combinations.

> >>>>>>

> >>>>>> In this case we need to discuss on what basis the decision will be

> >>>>>> made on whether hardware or software engine will be used.

> >>>>>>

> >>>>>> Possible options coming to mind:

> >>>>>> - an interface will be provided to determine max difference between

> >>>>>>   the settings supported by the hardware and the settings requested by

> >>>>>>   the user, that will result in aligning user's setting to the hardware

> >>>>>>   capabilities

> >>>>>> - the above alignment rate will be predefined instead

> >>>>>> - hardware engine will be used only if user requests supported settings

> >>>>>>   on the whole span of the requested pattern

> >>>>>> - in each of the above cases it would be worth to think of the

> >>>>>>   interface to show the scope of the settings supported by hardware

> >>>>>

> >>>>> I'd recommend keeping it simple. We use hardware engine if driver

> >>>>> author thinks pattern is "close enough".

> >>>>

> >>>> The thing is that in the ledtrig-pattern v5 implementation there

> >>>> is no option of using software fallback if pattern_set op

> >>>> is initialized:

> >>>>

> >>>> +    if (led_cdev->pattern_set) {

> >>>> +            return led_cdev->pattern_set(led_cdev, data->patterns,

> >>>> +                                         data->npatterns, data->repeat);

> >>>> +    }

> >>>

> >>> Yeah, that sounds wrong. (Sorry I did not pay enough attention).

> >>>

> >>> It pattern_set() returns special error code, it should just continue

> >>> and use software pattern fallback.

> >>

> >> And now we can get back to the issue I was concerned about in the

> >> email you replied to, i.e. what series of [brightness delta_t] tuples

> >> should be written to the pattern file to enable hardware breathing

> >> engine.

> > 

> > OK. So now we've made a consensus to use the software pattern fallback

> > if failed to set hardware pattern. How about introduce one interface

> > to convert hardware patterns to software patterns if necessary?

> > 

> > diff --git a/drivers/leds/trigger/ledtrig-pattern.c

> > b/drivers/leds/trigger/ledtrig-pattern.c

> > index 63b94a2..d46a641 100644

> > --- a/drivers/leds/trigger/ledtrig-pattern.c

> > +++ b/drivers/leds/trigger/ledtrig-pattern.c

> > @@ -66,8 +66,15 @@ static int pattern_trig_start_pattern(struct

> > pattern_trig_data *data,

> >                 return 0;

> > 

> >         if (led_cdev->pattern_set) {

> > -               return led_cdev->pattern_set(led_cdev, data->patterns,

> > -                                            data->npatterns, data->repeat);

> > +               ret = led_cdev->pattern_set(led_cdev, data->patterns,

> > +                                           data->npatterns, data->repeat);

> > +               if (!ret)

> > +                       return 0;

> > +

> > +               dev_warn(led_cdev->dev, "Failed to set hardware pattern\n");

> > +

> > +               if (led_cdev->pattern_convert)

> > +                       led_cdev->pattern_convert(led_cdev,

> 

> I can't see how it could help to assess if hw pattern

> engine can launch given pattern, and with what accuracy.

> 

> Instead, I propose to add a means for defining whether the pattern

> to be set is intended for hardware pattern engine or for software

> fallback. It could be either separate sysfs file e.g. hw_pattern,

> or a modifier to the pattern written to the pattern file, e,g,

> 

> echo "hw 100 2 200 3 100 2" > pattern

> 

> hw format expected by given driver would have to be described

> in the per-driver ABI documentation. All patterns without

> "hw" prefix would enable software pattern engine.

> 


We started this discussion with the suggestion that rather than
introducing a new Qualcomm specific sysfs interface for controlling the
pattern engine we should have a common one, but if I understand your
suggestion we should not have a common interface, just a common sysfs
file name?

Regards,
Bjorn
(Exiting) Baolin Wang Aug. 29, 2018, 9:48 a.m. UTC | #14
Hi Jacek,

On 29 August 2018 at 04:25, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:
> On 08/25/2018 09:51 AM, Baolin Wang wrote:

>> On 25 August 2018 at 04:44, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:

>>> On 08/24/2018 10:12 PM, Pavel Machek wrote:

>>>> On Fri 2018-08-24 21:49:50, Jacek Anaszewski wrote:

>>>>> Hi Pavel,

>>>>>

>>>>> On 08/24/2018 12:11 PM, Pavel Machek wrote:

>>>>>> Hi!

>>>>>>

>>>>>>> I think that it would be more flexible if software pattern fallback

>>>>>>> was applied in case of pattern_set failure. Otherwise, it would

>>>>>>> lead to the situation where LED class devices that support hardware

>>>>>>> blinking couldn't be applied the same set of patterns as LED class

>>>>>>> devices that don't implement pattern_set. The latter will always have to

>>>>>>> resort to using software pattern engine which will accept far greater

>>>>>>> amount of pattern combinations.

>>>>>>>

>>>>>>> In this case we need to discuss on what basis the decision will be

>>>>>>> made on whether hardware or software engine will be used.

>>>>>>>

>>>>>>> Possible options coming to mind:

>>>>>>> - an interface will be provided to determine max difference between

>>>>>>>   the settings supported by the hardware and the settings requested by

>>>>>>>   the user, that will result in aligning user's setting to the hardware

>>>>>>>   capabilities

>>>>>>> - the above alignment rate will be predefined instead

>>>>>>> - hardware engine will be used only if user requests supported settings

>>>>>>>   on the whole span of the requested pattern

>>>>>>> - in each of the above cases it would be worth to think of the

>>>>>>>   interface to show the scope of the settings supported by hardware

>>>>>>

>>>>>> I'd recommend keeping it simple. We use hardware engine if driver

>>>>>> author thinks pattern is "close enough".

>>>>>

>>>>> The thing is that in the ledtrig-pattern v5 implementation there

>>>>> is no option of using software fallback if pattern_set op

>>>>> is initialized:

>>>>>

>>>>> +    if (led_cdev->pattern_set) {

>>>>> +            return led_cdev->pattern_set(led_cdev, data->patterns,

>>>>> +                                         data->npatterns, data->repeat);

>>>>> +    }

>>>>

>>>> Yeah, that sounds wrong. (Sorry I did not pay enough attention).

>>>>

>>>> It pattern_set() returns special error code, it should just continue

>>>> and use software pattern fallback.

>>>

>>> And now we can get back to the issue I was concerned about in the

>>> email you replied to, i.e. what series of [brightness delta_t] tuples

>>> should be written to the pattern file to enable hardware breathing

>>> engine.

>>

>> OK. So now we've made a consensus to use the software pattern fallback

>> if failed to set hardware pattern. How about introduce one interface

>> to convert hardware patterns to software patterns if necessary?

>>

>> diff --git a/drivers/leds/trigger/ledtrig-pattern.c

>> b/drivers/leds/trigger/ledtrig-pattern.c

>> index 63b94a2..d46a641 100644

>> --- a/drivers/leds/trigger/ledtrig-pattern.c

>> +++ b/drivers/leds/trigger/ledtrig-pattern.c

>> @@ -66,8 +66,15 @@ static int pattern_trig_start_pattern(struct

>> pattern_trig_data *data,

>>                 return 0;

>>

>>         if (led_cdev->pattern_set) {

>> -               return led_cdev->pattern_set(led_cdev, data->patterns,

>> -                                            data->npatterns, data->repeat);

>> +               ret = led_cdev->pattern_set(led_cdev, data->patterns,

>> +                                           data->npatterns, data->repeat);

>> +               if (!ret)

>> +                       return 0;

>> +

>> +               dev_warn(led_cdev->dev, "Failed to set hardware pattern\n");

>> +

>> +               if (led_cdev->pattern_convert)

>> +                       led_cdev->pattern_convert(led_cdev,

>

> I can't see how it could help to assess if hw pattern

> engine can launch given pattern, and with what accuracy.

>

> Instead, I propose to add a means for defining whether the pattern

> to be set is intended for hardware pattern engine or for software

> fallback. It could be either separate sysfs file e.g. hw_pattern,

> or a modifier to the pattern written to the pattern file, e,g,

>

> echo "hw 100 2 200 3 100 2" > pattern

>

> hw format expected by given driver would have to be described

> in the per-driver ABI documentation. All patterns without

> "hw" prefix would enable software pattern engine.


OK. So I did some optimization based on v5 according to suggestion, if
you think this is okay for you, then I will send out the formal v6
patch set.

1. echo "hw 100 2 200 3 100 2" > pattern
Only for hardware pattern, if failed,  we will return error number.

2. echo "100 2 200 3 100 2" > pattern
Will try to set hardware pattern firstly, if failed, will use software
pattern instead.

diff --git a/drivers/leds/trigger/ledtrig-pattern.c
b/drivers/leds/trigger/ledtrig-pattern.c
index 63b94a2..f08e797 100644
--- a/drivers/leds/trigger/ledtrig-pattern.c
+++ b/drivers/leds/trigger/ledtrig-pattern.c
@@ -26,6 +26,7 @@ struct pattern_trig_data {
 	u32 repeat;
 	u32 last_repeat;
 	bool is_indefinite;
+	bool is_hw_pattern;
 	struct timer_list timer;
 };

@@ -62,12 +63,21 @@ static void pattern_trig_timer_function(struct
timer_list *t)
 static int pattern_trig_start_pattern(struct pattern_trig_data *data,
 				      struct led_classdev *led_cdev)
 {
+	int ret;
+
 	if (!data->npatterns)
 		return 0;

 	if (led_cdev->pattern_set) {
-		return led_cdev->pattern_set(led_cdev, data->patterns,
+		ret = led_cdev->pattern_set(led_cdev, data->patterns,
 					     data->npatterns, data->repeat);
+		if (!ret)
+			return 0;
+
+		dev_warn(led_cdev->dev, "Failed to set hardware patterns\n");
+
+		if (data->is_hw_pattern)
+			return ret;
 	}

 	data->curr = data->patterns;
@@ -106,7 +116,7 @@ static ssize_t repeat_store(struct device *dev,
struct device_attribute *attr,
 	if (err)
 		return err;

-	if (!led_cdev->pattern_set)
+	if (!data->is_hw_pattern)
 		del_timer_sync(&data->timer);

 	mutex_lock(&data->lock);
@@ -159,19 +169,32 @@ static ssize_t pattern_store(struct device *dev,
struct device_attribute *attr,
 {
 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
 	struct pattern_trig_data *data = led_cdev->trigger_data;
-	int ccount, cr, offset = 0, err = 0;
+	int ccount, cr, len, offset = 0, err = 0;
+	const char *s;

-	if (!led_cdev->pattern_set)
+	if (!data->is_hw_pattern)
 		del_timer_sync(&data->timer);

 	mutex_lock(&data->lock);

+	s = strstr(buf, "hw");
+	if (s && led_cdev->pattern_set) {
+		data->is_hw_pattern = true;
+		s += strlen("hw");
+		len = strlen(s);
+	} else {
+		data->is_hw_pattern = false;
+		s = buf;
+		len = count;
+	}
+
 	data->npatterns = 0;
-	while (offset < count - 1 && data->npatterns < MAX_PATTERNS) {
+	while (offset < len - 1 && data->npatterns < MAX_PATTERNS) {
 		cr = 0;
-		ccount = sscanf(buf + offset, "%d %d %n",
+		ccount = sscanf(s + offset, "%d %d %n",
 				&data->patterns[data->npatterns].brightness,
 				&data->patterns[data->npatterns].delta_t, &cr);
 		if (ccount != 2) {
 			data->npatterns = 0;
 			err = -EINVAL;
@@ -232,7 +255,8 @@ static void pattern_trig_deactivate(struct
led_classdev *led_cdev)

 	if (led_cdev->pattern_clear)
 		led_cdev->pattern_clear(led_cdev);
-	else
+
+	if (!data->is_hw_pattern)
 		del_timer_sync(&data->timer);

 	led_set_brightness(led_cdev, LED_OFF);

>

>> data->patterns, &data->npatterns);

>>         }

>>

>>         data->curr = data->patterns;

>> @@ -106,8 +113,7 @@ static ssize_t repeat_store(struct device *dev,

>> struct device_attribute *attr,

>>         if (err)

>>                 return err;

>>

>> -       if (!led_cdev->pattern_set)

>> -               del_timer_sync(&data->timer);

>> +       del_timer_sync(&data->timer);

>>

>>         mutex_lock(&data->lock);

>>

>> @@ -161,8 +167,7 @@ static ssize_t pattern_store(struct device *dev,

>> struct device_attribute *attr,

>>         struct pattern_trig_data *data = led_cdev->trigger_data;

>>         int ccount, cr, offset = 0, err = 0;

>>

>> -       if (!led_cdev->pattern_set)

>> -               del_timer_sync(&data->timer);

>> +       del_timer_sync(&data->timer);

>>

>>         mutex_lock(&data->lock);

>>

>> @@ -232,9 +237,8 @@ static void pattern_trig_deactivate(struct

>> led_classdev *led_cdev)

>>

>>         if (led_cdev->pattern_clear)

>>                 led_cdev->pattern_clear(led_cdev);

>> -       else

>> -               del_timer_sync(&data->timer);

>>

>> +       del_timer_sync(&data->timer);

>>         led_set_brightness(led_cdev, LED_OFF);

>>         kfree(data);

>>         led_cdev->activated = false;

>> diff --git a/include/linux/leds.h b/include/linux/leds.h

>> index 74fc2c6..04f3eaf 100644

>> --- a/include/linux/leds.h

>> +++ b/include/linux/leds.h

>> @@ -93,6 +93,8 @@ struct led_classdev {

>>                            struct led_pattern *pattern, int len,

>>                            unsigned int repeat);

>>         int (*pattern_clear)(struct led_classdev *led_cdev);

>> +       int (*pattern_convert)(struct led_classdev *led_cdev,

>> +                              struct led_pattern *pattern, int *len);

>>

>>         struct device           *dev;

>>         const struct attribute_group    **groups;

>>

>

> --

> Best regards,

> Jacek Anaszewski




-- 
Baolin Wang
Best Regards
Jacek Anaszewski Aug. 29, 2018, 6:55 p.m. UTC | #15
On 08/28/2018 11:13 PM, Bjorn Andersson wrote:
> On Tue 28 Aug 13:25 PDT 2018, Jacek Anaszewski wrote:

> 

>> On 08/25/2018 09:51 AM, Baolin Wang wrote:

>>> On 25 August 2018 at 04:44, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:

>>>> On 08/24/2018 10:12 PM, Pavel Machek wrote:

>>>>> On Fri 2018-08-24 21:49:50, Jacek Anaszewski wrote:

>>>>>> Hi Pavel,

>>>>>>

>>>>>> On 08/24/2018 12:11 PM, Pavel Machek wrote:

>>>>>>> Hi!

>>>>>>>

>>>>>>>> I think that it would be more flexible if software pattern fallback

>>>>>>>> was applied in case of pattern_set failure. Otherwise, it would

>>>>>>>> lead to the situation where LED class devices that support hardware

>>>>>>>> blinking couldn't be applied the same set of patterns as LED class

>>>>>>>> devices that don't implement pattern_set. The latter will always have to

>>>>>>>> resort to using software pattern engine which will accept far greater

>>>>>>>> amount of pattern combinations.

>>>>>>>>

>>>>>>>> In this case we need to discuss on what basis the decision will be

>>>>>>>> made on whether hardware or software engine will be used.

>>>>>>>>

>>>>>>>> Possible options coming to mind:

>>>>>>>> - an interface will be provided to determine max difference between

>>>>>>>>   the settings supported by the hardware and the settings requested by

>>>>>>>>   the user, that will result in aligning user's setting to the hardware

>>>>>>>>   capabilities

>>>>>>>> - the above alignment rate will be predefined instead

>>>>>>>> - hardware engine will be used only if user requests supported settings

>>>>>>>>   on the whole span of the requested pattern

>>>>>>>> - in each of the above cases it would be worth to think of the

>>>>>>>>   interface to show the scope of the settings supported by hardware

>>>>>>>

>>>>>>> I'd recommend keeping it simple. We use hardware engine if driver

>>>>>>> author thinks pattern is "close enough".

>>>>>>

>>>>>> The thing is that in the ledtrig-pattern v5 implementation there

>>>>>> is no option of using software fallback if pattern_set op

>>>>>> is initialized:

>>>>>>

>>>>>> +    if (led_cdev->pattern_set) {

>>>>>> +            return led_cdev->pattern_set(led_cdev, data->patterns,

>>>>>> +                                         data->npatterns, data->repeat);

>>>>>> +    }

>>>>>

>>>>> Yeah, that sounds wrong. (Sorry I did not pay enough attention).

>>>>>

>>>>> It pattern_set() returns special error code, it should just continue

>>>>> and use software pattern fallback.

>>>>

>>>> And now we can get back to the issue I was concerned about in the

>>>> email you replied to, i.e. what series of [brightness delta_t] tuples

>>>> should be written to the pattern file to enable hardware breathing

>>>> engine.

>>>

>>> OK. So now we've made a consensus to use the software pattern fallback

>>> if failed to set hardware pattern. How about introduce one interface

>>> to convert hardware patterns to software patterns if necessary?

>>>

>>> diff --git a/drivers/leds/trigger/ledtrig-pattern.c

>>> b/drivers/leds/trigger/ledtrig-pattern.c

>>> index 63b94a2..d46a641 100644

>>> --- a/drivers/leds/trigger/ledtrig-pattern.c

>>> +++ b/drivers/leds/trigger/ledtrig-pattern.c

>>> @@ -66,8 +66,15 @@ static int pattern_trig_start_pattern(struct

>>> pattern_trig_data *data,

>>>                 return 0;

>>>

>>>         if (led_cdev->pattern_set) {

>>> -               return led_cdev->pattern_set(led_cdev, data->patterns,

>>> -                                            data->npatterns, data->repeat);

>>> +               ret = led_cdev->pattern_set(led_cdev, data->patterns,

>>> +                                           data->npatterns, data->repeat);

>>> +               if (!ret)

>>> +                       return 0;

>>> +

>>> +               dev_warn(led_cdev->dev, "Failed to set hardware pattern\n");

>>> +

>>> +               if (led_cdev->pattern_convert)

>>> +                       led_cdev->pattern_convert(led_cdev,

>>

>> I can't see how it could help to assess if hw pattern

>> engine can launch given pattern, and with what accuracy.

>>

>> Instead, I propose to add a means for defining whether the pattern

>> to be set is intended for hardware pattern engine or for software

>> fallback. It could be either separate sysfs file e.g. hw_pattern,

>> or a modifier to the pattern written to the pattern file, e,g,

>>

>> echo "hw 100 2 200 3 100 2" > pattern

>>

>> hw format expected by given driver would have to be described

>> in the per-driver ABI documentation. All patterns without

>> "hw" prefix would enable software pattern engine.

>>

> 

> We started this discussion with the suggestion that rather than

> introducing a new Qualcomm specific sysfs interface for controlling the

> pattern engine we should have a common one, but if I understand your

> suggestion we should not have a common interface, just a common sysfs

> file name?


True, when it is related to hardware pattern. Software pattern would
have common both file and interface. Alternatively, we could have two
separate files for setting software and hardware pattern.

-- 
Best regards,
Jacek Anaszewski
(Exiting) Baolin Wang Aug. 30, 2018, 3:26 a.m. UTC | #16
Hi Jacek,

On 30 August 2018 at 03:15, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:
> Hi Baolin,

>

> On 08/29/2018 11:48 AM, Baolin Wang wrote:

>> Hi Jacek,

>>

>> On 29 August 2018 at 04:25, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:

>>> On 08/25/2018 09:51 AM, Baolin Wang wrote:

>>>> On 25 August 2018 at 04:44, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:

>>>>> On 08/24/2018 10:12 PM, Pavel Machek wrote:

>>>>>> On Fri 2018-08-24 21:49:50, Jacek Anaszewski wrote:

>>>>>>> Hi Pavel,

>>>>>>>

>>>>>>> On 08/24/2018 12:11 PM, Pavel Machek wrote:

>>>>>>>> Hi!

>>>>>>>>

>>>>>>>>> I think that it would be more flexible if software pattern fallback

>>>>>>>>> was applied in case of pattern_set failure. Otherwise, it would

>>>>>>>>> lead to the situation where LED class devices that support hardware

>>>>>>>>> blinking couldn't be applied the same set of patterns as LED class

>>>>>>>>> devices that don't implement pattern_set. The latter will always have to

>>>>>>>>> resort to using software pattern engine which will accept far greater

>>>>>>>>> amount of pattern combinations.

>>>>>>>>>

>>>>>>>>> In this case we need to discuss on what basis the decision will be

>>>>>>>>> made on whether hardware or software engine will be used.

>>>>>>>>>

>>>>>>>>> Possible options coming to mind:

>>>>>>>>> - an interface will be provided to determine max difference between

>>>>>>>>>   the settings supported by the hardware and the settings requested by

>>>>>>>>>   the user, that will result in aligning user's setting to the hardware

>>>>>>>>>   capabilities

>>>>>>>>> - the above alignment rate will be predefined instead

>>>>>>>>> - hardware engine will be used only if user requests supported settings

>>>>>>>>>   on the whole span of the requested pattern

>>>>>>>>> - in each of the above cases it would be worth to think of the

>>>>>>>>>   interface to show the scope of the settings supported by hardware

>>>>>>>>

>>>>>>>> I'd recommend keeping it simple. We use hardware engine if driver

>>>>>>>> author thinks pattern is "close enough".

>>>>>>>

>>>>>>> The thing is that in the ledtrig-pattern v5 implementation there

>>>>>>> is no option of using software fallback if pattern_set op

>>>>>>> is initialized:

>>>>>>>

>>>>>>> +    if (led_cdev->pattern_set) {

>>>>>>> +            return led_cdev->pattern_set(led_cdev, data->patterns,

>>>>>>> +                                         data->npatterns, data->repeat);

>>>>>>> +    }

>>>>>>

>>>>>> Yeah, that sounds wrong. (Sorry I did not pay enough attention).

>>>>>>

>>>>>> It pattern_set() returns special error code, it should just continue

>>>>>> and use software pattern fallback.

>>>>>

>>>>> And now we can get back to the issue I was concerned about in the

>>>>> email you replied to, i.e. what series of [brightness delta_t] tuples

>>>>> should be written to the pattern file to enable hardware breathing

>>>>> engine.

>>>>

>>>> OK. So now we've made a consensus to use the software pattern fallback

>>>> if failed to set hardware pattern. How about introduce one interface

>>>> to convert hardware patterns to software patterns if necessary?

>>>>

>>>> diff --git a/drivers/leds/trigger/ledtrig-pattern.c

>>>> b/drivers/leds/trigger/ledtrig-pattern.c

>>>> index 63b94a2..d46a641 100644

>>>> --- a/drivers/leds/trigger/ledtrig-pattern.c

>>>> +++ b/drivers/leds/trigger/ledtrig-pattern.c

>>>> @@ -66,8 +66,15 @@ static int pattern_trig_start_pattern(struct

>>>> pattern_trig_data *data,

>>>>                 return 0;

>>>>

>>>>         if (led_cdev->pattern_set) {

>>>> -               return led_cdev->pattern_set(led_cdev, data->patterns,

>>>> -                                            data->npatterns, data->repeat);

>>>> +               ret = led_cdev->pattern_set(led_cdev, data->patterns,

>>>> +                                           data->npatterns, data->repeat);

>>>> +               if (!ret)

>>>> +                       return 0;

>>>> +

>>>> +               dev_warn(led_cdev->dev, "Failed to set hardware pattern\n");

>>>> +

>>>> +               if (led_cdev->pattern_convert)

>>>> +                       led_cdev->pattern_convert(led_cdev,

>>>

>>> I can't see how it could help to assess if hw pattern

>>> engine can launch given pattern, and with what accuracy.

>>>

>>> Instead, I propose to add a means for defining whether the pattern

>>> to be set is intended for hardware pattern engine or for software

>>> fallback. It could be either separate sysfs file e.g. hw_pattern,

>>> or a modifier to the pattern written to the pattern file, e,g,

>>>

>>> echo "hw 100 2 200 3 100 2" > pattern

>>>

>>> hw format expected by given driver would have to be described

>>> in the per-driver ABI documentation. All patterns without

>>> "hw" prefix would enable software pattern engine.

>>

>> OK. So I did some optimization based on v5 according to suggestion, if

>> you think this is okay for you, then I will send out the formal v6

>> patch set.

>>

>> 1. echo "hw 100 2 200 3 100 2" > pattern

>> Only for hardware pattern, if failed,  we will return error number.

>

> After thinking more about it, I'd opt for a separate file

> dedicated to hardware pattern, e.g. hw_pattern, which would

> be created only if the LED class driver implemented pattern_set op.

>

>> 2. echo "100 2 200 3 100 2" > pattern

>> Will try to set hardware pattern firstly, if failed, will use software

>> pattern instead.

>>

>> diff --git a/drivers/leds/trigger/ledtrig-pattern.c

>> b/drivers/leds/trigger/ledtrig-pattern.c

>> index 63b94a2..f08e797 100644

>> --- a/drivers/leds/trigger/ledtrig-pattern.c

>> +++ b/drivers/leds/trigger/ledtrig-pattern.c

>> @@ -26,6 +26,7 @@ struct pattern_trig_data {

>>       u32 repeat;

>>       u32 last_repeat;

>>       bool is_indefinite;

>> +     bool is_hw_pattern;

>>       struct timer_list timer;

>>  };

>>

>> @@ -62,12 +63,21 @@ static void pattern_trig_timer_function(struct

>> timer_list *t)

>>  static int pattern_trig_start_pattern(struct pattern_trig_data *data,

>>                                     struct led_classdev *led_cdev)

>>  {

>> +     int ret;

>> +

>>       if (!data->npatterns)

>>               return 0;

>>

>>       if (led_cdev->pattern_set) {

>> -             return led_cdev->pattern_set(led_cdev, data->patterns,

>> +             ret = led_cdev->pattern_set(led_cdev, data->patterns,

>>                                            data->npatterns, data->repeat);

>> +             if (!ret)

>> +                     return 0;

>> +

>> +             dev_warn(led_cdev->dev, "Failed to set hardware patterns\n");

>> +

>> +             if (data->is_hw_pattern)

>> +                     return ret;

>

> With separate pattern files we could get rid of this ambiguity and

> attempt calling pattern_set only if requested via hw_pattern file.

> No software fallback should be employed in case of failure then.

>

> Similarly in case of requests originating from pattern file -

> the software engine should be set up.


Please correct me if I understood you incorrectly.

So we should create the hw_pattern file if the LED class driver
implemented pattern_set op, then no need use software fallback if it
failed to set hardware pattern.

If the LED class driver did not implement pattern_set op, we just show
up pattern file for users, and we always use software pattern now.

But AFAICT the V5 has implemented the logics, but did not change the
pattern file name according to if LED class driver implemented
pattern_set op. So now we just change the pattern file to hw_pattern
file if pattern_set op is set?

>

>>       }

>>

>>       data->curr = data->patterns;

>> @@ -106,7 +116,7 @@ static ssize_t repeat_store(struct device *dev,

>> struct device_attribute *attr,

>>       if (err)

>>               return err;

>>

>> -     if (!led_cdev->pattern_set)

>> +     if (!data->is_hw_pattern)

>>               del_timer_sync(&data->timer);

>>

>>       mutex_lock(&data->lock);

>> @@ -159,19 +169,32 @@ static ssize_t pattern_store(struct device *dev,

>> struct device_attribute *attr,

>>  {

>>       struct led_classdev *led_cdev = dev_get_drvdata(dev);

>>       struct pattern_trig_data *data = led_cdev->trigger_data;

>> -     int ccount, cr, offset = 0, err = 0;

>> +     int ccount, cr, len, offset = 0, err = 0;

>> +     const char *s;

>>

>> -     if (!led_cdev->pattern_set)

>> +     if (!data->is_hw_pattern)

>>               del_timer_sync(&data->timer);

>>

>>       mutex_lock(&data->lock);

>>

>> +     s = strstr(buf, "hw");

>> +     if (s && led_cdev->pattern_set) {

>> +             data->is_hw_pattern = true;

>> +             s += strlen("hw");

>> +             len = strlen(s);

>> +     } else {

>> +             data->is_hw_pattern = false;

>> +             s = buf;

>> +             len = count;

>> +     }

>> +

>>       data->npatterns = 0;

>> -     while (offset < count - 1 && data->npatterns < MAX_PATTERNS) {

>> +     while (offset < len - 1 && data->npatterns < MAX_PATTERNS) {

>>               cr = 0;

>> -             ccount = sscanf(buf + offset, "%d %d %n",

>> +             ccount = sscanf(s + offset, "%d %d %n",

>>                               &data->patterns[data->npatterns].brightness,

>>                               &data->patterns[data->npatterns].delta_t, &cr);

>>               if (ccount != 2) {

>>                       data->npatterns = 0;

>>                       err = -EINVAL;

>> @@ -232,7 +255,8 @@ static void pattern_trig_deactivate(struct

>> led_classdev *led_cdev)

>>

>>       if (led_cdev->pattern_clear)

>>               led_cdev->pattern_clear(led_cdev);

>> -     else

>> +

>> +     if (!data->is_hw_pattern)

>>               del_timer_sync(&data->timer);

>>

>>       led_set_brightness(led_cdev, LED_OFF);

>>

>>>

>>>> data->patterns, &data->npatterns);

>>>>         }

>>>>

>>>>         data->curr = data->patterns;

>>>> @@ -106,8 +113,7 @@ static ssize_t repeat_store(struct device *dev,

>>>> struct device_attribute *attr,

>>>>         if (err)

>>>>                 return err;

>>>>

>>>> -       if (!led_cdev->pattern_set)

>>>> -               del_timer_sync(&data->timer);

>>>> +       del_timer_sync(&data->timer);

>>>>

>>>>         mutex_lock(&data->lock);

>>>>

>>>> @@ -161,8 +167,7 @@ static ssize_t pattern_store(struct device *dev,

>>>> struct device_attribute *attr,

>>>>         struct pattern_trig_data *data = led_cdev->trigger_data;

>>>>         int ccount, cr, offset = 0, err = 0;

>>>>

>>>> -       if (!led_cdev->pattern_set)

>>>> -               del_timer_sync(&data->timer);

>>>> +       del_timer_sync(&data->timer);

>>>>

>>>>         mutex_lock(&data->lock);

>>>>

>>>> @@ -232,9 +237,8 @@ static void pattern_trig_deactivate(struct

>>>> led_classdev *led_cdev)

>>>>

>>>>         if (led_cdev->pattern_clear)

>>>>                 led_cdev->pattern_clear(led_cdev);

>>>> -       else

>>>> -               del_timer_sync(&data->timer);

>>>>

>>>> +       del_timer_sync(&data->timer);

>>>>         led_set_brightness(led_cdev, LED_OFF);

>>>>         kfree(data);

>>>>         led_cdev->activated = false;

>>>> diff --git a/include/linux/leds.h b/include/linux/leds.h

>>>> index 74fc2c6..04f3eaf 100644

>>>> --- a/include/linux/leds.h

>>>> +++ b/include/linux/leds.h

>>>> @@ -93,6 +93,8 @@ struct led_classdev {

>>>>                            struct led_pattern *pattern, int len,

>>>>                            unsigned int repeat);

>>>>         int (*pattern_clear)(struct led_classdev *led_cdev);

>>>> +       int (*pattern_convert)(struct led_classdev *led_cdev,

>>>> +                              struct led_pattern *pattern, int *len);

>>>>

>>>>         struct device           *dev;

>>>>         const struct attribute_group    **groups;

>>>>

>>>

>

>

> --

> Best regards,

> Jacek Anaszewski




-- 
Baolin Wang
Best Regards
(Exiting) Baolin Wang Aug. 30, 2018, 7:39 a.m. UTC | #17
Hi Jacek,

On 30 August 2018 at 11:26, Baolin Wang <baolin.wang@linaro.org> wrote:
> Hi Jacek,

>

> On 30 August 2018 at 03:15, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:

>> Hi Baolin,

>>

>> On 08/29/2018 11:48 AM, Baolin Wang wrote:

>>> Hi Jacek,

>>>

>>> On 29 August 2018 at 04:25, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:

>>>> On 08/25/2018 09:51 AM, Baolin Wang wrote:

>>>>> On 25 August 2018 at 04:44, Jacek Anaszewski <jacek.anaszewski@gmail.com> wrote:

>>>>>> On 08/24/2018 10:12 PM, Pavel Machek wrote:

>>>>>>> On Fri 2018-08-24 21:49:50, Jacek Anaszewski wrote:

>>>>>>>> Hi Pavel,

>>>>>>>>

>>>>>>>> On 08/24/2018 12:11 PM, Pavel Machek wrote:

>>>>>>>>> Hi!

>>>>>>>>>

>>>>>>>>>> I think that it would be more flexible if software pattern fallback

>>>>>>>>>> was applied in case of pattern_set failure. Otherwise, it would

>>>>>>>>>> lead to the situation where LED class devices that support hardware

>>>>>>>>>> blinking couldn't be applied the same set of patterns as LED class

>>>>>>>>>> devices that don't implement pattern_set. The latter will always have to

>>>>>>>>>> resort to using software pattern engine which will accept far greater

>>>>>>>>>> amount of pattern combinations.

>>>>>>>>>>

>>>>>>>>>> In this case we need to discuss on what basis the decision will be

>>>>>>>>>> made on whether hardware or software engine will be used.

>>>>>>>>>>

>>>>>>>>>> Possible options coming to mind:

>>>>>>>>>> - an interface will be provided to determine max difference between

>>>>>>>>>>   the settings supported by the hardware and the settings requested by

>>>>>>>>>>   the user, that will result in aligning user's setting to the hardware

>>>>>>>>>>   capabilities

>>>>>>>>>> - the above alignment rate will be predefined instead

>>>>>>>>>> - hardware engine will be used only if user requests supported settings

>>>>>>>>>>   on the whole span of the requested pattern

>>>>>>>>>> - in each of the above cases it would be worth to think of the

>>>>>>>>>>   interface to show the scope of the settings supported by hardware

>>>>>>>>>

>>>>>>>>> I'd recommend keeping it simple. We use hardware engine if driver

>>>>>>>>> author thinks pattern is "close enough".

>>>>>>>>

>>>>>>>> The thing is that in the ledtrig-pattern v5 implementation there

>>>>>>>> is no option of using software fallback if pattern_set op

>>>>>>>> is initialized:

>>>>>>>>

>>>>>>>> +    if (led_cdev->pattern_set) {

>>>>>>>> +            return led_cdev->pattern_set(led_cdev, data->patterns,

>>>>>>>> +                                         data->npatterns, data->repeat);

>>>>>>>> +    }

>>>>>>>

>>>>>>> Yeah, that sounds wrong. (Sorry I did not pay enough attention).

>>>>>>>

>>>>>>> It pattern_set() returns special error code, it should just continue

>>>>>>> and use software pattern fallback.

>>>>>>

>>>>>> And now we can get back to the issue I was concerned about in the

>>>>>> email you replied to, i.e. what series of [brightness delta_t] tuples

>>>>>> should be written to the pattern file to enable hardware breathing

>>>>>> engine.

>>>>>

>>>>> OK. So now we've made a consensus to use the software pattern fallback

>>>>> if failed to set hardware pattern. How about introduce one interface

>>>>> to convert hardware patterns to software patterns if necessary?

>>>>>

>>>>> diff --git a/drivers/leds/trigger/ledtrig-pattern.c

>>>>> b/drivers/leds/trigger/ledtrig-pattern.c

>>>>> index 63b94a2..d46a641 100644

>>>>> --- a/drivers/leds/trigger/ledtrig-pattern.c

>>>>> +++ b/drivers/leds/trigger/ledtrig-pattern.c

>>>>> @@ -66,8 +66,15 @@ static int pattern_trig_start_pattern(struct

>>>>> pattern_trig_data *data,

>>>>>                 return 0;

>>>>>

>>>>>         if (led_cdev->pattern_set) {

>>>>> -               return led_cdev->pattern_set(led_cdev, data->patterns,

>>>>> -                                            data->npatterns, data->repeat);

>>>>> +               ret = led_cdev->pattern_set(led_cdev, data->patterns,

>>>>> +                                           data->npatterns, data->repeat);

>>>>> +               if (!ret)

>>>>> +                       return 0;

>>>>> +

>>>>> +               dev_warn(led_cdev->dev, "Failed to set hardware pattern\n");

>>>>> +

>>>>> +               if (led_cdev->pattern_convert)

>>>>> +                       led_cdev->pattern_convert(led_cdev,

>>>>

>>>> I can't see how it could help to assess if hw pattern

>>>> engine can launch given pattern, and with what accuracy.

>>>>

>>>> Instead, I propose to add a means for defining whether the pattern

>>>> to be set is intended for hardware pattern engine or for software

>>>> fallback. It could be either separate sysfs file e.g. hw_pattern,

>>>> or a modifier to the pattern written to the pattern file, e,g,

>>>>

>>>> echo "hw 100 2 200 3 100 2" > pattern

>>>>

>>>> hw format expected by given driver would have to be described

>>>> in the per-driver ABI documentation. All patterns without

>>>> "hw" prefix would enable software pattern engine.

>>>

>>> OK. So I did some optimization based on v5 according to suggestion, if

>>> you think this is okay for you, then I will send out the formal v6

>>> patch set.

>>>

>>> 1. echo "hw 100 2 200 3 100 2" > pattern

>>> Only for hardware pattern, if failed,  we will return error number.

>>

>> After thinking more about it, I'd opt for a separate file

>> dedicated to hardware pattern, e.g. hw_pattern, which would

>> be created only if the LED class driver implemented pattern_set op.

>>

>>> 2. echo "100 2 200 3 100 2" > pattern

>>> Will try to set hardware pattern firstly, if failed, will use software

>>> pattern instead.

>>>

>>> diff --git a/drivers/leds/trigger/ledtrig-pattern.c

>>> b/drivers/leds/trigger/ledtrig-pattern.c

>>> index 63b94a2..f08e797 100644

>>> --- a/drivers/leds/trigger/ledtrig-pattern.c

>>> +++ b/drivers/leds/trigger/ledtrig-pattern.c

>>> @@ -26,6 +26,7 @@ struct pattern_trig_data {

>>>       u32 repeat;

>>>       u32 last_repeat;

>>>       bool is_indefinite;

>>> +     bool is_hw_pattern;

>>>       struct timer_list timer;

>>>  };

>>>

>>> @@ -62,12 +63,21 @@ static void pattern_trig_timer_function(struct

>>> timer_list *t)

>>>  static int pattern_trig_start_pattern(struct pattern_trig_data *data,

>>>                                     struct led_classdev *led_cdev)

>>>  {

>>> +     int ret;

>>> +

>>>       if (!data->npatterns)

>>>               return 0;

>>>

>>>       if (led_cdev->pattern_set) {

>>> -             return led_cdev->pattern_set(led_cdev, data->patterns,

>>> +             ret = led_cdev->pattern_set(led_cdev, data->patterns,

>>>                                            data->npatterns, data->repeat);

>>> +             if (!ret)

>>> +                     return 0;

>>> +

>>> +             dev_warn(led_cdev->dev, "Failed to set hardware patterns\n");

>>> +

>>> +             if (data->is_hw_pattern)

>>> +                     return ret;

>>

>> With separate pattern files we could get rid of this ambiguity and

>> attempt calling pattern_set only if requested via hw_pattern file.

>> No software fallback should be employed in case of failure then.

>>

>> Similarly in case of requests originating from pattern file -

>> the software engine should be set up.

>

> Please correct me if I understood you incorrectly.

>

> So we should create the hw_pattern file if the LED class driver

> implemented pattern_set op, then no need use software fallback if it

> failed to set hardware pattern.

>

> If the LED class driver did not implement pattern_set op, we just show

> up pattern file for users, and we always use software pattern now.

>

> But AFAICT the V5 has implemented the logics, but did not change the

> pattern file name according to if LED class driver implemented

> pattern_set op. So now we just change the pattern file to hw_pattern

> file if pattern_set op is set?

>


Sorry for misunderstanding your points, now I realized we will create
2 files for software pattern and hardware pattern if the pattern_set
op was implemented. I submitted v6 patch set according to your
suggestion. Please help to review. Thanks.

-- 
Baolin Wang
Best Regards
diff mbox series

Patch

diff --git a/Documentation/ABI/testing/sysfs-class-led-trigger-pattern b/Documentation/ABI/testing/sysfs-class-led-trigger-pattern
new file mode 100644
index 0000000..bc7475f
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-class-led-trigger-pattern
@@ -0,0 +1,24 @@ 
+What:		/sys/class/leds/<led>/pattern
+Date:		August 2018
+KernelVersion:	4.19
+Description:
+		Specify a pattern for the LED, for LED hardware that support
+		altering the brightness as a function of time.
+
+		The pattern is given by a series of tuples, of brightness and
+		duration (ms). The LED is expected to traverse the series and
+		each brightness value for the specified duration. Duration of
+		0 means brightness should immediately change to new value.
+
+		The format of the pattern values should be:
+		"brightness_1 duration_1 brightness_2 duration_2 brightness_3
+		duration_3 ...".
+
+What:		/sys/class/leds/<led>/repeat
+Date:		August 2018
+KernelVersion:	4.19
+Description:
+		Specify a pattern repeat number. 0 means repeat indefinitely.
+
+		This file will always return the originally written repeat
+		number.
diff --git a/drivers/leds/trigger/Kconfig b/drivers/leds/trigger/Kconfig
index 4018af7..b76fc3c 100644
--- a/drivers/leds/trigger/Kconfig
+++ b/drivers/leds/trigger/Kconfig
@@ -129,4 +129,11 @@  config LEDS_TRIGGER_NETDEV
 	  This allows LEDs to be controlled by network device activity.
 	  If unsure, say Y.
 
+config LEDS_TRIGGER_PATTERN
+	tristate "LED Pattern Trigger"
+	help
+	  This allows LEDs to be controlled by a software or hardware pattern
+	  which is a series of tuples, of brightness and duration (ms).
+	  If unsure, say N
+
 endif # LEDS_TRIGGERS
diff --git a/drivers/leds/trigger/Makefile b/drivers/leds/trigger/Makefile
index f3cfe19..9bcb64e 100644
--- a/drivers/leds/trigger/Makefile
+++ b/drivers/leds/trigger/Makefile
@@ -13,3 +13,4 @@  obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT)	+= ledtrig-transient.o
 obj-$(CONFIG_LEDS_TRIGGER_CAMERA)	+= ledtrig-camera.o
 obj-$(CONFIG_LEDS_TRIGGER_PANIC)	+= ledtrig-panic.o
 obj-$(CONFIG_LEDS_TRIGGER_NETDEV)	+= ledtrig-netdev.o
+obj-$(CONFIG_LEDS_TRIGGER_PATTERN)	+= ledtrig-pattern.o
diff --git a/drivers/leds/trigger/ledtrig-pattern.c b/drivers/leds/trigger/ledtrig-pattern.c
new file mode 100644
index 0000000..63b94a2
--- /dev/null
+++ b/drivers/leds/trigger/ledtrig-pattern.c
@@ -0,0 +1,266 @@ 
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * LED pattern trigger
+ *
+ * Idea discussed with Pavel Machek. Raphael Teysseyre implemented
+ * the first version, Baolin Wang simplified and improved the approach.
+ */
+
+#include <linux/kernel.h>
+#include <linux/leds.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include <linux/timer.h>
+
+#define MAX_PATTERNS		1024
+
+struct pattern_trig_data {
+	struct led_classdev *led_cdev;
+	struct led_pattern patterns[MAX_PATTERNS];
+	struct led_pattern *curr;
+	struct led_pattern *next;
+	struct mutex lock;
+	u32 npatterns;
+	u32 repeat;
+	u32 last_repeat;
+	bool is_indefinite;
+	struct timer_list timer;
+};
+
+static void pattern_trig_update_patterns(struct pattern_trig_data *data)
+{
+	data->curr = data->next;
+	if (!data->is_indefinite && data->curr == data->patterns)
+		data->repeat--;
+
+	if (data->next == data->patterns + data->npatterns - 1)
+		data->next = data->patterns;
+	else
+		data->next++;
+}
+
+static void pattern_trig_timer_function(struct timer_list *t)
+{
+	struct pattern_trig_data *data = from_timer(data, t, timer);
+
+	mutex_lock(&data->lock);
+
+	if (!data->is_indefinite && !data->repeat) {
+		mutex_unlock(&data->lock);
+		return;
+	}
+
+	led_set_brightness(data->led_cdev, data->curr->brightness);
+	mod_timer(&data->timer, jiffies + msecs_to_jiffies(data->curr->delta_t));
+	pattern_trig_update_patterns(data);
+
+	mutex_unlock(&data->lock);
+}
+
+static int pattern_trig_start_pattern(struct pattern_trig_data *data,
+				      struct led_classdev *led_cdev)
+{
+	if (!data->npatterns)
+		return 0;
+
+	if (led_cdev->pattern_set) {
+		return led_cdev->pattern_set(led_cdev, data->patterns,
+					     data->npatterns, data->repeat);
+	}
+
+	data->curr = data->patterns;
+	data->next = data->npatterns > 1 ? data->patterns + 1 : data->patterns;
+	data->timer.expires = jiffies;
+	add_timer(&data->timer);
+
+	return 0;
+}
+
+static ssize_t repeat_show(struct device *dev, struct device_attribute *attr,
+			   char *buf)
+{
+	struct led_classdev *led_cdev = dev_get_drvdata(dev);
+	struct pattern_trig_data *data = led_cdev->trigger_data;
+	u32 repeat;
+
+	mutex_lock(&data->lock);
+
+	repeat = data->last_repeat;
+
+	mutex_unlock(&data->lock);
+
+	return scnprintf(buf, PAGE_SIZE, "%u\n", repeat);
+}
+
+static ssize_t repeat_store(struct device *dev, struct device_attribute *attr,
+			    const char *buf, size_t count)
+{
+	struct led_classdev *led_cdev = dev_get_drvdata(dev);
+	struct pattern_trig_data *data = led_cdev->trigger_data;
+	unsigned long res;
+	int err;
+
+	err = kstrtoul(buf, 10, &res);
+	if (err)
+		return err;
+
+	if (!led_cdev->pattern_set)
+		del_timer_sync(&data->timer);
+
+	mutex_lock(&data->lock);
+
+	data->last_repeat = data->repeat = res;
+
+	/* 0 means repeat indefinitely */
+	if (!data->repeat)
+		data->is_indefinite = true;
+	else
+		data->is_indefinite = false;
+
+	err = pattern_trig_start_pattern(data, led_cdev);
+
+	mutex_unlock(&data->lock);
+	return err < 0 ? err : count;
+}
+
+static DEVICE_ATTR_RW(repeat);
+
+static ssize_t pattern_show(struct device *dev, struct device_attribute *attr,
+			    char *buf)
+{
+	struct led_classdev *led_cdev = dev_get_drvdata(dev);
+	struct pattern_trig_data *data = led_cdev->trigger_data;
+	ssize_t count = 0;
+	int i;
+
+	mutex_lock(&data->lock);
+
+	if (!data->npatterns)
+		goto out;
+
+	for (i = 0; i < data->npatterns; i++) {
+		count += scnprintf(buf + count, PAGE_SIZE - count,
+				   "%d %d ",
+				   data->patterns[i].brightness,
+				   data->patterns[i].delta_t);
+	}
+
+	buf[count - 1] = '\n';
+
+out:
+	mutex_unlock(&data->lock);
+	return count;
+}
+
+static ssize_t pattern_store(struct device *dev, struct device_attribute *attr,
+			     const char *buf, size_t count)
+{
+	struct led_classdev *led_cdev = dev_get_drvdata(dev);
+	struct pattern_trig_data *data = led_cdev->trigger_data;
+	int ccount, cr, offset = 0, err = 0;
+
+	if (!led_cdev->pattern_set)
+		del_timer_sync(&data->timer);
+
+	mutex_lock(&data->lock);
+
+	data->npatterns = 0;
+	while (offset < count - 1 && data->npatterns < MAX_PATTERNS) {
+		cr = 0;
+		ccount = sscanf(buf + offset, "%d %d %n",
+				&data->patterns[data->npatterns].brightness,
+				&data->patterns[data->npatterns].delta_t, &cr);
+		if (ccount != 2) {
+			data->npatterns = 0;
+			err = -EINVAL;
+			goto out;
+		}
+
+		offset += cr;
+		data->npatterns++;
+	}
+
+	err = pattern_trig_start_pattern(data, led_cdev);
+
+out:
+	mutex_unlock(&data->lock);
+	return err < 0 ? err : count;
+}
+
+static DEVICE_ATTR_RW(pattern);
+
+static struct attribute *pattern_trig_attrs[] = {
+	&dev_attr_pattern.attr,
+	&dev_attr_repeat.attr,
+	NULL
+};
+ATTRIBUTE_GROUPS(pattern_trig);
+
+static int pattern_trig_activate(struct led_classdev *led_cdev)
+{
+	struct pattern_trig_data *data;
+
+	data = kzalloc(sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	if (!!led_cdev->pattern_set ^ !!led_cdev->pattern_clear) {
+		dev_warn(led_cdev->dev,
+			 "Hardware pattern ops validation failed\n");
+		led_cdev->pattern_set = NULL;
+		led_cdev->pattern_clear = NULL;
+	}
+
+	data->is_indefinite = true;
+	mutex_init(&data->lock);
+	data->led_cdev = led_cdev;
+	led_set_trigger_data(led_cdev, data);
+	timer_setup(&data->timer, pattern_trig_timer_function, 0);
+	led_cdev->activated = true;
+
+	return 0;
+}
+
+static void pattern_trig_deactivate(struct led_classdev *led_cdev)
+{
+	struct pattern_trig_data *data = led_cdev->trigger_data;
+
+	if (!led_cdev->activated)
+		return;
+
+	if (led_cdev->pattern_clear)
+		led_cdev->pattern_clear(led_cdev);
+	else
+		del_timer_sync(&data->timer);
+
+	led_set_brightness(led_cdev, LED_OFF);
+	kfree(data);
+	led_cdev->activated = false;
+}
+
+static struct led_trigger pattern_led_trigger = {
+	.name = "pattern",
+	.activate = pattern_trig_activate,
+	.deactivate = pattern_trig_deactivate,
+	.groups = pattern_trig_groups,
+};
+
+static int __init pattern_trig_init(void)
+{
+	return led_trigger_register(&pattern_led_trigger);
+}
+
+static void __exit pattern_trig_exit(void)
+{
+	led_trigger_unregister(&pattern_led_trigger);
+}
+
+module_init(pattern_trig_init);
+module_exit(pattern_trig_exit);
+
+MODULE_AUTHOR("Raphael Teysseyre <rteysseyre@gmail.com");
+MODULE_AUTHOR("Baolin Wang <baolin.wang@linaro.org");
+MODULE_DESCRIPTION("LED Pattern trigger");
+MODULE_LICENSE("GPL v2");
diff --git a/include/linux/leds.h b/include/linux/leds.h
index 834683d..74fc2c6 100644
--- a/include/linux/leds.h
+++ b/include/linux/leds.h
@@ -22,6 +22,7 @@ 
 #include <linux/workqueue.h>
 
 struct device;
+struct led_pattern;
 /*
  * LED Core
  */
@@ -88,6 +89,11 @@  struct led_classdev {
 				     unsigned long *delay_on,
 				     unsigned long *delay_off);
 
+	int (*pattern_set)(struct led_classdev *led_cdev,
+			   struct led_pattern *pattern, int len,
+			   unsigned int repeat);
+	int (*pattern_clear)(struct led_classdev *led_cdev);
+
 	struct device		*dev;
 	const struct attribute_group	**groups;
 
@@ -472,4 +478,14 @@  static inline void led_classdev_notify_brightness_hw_changed(
 	struct led_classdev *led_cdev, enum led_brightness brightness) { }
 #endif
 
+/**
+ * struct led_pattern - pattern interval settings
+ * @delta_t: pattern interval delay, in milliseconds
+ * @brightness: pattern interval brightness
+ */
+struct led_pattern {
+	int delta_t;
+	int brightness;
+};
+
 #endif		/* __LINUX_LEDS_H_INCLUDED */