Skip to content

Commit 4d310b9

Browse files
Ansuellag-linaro
authored andcommitted
leds: leds-lp55xx: Generalize load_engine function
LED driver based on lp55xx have all a very similar implementation for load_engine function. Move the function to lp55xx-common and rework the define to be more dynamic instead of having to declare a temp array for them. Engine mask are the same for every LED based on lp55xx. Suggested-by: Lee Jones <lee@kernel.org> Signed-off-by: Christian Marangi <ansuelsmth@gmail.com> Link: https://lore.kernel.org/r/20240626160027.19703-6-ansuelsmth@gmail.com Signed-off-by: Lee Jones <lee@kernel.org>
1 parent db30c28 commit 4d310b9

6 files changed

Lines changed: 43 additions & 84 deletions

File tree

drivers/leds/leds-lp5521.c

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,6 @@ static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
115115
led_current);
116116
}
117117

118-
static void lp5521_load_engine(struct lp55xx_chip *chip)
119-
{
120-
enum lp55xx_engine_index idx = chip->engine_idx;
121-
static const u8 mask[] = {
122-
[LP55XX_ENGINE_1] = LP5521_MODE_R_M,
123-
[LP55XX_ENGINE_2] = LP5521_MODE_G_M,
124-
[LP55XX_ENGINE_3] = LP5521_MODE_B_M,
125-
};
126-
127-
static const u8 val[] = {
128-
[LP55XX_ENGINE_1] = LP5521_LOAD_R,
129-
[LP55XX_ENGINE_2] = LP5521_LOAD_G,
130-
[LP55XX_ENGINE_3] = LP5521_LOAD_B,
131-
};
132-
133-
lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]);
134-
135-
lp5521_wait_opmode_done();
136-
}
137-
138118
static void lp5521_stop_engine(struct lp55xx_chip *chip)
139119
{
140120
enum lp55xx_engine_index idx = chip->engine_idx;
@@ -264,7 +244,7 @@ static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
264244
* 2) write firmware data into program memory
265245
*/
266246

267-
lp5521_load_engine(chip);
247+
lp55xx_load_engine(chip);
268248
lp5521_update_program_memory(chip, fw->data, fw->size);
269249
}
270250

