2024-06-15 23:13:07

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 00/20] leds: leds-lp55xx: overhaul driver

This long series is (as requested) a big overhaul of the lp55xx based
LED driver.

As notice for these kind of LED chip there was the bad habit of copy
the old driver and just modify it enough to make it work with the new
model. Till v4 I was also doing the same by following the pattern and
the code format of previous driver.

Since Lee didn't like this, here is the BIG series that generalize
pretty much anything in the 4 model currently supported.

Indeed, although the LED chip have fundamental difference (page
support), things can be generalized and produce slimmer drivers by
putting everything in the lp55xx-common shared module.

This result in the new model lp5569 being very small with only the
selftest portion to be custom.

Lee also wasn't clear by the meaning of ENGINE in these LED driver,
so here some simple explaination. This is very common on these TI LED
chip. The ENGINE (there are always 3) is just some kind of processor
that execute a program (precompiled code ASM like) loaded in the SRAM.
Sysfs is used to load the pattern, and to start and stop the engine.

These pattern can do all kind of complex thing with LEDs. Old LED chip
had 32bytes of space for the pattern but newer one (like lp5569) have
pages and if correctly configured can have massive pattern.
These pattern can do all kind of magic like loops that make the LED
pulse, change color and all kind of stuff.

(For Lee, sorry if you will have to repeat some review that I might
have missed in the lp5569 driver)

Changes v5:
- Big generalization patch
- Rework lp5569 driver with new generalized functions
- Drop all copyright header in lp5569 as the driver got reworked
entirely and it's not based on previous one anymore.
Changes v4:
- Fix reported buffer overflow due to a copypaste error
- Add comments to describe fw size logic
Changes v3:
- Add ACK tag to DT patch
- Enlarge and support program size up to 128bytes
Changes v2:
- Add ACK tag to DT patch
- Fix compilation error with target that doesn't
include bitfield.h

Christian Marangi (20):
dt-bindings: leds-lp55xx: limit pwr-sel property to ti,lp8501
dt-bindings: leds-lp55xx: Add new ti,lp5569 compatible
leds: leds-lp55xx: generalize stop_all_engine OP
leds: leds-lp55xx: generalize probe/remove functions
leds: leds-lp55xx: generalize load_engine function
leds: leds-lp55xx: generalize load_engine_and_select_page function
leds: leds-lp55xx: generalize run_engine function
leds: leds-lp55xx: generalize update_program_memory function
leds: leds-lp55xx: generalize firmware_loaded function
leds: leds-lp55xx: generalize led_brightness function
leds: leds-lp55xx: generalize multicolor_brightness function
leds: leds-lp55xx: generalize set_led_current function
leds: leds-lp55xx: generalize turn_off_channels function
leds: leds-lp55xx: generalize stop_engine function
leds: leds-lp55xx: generalize sysfs engine_load and engine_mode
leds: leds-lp55xx: generalize sysfs engine_leds
leds: leds-lp55xx: generalize sysfs master_fader
leds: leds-lp55xx: support ENGINE program up to 128 bytes
leds: leds-lp55xx: drop deprecated defines
leds: leds-lp5569: Add support for Texas Instruments LP5569

.../devicetree/bindings/leds/leds-lp55xx.yaml | 11 +
drivers/leds/Kconfig | 16 +-
drivers/leds/Makefile | 1 +
drivers/leds/leds-lp5521.c | 405 +---------
drivers/leds/leds-lp5523.c | 728 ++----------------
drivers/leds/leds-lp5562.c | 261 +------
drivers/leds/leds-lp5569.c | 542 +++++++++++++
drivers/leds/leds-lp55xx-common.c | 728 +++++++++++++++++-
drivers/leds/leds-lp55xx-common.h | 133 +++-
drivers/leds/leds-lp8501.c | 313 +-------
10 files changed, 1521 insertions(+), 1617 deletions(-)
create mode 100644 drivers/leds/leds-lp5569.c

--
2.43.0



2024-06-15 23:13:12

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 01/20] dt-bindings: leds-lp55xx: limit pwr-sel property to ti,lp8501

pwr-sel property is specific to ti,lp8501, make it conditional of the
related compatible.

Signed-off-by: Christian Marangi <[email protected]>
Acked-by: Rob Herring (Arm) <[email protected]>
---
.../devicetree/bindings/leds/leds-lp55xx.yaml | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/Documentation/devicetree/bindings/leds/leds-lp55xx.yaml b/Documentation/devicetree/bindings/leds/leds-lp55xx.yaml
index e9d4514d0166..77828dedbb9f 100644
--- a/Documentation/devicetree/bindings/leds/leds-lp55xx.yaml
+++ b/Documentation/devicetree/bindings/leds/leds-lp55xx.yaml
@@ -151,6 +151,16 @@ patternProperties:
$ref: /schemas/types.yaml#/definitions/string
description: name of channel

+if:
+ not:
+ properties:
+ compatible:
+ contains:
+ const: ti,lp8501
+then:
+ properties:
+ pwr-sel: false
+
required:
- compatible
- reg
--
2.43.0


2024-06-15 23:13:23

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 02/20] dt-bindings: leds-lp55xx: Add new ti,lp5569 compatible

Add new ti,lp5569 compatible, this is similar to national,lp5523 with
slight change to reg order and reg type and advanced way for LED fault.

Signed-off-by: Christian Marangi <[email protected]>
Acked-by: Conor Dooley <[email protected]>
---
Documentation/devicetree/bindings/leds/leds-lp55xx.yaml | 1 +
1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/leds/leds-lp55xx.yaml b/Documentation/devicetree/bindings/leds/leds-lp55xx.yaml
index 77828dedbb9f..fe8aaecf3010 100644
--- a/Documentation/devicetree/bindings/leds/leds-lp55xx.yaml
+++ b/Documentation/devicetree/bindings/leds/leds-lp55xx.yaml
@@ -28,6 +28,7 @@ properties:
- national,lp5523
- ti,lp55231
- ti,lp5562
+ - ti,lp5569
- ti,lp8501

reg:
--
2.43.0


2024-06-15 23:13:51

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 03/20] 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.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5521.c | 11 ++++------
drivers/leds/leds-lp5523.c | 20 +++++++++++-------
drivers/leds/leds-lp5562.c | 15 ++++++-------
drivers/leds/leds-lp55xx-common.c | 35 +++++++++++++++++++++++++++++++
drivers/leds/leds-lp55xx-common.h | 16 ++++++++++++--
drivers/leds/leds-lp8501.c | 20 +++++++++++-------
6 files changed, 83 insertions(+), 34 deletions(-)

diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index f9c8b568b652..15ef5ae58623 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -135,12 +135,6 @@ static void lp5521_load_engine(struct lp55xx_chip *chip)
lp5521_wait_opmode_done();
}

-static void lp5521_stop_all_engines(struct lp55xx_chip *chip)
-{
- lp55xx_write(chip, LP5521_REG_OP_MODE, 0);
- lp5521_wait_opmode_done();
-}
-
static void lp5521_stop_engine(struct lp55xx_chip *chip)
{
enum lp55xx_engine_index idx = chip->engine_idx;
@@ -499,6 +493,9 @@ static const struct attribute_group lp5521_group = {

/* Chip specific configurations */
static struct lp55xx_device_config lp5521_cfg = {
+ .reg_op_mode = {
+ .addr = LP5521_REG_OP_MODE,
+ },
.reset = {
.addr = LP5521_REG_RESET,
.val = LP5521_RESET,
@@ -585,7 +582,7 @@ static void lp5521_remove(struct i2c_client *client)
struct lp55xx_led *led = i2c_get_clientdata(client);
struct lp55xx_chip *chip = led->chip;

- lp5521_stop_all_engines(chip);
+ lp55xx_stop_all_engine(chip);
lp55xx_unregister_sysfs(chip);
lp55xx_deinit_device(chip);
}
diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 38de853f9939..79931555eddd 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -41,7 +41,10 @@
#define LP5523_REG_LED_PWM_BASE 0x16
#define LP5523_REG_LED_CURRENT_BASE 0x26
#define LP5523_REG_CONFIG 0x36
+
#define LP5523_REG_STATUS 0x3A
+#define LP5523_ENGINE_BUSY BIT(4)
+
#define LP5523_REG_RESET 0x3D
#define LP5523_REG_LED_TEST_CTRL 0x41
#define LP5523_REG_LED_TEST_ADC 0x42
@@ -190,12 +193,6 @@ static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
}

-static void lp5523_stop_all_engines(struct lp55xx_chip *chip)
-{
- lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
- lp5523_wait_opmode_done();
-}
-
static void lp5523_stop_engine(struct lp55xx_chip *chip)
{
enum lp55xx_engine_index idx = chip->engine_idx;
@@ -322,7 +319,7 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip)
}

out:
- lp5523_stop_all_engines(chip);
+ lp55xx_stop_all_engine(chip);
return ret;
}

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

/* Chip specific configurations */
static struct lp55xx_device_config lp5523_cfg = {
+ .reg_op_mode = {
+ .addr = LP5523_REG_OP_MODE,
+ },
+ .engine_busy = {
+ .addr = LP5523_REG_STATUS,
+ .mask = LP5523_ENGINE_BUSY,
+ },
.reset = {
.addr = LP5523_REG_RESET,
.val = LP5523_RESET,
@@ -959,7 +963,7 @@ static void lp5523_remove(struct i2c_client *client)
struct lp55xx_led *led = i2c_get_clientdata(client);
struct lp55xx_chip *chip = led->chip;

- lp5523_stop_all_engines(chip);
+ lp55xx_stop_all_engine(chip);
lp55xx_unregister_sysfs(chip);
lp55xx_deinit_device(chip);
}
diff --git a/drivers/leds/leds-lp5562.c b/drivers/leds/leds-lp5562.c
index 39db9aeb67c5..28469bd2c75b 100644
--- a/drivers/leds/leds-lp5562.c
+++ b/drivers/leds/leds-lp5562.c
@@ -144,12 +144,6 @@ static void lp5562_load_engine(struct lp55xx_chip *chip)
lp5562_wait_opmode_done();
}

-static void lp5562_stop_engine(struct lp55xx_chip *chip)
-{
- lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DISABLE);
- lp5562_wait_opmode_done();
-}
-
static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
{
int ret;
@@ -160,7 +154,7 @@ static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
if (!start) {
lp55xx_write(chip, LP5562_REG_ENABLE, LP5562_ENABLE_DEFAULT);
lp5562_wait_enable_done();
- lp5562_stop_engine(chip);
+ lp55xx_stop_all_engine(chip);
lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
lp5562_wait_opmode_done();
@@ -369,7 +363,7 @@ static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
return -EINVAL;
}

- lp5562_stop_engine(chip);
+ lp55xx_stop_all_engine(chip);

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

- lp5562_stop_engine(chip);
+ lp55xx_stop_all_engine(chip);

lp55xx_unregister_sysfs(chip);
lp55xx_deinit_device(chip);
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 8e7074f0fee0..7d8fe67a38c7 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -13,6 +13,7 @@
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/i2c.h>
+#include <linux/iopoll.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/platform_data/leds-lp55xx.h>
@@ -22,6 +23,12 @@

#include "leds-lp55xx-common.h"

+/* OP MODE require at least 153 us to clear regs */
+#define LP55XX_CMD_SLEEP 200
+
+/* Program Commands */
+#define LP55xx_MODE_DISABLE_ALL_ENG 0x0
+
/* External clock rate */
#define LP55XX_CLK_32K 32768

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

+static void lp55xx_wait_opmode_done(struct lp55xx_chip *chip)
+{
+ struct lp55xx_device_config *cfg = chip->cfg;
+ int ret;
+ u8 val;
+
+ /*
+ * Recent chip supports BUSY bit for engine.
+ * Check support by checking if val is not 0.
+ * For legacy device, sleep at least 153 us.
+ */
+ if (cfg->engine_busy.val)
+ read_poll_timeout(lp55xx_read, ret, !(val & cfg->engine_busy.mask),
+ LP55XX_CMD_SLEEP, LP55XX_CMD_SLEEP * 10, false,
+ chip, cfg->engine_busy.addr, &val);
+ else
+ usleep_range(LP55XX_CMD_SLEEP, LP55XX_CMD_SLEEP * 2);
+}
+
+void lp55xx_stop_all_engine(struct lp55xx_chip *chip)
+{
+ struct lp55xx_device_config *cfg = chip->cfg;
+
+ lp55xx_write(chip, cfg->reg_op_mode.addr, LP55xx_MODE_DISABLE_ALL_ENG);
+ lp55xx_wait_opmode_done(chip);
+}
+EXPORT_SYMBOL_GPL(lp55xx_stop_all_engine);
+
static void lp55xx_reset_device(struct lp55xx_chip *chip)
{
struct lp55xx_device_config *cfg = chip->cfg;
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index 2f38c5b33830..c7c8a77ddb1a 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -81,15 +81,22 @@ struct lp55xx_chip;
/*
* struct lp55xx_reg
* @addr : Register address
- * @val : Register value
+ * @val : Register value (can also used as mask or shift)
*/
struct lp55xx_reg {
u8 addr;
- u8 val;
+ union {
+ u8 val;
+ u8 mask;
+ u8 shift;
+ };
};

/*
* struct lp55xx_device_config
+ * @reg_op_mode : Chip specific OP MODE reg addr
+ * @engine_busy : Chip specific engine busy
+ * (if not supported 153 us sleep)
* @reset : Chip specific reset command
* @enable : Chip specific enable command
* @max_channel : Maximum number of channels
@@ -102,6 +109,8 @@ struct lp55xx_reg {
* @dev_attr_group : Device specific attributes
*/
struct lp55xx_device_config {
+ const struct lp55xx_reg reg_op_mode; /* addr, shift */
+ const struct lp55xx_reg engine_busy; /* addr, mask */
const struct lp55xx_reg reset;
const struct lp55xx_reg enable;
const int max_channel;
@@ -191,6 +200,9 @@ extern int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg,
/* external clock detection */
extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip);

+/* common chip functions */
+extern void lp55xx_stop_all_engine(struct lp55xx_chip *chip);
+
/* common device init/deinit functions */
extern int lp55xx_init_device(struct lp55xx_chip *chip);
extern void lp55xx_deinit_device(struct lp55xx_chip *chip);
diff --git a/drivers/leds/leds-lp8501.c b/drivers/leds/leds-lp8501.c
index ac50aa88939a..57e184dda490 100644
--- a/drivers/leds/leds-lp8501.c
+++ b/drivers/leds/leds-lp8501.c
@@ -58,6 +58,9 @@
#define LP8501_INT_CLK BIT(0)
#define LP8501_DEFAULT_CFG (LP8501_PWM_PSAVE | LP8501_AUTO_INC | LP8501_PWR_SAVE)

+#define LP8501_REG_STATUS 0x3A
+#define LP8501_ENGINE_BUSY BIT(4)
+
#define LP8501_REG_RESET 0x3D
#define LP8501_RESET 0xFF

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

-static void lp8501_stop_engine(struct lp55xx_chip *chip)
-{
- lp55xx_write(chip, LP8501_REG_OP_MODE, 0);
- lp8501_wait_opmode_done();
-}
-
static void lp8501_turn_off_channels(struct lp55xx_chip *chip)
{
int i;
@@ -163,7 +160,7 @@ static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)

/* stop engine */
if (!start) {
- lp8501_stop_engine(chip);
+ lp55xx_stop_all_engine(chip);
lp8501_turn_off_channels(chip);
return;
}
@@ -285,6 +282,13 @@ static int lp8501_led_brightness(struct lp55xx_led *led)

/* Chip specific configurations */
static struct lp55xx_device_config lp8501_cfg = {
+ .reg_op_mode = {
+ .addr = LP8501_REG_OP_MODE,
+ },
+ .engine_busy = {
+ .addr = LP8501_REG_STATUS,
+ .maks = LP8501_ENGINE_BUSY,
+ },
.reset = {
.addr = LP8501_REG_RESET,
.val = LP8501_RESET,
@@ -369,7 +373,7 @@ static void lp8501_remove(struct i2c_client *client)
struct lp55xx_led *led = i2c_get_clientdata(client);
struct lp55xx_chip *chip = led->chip;

- lp8501_stop_engine(chip);
+ lp55xx_stop_all_engine(chip);
lp55xx_unregister_sysfs(chip);
lp55xx_deinit_device(chip);
}
--
2.43.0


2024-06-15 23:14:00

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 05/20] 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.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5521.c | 26 +++----------------------
drivers/leds/leds-lp5523.c | 26 +++----------------------
drivers/leds/leds-lp5562.c | 24 ++---------------------
drivers/leds/leds-lp55xx-common.c | 32 ++++++++++++++++++++++++++++++-
drivers/leds/leds-lp55xx-common.h | 1 +
drivers/leds/leds-lp8501.c | 17 ++--------------
6 files changed, 42 insertions(+), 84 deletions(-)

diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index 5015f385cc17..08db470fff6c 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -115,26 +115,6 @@ static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
led_current);
}

-static void lp5521_load_engine(struct lp55xx_chip *chip)
-{
- enum lp55xx_engine_index idx = chip->engine_idx;
- static const u8 mask[] = {
- [LP55XX_ENGINE_1] = LP5521_MODE_R_M,
- [LP55XX_ENGINE_2] = LP5521_MODE_G_M,
- [LP55XX_ENGINE_3] = LP5521_MODE_B_M,
- };
-
- static const u8 val[] = {
- [LP55XX_ENGINE_1] = LP5521_LOAD_R,
- [LP55XX_ENGINE_2] = LP5521_LOAD_G,
- [LP55XX_ENGINE_3] = LP5521_LOAD_B,
- };
-
- lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]);
-
- lp5521_wait_opmode_done();
-}
-
static void lp5521_stop_engine(struct lp55xx_chip *chip)
{
enum lp55xx_engine_index idx = chip->engine_idx;
@@ -264,7 +244,7 @@ static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
* 2) write firmware data into program memory
*/

- lp5521_load_engine(chip);
+ lp55xx_load_engine(chip);
lp5521_update_program_memory(chip, fw->data, fw->size);
}

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

chip->engine_idx = nr;
- lp5521_load_engine(chip);
+ lp55xx_load_engine(chip);
ret = lp5521_update_program_memory(chip, buf, len);

mutex_unlock(&chip->lock);
diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index bd0209e2ee42..086b4d8975a4 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -159,26 +159,6 @@ static int lp5523_post_init_device(struct lp55xx_chip *chip)
return lp5523_init_program_engine(chip);
}

