]>
Commit | Line | Data |
---|---|---|
8668d504 HG |
1 | /* |
2 | * Support for the sensor part which is integrated (I think) into the | |
3 | * st6422 stv06xx alike bridge, as its integrated there are no i2c writes | |
4 | * but instead direct bridge writes. | |
5 | * | |
6 | * Copyright (c) 2009 Hans de Goede <hdegoede@redhat.com> | |
7 | * | |
8 | * Strongly based on qc-usb-messenger, which is: | |
9 | * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher | |
10 | * Mark Cave-Ayland, Carlo E Prelz, Dick Streefland | |
11 | * Copyright (c) 2002, 2003 Tuukka Toivonen | |
12 | * | |
13 | * This program is free software; you can redistribute it and/or modify | |
14 | * it under the terms of the GNU General Public License as published by | |
15 | * the Free Software Foundation; either version 2 of the License, or | |
16 | * (at your option) any later version. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
24 | * along with this program; if not, write to the Free Software | |
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
26 | * | |
27 | */ | |
28 | ||
29 | #include "stv06xx_st6422.h" | |
30 | ||
31 | static struct v4l2_pix_format st6422_mode[] = { | |
32 | /* Note we actually get 124 lines of data, of which we skip the 4st | |
33 | 4 as they are garbage */ | |
34 | { | |
35 | 162, | |
36 | 120, | |
37 | V4L2_PIX_FMT_SGRBG8, | |
38 | V4L2_FIELD_NONE, | |
39 | .sizeimage = 162 * 120, | |
40 | .bytesperline = 162, | |
41 | .colorspace = V4L2_COLORSPACE_SRGB, | |
42 | .priv = 1 | |
43 | }, | |
44 | /* Note we actually get 248 lines of data, of which we skip the 4st | |
45 | 4 as they are garbage, and we tell the app it only gets the | |
46 | first 240 of the 244 lines it actually gets, so that it ignores | |
47 | the last 4. */ | |
48 | { | |
49 | 324, | |
50 | 240, | |
51 | V4L2_PIX_FMT_SGRBG8, | |
52 | V4L2_FIELD_NONE, | |
53 | .sizeimage = 324 * 244, | |
54 | .bytesperline = 324, | |
55 | .colorspace = V4L2_COLORSPACE_SRGB, | |
56 | .priv = 0 | |
57 | }, | |
58 | }; | |
59 | ||
60 | static const struct ctrl st6422_ctrl[] = { | |
61 | #define BRIGHTNESS_IDX 0 | |
62 | { | |
63 | { | |
64 | .id = V4L2_CID_BRIGHTNESS, | |
65 | .type = V4L2_CTRL_TYPE_INTEGER, | |
66 | .name = "Brightness", | |
67 | .minimum = 0, | |
68 | .maximum = 31, | |
69 | .step = 1, | |
70 | .default_value = 3 | |
71 | }, | |
72 | .set = st6422_set_brightness, | |
73 | .get = st6422_get_brightness | |
74 | }, | |
75 | #define CONTRAST_IDX 1 | |
76 | { | |
77 | { | |
78 | .id = V4L2_CID_CONTRAST, | |
79 | .type = V4L2_CTRL_TYPE_INTEGER, | |
80 | .name = "Contrast", | |
81 | .minimum = 0, | |
82 | .maximum = 15, | |
83 | .step = 1, | |
84 | .default_value = 11 | |
85 | }, | |
86 | .set = st6422_set_contrast, | |
87 | .get = st6422_get_contrast | |
88 | }, | |
89 | #define GAIN_IDX 2 | |
90 | { | |
91 | { | |
92 | .id = V4L2_CID_GAIN, | |
93 | .type = V4L2_CTRL_TYPE_INTEGER, | |
94 | .name = "Gain", | |
95 | .minimum = 0, | |
96 | .maximum = 255, | |
97 | .step = 1, | |
98 | .default_value = 64 | |
99 | }, | |
100 | .set = st6422_set_gain, | |
101 | .get = st6422_get_gain | |
102 | }, | |
103 | #define EXPOSURE_IDX 3 | |
104 | { | |
105 | { | |
106 | .id = V4L2_CID_EXPOSURE, | |
107 | .type = V4L2_CTRL_TYPE_INTEGER, | |
108 | .name = "Exposure", | |
109 | .minimum = 0, | |
110 | .maximum = 1023, | |
111 | .step = 1, | |
112 | .default_value = 256 | |
113 | }, | |
114 | .set = st6422_set_exposure, | |
115 | .get = st6422_get_exposure | |
116 | }, | |
117 | }; | |
118 | ||
119 | static int st6422_probe(struct sd *sd) | |
120 | { | |
121 | int i; | |
122 | s32 *sensor_settings; | |
123 | ||
124 | if (sd->bridge != BRIDGE_ST6422) | |
125 | return -ENODEV; | |
126 | ||
127 | info("st6422 sensor detected"); | |
128 | ||
129 | sensor_settings = kmalloc(ARRAY_SIZE(st6422_ctrl) * sizeof(s32), | |
130 | GFP_KERNEL); | |
131 | if (!sensor_settings) | |
132 | return -ENOMEM; | |
133 | ||
134 | sd->gspca_dev.cam.cam_mode = st6422_mode; | |
135 | sd->gspca_dev.cam.nmodes = ARRAY_SIZE(st6422_mode); | |
136 | sd->desc.ctrls = st6422_ctrl; | |
137 | sd->desc.nctrls = ARRAY_SIZE(st6422_ctrl); | |
138 | sd->sensor_priv = sensor_settings; | |
139 | ||
140 | for (i = 0; i < sd->desc.nctrls; i++) | |
141 | sensor_settings[i] = st6422_ctrl[i].qctrl.default_value; | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | static int st6422_init(struct sd *sd) | |
147 | { | |
148 | int err = 0, i; | |
149 | ||
150 | const u16 st6422_bridge_init[][2] = { | |
151 | { STV_ISO_ENABLE, 0x00 }, /* disable capture */ | |
152 | { 0x1436, 0x00 }, | |
153 | { 0x1432, 0x03 }, /* 0x00-0x1F brightness */ | |
154 | { 0x143a, 0xF9 }, /* 0x00-0x0F contrast */ | |
155 | { 0x0509, 0x38 }, /* R */ | |
156 | { 0x050a, 0x38 }, /* G */ | |
157 | { 0x050b, 0x38 }, /* B */ | |
158 | { 0x050c, 0x2A }, | |
159 | { 0x050d, 0x01 }, | |
160 | ||
161 | ||
162 | { 0x1431, 0x00 }, /* 0x00-0x07 ??? */ | |
163 | { 0x1433, 0x34 }, /* 160x120, 0x00-0x01 night filter */ | |
164 | { 0x1438, 0x18 }, /* 640x480 */ | |
165 | /* 18 bayes */ | |
166 | /* 10 compressed? */ | |
167 | ||
168 | { 0x1439, 0x00 }, | |
bf5c562a | 169 | /* anti-noise? 0xa2 gives a perfect image */ |
8668d504 HG |
170 | |
171 | { 0x143b, 0x05 }, | |
172 | { 0x143c, 0x00 }, /* 0x00-0x01 - ??? */ | |
173 | ||
174 | ||
175 | /* shutter time 0x0000-0x03FF */ | |
176 | /* low value give good picures on moving objects (but requires much light) */ | |
177 | /* high value gives good picures in darkness (but tends to be overexposed) */ | |
178 | { 0x143e, 0x01 }, | |
179 | { 0x143d, 0x00 }, | |
180 | ||
181 | { 0x1442, 0xe2 }, | |
182 | /* write: 1x1x xxxx */ | |
183 | /* read: 1x1x xxxx */ | |
184 | /* bit 5 == button pressed and hold if 0 */ | |
185 | /* write 0xe2,0xea */ | |
186 | ||
187 | /* 0x144a */ | |
188 | /* 0x00 init */ | |
189 | /* bit 7 == button has been pressed, but not handled */ | |
190 | ||
191 | /* interrupt */ | |
192 | /* if(urb->iso_frame_desc[i].status == 0x80) { */ | |
193 | /* if(urb->iso_frame_desc[i].status == 0x88) { */ | |
194 | ||
195 | { 0x1500, 0xd0 }, | |
196 | { 0x1500, 0xd0 }, | |
197 | { 0x1500, 0x50 }, /* 0x00 - 0xFF 0x80 == compr ? */ | |
198 | ||
199 | { 0x1501, 0xaf }, | |
bf5c562a EA |
200 | /* high val-> light area gets darker */ |
201 | /* low val -> light area gets lighter */ | |
8668d504 | 202 | { 0x1502, 0xc2 }, |
bf5c562a EA |
203 | /* high val-> light area gets darker */ |
204 | /* low val -> light area gets lighter */ | |
8668d504 | 205 | { 0x1503, 0x45 }, |
bf5c562a EA |
206 | /* high val-> light area gets darker */ |
207 | /* low val -> light area gets lighter */ | |
8668d504 HG |
208 | { 0x1505, 0x02 }, |
209 | /* 2 : 324x248 80352 bytes */ | |
210 | /* 7 : 248x162 40176 bytes */ | |
211 | /* c+f: 162*124 20088 bytes */ | |
212 | ||
213 | { 0x150e, 0x8e }, | |
214 | { 0x150f, 0x37 }, | |
215 | { 0x15c0, 0x00 }, | |
216 | { 0x15c1, 1023 }, /* 160x120, ISOC_PACKET_SIZE */ | |
217 | { 0x15c3, 0x08 }, /* 0x04/0x14 ... test pictures ??? */ | |
218 | ||
219 | ||
220 | { 0x143f, 0x01 }, /* commit settings */ | |
221 | ||
222 | }; | |
223 | ||
224 | for (i = 0; i < ARRAY_SIZE(st6422_bridge_init) && !err; i++) { | |
225 | err = stv06xx_write_bridge(sd, st6422_bridge_init[i][0], | |
226 | st6422_bridge_init[i][1]); | |
227 | } | |
228 | ||
229 | return err; | |
230 | } | |
231 | ||
232 | static void st6422_disconnect(struct sd *sd) | |
233 | { | |
234 | sd->sensor = NULL; | |
235 | kfree(sd->sensor_priv); | |
236 | } | |
237 | ||
238 | static int st6422_start(struct sd *sd) | |
239 | { | |
240 | int err, packet_size; | |
241 | struct cam *cam = &sd->gspca_dev.cam; | |
242 | s32 *sensor_settings = sd->sensor_priv; | |
243 | struct usb_host_interface *alt; | |
244 | struct usb_interface *intf; | |
245 | ||
246 | intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface); | |
247 | alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt); | |
248 | if (!alt) { | |
0b656321 | 249 | err("Couldn't get altsetting"); |
8668d504 HG |
250 | return -EIO; |
251 | } | |
252 | ||
253 | packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize); | |
254 | err = stv06xx_write_bridge(sd, 0x15c1, packet_size); | |
255 | if (err < 0) | |
256 | return err; | |
257 | ||
258 | if (cam->cam_mode[sd->gspca_dev.curr_mode].priv) | |
259 | err = stv06xx_write_bridge(sd, 0x1505, 0x0f); | |
260 | else | |
261 | err = stv06xx_write_bridge(sd, 0x1505, 0x02); | |
262 | if (err < 0) | |
263 | return err; | |
264 | ||
265 | err = st6422_set_brightness(&sd->gspca_dev, | |
266 | sensor_settings[BRIGHTNESS_IDX]); | |
267 | if (err < 0) | |
268 | return err; | |
269 | ||
270 | err = st6422_set_contrast(&sd->gspca_dev, | |
271 | sensor_settings[CONTRAST_IDX]); | |
272 | if (err < 0) | |
273 | return err; | |
274 | ||
275 | err = st6422_set_exposure(&sd->gspca_dev, | |
276 | sensor_settings[EXPOSURE_IDX]); | |
277 | if (err < 0) | |
278 | return err; | |
279 | ||
280 | err = st6422_set_gain(&sd->gspca_dev, | |
281 | sensor_settings[GAIN_IDX]); | |
282 | if (err < 0) | |
283 | return err; | |
284 | ||
285 | PDEBUG(D_STREAM, "Starting stream"); | |
286 | ||
287 | return 0; | |
288 | } | |
289 | ||
290 | static int st6422_stop(struct sd *sd) | |
291 | { | |
292 | PDEBUG(D_STREAM, "Halting stream"); | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | static int st6422_get_brightness(struct gspca_dev *gspca_dev, __s32 *val) | |
298 | { | |
299 | struct sd *sd = (struct sd *) gspca_dev; | |
300 | s32 *sensor_settings = sd->sensor_priv; | |
301 | ||
302 | *val = sensor_settings[BRIGHTNESS_IDX]; | |
303 | ||
304 | PDEBUG(D_V4L2, "Read brightness %d", *val); | |
305 | ||
306 | return 0; | |
307 | } | |
308 | ||
309 | static int st6422_set_brightness(struct gspca_dev *gspca_dev, __s32 val) | |
310 | { | |
311 | int err; | |
312 | struct sd *sd = (struct sd *) gspca_dev; | |
313 | s32 *sensor_settings = sd->sensor_priv; | |
314 | ||
315 | sensor_settings[BRIGHTNESS_IDX] = val; | |
316 | ||
317 | if (!gspca_dev->streaming) | |
318 | return 0; | |
319 | ||
320 | /* val goes from 0 -> 31 */ | |
321 | PDEBUG(D_V4L2, "Set brightness to %d", val); | |
322 | err = stv06xx_write_bridge(sd, 0x1432, val); | |
323 | if (err < 0) | |
324 | return err; | |
325 | ||
326 | /* commit settings */ | |
327 | err = stv06xx_write_bridge(sd, 0x143f, 0x01); | |
328 | return (err < 0) ? err : 0; | |
329 | } | |
330 | ||
331 | static int st6422_get_contrast(struct gspca_dev *gspca_dev, __s32 *val) | |
332 | { | |
333 | struct sd *sd = (struct sd *) gspca_dev; | |
334 | s32 *sensor_settings = sd->sensor_priv; | |
335 | ||
336 | *val = sensor_settings[CONTRAST_IDX]; | |
337 | ||
338 | PDEBUG(D_V4L2, "Read contrast %d", *val); | |
339 | ||
340 | return 0; | |
341 | } | |
342 | ||
343 | static int st6422_set_contrast(struct gspca_dev *gspca_dev, __s32 val) | |
344 | { | |
345 | int err; | |
346 | struct sd *sd = (struct sd *) gspca_dev; | |
347 | s32 *sensor_settings = sd->sensor_priv; | |
348 | ||
349 | sensor_settings[CONTRAST_IDX] = val; | |
350 | ||
351 | if (!gspca_dev->streaming) | |
352 | return 0; | |
353 | ||
354 | /* Val goes from 0 -> 15 */ | |
355 | PDEBUG(D_V4L2, "Set contrast to %d\n", val); | |
356 | err = stv06xx_write_bridge(sd, 0x143a, 0xf0 | val); | |
357 | if (err < 0) | |
358 | return err; | |
359 | ||
360 | /* commit settings */ | |
361 | err = stv06xx_write_bridge(sd, 0x143f, 0x01); | |
362 | return (err < 0) ? err : 0; | |
363 | } | |
364 | ||
365 | static int st6422_get_gain(struct gspca_dev *gspca_dev, __s32 *val) | |
366 | { | |
367 | struct sd *sd = (struct sd *) gspca_dev; | |
368 | s32 *sensor_settings = sd->sensor_priv; | |
369 | ||
370 | *val = sensor_settings[GAIN_IDX]; | |
371 | ||
372 | PDEBUG(D_V4L2, "Read gain %d", *val); | |
373 | ||
374 | return 0; | |
375 | } | |
376 | ||
377 | static int st6422_set_gain(struct gspca_dev *gspca_dev, __s32 val) | |
378 | { | |
379 | int err; | |
380 | struct sd *sd = (struct sd *) gspca_dev; | |
381 | s32 *sensor_settings = sd->sensor_priv; | |
382 | ||
383 | sensor_settings[GAIN_IDX] = val; | |
384 | ||
385 | if (!gspca_dev->streaming) | |
386 | return 0; | |
387 | ||
388 | PDEBUG(D_V4L2, "Set gain to %d", val); | |
389 | ||
390 | /* Set red, green, blue, gain */ | |
391 | err = stv06xx_write_bridge(sd, 0x0509, val); | |
392 | if (err < 0) | |
393 | return err; | |
394 | ||
395 | err = stv06xx_write_bridge(sd, 0x050a, val); | |
396 | if (err < 0) | |
397 | return err; | |
398 | ||
399 | err = stv06xx_write_bridge(sd, 0x050b, val); | |
400 | if (err < 0) | |
401 | return err; | |
402 | ||
403 | /* 2 mystery writes */ | |
404 | err = stv06xx_write_bridge(sd, 0x050c, 0x2a); | |
405 | if (err < 0) | |
406 | return err; | |
407 | ||
408 | err = stv06xx_write_bridge(sd, 0x050d, 0x01); | |
409 | if (err < 0) | |
410 | return err; | |
411 | ||
412 | /* commit settings */ | |
413 | err = stv06xx_write_bridge(sd, 0x143f, 0x01); | |
414 | return (err < 0) ? err : 0; | |
415 | } | |
416 | ||
417 | static int st6422_get_exposure(struct gspca_dev *gspca_dev, __s32 *val) | |
418 | { | |
419 | struct sd *sd = (struct sd *) gspca_dev; | |
420 | s32 *sensor_settings = sd->sensor_priv; | |
421 | ||
422 | *val = sensor_settings[EXPOSURE_IDX]; | |
423 | ||
424 | PDEBUG(D_V4L2, "Read exposure %d", *val); | |
425 | ||
426 | return 0; | |
427 | } | |
428 | ||
429 | static int st6422_set_exposure(struct gspca_dev *gspca_dev, __s32 val) | |
430 | { | |
431 | int err; | |
432 | struct sd *sd = (struct sd *) gspca_dev; | |
433 | s32 *sensor_settings = sd->sensor_priv; | |
434 | ||
435 | sensor_settings[EXPOSURE_IDX] = val; | |
436 | ||
437 | if (!gspca_dev->streaming) | |
438 | return 0; | |
439 | ||
440 | PDEBUG(D_V4L2, "Set exposure to %d\n", val); | |
441 | err = stv06xx_write_bridge(sd, 0x143d, val & 0xff); | |
442 | if (err < 0) | |
443 | return err; | |
444 | ||
445 | err = stv06xx_write_bridge(sd, 0x143e, val >> 8); | |
446 | if (err < 0) | |
447 | return err; | |
448 | ||
449 | /* commit settings */ | |
450 | err = stv06xx_write_bridge(sd, 0x143f, 0x01); | |
451 | return (err < 0) ? err : 0; | |
452 | } |