drm: Stop accessing encoder->bridge directly
[linux/fpc-iii.git] / drivers / leds / leds-max77693.c
blobfec56090c2ba3d8b38d232237a985c7cb509451b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * LED Flash class driver for the flash cell of max77693 mfd.
5 * Copyright (C) 2015, Samsung Electronics Co., Ltd.
7 * Authors: Jacek Anaszewski <j.anaszewski@samsung.com>
8 * Andrzej Hajda <a.hajda@samsung.com>
9 */
11 #include <linux/led-class-flash.h>
12 #include <linux/mfd/max77693.h>
13 #include <linux/mfd/max77693-common.h>
14 #include <linux/mfd/max77693-private.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 #include <media/v4l2-flash-led-class.h>
22 #define MODE_OFF 0
23 #define MODE_FLASH(a) (1 << (a))
24 #define MODE_TORCH(a) (1 << (2 + (a)))
25 #define MODE_FLASH_EXTERNAL(a) (1 << (4 + (a)))
27 #define MODE_FLASH_MASK (MODE_FLASH(FLED1) | MODE_FLASH(FLED2) | \
28 MODE_FLASH_EXTERNAL(FLED1) | \
29 MODE_FLASH_EXTERNAL(FLED2))
30 #define MODE_TORCH_MASK (MODE_TORCH(FLED1) | MODE_TORCH(FLED2))
32 #define FLED1_IOUT (1 << 0)
33 #define FLED2_IOUT (1 << 1)
35 enum max77693_fled {
36 FLED1,
37 FLED2,
40 enum max77693_led_mode {
41 FLASH,
42 TORCH,
45 struct max77693_led_config_data {
46 const char *label[2];
47 u32 iout_torch_max[2];
48 u32 iout_flash_max[2];
49 u32 flash_timeout_max[2];
50 u32 num_leds;
51 u32 boost_mode;
52 u32 boost_vout;
53 u32 low_vsys;
56 struct max77693_sub_led {
57 /* corresponding FLED output identifier */
58 int fled_id;
59 /* corresponding LED Flash class device */
60 struct led_classdev_flash fled_cdev;
61 /* V4L2 Flash device */
62 struct v4l2_flash *v4l2_flash;
64 /* brightness cache */
65 unsigned int torch_brightness;
66 /* flash timeout cache */
67 unsigned int flash_timeout;
68 /* flash faults that may have occurred */
69 u32 flash_faults;
72 struct max77693_led_device {
73 /* parent mfd regmap */
74 struct regmap *regmap;
75 /* platform device data */
76 struct platform_device *pdev;
77 /* secures access to the device */
78 struct mutex lock;
80 /* sub led data */
81 struct max77693_sub_led sub_leds[2];
83 /* maximum torch current values for FLED outputs */
84 u32 iout_torch_max[2];
85 /* maximum flash current values for FLED outputs */
86 u32 iout_flash_max[2];
88 /* current flash timeout cache */
89 unsigned int current_flash_timeout;
90 /* ITORCH register cache */
91 u8 torch_iout_reg;
92 /* mode of fled outputs */
93 unsigned int mode_flags;
94 /* recently strobed fled */
95 int strobing_sub_led_id;
96 /* bitmask of FLED outputs use state (bit 0. - FLED1, bit 1. - FLED2) */
97 u8 fled_mask;
98 /* FLED modes that can be set */
99 u8 allowed_modes;
101 /* arrangement of current outputs */
102 bool iout_joint;
105 static u8 max77693_led_iout_to_reg(u32 ua)
107 if (ua < FLASH_IOUT_MIN)
108 ua = FLASH_IOUT_MIN;
109 return (ua - FLASH_IOUT_MIN) / FLASH_IOUT_STEP;
112 static u8 max77693_flash_timeout_to_reg(u32 us)
114 return (us - FLASH_TIMEOUT_MIN) / FLASH_TIMEOUT_STEP;
117 static inline struct max77693_sub_led *flcdev_to_sub_led(
118 struct led_classdev_flash *fled_cdev)
120 return container_of(fled_cdev, struct max77693_sub_led, fled_cdev);
123 static inline struct max77693_led_device *sub_led_to_led(
124 struct max77693_sub_led *sub_led)
126 return container_of(sub_led, struct max77693_led_device,
127 sub_leds[sub_led->fled_id]);
130 static inline u8 max77693_led_vsys_to_reg(u32 mv)
132 return ((mv - MAX_FLASH1_VSYS_MIN) / MAX_FLASH1_VSYS_STEP) << 2;
135 static inline u8 max77693_led_vout_to_reg(u32 mv)
137 return (mv - FLASH_VOUT_MIN) / FLASH_VOUT_STEP + FLASH_VOUT_RMIN;
140 static inline bool max77693_fled_used(struct max77693_led_device *led,
141 int fled_id)
143 u8 fled_bit = (fled_id == FLED1) ? FLED1_IOUT : FLED2_IOUT;
145 return led->fled_mask & fled_bit;
148 static int max77693_set_mode_reg(struct max77693_led_device *led, u8 mode)
150 struct regmap *rmap = led->regmap;
151 int ret, v = 0, i;
153 for (i = FLED1; i <= FLED2; ++i) {
154 if (mode & MODE_TORCH(i))
155 v |= FLASH_EN_ON << TORCH_EN_SHIFT(i);
157 if (mode & MODE_FLASH(i)) {
158 v |= FLASH_EN_ON << FLASH_EN_SHIFT(i);
159 } else if (mode & MODE_FLASH_EXTERNAL(i)) {
160 v |= FLASH_EN_FLASH << FLASH_EN_SHIFT(i);
162 * Enable hw triggering also for torch mode, as some
163 * camera sensors use torch led to fathom ambient light
164 * conditions before strobing the flash.
166 v |= FLASH_EN_TORCH << TORCH_EN_SHIFT(i);
170 /* Reset the register only prior setting flash modes */
171 if (mode & ~(MODE_TORCH(FLED1) | MODE_TORCH(FLED2))) {
172 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, 0);
173 if (ret < 0)
174 return ret;
177 return regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, v);
180 static int max77693_add_mode(struct max77693_led_device *led, u8 mode)
182 u8 new_mode_flags;
183 int i, ret;
185 if (led->iout_joint)
186 /* Span the mode on FLED2 for joint iouts case */
187 mode |= (mode << 1);
190 * FLASH_EXTERNAL mode activates FLASHEN and TORCHEN pins in the device.
191 * Corresponding register bit fields interfere with SW triggered modes,
192 * thus clear them to ensure proper device configuration.
194 for (i = FLED1; i <= FLED2; ++i)
195 if (mode & MODE_FLASH_EXTERNAL(i))
196 led->mode_flags &= (~MODE_TORCH(i) & ~MODE_FLASH(i));
198 new_mode_flags = mode | led->mode_flags;
199 new_mode_flags &= led->allowed_modes;
201 if (new_mode_flags ^ led->mode_flags)
202 led->mode_flags = new_mode_flags;
203 else
204 return 0;
206 ret = max77693_set_mode_reg(led, led->mode_flags);
207 if (ret < 0)
208 return ret;
211 * Clear flash mode flag after setting the mode to avoid spurious flash
212 * strobing on each subsequent torch mode setting.
214 if (mode & MODE_FLASH_MASK)
215 led->mode_flags &= ~mode;
217 return ret;
220 static int max77693_clear_mode(struct max77693_led_device *led,
221 u8 mode)
223 if (led->iout_joint)
224 /* Clear mode also on FLED2 for joint iouts case */
225 mode |= (mode << 1);
227 led->mode_flags &= ~mode;
229 return max77693_set_mode_reg(led, led->mode_flags);
232 static void max77693_add_allowed_modes(struct max77693_led_device *led,
233 int fled_id, enum max77693_led_mode mode)
235 if (mode == FLASH)
236 led->allowed_modes |= (MODE_FLASH(fled_id) |
237 MODE_FLASH_EXTERNAL(fled_id));
238 else
239 led->allowed_modes |= MODE_TORCH(fled_id);
242 static void max77693_distribute_currents(struct max77693_led_device *led,
243 int fled_id, enum max77693_led_mode mode,
244 u32 micro_amp, u32 iout_max[2], u32 iout[2])
246 if (!led->iout_joint) {
247 iout[fled_id] = micro_amp;
248 max77693_add_allowed_modes(led, fled_id, mode);
249 return;
252 iout[FLED1] = min(micro_amp, iout_max[FLED1]);
253 iout[FLED2] = micro_amp - iout[FLED1];
255 if (mode == FLASH)
256 led->allowed_modes &= ~MODE_FLASH_MASK;
257 else
258 led->allowed_modes &= ~MODE_TORCH_MASK;
260 max77693_add_allowed_modes(led, FLED1, mode);
262 if (iout[FLED2])
263 max77693_add_allowed_modes(led, FLED2, mode);
266 static int max77693_set_torch_current(struct max77693_led_device *led,
267 int fled_id, u32 micro_amp)
269 struct regmap *rmap = led->regmap;
270 u8 iout1_reg = 0, iout2_reg = 0;
271 u32 iout[2];
273 max77693_distribute_currents(led, fled_id, TORCH, micro_amp,
274 led->iout_torch_max, iout);
276 if (fled_id == FLED1 || led->iout_joint) {
277 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
278 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT2_SHIFT);
280 if (fled_id == FLED2 || led->iout_joint) {
281 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
282 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT1_SHIFT);
285 led->torch_iout_reg |= ((iout1_reg << TORCH_IOUT1_SHIFT) |
286 (iout2_reg << TORCH_IOUT2_SHIFT));
288 return regmap_write(rmap, MAX77693_LED_REG_ITORCH,
289 led->torch_iout_reg);
292 static int max77693_set_flash_current(struct max77693_led_device *led,
293 int fled_id,
294 u32 micro_amp)
296 struct regmap *rmap = led->regmap;
297 u8 iout1_reg, iout2_reg;
298 u32 iout[2];
299 int ret = -EINVAL;
301 max77693_distribute_currents(led, fled_id, FLASH, micro_amp,
302 led->iout_flash_max, iout);
304 if (fled_id == FLED1 || led->iout_joint) {
305 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
306 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH1,
307 iout1_reg);
308 if (ret < 0)
309 return ret;
311 if (fled_id == FLED2 || led->iout_joint) {
312 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
313 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH2,
314 iout2_reg);
317 return ret;
320 static int max77693_set_timeout(struct max77693_led_device *led, u32 microsec)
322 struct regmap *rmap = led->regmap;
323 u8 v;
324 int ret;
326 v = max77693_flash_timeout_to_reg(microsec) | FLASH_TMR_LEVEL;
328 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_TIMER, v);
329 if (ret < 0)
330 return ret;
332 led->current_flash_timeout = microsec;
334 return 0;
337 static int max77693_get_strobe_status(struct max77693_led_device *led,
338 bool *state)
340 struct regmap *rmap = led->regmap;
341 unsigned int v;
342 int ret;
344 ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_STATUS, &v);
345 if (ret < 0)
346 return ret;
348 *state = v & FLASH_STATUS_FLASH_ON;
350 return ret;
353 static int max77693_get_flash_faults(struct max77693_sub_led *sub_led)
355 struct max77693_led_device *led = sub_led_to_led(sub_led);
356 struct regmap *rmap = led->regmap;
357 unsigned int v;
358 u8 fault_open_mask, fault_short_mask;
359 int ret;
361 sub_led->flash_faults = 0;
363 if (led->iout_joint) {
364 fault_open_mask = FLASH_INT_FLED1_OPEN | FLASH_INT_FLED2_OPEN;
365 fault_short_mask = FLASH_INT_FLED1_SHORT |
366 FLASH_INT_FLED2_SHORT;
367 } else {
368 fault_open_mask = (sub_led->fled_id == FLED1) ?
369 FLASH_INT_FLED1_OPEN :
370 FLASH_INT_FLED2_OPEN;
371 fault_short_mask = (sub_led->fled_id == FLED1) ?
372 FLASH_INT_FLED1_SHORT :
373 FLASH_INT_FLED2_SHORT;
376 ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_INT, &v);
377 if (ret < 0)
378 return ret;
380 if (v & fault_open_mask)
381 sub_led->flash_faults |= LED_FAULT_OVER_VOLTAGE;
382 if (v & fault_short_mask)
383 sub_led->flash_faults |= LED_FAULT_SHORT_CIRCUIT;
384 if (v & FLASH_INT_OVER_CURRENT)
385 sub_led->flash_faults |= LED_FAULT_OVER_CURRENT;
387 return 0;
390 static int max77693_setup(struct max77693_led_device *led,
391 struct max77693_led_config_data *led_cfg)
393 struct regmap *rmap = led->regmap;
394 int i, first_led, last_led, ret;
395 u32 max_flash_curr[2];
396 u8 v;
399 * Initialize only flash current. Torch current doesn't
400 * require initialization as ITORCH register is written with
401 * new value each time brightness_set op is called.
403 if (led->iout_joint) {
404 first_led = FLED1;
405 last_led = FLED1;
406 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1] +
407 led_cfg->iout_flash_max[FLED2];
408 } else {
409 first_led = max77693_fled_used(led, FLED1) ? FLED1 : FLED2;
410 last_led = max77693_fled_used(led, FLED2) ? FLED2 : FLED1;
411 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1];
412 max_flash_curr[FLED2] = led_cfg->iout_flash_max[FLED2];
415 for (i = first_led; i <= last_led; ++i) {
416 ret = max77693_set_flash_current(led, i,
417 max_flash_curr[i]);
418 if (ret < 0)
419 return ret;
422 v = TORCH_TMR_NO_TIMER | MAX77693_LED_TRIG_TYPE_LEVEL;
423 ret = regmap_write(rmap, MAX77693_LED_REG_ITORCHTIMER, v);
424 if (ret < 0)
425 return ret;
427 if (led_cfg->low_vsys > 0)
428 v = max77693_led_vsys_to_reg(led_cfg->low_vsys) |
429 MAX_FLASH1_MAX_FL_EN;
430 else
431 v = 0;
433 ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH1, v);
434 if (ret < 0)
435 return ret;
436 ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH2, 0);
437 if (ret < 0)
438 return ret;
440 if (led_cfg->boost_mode == MAX77693_LED_BOOST_FIXED)
441 v = FLASH_BOOST_FIXED;
442 else
443 v = led_cfg->boost_mode | led_cfg->boost_mode << 1;
445 if (max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
446 v |= FLASH_BOOST_LEDNUM_2;
448 ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_CNTL, v);
449 if (ret < 0)
450 return ret;
452 v = max77693_led_vout_to_reg(led_cfg->boost_vout);
453 ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_FLASH1, v);
454 if (ret < 0)
455 return ret;
457 return max77693_set_mode_reg(led, MODE_OFF);
460 /* LED subsystem callbacks */
461 static int max77693_led_brightness_set(struct led_classdev *led_cdev,
462 enum led_brightness value)
464 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
465 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
466 struct max77693_led_device *led = sub_led_to_led(sub_led);
467 int fled_id = sub_led->fled_id, ret;
469 mutex_lock(&led->lock);
471 if (value == 0) {
472 ret = max77693_clear_mode(led, MODE_TORCH(fled_id));
473 if (ret < 0)
474 dev_dbg(&led->pdev->dev,
475 "Failed to clear torch mode (%d)\n",
476 ret);
477 goto unlock;
480 ret = max77693_set_torch_current(led, fled_id, value * TORCH_IOUT_STEP);
481 if (ret < 0) {
482 dev_dbg(&led->pdev->dev,
483 "Failed to set torch current (%d)\n",
484 ret);
485 goto unlock;
488 ret = max77693_add_mode(led, MODE_TORCH(fled_id));
489 if (ret < 0)
490 dev_dbg(&led->pdev->dev,
491 "Failed to set torch mode (%d)\n",
492 ret);
493 unlock:
494 mutex_unlock(&led->lock);
496 return ret;
499 static int max77693_led_flash_brightness_set(
500 struct led_classdev_flash *fled_cdev,
501 u32 brightness)
503 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
504 struct max77693_led_device *led = sub_led_to_led(sub_led);
505 int ret;
507 mutex_lock(&led->lock);
508 ret = max77693_set_flash_current(led, sub_led->fled_id, brightness);
509 mutex_unlock(&led->lock);
511 return ret;
514 static int max77693_led_flash_strobe_set(
515 struct led_classdev_flash *fled_cdev,
516 bool state)
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 int fled_id = sub_led->fled_id;
521 int ret;
523 mutex_lock(&led->lock);
525 if (!state) {
526 ret = max77693_clear_mode(led, MODE_FLASH(fled_id));
527 goto unlock;
530 if (sub_led->flash_timeout != led->current_flash_timeout) {
531 ret = max77693_set_timeout(led, sub_led->flash_timeout);
532 if (ret < 0)
533 goto unlock;
536 led->strobing_sub_led_id = fled_id;
538 ret = max77693_add_mode(led, MODE_FLASH(fled_id));
539 if (ret < 0)
540 goto unlock;
542 ret = max77693_get_flash_faults(sub_led);
544 unlock:
545 mutex_unlock(&led->lock);
546 return ret;
549 static int max77693_led_flash_fault_get(
550 struct led_classdev_flash *fled_cdev,
551 u32 *fault)
553 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
555 *fault = sub_led->flash_faults;
557 return 0;
560 static int max77693_led_flash_strobe_get(
561 struct led_classdev_flash *fled_cdev,
562 bool *state)
564 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
565 struct max77693_led_device *led = sub_led_to_led(sub_led);
566 int ret;
568 if (!state)
569 return -EINVAL;
571 mutex_lock(&led->lock);
573 ret = max77693_get_strobe_status(led, state);
575 *state = !!(*state && (led->strobing_sub_led_id == sub_led->fled_id));
577 mutex_unlock(&led->lock);
579 return ret;
582 static int max77693_led_flash_timeout_set(
583 struct led_classdev_flash *fled_cdev,
584 u32 timeout)
586 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
587 struct max77693_led_device *led = sub_led_to_led(sub_led);
589 mutex_lock(&led->lock);
590 sub_led->flash_timeout = timeout;
591 mutex_unlock(&led->lock);
593 return 0;
596 static int max77693_led_parse_dt(struct max77693_led_device *led,
597 struct max77693_led_config_data *cfg,
598 struct device_node **sub_nodes)
600 struct device *dev = &led->pdev->dev;
601 struct max77693_sub_led *sub_leds = led->sub_leds;
602 struct device_node *node = dev->of_node, *child_node;
603 struct property *prop;
604 u32 led_sources[2];
605 int i, ret, fled_id;
607 of_property_read_u32(node, "maxim,boost-mode", &cfg->boost_mode);
608 of_property_read_u32(node, "maxim,boost-mvout", &cfg->boost_vout);
609 of_property_read_u32(node, "maxim,mvsys-min", &cfg->low_vsys);
611 for_each_available_child_of_node(node, child_node) {
612 prop = of_find_property(child_node, "led-sources", NULL);
613 if (prop) {
614 const __be32 *srcs = NULL;
616 for (i = 0; i < ARRAY_SIZE(led_sources); ++i) {
617 srcs = of_prop_next_u32(prop, srcs,
618 &led_sources[i]);
619 if (!srcs)
620 break;
622 } else {
623 dev_err(dev,
624 "led-sources DT property missing\n");
625 of_node_put(child_node);
626 return -EINVAL;
629 if (i == 2) {
630 fled_id = FLED1;
631 led->fled_mask = FLED1_IOUT | FLED2_IOUT;
632 } else if (led_sources[0] == FLED1) {
633 fled_id = FLED1;
634 led->fled_mask |= FLED1_IOUT;
635 } else if (led_sources[0] == FLED2) {
636 fled_id = FLED2;
637 led->fled_mask |= FLED2_IOUT;
638 } else {
639 dev_err(dev,
640 "Wrong led-sources DT property value.\n");
641 of_node_put(child_node);
642 return -EINVAL;
645 if (sub_nodes[fled_id]) {
646 dev_err(dev,
647 "Conflicting \"led-sources\" DT properties\n");
648 of_node_put(child_node);
649 return -EINVAL;
652 sub_nodes[fled_id] = child_node;
653 sub_leds[fled_id].fled_id = fled_id;
655 cfg->label[fled_id] =
656 of_get_property(child_node, "label", NULL) ? :
657 child_node->name;
659 ret = of_property_read_u32(child_node, "led-max-microamp",
660 &cfg->iout_torch_max[fled_id]);
661 if (ret < 0) {
662 cfg->iout_torch_max[fled_id] = TORCH_IOUT_MIN;
663 dev_warn(dev, "led-max-microamp DT property missing\n");
666 ret = of_property_read_u32(child_node, "flash-max-microamp",
667 &cfg->iout_flash_max[fled_id]);
668 if (ret < 0) {
669 cfg->iout_flash_max[fled_id] = FLASH_IOUT_MIN;
670 dev_warn(dev,
671 "flash-max-microamp DT property missing\n");
674 ret = of_property_read_u32(child_node, "flash-max-timeout-us",
675 &cfg->flash_timeout_max[fled_id]);
676 if (ret < 0) {
677 cfg->flash_timeout_max[fled_id] = FLASH_TIMEOUT_MIN;
678 dev_warn(dev,
679 "flash-max-timeout-us DT property missing\n");
682 if (++cfg->num_leds == 2 ||
683 (max77693_fled_used(led, FLED1) &&
684 max77693_fled_used(led, FLED2))) {
685 of_node_put(child_node);
686 break;
690 if (cfg->num_leds == 0) {
691 dev_err(dev, "No DT child node found for connected LED(s).\n");
692 return -EINVAL;
695 return 0;
698 static void clamp_align(u32 *v, u32 min, u32 max, u32 step)
700 *v = clamp_val(*v, min, max);
701 if (step > 1)
702 *v = (*v - min) / step * step + min;
705 static void max77693_align_iout_current(struct max77693_led_device *led,
706 u32 *iout, u32 min, u32 max, u32 step)
708 int i;
710 if (led->iout_joint) {
711 if (iout[FLED1] > min) {
712 iout[FLED1] /= 2;
713 iout[FLED2] = iout[FLED1];
714 } else {
715 iout[FLED1] = min;
716 iout[FLED2] = 0;
717 return;
721 for (i = FLED1; i <= FLED2; ++i)
722 if (max77693_fled_used(led, i))
723 clamp_align(&iout[i], min, max, step);
724 else
725 iout[i] = 0;
728 static void max77693_led_validate_configuration(struct max77693_led_device *led,
729 struct max77693_led_config_data *cfg)
731 u32 flash_iout_max = cfg->boost_mode ? FLASH_IOUT_MAX_2LEDS :
732 FLASH_IOUT_MAX_1LED;
733 int i;
735 if (cfg->num_leds == 1 &&
736 max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
737 led->iout_joint = true;
739 cfg->boost_mode = clamp_val(cfg->boost_mode, MAX77693_LED_BOOST_NONE,
740 MAX77693_LED_BOOST_FIXED);
742 /* Boost must be enabled if both current outputs are used */
743 if ((cfg->boost_mode == MAX77693_LED_BOOST_NONE) && led->iout_joint)
744 cfg->boost_mode = MAX77693_LED_BOOST_FIXED;
746 max77693_align_iout_current(led, cfg->iout_torch_max,
747 TORCH_IOUT_MIN, TORCH_IOUT_MAX, TORCH_IOUT_STEP);
749 max77693_align_iout_current(led, cfg->iout_flash_max,
750 FLASH_IOUT_MIN, flash_iout_max, FLASH_IOUT_STEP);
752 for (i = 0; i < ARRAY_SIZE(cfg->flash_timeout_max); ++i)
753 clamp_align(&cfg->flash_timeout_max[i], FLASH_TIMEOUT_MIN,
754 FLASH_TIMEOUT_MAX, FLASH_TIMEOUT_STEP);
756 clamp_align(&cfg->boost_vout, FLASH_VOUT_MIN, FLASH_VOUT_MAX,
757 FLASH_VOUT_STEP);
759 if (cfg->low_vsys)
760 clamp_align(&cfg->low_vsys, MAX_FLASH1_VSYS_MIN,
761 MAX_FLASH1_VSYS_MAX, MAX_FLASH1_VSYS_STEP);
764 static int max77693_led_get_configuration(struct max77693_led_device *led,
765 struct max77693_led_config_data *cfg,
766 struct device_node **sub_nodes)
768 int ret;
770 ret = max77693_led_parse_dt(led, cfg, sub_nodes);
771 if (ret < 0)
772 return ret;
774 max77693_led_validate_configuration(led, cfg);
776 memcpy(led->iout_torch_max, cfg->iout_torch_max,
777 sizeof(led->iout_torch_max));
778 memcpy(led->iout_flash_max, cfg->iout_flash_max,
779 sizeof(led->iout_flash_max));
781 return 0;
784 static const struct led_flash_ops flash_ops = {
785 .flash_brightness_set = max77693_led_flash_brightness_set,
786 .strobe_set = max77693_led_flash_strobe_set,
787 .strobe_get = max77693_led_flash_strobe_get,
788 .timeout_set = max77693_led_flash_timeout_set,
789 .fault_get = max77693_led_flash_fault_get,
792 static void max77693_init_flash_settings(struct max77693_sub_led *sub_led,
793 struct max77693_led_config_data *led_cfg)
795 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
796 struct max77693_led_device *led = sub_led_to_led(sub_led);
797 int fled_id = sub_led->fled_id;
798 struct led_flash_setting *setting;
800 /* Init flash intensity setting */
801 setting = &fled_cdev->brightness;
802 setting->min = FLASH_IOUT_MIN;
803 setting->max = led->iout_joint ?
804 led_cfg->iout_flash_max[FLED1] +
805 led_cfg->iout_flash_max[FLED2] :
806 led_cfg->iout_flash_max[fled_id];
807 setting->step = FLASH_IOUT_STEP;
808 setting->val = setting->max;
810 /* Init flash timeout setting */
811 setting = &fled_cdev->timeout;
812 setting->min = FLASH_TIMEOUT_MIN;
813 setting->max = led_cfg->flash_timeout_max[fled_id];
814 setting->step = FLASH_TIMEOUT_STEP;
815 setting->val = setting->max;
818 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
820 static int max77693_led_external_strobe_set(
821 struct v4l2_flash *v4l2_flash,
822 bool enable)
824 struct max77693_sub_led *sub_led =
825 flcdev_to_sub_led(v4l2_flash->fled_cdev);
826 struct max77693_led_device *led = sub_led_to_led(sub_led);
827 int fled_id = sub_led->fled_id;
828 int ret;
830 mutex_lock(&led->lock);
832 if (enable)
833 ret = max77693_add_mode(led, MODE_FLASH_EXTERNAL(fled_id));
834 else
835 ret = max77693_clear_mode(led, MODE_FLASH_EXTERNAL(fled_id));
837 mutex_unlock(&led->lock);
839 return ret;
842 static void max77693_init_v4l2_flash_config(struct max77693_sub_led *sub_led,
843 struct max77693_led_config_data *led_cfg,
844 struct v4l2_flash_config *v4l2_sd_cfg)
846 struct max77693_led_device *led = sub_led_to_led(sub_led);
847 struct device *dev = &led->pdev->dev;
848 struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
849 struct i2c_client *i2c = iodev->i2c;
850 struct led_flash_setting *s;
852 snprintf(v4l2_sd_cfg->dev_name, sizeof(v4l2_sd_cfg->dev_name),
853 "%s %d-%04x", sub_led->fled_cdev.led_cdev.name,
854 i2c_adapter_id(i2c->adapter), i2c->addr);
856 s = &v4l2_sd_cfg->intensity;
857 s->min = TORCH_IOUT_MIN;
858 s->max = sub_led->fled_cdev.led_cdev.max_brightness * TORCH_IOUT_STEP;
859 s->step = TORCH_IOUT_STEP;
860 s->val = s->max;
862 /* Init flash faults config */
863 v4l2_sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE |
864 LED_FAULT_SHORT_CIRCUIT |
865 LED_FAULT_OVER_CURRENT;
867 v4l2_sd_cfg->has_external_strobe = true;
870 static const struct v4l2_flash_ops v4l2_flash_ops = {
871 .external_strobe_set = max77693_led_external_strobe_set,
873 #else
874 static inline void max77693_init_v4l2_flash_config(
875 struct max77693_sub_led *sub_led,
876 struct max77693_led_config_data *led_cfg,
877 struct v4l2_flash_config *v4l2_sd_cfg)
880 static const struct v4l2_flash_ops v4l2_flash_ops;
881 #endif
883 static void max77693_init_fled_cdev(struct max77693_sub_led *sub_led,
884 struct max77693_led_config_data *led_cfg)
886 struct max77693_led_device *led = sub_led_to_led(sub_led);
887 int fled_id = sub_led->fled_id;
888 struct led_classdev_flash *fled_cdev;
889 struct led_classdev *led_cdev;
891 /* Initialize LED Flash class device */
892 fled_cdev = &sub_led->fled_cdev;
893 fled_cdev->ops = &flash_ops;
894 led_cdev = &fled_cdev->led_cdev;
896 led_cdev->name = led_cfg->label[fled_id];
898 led_cdev->brightness_set_blocking = max77693_led_brightness_set;
899 led_cdev->max_brightness = (led->iout_joint ?
900 led_cfg->iout_torch_max[FLED1] +
901 led_cfg->iout_torch_max[FLED2] :
902 led_cfg->iout_torch_max[fled_id]) /
903 TORCH_IOUT_STEP;
904 led_cdev->flags |= LED_DEV_CAP_FLASH;
906 max77693_init_flash_settings(sub_led, led_cfg);
908 /* Init flash timeout cache */
909 sub_led->flash_timeout = fled_cdev->timeout.val;
912 static int max77693_register_led(struct max77693_sub_led *sub_led,
913 struct max77693_led_config_data *led_cfg,
914 struct device_node *sub_node)
916 struct max77693_led_device *led = sub_led_to_led(sub_led);
917 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
918 struct device *dev = &led->pdev->dev;
919 struct v4l2_flash_config v4l2_sd_cfg = {};
920 int ret;
922 /* Register in the LED subsystem */
923 ret = led_classdev_flash_register(dev, fled_cdev);
924 if (ret < 0)
925 return ret;
927 max77693_init_v4l2_flash_config(sub_led, led_cfg, &v4l2_sd_cfg);
929 /* Register in the V4L2 subsystem. */
930 sub_led->v4l2_flash = v4l2_flash_init(dev, of_fwnode_handle(sub_node),
931 fled_cdev, &v4l2_flash_ops,
932 &v4l2_sd_cfg);
933 if (IS_ERR(sub_led->v4l2_flash)) {
934 ret = PTR_ERR(sub_led->v4l2_flash);
935 goto err_v4l2_flash_init;
938 return 0;
940 err_v4l2_flash_init:
941 led_classdev_flash_unregister(fled_cdev);
942 return ret;
945 static int max77693_led_probe(struct platform_device *pdev)
947 struct device *dev = &pdev->dev;
948 struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
949 struct max77693_led_device *led;
950 struct max77693_sub_led *sub_leds;
951 struct device_node *sub_nodes[2] = {};
952 struct max77693_led_config_data led_cfg = {};
953 int init_fled_cdev[2], i, ret;
955 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
956 if (!led)
957 return -ENOMEM;
959 led->pdev = pdev;
960 led->regmap = iodev->regmap;
961 led->allowed_modes = MODE_FLASH_MASK;
962 sub_leds = led->sub_leds;
964 platform_set_drvdata(pdev, led);
965 ret = max77693_led_get_configuration(led, &led_cfg, sub_nodes);
966 if (ret < 0)
967 return ret;
969 ret = max77693_setup(led, &led_cfg);
970 if (ret < 0)
971 return ret;
973 mutex_init(&led->lock);
975 init_fled_cdev[FLED1] =
976 led->iout_joint || max77693_fled_used(led, FLED1);
977 init_fled_cdev[FLED2] =
978 !led->iout_joint && max77693_fled_used(led, FLED2);
980 for (i = FLED1; i <= FLED2; ++i) {
981 if (!init_fled_cdev[i])
982 continue;
984 /* Initialize LED Flash class device */
985 max77693_init_fled_cdev(&sub_leds[i], &led_cfg);
988 * Register LED Flash class device and corresponding
989 * V4L2 Flash device.
991 ret = max77693_register_led(&sub_leds[i], &led_cfg,
992 sub_nodes[i]);
993 if (ret < 0) {
995 * At this moment FLED1 might have been already
996 * registered and it needs to be released.
998 if (i == FLED2)
999 goto err_register_led2;
1000 else
1001 goto err_register_led1;
1005 return 0;
1007 err_register_led2:
1008 /* It is possible than only FLED2 was to be registered */
1009 if (!init_fled_cdev[FLED1])
1010 goto err_register_led1;
1011 v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1012 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1013 err_register_led1:
1014 mutex_destroy(&led->lock);
1016 return ret;
1019 static int max77693_led_remove(struct platform_device *pdev)
1021 struct max77693_led_device *led = platform_get_drvdata(pdev);
1022 struct max77693_sub_led *sub_leds = led->sub_leds;
1024 if (led->iout_joint || max77693_fled_used(led, FLED1)) {
1025 v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1026 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1029 if (!led->iout_joint && max77693_fled_used(led, FLED2)) {
1030 v4l2_flash_release(sub_leds[FLED2].v4l2_flash);
1031 led_classdev_flash_unregister(&sub_leds[FLED2].fled_cdev);
1034 mutex_destroy(&led->lock);
1036 return 0;
1039 static const struct of_device_id max77693_led_dt_match[] = {
1040 { .compatible = "maxim,max77693-led" },
1043 MODULE_DEVICE_TABLE(of, max77693_led_dt_match);
1045 static struct platform_driver max77693_led_driver = {
1046 .probe = max77693_led_probe,
1047 .remove = max77693_led_remove,
1048 .driver = {
1049 .name = "max77693-led",
1050 .of_match_table = max77693_led_dt_match,
1054 module_platform_driver(max77693_led_driver);
1056 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
1057 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1058 MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
1059 MODULE_LICENSE("GPL v2");