Skip to content

Commit 1471c2f

Browse files
cx-anuj-pathakcarlescufi
authored andcommitted
drivers: led: lp5562: Add PM Device support
- Add PM_DEVICE support for power management Signed-off-by: Anuj Pathak <[email protected]>
1 parent c13eabb commit 1471c2f

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

drivers/led/lp5562.c

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <zephyr/drivers/gpio.h>
3636
#include <zephyr/device.h>
3737
#include <zephyr/kernel.h>
38+
#include <zephyr/pm/device.h>
3839

3940
#define LOG_LEVEL CONFIG_LED_LOG_LEVEL
4041
#include <zephyr/logging/log.h>
@@ -937,7 +938,7 @@ static int lp5562_led_update_current(const struct device *dev)
937938
return ret;
938939
}
939940

940-
static int lp5562_enable(const struct device *dev)
941+
static int lp5562_enable(const struct device *dev, bool soft_reset)
941942
{
942943
const struct lp5562_config *config = dev->config;
943944
const struct gpio_dt_spec *enable_gpio = &config->enable_gpio;
@@ -958,11 +959,13 @@ static int lp5562_enable(const struct device *dev)
958959
k_sleep(K_MSEC(1));
959960
}
960961

961-
/* Reset all internal registers to have a deterministic state. */
962-
err = i2c_reg_write_byte_dt(&config->bus, LP5562_RESET, 0xFF);
963-
if (err) {
964-
LOG_ERR("%s: failed to soft-reset device", dev->name);
965-
return err;
962+
if (soft_reset) {
963+
/* Reset all internal registers to have a deterministic state. */
964+
err = i2c_reg_write_byte_dt(&config->bus, LP5562_RESET, 0xFF);
965+
if (err) {
966+
LOG_ERR("%s: failed to soft-reset device", dev->name);
967+
return err;
968+
}
966969
}
967970

968971
/* Set en bit in LP5562_ENABLE register. */
@@ -977,6 +980,33 @@ static int lp5562_enable(const struct device *dev)
977980
return 0;
978981
}
979982

983+
#ifdef CONFIG_PM_DEVICE
984+
static int lp5562_disable(const struct device *dev)
985+
{
986+
const struct lp5562_config *config = dev->config;
987+
const struct gpio_dt_spec *enable_gpio = &config->enable_gpio;
988+
int err = 0;
989+
990+
/* clear en bit in register configurations */
991+
err = i2c_reg_update_byte_dt(&config->bus, LP5562_ENABLE, LP5562_ENABLE_CHIP_EN_MASK,
992+
LP5562_ENABLE_CHIP_EN_CLR);
993+
if (err) {
994+
LOG_ERR("%s: failed to clear EN Bit in ENABLE register", dev->name);
995+
return err;
996+
}
997+
998+
/* if gpio control is enabled, we can de-assert EN_GPIO now */
999+
if (enable_gpio->port != NULL) {
1000+
err = gpio_pin_set_dt(enable_gpio, 0);
1001+
if (err) {
1002+
LOG_ERR("%s: failed to set enable GPIO to 0", dev->name);
1003+
return err;
1004+
}
1005+
}
1006+
return 0;
1007+
}
1008+
#endif
1009+
9801010
static int lp5562_led_init(const struct device *dev)
9811011
{
9821012
const struct lp5562_config *config = dev->config;
@@ -1001,7 +1031,7 @@ static int lp5562_led_init(const struct device *dev)
10011031
return -ENODEV;
10021032
}
10031033

1004-
ret = lp5562_enable(dev);
1034+
ret = lp5562_enable(dev, true);
10051035
if (ret) {
10061036
return ret;
10071037
}
@@ -1045,6 +1075,20 @@ static const struct led_driver_api lp5562_led_api = {
10451075
.off = lp5562_led_off,
10461076
};
10471077

1078+
#ifdef CONFIG_PM_DEVICE
1079+
static int lp5562_pm_action(const struct device *dev, enum pm_device_action action)
1080+
{
1081+
switch (action) {
1082+
case PM_DEVICE_ACTION_SUSPEND:
1083+
return lp5562_disable(dev);
1084+
case PM_DEVICE_ACTION_RESUME:
1085+
return lp5562_enable(dev, false);
1086+
default:
1087+
return -ENOTSUP;
1088+
}
1089+
}
1090+
#endif /* CONFIG_PM_DEVICE */
1091+
10481092
#define LP5562_DEFINE(id) \
10491093
BUILD_ASSERT(DT_INST_PROP(id, red_output_current) <= LP5562_MAX_CURRENT_SETTING,\
10501094
"Red channel current must be between 0 and 25.5 mA."); \
@@ -1063,8 +1107,10 @@ static const struct led_driver_api lp5562_led_api = {
10631107
.enable_gpio = GPIO_DT_SPEC_INST_GET_OR(id, enable_gpios, {0}), \
10641108
}; \
10651109
\
1110+
PM_DEVICE_DT_INST_DEFINE(id, lp5562_pm_action); \
1111+
\
10661112
struct lp5562_data lp5562_data_##id; \
1067-
DEVICE_DT_INST_DEFINE(id, &lp5562_led_init, NULL, \
1113+
DEVICE_DT_INST_DEFINE(id, &lp5562_led_init, PM_DEVICE_DT_INST_GET(id), \
10681114
&lp5562_data_##id, \
10691115
&lp5562_config_##id, POST_KERNEL, \
10701116
CONFIG_LED_INIT_PRIORITY, \

0 commit comments

Comments
 (0)