dma-fence: Add some more fence-merge-unwrap tests
[drm/drm-misc.git] / drivers / leds / leds-lp5562.c
blob14a4af361b267697d30976e61ad6cfe59dd8dd4b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * LP5562 LED driver
5 * Copyright (C) 2013 Texas Instruments
7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8 */
10 #include <linux/cleanup.h>
11 #include <linux/delay.h>
12 #include <linux/firmware.h>
13 #include <linux/i2c.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>
21 #include "leds-lp55xx-common.h"
23 #define LP5562_MAX_LEDS 4
25 /* ENABLE Register 00h */
26 #define LP5562_REG_ENABLE 0x00
27 #define LP5562_EXEC_ENG1_M 0x30
28 #define LP5562_EXEC_ENG2_M 0x0C
29 #define LP5562_EXEC_ENG3_M 0x03
30 #define LP5562_EXEC_M 0x3F
31 #define LP5562_MASTER_ENABLE 0x40 /* Chip master enable */
32 #define LP5562_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
33 #define LP5562_EXEC_RUN 0x2A
34 #define LP5562_ENABLE_DEFAULT \
35 (LP5562_MASTER_ENABLE | LP5562_LOGARITHMIC_PWM)
36 #define LP5562_ENABLE_RUN_PROGRAM \
37 (LP5562_ENABLE_DEFAULT | LP5562_EXEC_RUN)
39 /* OPMODE Register 01h */
40 #define LP5562_REG_OP_MODE 0x01
42 /* BRIGHTNESS Registers */
43 #define LP5562_REG_R_PWM 0x04
44 #define LP5562_REG_G_PWM 0x03
45 #define LP5562_REG_B_PWM 0x02
46 #define LP5562_REG_W_PWM 0x0E
48 /* CURRENT Registers */
49 #define LP5562_REG_R_CURRENT 0x07
50 #define LP5562_REG_G_CURRENT 0x06
51 #define LP5562_REG_B_CURRENT 0x05
52 #define LP5562_REG_W_CURRENT 0x0F
54 /* CONFIG Register 08h */
55 #define LP5562_REG_CONFIG 0x08
56 #define LP5562_PWM_HF 0x40
57 #define LP5562_PWRSAVE_EN 0x20
58 #define LP5562_CLK_INT 0x01 /* Internal clock */
59 #define LP5562_DEFAULT_CFG (LP5562_PWM_HF | LP5562_PWRSAVE_EN)
61 /* RESET Register 0Dh */
62 #define LP5562_REG_RESET 0x0D
63 #define LP5562_RESET 0xFF
65 /* PROGRAM ENGINE Registers */
66 #define LP5562_REG_PROG_MEM_ENG1 0x10
67 #define LP5562_REG_PROG_MEM_ENG2 0x30
68 #define LP5562_REG_PROG_MEM_ENG3 0x50
70 /* LEDMAP Register 70h */
71 #define LP5562_REG_ENG_SEL 0x70
72 #define LP5562_ENG_SEL_PWM 0
73 #define LP5562_ENG_FOR_RGB_M 0x3F
74 #define LP5562_ENG_SEL_RGB 0x1B /* R:ENG1, G:ENG2, B:ENG3 */
75 #define LP5562_ENG_FOR_W_M 0xC0
76 #define LP5562_ENG1_FOR_W 0x40 /* W:ENG1 */
77 #define LP5562_ENG2_FOR_W 0x80 /* W:ENG2 */
78 #define LP5562_ENG3_FOR_W 0xC0 /* W:ENG3 */
80 /* Program Commands */
81 #define LP5562_CMD_DISABLE 0x00
82 #define LP5562_CMD_LOAD 0x15
83 #define LP5562_CMD_RUN 0x2A
84 #define LP5562_CMD_DIRECT 0x3F
85 #define LP5562_PATTERN_OFF 0
87 static inline void lp5562_wait_opmode_done(void)
89 /* operation mode change needs to be longer than 153 us */
90 usleep_range(200, 300);
93 static inline void lp5562_wait_enable_done(void)
95 /* it takes more 488 us to update ENABLE register */
96 usleep_range(500, 600);
99 static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current)
101 static const u8 addr[] = {
102 LP5562_REG_R_CURRENT,
103 LP5562_REG_G_CURRENT,
104 LP5562_REG_B_CURRENT,
105 LP5562_REG_W_CURRENT,
108 led->led_current = led_current;
109 lp55xx_write(led->chip, addr[led->chan_nr], led_current);
112 static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
114 int ret;
116 /* stop engine */
117 if (!start) {
118 lp55xx_write(chip, LP5562_REG_ENABLE, LP5562_ENABLE_DEFAULT);
119 lp5562_wait_enable_done();
120 lp55xx_stop_all_engine(chip);
121 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
122 lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
123 lp5562_wait_opmode_done();
124 return;
127 ret = lp55xx_run_engine_common(chip);
128 if (!ret)
129 lp5562_wait_enable_done();
132 static int lp5562_post_init_device(struct lp55xx_chip *chip)
134 int ret;
135 u8 cfg = LP5562_DEFAULT_CFG;
137 /* Set all PWMs to direct control mode */
138 ret = lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
139 if (ret)
140 return ret;
142 lp5562_wait_opmode_done();
144 /* Update configuration for the clock setting */
145 if (!lp55xx_is_extclk_used(chip))
146 cfg |= LP5562_CLK_INT;
148 ret = lp55xx_write(chip, LP5562_REG_CONFIG, cfg);
149 if (ret)
150 return ret;
152 /* Initialize all channels PWM to zero -> leds off */
153 lp55xx_write(chip, LP5562_REG_R_PWM, 0);
154 lp55xx_write(chip, LP5562_REG_G_PWM, 0);
155 lp55xx_write(chip, LP5562_REG_B_PWM, 0);
156 lp55xx_write(chip, LP5562_REG_W_PWM, 0);
158 /* Set LED map as register PWM by default */
159 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
161 return 0;
164 static int lp5562_multicolor_brightness(struct lp55xx_led *led)
166 struct lp55xx_chip *chip = led->chip;
167 static const u8 addr[] = {
168 LP5562_REG_R_PWM,
169 LP5562_REG_G_PWM,
170 LP5562_REG_B_PWM,
171 LP5562_REG_W_PWM,
173 int ret;
174 int i;
176 guard(mutex)(&chip->lock);
177 for (i = 0; i < led->mc_cdev.num_colors; i++) {
178 ret = lp55xx_write(chip,
179 addr[led->mc_cdev.subled_info[i].channel],
180 led->mc_cdev.subled_info[i].brightness);
181 if (ret)
182 break;
185 return ret;
188 static int lp5562_led_brightness(struct lp55xx_led *led)
190 struct lp55xx_chip *chip = led->chip;
191 static const u8 addr[] = {
192 LP5562_REG_R_PWM,
193 LP5562_REG_G_PWM,
194 LP5562_REG_B_PWM,
195 LP5562_REG_W_PWM,
197 int ret;
199 guard(mutex)(&chip->lock);
201 ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness);
203 return ret;
206 static void lp5562_write_program_memory(struct lp55xx_chip *chip,
207 u8 base, const u8 *rgb, int size)
209 int i;
211 if (!rgb || size <= 0)
212 return;
214 for (i = 0; i < size; i++)
215 lp55xx_write(chip, base + i, *(rgb + i));
217 lp55xx_write(chip, base + i, 0);
218 lp55xx_write(chip, base + i + 1, 0);
221 /* check the size of program count */
222 static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn)
224 return ptn->size_r >= LP55xx_BYTES_PER_PAGE ||
225 ptn->size_g >= LP55xx_BYTES_PER_PAGE ||
226 ptn->size_b >= LP55xx_BYTES_PER_PAGE;
229 static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
231 struct lp55xx_predef_pattern *ptn;
232 int i;
234 if (mode == LP5562_PATTERN_OFF) {
235 lp5562_run_engine(chip, false);
236 return 0;
239 ptn = chip->pdata->patterns + (mode - 1);
240 if (!ptn || _is_pc_overflow(ptn)) {
241 dev_err(&chip->cl->dev, "invalid pattern data\n");
242 return -EINVAL;
245 lp55xx_stop_all_engine(chip);
247 /* Set LED map as RGB */
248 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_RGB);
250 /* Load engines */
251 for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
252 chip->engine_idx = i;
253 lp55xx_load_engine(chip);
256 /* Clear program registers */
257 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1, 0);
258 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1 + 1, 0);
259 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2, 0);
260 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2 + 1, 0);
261 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3, 0);
262 lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3 + 1, 0);
264 /* Program engines */
265 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG1,
266 ptn->r, ptn->size_r);
267 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG2,
268 ptn->g, ptn->size_g);
269 lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG3,
270 ptn->b, ptn->size_b);
272 /* Run engines */
273 lp5562_run_engine(chip, true);
275 return 0;
278 static ssize_t lp5562_store_pattern(struct device *dev,
279 struct device_attribute *attr,
280 const char *buf, size_t len)
282 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
283 struct lp55xx_chip *chip = led->chip;
284 struct lp55xx_predef_pattern *ptn = chip->pdata->patterns;
285 int num_patterns = chip->pdata->num_patterns;
286 unsigned long mode;
287 int ret;
289 ret = kstrtoul(buf, 0, &mode);
290 if (ret)
291 return ret;
293 if (mode > num_patterns || !ptn)
294 return -EINVAL;
296 guard(mutex)(&chip->lock);
298 ret = lp5562_run_predef_led_pattern(chip, mode);
300 if (ret)
301 return ret;
303 return len;
306 static ssize_t lp5562_store_engine_mux(struct device *dev,
307 struct device_attribute *attr,
308 const char *buf, size_t len)
310 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
311 struct lp55xx_chip *chip = led->chip;
312 u8 mask;
313 u8 val;
315 /* LED map
316 * R ... Engine 1 (fixed)
317 * G ... Engine 2 (fixed)
318 * B ... Engine 3 (fixed)
319 * W ... Engine 1 or 2 or 3
322 if (sysfs_streq(buf, "RGB")) {
323 mask = LP5562_ENG_FOR_RGB_M;
324 val = LP5562_ENG_SEL_RGB;
325 } else if (sysfs_streq(buf, "W")) {
326 enum lp55xx_engine_index idx = chip->engine_idx;
328 mask = LP5562_ENG_FOR_W_M;
329 switch (idx) {
330 case LP55XX_ENGINE_1:
331 val = LP5562_ENG1_FOR_W;
332 break;
333 case LP55XX_ENGINE_2:
334 val = LP5562_ENG2_FOR_W;
335 break;
336 case LP55XX_ENGINE_3:
337 val = LP5562_ENG3_FOR_W;
338 break;
339 default:
340 return -EINVAL;
343 } else {
344 dev_err(dev, "choose RGB or W\n");
345 return -EINVAL;
348 guard(mutex)(&chip->lock);
350 lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val);
352 return len;
355 static LP55XX_DEV_ATTR_WO(led_pattern, lp5562_store_pattern);
356 static LP55XX_DEV_ATTR_WO(engine_mux, lp5562_store_engine_mux);
358 static struct attribute *lp5562_attributes[] = {
359 &dev_attr_led_pattern.attr,
360 &dev_attr_engine_mux.attr,
361 NULL,
364 static const struct attribute_group lp5562_group = {
365 .attrs = lp5562_attributes,
368 /* Chip specific configurations */
369 static struct lp55xx_device_config lp5562_cfg = {
370 .max_channel = LP5562_MAX_LEDS,
371 .reg_op_mode = {
372 .addr = LP5562_REG_OP_MODE,
374 .reg_exec = {
375 .addr = LP5562_REG_ENABLE,
377 .reset = {
378 .addr = LP5562_REG_RESET,
379 .val = LP5562_RESET,
381 .enable = {
382 .addr = LP5562_REG_ENABLE,
383 .val = LP5562_ENABLE_DEFAULT,
385 .prog_mem_base = {
386 .addr = LP5562_REG_PROG_MEM_ENG1,
388 .post_init_device = lp5562_post_init_device,
389 .set_led_current = lp5562_set_led_current,
390 .brightness_fn = lp5562_led_brightness,
391 .multicolor_brightness_fn = lp5562_multicolor_brightness,
392 .run_engine = lp5562_run_engine,
393 .firmware_cb = lp55xx_firmware_loaded_cb,
394 .dev_attr_group = &lp5562_group,
397 static const struct i2c_device_id lp5562_id[] = {
398 { "lp5562", .driver_data = (kernel_ulong_t)&lp5562_cfg, },
401 MODULE_DEVICE_TABLE(i2c, lp5562_id);
403 static const struct of_device_id of_lp5562_leds_match[] = {
404 { .compatible = "ti,lp5562", .data = &lp5562_cfg, },
408 MODULE_DEVICE_TABLE(of, of_lp5562_leds_match);
410 static struct i2c_driver lp5562_driver = {
411 .driver = {
412 .name = "lp5562",
413 .of_match_table = of_lp5562_leds_match,
415 .probe = lp55xx_probe,
416 .remove = lp55xx_remove,
417 .id_table = lp5562_id,
420 module_i2c_driver(lp5562_driver);
422 MODULE_DESCRIPTION("Texas Instruments LP5562 LED Driver");
423 MODULE_AUTHOR("Milo Kim");
424 MODULE_LICENSE("GPL");