• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * atomisp_device.cpp - atomisp device
3  *
4  *  Copyright (c) 2014-2015 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Wind Yuan <feng.yuan@intel.com>
19  */
20 
21 #include "atomisp_device.h"
22 #include "v4l2_buffer_proxy.h"
23 #include <linux/v4l2-subdev.h>
24 
25 namespace XCam {
26 
AtomispDevice(const char * name)27 AtomispDevice::AtomispDevice (const char *name)
28     : V4l2Device (name)
29 {
30 }
31 
~AtomispDevice()32 AtomispDevice::~AtomispDevice ()
33 {
34 }
35 
36 XCamReturn
pre_set_format(struct v4l2_format & format)37 AtomispDevice::pre_set_format (struct v4l2_format &format)
38 {
39     uint32_t fps_n = 0, fps_d = 0;
40     struct v4l2_subdev_format subdev_fmt;
41 
42     // set framerate by subdev
43     this->get_framerate (fps_n, fps_d);
44     if (fps_n != 0 && fps_d != 0) {
45         struct v4l2_subdev_frame_interval frame_intvl;
46         xcam_mem_clear (frame_intvl);
47         if (io_control (VIDIOC_SUBDEV_G_FRAME_INTERVAL, &frame_intvl) < 0) {
48             XCAM_LOG_WARNING ("atomisp device(%s) get framerate failed ", XCAM_STR (get_device_name()));
49         } else {
50             frame_intvl.interval.denominator = fps_n;
51             frame_intvl.interval.numerator = fps_d;
52             if (io_control (VIDIOC_SUBDEV_S_FRAME_INTERVAL, &frame_intvl) < 0) {
53                 XCAM_LOG_WARNING ("atomisp device(%s) set framerate failed", XCAM_STR (get_device_name()));
54             }
55         }
56     }
57 
58     // negotiate and set sensor output format by subdev
59     xcam_mem_clear (subdev_fmt);
60     subdev_fmt.pad = 0;
61     subdev_fmt.which = V4L2_SUBDEV_FORMAT_TRY;
62     subdev_fmt.format.width = format.fmt.pix.width;
63     subdev_fmt.format.height = format.fmt.pix.height;
64     subdev_fmt.format.field = V4L2_FIELD_NONE;
65     if (format.fmt.pix.pixelformat == V4L2_PIX_FMT_SGRBG12) {
66         subdev_fmt.format.code = V4L2_MBUS_FMT_SRGGB12_1X12;
67     } else {
68         subdev_fmt.format.code = V4L2_MBUS_FMT_SRGGB10_1X10;
69     }
70 
71     if (io_control(VIDIOC_SUBDEV_S_FMT, &subdev_fmt) < 0) {
72         XCAM_LOG_ERROR ("atomisp device(%s) try subdev format failed", XCAM_STR (get_device_name()));
73         return XCAM_RETURN_ERROR_IOCTL;
74     }
75     XCAM_LOG_INFO ("target subdev format (%dx%d, code %d)",
76                    subdev_fmt.format.width,
77                    subdev_fmt.format.height,
78                    subdev_fmt.format.code);
79 
80     subdev_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
81     if (io_control (VIDIOC_SUBDEV_G_FMT, &subdev_fmt) < 0) {
82         XCAM_LOG_ERROR ("atomisp device(%s) get subdev format failed", XCAM_STR (get_device_name()));
83     }
84     XCAM_LOG_INFO ("negotiated subdev format (%dx%d, code %d)",
85                    subdev_fmt.format.width,
86                    subdev_fmt.format.height,
87                    subdev_fmt.format.code);
88 
89     if (io_control(VIDIOC_SUBDEV_S_FMT, &subdev_fmt) < 0) {
90         XCAM_LOG_ERROR ("atomisp device(%s) set subdev format failed", XCAM_STR (get_device_name()));
91         return XCAM_RETURN_ERROR_IOCTL;
92     }
93 
94     return XCAM_RETURN_NO_ERROR;
95 }
96 
97 XCamReturn
allocate_buffer(SmartPtr<V4l2Buffer> & buf,const struct v4l2_format & format,const uint32_t index)98 AtomispDevice::allocate_buffer (
99     SmartPtr<V4l2Buffer> &buf,
100     const struct v4l2_format &format,
101     const uint32_t index)
102 {
103 #if HAVE_LIBDRM
104     if (!_drm_disp.ptr()) {
105         _drm_disp = DrmDisplay::instance ();
106     }
107 
108     if (get_mem_type () == V4L2_MEMORY_DMABUF && _drm_disp.ptr () != NULL) {
109         buf = _drm_disp->create_drm_buf (format, index, get_capture_buf_type ());
110         if (!buf.ptr()) {
111             XCAM_LOG_WARNING ("atomisp device(%s) allocate buffer failed", XCAM_STR (get_device_name()));
112             return XCAM_RETURN_ERROR_MEM;
113         }
114         return XCAM_RETURN_NO_ERROR;
115     }
116 #endif
117 
118     return V4l2Device::allocate_buffer (buf, format, index);
119 }
120 
121 };
122