Skip to content

Commit 791bc41

Browse files
OsterlaDpavelmachek
authored andcommitted
leds: move default_state read from fwnode to core
This patch introduces a new function to read initial default_state from fwnode. Suggested-by: Pavel Machek <[email protected]> Signed-off-by: Denis Osterland-Heim <[email protected]> Signed-off-by: Pavel Machek <[email protected]>
1 parent 654933a commit 791bc41

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

drivers/leds/led-core.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,18 @@ int led_compose_name(struct device *dev, struct led_init_data *init_data,
477477
return 0;
478478
}
479479
EXPORT_SYMBOL_GPL(led_compose_name);
480+
481+
enum led_default_state led_init_default_state_get(struct fwnode_handle *fwnode)
482+
{
483+
const char *state = NULL;
484+
485+
if (!fwnode_property_read_string(fwnode, "default-state", &state)) {
486+
if (!strcmp(state, "keep"))
487+
return LEDS_DEFSTATE_KEEP;
488+
if (!strcmp(state, "on"))
489+
return LEDS_DEFSTATE_ON;
490+
}
491+
492+
return LEDS_DEFSTATE_OFF;
493+
}
494+
EXPORT_SYMBOL_GPL(led_init_default_state_get);

drivers/leds/leds-gpio.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/platform_device.h>
1717
#include <linux/property.h>
1818
#include <linux/slab.h>
19+
#include "leds.h"
1920

2021
struct gpio_led_data {
2122
struct led_classdev cdev;
@@ -144,7 +145,6 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
144145
device_for_each_child_node(dev, child) {
145146
struct gpio_led_data *led_dat = &priv->leds[priv->num_leds];
146147
struct gpio_led led = {};
147-
const char *state = NULL;
148148

149149
/*
150150
* Acquire gpiod from DT with uninitialized label, which
@@ -161,15 +161,7 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
161161

162162
led_dat->gpiod = led.gpiod;
163163

164-
if (!fwnode_property_read_string(child, "default-state",
165-
&state)) {
166-
if (!strcmp(state, "keep"))
167-
led.default_state = LEDS_GPIO_DEFSTATE_KEEP;
168-
else if (!strcmp(state, "on"))
169-
led.default_state = LEDS_GPIO_DEFSTATE_ON;
170-
else
171-
led.default_state = LEDS_GPIO_DEFSTATE_OFF;
172-
}
164+
led.default_state = led_init_default_state_get(child);
173165

174166
if (fwnode_property_present(child, "retain-state-suspended"))
175167
led.retain_state_suspended = 1;

drivers/leds/leds.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ ssize_t led_trigger_read(struct file *filp, struct kobject *kobj,
2727
ssize_t led_trigger_write(struct file *filp, struct kobject *kobj,
2828
struct bin_attribute *bin_attr, char *buf,
2929
loff_t pos, size_t count);
30+
enum led_default_state led_init_default_state_get(struct fwnode_handle *fwnode);
3031

3132
extern struct rw_semaphore leds_list_lock;
3233
extern struct list_head leds_list;

include/linux/leds.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ enum led_brightness {
3333
LED_FULL = 255,
3434
};
3535

36+
enum led_default_state {
37+
LEDS_DEFSTATE_OFF = 0,
38+
LEDS_DEFSTATE_ON = 1,
39+
LEDS_DEFSTATE_KEEP = 2,
40+
};
41+
3642
struct led_init_data {
3743
/* device fwnode handle */
3844
struct fwnode_handle *fwnode;
@@ -520,9 +526,9 @@ struct gpio_led {
520526
/* default_state should be one of LEDS_GPIO_DEFSTATE_(ON|OFF|KEEP) */
521527
struct gpio_desc *gpiod;
522528
};
523-
#define LEDS_GPIO_DEFSTATE_OFF 0
524-
#define LEDS_GPIO_DEFSTATE_ON 1
525-
#define LEDS_GPIO_DEFSTATE_KEEP 2
529+
#define LEDS_GPIO_DEFSTATE_OFF LEDS_DEFSTATE_OFF
530+
#define LEDS_GPIO_DEFSTATE_ON LEDS_DEFSTATE_ON
531+
#define LEDS_GPIO_DEFSTATE_KEEP LEDS_DEFSTATE_KEEP
526532

527533
struct gpio_led_platform_data {
528534
int num_leds;

0 commit comments

Comments
 (0)