Skip to content

Commit e46cb6d

Browse files
Eddie Jamespavelmachek
authored andcommitted
leds: pca955x: Implement the default-state property
In order to retain the LED state after a system reboot, check the documented default-state device tree property during initialization. Modify the behavior of the probe according to the property. Signed-off-by: Eddie James <[email protected]> Signed-off-by: Pavel Machek <[email protected]>
1 parent 7086625 commit e46cb6d

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

drivers/leds/leds-pca955x.c

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ struct pca955x_led {
129129
int led_num; /* 0 .. 15 potentially */
130130
char name[32];
131131
u32 type;
132+
int default_state;
132133
const char *default_trigger;
133134
};
134135

@@ -439,6 +440,7 @@ pca955x_get_pdata(struct i2c_client *client, struct pca955x_chipdef *chip)
439440

440441
device_for_each_child_node(&client->dev, child) {
441442
const char *name;
443+
const char *state;
442444
u32 reg;
443445
int res;
444446

@@ -457,6 +459,18 @@ pca955x_get_pdata(struct i2c_client *client, struct pca955x_chipdef *chip)
457459
fwnode_property_read_u32(child, "type", &led->type);
458460
fwnode_property_read_string(child, "linux,default-trigger",
459461
&led->default_trigger);
462+
463+
if (!fwnode_property_read_string(child, "default-state",
464+
&state)) {
465+
if (!strcmp(state, "keep"))
466+
led->default_state = LEDS_GPIO_DEFSTATE_KEEP;
467+
else if (!strcmp(state, "on"))
468+
led->default_state = LEDS_GPIO_DEFSTATE_ON;
469+
else
470+
led->default_state = LEDS_GPIO_DEFSTATE_OFF;
471+
} else {
472+
led->default_state = LEDS_GPIO_DEFSTATE_OFF;
473+
}
460474
}
461475

462476
pdata->num_leds = chip->bits;
@@ -485,6 +499,7 @@ static int pca955x_probe(struct i2c_client *client,
485499
int i, err;
486500
struct pca955x_platform_data *pdata;
487501
int ngpios = 0;
502+
bool keep_pwm = false;
488503

489504
chip = &pca955x_chipdefs[id->driver_data];
490505
adapter = client->adapter;
@@ -565,14 +580,35 @@ static int pca955x_probe(struct i2c_client *client,
565580
led->brightness_set_blocking = pca955x_led_set;
566581
led->brightness_get = pca955x_led_get;
567582

583+
if (pdata->leds[i].default_state ==
584+
LEDS_GPIO_DEFSTATE_OFF) {
585+
err = pca955x_led_set(led, LED_OFF);
586+
if (err)
587+
return err;
588+
} else if (pdata->leds[i].default_state ==
589+
LEDS_GPIO_DEFSTATE_ON) {
590+
err = pca955x_led_set(led, LED_FULL);
591+
if (err)
592+
return err;
593+
}
594+
568595
err = devm_led_classdev_register(&client->dev, led);
569596
if (err)
570597
return err;
571598

572-
/* Turn off LED */
573-
err = pca955x_led_set(led, LED_OFF);
574-
if (err)
575-
return err;
599+
/*
600+
* For default-state == "keep", let the core update the
601+
* brightness from the hardware, then check the
602+
* brightness to see if it's using PWM1. If so, PWM1
603+
* should not be written below.
604+
*/
605+
if (pdata->leds[i].default_state ==
606+
LEDS_GPIO_DEFSTATE_KEEP) {
607+
if (led->brightness != LED_FULL &&
608+
led->brightness != LED_OFF &&
609+
led->brightness != LED_HALF)
610+
keep_pwm = true;
611+
}
576612
}
577613
}
578614

@@ -581,10 +617,12 @@ static int pca955x_probe(struct i2c_client *client,
581617
if (err)
582618
return err;
583619

584-
/* PWM1 is used for variable brightness, default to OFF */
585-
err = pca955x_write_pwm(client, 1, 0);
586-
if (err)
587-
return err;
620+
if (!keep_pwm) {
621+
/* PWM1 is used for variable brightness, default to OFF */
622+
err = pca955x_write_pwm(client, 1, 0);
623+
if (err)
624+
return err;
625+
}
588626

589627
/* Set to fast frequency so we do not see flashing */
590628
err = pca955x_write_psc(client, 0, 0);

0 commit comments

Comments
 (0)