2023-05-30 05:04:48 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023 Phytec Messtechnik GmbH.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT ti_lp5569
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief LP5569 LED controller
|
|
|
|
*
|
|
|
|
* The LP5569 is a 9-channel LED driver that communicates over I2C.
|
|
|
|
*/
|
|
|
|
|
2024-03-21 17:45:45 +08:00
|
|
|
#include <zephyr/drivers/gpio.h>
|
2023-05-30 05:04:48 +08:00
|
|
|
#include <zephyr/drivers/i2c.h>
|
|
|
|
#include <zephyr/drivers/led.h>
|
|
|
|
#include <zephyr/device.h>
|
2023-05-30 05:08:16 +08:00
|
|
|
#include <zephyr/pm/device.h>
|
2023-05-30 05:04:48 +08:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(lp5569, CONFIG_LED_LOG_LEVEL);
|
|
|
|
|
2024-08-22 16:22:28 +08:00
|
|
|
#define LP5569_NUM_LEDS 9
|
2023-05-30 05:04:48 +08:00
|
|
|
|
|
|
|
/* General Registers */
|
2024-08-22 16:22:28 +08:00
|
|
|
#define LP5569_CONFIG 0x00
|
|
|
|
#define LP5569_CHIP_EN BIT(6)
|
2023-05-30 05:04:48 +08:00
|
|
|
|
2024-08-22 16:22:28 +08:00
|
|
|
#define LP5569_MISC 0x2F
|
|
|
|
#define LP5569_POWERSAVE_EN BIT(5)
|
|
|
|
#define LP5569_CP_MODE_SHIFT 3
|
2023-05-30 05:04:48 +08:00
|
|
|
|
|
|
|
/* PWM base Register for controlling the duty-cycle */
|
2024-08-22 16:22:28 +08:00
|
|
|
#define LP5569_LED0_PWM 0x16
|
2023-05-30 05:04:48 +08:00
|
|
|
|
|
|
|
struct lp5569_config {
|
|
|
|
struct i2c_dt_spec bus;
|
2024-03-21 17:45:45 +08:00
|
|
|
struct gpio_dt_spec enable_gpio;
|
2024-03-21 17:07:46 +08:00
|
|
|
const uint8_t cp_mode;
|
2023-05-30 05:04:48 +08:00
|
|
|
};
|
|
|
|
|
2024-08-22 16:22:28 +08:00
|
|
|
static int lp5569_led_set_brightness(const struct device *dev, uint32_t led, uint8_t brightness)
|
2023-05-30 05:04:48 +08:00
|
|
|
{
|
|
|
|
const struct lp5569_config *config = dev->config;
|
|
|
|
uint8_t val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (led >= LP5569_NUM_LEDS || brightness > 100) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Map 0-100 % to 0-255 pwm register value */
|
|
|
|
val = brightness * 255 / 100;
|
|
|
|
|
|
|
|
ret = i2c_reg_write_byte_dt(&config->bus, LP5569_LED0_PWM + led, val);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG_ERR("LED reg update failed");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int lp5569_led_on(const struct device *dev, uint32_t led)
|
|
|
|
{
|
|
|
|
/* Set LED brightness to 100 % */
|
|
|
|
return lp5569_led_set_brightness(dev, led, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int lp5569_led_off(const struct device *dev, uint32_t led)
|
|
|
|
{
|
|
|
|
/* Set LED brightness to 0 % */
|
|
|
|
return lp5569_led_set_brightness(dev, led, 0);
|
|
|
|
}
|
|
|
|
|
2023-05-30 05:08:16 +08:00
|
|
|
static int lp5569_enable(const struct device *dev)
|
2023-05-30 05:04:48 +08:00
|
|
|
{
|
|
|
|
const struct lp5569_config *config = dev->config;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!i2c_is_ready_dt(&config->bus)) {
|
|
|
|
LOG_ERR("I2C device not ready");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2024-03-21 17:45:45 +08:00
|
|
|
/* flip the enable pin if specified */
|
|
|
|
if (config->enable_gpio.port) {
|
|
|
|
if (!gpio_is_ready_dt(&config->enable_gpio)) {
|
|
|
|
LOG_ERR("Enable GPIO not ready");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2024-08-22 16:22:28 +08:00
|
|
|
ret = gpio_pin_configure_dt(&config->enable_gpio, GPIO_OUTPUT_ACTIVE);
|
2024-03-21 17:45:45 +08:00
|
|
|
if (ret < 0) {
|
2024-08-22 16:22:28 +08:00
|
|
|
LOG_ERR("Failed to configure enable_gpio, err: %d", ret);
|
2024-03-21 17:45:45 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* datasheet 7.9: t_en max 3 ms for chip initialization */
|
|
|
|
k_msleep(3);
|
|
|
|
}
|
|
|
|
|
2024-08-22 16:22:28 +08:00
|
|
|
ret = i2c_reg_write_byte_dt(&config->bus, LP5569_CONFIG, LP5569_CHIP_EN);
|
2023-05-30 05:04:48 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
LOG_ERR("Enable LP5569 failed");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = i2c_reg_write_byte_dt(&config->bus, LP5569_MISC,
|
2024-03-21 17:07:46 +08:00
|
|
|
LP5569_POWERSAVE_EN |
|
2024-08-22 16:22:28 +08:00
|
|
|
(config->cp_mode << LP5569_CP_MODE_SHIFT));
|
2023-05-30 05:04:48 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
LOG_ERR("LED reg update failed");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-30 05:08:16 +08:00
|
|
|
static int lp5569_init(const struct device *dev)
|
|
|
|
{
|
|
|
|
/* If the device is behind a power domain, it will start in
|
|
|
|
* PM_DEVICE_STATE_OFF.
|
|
|
|
*/
|
|
|
|
if (pm_device_on_power_domain(dev)) {
|
|
|
|
pm_device_init_off(dev);
|
|
|
|
LOG_INF("Init %s as PM_DEVICE_STATE_OFF", dev->name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lp5569_enable(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PM_DEVICE
|
2024-08-22 16:22:28 +08:00
|
|
|
static int lp5569_pm_action(const struct device *dev, enum pm_device_action action)
|
2023-05-30 05:08:16 +08:00
|
|
|
{
|
|
|
|
const struct lp5569_config *config = dev->config;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case PM_DEVICE_ACTION_TURN_ON:
|
|
|
|
case PM_DEVICE_ACTION_RESUME:
|
|
|
|
ret = lp5569_enable(dev);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG_ERR("Enable LP5569 failed");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PM_DEVICE_ACTION_TURN_OFF:
|
|
|
|
case PM_DEVICE_ACTION_SUSPEND:
|
2024-08-22 16:22:28 +08:00
|
|
|
ret = i2c_reg_update_byte_dt(&config->bus, LP5569_CONFIG, LP5569_CHIP_EN, 0);
|
2023-05-30 05:08:16 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
LOG_ERR("Disable LP5569 failed");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_PM_DEVICE */
|
2023-05-30 05:04:48 +08:00
|
|
|
|
|
|
|
static const struct led_driver_api lp5569_led_api = {
|
|
|
|
.set_brightness = lp5569_led_set_brightness,
|
|
|
|
.on = lp5569_led_on,
|
|
|
|
.off = lp5569_led_off,
|
|
|
|
};
|
|
|
|
|
2024-08-22 16:22:28 +08:00
|
|
|
#define LP5569_DEFINE(id) \
|
|
|
|
static const struct lp5569_config lp5569_config_##id = { \
|
|
|
|
.bus = I2C_DT_SPEC_INST_GET(id), \
|
|
|
|
.enable_gpio = GPIO_DT_SPEC_INST_GET_OR(id, enable_gpios, {0}), \
|
|
|
|
.cp_mode = DT_ENUM_IDX(DT_DRV_INST(id), charge_pump_mode), \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
PM_DEVICE_DT_INST_DEFINE(id, lp5569_pm_action); \
|
|
|
|
\
|
|
|
|
DEVICE_DT_INST_DEFINE(id, &lp5569_init, PM_DEVICE_DT_INST_GET(id), NULL, \
|
|
|
|
&lp5569_config_##id, POST_KERNEL, CONFIG_LED_INIT_PRIORITY, \
|
2023-05-30 05:04:48 +08:00
|
|
|
&lp5569_led_api);
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(LP5569_DEFINE)
|