]> Git Repo - linux.git/blob - drivers/leds/leds-lp5562.c
Merge tag 'for-6.11/block-20240722' of git://git.kernel.dk/linux
[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/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>
20
21 #include "leds-lp55xx-common.h"
22
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
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
47
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
53
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)
60
61 /* RESET Register 0Dh */
62 #define LP5562_REG_RESET                0x0D
63 #define LP5562_RESET                    0xFF
64
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
69
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 */
79
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
86
87 static inline void lp5562_wait_opmode_done(void)
88 {
89         /* operation mode change needs to be longer than 153 us */
90         usleep_range(200, 300);
91 }
92
93 static inline void lp5562_wait_enable_done(void)
94 {
95         /* it takes more 488 us to update ENABLE register */
96         usleep_range(500, 600);
97 }
98
99 static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current)
100 {
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,
106         };
107
108         led->led_current = led_current;
109         lp55xx_write(led->chip, addr[led->chan_nr], led_current);
110 }
111
112 static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
113 {
114         int ret;
115
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;
125         }
126
127         ret = lp55xx_run_engine_common(chip);
128         if (!ret)
129                 lp5562_wait_enable_done();
130 }
131
132 static int lp5562_post_init_device(struct lp55xx_chip *chip)
133 {
134         int ret;
135         u8 cfg = LP5562_DEFAULT_CFG;
136
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;
141
142         lp5562_wait_opmode_done();
143
144         /* Update configuration for the clock setting */
145         if (!lp55xx_is_extclk_used(chip))
146                 cfg |= LP5562_CLK_INT;
147
148         ret = lp55xx_write(chip, LP5562_REG_CONFIG, cfg);
149         if (ret)
150                 return ret;
151
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);
157
158         /* Set LED map as register PWM by default */
159         lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
160
161         return 0;
162 }
163
164 static int lp5562_led_brightness(struct lp55xx_led *led)
165 {
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,
172         };
173         int ret;
174
175         guard(mutex)(&chip->lock);
176
177         ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness);
178
179         return ret;
180 }
181
182 static void lp5562_write_program_memory(struct lp55xx_chip *chip,
183                                         u8 base, const u8 *rgb, int size)
184 {
185         int i;
186
187         if (!rgb || size <= 0)
188                 return;
189
190         for (i = 0; i < size; i++)
191                 lp55xx_write(chip, base + i, *(rgb + i));
192
193         lp55xx_write(chip, base + i, 0);
194         lp55xx_write(chip, base + i + 1, 0);
195 }
196
197 /* check the size of program count */
198 static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn)
199 {
200         return ptn->size_r >= LP55xx_BYTES_PER_PAGE ||
201                ptn->size_g >= LP55xx_BYTES_PER_PAGE ||
202                ptn->size_b >= LP55xx_BYTES_PER_PAGE;
203 }
204
205 static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
206 {
207         struct lp55xx_predef_pattern *ptn;
208         int i;
209
210         if (mode == LP5562_PATTERN_OFF) {
211                 lp5562_run_engine(chip, false);
212                 return 0;
213         }
214
215         ptn = chip->pdata->patterns + (mode - 1);
216         if (!ptn || _is_pc_overflow(ptn)) {
217                 dev_err(&chip->cl->dev, "invalid pattern data\n");
218                 return -EINVAL;
219         }
220
221         lp55xx_stop_all_engine(chip);
222
223         /* Set LED map as RGB */
224         lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_RGB);
225
226         /* Load engines */
227         for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
228                 chip->engine_idx = i;
229                 lp55xx_load_engine(chip);
230         }
231
232         /* Clear program registers */
233         lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1, 0);
234         lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1 + 1, 0);
235         lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2, 0);
236         lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2 + 1, 0);
237         lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3, 0);
238         lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3 + 1, 0);
239
240         /* Program engines */
241         lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG1,
242                                 ptn->r, ptn->size_r);
243         lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG2,
244                                 ptn->g, ptn->size_g);
245         lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG3,
246                                 ptn->b, ptn->size_b);
247
248         /* Run engines */
249         lp5562_run_engine(chip, true);
250
251         return 0;
252 }
253
254 static ssize_t lp5562_store_pattern(struct device *dev,
255                                 struct device_attribute *attr,
256                                 const char *buf, size_t len)
257 {
258         struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
259         struct lp55xx_chip *chip = led->chip;
260         struct lp55xx_predef_pattern *ptn = chip->pdata->patterns;
261         int num_patterns = chip->pdata->num_patterns;
262         unsigned long mode;
263         int ret;
264
265         ret = kstrtoul(buf, 0, &mode);
266         if (ret)
267                 return ret;
268
269         if (mode > num_patterns || !ptn)
270                 return -EINVAL;
271
272         guard(mutex)(&chip->lock);
273
274         ret = lp5562_run_predef_led_pattern(chip, mode);
275
276         if (ret)
277                 return ret;
278
279         return len;
280 }
281
282 static ssize_t lp5562_store_engine_mux(struct device *dev,
283                                      struct device_attribute *attr,
284                                      const char *buf, size_t len)
285 {
286         struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
287         struct lp55xx_chip *chip = led->chip;
288         u8 mask;
289         u8 val;
290
291         /* LED map
292          * R ... Engine 1 (fixed)
293          * G ... Engine 2 (fixed)
294          * B ... Engine 3 (fixed)
295          * W ... Engine 1 or 2 or 3
296          */
297
298         if (sysfs_streq(buf, "RGB")) {
299                 mask = LP5562_ENG_FOR_RGB_M;
300                 val = LP5562_ENG_SEL_RGB;
301         } else if (sysfs_streq(buf, "W")) {
302                 enum lp55xx_engine_index idx = chip->engine_idx;
303
304                 mask = LP5562_ENG_FOR_W_M;
305                 switch (idx) {
306                 case LP55XX_ENGINE_1:
307                         val = LP5562_ENG1_FOR_W;
308                         break;
309                 case LP55XX_ENGINE_2:
310                         val = LP5562_ENG2_FOR_W;
311                         break;
312                 case LP55XX_ENGINE_3:
313                         val = LP5562_ENG3_FOR_W;
314                         break;
315                 default:
316                         return -EINVAL;
317                 }
318
319         } else {
320                 dev_err(dev, "choose RGB or W\n");
321                 return -EINVAL;
322         }
323
324         guard(mutex)(&chip->lock);
325
326         lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val);
327
328         return len;
329 }
330
331 static LP55XX_DEV_ATTR_WO(led_pattern, lp5562_store_pattern);
332 static LP55XX_DEV_ATTR_WO(engine_mux, lp5562_store_engine_mux);
333
334 static struct attribute *lp5562_attributes[] = {
335         &dev_attr_led_pattern.attr,
336         &dev_attr_engine_mux.attr,
337         NULL,
338 };
339
340 static const struct attribute_group lp5562_group = {
341         .attrs = lp5562_attributes,
342 };
343
344 /* Chip specific configurations */
345 static struct lp55xx_device_config lp5562_cfg = {
346         .max_channel  = LP5562_MAX_LEDS,
347         .reg_op_mode = {
348                 .addr = LP5562_REG_OP_MODE,
349         },
350         .reg_exec = {
351                 .addr = LP5562_REG_ENABLE,
352         },
353         .reset = {
354                 .addr = LP5562_REG_RESET,
355                 .val  = LP5562_RESET,
356         },
357         .enable = {
358                 .addr = LP5562_REG_ENABLE,
359                 .val  = LP5562_ENABLE_DEFAULT,
360         },
361         .prog_mem_base = {
362                 .addr = LP5562_REG_PROG_MEM_ENG1,
363         },
364         .post_init_device   = lp5562_post_init_device,
365         .set_led_current    = lp5562_set_led_current,
366         .brightness_fn      = lp5562_led_brightness,
367         .run_engine         = lp5562_run_engine,
368         .firmware_cb        = lp55xx_firmware_loaded_cb,
369         .dev_attr_group     = &lp5562_group,
370 };
371
372 static const struct i2c_device_id lp5562_id[] = {
373         { "lp5562", .driver_data = (kernel_ulong_t)&lp5562_cfg, },
374         { }
375 };
376 MODULE_DEVICE_TABLE(i2c, lp5562_id);
377
378 static const struct of_device_id of_lp5562_leds_match[] = {
379         { .compatible = "ti,lp5562", .data = &lp5562_cfg, },
380         {},
381 };
382
383 MODULE_DEVICE_TABLE(of, of_lp5562_leds_match);
384
385 static struct i2c_driver lp5562_driver = {
386         .driver = {
387                 .name   = "lp5562",
388                 .of_match_table = of_lp5562_leds_match,
389         },
390         .probe          = lp55xx_probe,
391         .remove         = lp55xx_remove,
392         .id_table       = lp5562_id,
393 };
394
395 module_i2c_driver(lp5562_driver);
396
397 MODULE_DESCRIPTION("Texas Instruments LP5562 LED Driver");
398 MODULE_AUTHOR("Milo Kim");
399 MODULE_LICENSE("GPL");
This page took 0.059303 seconds and 4 git commands to generate.