-static void lp5523_load_engine(struct lp55xx_chip *chip)
-{
- enum lp55xx_engine_index idx = chip->engine_idx;
- static const u8 mask[] = {
- [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
- [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
- [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
- };
-
- static const u8 val[] = {
- [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
- [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
- [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
- };
-
- lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
-
- lp5523_wait_opmode_done();
-}
-
static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
{
enum lp55xx_engine_index idx = chip->engine_idx;
@@ -188,7 +168,7 @@ static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
[LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
};

- lp5523_load_engine(chip);
+ lp55xx_load_engine(chip);

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

- lp5523_load_engine(chip);
+ lp55xx_load_engine(chip);

ret = lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, mux_page[nr]);
if (ret)
diff --git a/drivers/leds/leds-lp5562.c b/drivers/leds/leds-lp5562.c
index 65a6a05c3848..5e26a52f534f 100644
--- a/drivers/leds/leds-lp5562.c
+++ b/drivers/leds/leds-lp5562.c
@@ -124,26 +124,6 @@ static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current)
lp55xx_write(led->chip, addr[led->chan_nr], led_current);
}

-static void lp5562_load_engine(struct lp55xx_chip *chip)
-{
- enum lp55xx_engine_index idx = chip->engine_idx;
- static const u8 mask[] = {
- [LP55XX_ENGINE_1] = LP5562_MODE_ENG1_M,
- [LP55XX_ENGINE_2] = LP5562_MODE_ENG2_M,
- [LP55XX_ENGINE_3] = LP5562_MODE_ENG3_M,
- };
-
- static const u8 val[] = {
- [LP55XX_ENGINE_1] = LP5562_LOAD_ENG1,
- [LP55XX_ENGINE_2] = LP5562_LOAD_ENG2,
- [LP55XX_ENGINE_3] = LP5562_LOAD_ENG3,
- };
-
- lp55xx_update_bits(chip, LP5562_REG_OP_MODE, mask[idx], val[idx]);
-
- lp5562_wait_opmode_done();
-}
-
static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
{
int ret;
@@ -270,7 +250,7 @@ static void lp5562_firmware_loaded(struct lp55xx_chip *chip)
* 2) write firmware data into program memory
*/

- lp5562_load_engine(chip);
+ lp55xx_load_engine(chip);
lp5562_update_firmware(chip, fw->data, fw->size);
}

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

/* Clear program registers */
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index a2ea449a1d5e..06cbdef70f9a 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -26,8 +26,24 @@
/* OP MODE require at least 153 us to clear regs */
#define LP55XX_CMD_SLEEP 200

-/* Program Commands */
+/*
+ * Program Memory Operations
+ * Same Mask for each engine for both mode and exec
+ * ENG1 GENMASK(3, 2)
+ * ENG2 GENMASK(5, 4)
+ * ENG3 GENMASK(7, 6)
+ */
#define LP55xx_MODE_DISABLE_ALL_ENG 0x0
+#define LP55xx_MODE_ENG_MASK GENMASK(1, 0)
+#define LP55xx_MODE_DISABLE_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x0)
+#define LP55xx_MODE_LOAD_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x1)
+#define LP55xx_MODE_RUN_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x2)
+#define LP55xx_MODE_HALT_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x3)
+
+#define LP55xx_MODE_ENGn_SHIFT(n, shift) ((shift) + (2 * (3 - (n))))
+#define LP55xx_MODE_ENGn_MASK(n, shift) (LP55xx_MODE_ENG_MASK << LP55xx_MODE_ENGn_SHIFT(n, shift))
+#define LP55xx_MODE_ENGn_GET(n, mode, shift) \
+ (((mode) >> LP55xx_MODE_ENGn_SHIFT(n, shift)) & LP55xx_MODE_ENG_MASK)

/* External clock rate */
#define LP55XX_CLK_32K 32768
@@ -77,6 +93,20 @@ void lp55xx_stop_all_engine(struct lp55xx_chip *chip)
}
EXPORT_SYMBOL_GPL(lp55xx_stop_all_engine);

+void lp55xx_load_engine(struct lp55xx_chip *chip)
+{
+ enum lp55xx_engine_index idx = chip->engine_idx;
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ u8 mask, val;
+
+ mask = LP55xx_MODE_ENGn_MASK(idx, cfg->reg_op_mode.shift);
+ val = LP55xx_MODE_LOAD_ENG << LP55xx_MODE_ENGn_SHIFT(idx, cfg->reg_op_mode.shift);
+
+ lp55xx_update_bits(chip, cfg->reg_op_mode.addr, mask, val);
+ lp55xx_wait_opmode_done(chip);
+}
+EXPORT_SYMBOL_GPL(lp55xx_load_engine);
+
static void lp55xx_reset_device(struct lp55xx_chip *chip)
{
const struct lp55xx_device_config *cfg = chip->cfg;
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index 26a724acac16..bb146bcecdcf 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -202,6 +202,7 @@ extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip);

/* common chip functions */
extern void lp55xx_stop_all_engine(struct lp55xx_chip *chip);
+extern void lp55xx_load_engine(struct lp55xx_chip *chip);

/* common probe/remove function */
extern int lp55xx_probe(struct i2c_client *client);
diff --git a/drivers/leds/leds-lp8501.c b/drivers/leds/leds-lp8501.c
index d3c718bb8275..04173d6875af 100644
--- a/drivers/leds/leds-lp8501.c
+++ b/drivers/leds/leds-lp8501.c
@@ -119,17 +119,6 @@ static int lp8501_post_init_device(struct lp55xx_chip *chip)
static void lp8501_load_engine(struct lp55xx_chip *chip)
{
enum lp55xx_engine_index idx = chip->engine_idx;
- static const u8 mask[] = {
- [LP55XX_ENGINE_1] = LP8501_MODE_ENG1_M,
- [LP55XX_ENGINE_2] = LP8501_MODE_ENG2_M,
- [LP55XX_ENGINE_3] = LP8501_MODE_ENG3_M,
- };
-
- static const u8 val[] = {
- [LP55XX_ENGINE_1] = LP8501_LOAD_ENG1,
- [LP55XX_ENGINE_2] = LP8501_LOAD_ENG2,
- [LP55XX_ENGINE_3] = LP8501_LOAD_ENG3,
- };

static const u8 page_sel[] = {
[LP55XX_ENGINE_1] = LP8501_PAGE_ENG1,
@@ -137,9 +126,7 @@ static void lp8501_load_engine(struct lp55xx_chip *chip)
[LP55XX_ENGINE_3] = LP8501_PAGE_ENG3,
};

- lp55xx_update_bits(chip, LP8501_REG_OP_MODE, mask[idx], val[idx]);
-
- lp8501_wait_opmode_done();
+ lp55xx_load_engine(chip);

lp55xx_write(chip, LP8501_REG_PROG_PAGE_SEL, page_sel[idx]);
}
@@ -287,7 +274,7 @@ static struct lp55xx_device_config lp8501_cfg = {
},
.engine_busy = {
.addr = LP8501_REG_STATUS,
- .maks = LP8501_ENGINE_BUSY,
+ .mask = LP8501_ENGINE_BUSY,
},
.reset = {
.addr = LP8501_REG_RESET,
--
2.43.0


2024-06-15 23:14:04

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 04/20] leds: leds-lp55xx: generalize probe/remove functions

Now that stop_all_engine is generalized, probe and remove function are
the same across every lp55xx based LED driver and can be generalized.

To permit to use a common probe, make use of the OF match_data and i2c
driver_data value to store the device_config struct specific for the
LED.

Also drop the now unused exported symbol in lp55xx-common and make them
static.

Update any lp55xx based LED driver to use the new generic probe/remove.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5521.c | 81 +-------------------
drivers/leds/leds-lp5523.c | 85 ++-------------------
drivers/leds/leds-lp5562.c | 80 +------------------
drivers/leds/leds-lp55xx-common.c | 123 +++++++++++++++++++++++-------
drivers/leds/leds-lp55xx-common.h | 21 +----
drivers/leds/leds-lp8501.c | 81 +-------------------
6 files changed, 119 insertions(+), 352 deletions(-)

diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index 15ef5ae58623..5015f385cc17 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -514,87 +514,14 @@ static struct lp55xx_device_config lp5521_cfg = {
.dev_attr_group = &lp5521_group,
};

-static int lp5521_probe(struct i2c_client *client)
-{
- const struct i2c_device_id *id = i2c_client_get_device_id(client);
- int ret;
- struct lp55xx_chip *chip;
- struct lp55xx_led *led;
- struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
- struct device_node *np = dev_of_node(&client->dev);
-
- chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
- if (!chip)
- return -ENOMEM;
-
- chip->cfg = &lp5521_cfg;
-
- if (!pdata) {
- if (np) {
- pdata = lp55xx_of_populate_pdata(&client->dev, np,
- chip);
- if (IS_ERR(pdata))
- return PTR_ERR(pdata);
- } else {
- dev_err(&client->dev, "no platform data\n");
- return -EINVAL;
- }
- }
-
- led = devm_kcalloc(&client->dev,
- pdata->num_channels, sizeof(*led), GFP_KERNEL);
- if (!led)
- return -ENOMEM;
-
- chip->cl = client;
- chip->pdata = pdata;
-
- mutex_init(&chip->lock);
-
- i2c_set_clientdata(client, led);
-
- ret = lp55xx_init_device(chip);
- if (ret)
- goto err_init;
-
- dev_info(&client->dev, "%s programmable led chip found\n", id->name);
-
- ret = lp55xx_register_leds(led, chip);
- if (ret)
- goto err_out;
-
- ret = lp55xx_register_sysfs(chip);
- if (ret) {
- dev_err(&client->dev, "registering sysfs failed\n");
- goto err_out;
- }
-
- return 0;
-
-err_out:
- lp55xx_deinit_device(chip);
-err_init:
- return ret;
-}
-
-static void lp5521_remove(struct i2c_client *client)
-{
- struct lp55xx_led *led = i2c_get_clientdata(client);
- struct lp55xx_chip *chip = led->chip;
-
- lp55xx_stop_all_engine(chip);
- lp55xx_unregister_sysfs(chip);
- lp55xx_deinit_device(chip);
-}
-
static const struct i2c_device_id lp5521_id[] = {
- { "lp5521", 0 }, /* Three channel chip */
+ { "lp5521", .driver_data = (kernel_ulong_t)&lp5521_cfg, }, /* Three channel chip */
{ }
};
MODULE_DEVICE_TABLE(i2c, lp5521_id);

static const struct of_device_id of_lp5521_leds_match[] = {
- { .compatible = "national,lp5521", },
+ { .compatible = "national,lp5521", .data = &lp5521_cfg, },
{},
};

@@ -605,8 +532,8 @@ static struct i2c_driver lp5521_driver = {
.name = "lp5521",
.of_match_table = of_lp5521_leds_match,
},
- .probe = lp5521_probe,
- .remove = lp5521_remove,
+ .probe = lp55xx_probe,
+ .remove = lp55xx_remove,
.id_table = lp5521_id,
};

diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 79931555eddd..bd0209e2ee42 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -895,90 +895,17 @@ static struct lp55xx_device_config lp5523_cfg = {
.dev_attr_group = &lp5523_group,
};

-static int lp5523_probe(struct i2c_client *client)
-{
- const struct i2c_device_id *id = i2c_client_get_device_id(client);
- int ret;
- struct lp55xx_chip *chip;
- struct lp55xx_led *led;
- struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
- struct device_node *np = dev_of_node(&client->dev);
-
- chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
- if (!chip)
- return -ENOMEM;
-
- chip->cfg = &lp5523_cfg;
-
- if (!pdata) {
- if (np) {
- pdata = lp55xx_of_populate_pdata(&client->dev, np,
- chip);
- if (IS_ERR(pdata))
- return PTR_ERR(pdata);
- } else {
- dev_err(&client->dev, "no platform data\n");
- return -EINVAL;
- }
- }
-
- led = devm_kcalloc(&client->dev,
- pdata->num_channels, sizeof(*led), GFP_KERNEL);
- if (!led)
- return -ENOMEM;
-
- chip->cl = client;
- chip->pdata = pdata;
-
- mutex_init(&chip->lock);
-
- i2c_set_clientdata(client, led);
-
- ret = lp55xx_init_device(chip);
- if (ret)
- goto err_init;
-
- dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
-
- ret = lp55xx_register_leds(led, chip);
- if (ret)
- goto err_out;
-
- ret = lp55xx_register_sysfs(chip);
- if (ret) {
- dev_err(&client->dev, "registering sysfs failed\n");
- goto err_out;
- }
-
- return 0;
-
-err_out:
- lp55xx_deinit_device(chip);
-err_init:
- return ret;
-}
-
-static void lp5523_remove(struct i2c_client *client)
-{
- struct lp55xx_led *led = i2c_get_clientdata(client);
- struct lp55xx_chip *chip = led->chip;
-
- lp55xx_stop_all_engine(chip);
- lp55xx_unregister_sysfs(chip);
- lp55xx_deinit_device(chip);
-}
-
static const struct i2c_device_id lp5523_id[] = {
- { "lp5523", LP5523 },
- { "lp55231", LP55231 },
+ { "lp5523", .driver_data = (kernel_ulong_t)&lp5523_cfg, },
+ { "lp55231", .driver_data = (kernel_ulong_t)&lp5523_cfg, },
{ }
};

MODULE_DEVICE_TABLE(i2c, lp5523_id);

static const struct of_device_id of_lp5523_leds_match[] = {
- { .compatible = "national,lp5523", },
- { .compatible = "ti,lp55231", },
+ { .compatible = "national,lp5523", .data = &lp5523_cfg, },
+ { .compatible = "ti,lp55231", .data = &lp5523_cfg, },
{},
};

@@ -989,8 +916,8 @@ static struct i2c_driver lp5523_driver = {
.name = "lp5523x",
.of_match_table = of_lp5523_leds_match,
},
- .probe = lp5523_probe,
- .remove = lp5523_remove,
+ .probe = lp55xx_probe,
+ .remove = lp55xx_remove,
.id_table = lp5523_id,
};

diff --git a/drivers/leds/leds-lp5562.c b/drivers/leds/leds-lp5562.c
index 28469bd2c75b..65a6a05c3848 100644
--- a/drivers/leds/leds-lp5562.c
+++ b/drivers/leds/leds-lp5562.c
@@ -508,86 +508,14 @@ static struct lp55xx_device_config lp5562_cfg = {
.dev_attr_group = &lp5562_group,
};

-static int lp5562_probe(struct i2c_client *client)
-{
- int ret;
- struct lp55xx_chip *chip;
- struct lp55xx_led *led;
- struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
- struct device_node *np = dev_of_node(&client->dev);
-
- chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
- if (!chip)
- return -ENOMEM;
-
- chip->cfg = &lp5562_cfg;
-
- if (!pdata) {
- if (np) {
- pdata = lp55xx_of_populate_pdata(&client->dev, np,
- chip);
- if (IS_ERR(pdata))
- return PTR_ERR(pdata);
- } else {
- dev_err(&client->dev, "no platform data\n");
- return -EINVAL;
- }
- }
-
-
- led = devm_kcalloc(&client->dev,
- pdata->num_channels, sizeof(*led), GFP_KERNEL);
- if (!led)
- return -ENOMEM;
-
- chip->cl = client;
- chip->pdata = pdata;
-
- mutex_init(&chip->lock);
-
- i2c_set_clientdata(client, led);
-
- ret = lp55xx_init_device(chip);
- if (ret)
- goto err_init;
-
- ret = lp55xx_register_leds(led, chip);
- if (ret)
- goto err_out;
-
- ret = lp55xx_register_sysfs(chip);
- if (ret) {
- dev_err(&client->dev, "registering sysfs failed\n");
- goto err_out;
- }
-
- return 0;
-
-err_out:
- lp55xx_deinit_device(chip);
-err_init:
- return ret;
-}
-
-static void lp5562_remove(struct i2c_client *client)
-{
- struct lp55xx_led *led = i2c_get_clientdata(client);
- struct lp55xx_chip *chip = led->chip;
-
- lp55xx_stop_all_engine(chip);
-
- lp55xx_unregister_sysfs(chip);
- lp55xx_deinit_device(chip);
-}
-
static const struct i2c_device_id lp5562_id[] = {
- { "lp5562", 0 },
+ { "lp5562", .driver_data = (kernel_ulong_t)&lp5562_cfg, },
{ }
};
MODULE_DEVICE_TABLE(i2c, lp5562_id);

static const struct of_device_id of_lp5562_leds_match[] = {
- { .compatible = "ti,lp5562", },
+ { .compatible = "ti,lp5562", .data = &lp5562_cfg, },
{},
};

@@ -598,8 +526,8 @@ static struct i2c_driver lp5562_driver = {
.name = "lp5562",
.of_match_table = of_lp5562_leds_match,
},
- .probe = lp5562_probe,
- .remove = lp5562_remove,
+ .probe = lp55xx_probe,
+ .remove = lp55xx_remove,
.id_table = lp5562_id,
};

diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 7d8fe67a38c7..a2ea449a1d5e 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -32,6 +32,8 @@
/* External clock rate */
#define LP55XX_CLK_32K 32768

+static void lp55xx_deinit_device(struct lp55xx_chip *chip);
+
static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
{
return container_of(cdev, struct lp55xx_led, cdev);
@@ -49,7 +51,7 @@ static struct lp55xx_led *mcled_cdev_to_led(struct led_classdev_mc *mc_cdev)

static void lp55xx_wait_opmode_done(struct lp55xx_chip *chip)
{
- struct lp55xx_device_config *cfg = chip->cfg;
+ const struct lp55xx_device_config *cfg = chip->cfg;
int ret;
u8 val;

@@ -68,7 +70,7 @@ static void lp55xx_wait_opmode_done(struct lp55xx_chip *chip)

void lp55xx_stop_all_engine(struct lp55xx_chip *chip)
{
- struct lp55xx_device_config *cfg = chip->cfg;
+ const struct lp55xx_device_config *cfg = chip->cfg;

lp55xx_write(chip, cfg->reg_op_mode.addr, LP55xx_MODE_DISABLE_ALL_ENG);
lp55xx_wait_opmode_done(chip);
@@ -77,7 +79,7 @@ EXPORT_SYMBOL_GPL(lp55xx_stop_all_engine);

static void lp55xx_reset_device(struct lp55xx_chip *chip)
{
- struct lp55xx_device_config *cfg = chip->cfg;
+ const struct lp55xx_device_config *cfg = chip->cfg;
u8 addr = cfg->reset.addr;
u8 val = cfg->reset.val;

@@ -87,7 +89,7 @@ static void lp55xx_reset_device(struct lp55xx_chip *chip)

static int lp55xx_detect_device(struct lp55xx_chip *chip)
{
- struct lp55xx_device_config *cfg = chip->cfg;
+ const struct lp55xx_device_config *cfg = chip->cfg;
u8 addr = cfg->enable.addr;
u8 val = cfg->enable.val;
int ret;
@@ -110,7 +112,7 @@ static int lp55xx_detect_device(struct lp55xx_chip *chip)

static int lp55xx_post_init_device(struct lp55xx_chip *chip)
{
- struct lp55xx_device_config *cfg = chip->cfg;
+ const struct lp55xx_device_config *cfg = chip->cfg;

if (!cfg->post_init_device)
return 0;
@@ -175,7 +177,7 @@ static int lp55xx_set_mc_brightness(struct led_classdev *cdev,
{
struct led_classdev_mc *mc_dev = lcdev_to_mccdev(cdev);
struct lp55xx_led *led = mcled_cdev_to_led(mc_dev);
- struct lp55xx_device_config *cfg = led->chip->cfg;
+ const struct lp55xx_device_config *cfg = led->chip->cfg;

led_mc_calc_color_components(&led->mc_cdev, brightness);
return cfg->multicolor_brightness_fn(led);
@@ -186,7 +188,7 @@ static int lp55xx_set_brightness(struct led_classdev *cdev,
enum led_brightness brightness)
{
struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
- struct lp55xx_device_config *cfg = led->chip->cfg;
+ const struct lp55xx_device_config *cfg = led->chip->cfg;

led->brightness = (u8)brightness;
return cfg->brightness_fn(led);
@@ -196,7 +198,7 @@ static int lp55xx_init_led(struct lp55xx_led *led,
struct lp55xx_chip *chip, int chan)
{
struct lp55xx_platform_data *pdata = chip->pdata;
- struct lp55xx_device_config *cfg = chip->cfg;
+ const struct lp55xx_device_config *cfg = chip->cfg;
struct device *dev = &chip->cl->dev;
int max_channel = cfg->max_channel;
struct mc_subled *mc_led_info;
@@ -458,10 +460,10 @@ bool lp55xx_is_extclk_used(struct lp55xx_chip *chip)
}
EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used);

-int lp55xx_init_device(struct lp55xx_chip *chip)
+static int lp55xx_init_device(struct lp55xx_chip *chip)
{
struct lp55xx_platform_data *pdata;
- struct lp55xx_device_config *cfg;
+ const struct lp55xx_device_config *cfg;
struct device *dev = &chip->cl->dev;
int ret = 0;

@@ -511,9 +513,8 @@ int lp55xx_init_device(struct lp55xx_chip *chip)
err:
return ret;
}
-EXPORT_SYMBOL_GPL(lp55xx_init_device);

-void lp55xx_deinit_device(struct lp55xx_chip *chip)
+static void lp55xx_deinit_device(struct lp55xx_chip *chip)
{
struct lp55xx_platform_data *pdata = chip->pdata;

@@ -523,12 +524,11 @@ void lp55xx_deinit_device(struct lp55xx_chip *chip)
if (pdata->enable_gpiod)
gpiod_set_value(pdata->enable_gpiod, 0);
}
-EXPORT_SYMBOL_GPL(lp55xx_deinit_device);

-int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
+static int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
{
struct lp55xx_platform_data *pdata = chip->pdata;
- struct lp55xx_device_config *cfg = chip->cfg;
+ const struct lp55xx_device_config *cfg = chip->cfg;
int num_channels = pdata->num_channels;
struct lp55xx_led *each;
u8 led_current;
@@ -565,12 +565,11 @@ int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
err_init_led:
return ret;
}
-EXPORT_SYMBOL_GPL(lp55xx_register_leds);

-int lp55xx_register_sysfs(struct lp55xx_chip *chip)
+static int lp55xx_register_sysfs(struct lp55xx_chip *chip)
{
struct device *dev = &chip->cl->dev;
- struct lp55xx_device_config *cfg = chip->cfg;
+ const struct lp55xx_device_config *cfg = chip->cfg;
int ret;

if (!cfg->run_engine || !cfg->firmware_cb)
@@ -584,19 +583,17 @@ int lp55xx_register_sysfs(struct lp55xx_chip *chip)
return cfg->dev_attr_group ?
sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
}
-EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);

-void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
+static void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
{
struct device *dev = &chip->cl->dev;
- struct lp55xx_device_config *cfg = chip->cfg;
+ const struct lp55xx_device_config *cfg = chip->cfg;

if (cfg->dev_attr_group)
sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);

sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
}
-EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);

static int lp55xx_parse_common_child(struct device_node *np,
struct lp55xx_led_config *cfg,
@@ -689,9 +686,9 @@ static int lp55xx_parse_logical_led(struct device_node *np,
return ret;
}

-struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
- struct device_node *np,
- struct lp55xx_chip *chip)
+static struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
+ struct device_node *np,
+ struct lp55xx_chip *chip)
{
struct device_node *child;
struct lp55xx_platform_data *pdata;
@@ -748,7 +745,81 @@ struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,

return pdata;
}
-EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata);
+
+int lp55xx_probe(struct i2c_client *client)
+{
+ const struct i2c_device_id *id = i2c_client_get_device_id(client);
+ int ret;
+ struct lp55xx_chip *chip;
+ struct lp55xx_led *led;
+ struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
+ struct device_node *np = dev_of_node(&client->dev);
+
+ chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
+ if (!chip)
+ return -ENOMEM;
+
+ chip->cfg = i2c_get_match_data(client);
+
+ if (!pdata) {
+ if (np) {
+ pdata = lp55xx_of_populate_pdata(&client->dev, np,
+ chip);
+ if (IS_ERR(pdata))
+ return PTR_ERR(pdata);
+ } else {
+ dev_err(&client->dev, "no platform data\n");
+ return -EINVAL;
+ }
+ }
+
+ led = devm_kcalloc(&client->dev,
+ pdata->num_channels, sizeof(*led), GFP_KERNEL);
+ if (!led)
+ return -ENOMEM;
+
+ chip->cl = client;
+ chip->pdata = pdata;
+
+ mutex_init(&chip->lock);
+
+ i2c_set_clientdata(client, led);
+
+ ret = lp55xx_init_device(chip);
+ if (ret)
+ goto err_init;
+
+ dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
+
+ ret = lp55xx_register_leds(led, chip);
+ if (ret)
+ goto err_out;
+
+ ret = lp55xx_register_sysfs(chip);
+ if (ret) {
+ dev_err(&client->dev, "registering sysfs failed\n");
+ goto err_out;
+ }
+
+ return 0;
+
+err_out:
+ lp55xx_deinit_device(chip);
+err_init:
+ return ret;
+}
+EXPORT_SYMBOL_GPL(lp55xx_probe);
+
+void lp55xx_remove(struct i2c_client *client)
+{
+ struct lp55xx_led *led = i2c_get_clientdata(client);
+ struct lp55xx_chip *chip = led->chip;
+
+ lp55xx_stop_all_engine(chip);
+ lp55xx_unregister_sysfs(chip);
+ lp55xx_deinit_device(chip);
+}
+EXPORT_SYMBOL_GPL(lp55xx_remove);

MODULE_AUTHOR("Milo Kim <[email protected]>");
MODULE_DESCRIPTION("LP55xx Common Driver");
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index c7c8a77ddb1a..26a724acac16 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -164,7 +164,7 @@ struct lp55xx_chip {
struct lp55xx_platform_data *pdata;
struct mutex lock; /* lock for user-space interface */
int num_leds;
- struct lp55xx_device_config *cfg;
+ const struct lp55xx_device_config *cfg;
enum lp55xx_engine_index engine_idx;
struct lp55xx_engine engines[LP55XX_ENGINE_MAX];
const struct firmware *fw;
@@ -203,21 +203,8 @@ extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip);
/* common chip functions */
extern void lp55xx_stop_all_engine(struct lp55xx_chip *chip);

-/* common device init/deinit functions */
-extern int lp55xx_init_device(struct lp55xx_chip *chip);
-extern void lp55xx_deinit_device(struct lp55xx_chip *chip);
-
-/* common LED class device functions */
-extern int lp55xx_register_leds(struct lp55xx_led *led,
- struct lp55xx_chip *chip);
-
-/* common device attributes functions */
-extern int lp55xx_register_sysfs(struct lp55xx_chip *chip);
-extern void lp55xx_unregister_sysfs(struct lp55xx_chip *chip);
-
-/* common device tree population function */
-extern struct lp55xx_platform_data
-*lp55xx_of_populate_pdata(struct device *dev, struct device_node *np,
- struct lp55xx_chip *chip);
+/* common probe/remove function */
+extern int lp55xx_probe(struct i2c_client *client);
+extern void lp55xx_remove(struct i2c_client *client);

#endif /* _LEDS_LP55XX_COMMON_H */
diff --git a/drivers/leds/leds-lp8501.c b/drivers/leds/leds-lp8501.c
index 57e184dda490..d3c718bb8275 100644
--- a/drivers/leds/leds-lp8501.c
+++ b/drivers/leds/leds-lp8501.c
@@ -305,87 +305,14 @@ static struct lp55xx_device_config lp8501_cfg = {
.run_engine = lp8501_run_engine,
};

-static int lp8501_probe(struct i2c_client *client)
-{
- const struct i2c_device_id *id = i2c_client_get_device_id(client);
- int ret;
- struct lp55xx_chip *chip;
- struct lp55xx_led *led;
- struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
- struct device_node *np = dev_of_node(&client->dev);
-
- chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
- if (!chip)
- return -ENOMEM;
-
- chip->cfg = &lp8501_cfg;
-
- if (!pdata) {
- if (np) {
- pdata = lp55xx_of_populate_pdata(&client->dev, np,
- chip);
- if (IS_ERR(pdata))
- return PTR_ERR(pdata);
- } else {
- dev_err(&client->dev, "no platform data\n");
- return -EINVAL;
- }
- }
-
- led = devm_kcalloc(&client->dev,
- pdata->num_channels, sizeof(*led), GFP_KERNEL);
- if (!led)
- return -ENOMEM;
-
- chip->cl = client;
- chip->pdata = pdata;
-
- mutex_init(&chip->lock);
-
- i2c_set_clientdata(client, led);
-
- ret = lp55xx_init_device(chip);
- if (ret)
- goto err_init;
-
- dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
-
- ret = lp55xx_register_leds(led, chip);
- if (ret)
- goto err_out;
-
- ret = lp55xx_register_sysfs(chip);
- if (ret) {
- dev_err(&client->dev, "registering sysfs failed\n");
- goto err_out;
- }
-
- return 0;
-
-err_out:
- lp55xx_deinit_device(chip);
-err_init:
- return ret;
-}
-
-static void lp8501_remove(struct i2c_client *client)
-{
- struct lp55xx_led *led = i2c_get_clientdata(client);
- struct lp55xx_chip *chip = led->chip;
-
- lp55xx_stop_all_engine(chip);
- lp55xx_unregister_sysfs(chip);
- lp55xx_deinit_device(chip);
-}
-
static const struct i2c_device_id lp8501_id[] = {
- { "lp8501", 0 },
+ { "lp8501", .driver_data = (kernel_ulong_t)&lp8501_cfg, },
{ }
};
MODULE_DEVICE_TABLE(i2c, lp8501_id);

static const struct of_device_id of_lp8501_leds_match[] = {
- { .compatible = "ti,lp8501", },
+ { .compatible = "ti,lp8501", .data = &lp8501_cfg, },
{},
};

@@ -396,8 +323,8 @@ static struct i2c_driver lp8501_driver = {
.name = "lp8501",
.of_match_table = of_lp8501_leds_match,
},
- .probe = lp8501_probe,
- .remove = lp8501_remove,
+ .probe = lp55xx_probe,
+ .remove = lp55xx_remove,
.id_table = lp8501_id,
};

--
2.43.0


2024-06-15 23:14:08

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 06/20] leds: leds-lp55xx: generalize load_engine_and_select_page function

Generalize load_engine_and_select_page by reworking the implementation
and making it part of the generic load_engine function.

Add a new option in device_config, pages_per_engine used to define pages
assigned to each engine. With this option set, it's assumed LED chip
supports pages and load_engine will correctly setup the write page.

An equal amount of pages is assigned to each engine and they are
assigned from page 0.

Update any lp55xx based LED driver to define the option and use the new
function.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5523.c | 22 +++++-----------------
drivers/leds/leds-lp55xx-common.c | 10 ++++++++++
drivers/leds/leds-lp55xx-common.h | 3 +++
drivers/leds/leds-lp8501.c | 19 +++----------------
4 files changed, 21 insertions(+), 33 deletions(-)

diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 086b4d8975a4..8dabd5814110 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -30,6 +30,7 @@
* 0x40 engine 2 muxing info
* 0x50 engine 3 muxing info
*/
+#define LP5523_PAGES_PER_ENGINE 1
#define LP5523_MAX_LEDS 9

/* Registers */
@@ -159,20 +160,6 @@ static int lp5523_post_init_device(struct lp55xx_chip *chip)
return lp5523_init_program_engine(chip);
}

