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