Commit b9d55087 authored by Christian Marangi's avatar Christian Marangi Committed by Lee Jones

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: default avatarChristian Marangi <ansuelsmth@gmail.com>
Link: https://lore.kernel.org/r/20240626160027.19703-19-ansuelsmth@gmail.comSigned-off-by: default avatarLee Jones <lee@kernel.org>
parent 5a15b2ab
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#include "leds-lp55xx-common.h" #include "leds-lp55xx-common.h"
#define LP5523_PROGRAM_LENGTH 32 /* bytes */
/* Memory is used like this: /* Memory is used like this:
* 0x00 engine 1 program * 0x00 engine 1 program
* 0x10 engine 2 program * 0x10 engine 2 program
...@@ -172,7 +171,7 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip) ...@@ -172,7 +171,7 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip)
int ret; int ret;
u8 status; u8 status;
/* one pattern per engine setting LED MUX start and stop addresses */ /* 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, 0x30, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
{ 0x9c, 0x40, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0}, { 0x9c, 0x40, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
{ 0x9c, 0x50, 0x9c, 0xd0, 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) ...@@ -196,7 +195,7 @@ static int lp5523_init_program_engine(struct lp55xx_chip *chip)
chip->engine_idx = i; chip->engine_idx = i;
lp55xx_load_engine(chip); 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, ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + j,
pattern[i - 1][j]); pattern[i - 1][j]);
if (ret) if (ret)
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "leds-lp55xx-common.h" #include "leds-lp55xx-common.h"
#define LP5562_PROGRAM_LENGTH 32
#define LP5562_MAX_LEDS 4 #define LP5562_MAX_LEDS 4
/* ENABLE Register 00h */ /* ENABLE Register 00h */
...@@ -212,9 +211,9 @@ static void lp5562_write_program_memory(struct lp55xx_chip *chip, ...@@ -212,9 +211,9 @@ static void lp5562_write_program_memory(struct lp55xx_chip *chip,
/* check the size of program count */ /* check the size of program count */
static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn) static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn)
{ {
return ptn->size_r >= LP5562_PROGRAM_LENGTH || return ptn->size_r >= LP55xx_BYTES_PER_PAGE ||
ptn->size_g >= LP5562_PROGRAM_LENGTH || ptn->size_g >= LP55xx_BYTES_PER_PAGE ||
ptn->size_b >= LP5562_PROGRAM_LENGTH; ptn->size_b >= LP55xx_BYTES_PER_PAGE;
} }
static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode) static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
......
...@@ -27,7 +27,8 @@ ...@@ -27,7 +27,8 @@
/* OP MODE require at least 153 us to clear regs */ /* OP MODE require at least 153 us to clear regs */
#define LP55XX_CMD_SLEEP 200 #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 * Program Memory Operations
...@@ -172,12 +173,16 @@ int lp55xx_update_program_memory(struct lp55xx_chip *chip, ...@@ -172,12 +173,16 @@ int lp55xx_update_program_memory(struct lp55xx_chip *chip,
{ {
enum lp55xx_engine_index idx = chip->engine_idx; enum lp55xx_engine_index idx = chip->engine_idx;
const struct lp55xx_device_config *cfg = chip->cfg; 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; u8 start_addr = cfg->prog_mem_base.addr;
int i = 0, offset = 0; int page, i = 0, offset = 0;
int ret; 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; unsigned int cmd;
int nrchars; int nrchars;
char c[3]; char c[3];
...@@ -206,12 +211,20 @@ int lp55xx_update_program_memory(struct lp55xx_chip *chip, ...@@ -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. * For LED chip that support page, PAGE is already set in load_engine.
*/ */
if (!cfg->pages_per_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++) { for (page = 0; page < program_length / LP55xx_BYTES_PER_PAGE; page++) {
ret = lp55xx_write(chip, start_addr + i, pattern[i]); /* Write to the next page each 32 bytes (if supported) */
if (ret) if (cfg->pages_per_engine)
return -EINVAL; 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; return size;
...@@ -224,13 +237,19 @@ EXPORT_SYMBOL_GPL(lp55xx_update_program_memory); ...@@ -224,13 +237,19 @@ EXPORT_SYMBOL_GPL(lp55xx_update_program_memory);
void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip) void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip)
{ {
const struct lp55xx_device_config *cfg = chip->cfg;
const struct firmware *fw = chip->fw; 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 * the firmware is encoded in ascii hex character, with 2 chars
* per byte * 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", dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
fw->size); fw->size);
return; return;
...@@ -1276,7 +1295,7 @@ static struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev, ...@@ -1276,7 +1295,7 @@ static struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
int lp55xx_probe(struct i2c_client *client) int lp55xx_probe(struct i2c_client *client)
{ {
const struct i2c_device_id *id = i2c_client_get_device_id(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_chip *chip;
struct lp55xx_led *led; struct lp55xx_led *led;
struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev); struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
...@@ -1300,6 +1319,17 @@ int lp55xx_probe(struct i2c_client *client) ...@@ -1300,6 +1319,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, led = devm_kcalloc(&client->dev,
pdata->num_channels, sizeof(*led), GFP_KERNEL); pdata->num_channels, sizeof(*led), GFP_KERNEL);
if (!led) if (!led)
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include <linux/led-class-multicolor.h> #include <linux/led-class-multicolor.h>
#define LP55xx_BYTES_PER_PAGE 32 /* bytes */
enum lp55xx_engine_index { enum lp55xx_engine_index {
LP55XX_ENGINE_INVALID, LP55XX_ENGINE_INVALID,
LP55XX_ENGINE_1, LP55XX_ENGINE_1,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment