Skip to content

Commit a9b202b

Browse files
Ansuellag-linaro
authored andcommitted
leds: leds-lp55xx: Generalize stop_all_engine OP
In all the lp55xx based driver, we have a similar implementation of the stop_all_engine function with the only difference of the required sleep for the OP MODE change. The main difference is legacy LEDs require a min of 152 us while new one use a generic 1-2ms. The new one use a 1-2ms sleep as suggested in the datasheet IN ALTERNATIVE to a much more robust approach by using the newly introduced ENGINE_BUSY bit in the STATUS reg. To better handle sleep after OP MODE change, add support for polling the ENGINE_BUSY bit and use the legacy sleep for old LEDs. With this change, stop_all_engine can be generalized and moved to lp55xx-common. To make more clear the double usage of lp55xx_reg, define a union for additional scope of mask and shift. Update all lp55xx based driver to use the new generalized function and define the required bits in the device_config struct. Suggested-by: Lee Jones <lee@kernel.org> Signed-off-by: Christian Marangi <ansuelsmth@gmail.com> Link: https://lore.kernel.org/r/20240626160027.19703-4-ansuelsmth@gmail.com Signed-off-by: Lee Jones <lee@kernel.org>
1 parent a6ca484 commit a9b202b

6 files changed

Lines changed: 84 additions & 34 deletions

File tree

drivers/leds/leds-lp5521.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,6 @@ static void lp5521_load_engine(struct lp55xx_chip *chip)
135135
lp5521_wait_opmode_done();
136136
}
137137

138-
static void lp5521_stop_all_engines(struct lp55xx_chip *chip)
139-
{
140-
lp55xx_write(chip, LP5521_REG_OP_MODE, 0);
141-
lp5521_wait_opmode_done();
142-
}
143-
144138
static void lp5521_stop_engine(struct lp55xx_chip *chip)
145139
{
146140
enum lp55xx_engine_index idx = chip->engine_idx;
@@ -499,6 +493,9 @@ static const struct attribute_group lp5521_group = {
499493

500494
/* Chip specific configurations */
501495
static struct lp55xx_device_config lp5521_cfg = {
496+
.reg_op_mode = {
497+
.addr = LP5521_REG_OP_MODE,
498+
},
502499
.reset = {
503500
.addr = LP5521_REG_RESET,
504501
.val = LP5521_RESET,
@@ -585,7 +582,7 @@ static void lp5521_remove(struct i2c_client *client)
585582
struct lp55xx_led *led = i2c_get_clientdata(client);
586583
struct lp55xx_chip *chip = led->chip;
587584

588-
lp5521_stop_all_engines(chip);
585+
lp55xx_stop_all_engine(chip);
589586
lp55xx_unregister_sysfs(chip);
590587
lp55xx_deinit_device(chip);
591588
}

drivers/leds/leds-lp5523.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@
4141
#define LP5523_REG_LED_PWM_BASE 0x16
4242
#define LP5523_REG_LED_CURRENT_BASE 0x26
4343
#define LP5523_REG_CONFIG 0x36
44+
4445
#define LP5523_REG_STATUS 0x3A
46+
#define LP5523_ENGINE_BUSY BIT(4)
47+
4548
#define LP5523_REG_RESET 0x3D
4649
#define LP5523_REG_LED_TEST_CTRL 0x41
4750
#define LP5523_REG_LED_TEST_ADC 0x42
@@ -190,12 +193,6 @@ static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
190193
lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
191194
}
192195

193-
static void lp5523_stop_all_engines(struct lp55xx_chip *chip)
194-
{
195-
lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
196-
lp5523_wait_opmode_done();
197-
}
198-
199196
static void lp5523_stop_engine(struct lp55xx_chip *chip)
200197
{
201198
enum lp55xx_engine_index idx = chip->engine_idx;
@@ -322,7 +319,7 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip)
322319
}
323320

324321
out:
325-
lp5523_stop_all_engines(chip);
322+
lp55xx_stop_all_engine(chip);
326323
return ret;
327324
}
328325

