1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * TI LP8501 9 channel LED Driver 4 * 5 * Copyright (C) 2013 Texas Instruments 6 * 7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com> 8 */ 9 10 #include <linux/delay.h> 11 #include <linux/firmware.h> 12 #include <linux/i2c.h> 13 #include <linux/init.h> 14 #include <linux/leds.h> 15 #include <linux/module.h> 16 #include <linux/mutex.h> 17 #include <linux/of.h> 18 #include <linux/platform_data/leds-lp55xx.h> 19 #include <linux/slab.h> 20 21 #include "leds-lp55xx-common.h" 22 23 #define LP8501_PROGRAM_LENGTH 32 24 #define LP8501_MAX_LEDS 9 25 26 /* Registers */ 27 #define LP8501_REG_ENABLE 0x00 28 #define LP8501_ENABLE BIT(6) 29 #define LP8501_EXEC_M 0x3F 30 #define LP8501_EXEC_ENG1_M 0x30 31 #define LP8501_EXEC_ENG2_M 0x0C 32 #define LP8501_EXEC_ENG3_M 0x03 33 #define LP8501_RUN_ENG1 0x20 34 #define LP8501_RUN_ENG2 0x08 35 #define LP8501_RUN_ENG3 0x02 36 37 #define LP8501_REG_OP_MODE 0x01 38 #define LP8501_MODE_ENG1_M 0x30 39 #define LP8501_MODE_ENG2_M 0x0C 40 #define LP8501_MODE_ENG3_M 0x03 41 #define LP8501_LOAD_ENG1 0x10 42 #define LP8501_LOAD_ENG2 0x04 43 #define LP8501_LOAD_ENG3 0x01 44 45 #define LP8501_REG_PWR_CONFIG 0x05 46 #define LP8501_PWR_CONFIG_M 0x03 47 48 #define LP8501_REG_LED_PWM_BASE 0x16 49 50 #define LP8501_REG_LED_CURRENT_BASE 0x26 51 52 #define LP8501_REG_CONFIG 0x36 53 #define LP8501_PWM_PSAVE BIT(7) 54 #define LP8501_AUTO_INC BIT(6) 55 #define LP8501_PWR_SAVE BIT(5) 56 #define LP8501_CP_MODE_MASK 0x18 57 #define LP8501_CP_MODE_SHIFT 3 58 #define LP8501_INT_CLK BIT(0) 59 #define LP8501_DEFAULT_CFG (LP8501_PWM_PSAVE | LP8501_AUTO_INC | LP8501_PWR_SAVE) 60 61 #define LP8501_REG_STATUS 0x3A 62 #define LP8501_ENGINE_BUSY BIT(4) 63 64 #define LP8501_REG_RESET 0x3D 65 #define LP8501_RESET 0xFF 66 67 #define LP8501_REG_PROG_PAGE_SEL 0x4F 68 #define LP8501_PAGE_ENG1 0 69 #define LP8501_PAGE_ENG2 1 70 #define LP8501_PAGE_ENG3 2 71 72 #define LP8501_REG_PROG_MEM 0x50 73 74 #define LP8501_ENG1_IS_LOADING(mode) \ 75 ((mode & LP8501_MODE_ENG1_M) == LP8501_LOAD_ENG1) 76 #define LP8501_ENG2_IS_LOADING(mode) \ 77 ((mode & LP8501_MODE_ENG2_M) == LP8501_LOAD_ENG2) 78 #define LP8501_ENG3_IS_LOADING(mode) \ 79 ((mode & LP8501_MODE_ENG3_M) == LP8501_LOAD_ENG3) 80 81 static inline void lp8501_wait_opmode_done(void) 82 { 83 usleep_range(1000, 2000); 84 } 85 86 static void lp8501_set_led_current(struct lp55xx_led *led, u8 led_current) 87 { 88 led->led_current = led_current; 89 lp55xx_write(led->chip, LP8501_REG_LED_CURRENT_BASE + led->chan_nr, 90 led_current); 91 } 92 93 static int lp8501_post_init_device(struct lp55xx_chip *chip) 94 { 95 int ret; 96 u8 val = LP8501_DEFAULT_CFG; 97 98 ret = lp55xx_write(chip, LP8501_REG_ENABLE, LP8501_ENABLE); 99 if (ret) 100 return ret; 101 102 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */ 103 usleep_range(1000, 2000); 104 105 if (chip->pdata->clock_mode != LP55XX_CLOCK_EXT) 106 val |= LP8501_INT_CLK; 107 108 val |= (chip->pdata->charge_pump_mode << LP8501_CP_MODE_SHIFT) & LP8501_CP_MODE_MASK; 109 110 ret = lp55xx_write(chip, LP8501_REG_CONFIG, val); 111 if (ret) 112 return ret; 113 114 /* Power selection for each output */ 115 return lp55xx_update_bits(chip, LP8501_REG_PWR_CONFIG, 116 LP8501_PWR_CONFIG_M, chip->pdata->pwr_sel); 117 } 118 119 static void lp8501_load_engine(struct lp55xx_chip *chip) 120 { 121 enum lp55xx_engine_index idx = chip->engine_idx; 122 static const u8 mask[] = { 123 [LP55XX_ENGINE_1] = LP8501_MODE_ENG1_M, 124 [LP55XX_ENGINE_2] = LP8501_MODE_ENG2_M, 125 [LP55XX_ENGINE_3] = LP8501_MODE_ENG3_M, 126 }; 127 128 static const u8 val[] = { 129 [LP55XX_ENGINE_1] = LP8501_LOAD_ENG1, 130 [LP55XX_ENGINE_2] = LP8501_LOAD_ENG2, 131 [LP55XX_ENGINE_3] = LP8501_LOAD_ENG3, 132 }; 133 134 static const u8 page_sel[] = { 135 [LP55XX_ENGINE_1] = LP8501_PAGE_ENG1, 136 [LP55XX_ENGINE_2] = LP8501_PAGE_ENG2, 137 [LP55XX_ENGINE_3] = LP8501_PAGE_ENG3, 138 }; 139 140 lp55xx_update_bits(chip, LP8501_REG_OP_MODE, mask[idx], val[idx]); 141 142 lp8501_wait_opmode_done(); 143 144 lp55xx_write(chip, LP8501_REG_PROG_PAGE_SEL, page_sel[idx]); 145 } 146 147 static void lp8501_turn_off_channels(struct lp55xx_chip *chip) 148 { 149 int i; 150 151 for (i = 0; i < LP8501_MAX_LEDS; i++) 152 lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + i, 0); 153 } 154 155 static void lp8501_run_engine(struct lp55xx_chip *chip, bool start) 156 { 157 int ret; 158 u8 mode; 159 u8 exec; 160 161 /* stop engine */ 162 if (!start) { 163 lp55xx_stop_all_engine(chip); 164 lp8501_turn_off_channels(chip); 165 return; 166 } 167 168 /* 169 * To run the engine, 170 * operation mode and enable register should updated at the same time 171 */ 172 173 ret = lp55xx_read(chip, LP8501_REG_OP_MODE, &mode); 174 if (ret) 175 return; 176 177 ret = lp55xx_read(chip, LP8501_REG_ENABLE, &exec); 178 if (ret) 179 return; 180 181 /* change operation mode to RUN only when each engine is loading */ 182 if (LP8501_ENG1_IS_LOADING(mode)) { 183 mode = (mode & ~LP8501_MODE_ENG1_M) | LP8501_RUN_ENG1; 184 exec = (exec & ~LP8501_EXEC_ENG1_M) | LP8501_RUN_ENG1; 185 } 186 187 if (LP8501_ENG2_IS_LOADING(mode)) { 188 mode = (mode & ~LP8501_MODE_ENG2_M) | LP8501_RUN_ENG2; 189 exec = (exec & ~LP8501_EXEC_ENG2_M) | LP8501_RUN_ENG2; 190 } 191 192 if (LP8501_ENG3_IS_LOADING(mode)) { 193 mode = (mode & ~LP8501_MODE_ENG3_M) | LP8501_RUN_ENG3; 194 exec = (exec & ~LP8501_EXEC_ENG3_M) | LP8501_RUN_ENG3; 195 } 196 197 lp55xx_write(chip, LP8501_REG_OP_MODE, mode); 198 lp8501_wait_opmode_done(); 199 200 lp55xx_update_bits(chip, LP8501_REG_ENABLE, LP8501_EXEC_M, exec); 201 } 202 203 static int lp8501_update_program_memory(struct lp55xx_chip *chip, 204 const u8 *data, size_t size) 205 { 206 u8 pattern[LP8501_PROGRAM_LENGTH] = {0}; 207 unsigned cmd; 208 char c[3]; 209 int update_size; 210 int nrchars; 211 int offset = 0; 212 int ret; 213 int i; 214 215 /* clear program memory before updating */ 216 for (i = 0; i < LP8501_PROGRAM_LENGTH; i++) 217 lp55xx_write(chip, LP8501_REG_PROG_MEM + i, 0); 218 219 i = 0; 220 while ((offset < size - 1) && (i < LP8501_PROGRAM_LENGTH)) { 221 /* separate sscanfs because length is working only for %s */ 222 ret = sscanf(data + offset, "%2s%n ", c, &nrchars); 223 if (ret != 1) 224 goto err; 225 226 ret = sscanf(c, "%2x", &cmd); 227 if (ret != 1) 228 goto err; 229 230 pattern[i] = (u8)cmd; 231 offset += nrchars; 232 i++; 233 } 234 235 /* Each instruction is 16bit long. Check that length is even */ 236 if (i % 2) 237 goto err; 238 239 update_size = i; 240 for (i = 0; i < update_size; i++) 241 lp55xx_write(chip, LP8501_REG_PROG_MEM + i, pattern[i]); 242 243 return 0; 244 245 err: 246 dev_err(&chip->cl->dev, "wrong pattern format\n"); 247 return -EINVAL; 248 } 249 250 static void lp8501_firmware_loaded(struct lp55xx_chip *chip) 251 { 252 const struct firmware *fw = chip->fw; 253 254 if (fw->size > LP8501_PROGRAM_LENGTH) { 255 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n", 256 fw->size); 257 return; 258 } 259 260 /* 261 * Program memory sequence 262 * 1) set engine mode to "LOAD" 263 * 2) write firmware data into program memory 264 */ 265 266 lp8501_load_engine(chip); 267 lp8501_update_program_memory(chip, fw->data, fw->size); 268 } 269 270 static int lp8501_led_brightness(struct lp55xx_led *led) 271 { 272 struct lp55xx_chip *chip = led->chip; 273 int ret; 274 275 mutex_lock(&chip->lock); 276 ret = lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + led->chan_nr, 277 led->brightness); 278 mutex_unlock(&chip->lock); 279 280 return ret; 281 } 282 283 /* Chip specific configurations */ 284 static struct lp55xx_device_config lp8501_cfg = { 285 .reg_op_mode = { 286 .addr = LP8501_REG_OP_MODE, 287 }, 288 .engine_busy = { 289 .addr = LP8501_REG_STATUS, 290 .maks = LP8501_ENGINE_BUSY, 291 }, 292 .reset = { 293 .addr = LP8501_REG_RESET, 294 .val = LP8501_RESET, 295 }, 296 .enable = { 297 .addr = LP8501_REG_ENABLE, 298 .val = LP8501_ENABLE, 299 }, 300 .max_channel = LP8501_MAX_LEDS, 301 .post_init_device = lp8501_post_init_device, 302 .brightness_fn = lp8501_led_brightness, 303 .set_led_current = lp8501_set_led_current, 304 .firmware_cb = lp8501_firmware_loaded, 305 .run_engine = lp8501_run_engine, 306 }; 307 308 static const struct i2c_device_id lp8501_id[] = { 309 { "lp8501", .driver_data = (kernel_ulong_t)&lp8501_cfg, }, 310 { } 311 }; 312 MODULE_DEVICE_TABLE(i2c, lp8501_id); 313 314 static const struct of_device_id of_lp8501_leds_match[] = { 315 { .compatible = "ti,lp8501", .data = &lp8501_cfg, }, 316 {}, 317 }; 318 319 MODULE_DEVICE_TABLE(of, of_lp8501_leds_match); 320 321 static struct i2c_driver lp8501_driver = { 322 .driver = { 323 .name = "lp8501", 324 .of_match_table = of_lp8501_leds_match, 325 }, 326 .probe = lp55xx_probe, 327 .remove = lp55xx_remove, 328 .id_table = lp8501_id, 329 }; 330 331 module_i2c_driver(lp8501_driver); 332 333 MODULE_DESCRIPTION("Texas Instruments LP8501 LED driver"); 334 MODULE_AUTHOR("Milo Kim"); 335 MODULE_LICENSE("GPL"); 336