Skip to content

Commit b9d5508

Browse files
Ansuellag-linaro
authored andcommitted
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]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Lee Jones <[email protected]>
1 parent 5a15b2a commit b9d5508

File tree

4 files changed

+49
-19
lines changed

4 files changed

+49
-19
lines changed

drivers/leds/leds-lp5523.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include "leds-lp55xx-common.h"
2323

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

199-
for (j = 0; j < LP5523_PROGRAM_LENGTH; j++) {
198+
for (j = 0; j < LP55xx_BYTES_PER_PAGE; j++) {
200199
ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + j,
201200
pattern[i - 1][j]);
202201
if (ret)

drivers/leds/leds-lp5562.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include "leds-lp55xx-common.h"
2121

22-
#define LP5562_PROGRAM_LENGTH 32
2322
#define LP5562_MAX_LEDS 4
2423

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

220219
static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)

drivers/leds/leds-lp55xx-common.c

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
/* OP MODE require at least 153 us to clear regs */
2828
#define LP55XX_CMD_SLEEP 200
2929

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

3233
/*
3334
* Program Memory Operations
@@ -172,12 +173,16 @@ int lp55xx_update_program_memory(struct lp55xx_chip *chip,
172173
{
173174
enum lp55xx_engine_index idx = chip->engine_idx;
174175
const struct lp55xx_device_config *cfg = chip->cfg;
175-
u8 pattern[LP55xx_PROGRAM_LENGTH] = { };
176+
u8 pattern[LP55xx_MAX_PROGRAM_LENGTH] = { };
176177
u8 start_addr = cfg->prog_mem_base.addr;
177-
int i = 0, offset = 0;
178-
int ret;
178+
int page, i = 0, offset = 0;
179+
int program_length, ret;
180+
181+
program_length = LP55xx_BYTES_PER_PAGE;
182+
if (cfg->pages_per_engine)
183+
program_length *= cfg->pages_per_engine;
179184

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

211-
for (i = 0; i < LP55xx_PROGRAM_LENGTH; i++) {
212-
ret = lp55xx_write(chip, start_addr + i, pattern[i]);
213-
if (ret)
214-
return -EINVAL;
216+
for (page = 0; page < program_length / LP55xx_BYTES_PER_PAGE; page++) {
217+
/* Write to the next page each 32 bytes (if supported) */
218+
if (cfg->pages_per_engine)
219+
lp55xx_write(chip, LP55xx_REG_PROG_PAGE_SEL,
220+
LP55xx_PAGE_OFFSET(idx, cfg->pages_per_engine) + page);
221+
222+
for (i = 0; i < LP55xx_BYTES_PER_PAGE; i++) {
223+
ret = lp55xx_write(chip, start_addr + i,
224+
pattern[i + (page * LP55xx_BYTES_PER_PAGE)]);
225+
if (ret)
226+
return -EINVAL;
227+
}
215228
}
216229

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

225238
void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip)
226239
{
240+
const struct lp55xx_device_config *cfg = chip->cfg;
227241
const struct firmware *fw = chip->fw;
242+
int program_length;
243+
244+
program_length = LP55xx_BYTES_PER_PAGE;
245+
if (cfg->pages_per_engine)
246+
program_length *= cfg->pages_per_engine;
228247

229248
/*
230249
* the firmware is encoded in ascii hex character, with 2 chars
231250
* per byte
232251
*/
233-
if (fw->size > LP55xx_PROGRAM_LENGTH * 2) {
252+
if (fw->size > program_length * 2) {
234253
dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
235254
fw->size);
236255
return;
@@ -1276,7 +1295,7 @@ static struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
12761295
int lp55xx_probe(struct i2c_client *client)
12771296
{
12781297
const struct i2c_device_id *id = i2c_client_get_device_id(client);
1279-
int ret;
1298+
int program_length, ret;
12801299
struct lp55xx_chip *chip;
12811300
struct lp55xx_led *led;
12821301
struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
@@ -1300,6 +1319,17 @@ int lp55xx_probe(struct i2c_client *client)
13001319
}
13011320
}
13021321

1322+
/* Validate max program page */
1323+
program_length = LP55xx_BYTES_PER_PAGE;
1324+
if (chip->cfg->pages_per_engine)
1325+
program_length *= chip->cfg->pages_per_engine;
1326+
1327+
/* support a max of 128bytes */
1328+
if (program_length > LP55xx_MAX_PROGRAM_LENGTH) {
1329+
dev_err(&client->dev, "invalid pages_per_engine configured\n");
1330+
return -EINVAL;
1331+
}
1332+
13031333
led = devm_kcalloc(&client->dev,
13041334
pdata->num_channels, sizeof(*led), GFP_KERNEL);
13051335
if (!led)

drivers/leds/leds-lp55xx-common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

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

17+
#define LP55xx_BYTES_PER_PAGE 32 /* bytes */
18+
1719
enum lp55xx_engine_index {
1820
LP55XX_ENGINE_INVALID,
1921
LP55XX_ENGINE_1,

0 commit comments

Comments
 (0)