Camera | 9.如何让camera支持闪光灯?-基于rk3568

一、闪光灯基本原理

Camera | 9.如何让camera支持闪光灯?-基于rk3568_第1张图片

工作模式

Camera flash led分flash和torch两种模式。

  • flash:

拍照时上光灯瞬间亮一下,电流比较大,目前是1000mA,最大电流不能超过led最大承受能力

  • torch:

只用于录video或者拿led当手电筒的情况,电流不能太大,

flash开启需要先从torch过渡,这样电流可以慢慢增大,减少冲击。

二、驱动移植

1. 硬件电路

Camera | 9.如何让camera支持闪光灯?-基于rk3568_第2张图片

sgm3141总体来说比较简单,只有两个引脚用于控制:FLASH_EN、FLASH/TORCH。

  • FLASH_EN

闪光灯使能引脚。

  • FLASH/TORCH

此处led flash和TORCH功能共用了同一个gpio。

注意:
有的摄像头是通过pwm来控制闪光灯,具体问题具体分析,不要教条

2. 设备树

瑞芯微sdk中没有专门针对sgm3141的驱动,网上搜了一大圈也没有搜到,很多闪光灯都是基于I2C接口,通过I2C来设置上光灯的工作模式,好在sdk中有类似的芯片rgb13h驱动,该驱动只有一个引脚用于闪光灯,和我们的功能很类似。

参考驱动文件:leds-rgb13h.c

sgm3141设备树节点
       flash_sgm3141:flash-sgm3141 {
               status = "okay";
               compatible = "sgmicro,sgm3141";
               label = "gpio-flash";
               pinctrl-names = "default";
               pinctrl-0 = <&flash_led_gpios>;
               
               led-max-microamp = <20000>;
               flash-max-microamp = <20000>;
               flash-max-timeout-us = <1000000>;
               strobe-gpios = <&gpio3 RK_PC7 GPIO_ACTIVE_HIGH>;
               enable-gpios = <&gpio3 RK_PD0 GPIO_ACTIVE_HIGH>;
               rockchip,camera-module-index = <0>;
               rockchip,camera-module-facing = "back"; 
       };

        ov13850: ov13850@10 {
        	……
            flash-leds = <&flash_sgm3141>;
        	……
        };
pinctrl设置:
 &pinctrl {
       ……
       flash-led {
               flash_led_gpios: flash-led {
                       rockchip,pins =
                       /* flash led enable*/
                       <3 RK_PD0 RK_FUNC_GPIO &pcfg_pull_none>,
                       <3 RK_PC6 RK_FUNC_GPIO &pcfg_pull_none>;
               };
       };
       ……
 };
@drivers/leds/Makefile
obj-y                  += sgm3141.o

驱动模块直接写死,不罗嗦

三、xml

要app支持闪光灯,还需要修改xml文件,打开该功能

@hardware/rockchip/camera/etc/camera/camera3_profiles.xml
@hardware/rockchip/camera/etc/camera/camera3_profiles_rk356x.xml


这样,打开app就可以看到上光灯的按钮。

四、拓扑结构

拓扑结构中entity

rk3568_r:/ # media-ctl -d /dev/media0 -p
……
- entity 70: m01_f_ov5648 4-0036 (1 pad, 1 link)
             type V4L2 subdev subtype Sensor
             device node name /dev/v4l-subdev3
        pad0: Source
                [fmt:SBGGR10/2592x1944]
                -> "rockchip-csi2-dphy0":0 []

- entity 74: m00_b_ov13850 4-0010 (1 pad, 1 link)
             type V4L2 subdev subtype Sensor
             device node name /dev/v4l-subdev4
        pad0: Source
                [fmt:SBGGR10/4224x3136]
                -> "rockchip-csi2-dphy0":0 []

- entity 78: m00_b_gpio-flash (0 pad, 0 link)
             type V4L2 subdev subtype Flash
             device node name /dev/v4l-subdev5
  • 摄像头:

m00_b_ov13850、m01_f_ov5648

  • 闪光灯:

m00_b_gpio-flash,v4l2子设备/dev/v4l-subdev5

第4个字母b表示后置摄像头。

五、调试

驱动会通过sysfs创建文件节点

rk3568_r:/sys/class/leds/gpio-flash # ls
brightness  flash_strobe   max_brightness     power      trigger
device      flash_timeout  max_flash_timeout  subsystem  uevent
  • 亮灭灯
亮灯
rk3568_r:/sys/class/leds/gpio-flash # echo 1 > brightness
echo 1 > brightness
灭灯
1|rk3568_r:/sys/class/leds/gpio-flash # echo 0 > brightness
echo 0 > brightness

操作对应函数调用关系

[  374.302386] Call trace:                                                                     
[  374.302428] dump_backtrace+0x0/0x178                                                        
[  374.302471] show_stack+0x14/0x20                                                            
[  374.302518] dump_stack+0x94/0xb4                                                            
[  374.302560] sgm3141_led_brightness_set+0x18/0x40                                            
[  374.302592] led_set_brightness_nopm+0x14/0x48                                               
[  374.302624] led_set_brightness+0x50/0x80                                                    
[  374.302662] brightness_store+0x74/0xc8                                                      
[  374.302691] dev_attr_store+0x18/0x28                                                        
[  374.302728] sysfs_kf_write+0x48/0x58                                                        
[  374.302759] kernfs_fop_write+0xf4/0x220                                                     
[  374.302793] __vfs_write+0x34/0x158                                                          
[  374.302810] vfs_write+0xb0/0x1d0                                                            
[  374.302828] ksys_write+0x64/0xe0                                                            
[  374.302860] __arm64_sys_write+0x14/0x20                                                     
[  374.302904] el0_svc_common.constprop.0+0x64/0x178                                           
[  374.302947] el0_svc_handler+0x28/0x78                                                       
[  374.302984] el0_svc+0x8/0xc                                                                 
[  425.607850] healthd: battery l=50 v=3 t=2.6 h=2 st=3 fc=100 chg=au                          
[  437.663757] CPU: 3 PID: 1727 Comm: sh Not tainted 4.19.232 #326                             
[  437.663869] Hardware name: Rockchip RK3568 EVB1 DDR4 V10 Board (DT)  
  • 闪灯
rk3568_r:/sys/class/leds/gpio-flash # echo 1 > flash_strobe
echo 1 > flash_strobe

操作对应函数调用关系

[  492.026239] Call trace:                                                                     
[  492.026291] dump_backtrace+0x0/0x178                                                        
[  492.026320] show_stack+0x14/0x20                                                            
[  492.026354] dump_stack+0x94/0xb4                                                            
[  492.026391] sgm3141_led_flash_strobe_set+0x24/0x78                                          
[  492.026453] flash_strobe_store+0x88/0xd8                                                    
[  492.026517] dev_attr_store+0x18/0x28                                                        
[  492.026571] sysfs_kf_write+0x48/0x58                                                        
[  492.026620] kernfs_fop_write+0xf4/0x220                                                     
[  492.026683] __vfs_write+0x34/0x158                                                          
[  492.026733] vfs_write+0xb0/0x1d0                                                            
[  492.026784] ksys_write+0x64/0xe0                                                            
[  492.026833] __arm64_sys_write+0x14/0x20                                                     
[  492.026867] el0_svc_common.constprop.0+0x64/0x178                                           
[  492.026912] el0_svc_handler+0x28/0x78                                                       
[  492.026966] el0_svc+0x8/0xc                                                                 
[  494.218112] CPU: 2 PID: 1727 Comm: sh Not tainted 4.19.232 #326                             
[  494.218218] Hardware name: Rockchip RK3568 EVB1 DDR4 V10 Board (DT)  

为方便查看驱动回调函数调用顺序,可以在下面3个回调函数入口增加dump_stack(),
sgm3141_led_brightness_set()
sgm3141_led_flash_strobe_set()
sgm3141_set_ctrl()

六、app 测试驱动 log分析

1. 上电加载驱动log

rk3568_r:/ # dmesg | grep sgm3141
dmesg | grep sgm3141
[    0.853883] sgm3141-flash flash-sgm3141: driver version: 00.01.01
[    0.853956] sgm3141-flash flash-sgm3141: Unable to get pwm device

2. 打开摄像头

打开摄像头功能,app调用ioctl命令如下:

  1. V4L2_CID_FLASH_LED_MODE :app 设置led mode为 V4L2_FLASH_LED_MODE_FLASH(1)
  2. V4L2_CID_FLASH_TIMEOUT :设置超时时间
  3. V4L2_CID_FLASH_STROBE_STOP :停止闪光
[   36.213220] sgm3141 V4L2_CID_FLASH_LED_MODE 1
[   36.213250] sgm3141_set_ctrl(),376
[   36.213256] sgm3141_set_output(),78 0
[   36.213265] sgm3141_led_flash_strobe_set(),166 state=1
[   36.213281] sgm3141_set_output(),78 1

[   36.213298] sgm3141 V4L2_CID_FLASH_TIMEOUT
[   36.213300] sgm3141_set_ctrl(),412
[   36.213308] sgm3141_led_flash_timeout_set(),195 timeout=500000


[   36.213318] sgm3141 V4L2_CID_FLASH_STROBE_STOP 1
[   36.213323] sgm3141_set_ctrl(),406
[   36.213327] sgm3141_led_flash_strobe_set(),166 state=0
[   36.213332] sgm3141_set_output(),78 0
[   36.213461] sgm3141_timeout_work(),117

3. 拍照

拍照功能,app调用ioctl命令如下:

  1. V4L2_CID_FLASH_LED_MODE :设置led mod为 V4L2_FLASH_LED_MODE_TORCH(2),并点灯
  2. V4L2_CID_FLASH_LED_MODE:到达指定超时时间(2.7秒),设置led mod为 V4L2_FLASH_LED_MODE_NONE 0
  3. V4L2_CID_FLASH_LED_MODE:在此设置led mod为V4L2_FLASH_LED_MODE_FLASH(1)
  4. V4L2_CID_FLASH_STROBE_STOP:停止闪光

[   90.246203] sgm3141 V4L2_CID_FLASH_LED_MODE 2
[   90.246251] sgm3141_set_ctrl(),376
[   90.246262] sgm3141_set_output(),78 0
[   90.246277] sgm3141_set_output(),78 1

[   92.902746] sgm3141 V4L2_CID_FLASH_LED_MODE 0
[   92.902775] sgm3141_set_ctrl(),376
[   92.902781] sgm3141_set_output(),78 0

[   93.034903] sgm3141 V4L2_CID_FLASH_LED_MODE 1
[   93.034929] sgm3141_set_ctrl(),376
[   93.034934] sgm3141_set_output(),78 0
[   93.034943] sgm3141_led_flash_strobe_set(),166 state=1
[   93.034959] sgm3141_set_output(),78 1

[   93.034977] sgm3141 V4L2_CID_FLASH_STROBE_STOP 1
[   93.034988] sgm3141_set_ctrl(),406
[   93.034993] sgm3141_led_flash_strobe_set(),166 state=0
[   93.035002] sgm3141_set_output(),78 0
[   93.035058] sgm3141_timeout_work(),117

从log可以看出,app拍照的时候,闪光灯操作是通过v4l2下发的命令,

而我们直接操作宏dev_attr_store创建的文件节点则是通过在sysfs文件节点(brightness flash_strobe)直接调用到操作函数。

4. 退出摄像头

[  317.882459] sgm3141 V4L2_CID_FLASH_LED_MODE 0
[  317.882522] sgm3141_set_ctrl(),376
[  317.882532] sgm3141_set_output(),78 0

七、驱动获取

一口君修改过的驱动已经上传到gitee,获取地址:

https://gitee.com/yikoulinux/sgm3141

交流嵌入式底层开发技术,后台留言,加一口君好友。

你可能感兴趣的:(瑞芯微,原创,Linux驱动,linux,驱动)