Lines Matching +full:current +full:- +full:boost +full:- +full:microamp
1 // SPDX-License-Identifier: GPL-2.0-only
11 #include <linux/led-class-flash.h>
13 #include <linux/mfd/max77693-common.h>
14 #include <linux/mfd/max77693-private.h>
20 #include <media/v4l2-flash-led-class.h>
83 /* maximum torch current values for FLED outputs */
85 /* maximum flash current values for FLED outputs */
88 /* current flash timeout cache */
96 /* bitmask of FLED outputs use state (bit 0. - FLED1, bit 1. - FLED2) */
101 /* arrangement of current outputs */
109 return (ua - FLASH_IOUT_MIN) / FLASH_IOUT_STEP; in max77693_led_iout_to_reg()
114 return (us - FLASH_TIMEOUT_MIN) / FLASH_TIMEOUT_STEP; in max77693_flash_timeout_to_reg()
127 sub_leds[sub_led->fled_id]); in sub_led_to_led()
132 return ((mv - MAX_FLASH1_VSYS_MIN) / MAX_FLASH1_VSYS_STEP) << 2; in max77693_led_vsys_to_reg()
137 return (mv - FLASH_VOUT_MIN) / FLASH_VOUT_STEP + FLASH_VOUT_RMIN; in max77693_led_vout_to_reg()
145 return led->fled_mask & fled_bit; in max77693_fled_used()
150 struct regmap *rmap = led->regmap; in max77693_set_mode_reg()
185 if (led->iout_joint) in max77693_add_mode()
196 led->mode_flags &= (~MODE_TORCH(i) & ~MODE_FLASH(i)); in max77693_add_mode()
198 new_mode_flags = mode | led->mode_flags; in max77693_add_mode()
199 new_mode_flags &= led->allowed_modes; in max77693_add_mode()
201 if (new_mode_flags ^ led->mode_flags) in max77693_add_mode()
202 led->mode_flags = new_mode_flags; in max77693_add_mode()
206 ret = max77693_set_mode_reg(led, led->mode_flags); in max77693_add_mode()
215 led->mode_flags &= ~mode; in max77693_add_mode()
223 if (led->iout_joint) in max77693_clear_mode()
227 led->mode_flags &= ~mode; in max77693_clear_mode()
229 return max77693_set_mode_reg(led, led->mode_flags); in max77693_clear_mode()
236 led->allowed_modes |= (MODE_FLASH(fled_id) | in max77693_add_allowed_modes()
239 led->allowed_modes |= MODE_TORCH(fled_id); in max77693_add_allowed_modes()
246 if (!led->iout_joint) { in max77693_distribute_currents()
253 iout[FLED2] = micro_amp - iout[FLED1]; in max77693_distribute_currents()
256 led->allowed_modes &= ~MODE_FLASH_MASK; in max77693_distribute_currents()
258 led->allowed_modes &= ~MODE_TORCH_MASK; in max77693_distribute_currents()
269 struct regmap *rmap = led->regmap; in max77693_set_torch_current()
274 led->iout_torch_max, iout); in max77693_set_torch_current()
276 if (fled_id == FLED1 || led->iout_joint) { in max77693_set_torch_current()
278 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT2_SHIFT); in max77693_set_torch_current()
280 if (fled_id == FLED2 || led->iout_joint) { in max77693_set_torch_current()
282 led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT1_SHIFT); in max77693_set_torch_current()
285 led->torch_iout_reg |= ((iout1_reg << TORCH_IOUT1_SHIFT) | in max77693_set_torch_current()
289 led->torch_iout_reg); in max77693_set_torch_current()
296 struct regmap *rmap = led->regmap; in max77693_set_flash_current()
299 int ret = -EINVAL; in max77693_set_flash_current()
302 led->iout_flash_max, iout); in max77693_set_flash_current()
304 if (fled_id == FLED1 || led->iout_joint) { in max77693_set_flash_current()
311 if (fled_id == FLED2 || led->iout_joint) { in max77693_set_flash_current()
322 struct regmap *rmap = led->regmap; in max77693_set_timeout()
332 led->current_flash_timeout = microsec; in max77693_set_timeout()
340 struct regmap *rmap = led->regmap; in max77693_get_strobe_status()
356 struct regmap *rmap = led->regmap; in max77693_get_flash_faults()
361 sub_led->flash_faults = 0; in max77693_get_flash_faults()
363 if (led->iout_joint) { in max77693_get_flash_faults()
368 fault_open_mask = (sub_led->fled_id == FLED1) ? in max77693_get_flash_faults()
371 fault_short_mask = (sub_led->fled_id == FLED1) ? in max77693_get_flash_faults()
381 sub_led->flash_faults |= LED_FAULT_OVER_VOLTAGE; in max77693_get_flash_faults()
383 sub_led->flash_faults |= LED_FAULT_SHORT_CIRCUIT; in max77693_get_flash_faults()
385 sub_led->flash_faults |= LED_FAULT_OVER_CURRENT; in max77693_get_flash_faults()
393 struct regmap *rmap = led->regmap; in max77693_setup()
399 * Initialize only flash current. Torch current doesn't in max77693_setup()
403 if (led->iout_joint) { in max77693_setup()
406 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1] + in max77693_setup()
407 led_cfg->iout_flash_max[FLED2]; in max77693_setup()
411 max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1]; in max77693_setup()
412 max_flash_curr[FLED2] = led_cfg->iout_flash_max[FLED2]; in max77693_setup()
427 if (led_cfg->low_vsys > 0) in max77693_setup()
428 v = max77693_led_vsys_to_reg(led_cfg->low_vsys) | in max77693_setup()
440 if (led_cfg->boost_mode == MAX77693_LED_BOOST_FIXED) in max77693_setup()
443 v = led_cfg->boost_mode | led_cfg->boost_mode << 1; in max77693_setup()
452 v = max77693_led_vout_to_reg(led_cfg->boost_vout); in max77693_setup()
467 int fled_id = sub_led->fled_id, ret; in max77693_led_brightness_set()
469 mutex_lock(&led->lock); in max77693_led_brightness_set()
474 dev_dbg(&led->pdev->dev, in max77693_led_brightness_set()
482 dev_dbg(&led->pdev->dev, in max77693_led_brightness_set()
483 "Failed to set torch current (%d)\n", in max77693_led_brightness_set()
490 dev_dbg(&led->pdev->dev, in max77693_led_brightness_set()
494 mutex_unlock(&led->lock); in max77693_led_brightness_set()
507 mutex_lock(&led->lock); in max77693_led_flash_brightness_set()
508 ret = max77693_set_flash_current(led, sub_led->fled_id, brightness); in max77693_led_flash_brightness_set()
509 mutex_unlock(&led->lock); in max77693_led_flash_brightness_set()
520 int fled_id = sub_led->fled_id; in max77693_led_flash_strobe_set()
523 mutex_lock(&led->lock); in max77693_led_flash_strobe_set()
530 if (sub_led->flash_timeout != led->current_flash_timeout) { in max77693_led_flash_strobe_set()
531 ret = max77693_set_timeout(led, sub_led->flash_timeout); in max77693_led_flash_strobe_set()
536 led->strobing_sub_led_id = fled_id; in max77693_led_flash_strobe_set()
545 mutex_unlock(&led->lock); in max77693_led_flash_strobe_set()
555 *fault = sub_led->flash_faults; in max77693_led_flash_fault_get()
569 return -EINVAL; in max77693_led_flash_strobe_get()
571 mutex_lock(&led->lock); in max77693_led_flash_strobe_get()
575 *state = !!(*state && (led->strobing_sub_led_id == sub_led->fled_id)); in max77693_led_flash_strobe_get()
577 mutex_unlock(&led->lock); in max77693_led_flash_strobe_get()
589 mutex_lock(&led->lock); in max77693_led_flash_timeout_set()
590 sub_led->flash_timeout = timeout; in max77693_led_flash_timeout_set()
591 mutex_unlock(&led->lock); in max77693_led_flash_timeout_set()
600 struct device *dev = &led->pdev->dev; in max77693_led_parse_dt()
601 struct max77693_sub_led *sub_leds = led->sub_leds; in max77693_led_parse_dt()
607 of_property_read_u32(node, "maxim,boost-mode", &cfg->boost_mode); in max77693_led_parse_dt()
608 of_property_read_u32(node, "maxim,boost-mvout", &cfg->boost_vout); in max77693_led_parse_dt()
609 of_property_read_u32(node, "maxim,mvsys-min", &cfg->low_vsys); in max77693_led_parse_dt()
612 prop = of_find_property(child_node, "led-sources", NULL); in max77693_led_parse_dt()
624 "led-sources DT property missing\n"); in max77693_led_parse_dt()
625 return -EINVAL; in max77693_led_parse_dt()
630 led->fled_mask = FLED1_IOUT | FLED2_IOUT; in max77693_led_parse_dt()
633 led->fled_mask |= FLED1_IOUT; in max77693_led_parse_dt()
636 led->fled_mask |= FLED2_IOUT; in max77693_led_parse_dt()
639 "Wrong led-sources DT property value.\n"); in max77693_led_parse_dt()
640 return -EINVAL; in max77693_led_parse_dt()
645 "Conflicting \"led-sources\" DT properties\n"); in max77693_led_parse_dt()
646 return -EINVAL; in max77693_led_parse_dt()
652 cfg->label[fled_id] = in max77693_led_parse_dt()
654 child_node->name; in max77693_led_parse_dt()
656 ret = of_property_read_u32(child_node, "led-max-microamp", in max77693_led_parse_dt()
657 &cfg->iout_torch_max[fled_id]); in max77693_led_parse_dt()
659 cfg->iout_torch_max[fled_id] = TORCH_IOUT_MIN; in max77693_led_parse_dt()
660 dev_warn(dev, "led-max-microamp DT property missing\n"); in max77693_led_parse_dt()
663 ret = of_property_read_u32(child_node, "flash-max-microamp", in max77693_led_parse_dt()
664 &cfg->iout_flash_max[fled_id]); in max77693_led_parse_dt()
666 cfg->iout_flash_max[fled_id] = FLASH_IOUT_MIN; in max77693_led_parse_dt()
668 "flash-max-microamp DT property missing\n"); in max77693_led_parse_dt()
671 ret = of_property_read_u32(child_node, "flash-max-timeout-us", in max77693_led_parse_dt()
672 &cfg->flash_timeout_max[fled_id]); in max77693_led_parse_dt()
674 cfg->flash_timeout_max[fled_id] = FLASH_TIMEOUT_MIN; in max77693_led_parse_dt()
676 "flash-max-timeout-us DT property missing\n"); in max77693_led_parse_dt()
679 if (++cfg->num_leds == 2 || in max77693_led_parse_dt()
685 if (cfg->num_leds == 0) { in max77693_led_parse_dt()
687 return -EINVAL; in max77693_led_parse_dt()
697 *v = (*v - min) / step * step + min; in clamp_align()
705 if (led->iout_joint) { in max77693_align_iout_current()
726 u32 flash_iout_max = cfg->boost_mode ? FLASH_IOUT_MAX_2LEDS : in max77693_led_validate_configuration()
730 if (cfg->num_leds == 1 && in max77693_led_validate_configuration()
732 led->iout_joint = true; in max77693_led_validate_configuration()
734 cfg->boost_mode = clamp_val(cfg->boost_mode, MAX77693_LED_BOOST_NONE, in max77693_led_validate_configuration()
737 /* Boost must be enabled if both current outputs are used */ in max77693_led_validate_configuration()
738 if ((cfg->boost_mode == MAX77693_LED_BOOST_NONE) && led->iout_joint) in max77693_led_validate_configuration()
739 cfg->boost_mode = MAX77693_LED_BOOST_FIXED; in max77693_led_validate_configuration()
741 max77693_align_iout_current(led, cfg->iout_torch_max, in max77693_led_validate_configuration()
744 max77693_align_iout_current(led, cfg->iout_flash_max, in max77693_led_validate_configuration()
747 for (i = 0; i < ARRAY_SIZE(cfg->flash_timeout_max); ++i) in max77693_led_validate_configuration()
748 clamp_align(&cfg->flash_timeout_max[i], FLASH_TIMEOUT_MIN, in max77693_led_validate_configuration()
751 clamp_align(&cfg->boost_vout, FLASH_VOUT_MIN, FLASH_VOUT_MAX, in max77693_led_validate_configuration()
754 if (cfg->low_vsys) in max77693_led_validate_configuration()
755 clamp_align(&cfg->low_vsys, MAX_FLASH1_VSYS_MIN, in max77693_led_validate_configuration()
771 memcpy(led->iout_torch_max, cfg->iout_torch_max, in max77693_led_get_configuration()
772 sizeof(led->iout_torch_max)); in max77693_led_get_configuration()
773 memcpy(led->iout_flash_max, cfg->iout_flash_max, in max77693_led_get_configuration()
774 sizeof(led->iout_flash_max)); in max77693_led_get_configuration()
790 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev; in max77693_init_flash_settings()
792 int fled_id = sub_led->fled_id; in max77693_init_flash_settings()
796 setting = &fled_cdev->brightness; in max77693_init_flash_settings()
797 setting->min = FLASH_IOUT_MIN; in max77693_init_flash_settings()
798 setting->max = led->iout_joint ? in max77693_init_flash_settings()
799 led_cfg->iout_flash_max[FLED1] + in max77693_init_flash_settings()
800 led_cfg->iout_flash_max[FLED2] : in max77693_init_flash_settings()
801 led_cfg->iout_flash_max[fled_id]; in max77693_init_flash_settings()
802 setting->step = FLASH_IOUT_STEP; in max77693_init_flash_settings()
803 setting->val = setting->max; in max77693_init_flash_settings()
806 setting = &fled_cdev->timeout; in max77693_init_flash_settings()
807 setting->min = FLASH_TIMEOUT_MIN; in max77693_init_flash_settings()
808 setting->max = led_cfg->flash_timeout_max[fled_id]; in max77693_init_flash_settings()
809 setting->step = FLASH_TIMEOUT_STEP; in max77693_init_flash_settings()
810 setting->val = setting->max; in max77693_init_flash_settings()
820 flcdev_to_sub_led(v4l2_flash->fled_cdev); in max77693_led_external_strobe_set()
822 int fled_id = sub_led->fled_id; in max77693_led_external_strobe_set()
825 mutex_lock(&led->lock); in max77693_led_external_strobe_set()
832 mutex_unlock(&led->lock); in max77693_led_external_strobe_set()
842 struct device *dev = &led->pdev->dev; in max77693_init_v4l2_flash_config()
843 struct max77693_dev *iodev = dev_get_drvdata(dev->parent); in max77693_init_v4l2_flash_config()
844 struct i2c_client *i2c = iodev->i2c; in max77693_init_v4l2_flash_config()
847 snprintf(v4l2_sd_cfg->dev_name, sizeof(v4l2_sd_cfg->dev_name), in max77693_init_v4l2_flash_config()
848 "%s %d-%04x", sub_led->fled_cdev.led_cdev.name, in max77693_init_v4l2_flash_config()
849 i2c_adapter_id(i2c->adapter), i2c->addr); in max77693_init_v4l2_flash_config()
851 s = &v4l2_sd_cfg->intensity; in max77693_init_v4l2_flash_config()
852 s->min = TORCH_IOUT_MIN; in max77693_init_v4l2_flash_config()
853 s->max = sub_led->fled_cdev.led_cdev.max_brightness * TORCH_IOUT_STEP; in max77693_init_v4l2_flash_config()
854 s->step = TORCH_IOUT_STEP; in max77693_init_v4l2_flash_config()
855 s->val = s->max; in max77693_init_v4l2_flash_config()
858 v4l2_sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE | in max77693_init_v4l2_flash_config()
862 v4l2_sd_cfg->has_external_strobe = true; in max77693_init_v4l2_flash_config()
882 int fled_id = sub_led->fled_id; in max77693_init_fled_cdev()
887 fled_cdev = &sub_led->fled_cdev; in max77693_init_fled_cdev()
888 fled_cdev->ops = &flash_ops; in max77693_init_fled_cdev()
889 led_cdev = &fled_cdev->led_cdev; in max77693_init_fled_cdev()
891 led_cdev->name = led_cfg->label[fled_id]; in max77693_init_fled_cdev()
893 led_cdev->brightness_set_blocking = max77693_led_brightness_set; in max77693_init_fled_cdev()
894 led_cdev->max_brightness = (led->iout_joint ? in max77693_init_fled_cdev()
895 led_cfg->iout_torch_max[FLED1] + in max77693_init_fled_cdev()
896 led_cfg->iout_torch_max[FLED2] : in max77693_init_fled_cdev()
897 led_cfg->iout_torch_max[fled_id]) / in max77693_init_fled_cdev()
899 led_cdev->flags |= LED_DEV_CAP_FLASH; in max77693_init_fled_cdev()
904 sub_led->flash_timeout = fled_cdev->timeout.val; in max77693_init_fled_cdev()
912 struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev; in max77693_register_led()
913 struct device *dev = &led->pdev->dev; in max77693_register_led()
925 sub_led->v4l2_flash = v4l2_flash_init(dev, of_fwnode_handle(sub_node), in max77693_register_led()
928 if (IS_ERR(sub_led->v4l2_flash)) { in max77693_register_led()
929 ret = PTR_ERR(sub_led->v4l2_flash); in max77693_register_led()
942 struct device *dev = &pdev->dev; in max77693_led_probe()
943 struct max77693_dev *iodev = dev_get_drvdata(dev->parent); in max77693_led_probe()
952 return -ENOMEM; in max77693_led_probe()
954 led->pdev = pdev; in max77693_led_probe()
955 led->regmap = iodev->regmap; in max77693_led_probe()
956 led->allowed_modes = MODE_FLASH_MASK; in max77693_led_probe()
957 sub_leds = led->sub_leds; in max77693_led_probe()
968 mutex_init(&led->lock); in max77693_led_probe()
971 led->iout_joint || max77693_fled_used(led, FLED1); in max77693_led_probe()
973 !led->iout_joint && max77693_fled_used(led, FLED2); in max77693_led_probe()
1011 mutex_destroy(&led->lock); in max77693_led_probe()
1022 struct max77693_sub_led *sub_leds = led->sub_leds; in max77693_led_remove()
1024 if (led->iout_joint || max77693_fled_used(led, FLED1)) { in max77693_led_remove()
1029 if (!led->iout_joint && max77693_fled_used(led, FLED2)) { in max77693_led_remove()
1034 mutex_destroy(&led->lock); in max77693_led_remove()
1038 { .compatible = "maxim,max77693-led" },
1047 .name = "max77693-led",