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