-static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
-{
- enum lp55xx_engine_index idx = chip->engine_idx;
- static const u8 page_sel[] = {
- [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1,
- [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2,
- [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
- };
-
- lp55xx_load_engine(chip);
-
- lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
-}
-
static void lp5523_stop_engine(struct lp55xx_chip *chip)
{
enum lp55xx_engine_index idx = chip->engine_idx;
@@ -272,7 +259,7 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip)
/* write LED MUX address space for each engine */
for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
chip->engine_idx = i;
- lp5523_load_engine_and_select_page(chip);
+ lp55xx_load_engine(chip);

for (j = 0; j < LP5523_PROGRAM_LENGTH; j++) {
ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + j,
@@ -362,7 +349,7 @@ static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
* 2) write firmware data into program memory
*/

- lp5523_load_engine_and_select_page(chip);
+ lp55xx_load_engine(chip);
lp5523_update_program_memory(chip, fw->data, fw->size);
}

@@ -544,7 +531,7 @@ static ssize_t store_engine_load(struct device *dev,
mutex_lock(&chip->lock);

chip->engine_idx = nr;
- lp5523_load_engine_and_select_page(chip);
+ lp55xx_load_engine(chip);
ret = lp5523_update_program_memory(chip, buf, len);

mutex_unlock(&chip->lock);
@@ -865,6 +852,7 @@ static struct lp55xx_device_config lp5523_cfg = {
.addr = LP5523_REG_ENABLE,
.val = LP5523_ENABLE,
},
+ .pages_per_engine = LP5523_PAGES_PER_ENGINE,
.max_channel = LP5523_MAX_LEDS,
.post_init_device = lp5523_post_init_device,
.brightness_fn = lp5523_led_brightness,
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 06cbdef70f9a..636e3ca0bb87 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -45,6 +45,11 @@
#define LP55xx_MODE_ENGn_GET(n, mode, shift) \
(((mode) >> LP55xx_MODE_ENGn_SHIFT(n, shift)) & LP55xx_MODE_ENG_MASK)

+/* Memory Page Selection */
+#define LP55xx_REG_PROG_PAGE_SEL 0x4f
+/* If supported, each ENGINE have an equal amount of pages offset from page 0 */
+#define LP55xx_PAGE_OFFSET(n, pages) (((n) - 1) * (pages))
+
/* External clock rate */
#define LP55XX_CLK_32K 32768

@@ -104,6 +109,11 @@ void lp55xx_load_engine(struct lp55xx_chip *chip)

lp55xx_update_bits(chip, cfg->reg_op_mode.addr, mask, val);
lp55xx_wait_opmode_done(chip);
+
+ /* Setup PAGE if supported (pages_per_engine not 0)*/
+ if (cfg->pages_per_engine)
+ lp55xx_write(chip, LP55xx_REG_PROG_PAGE_SEL,
+ LP55xx_PAGE_OFFSET(idx, cfg->pages_per_engine));
}
EXPORT_SYMBOL_GPL(lp55xx_load_engine);

diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index bb146bcecdcf..015ac2ef4e4d 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -99,6 +99,8 @@ struct lp55xx_reg {
* (if not supported 153 us sleep)
* @reset : Chip specific reset command
* @enable : Chip specific enable command
+ * @pages_per_engine : Assigned pages for each engine
+ * (if not set chip doesn't support pages)
* @max_channel : Maximum number of channels
* @post_init_device : Chip specific initialization code
* @brightness_fn : Brightness function
@@ -113,6 +115,7 @@ struct lp55xx_device_config {
const struct lp55xx_reg engine_busy; /* addr, mask */
const struct lp55xx_reg reset;
const struct lp55xx_reg enable;
+ const int pages_per_engine;
const int max_channel;

/* define if the device has specific initialization process */
diff --git a/drivers/leds/leds-lp8501.c b/drivers/leds/leds-lp8501.c
index 04173d6875af..abe2c4b213d7 100644
--- a/drivers/leds/leds-lp8501.c
+++ b/drivers/leds/leds-lp8501.c
@@ -21,6 +21,7 @@
#include "leds-lp55xx-common.h"

#define LP8501_PROGRAM_LENGTH 32
+#define LP8501_PAGES_PER_ENGINE 1
#define LP8501_MAX_LEDS 9

/* Registers */
@@ -116,21 +117,6 @@ static int lp8501_post_init_device(struct lp55xx_chip *chip)
LP8501_PWR_CONFIG_M, chip->pdata->pwr_sel);
}

-static void lp8501_load_engine(struct lp55xx_chip *chip)
-{
- enum lp55xx_engine_index idx = chip->engine_idx;
-
- static const u8 page_sel[] = {
- [LP55XX_ENGINE_1] = LP8501_PAGE_ENG1,
- [LP55XX_ENGINE_2] = LP8501_PAGE_ENG2,
- [LP55XX_ENGINE_3] = LP8501_PAGE_ENG3,
- };
-
- lp55xx_load_engine(chip);
-
- lp55xx_write(chip, LP8501_REG_PROG_PAGE_SEL, page_sel[idx]);
-}
-
static void lp8501_turn_off_channels(struct lp55xx_chip *chip)
{
int i;
@@ -250,7 +236,7 @@ static void lp8501_firmware_loaded(struct lp55xx_chip *chip)
* 2) write firmware data into program memory
*/

- lp8501_load_engine(chip);
+ lp55xx_load_engine(chip);
lp8501_update_program_memory(chip, fw->data, fw->size);
}

@@ -284,6 +270,7 @@ static struct lp55xx_device_config lp8501_cfg = {
.addr = LP8501_REG_ENABLE,
.val = LP8501_ENABLE,
},
+ .pages_per_engine = LP8501_PAGES_PER_ENGINE,
.max_channel = LP8501_MAX_LEDS,
.post_init_device = lp8501_post_init_device,
.brightness_fn = lp8501_led_brightness,
--
2.43.0


2024-06-15 23:14:23

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 07/20] leds: leds-lp55xx: generalize run_engine function

Generalize run_engine function for lp55xx based LED driver. The logic is
similar to every LED driver, rework it with more macro magic and account
for LED model that might have OP MODE and EXEC at base offset in the
reg.

Update any lp55xx based LED driver to use this generalized function and
declare required bits.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5521.c | 42 +++++-------------------------
drivers/leds/leds-lp5523.c | 41 +++--------------------------
drivers/leds/leds-lp5562.c | 42 +++++-------------------------
drivers/leds/leds-lp55xx-common.c | 43 +++++++++++++++++++++++++++++++
drivers/leds/leds-lp55xx-common.h | 2 ++
drivers/leds/leds-lp8501.c | 41 +++--------------------------
6 files changed, 65 insertions(+), 146 deletions(-)

diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index 08db470fff6c..0b9f99f4fff2 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -132,8 +132,6 @@ static void lp5521_stop_engine(struct lp55xx_chip *chip)
static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
{
int ret;
- u8 mode;
- u8 exec;

/* stop engine */
if (!start) {
@@ -143,40 +141,9 @@ static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
return;
}

- /*
- * To run the engine,
- * operation mode and enable register should updated at the same time
- */
-
- ret = lp55xx_read(chip, LP5521_REG_OP_MODE, &mode);
- if (ret)
- return;
-
- ret = lp55xx_read(chip, LP5521_REG_ENABLE, &exec);
- if (ret)
- return;
-
- /* change operation mode to RUN only when each engine is loading */
- if (LP5521_R_IS_LOADING(mode)) {
- mode = (mode & ~LP5521_MODE_R_M) | LP5521_RUN_R;
- exec = (exec & ~LP5521_EXEC_R_M) | LP5521_RUN_R;
- }
-
- if (LP5521_G_IS_LOADING(mode)) {
- mode = (mode & ~LP5521_MODE_G_M) | LP5521_RUN_G;
- exec = (exec & ~LP5521_EXEC_G_M) | LP5521_RUN_G;
- }
-
- if (LP5521_B_IS_LOADING(mode)) {
- mode = (mode & ~LP5521_MODE_B_M) | LP5521_RUN_B;
- exec = (exec & ~LP5521_EXEC_B_M) | LP5521_RUN_B;
- }
-
- lp55xx_write(chip, LP5521_REG_OP_MODE, mode);
- lp5521_wait_opmode_done();
-
- lp55xx_update_bits(chip, LP5521_REG_ENABLE, LP5521_EXEC_M, exec);
- lp5521_wait_enable_done();
+ ret = lp55xx_run_engine_common(chip);
+ if (!ret)
+ lp5521_wait_enable_done();
}

static int lp5521_update_program_memory(struct lp55xx_chip *chip,
@@ -476,6 +443,9 @@ static struct lp55xx_device_config lp5521_cfg = {
.reg_op_mode = {
.addr = LP5521_REG_OP_MODE,
},
+ .reg_exec = {
+ .addr = LP5521_REG_ENABLE,
+ },
.reset = {
.addr = LP5521_REG_RESET,
.val = LP5521_RESET,
diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 8dabd5814110..b28955b72189 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -184,10 +184,6 @@ static void lp5523_turn_off_channels(struct lp55xx_chip *chip)

static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
{
- int ret;
- u8 mode;
- u8 exec;
-
/* stop engine */
if (!start) {
lp5523_stop_engine(chip);
@@ -195,39 +191,7 @@ static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
return;
}

- /*
- * To run the engine,
- * operation mode and enable register should updated at the same time
- */
-
- ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
- if (ret)
- return;
-
- ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
- if (ret)
- return;
-
- /* change operation mode to RUN only when each engine is loading */
- if (LP5523_ENG1_IS_LOADING(mode)) {
- mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1;
- exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1;
- }
-
- if (LP5523_ENG2_IS_LOADING(mode)) {
- mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2;
- exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2;
- }
-
- if (LP5523_ENG3_IS_LOADING(mode)) {
- mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3;
- exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3;
- }
-
- lp55xx_write(chip, LP5523_REG_OP_MODE, mode);
- lp5523_wait_opmode_done();
-
- lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec);
+ lp55xx_run_engine_common(chip);
}

static int lp5523_init_program_engine(struct lp55xx_chip *chip)
@@ -840,6 +804,9 @@ static struct lp55xx_device_config lp5523_cfg = {
.reg_op_mode = {
.addr = LP5523_REG_OP_MODE,
},
+ .reg_exec = {
+ .addr = LP5523_REG_ENABLE,
+ },
.engine_busy = {
.addr = LP5523_REG_STATUS,
.mask = LP5523_ENGINE_BUSY,
diff --git a/drivers/leds/leds-lp5562.c b/drivers/leds/leds-lp5562.c
index 5e26a52f534f..fb05439576c3 100644
--- a/drivers/leds/leds-lp5562.c
+++ b/drivers/leds/leds-lp5562.c
@@ -127,8 +127,6 @@ static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current)
static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
{
int ret;
- u8 mode;
- u8 exec;

/* stop engine */
if (!start) {
@@ -141,40 +139,9 @@ static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
return;
}

- /*
- * To run the engine,
- * operation mode and enable register should updated at the same time
- */
-
- ret = lp55xx_read(chip, LP5562_REG_OP_MODE, &mode);
- if (ret)
- return;
-
- ret = lp55xx_read(chip, LP5562_REG_ENABLE, &exec);
- if (ret)
- return;
-
- /* change operation mode to RUN only when each engine is loading */
- if (LP5562_ENG1_IS_LOADING(mode)) {
- mode = (mode & ~LP5562_MODE_ENG1_M) | LP5562_RUN_ENG1;
- exec = (exec & ~LP5562_EXEC_ENG1_M) | LP5562_RUN_ENG1;
- }
-
- if (LP5562_ENG2_IS_LOADING(mode)) {
- mode = (mode & ~LP5562_MODE_ENG2_M) | LP5562_RUN_ENG2;
- exec = (exec & ~LP5562_EXEC_ENG2_M) | LP5562_RUN_ENG2;
- }
-
- if (LP5562_ENG3_IS_LOADING(mode)) {
- mode = (mode & ~LP5562_MODE_ENG3_M) | LP5562_RUN_ENG3;
- exec = (exec & ~LP5562_EXEC_ENG3_M) | LP5562_RUN_ENG3;
- }
-
- lp55xx_write(chip, LP5562_REG_OP_MODE, mode);
- lp5562_wait_opmode_done();
-
- lp55xx_update_bits(chip, LP5562_REG_ENABLE, LP5562_EXEC_M, exec);
- lp5562_wait_enable_done();
+ ret = lp55xx_run_engine_common(chip);
+ if (!ret)
+ lp5562_wait_enable_done();
}

static int lp5562_update_firmware(struct lp55xx_chip *chip,
@@ -472,6 +439,9 @@ static struct lp55xx_device_config lp5562_cfg = {
.reg_op_mode = {
.addr = LP5562_REG_OP_MODE,
},
+ .reg_exec = {
+ .addr = LP5562_REG_ENABLE,
+ },
.reset = {
.addr = LP5562_REG_RESET,
.val = LP5562_RESET,
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 636e3ca0bb87..9c2e3b2c72fb 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -45,6 +45,15 @@
#define LP55xx_MODE_ENGn_GET(n, mode, shift) \
(((mode) >> LP55xx_MODE_ENGn_SHIFT(n, shift)) & LP55xx_MODE_ENG_MASK)

+#define LP55xx_EXEC_ENG_MASK GENMASK(1, 0)
+#define LP55xx_EXEC_HOLD_ENG FIELD_PREP_CONST(LP55xx_EXEC_ENG_MASK, 0x0)
+#define LP55xx_EXEC_STEP_ENG FIELD_PREP_CONST(LP55xx_EXEC_ENG_MASK, 0x1)
+#define LP55xx_EXEC_RUN_ENG FIELD_PREP_CONST(LP55xx_EXEC_ENG_MASK, 0x2)
+#define LP55xx_EXEC_ONCE_ENG FIELD_PREP_CONST(LP55xx_EXEC_ENG_MASK, 0x3)
+
+#define LP55xx_EXEC_ENGn_SHIFT(n, shift) ((shift) + (2 * (3 - (n))))
+#define LP55xx_EXEC_ENGn_MASK(n, shift) (LP55xx_EXEC_ENG_MASK << LP55xx_EXEC_ENGn_SHIFT(n, shift))
+
/* Memory Page Selection */
#define LP55xx_REG_PROG_PAGE_SEL 0x4f
/* If supported, each ENGINE have an equal amount of pages offset from page 0 */
@@ -117,6 +126,40 @@ void lp55xx_load_engine(struct lp55xx_chip *chip)
}
EXPORT_SYMBOL_GPL(lp55xx_load_engine);

+int lp55xx_run_engine_common(struct lp55xx_chip *chip)
+{
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ u8 mode, exec;
+ int i, ret;
+
+ /* To run the engine, both OP MODE and EXEC needs to be put in RUN mode */
+ ret = lp55xx_read(chip, cfg->reg_op_mode.addr, &mode);
+ if (ret)
+ return ret;
+
+ ret = lp55xx_read(chip, cfg->reg_exec.addr, &exec);
+ if (ret)
+ return ret;
+
+ /* Switch to RUN only for engine that were put in LOAD previously */
+ for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
+ if (LP55xx_MODE_ENGn_GET(i, mode, cfg->reg_op_mode.shift) != LP55xx_MODE_LOAD_ENG)
+ continue;
+
+ mode &= ~LP55xx_MODE_ENGn_MASK(i, cfg->reg_op_mode.shift);
+ mode |= LP55xx_MODE_RUN_ENG << LP55xx_MODE_ENGn_SHIFT(i, cfg->reg_op_mode.shift);
+ exec &= ~LP55xx_EXEC_ENGn_MASK(i, cfg->reg_exec.shift);
+ exec |= LP55xx_EXEC_RUN_ENG << LP55xx_EXEC_ENGn_SHIFT(i, cfg->reg_exec.shift);
+ }
+
+ lp55xx_write(chip, cfg->reg_op_mode.addr, mode);
+ lp55xx_wait_opmode_done(chip);
+ lp55xx_write(chip, cfg->reg_exec.addr, exec);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(lp55xx_run_engine_common);
+
static void lp55xx_reset_device(struct lp55xx_chip *chip)
{
const struct lp55xx_device_config *cfg = chip->cfg;
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index 015ac2ef4e4d..dd74b214ec74 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -112,6 +112,7 @@ struct lp55xx_reg {
*/
struct lp55xx_device_config {
const struct lp55xx_reg reg_op_mode; /* addr, shift */
+ const struct lp55xx_reg reg_exec; /* addr, shift */
const struct lp55xx_reg engine_busy; /* addr, mask */
const struct lp55xx_reg reset;
const struct lp55xx_reg enable;
@@ -206,6 +207,7 @@ extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip);
/* common chip functions */
extern void lp55xx_stop_all_engine(struct lp55xx_chip *chip);
extern void lp55xx_load_engine(struct lp55xx_chip *chip);
+extern int lp55xx_run_engine_common(struct lp55xx_chip *chip);

/* common probe/remove function */
extern int lp55xx_probe(struct i2c_client *client);
diff --git a/drivers/leds/leds-lp8501.c b/drivers/leds/leds-lp8501.c
index abe2c4b213d7..47b30e9d04a2 100644
--- a/drivers/leds/leds-lp8501.c
+++ b/drivers/leds/leds-lp8501.c
@@ -127,10 +127,6 @@ static void lp8501_turn_off_channels(struct lp55xx_chip *chip)

static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
{
- int ret;
- u8 mode;
- u8 exec;
-
/* stop engine */
if (!start) {
lp55xx_stop_all_engine(chip);
@@ -138,39 +134,7 @@ static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
return;
}

- /*
- * To run the engine,
- * operation mode and enable register should updated at the same time
- */
-
- ret = lp55xx_read(chip, LP8501_REG_OP_MODE, &mode);
- if (ret)
- return;
-
- ret = lp55xx_read(chip, LP8501_REG_ENABLE, &exec);
- if (ret)
- return;
-
- /* change operation mode to RUN only when each engine is loading */
- if (LP8501_ENG1_IS_LOADING(mode)) {
- mode = (mode & ~LP8501_MODE_ENG1_M) | LP8501_RUN_ENG1;
- exec = (exec & ~LP8501_EXEC_ENG1_M) | LP8501_RUN_ENG1;
- }
-
- if (LP8501_ENG2_IS_LOADING(mode)) {
- mode = (mode & ~LP8501_MODE_ENG2_M) | LP8501_RUN_ENG2;
- exec = (exec & ~LP8501_EXEC_ENG2_M) | LP8501_RUN_ENG2;
- }
-
- if (LP8501_ENG3_IS_LOADING(mode)) {
- mode = (mode & ~LP8501_MODE_ENG3_M) | LP8501_RUN_ENG3;
- exec = (exec & ~LP8501_EXEC_ENG3_M) | LP8501_RUN_ENG3;
- }
-
- lp55xx_write(chip, LP8501_REG_OP_MODE, mode);
- lp8501_wait_opmode_done();
-
- lp55xx_update_bits(chip, LP8501_REG_ENABLE, LP8501_EXEC_M, exec);
+ lp55xx_run_engine_common(chip);
}

static int lp8501_update_program_memory(struct lp55xx_chip *chip,
@@ -258,6 +222,9 @@ static struct lp55xx_device_config lp8501_cfg = {
.reg_op_mode = {
.addr = LP8501_REG_OP_MODE,
},
+ .reg_exec = {
+ .addr = LP8501_REG_ENABLE,
+ },
.engine_busy = {
.addr = LP8501_REG_STATUS,
.mask = LP8501_ENGINE_BUSY,
--
2.43.0


2024-06-15 23:14:49

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 09/20] leds: leds-lp55xx: generalize firmware_loaded function

Generalize firmware_loaded function as lp55xx based LED driver all share
the same logic.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5521.c | 22 +---------------------
drivers/leds/leds-lp5523.c | 22 +---------------------
drivers/leds/leds-lp5562.c | 26 +-------------------------
drivers/leds/leds-lp55xx-common.c | 25 +++++++++++++++++++++++++
drivers/leds/leds-lp55xx-common.h | 1 +
drivers/leds/leds-lp8501.c | 22 +---------------------
6 files changed, 30 insertions(+), 88 deletions(-)

diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index 7ea3e5715f59..722b5cd9236e 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -146,26 +146,6 @@ static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
lp5521_wait_enable_done();
}

-static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
-{
- const struct firmware *fw = chip->fw;
-
- if (fw->size > LP5521_PROGRAM_LENGTH) {
- dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
- fw->size);
- return;
- }
-
- /*
- * Program memory sequence
- * 1) set engine mode to "LOAD"
- * 2) write firmware data into program memory
- */
-
- lp55xx_load_engine(chip);
- lp55xx_update_program_memory(chip, fw->data, fw->size);
-}
-
static int lp5521_post_init_device(struct lp55xx_chip *chip)
{
int ret;
@@ -413,7 +393,7 @@ static struct lp55xx_device_config lp5521_cfg = {
.brightness_fn = lp5521_led_brightness,
.multicolor_brightness_fn = lp5521_multicolor_brightness,
.set_led_current = lp5521_set_led_current,
- .firmware_cb = lp5521_firmware_loaded,
+ .firmware_cb = lp55xx_firmware_loaded_cb,
.run_engine = lp5521_run_engine,
.dev_attr_group = &lp5521_group,
};
diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 395c57330484..5525d60c342c 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -254,26 +254,6 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip)
return ret;
}

-static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
-{
- const struct firmware *fw = chip->fw;
-
- if (fw->size > LP5523_PROGRAM_LENGTH) {
- dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
- fw->size);
- return;
- }
-
- /*
- * Program memory sequence
- * 1) set engine mode to "LOAD"
- * 2) write firmware data into program memory
- */
-
- lp55xx_load_engine(chip);
- lp55xx_update_program_memory(chip, fw->data, fw->size);
-}
-
static ssize_t show_engine_mode(struct device *dev,
struct device_attribute *attr,
char *buf, int nr)
@@ -785,7 +765,7 @@ static struct lp55xx_device_config lp5523_cfg = {
.brightness_fn = lp5523_led_brightness,
.multicolor_brightness_fn = lp5523_multicolor_brightness,
.set_led_current = lp5523_set_led_current,
- .firmware_cb = lp5523_firmware_loaded,
+ .firmware_cb = lp55xx_firmware_loaded_cb,
.run_engine = lp5523_run_engine,
.dev_attr_group = &lp5523_group,
};
diff --git a/drivers/leds/leds-lp5562.c b/drivers/leds/leds-lp5562.c
index 7f3733fc446e..e50b68c9ccf3 100644
--- a/drivers/leds/leds-lp5562.c
+++ b/drivers/leds/leds-lp5562.c
@@ -144,30 +144,6 @@ static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
lp5562_wait_enable_done();
}

-static void lp5562_firmware_loaded(struct lp55xx_chip *chip)
-{
- const struct firmware *fw = chip->fw;
-
- /*
- * the firmware is encoded in ascii hex character, with 2 chars
- * per byte
- */
- if (fw->size > (LP5562_PROGRAM_LENGTH * 2)) {
- dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
- fw->size);
- return;
- }
-
- /*
- * Program memory sequence
- * 1) set engine mode to "LOAD"
- * 2) write firmware data into program memory
- */
-
- lp55xx_load_engine(chip);
- lp55xx_update_program_memory(chip, fw->data, fw->size);
-}
-
static int lp5562_post_init_device(struct lp55xx_chip *chip)
{
int ret;
@@ -404,7 +380,7 @@ static struct lp55xx_device_config lp5562_cfg = {
.set_led_current = lp5562_set_led_current,
.brightness_fn = lp5562_led_brightness,
.run_engine = lp5562_run_engine,
- .firmware_cb = lp5562_firmware_loaded,
+ .firmware_cb = lp55xx_firmware_loaded_cb,
.dev_attr_group = &lp5562_group,
};

diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 9f060b412435..51bbe91cc7f2 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -217,6 +217,31 @@ int lp55xx_update_program_memory(struct lp55xx_chip *chip,
}
EXPORT_SYMBOL_GPL(lp55xx_update_program_memory);

+void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip)
+{
+ const struct firmware *fw = chip->fw;
+
+ /*
+ * the firmware is encoded in ascii hex character, with 2 chars
+ * per byte
+ */
+ if (fw->size > LP55xx_PROGRAM_LENGTH * 2) {
+ dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
+ fw->size);
+ return;
+ }
+
+ /*
+ * Program memory sequence
+ * 1) set engine mode to "LOAD"
+ * 2) write firmware data into program memory
+ */
+
+ lp55xx_load_engine(chip);
+ lp55xx_update_program_memory(chip, fw->data, fw->size);
+}
+EXPORT_SYMBOL_GPL(lp55xx_firmware_loaded_cb);
+
static void lp55xx_reset_device(struct lp55xx_chip *chip)
{
const struct lp55xx_device_config *cfg = chip->cfg;
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index f0bbd41fdab3..cbc122c56828 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -212,6 +212,7 @@ extern void lp55xx_load_engine(struct lp55xx_chip *chip);
extern int lp55xx_run_engine_common(struct lp55xx_chip *chip);
extern int lp55xx_update_program_memory(struct lp55xx_chip *chip,
const u8 *data, size_t size);
+extern void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip);

/* common probe/remove function */
extern int lp55xx_probe(struct i2c_client *client);
diff --git a/drivers/leds/leds-lp8501.c b/drivers/leds/leds-lp8501.c
index d4094d20bdc1..1ea7bb73cd22 100644
--- a/drivers/leds/leds-lp8501.c
+++ b/drivers/leds/leds-lp8501.c
@@ -137,26 +137,6 @@ static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
lp55xx_run_engine_common(chip);
}

-static void lp8501_firmware_loaded(struct lp55xx_chip *chip)
-{
- const struct firmware *fw = chip->fw;
-
- if (fw->size > LP8501_PROGRAM_LENGTH) {
- dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
- fw->size);
- return;
- }
-
- /*
- * Program memory sequence
- * 1) set engine mode to "LOAD"
- * 2) write firmware data into program memory
- */
-
- lp55xx_load_engine(chip);
- lp55xx_update_program_memory(chip, fw->data, fw->size);
-}
-
static int lp8501_led_brightness(struct lp55xx_led *led)
{
struct lp55xx_chip *chip = led->chip;
@@ -198,7 +178,7 @@ static struct lp55xx_device_config lp8501_cfg = {
.post_init_device = lp8501_post_init_device,
.brightness_fn = lp8501_led_brightness,
.set_led_current = lp8501_set_led_current,
- .firmware_cb = lp8501_firmware_loaded,
+ .firmware_cb = lp55xx_firmware_loaded_cb,
.run_engine = lp8501_run_engine,
};

--
2.43.0


2024-06-15 23:14:49

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 08/20] leds: leds-lp55xx: generalize update_program_memory function

LED Driver based on lp55xx all use the same logic to write memory in
SMEM. The only difference is that legacy chip doesn't support pages and
have the engine regs one after another.

To handle this apply the same logic used for load_engine also for
update_program_memory.

Introduce a new config in device_config, base_prog. For LED chip
that doesn't support pages, offset this values of 32 for each engine.

Update all lp55xx based LED driver to use this new function and define
all the required bits.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5521.c | 56 +++--------------------------
drivers/leds/leds-lp5523.c | 50 +++-----------------------
drivers/leds/leds-lp5562.c | 58 +++----------------------------
drivers/leds/leds-lp55xx-common.c | 57 ++++++++++++++++++++++++++++++
drivers/leds/leds-lp55xx-common.h | 4 +++
drivers/leds/leds-lp8501.c | 52 +++------------------------
6 files changed, 79 insertions(+), 198 deletions(-)

diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index 0b9f99f4fff2..7ea3e5715f59 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -146,55 +146,6 @@ static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
lp5521_wait_enable_done();
}