@@ -873,6 +870,13 @@ static const struct attribute_group lp5523_group = {
873870

874871
/* Chip specific configurations */
875872
static struct lp55xx_device_config lp5523_cfg = {
873+
.reg_op_mode = {
874+
.addr = LP5523_REG_OP_MODE,
875+
},
876+
.engine_busy = {
877+
.addr = LP5523_REG_STATUS,
878+
.mask = LP5523_ENGINE_BUSY,
879+
},
876880
.reset = {
877881
.addr = LP5523_REG_RESET,
878882
.val = LP5523_RESET,
@@ -959,7 +963,7 @@ static void lp5523_remove(struct i2c_client *client)
959963
struct lp55xx_led *led = i2c_get_clientdata(client);
960964
struct lp55xx_chip *chip = led->chip;
961965

962-
lp5523_stop_all_engines(chip);
966+
lp55xx_stop_all_engine(chip);
963967
lp55xx_unregister_sysfs(chip);
964968
lp55xx_deinit_device(chip);
965969
}

drivers/leds/leds-lp5562.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,6 @@ static void lp5562_load_engine(struct lp55xx_chip *chip)
144144
lp5562_wait_opmode_done();
145145
}
146146

147-
static void lp5562_stop_engine(struct lp55xx_chip *chip)
148-
{
149-
lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DISABLE);
150-
lp5562_wait_opmode_done();
151-
}
152-
153147
static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
154148
{
155149
int ret;
@@ -160,7 +154,7 @@ static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
160154
if (!start) {
161155
lp55xx_write(chip, LP5562_REG_ENABLE, LP5562_ENABLE_DEFAULT);
162156
lp5562_wait_enable_done();
163-
lp5562_stop_engine(chip);
157+
lp55xx_stop_all_engine(chip);
164158
lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
165159
lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
166160
lp5562_wait_opmode_done();
@@ -369,7 +363,7 @@ static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
369363
return -EINVAL;
370364
}
371365

372-
lp5562_stop_engine(chip);
366+
lp55xx_stop_all_engine(chip);
373367

374368
/* Set LED map as RGB */
375369
lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_RGB);
@@ -495,6 +489,9 @@ static const struct attribute_group lp5562_group = {
495489
/* Chip specific configurations */
496490
static struct lp55xx_device_config lp5562_cfg = {
497491
.max_channel = LP5562_MAX_LEDS,
492+
.reg_op_mode = {
493+
.addr = LP5562_REG_OP_MODE,
494+
},
498495
.reset = {
499496
.addr = LP5562_REG_RESET,
500497
.val = LP5562_RESET,
@@ -577,7 +574,7 @@ static void lp5562_remove(struct i2c_client *client)
577574
struct lp55xx_led *led = i2c_get_clientdata(client);
578575
struct lp55xx_chip *chip = led->chip;
579576

580-
lp5562_stop_engine(chip);
577+
lp55xx_stop_all_engine(chip);
581578

582579
lp55xx_unregister_sysfs(chip);
583580
lp55xx_deinit_device(chip);

drivers/leds/leds-lp55xx-common.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/delay.h>
1414
#include <linux/firmware.h>
1515
#include <linux/i2c.h>
16+
#include <linux/iopoll.h>
1617
#include <linux/leds.h>
1718
#include <linux/module.h>
1819
#include <linux/platform_data/leds-lp55xx.h>
@@ -22,6 +23,12 @@
2223

2324
#include "leds-lp55xx-common.h"
2425

26+
/* OP MODE require at least 153 us to clear regs */
27+
#define LP55XX_CMD_SLEEP 200
28+
29+
/* Program Commands */
30+
#define LP55xx_MODE_DISABLE_ALL_ENG 0x0
31+
2532
/* External clock rate */
2633
#define LP55XX_CLK_32K 32768
2734

@@ -40,6 +47,35 @@ static struct lp55xx_led *mcled_cdev_to_led(struct led_classdev_mc *mc_cdev)
4047
return container_of(mc_cdev, struct lp55xx_led, mc_cdev);
4148
}
4249

50+
static void lp55xx_wait_opmode_done(struct lp55xx_chip *chip)
51+
{
52+
struct lp55xx_device_config *cfg = chip->cfg;
53+
int __always_unused ret;
54+
u8 val;
55+
56+
/*
57+
* Recent chip supports BUSY bit for engine.
58+
* Check support by checking if val is not 0.
59+
* For legacy device, sleep at least 153 us.
60+
*/
61+
if (cfg->engine_busy.val) {
62+
read_poll_timeout(lp55xx_read, ret, !(val & cfg->engine_busy.mask),
63+
LP55XX_CMD_SLEEP, LP55XX_CMD_SLEEP * 10, false,
64+
chip, cfg->engine_busy.addr, &val);
65+
} else {
66+
usleep_range(LP55XX_CMD_SLEEP, LP55XX_CMD_SLEEP * 2);
67+
}
68+
}
69+
70+
void lp55xx_stop_all_engine(struct lp55xx_chip *chip)
71+
{
72+
struct lp55xx_device_config *cfg = chip->cfg;
73+
74+
lp55xx_write(chip, cfg->reg_op_mode.addr, LP55xx_MODE_DISABLE_ALL_ENG);
75+
lp55xx_wait_opmode_done(chip);
76+
}
77+
EXPORT_SYMBOL_GPL(lp55xx_stop_all_engine);
78+
4379
static void lp55xx_reset_device(struct lp55xx_chip *chip)
4480
{
4581
struct lp55xx_device_config *cfg = chip->cfg;

drivers/leds/leds-lp55xx-common.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,22 @@ struct lp55xx_chip;
8181
/*
8282
* struct lp55xx_reg
8383
* @addr : Register address
84-
* @val : Register value
84+
* @val : Register value (can also used as mask or shift)
8585
*/
8686
struct lp55xx_reg {
8787
u8 addr;
88-
u8 val;
88+
union {
89+
u8 val;
90+
u8 mask;
91+
u8 shift;
92+
};
8993
};
9094

9195
/*
9296
* struct lp55xx_device_config
97+
* @reg_op_mode : Chip specific OP MODE reg addr
98+
* @engine_busy : Chip specific engine busy
99+
* (if not supported 153 us sleep)
93100
* @reset : Chip specific reset command
94101
* @enable : Chip specific enable command
95102
* @max_channel : Maximum number of channels
@@ -102,6 +109,8 @@ struct lp55xx_reg {
102109
* @dev_attr_group : Device specific attributes
103110
*/
104111
struct lp55xx_device_config {
112+
const struct lp55xx_reg reg_op_mode; /* addr, shift */
113+
const struct lp55xx_reg engine_busy; /* addr, mask */
105114
const struct lp55xx_reg reset;
106115
const struct lp55xx_reg enable;
107116
const int max_channel;
@@ -191,6 +200,9 @@ extern int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg,
191200
/* external clock detection */
192201
extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip);
193202

203+
/* common chip functions */
204+
extern void lp55xx_stop_all_engine(struct lp55xx_chip *chip);
205+
194206
/* common device init/deinit functions */
195207
extern int lp55xx_init_device(struct lp55xx_chip *chip);
196208
extern void lp55xx_deinit_device(struct lp55xx_chip *chip);

drivers/leds/leds-lp8501.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
#define LP8501_INT_CLK BIT(0)
5959
#define LP8501_DEFAULT_CFG (LP8501_PWM_PSAVE | LP8501_AUTO_INC | LP8501_PWR_SAVE)
6060

61+
#define LP8501_REG_STATUS 0x3A
62+
#define LP8501_ENGINE_BUSY BIT(4)
63+
6164
#define LP8501_REG_RESET 0x3D
6265
#define LP8501_RESET 0xFF
6366

@@ -141,12 +144,6 @@ static void lp8501_load_engine(struct lp55xx_chip *chip)
141144
lp55xx_write(chip, LP8501_REG_PROG_PAGE_SEL, page_sel[idx]);
142145
}
143146

144-
static void lp8501_stop_engine(struct lp55xx_chip *chip)
145-
{
146-
lp55xx_write(chip, LP8501_REG_OP_MODE, 0);
147-
lp8501_wait_opmode_done();
148-
}
149-
150147
static void lp8501_turn_off_channels(struct lp55xx_chip *chip)
151148
{
152149
int i;
@@ -163,7 +160,7 @@ static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
163160

164161
/* stop engine */
165162
if (!start) {
166-
lp8501_stop_engine(chip);
163+
lp55xx_stop_all_engine(chip);
167164
lp8501_turn_off_channels(chip);
168165
return;
169166
}
@@ -285,6 +282,13 @@ static int lp8501_led_brightness(struct lp55xx_led *led)
285282

286283
/* Chip specific configurations */
287284
static struct lp55xx_device_config lp8501_cfg = {
285+
.reg_op_mode = {
286+
.addr = LP8501_REG_OP_MODE,
287+
},
288+
.engine_busy = {
289+
.addr = LP8501_REG_STATUS,
290+
.maks = LP8501_ENGINE_BUSY,
291+
},
288292
.reset = {
289293
.addr = LP8501_REG_RESET,
290294
.val = LP8501_RESET,
@@ -369,7 +373,7 @@ static void lp8501_remove(struct i2c_client *client)
369373
struct lp55xx_led *led = i2c_get_clientdata(client);
370374
struct lp55xx_chip *chip = led->chip;
371375

372-
lp8501_stop_engine(chip);
376+
lp55xx_stop_all_engine(chip);
373377
lp55xx_unregister_sysfs(chip);
374378
lp55xx_deinit_device(chip);
375379
}

0 commit comments

Comments
 (0)