Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * lp5523.c - LP5523, LP55231 LED Driver
0004  *
0005  * Copyright (C) 2010 Nokia Corporation
0006  * Copyright (C) 2012 Texas Instruments
0007  *
0008  * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
0009  *          Milo(Woogyom) Kim <milo.kim@ti.com>
0010  */
0011 
0012 #include <linux/delay.h>
0013 #include <linux/firmware.h>
0014 #include <linux/i2c.h>
0015 #include <linux/leds.h>
0016 #include <linux/module.h>
0017 #include <linux/mutex.h>
0018 #include <linux/of.h>
0019 #include <linux/platform_data/leds-lp55xx.h>
0020 #include <linux/slab.h>
0021 
0022 #include "leds-lp55xx-common.h"
0023 
0024 #define LP5523_PROGRAM_LENGTH       32  /* bytes */
0025 /* Memory is used like this:
0026  * 0x00 engine 1 program
0027  * 0x10 engine 2 program
0028  * 0x20 engine 3 program
0029  * 0x30 engine 1 muxing info
0030  * 0x40 engine 2 muxing info
0031  * 0x50 engine 3 muxing info
0032  */
0033 #define LP5523_MAX_LEDS         9
0034 
0035 /* Registers */
0036 #define LP5523_REG_ENABLE       0x00
0037 #define LP5523_REG_OP_MODE      0x01
0038 #define LP5523_REG_ENABLE_LEDS_MSB  0x04
0039 #define LP5523_REG_ENABLE_LEDS_LSB  0x05
0040 #define LP5523_REG_LED_CTRL_BASE    0x06
0041 #define LP5523_REG_LED_PWM_BASE     0x16
0042 #define LP5523_REG_LED_CURRENT_BASE 0x26
0043 #define LP5523_REG_CONFIG       0x36
0044 #define LP5523_REG_STATUS       0x3A
0045 #define LP5523_REG_RESET        0x3D
0046 #define LP5523_REG_LED_TEST_CTRL    0x41
0047 #define LP5523_REG_LED_TEST_ADC     0x42
0048 #define LP5523_REG_MASTER_FADER_BASE    0x48
0049 #define LP5523_REG_CH1_PROG_START   0x4C
0050 #define LP5523_REG_CH2_PROG_START   0x4D
0051 #define LP5523_REG_CH3_PROG_START   0x4E
0052 #define LP5523_REG_PROG_PAGE_SEL    0x4F
0053 #define LP5523_REG_PROG_MEM     0x50
0054 
0055 /* Bit description in registers */
0056 #define LP5523_ENABLE           0x40
0057 #define LP5523_AUTO_INC         0x40
0058 #define LP5523_PWR_SAVE         0x20
0059 #define LP5523_PWM_PWR_SAVE     0x04
0060 #define LP5523_CP_AUTO          0x18
0061 #define LP5523_AUTO_CLK         0x02
0062 
0063 #define LP5523_EN_LEDTEST       0x80
0064 #define LP5523_LEDTEST_DONE     0x80
0065 #define LP5523_RESET            0xFF
0066 #define LP5523_ADC_SHORTCIRC_LIM    80
0067 #define LP5523_EXT_CLK_USED     0x08
0068 #define LP5523_ENG_STATUS_MASK      0x07
0069 
0070 #define LP5523_FADER_MAPPING_MASK   0xC0
0071 #define LP5523_FADER_MAPPING_SHIFT  6
0072 
0073 /* Memory Page Selection */
0074 #define LP5523_PAGE_ENG1        0
0075 #define LP5523_PAGE_ENG2        1
0076 #define LP5523_PAGE_ENG3        2
0077 #define LP5523_PAGE_MUX1        3
0078 #define LP5523_PAGE_MUX2        4
0079 #define LP5523_PAGE_MUX3        5
0080 
0081 /* Program Memory Operations */
0082 #define LP5523_MODE_ENG1_M      0x30    /* Operation Mode Register */
0083 #define LP5523_MODE_ENG2_M      0x0C
0084 #define LP5523_MODE_ENG3_M      0x03
0085 #define LP5523_LOAD_ENG1        0x10
0086 #define LP5523_LOAD_ENG2        0x04
0087 #define LP5523_LOAD_ENG3        0x01
0088 
0089 #define LP5523_ENG1_IS_LOADING(mode)    \
0090     ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1)
0091 #define LP5523_ENG2_IS_LOADING(mode)    \
0092     ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2)
0093 #define LP5523_ENG3_IS_LOADING(mode)    \
0094     ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3)
0095 
0096 #define LP5523_EXEC_ENG1_M      0x30    /* Enable Register */
0097 #define LP5523_EXEC_ENG2_M      0x0C
0098 #define LP5523_EXEC_ENG3_M      0x03
0099 #define LP5523_EXEC_M           0x3F
0100 #define LP5523_RUN_ENG1         0x20
0101 #define LP5523_RUN_ENG2         0x08
0102 #define LP5523_RUN_ENG3         0x02
0103 
0104 #define LED_ACTIVE(mux, led)        (!!(mux & (0x0001 << led)))
0105 
0106 enum lp5523_chip_id {
0107     LP5523,
0108     LP55231,
0109 };
0110 
0111 static int lp5523_init_program_engine(struct lp55xx_chip *chip);
0112 
0113 static inline void lp5523_wait_opmode_done(void)
0114 {
0115     usleep_range(1000, 2000);
0116 }
0117 
0118 static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
0119 {
0120     led->led_current = led_current;
0121     lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
0122         led_current);
0123 }
0124 
0125 static int lp5523_post_init_device(struct lp55xx_chip *chip)
0126 {
0127     int ret;
0128 
0129     ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
0130     if (ret)
0131         return ret;
0132 
0133     /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
0134     usleep_range(1000, 2000);
0135 
0136     ret = lp55xx_write(chip, LP5523_REG_CONFIG,
0137                 LP5523_AUTO_INC | LP5523_PWR_SAVE |
0138                 LP5523_CP_AUTO | LP5523_AUTO_CLK |
0139                 LP5523_PWM_PWR_SAVE);
0140     if (ret)
0141         return ret;
0142 
0143     /* turn on all leds */
0144     ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
0145     if (ret)
0146         return ret;
0147 
0148     ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
0149     if (ret)
0150         return ret;
0151 
0152     return lp5523_init_program_engine(chip);
0153 }
0154 
0155 static void lp5523_load_engine(struct lp55xx_chip *chip)
0156 {
0157     enum lp55xx_engine_index idx = chip->engine_idx;
0158     static const u8 mask[] = {
0159         [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
0160         [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
0161         [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
0162     };
0163 
0164     static const u8 val[] = {
0165         [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
0166         [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
0167         [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
0168     };
0169 
0170     lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
0171 
0172     lp5523_wait_opmode_done();
0173 }
0174 
0175 static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
0176 {
0177     enum lp55xx_engine_index idx = chip->engine_idx;
0178     static const u8 page_sel[] = {
0179         [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1,
0180         [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2,
0181         [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
0182     };
0183 
0184     lp5523_load_engine(chip);
0185 
0186     lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
0187 }
0188 
0189 static void lp5523_stop_all_engines(struct lp55xx_chip *chip)
0190 {
0191     lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
0192     lp5523_wait_opmode_done();
0193 }
0194 
0195 static void lp5523_stop_engine(struct lp55xx_chip *chip)
0196 {
0197     enum lp55xx_engine_index idx = chip->engine_idx;
0198     static const u8 mask[] = {
0199         [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
0200         [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
0201         [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
0202     };
0203 
0204     lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], 0);
0205 
0206     lp5523_wait_opmode_done();
0207 }
0208 
0209 static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
0210 {
0211     int i;
0212 
0213     for (i = 0; i < LP5523_MAX_LEDS; i++)
0214         lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
0215 }
0216 
0217 static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
0218 {
0219     int ret;
0220     u8 mode;
0221     u8 exec;
0222 
0223     /* stop engine */
0224     if (!start) {
0225         lp5523_stop_engine(chip);
0226         lp5523_turn_off_channels(chip);
0227         return;
0228     }
0229 
0230     /*
0231      * To run the engine,
0232      * operation mode and enable register should updated at the same time
0233      */
0234 
0235     ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
0236     if (ret)
0237         return;
0238 
0239     ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
0240     if (ret)
0241         return;
0242 
0243     /* change operation mode to RUN only when each engine is loading */
0244     if (LP5523_ENG1_IS_LOADING(mode)) {
0245         mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1;
0246         exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1;
0247     }
0248 
0249     if (LP5523_ENG2_IS_LOADING(mode)) {
0250         mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2;
0251         exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2;
0252     }
0253 
0254     if (LP5523_ENG3_IS_LOADING(mode)) {
0255         mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3;
0256         exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3;
0257     }
0258 
0259     lp55xx_write(chip, LP5523_REG_OP_MODE, mode);
0260     lp5523_wait_opmode_done();
0261 
0262     lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec);
0263 }
0264 
0265 static int lp5523_init_program_engine(struct lp55xx_chip *chip)
0266 {
0267     int i;
0268     int j;
0269     int ret;
0270     u8 status;
0271     /* one pattern per engine setting LED MUX start and stop addresses */
0272     static const u8 pattern[][LP5523_PROGRAM_LENGTH] =  {
0273         { 0x9c, 0x30, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
0274         { 0x9c, 0x40, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
0275         { 0x9c, 0x50, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
0276     };
0277 
0278     /* hardcode 32 bytes of memory for each engine from program memory */
0279     ret = lp55xx_write(chip, LP5523_REG_CH1_PROG_START, 0x00);
0280     if (ret)
0281         return ret;
0282 
0283     ret = lp55xx_write(chip, LP5523_REG_CH2_PROG_START, 0x10);
0284     if (ret)
0285         return ret;
0286 
0287     ret = lp55xx_write(chip, LP5523_REG_CH3_PROG_START, 0x20);
0288     if (ret)
0289         return ret;
0290 
0291     /* write LED MUX address space for each engine */
0292     for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
0293         chip->engine_idx = i;
0294         lp5523_load_engine_and_select_page(chip);
0295 
0296         for (j = 0; j < LP5523_PROGRAM_LENGTH; j++) {
0297             ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + j,
0298                     pattern[i - 1][j]);
0299             if (ret)
0300                 goto out;
0301         }
0302     }
0303 
0304     lp5523_run_engine(chip, true);
0305 
0306     /* Let the programs run for couple of ms and check the engine status */
0307     usleep_range(3000, 6000);
0308     ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
0309     if (ret)
0310         goto out;
0311     status &= LP5523_ENG_STATUS_MASK;
0312 
0313     if (status != LP5523_ENG_STATUS_MASK) {
0314         dev_err(&chip->cl->dev,
0315             "could not configure LED engine, status = 0x%.2x\n",
0316             status);
0317         ret = -1;
0318     }
0319 
0320 out:
0321     lp5523_stop_all_engines(chip);
0322     return ret;
0323 }
0324 
0325 static int lp5523_update_program_memory(struct lp55xx_chip *chip,
0326                     const u8 *data, size_t size)
0327 {
0328     u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
0329     unsigned int cmd;
0330     char c[3];
0331     int nrchars;
0332     int ret;
0333     int offset = 0;
0334     int i = 0;
0335 
0336     while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
0337         /* separate sscanfs because length is working only for %s */
0338         ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
0339         if (ret != 1)
0340             goto err;
0341 
0342         ret = sscanf(c, "%2x", &cmd);
0343         if (ret != 1)
0344             goto err;
0345 
0346         pattern[i] = (u8)cmd;
0347         offset += nrchars;
0348         i++;
0349     }
0350 
0351     /* Each instruction is 16bit long. Check that length is even */
0352     if (i % 2)
0353         goto err;
0354 
0355     for (i = 0; i < LP5523_PROGRAM_LENGTH; i++) {
0356         ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
0357         if (ret)
0358             return -EINVAL;
0359     }
0360 
0361     return size;
0362 
0363 err:
0364     dev_err(&chip->cl->dev, "wrong pattern format\n");
0365     return -EINVAL;
0366 }
0367 
0368 static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
0369 {
0370     const struct firmware *fw = chip->fw;
0371 
0372     if (fw->size > LP5523_PROGRAM_LENGTH) {
0373         dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
0374             fw->size);
0375         return;
0376     }
0377 
0378     /*
0379      * Program memory sequence
0380      *  1) set engine mode to "LOAD"
0381      *  2) write firmware data into program memory
0382      */
0383 
0384     lp5523_load_engine_and_select_page(chip);
0385     lp5523_update_program_memory(chip, fw->data, fw->size);
0386 }
0387 
0388 static ssize_t show_engine_mode(struct device *dev,
0389                 struct device_attribute *attr,
0390                 char *buf, int nr)
0391 {
0392     struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0393     struct lp55xx_chip *chip = led->chip;
0394     enum lp55xx_engine_mode mode = chip->engines[nr - 1].mode;
0395 
0396     switch (mode) {
0397     case LP55XX_ENGINE_RUN:
0398         return sprintf(buf, "run\n");
0399     case LP55XX_ENGINE_LOAD:
0400         return sprintf(buf, "load\n");
0401     case LP55XX_ENGINE_DISABLED:
0402     default:
0403         return sprintf(buf, "disabled\n");
0404     }
0405 }
0406 show_mode(1)
0407 show_mode(2)
0408 show_mode(3)
0409 
0410 static ssize_t store_engine_mode(struct device *dev,
0411                  struct device_attribute *attr,
0412                  const char *buf, size_t len, int nr)
0413 {
0414     struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0415     struct lp55xx_chip *chip = led->chip;
0416     struct lp55xx_engine *engine = &chip->engines[nr - 1];
0417 
0418     mutex_lock(&chip->lock);
0419 
0420     chip->engine_idx = nr;
0421 
0422     if (!strncmp(buf, "run", 3)) {
0423         lp5523_run_engine(chip, true);
0424         engine->mode = LP55XX_ENGINE_RUN;
0425     } else if (!strncmp(buf, "load", 4)) {
0426         lp5523_stop_engine(chip);
0427         lp5523_load_engine(chip);
0428         engine->mode = LP55XX_ENGINE_LOAD;
0429     } else if (!strncmp(buf, "disabled", 8)) {
0430         lp5523_stop_engine(chip);
0431         engine->mode = LP55XX_ENGINE_DISABLED;
0432     }
0433 
0434     mutex_unlock(&chip->lock);
0435 
0436     return len;
0437 }
0438 store_mode(1)
0439 store_mode(2)
0440 store_mode(3)
0441 
0442 static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
0443 {
0444     u16 tmp_mux = 0;
0445     int i;
0446 
0447     len = min_t(int, len, LP5523_MAX_LEDS);
0448 
0449     for (i = 0; i < len; i++) {
0450         switch (buf[i]) {
0451         case '1':
0452             tmp_mux |= (1 << i);
0453             break;
0454         case '0':
0455             break;
0456         case '\n':
0457             i = len;
0458             break;
0459         default:
0460             return -1;
0461         }
0462     }
0463     *mux = tmp_mux;
0464 
0465     return 0;
0466 }
0467 
0468 static void lp5523_mux_to_array(u16 led_mux, char *array)
0469 {
0470     int i, pos = 0;
0471 
0472     for (i = 0; i < LP5523_MAX_LEDS; i++)
0473         pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i));
0474 
0475     array[pos] = '\0';
0476 }
0477 
0478 static ssize_t show_engine_leds(struct device *dev,
0479                 struct device_attribute *attr,
0480                 char *buf, int nr)
0481 {
0482     struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0483     struct lp55xx_chip *chip = led->chip;
0484     char mux[LP5523_MAX_LEDS + 1];
0485 
0486     lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux);
0487 
0488     return sprintf(buf, "%s\n", mux);
0489 }
0490 show_leds(1)
0491 show_leds(2)
0492 show_leds(3)
0493 
0494 static int lp5523_load_mux(struct lp55xx_chip *chip, u16 mux, int nr)
0495 {
0496     struct lp55xx_engine *engine = &chip->engines[nr - 1];
0497     int ret;
0498     static const u8 mux_page[] = {
0499         [LP55XX_ENGINE_1] = LP5523_PAGE_MUX1,
0500         [LP55XX_ENGINE_2] = LP5523_PAGE_MUX2,
0501         [LP55XX_ENGINE_3] = LP5523_PAGE_MUX3,
0502     };
0503 
0504     lp5523_load_engine(chip);
0505 
0506     ret = lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, mux_page[nr]);
0507     if (ret)
0508         return ret;
0509 
0510     ret = lp55xx_write(chip, LP5523_REG_PROG_MEM, (u8)(mux >> 8));
0511     if (ret)
0512         return ret;
0513 
0514     ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + 1, (u8)(mux));
0515     if (ret)
0516         return ret;
0517 
0518     engine->led_mux = mux;
0519     return 0;
0520 }
0521 
0522 static ssize_t store_engine_leds(struct device *dev,
0523                  struct device_attribute *attr,
0524                  const char *buf, size_t len, int nr)
0525 {
0526     struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0527     struct lp55xx_chip *chip = led->chip;
0528     struct lp55xx_engine *engine = &chip->engines[nr - 1];
0529     u16 mux = 0;
0530     ssize_t ret;
0531 
0532     if (lp5523_mux_parse(buf, &mux, len))
0533         return -EINVAL;
0534 
0535     mutex_lock(&chip->lock);
0536 
0537     chip->engine_idx = nr;
0538     ret = -EINVAL;
0539 
0540     if (engine->mode != LP55XX_ENGINE_LOAD)
0541         goto leave;
0542 
0543     if (lp5523_load_mux(chip, mux, nr))
0544         goto leave;
0545 
0546     ret = len;
0547 leave:
0548     mutex_unlock(&chip->lock);
0549     return ret;
0550 }
0551 store_leds(1)
0552 store_leds(2)
0553 store_leds(3)
0554 
0555 static ssize_t store_engine_load(struct device *dev,
0556                  struct device_attribute *attr,
0557                  const char *buf, size_t len, int nr)
0558 {
0559     struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0560     struct lp55xx_chip *chip = led->chip;
0561     int ret;
0562 
0563     mutex_lock(&chip->lock);
0564 
0565     chip->engine_idx = nr;
0566     lp5523_load_engine_and_select_page(chip);
0567     ret = lp5523_update_program_memory(chip, buf, len);
0568 
0569     mutex_unlock(&chip->lock);
0570 
0571     return ret;
0572 }
0573 store_load(1)
0574 store_load(2)
0575 store_load(3)
0576 
0577 static ssize_t lp5523_selftest(struct device *dev,
0578                    struct device_attribute *attr,
0579                    char *buf)
0580 {
0581     struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0582     struct lp55xx_chip *chip = led->chip;
0583     struct lp55xx_platform_data *pdata = chip->pdata;
0584     int i, ret, pos = 0;
0585     u8 status, adc, vdd;
0586 
0587     mutex_lock(&chip->lock);
0588 
0589     ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
0590     if (ret < 0)
0591         goto fail;
0592 
0593     /* Check that ext clock is really in use if requested */
0594     if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
0595         if  ((status & LP5523_EXT_CLK_USED) == 0)
0596             goto fail;
0597     }
0598 
0599     /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
0600     lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
0601     usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
0602     ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
0603     if (ret < 0)
0604         goto fail;
0605 
0606     if (!(status & LP5523_LEDTEST_DONE))
0607         usleep_range(3000, 6000); /* Was not ready. Wait little bit */
0608 
0609     ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
0610     if (ret < 0)
0611         goto fail;
0612 
0613     vdd--;  /* There may be some fluctuation in measurement */
0614 
0615     for (i = 0; i < LP5523_MAX_LEDS; i++) {
0616         /* Skip non-existing channels */
0617         if (pdata->led_config[i].led_current == 0)
0618             continue;
0619 
0620         /* Set default current */
0621         lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
0622             pdata->led_config[i].led_current);
0623 
0624         lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
0625         /* let current stabilize 2 - 4ms before measurements start */
0626         usleep_range(2000, 4000);
0627         lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
0628                  LP5523_EN_LEDTEST | i);
0629         /* ADC conversion time is 2.7 ms typically */
0630         usleep_range(3000, 6000);
0631         ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
0632         if (ret < 0)
0633             goto fail;
0634 
0635         if (!(status & LP5523_LEDTEST_DONE))
0636             usleep_range(3000, 6000);/* Was not ready. Wait. */
0637 
0638         ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
0639         if (ret < 0)
0640             goto fail;
0641 
0642         if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
0643             pos += sprintf(buf + pos, "LED %d FAIL\n", i);
0644 
0645         lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
0646 
0647         /* Restore current */
0648         lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
0649             led->led_current);
0650         led++;
0651     }
0652     if (pos == 0)
0653         pos = sprintf(buf, "OK\n");
0654     goto release_lock;
0655 fail:
0656     pos = sprintf(buf, "FAIL\n");
0657 
0658 release_lock:
0659     mutex_unlock(&chip->lock);
0660 
0661     return pos;
0662 }
0663 
0664 #define show_fader(nr)                      \
0665 static ssize_t show_master_fader##nr(struct device *dev,    \
0666                 struct device_attribute *attr,  \
0667                 char *buf)              \
0668 {                               \
0669     return show_master_fader(dev, attr, buf, nr);       \
0670 }
0671 
0672 #define store_fader(nr)                     \
0673 static ssize_t store_master_fader##nr(struct device *dev,   \
0674                  struct device_attribute *attr, \
0675                  const char *buf, size_t len)   \
0676 {                               \
0677     return store_master_fader(dev, attr, buf, len, nr); \
0678 }
0679 
0680 static ssize_t show_master_fader(struct device *dev,
0681                  struct device_attribute *attr,
0682                  char *buf, int nr)
0683 {
0684     struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0685     struct lp55xx_chip *chip = led->chip;
0686     int ret;
0687     u8 val;
0688 
0689     mutex_lock(&chip->lock);
0690     ret = lp55xx_read(chip, LP5523_REG_MASTER_FADER_BASE + nr - 1, &val);
0691     mutex_unlock(&chip->lock);
0692 
0693     if (ret == 0)
0694         ret = sprintf(buf, "%u\n", val);
0695 
0696     return ret;
0697 }
0698 show_fader(1)
0699 show_fader(2)
0700 show_fader(3)
0701 
0702 static ssize_t store_master_fader(struct device *dev,
0703                   struct device_attribute *attr,
0704                   const char *buf, size_t len, int nr)
0705 {
0706     struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0707     struct lp55xx_chip *chip = led->chip;
0708     int ret;
0709     unsigned long val;
0710 
0711     if (kstrtoul(buf, 0, &val))
0712         return -EINVAL;
0713 
0714     if (val > 0xff)
0715         return -EINVAL;
0716 
0717     mutex_lock(&chip->lock);
0718     ret = lp55xx_write(chip, LP5523_REG_MASTER_FADER_BASE + nr - 1,
0719                (u8)val);
0720     mutex_unlock(&chip->lock);
0721 
0722     if (ret == 0)
0723         ret = len;
0724 
0725     return ret;
0726 }
0727 store_fader(1)
0728 store_fader(2)
0729 store_fader(3)
0730 
0731 static ssize_t show_master_fader_leds(struct device *dev,
0732                       struct device_attribute *attr,
0733                       char *buf)
0734 {
0735     struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0736     struct lp55xx_chip *chip = led->chip;
0737     int i, ret, pos = 0;
0738     u8 val;
0739 
0740     mutex_lock(&chip->lock);
0741 
0742     for (i = 0; i < LP5523_MAX_LEDS; i++) {
0743         ret = lp55xx_read(chip, LP5523_REG_LED_CTRL_BASE + i, &val);
0744         if (ret)
0745             goto leave;
0746 
0747         val = (val & LP5523_FADER_MAPPING_MASK)
0748             >> LP5523_FADER_MAPPING_SHIFT;
0749         if (val > 3) {
0750             ret = -EINVAL;
0751             goto leave;
0752         }
0753         buf[pos++] = val + '0';
0754     }
0755     buf[pos++] = '\n';
0756     ret = pos;
0757 leave:
0758     mutex_unlock(&chip->lock);
0759     return ret;
0760 }
0761 
0762 static ssize_t store_master_fader_leds(struct device *dev,
0763                        struct device_attribute *attr,
0764                        const char *buf, size_t len)
0765 {
0766     struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
0767     struct lp55xx_chip *chip = led->chip;
0768     int i, n, ret;
0769     u8 val;
0770 
0771     n = min_t(int, len, LP5523_MAX_LEDS);
0772 
0773     mutex_lock(&chip->lock);
0774 
0775     for (i = 0; i < n; i++) {
0776         if (buf[i] >= '0' && buf[i] <= '3') {
0777             val = (buf[i] - '0') << LP5523_FADER_MAPPING_SHIFT;
0778             ret = lp55xx_update_bits(chip,
0779                          LP5523_REG_LED_CTRL_BASE + i,
0780                          LP5523_FADER_MAPPING_MASK,
0781                          val);
0782             if (ret)
0783                 goto leave;
0784         } else {
0785             ret = -EINVAL;
0786             goto leave;
0787         }
0788     }
0789     ret = len;
0790 leave:
0791     mutex_unlock(&chip->lock);
0792     return ret;
0793 }
0794 
0795 static int lp5523_multicolor_brightness(struct lp55xx_led *led)
0796 {
0797     struct lp55xx_chip *chip = led->chip;
0798     int ret;
0799     int i;
0800 
0801     mutex_lock(&chip->lock);
0802     for (i = 0; i < led->mc_cdev.num_colors; i++) {
0803         ret = lp55xx_write(chip,
0804                    LP5523_REG_LED_PWM_BASE +
0805                    led->mc_cdev.subled_info[i].channel,
0806                    led->mc_cdev.subled_info[i].brightness);
0807         if (ret)
0808             break;
0809     }
0810     mutex_unlock(&chip->lock);
0811     return ret;
0812 }
0813 
0814 static int lp5523_led_brightness(struct lp55xx_led *led)
0815 {
0816     struct lp55xx_chip *chip = led->chip;
0817     int ret;
0818 
0819     mutex_lock(&chip->lock);
0820     ret = lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
0821              led->brightness);
0822     mutex_unlock(&chip->lock);
0823     return ret;
0824 }
0825 
0826 static LP55XX_DEV_ATTR_RW(engine1_mode, show_engine1_mode, store_engine1_mode);
0827 static LP55XX_DEV_ATTR_RW(engine2_mode, show_engine2_mode, store_engine2_mode);
0828 static LP55XX_DEV_ATTR_RW(engine3_mode, show_engine3_mode, store_engine3_mode);
0829 static LP55XX_DEV_ATTR_RW(engine1_leds, show_engine1_leds, store_engine1_leds);
0830 static LP55XX_DEV_ATTR_RW(engine2_leds, show_engine2_leds, store_engine2_leds);
0831 static LP55XX_DEV_ATTR_RW(engine3_leds, show_engine3_leds, store_engine3_leds);
0832 static LP55XX_DEV_ATTR_WO(engine1_load, store_engine1_load);
0833 static LP55XX_DEV_ATTR_WO(engine2_load, store_engine2_load);
0834 static LP55XX_DEV_ATTR_WO(engine3_load, store_engine3_load);
0835 static LP55XX_DEV_ATTR_RO(selftest, lp5523_selftest);
0836 static LP55XX_DEV_ATTR_RW(master_fader1, show_master_fader1,
0837               store_master_fader1);
0838 static LP55XX_DEV_ATTR_RW(master_fader2, show_master_fader2,
0839               store_master_fader2);
0840 static LP55XX_DEV_ATTR_RW(master_fader3, show_master_fader3,
0841               store_master_fader3);
0842 static LP55XX_DEV_ATTR_RW(master_fader_leds, show_master_fader_leds,
0843               store_master_fader_leds);
0844 
0845 static struct attribute *lp5523_attributes[] = {
0846     &dev_attr_engine1_mode.attr,
0847     &dev_attr_engine2_mode.attr,
0848     &dev_attr_engine3_mode.attr,
0849     &dev_attr_engine1_load.attr,
0850     &dev_attr_engine2_load.attr,
0851     &dev_attr_engine3_load.attr,
0852     &dev_attr_engine1_leds.attr,
0853     &dev_attr_engine2_leds.attr,
0854     &dev_attr_engine3_leds.attr,
0855     &dev_attr_selftest.attr,
0856     &dev_attr_master_fader1.attr,
0857     &dev_attr_master_fader2.attr,
0858     &dev_attr_master_fader3.attr,
0859     &dev_attr_master_fader_leds.attr,
0860     NULL,
0861 };
0862 
0863 static const struct attribute_group lp5523_group = {
0864     .attrs = lp5523_attributes,
0865 };
0866 
0867 /* Chip specific configurations */
0868 static struct lp55xx_device_config lp5523_cfg = {
0869     .reset = {
0870         .addr = LP5523_REG_RESET,
0871         .val  = LP5523_RESET,
0872     },
0873     .enable = {
0874         .addr = LP5523_REG_ENABLE,
0875         .val  = LP5523_ENABLE,
0876     },
0877     .max_channel  = LP5523_MAX_LEDS,
0878     .post_init_device   = lp5523_post_init_device,
0879     .brightness_fn      = lp5523_led_brightness,
0880     .multicolor_brightness_fn = lp5523_multicolor_brightness,
0881     .set_led_current    = lp5523_set_led_current,
0882     .firmware_cb        = lp5523_firmware_loaded,
0883     .run_engine         = lp5523_run_engine,
0884     .dev_attr_group     = &lp5523_group,
0885 };
0886 
0887 static int lp5523_probe(struct i2c_client *client,
0888             const struct i2c_device_id *id)
0889 {
0890     int ret;
0891     struct lp55xx_chip *chip;
0892     struct lp55xx_led *led;
0893     struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
0894     struct device_node *np = dev_of_node(&client->dev);
0895 
0896     chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
0897     if (!chip)
0898         return -ENOMEM;
0899 
0900     chip->cfg = &lp5523_cfg;
0901 
0902     if (!pdata) {
0903         if (np) {
0904             pdata = lp55xx_of_populate_pdata(&client->dev, np,
0905                              chip);
0906             if (IS_ERR(pdata))
0907                 return PTR_ERR(pdata);
0908         } else {
0909             dev_err(&client->dev, "no platform data\n");
0910             return -EINVAL;
0911         }
0912     }
0913 
0914     led = devm_kcalloc(&client->dev,
0915             pdata->num_channels, sizeof(*led), GFP_KERNEL);
0916     if (!led)
0917         return -ENOMEM;
0918 
0919     chip->cl = client;
0920     chip->pdata = pdata;
0921 
0922     mutex_init(&chip->lock);
0923 
0924     i2c_set_clientdata(client, led);
0925 
0926     ret = lp55xx_init_device(chip);
0927     if (ret)
0928         goto err_init;
0929 
0930     dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
0931 
0932     ret = lp55xx_register_leds(led, chip);
0933     if (ret)
0934         goto err_out;
0935 
0936     ret = lp55xx_register_sysfs(chip);
0937     if (ret) {
0938         dev_err(&client->dev, "registering sysfs failed\n");
0939         goto err_out;
0940     }
0941 
0942     return 0;
0943 
0944 err_out:
0945     lp55xx_deinit_device(chip);
0946 err_init:
0947     return ret;
0948 }
0949 
0950 static int lp5523_remove(struct i2c_client *client)
0951 {
0952     struct lp55xx_led *led = i2c_get_clientdata(client);
0953     struct lp55xx_chip *chip = led->chip;
0954 
0955     lp5523_stop_all_engines(chip);
0956     lp55xx_unregister_sysfs(chip);
0957     lp55xx_deinit_device(chip);
0958 
0959     return 0;
0960 }
0961 
0962 static const struct i2c_device_id lp5523_id[] = {
0963     { "lp5523",  LP5523 },
0964     { "lp55231", LP55231 },
0965     { }
0966 };
0967 
0968 MODULE_DEVICE_TABLE(i2c, lp5523_id);
0969 
0970 #ifdef CONFIG_OF
0971 static const struct of_device_id of_lp5523_leds_match[] = {
0972     { .compatible = "national,lp5523", },
0973     { .compatible = "ti,lp55231", },
0974     {},
0975 };
0976 
0977 MODULE_DEVICE_TABLE(of, of_lp5523_leds_match);
0978 #endif
0979 
0980 static struct i2c_driver lp5523_driver = {
0981     .driver = {
0982         .name   = "lp5523x",
0983         .of_match_table = of_match_ptr(of_lp5523_leds_match),
0984     },
0985     .probe      = lp5523_probe,
0986     .remove     = lp5523_remove,
0987     .id_table   = lp5523_id,
0988 };
0989 
0990 module_i2c_driver(lp5523_driver);
0991 
0992 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
0993 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
0994 MODULE_DESCRIPTION("LP5523 LED engine");
0995 MODULE_LICENSE("GPL");