1 /* 2 * Copyright (c) 2019 Rockchip Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 #ifndef _LENS_HW_BASE_H_ 19 #define _LENS_HW_BASE_H_ 20 21 #include <map> 22 #include <list> 23 #include <xcam_mutex.h> 24 #include "xcam_thread.h" 25 #include "smartptr.h" 26 #include "safe_list.h" 27 #include "v4l2_device.h" 28 #include "rk_aiq_pool.h" 29 #include "common/rk-camera-module.h" 30 31 #define VCMDRV_SETZOOM_MAXCNT 300U 32 #define LENSHW_RECORD_SOF_NUM 256 33 #define LENSHW_RECORD_LOWPASSFV_NUM 256 34 35 #define RK_VIDIOC_VCM_TIMEINFO \ 36 _IOR('V', BASE_VIDIOC_PRIVATE + 0, struct rk_cam_vcm_tim) 37 #define RK_VIDIOC_IRIS_TIMEINFO \ 38 _IOR('V', BASE_VIDIOC_PRIVATE + 1, struct rk_cam_vcm_tim) 39 #define RK_VIDIOC_ZOOM_TIMEINFO \ 40 _IOR('V', BASE_VIDIOC_PRIVATE + 2, struct rk_cam_vcm_tim) 41 42 #define RK_VIDIOC_GET_VCM_CFG \ 43 _IOR('V', BASE_VIDIOC_PRIVATE + 3, struct rk_cam_vcm_cfg) 44 #define RK_VIDIOC_SET_VCM_CFG \ 45 _IOW('V', BASE_VIDIOC_PRIVATE + 4, struct rk_cam_vcm_cfg) 46 47 #define RK_VIDIOC_FOCUS_CORRECTION \ 48 _IOR('V', BASE_VIDIOC_PRIVATE + 5, unsigned int) 49 #define RK_VIDIOC_IRIS_CORRECTION \ 50 _IOR('V', BASE_VIDIOC_PRIVATE + 6, unsigned int) 51 #define RK_VIDIOC_ZOOM_CORRECTION \ 52 _IOR('V', BASE_VIDIOC_PRIVATE + 7, unsigned int) 53 54 #define RK_VIDIOC_FOCUS_SET_BACKLASH \ 55 _IOR('V', BASE_VIDIOC_PRIVATE + 8, unsigned int) 56 #define RK_VIDIOC_IRIS_SET_BACKLASH \ 57 _IOR('V', BASE_VIDIOC_PRIVATE + 9, unsigned int) 58 #define RK_VIDIOC_ZOOM_SET_BACKLASH \ 59 _IOR('V', BASE_VIDIOC_PRIVATE + 10, unsigned int) 60 61 #define RK_VIDIOC_ZOOM1_TIMEINFO \ 62 _IOR('V', BASE_VIDIOC_PRIVATE + 11, struct rk_cam_vcm_tim) 63 #define RK_VIDIOC_ZOOM1_CORRECTION \ 64 _IOR('V', BASE_VIDIOC_PRIVATE + 12, unsigned int) 65 #define RK_VIDIOC_ZOOM1_SET_BACKLASH \ 66 _IOR('V', BASE_VIDIOC_PRIVATE + 13, unsigned int) 67 68 #define RK_VIDIOC_ZOOM_SET_POSITION \ 69 _IOW('V', BASE_VIDIOC_PRIVATE + 14, struct rk_cam_set_zoom) 70 #define RK_VIDIOC_FOCUS_SET_POSITION \ 71 _IOW('V', BASE_VIDIOC_PRIVATE + 15, struct rk_cam_set_focus) 72 #define RK_VIDIOC_MODIFY_POSITION \ 73 _IOW('V', BASE_VIDIOC_PRIVATE + 16, struct rk_cam_modify_pos) 74 75 #define RK_VIDIOC_GET_DCIRIS_HALL_ADC \ 76 _IOR('V', BASE_VIDIOC_PRIVATE + 20, unsigned int) 77 78 #ifdef CONFIG_COMPAT 79 #define RK_VIDIOC_COMPAT_VCM_TIMEINFO \ 80 _IOR('V', BASE_VIDIOC_PRIVATE + 0, struct rk_cam_compat_vcm_tim) 81 #define RK_VIDIOC_COMPAT_IRIS_TIMEINFO \ 82 _IOR('V', BASE_VIDIOC_PRIVATE + 1, struct rk_cam_compat_vcm_tim) 83 #define RK_VIDIOC_COMPAT_ZOOM_TIMEINFO \ 84 _IOR('V', BASE_VIDIOC_PRIVATE + 2, struct rk_cam_compat_vcm_tim) 85 #define RK_VIDIOC_COMPAT_ZOOM1_TIMEINFO \ 86 _IOR('V', BASE_VIDIOC_PRIVATE + 11, struct rk_cam_compat_vcm_tim) 87 #endif 88 89 #define RK_VIDIOC_SET_VCM_MAX_LOGICALPOS \ 90 _IOW('V', BASE_VIDIOC_PRIVATE + 17, unsigned int) 91 92 typedef int s32; 93 typedef unsigned int u32; 94 95 struct rk_cam_modify_pos { 96 s32 focus_pos; 97 s32 zoom_pos; 98 s32 zoom1_pos; 99 }; 100 101 struct rk_cam_set_focus { 102 bool is_need_reback; 103 s32 focus_pos; 104 }; 105 106 struct rk_cam_zoom_pos { 107 s32 zoom_pos; 108 s32 focus_pos; 109 }; 110 111 struct rk_cam_set_zoom { 112 bool is_need_zoom_reback; 113 bool is_need_focus_reback; 114 u32 setzoom_cnt; 115 struct rk_cam_zoom_pos zoom_pos[VCMDRV_SETZOOM_MAXCNT]; 116 }; 117 118 struct rk_cam_vcm_tim { 119 struct timeval vcm_start_t; 120 struct timeval vcm_end_t; 121 }; 122 123 #ifdef CONFIG_COMPAT 124 struct rk_cam_compat_vcm_tim { 125 struct compat_timeval vcm_start_t; 126 struct compat_timeval vcm_end_t; 127 }; 128 #endif 129 130 struct rk_cam_motor_tim { 131 struct timeval motor_start_t; 132 struct timeval motor_end_t; 133 }; 134 135 struct rk_cam_vcm_cfg { 136 int start_ma; 137 int rated_ma; 138 int step_mode; 139 }; 140 141 142 #define LENSHW_RECORD_SOF_NUM 256 143 144 using namespace XCam; 145 146 namespace RkCam { 147 148 #define LENS_SUBM (0x10) 149 150 class LensHwHelperThd; 151 152 class LensHw : public V4l2SubDevice { 153 public: 154 explicit LensHw(const char* name); 155 virtual ~LensHw(); 156 157 XCamReturn getOTPData(); 158 XCamReturn start(bool prepared = false) override; 159 XCamReturn stop() override; 160 XCamReturn start_internal(); 161 XCamReturn getLensModeData(rk_aiq_lens_descriptor& lens_des); 162 XCamReturn getLensVcmCfg(rk_aiq_lens_vcmcfg& lens_cfg); 163 XCamReturn setLensVcmCfg(rk_aiq_lens_vcmcfg& lens_cfg); 164 XCamReturn getLensVcmMaxlogpos(int& max_log_pos); 165 XCamReturn setLensVcmMaxlogpos(int& max_log_pos); 166 XCamReturn setPIrisParams(int step); 167 XCamReturn setDCIrisParams(int pwmDuty); 168 XCamReturn setHDCIrisParams(int target); 169 XCamReturn getHDCIrisParams(int* adc); 170 XCamReturn setFocusParams(SmartPtr<RkAiqFocusParamsProxy>& focus_params); 171 XCamReturn setFocusParamsSync(int position, bool is_update_time, bool focus_noreback); 172 XCamReturn setZoomParams(int position); 173 XCamReturn setZoomFocusParams(SmartPtr<RkAiqFocusParamsProxy>& focus_params); 174 XCamReturn setZoomFocusParamsSync(SmartPtr<rk_aiq_focus_params_t> attrPtr, bool is_update_time); 175 XCamReturn setZoomFocusRebackSync(SmartPtr<rk_aiq_focus_params_t> attrPtr, bool is_update_time); 176 XCamReturn endZoomChgSync(SmartPtr<rk_aiq_focus_params_t> attrPtr, bool is_update_time); 177 XCamReturn getPIrisParams(int* step); 178 XCamReturn getFocusParams(int* position); 179 XCamReturn getZoomParams(int* position); 180 XCamReturn FocusCorrectionSync(); 181 XCamReturn ZoomCorrectionSync(); 182 XCamReturn FocusCorrection(); 183 XCamReturn ZoomCorrection(); 184 XCamReturn ZoomFocusModifyPositionSync(SmartPtr<rk_aiq_focus_params_t> attrPtr); 185 XCamReturn ZoomFocusModifyPosition(SmartPtr<RkAiqFocusParamsProxy>& focus_params); 186 XCamReturn handle_sof(int64_t time, uint32_t frameid); 187 XCamReturn setLowPassFv(uint32_t sub_shp4_4[RKAIQ_RAWAF_SUMDATA_NUM], uint32_t sub_shp8_8[RKAIQ_RAWAF_SUMDATA_NUM], 188 uint32_t high_light[RKAIQ_RAWAF_SUMDATA_NUM], uint32_t high_light2[RKAIQ_RAWAF_SUMDATA_NUM], uint32_t frameid); 189 XCamReturn getIrisInfoParams(SmartPtr<RkAiqIrisParamsProxy>& irisParams, uint32_t frame_id); 190 XCamReturn getAfInfoParams(SmartPtr<RkAiqAfInfoProxy>& afInfo, uint32_t frame_id); 191 XCamReturn setAngleZ(float angleZ); 192 193 private: 194 XCamReturn queryLensSupport(); 195 196 XCAM_DEAD_COPY (LensHw); 197 Mutex _mutex; 198 SmartPtr<RkAiqAfInfoPool> _afInfoPool; 199 SmartPtr<RkAiqIrisParamsPool> _irisInfoPool; 200 static uint16_t DEFAULT_POOL_SIZE; 201 struct v4l2_queryctrl _iris_query; 202 struct v4l2_queryctrl _focus_query; 203 struct v4l2_queryctrl _zoom_query; 204 struct rk_cam_motor_tim _dciris_tim; 205 struct rk_cam_motor_tim _piris_tim; 206 struct rk_cam_vcm_tim _focus_tim; 207 struct rk_cam_vcm_tim _zoom_tim; 208 bool _iris_enable; 209 bool _focus_enable; 210 bool _zoom_enable; 211 bool _zoom_correction; 212 bool _focus_correction; 213 int _piris_step; 214 int _last_piris_step; 215 int _dciris_pwmduty; 216 int _last_dciris_pwmduty; 217 int _hdciris_target; 218 int _last_hdciris_target; 219 int _focus_pos; 220 int _zoom_pos; 221 int _last_zoomchg_focus; 222 int _last_zoomchg_zoom; 223 int64_t _frame_time[LENSHW_RECORD_SOF_NUM]; 224 uint32_t _frame_sequence[LENSHW_RECORD_SOF_NUM]; 225 int _rec_sof_idx; 226 int32_t _lowfv_fv4_4[LENSHW_RECORD_LOWPASSFV_NUM][RKAIQ_RAWAF_SUMDATA_NUM]; 227 int32_t _lowfv_fv8_8[LENSHW_RECORD_LOWPASSFV_NUM][RKAIQ_RAWAF_SUMDATA_NUM]; 228 int32_t _lowfv_highlht[LENSHW_RECORD_LOWPASSFV_NUM][RKAIQ_RAWAF_SUMDATA_NUM]; 229 int32_t _lowfv_highlht2[LENSHW_RECORD_LOWPASSFV_NUM][RKAIQ_RAWAF_SUMDATA_NUM]; 230 uint32_t _lowfv_seq[LENSHW_RECORD_LOWPASSFV_NUM]; 231 int _rec_lowfv_idx; 232 SmartPtr<LensHwHelperThd> _lenshw_thd; 233 SmartPtr<LensHwHelperThd> _lenshw_thd1; 234 235 bool _otp_valid; 236 float _posture; 237 float _hysteresis; 238 float _startCurrent; 239 float _endCurrent; 240 float _angleZ; 241 int _max_logical_pos; 242 }; 243 244 class LensHwHelperThd 245 : public Thread { 246 public: LensHwHelperThd(LensHw * lenshw,int id)247 LensHwHelperThd(LensHw *lenshw, int id) 248 : Thread("LensHwHelperThread") 249 , mLensHw(lenshw), mId(id) { 250 (void)(mId); 251 }; ~LensHwHelperThd()252 ~LensHwHelperThd() { 253 mAttrQueue.clear (); 254 }; 255 triger_stop()256 void triger_stop() { 257 mAttrQueue.pause_pop (); 258 }; 259 triger_start()260 void triger_start() { 261 mAttrQueue.resume_pop (); 262 }; 263 push_attr(const SmartPtr<rk_aiq_focus_params_t> buffer)264 bool push_attr (const SmartPtr<rk_aiq_focus_params_t> buffer) { 265 mAttrQueue.push (buffer); 266 return true; 267 }; 268 is_empty()269 bool is_empty () { 270 return mAttrQueue.is_empty(); 271 }; 272 clear_attr()273 void clear_attr () { 274 mAttrQueue.clear (); 275 }; 276 277 protected: 278 //virtual bool started (); stopped()279 virtual void stopped () { 280 mAttrQueue.clear (); 281 }; 282 virtual bool loop (); 283 private: 284 LensHw *mLensHw; 285 int mId; 286 SafeList<rk_aiq_focus_params_t> mAttrQueue; 287 }; 288 289 } //namespace RkCam 290 291 #endif 292