@@ -415,7 +395,7 @@ static ssize_t store_engine_mode(struct device *dev,
415395
engine->mode = LP55XX_ENGINE_RUN;
416396
} else if (!strncmp(buf, "load", 4)) {
417397
lp5521_stop_engine(chip);
418-
lp5521_load_engine(chip);
398+
lp55xx_load_engine(chip);
419399
engine->mode = LP55XX_ENGINE_LOAD;
420400
} else if (!strncmp(buf, "disabled", 8)) {
421401
lp5521_stop_engine(chip);
@@ -441,7 +421,7 @@ static ssize_t store_engine_load(struct device *dev,
441421
mutex_lock(&chip->lock);
442422

443423
chip->engine_idx = nr;
444-
lp5521_load_engine(chip);
424+
lp55xx_load_engine(chip);
445425
ret = lp5521_update_program_memory(chip, buf, len);
446426

447427
mutex_unlock(&chip->lock);

drivers/leds/leds-lp5523.c

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -159,26 +159,6 @@ static int lp5523_post_init_device(struct lp55xx_chip *chip)
159159
return lp5523_init_program_engine(chip);
160160
}
161161

162-
static void lp5523_load_engine(struct lp55xx_chip *chip)
163-
{
164-
enum lp55xx_engine_index idx = chip->engine_idx;
165-
static const u8 mask[] = {
166-
[LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
167-
[LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
168-
[LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
169-
};
170-
171-
static const u8 val[] = {
172-
[LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
173-
[LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
174-
[LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
175-
};
176-
177-
lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
178-
179-
lp5523_wait_opmode_done();
180-
}
181-
182162
static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
183163
{
184164
enum lp55xx_engine_index idx = chip->engine_idx;
@@ -188,7 +168,7 @@ static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
188168
[LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
189169
};
190170

191-
lp5523_load_engine(chip);
171+
lp55xx_load_engine(chip);
192172

193173
lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
194174
}
@@ -425,7 +405,7 @@ static ssize_t store_engine_mode(struct device *dev,
425405
engine->mode = LP55XX_ENGINE_RUN;
426406
} else if (!strncmp(buf, "load", 4)) {
427407
lp5523_stop_engine(chip);
428-
lp5523_load_engine(chip);
408+
lp55xx_load_engine(chip);
429409
engine->mode = LP55XX_ENGINE_LOAD;
430410
} else if (!strncmp(buf, "disabled", 8)) {
431411
lp5523_stop_engine(chip);
@@ -502,7 +482,7 @@ static int lp5523_load_mux(struct lp55xx_chip *chip, u16 mux, int nr)
502482
[LP55XX_ENGINE_3] = LP5523_PAGE_MUX3,
503483
};
504484

505-
lp5523_load_engine(chip);
485+
lp55xx_load_engine(chip);
506486

507487
ret = lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, mux_page[nr]);
508488
if (ret)

drivers/leds/leds-lp5562.c

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -124,26 +124,6 @@ static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current)
124124
lp55xx_write(led->chip, addr[led->chan_nr], led_current);
125125
}
126126

127-
static void lp5562_load_engine(struct lp55xx_chip *chip)
128-
{
129-
enum lp55xx_engine_index idx = chip->engine_idx;
130-
static const u8 mask[] = {
131-
[LP55XX_ENGINE_1] = LP5562_MODE_ENG1_M,
132-
[LP55XX_ENGINE_2] = LP5562_MODE_ENG2_M,
133-
[LP55XX_ENGINE_3] = LP5562_MODE_ENG3_M,
134-
};
135-
136-
static const u8 val[] = {
137-
[LP55XX_ENGINE_1] = LP5562_LOAD_ENG1,
138-
[LP55XX_ENGINE_2] = LP5562_LOAD_ENG2,
139-
[LP55XX_ENGINE_3] = LP5562_LOAD_ENG3,
140-
};
141-
142-
lp55xx_update_bits(chip, LP5562_REG_OP_MODE, mask[idx], val[idx]);
143-
144-
lp5562_wait_opmode_done();
145-
}
146-
147127
static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
148128
{
149129
int ret;
@@ -270,7 +250,7 @@ static void lp5562_firmware_loaded(struct lp55xx_chip *chip)
270250
* 2) write firmware data into program memory
271251
*/
272252

273-
lp5562_load_engine(chip);
253+
lp55xx_load_engine(chip);
274254
lp5562_update_firmware(chip, fw->data, fw->size);
275255
}
276256

@@ -371,7 +351,7 @@ static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
371351
/* Load engines */
372352
for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
373353
chip->engine_idx = i;
374-
lp5562_load_engine(chip);
354+
lp55xx_load_engine(chip);
375355
}
376356

377357
/* Clear program registers */

drivers/leds/leds-lp55xx-common.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Derived from leds-lp5521.c, leds-lp5523.c
1010
*/
1111

12+
#include <linux/bitfield.h>
1213
#include <linux/clk.h>
1314
#include <linux/delay.h>
1415
#include <linux/firmware.h>
@@ -26,8 +27,24 @@
2627
/* OP MODE require at least 153 us to clear regs */
2728
#define LP55XX_CMD_SLEEP 200
2829

29-
/* Program Commands */
30+
/*
31+
* Program Memory Operations
32+
* Same Mask for each engine for both mode and exec
33+
* ENG1 GENMASK(3, 2)
34+
* ENG2 GENMASK(5, 4)
35+
* ENG3 GENMASK(7, 6)
36+
*/
3037
#define LP55xx_MODE_DISABLE_ALL_ENG 0x0
38+
#define LP55xx_MODE_ENG_MASK GENMASK(1, 0)
39+
#define LP55xx_MODE_DISABLE_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x0)
40+
#define LP55xx_MODE_LOAD_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x1)
41+
#define LP55xx_MODE_RUN_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x2)
42+
#define LP55xx_MODE_HALT_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x3)
43+
44+
#define LP55xx_MODE_ENGn_SHIFT(n, shift) ((shift) + (2 * (3 - (n))))
45+
#define LP55xx_MODE_ENGn_MASK(n, shift) (LP55xx_MODE_ENG_MASK << LP55xx_MODE_ENGn_SHIFT(n, shift))
46+
#define LP55xx_MODE_ENGn_GET(n, mode, shift) \
47+
(((mode) >> LP55xx_MODE_ENGn_SHIFT(n, shift)) & LP55xx_MODE_ENG_MASK)
3148

3249
/* External clock rate */
3350
#define LP55XX_CLK_32K 32768
@@ -76,6 +93,20 @@ void lp55xx_stop_all_engine(struct lp55xx_chip *chip)
7693
}
7794
EXPORT_SYMBOL_GPL(lp55xx_stop_all_engine);
7895

96+
void lp55xx_load_engine(struct lp55xx_chip *chip)
97+
{
98+
enum lp55xx_engine_index idx = chip->engine_idx;
99+
const struct lp55xx_device_config *cfg = chip->cfg;
100+
u8 mask, val;
101+
102+
mask = LP55xx_MODE_ENGn_MASK(idx, cfg->reg_op_mode.shift);
103+
val = LP55xx_MODE_LOAD_ENG << LP55xx_MODE_ENGn_SHIFT(idx, cfg->reg_op_mode.shift);
104+
105+
lp55xx_update_bits(chip, cfg->reg_op_mode.addr, mask, val);
106+
lp55xx_wait_opmode_done(chip);
107+
}
108+
EXPORT_SYMBOL_GPL(lp55xx_load_engine);
109+
79110
static void lp55xx_reset_device(struct lp55xx_chip *chip)
80111
{
81112
const struct lp55xx_device_config *cfg = chip->cfg;

drivers/leds/leds-lp55xx-common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip);
202202

203203
/* common chip functions */
204204
extern void lp55xx_stop_all_engine(struct lp55xx_chip *chip);
205+
extern void lp55xx_load_engine(struct lp55xx_chip *chip);
205206

206207
/* common probe/remove function */
207208
extern int lp55xx_probe(struct i2c_client *client);

drivers/leds/leds-lp8501.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,27 +119,14 @@ static int lp8501_post_init_device(struct lp55xx_chip *chip)
119119
static void lp8501_load_engine(struct lp55xx_chip *chip)
120120
{
121121
enum lp55xx_engine_index idx = chip->engine_idx;
122-
static const u8 mask[] = {
123-
[LP55XX_ENGINE_1] = LP8501_MODE_ENG1_M,
124-
[LP55XX_ENGINE_2] = LP8501_MODE_ENG2_M,
125-
[LP55XX_ENGINE_3] = LP8501_MODE_ENG3_M,
126-
};
127-
128-
static const u8 val[] = {
129-
[LP55XX_ENGINE_1] = LP8501_LOAD_ENG1,
130-
[LP55XX_ENGINE_2] = LP8501_LOAD_ENG2,
131-
[LP55XX_ENGINE_3] = LP8501_LOAD_ENG3,
132-
};
133122

134123
static const u8 page_sel[] = {
135124
[LP55XX_ENGINE_1] = LP8501_PAGE_ENG1,
136125
[LP55XX_ENGINE_2] = LP8501_PAGE_ENG2,
137126
[LP55XX_ENGINE_3] = LP8501_PAGE_ENG3,
138127
};
139128

140-
lp55xx_update_bits(chip, LP8501_REG_OP_MODE, mask[idx], val[idx]);
141-
142-
lp8501_wait_opmode_done();
129+
lp55xx_load_engine(chip);
143130

144131
lp55xx_write(chip, LP8501_REG_PROG_PAGE_SEL, page_sel[idx]);
145132
}
@@ -287,7 +274,7 @@ static struct lp55xx_device_config lp8501_cfg = {
287274
},
288275
.engine_busy = {
289276
.addr = LP8501_REG_STATUS,
290-
.maks = LP8501_ENGINE_BUSY,
277+
.mask = LP8501_ENGINE_BUSY,
291278
},
292279
.reset = {
293280
.addr = LP8501_REG_RESET,

0 commit comments

Comments
 (0)