drivers: led: tlc59108: Update driver to use i2c_dt_spec

Simplify driver by using i2c_dt_spec for bus access.

Signed-off-by: Benjamin Björnsson <benjamin.bjornsson@gmail.com>
This commit is contained in:
Benjamin Björnsson 2022-07-15 15:03:10 +02:00 committed by Carles Cufí
commit 21cf889c87

View file

@ -50,8 +50,7 @@ LOG_MODULE_REGISTER(tlc59108, CONFIG_LED_LOG_LEVEL);
struct tlc59108_cfg {
const struct device *i2c_dev;
uint16_t i2c_addr;
struct i2c_dt_spec i2c;
};
struct tlc59108_data {
@ -64,18 +63,15 @@ static int tlc59108_set_ledout(const struct device *dev, uint32_t led,
const struct tlc59108_cfg *config = dev->config;
if (led < 4) {
if (i2c_reg_update_byte(config->i2c_dev, config->i2c_addr,
TLC59108_LEDOUT0,
TLC59108_MASK << (led << 1),
val << (led << 1))) {
if (i2c_reg_update_byte_dt(&config->i2c, TLC59108_LEDOUT0,
TLC59108_MASK << (led << 1), val << (led << 1))) {
LOG_ERR("LED reg 0x%x update failed", TLC59108_LEDOUT0);
return -EIO;
}
} else {
if (i2c_reg_update_byte(config->i2c_dev, config->i2c_addr,
TLC59108_LEDOUT1,
TLC59108_MASK << ((led - 4) << 1),
val << ((led - 4) << 1))) {
if (i2c_reg_update_byte_dt(&config->i2c, TLC59108_LEDOUT1,
TLC59108_MASK << ((led - 4) << 1),
val << ((led - 4) << 1))) {
LOG_ERR("LED reg 0x%x update failed", TLC59108_LEDOUT1);
return -EIO;
}
@ -110,9 +106,7 @@ static int tlc59108_led_blink(const struct device *dev, uint32_t led,
* GDC = ((time_on * 256) / period)
*/
gdc = delay_on * 256U / period;
if (i2c_reg_write_byte(config->i2c_dev, config->i2c_addr,
TLC59108_GRPPWM,
gdc)) {
if (i2c_reg_write_byte_dt(&config->i2c, TLC59108_GRPPWM, gdc)) {
LOG_ERR("LED reg 0x%x write failed", TLC59108_GRPPWM);
return -EIO;
}
@ -124,18 +118,14 @@ static int tlc59108_led_blink(const struct device *dev, uint32_t led,
* GFRQ = ((period * 24 / 1000) - 1)
*/
gfrq = (period * 24U / 1000) - 1;
if (i2c_reg_write_byte(config->i2c_dev, config->i2c_addr,
TLC59108_GRPFREQ,
gfrq)) {
if (i2c_reg_write_byte_dt(&config->i2c, TLC59108_GRPFREQ, gfrq)) {
LOG_ERR("LED reg 0x%x write failed", TLC59108_GRPFREQ);
return -EIO;
}
/* Enable blinking mode */
if (i2c_reg_update_byte(config->i2c_dev, config->i2c_addr,
TLC59108_MODE2,
TLC59108_MODE2_DMBLNK,
TLC59108_MODE2_DMBLNK)) {
if (i2c_reg_update_byte_dt(&config->i2c, TLC59108_MODE2, TLC59108_MODE2_DMBLNK,
TLC59108_MODE2_DMBLNK)) {
LOG_ERR("LED reg 0x%x update failed", TLC59108_MODE2);
return -EIO;
}
@ -163,9 +153,7 @@ static int tlc59108_led_set_brightness(const struct device *dev, uint32_t led,
/* Set the LED brightness value */
val = (value * 255U) / dev_data->max_brightness;
if (i2c_reg_write_byte(config->i2c_dev, config->i2c_addr,
TLC59108_PWM_BASE + led,
val)) {
if (i2c_reg_write_byte_dt(&config->i2c, TLC59108_PWM_BASE + led, val)) {
LOG_ERR("LED 0x%x reg write failed", TLC59108_PWM_BASE + led);
return -EIO;
}
@ -200,15 +188,13 @@ static int tlc59108_led_init(const struct device *dev)
struct tlc59108_data *data = dev->data;
struct led_data *dev_data = &data->dev_data;
if (!device_is_ready(config->i2c_dev)) {
LOG_ERR("I2C bus device %s is not ready", config->i2c_dev->name);
if (!device_is_ready(config->i2c.bus)) {
LOG_ERR("I2C bus device %s is not ready", config->i2c.bus->name);
return -ENODEV;
}
/* Wake up from sleep mode */
if (i2c_reg_update_byte(config->i2c_dev, config->i2c_addr,
TLC59108_MODE1,
TLC59108_MODE1_OSC, 0)) {
if (i2c_reg_update_byte_dt(&config->i2c, TLC59108_MODE1, TLC59108_MODE1_OSC, 0)) {
LOG_ERR("LED reg 0x%x update failed", TLC59108_MODE1);
return -EIO;
}
@ -231,8 +217,7 @@ static const struct led_driver_api tlc59108_led_api = {
#define TLC59108_DEVICE(id) \
static const struct tlc59108_cfg tlc59108_##id##_cfg = { \
.i2c_dev = DEVICE_DT_GET(DT_INST_BUS(id)), \
.i2c_addr = DT_INST_REG_ADDR(id), \
.i2c = I2C_DT_SPEC_INST_GET(id), \
}; \
static struct tlc59108_data tlc59108_##id##_data; \
\