fixes for Hue HD
[microdia.git] / sn9c20x-dev.c
blob96964dffdaccdb03b8b1d567c18a6bc0fdef1ab8
1 /**
2 * @file sn9c20x-dev.c
3 * @author Nicolas VIVIEN
4 * @date 2008-02-01
6 * @brief Device specific functions
8 * @note Copyright (C) Nicolas VIVIEN
10 * @par Licences
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/errno.h>
28 #include <linux/string.h>
29 #include <stdarg.h>
31 #include "sn9c20x.h"
32 #include "sn9c20x-bridge.h"
33 #include "omnivision.h"
34 #include "micron.h"
36 int (*sn_probes[])(struct usb_sn9c20x *dev) = {
37 mt9v011_probe,
38 mt9v111_probe,
39 mt9v112_probe,
42 struct sn9c20x_video_mode sn9c20x_modes[SN9C20X_N_MODES] = {
44 .width = 128,
45 .height = 96,
46 .scale = SN9C20X_1_4_SCALE,
47 .window = {65, 48, 1024, 768}
50 .width = 160,
51 .height = 120,
52 .scale = SN9C20X_1_4_SCALE,
53 .window = {0, 0, 640, 480}
56 .width = 176,
57 .height = 144,
58 .scale = SN9C20X_1_2_SCALE,
59 .window = {145, 96, 704, 576}
62 .width = 320,
63 .height = 240,
64 .scale = SN9C20X_1_2_SCALE,
65 .window = {0, 0, 640, 480}
68 .width = 352,
69 .height = 288,
70 .scale = SN9C20X_NO_SCALE,
71 .window = {145, 96, 704, 576}
74 .width = 640,
75 .height = 480,
76 .scale = SN9C20X_NO_SCALE,
77 .window = {0, 0, 640, 480}
81 struct sn9c20x_video_format sn9c20x_fmts[SN9C20X_N_FMTS] = {
83 .pix_fmt = V4L2_PIX_FMT_SN9C20X_I420,
84 .desc = "SN9C20X I420 (YUV 4:2:0)",
85 .depth = 12,
86 .set_format = sn9c20x_set_yuv420,
89 .pix_fmt = V4L2_PIX_FMT_SBGGR8,
90 .desc = "Bayer 8bit (BGGR)",
91 .depth = 8,
92 .set_format = sn9c20x_set_raw,
96 .pix_fmt = V4L2_PIX_FMT_YUYV,
97 .desc = "YUYV (YUV 4:2:0)",
98 .depth = 16,
99 .set_format = sn9c20x_set_yuv422,
103 .pix_fmt = V4L2_PIX_FMT_JPEG,
104 .desc = "JPEG (YUV 4:2:2)",
105 .depth = 16,
106 .set_format = sn9c20x_set_jpeg,
110 int sn9c20x_initialize_sensor(struct usb_sn9c20x *dev)
112 int ret = 0;
113 int i;
115 dev->camera.min_yavg = 60;
116 dev->camera.max_yavg = 150;
117 dev->camera.min_stable_yavg = 70;
118 dev->camera.max_stable_yavg = 140;
119 dev->camera.old_yavg = -1;
121 /* Probe sensor first if sensor set to probe*/
122 if (dev->camera.sensor == PROBE_SENSOR) {
123 for (i = 0; i < ARRAY_SIZE(sn_probes); i++) {
124 ret = sn_probes[i](dev);
125 dev->camera.sensor = ret;
126 if (ret > 0)
127 break;
131 switch (dev->camera.sensor) {
132 case SOI968_SENSOR:
133 sn9c20x_write_i2c_array(dev, soi968_init, 0);
134 dev->camera.set_exposure = soi968_set_exposure;
135 dev->camera.set_auto_exposure = soi968_set_autoexposure;
136 dev->camera.set_gain = soi968_set_gain;
137 dev->camera.set_auto_gain = ov_set_autogain;
138 dev->camera.button_detect = soi968_button_detect;
139 dev->camera.hstart = 60;
140 dev->camera.vstart = 11;
141 UDIA_INFO("Detected SOI968 Sensor.\n");
142 break;
143 case OV9650_SENSOR:
144 sn9c20x_write_i2c_array(dev, ov9650_init, 0);
145 dev->camera.hstart = 1;
146 dev->camera.vstart = 7;
147 dev->camera.set_hvflip = ov965x_set_hvflip;
148 dev->camera.set_exposure = ov_set_exposure;
149 dev->camera.set_auto_gain = ov_set_autogain;
150 dev->camera.set_gain = ov9650_set_gain;
151 dev->camera.flip_detect = ov965x_flip_detect;
152 UDIA_INFO("Detected OV9650 Sensor.\n");
153 break;
154 case OV9655_SENSOR:
155 sn9c20x_write_i2c_array(dev, ov9655_init, 0);
156 dev->camera.set_exposure = ov_set_exposure;
157 dev->camera.set_auto_gain = ov_set_autogain;
158 dev->camera.hstart = 0;
159 dev->camera.vstart = 7;
160 UDIA_INFO("Detected OV9655 Sensor.\n");
161 break;
162 case OV7670_SENSOR:
163 sn9c20x_write_i2c_array(dev, ov7670_init, 0);
164 dev->camera.set_exposure = ov_set_exposure;
165 dev->camera.set_auto_gain = ov_set_autogain;
166 dev->camera.flip_detect = ov7670_flip_detect;
167 dev->camera.hstart = 0;
168 dev->camera.vstart = 1;
169 UDIA_INFO("Detected OV7670 Sensor.\n");
170 break;
171 case OV7660_SENSOR:
172 sn9c20x_write_i2c_array(dev, ov7660_init, 0);
173 dev->camera.set_exposure = ov_set_exposure;
174 dev->camera.set_auto_gain = ov_set_autogain;
175 dev->camera.hstart = 1;
176 dev->camera.vstart = 1;
177 UDIA_INFO("Detected OV7660 Sensor.\n");
178 break;
179 case MT9V111_SENSOR:
180 sn9c20x_write_i2c_array(dev, mt9v111_init, 1);
181 dev->camera.set_hvflip = mt9v111_set_hvflip;
182 dev->camera.set_exposure = mt9v111_set_exposure;
183 dev->camera.set_auto_exposure = mt9v111_set_autoexposure;
184 dev->camera.hstart = 2;
185 dev->camera.vstart = 2;
186 UDIA_INFO("Detected MT9V111 Sensor.\n");
187 break;
188 case MT9V112_SENSOR:
189 sn9c20x_write_i2c_array(dev, mt9v112_init, 1);
190 dev->camera.hstart = 6;
191 dev->camera.vstart = 2;
192 UDIA_INFO("Detected MT9V112 Sensor.\n");
193 break;
194 case MT9M111_SENSOR:
195 sn9c20x_write_i2c_array(dev, mt9m111_init, 1);
196 dev->camera.set_exposure = mt9m111_set_exposure;
197 dev->camera.set_auto_exposure = mt9m111_set_autoexposure;
198 dev->camera.set_auto_whitebalance = mt9m111_set_autowhitebalance;
199 dev->camera.hstart = 0;
200 dev->camera.vstart = 2;
201 UDIA_INFO("Detected MT9M111 Sensor.\n");
202 break;
203 case MT9V011_SENSOR:
204 sn9c20x_write_i2c_array(dev, mt9v011_init, 1);
205 dev->camera.set_hvflip = mt9v011_set_hvflip;
206 dev->camera.set_exposure = mt9v011_set_exposure;
207 dev->camera.hstart = 2;
208 dev->camera.vstart = 2;
209 UDIA_INFO("Detected MT9V011 Sensor.\n");
210 break;
211 case MT9M001_SENSOR:
212 sn9c20x_write_i2c_array(dev, mt9m001_init, 1);
213 dev->camera.hstart = 2;
214 dev->camera.vstart = 2;
215 UDIA_INFO("Detected MT9M001 Sensor.\n");
216 break;
217 case HV7131R_SENSOR:
218 dev->camera.i2c_flags |= SN9C20X_I2C_400KHZ;
219 sn9c20x_write_i2c_array(dev, hv7131r_init, 0);
220 dev->camera.set_hvflip = hv7131r_set_hvflip;
221 dev->camera.set_gain = hv7131r_set_gain;
222 dev->camera.set_exposure = hv7131r_set_exposure;
223 dev->camera.hstart = 0;
224 dev->camera.vstart = 1;
225 UDIA_INFO("Detected HV7131R Sensor.\n");
226 break;
227 default:
228 ret = -EINVAL;
229 UDIA_INFO("Unsupported sensor.\n");
231 return ret;
236 * @brief Wrapper function to detect hardware states
238 * @param dev Pointer to device structure
240 * @returns 0
242 int dev_sn9c20x_call_constantly(struct usb_sn9c20x *dev)
245 /* Know to be broken, temporarely disabled */
246 /*dev_sn9c20x_flip_detection(dev);*/
247 dev_sn9c20x_button_detection(dev);
248 if (!dev->camera.set_auto_exposure &&
249 dev->vsettings.auto_exposure) {
250 dev_sn9c20x_perform_soft_ae(dev);
253 return 0;
257 * @brief Wrapper function to detect a flipped sensor
259 * @param dev Pointer to device structure
261 * @returns 0 or negative error value
264 int dev_sn9c20x_flip_detection(struct usb_sn9c20x *dev)
266 int ret = -ENODEV;
267 if (dev && dev->camera.flip_detect)
268 ret = dev->camera.flip_detect(dev);
269 return ret;
273 * @brief Wrapper function to detect a pushed button
275 * @param dev Pointer to device structure
277 * @returns 0 or negative error value
280 int dev_sn9c20x_button_detection(struct usb_sn9c20x *dev)
282 int ret = -ENODEV;
283 if (dev && dev->camera.button_detect)
284 ret = dev->camera.button_detect(dev);
285 return ret;
289 * @brief Perform software autoexposure
291 * @param dev
293 * @returns 0 or negative error value
295 * @author Vasily Khoruzhick
297 int dev_sn9c20x_perform_soft_ae(struct usb_sn9c20x *dev)
299 int yavg;
300 int koef = 100, new_exp, i;
301 yavg = -1;
302 i = 10;
304 if (!dev->camera.set_exposure)
305 return -1;
306 yavg = atomic_read(&dev->camera.yavg);
308 UDIA_DEBUG("Sensor YAVG: %d\n", yavg);
309 if (yavg < 0) {
310 /* Can't get YAVG - we have nothing to do */
311 return -1;
314 /* Image is too dark */
315 if (yavg < dev->camera.min_yavg) {
316 /* yavg can't be 0 - in that way new_exp == infinity */
317 if (yavg == 0)
318 yavg = 1;
319 if (dev->camera.old_yavg != -1) {
320 /* Previous correction was made to make image darker,
321 * but we made it too dark. Calculating our error
322 * and taking it into account.
323 * Usually error is 0.7 - 2, so we multiply it by 100
325 if (dev->camera.old_yavg > dev->camera.max_yavg)
326 koef = 100 * dev->camera.
327 min_stable_yavg / yavg;
328 else
329 koef = 100;
332 /* Calculating new exposure value. We assuming that
333 * exposure linearly depends on YAVG, but we taking
334 * our calculated error into account */
335 new_exp = (dev->camera.min_stable_yavg
336 * dev->vsettings.exposure * 100) / (yavg * koef);
338 /* Exposure can't be more than 0xff or less than 0x1 */
339 if (new_exp > 0xff)
340 new_exp = 0xff;
341 if (new_exp < 0x1)
342 new_exp = 1;
344 dev->camera.old_yavg = yavg;
346 /* Applying new exposure */
347 dev->vsettings.exposure = new_exp;
348 dev->camera.set_exposure(dev);
351 /* Image is too bright */
352 else if (yavg > dev->camera.max_yavg) {
353 /* yavg can't be 0 - in that way new_exp == infinity */
354 if (yavg == 0)
355 yavg = 1;
356 if (dev->camera.old_yavg != -1) {
358 /* Previous correction was made to make image brighter,
359 * but we made it too bright. Calculating our error
360 * and taking it into account.
361 * Usually error is 0.7 - 2, so we multiply it by 100
363 if (dev->camera.old_yavg < dev->camera.min_yavg)
364 koef = 100 * yavg / dev->camera.
365 max_stable_yavg;
366 else
367 koef = 100;
370 /* Calculating new exposure value. We assuming that
371 * exposure linearly depends on YAVG, but we taking
372 * our calculated error into account */
373 new_exp = (koef * dev->camera.max_stable_yavg
374 * dev->vsettings.exposure) / (yavg * 100);
376 /* Exposure can't be more than 0xff or less than 0x1 */
377 if (new_exp > 0xff)
378 new_exp = 0xff;
379 if (new_exp < 0x1)
380 new_exp = 1;
382 dev->camera.old_yavg = yavg;
384 /* Applying new exposure */
385 dev->vsettings.exposure = new_exp;
386 dev->camera.set_exposure(dev);
389 return 0;