1 #include "CamHwIsp20.h"
2 #include "rk_aiq_comm.h"
3 #include "fake_v4l2_device.h"
4 #include "rkcif-config.h"
5
6 namespace RkCam {
7
CifSclStream()8 CifSclStream::CifSclStream()
9 : _working_mode(0)
10 , _bpp(0)
11 , index(0)
12 , _width(0)
13 , _height(0)
14 , _sns_v4l_pix_fmt(0)
15 , _ratio(0)
16 , _first_start(true)
17 {}
18
~CifSclStream()19 CifSclStream::~CifSclStream() {}
20
21 bool
setPollCallback(PollCallback * callback)22 CifSclStream::setPollCallback (PollCallback *callback)
23 {
24 for (int i = 0; i < index; i++) {
25 _stream[i]->setPollCallback(callback);
26 }
27 return true;
28 }
29
30 XCamReturn
set_format(const struct v4l2_subdev_format & sns_sd_fmt,uint32_t sns_v4l_pix_fmt,int bpp)31 CifSclStream::set_format(const struct v4l2_subdev_format& sns_sd_fmt,
32 uint32_t sns_v4l_pix_fmt, int bpp)
33 {
34 XCamReturn ret = XCAM_RETURN_NO_ERROR;
35
36 _width = sns_sd_fmt.format.width;
37 _height = sns_sd_fmt.format.height;
38 _bpp = bpp;
39
40 switch (sns_v4l_pix_fmt)
41 {
42 case V4L2_PIX_FMT_SBGGR8:
43 case V4L2_PIX_FMT_SBGGR10:
44 case V4L2_PIX_FMT_SBGGR12:
45 case V4L2_PIX_FMT_SBGGR14:
46 case V4L2_PIX_FMT_SBGGR16:
47 _sns_v4l_pix_fmt = V4L2_PIX_FMT_SBGGR16;
48 break;
49 case V4L2_PIX_FMT_SGBRG8:
50 case V4L2_PIX_FMT_SGBRG10:
51 case V4L2_PIX_FMT_SGBRG12:
52 case V4L2_PIX_FMT_SGBRG14:
53 case V4L2_PIX_FMT_SGBRG16:
54 _sns_v4l_pix_fmt = V4L2_PIX_FMT_SGBRG16;
55 break;
56 case V4L2_PIX_FMT_SGRBG8:
57 case V4L2_PIX_FMT_SGRBG10:
58 case V4L2_PIX_FMT_SGRBG12:
59 case V4L2_PIX_FMT_SGRBG14:
60 case V4L2_PIX_FMT_SGRBG16:
61 _sns_v4l_pix_fmt = V4L2_PIX_FMT_SGRBG16;
62 break;
63 case V4L2_PIX_FMT_SRGGB8:
64 case V4L2_PIX_FMT_SRGGB10:
65 case V4L2_PIX_FMT_SRGGB12:
66 case V4L2_PIX_FMT_SRGGB14:
67 case V4L2_PIX_FMT_SRGGB16:
68 _sns_v4l_pix_fmt = V4L2_PIX_FMT_SRGGB16;
69 break;
70 default:
71 LOGE_CAMHW_SUBM(ISP20HW_SUBM, "unknown format");
72 return XCAM_RETURN_ERROR_PARAM;
73 }
74
75 return ret;
76 }
77
78 XCamReturn
set_format(const struct v4l2_subdev_selection & sns_sd_sel,uint32_t sns_v4l_pix_fmt,int bpp)79 CifSclStream::set_format(const struct v4l2_subdev_selection& sns_sd_sel,
80 uint32_t sns_v4l_pix_fmt, int bpp)
81 {
82 XCamReturn ret = XCAM_RETURN_NO_ERROR;
83
84 _width = sns_sd_sel.r.width;
85 _height = sns_sd_sel.r.height;
86 _bpp = bpp;
87
88 switch (sns_v4l_pix_fmt)
89 {
90 case V4L2_PIX_FMT_SBGGR8:
91 case V4L2_PIX_FMT_SBGGR10:
92 case V4L2_PIX_FMT_SBGGR12:
93 case V4L2_PIX_FMT_SBGGR14:
94 case V4L2_PIX_FMT_SBGGR16:
95 _sns_v4l_pix_fmt = V4L2_PIX_FMT_SBGGR16;
96 break;
97 case V4L2_PIX_FMT_SGBRG8:
98 case V4L2_PIX_FMT_SGBRG10:
99 case V4L2_PIX_FMT_SGBRG12:
100 case V4L2_PIX_FMT_SGBRG14:
101 case V4L2_PIX_FMT_SGBRG16:
102 _sns_v4l_pix_fmt = V4L2_PIX_FMT_SGBRG16;
103 break;
104 case V4L2_PIX_FMT_SGRBG8:
105 case V4L2_PIX_FMT_SGRBG10:
106 case V4L2_PIX_FMT_SGRBG12:
107 case V4L2_PIX_FMT_SGRBG14:
108 case V4L2_PIX_FMT_SGRBG16:
109 _sns_v4l_pix_fmt = V4L2_PIX_FMT_SGRBG16;
110 break;
111 case V4L2_PIX_FMT_SRGGB8:
112 case V4L2_PIX_FMT_SRGGB10:
113 case V4L2_PIX_FMT_SRGGB12:
114 case V4L2_PIX_FMT_SRGGB14:
115 case V4L2_PIX_FMT_SRGGB16:
116 _sns_v4l_pix_fmt = V4L2_PIX_FMT_SRGGB16;
117 break;
118 default:
119 LOGE_CAMHW_SUBM(ISP20HW_SUBM, "unknown format");
120 return XCAM_RETURN_ERROR_PARAM;
121 }
122 return ret;
123 }
124
125 void
set_working_mode(int mode)126 CifSclStream::set_working_mode(int mode)
127 {
128 _working_mode = mode;
129 }
130
start()131 XCamReturn CifSclStream::start()
132 {
133 if (_init && !_active) {
134 for (int i = 0; i < index; i++) {
135 if (_stream[i].ptr())
136 _stream[i]->start();
137 }
138 _active = true;
139 }
140 return XCAM_RETURN_NO_ERROR;
141 }
142
stop()143 XCamReturn CifSclStream::stop()
144 {
145 for (int i = 0; i < index; i++) {
146 if (_stream[i].ptr())
147 _stream[i]->stop();
148 }
149 _active = false;
150 return XCAM_RETURN_NO_ERROR;
151 }
152
153 XCamReturn
restart(const rk_sensor_full_info_t * s_info,int ratio,PollCallback * callback,int mode)154 CifSclStream::restart(const rk_sensor_full_info_t *s_info, int ratio, PollCallback *callback, int mode)
155 {
156 XCamReturn ret = XCAM_RETURN_NO_ERROR;
157
158 if (mode) {
159 if (!_init)
160 ret = init(s_info, callback);
161
162 if (ratio != _ratio) {
163 if (_active) {
164 stop();
165 }
166 ret = set_ratio_fmt(ratio);
167 }
168
169 if (!_active && _init) {
170 ret = prepare();
171 ret = start();
172 }
173 } else {
174 if (_active)
175 ret = stop();
176 }
177 return ret;
178 }
179
180 XCamReturn
init(const rk_sensor_full_info_t * s_info,PollCallback * callback)181 CifSclStream::init(const rk_sensor_full_info_t *s_info, PollCallback *callback)
182 {
183 XCamReturn ret = XCAM_RETURN_NO_ERROR;
184
185 if (!s_info->cif_info) {
186 LOGD_CAMHW("no link to vicap\n");
187 return XCAM_RETURN_NO_ERROR;
188 }
189
190 switch (_working_mode)
191 {
192 case 0x0:
193 index = 1;
194 break;
195 case 0x11:
196 case 0x12:
197 index = 2;
198 break;
199 case 0x21:
200 case 0x22:
201 index = 3;
202 break;
203 default:
204 index = 0;
205 break;
206 }
207
208
209 if (index >= 1) {
210 if (strlen(s_info->cif_info->mipi_scl0)) {
211 _dev[0] = new V4l2Device(s_info->cif_info->mipi_scl0);
212 }
213 if (_dev[0].ptr())
214 _dev[0]->open();
215 }
216 if (index >= 2) {
217 if (strlen(s_info->cif_info->mipi_scl1)) {
218 _dev[1] = new V4l2Device(s_info->cif_info->mipi_scl1);
219 }
220 if (_dev[1].ptr())
221 _dev[1]->open();
222 }
223 if (index == 3) {
224 if (strlen(s_info->cif_info->mipi_scl2)) {
225 _dev[2] = new V4l2Device(s_info->cif_info->mipi_scl2);
226 }
227 if (_dev[2].ptr())
228 _dev[2]->open();
229 }
230 for (int i = 0; i < index; i++) {
231 if (_dev[i].ptr()) {
232 _stream[i] = new RKRawStream(_dev[i], i, VICAP_POLL_SCL);
233 _stream[i]->setPollCallback(callback);
234 SmartPtr<RKRawStream> stream = _stream[i].dynamic_cast_ptr<RKRawStream>();
235 stream->set_reserved_data(_bpp);
236 }
237 }
238
239 _init = true;
240
241 return ret;
242 }
243
244 XCamReturn
set_ratio_fmt(int ratio)245 CifSclStream::set_ratio_fmt(int ratio)
246 {
247 XCamReturn ret = XCAM_RETURN_NO_ERROR;
248
249 if (ratio > 0 && ratio <= 8) {
250 _ratio = 8;
251 } else if (ratio > 8 && ratio <= 16) {
252 _ratio = 16;
253 } else {
254 _ratio = 32;
255 }
256
257 uint32_t dst_width = _width / _ratio;
258 if ((dst_width % 2) != 0)
259 dst_width -= 1;
260
261 uint32_t dst_height = _height / _ratio;
262 if ((dst_height % 2) != 0)
263 dst_height -= 1;
264
265 for (int i = 0; i < index; i++) {
266 ret = _dev[i]->set_format(dst_width,
267 dst_height,
268 _sns_v4l_pix_fmt,
269 V4L2_FIELD_NONE,
270 0);
271 if (ret < 0) {
272 LOGE_CAMHW_SUBM(ISP20HW_SUBM, "device(%s) set format failed", _dev[i]->get_device_name());
273 } else {
274 LOGD_CAMHW_SUBM(ISP20HW_SUBM, "device(%s) set fmt info: fmt 0x%x, %dx%d !",
275 _dev[i]->get_device_name(), _sns_v4l_pix_fmt,
276 dst_width, dst_height);
277 }
278 }
279
280 return ret;
281 }
282
283 XCamReturn
prepare()284 CifSclStream::prepare()
285 {
286 XCamReturn ret = XCAM_RETURN_NO_ERROR;
287
288 if (!_init || _active)
289 return XCAM_RETURN_NO_ERROR;
290
291 for (int i = 0; i < index; i++) {
292 ret = _dev[i]->prepare();
293 if (ret < 0)
294 LOGE_CAMHW_SUBM(ISP20HW_SUBM, "mipi tx:%d prepare err: %d\n", ret);
295
296 _stream[i]->set_device_prepared(true);
297 }
298
299 return ret;
300 }
301
302 bool
getIsActive()303 CifSclStream::getIsActive()
304 {
305 return _active;
306 }
307
308 }