]> Git Repo - linux.git/blob - drivers/leds/leds-lp5562.c
leds: leds-lp55xx: Generalize update_program_memory function
[linux.git] / drivers / leds / leds-lp5562.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * LP5562 LED driver
4  *
5  * Copyright (C) 2013 Texas Instruments
6  *
7  * Author: Milo(Woogyom) Kim <[email protected]>
8  */
9
10 #include <linux/delay.h>
11 #include <linux/firmware.h>
12 #include <linux/i2c.h>
13 #include <linux/leds.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/of.h>
17 #include <linux/platform_data/leds-lp55xx.h>
18 #include <linux/slab.h>
19
20 #include "leds-lp55xx-common.h"
21
22 #define LP5562_PROGRAM_LENGTH           32
23 #define LP5562_MAX_LEDS                 4
24
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)
38
39 /* OPMODE Register 01h */
40 #define LP5562_REG_OP_MODE              0x01
41 #define LP5562_MODE_ENG1_M              0x30
42 #define LP5562_MODE_ENG2_M              0x0C
43 #define LP5562_MODE_ENG3_M              0x03
44 #define LP5562_LOAD_ENG1                0x10
45 #define LP5562_LOAD_ENG2                0x04
46 #define LP5562_LOAD_ENG3                0x01
47 #define LP5562_RUN_ENG1                 0x20
48 #define LP5562_RUN_ENG2                 0x08
49 #define LP5562_RUN_ENG3                 0x02
50 #define LP5562_ENG1_IS_LOADING(mode)    \
51         ((mode & LP5562_MODE_ENG1_M) == LP5562_LOAD_ENG1)
52 #define LP5562_ENG2_IS_LOADING(mode)    \
53         ((mode & LP5562_MODE_ENG2_M) == LP5562_LOAD_ENG2)
54 #define LP5562_ENG3_IS_LOADING(mode)    \
55         ((mode & LP5562_MODE_ENG3_M) == LP5562_LOAD_ENG3)
56
57 /* BRIGHTNESS Registers */
58 #define LP5562_REG_R_PWM                0x04
59 #define LP5562_REG_G_PWM                0x03
60 #define LP5562_REG_B_PWM                0x02
61 #define LP5562_REG_W_PWM                0x0E
62
63 /* CURRENT Registers */
64 #define LP5562_REG_R_CURRENT            0x07
65 #define LP5562_REG_G_CURRENT            0x06
66 #define LP5562_REG_B_CURRENT            0x05
67 #define LP5562_REG_W_CURRENT            0x0F
68
69 /* CONFIG Register 08h */
70 #define LP5562_REG_CONFIG               0x08
71 #define LP5562_PWM_HF                   0x40
72 #define LP5562_PWRSAVE_EN               0x20
73 #define LP5562_CLK_INT                  0x01    /* Internal clock */
74 #define LP5562_DEFAULT_CFG              (LP5562_PWM_HF | LP5562_PWRSAVE_EN)
75
76 /* RESET Register 0Dh */
77 #define LP5562_REG_RESET                0x0D
78 #define LP5562_RESET                    0xFF
79
80 /* PROGRAM ENGINE Registers */
81 #define LP5562_REG_PROG_MEM_ENG1        0x10
82 #define LP5562_REG_PROG_MEM_ENG2        0x30
83 #define LP5562_REG_PROG_MEM_ENG3        0x50
84
85 /* LEDMAP Register 70h */
86 #define LP5562_REG_ENG_SEL              0x70
87 #define LP5562_ENG_SEL_PWM              0
88 #define LP5562_ENG_FOR_RGB_M            0x3F
89 #define LP5562_ENG_SEL_RGB              0x1B    /* R:ENG1, G:ENG2, B:ENG3 */
90 #define LP5562_ENG_FOR_W_M              0xC0
91 #define LP5562_ENG1_FOR_W               0x40    /* W:ENG1 */
92 #define LP5562_ENG2_FOR_W               0x80    /* W:ENG2 */
93 #define LP5562_ENG3_FOR_W               0xC0    /* W:ENG3 */
94
95 /* Program Commands */
96 #define LP5562_CMD_DISABLE              0x00
97 #define LP5562_CMD_LOAD                 0x15
98 #define LP5562_CMD_RUN                  0x2A
99 #define LP5562_CMD_DIRECT               0x3F
100 #define LP5562_PATTERN_OFF              0
101
102 static inline void lp5562_wait_opmode_done(void)
103 {
104         /* operation mode change needs to be longer than 153 us */
105         usleep_range(200, 300);
106 }
107
108 static inline void lp5562_wait_enable_done(void)
109 {
110         /* it takes more 488 us to update ENABLE register */
111         usleep_range(500, 600);
112 }
113
114 static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current)
115 {
116         static const u8 addr[] = {
117                 LP5562_REG_R_CURRENT,
118                 LP5562_REG_G_CURRENT,
119                 LP5562_REG_B_CURRENT,
120                 LP5562_REG_W_CURRENT,
121         };
122
123         led->led_current = led_current;
124         lp55xx_write(led->chip, addr[led->chan_nr], led_current);
125 }
126
127 static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
128 {
129         int ret;
130
131         /* stop engine */
132         if (!start) {
133                 lp55xx_write(chip, LP5562_REG_ENABLE, LP5562_ENABLE_DEFAULT);
134                 lp5562_wait_enable_done();
135                 lp55xx_stop_all_engine(chip);
136                 lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
137                 lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
138                 lp5562_wait_opmode_done();
139                 return;
140         }
141
142         ret = lp55xx_run_engine_common(chip);
143         if (!ret)
144                 lp5562_wait_enable_done();
145 }
146
147 static void lp5562_firmware_loaded(struct lp55xx_chip *chip)
148 {
149         const struct firmware *fw = chip->fw;
150
151         /*
152          * the firmware is encoded in ascii hex character, with 2 chars
153          * per byte
154          */
155         if (fw->size > (LP5562_PROGRAM_LENGTH * 2)) {
156                 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
157                         fw->size);
158                 return;
159         }
160
161         /*
162          * Program memory sequence
163          *  1) set engine mode to "LOAD"
164          *  2) write firmware data into program memory
165          */
166
167         lp55xx_load_engine(chip);
168         lp55xx_update_program_memory(chip, fw->data, fw->size);
169 }
170
171 static int lp5562_post_init_device(struct lp55xx_chip *chip)
172 {
173         int ret;
174         u8 cfg = LP5562_DEFAULT_CFG;
175
176         /* Set all PWMs to direct control mode */
177         ret = lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
178         if (ret)
179                 return ret;
180
181         lp5562_wait_opmode_done();
182
183         /* Update configuration for the clock setting */
184         if (!lp55xx_is_extclk_used(chip))
185                 cfg |= LP5562_CLK_INT;
186
187         ret = lp55xx_write(chip, LP5562_REG_CONFIG, cfg);
188         if (ret)
189                 return ret;
190
191         /* Initialize all channels PWM to zero -> leds off */
192         lp55xx_write(chip, LP5562_REG_R_PWM, 0);
193         lp55xx_write(chip, LP5562_REG_G_PWM, 0);
194         lp55xx_write(chip, LP5562_REG_B_PWM, 0);
195         lp55xx_write(chip, LP5562_REG_W_PWM, 0);
196
197         /* Set LED map as register PWM by default */
198         lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
199
200         return 0;
201 }
202
203 static int lp5562_led_brightness(struct lp55xx_led *led)
204 {
205         struct lp55xx_chip *chip = led->chip;
206         static const u8 addr[] = {
207                 LP5562_REG_R_PWM,
208                 LP5562_REG_G_PWM,
209                 LP5562_REG_B_PWM,
210                 LP5562_REG_W_PWM,
211         };
212         int ret;
213
214         mutex_lock(&chip->lock);
215         ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness);
216         mutex_unlock(&chip->lock);
217
218         return ret;
219 }
220
221 static void lp5562_write_program_memory(struct lp55xx_chip *chip,
222                                         u8 base, const u8 *rgb, int size)
223 {
224         int i;
225
226         if (!rgb || size <= 0)
227                 return;
228
229         for (i = 0; i < size; i++)
230                 lp55xx_write(chip, base + i, *(rgb + i));
231
232         lp55xx_write(chip, base + i, 0);
233         lp55xx_write(chip, base + i + 1, 0);
234 }
235
236 /* check the size of program count */
237 static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn)
238 {
239         return ptn->size_r >= LP5562_PROGRAM_LENGTH ||
240                ptn->size_g >= LP5562_PROGRAM_LENGTH ||
241                ptn->size_b >= LP5562_PROGRAM_LENGTH;
242 }
243
244 static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
245 {
246         struct lp55xx_predef_pattern *ptn;
247         int i;
248
249         if (mode == LP5562_PATTERN_OFF) {
250                 lp5562_run_engine(chip, false);
251                 return 0;
252         }
253
254         ptn = chip->pdata->patterns + (mode - 1);
255         if (!ptn || _is_pc_overflow(ptn)) {
256                 dev_err(&chip->cl->dev, "invalid pattern data\n");
257                 return -EINVAL;
258         }
259
260         lp55xx_stop_all_engine(chip);
261
262         /* Set LED map as RGB */
263         lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_RGB);
264
265         /* Load engines */
266         for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
267                 chip->engine_idx = i;
268                 lp55xx_load_engine(chip);
269         }
270
271         /* Clear program registers */
272         lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1, 0);
273         lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1 + 1, 0);
274         lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2, 0);
275         lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2 + 1, 0);
276         lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3, 0);
277         lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3 + 1, 0);
278
279         /* Program engines */
280         lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG1,
281                                 ptn->r, ptn->size_r);
282         lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG2,
283                                 ptn->g, ptn->size_g);
284         lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG3,
285                                 ptn->b, ptn->size_b);
286
287         /* Run engines */
288         lp5562_run_engine(chip, true);
289
290         return 0;
291 }
292
293 static ssize_t lp5562_store_pattern(struct device *dev,
294                                 struct device_attribute *attr,
295                                 const char *buf, size_t len)
296 {
297         struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
298         struct lp55xx_chip *chip = led->chip;
299         struct lp55xx_predef_pattern *ptn = chip->pdata->patterns;
300         int num_patterns = chip->pdata->num_patterns;
301         unsigned long mode;
302         int ret;
303
304         ret = kstrtoul(buf, 0, &mode);
305         if (ret)
306                 return ret;
307
308         if (mode > num_patterns || !ptn)
309                 return -EINVAL;
310
311         mutex_lock(&chip->lock);
312         ret = lp5562_run_predef_led_pattern(chip, mode);
313         mutex_unlock(&chip->lock);
314
315         if (ret)
316                 return ret;
317
318         return len;
319 }
320
321 static ssize_t lp5562_store_engine_mux(struct device *dev,
322                                      struct device_attribute *attr,
323                                      const char *buf, size_t len)
324 {
325         struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
326         struct lp55xx_chip *chip = led->chip;
327         u8 mask;
328         u8 val;
329
330         /* LED map
331          * R ... Engine 1 (fixed)
332          * G ... Engine 2 (fixed)
333          * B ... Engine 3 (fixed)
334          * W ... Engine 1 or 2 or 3
335          */
336
337         if (sysfs_streq(buf, "RGB")) {
338                 mask = LP5562_ENG_FOR_RGB_M;
339                 val = LP5562_ENG_SEL_RGB;
340         } else if (sysfs_streq(buf, "W")) {
341                 enum lp55xx_engine_index idx = chip->engine_idx;
342
343                 mask = LP5562_ENG_FOR_W_M;
344                 switch (idx) {
345                 case LP55XX_ENGINE_1:
346                         val = LP5562_ENG1_FOR_W;
347                         break;
348                 case LP55XX_ENGINE_2:
349                         val = LP5562_ENG2_FOR_W;
350                         break;
351                 case LP55XX_ENGINE_3:
352                         val = LP5562_ENG3_FOR_W;
353                         break;
354                 default:
355                         return -EINVAL;
356                 }
357
358         } else {
359                 dev_err(dev, "choose RGB or W\n");
360                 return -EINVAL;
361         }
362
363         mutex_lock(&chip->lock);
364         lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val);
365         mutex_unlock(&chip->lock);
366
367         return len;
368 }
369
370 static LP55XX_DEV_ATTR_WO(led_pattern, lp5562_store_pattern);
371 static LP55XX_DEV_ATTR_WO(engine_mux, lp5562_store_engine_mux);
372
373 static struct attribute *lp5562_attributes[] = {
374         &dev_attr_led_pattern.attr,
375         &dev_attr_engine_mux.attr,
376         NULL,
377 };
378
379 static const struct attribute_group lp5562_group = {
380         .attrs = lp5562_attributes,
381 };
382
383 /* Chip specific configurations */
384 static struct lp55xx_device_config lp5562_cfg = {
385         .max_channel  = LP5562_MAX_LEDS,
386         .reg_op_mode = {
387                 .addr = LP5562_REG_OP_MODE,
388         },
389         .reg_exec = {
390                 .addr = LP5562_REG_ENABLE,
391         },
392         .reset = {
393                 .addr = LP5562_REG_RESET,
394                 .val  = LP5562_RESET,
395         },
396         .enable = {
397                 .addr = LP5562_REG_ENABLE,
398                 .val  = LP5562_ENABLE_DEFAULT,
399         },
400         .prog_mem_base = {
401                 .addr = LP5562_REG_PROG_MEM_ENG1,
402         },
403         .post_init_device   = lp5562_post_init_device,
404         .set_led_current    = lp5562_set_led_current,
405         .brightness_fn      = lp5562_led_brightness,
406         .run_engine         = lp5562_run_engine,
407         .firmware_cb        = lp5562_firmware_loaded,
408         .dev_attr_group     = &lp5562_group,
409 };
410
411 static const struct i2c_device_id lp5562_id[] = {
412         { "lp5562", .driver_data = (kernel_ulong_t)&lp5562_cfg, },
413         { }
414 };
415 MODULE_DEVICE_TABLE(i2c, lp5562_id);
416
417 static const struct of_device_id of_lp5562_leds_match[] = {
418         { .compatible = "ti,lp5562", .data = &lp5562_cfg, },
419         {},
420 };
421
422 MODULE_DEVICE_TABLE(of, of_lp5562_leds_match);
423
424 static struct i2c_driver lp5562_driver = {
425         .driver = {
426                 .name   = "lp5562",
427                 .of_match_table = of_lp5562_leds_match,
428         },
429         .probe          = lp55xx_probe,
430         .remove         = lp55xx_remove,
431         .id_table       = lp5562_id,
432 };
433
434 module_i2c_driver(lp5562_driver);
435
436 MODULE_DESCRIPTION("Texas Instruments LP5562 LED Driver");
437 MODULE_AUTHOR("Milo Kim");
438 MODULE_LICENSE("GPL");
This page took 0.061276 seconds and 4 git commands to generate.