Lines Matching +full:flash +full:- +full:led

1 // SPDX-License-Identifier: GPL-2.0-only
9 #include <linux/led-class-flash.h>
14 #include <media/v4l2-flash-led-class.h>
140 struct led_classdev_flash flash; member
151 static int set_flash_module_en(struct qcom_flash_led *led, bool en) in set_flash_module_en() argument
153 struct qcom_flash_data *flash_data = led->flash_data; in set_flash_module_en()
157 for (i = 0; i < led->chan_count; i++) in set_flash_module_en()
158 led_mask |= BIT(led->chan_id[i]); in set_flash_module_en()
160 mutex_lock(&flash_data->lock); in set_flash_module_en()
162 flash_data->chan_en_bits |= led_mask; in set_flash_module_en()
164 flash_data->chan_en_bits &= ~led_mask; in set_flash_module_en()
166 enable = !!flash_data->chan_en_bits; in set_flash_module_en()
167 rc = regmap_field_write(flash_data->r_fields[REG_MODULE_EN], enable); in set_flash_module_en()
169 dev_err(led->flash.led_cdev.dev, "write module_en failed, rc=%d\n", rc); in set_flash_module_en()
170 mutex_unlock(&flash_data->lock); in set_flash_module_en()
175 static int set_flash_current(struct qcom_flash_led *led, u32 current_ma, enum led_mode mode) in set_flash_current() argument
177 struct qcom_flash_data *flash_data = led->flash_data; in set_flash_current()
186 itarg_ua = (current_ma * UA_PER_MA) / led->chan_count + 1; in set_flash_current()
189 for (i = 0; i < led->chan_count; i++) { in set_flash_current()
193 itarget = itarg_ua / ires_ua - 1; in set_flash_current()
195 chan_id = led->chan_id[i]; in set_flash_current()
197 rc = regmap_fields_write(flash_data->r_fields[REG_ITARGET], chan_id, itarget); in set_flash_current()
201 if (flash_data->hw_type == QCOM_MVFLASH_3CH) { in set_flash_current()
207 } else if (flash_data->hw_type == QCOM_MVFLASH_4CH) { in set_flash_current()
214 dev_err(led->flash.led_cdev.dev, in set_flash_current()
215 "HW type %d is not supported\n", flash_data->hw_type); in set_flash_current()
216 return -EOPNOTSUPP; in set_flash_current()
220 return regmap_field_update_bits(flash_data->r_fields[REG_IRESOLUTION], ires_mask, ires_val); in set_flash_current()
223 static int set_flash_timeout(struct qcom_flash_led *led, u32 timeout_ms) in set_flash_timeout() argument
225 struct qcom_flash_data *flash_data = led->flash_data; in set_flash_timeout()
229 /* set SAFETY_TIMER for all the channels connected to the same LED */ in set_flash_timeout()
230 timeout_ms = min_t(u32, timeout_ms, led->max_timeout_ms); in set_flash_timeout()
232 for (i = 0; i < led->chan_count; i++) { in set_flash_timeout()
233 chan_id = led->chan_id[i]; in set_flash_timeout()
241 rc = regmap_fields_write(flash_data->r_fields[REG_CHAN_TIMER], chan_id, timer); in set_flash_timeout()
249 static int set_flash_strobe(struct qcom_flash_led *led, enum led_strobe strobe, bool state) in set_flash_strobe() argument
251 struct qcom_flash_data *flash_data = led->flash_data; in set_flash_strobe()
255 /* Set SW strobe config for all channels connected to the LED */ in set_flash_strobe()
256 for (i = 0; i < led->chan_count; i++) { in set_flash_strobe()
257 chan_id = led->chan_id[i]; in set_flash_strobe()
269 flash_data->r_fields[REG_CHAN_STROBE], chan_id, strobe_sel); in set_flash_strobe()
276 /* Enable/disable flash channels */ in set_flash_strobe()
278 rc = regmap_field_update_bits(flash_data->r_fields[REG_CHAN_EN], chan_mask, chan_en); in set_flash_strobe()
282 led->enabled = state; in set_flash_strobe()
288 return container_of(flcdev, struct qcom_flash_led, flash); in flcdev_to_qcom_fled()
293 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); in qcom_flash_brightness_set() local
295 led->flash_current_ma = min_t(u32, led->max_flash_current_ma, brightness / UA_PER_MA); in qcom_flash_brightness_set()
301 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); in qcom_flash_timeout_set() local
303 led->flash_timeout_ms = timeout / USEC_PER_MSEC; in qcom_flash_timeout_set()
309 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); in qcom_flash_strobe_set() local
312 rc = set_flash_strobe(led, SW_STROBE, false); in qcom_flash_strobe_set()
316 rc = set_flash_current(led, led->flash_current_ma, FLASH_MODE); in qcom_flash_strobe_set()
320 rc = set_flash_timeout(led, led->flash_timeout_ms); in qcom_flash_strobe_set()
324 rc = set_flash_module_en(led, state); in qcom_flash_strobe_set()
328 return set_flash_strobe(led, SW_STROBE, state); in qcom_flash_strobe_set()
333 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); in qcom_flash_strobe_get() local
335 *state = led->enabled; in qcom_flash_strobe_get()
341 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); in qcom_flash_fault_get() local
342 struct qcom_flash_data *flash_data = led->flash_data; in qcom_flash_fault_get()
348 rc = regmap_field_read(flash_data->r_fields[REG_STATUS1], &val); in qcom_flash_fault_get()
352 for (i = 0; i < led->chan_count; i++) { in qcom_flash_fault_get()
353 chan_id = led->chan_id[i]; in qcom_flash_fault_get()
362 rc = regmap_field_read(flash_data->r_fields[REG_STATUS2], &val); in qcom_flash_fault_get()
366 if (flash_data->hw_type == QCOM_MVFLASH_3CH) { in qcom_flash_fault_get()
375 } else if (flash_data->hw_type == QCOM_MVFLASH_4CH) { in qcom_flash_fault_get()
394 rc = regmap_field_read(flash_data->r_fields[REG_STATUS3], &val); in qcom_flash_fault_get()
398 if (flash_data->hw_type == QCOM_MVFLASH_3CH) { in qcom_flash_fault_get()
401 } else if (flash_data->hw_type == QCOM_MVFLASH_4CH) { in qcom_flash_fault_get()
402 for (i = 0; i < led->chan_count; i++) { in qcom_flash_fault_get()
403 chan_id = led->chan_id[i]; in qcom_flash_fault_get()
419 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); in qcom_flash_led_brightness_set() local
420 u32 current_ma = brightness * led->max_torch_current_ma / LED_FULL; in qcom_flash_led_brightness_set()
424 rc = set_flash_strobe(led, SW_STROBE, false); in qcom_flash_led_brightness_set()
428 rc = set_flash_module_en(led, false); in qcom_flash_led_brightness_set()
432 rc = set_flash_current(led, current_ma, TORCH_MODE); in qcom_flash_led_brightness_set()
436 /* Disable flash timeout for torch LED */ in qcom_flash_led_brightness_set()
437 rc = set_flash_timeout(led, 0); in qcom_flash_led_brightness_set()
441 rc = set_flash_module_en(led, enable); in qcom_flash_led_brightness_set()
445 return set_flash_strobe(led, SW_STROBE, enable); in qcom_flash_led_brightness_set()
459 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; in qcom_flash_external_strobe_set()
460 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); in qcom_flash_external_strobe_set() local
463 rc = set_flash_module_en(led, enable); in qcom_flash_external_strobe_set()
468 return set_flash_strobe(led, HW_STROBE, true); in qcom_flash_external_strobe_set()
470 return set_flash_strobe(led, SW_STROBE, false); in qcom_flash_external_strobe_set()
476 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; in qcom_flash_intensity_to_led_brightness()
477 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); in qcom_flash_intensity_to_led_brightness() local
480 current_ma = min_t(u32, current_ma, led->max_torch_current_ma); in qcom_flash_intensity_to_led_brightness()
484 return (current_ma * LED_FULL) / led->max_torch_current_ma; in qcom_flash_intensity_to_led_brightness()
490 struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; in qcom_flash_brightness_to_led_intensity()
491 struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev); in qcom_flash_brightness_to_led_intensity() local
493 return (brightness * led->max_torch_current_ma * UA_PER_MA) / LED_FULL; in qcom_flash_brightness_to_led_intensity()
503 qcom_flash_v4l2_init(struct device *dev, struct qcom_flash_led *led, struct fwnode_handle *fwnode) in qcom_flash_v4l2_init() argument
505 struct qcom_flash_data *flash_data = led->flash_data; in qcom_flash_v4l2_init()
510 if (!(led->flash.led_cdev.flags & LED_DEV_CAP_FLASH)) in qcom_flash_v4l2_init()
513 intensity->min = intensity->step = TORCH_IRES_UA * led->chan_count; in qcom_flash_v4l2_init()
514 intensity->max = led->max_torch_current_ma * UA_PER_MA; in qcom_flash_v4l2_init()
515 intensity->val = min_t(u32, intensity->max, TORCH_CURRENT_DEFAULT_UA); in qcom_flash_v4l2_init()
517 strscpy(v4l2_cfg.dev_name, led->flash.led_cdev.dev->kobj.name, in qcom_flash_v4l2_init()
527 v4l2_flash = v4l2_flash_init(dev, fwnode, &led->flash, &qcom_v4l2_flash_ops, &v4l2_cfg); in qcom_flash_v4l2_init()
531 flash_data->v4l2_flash[flash_data->leds_count] = v4l2_flash; in qcom_flash_v4l2_init()
536 qcom_flash_v4l2_init(struct device *dev, struct qcom_flash_led *led, struct fwnode_handle *fwnode) in qcom_flash_v4l2_init() argument
543 struct fwnode_handle *node, struct qcom_flash_led *led) in qcom_flash_register_led_device() argument
545 struct qcom_flash_data *flash_data = led->flash_data; in qcom_flash_register_led_device()
547 struct led_classdev_flash *flash = &led->flash; in qcom_flash_register_led_device() local
553 count = fwnode_property_count_u32(node, "led-sources"); in qcom_flash_register_led_device()
555 dev_err(dev, "No led-sources specified\n"); in qcom_flash_register_led_device()
556 return -ENODEV; in qcom_flash_register_led_device()
559 if (count > flash_data->max_channels) { in qcom_flash_register_led_device()
560 dev_err(dev, "led-sources count %u exceeds maximum channel count %u\n", in qcom_flash_register_led_device()
561 count, flash_data->max_channels); in qcom_flash_register_led_device()
562 return -EINVAL; in qcom_flash_register_led_device()
565 rc = fwnode_property_read_u32_array(node, "led-sources", channels, count); in qcom_flash_register_led_device()
567 dev_err(dev, "Failed to read led-sources property, rc=%d\n", rc); in qcom_flash_register_led_device()
571 led->chan_count = count; in qcom_flash_register_led_device()
572 led->chan_id = devm_kcalloc(dev, count, sizeof(u8), GFP_KERNEL); in qcom_flash_register_led_device()
573 if (!led->chan_id) in qcom_flash_register_led_device()
574 return -ENOMEM; in qcom_flash_register_led_device()
577 if ((channels[i] == 0) || (channels[i] > flash_data->max_channels)) { in qcom_flash_register_led_device()
578 dev_err(dev, "led-source out of HW support range [1-%u]\n", in qcom_flash_register_led_device()
579 flash_data->max_channels); in qcom_flash_register_led_device()
580 return -EINVAL; in qcom_flash_register_led_device()
584 led->chan_id[i] = channels[i] - 1; in qcom_flash_register_led_device()
587 rc = fwnode_property_read_u32(node, "led-max-microamp", &current_ua); in qcom_flash_register_led_device()
589 dev_err(dev, "Failed to read led-max-microamp property, rc=%d\n", rc); in qcom_flash_register_led_device()
594 dev_err(dev, "led-max-microamp shouldn't be 0\n"); in qcom_flash_register_led_device()
595 return -EINVAL; in qcom_flash_register_led_device()
598 current_ua = min_t(u32, current_ua, TORCH_CURRENT_MAX_UA * led->chan_count); in qcom_flash_register_led_device()
599 led->max_torch_current_ma = current_ua / UA_PER_MA; in qcom_flash_register_led_device()
601 if (fwnode_property_present(node, "flash-max-microamp")) { in qcom_flash_register_led_device()
602 flash->led_cdev.flags |= LED_DEV_CAP_FLASH; in qcom_flash_register_led_device()
604 rc = fwnode_property_read_u32(node, "flash-max-microamp", &current_ua); in qcom_flash_register_led_device()
606 dev_err(dev, "Failed to read flash-max-microamp property, rc=%d\n", in qcom_flash_register_led_device()
611 current_ua = min_t(u32, current_ua, FLASH_CURRENT_MAX_UA * led->chan_count); in qcom_flash_register_led_device()
614 /* Initialize flash class LED device brightness settings */ in qcom_flash_register_led_device()
615 brightness = &flash->brightness; in qcom_flash_register_led_device()
616 brightness->min = brightness->step = FLASH_IRES_UA * led->chan_count; in qcom_flash_register_led_device()
617 brightness->max = current_ua; in qcom_flash_register_led_device()
618 brightness->val = min_t(u32, current_ua, FLASH_CURRENT_DEFAULT_UA); in qcom_flash_register_led_device()
620 led->max_flash_current_ma = current_ua / UA_PER_MA; in qcom_flash_register_led_device()
621 led->flash_current_ma = brightness->val / UA_PER_MA; in qcom_flash_register_led_device()
623 rc = fwnode_property_read_u32(node, "flash-max-timeout-us", &timeout_us); in qcom_flash_register_led_device()
625 dev_err(dev, "Failed to read flash-max-timeout-us property, rc=%d\n", in qcom_flash_register_led_device()
632 /* Initialize flash class LED device timeout settings */ in qcom_flash_register_led_device()
633 timeout = &flash->timeout; in qcom_flash_register_led_device()
634 timeout->min = timeout->step = FLASH_TIMEOUT_STEP_US; in qcom_flash_register_led_device()
635 timeout->val = timeout->max = timeout_us; in qcom_flash_register_led_device()
637 led->max_timeout_ms = led->flash_timeout_ms = timeout_us / USEC_PER_MSEC; in qcom_flash_register_led_device()
639 flash->ops = &qcom_flash_ops; in qcom_flash_register_led_device()
642 flash->led_cdev.brightness_set_blocking = qcom_flash_led_brightness_set; in qcom_flash_register_led_device()
649 rc = devm_led_classdev_flash_register_ext(dev, flash, &init_data); in qcom_flash_register_led_device()
651 dev_err(dev, "Register flash LED classdev failed, rc=%d\n", rc); in qcom_flash_register_led_device()
655 return qcom_flash_v4l2_init(dev, led, node); in qcom_flash_register_led_device()
661 struct qcom_flash_led *led; in qcom_flash_led_probe() local
663 struct device *dev = &pdev->dev; in qcom_flash_led_probe()
671 return -ENOMEM; in qcom_flash_led_probe()
673 regmap = dev_get_regmap(dev->parent, NULL); in qcom_flash_led_probe()
676 return -EINVAL; in qcom_flash_led_probe()
679 rc = fwnode_property_read_u32(dev->fwnode, "reg", &reg_base); in qcom_flash_led_probe()
687 dev_err(dev, "Read flash LED module type failed, rc=%d\n", rc); in qcom_flash_led_probe()
692 dev_err(dev, "type %#x is not a flash LED module\n", val); in qcom_flash_led_probe()
693 return -ENODEV; in qcom_flash_led_probe()
698 dev_err(dev, "Read flash LED module subtype failed, rc=%d\n", rc); in qcom_flash_led_probe()
703 flash_data->hw_type = QCOM_MVFLASH_3CH; in qcom_flash_led_probe()
704 flash_data->max_channels = 3; in qcom_flash_led_probe()
707 flash_data->hw_type = QCOM_MVFLASH_4CH; in qcom_flash_led_probe()
708 flash_data->max_channels = 4; in qcom_flash_led_probe()
711 dev_err(dev, "flash LED subtype %#x is not yet supported\n", val); in qcom_flash_led_probe()
712 return -ENODEV; in qcom_flash_led_probe()
718 rc = devm_regmap_field_bulk_alloc(dev, regmap, flash_data->r_fields, regs, REG_MAX_COUNT); in qcom_flash_led_probe()
725 mutex_init(&flash_data->lock); in qcom_flash_led_probe()
728 if (count == 0 || count > flash_data->max_channels) { in qcom_flash_led_probe()
729 dev_err(dev, "No child or child count exceeds %d\n", flash_data->max_channels); in qcom_flash_led_probe()
730 return -EINVAL; in qcom_flash_led_probe()
733 flash_data->v4l2_flash = devm_kcalloc(dev, count, in qcom_flash_led_probe()
734 sizeof(*flash_data->v4l2_flash), GFP_KERNEL); in qcom_flash_led_probe()
735 if (!flash_data->v4l2_flash) in qcom_flash_led_probe()
736 return -ENOMEM; in qcom_flash_led_probe()
739 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL); in qcom_flash_led_probe()
740 if (!led) { in qcom_flash_led_probe()
741 rc = -ENOMEM; in qcom_flash_led_probe()
745 led->flash_data = flash_data; in qcom_flash_led_probe()
746 rc = qcom_flash_register_led_device(dev, child, led); in qcom_flash_led_probe()
750 flash_data->leds_count++; in qcom_flash_led_probe()
757 while (flash_data->v4l2_flash[flash_data->leds_count] && flash_data->leds_count) in qcom_flash_led_probe()
758 v4l2_flash_release(flash_data->v4l2_flash[flash_data->leds_count--]); in qcom_flash_led_probe()
766 while (flash_data->v4l2_flash[flash_data->leds_count] && flash_data->leds_count) in qcom_flash_led_remove()
767 v4l2_flash_release(flash_data->v4l2_flash[flash_data->leds_count--]); in qcom_flash_led_remove()
769 mutex_destroy(&flash_data->lock); in qcom_flash_led_remove()
774 { .compatible = "qcom,spmi-flash-led" },
781 .name = "leds-qcom-flash",
790 MODULE_DESCRIPTION("QCOM Flash LED driver");