1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Functions for auto gain.
5 * Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com>
9 /* auto gain and exposure algorithm based on the knee algorithm described here:
10 http://ytse.tricolour.net/docs/LowLightOptimization.html
12 Returns 0 if no changes were made, 1 if the gain and or exposure settings
14 int gspca_expo_autogain(
15 struct gspca_dev
*gspca_dev
,
22 s32 gain
, orig_gain
, exposure
, orig_exposure
;
23 int i
, steps
, retval
= 0;
25 if (v4l2_ctrl_g_ctrl(gspca_dev
->autogain
) == 0)
28 orig_gain
= gain
= v4l2_ctrl_g_ctrl(gspca_dev
->gain
);
29 orig_exposure
= exposure
= v4l2_ctrl_g_ctrl(gspca_dev
->exposure
);
31 /* If we are of a multiple of deadzone, do multiple steps to reach the
32 desired lumination fast (with the risc of a slight overshoot) */
33 steps
= abs(desired_avg_lum
- avg_lum
) / deadzone
;
35 gspca_dbg(gspca_dev
, D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d\n",
36 avg_lum
, desired_avg_lum
, steps
);
38 for (i
= 0; i
< steps
; i
++) {
39 if (avg_lum
> desired_avg_lum
) {
42 else if (exposure
> exposure_knee
)
44 else if (gain
> gspca_dev
->gain
->default_value
)
46 else if (exposure
> gspca_dev
->exposure
->minimum
)
48 else if (gain
> gspca_dev
->gain
->minimum
)
53 if (gain
< gspca_dev
->gain
->default_value
)
55 else if (exposure
< exposure_knee
)
57 else if (gain
< gain_knee
)
59 else if (exposure
< gspca_dev
->exposure
->maximum
)
61 else if (gain
< gspca_dev
->gain
->maximum
)
68 if (gain
!= orig_gain
) {
69 v4l2_ctrl_s_ctrl(gspca_dev
->gain
, gain
);
72 if (exposure
!= orig_exposure
) {
73 v4l2_ctrl_s_ctrl(gspca_dev
->exposure
, exposure
);
78 gspca_dbg(gspca_dev
, D_FRAM
, "autogain: changed gain: %d, expo: %d\n",
82 EXPORT_SYMBOL(gspca_expo_autogain
);
84 /* Autogain + exposure algorithm for cameras with a coarse exposure control
85 (usually this means we can only control the clockdiv to change exposure)
86 As changing the clockdiv so that the fps drops from 30 to 15 fps for
87 example, will lead to a huge exposure change (it effectively doubles),
88 this algorithm normally tries to only adjust the gain (between 40 and
89 80 %) and if that does not help, only then changes exposure. This leads
90 to a much more stable image then using the knee algorithm which at
91 certain points of the knee graph will only try to adjust exposure,
92 which leads to oscillating as one exposure step is huge.
94 Returns 0 if no changes were made, 1 if the gain and or exposure settings
96 int gspca_coarse_grained_expo_autogain(
97 struct gspca_dev
*gspca_dev
,
102 s32 gain_low
, gain_high
, gain
, orig_gain
, exposure
, orig_exposure
;
103 int steps
, retval
= 0;
105 if (v4l2_ctrl_g_ctrl(gspca_dev
->autogain
) == 0)
108 orig_gain
= gain
= v4l2_ctrl_g_ctrl(gspca_dev
->gain
);
109 orig_exposure
= exposure
= v4l2_ctrl_g_ctrl(gspca_dev
->exposure
);
111 gain_low
= (s32
)(gspca_dev
->gain
->maximum
- gspca_dev
->gain
->minimum
) /
112 5 * 2 + gspca_dev
->gain
->minimum
;
113 gain_high
= (s32
)(gspca_dev
->gain
->maximum
- gspca_dev
->gain
->minimum
) /
114 5 * 4 + gspca_dev
->gain
->minimum
;
116 /* If we are of a multiple of deadzone, do multiple steps to reach the
117 desired lumination fast (with the risc of a slight overshoot) */
118 steps
= (desired_avg_lum
- avg_lum
) / deadzone
;
120 gspca_dbg(gspca_dev
, D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d\n",
121 avg_lum
, desired_avg_lum
, steps
);
123 if ((gain
+ steps
) > gain_high
&&
124 exposure
< gspca_dev
->exposure
->maximum
) {
126 gspca_dev
->exp_too_low_cnt
++;
127 gspca_dev
->exp_too_high_cnt
= 0;
128 } else if ((gain
+ steps
) < gain_low
&&
129 exposure
> gspca_dev
->exposure
->minimum
) {
131 gspca_dev
->exp_too_high_cnt
++;
132 gspca_dev
->exp_too_low_cnt
= 0;
135 if (gain
> gspca_dev
->gain
->maximum
)
136 gain
= gspca_dev
->gain
->maximum
;
137 else if (gain
< gspca_dev
->gain
->minimum
)
138 gain
= gspca_dev
->gain
->minimum
;
139 gspca_dev
->exp_too_high_cnt
= 0;
140 gspca_dev
->exp_too_low_cnt
= 0;
143 if (gspca_dev
->exp_too_high_cnt
> 3) {
145 gspca_dev
->exp_too_high_cnt
= 0;
146 } else if (gspca_dev
->exp_too_low_cnt
> 3) {
148 gspca_dev
->exp_too_low_cnt
= 0;
151 if (gain
!= orig_gain
) {
152 v4l2_ctrl_s_ctrl(gspca_dev
->gain
, gain
);
155 if (exposure
!= orig_exposure
) {
156 v4l2_ctrl_s_ctrl(gspca_dev
->exposure
, exposure
);
161 gspca_dbg(gspca_dev
, D_FRAM
, "autogain: changed gain: %d, expo: %d\n",
165 EXPORT_SYMBOL(gspca_coarse_grained_expo_autogain
);