-static int lp5521_update_program_memory(struct lp55xx_chip *chip,
- const u8 *data, size_t size)
-{
- enum lp55xx_engine_index idx = chip->engine_idx;
- u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
- static const u8 addr[] = {
- [LP55XX_ENGINE_1] = LP5521_REG_R_PROG_MEM,
- [LP55XX_ENGINE_2] = LP5521_REG_G_PROG_MEM,
- [LP55XX_ENGINE_3] = LP5521_REG_B_PROG_MEM,
- };
- unsigned cmd;
- char c[3];
- int nrchars;
- int ret;
- int offset = 0;
- int i = 0;
-
- while ((offset < size - 1) && (i < LP5521_PROGRAM_LENGTH)) {
- /* separate sscanfs because length is working only for %s */
- ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
- if (ret != 1)
- goto err;
-
- ret = sscanf(c, "%2x", &cmd);
- if (ret != 1)
- goto err;
-
- pattern[i] = (u8)cmd;
- offset += nrchars;
- i++;
- }
-
- /* Each instruction is 16bit long. Check that length is even */
- if (i % 2)
- goto err;
-
- for (i = 0; i < LP5521_PROGRAM_LENGTH; i++) {
- ret = lp55xx_write(chip, addr[idx] + i, pattern[i]);
- if (ret)
- return -EINVAL;
- }
-
- return size;
-
-err:
- dev_err(&chip->cl->dev, "wrong pattern format\n");
- return -EINVAL;
-}
-
static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
{
const struct firmware *fw = chip->fw;
@@ -212,7 +163,7 @@ static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
*/

lp55xx_load_engine(chip);
- lp5521_update_program_memory(chip, fw->data, fw->size);
+ lp55xx_update_program_memory(chip, fw->data, fw->size);
}

static int lp5521_post_init_device(struct lp55xx_chip *chip)
@@ -389,7 +340,7 @@ static ssize_t store_engine_load(struct device *dev,

chip->engine_idx = nr;
lp55xx_load_engine(chip);
- ret = lp5521_update_program_memory(chip, buf, len);
+ ret = lp55xx_update_program_memory(chip, buf, len);

mutex_unlock(&chip->lock);

@@ -454,6 +405,9 @@ static struct lp55xx_device_config lp5521_cfg = {
.addr = LP5521_REG_ENABLE,
.val = LP5521_ENABLE_DEFAULT,
},
+ .prog_mem_base = {
+ .addr = LP5521_REG_R_PROG_MEM,
+ },
.max_channel = LP5521_MAX_LEDS,
.post_init_device = lp5521_post_init_device,
.brightness_fn = lp5521_led_brightness,
diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index b28955b72189..395c57330484 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -254,49 +254,6 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip)
return ret;
}

-static int lp5523_update_program_memory(struct lp55xx_chip *chip,
- const u8 *data, size_t size)
-{
- u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
- unsigned int cmd;
- char c[3];
- int nrchars;
- int ret;
- int offset = 0;
- int i = 0;
-
- while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
- /* separate sscanfs because length is working only for %s */
- ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
- if (ret != 1)
- goto err;
-
- ret = sscanf(c, "%2x", &cmd);
- if (ret != 1)
- goto err;
-
- pattern[i] = (u8)cmd;
- offset += nrchars;
- i++;
- }
-
- /* Each instruction is 16bit long. Check that length is even */
- if (i % 2)
- goto err;
-
- for (i = 0; i < LP5523_PROGRAM_LENGTH; i++) {
- ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
- if (ret)
- return -EINVAL;
- }
-
- return size;
-
-err:
- dev_err(&chip->cl->dev, "wrong pattern format\n");
- return -EINVAL;
-}
-
static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
{
const struct firmware *fw = chip->fw;
@@ -314,7 +271,7 @@ static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
*/

lp55xx_load_engine(chip);
- lp5523_update_program_memory(chip, fw->data, fw->size);
+ lp55xx_update_program_memory(chip, fw->data, fw->size);
}

static ssize_t show_engine_mode(struct device *dev,
@@ -496,7 +453,7 @@ static ssize_t store_engine_load(struct device *dev,

chip->engine_idx = nr;
lp55xx_load_engine(chip);
- ret = lp5523_update_program_memory(chip, buf, len);
+ ret = lp55xx_update_program_memory(chip, buf, len);

mutex_unlock(&chip->lock);

@@ -819,6 +776,9 @@ static struct lp55xx_device_config lp5523_cfg = {
.addr = LP5523_REG_ENABLE,
.val = LP5523_ENABLE,
},
+ .prog_mem_base = {
+ .addr = LP5523_REG_PROG_MEM,
+ },
.pages_per_engine = LP5523_PAGES_PER_ENGINE,
.max_channel = LP5523_MAX_LEDS,
.post_init_device = lp5523_post_init_device,
diff --git a/drivers/leds/leds-lp5562.c b/drivers/leds/leds-lp5562.c
index fb05439576c3..7f3733fc446e 100644
--- a/drivers/leds/leds-lp5562.c
+++ b/drivers/leds/leds-lp5562.c
@@ -144,59 +144,6 @@ static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
lp5562_wait_enable_done();
}

-static int lp5562_update_firmware(struct lp55xx_chip *chip,
- const u8 *data, size_t size)
-{
- enum lp55xx_engine_index idx = chip->engine_idx;
- u8 pattern[LP5562_PROGRAM_LENGTH] = {0};
- static const u8 addr[] = {
- [LP55XX_ENGINE_1] = LP5562_REG_PROG_MEM_ENG1,
- [LP55XX_ENGINE_2] = LP5562_REG_PROG_MEM_ENG2,
- [LP55XX_ENGINE_3] = LP5562_REG_PROG_MEM_ENG3,
- };
- unsigned cmd;
- char c[3];
- int program_size;
- int nrchars;
- int offset = 0;
- int ret;
- int i;
-
- /* clear program memory before updating */
- for (i = 0; i < LP5562_PROGRAM_LENGTH; i++)
- lp55xx_write(chip, addr[idx] + i, 0);
-
- i = 0;
- while ((offset < size - 1) && (i < LP5562_PROGRAM_LENGTH)) {
- /* separate sscanfs because length is working only for %s */
- ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
- if (ret != 1)
- goto err;
-
- ret = sscanf(c, "%2x", &cmd);
- if (ret != 1)
- goto err;
-
- pattern[i] = (u8)cmd;
- offset += nrchars;
- i++;
- }
-
- /* Each instruction is 16bit long. Check that length is even */
- if (i % 2)
- goto err;
-
- program_size = i;
- for (i = 0; i < program_size; i++)
- lp55xx_write(chip, addr[idx] + i, pattern[i]);
-
- return 0;
-
-err:
- dev_err(&chip->cl->dev, "wrong pattern format\n");
- return -EINVAL;
-}
-
static void lp5562_firmware_loaded(struct lp55xx_chip *chip)
{
const struct firmware *fw = chip->fw;
@@ -218,7 +165,7 @@ static void lp5562_firmware_loaded(struct lp55xx_chip *chip)
*/

lp55xx_load_engine(chip);
- lp5562_update_firmware(chip, fw->data, fw->size);
+ lp55xx_update_program_memory(chip, fw->data, fw->size);
}

static int lp5562_post_init_device(struct lp55xx_chip *chip)
@@ -450,6 +397,9 @@ static struct lp55xx_device_config lp5562_cfg = {
.addr = LP5562_REG_ENABLE,
.val = LP5562_ENABLE_DEFAULT,
},
+ .prog_mem_base = {
+ .addr = LP5562_REG_PROG_MEM_ENG1,
+ },
.post_init_device = lp5562_post_init_device,
.set_led_current = lp5562_set_led_current,
.brightness_fn = lp5562_led_brightness,
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 9c2e3b2c72fb..9f060b412435 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -26,6 +26,8 @@
/* OP MODE require at least 153 us to clear regs */
#define LP55XX_CMD_SLEEP 200

+#define LP55xx_PROGRAM_LENGTH 32
+
/*
* Program Memory Operations
* Same Mask for each engine for both mode and exec
@@ -160,6 +162,61 @@ int lp55xx_run_engine_common(struct lp55xx_chip *chip)
}
EXPORT_SYMBOL_GPL(lp55xx_run_engine_common);

+int lp55xx_update_program_memory(struct lp55xx_chip *chip,
+ const u8 *data, size_t size)
+{
+ enum lp55xx_engine_index idx = chip->engine_idx;
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ u8 pattern[LP55xx_PROGRAM_LENGTH] = { };
+ u8 start_addr = cfg->prog_mem_base.addr;
+ int i = 0, offset = 0;
+ int ret;
+
+ while ((offset < size - 1) && (i < LP55xx_PROGRAM_LENGTH)) {
+ unsigned int cmd;
+ int nrchars;
+ char c[3];
+
+ /* separate sscanfs because length is working only for %s */
+ ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
+ if (ret != 1)
+ goto err;
+
+ ret = sscanf(c, "%2x", &cmd);
+ if (ret != 1)
+ goto err;
+
+ pattern[i] = (u8)cmd;
+ offset += nrchars;
+ i++;
+ }
+
+ /* Each instruction is 16bit long. Check that length is even */
+ if (i % 2)
+ goto err;
+
+ /*
+ * For legacy LED chip with no page support, engine base address are
+ * one after another at offset of 32.
+ * For LED chip that support page, PAGE is already set in load_engine.
+ */
+ if (!cfg->pages_per_engine)
+ start_addr += LP55xx_PROGRAM_LENGTH * idx;
+
+ for (i = 0; i < LP55xx_PROGRAM_LENGTH; i++) {
+ ret = lp55xx_write(chip, start_addr + i, pattern[i]);
+ if (ret)
+ return -EINVAL;
+ }
+
+ return size;
+
+err:
+ dev_err(&chip->cl->dev, "wrong pattern format\n");
+ return -EINVAL;
+}
+EXPORT_SYMBOL_GPL(lp55xx_update_program_memory);
+
static void lp55xx_reset_device(struct lp55xx_chip *chip)
{
const struct lp55xx_device_config *cfg = chip->cfg;
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index dd74b214ec74..f0bbd41fdab3 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -99,6 +99,7 @@ struct lp55xx_reg {
* (if not supported 153 us sleep)
* @reset : Chip specific reset command
* @enable : Chip specific enable command
+ * @prog_mem_base : Chip specific base reg address for chip SMEM programming
* @pages_per_engine : Assigned pages for each engine
* (if not set chip doesn't support pages)
* @max_channel : Maximum number of channels
@@ -116,6 +117,7 @@ struct lp55xx_device_config {
const struct lp55xx_reg engine_busy; /* addr, mask */
const struct lp55xx_reg reset;
const struct lp55xx_reg enable;
+ const struct lp55xx_reg prog_mem_base;
const int pages_per_engine;
const int max_channel;

@@ -208,6 +210,8 @@ extern bool lp55xx_is_extclk_used(struct lp55xx_chip *chip);
extern void lp55xx_stop_all_engine(struct lp55xx_chip *chip);
extern void lp55xx_load_engine(struct lp55xx_chip *chip);
extern int lp55xx_run_engine_common(struct lp55xx_chip *chip);
+extern int lp55xx_update_program_memory(struct lp55xx_chip *chip,
+ const u8 *data, size_t size);

/* common probe/remove function */
extern int lp55xx_probe(struct i2c_client *client);
diff --git a/drivers/leds/leds-lp8501.c b/drivers/leds/leds-lp8501.c
index 47b30e9d04a2..d4094d20bdc1 100644
--- a/drivers/leds/leds-lp8501.c
+++ b/drivers/leds/leds-lp8501.c
@@ -137,53 +137,6 @@ static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
lp55xx_run_engine_common(chip);
}

-static int lp8501_update_program_memory(struct lp55xx_chip *chip,
- const u8 *data, size_t size)
-{
- u8 pattern[LP8501_PROGRAM_LENGTH] = {0};
- unsigned cmd;
- char c[3];
- int update_size;
- int nrchars;
- int offset = 0;
- int ret;
- int i;
-
- /* clear program memory before updating */
- for (i = 0; i < LP8501_PROGRAM_LENGTH; i++)
- lp55xx_write(chip, LP8501_REG_PROG_MEM + i, 0);
-
- i = 0;
- while ((offset < size - 1) && (i < LP8501_PROGRAM_LENGTH)) {
- /* separate sscanfs because length is working only for %s */
- ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
- if (ret != 1)
- goto err;
-
- ret = sscanf(c, "%2x", &cmd);
- if (ret != 1)
- goto err;
-
- pattern[i] = (u8)cmd;
- offset += nrchars;
- i++;
- }
-
- /* Each instruction is 16bit long. Check that length is even */
- if (i % 2)
- goto err;
-
- update_size = i;
- for (i = 0; i < update_size; i++)
- lp55xx_write(chip, LP8501_REG_PROG_MEM + i, pattern[i]);
-
- return 0;
-
-err:
- dev_err(&chip->cl->dev, "wrong pattern format\n");
- return -EINVAL;
-}
-
static void lp8501_firmware_loaded(struct lp55xx_chip *chip)
{
const struct firmware *fw = chip->fw;
@@ -201,7 +154,7 @@ static void lp8501_firmware_loaded(struct lp55xx_chip *chip)
*/

lp55xx_load_engine(chip);
- lp8501_update_program_memory(chip, fw->data, fw->size);
+ lp55xx_update_program_memory(chip, fw->data, fw->size);
}

static int lp8501_led_brightness(struct lp55xx_led *led)
@@ -237,6 +190,9 @@ static struct lp55xx_device_config lp8501_cfg = {
.addr = LP8501_REG_ENABLE,
.val = LP8501_ENABLE,
},
+ .prog_mem_base = {
+ .addr = LP8501_REG_PROG_MEM,
+ },
.pages_per_engine = LP8501_PAGES_PER_ENGINE,
.max_channel = LP8501_MAX_LEDS,
.post_init_device = lp8501_post_init_device,
--
2.43.0


2024-06-15 23:15:05

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 10/20] leds: leds-lp55xx: generalize led_brightness function

Generalize led_brightness function as the implementation is the same for
most of the lp55xx based LED driver.

Introduce a new option in device_config, reg_led_pwm_base since the reg
value is not the same for every LED chip.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5521.c | 18 ++++--------------
drivers/leds/leds-lp5523.c | 17 ++++-------------
drivers/leds/leds-lp55xx-common.c | 14 ++++++++++++++
drivers/leds/leds-lp55xx-common.h | 3 +++
drivers/leds/leds-lp8501.c | 18 ++++--------------
5 files changed, 29 insertions(+), 41 deletions(-)

diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index 722b5cd9236e..e4d0dcdbf11b 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -241,19 +241,6 @@ static int lp5521_multicolor_brightness(struct lp55xx_led *led)
return ret;
}

-static int lp5521_led_brightness(struct lp55xx_led *led)
-{
- struct lp55xx_chip *chip = led->chip;
- int ret;
-
- mutex_lock(&chip->lock);
- ret = lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr,
- led->brightness);
- mutex_unlock(&chip->lock);
-
- return ret;
-}
-
static ssize_t show_engine_mode(struct device *dev,
struct device_attribute *attr,
char *buf, int nr)
@@ -388,9 +375,12 @@ static struct lp55xx_device_config lp5521_cfg = {
.prog_mem_base = {
.addr = LP5521_REG_R_PROG_MEM,
},
+ .reg_led_pwm_base = {
+ .addr = LP5521_REG_LED_PWM_BASE,
+ },
.max_channel = LP5521_MAX_LEDS,
.post_init_device = lp5521_post_init_device,
- .brightness_fn = lp5521_led_brightness,
+ .brightness_fn = lp55xx_led_brightness,
.multicolor_brightness_fn = lp5521_multicolor_brightness,
.set_led_current = lp5521_set_led_current,
.firmware_cb = lp55xx_firmware_loaded_cb,
diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 5525d60c342c..a3b4063d504e 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -683,18 +683,6 @@ static int lp5523_multicolor_brightness(struct lp55xx_led *led)
return ret;
}

-static int lp5523_led_brightness(struct lp55xx_led *led)
-{
- struct lp55xx_chip *chip = led->chip;
- int ret;
-
- mutex_lock(&chip->lock);
- ret = lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
- led->brightness);
- mutex_unlock(&chip->lock);
- return ret;
-}
-
static LP55XX_DEV_ATTR_RW(engine1_mode, show_engine1_mode, store_engine1_mode);
static LP55XX_DEV_ATTR_RW(engine2_mode, show_engine2_mode, store_engine2_mode);
static LP55XX_DEV_ATTR_RW(engine3_mode, show_engine3_mode, store_engine3_mode);
@@ -759,10 +747,13 @@ static struct lp55xx_device_config lp5523_cfg = {
.prog_mem_base = {
.addr = LP5523_REG_PROG_MEM,
},
+ .reg_led_pwm_base = {
+ .addr = LP5523_REG_LED_PWM_BASE,
+ },
.pages_per_engine = LP5523_PAGES_PER_ENGINE,
.max_channel = LP5523_MAX_LEDS,
.post_init_device = lp5523_post_init_device,
- .brightness_fn = lp5523_led_brightness,
+ .brightness_fn = lp55xx_led_brightness,
.multicolor_brightness_fn = lp5523_multicolor_brightness,
.set_led_current = lp5523_set_led_current,
.firmware_cb = lp55xx_firmware_loaded_cb,
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 51bbe91cc7f2..726283f155a0 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -242,6 +242,20 @@ void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip)
}
EXPORT_SYMBOL_GPL(lp55xx_firmware_loaded_cb);

+int lp55xx_led_brightness(struct lp55xx_led *led)
+{
+ struct lp55xx_chip *chip = led->chip;
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ int ret;
+
+ mutex_lock(&chip->lock);
+ ret = lp55xx_write(chip, cfg->reg_led_pwm_base.addr + led->chan_nr,
+ led->brightness);
+ mutex_unlock(&chip->lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(lp55xx_led_brightness);
+
static void lp55xx_reset_device(struct lp55xx_chip *chip)
{
const struct lp55xx_device_config *cfg = chip->cfg;
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index cbc122c56828..4578418e5011 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -100,6 +100,7 @@ struct lp55xx_reg {
* @reset : Chip specific reset command
* @enable : Chip specific enable command
* @prog_mem_base : Chip specific base reg address for chip SMEM programming
+ * @reg_led_pwm_base : Chip specific base reg address for LED PWM conf
* @pages_per_engine : Assigned pages for each engine
* (if not set chip doesn't support pages)
* @max_channel : Maximum number of channels
@@ -118,6 +119,7 @@ struct lp55xx_device_config {
const struct lp55xx_reg reset;
const struct lp55xx_reg enable;
const struct lp55xx_reg prog_mem_base;
+ const struct lp55xx_reg reg_led_pwm_base;
const int pages_per_engine;
const int max_channel;

@@ -213,6 +215,7 @@ extern int lp55xx_run_engine_common(struct lp55xx_chip *chip);
extern int lp55xx_update_program_memory(struct lp55xx_chip *chip,
const u8 *data, size_t size);
extern void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip);
+extern int lp55xx_led_brightness(struct lp55xx_led *led);

/* common probe/remove function */
extern int lp55xx_probe(struct i2c_client *client);
diff --git a/drivers/leds/leds-lp8501.c b/drivers/leds/leds-lp8501.c
index 1ea7bb73cd22..8f1fd9525e9a 100644
--- a/drivers/leds/leds-lp8501.c
+++ b/drivers/leds/leds-lp8501.c
@@ -137,19 +137,6 @@ static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
lp55xx_run_engine_common(chip);
}

-static int lp8501_led_brightness(struct lp55xx_led *led)
-{
- struct lp55xx_chip *chip = led->chip;
- int ret;
-
- mutex_lock(&chip->lock);
- ret = lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + led->chan_nr,
- led->brightness);
- mutex_unlock(&chip->lock);
-
- return ret;
-}
-
/* Chip specific configurations */
static struct lp55xx_device_config lp8501_cfg = {
.reg_op_mode = {
@@ -173,10 +160,13 @@ static struct lp55xx_device_config lp8501_cfg = {
.prog_mem_base = {
.addr = LP8501_REG_PROG_MEM,
},
+ .reg_led_pwm_base = {
+ .addr = LP8501_REG_LED_PWM_BASE,
+ },
.pages_per_engine = LP8501_PAGES_PER_ENGINE,
.max_channel = LP8501_MAX_LEDS,
.post_init_device = lp8501_post_init_device,
- .brightness_fn = lp8501_led_brightness,
+ .brightness_fn = lp55xx_led_brightness,
.set_led_current = lp8501_set_led_current,
.firmware_cb = lp55xx_firmware_loaded_cb,
.run_engine = lp8501_run_engine,
--
2.43.0


2024-06-15 23:15:16

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 11/20] leds: leds-lp55xx: generalize multicolor_brightness function

Generalize multicolor_brightness function as the implementation is the
same for most of the lp55xx based LED driver.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5521.c | 21 +--------------------
drivers/leds/leds-lp5523.c | 21 +--------------------
drivers/leds/leds-lp55xx-common.c | 21 +++++++++++++++++++++
drivers/leds/leds-lp55xx-common.h | 1 +
4 files changed, 24 insertions(+), 40 deletions(-)

diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index e4d0dcdbf11b..dd7e996f22f9 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -222,25 +222,6 @@ static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
return 0;
}

-static int lp5521_multicolor_brightness(struct lp55xx_led *led)
-{
- struct lp55xx_chip *chip = led->chip;
- int ret;
- int i;
-
- mutex_lock(&chip->lock);
- for (i = 0; i < led->mc_cdev.num_colors; i++) {
- ret = lp55xx_write(chip,
- LP5521_REG_LED_PWM_BASE +
- led->mc_cdev.subled_info[i].channel,
- led->mc_cdev.subled_info[i].brightness);
- if (ret)
- break;
- }
- mutex_unlock(&chip->lock);
- return ret;
-}
-
static ssize_t show_engine_mode(struct device *dev,
struct device_attribute *attr,
char *buf, int nr)
@@ -381,7 +362,7 @@ static struct lp55xx_device_config lp5521_cfg = {
.max_channel = LP5521_MAX_LEDS,
.post_init_device = lp5521_post_init_device,
.brightness_fn = lp55xx_led_brightness,
- .multicolor_brightness_fn = lp5521_multicolor_brightness,
+ .multicolor_brightness_fn = lp55xx_multicolor_brightness,
.set_led_current = lp5521_set_led_current,
.firmware_cb = lp55xx_firmware_loaded_cb,
.run_engine = lp5521_run_engine,
diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index a3b4063d504e..bfa0c4431ede 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -664,25 +664,6 @@ static ssize_t store_master_fader_leds(struct device *dev,
return ret;
}

-static int lp5523_multicolor_brightness(struct lp55xx_led *led)
-{
- struct lp55xx_chip *chip = led->chip;
- int ret;
- int i;
-
- mutex_lock(&chip->lock);
- for (i = 0; i < led->mc_cdev.num_colors; i++) {
- ret = lp55xx_write(chip,
- LP5523_REG_LED_PWM_BASE +
- led->mc_cdev.subled_info[i].channel,
- led->mc_cdev.subled_info[i].brightness);
- if (ret)
- break;
- }
- mutex_unlock(&chip->lock);
- return ret;
-}
-
static LP55XX_DEV_ATTR_RW(engine1_mode, show_engine1_mode, store_engine1_mode);
static LP55XX_DEV_ATTR_RW(engine2_mode, show_engine2_mode, store_engine2_mode);
static LP55XX_DEV_ATTR_RW(engine3_mode, show_engine3_mode, store_engine3_mode);
@@ -754,7 +735,7 @@ static struct lp55xx_device_config lp5523_cfg = {
.max_channel = LP5523_MAX_LEDS,
.post_init_device = lp5523_post_init_device,
.brightness_fn = lp55xx_led_brightness,
- .multicolor_brightness_fn = lp5523_multicolor_brightness,
+ .multicolor_brightness_fn = lp55xx_multicolor_brightness,
.set_led_current = lp5523_set_led_current,
.firmware_cb = lp55xx_firmware_loaded_cb,
.run_engine = lp5523_run_engine,
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 726283f155a0..9e7c4a798687 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -256,6 +256,27 @@ int lp55xx_led_brightness(struct lp55xx_led *led)
}
EXPORT_SYMBOL_GPL(lp55xx_led_brightness);

+int lp55xx_multicolor_brightness(struct lp55xx_led *led)
+{
+ struct lp55xx_chip *chip = led->chip;
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ int ret;
+ int i;
+
+ mutex_lock(&chip->lock);
+ for (i = 0; i < led->mc_cdev.num_colors; i++) {
+ ret = lp55xx_write(chip,
+ cfg->reg_led_pwm_base.addr +
+ led->mc_cdev.subled_info[i].channel,
+ led->mc_cdev.subled_info[i].brightness);
+ if (ret)
+ break;
+ }
+ mutex_unlock(&chip->lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(lp55xx_multicolor_brightness);
+
static void lp55xx_reset_device(struct lp55xx_chip *chip)
{
const struct lp55xx_device_config *cfg = chip->cfg;
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index 4578418e5011..021dd17bc5d7 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -216,6 +216,7 @@ extern int lp55xx_update_program_memory(struct lp55xx_chip *chip,
const u8 *data, size_t size);
extern void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip);
extern int lp55xx_led_brightness(struct lp55xx_led *led);
+extern int lp55xx_multicolor_brightness(struct lp55xx_led *led);

/* common probe/remove function */
extern int lp55xx_probe(struct i2c_client *client);
--
2.43.0


2024-06-15 23:15:36

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 12/20] leds: leds-lp55xx: generalize set_led_current function

Generalize set_led_current function as the implementation is the same for
most of the lp55xx based LED driver.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5521.c | 12 ++++--------
drivers/leds/leds-lp5523.c | 12 ++++--------
drivers/leds/leds-lp55xx-common.c | 11 +++++++++++
drivers/leds/leds-lp55xx-common.h | 3 +++
drivers/leds/leds-lp8501.c | 12 ++++--------
5 files changed, 26 insertions(+), 24 deletions(-)

diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index dd7e996f22f9..a1a3bf0ff703 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -108,13 +108,6 @@ static inline void lp5521_wait_enable_done(void)
usleep_range(500, 600);
}

-static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
-{
- led->led_current = led_current;
- lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
- led_current);
-}
-
static void lp5521_stop_engine(struct lp55xx_chip *chip)
{
enum lp55xx_engine_index idx = chip->engine_idx;
@@ -359,11 +352,14 @@ static struct lp55xx_device_config lp5521_cfg = {
.reg_led_pwm_base = {
.addr = LP5521_REG_LED_PWM_BASE,
},
+ .reg_led_current_base = {
+ .addr = LP5521_REG_LED_CURRENT_BASE,
+ },
.max_channel = LP5521_MAX_LEDS,
.post_init_device = lp5521_post_init_device,
.brightness_fn = lp55xx_led_brightness,
.multicolor_brightness_fn = lp55xx_multicolor_brightness,
- .set_led_current = lp5521_set_led_current,
+ .set_led_current = lp55xx_set_led_current,
.firmware_cb = lp55xx_firmware_loaded_cb,
.run_engine = lp5521_run_engine,
.dev_attr_group = &lp5521_group,
diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index bfa0c4431ede..3030a4495808 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -122,13 +122,6 @@ static inline void lp5523_wait_opmode_done(void)
usleep_range(1000, 2000);
}

-static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
-{
- led->led_current = led_current;
- lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
- led_current);
-}
-
static int lp5523_post_init_device(struct lp55xx_chip *chip)
{
int ret;
@@ -731,12 +724,15 @@ static struct lp55xx_device_config lp5523_cfg = {
.reg_led_pwm_base = {
.addr = LP5523_REG_LED_PWM_BASE,
},
+ .reg_led_current_base = {
+ .addr = LP5523_REG_LED_CURRENT_BASE,
+ },
.pages_per_engine = LP5523_PAGES_PER_ENGINE,
.max_channel = LP5523_MAX_LEDS,
.post_init_device = lp5523_post_init_device,
.brightness_fn = lp55xx_led_brightness,
.multicolor_brightness_fn = lp55xx_multicolor_brightness,
- .set_led_current = lp5523_set_led_current,
+ .set_led_current = lp55xx_set_led_current,
.firmware_cb = lp55xx_firmware_loaded_cb,
.run_engine = lp5523_run_engine,
.dev_attr_group = &lp5523_group,
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 9e7c4a798687..0d9c2990c335 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -277,6 +277,17 @@ int lp55xx_multicolor_brightness(struct lp55xx_led *led)
}
EXPORT_SYMBOL_GPL(lp55xx_multicolor_brightness);

+void lp55xx_set_led_current(struct lp55xx_led *led, u8 led_current)
+{
+ struct lp55xx_chip *chip = led->chip;
+ const struct lp55xx_device_config *cfg = chip->cfg;
+
+ led->led_current = led_current;
+ lp55xx_write(led->chip, cfg->reg_led_current_base.addr + led->chan_nr,
+ led_current);
+}
+EXPORT_SYMBOL_GPL(lp55xx_set_led_current);
+
static void lp55xx_reset_device(struct lp55xx_chip *chip)
{
const struct lp55xx_device_config *cfg = chip->cfg;
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index 021dd17bc5d7..e638049d9297 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -101,6 +101,7 @@ struct lp55xx_reg {
* @enable : Chip specific enable command
* @prog_mem_base : Chip specific base reg address for chip SMEM programming
* @reg_led_pwm_base : Chip specific base reg address for LED PWM conf
+ * @reg_led_current_base : Chip specific base reg address for LED current conf
* @pages_per_engine : Assigned pages for each engine
* (if not set chip doesn't support pages)
* @max_channel : Maximum number of channels
@@ -120,6 +121,7 @@ struct lp55xx_device_config {
const struct lp55xx_reg enable;
const struct lp55xx_reg prog_mem_base;
const struct lp55xx_reg reg_led_pwm_base;
+ const struct lp55xx_reg reg_led_current_base;
const int pages_per_engine;
const int max_channel;

@@ -217,6 +219,7 @@ extern int lp55xx_update_program_memory(struct lp55xx_chip *chip,
extern void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip);
extern int lp55xx_led_brightness(struct lp55xx_led *led);
extern int lp55xx_multicolor_brightness(struct lp55xx_led *led);
+extern void lp55xx_set_led_current(struct lp55xx_led *led, u8 led_current);

/* common probe/remove function */
extern int lp55xx_probe(struct i2c_client *client);
diff --git a/drivers/leds/leds-lp8501.c b/drivers/leds/leds-lp8501.c
index 8f1fd9525e9a..d924572e4533 100644
--- a/drivers/leds/leds-lp8501.c
+++ b/drivers/leds/leds-lp8501.c
@@ -84,13 +84,6 @@ static inline void lp8501_wait_opmode_done(void)
usleep_range(1000, 2000);
}

-static void lp8501_set_led_current(struct lp55xx_led *led, u8 led_current)
-{
- led->led_current = led_current;
- lp55xx_write(led->chip, LP8501_REG_LED_CURRENT_BASE + led->chan_nr,
- led_current);
-}
-
static int lp8501_post_init_device(struct lp55xx_chip *chip)
{
int ret;
@@ -163,11 +156,14 @@ static struct lp55xx_device_config lp8501_cfg = {
.reg_led_pwm_base = {
.addr = LP8501_REG_LED_PWM_BASE,
},
+ .reg_led_current_base = {
+ .addr = LP8501_REG_LED_CURRENT_BASE,
+ },
.pages_per_engine = LP8501_PAGES_PER_ENGINE,
.max_channel = LP8501_MAX_LEDS,
.post_init_device = lp8501_post_init_device,
.brightness_fn = lp55xx_led_brightness,
- .set_led_current = lp8501_set_led_current,
+ .set_led_current = lp55xx_set_led_current,
.firmware_cb = lp55xx_firmware_loaded_cb,
.run_engine = lp8501_run_engine,
};
--
2.43.0


2024-06-15 23:15:52

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 13/20] leds: leds-lp55xx: generalize turn_off_channels function

Generalize turn_off_channels function as the implementation is the same for
most of the lp55xx based LED driver.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5523.c | 10 +---------
drivers/leds/leds-lp55xx-common.c | 10 ++++++++++
drivers/leds/leds-lp55xx-common.h | 1 +
drivers/leds/leds-lp8501.c | 10 +---------
4 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 3030a4495808..4a4463cb44a4 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -167,20 +167,12 @@ static void lp5523_stop_engine(struct lp55xx_chip *chip)
lp5523_wait_opmode_done();
}

-static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
-{
- int i;
-
- for (i = 0; i < LP5523_MAX_LEDS; i++)
- lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
-}
-
static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
{
/* stop engine */
if (!start) {
lp5523_stop_engine(chip);
- lp5523_turn_off_channels(chip);
+ lp55xx_turn_off_channels(chip);
return;
}

diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 0d9c2990c335..028a485d3561 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -288,6 +288,16 @@ void lp55xx_set_led_current(struct lp55xx_led *led, u8 led_current)
}
EXPORT_SYMBOL_GPL(lp55xx_set_led_current);

+void lp55xx_turn_off_channels(struct lp55xx_chip *chip)
+{
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ int i;
+
+ for (i = 0; i < cfg->max_channel; i++)
+ lp55xx_write(chip, cfg->reg_led_pwm_base.addr + i, 0);
+}
+EXPORT_SYMBOL_GPL(lp55xx_turn_off_channels);
+
static void lp55xx_reset_device(struct lp55xx_chip *chip)
{
const struct lp55xx_device_config *cfg = chip->cfg;
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index e638049d9297..531fbb0acb2e 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -220,6 +220,7 @@ extern void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip);
extern int lp55xx_led_brightness(struct lp55xx_led *led);
extern int lp55xx_multicolor_brightness(struct lp55xx_led *led);
extern void lp55xx_set_led_current(struct lp55xx_led *led, u8 led_current);
+extern void lp55xx_turn_off_channels(struct lp55xx_chip *chip);

/* common probe/remove function */
extern int lp55xx_probe(struct i2c_client *client);
diff --git a/drivers/leds/leds-lp8501.c b/drivers/leds/leds-lp8501.c
index d924572e4533..1fb876f64cb7 100644
--- a/drivers/leds/leds-lp8501.c
+++ b/drivers/leds/leds-lp8501.c
@@ -110,20 +110,12 @@ static int lp8501_post_init_device(struct lp55xx_chip *chip)
LP8501_PWR_CONFIG_M, chip->pdata->pwr_sel);
}

-static void lp8501_turn_off_channels(struct lp55xx_chip *chip)
-{
- int i;
-
- for (i = 0; i < LP8501_MAX_LEDS; i++)
- lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + i, 0);
-}
-
static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
{
/* stop engine */
if (!start) {
lp55xx_stop_all_engine(chip);
- lp8501_turn_off_channels(chip);
+ lp55xx_turn_off_channels(chip);
return;
}

--
2.43.0


2024-06-15 23:16:12

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 14/20] leds: leds-lp55xx: generalize stop_engine function

Generalize stop_engine function as the implementation is the same for
most of the lp55xx based LED driver.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5521.c | 20 +++-----------------
drivers/leds/leds-lp5523.c | 20 +++-----------------
drivers/leds/leds-lp55xx-common.c | 13 +++++++++++++
drivers/leds/leds-lp55xx-common.h | 1 +
4 files changed, 20 insertions(+), 34 deletions(-)

diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index a1a3bf0ff703..4afae0c70d19 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -108,27 +108,13 @@ static inline void lp5521_wait_enable_done(void)
usleep_range(500, 600);
}

-static void lp5521_stop_engine(struct lp55xx_chip *chip)
-{
- enum lp55xx_engine_index idx = chip->engine_idx;
- static const u8 mask[] = {
- [LP55XX_ENGINE_1] = LP5521_MODE_R_M,
- [LP55XX_ENGINE_2] = LP5521_MODE_G_M,
- [LP55XX_ENGINE_3] = LP5521_MODE_B_M,
- };
-
- lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], 0);
-
- lp5521_wait_opmode_done();
-}
-
static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
{
int ret;

/* stop engine */
if (!start) {
- lp5521_stop_engine(chip);
+ lp55xx_stop_engine(chip);
lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
lp5521_wait_opmode_done();
return;
@@ -253,11 +239,11 @@ static ssize_t store_engine_mode(struct device *dev,
lp5521_run_engine(chip, true);
engine->mode = LP55XX_ENGINE_RUN;
} else if (!strncmp(buf, "load", 4)) {
- lp5521_stop_engine(chip);
+ lp55xx_stop_engine(chip);
lp55xx_load_engine(chip);
engine->mode = LP55XX_ENGINE_LOAD;
} else if (!strncmp(buf, "disabled", 8)) {
- lp5521_stop_engine(chip);
+ lp55xx_stop_engine(chip);
engine->mode = LP55XX_ENGINE_DISABLED;
}

diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 4a4463cb44a4..1dd909a0fff5 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -153,25 +153,11 @@ static int lp5523_post_init_device(struct lp55xx_chip *chip)
return lp5523_init_program_engine(chip);
}

-static void lp5523_stop_engine(struct lp55xx_chip *chip)
-{
- enum lp55xx_engine_index idx = chip->engine_idx;
- static const u8 mask[] = {
- [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
- [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
- [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
- };
-
- lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], 0);
-
- lp5523_wait_opmode_done();
-}
-
static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
{
/* stop engine */
if (!start) {
- lp5523_stop_engine(chip);
+ lp55xx_stop_engine(chip);
lp55xx_turn_off_channels(chip);
return;
}
@@ -277,11 +263,11 @@ static ssize_t store_engine_mode(struct device *dev,
lp5523_run_engine(chip, true);
engine->mode = LP55XX_ENGINE_RUN;
} else if (!strncmp(buf, "load", 4)) {
- lp5523_stop_engine(chip);
+ lp55xx_stop_engine(chip);
lp55xx_load_engine(chip);
engine->mode = LP55XX_ENGINE_LOAD;
} else if (!strncmp(buf, "disabled", 8)) {
- lp5523_stop_engine(chip);
+ lp55xx_stop_engine(chip);
engine->mode = LP55XX_ENGINE_DISABLED;
}

diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 028a485d3561..46b1eb1ce775 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -298,6 +298,19 @@ void lp55xx_turn_off_channels(struct lp55xx_chip *chip)
}
EXPORT_SYMBOL_GPL(lp55xx_turn_off_channels);

+void lp55xx_stop_engine(struct lp55xx_chip *chip)
+{
+ enum lp55xx_engine_index idx = chip->engine_idx;
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ u8 mask;
+
+ mask = LP55xx_MODE_ENGn_MASK(idx, cfg->reg_op_mode.shift);
+ lp55xx_update_bits(chip, cfg->reg_op_mode.addr, mask, 0);
+
+ lp55xx_wait_opmode_done(chip);
+}
+EXPORT_SYMBOL_GPL(lp55xx_stop_engine);
+
static void lp55xx_reset_device(struct lp55xx_chip *chip)
{
const struct lp55xx_device_config *cfg = chip->cfg;
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index 531fbb0acb2e..0aba6955a3af 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -221,6 +221,7 @@ extern int lp55xx_led_brightness(struct lp55xx_led *led);
extern int lp55xx_multicolor_brightness(struct lp55xx_led *led);
extern void lp55xx_set_led_current(struct lp55xx_led *led, u8 led_current);
extern void lp55xx_turn_off_channels(struct lp55xx_chip *chip);
+extern void lp55xx_stop_engine(struct lp55xx_chip *chip);

/* common probe/remove function */
extern int lp55xx_probe(struct i2c_client *client);
--
2.43.0


2024-06-15 23:16:44

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 15/20] leds: leds-lp55xx: generalize sysfs engine_load and engine_mode

Generalize sysfs engine_load and engine_mode since their implementation
is the same acrosso some lp55xx based LED driver.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5521.c | 88 +++----------------------------
drivers/leds/leds-lp5523.c | 88 +++----------------------------
drivers/leds/leds-lp55xx-common.c | 83 ++++++++++++++++++++++++++---
drivers/leds/leds-lp55xx-common.h | 26 +++++++--
4 files changed, 112 insertions(+), 173 deletions(-)

diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index 4afae0c70d19..519e7627ac22 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -201,82 +201,6 @@ static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
return 0;
}

-static ssize_t show_engine_mode(struct device *dev,
- struct device_attribute *attr,
- char *buf, int nr)
-{
- struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
- struct lp55xx_chip *chip = led->chip;
- enum lp55xx_engine_mode mode = chip->engines[nr - 1].mode;
-
- switch (mode) {
- case LP55XX_ENGINE_RUN:
- return sprintf(buf, "run\n");
- case LP55XX_ENGINE_LOAD:
- return sprintf(buf, "load\n");
- case LP55XX_ENGINE_DISABLED:
- default:
- return sprintf(buf, "disabled\n");
- }
-}
-show_mode(1)
-show_mode(2)
-show_mode(3)
-
-static ssize_t store_engine_mode(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t len, int nr)
-{
- struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
- struct lp55xx_chip *chip = led->chip;
- struct lp55xx_engine *engine = &chip->engines[nr - 1];
-
- mutex_lock(&chip->lock);
-
- chip->engine_idx = nr;
-
- if (!strncmp(buf, "run", 3)) {
- lp5521_run_engine(chip, true);
- engine->mode = LP55XX_ENGINE_RUN;
- } else if (!strncmp(buf, "load", 4)) {
- lp55xx_stop_engine(chip);
- lp55xx_load_engine(chip);
- engine->mode = LP55XX_ENGINE_LOAD;
- } else if (!strncmp(buf, "disabled", 8)) {
- lp55xx_stop_engine(chip);
- engine->mode = LP55XX_ENGINE_DISABLED;
- }
-
- mutex_unlock(&chip->lock);
-
- return len;
-}
-store_mode(1)
-store_mode(2)
-store_mode(3)
-
-static ssize_t store_engine_load(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t len, int nr)
-{
- struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
- struct lp55xx_chip *chip = led->chip;
- int ret;
-
- mutex_lock(&chip->lock);
-
- chip->engine_idx = nr;
- lp55xx_load_engine(chip);
- ret = lp55xx_update_program_memory(chip, buf, len);
-
- mutex_unlock(&chip->lock);
-
- return ret;
-}
-store_load(1)
-store_load(2)
-store_load(3)
-
static ssize_t lp5521_selftest(struct device *dev,
struct device_attribute *attr,
char *buf)
@@ -293,12 +217,12 @@ static ssize_t lp5521_selftest(struct device *dev,
}

/* device attributes */
-static LP55XX_DEV_ATTR_RW(engine1_mode, show_engine1_mode, store_engine1_mode);
-static LP55XX_DEV_ATTR_RW(engine2_mode, show_engine2_mode, store_engine2_mode);
-static LP55XX_DEV_ATTR_RW(engine3_mode, show_engine3_mode, store_engine3_mode);
-static LP55XX_DEV_ATTR_WO(engine1_load, store_engine1_load);
-static LP55XX_DEV_ATTR_WO(engine2_load, store_engine2_load);
-static LP55XX_DEV_ATTR_WO(engine3_load, store_engine3_load);
+LP55XX_DEV_ATTR_ENGINE_MODE(1);
+LP55XX_DEV_ATTR_ENGINE_MODE(2);
+LP55XX_DEV_ATTR_ENGINE_MODE(3);
+LP55XX_DEV_ATTR_ENGINE_LOAD(1);
+LP55XX_DEV_ATTR_ENGINE_LOAD(2);
+LP55XX_DEV_ATTR_ENGINE_LOAD(3);
static LP55XX_DEV_ATTR_RO(selftest, lp5521_selftest);

static struct attribute *lp5521_attributes[] = {
diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 1dd909a0fff5..19b119a2b256 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -225,60 +225,6 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip)
return ret;
}

-static ssize_t show_engine_mode(struct device *dev,
- struct device_attribute *attr,
- char *buf, int nr)
-{
- struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
- struct lp55xx_chip *chip = led->chip;
- enum lp55xx_engine_mode mode = chip->engines[nr - 1].mode;
-
- switch (mode) {
- case LP55XX_ENGINE_RUN:
- return sprintf(buf, "run\n");
- case LP55XX_ENGINE_LOAD:
- return sprintf(buf, "load\n");
- case LP55XX_ENGINE_DISABLED:
- default:
- return sprintf(buf, "disabled\n");
- }
-}
-show_mode(1)
-show_mode(2)
-show_mode(3)
-
-static ssize_t store_engine_mode(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t len, int nr)
-{
- struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
- struct lp55xx_chip *chip = led->chip;
- struct lp55xx_engine *engine = &chip->engines[nr - 1];
-
- mutex_lock(&chip->lock);
-
- chip->engine_idx = nr;
-
- if (!strncmp(buf, "run", 3)) {
- lp5523_run_engine(chip, true);
- engine->mode = LP55XX_ENGINE_RUN;
- } else if (!strncmp(buf, "load", 4)) {
- lp55xx_stop_engine(chip);
- lp55xx_load_engine(chip);
- engine->mode = LP55XX_ENGINE_LOAD;
- } else if (!strncmp(buf, "disabled", 8)) {
- lp55xx_stop_engine(chip);
- engine->mode = LP55XX_ENGINE_DISABLED;
- }
-
- mutex_unlock(&chip->lock);
-
- return len;
-}
-store_mode(1)
-store_mode(2)
-store_mode(3)
-
static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
{
u16 tmp_mux = 0;
@@ -392,28 +338,6 @@ store_leds(1)
store_leds(2)
store_leds(3)

-static ssize_t store_engine_load(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t len, int nr)
-{
- struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
- struct lp55xx_chip *chip = led->chip;
- int ret;
-
- mutex_lock(&chip->lock);
-
- chip->engine_idx = nr;
- lp55xx_load_engine(chip);
- ret = lp55xx_update_program_memory(chip, buf, len);
-
- mutex_unlock(&chip->lock);
-
- return ret;
-}
-store_load(1)
-store_load(2)
-store_load(3)
-
static ssize_t lp5523_selftest(struct device *dev,
struct device_attribute *attr,
char *buf)
@@ -635,15 +559,15 @@ static ssize_t store_master_fader_leds(struct device *dev,
return ret;
}

-static LP55XX_DEV_ATTR_RW(engine1_mode, show_engine1_mode, store_engine1_mode);
-static LP55XX_DEV_ATTR_RW(engine2_mode, show_engine2_mode, store_engine2_mode);
-static LP55XX_DEV_ATTR_RW(engine3_mode, show_engine3_mode, store_engine3_mode);
+LP55XX_DEV_ATTR_ENGINE_MODE(1);
+LP55XX_DEV_ATTR_ENGINE_MODE(2);
+LP55XX_DEV_ATTR_ENGINE_MODE(3);
static LP55XX_DEV_ATTR_RW(engine1_leds, show_engine1_leds, store_engine1_leds);
static LP55XX_DEV_ATTR_RW(engine2_leds, show_engine2_leds, store_engine2_leds);
static LP55XX_DEV_ATTR_RW(engine3_leds, show_engine3_leds, store_engine3_leds);
-static LP55XX_DEV_ATTR_WO(engine1_load, store_engine1_load);
-static LP55XX_DEV_ATTR_WO(engine2_load, store_engine2_load);
-static LP55XX_DEV_ATTR_WO(engine3_load, store_engine3_load);
+LP55XX_DEV_ATTR_ENGINE_LOAD(1);
+LP55XX_DEV_ATTR_ENGINE_LOAD(2);
+LP55XX_DEV_ATTR_ENGINE_LOAD(3);
static LP55XX_DEV_ATTR_RO(selftest, lp5523_selftest);
static LP55XX_DEV_ATTR_RW(master_fader1, show_master_fader1,
store_master_fader1);
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 46b1eb1ce775..1a23352c9850 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -541,8 +541,8 @@ static int lp55xx_request_firmware(struct lp55xx_chip *chip)
}

static ssize_t select_engine_show(struct device *dev,
- struct device_attribute *attr,
- char *buf)
+ struct device_attribute *attr,
+ char *buf)
{
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
struct lp55xx_chip *chip = led->chip;
@@ -551,8 +551,8 @@ static ssize_t select_engine_show(struct device *dev,
}

static ssize_t select_engine_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t len)
+ struct device_attribute *attr,
+ const char *buf, size_t len)
{
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
struct lp55xx_chip *chip = led->chip;
@@ -593,8 +593,8 @@ static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
}

