drivers: led: lp3943: use i2c_dt_spec
Simplify driver implementation by using i2c_dt_spec. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
parent
071b80290c
commit
a5b68f51ea
1 changed files with 31 additions and 29 deletions
|
@ -51,8 +51,11 @@ enum lp3943_modes {
|
||||||
LP3943_DIM1,
|
LP3943_DIM1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct lp3943_config {
|
||||||
|
struct i2c_dt_spec bus;
|
||||||
|
};
|
||||||
|
|
||||||
struct lp3943_data {
|
struct lp3943_data {
|
||||||
const struct device *i2c;
|
|
||||||
struct led_data dev_data;
|
struct led_data dev_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -98,7 +101,8 @@ static int lp3943_get_led_reg(uint32_t *led, uint8_t *reg)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lp3943_set_dim_states(struct lp3943_data *data, uint32_t led, uint8_t mode)
|
static int lp3943_set_dim_states(const struct lp3943_config *config,
|
||||||
|
uint32_t led, uint8_t mode)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
uint8_t reg;
|
uint8_t reg;
|
||||||
|
@ -109,10 +113,8 @@ static int lp3943_set_dim_states(struct lp3943_data *data, uint32_t led, uint8_t
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set DIMx states for the LEDs */
|
/* Set DIMx states for the LEDs */
|
||||||
if (i2c_reg_update_byte(data->i2c, DT_INST_REG_ADDR(0),
|
if (i2c_reg_update_byte_dt(&config->bus, reg, LP3943_MASK << (led << 1),
|
||||||
reg,
|
mode << (led << 1))) {
|
||||||
LP3943_MASK << (led << 1),
|
|
||||||
mode << (led << 1))) {
|
|
||||||
LOG_ERR("LED reg update failed");
|
LOG_ERR("LED reg update failed");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -123,6 +125,7 @@ static int lp3943_set_dim_states(struct lp3943_data *data, uint32_t led, uint8_t
|
||||||
static int lp3943_led_blink(const struct device *dev, uint32_t led,
|
static int lp3943_led_blink(const struct device *dev, uint32_t led,
|
||||||
uint32_t delay_on, uint32_t delay_off)
|
uint32_t delay_on, uint32_t delay_off)
|
||||||
{
|
{
|
||||||
|
const struct lp3943_config *config = dev->config;
|
||||||
struct lp3943_data *data = dev->data;
|
struct lp3943_data *data = dev->data;
|
||||||
struct led_data *dev_data = &data->dev_data;
|
struct led_data *dev_data = &data->dev_data;
|
||||||
int ret;
|
int ret;
|
||||||
|
@ -149,13 +152,12 @@ static int lp3943_led_blink(const struct device *dev, uint32_t led,
|
||||||
}
|
}
|
||||||
|
|
||||||
val = (period * 255U) / dev_data->max_period;
|
val = (period * 255U) / dev_data->max_period;
|
||||||
if (i2c_reg_write_byte(data->i2c, DT_INST_REG_ADDR(0),
|
if (i2c_reg_write_byte_dt(&config->bus, reg, val)) {
|
||||||
reg, val)) {
|
|
||||||
LOG_ERR("LED write failed");
|
LOG_ERR("LED write failed");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = lp3943_set_dim_states(data, led, mode);
|
ret = lp3943_set_dim_states(config, led, mode);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -166,6 +168,7 @@ static int lp3943_led_blink(const struct device *dev, uint32_t led,
|
||||||
static int lp3943_led_set_brightness(const struct device *dev, uint32_t led,
|
static int lp3943_led_set_brightness(const struct device *dev, uint32_t led,
|
||||||
uint8_t value)
|
uint8_t value)
|
||||||
{
|
{
|
||||||
|
const struct lp3943_config *config = dev->config;
|
||||||
struct lp3943_data *data = dev->data;
|
struct lp3943_data *data = dev->data;
|
||||||
struct led_data *dev_data = &data->dev_data;
|
struct led_data *dev_data = &data->dev_data;
|
||||||
int ret;
|
int ret;
|
||||||
|
@ -190,13 +193,12 @@ static int lp3943_led_set_brightness(const struct device *dev, uint32_t led,
|
||||||
}
|
}
|
||||||
|
|
||||||
val = (value * 255U) / dev_data->max_brightness;
|
val = (value * 255U) / dev_data->max_brightness;
|
||||||
if (i2c_reg_write_byte(data->i2c, DT_INST_REG_ADDR(0),
|
if (i2c_reg_write_byte_dt(&config->bus, reg, val)) {
|
||||||
reg, val)) {
|
|
||||||
LOG_ERR("LED write failed");
|
LOG_ERR("LED write failed");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = lp3943_set_dim_states(data, led, mode);
|
ret = lp3943_set_dim_states(config, led, mode);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -206,7 +208,7 @@ static int lp3943_led_set_brightness(const struct device *dev, uint32_t led,
|
||||||
|
|
||||||
static inline int lp3943_led_on(const struct device *dev, uint32_t led)
|
static inline int lp3943_led_on(const struct device *dev, uint32_t led)
|
||||||
{
|
{
|
||||||
struct lp3943_data *data = dev->data;
|
const struct lp3943_config *config = dev->config;
|
||||||
int ret;
|
int ret;
|
||||||
uint8_t reg, mode;
|
uint8_t reg, mode;
|
||||||
|
|
||||||
|
@ -217,10 +219,8 @@ static inline int lp3943_led_on(const struct device *dev, uint32_t led)
|
||||||
|
|
||||||
/* Set LED state to ON */
|
/* Set LED state to ON */
|
||||||
mode = LP3943_ON;
|
mode = LP3943_ON;
|
||||||
if (i2c_reg_update_byte(data->i2c, DT_INST_REG_ADDR(0),
|
if (i2c_reg_update_byte_dt(&config->bus, reg, LP3943_MASK << (led << 1),
|
||||||
reg,
|
mode << (led << 1))) {
|
||||||
LP3943_MASK << (led << 1),
|
|
||||||
mode << (led << 1))) {
|
|
||||||
LOG_ERR("LED reg update failed");
|
LOG_ERR("LED reg update failed");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -230,7 +230,7 @@ static inline int lp3943_led_on(const struct device *dev, uint32_t led)
|
||||||
|
|
||||||
static inline int lp3943_led_off(const struct device *dev, uint32_t led)
|
static inline int lp3943_led_off(const struct device *dev, uint32_t led)
|
||||||
{
|
{
|
||||||
struct lp3943_data *data = dev->data;
|
const struct lp3943_config *config = dev->config;
|
||||||
int ret;
|
int ret;
|
||||||
uint8_t reg;
|
uint8_t reg;
|
||||||
|
|
||||||
|
@ -240,9 +240,8 @@ static inline int lp3943_led_off(const struct device *dev, uint32_t led)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set LED state to OFF */
|
/* Set LED state to OFF */
|
||||||
if (i2c_reg_update_byte(data->i2c, DT_INST_REG_ADDR(0),
|
if (i2c_reg_update_byte_dt(&config->bus, reg, LP3943_MASK << (led << 1),
|
||||||
reg,
|
0)) {
|
||||||
LP3943_MASK << (led << 1), 0)) {
|
|
||||||
LOG_ERR("LED reg update failed");
|
LOG_ERR("LED reg update failed");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -252,13 +251,13 @@ static inline int lp3943_led_off(const struct device *dev, uint32_t led)
|
||||||
|
|
||||||
static int lp3943_led_init(const struct device *dev)
|
static int lp3943_led_init(const struct device *dev)
|
||||||
{
|
{
|
||||||
|
const struct lp3943_config *config = dev->config;
|
||||||
struct lp3943_data *data = dev->data;
|
struct lp3943_data *data = dev->data;
|
||||||
struct led_data *dev_data = &data->dev_data;
|
struct led_data *dev_data = &data->dev_data;
|
||||||
|
|
||||||
data->i2c = device_get_binding(DT_INST_BUS_LABEL(0));
|
if (!device_is_ready(config->bus.bus)) {
|
||||||
if (data->i2c == NULL) {
|
LOG_ERR("I2C device not ready");
|
||||||
LOG_DBG("Failed to get I2C device");
|
return -ENODEV;
|
||||||
return -EINVAL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Hardware specific limits */
|
/* Hardware specific limits */
|
||||||
|
@ -272,6 +271,10 @@ static int lp3943_led_init(const struct device *dev)
|
||||||
|
|
||||||
static struct lp3943_data lp3943_led_data;
|
static struct lp3943_data lp3943_led_data;
|
||||||
|
|
||||||
|
static const struct lp3943_config lp3943_led_config = {
|
||||||
|
.bus = I2C_DT_SPEC_INST_GET(0),
|
||||||
|
};
|
||||||
|
|
||||||
static const struct led_driver_api lp3943_led_api = {
|
static const struct led_driver_api lp3943_led_api = {
|
||||||
.blink = lp3943_led_blink,
|
.blink = lp3943_led_blink,
|
||||||
.set_brightness = lp3943_led_set_brightness,
|
.set_brightness = lp3943_led_set_brightness,
|
||||||
|
@ -279,7 +282,6 @@ static const struct led_driver_api lp3943_led_api = {
|
||||||
.off = lp3943_led_off,
|
.off = lp3943_led_off,
|
||||||
};
|
};
|
||||||
|
|
||||||
DEVICE_DT_INST_DEFINE(0, &lp3943_led_init, NULL,
|
DEVICE_DT_INST_DEFINE(0, &lp3943_led_init, NULL, &lp3943_led_data,
|
||||||
&lp3943_led_data,
|
&lp3943_led_config, POST_KERNEL, CONFIG_LED_INIT_PRIORITY,
|
||||||
NULL, POST_KERNEL, CONFIG_LED_INIT_PRIORITY,
|
&lp3943_led_api);
|
||||||
&lp3943_led_api);
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue