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.
6 * Copyright (c) 2009 Hans de Goede <hdegoede@redhat.com>
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
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.
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.
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
29 #include "stv06xx_st6422.h"
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 */
39 .sizeimage
= 162 * 120,
41 .colorspace
= V4L2_COLORSPACE_SRGB
,
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
53 .sizeimage
= 324 * 244,
55 .colorspace
= V4L2_COLORSPACE_SRGB
,
60 static const struct ctrl st6422_ctrl
[] = {
61 #define BRIGHTNESS_IDX 0
64 .id
= V4L2_CID_BRIGHTNESS
,
65 .type
= V4L2_CTRL_TYPE_INTEGER
,
72 .set
= st6422_set_brightness
,
73 .get
= st6422_get_brightness
75 #define CONTRAST_IDX 1
78 .id
= V4L2_CID_CONTRAST
,
79 .type
= V4L2_CTRL_TYPE_INTEGER
,
86 .set
= st6422_set_contrast
,
87 .get
= st6422_get_contrast
93 .type
= V4L2_CTRL_TYPE_INTEGER
,
100 .set
= st6422_set_gain
,
101 .get
= st6422_get_gain
103 #define EXPOSURE_IDX 3
106 .id
= V4L2_CID_EXPOSURE
,
107 .type
= V4L2_CTRL_TYPE_INTEGER
,
114 .set
= st6422_set_exposure
,
115 .get
= st6422_get_exposure
119 static int st6422_probe(struct sd
*sd
)
122 s32
*sensor_settings
;
124 if (sd
->bridge
!= BRIDGE_ST6422
)
127 info("st6422 sensor detected");
129 sensor_settings
= kmalloc(ARRAY_SIZE(st6422_ctrl
) * sizeof(s32
),
131 if (!sensor_settings
)
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
;
140 for (i
= 0; i
< sd
->desc
.nctrls
; i
++)
141 sensor_settings
[i
] = st6422_ctrl
[i
].qctrl
.default_value
;
146 static int st6422_init(struct sd
*sd
)
150 const u16 st6422_bridge_init
[][2] = {
151 { STV_ISO_ENABLE
, 0x00 }, /* disable capture */
153 { 0x1432, 0x03 }, /* 0x00-0x1F brightness */
154 { 0x143a, 0xF9 }, /* 0x00-0x0F contrast */
155 { 0x0509, 0x38 }, /* R */
156 { 0x050a, 0x38 }, /* G */
157 { 0x050b, 0x38 }, /* B */
162 { 0x1431, 0x00 }, /* 0x00-0x07 ??? */
163 { 0x1433, 0x34 }, /* 160x120, 0x00-0x01 night filter */
164 { 0x1438, 0x18 }, /* 640x480 */
169 /* antiflimmer?? 0xa2 ger perfekt bild mot monitor */
172 { 0x143c, 0x00 }, /* 0x00-0x01 - ??? */
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) */
182 /* write: 1x1x xxxx */
183 /* read: 1x1x xxxx */
184 /* bit 5 == button pressed and hold if 0 */
185 /* write 0xe2,0xea */
189 /* bit 7 == button has been pressed, but not handled */
192 /* if(urb->iso_frame_desc[i].status == 0x80) { */
193 /* if(urb->iso_frame_desc[i].status == 0x88) { */
197 { 0x1500, 0x50 }, /* 0x00 - 0xFF 0x80 == compr ? */
200 /* high val-> ljus area blir morkare. */
201 /* low val -> ljus area blir ljusare. */
203 /* high val-> ljus area blir morkare. */
204 /* low val -> ljus area blir ljusare. */
206 /* high val-> ljus area blir morkare. */
207 /* low val -> ljus area blir ljusare. */
210 /* 2 : 324x248 80352 bytes */
211 /* 7 : 248x162 40176 bytes */
212 /* c+f: 162*124 20088 bytes */
217 { 0x15c1, 1023 }, /* 160x120, ISOC_PACKET_SIZE */
218 { 0x15c3, 0x08 }, /* 0x04/0x14 ... test pictures ??? */
221 { 0x143f, 0x01 }, /* commit settings */
225 for (i
= 0; i
< ARRAY_SIZE(st6422_bridge_init
) && !err
; i
++) {
226 err
= stv06xx_write_bridge(sd
, st6422_bridge_init
[i
][0],
227 st6422_bridge_init
[i
][1]);
233 static void st6422_disconnect(struct sd
*sd
)
236 kfree(sd
->sensor_priv
);
239 static int st6422_start(struct sd
*sd
)
241 int err
, packet_size
;
242 struct cam
*cam
= &sd
->gspca_dev
.cam
;
243 s32
*sensor_settings
= sd
->sensor_priv
;
244 struct usb_host_interface
*alt
;
245 struct usb_interface
*intf
;
247 intf
= usb_ifnum_to_if(sd
->gspca_dev
.dev
, sd
->gspca_dev
.iface
);
248 alt
= usb_altnum_to_altsetting(intf
, sd
->gspca_dev
.alt
);
250 PDEBUG(D_ERR
, "Couldn't get altsetting");
254 packet_size
= le16_to_cpu(alt
->endpoint
[0].desc
.wMaxPacketSize
);
255 err
= stv06xx_write_bridge(sd
, 0x15c1, packet_size
);
259 if (cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].priv
)
260 err
= stv06xx_write_bridge(sd
, 0x1505, 0x0f);
262 err
= stv06xx_write_bridge(sd
, 0x1505, 0x02);
266 err
= st6422_set_brightness(&sd
->gspca_dev
,
267 sensor_settings
[BRIGHTNESS_IDX
]);
271 err
= st6422_set_contrast(&sd
->gspca_dev
,
272 sensor_settings
[CONTRAST_IDX
]);
276 err
= st6422_set_exposure(&sd
->gspca_dev
,
277 sensor_settings
[EXPOSURE_IDX
]);
281 err
= st6422_set_gain(&sd
->gspca_dev
,
282 sensor_settings
[GAIN_IDX
]);
286 PDEBUG(D_STREAM
, "Starting stream");
291 static int st6422_stop(struct sd
*sd
)
293 PDEBUG(D_STREAM
, "Halting stream");
298 static int st6422_get_brightness(struct gspca_dev
*gspca_dev
, __s32
*val
)
300 struct sd
*sd
= (struct sd
*) gspca_dev
;
301 s32
*sensor_settings
= sd
->sensor_priv
;
303 *val
= sensor_settings
[BRIGHTNESS_IDX
];
305 PDEBUG(D_V4L2
, "Read brightness %d", *val
);
310 static int st6422_set_brightness(struct gspca_dev
*gspca_dev
, __s32 val
)
313 struct sd
*sd
= (struct sd
*) gspca_dev
;
314 s32
*sensor_settings
= sd
->sensor_priv
;
316 sensor_settings
[BRIGHTNESS_IDX
] = val
;
318 if (!gspca_dev
->streaming
)
321 /* val goes from 0 -> 31 */
322 PDEBUG(D_V4L2
, "Set brightness to %d", val
);
323 err
= stv06xx_write_bridge(sd
, 0x1432, val
);
327 /* commit settings */
328 err
= stv06xx_write_bridge(sd
, 0x143f, 0x01);
329 return (err
< 0) ? err
: 0;
332 static int st6422_get_contrast(struct gspca_dev
*gspca_dev
, __s32
*val
)
334 struct sd
*sd
= (struct sd
*) gspca_dev
;
335 s32
*sensor_settings
= sd
->sensor_priv
;
337 *val
= sensor_settings
[CONTRAST_IDX
];
339 PDEBUG(D_V4L2
, "Read contrast %d", *val
);
344 static int st6422_set_contrast(struct gspca_dev
*gspca_dev
, __s32 val
)
347 struct sd
*sd
= (struct sd
*) gspca_dev
;
348 s32
*sensor_settings
= sd
->sensor_priv
;
350 sensor_settings
[CONTRAST_IDX
] = val
;
352 if (!gspca_dev
->streaming
)
355 /* Val goes from 0 -> 15 */
356 PDEBUG(D_V4L2
, "Set contrast to %d\n", val
);
357 err
= stv06xx_write_bridge(sd
, 0x143a, 0xf0 | val
);
361 /* commit settings */
362 err
= stv06xx_write_bridge(sd
, 0x143f, 0x01);
363 return (err
< 0) ? err
: 0;
366 static int st6422_get_gain(struct gspca_dev
*gspca_dev
, __s32
*val
)
368 struct sd
*sd
= (struct sd
*) gspca_dev
;
369 s32
*sensor_settings
= sd
->sensor_priv
;
371 *val
= sensor_settings
[GAIN_IDX
];
373 PDEBUG(D_V4L2
, "Read gain %d", *val
);
378 static int st6422_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
381 struct sd
*sd
= (struct sd
*) gspca_dev
;
382 s32
*sensor_settings
= sd
->sensor_priv
;
384 sensor_settings
[GAIN_IDX
] = val
;
386 if (!gspca_dev
->streaming
)
389 PDEBUG(D_V4L2
, "Set gain to %d", val
);
391 /* Set red, green, blue, gain */
392 err
= stv06xx_write_bridge(sd
, 0x0509, val
);
396 err
= stv06xx_write_bridge(sd
, 0x050a, val
);
400 err
= stv06xx_write_bridge(sd
, 0x050b, val
);
404 /* 2 mystery writes */
405 err
= stv06xx_write_bridge(sd
, 0x050c, 0x2a);
409 err
= stv06xx_write_bridge(sd
, 0x050d, 0x01);
413 /* commit settings */
414 err
= stv06xx_write_bridge(sd
, 0x143f, 0x01);
415 return (err
< 0) ? err
: 0;
418 static int st6422_get_exposure(struct gspca_dev
*gspca_dev
, __s32
*val
)
420 struct sd
*sd
= (struct sd
*) gspca_dev
;
421 s32
*sensor_settings
= sd
->sensor_priv
;
423 *val
= sensor_settings
[EXPOSURE_IDX
];
425 PDEBUG(D_V4L2
, "Read exposure %d", *val
);
430 static int st6422_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
)
433 struct sd
*sd
= (struct sd
*) gspca_dev
;
434 s32
*sensor_settings
= sd
->sensor_priv
;
436 sensor_settings
[EXPOSURE_IDX
] = val
;
438 if (!gspca_dev
->streaming
)
441 PDEBUG(D_V4L2
, "Set exposure to %d\n", val
);
442 err
= stv06xx_write_bridge(sd
, 0x143d, val
& 0xff);
446 err
= stv06xx_write_bridge(sd
, 0x143e, val
>> 8);
450 /* commit settings */
451 err
= stv06xx_write_bridge(sd
, 0x143f, 0x01);
452 return (err
< 0) ? err
: 0;