| rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * LP5562 LED driver | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2013 Texas Instruments | 
|  | 5 | * | 
|  | 6 | * Author: Milo(Woogyom) Kim <milo.kim@ti.com> | 
|  | 7 | * | 
|  | 8 | * This program is free software; you can redistribute it and/or modify | 
|  | 9 | * it under the terms of the GNU General Public License version 2 as | 
|  | 10 | * published by the Free Software Foundation. | 
|  | 11 | */ | 
|  | 12 |  | 
|  | 13 | #include <linux/delay.h> | 
|  | 14 | #include <linux/firmware.h> | 
|  | 15 | #include <linux/i2c.h> | 
|  | 16 | #include <linux/leds.h> | 
|  | 17 | #include <linux/module.h> | 
|  | 18 | #include <linux/mutex.h> | 
|  | 19 | #include <linux/of.h> | 
|  | 20 | #include <linux/platform_data/leds-lp55xx.h> | 
|  | 21 | #include <linux/slab.h> | 
|  | 22 |  | 
|  | 23 | #include "leds-lp55xx-common.h" | 
|  | 24 |  | 
|  | 25 | #define LP5562_PROGRAM_LENGTH		32 | 
|  | 26 | #define LP5562_MAX_LEDS			4 | 
|  | 27 |  | 
|  | 28 | /* ENABLE Register 00h */ | 
|  | 29 | #define LP5562_REG_ENABLE		0x00 | 
|  | 30 | #define LP5562_EXEC_ENG1_M		0x30 | 
|  | 31 | #define LP5562_EXEC_ENG2_M		0x0C | 
|  | 32 | #define LP5562_EXEC_ENG3_M		0x03 | 
|  | 33 | #define LP5562_EXEC_M			0x3F | 
|  | 34 | #define LP5562_MASTER_ENABLE		0x40	/* Chip master enable */ | 
|  | 35 | #define LP5562_LOGARITHMIC_PWM		0x80	/* Logarithmic PWM adjustment */ | 
|  | 36 | #define LP5562_EXEC_RUN			0x2A | 
|  | 37 | #define LP5562_ENABLE_DEFAULT	\ | 
|  | 38 | (LP5562_MASTER_ENABLE | LP5562_LOGARITHMIC_PWM) | 
|  | 39 | #define LP5562_ENABLE_RUN_PROGRAM	\ | 
|  | 40 | (LP5562_ENABLE_DEFAULT | LP5562_EXEC_RUN) | 
|  | 41 |  | 
|  | 42 | /* OPMODE Register 01h */ | 
|  | 43 | #define LP5562_REG_OP_MODE		0x01 | 
|  | 44 | #define LP5562_MODE_ENG1_M		0x30 | 
|  | 45 | #define LP5562_MODE_ENG2_M		0x0C | 
|  | 46 | #define LP5562_MODE_ENG3_M		0x03 | 
|  | 47 | #define LP5562_LOAD_ENG1		0x10 | 
|  | 48 | #define LP5562_LOAD_ENG2		0x04 | 
|  | 49 | #define LP5562_LOAD_ENG3		0x01 | 
|  | 50 | #define LP5562_RUN_ENG1			0x20 | 
|  | 51 | #define LP5562_RUN_ENG2			0x08 | 
|  | 52 | #define LP5562_RUN_ENG3			0x02 | 
|  | 53 | #define LP5562_ENG1_IS_LOADING(mode)	\ | 
|  | 54 | ((mode & LP5562_MODE_ENG1_M) == LP5562_LOAD_ENG1) | 
|  | 55 | #define LP5562_ENG2_IS_LOADING(mode)	\ | 
|  | 56 | ((mode & LP5562_MODE_ENG2_M) == LP5562_LOAD_ENG2) | 
|  | 57 | #define LP5562_ENG3_IS_LOADING(mode)	\ | 
|  | 58 | ((mode & LP5562_MODE_ENG3_M) == LP5562_LOAD_ENG3) | 
|  | 59 |  | 
|  | 60 | /* BRIGHTNESS Registers */ | 
|  | 61 | #define LP5562_REG_R_PWM		0x04 | 
|  | 62 | #define LP5562_REG_G_PWM		0x03 | 
|  | 63 | #define LP5562_REG_B_PWM		0x02 | 
|  | 64 | #define LP5562_REG_W_PWM		0x0E | 
|  | 65 |  | 
|  | 66 | /* CURRENT Registers */ | 
|  | 67 | #define LP5562_REG_R_CURRENT		0x07 | 
|  | 68 | #define LP5562_REG_G_CURRENT		0x06 | 
|  | 69 | #define LP5562_REG_B_CURRENT		0x05 | 
|  | 70 | #define LP5562_REG_W_CURRENT		0x0F | 
|  | 71 |  | 
|  | 72 | /* CONFIG Register 08h */ | 
|  | 73 | #define LP5562_REG_CONFIG		0x08 | 
|  | 74 | #define LP5562_PWM_HF			0x40 | 
|  | 75 | #define LP5562_PWRSAVE_EN		0x20 | 
|  | 76 | #define LP5562_CLK_INT			0x01	/* Internal clock */ | 
|  | 77 | #define LP5562_DEFAULT_CFG		(LP5562_PWM_HF | LP5562_PWRSAVE_EN) | 
|  | 78 |  | 
|  | 79 | /* RESET Register 0Dh */ | 
|  | 80 | #define LP5562_REG_RESET		0x0D | 
|  | 81 | #define LP5562_RESET			0xFF | 
|  | 82 |  | 
|  | 83 | /* PROGRAM ENGINE Registers */ | 
|  | 84 | #define LP5562_REG_PROG_MEM_ENG1	0x10 | 
|  | 85 | #define LP5562_REG_PROG_MEM_ENG2	0x30 | 
|  | 86 | #define LP5562_REG_PROG_MEM_ENG3	0x50 | 
|  | 87 |  | 
|  | 88 | /* LEDMAP Register 70h */ | 
|  | 89 | #define LP5562_REG_ENG_SEL		0x70 | 
|  | 90 | #define LP5562_ENG_SEL_PWM		0 | 
|  | 91 | #define LP5562_ENG_FOR_RGB_M		0x3F | 
|  | 92 | #define LP5562_ENG_SEL_RGB		0x1B	/* R:ENG1, G:ENG2, B:ENG3 */ | 
|  | 93 | #define LP5562_ENG_FOR_W_M		0xC0 | 
|  | 94 | #define LP5562_ENG1_FOR_W		0x40	/* W:ENG1 */ | 
|  | 95 | #define LP5562_ENG2_FOR_W		0x80	/* W:ENG2 */ | 
|  | 96 | #define LP5562_ENG3_FOR_W		0xC0	/* W:ENG3 */ | 
|  | 97 |  | 
|  | 98 | /* Program Commands */ | 
|  | 99 | #define LP5562_CMD_DISABLE		0x00 | 
|  | 100 | #define LP5562_CMD_LOAD			0x15 | 
|  | 101 | #define LP5562_CMD_RUN			0x2A | 
|  | 102 | #define LP5562_CMD_DIRECT		0x3F | 
|  | 103 | #define LP5562_PATTERN_OFF		0 | 
|  | 104 |  | 
|  | 105 | static inline void lp5562_wait_opmode_done(void) | 
|  | 106 | { | 
|  | 107 | /* operation mode change needs to be longer than 153 us */ | 
|  | 108 | usleep_range(200, 300); | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | static inline void lp5562_wait_enable_done(void) | 
|  | 112 | { | 
|  | 113 | /* it takes more 488 us to update ENABLE register */ | 
|  | 114 | usleep_range(500, 600); | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current) | 
|  | 118 | { | 
|  | 119 | static const u8 addr[] = { | 
|  | 120 | LP5562_REG_R_CURRENT, | 
|  | 121 | LP5562_REG_G_CURRENT, | 
|  | 122 | LP5562_REG_B_CURRENT, | 
|  | 123 | LP5562_REG_W_CURRENT, | 
|  | 124 | }; | 
|  | 125 |  | 
|  | 126 | led->led_current = led_current; | 
|  | 127 | lp55xx_write(led->chip, addr[led->chan_nr], led_current); | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | static void lp5562_load_engine(struct lp55xx_chip *chip) | 
|  | 131 | { | 
|  | 132 | enum lp55xx_engine_index idx = chip->engine_idx; | 
|  | 133 | static const u8 mask[] = { | 
|  | 134 | [LP55XX_ENGINE_1] = LP5562_MODE_ENG1_M, | 
|  | 135 | [LP55XX_ENGINE_2] = LP5562_MODE_ENG2_M, | 
|  | 136 | [LP55XX_ENGINE_3] = LP5562_MODE_ENG3_M, | 
|  | 137 | }; | 
|  | 138 |  | 
|  | 139 | static const u8 val[] = { | 
|  | 140 | [LP55XX_ENGINE_1] = LP5562_LOAD_ENG1, | 
|  | 141 | [LP55XX_ENGINE_2] = LP5562_LOAD_ENG2, | 
|  | 142 | [LP55XX_ENGINE_3] = LP5562_LOAD_ENG3, | 
|  | 143 | }; | 
|  | 144 |  | 
|  | 145 | lp55xx_update_bits(chip, LP5562_REG_OP_MODE, mask[idx], val[idx]); | 
|  | 146 |  | 
|  | 147 | lp5562_wait_opmode_done(); | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | static void lp5562_stop_engine(struct lp55xx_chip *chip) | 
|  | 151 | { | 
|  | 152 | lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DISABLE); | 
|  | 153 | lp5562_wait_opmode_done(); | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | static void lp5562_run_engine(struct lp55xx_chip *chip, bool start) | 
|  | 157 | { | 
|  | 158 | int ret; | 
|  | 159 | u8 mode; | 
|  | 160 | u8 exec; | 
|  | 161 |  | 
|  | 162 | /* stop engine */ | 
|  | 163 | if (!start) { | 
|  | 164 | lp55xx_write(chip, LP5562_REG_ENABLE, LP5562_ENABLE_DEFAULT); | 
|  | 165 | lp5562_wait_enable_done(); | 
|  | 166 | lp5562_stop_engine(chip); | 
|  | 167 | lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM); | 
|  | 168 | lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT); | 
|  | 169 | lp5562_wait_opmode_done(); | 
|  | 170 | return; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | /* | 
|  | 174 | * To run the engine, | 
|  | 175 | * operation mode and enable register should updated at the same time | 
|  | 176 | */ | 
|  | 177 |  | 
|  | 178 | ret = lp55xx_read(chip, LP5562_REG_OP_MODE, &mode); | 
|  | 179 | if (ret) | 
|  | 180 | return; | 
|  | 181 |  | 
|  | 182 | ret = lp55xx_read(chip, LP5562_REG_ENABLE, &exec); | 
|  | 183 | if (ret) | 
|  | 184 | return; | 
|  | 185 |  | 
|  | 186 | /* change operation mode to RUN only when each engine is loading */ | 
|  | 187 | if (LP5562_ENG1_IS_LOADING(mode)) { | 
|  | 188 | mode = (mode & ~LP5562_MODE_ENG1_M) | LP5562_RUN_ENG1; | 
|  | 189 | exec = (exec & ~LP5562_EXEC_ENG1_M) | LP5562_RUN_ENG1; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | if (LP5562_ENG2_IS_LOADING(mode)) { | 
|  | 193 | mode = (mode & ~LP5562_MODE_ENG2_M) | LP5562_RUN_ENG2; | 
|  | 194 | exec = (exec & ~LP5562_EXEC_ENG2_M) | LP5562_RUN_ENG2; | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | if (LP5562_ENG3_IS_LOADING(mode)) { | 
|  | 198 | mode = (mode & ~LP5562_MODE_ENG3_M) | LP5562_RUN_ENG3; | 
|  | 199 | exec = (exec & ~LP5562_EXEC_ENG3_M) | LP5562_RUN_ENG3; | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | lp55xx_write(chip, LP5562_REG_OP_MODE, mode); | 
|  | 203 | lp5562_wait_opmode_done(); | 
|  | 204 |  | 
|  | 205 | lp55xx_update_bits(chip, LP5562_REG_ENABLE, LP5562_EXEC_M, exec); | 
|  | 206 | lp5562_wait_enable_done(); | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | static int lp5562_update_firmware(struct lp55xx_chip *chip, | 
|  | 210 | const u8 *data, size_t size) | 
|  | 211 | { | 
|  | 212 | enum lp55xx_engine_index idx = chip->engine_idx; | 
|  | 213 | u8 pattern[LP5562_PROGRAM_LENGTH] = {0}; | 
|  | 214 | static const u8 addr[] = { | 
|  | 215 | [LP55XX_ENGINE_1] = LP5562_REG_PROG_MEM_ENG1, | 
|  | 216 | [LP55XX_ENGINE_2] = LP5562_REG_PROG_MEM_ENG2, | 
|  | 217 | [LP55XX_ENGINE_3] = LP5562_REG_PROG_MEM_ENG3, | 
|  | 218 | }; | 
|  | 219 | unsigned cmd; | 
|  | 220 | char c[3]; | 
|  | 221 | int program_size; | 
|  | 222 | int nrchars; | 
|  | 223 | int offset = 0; | 
|  | 224 | int ret; | 
|  | 225 | int i; | 
|  | 226 |  | 
|  | 227 | /* clear program memory before updating */ | 
|  | 228 | for (i = 0; i < LP5562_PROGRAM_LENGTH; i++) | 
|  | 229 | lp55xx_write(chip, addr[idx] + i, 0); | 
|  | 230 |  | 
|  | 231 | i = 0; | 
|  | 232 | while ((offset < size - 1) && (i < LP5562_PROGRAM_LENGTH)) { | 
|  | 233 | /* separate sscanfs because length is working only for %s */ | 
|  | 234 | ret = sscanf(data + offset, "%2s%n ", c, &nrchars); | 
|  | 235 | if (ret != 1) | 
|  | 236 | goto err; | 
|  | 237 |  | 
|  | 238 | ret = sscanf(c, "%2x", &cmd); | 
|  | 239 | if (ret != 1) | 
|  | 240 | goto err; | 
|  | 241 |  | 
|  | 242 | pattern[i] = (u8)cmd; | 
|  | 243 | offset += nrchars; | 
|  | 244 | i++; | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | /* Each instruction is 16bit long. Check that length is even */ | 
|  | 248 | if (i % 2) | 
|  | 249 | goto err; | 
|  | 250 |  | 
|  | 251 | program_size = i; | 
|  | 252 | for (i = 0; i < program_size; i++) | 
|  | 253 | lp55xx_write(chip, addr[idx] + i, pattern[i]); | 
|  | 254 |  | 
|  | 255 | return 0; | 
|  | 256 |  | 
|  | 257 | err: | 
|  | 258 | dev_err(&chip->cl->dev, "wrong pattern format\n"); | 
|  | 259 | return -EINVAL; | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | static void lp5562_firmware_loaded(struct lp55xx_chip *chip) | 
|  | 263 | { | 
|  | 264 | const struct firmware *fw = chip->fw; | 
|  | 265 |  | 
|  | 266 | /* | 
|  | 267 | * the firmware is encoded in ascii hex character, with 2 chars | 
|  | 268 | * per byte | 
|  | 269 | */ | 
|  | 270 | if (fw->size > (LP5562_PROGRAM_LENGTH * 2)) { | 
|  | 271 | dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n", | 
|  | 272 | fw->size); | 
|  | 273 | return; | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | /* | 
|  | 277 | * Program memory sequence | 
|  | 278 | *  1) set engine mode to "LOAD" | 
|  | 279 | *  2) write firmware data into program memory | 
|  | 280 | */ | 
|  | 281 |  | 
|  | 282 | lp5562_load_engine(chip); | 
|  | 283 | lp5562_update_firmware(chip, fw->data, fw->size); | 
|  | 284 | } | 
|  | 285 |  | 
|  | 286 | static int lp5562_post_init_device(struct lp55xx_chip *chip) | 
|  | 287 | { | 
|  | 288 | int ret; | 
|  | 289 | u8 cfg = LP5562_DEFAULT_CFG; | 
|  | 290 |  | 
|  | 291 | /* Set all PWMs to direct control mode */ | 
|  | 292 | ret = lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT); | 
|  | 293 | if (ret) | 
|  | 294 | return ret; | 
|  | 295 |  | 
|  | 296 | lp5562_wait_opmode_done(); | 
|  | 297 |  | 
|  | 298 | /* Update configuration for the clock setting */ | 
|  | 299 | if (!lp55xx_is_extclk_used(chip)) | 
|  | 300 | cfg |= LP5562_CLK_INT; | 
|  | 301 |  | 
|  | 302 | ret = lp55xx_write(chip, LP5562_REG_CONFIG, cfg); | 
|  | 303 | if (ret) | 
|  | 304 | return ret; | 
|  | 305 |  | 
|  | 306 | /* Initialize all channels PWM to zero -> leds off */ | 
|  | 307 | lp55xx_write(chip, LP5562_REG_R_PWM, 0); | 
|  | 308 | lp55xx_write(chip, LP5562_REG_G_PWM, 0); | 
|  | 309 | lp55xx_write(chip, LP5562_REG_B_PWM, 0); | 
|  | 310 | lp55xx_write(chip, LP5562_REG_W_PWM, 0); | 
|  | 311 |  | 
|  | 312 | /* Set LED map as register PWM by default */ | 
|  | 313 | lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM); | 
|  | 314 |  | 
|  | 315 | return 0; | 
|  | 316 | } | 
|  | 317 |  | 
|  | 318 | static int lp5562_led_brightness(struct lp55xx_led *led) | 
|  | 319 | { | 
|  | 320 | struct lp55xx_chip *chip = led->chip; | 
|  | 321 | static const u8 addr[] = { | 
|  | 322 | LP5562_REG_R_PWM, | 
|  | 323 | LP5562_REG_G_PWM, | 
|  | 324 | LP5562_REG_B_PWM, | 
|  | 325 | LP5562_REG_W_PWM, | 
|  | 326 | }; | 
|  | 327 | int ret; | 
|  | 328 |  | 
|  | 329 | mutex_lock(&chip->lock); | 
|  | 330 | ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness); | 
|  | 331 | mutex_unlock(&chip->lock); | 
|  | 332 |  | 
|  | 333 | return ret; | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | static void lp5562_write_program_memory(struct lp55xx_chip *chip, | 
|  | 337 | u8 base, const u8 *rgb, int size) | 
|  | 338 | { | 
|  | 339 | int i; | 
|  | 340 |  | 
|  | 341 | if (!rgb || size <= 0) | 
|  | 342 | return; | 
|  | 343 |  | 
|  | 344 | for (i = 0; i < size; i++) | 
|  | 345 | lp55xx_write(chip, base + i, *(rgb + i)); | 
|  | 346 |  | 
|  | 347 | lp55xx_write(chip, base + i, 0); | 
|  | 348 | lp55xx_write(chip, base + i + 1, 0); | 
|  | 349 | } | 
|  | 350 |  | 
|  | 351 | /* check the size of program count */ | 
|  | 352 | static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn) | 
|  | 353 | { | 
|  | 354 | return ptn->size_r >= LP5562_PROGRAM_LENGTH || | 
|  | 355 | ptn->size_g >= LP5562_PROGRAM_LENGTH || | 
|  | 356 | ptn->size_b >= LP5562_PROGRAM_LENGTH; | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode) | 
|  | 360 | { | 
|  | 361 | struct lp55xx_predef_pattern *ptn; | 
|  | 362 | int i; | 
|  | 363 |  | 
|  | 364 | if (mode == LP5562_PATTERN_OFF) { | 
|  | 365 | lp5562_run_engine(chip, false); | 
|  | 366 | return 0; | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | ptn = chip->pdata->patterns + (mode - 1); | 
|  | 370 | if (!ptn || _is_pc_overflow(ptn)) { | 
|  | 371 | dev_err(&chip->cl->dev, "invalid pattern data\n"); | 
|  | 372 | return -EINVAL; | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | lp5562_stop_engine(chip); | 
|  | 376 |  | 
|  | 377 | /* Set LED map as RGB */ | 
|  | 378 | lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_RGB); | 
|  | 379 |  | 
|  | 380 | /* Load engines */ | 
|  | 381 | for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) { | 
|  | 382 | chip->engine_idx = i; | 
|  | 383 | lp5562_load_engine(chip); | 
|  | 384 | } | 
|  | 385 |  | 
|  | 386 | /* Clear program registers */ | 
|  | 387 | lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1, 0); | 
|  | 388 | lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1 + 1, 0); | 
|  | 389 | lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2, 0); | 
|  | 390 | lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2 + 1, 0); | 
|  | 391 | lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3, 0); | 
|  | 392 | lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3 + 1, 0); | 
|  | 393 |  | 
|  | 394 | /* Program engines */ | 
|  | 395 | lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG1, | 
|  | 396 | ptn->r, ptn->size_r); | 
|  | 397 | lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG2, | 
|  | 398 | ptn->g, ptn->size_g); | 
|  | 399 | lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG3, | 
|  | 400 | ptn->b, ptn->size_b); | 
|  | 401 |  | 
|  | 402 | /* Run engines */ | 
|  | 403 | lp5562_run_engine(chip, true); | 
|  | 404 |  | 
|  | 405 | return 0; | 
|  | 406 | } | 
|  | 407 |  | 
|  | 408 | static ssize_t lp5562_store_pattern(struct device *dev, | 
|  | 409 | struct device_attribute *attr, | 
|  | 410 | const char *buf, size_t len) | 
|  | 411 | { | 
|  | 412 | struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev)); | 
|  | 413 | struct lp55xx_chip *chip = led->chip; | 
|  | 414 | struct lp55xx_predef_pattern *ptn = chip->pdata->patterns; | 
|  | 415 | int num_patterns = chip->pdata->num_patterns; | 
|  | 416 | unsigned long mode; | 
|  | 417 | int ret; | 
|  | 418 |  | 
|  | 419 | ret = kstrtoul(buf, 0, &mode); | 
|  | 420 | if (ret) | 
|  | 421 | return ret; | 
|  | 422 |  | 
|  | 423 | if (mode > num_patterns || !ptn) | 
|  | 424 | return -EINVAL; | 
|  | 425 |  | 
|  | 426 | mutex_lock(&chip->lock); | 
|  | 427 | ret = lp5562_run_predef_led_pattern(chip, mode); | 
|  | 428 | mutex_unlock(&chip->lock); | 
|  | 429 |  | 
|  | 430 | if (ret) | 
|  | 431 | return ret; | 
|  | 432 |  | 
|  | 433 | return len; | 
|  | 434 | } | 
|  | 435 |  | 
|  | 436 | static ssize_t lp5562_store_engine_mux(struct device *dev, | 
|  | 437 | struct device_attribute *attr, | 
|  | 438 | const char *buf, size_t len) | 
|  | 439 | { | 
|  | 440 | struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev)); | 
|  | 441 | struct lp55xx_chip *chip = led->chip; | 
|  | 442 | u8 mask; | 
|  | 443 | u8 val; | 
|  | 444 |  | 
|  | 445 | /* LED map | 
|  | 446 | * R ... Engine 1 (fixed) | 
|  | 447 | * G ... Engine 2 (fixed) | 
|  | 448 | * B ... Engine 3 (fixed) | 
|  | 449 | * W ... Engine 1 or 2 or 3 | 
|  | 450 | */ | 
|  | 451 |  | 
|  | 452 | if (sysfs_streq(buf, "RGB")) { | 
|  | 453 | mask = LP5562_ENG_FOR_RGB_M; | 
|  | 454 | val = LP5562_ENG_SEL_RGB; | 
|  | 455 | } else if (sysfs_streq(buf, "W")) { | 
|  | 456 | enum lp55xx_engine_index idx = chip->engine_idx; | 
|  | 457 |  | 
|  | 458 | mask = LP5562_ENG_FOR_W_M; | 
|  | 459 | switch (idx) { | 
|  | 460 | case LP55XX_ENGINE_1: | 
|  | 461 | val = LP5562_ENG1_FOR_W; | 
|  | 462 | break; | 
|  | 463 | case LP55XX_ENGINE_2: | 
|  | 464 | val = LP5562_ENG2_FOR_W; | 
|  | 465 | break; | 
|  | 466 | case LP55XX_ENGINE_3: | 
|  | 467 | val = LP5562_ENG3_FOR_W; | 
|  | 468 | break; | 
|  | 469 | default: | 
|  | 470 | return -EINVAL; | 
|  | 471 | } | 
|  | 472 |  | 
|  | 473 | } else { | 
|  | 474 | dev_err(dev, "choose RGB or W\n"); | 
|  | 475 | return -EINVAL; | 
|  | 476 | } | 
|  | 477 |  | 
|  | 478 | mutex_lock(&chip->lock); | 
|  | 479 | lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val); | 
|  | 480 | mutex_unlock(&chip->lock); | 
|  | 481 |  | 
|  | 482 | return len; | 
|  | 483 | } | 
|  | 484 |  | 
|  | 485 | static LP55XX_DEV_ATTR_WO(led_pattern, lp5562_store_pattern); | 
|  | 486 | static LP55XX_DEV_ATTR_WO(engine_mux, lp5562_store_engine_mux); | 
|  | 487 |  | 
|  | 488 | static struct attribute *lp5562_attributes[] = { | 
|  | 489 | &dev_attr_led_pattern.attr, | 
|  | 490 | &dev_attr_engine_mux.attr, | 
|  | 491 | NULL, | 
|  | 492 | }; | 
|  | 493 |  | 
|  | 494 | static const struct attribute_group lp5562_group = { | 
|  | 495 | .attrs = lp5562_attributes, | 
|  | 496 | }; | 
|  | 497 |  | 
|  | 498 | /* Chip specific configurations */ | 
|  | 499 | static struct lp55xx_device_config lp5562_cfg = { | 
|  | 500 | .max_channel  = LP5562_MAX_LEDS, | 
|  | 501 | .reset = { | 
|  | 502 | .addr = LP5562_REG_RESET, | 
|  | 503 | .val  = LP5562_RESET, | 
|  | 504 | }, | 
|  | 505 | .enable = { | 
|  | 506 | .addr = LP5562_REG_ENABLE, | 
|  | 507 | .val  = LP5562_ENABLE_DEFAULT, | 
|  | 508 | }, | 
|  | 509 | .post_init_device   = lp5562_post_init_device, | 
|  | 510 | .set_led_current    = lp5562_set_led_current, | 
|  | 511 | .brightness_fn      = lp5562_led_brightness, | 
|  | 512 | .run_engine         = lp5562_run_engine, | 
|  | 513 | .firmware_cb        = lp5562_firmware_loaded, | 
|  | 514 | .dev_attr_group     = &lp5562_group, | 
|  | 515 | }; | 
|  | 516 |  | 
|  | 517 | static int lp5562_probe(struct i2c_client *client, | 
|  | 518 | const struct i2c_device_id *id) | 
|  | 519 | { | 
|  | 520 | int ret; | 
|  | 521 | struct lp55xx_chip *chip; | 
|  | 522 | struct lp55xx_led *led; | 
|  | 523 | struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev); | 
|  | 524 | struct device_node *np = client->dev.of_node; | 
|  | 525 |  | 
|  | 526 | if (!pdata) { | 
|  | 527 | if (np) { | 
|  | 528 | pdata = lp55xx_of_populate_pdata(&client->dev, np); | 
|  | 529 | if (IS_ERR(pdata)) | 
|  | 530 | return PTR_ERR(pdata); | 
|  | 531 | } else { | 
|  | 532 | dev_err(&client->dev, "no platform data\n"); | 
|  | 533 | return -EINVAL; | 
|  | 534 | } | 
|  | 535 | } | 
|  | 536 |  | 
|  | 537 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); | 
|  | 538 | if (!chip) | 
|  | 539 | return -ENOMEM; | 
|  | 540 |  | 
|  | 541 | led = devm_kzalloc(&client->dev, | 
|  | 542 | sizeof(*led) * pdata->num_channels, GFP_KERNEL); | 
|  | 543 | if (!led) | 
|  | 544 | return -ENOMEM; | 
|  | 545 |  | 
|  | 546 | chip->cl = client; | 
|  | 547 | chip->pdata = pdata; | 
|  | 548 | chip->cfg = &lp5562_cfg; | 
|  | 549 |  | 
|  | 550 | mutex_init(&chip->lock); | 
|  | 551 |  | 
|  | 552 | i2c_set_clientdata(client, led); | 
|  | 553 |  | 
|  | 554 | ret = lp55xx_init_device(chip); | 
|  | 555 | if (ret) | 
|  | 556 | goto err_init; | 
|  | 557 |  | 
|  | 558 | ret = lp55xx_register_leds(led, chip); | 
|  | 559 | if (ret) | 
|  | 560 | goto err_register_leds; | 
|  | 561 |  | 
|  | 562 | ret = lp55xx_register_sysfs(chip); | 
|  | 563 | if (ret) { | 
|  | 564 | dev_err(&client->dev, "registering sysfs failed\n"); | 
|  | 565 | goto err_register_sysfs; | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | return 0; | 
|  | 569 |  | 
|  | 570 | err_register_sysfs: | 
|  | 571 | lp55xx_unregister_leds(led, chip); | 
|  | 572 | err_register_leds: | 
|  | 573 | lp55xx_deinit_device(chip); | 
|  | 574 | err_init: | 
|  | 575 | return ret; | 
|  | 576 | } | 
|  | 577 |  | 
|  | 578 | static int lp5562_remove(struct i2c_client *client) | 
|  | 579 | { | 
|  | 580 | struct lp55xx_led *led = i2c_get_clientdata(client); | 
|  | 581 | struct lp55xx_chip *chip = led->chip; | 
|  | 582 |  | 
|  | 583 | lp5562_stop_engine(chip); | 
|  | 584 |  | 
|  | 585 | lp55xx_unregister_sysfs(chip); | 
|  | 586 | lp55xx_unregister_leds(led, chip); | 
|  | 587 | lp55xx_deinit_device(chip); | 
|  | 588 |  | 
|  | 589 | return 0; | 
|  | 590 | } | 
|  | 591 |  | 
|  | 592 | static const struct i2c_device_id lp5562_id[] = { | 
|  | 593 | { "lp5562", 0 }, | 
|  | 594 | { } | 
|  | 595 | }; | 
|  | 596 | MODULE_DEVICE_TABLE(i2c, lp5562_id); | 
|  | 597 |  | 
|  | 598 | #ifdef CONFIG_OF | 
|  | 599 | static const struct of_device_id of_lp5562_leds_match[] = { | 
|  | 600 | { .compatible = "ti,lp5562", }, | 
|  | 601 | {}, | 
|  | 602 | }; | 
|  | 603 |  | 
|  | 604 | MODULE_DEVICE_TABLE(of, of_lp5562_leds_match); | 
|  | 605 | #endif | 
|  | 606 |  | 
|  | 607 | static struct i2c_driver lp5562_driver = { | 
|  | 608 | .driver = { | 
|  | 609 | .name	= "lp5562", | 
|  | 610 | .of_match_table = of_match_ptr(of_lp5562_leds_match), | 
|  | 611 | }, | 
|  | 612 | .probe		= lp5562_probe, | 
|  | 613 | .remove		= lp5562_remove, | 
|  | 614 | .id_table	= lp5562_id, | 
|  | 615 | }; | 
|  | 616 |  | 
|  | 617 | module_i2c_driver(lp5562_driver); | 
|  | 618 |  | 
|  | 619 | MODULE_DESCRIPTION("Texas Instruments LP5562 LED Driver"); | 
|  | 620 | MODULE_AUTHOR("Milo Kim"); | 
|  | 621 | MODULE_LICENSE("GPL"); |