Skip to content

Commit 16ef607

Browse files
committed
drivers: led: is31fl319x - hard coded registers
more refactoring, don't use the 94 registers everywhere. Introduce structure, with registers and fixed values, and use it. Signed-off-by: Kurt Eckhardt <[email protected]>
1 parent 28bc566 commit 16ef607

File tree

1 file changed

+63
-25
lines changed

1 file changed

+63
-25
lines changed

drivers/led/is31fl319x.c

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@
2121
#include <zephyr/dt-bindings/led/led.h>
2222

2323
LOG_MODULE_REGISTER(is31fl319x, CONFIG_LED_LOG_LEVEL);
24+
#define REG_NOT_DEFINED 0xff
25+
26+
struct is31f1319x_model {
27+
const uint8_t prod_id_reg;
28+
const uint8_t shutdown_reg;
29+
const uint8_t conf_reg;
30+
const uint8_t current_reg;
31+
const uint8_t update_reg;
32+
33+
const uint8_t prod_id_val;
34+
const uint8_t shutdown_reg_val;
35+
const uint8_t conf_enable;
36+
const uint8_t update_val;
37+
38+
const uint8_t led_channels[];
39+
};
40+
2441

2542
#define IS31FL3194_PROD_ID_REG 0x00
2643
#define IS31FL3194_CONF_REG 0x01
@@ -36,17 +53,30 @@ LOG_MODULE_REGISTER(is31fl319x, CONFIG_LED_LOG_LEVEL);
3653

3754
#define IS31FL3194_CHANNEL_COUNT 3
3855

39-
static const uint8_t led_channels[] = {
40-
IS31FL3194_OUT1_REG,
41-
IS31FL3194_OUT2_REG,
42-
IS31FL3194_OUT3_REG
43-
};
56+
static const struct is31f1319x_model is31f13194_model = {
57+
/* register indexes */
58+
.prod_id_reg = IS31FL3194_PROD_ID_REG,
59+
.shutdown_reg = REG_NOT_DEFINED,
60+
.conf_reg = IS31FL3194_CONF_REG,
61+
.current_reg = IS31FL3194_CURRENT_REG,
62+
.update_reg = IS31FL3194_UPDATE_REG,
63+
64+
/* values for those registers */
65+
.prod_id_val = IS31FL3194_PROD_ID_VAL,
66+
.shutdown_reg_val = 0,
67+
.conf_enable = IS31FL3194_CONF_ENABLE,
68+
.update_val = IS31FL3194_UPDATE_VAL,
69+
70+
/* channel output registers */
71+
.led_channels = {IS31FL3194_OUT1_REG, IS31FL3194_OUT2_REG, IS31FL3194_OUT3_REG}};
4472

4573
struct is31fl319x_config {
4674
struct i2c_dt_spec bus;
75+
uint8_t channel_count;
4776
uint8_t num_leds;
4877
const struct led_info *led_infos;
4978
const uint8_t *current_limits;
79+
const struct is31f1319x_model *regs;
5080
};
5181

