OSDN Git Service

tick/broadcast: Prevent NULL pointer dereference
[uclinux-h8/linux.git] / drivers / leds / leds-max77693.c
1 /*
2  * LED Flash class driver for the flash cell of max77693 mfd.
3  *
4  *      Copyright (C) 2015, Samsung Electronics Co., Ltd.
5  *
6  *      Authors: Jacek Anaszewski <j.anaszewski@samsung.com>
7  *               Andrzej Hajda <a.hajda@samsung.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  */
13
14 #include <linux/led-class-flash.h>
15 #include <linux/mfd/max77693.h>
16 #include <linux/mfd/max77693-private.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
22 #include <linux/workqueue.h>
23 #include <media/v4l2-flash-led-class.h>
24
25 #define MODE_OFF                0
26 #define MODE_FLASH(a)           (1 << (a))
27 #define MODE_TORCH(a)           (1 << (2 + (a)))
28 #define MODE_FLASH_EXTERNAL(a)  (1 << (4 + (a)))
29
30 #define MODE_FLASH_MASK         (MODE_FLASH(FLED1) | MODE_FLASH(FLED2) | \
31                                  MODE_FLASH_EXTERNAL(FLED1) | \
32                                  MODE_FLASH_EXTERNAL(FLED2))
33 #define MODE_TORCH_MASK         (MODE_TORCH(FLED1) | MODE_TORCH(FLED2))
34
35 #define FLED1_IOUT              (1 << 0)
36 #define FLED2_IOUT              (1 << 1)
37
38 enum max77693_fled {
39         FLED1,
40         FLED2,
41 };
42
43 enum max77693_led_mode {
44         FLASH,
45         TORCH,
46 };
47
48 struct max77693_led_config_data {
49         const char *label[2];
50         u32 iout_torch_max[2];
51         u32 iout_flash_max[2];
52         u32 flash_timeout_max[2];
53         u32 num_leds;
54         u32 boost_mode;
55         u32 boost_vout;
56         u32 low_vsys;
57 };
58
59 struct max77693_sub_led {
60         /* corresponding FLED output identifier */
61         int fled_id;
62         /* corresponding LED Flash class device */
63         struct led_classdev_flash fled_cdev;
64         /* assures led-triggers compatibility */
65         struct work_struct work_brightness_set;
66         /* V4L2 Flash device */
67         struct v4l2_flash *v4l2_flash;
68
69         /* brightness cache */
70         unsigned int torch_brightness;
71         /* flash timeout cache */
72         unsigned int flash_timeout;
73         /* flash faults that may have occurred */
74         u32 flash_faults;
75 };
76
77 struct max77693_led_device {
78         /* parent mfd regmap */
79         struct regmap *regmap;
80         /* platform device data */
81         struct platform_device *pdev;
82         /* secures access to the device */
83         struct mutex lock;
84
85         /* sub led data */
86         struct max77693_sub_led sub_leds[2];
87
88         /* maximum torch current values for FLED outputs */
89         u32 iout_torch_max[2];
90         /* maximum flash current values for FLED outputs */
91         u32 iout_flash_max[2];
92
93         /* current flash timeout cache */
94         unsigned int current_flash_timeout;
95         /* ITORCH register cache */
96         u8 torch_iout_reg;
97         /* mode of fled outputs */
98         unsigned int mode_flags;
99         /* recently strobed fled */
100         int strobing_sub_led_id;
101         /* bitmask of FLED outputs use state (bit 0. - FLED1, bit 1. - FLED2) */
102         u8 fled_mask;
103         /* FLED modes that can be set */
104         u8 allowed_modes;
105
106         /* arrangement of current outputs */
107         bool iout_joint;
108 };
109
110 static u8 max77693_led_iout_to_reg(u32 ua)
111 {
112         if (ua < FLASH_IOUT_MIN)
113                 ua = FLASH_IOUT_MIN;
114         return (ua - FLASH_IOUT_MIN) / FLASH_IOUT_STEP;
115 }
116
117 static u8 max77693_flash_timeout_to_reg(u32 us)
118 {
119         return (us - FLASH_TIMEOUT_MIN) / FLASH_TIMEOUT_STEP;
120 }
121
122 static inline struct max77693_sub_led *flcdev_to_sub_led(
123                                         struct led_classdev_flash *fled_cdev)
124 {
125         return container_of(fled_cdev, struct max77693_sub_led, fled_cdev);
126 }
127
128 static inline struct max77693_led_device *sub_led_to_led(
129                                         struct max77693_sub_led *sub_led)
130 {
131         return container_of(sub_led, struct max77693_led_device,
132                                 sub_leds[sub_led->fled_id]);
133 }
134
135 static inline u8 max77693_led_vsys_to_reg(u32 mv)
136 {
137         return ((mv - MAX_FLASH1_VSYS_MIN) / MAX_FLASH1_VSYS_STEP) << 2;
138 }
139
140 static inline u8 max77693_led_vout_to_reg(u32 mv)
141 {
142         return (mv - FLASH_VOUT_MIN) / FLASH_VOUT_STEP + FLASH_VOUT_RMIN;
143 }
144
145 static inline bool max77693_fled_used(struct max77693_led_device *led,
146                                          int fled_id)
147 {
148         u8 fled_bit = (fled_id == FLED1) ? FLED1_IOUT : FLED2_IOUT;
149
150         return led->fled_mask & fled_bit;
151 }
152
153 static int max77693_set_mode_reg(struct max77693_led_device *led, u8 mode)
154 {
155         struct regmap *rmap = led->regmap;
156         int ret, v = 0, i;
157
158         for (i = FLED1; i <= FLED2; ++i) {
159                 if (mode & MODE_TORCH(i))
160                         v |= FLASH_EN_ON << TORCH_EN_SHIFT(i);
161
162                 if (mode & MODE_FLASH(i)) {
163                         v |= FLASH_EN_ON << FLASH_EN_SHIFT(i);
164                 } else if (mode & MODE_FLASH_EXTERNAL(i)) {
165                         v |= FLASH_EN_FLASH << FLASH_EN_SHIFT(i);
166                         /*
167                          * Enable hw triggering also for torch mode, as some
168                          * camera sensors use torch led to fathom ambient light
169                          * conditions before strobing the flash.
170                          */
171                         v |= FLASH_EN_TORCH << TORCH_EN_SHIFT(i);
172                 }
173         }
174
175         /* Reset the register only prior setting flash modes */
176         if (mode & ~(MODE_TORCH(FLED1) | MODE_TORCH(FLED2))) {
177                 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, 0);
178                 if (ret < 0)
179                         return ret;
180         }
181
182         return regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, v);
183 }
184
185 static int max77693_add_mode(struct max77693_led_device *led, u8 mode)
186 {
187         u8 new_mode_flags;
188         int i, ret;
189
190         if (led->iout_joint)
191                 /* Span the mode on FLED2 for joint iouts case */
192                 mode |= (mode << 1);
193
194         /*
195          * FLASH_EXTERNAL mode activates FLASHEN and TORCHEN pins in the device.
196          * Corresponding register bit fields interfere with SW triggered modes,
197          * thus clear them to ensure proper device configuration.
198          */
199         for (i = FLED1; i <= FLED2; ++i)
200                 if (mode & MODE_FLASH_EXTERNAL(i))
201                         led->mode_flags &= (~MODE_TORCH(i) & ~MODE_FLASH(i));
202
203         new_mode_flags = mode | led->mode_flags;
204         new_mode_flags &= led->allowed_modes;
205
206         if (new_mode_flags ^ led->mode_flags)
207                 led->mode_flags = new_mode_flags;
208         else
209                 return 0;
210
211         ret = max77693_set_mode_reg(led, led->mode_flags);
212         if (ret < 0)
213                 return ret;
214
215         /*
216          * Clear flash mode flag after setting the mode to avoid spurious flash
217          * strobing on each subsequent torch mode setting.
218          */
219         if (mode & MODE_FLASH_MASK)
220                 led->mode_flags &= ~mode;
221
222         return ret;
223 }
224
225 static int max77693_clear_mode(struct max77693_led_device *led,
226                                 u8 mode)
227 {
228         if (led->iout_joint)
229                 /* Clear mode also on FLED2 for joint iouts case */
230                 mode |= (mode << 1);
231
232         led->mode_flags &= ~mode;
233
234         return max77693_set_mode_reg(led, led->mode_flags);
235 }
236
237 static void max77693_add_allowed_modes(struct max77693_led_device *led,
238                                 int fled_id, enum max77693_led_mode mode)
239 {
240         if (mode == FLASH)
241                 led->allowed_modes |= (MODE_FLASH(fled_id) |
242                                        MODE_FLASH_EXTERNAL(fled_id));
243         else
244                 led->allowed_modes |= MODE_TORCH(fled_id);
245 }
246
247 static void max77693_distribute_currents(struct max77693_led_device *led,
248                                 int fled_id, enum max77693_led_mode mode,
249                                 u32 micro_amp, u32 iout_max[2], u32 iout[2])
250 {
251         if (!led->iout_joint) {
252                 iout[fled_id] = micro_amp;
253                 max77693_add_allowed_modes(led, fled_id, mode);
254                 return;
255         }
256
257         iout[FLED1] = min(micro_amp, iout_max[FLED1]);
258         iout[FLED2] = micro_amp - iout[FLED1];
259
260         if (mode == FLASH)
261                 led->allowed_modes &= ~MODE_FLASH_MASK;
262         else
263                 led->allowed_modes &= ~MODE_TORCH_MASK;
264
265         max77693_add_allowed_modes(led, FLED1, mode);
266
267         if (iout[FLED2])
268                 max77693_add_allowed_modes(led, FLED2, mode);
269 }
270
271 static int max77693_set_torch_current(struct max77693_led_device *led,
272                                 int fled_id, u32 micro_amp)
273 {
274         struct regmap *rmap = led->regmap;
275         u8 iout1_reg = 0, iout2_reg = 0;
276         u32 iout[2];
277
278         max77693_distribute_currents(led, fled_id, TORCH, micro_amp,
279                                         led->iout_torch_max, iout);
280
281         if (fled_id == FLED1 || led->iout_joint) {
282                 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
283                 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT2_SHIFT);
284         }
285         if (fled_id == FLED2 || led->iout_joint) {
286                 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
287                 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT1_SHIFT);
288         }
289
290         led->torch_iout_reg |= ((iout1_reg << TORCH_IOUT1_SHIFT) |
291                                 (iout2_reg << TORCH_IOUT2_SHIFT));
292
293         return regmap_write(rmap, MAX77693_LED_REG_ITORCH,
294                                                 led->torch_iout_reg);
295 }
296
297 static int max77693_set_flash_current(struct max77693_led_device *led,
298                                         int fled_id,
299                                         u32 micro_amp)
300 {
301         struct regmap *rmap = led->regmap;
302         u8 iout1_reg, iout2_reg;
303         u32 iout[2];
304         int ret = -EINVAL;
305
306         max77693_distribute_currents(led, fled_id, FLASH, micro_amp,
307                                         led->iout_flash_max, iout);
308
309         if (fled_id == FLED1 || led->iout_joint) {
310                 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
311                 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH1,
312                                                         iout1_reg);
313                 if (ret < 0)
314                         return ret;
315         }
316         if (fled_id == FLED2 || led->iout_joint) {
317                 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
318                 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH2,
319                                                         iout2_reg);
320         }
321
322         return ret;
323 }
324
325 static int max77693_set_timeout(struct max77693_led_device *led, u32 microsec)
326 {
327         struct regmap *rmap = led->regmap;
328         u8 v;
329         int ret;
330
331         v = max77693_flash_timeout_to_reg(microsec) | FLASH_TMR_LEVEL;
332
333         ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_TIMER, v);
334         if (ret < 0)
335                 return ret;
336
337         led->current_flash_timeout = microsec;
338
339         return 0;
340 }
341
342 static int max77693_get_strobe_status(struct max77693_led_device *led,
343                                         bool *state)
344 {
345         struct regmap *rmap = led->regmap;
346         unsigned int v;
347         int ret;
348
349         ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_STATUS, &v);
350         if (ret < 0)
351                 return ret;
352
353         *state = v & FLASH_STATUS_FLASH_ON;
354
355         return ret;
356 }
357
358 static int max77693_get_flash_faults(struct max77693_sub_led *sub_led)
359 {
360         struct max77693_led_device *led = sub_led_to_led(sub_led);
361         struct regmap *rmap = led->regmap;
362         unsigned int v;
363         u8 fault_open_mask, fault_short_mask;
364         int ret;
365
366         sub_led->flash_faults = 0;
367
368         if (led->iout_joint) {
369                 fault_open_mask = FLASH_INT_FLED1_OPEN | FLASH_INT_FLED2_OPEN;
370                 fault_short_mask = FLASH_INT_FLED1_SHORT |
371                                                         FLASH_INT_FLED2_SHORT;
372         } else {
373                 fault_open_mask = (sub_led->fled_id == FLED1) ?
374                                                 FLASH_INT_FLED1_OPEN :
375                                                 FLASH_INT_FLED2_OPEN;
376                 fault_short_mask = (sub_led->fled_id == FLED1) ?
377                                                 FLASH_INT_FLED1_SHORT :
378                                                 FLASH_INT_FLED2_SHORT;
379         }
380
381         ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_INT, &v);
382         if (ret < 0)
383                 return ret;
384
385         if (v & fault_open_mask)
386                 sub_led->flash_faults |= LED_FAULT_OVER_VOLTAGE;
387         if (v & fault_short_mask)
388                 sub_led->flash_faults |= LED_FAULT_SHORT_CIRCUIT;
389         if (v & FLASH_INT_OVER_CURRENT)
390                 sub_led->flash_faults |= LED_FAULT_OVER_CURRENT;
391
392         return 0;
393 }
394
395 static int max77693_setup(struct max77693_led_device *led,
396                          struct max77693_led_config_data *led_cfg)
397 {
398         struct regmap *rmap = led->regmap;
399         int i, first_led, last_led, ret;
400         u32 max_flash_curr[2];
401         u8 v;
402
403         /*
404          * Initialize only flash current. Torch current doesn't
405          * require initialization as ITORCH register is written with
406          * new value each time brightness_set op is called.
407          */
408         if (led->iout_joint) {
409                 first_led = FLED1;
410                 last_led = FLED1;
411                 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1] +
412                                         led_cfg->iout_flash_max[FLED2];
413         } else {
414                 first_led = max77693_fled_used(led, FLED1) ? FLED1 : FLED2;
415                 last_led = max77693_fled_used(led, FLED2) ? FLED2 : FLED1;
416                 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1];
417                 max_flash_curr[FLED2] = led_cfg->iout_flash_max[FLED2];
418         }
419
420         for (i = first_led; i <= last_led; ++i) {
421                 ret = max77693_set_flash_current(led, i,
422                                         max_flash_curr[i]);
423                 if (ret < 0)
424                         return ret;
425         }
426
427         v = TORCH_TMR_NO_TIMER | MAX77693_LED_TRIG_TYPE_LEVEL;
428         ret = regmap_write(rmap, MAX77693_LED_REG_ITORCHTIMER, v);
429         if (ret < 0)
430                 return ret;
431
432         if (led_cfg->low_vsys > 0)
433                 v = max77693_led_vsys_to_reg(led_cfg->low_vsys) |
434                                                 MAX_FLASH1_MAX_FL_EN;
435         else
436                 v = 0;
437
438         ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH1, v);
439         if (ret < 0)
440                 return ret;
441         ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH2, 0);
442         if (ret < 0)
443                 return ret;
444
445         if (led_cfg->boost_mode == MAX77693_LED_BOOST_FIXED)
446                 v = FLASH_BOOST_FIXED;
447         else
448                 v = led_cfg->boost_mode | led_cfg->boost_mode << 1;
449
450         if (max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
451                 v |= FLASH_BOOST_LEDNUM_2;
452
453         ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_CNTL, v);
454         if (ret < 0)
455                 return ret;
456
457         v = max77693_led_vout_to_reg(led_cfg->boost_vout);
458         ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_FLASH1, v);
459         if (ret < 0)
460                 return ret;
461
462         return max77693_set_mode_reg(led, MODE_OFF);
463 }
464
465 static int __max77693_led_brightness_set(struct max77693_led_device *led,
466                                         int fled_id, enum led_brightness value)
467 {
468         int ret;
469
470         mutex_lock(&led->lock);
471
472         if (value == 0) {
473                 ret = max77693_clear_mode(led, MODE_TORCH(fled_id));
474                 if (ret < 0)
475                         dev_dbg(&led->pdev->dev,
476                                 "Failed to clear torch mode (%d)\n",
477                                 ret);
478                 goto unlock;
479         }
480
481         ret = max77693_set_torch_current(led, fled_id, value * TORCH_IOUT_STEP);
482         if (ret < 0) {
483                 dev_dbg(&led->pdev->dev,
484                         "Failed to set torch current (%d)\n",
485                         ret);
486                 goto unlock;
487         }
488
489         ret = max77693_add_mode(led, MODE_TORCH(fled_id));
490         if (ret < 0)
491                 dev_dbg(&led->pdev->dev,
492                         "Failed to set torch mode (%d)\n",
493                         ret);
494 unlock:
495         mutex_unlock(&led->lock);
496         return ret;
497 }
498
499 static void max77693_led_brightness_set_work(
500                                         struct work_struct *work)
501 {
502         struct max77693_sub_led *sub_led =
503                         container_of(work, struct max77693_sub_led,
504                                         work_brightness_set);
505         struct max77693_led_device *led = sub_led_to_led(sub_led);
506
507         __max77693_led_brightness_set(led, sub_led->fled_id,
508                                 sub_led->torch_brightness);
509 }
510
511 /* LED subsystem callbacks */
512
513 static int max77693_led_brightness_set_sync(
514                                 struct led_classdev *led_cdev,
515                                 enum led_brightness value)
516 {
517         struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
518         struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
519         struct max77693_led_device *led = sub_led_to_led(sub_led);
520
521         return __max77693_led_brightness_set(led, sub_led->fled_id, value);
522 }
523
524 static void max77693_led_brightness_set(
525                                 struct led_classdev *led_cdev,
526                                 enum led_brightness value)
527 {
528         struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
529         struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
530
531         sub_led->torch_brightness = value;
532         schedule_work(&sub_led->work_brightness_set);
533 }
534
535 static int max77693_led_flash_brightness_set(
536                                 struct led_classdev_flash *fled_cdev,
537                                 u32 brightness)
538 {
539         struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
540         struct max77693_led_device *led = sub_led_to_led(sub_led);
541         int ret;
542
543         mutex_lock(&led->lock);
544         ret = max77693_set_flash_current(led, sub_led->fled_id, brightness);
545         mutex_unlock(&led->lock);
546
547         return ret;
548 }
549
550 static int max77693_led_flash_strobe_set(
551                                 struct led_classdev_flash *fled_cdev,
552                                 bool state)
553 {
554         struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
555         struct max77693_led_device *led = sub_led_to_led(sub_led);
556         int fled_id = sub_led->fled_id;
557         int ret;
558
559         mutex_lock(&led->lock);
560
561         if (!state) {
562                 ret = max77693_clear_mode(led, MODE_FLASH(fled_id));
563                 goto unlock;
564         }
565
566         if (sub_led->flash_timeout != led->current_flash_timeout) {
567                 ret = max77693_set_timeout(led, sub_led->flash_timeout);
568                 if (ret < 0)
569                         goto unlock;
570         }
571
572         led->strobing_sub_led_id = fled_id;
573
574         ret = max77693_add_mode(led, MODE_FLASH(fled_id));
575         if (ret < 0)
576                 goto unlock;
577
578         ret = max77693_get_flash_faults(sub_led);
579
580 unlock:
581         mutex_unlock(&led->lock);
582         return ret;
583 }
584
585 static int max77693_led_flash_fault_get(
586                                 struct led_classdev_flash *fled_cdev,
587                                 u32 *fault)
588 {
589         struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
590
591         *fault = sub_led->flash_faults;
592
593         return 0;
594 }
595
596 static int max77693_led_flash_strobe_get(
597                                 struct led_classdev_flash *fled_cdev,
598                                 bool *state)
599 {
600         struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
601         struct max77693_led_device *led = sub_led_to_led(sub_led);
602         int ret;
603
604         if (!state)
605                 return -EINVAL;
606
607         mutex_lock(&led->lock);
608
609         ret = max77693_get_strobe_status(led, state);
610
611         *state = !!(*state && (led->strobing_sub_led_id == sub_led->fled_id));
612
613         mutex_unlock(&led->lock);
614
615         return ret;
616 }
617
618 static int max77693_led_flash_timeout_set(
619                                 struct led_classdev_flash *fled_cdev,
620                                 u32 timeout)
621 {
622         struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
623         struct max77693_led_device *led = sub_led_to_led(sub_led);
624
625         mutex_lock(&led->lock);
626         sub_led->flash_timeout = timeout;
627         mutex_unlock(&led->lock);
628
629         return 0;
630 }
631
632 static int max77693_led_parse_dt(struct max77693_led_device *led,
633                                 struct max77693_led_config_data *cfg,
634                                 struct device_node **sub_nodes)
635 {
636         struct device *dev = &led->pdev->dev;
637         struct max77693_sub_led *sub_leds = led->sub_leds;
638         struct device_node *node = dev->of_node, *child_node;
639         struct property *prop;
640         u32 led_sources[2];
641         int i, ret, fled_id;
642
643         of_property_read_u32(node, "maxim,boost-mode", &cfg->boost_mode);
644         of_property_read_u32(node, "maxim,boost-mvout", &cfg->boost_vout);
645         of_property_read_u32(node, "maxim,mvsys-min", &cfg->low_vsys);
646
647         for_each_available_child_of_node(node, child_node) {
648                 prop = of_find_property(child_node, "led-sources", NULL);
649                 if (prop) {
650                         const __be32 *srcs = NULL;
651
652                         for (i = 0; i < ARRAY_SIZE(led_sources); ++i) {
653                                 srcs = of_prop_next_u32(prop, srcs,
654                                                         &led_sources[i]);
655                                 if (!srcs)
656                                         break;
657                         }
658                 } else {
659                         dev_err(dev,
660                                 "led-sources DT property missing\n");
661                         of_node_put(child_node);
662                         return -EINVAL;
663                 }
664
665                 if (i == 2) {
666                         fled_id = FLED1;
667                         led->fled_mask = FLED1_IOUT | FLED2_IOUT;
668                 } else if (led_sources[0] == FLED1) {
669                         fled_id = FLED1;
670                         led->fled_mask |= FLED1_IOUT;
671                 } else if (led_sources[0] == FLED2) {
672                         fled_id = FLED2;
673                         led->fled_mask |= FLED2_IOUT;
674                 } else {
675                         dev_err(dev,
676                                 "Wrong led-sources DT property value.\n");
677                         of_node_put(child_node);
678                         return -EINVAL;
679                 }
680
681                 if (sub_nodes[fled_id]) {
682                         dev_err(dev,
683                                 "Conflicting \"led-sources\" DT properties\n");
684                         return -EINVAL;
685                 }
686
687                 sub_nodes[fled_id] = child_node;
688                 sub_leds[fled_id].fled_id = fled_id;
689
690                 cfg->label[fled_id] =
691                         of_get_property(child_node, "label", NULL) ? :
692                                                 child_node->name;
693
694                 ret = of_property_read_u32(child_node, "led-max-microamp",
695                                         &cfg->iout_torch_max[fled_id]);
696                 if (ret < 0) {
697                         cfg->iout_torch_max[fled_id] = TORCH_IOUT_MIN;
698                         dev_warn(dev, "led-max-microamp DT property missing\n");
699                 }
700
701                 ret = of_property_read_u32(child_node, "flash-max-microamp",
702                                         &cfg->iout_flash_max[fled_id]);
703                 if (ret < 0) {
704                         cfg->iout_flash_max[fled_id] = FLASH_IOUT_MIN;
705                         dev_warn(dev,
706                                  "flash-max-microamp DT property missing\n");
707                 }
708
709                 ret = of_property_read_u32(child_node, "flash-max-timeout-us",
710                                         &cfg->flash_timeout_max[fled_id]);
711                 if (ret < 0) {
712                         cfg->flash_timeout_max[fled_id] = FLASH_TIMEOUT_MIN;
713                         dev_warn(dev,
714                                  "flash-max-timeout-us DT property missing\n");
715                 }
716
717                 if (++cfg->num_leds == 2 ||
718                     (max77693_fled_used(led, FLED1) &&
719                      max77693_fled_used(led, FLED2))) {
720                         of_node_put(child_node);
721                         break;
722                 }
723         }
724
725         if (cfg->num_leds == 0) {
726                 dev_err(dev, "No DT child node found for connected LED(s).\n");
727                 return -EINVAL;
728         }
729
730         return 0;
731 }
732
733 static void clamp_align(u32 *v, u32 min, u32 max, u32 step)
734 {
735         *v = clamp_val(*v, min, max);
736         if (step > 1)
737                 *v = (*v - min) / step * step + min;
738 }
739
740 static void max77693_align_iout_current(struct max77693_led_device *led,
741                                         u32 *iout, u32 min, u32 max, u32 step)
742 {
743         int i;
744
745         if (led->iout_joint) {
746                 if (iout[FLED1] > min) {
747                         iout[FLED1] /= 2;
748                         iout[FLED2] = iout[FLED1];
749                 } else {
750                         iout[FLED1] = min;
751                         iout[FLED2] = 0;
752                         return;
753                 }
754         }
755
756         for (i = FLED1; i <= FLED2; ++i)
757                 if (max77693_fled_used(led, i))
758                         clamp_align(&iout[i], min, max, step);
759                 else
760                         iout[i] = 0;
761 }
762
763 static void max77693_led_validate_configuration(struct max77693_led_device *led,
764                                         struct max77693_led_config_data *cfg)
765 {
766         u32 flash_iout_max = cfg->boost_mode ? FLASH_IOUT_MAX_2LEDS :
767                                                FLASH_IOUT_MAX_1LED;
768         int i;
769
770         if (cfg->num_leds == 1 &&
771             max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
772                 led->iout_joint = true;
773
774         cfg->boost_mode = clamp_val(cfg->boost_mode, MAX77693_LED_BOOST_NONE,
775                             MAX77693_LED_BOOST_FIXED);
776
777         /* Boost must be enabled if both current outputs are used */
778         if ((cfg->boost_mode == MAX77693_LED_BOOST_NONE) && led->iout_joint)
779                 cfg->boost_mode = MAX77693_LED_BOOST_FIXED;
780
781         max77693_align_iout_current(led, cfg->iout_torch_max,
782                         TORCH_IOUT_MIN, TORCH_IOUT_MAX, TORCH_IOUT_STEP);
783
784         max77693_align_iout_current(led, cfg->iout_flash_max,
785                         FLASH_IOUT_MIN, flash_iout_max, FLASH_IOUT_STEP);
786
787         for (i = 0; i < ARRAY_SIZE(cfg->flash_timeout_max); ++i)
788                 clamp_align(&cfg->flash_timeout_max[i], FLASH_TIMEOUT_MIN,
789                                 FLASH_TIMEOUT_MAX, FLASH_TIMEOUT_STEP);
790
791         clamp_align(&cfg->boost_vout, FLASH_VOUT_MIN, FLASH_VOUT_MAX,
792                                                         FLASH_VOUT_STEP);
793
794         if (cfg->low_vsys)
795                 clamp_align(&cfg->low_vsys, MAX_FLASH1_VSYS_MIN,
796                                 MAX_FLASH1_VSYS_MAX, MAX_FLASH1_VSYS_STEP);
797 }
798
799 static int max77693_led_get_configuration(struct max77693_led_device *led,
800                                 struct max77693_led_config_data *cfg,
801                                 struct device_node **sub_nodes)
802 {
803         int ret;
804
805         ret = max77693_led_parse_dt(led, cfg, sub_nodes);
806         if (ret < 0)
807                 return ret;
808
809         max77693_led_validate_configuration(led, cfg);
810
811         memcpy(led->iout_torch_max, cfg->iout_torch_max,
812                                 sizeof(led->iout_torch_max));
813         memcpy(led->iout_flash_max, cfg->iout_flash_max,
814                                 sizeof(led->iout_flash_max));
815
816         return 0;
817 }
818
819 static const struct led_flash_ops flash_ops = {
820         .flash_brightness_set   = max77693_led_flash_brightness_set,
821         .strobe_set             = max77693_led_flash_strobe_set,
822         .strobe_get             = max77693_led_flash_strobe_get,
823         .timeout_set            = max77693_led_flash_timeout_set,
824         .fault_get              = max77693_led_flash_fault_get,
825 };
826
827 static void max77693_init_flash_settings(struct max77693_sub_led *sub_led,
828                                  struct max77693_led_config_data *led_cfg)
829 {
830         struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
831         struct max77693_led_device *led = sub_led_to_led(sub_led);
832         int fled_id = sub_led->fled_id;
833         struct led_flash_setting *setting;
834
835         /* Init flash intensity setting */
836         setting = &fled_cdev->brightness;
837         setting->min = FLASH_IOUT_MIN;
838         setting->max = led->iout_joint ?
839                 led_cfg->iout_flash_max[FLED1] +
840                 led_cfg->iout_flash_max[FLED2] :
841                 led_cfg->iout_flash_max[fled_id];
842         setting->step = FLASH_IOUT_STEP;
843         setting->val = setting->max;
844
845         /* Init flash timeout setting */
846         setting = &fled_cdev->timeout;
847         setting->min = FLASH_TIMEOUT_MIN;
848         setting->max = led_cfg->flash_timeout_max[fled_id];
849         setting->step = FLASH_TIMEOUT_STEP;
850         setting->val = setting->max;
851 }
852
853 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
854
855 static int max77693_led_external_strobe_set(
856                                 struct v4l2_flash *v4l2_flash,
857                                 bool enable)
858 {
859         struct max77693_sub_led *sub_led =
860                                 flcdev_to_sub_led(v4l2_flash->fled_cdev);
861         struct max77693_led_device *led = sub_led_to_led(sub_led);
862         int fled_id = sub_led->fled_id;
863         int ret;
864
865         mutex_lock(&led->lock);
866
867         if (enable)
868                 ret = max77693_add_mode(led, MODE_FLASH_EXTERNAL(fled_id));
869         else
870                 ret = max77693_clear_mode(led, MODE_FLASH_EXTERNAL(fled_id));
871
872         mutex_unlock(&led->lock);
873
874         return ret;
875 }
876
877 static void max77693_init_v4l2_flash_config(struct max77693_sub_led *sub_led,
878                                 struct max77693_led_config_data *led_cfg,
879                                 struct v4l2_flash_config *v4l2_sd_cfg)
880 {
881         struct max77693_led_device *led = sub_led_to_led(sub_led);
882         struct device *dev = &led->pdev->dev;
883         struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
884         struct i2c_client *i2c = iodev->i2c;
885         struct led_flash_setting *s;
886
887         snprintf(v4l2_sd_cfg->dev_name, sizeof(v4l2_sd_cfg->dev_name),
888                  "%s %d-%04x", sub_led->fled_cdev.led_cdev.name,
889                  i2c_adapter_id(i2c->adapter), i2c->addr);
890
891         s = &v4l2_sd_cfg->torch_intensity;
892         s->min = TORCH_IOUT_MIN;
893         s->max = sub_led->fled_cdev.led_cdev.max_brightness * TORCH_IOUT_STEP;
894         s->step = TORCH_IOUT_STEP;
895         s->val = s->max;
896
897         /* Init flash faults config */
898         v4l2_sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE |
899                                 LED_FAULT_SHORT_CIRCUIT |
900                                 LED_FAULT_OVER_CURRENT;
901
902         v4l2_sd_cfg->has_external_strobe = true;
903 }
904
905 static const struct v4l2_flash_ops v4l2_flash_ops = {
906         .external_strobe_set = max77693_led_external_strobe_set,
907 };
908 #else
909 static inline void max77693_init_v4l2_flash_config(
910                                 struct max77693_sub_led *sub_led,
911                                 struct max77693_led_config_data *led_cfg,
912                                 struct v4l2_flash_config *v4l2_sd_cfg)
913 {
914 }
915 static const struct v4l2_flash_ops v4l2_flash_ops;
916 #endif
917
918 static void max77693_init_fled_cdev(struct max77693_sub_led *sub_led,
919                                 struct max77693_led_config_data *led_cfg)
920 {
921         struct max77693_led_device *led = sub_led_to_led(sub_led);
922         int fled_id = sub_led->fled_id;
923         struct led_classdev_flash *fled_cdev;
924         struct led_classdev *led_cdev;
925
926         /* Initialize LED Flash class device */
927         fled_cdev = &sub_led->fled_cdev;
928         fled_cdev->ops = &flash_ops;
929         led_cdev = &fled_cdev->led_cdev;
930
931         led_cdev->name = led_cfg->label[fled_id];
932
933         led_cdev->brightness_set = max77693_led_brightness_set;
934         led_cdev->brightness_set_sync = max77693_led_brightness_set_sync;
935         led_cdev->max_brightness = (led->iout_joint ?
936                                         led_cfg->iout_torch_max[FLED1] +
937                                         led_cfg->iout_torch_max[FLED2] :
938                                         led_cfg->iout_torch_max[fled_id]) /
939                                    TORCH_IOUT_STEP;
940         led_cdev->flags |= LED_DEV_CAP_FLASH;
941         INIT_WORK(&sub_led->work_brightness_set,
942                         max77693_led_brightness_set_work);
943
944         max77693_init_flash_settings(sub_led, led_cfg);
945
946         /* Init flash timeout cache */
947         sub_led->flash_timeout = fled_cdev->timeout.val;
948 }
949
950 static int max77693_register_led(struct max77693_sub_led *sub_led,
951                                  struct max77693_led_config_data *led_cfg,
952                                  struct device_node *sub_node)
953 {
954         struct max77693_led_device *led = sub_led_to_led(sub_led);
955         struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
956         struct device *dev = &led->pdev->dev;
957         struct v4l2_flash_config v4l2_sd_cfg = {};
958         int ret;
959
960         /* Register in the LED subsystem */
961         ret = led_classdev_flash_register(dev, fled_cdev);
962         if (ret < 0)
963                 return ret;
964
965         max77693_init_v4l2_flash_config(sub_led, led_cfg, &v4l2_sd_cfg);
966
967         /* Register in the V4L2 subsystem. */
968         sub_led->v4l2_flash = v4l2_flash_init(dev, sub_node, fled_cdev, NULL,
969                                               &v4l2_flash_ops, &v4l2_sd_cfg);
970         if (IS_ERR(sub_led->v4l2_flash)) {
971                 ret = PTR_ERR(sub_led->v4l2_flash);
972                 goto err_v4l2_flash_init;
973         }
974
975         return 0;
976
977 err_v4l2_flash_init:
978         led_classdev_flash_unregister(fled_cdev);
979         return ret;
980 }
981
982 static int max77693_led_probe(struct platform_device *pdev)
983 {
984         struct device *dev = &pdev->dev;
985         struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
986         struct max77693_led_device *led;
987         struct max77693_sub_led *sub_leds;
988         struct device_node *sub_nodes[2] = {};
989         struct max77693_led_config_data led_cfg = {};
990         int init_fled_cdev[2], i, ret;
991
992         led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
993         if (!led)
994                 return -ENOMEM;
995
996         led->pdev = pdev;
997         led->regmap = iodev->regmap;
998         led->allowed_modes = MODE_FLASH_MASK;
999         sub_leds = led->sub_leds;
1000
1001         platform_set_drvdata(pdev, led);
1002         ret = max77693_led_get_configuration(led, &led_cfg, sub_nodes);
1003         if (ret < 0)
1004                 return ret;
1005
1006         ret = max77693_setup(led, &led_cfg);
1007         if (ret < 0)
1008                 return ret;
1009
1010         mutex_init(&led->lock);
1011
1012         init_fled_cdev[FLED1] =
1013                         led->iout_joint || max77693_fled_used(led, FLED1);
1014         init_fled_cdev[FLED2] =
1015                         !led->iout_joint && max77693_fled_used(led, FLED2);
1016
1017         for (i = FLED1; i <= FLED2; ++i) {
1018                 if (!init_fled_cdev[i])
1019                         continue;
1020
1021                 /* Initialize LED Flash class device */
1022                 max77693_init_fled_cdev(&sub_leds[i], &led_cfg);
1023
1024                 /*
1025                  * Register LED Flash class device and corresponding
1026                  * V4L2 Flash device.
1027                  */
1028                 ret = max77693_register_led(&sub_leds[i], &led_cfg,
1029                                                 sub_nodes[i]);
1030                 if (ret < 0) {
1031                         /*
1032                          * At this moment FLED1 might have been already
1033                          * registered and it needs to be released.
1034                          */
1035                         if (i == FLED2)
1036                                 goto err_register_led2;
1037                         else
1038                                 goto err_register_led1;
1039                 }
1040         }
1041
1042         return 0;
1043
1044 err_register_led2:
1045         /* It is possible than only FLED2 was to be registered */
1046         if (!init_fled_cdev[FLED1])
1047                 goto err_register_led1;
1048         v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1049         led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1050 err_register_led1:
1051         mutex_destroy(&led->lock);
1052
1053         return ret;
1054 }
1055
1056 static int max77693_led_remove(struct platform_device *pdev)
1057 {
1058         struct max77693_led_device *led = platform_get_drvdata(pdev);
1059         struct max77693_sub_led *sub_leds = led->sub_leds;
1060
1061         if (led->iout_joint || max77693_fled_used(led, FLED1)) {
1062                 v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1063                 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1064                 cancel_work_sync(&sub_leds[FLED1].work_brightness_set);
1065         }
1066
1067         if (!led->iout_joint && max77693_fled_used(led, FLED2)) {
1068                 v4l2_flash_release(sub_leds[FLED2].v4l2_flash);
1069                 led_classdev_flash_unregister(&sub_leds[FLED2].fled_cdev);
1070                 cancel_work_sync(&sub_leds[FLED2].work_brightness_set);
1071         }
1072
1073         mutex_destroy(&led->lock);
1074
1075         return 0;
1076 }
1077
1078 static const struct of_device_id max77693_led_dt_match[] = {
1079         { .compatible = "maxim,max77693-led" },
1080         {},
1081 };
1082
1083 static struct platform_driver max77693_led_driver = {
1084         .probe          = max77693_led_probe,
1085         .remove         = max77693_led_remove,
1086         .driver         = {
1087                 .name   = "max77693-led",
1088                 .of_match_table = max77693_led_dt_match,
1089         },
1090 };
1091
1092 module_platform_driver(max77693_led_driver);
1093
1094 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
1095 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1096 MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
1097 MODULE_LICENSE("GPL v2");