static ssize_t run_engine_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t len)
+ struct device_attribute *attr,
+ const char *buf, size_t len)
{
struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
struct lp55xx_chip *chip = led->chip;
@@ -620,6 +620,77 @@ static ssize_t run_engine_store(struct device *dev,
static DEVICE_ATTR_RW(select_engine);
static DEVICE_ATTR_WO(run_engine);

+ssize_t lp55xx_show_engine_mode(struct device *dev,
+ struct device_attribute *attr,
+ char *buf, int nr)
+{
+ struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
+ struct lp55xx_chip *chip = led->chip;
+ enum lp55xx_engine_mode mode = chip->engines[nr - 1].mode;
+
+ switch (mode) {
+ case LP55XX_ENGINE_RUN:
+ return sprintf(buf, "run\n");
+ case LP55XX_ENGINE_LOAD:
+ return sprintf(buf, "load\n");
+ case LP55XX_ENGINE_DISABLED:
+ default:
+ return sprintf(buf, "disabled\n");
+ }
+}
+EXPORT_SYMBOL_GPL(lp55xx_show_engine_mode);
+
+ssize_t lp55xx_store_engine_mode(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t len, int nr)
+{
+ struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
+ struct lp55xx_chip *chip = led->chip;
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ struct lp55xx_engine *engine = &chip->engines[nr - 1];
+
+ mutex_lock(&chip->lock);
+
+ chip->engine_idx = nr;
+
+ if (!strncmp(buf, "run", 3)) {
+ cfg->run_engine(chip, true);
+ engine->mode = LP55XX_ENGINE_RUN;
+ } else if (!strncmp(buf, "load", 4)) {
+ lp55xx_stop_engine(chip);
+ lp55xx_load_engine(chip);
+ engine->mode = LP55XX_ENGINE_LOAD;
+ } else if (!strncmp(buf, "disabled", 8)) {
+ lp55xx_stop_engine(chip);
+ engine->mode = LP55XX_ENGINE_DISABLED;
+ }
+
+ mutex_unlock(&chip->lock);
+
+ return len;
+}
+EXPORT_SYMBOL_GPL(lp55xx_store_engine_mode);
+
+ssize_t lp55xx_store_engine_load(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t len, int nr)
+{
+ struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
+ struct lp55xx_chip *chip = led->chip;
+ int ret;
+
+ mutex_lock(&chip->lock);
+
+ chip->engine_idx = nr;
+ lp55xx_load_engine(chip);
+ ret = lp55xx_update_program_memory(chip, buf, len);
+
+ mutex_unlock(&chip->lock);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(lp55xx_store_engine_load);
+
static struct attribute *lp55xx_engine_attributes[] = {
&dev_attr_select_engine.attr,
&dev_attr_run_engine.attr,
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index 0aba6955a3af..00d16a86b750 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -40,7 +40,7 @@ static ssize_t show_engine##nr##_mode(struct device *dev, \
struct device_attribute *attr, \
char *buf) \
{ \
- return show_engine_mode(dev, attr, buf, nr); \
+ return lp55xx_show_engine_mode(dev, attr, buf, nr); \
}

#define store_mode(nr) \
@@ -48,9 +48,14 @@ static ssize_t store_engine##nr##_mode(struct device *dev, \
struct device_attribute *attr, \
const char *buf, size_t len) \
{ \
- return store_engine_mode(dev, attr, buf, len, nr); \
+ return lp55xx_store_engine_mode(dev, attr, buf, len, nr); \
}

+#define LP55XX_DEV_ATTR_ENGINE_MODE(nr) \
+ show_mode(nr) \
+ store_mode(nr) \
+ static LP55XX_DEV_ATTR_RW(engine##nr##_mode, show_engine##nr##_mode, store_engine##nr##_mode)
+
#define show_leds(nr) \
static ssize_t show_engine##nr##_leds(struct device *dev, \
struct device_attribute *attr, \
@@ -72,9 +77,13 @@ static ssize_t store_engine##nr##_load(struct device *dev, \
struct device_attribute *attr, \
const char *buf, size_t len) \
{ \
- return store_engine_load(dev, attr, buf, len, nr); \
+ return lp55xx_store_engine_load(dev, attr, buf, len, nr); \
}

+#define LP55XX_DEV_ATTR_ENGINE_LOAD(nr) \
+ store_load(nr) \
+ static LP55XX_DEV_ATTR_WO(engine##nr##_load, store_engine##nr##_load)
+
struct lp55xx_led;
struct lp55xx_chip;

@@ -227,4 +236,15 @@ extern void lp55xx_stop_engine(struct lp55xx_chip *chip);
extern int lp55xx_probe(struct i2c_client *client);
extern void lp55xx_remove(struct i2c_client *client);

+/* common sysfs function */
+extern ssize_t lp55xx_show_engine_mode(struct device *dev,
+ struct device_attribute *attr,
+ char *buf, int nr);
+extern ssize_t lp55xx_store_engine_mode(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t len, int nr);
+extern ssize_t lp55xx_store_engine_load(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t len, int nr);
+
#endif /* _LEDS_LP55XX_COMMON_H */
--
2.43.0


2024-06-15 23:16:47

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 16/20] leds: leds-lp55xx: generalize sysfs engine_leds

Generalize sysfs engine_leds since their implementation is the same across
some lp55xx based LED driver.

While at it simplify the implementation for show_engine_leds.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5523.c | 119 +-----------------------------
drivers/leds/leds-lp55xx-common.c | 108 +++++++++++++++++++++++++++
drivers/leds/leds-lp55xx-common.h | 15 +++-
3 files changed, 124 insertions(+), 118 deletions(-)

diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 19b119a2b256..9d91c2c5a3eb 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -225,119 +225,6 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip)
return ret;
}