5282
static const struct led_info *is31fl319x_led_to_info(const struct is31fl319x_config *config,
@@ -79,21 +109,22 @@ static int is31fl319x_set_color(const struct device *dev, uint32_t led, uint8_t
79109
{
80110
const struct is31fl319x_config *config = dev->config;
81111
const struct led_info *info = is31fl319x_led_to_info(config, led);
112+
const struct is31f1319x_model *regs = config->regs;
82113
int ret;
83114

84115
if (info == NULL) {
85116
return -ENODEV;
86117
}
87118

88-
if (info->num_colors != 3) {
119+
if (info->num_colors > config->channel_count) {
89120
return -ENOTSUP;
90121
}
91122

92-
if (num_colors != 3) {
93-
return -EINVAL;
123+
if (num_colors > config->channel_count) {
124+
return -ENOTSUP;
94125
}
95126

96-
for (int i = 0; i < 3; i++) {
127+
for (int i = 0; i < num_colors; i++) {
97128
uint8_t value;
98129

99130
switch (info->color_mapping[i]) {
@@ -111,16 +142,16 @@ static int is31fl319x_set_color(const struct device *dev, uint32_t led, uint8_t
111142
return -EINVAL;
112143
}
113144

114-
ret = i2c_reg_write_byte_dt(&config->bus, led_channels[i], value);
145+
ret = i2c_reg_write_byte_dt(&config->bus, regs->led_channels[i], value);
115146
if (ret != 0) {
116147
break;
117148
}
118149
}
119150

120151
if (ret == 0) {
121152
ret = i2c_reg_write_byte_dt(&config->bus,
122-
IS31FL3194_UPDATE_REG,
123-
IS31FL3194_UPDATE_VAL);
153+
regs->update_reg,
154+
regs->update_val);
124155
}
125156

126157
if (ret != 0) {
@@ -134,6 +165,8 @@ static int is31fl319x_set_brightness(const struct device *dev, uint32_t led, uin
134165
{
135166
const struct is31fl319x_config *config = dev->config;
136167
const struct led_info *info = is31fl319x_led_to_info(config, led);
168+
const struct is31f1319x_model *regs = config->regs;
169+
137170
int ret = 0;
138171

139172
if (info == NULL) {
@@ -147,11 +180,11 @@ static int is31fl319x_set_brightness(const struct device *dev, uint32_t led, uin
147180
/* Rescale 0..100 to 0..255 */
148181
value = value * 255 / LED_BRIGHTNESS_MAX;
149182

150-
ret = i2c_reg_write_byte_dt(&config->bus, led_channels[led], value);
183+
ret = i2c_reg_write_byte_dt(&config->bus, regs->led_channels[led], value);
151184
if (ret == 0) {
152185
ret = i2c_reg_write_byte_dt(&config->bus,
153-
IS31FL3194_UPDATE_REG,
154-
IS31FL3194_UPDATE_VAL);
186+
regs->update_reg,
187+
regs->update_val);
155188
}
156189

157190
if (ret != 0) {
@@ -246,6 +279,7 @@ static int is31fl319x_init(const struct device *dev)
246279
{
247280
const struct is31fl319x_config *config = dev->config;
248281
const struct led_info *info = NULL;
282+
const struct is31f1319x_model *regs = config->regs;
249283
int i, ret;
250284
uint8_t prod_id, band;
251285
uint8_t current_reg = 0;
@@ -260,15 +294,15 @@ static int is31fl319x_init(const struct device *dev)
260294
return -ENODEV;
261295
}
262296

263-
ret = i2c_reg_read_byte_dt(&config->bus, IS31FL3194_PROD_ID_REG, &prod_id);
297+
ret = i2c_reg_read_byte_dt(&config->bus, regs->prod_id_reg, &prod_id);
264298
if (ret != 0) {
265299
LOG_ERR("%s: failed to read product ID", dev->name);
266300
return ret;
267301
}
268302

269-
if (prod_id != IS31FL3194_PROD_ID_VAL) {
303+
if (prod_id != regs->prod_id_val) {
270304
LOG_ERR("%s: invalid product ID 0x%02x (expected 0x%02x)", dev->name, prod_id,
271-
IS31FL3194_PROD_ID_VAL);
305+
regs->prod_id_val);
272306
return -ENODEV;
273307
}
274308

@@ -295,7 +329,8 @@ static int is31fl319x_init(const struct device *dev)
295329
}
296330

297331
/* enable device */
298-
return i2c_reg_write_byte_dt(&config->bus, IS31FL3194_CONF_REG, IS31FL3194_CONF_ENABLE);
332+
return i2c_reg_write_byte_dt(&config->bus, regs->conf_reg,
333+
regs->conf_enable);
299334
}
300335

301336
static DEVICE_API(led, is31fl319x_led_api) = {
@@ -318,28 +353,31 @@ static DEVICE_API(led, is31fl319x_led_api) = {
318353
#define LED_CURRENT(led_node_id) \
319354
DT_PROP(led_node_id, current_limit),
320355

321-
#define IS31FL319X_DEVICE(n, id) \
356+
#define IS31FL319X_DEVICE(n, id, nchannels, pregs) \
322357
\
323358
DT_INST_FOREACH_CHILD(n, COLOR_MAPPING) \
324359
\
325360
static const struct led_info is31fl319##id##_leds_##n[] = \
326-
{ DT_INST_FOREACH_CHILD(n, LED_INFO) }; \
361+
{ DT_INST_FOREACH_CHILD(n, LED_INFO) }; \
327362
\
328363
static const uint8_t is31fl319##id##_currents_##n[] = \
329364
{ DT_INST_FOREACH_CHILD(n, LED_CURRENT) }; \
330365
BUILD_ASSERT(ARRAY_SIZE(is31fl319##id##_leds_##n) > 0, \
331366
"No LEDs defined for " #n); \
332367
\
333368
static const struct is31fl319x_config is31fl319##id##_config_##n = { \
334-
.bus = I2C_DT_SPEC_INST_GET(n), \
369+
.bus = I2C_DT_SPEC_INST_GET(n), \
370+
.channel_count = nchannels, \
335371
.num_leds = ARRAY_SIZE(is31fl319##id##_leds_##n), \
336372
.led_infos = is31fl319##id##_leds_##n, \
337373
.current_limits = is31fl319##id##_currents_##n, \
374+
.regs = pregs, \
338375
}; \
339-
DEVICE_DT_INST_DEFINE(n, &is31fl319x_init, NULL, NULL, \
340-
&is31fl319##id##_config_##n, POST_KERNEL, \
376+
DEVICE_DT_INST_DEFINE(n, &is31fl319x_init, NULL, NULL, \
377+
&is31fl319##id##_config_##n, POST_KERNEL, \
341378
CONFIG_LED_INIT_PRIORITY, &is31fl319x_led_api);
342379

343380
#undef DT_DRV_COMPAT
344381
#define DT_DRV_COMPAT issi_is31fl3194
345-
DT_INST_FOREACH_STATUS_OKAY_VARGS(IS31FL319X_DEVICE, 4)
382+
DT_INST_FOREACH_STATUS_OKAY_VARGS(IS31FL319X_DEVICE, 4, IS31FL3194_CHANNEL_COUNT,
383+
&is31f13194_model)

0 commit comments

Comments
 (0)