staging: iio: ad7606: fix improper setting of oversampling pins
[linux/fpc-iii.git] / drivers / leds / leds-max77693.c
blob1eb58ef6aefe44c888dd52a7ddb10631231d92f0
1 /*
2 * LED Flash class driver for the flash cell of max77693 mfd.
4 * Copyright (C) 2015, Samsung Electronics Co., Ltd.
6 * Authors: Jacek Anaszewski <j.anaszewski@samsung.com>
7 * Andrzej Hajda <a.hajda@samsung.com>
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.
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 <media/v4l2-flash-led-class.h>
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)))
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))
35 #define FLED1_IOUT (1 << 0)
36 #define FLED2_IOUT (1 << 1)
38 enum max77693_fled {
39 FLED1,
40 FLED2,
43 enum max77693_led_mode {
44 FLASH,
45 TORCH,
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;
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 /* V4L2 Flash device */
65 struct v4l2_flash *v4l2_flash;
67 /* brightness cache */
68 unsigned int torch_brightness;
69 /* flash timeout cache */
70 unsigned int flash_timeout;
71 /* flash faults that may have occurred */
72 u32 flash_faults;
75 struct max77693_led_device {
76 /* parent mfd regmap */
77 struct regmap *regmap;
78 /* platform device data */
79 struct platform_device *pdev;
80 /* secures access to the device */
81 struct mutex lock;
83 /* sub led data */
84 struct max77693_sub_led sub_leds[2];
86 /* maximum torch current values for FLED outputs */
87 u32 iout_torch_max[2];
88 /* maximum flash current values for FLED outputs */
89 u32 iout_flash_max[2];
91 /* current flash timeout cache */
92 unsigned int current_flash_timeout;
93 /* ITORCH register cache */
94 u8 torch_iout_reg;
95 /* mode of fled outputs */
96 unsigned int mode_flags;
97 /* recently strobed fled */
98 int strobing_sub_led_id;
99 /* bitmask of FLED outputs use state (bit 0. - FLED1, bit 1. - FLED2) */
100 u8 fled_mask;
101 /* FLED modes that can be set */
102 u8 allowed_modes;
104 /* arrangement of current outputs */
105 bool iout_joint;
108 static u8 max77693_led_iout_to_reg(u32 ua)
110 if (ua < FLASH_IOUT_MIN)
111 ua = FLASH_IOUT_MIN;
112 return (ua - FLASH_IOUT_MIN) / FLASH_IOUT_STEP;
115 static u8 max77693_flash_timeout_to_reg(u32 us)
117 return (us - FLASH_TIMEOUT_MIN) / FLASH_TIMEOUT_STEP;
120 static inline struct max77693_sub_led *flcdev_to_sub_led(
121 struct led_classdev_flash *fled_cdev)
123 return container_of(fled_cdev, struct max77693_sub_led, fled_cdev);
126 static inline struct max77693_led_device *sub_led_to_led(
127 struct max77693_sub_led *sub_led)
129 return container_of(sub_led, struct max77693_led_device,
130 sub_leds[sub_led->fled_id]);
133 static inline u8 max77693_led_vsys_to_reg(u32 mv)
135 return ((mv - MAX_FLASH1_VSYS_MIN) / MAX_FLASH1_VSYS_STEP) << 2;
138 static inline u8 max77693_led_vout_to_reg(u32 mv)
140 return (mv - FLASH_VOUT_MIN) / FLASH_VOUT_STEP + FLASH_VOUT_RMIN;
143 static inline bool max77693_fled_used(struct max77693_led_device *led,
144 int fled_id)
146 u8 fled_bit = (fled_id == FLED1) ? FLED1_IOUT : FLED2_IOUT;
148 return led->fled_mask & fled_bit;
151 static int max77693_set_mode_reg(struct max77693_led_device *led, u8 mode)
153 struct regmap *rmap = led->regmap;
154 int ret, v = 0, i;
156 for (i = FLED1; i <= FLED2; ++i) {
157 if (mode & MODE_TORCH(i))
158 v |= FLASH_EN_ON << TORCH_EN_SHIFT(i);
160 if (mode & MODE_FLASH(i)) {
161 v |= FLASH_EN_ON << FLASH_EN_SHIFT(i);
162 } else if (mode & MODE_FLASH_EXTERNAL(i)) {
163 v |= FLASH_EN_FLASH << FLASH_EN_SHIFT(i);
165 * Enable hw triggering also for torch mode, as some
166 * camera sensors use torch led to fathom ambient light
167 * conditions before strobing the flash.
169 v |= FLASH_EN_TORCH << TORCH_EN_SHIFT(i);
173 /* Reset the register only prior setting flash modes */
174 if (mode & ~(MODE_TORCH(FLED1) | MODE_TORCH(FLED2))) {
175 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, 0);
176 if (ret < 0)
177 return ret;
180 return regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, v);
183 static int max77693_add_mode(struct max77693_led_device *led, u8 mode)
185 u8 new_mode_flags;
186 int i, ret;
188 if (led->iout_joint)
189 /* Span the mode on FLED2 for joint iouts case */
190 mode |= (mode << 1);
193 * FLASH_EXTERNAL mode activates FLASHEN and TORCHEN pins in the device.
194 * Corresponding register bit fields interfere with SW triggered modes,
195 * thus clear them to ensure proper device configuration.
197 for (i = FLED1; i <= FLED2; ++i)
198 if (mode & MODE_FLASH_EXTERNAL(i))
199 led->mode_flags &= (~MODE_TORCH(i) & ~MODE_FLASH(i));
201 new_mode_flags = mode | led->mode_flags;
202 new_mode_flags &= led->allowed_modes;
204 if (new_mode_flags ^ led->mode_flags)
205 led->mode_flags = new_mode_flags;
206 else
207 return 0;
209 ret = max77693_set_mode_reg(led, led->mode_flags);
210 if (ret < 0)
211 return ret;
214 * Clear flash mode flag after setting the mode to avoid spurious flash
215 * strobing on each subsequent torch mode setting.
217 if (mode & MODE_FLASH_MASK)
218 led->mode_flags &= ~mode;
220 return ret;
223 static int max77693_clear_mode(struct max77693_led_device *led,
224 u8 mode)
226 if (led->iout_joint)
227 /* Clear mode also on FLED2 for joint iouts case */
228 mode |= (mode << 1);
230 led->mode_flags &= ~mode;
232 return max77693_set_mode_reg(led, led->mode_flags);
235 static void max77693_add_allowed_modes(struct max77693_led_device *led,
236 int fled_id, enum max77693_led_mode mode)
238 if (mode == FLASH)
239 led->allowed_modes |= (MODE_FLASH(fled_id) |
240 MODE_FLASH_EXTERNAL(fled_id));
241 else
242 led->allowed_modes |= MODE_TORCH(fled_id);
245 static void max77693_distribute_currents(struct max77693_led_device *led,
246 int fled_id, enum max77693_led_mode mode,
247 u32 micro_amp, u32 iout_max[2], u32 iout[2])
249 if (!led->iout_joint) {
250 iout[fled_id] = micro_amp;
251 max77693_add_allowed_modes(led, fled_id, mode);
252 return;
255 iout[FLED1] = min(micro_amp, iout_max[FLED1]);
256 iout[FLED2] = micro_amp - iout[FLED1];
258 if (mode == FLASH)
259 led->allowed_modes &= ~MODE_FLASH_MASK;
260 else
261 led->allowed_modes &= ~MODE_TORCH_MASK;
263 max77693_add_allowed_modes(led, FLED1, mode);
265 if (iout[FLED2])
266 max77693_add_allowed_modes(led, FLED2, mode);
269 static int max77693_set_torch_current(struct max77693_led_device *led,
270 int fled_id, u32 micro_amp)
272 struct regmap *rmap = led->regmap;
273 u8 iout1_reg = 0, iout2_reg = 0;
274 u32 iout[2];
276 max77693_distribute_currents(led, fled_id, TORCH, micro_amp,
277 led->iout_torch_max, iout);
279 if (fled_id == FLED1 || led->iout_joint) {
280 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
281 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT2_SHIFT);
283 if (fled_id == FLED2 || led->iout_joint) {
284 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
285 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT1_SHIFT);
288 led->torch_iout_reg |= ((iout1_reg << TORCH_IOUT1_SHIFT) |
289 (iout2_reg << TORCH_IOUT2_SHIFT));
291 return regmap_write(rmap, MAX77693_LED_REG_ITORCH,
292 led->torch_iout_reg);
295 static int max77693_set_flash_current(struct max77693_led_device *led,
296 int fled_id,
297 u32 micro_amp)
299 struct regmap *rmap = led->regmap;
300 u8 iout1_reg, iout2_reg;
301 u32 iout[2];
302 int ret = -EINVAL;
304 max77693_distribute_currents(led, fled_id, FLASH, micro_amp,
305 led->iout_flash_max, iout);
307 if (fled_id == FLED1 || led->iout_joint) {
308 iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
309 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH1,
310 iout1_reg);
311 if (ret < 0)
312 return ret;
314 if (fled_id == FLED2 || led->iout_joint) {
315 iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
316 ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH2,
317 iout2_reg);
320 return ret;
323 static int max77693_set_timeout(struct max77693_led_device *led, u32 microsec)
325 struct regmap *rmap = led->regmap;
326 u8 v;
327 int ret;
329 v = max77693_flash_timeout_to_reg(microsec) | FLASH_TMR_LEVEL;
331 ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_TIMER, v);
332 if (ret < 0)
333 return ret;
335 led->current_flash_timeout = microsec;
337 return 0;
340 static int max77693_get_strobe_status(struct max77693_led_device *led,
341 bool *state)
343 struct regmap *rmap = led->regmap;
344 unsigned int v;
345 int ret;
347 ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_STATUS, &v);
348 if (ret < 0)
349 return ret;
351 *state = v & FLASH_STATUS_FLASH_ON;
353 return ret;
356 static int max77693_get_flash_faults(struct max77693_sub_led *sub_led)
358 struct max77693_led_device *led = sub_led_to_led(sub_led);
359 struct regmap *rmap = led->regmap;
360 unsigned int v;
361 u8 fault_open_mask, fault_short_mask;
362 int ret;
364 sub_led->flash_faults = 0;
366 if (led->iout_joint) {
367 fault_open_mask = FLASH_INT_FLED1_OPEN | FLASH_INT_FLED2_OPEN;
368 fault_short_mask = FLASH_INT_FLED1_SHORT |
369 FLASH_INT_FLED2_SHORT;
370 } else {
371 fault_open_mask = (sub_led->fled_id == FLED1) ?
372 FLASH_INT_FLED1_OPEN :
373 FLASH_INT_FLED2_OPEN;
374 fault_short_mask = (sub_led->fled_id == FLED1) ?
375 FLASH_INT_FLED1_SHORT :
376 FLASH_INT_FLED2_SHORT;
379 ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_INT, &v);
380 if (ret < 0)
381 return ret;
383 if (v & fault_open_mask)
384 sub_led->flash_faults |= LED_FAULT_OVER_VOLTAGE;
385 if (v & fault_short_mask)
386 sub_led->flash_faults |= LED_FAULT_SHORT_CIRCUIT;
387 if (v & FLASH_INT_OVER_CURRENT)
388 sub_led->flash_faults |= LED_FAULT_OVER_CURRENT;
390 return 0;
393 static int max77693_setup(struct max77693_led_device *led,
394 struct max77693_led_config_data *led_cfg)
396 struct regmap *rmap = led->regmap;
397 int i, first_led, last_led, ret;
398 u32 max_flash_curr[2];
399 u8 v;
402 * Initialize only flash current. Torch current doesn't
403 * require initialization as ITORCH register is written with
404 * new value each time brightness_set op is called.
406 if (led->iout_joint) {
407 first_led = FLED1;
408 last_led = FLED1;
409 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1] +
410 led_cfg->iout_flash_max[FLED2];
411 } else {
412 first_led = max77693_fled_used(led, FLED1) ? FLED1 : FLED2;
413 last_led = max77693_fled_used(led, FLED2) ? FLED2 : FLED1;
414 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1];
415 max_flash_curr[FLED2] = led_cfg->iout_flash_max[FLED2];
418 for (i = first_led; i <= last_led; ++i) {
419 ret = max77693_set_flash_current(led, i,
420 max_flash_curr[i]);
421 if (ret < 0)
422 return ret;
425 v = TORCH_TMR_NO_TIMER | MAX77693_LED_TRIG_TYPE_LEVEL;
426 ret = regmap_write(rmap, MAX77693_LED_REG_ITORCHTIMER, v);
427 if (ret < 0)
428 return ret;
430 if (led_cfg->low_vsys > 0)
431 v = max77693_led_vsys_to_reg(led_cfg->low_vsys) |
432 MAX_FLASH1_MAX_FL_EN;
433 else
434 v = 0;
436 ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH1, v);
437 if (ret < 0)
438 return ret;
439 ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH2, 0);
440 if (ret < 0)
441 return ret;
443 if (led_cfg->boost_mode == MAX77693_LED_BOOST_FIXED)
444 v = FLASH_BOOST_FIXED;
445 else
446 v = led_cfg->boost_mode | led_cfg->boost_mode << 1;
448 if (max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
449 v |= FLASH_BOOST_LEDNUM_2;
451 ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_CNTL, v);
452 if (ret < 0)
453 return ret;
455 v = max77693_led_vout_to_reg(led_cfg->boost_vout);
456 ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_FLASH1, v);
457 if (ret < 0)
458 return ret;
460 return max77693_set_mode_reg(led, MODE_OFF);
463 /* LED subsystem callbacks */
464 static int max77693_led_brightness_set(struct led_classdev *led_cdev,
465 enum led_brightness value)
467 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
468 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
469 struct max77693_led_device *led = sub_led_to_led(sub_led);
470 int fled_id = sub_led->fled_id, ret;
472 mutex_lock(&led->lock);
474 if (value == 0) {
475 ret = max77693_clear_mode(led, MODE_TORCH(fled_id));
476 if (ret < 0)
477 dev_dbg(&led->pdev->dev,
478 "Failed to clear torch mode (%d)\n",
479 ret);
480 goto unlock;
483 ret = max77693_set_torch_current(led, fled_id, value * TORCH_IOUT_STEP);
484 if (ret < 0) {
485 dev_dbg(&led->pdev->dev,
486 "Failed to set torch current (%d)\n",
487 ret);
488 goto unlock;
491 ret = max77693_add_mode(led, MODE_TORCH(fled_id));
492 if (ret < 0)
493 dev_dbg(&led->pdev->dev,
494 "Failed to set torch mode (%d)\n",
495 ret);
496 unlock:
497 mutex_unlock(&led->lock);
499 return ret;
502 static int max77693_led_flash_brightness_set(
503 struct led_classdev_flash *fled_cdev,
504 u32 brightness)
506 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
507 struct max77693_led_device *led = sub_led_to_led(sub_led);
508 int ret;
510 mutex_lock(&led->lock);
511 ret = max77693_set_flash_current(led, sub_led->fled_id, brightness);
512 mutex_unlock(&led->lock);
514 return ret;
517 static int max77693_led_flash_strobe_set(
518 struct led_classdev_flash *fled_cdev,
519 bool state)
521 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
522 struct max77693_led_device *led = sub_led_to_led(sub_led);
523 int fled_id = sub_led->fled_id;
524 int ret;
526 mutex_lock(&led->lock);
528 if (!state) {
529 ret = max77693_clear_mode(led, MODE_FLASH(fled_id));
530 goto unlock;
533 if (sub_led->flash_timeout != led->current_flash_timeout) {
534 ret = max77693_set_timeout(led, sub_led->flash_timeout);
535 if (ret < 0)
536 goto unlock;
539 led->strobing_sub_led_id = fled_id;
541 ret = max77693_add_mode(led, MODE_FLASH(fled_id));
542 if (ret < 0)
543 goto unlock;
545 ret = max77693_get_flash_faults(sub_led);
547 unlock:
548 mutex_unlock(&led->lock);
549 return ret;
552 static int max77693_led_flash_fault_get(
553 struct led_classdev_flash *fled_cdev,
554 u32 *fault)
556 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
558 *fault = sub_led->flash_faults;
560 return 0;
563 static int max77693_led_flash_strobe_get(
564 struct led_classdev_flash *fled_cdev,
565 bool *state)
567 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
568 struct max77693_led_device *led = sub_led_to_led(sub_led);
569 int ret;
571 if (!state)
572 return -EINVAL;
574 mutex_lock(&led->lock);
576 ret = max77693_get_strobe_status(led, state);
578 *state = !!(*state && (led->strobing_sub_led_id == sub_led->fled_id));
580 mutex_unlock(&led->lock);
582 return ret;
585 static int max77693_led_flash_timeout_set(
586 struct led_classdev_flash *fled_cdev,
587 u32 timeout)
589 struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
590 struct max77693_led_device *led = sub_led_to_led(sub_led);
592 mutex_lock(&led->lock);
593 sub_led->flash_timeout = timeout;
594 mutex_unlock(&led->lock);
596 return 0;
599 static int max77693_led_parse_dt(struct max77693_led_device *led,
600 struct max77693_led_config_data *cfg,
601 struct device_node **sub_nodes)
603 struct device *dev = &led->pdev->dev;
604 struct max77693_sub_led *sub_leds = led->sub_leds;
605 struct device_node *node = dev->of_node, *child_node;
606 struct property *prop;
607 u32 led_sources[2];
608 int i, ret, fled_id;
610 of_property_read_u32(node, "maxim,boost-mode", &cfg->boost_mode);
611 of_property_read_u32(node, "maxim,boost-mvout", &cfg->boost_vout);
612 of_property_read_u32(node, "maxim,mvsys-min", &cfg->low_vsys);
614 for_each_available_child_of_node(node, child_node) {
615 prop = of_find_property(child_node, "led-sources", NULL);
616 if (prop) {
617 const __be32 *srcs = NULL;
619 for (i = 0; i < ARRAY_SIZE(led_sources); ++i) {
620 srcs = of_prop_next_u32(prop, srcs,
621 &led_sources[i]);
622 if (!srcs)
623 break;
625 } else {
626 dev_err(dev,
627 "led-sources DT property missing\n");
628 of_node_put(child_node);
629 return -EINVAL;
632 if (i == 2) {
633 fled_id = FLED1;
634 led->fled_mask = FLED1_IOUT | FLED2_IOUT;
635 } else if (led_sources[0] == FLED1) {
636 fled_id = FLED1;
637 led->fled_mask |= FLED1_IOUT;
638 } else if (led_sources[0] == FLED2) {
639 fled_id = FLED2;
640 led->fled_mask |= FLED2_IOUT;
641 } else {
642 dev_err(dev,
643 "Wrong led-sources DT property value.\n");
644 of_node_put(child_node);
645 return -EINVAL;
648 if (sub_nodes[fled_id]) {
649 dev_err(dev,
650 "Conflicting \"led-sources\" DT properties\n");
651 of_node_put(child_node);
652 return -EINVAL;
655 sub_nodes[fled_id] = child_node;
656 sub_leds[fled_id].fled_id = fled_id;
658 cfg->label[fled_id] =
659 of_get_property(child_node, "label", NULL) ? :
660 child_node->name;
662 ret = of_property_read_u32(child_node, "led-max-microamp",
663 &cfg->iout_torch_max[fled_id]);
664 if (ret < 0) {
665 cfg->iout_torch_max[fled_id] = TORCH_IOUT_MIN;
666 dev_warn(dev, "led-max-microamp DT property missing\n");
669 ret = of_property_read_u32(child_node, "flash-max-microamp",
670 &cfg->iout_flash_max[fled_id]);
671 if (ret < 0) {
672 cfg->iout_flash_max[fled_id] = FLASH_IOUT_MIN;
673 dev_warn(dev,
674 "flash-max-microamp DT property missing\n");
677 ret = of_property_read_u32(child_node, "flash-max-timeout-us",
678 &cfg->flash_timeout_max[fled_id]);
679 if (ret < 0) {
680 cfg->flash_timeout_max[fled_id] = FLASH_TIMEOUT_MIN;
681 dev_warn(dev,
682 "flash-max-timeout-us DT property missing\n");
685 if (++cfg->num_leds == 2 ||
686 (max77693_fled_used(led, FLED1) &&
687 max77693_fled_used(led, FLED2))) {
688 of_node_put(child_node);
689 break;
693 if (cfg->num_leds == 0) {
694 dev_err(dev, "No DT child node found for connected LED(s).\n");
695 return -EINVAL;
698 return 0;
701 static void clamp_align(u32 *v, u32 min, u32 max, u32 step)
703 *v = clamp_val(*v, min, max);
704 if (step > 1)
705 *v = (*v - min) / step * step + min;
708 static void max77693_align_iout_current(struct max77693_led_device *led,
709 u32 *iout, u32 min, u32 max, u32 step)
711 int i;
713 if (led->iout_joint) {
714 if (iout[FLED1] > min) {
715 iout[FLED1] /= 2;
716 iout[FLED2] = iout[FLED1];
717 } else {
718 iout[FLED1] = min;
719 iout[FLED2] = 0;
720 return;
724 for (i = FLED1; i <= FLED2; ++i)
725 if (max77693_fled_used(led, i))
726 clamp_align(&iout[i], min, max, step);
727 else
728 iout[i] = 0;
731 static void max77693_led_validate_configuration(struct max77693_led_device *led,
732 struct max77693_led_config_data *cfg)
734 u32 flash_iout_max = cfg->boost_mode ? FLASH_IOUT_MAX_2LEDS :
735 FLASH_IOUT_MAX_1LED;
736 int i;
738 if (cfg->num_leds == 1 &&
739 max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
740 led->iout_joint = true;
742 cfg->boost_mode = clamp_val(cfg->boost_mode, MAX77693_LED_BOOST_NONE,
743 MAX77693_LED_BOOST_FIXED);
745 /* Boost must be enabled if both current outputs are used */
746 if ((cfg->boost_mode == MAX77693_LED_BOOST_NONE) && led->iout_joint)
747 cfg->boost_mode = MAX77693_LED_BOOST_FIXED;
749 max77693_align_iout_current(led, cfg->iout_torch_max,
750 TORCH_IOUT_MIN, TORCH_IOUT_MAX, TORCH_IOUT_STEP);
752 max77693_align_iout_current(led, cfg->iout_flash_max,
753 FLASH_IOUT_MIN, flash_iout_max, FLASH_IOUT_STEP);
755 for (i = 0; i < ARRAY_SIZE(cfg->flash_timeout_max); ++i)
756 clamp_align(&cfg->flash_timeout_max[i], FLASH_TIMEOUT_MIN,
757 FLASH_TIMEOUT_MAX, FLASH_TIMEOUT_STEP);
759 clamp_align(&cfg->boost_vout, FLASH_VOUT_MIN, FLASH_VOUT_MAX,
760 FLASH_VOUT_STEP);
762 if (cfg->low_vsys)
763 clamp_align(&cfg->low_vsys, MAX_FLASH1_VSYS_MIN,
764 MAX_FLASH1_VSYS_MAX, MAX_FLASH1_VSYS_STEP);
767 static int max77693_led_get_configuration(struct max77693_led_device *led,
768 struct max77693_led_config_data *cfg,
769 struct device_node **sub_nodes)
771 int ret;
773 ret = max77693_led_parse_dt(led, cfg, sub_nodes);
774 if (ret < 0)
775 return ret;
777 max77693_led_validate_configuration(led, cfg);
779 memcpy(led->iout_torch_max, cfg->iout_torch_max,
780 sizeof(led->iout_torch_max));
781 memcpy(led->iout_flash_max, cfg->iout_flash_max,
782 sizeof(led->iout_flash_max));
784 return 0;
787 static const struct led_flash_ops flash_ops = {
788 .flash_brightness_set = max77693_led_flash_brightness_set,
789 .strobe_set = max77693_led_flash_strobe_set,
790 .strobe_get = max77693_led_flash_strobe_get,
791 .timeout_set = max77693_led_flash_timeout_set,
792 .fault_get = max77693_led_flash_fault_get,
795 static void max77693_init_flash_settings(struct max77693_sub_led *sub_led,
796 struct max77693_led_config_data *led_cfg)
798 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
799 struct max77693_led_device *led = sub_led_to_led(sub_led);
800 int fled_id = sub_led->fled_id;
801 struct led_flash_setting *setting;
803 /* Init flash intensity setting */
804 setting = &fled_cdev->brightness;
805 setting->min = FLASH_IOUT_MIN;
806 setting->max = led->iout_joint ?
807 led_cfg->iout_flash_max[FLED1] +
808 led_cfg->iout_flash_max[FLED2] :
809 led_cfg->iout_flash_max[fled_id];
810 setting->step = FLASH_IOUT_STEP;
811 setting->val = setting->max;
813 /* Init flash timeout setting */
814 setting = &fled_cdev->timeout;
815 setting->min = FLASH_TIMEOUT_MIN;
816 setting->max = led_cfg->flash_timeout_max[fled_id];
817 setting->step = FLASH_TIMEOUT_STEP;
818 setting->val = setting->max;
821 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
823 static int max77693_led_external_strobe_set(
824 struct v4l2_flash *v4l2_flash,
825 bool enable)
827 struct max77693_sub_led *sub_led =
828 flcdev_to_sub_led(v4l2_flash->fled_cdev);
829 struct max77693_led_device *led = sub_led_to_led(sub_led);
830 int fled_id = sub_led->fled_id;
831 int ret;
833 mutex_lock(&led->lock);
835 if (enable)
836 ret = max77693_add_mode(led, MODE_FLASH_EXTERNAL(fled_id));
837 else
838 ret = max77693_clear_mode(led, MODE_FLASH_EXTERNAL(fled_id));
840 mutex_unlock(&led->lock);
842 return ret;
845 static void max77693_init_v4l2_flash_config(struct max77693_sub_led *sub_led,
846 struct max77693_led_config_data *led_cfg,
847 struct v4l2_flash_config *v4l2_sd_cfg)
849 struct max77693_led_device *led = sub_led_to_led(sub_led);
850 struct device *dev = &led->pdev->dev;
851 struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
852 struct i2c_client *i2c = iodev->i2c;
853 struct led_flash_setting *s;
855 snprintf(v4l2_sd_cfg->dev_name, sizeof(v4l2_sd_cfg->dev_name),
856 "%s %d-%04x", sub_led->fled_cdev.led_cdev.name,
857 i2c_adapter_id(i2c->adapter), i2c->addr);
859 s = &v4l2_sd_cfg->torch_intensity;
860 s->min = TORCH_IOUT_MIN;
861 s->max = sub_led->fled_cdev.led_cdev.max_brightness * TORCH_IOUT_STEP;
862 s->step = TORCH_IOUT_STEP;
863 s->val = s->max;
865 /* Init flash faults config */
866 v4l2_sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE |
867 LED_FAULT_SHORT_CIRCUIT |
868 LED_FAULT_OVER_CURRENT;
870 v4l2_sd_cfg->has_external_strobe = true;
873 static const struct v4l2_flash_ops v4l2_flash_ops = {
874 .external_strobe_set = max77693_led_external_strobe_set,
876 #else
877 static inline void max77693_init_v4l2_flash_config(
878 struct max77693_sub_led *sub_led,
879 struct max77693_led_config_data *led_cfg,
880 struct v4l2_flash_config *v4l2_sd_cfg)
883 static const struct v4l2_flash_ops v4l2_flash_ops;
884 #endif
886 static void max77693_init_fled_cdev(struct max77693_sub_led *sub_led,
887 struct max77693_led_config_data *led_cfg)
889 struct max77693_led_device *led = sub_led_to_led(sub_led);
890 int fled_id = sub_led->fled_id;
891 struct led_classdev_flash *fled_cdev;
892 struct led_classdev *led_cdev;
894 /* Initialize LED Flash class device */
895 fled_cdev = &sub_led->fled_cdev;
896 fled_cdev->ops = &flash_ops;
897 led_cdev = &fled_cdev->led_cdev;
899 led_cdev->name = led_cfg->label[fled_id];
901 led_cdev->brightness_set_blocking = max77693_led_brightness_set;
902 led_cdev->max_brightness = (led->iout_joint ?
903 led_cfg->iout_torch_max[FLED1] +
904 led_cfg->iout_torch_max[FLED2] :
905 led_cfg->iout_torch_max[fled_id]) /
906 TORCH_IOUT_STEP;
907 led_cdev->flags |= LED_DEV_CAP_FLASH;
909 max77693_init_flash_settings(sub_led, led_cfg);
911 /* Init flash timeout cache */
912 sub_led->flash_timeout = fled_cdev->timeout.val;
915 static int max77693_register_led(struct max77693_sub_led *sub_led,
916 struct max77693_led_config_data *led_cfg,
917 struct device_node *sub_node)
919 struct max77693_led_device *led = sub_led_to_led(sub_led);
920 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
921 struct device *dev = &led->pdev->dev;
922 struct v4l2_flash_config v4l2_sd_cfg = {};
923 int ret;
925 /* Register in the LED subsystem */
926 ret = led_classdev_flash_register(dev, fled_cdev);
927 if (ret < 0)
928 return ret;
930 max77693_init_v4l2_flash_config(sub_led, led_cfg, &v4l2_sd_cfg);
932 /* Register in the V4L2 subsystem. */
933 sub_led->v4l2_flash = v4l2_flash_init(dev, sub_node, fled_cdev, NULL,
934 &v4l2_flash_ops, &v4l2_sd_cfg);
935 if (IS_ERR(sub_led->v4l2_flash)) {
936 ret = PTR_ERR(sub_led->v4l2_flash);
937 goto err_v4l2_flash_init;
940 return 0;
942 err_v4l2_flash_init:
943 led_classdev_flash_unregister(fled_cdev);
944 return ret;
947 static int max77693_led_probe(struct platform_device *pdev)
949 struct device *dev = &pdev->dev;
950 struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
951 struct max77693_led_device *led;
952 struct max77693_sub_led *sub_leds;
953 struct device_node *sub_nodes[2] = {};
954 struct max77693_led_config_data led_cfg = {};
955 int init_fled_cdev[2], i, ret;
957 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
958 if (!led)
959 return -ENOMEM;
961 led->pdev = pdev;
962 led->regmap = iodev->regmap;
963 led->allowed_modes = MODE_FLASH_MASK;
964 sub_leds = led->sub_leds;
966 platform_set_drvdata(pdev, led);
967 ret = max77693_led_get_configuration(led, &led_cfg, sub_nodes);
968 if (ret < 0)
969 return ret;
971 ret = max77693_setup(led, &led_cfg);
972 if (ret < 0)
973 return ret;
975 mutex_init(&led->lock);
977 init_fled_cdev[FLED1] =
978 led->iout_joint || max77693_fled_used(led, FLED1);
979 init_fled_cdev[FLED2] =
980 !led->iout_joint && max77693_fled_used(led, FLED2);
982 for (i = FLED1; i <= FLED2; ++i) {
983 if (!init_fled_cdev[i])
984 continue;
986 /* Initialize LED Flash class device */
987 max77693_init_fled_cdev(&sub_leds[i], &led_cfg);
990 * Register LED Flash class device and corresponding
991 * V4L2 Flash device.
993 ret = max77693_register_led(&sub_leds[i], &led_cfg,
994 sub_nodes[i]);
995 if (ret < 0) {
997 * At this moment FLED1 might have been already
998 * registered and it needs to be released.
1000 if (i == FLED2)
1001 goto err_register_led2;
1002 else
1003 goto err_register_led1;
1007 return 0;
1009 err_register_led2:
1010 /* It is possible than only FLED2 was to be registered */
1011 if (!init_fled_cdev[FLED1])
1012 goto err_register_led1;
1013 v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1014 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1015 err_register_led1:
1016 mutex_destroy(&led->lock);
1018 return ret;
1021 static int max77693_led_remove(struct platform_device *pdev)
1023 struct max77693_led_device *led = platform_get_drvdata(pdev);
1024 struct max77693_sub_led *sub_leds = led->sub_leds;
1026 if (led->iout_joint || max77693_fled_used(led, FLED1)) {
1027 v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
1028 led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
1031 if (!led->iout_joint && max77693_fled_used(led, FLED2)) {
1032 v4l2_flash_release(sub_leds[FLED2].v4l2_flash);
1033 led_classdev_flash_unregister(&sub_leds[FLED2].fled_cdev);
1036 mutex_destroy(&led->lock);
1038 return 0;
1041 static const struct of_device_id max77693_led_dt_match[] = {
1042 { .compatible = "maxim,max77693-led" },
1045 MODULE_DEVICE_TABLE(of, max77693_led_dt_match);
1047 static struct platform_driver max77693_led_driver = {
1048 .probe = max77693_led_probe,
1049 .remove = max77693_led_remove,
1050 .driver = {
1051 .name = "max77693-led",
1052 .of_match_table = max77693_led_dt_match,
1056 module_platform_driver(max77693_led_driver);
1058 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
1059 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1060 MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
1061 MODULE_LICENSE("GPL v2");