-static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
-{
- u16 tmp_mux = 0;
- int i;
-
- len = min_t(int, len, LP5523_MAX_LEDS);
-
- for (i = 0; i < len; i++) {
- switch (buf[i]) {
- case '1':
- tmp_mux |= (1 << i);
- break;
- case '0':
- break;
- case '\n':
- i = len;
- break;
- default:
- return -1;
- }
- }
- *mux = tmp_mux;
-
- return 0;
-}
-
-static void lp5523_mux_to_array(u16 led_mux, char *array)
-{
- int i, pos = 0;
-
- for (i = 0; i < LP5523_MAX_LEDS; i++)
- pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i));
-
- array[pos] = '\0';
-}
-
-static ssize_t show_engine_leds(struct device *dev,
- struct device_attribute *attr,
- char *buf, int nr)
-{
- struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
- struct lp55xx_chip *chip = led->chip;
- char mux[LP5523_MAX_LEDS + 1];
-
- lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux);
-
- return sprintf(buf, "%s\n", mux);
-}
-show_leds(1)
-show_leds(2)
-show_leds(3)
-
-static int lp5523_load_mux(struct lp55xx_chip *chip, u16 mux, int nr)
-{
- struct lp55xx_engine *engine = &chip->engines[nr - 1];
- int ret;
- static const u8 mux_page[] = {
- [LP55XX_ENGINE_1] = LP5523_PAGE_MUX1,
- [LP55XX_ENGINE_2] = LP5523_PAGE_MUX2,
- [LP55XX_ENGINE_3] = LP5523_PAGE_MUX3,
- };
-
- lp55xx_load_engine(chip);
-
- ret = lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, mux_page[nr]);
- if (ret)
- return ret;
-
- ret = lp55xx_write(chip, LP5523_REG_PROG_MEM, (u8)(mux >> 8));
- if (ret)
- return ret;
-
- ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + 1, (u8)(mux));
- if (ret)
- return ret;
-
- engine->led_mux = mux;
- return 0;
-}
-
-static ssize_t store_engine_leds(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t len, int nr)
-{
- struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
- struct lp55xx_chip *chip = led->chip;
- struct lp55xx_engine *engine = &chip->engines[nr - 1];
- u16 mux = 0;
- ssize_t ret;
-
- if (lp5523_mux_parse(buf, &mux, len))
- return -EINVAL;
-
- mutex_lock(&chip->lock);
-
- chip->engine_idx = nr;
- ret = -EINVAL;
-
- if (engine->mode != LP55XX_ENGINE_LOAD)
- goto leave;
-
- if (lp5523_load_mux(chip, mux, nr))
- goto leave;
-
- ret = len;
-leave:
- mutex_unlock(&chip->lock);
- return ret;
-}
-store_leds(1)
-store_leds(2)
-store_leds(3)
-
static ssize_t lp5523_selftest(struct device *dev,
struct device_attribute *attr,
char *buf)
@@ -562,9 +449,9 @@ static ssize_t store_master_fader_leds(struct device *dev,
LP55XX_DEV_ATTR_ENGINE_MODE(1);
LP55XX_DEV_ATTR_ENGINE_MODE(2);
LP55XX_DEV_ATTR_ENGINE_MODE(3);
-static LP55XX_DEV_ATTR_RW(engine1_leds, show_engine1_leds, store_engine1_leds);
-static LP55XX_DEV_ATTR_RW(engine2_leds, show_engine2_leds, store_engine2_leds);
-static LP55XX_DEV_ATTR_RW(engine3_leds, show_engine3_leds, store_engine3_leds);
+LP55XX_DEV_ATTR_ENGINE_LEDS(1);
+LP55XX_DEV_ATTR_ENGINE_LEDS(2);
+LP55XX_DEV_ATTR_ENGINE_LEDS(3);
LP55XX_DEV_ATTR_ENGINE_LOAD(1);
LP55XX_DEV_ATTR_ENGINE_LOAD(2);
LP55XX_DEV_ATTR_ENGINE_LOAD(3);
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 1a23352c9850..687693bed868 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -61,6 +61,8 @@
/* If supported, each ENGINE have an equal amount of pages offset from page 0 */
#define LP55xx_PAGE_OFFSET(n, pages) (((n) - 1) * (pages))

+#define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led)))
+
/* External clock rate */
#define LP55XX_CLK_32K 32768

@@ -691,6 +693,112 @@ ssize_t lp55xx_store_engine_load(struct device *dev,
}
EXPORT_SYMBOL_GPL(lp55xx_store_engine_load);

+static int lp55xx_mux_parse(struct lp55xx_chip *chip, const char *buf,
+ u16 *mux, size_t len)
+{
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ u16 tmp_mux = 0;
+ int i;
+
+ len = min_t(int, len, cfg->max_channel);
+
+ for (i = 0; i < len; i++) {
+ switch (buf[i]) {
+ case '1':
+ tmp_mux |= (1 << i);
+ break;
+ case '0':
+ break;
+ case '\n':
+ i = len;
+ break;
+ default:
+ return -1;
+ }
+ }
+ *mux = tmp_mux;
+
+ return 0;
+}
+
+ssize_t lp55xx_show_engine_leds(struct device *dev,
+ struct device_attribute *attr,
+ char *buf, int nr)
+{
+ struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
+ struct lp55xx_chip *chip = led->chip;
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ int i, pos = 0;
+
+ for (i = 0; i < cfg->max_channel; i++)
+ pos += sysfs_emit_at(buf, pos, "%x",
+ LED_ACTIVE(chip->engines[nr - 1].led_mux,
+ i));
+
+ pos += sysfs_emit_at(buf, pos, "\n");
+
+ return pos;
+}
+EXPORT_SYMBOL_GPL(lp55xx_show_engine_leds);
+
+static int lp55xx_load_mux(struct lp55xx_chip *chip, u16 mux, int nr)
+{
+ struct lp55xx_engine *engine = &chip->engines[nr - 1];
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ u8 mux_page;
+ int ret;
+
+ lp55xx_load_engine(chip);
+
+ /* Derive the MUX page offset by starting at the end of the ENGINE pages */
+ mux_page = cfg->pages_per_engine * LP55XX_ENGINE_MAX + (nr - 1);
+ ret = lp55xx_write(chip, LP55xx_REG_PROG_PAGE_SEL, mux_page);
+ if (ret)
+ return ret;
+
+ ret = lp55xx_write(chip, cfg->prog_mem_base.addr, (u8)(mux >> 8));
+ if (ret)
+ return ret;
+
+ ret = lp55xx_write(chip, cfg->prog_mem_base.addr + 1, (u8)(mux));
+ if (ret)
+ return ret;
+
+ engine->led_mux = mux;
+ return 0;
+}
+
+ssize_t lp55xx_store_engine_leds(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t len, int nr)
+{
+ struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
+ struct lp55xx_chip *chip = led->chip;
+ struct lp55xx_engine *engine = &chip->engines[nr - 1];
+ u16 mux = 0;
+ ssize_t ret;
+
+ if (lp55xx_mux_parse(chip, buf, &mux, len))
+ return -EINVAL;
+
+ mutex_lock(&chip->lock);
+
+ chip->engine_idx = nr;
+ ret = -EINVAL;
+
+ if (engine->mode != LP55XX_ENGINE_LOAD)
+ goto leave;
+
+ if (lp55xx_load_mux(chip, mux, nr))
+ goto leave;
+
+ ret = len;
+leave:
+ mutex_unlock(&chip->lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(lp55xx_store_engine_leds);
+
static struct attribute *lp55xx_engine_attributes[] = {
&dev_attr_select_engine.attr,
&dev_attr_run_engine.attr,
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index 00d16a86b750..212bdb216c9a 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -61,7 +61,7 @@ static ssize_t show_engine##nr##_leds(struct device *dev, \
struct device_attribute *attr, \
char *buf) \
{ \
- return show_engine_leds(dev, attr, buf, nr); \
+ return lp55xx_show_engine_leds(dev, attr, buf, nr); \
}

#define store_leds(nr) \
@@ -69,9 +69,14 @@ static ssize_t store_engine##nr##_leds(struct device *dev, \
struct device_attribute *attr, \
const char *buf, size_t len) \
{ \
- return store_engine_leds(dev, attr, buf, len, nr); \
+ return lp55xx_store_engine_leds(dev, attr, buf, len, nr); \
}

+#define LP55XX_DEV_ATTR_ENGINE_LEDS(nr) \
+ show_leds(nr) \
+ store_leds(nr) \
+ static LP55XX_DEV_ATTR_RW(engine##nr##_leds, show_engine##nr##_leds, store_engine##nr##_leds)
+
#define store_load(nr) \
static ssize_t store_engine##nr##_load(struct device *dev, \
struct device_attribute *attr, \
@@ -246,5 +251,11 @@ extern ssize_t lp55xx_store_engine_mode(struct device *dev,
extern ssize_t lp55xx_store_engine_load(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len, int nr);
+extern ssize_t lp55xx_show_engine_leds(struct device *dev,
+ struct device_attribute *attr,
+ char *buf, int nr);
+extern ssize_t lp55xx_store_engine_leds(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t len, int nr);

#endif /* _LEDS_LP55XX_COMMON_H */
--
2.43.0


2024-06-15 23:17:01

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 17/20] leds: leds-lp55xx: generalize sysfs master_fader

Generalize sysfs master_fader since their implementation is the same across
some lp55xx based LED driver.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5523.c | 144 ++----------------------------
drivers/leds/leds-lp55xx-common.c | 119 ++++++++++++++++++++++++
drivers/leds/leds-lp55xx-common.h | 37 ++++++++
3 files changed, 161 insertions(+), 139 deletions(-)

diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 9d91c2c5a3eb..6f25a6c32869 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -315,137 +315,6 @@ static ssize_t lp5523_selftest(struct device *dev,
return pos;
}

-#define show_fader(nr) \
-static ssize_t show_master_fader##nr(struct device *dev, \
- struct device_attribute *attr, \
- char *buf) \
-{ \
- return show_master_fader(dev, attr, buf, nr); \
-}
-
-#define store_fader(nr) \
-static ssize_t store_master_fader##nr(struct device *dev, \
- struct device_attribute *attr, \
- const char *buf, size_t len) \
-{ \
- return store_master_fader(dev, attr, buf, len, nr); \
-}
-
-static ssize_t show_master_fader(struct device *dev,
- struct device_attribute *attr,
- char *buf, int nr)
-{
- struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
- struct lp55xx_chip *chip = led->chip;
- int ret;
- u8 val;
-
- mutex_lock(&chip->lock);
- ret = lp55xx_read(chip, LP5523_REG_MASTER_FADER_BASE + nr - 1, &val);
- mutex_unlock(&chip->lock);
-
- if (ret == 0)
- ret = sprintf(buf, "%u\n", val);
-
- return ret;
-}
-show_fader(1)
-show_fader(2)
-show_fader(3)
-
-static ssize_t store_master_fader(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t len, int nr)
-{
- struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
- struct lp55xx_chip *chip = led->chip;
- int ret;
- unsigned long val;
-
- if (kstrtoul(buf, 0, &val))
- return -EINVAL;
-
- if (val > 0xff)
- return -EINVAL;
-
- mutex_lock(&chip->lock);
- ret = lp55xx_write(chip, LP5523_REG_MASTER_FADER_BASE + nr - 1,
- (u8)val);
- mutex_unlock(&chip->lock);
-
- if (ret == 0)
- ret = len;
-
- return ret;
-}
-store_fader(1)
-store_fader(2)
-store_fader(3)
-
-static ssize_t show_master_fader_leds(struct device *dev,
- struct device_attribute *attr,
- char *buf)
-{
- struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
- struct lp55xx_chip *chip = led->chip;
- int i, ret, pos = 0;
- u8 val;
-
- mutex_lock(&chip->lock);
-
- for (i = 0; i < LP5523_MAX_LEDS; i++) {
- ret = lp55xx_read(chip, LP5523_REG_LED_CTRL_BASE + i, &val);
- if (ret)
- goto leave;
-
- val = (val & LP5523_FADER_MAPPING_MASK)
- >> LP5523_FADER_MAPPING_SHIFT;
- if (val > 3) {
- ret = -EINVAL;
- goto leave;
- }
- buf[pos++] = val + '0';
- }
- buf[pos++] = '\n';
- ret = pos;
-leave:
- mutex_unlock(&chip->lock);
- return ret;
-}
-
-static ssize_t store_master_fader_leds(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t len)
-{
- struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
- struct lp55xx_chip *chip = led->chip;
- int i, n, ret;
- u8 val;
-
- n = min_t(int, len, LP5523_MAX_LEDS);
-
- mutex_lock(&chip->lock);
-
- for (i = 0; i < n; i++) {
- if (buf[i] >= '0' && buf[i] <= '3') {
- val = (buf[i] - '0') << LP5523_FADER_MAPPING_SHIFT;
- ret = lp55xx_update_bits(chip,
- LP5523_REG_LED_CTRL_BASE + i,
- LP5523_FADER_MAPPING_MASK,
- val);
- if (ret)
- goto leave;
- } else {
- ret = -EINVAL;
- goto leave;
- }
- }
- ret = len;
-leave:
- mutex_unlock(&chip->lock);
- return ret;
-}
-
LP55XX_DEV_ATTR_ENGINE_MODE(1);
LP55XX_DEV_ATTR_ENGINE_MODE(2);
LP55XX_DEV_ATTR_ENGINE_MODE(3);
@@ -456,14 +325,11 @@ LP55XX_DEV_ATTR_ENGINE_LOAD(1);
LP55XX_DEV_ATTR_ENGINE_LOAD(2);
LP55XX_DEV_ATTR_ENGINE_LOAD(3);
static LP55XX_DEV_ATTR_RO(selftest, lp5523_selftest);
-static LP55XX_DEV_ATTR_RW(master_fader1, show_master_fader1,
- store_master_fader1);
-static LP55XX_DEV_ATTR_RW(master_fader2, show_master_fader2,
- store_master_fader2);
-static LP55XX_DEV_ATTR_RW(master_fader3, show_master_fader3,
- store_master_fader3);
-static LP55XX_DEV_ATTR_RW(master_fader_leds, show_master_fader_leds,
- store_master_fader_leds);
+LP55XX_DEV_ATTR_MASTER_FADER(1);
+LP55XX_DEV_ATTR_MASTER_FADER(2);
+LP55XX_DEV_ATTR_MASTER_FADER(3);
+static LP55XX_DEV_ATTR_RW(master_fader_leds, lp55xx_show_master_fader_leds,
+ lp55xx_store_master_fader_leds);

static struct attribute *lp5523_attributes[] = {
&dev_attr_engine1_mode.attr,
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 687693bed868..ac02ab938dd8 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -63,6 +63,9 @@

#define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led)))

+/* MASTER FADER common property */
+#define LP55xx_FADER_MAPPING_MASK GENMASK(7, 6)
+
/* External clock rate */
#define LP55XX_CLK_32K 32768

@@ -799,6 +802,122 @@ ssize_t lp55xx_store_engine_leds(struct device *dev,
}
EXPORT_SYMBOL_GPL(lp55xx_store_engine_leds);

+ssize_t lp55xx_show_master_fader(struct device *dev,
+ struct device_attribute *attr,
+ char *buf, int nr)
+{
+ struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
+ struct lp55xx_chip *chip = led->chip;
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ int ret;
+ u8 val;
+
+ mutex_lock(&chip->lock);
+ ret = lp55xx_read(chip, cfg->reg_master_fader_base.addr + nr - 1, &val);
+ mutex_unlock(&chip->lock);
+
+ if (ret == 0)
+ ret = sprintf(buf, "%u\n", val);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(lp55xx_show_master_fader);
+
+ssize_t lp55xx_store_master_fader(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t len, int nr)
+{
+ struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
+ struct lp55xx_chip *chip = led->chip;
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ int ret;
+ unsigned long val;
+
+ if (kstrtoul(buf, 0, &val))
+ return -EINVAL;
+
+ if (val > 0xff)
+ return -EINVAL;
+
+ mutex_lock(&chip->lock);
+ ret = lp55xx_write(chip, cfg->reg_master_fader_base.addr + nr - 1,
+ (u8)val);
+ mutex_unlock(&chip->lock);
+
+ if (ret == 0)
+ ret = len;
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(lp55xx_store_master_fader);
+
+ssize_t lp55xx_show_master_fader_leds(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
+ struct lp55xx_chip *chip = led->chip;
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ int i, ret, pos = 0;
+ u8 val;
+
+ mutex_lock(&chip->lock);
+
+ for (i = 0; i < cfg->max_channel; i++) {
+ ret = lp55xx_read(chip, cfg->reg_led_ctrl_base.addr + i, &val);
+ if (ret)
+ goto leave;
+
+ val = FIELD_GET(LP55xx_FADER_MAPPING_MASK, val);
+ if (val > FIELD_MAX(LP55xx_FADER_MAPPING_MASK)) {
+ ret = -EINVAL;
+ goto leave;
+ }
+ buf[pos++] = val + '0';
+ }
+ buf[pos++] = '\n';
+ ret = pos;
+leave:
+ mutex_unlock(&chip->lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(lp55xx_show_master_fader_leds);
+
+ssize_t lp55xx_store_master_fader_leds(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t len)
+{
+ struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
+ struct lp55xx_chip *chip = led->chip;
+ const struct lp55xx_device_config *cfg = chip->cfg;
+ int i, n, ret;
+ u8 val;
+
+ n = min_t(int, len, cfg->max_channel);
+
+ mutex_lock(&chip->lock);
+
+ for (i = 0; i < n; i++) {
+ if (buf[i] >= '0' && buf[i] <= '3') {
+ val = (buf[i] - '0') << __bf_shf(LP55xx_FADER_MAPPING_MASK);
+ ret = lp55xx_update_bits(chip,
+ cfg->reg_led_ctrl_base.addr + i,
+ LP55xx_FADER_MAPPING_MASK,
+ val);
+ if (ret)
+ goto leave;
+ } else {
+ ret = -EINVAL;
+ goto leave;
+ }
+ }
+ ret = len;
+leave:
+ mutex_unlock(&chip->lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(lp55xx_store_master_fader_leds);
+
static struct attribute *lp55xx_engine_attributes[] = {
&dev_attr_select_engine.attr,
&dev_attr_run_engine.attr,
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index 212bdb216c9a..063d7392b6a2 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -89,6 +89,27 @@ static ssize_t store_engine##nr##_load(struct device *dev, \
store_load(nr) \
static LP55XX_DEV_ATTR_WO(engine##nr##_load, store_engine##nr##_load)

+#define show_fader(nr) \
+static ssize_t show_master_fader##nr(struct device *dev, \
+ struct device_attribute *attr, \
+ char *buf) \
+{ \
+ return lp55xx_show_master_fader(dev, attr, buf, nr); \
+}
+
+#define store_fader(nr) \
+static ssize_t store_master_fader##nr(struct device *dev, \
+ struct device_attribute *attr, \
+ const char *buf, size_t len) \
+{ \
+ return lp55xx_store_master_fader(dev, attr, buf, len, nr); \
+}
+
+#define LP55XX_DEV_ATTR_MASTER_FADER(nr) \
+ show_fader(nr) \
+ store_fader(nr) \
+ static LP55XX_DEV_ATTR_RW(master_fader##nr, show_master_fader##nr, store_master_fader##nr)
+
struct lp55xx_led;
struct lp55xx_chip;

@@ -116,6 +137,8 @@ struct lp55xx_reg {
* @prog_mem_base : Chip specific base reg address for chip SMEM programming
* @reg_led_pwm_base : Chip specific base reg address for LED PWM conf
* @reg_led_current_base : Chip specific base reg address for LED current conf
+ * @reg_master_fader_base : Chip specific base reg address for master fader base
+ * @reg_led_ctrl_base : Chip specific base reg address for LED ctrl base
* @pages_per_engine : Assigned pages for each engine
* (if not set chip doesn't support pages)
* @max_channel : Maximum number of channels
@@ -136,6 +159,8 @@ struct lp55xx_device_config {
const struct lp55xx_reg prog_mem_base;
const struct lp55xx_reg reg_led_pwm_base;
const struct lp55xx_reg reg_led_current_base;
+ const struct lp55xx_reg reg_master_fader_base;
+ const struct lp55xx_reg reg_led_ctrl_base;
const int pages_per_engine;
const int max_channel;

@@ -257,5 +282,17 @@ extern ssize_t lp55xx_show_engine_leds(struct device *dev,
extern ssize_t lp55xx_store_engine_leds(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len, int nr);
+extern ssize_t lp55xx_show_master_fader(struct device *dev,
+ struct device_attribute *attr,
+ char *buf, int nr);
+extern ssize_t lp55xx_store_master_fader(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t len, int nr);
+extern ssize_t lp55xx_show_master_fader_leds(struct device *dev,
+ struct device_attribute *attr,
+ char *buf);
+extern ssize_t lp55xx_store_master_fader_leds(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t len);

#endif /* _LEDS_LP55XX_COMMON_H */
--
2.43.0


2024-06-15 23:17:16

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 19/20] leds: leds-lp55xx: drop deprecated defines

Drop deprecated defines not used anymore as the related function got
moved to lp55xx-common.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5521.c | 24 --------------------
drivers/leds/leds-lp5523.c | 46 --------------------------------------
drivers/leds/leds-lp5562.c | 15 -------------
drivers/leds/leds-lp8501.c | 31 -------------------------
4 files changed, 116 deletions(-)

diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index 519e7627ac22..de0f8ea48eba 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -21,7 +21,6 @@

#include "leds-lp55xx-common.h"

-#define LP5521_PROGRAM_LENGTH 32
#define LP5521_MAX_LEDS 3
#define LP5521_CMD_DIRECT 0x3F

@@ -73,29 +72,6 @@
/* Reset register value */
#define LP5521_RESET 0xFF

-/* Program Memory Operations */
-#define LP5521_MODE_R_M 0x30 /* Operation Mode Register */
-#define LP5521_MODE_G_M 0x0C
-#define LP5521_MODE_B_M 0x03
-#define LP5521_LOAD_R 0x10
-#define LP5521_LOAD_G 0x04
-#define LP5521_LOAD_B 0x01
-
-#define LP5521_R_IS_LOADING(mode) \
- ((mode & LP5521_MODE_R_M) == LP5521_LOAD_R)
-#define LP5521_G_IS_LOADING(mode) \
- ((mode & LP5521_MODE_G_M) == LP5521_LOAD_G)
-#define LP5521_B_IS_LOADING(mode) \
- ((mode & LP5521_MODE_B_M) == LP5521_LOAD_B)
-
-#define LP5521_EXEC_R_M 0x30 /* Enable Register */
-#define LP5521_EXEC_G_M 0x0C
-#define LP5521_EXEC_B_M 0x03
-#define LP5521_EXEC_M 0x3F
-#define LP5521_RUN_R 0x20
-#define LP5521_RUN_G 0x08
-#define LP5521_RUN_B 0x02
-
static inline void lp5521_wait_opmode_done(void)
{
/* operation mode change needs to be longer than 153 us */
diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 1d00c6cc4174..eb686240f097 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -73,54 +73,8 @@
#define LP5523_EXT_CLK_USED 0x08
#define LP5523_ENG_STATUS_MASK 0x07

-#define LP5523_FADER_MAPPING_MASK 0xC0
-#define LP5523_FADER_MAPPING_SHIFT 6
-
-/* Memory Page Selection */
-#define LP5523_PAGE_ENG1 0
-#define LP5523_PAGE_ENG2 1
-#define LP5523_PAGE_ENG3 2
-#define LP5523_PAGE_MUX1 3
-#define LP5523_PAGE_MUX2 4
-#define LP5523_PAGE_MUX3 5
-
-/* Program Memory Operations */
-#define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */
-#define LP5523_MODE_ENG2_M 0x0C
-#define LP5523_MODE_ENG3_M 0x03
-#define LP5523_LOAD_ENG1 0x10
-#define LP5523_LOAD_ENG2 0x04
-#define LP5523_LOAD_ENG3 0x01
-
-#define LP5523_ENG1_IS_LOADING(mode) \
- ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1)
-#define LP5523_ENG2_IS_LOADING(mode) \
- ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2)
-#define LP5523_ENG3_IS_LOADING(mode) \
- ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3)
-
-#define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */
-#define LP5523_EXEC_ENG2_M 0x0C
-#define LP5523_EXEC_ENG3_M 0x03
-#define LP5523_EXEC_M 0x3F
-#define LP5523_RUN_ENG1 0x20
-#define LP5523_RUN_ENG2 0x08
-#define LP5523_RUN_ENG3 0x02
-
-#define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led)))
-
-enum lp5523_chip_id {
- LP5523,
- LP55231,
-};
-
static int lp5523_init_program_engine(struct lp55xx_chip *chip);

-static inline void lp5523_wait_opmode_done(void)
-{
- usleep_range(1000, 2000);
-}
-
static int lp5523_post_init_device(struct lp55xx_chip *chip)
{
int ret;
diff --git a/drivers/leds/leds-lp5562.c b/drivers/leds/leds-lp5562.c
index 109162f1720f..6ba5dbb9cace 100644
--- a/drivers/leds/leds-lp5562.c
+++ b/drivers/leds/leds-lp5562.c
@@ -37,21 +37,6 @@

/* OPMODE Register 01h */
#define LP5562_REG_OP_MODE 0x01
-#define LP5562_MODE_ENG1_M 0x30
-#define LP5562_MODE_ENG2_M 0x0C
-#define LP5562_MODE_ENG3_M 0x03
-#define LP5562_LOAD_ENG1 0x10
-#define LP5562_LOAD_ENG2 0x04
-#define LP5562_LOAD_ENG3 0x01
-#define LP5562_RUN_ENG1 0x20
-#define LP5562_RUN_ENG2 0x08
-#define LP5562_RUN_ENG3 0x02
-#define LP5562_ENG1_IS_LOADING(mode) \
- ((mode & LP5562_MODE_ENG1_M) == LP5562_LOAD_ENG1)
-#define LP5562_ENG2_IS_LOADING(mode) \
- ((mode & LP5562_MODE_ENG2_M) == LP5562_LOAD_ENG2)
-#define LP5562_ENG3_IS_LOADING(mode) \
- ((mode & LP5562_MODE_ENG3_M) == LP5562_LOAD_ENG3)

/* BRIGHTNESS Registers */
#define LP5562_REG_R_PWM 0x04
diff --git a/drivers/leds/leds-lp8501.c b/drivers/leds/leds-lp8501.c
index 1fb876f64cb7..ee4ff4586bc0 100644
--- a/drivers/leds/leds-lp8501.c
+++ b/drivers/leds/leds-lp8501.c
@@ -20,28 +20,14 @@

#include "leds-lp55xx-common.h"

-#define LP8501_PROGRAM_LENGTH 32
#define LP8501_PAGES_PER_ENGINE 1
#define LP8501_MAX_LEDS 9

/* Registers */
#define LP8501_REG_ENABLE 0x00
#define LP8501_ENABLE BIT(6)
-#define LP8501_EXEC_M 0x3F
-#define LP8501_EXEC_ENG1_M 0x30
-#define LP8501_EXEC_ENG2_M 0x0C
-#define LP8501_EXEC_ENG3_M 0x03
-#define LP8501_RUN_ENG1 0x20
-#define LP8501_RUN_ENG2 0x08
-#define LP8501_RUN_ENG3 0x02

#define LP8501_REG_OP_MODE 0x01
-#define LP8501_MODE_ENG1_M 0x30
-#define LP8501_MODE_ENG2_M 0x0C
-#define LP8501_MODE_ENG3_M 0x03
-#define LP8501_LOAD_ENG1 0x10
-#define LP8501_LOAD_ENG2 0x04
-#define LP8501_LOAD_ENG3 0x01

#define LP8501_REG_PWR_CONFIG 0x05
#define LP8501_PWR_CONFIG_M 0x03
@@ -65,25 +51,8 @@
#define LP8501_REG_RESET 0x3D
#define LP8501_RESET 0xFF

-#define LP8501_REG_PROG_PAGE_SEL 0x4F
-#define LP8501_PAGE_ENG1 0
-#define LP8501_PAGE_ENG2 1
-#define LP8501_PAGE_ENG3 2
-
#define LP8501_REG_PROG_MEM 0x50

-#define LP8501_ENG1_IS_LOADING(mode) \
- ((mode & LP8501_MODE_ENG1_M) == LP8501_LOAD_ENG1)
-#define LP8501_ENG2_IS_LOADING(mode) \
- ((mode & LP8501_MODE_ENG2_M) == LP8501_LOAD_ENG2)
-#define LP8501_ENG3_IS_LOADING(mode) \
- ((mode & LP8501_MODE_ENG3_M) == LP8501_LOAD_ENG3)
-
-static inline void lp8501_wait_opmode_done(void)
-{
- usleep_range(1000, 2000);
-}
-
static int lp8501_post_init_device(struct lp55xx_chip *chip)
{
int ret;
--
2.43.0


2024-06-15 23:17:49

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 20/20] leds: leds-lp5569: Add support for Texas Instruments LP5569

Add support for Texas Instruments LP5569 LED driver.

Texas Instruments LP5569 is 9 channels chip with programmable engines.

It almost a copy of LP5523 with fundamental changes to regs order and
regs content.

Has difference in how the clock is handled and doesn't support detecting
clock time automatically, different handling for selftest and different
scheme for the status regs.

LED chip supports ENGINE and MUX to group LED and run precompiled code
with magic values to run patterns. This is loaded via the sysfs entry
and it's passed as a string of ASCII HEX char.

One some devices using this LED Controller (a NBG7815 Router) it was
found loading big precompiled pattern with up to 96 bytes of code. To
have support for this "extended" scenario, hardcode each engine to
support 4 pages of precompiled pattern (128 bytes of code) and 1 page
for each MUX. This gives plenty of space for any kind precompiled
pattern keeping simple logic for page handling of each engine and mux.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/Kconfig | 16 +-
drivers/leds/Makefile | 1 +
drivers/leds/leds-lp5569.c | 542 +++++++++++++++++++++++++++++++++++++
3 files changed, 556 insertions(+), 3 deletions(-)
create mode 100644 drivers/leds/leds-lp5569.c

diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index 05e6af88b88c..b1d7d94317b3 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -414,7 +414,7 @@ config LEDS_LP50XX
module will be called leds-lp50xx.

config LEDS_LP55XX_COMMON
- tristate "Common Driver for TI/National LP5521/5523/55231/5562/8501"
+ tristate "Common Driver for TI/National LP5521/5523/55231/5562/5569/8501"
depends on LEDS_CLASS
depends on LEDS_CLASS_MULTICOLOR
depends on OF
@@ -422,8 +422,8 @@ config LEDS_LP55XX_COMMON
select FW_LOADER
select FW_LOADER_USER_HELPER
help
- This option supports common operations for LP5521/5523/55231/5562/8501
- devices.
+ This option supports common operations for LP5521/5523/55231/5562/5569/
+ 8501 devices.

config LEDS_LP5521
tristate "LED Support for N.S. LP5521 LED driver chip"
@@ -456,6 +456,16 @@ config LEDS_LP5562
Driver provides direct control via LED class and interface for
programming the engines.

+config LEDS_LP5569
+ tristate "LED Support for TI LP5569 LED driver chip"
+ depends on LEDS_CLASS && I2C
+ depends on LEDS_LP55XX_COMMON
+ help
+ If you say yes here you get support for TI LP5569 LED driver.
+ It is 9 channels chip with programmable engines.
+ Driver provides direct control via LED class and interface for
+ programming the engines.
+
config LEDS_LP8501
tristate "LED Support for TI LP8501 LED driver chip"
depends on LEDS_CLASS && I2C
diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
index effdfc6f1e95..ad21941efa19 100644
--- a/drivers/leds/Makefile
+++ b/drivers/leds/Makefile
@@ -51,6 +51,7 @@ obj-$(CONFIG_LEDS_LP50XX) += leds-lp50xx.o
obj-$(CONFIG_LEDS_LP5521) += leds-lp5521.o
obj-$(CONFIG_LEDS_LP5523) += leds-lp5523.o
obj-$(CONFIG_LEDS_LP5562) += leds-lp5562.o
+obj-$(CONFIG_LEDS_LP5569) += leds-lp5569.o
obj-$(CONFIG_LEDS_LP55XX_COMMON) += leds-lp55xx-common.o
obj-$(CONFIG_LEDS_LP8501) += leds-lp8501.o
obj-$(CONFIG_LEDS_LP8788) += leds-lp8788.o
diff --git a/drivers/leds/leds-lp5569.c b/drivers/leds/leds-lp5569.c
new file mode 100644
index 000000000000..389ed307935c
--- /dev/null
+++ b/drivers/leds/leds-lp5569.c
@@ -0,0 +1,542 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2024 Christian Marangi <[email protected]>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/delay.h>
+#include <linux/firmware.h>
+#include <linux/i2c.h>
+#include <linux/leds.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/platform_data/leds-lp55xx.h>
+#include <linux/slab.h>
+#include <dt-bindings/leds/leds-lp55xx.h>
+
+#include "leds-lp55xx-common.h"
+
+#define LP5569_MAX_LEDS 9
+
+/* Memory is used like this:
+ * 0x00 engine 1 program (4 pages)
+ * 0x40 engine 2 program (4 pages)
+ * 0x80 engine 3 program (4 pages)
+ * 0xc0 engine 1 muxing info (1 page)
+ * 0xd0 engine 2 muxing info (1 page)
+ * 0xe0 engine 3 muxing info (1 page)
+ */
+#define LP5569_PAGES_PER_ENGINE 4
+
+#define LP5569_REG_ENABLE 0x00
+#define LP5569_ENABLE BIT(6)
+
+#define LP5569_REG_EXEC_CTRL 0x01
+#define LP5569_MODE_ENG_SHIFT 2
+
+#define LP5569_REG_OP_MODE 0x02
+#define LP5569_EXEC_ENG_SHIFT 2
+
+#define LP5569_REG_ENABLE_LEDS_MSB 0x04
+#define LP5569_REG_ENABLE_LEDS_LSB 0x05
+#define LP5569_REG_LED_CTRL_BASE 0x07
+#define LP5569_FADER_MAPPING_MASK GENMASK(7, 5)
+#define LP5569_REG_LED_PWM_BASE 0x16
+#define LP5569_REG_LED_CURRENT_BASE 0x22
+#define LP5569_REG_MISC 0x2F
+#define LP5569_AUTO_INC BIT(6)
+#define LP5569_PWR_SAVE BIT(5)
+#define LP5569_CP_MODE_MASK GENMASK(4, 3)
+#define LP5569_PWM_PWR_SAVE BIT(2)
+#define LP5569_INTERNAL_CLK BIT(0)
+#define LP5569_REG_MISC2 0x33
+#define LP5569_LED_SHORT_TEST BIT(4)
+#define LP5569_LED_OPEN_TEST BIT(3)
+#define LP5569_REG_STATUS 0x3C
+#define LP5569_MASK_BUSY BIT(7)
+#define LP5569_STARTUP_BUSY BIT(6)
+#define LP5569_ENGINE_BUSY BIT(5)
+#define LP5569_ENGINE1_INT BIT(2)
+#define LP5569_ENGINE2_INT BIT(1)
+#define LP5569_ENGINE3_INT BIT(0)
+#define LP5569_ENG_STATUS_MASK (LP5569_ENGINE1_INT | LP5569_ENGINE2_INT | \
+ LP5569_ENGINE3_INT)
+#define LP5569_REG_IO_CONTROL 0x3D
+#define LP5569_CLK_OUTPUT BIT(3)
+#define LP5569_REG_RESET 0x3F
+#define LP5569_RESET 0xFF
+#define LP5569_REG_MASTER_FADER_BASE 0x46
+#define LP5569_REG_CH1_PROG_START 0x4B
+#define LP5569_REG_CH2_PROG_START 0x4C
+#define LP5569_REG_CH3_PROG_START 0x4D
+#define LP5569_REG_PROG_PAGE_SEL 0x4F
+#define LP5569_REG_PROG_MEM 0x50
+#define LP5569_REG_LED_FAULT1 0x81
+#define LP5569_LED_FAULT8 BIT(0)
+#define LP5569_REG_LED_FAULT2 0x82
+#define LP5569_LED_FAULT7 BIT(7)
+#define LP5569_LED_FAULT6 BIT(6)
+#define LP5569_LED_FAULT5 BIT(5)
+#define LP5569_LED_FAULT4 BIT(4)
+#define LP5569_LED_FAULT3 BIT(3)
+#define LP5569_LED_FAULT2 BIT(2)
+#define LP5569_LED_FAULT1 BIT(1)
+#define LP5569_LED_FAULT0 BIT(0)
+
+#define LP5569_ENG1_PROG_ADDR 0x0
+#define LP5569_ENG2_PROG_ADDR 0x40
+#define LP5569_ENG3_PROG_ADDR 0x80
+#define LP5569_ENG1_MUX_ADDR 0xc0
+#define LP5569_ENG2_MUX_ADDR 0xd0
+#define LP5569_ENG3_MUX_ADDR 0xe0
+
+#define LEDn_STATUS_FAULT(n, status) ((status) >> (n) & BIT(0))
+
+#define LP5569_DEFAULT_CONFIG \
+ (LP5569_AUTO_INC | LP5569_PWR_SAVE | LP5569_PWM_PWR_SAVE)
+
+static void lp5569_run_engine(struct lp55xx_chip *chip, bool start)
+{
+ if (!start) {
+ lp55xx_stop_engine(chip);
+ lp55xx_turn_off_channels(chip);
+ return;
+ }
+
+ lp55xx_run_engine_common(chip);
+}
+
+static int lp5569_init_program_engine(struct lp55xx_chip *chip)
+{
+ int i;
+ int j;
+ int ret;
+ u8 status;
+ /* Precompiled pattern per ENGINE setting LED MUX start and stop addresses */
+ static const u8 pattern[][LP55xx_BYTES_PER_PAGE] = {
+ { 0x9c, LP5569_ENG1_MUX_ADDR, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
+ { 0x9c, LP5569_ENG2_MUX_ADDR, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
+ { 0x9c, LP5569_ENG3_MUX_ADDR, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
+ };
+
+ /* Setup each ENGINE program start address */
+ ret = lp55xx_write(chip, LP5569_REG_CH1_PROG_START, LP5569_ENG1_PROG_ADDR);
+ if (ret)
+ return ret;
+
+ ret = lp55xx_write(chip, LP5569_REG_CH2_PROG_START, LP5569_ENG2_PROG_ADDR);
+ if (ret)
+ return ret;
+
+ ret = lp55xx_write(chip, LP5569_REG_CH3_PROG_START, LP5569_ENG3_PROG_ADDR);
+ if (ret)
+ return ret;
+
+ /* Write precompiled pattern for LED MUX address space for each ENGINE */
+ for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
+ chip->engine_idx = i;
+ lp55xx_load_engine(chip);
+
+ for (j = 0; j < LP55xx_BYTES_PER_PAGE; j++) {
+ ret = lp55xx_write(chip, LP5569_REG_PROG_MEM + j,
+ pattern[i - 1][j]);
+ if (ret)
+ goto out;
+ }
+ }
+
+ lp5569_run_engine(chip, true);
+
+ /* Let the programs run for couple of ms and check the engine status */
+ usleep_range(3000, 6000);
+ lp55xx_read(chip, LP5569_REG_STATUS, &status);
+ status = FIELD_GET(LP5569_ENG_STATUS_MASK, status);
+
+ if (status != LP5569_ENG_STATUS_MASK) {
+ dev_err(&chip->cl->dev,
+ "could not configure LED engine, status = 0x%.2x\n",
+ status);
+ ret = -EINVAL;
+ }
+
+out:
+ lp55xx_stop_all_engine(chip);
+ return ret;
+}
+
+static int lp5569_post_init_device(struct lp55xx_chip *chip)
+{
+ int ret;
+ int val;
+
+ ret = lp55xx_write(chip, LP5569_REG_ENABLE, LP5569_ENABLE);
+ if (ret)
+ return ret;
+
+ /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
+ usleep_range(1000, 2000);
+
+ val = LP5569_DEFAULT_CONFIG;
+ val |= FIELD_PREP(LP5569_CP_MODE_MASK, chip->pdata->charge_pump_mode);
+
+ if (chip->pdata->clock_mode == LP55XX_CLOCK_INT) {
+ ret = lp55xx_update_bits(chip, LP5569_REG_IO_CONTROL,
+ LP5569_CLK_OUTPUT,
+ LP5569_CLK_OUTPUT);
+ if (ret)
+ return ret;
+
+ val |= LP5569_INTERNAL_CLK;
+ }
+
+ ret = lp55xx_write(chip, LP5569_REG_MISC, val);
+ if (ret)
+ return ret;
+
+ return lp5569_init_program_engine(chip);
+}
+
+static ssize_t lp5569_led_open_test(struct lp55xx_led *led, char *buf)
+{
+ struct lp55xx_chip *chip = led->chip;
+ struct lp55xx_platform_data *pdata = chip->pdata;
+ bool leds_fault[LP5569_MAX_LEDS];
+ struct lp55xx_led *led_tmp = led;
+ int i, ret, pos = 0;
+ u8 status;
+
+ /* Set in STANDBY state */
+ ret = lp55xx_write(chip, LP5569_REG_ENABLE, 0);
+ if (ret)
+ goto exit;
+
+ /* Wait 1ms for device to enter STANDBY state */
+ usleep_range(1000, 2000);
+
+ /* Set Charge Pump to 1.5x */
+ ret = lp55xx_update_bits(chip, LP5569_REG_MISC,
+ FIELD_PREP(LP5569_CP_MODE_MASK, LP55XX_CP_BOOST),
+ LP5569_CP_MODE_MASK);
+ if (ret)
+ goto exit;
+
+ /* Enable LED Open Test */
+ ret = lp55xx_update_bits(chip, LP5569_REG_MISC2, LP5569_LED_OPEN_TEST,
+ LP5569_LED_OPEN_TEST);
+ if (ret)
+ goto exit;
+
+ /* Put Device in NORMAL state */
+ ret = lp55xx_write(chip, LP5569_REG_ENABLE, LP5569_ENABLE);
+ if (ret)
+ goto exit;
+
+ /* Wait 500 us for device to enter NORMAL state */
+ usleep_range(500, 750);
+
+ /* Enable LED and set to 100% brightness */
+ for (i = 0; i < pdata->num_channels; i++) {
+ ret = lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led_tmp->chan_nr,
+ LED_FULL);
+ if (ret)
+ goto exit;
+
+ led_tmp++;
+ }
+
+ /* Wait 500 us for device to fill status regs */
+ usleep_range(500, 750);
+
+ /* Parse status led fault 1 regs */
+ ret = lp55xx_read(chip, LP5569_REG_LED_FAULT1, &status);
+ if (ret < 0)
+ goto exit;
+
+ for (i = 0; i < 8; i++)
+ leds_fault[i] = !!((status >> i) & 0x1);
+
+ /* Parse status led fault 2 regs */
+ ret = lp55xx_read(chip, LP5569_REG_LED_FAULT2, &status);
+ if (ret < 0)
+ goto exit;
+
+ for (i = 0; i < 1; i++)
+ leds_fault[i + 8] = !!((status >> i) & 0x1);
+
+ /* Report LED fault */
+ led_tmp = led;
+ for (i = 0; i < pdata->num_channels; i++) {
+ if (leds_fault[led_tmp->chan_nr])
+ pos += sprintf(buf + pos, "LED %d OPEN FAIL\n",
+ led_tmp->chan_nr);
+
+ led_tmp++;
+ }
+
+ ret = pos;
+
+exit:
+ /* Disable LED Open Test */
+ lp55xx_update_bits(chip, LP5569_REG_MISC2, LP5569_LED_OPEN_TEST,
+ 0);
+
+ led_tmp = led;
+ for (i = 0; i < pdata->num_channels; i++) {
+ lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led_tmp->chan_nr,
+ 0);
+
+ led_tmp++;
+ }
+
+ return ret;
+}
+
+static ssize_t lp5569_led_short_test(struct lp55xx_led *led, char *buf)
+{
+ struct lp55xx_chip *chip = led->chip;
+ struct lp55xx_platform_data *pdata = chip->pdata;
+ bool leds_fault[LP5569_MAX_LEDS];
+ struct lp55xx_led *led_tmp = led;
+ int i, ret, pos = 0;
+ u8 status;
+
+ /* Set in STANDBY state */
+ ret = lp55xx_write(chip, LP5569_REG_ENABLE, 0);
+ if (ret)
+ goto exit;
+
+ /* Wait 1ms for device to enter STANDBY state */
+ usleep_range(1000, 2000);
+
+ /* Set Charge Pump to 1x */
+ ret = lp55xx_update_bits(chip, LP5569_REG_MISC,
+ FIELD_PREP(LP5569_CP_MODE_MASK, LP55XX_CP_BYPASS),
+ LP5569_CP_MODE_MASK);
+ if (ret)
+ goto exit;
+
+ /* Enable LED and set to 100% brightness and current to 100% (25.5mA) */
+ for (i = 0; i < pdata->num_channels; i++) {
+ ret = lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led_tmp->chan_nr,
+ LED_FULL);
+ if (ret)
+ goto exit;
+
+ ret = lp55xx_write(chip, LP5569_REG_LED_CURRENT_BASE + led_tmp->chan_nr,
+ LED_FULL);
+ if (ret)
+ goto exit;
+
+ led_tmp++;
+ }
+
+ /* Put Device in NORMAL state */
+ ret = lp55xx_write(chip, LP5569_REG_ENABLE, LP5569_ENABLE);
+ if (ret)
+ goto exit;
+
+ /* Wait 500 us for device to enter NORMAL state */
+ usleep_range(500, 750);
+
+ /* Enable LED Shorted Test */
+ ret = lp55xx_update_bits(chip, LP5569_REG_MISC2, LP5569_LED_OPEN_TEST,
+ LP5569_LED_SHORT_TEST);
+ if (ret)
+ goto exit;
+
+ /* Wait 500 us for device to fill status regs */
+ usleep_range(500, 750);
+
+ /* Parse status led fault 1 regs */
+ ret = lp55xx_read(chip, LP5569_REG_LED_FAULT1, &status);
+ if (ret < 0)
+ goto exit;
+
+ for (i = 0; i < 8; i++)
+ leds_fault[i] = !!LEDn_STATUS_FAULT(i, status);
+
+ /* Parse status led fault 2 regs */
+ ret = lp55xx_read(chip, LP5569_REG_LED_FAULT2, &status);
+ if (ret < 0)
+ goto exit;
+
+ for (i = 0; i < 1; i++)
+ leds_fault[i + 8] = !!LEDn_STATUS_FAULT(i, status);
+
+ /* Report LED fault */
+ led_tmp = led;
+ for (i = 0; i < pdata->num_channels; i++) {
+ if (leds_fault[led_tmp->chan_nr])
+ pos += sprintf(buf + pos, "LED %d SHORTED FAIL\n",
+ led_tmp->chan_nr);
+
+ led_tmp++;
+ }
+
+ ret = pos;
+
+exit:
+ /* Disable LED Shorted Test */
+ lp55xx_update_bits(chip, LP5569_REG_MISC2, LP5569_LED_SHORT_TEST,
+ 0);
+
+ led_tmp = led;
+ for (i = 0; i < pdata->num_channels; i++) {
+ lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led_tmp->chan_nr,
+ 0);
+
+ led_tmp++;
+ }
+
+ return ret;
+}
+
+static ssize_t lp5569_selftest(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
+ struct lp55xx_chip *chip = led->chip;
+ int i, pos = 0;
+
+ mutex_lock(&chip->lock);
+
+ /* Test LED Open */
+ pos = lp5569_led_open_test(led, buf);
+ if (pos < 0)
+ goto fail;
+
+ /* Test LED Shorted */
+ pos = lp5569_led_short_test(led, buf);
+ if (pos < 0)
+ goto fail;
+
+ for (i = 0; i < chip->pdata->num_channels; i++) {
+ /* Restore current */
+ lp55xx_write(chip, LP5569_REG_LED_CURRENT_BASE + led->chan_nr,
+ led->led_current);
+
+ /* Restore brightness */
+ lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led->chan_nr,
+ led->brightness);
+ led++;
+ }
+
+ if (pos == 0)
+ pos = sprintf(buf, "OK\n");
+ goto release_lock;
+fail:
+ pos = sprintf(buf, "FAIL\n");
+
+release_lock:
+ mutex_unlock(&chip->lock);
+
+ return pos;
+}
+
+LP55XX_DEV_ATTR_ENGINE_MODE(1);
+LP55XX_DEV_ATTR_ENGINE_MODE(2);
+LP55XX_DEV_ATTR_ENGINE_MODE(3);
+LP55XX_DEV_ATTR_ENGINE_LEDS(1);
+LP55XX_DEV_ATTR_ENGINE_LEDS(2);
+LP55XX_DEV_ATTR_ENGINE_LEDS(3);
+LP55XX_DEV_ATTR_ENGINE_LOAD(1);
+LP55XX_DEV_ATTR_ENGINE_LOAD(2);
+LP55XX_DEV_ATTR_ENGINE_LOAD(3);
+static LP55XX_DEV_ATTR_RO(selftest, lp5569_selftest);
+LP55XX_DEV_ATTR_MASTER_FADER(1);
+LP55XX_DEV_ATTR_MASTER_FADER(2);
+LP55XX_DEV_ATTR_MASTER_FADER(3);
+static LP55XX_DEV_ATTR_RW(master_fader_leds, lp55xx_show_master_fader_leds,
+ lp55xx_store_master_fader_leds);
+
+static struct attribute *lp5569_attributes[] = {
+ &dev_attr_engine1_mode.attr,
+ &dev_attr_engine2_mode.attr,
+ &dev_attr_engine3_mode.attr,
+ &dev_attr_engine1_load.attr,
+ &dev_attr_engine2_load.attr,
+ &dev_attr_engine3_load.attr,
+ &dev_attr_engine1_leds.attr,
+ &dev_attr_engine2_leds.attr,
+ &dev_attr_engine3_leds.attr,
+ &dev_attr_selftest.attr,
+ &dev_attr_master_fader1.attr,
+ &dev_attr_master_fader2.attr,
+ &dev_attr_master_fader3.attr,
+ &dev_attr_master_fader_leds.attr,
+ NULL,
+};
+
+static const struct attribute_group lp5569_group = {
+ .attrs = lp5569_attributes,
+};
+
+/* Chip specific configurations */
+static struct lp55xx_device_config lp5569_cfg = {
+ .reg_op_mode = {
+ .addr = LP5569_REG_OP_MODE,
+ .shift = LP5569_MODE_ENG_SHIFT,
+ },
+ .reg_exec = {
+ .addr = LP5569_REG_EXEC_CTRL,
+ .shift = LP5569_EXEC_ENG_SHIFT,
+ },
+ .reset = {
+ .addr = LP5569_REG_RESET,
+ .val = LP5569_RESET,
+ },
+ .enable = {
+ .addr = LP5569_REG_ENABLE,
+ .val = LP5569_ENABLE,
+ },
+ .prog_mem_base = {
+ .addr = LP5569_REG_PROG_MEM,
+ },
+ .reg_led_pwm_base = {
+ .addr = LP5569_REG_LED_PWM_BASE,
+ },
+ .reg_led_current_base = {
+ .addr = LP5569_REG_LED_CURRENT_BASE,
+ },
+ .pages_per_engine = LP5569_PAGES_PER_ENGINE,
+ .max_channel = LP5569_MAX_LEDS,
+ .post_init_device = lp5569_post_init_device,
+ .brightness_fn = lp55xx_led_brightness,
+ .multicolor_brightness_fn = lp55xx_multicolor_brightness,
+ .set_led_current = lp55xx_set_led_current,
+ .firmware_cb = lp55xx_firmware_loaded_cb,
+ .run_engine = lp5569_run_engine,
+ .dev_attr_group = &lp5569_group,
+};
+
+static const struct i2c_device_id lp5569_id[] = {
+ { "lp5569", .driver_data = (kernel_ulong_t)&lp5569_cfg, },
+ { }
+};
+
+MODULE_DEVICE_TABLE(i2c, lp5569_id);
+
+static const struct of_device_id of_lp5569_leds_match[] = {
+ { .compatible = "ti,lp5569", .data = &lp5569_cfg, },
+ {},
+};
+
+MODULE_DEVICE_TABLE(of, of_lp5569_leds_match);
+
+static struct i2c_driver lp5569_driver = {
+ .driver = {
+ .name = "lp5569x",
+ .of_match_table = of_lp5569_leds_match,
+ },
+ .probe = lp55xx_probe,
+ .remove = lp55xx_remove,
+ .id_table = lp5569_id,
+};
+
+module_i2c_driver(lp5569_driver);
+
+MODULE_AUTHOR("Christian Marangi <[email protected]>");
+MODULE_DESCRIPTION("LP5569 LED engine");
+MODULE_LICENSE("GPL");
--
2.43.0


2024-06-15 23:18:05

by Christian Marangi

[permalink] [raw]
Subject: [PATCH v5 18/20] leds: leds-lp55xx: support ENGINE program up to 128 bytes

Some LED chip supports up to 16 pages and with some magic they can be
divided in 4 page for each ENGINE + 1 for each MUX. Following this we
can support bigger programs up to 128 bytes.

Rework the update_program_memory function to support program of multiple
pages instead of hardcoding it to one page per programs.

Signed-off-by: Christian Marangi <[email protected]>
---
drivers/leds/leds-lp5523.c | 5 ++-
drivers/leds/leds-lp5562.c | 7 ++--
drivers/leds/leds-lp55xx-common.c | 54 ++++++++++++++++++++++++-------
drivers/leds/leds-lp55xx-common.h | 2 ++
4 files changed, 49 insertions(+), 19 deletions(-)

diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 6f25a6c32869..1d00c6cc4174 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -21,7 +21,6 @@

#include "leds-lp55xx-common.h"

-#define LP5523_PROGRAM_LENGTH 32 /* bytes */
/* Memory is used like this:
* 0x00 engine 1 program
* 0x10 engine 2 program
@@ -172,7 +171,7 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip)
int ret;
u8 status;
/* one pattern per engine setting LED MUX start and stop addresses */
- static const u8 pattern[][LP5523_PROGRAM_LENGTH] = {
+ static const u8 pattern[][LP55xx_BYTES_PER_PAGE] = {
{ 0x9c, 0x30, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
{ 0x9c, 0x40, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
{ 0x9c, 0x50, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
@@ -196,7 +195,7 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip)
chip->engine_idx = i;
lp55xx_load_engine(chip);

- for (j = 0; j < LP5523_PROGRAM_LENGTH; j++) {
+ for (j = 0; j < LP55xx_BYTES_PER_PAGE; j++) {
ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + j,
pattern[i - 1][j]);
if (ret)
diff --git a/drivers/leds/leds-lp5562.c b/drivers/leds/leds-lp5562.c
index e50b68c9ccf3..109162f1720f 100644
--- a/drivers/leds/leds-lp5562.c
+++ b/drivers/leds/leds-lp5562.c
@@ -19,7 +19,6 @@

#include "leds-lp55xx-common.h"

-#define LP5562_PROGRAM_LENGTH 32
#define LP5562_MAX_LEDS 4

/* ENABLE Register 00h */
@@ -212,9 +211,9 @@ static void lp5562_write_program_memory(struct lp55xx_chip *chip,
/* check the size of program count */
static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn)
{
- return ptn->size_r >= LP5562_PROGRAM_LENGTH ||
- ptn->size_g >= LP5562_PROGRAM_LENGTH ||
- ptn->size_b >= LP5562_PROGRAM_LENGTH;
+ return ptn->size_r >= LP55xx_BYTES_PER_PAGE ||
+ ptn->size_g >= LP55xx_BYTES_PER_PAGE ||
+ ptn->size_b >= LP55xx_BYTES_PER_PAGE;
}

static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index ac02ab938dd8..9b03d6596c24 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -26,7 +26,8 @@
/* OP MODE require at least 153 us to clear regs */
#define LP55XX_CMD_SLEEP 200

-#define LP55xx_PROGRAM_LENGTH 32
+#define LP55xx_PROGRAM_PAGES 16
+#define LP55xx_MAX_PROGRAM_LENGTH (LP55xx_BYTES_PER_PAGE * 4) /* 128 bytes (4 pages) */

/*
* Program Memory Operations
@@ -172,12 +173,16 @@ int lp55xx_update_program_memory(struct lp55xx_chip *chip,
{
enum lp55xx_engine_index idx = chip->engine_idx;
const struct lp55xx_device_config *cfg = chip->cfg;
- u8 pattern[LP55xx_PROGRAM_LENGTH] = { };
+ u8 pattern[LP55xx_MAX_PROGRAM_LENGTH] = { };
u8 start_addr = cfg->prog_mem_base.addr;
- int i = 0, offset = 0;
- int ret;
+ int page, i = 0, offset = 0;
+ int program_length, ret;
+
+ program_length = LP55xx_BYTES_PER_PAGE;
+ if (cfg->pages_per_engine)
+ program_length *= cfg->pages_per_engine;

- while ((offset < size - 1) && (i < LP55xx_PROGRAM_LENGTH)) {
+ while ((offset < size - 1) && (i < program_length)) {
unsigned int cmd;
int nrchars;
char c[3];
@@ -206,12 +211,20 @@ int lp55xx_update_program_memory(struct lp55xx_chip *chip,
* For LED chip that support page, PAGE is already set in load_engine.
*/
if (!cfg->pages_per_engine)
- start_addr += LP55xx_PROGRAM_LENGTH * idx;
+ start_addr += LP55xx_BYTES_PER_PAGE * idx;

- for (i = 0; i < LP55xx_PROGRAM_LENGTH; i++) {
- ret = lp55xx_write(chip, start_addr + i, pattern[i]);
- if (ret)
- return -EINVAL;
+ for (page = 0; page < program_length / LP55xx_BYTES_PER_PAGE; page++) {
+ /* Write to the next page each 32 bytes (if supported) */
+ if (cfg->pages_per_engine)
+ lp55xx_write(chip, LP55xx_REG_PROG_PAGE_SEL,
+ LP55xx_PAGE_OFFSET(idx, cfg->pages_per_engine) + page);
+
+ for (i = 0; i < LP55xx_BYTES_PER_PAGE; i++) {
+ ret = lp55xx_write(chip, start_addr + i,
+ pattern[i + (page * LP55xx_BYTES_PER_PAGE)]);
+ if (ret)
+ return -EINVAL;
+ }
}

return size;
@@ -224,13 +237,19 @@ EXPORT_SYMBOL_GPL(lp55xx_update_program_memory);

void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip)
{
+ const struct lp55xx_device_config *cfg = chip->cfg;
const struct firmware *fw = chip->fw;
+ int program_length;
+
+ program_length = LP55xx_BYTES_PER_PAGE;
+ if (cfg->pages_per_engine)
+ program_length *= cfg->pages_per_engine;

/*
* the firmware is encoded in ascii hex character, with 2 chars
* per byte
*/
- if (fw->size > LP55xx_PROGRAM_LENGTH * 2) {
+ if (fw->size > program_length * 2) {
dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
fw->size);
return;
@@ -1281,7 +1300,7 @@ static struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
int lp55xx_probe(struct i2c_client *client)
{
const struct i2c_device_id *id = i2c_client_get_device_id(client);
- int ret;
+ int program_length, ret;
struct lp55xx_chip *chip;
struct lp55xx_led *led;
struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
@@ -1305,6 +1324,17 @@ int lp55xx_probe(struct i2c_client *client)
}
}

+ /* Validate max program page */
+ program_length = LP55xx_BYTES_PER_PAGE;
+ if (chip->cfg->pages_per_engine)
+ program_length *= chip->cfg->pages_per_engine;
+
+ /* support a max of 128bytes */
+ if (program_length > LP55xx_MAX_PROGRAM_LENGTH) {
+ dev_err(&client->dev, "invalid pages_per_engine configured\n");
+ return -EINVAL;
+ }
+
led = devm_kcalloc(&client->dev,
pdata->num_channels, sizeof(*led), GFP_KERNEL);
if (!led)
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
index 063d7392b6a2..5e1f7fea5985 100644
--- a/drivers/leds/leds-lp55xx-common.h
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -14,6 +14,8 @@

#include <linux/led-class-multicolor.h>

+#define LP55xx_BYTES_PER_PAGE 32 /* bytes */
+
enum lp55xx_engine_index {
LP55XX_ENGINE_INVALID,
LP55XX_ENGINE_1,
--
2.43.0


2024-06-16 04:38:31

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v5 05/20] leds: leds-lp55xx: generalize load_engine function

Hi Christian,

kernel test robot noticed the following build errors:

[auto build test ERROR on robh/for-next]
[also build test ERROR on linus/master v6.10-rc3]
[cannot apply to lee-leds/for-leds-next pavel-leds/for-next next-20240613]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Christian-Marangi/dt-bindings-leds-lp55xx-limit-pwr-sel-property-to-ti-lp8501/20240616-071915
base: https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next
patch link: https://lore.kernel.org/r/20240615231152.28201-6-ansuelsmth%40gmail.com
patch subject: [PATCH v5 05/20] leds: leds-lp55xx: generalize load_engine function
config: i386-buildonly-randconfig-001-20240616 (https://download.01.org/0day-ci/archive/20240616/[email protected]/config)
compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240616/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/leds/leds-lp55xx-common.c:103:8: error: call to undeclared function 'FIELD_PREP_CONST'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
103 | val = LP55xx_MODE_LOAD_ENG << LP55xx_MODE_ENGn_SHIFT(idx, cfg->reg_op_mode.shift);
| ^
drivers/leds/leds-lp55xx-common.c:39:40: note: expanded from macro 'LP55xx_MODE_LOAD_ENG'
39 | #define LP55xx_MODE_LOAD_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x1)
| ^
1 error generated.


vim +/FIELD_PREP_CONST +103 drivers/leds/leds-lp55xx-common.c

95
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

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

2024-06-16 04:51:29

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v5 03/20] leds: leds-lp55xx: generalize stop_all_engine OP

Hi Christian,

kernel test robot noticed the following build warnings:

[auto build test WARNING on robh/for-next]
[also build test WARNING on linus/master v6.10-rc3 next-20240613]
[cannot apply to lee-leds/for-leds-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Christian-Marangi/dt-bindings-leds-lp55xx-limit-pwr-sel-property-to-ti-lp8501/20240616-071915
base: https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next
patch link: https://lore.kernel.org/r/20240615231152.28201-4-ansuelsmth%40gmail.com
patch subject: [PATCH v5 03/20] leds: leds-lp55xx: generalize stop_all_engine OP
config: i386-buildonly-randconfig-004-20240616 (https://download.01.org/0day-ci/archive/20240616/[email protected]/config)
compiler: gcc-8 (Ubuntu 8.4.0-3ubuntu2) 8.4.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240616/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

drivers/leds/leds-lp55xx-common.c: In function 'lp55xx_wait_opmode_done':
>> drivers/leds/leds-lp55xx-common.c:53:6: warning: variable 'ret' set but not used [-Wunused-but-set-variable]
int ret;
^~~


vim +/ret +53 drivers/leds/leds-lp55xx-common.c

49
50 static void lp55xx_wait_opmode_done(struct lp55xx_chip *chip)
51 {
52 struct lp55xx_device_config *cfg = chip->cfg;
> 53 int 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

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

2024-06-16 05:42:31

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v5 05/20] leds: leds-lp55xx: generalize load_engine function

Hi Christian,

kernel test robot noticed the following build errors:

[auto build test ERROR on robh/for-next]
[also build test ERROR on linus/master v6.10-rc3]
[cannot apply to lee-leds/for-leds-next pavel-leds/for-next next-20240613]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Christian-Marangi/dt-bindings-leds-lp55xx-limit-pwr-sel-property-to-ti-lp8501/20240616-071915
base: https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next
patch link: https://lore.kernel.org/r/20240615231152.28201-6-ansuelsmth%40gmail.com
patch subject: [PATCH v5 05/20] leds: leds-lp55xx: generalize load_engine function
config: arc-randconfig-002-20240616 (https://download.01.org/0day-ci/archive/20240616/[email protected]/config)
compiler: arc-elf-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240616/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/leds/leds-lp55xx-common.c: In function 'lp55xx_wait_opmode_done':
drivers/leds/leds-lp55xx-common.c:71:13: warning: variable 'ret' set but not used [-Wunused-but-set-variable]
71 | int ret;
| ^~~
drivers/leds/leds-lp55xx-common.c: In function 'lp55xx_load_engine':
>> drivers/leds/leds-lp55xx-common.c:39:40: error: implicit declaration of function 'FIELD_PREP_CONST' [-Werror=implicit-function-declaration]
39 | #define LP55xx_MODE_LOAD_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x1)
| ^~~~~~~~~~~~~~~~
drivers/leds/leds-lp55xx-common.c:103:15: note: in expansion of macro 'LP55xx_MODE_LOAD_ENG'
103 | val = LP55xx_MODE_LOAD_ENG << LP55xx_MODE_ENGn_SHIFT(idx, cfg->reg_op_mode.shift);
| ^~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for REGMAP_SPI
Depends on [n]: SPI [=n]
Selected by [y]:
- AD9739A [=y] && IIO [=y] && (SPI [=n] || COMPILE_TEST [=y])


vim +/FIELD_PREP_CONST +39 drivers/leds/leds-lp55xx-common.c

28
29 /*
30 * Program Memory Operations
31 * Same Mask for each engine for both mode and exec
32 * ENG1 GENMASK(3, 2)
33 * ENG2 GENMASK(5, 4)
34 * ENG3 GENMASK(7, 6)
35 */
36 #define LP55xx_MODE_DISABLE_ALL_ENG 0x0
37 #define LP55xx_MODE_ENG_MASK GENMASK(1, 0)
38 #define LP55xx_MODE_DISABLE_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x0)
> 39 #define LP55xx_MODE_LOAD_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x1)
40 #define LP55xx_MODE_RUN_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x2)
41 #define LP55xx_MODE_HALT_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x3)
42

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki