May 15, 2013

OV5642 auto-focus on i.MX6





Source is here.

‘Nuff said?

Probably not.

More detail is probably useful.

A word of thanks

Before the details, I’d first like to thank the folks at Ray Business Technologies for helping put this together. They have a strong team of embedded developers with competency in embedded Linux, i.MX6, and gstreamer and we wouldn’t have gotten this done without their help.

Okay, more details

For those of you using our Omnivision OV5642 parallel camera module, you may have noticed that it doesn’t do auto focus, but defaults to a long-range (infinite) focal length.

This doesn’t mean that the hardware doesn’t support it, only that the drivers supplied by Freescale were missing some key pieces.

The camera modules themselves contain a voice coil motor controlling the lense assembly and the OV5642 contains a micro-controller that knows how to drive it.

What was missing were:

How to test

We’re not going to delve too far into the details about this, but there’s a simple gstreamer utility that you can use to test it in this package:

It should compile and run on Yocto, Timesys or LTIB distributions using a command-line something like this:
~/gstcamera$ export RFS=$HOME/ltib-01.01/rootfs
~/gstcamera$ arm-fsl-linux-gnueabi-gcc 
               -o gstcamera 
               -lsupc++ -lpthread -lgstreamer-0.10 
               -lgobject-2.0 -lgthread-2.0 
               -lgmodule-2.0 -lglib-2.0 -lxml2 -lz
This app is a very simple wrapper around some gstreamer bits and is a total hack. It uses the gst_parse_launch() routine to parse the command line gst-launch and totally hijacks the camera file descriptor to perform the ioctl calls for auto-focus and manual focus.

Typical usage is:
~/$ gstcamera mfw_v4lsrc ! mfw_v4lsink
 or  to exit
The commands supported include:
  • AF – triggers auto-focus
  • SPn – Set focus to Position n where n in the range [0..255]. 0 is furthest focal length. 255 is nearest.


Note that this patch only applies to the OV5642 module and only in the non-Android kernel branch at the moment. Support for the OV5640 MIPI module and Android support will be added soon.

Stay tuned for more…

Comments 35

  1. Sébastien Taylor

    Seeing errors when using the latest 3.0.35_1.1.1 kernel from Boundary with this gstcamera software. A diff of the link to your github confirms I’m using the same ov5642.c driver.

    ioctl V4L2_CID_AUTO_FOCUS_START fail: Invalid argument

  2. Andrew

    you need to rebuild the ov5642_camera and mxc_v4l2_camera kernel modules and install them on the target. I was getting the same error and it went away after I rebuilt.

  3. Andrew

    I have a FIXED focus ov5642 camera module (from here: The pinout is similar to the auto-focus ov5642 I bought from your site. Only difference is AF_VCC and AF_GND in auto-focus camera is now D1 and D0 in fixed-focus camera. I assume D1 and D0 are therefore unused. however, I get the following error when i run ‘gst-launch mfw_v4lsrc ! mfw_v4lsink’

    mx6_csi0_powerdown: powerdown=0, power_gp=0x6
    mx6_csi0_powerdown: powerdown=1, power_gp=0x6
    ERROR: v4l2 capture: slave not found!
    MFW_GST_V4LSINK_PLUGIN 3.0.5 build on Apr 13 2013 04:18:14.
    Setting pipeline to PAUSED ...
    mx6_csi0_powerdown: powerdown=0, power_gp=0x6
    mx6_csi0_powerdown: powerdown=0, power_gp=0x6
    Pipeline is live and does not need PREROLL ...
    Setting pipeline to PLAYING ...
    New clock: GstSystemClock
    ERROR: v4l2 capture: mxc_v4l_dqueue timeout enc_counter 0
    ERROR: from element /GstPipeline:pipeline0/MFWGstV4LSrc:mfwgstv4lsrc0: Internal data flow error.
    Additional debug info:
    /home/foo/yocto/build/tmp/work/armv7a-vfp-neon-poky-linux-gnueabi/gstreamer/0.10.36-r2/gstreamer-0.10.36/libs/gst/base/gstbasesrc.c(2625): gst_base_src_loop (): /GstPipeline:p
    streaming task paused, reason error (-5)
    Execution ended after 10004917680 ns.
    Setting pipeline to PAUSED ...
    Setting pipeline to READY ...
    mx6_csi0_powerdown: powerdown=1, power_gp=0x6
    Setting pipeline to NULL ...
    Total rendered:0
    Freeing pipeline ...
    [--->FINALIZE v4l_sink

    so it seems like not a single camera frame buffer is not being dequeued. I think the same ov5642 firmware and software should run for the fixed-focus mode without errors. am i overlooking something? (auto-focus camera works with same software and command as a reference)

    1. Post

      I suspect a connection issue here. The firmware is a bit specific to the Truly auto-focus camera (tuned for the particular VCM), but the firmware isn’t invoked until asked.

      Did you try this camera before the patch?

  4. lisarden


    I want to enable this camera+Lite in linphone application. Will it work? Is there any guide how to test this camera in ubuntu?

    1. Post

      This Ubuntu image has Freescale’s gstreamer plugins, so it will allow testing as described above.

      It’s unlikely that Linphone will just work because the ioctl’s supported and required by the camera driver are not completely standard. In other words, some hacking is likely necessary.

  5. scott

    The gstcamera compile command string you show above will not compile on the native nitrogen-6x platform, at least for the Ubuntu 4.0.0 kernel file system due to glibconfig.h path issues. One can use pkg-config to automagically solve the path issues:

    gcc -L/usr/lib/ -I/usr/include/gstreamer-0.10/ 
    	`pkg-config --cflags glib-2.0` -I/usr/lib/glib-2.0/include/ 
    	-I/usr/include/libxml2 -I/usr/include/libxml2/libxml/ 
    	 -o gstcamera 
    	gstcamera.cpp -lsupc++ -lpthread -lgstreamer-0.10 
    	-lgobject-2.0 -lgthread-2.0 -lgmodule-2.0 
    	`pkg-config --libs glib-2.0`
    	-lglib-2.0 -lglib-2.0 

    By the way, will you have a compiled kernel with the necessary changes to ov5642.c in an updated kernel anytime soon? (Save me the time in re-compiling the kernel.) Also, any time frame for auto focus or manual focus adjustment on the MIPI camera?


    1. Post

      Hi Scott,

      The package imx6_kernel_overlay.tar.gz contains a kernel and modules with a very recent kernel and auto-focus support for the OV5642.

      If you’d have asked me two weeks ago about the auto-focus code and OV5640, I probably would have lied and said it would be available by now. I’ll try to scare up some cycles to get this out in the next week.

      1. scott


        This worked like a champ for simple video streaming – example:

        gstcamera mfw_src capture-mode=4 ! mfw_sink disp-width=640 disp-height=480

        enter either AF (or use SP0 through SP255 for setting manual focus to a setpoint distance)

        Two questions I have at this point relative to your simple hack:

        1. How do you incorporate focusing into a frame capture stream; for example I normally use the following type of gst-launch file capture to create jpeg’s of captured video – I don’t see how I could easily manipulate or modify gstcamera to handle this type of capture stream.

        gst-launch mfw_v4lsrc num-buffers=1 ! jpegenc ! filesink location=/home/linaro/Desktop/test.jpg

        2. Would it be easy to set the ov5642 camera to start up in AF mode? I looked at the code but I am not familiar with the driver/capture code enough to easily hack this together.



        1. Post

          Hi Scott,

          The short answer is that this needs to be done inside of an application of some sort that controls the pipeline such that the following can occur in response to a capture request:

          • — auto-focus is triggered
          • — jpegenc and filesink are sliced into the pipeline (possibly through a tee), and
          • — pipeline is restored

          Unfortunately, I don’t have such a sample handy.

          It should also be easy enough to modify gstcamera.cpp to trigger an auto-focus event on startup (or shortly thereafter), but I’m not sure that satisfies your need.

    2. Post
      1. scott


        It doesn’t appear that the freescale gstreamer source and sink exist in the 4.1.0 kernel? I can modprobe the mxc_v4l2_capture but when I invoke gst-launch with mfw_v4lsrc (or sink), I get the ‘no element’ message.


        1. Post

          Hi Scott,

          The mfw_v4lsrc and mfw_v4lsink components are a part of the userspace, not the kernel itself.

          If you check out the comments in this thread, you’ll hear about some of the difficulties of getting the gstreamer plugins up on arbitrary userspaces.

      2. vahid

        hi ericn.
        i updated ov5640_mipi.c on kernel android4.4.2 but in compile of kernel there are multi-error from IOCTL_SEND_COMMAND and IOCTL_S_CTRL . how can i solve them?

  6. Post

    Hi zmb,

    Auto-focus is now in place for OV5640-MIPI in both the Android and non-Android kernel trees.

    Code to trigger auto-focus is also present in the Android 4.2.2 code base, though we haven’t generated a new image.

    1. Post

      Hi Michael,

      I can’t say, since we don’t have one, but the folks at e-Con-Systems are pros, and can guide you through their use.

      My understanding (from reading their product announcements) is that they’re doing a lot more than simply auto-focus, and enabling lots of features of the cameras they supply.

    1. Post
  7. pere czes

    Hi, have a 5642 chip with the thin cable from a tablet that is broken. Is it possible to find some cheap usb or serial interface to use it as trivial camera?

  8. Andrea

    I’m using a linux-boundary kernel (3.0.35_4.1.0) with manual step focus postition implemented (ioctl_send_command 105) for ov5640_mipi camera. I’m trying to setup focus to user postition but nothing seems to change. Could someone give me some hit about that? Where can I find details about focus registers CMD_PARA0-CMD_PARA4?

    thank you

  9. Laci Tele

    Hi Andrea,

    Do you mean , if you build the above example in the post, that is not working ?
    What happens when you give the :
    commands? (0-furthest, 255-nearest focus) Is there any error message ?
    Is the AF (autofocus) command working anyway ?

  10. Andrea

    Hi Laci,
    thank you for the reply. If I use given example I can see a valid response only with AF even I obtain always successful messages

    ioctl V4L2_CID_AUTO_FOCUS_START sucessful
    ioctl STEP sucessful: 0
    ioctl STEP sucessful: 0

    When I type AF I can see focus adjustment on display but when I type SP nothing changes and there aren’t error messages. I’m using that kernel

    Have I to try with another kernel? Do I need some ov5640_mipi.c patches to work?
    Are you able to obtain successful result with ov5640_mipi camera?

    thank you,

    1. Andrea

      Where can I find a document that explain properly how to setup registers to step focus at a specified position? If I search over internet I found just a “OV5642 Embedded Firmware User Guide – VCM AF Moudle”, where can I found a similar one for OV5640?

      Into kernel module ov5640_mipi.c I found that setup for manual step to loca_val. Are it correct?

      ov5640_write_reg(CMD_PARA3, 0);
      ov5640_write_reg(CMD_PARA4, loca_val);
      retval1 = ov5640_write_reg(CMD_ACK, 0x01);
      retval2 = ov5640_write_reg(CMD_MAIN, 0x1a);

      I hope someone colud help me, thank you again,

    2. Laci Tele

      Hi Andrea,

      Just a little bit more patience please. You’re right stepping is not working with OV5640-mipi.
      Eric is working on it, hopefully he can fix it.
      Its not documented, at least I couldn’t find any valuable info about CMD_PARA0-CMD_PARA4 and the appropriate CMD_ACK and CMD_MAIN values.

    3. Laci Tele

      Hi Andrea,

      Eric has fixed it, I told you he would. You can select the kernel 3.0.35-4.1.0 or 3.10.17-1.0.2_ga, as you like it. Both working.
      Its still not documented, but Eric applied a dirty trick, he mapped the 0..255 values, and he controls the focus stepping motor directly, if I’m not mistaken. Its working, I tested it.

  11. Michael Simmonds

    I was looking at a recent version of ov5640_mipi.c and noticed that you are downloading almost 4K of code to the camera from a structure called “ov5640_af_firmware”.

    I also noticed that triggering auto-focus involves writing to registers in the camera that are “not documented”. Pretty exotic stuff.

    I am curious…
    Is this just a firmware update to the camera, or is this new code that provides auto-focus capability that did not exist before? If it is new code, who wrote it? OmniVision?

    1. Laci Tele

      Hi Michael,
      That firmware is written by OmniVision and its partially documented, at least the autofocus part is. The focus stepping was a private idea from Eric, because that part is not documented, indeed. But Eric has ideas. He solves things.

  12. himanshu

    Hi ericn

    Do we have the patch for Autofocus in ov5640_mipi_camera as well ?


  13. himanshu

    Hi eric

    Seems like it is as i can see autofocus triggering in one of the Custom Video Capture App
    But AF doesn’t gets triggered in Native Camera Application.
    Do you have any information in this

    One more thing which i observed is when i launch native camera app at the beginning the pictures
    are blur ,but if i launch any sample video and then launch camera i can see images clearly


  14. himanshu

    Hi Eric,

    Is there any issue related to Autofocus triggering in jellybean image
    As i found out with native camera app autofocus doesn’t gets triggered
    But in some of the application autofocus gets triggered properly


Leave a Reply

Your email address will not be published. Required fields are marked *