OMAP3 SRF: Generic shared resource f/w
[linux-ginger.git] / drivers / media / video / gspca / pac207.c
blob96659433d2488d901b112bc56ef015f763f58871
1 /*
2 * Pixart PAC207BCA library
4 * Copyright (C) 2008 Hans de Goede <hdgoede@redhat.com>
5 * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
6 * Copyleft (C) 2005 Michel Xhaard mxhaard@magic.fr
8 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #define MODULE_NAME "pac207"
28 #include "gspca.h"
30 MODULE_AUTHOR("Hans de Goede <hdgoede@redhat.com>");
31 MODULE_DESCRIPTION("Pixart PAC207");
32 MODULE_LICENSE("GPL");
34 #define PAC207_CTRL_TIMEOUT 100 /* ms */
36 #define PAC207_BRIGHTNESS_MIN 0
37 #define PAC207_BRIGHTNESS_MAX 255
38 #define PAC207_BRIGHTNESS_DEFAULT 46
40 #define PAC207_EXPOSURE_MIN 3
41 #define PAC207_EXPOSURE_MAX 26
42 #define PAC207_EXPOSURE_DEFAULT 5 /* power on default: 3 */
43 #define PAC207_EXPOSURE_KNEE 8 /* 4 = 30 fps, 11 = 8, 15 = 6 */
45 #define PAC207_GAIN_MIN 0
46 #define PAC207_GAIN_MAX 31
47 #define PAC207_GAIN_DEFAULT 9 /* power on default: 9 */
48 #define PAC207_GAIN_KNEE 31
50 #define PAC207_AUTOGAIN_DEADZONE 30
52 /* specific webcam descriptor */
53 struct sd {
54 struct gspca_dev gspca_dev; /* !! must be the first item */
56 u8 mode;
58 u8 brightness;
59 u8 exposure;
60 u8 autogain;
61 u8 gain;
63 u8 sof_read;
64 u8 header_read;
65 u8 autogain_ignore_frames;
67 atomic_t avg_lum;
70 /* V4L2 controls supported by the driver */
71 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
72 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
73 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
74 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
75 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
76 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
77 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
78 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
80 static struct ctrl sd_ctrls[] = {
81 #define SD_BRIGHTNESS 0
84 .id = V4L2_CID_BRIGHTNESS,
85 .type = V4L2_CTRL_TYPE_INTEGER,
86 .name = "Brightness",
87 .minimum = PAC207_BRIGHTNESS_MIN,
88 .maximum = PAC207_BRIGHTNESS_MAX,
89 .step = 1,
90 .default_value = PAC207_BRIGHTNESS_DEFAULT,
91 .flags = 0,
93 .set = sd_setbrightness,
94 .get = sd_getbrightness,
96 #define SD_EXPOSURE 1
99 .id = V4L2_CID_EXPOSURE,
100 .type = V4L2_CTRL_TYPE_INTEGER,
101 .name = "exposure",
102 .minimum = PAC207_EXPOSURE_MIN,
103 .maximum = PAC207_EXPOSURE_MAX,
104 .step = 1,
105 .default_value = PAC207_EXPOSURE_DEFAULT,
106 .flags = 0,
108 .set = sd_setexposure,
109 .get = sd_getexposure,
111 #define SD_AUTOGAIN 2
114 .id = V4L2_CID_AUTOGAIN,
115 .type = V4L2_CTRL_TYPE_BOOLEAN,
116 .name = "Auto Gain",
117 .minimum = 0,
118 .maximum = 1,
119 .step = 1,
120 #define AUTOGAIN_DEF 1
121 .default_value = AUTOGAIN_DEF,
122 .flags = 0,
124 .set = sd_setautogain,
125 .get = sd_getautogain,
127 #define SD_GAIN 3
130 .id = V4L2_CID_GAIN,
131 .type = V4L2_CTRL_TYPE_INTEGER,
132 .name = "gain",
133 .minimum = PAC207_GAIN_MIN,
134 .maximum = PAC207_GAIN_MAX,
135 .step = 1,
136 .default_value = PAC207_GAIN_DEFAULT,
137 .flags = 0,
139 .set = sd_setgain,
140 .get = sd_getgain,
144 static const struct v4l2_pix_format sif_mode[] = {
145 {176, 144, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE,
146 .bytesperline = 176,
147 .sizeimage = (176 + 2) * 144,
148 /* uncompressed, add 2 bytes / line for line header */
149 .colorspace = V4L2_COLORSPACE_SRGB,
150 .priv = 1},
151 {352, 288, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE,
152 .bytesperline = 352,
153 /* compressed, but only when needed (not compressed
154 when the framerate is low) */
155 .sizeimage = (352 + 2) * 288,
156 .colorspace = V4L2_COLORSPACE_SRGB,
157 .priv = 0},
160 static const __u8 pac207_sensor_init[][8] = {
161 {0x10, 0x12, 0x0d, 0x12, 0x0c, 0x01, 0x29, 0x84},
162 {0x49, 0x64, 0x64, 0x64, 0x04, 0x10, 0xf0, 0x30},
163 {0x00, 0x00, 0x00, 0x70, 0xa0, 0xf8, 0x00, 0x00},
164 {0x32, 0x00, 0x96, 0x00, 0xA2, 0x02, 0xaf, 0x00},
167 static int pac207_write_regs(struct gspca_dev *gspca_dev, u16 index,
168 const u8 *buffer, u16 length)
170 struct usb_device *udev = gspca_dev->dev;
171 int err;
173 memcpy(gspca_dev->usb_buf, buffer, length);
175 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x01,
176 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
177 0x00, index,
178 gspca_dev->usb_buf, length, PAC207_CTRL_TIMEOUT);
179 if (err < 0)
180 PDEBUG(D_ERR,
181 "Failed to write registers to index 0x%04X, error %d)",
182 index, err);
184 return err;
188 static int pac207_write_reg(struct gspca_dev *gspca_dev, u16 index, u16 value)
190 struct usb_device *udev = gspca_dev->dev;
191 int err;
193 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00,
194 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
195 value, index, NULL, 0, PAC207_CTRL_TIMEOUT);
196 if (err)
197 PDEBUG(D_ERR, "Failed to write a register (index 0x%04X,"
198 " value 0x%02X, error %d)", index, value, err);
200 return err;
203 static int pac207_read_reg(struct gspca_dev *gspca_dev, u16 index)
205 struct usb_device *udev = gspca_dev->dev;
206 int res;
208 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00,
209 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
210 0x00, index,
211 gspca_dev->usb_buf, 1, PAC207_CTRL_TIMEOUT);
212 if (res < 0) {
213 PDEBUG(D_ERR,
214 "Failed to read a register (index 0x%04X, error %d)",
215 index, res);
216 return res;
219 return gspca_dev->usb_buf[0];
222 /* this function is called at probe time */
223 static int sd_config(struct gspca_dev *gspca_dev,
224 const struct usb_device_id *id)
226 struct sd *sd = (struct sd *) gspca_dev;
227 struct cam *cam;
228 u8 idreg[2];
230 idreg[0] = pac207_read_reg(gspca_dev, 0x0000);
231 idreg[1] = pac207_read_reg(gspca_dev, 0x0001);
232 idreg[0] = ((idreg[0] & 0x0F) << 4) | ((idreg[1] & 0xf0) >> 4);
233 idreg[1] = idreg[1] & 0x0f;
234 PDEBUG(D_PROBE, "Pixart Sensor ID 0x%02X Chips ID 0x%02X",
235 idreg[0], idreg[1]);
237 if (idreg[0] != 0x27) {
238 PDEBUG(D_PROBE, "Error invalid sensor ID!");
239 return -ENODEV;
242 PDEBUG(D_PROBE,
243 "Pixart PAC207BCA Image Processor and Control Chip detected"
244 " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
246 cam = &gspca_dev->cam;
247 cam->cam_mode = sif_mode;
248 cam->nmodes = ARRAY_SIZE(sif_mode);
249 sd->brightness = PAC207_BRIGHTNESS_DEFAULT;
250 sd->exposure = PAC207_EXPOSURE_DEFAULT;
251 sd->gain = PAC207_GAIN_DEFAULT;
252 sd->autogain = AUTOGAIN_DEF;
254 return 0;
257 /* this function is called at probe and resume time */
258 static int sd_init(struct gspca_dev *gspca_dev)
260 pac207_write_reg(gspca_dev, 0x41, 0x00);
261 /* Bit_0=Image Format,
262 * Bit_1=LED,
263 * Bit_2=Compression test mode enable */
264 pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
266 return 0;
269 /* -- start the camera -- */
270 static int sd_start(struct gspca_dev *gspca_dev)
272 struct sd *sd = (struct sd *) gspca_dev;
273 __u8 mode;
275 pac207_write_reg(gspca_dev, 0x0f, 0x10); /* Power control (Bit 6-0) */
276 pac207_write_regs(gspca_dev, 0x0002, pac207_sensor_init[0], 8);
277 pac207_write_regs(gspca_dev, 0x000a, pac207_sensor_init[1], 8);
278 pac207_write_regs(gspca_dev, 0x0012, pac207_sensor_init[2], 8);
279 pac207_write_regs(gspca_dev, 0x0042, pac207_sensor_init[3], 8);
281 /* Compression Balance */
282 if (gspca_dev->width == 176)
283 pac207_write_reg(gspca_dev, 0x4a, 0xff);
284 else
285 pac207_write_reg(gspca_dev, 0x4a, 0x30);
286 pac207_write_reg(gspca_dev, 0x4b, 0x00); /* Sram test value */
287 pac207_write_reg(gspca_dev, 0x08, sd->brightness);
289 /* PGA global gain (Bit 4-0) */
290 pac207_write_reg(gspca_dev, 0x0e, sd->gain);
291 pac207_write_reg(gspca_dev, 0x02, sd->exposure); /* PXCK = 12MHz /n */
293 mode = 0x02; /* Image Format (Bit 0), LED (1), Compr. test mode (2) */
294 if (gspca_dev->width == 176) { /* 176x144 */
295 mode |= 0x01;
296 PDEBUG(D_STREAM, "pac207_start mode 176x144");
297 } else { /* 352x288 */
298 PDEBUG(D_STREAM, "pac207_start mode 352x288");
300 pac207_write_reg(gspca_dev, 0x41, mode);
302 pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
303 pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
304 msleep(10);
305 pac207_write_reg(gspca_dev, 0x40, 0x01); /* Start ISO pipe */
307 sd->sof_read = 0;
308 sd->autogain_ignore_frames = 0;
309 atomic_set(&sd->avg_lum, -1);
310 return 0;
313 static void sd_stopN(struct gspca_dev *gspca_dev)
315 pac207_write_reg(gspca_dev, 0x40, 0x00); /* Stop ISO pipe */
316 pac207_write_reg(gspca_dev, 0x41, 0x00); /* Turn of LED */
317 pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
320 /* Include pac common sof detection functions */
321 #include "pac_common.h"
323 static void pac207_do_auto_gain(struct gspca_dev *gspca_dev)
325 struct sd *sd = (struct sd *) gspca_dev;
326 int avg_lum = atomic_read(&sd->avg_lum);
328 if (avg_lum == -1)
329 return;
331 if (sd->autogain_ignore_frames > 0)
332 sd->autogain_ignore_frames--;
333 else if (gspca_auto_gain_n_exposure(gspca_dev, avg_lum,
334 100, PAC207_AUTOGAIN_DEADZONE,
335 PAC207_GAIN_KNEE, PAC207_EXPOSURE_KNEE))
336 sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
339 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
340 struct gspca_frame *frame,
341 __u8 *data,
342 int len)
344 struct sd *sd = (struct sd *) gspca_dev;
345 unsigned char *sof;
347 sof = pac_find_sof(gspca_dev, data, len);
348 if (sof) {
349 int n;
351 /* finish decoding current frame */
352 n = sof - data;
353 if (n > sizeof pac_sof_marker)
354 n -= sizeof pac_sof_marker;
355 else
356 n = 0;
357 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
358 data, n);
359 sd->header_read = 0;
360 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, NULL, 0);
361 len -= sof - data;
362 data = sof;
364 if (sd->header_read < 11) {
365 int needed;
367 /* get average lumination from frame header (byte 5) */
368 if (sd->header_read < 5) {
369 needed = 5 - sd->header_read;
370 if (len >= needed)
371 atomic_set(&sd->avg_lum, data[needed - 1]);
373 /* skip the rest of the header */
374 needed = 11 - sd->header_read;
375 if (len <= needed) {
376 sd->header_read += len;
377 return;
379 data += needed;
380 len -= needed;
381 sd->header_read = 11;
384 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
387 static void setbrightness(struct gspca_dev *gspca_dev)
389 struct sd *sd = (struct sd *) gspca_dev;
391 pac207_write_reg(gspca_dev, 0x08, sd->brightness);
392 pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
393 pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
396 static void setexposure(struct gspca_dev *gspca_dev)
398 struct sd *sd = (struct sd *) gspca_dev;
400 pac207_write_reg(gspca_dev, 0x02, sd->exposure);
401 pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
402 pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
405 static void setgain(struct gspca_dev *gspca_dev)
407 struct sd *sd = (struct sd *) gspca_dev;
409 pac207_write_reg(gspca_dev, 0x0e, sd->gain);
410 pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
411 pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
414 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
416 struct sd *sd = (struct sd *) gspca_dev;
418 sd->brightness = val;
419 if (gspca_dev->streaming)
420 setbrightness(gspca_dev);
421 return 0;
424 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
426 struct sd *sd = (struct sd *) gspca_dev;
428 *val = sd->brightness;
429 return 0;
432 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
434 struct sd *sd = (struct sd *) gspca_dev;
436 sd->exposure = val;
437 if (gspca_dev->streaming)
438 setexposure(gspca_dev);
439 return 0;
442 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
444 struct sd *sd = (struct sd *) gspca_dev;
446 *val = sd->exposure;
447 return 0;
450 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
452 struct sd *sd = (struct sd *) gspca_dev;
454 sd->gain = val;
455 if (gspca_dev->streaming)
456 setgain(gspca_dev);
457 return 0;
460 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
462 struct sd *sd = (struct sd *) gspca_dev;
464 *val = sd->gain;
465 return 0;
468 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
470 struct sd *sd = (struct sd *) gspca_dev;
472 sd->autogain = val;
473 /* when switching to autogain set defaults to make sure
474 we are on a valid point of the autogain gain /
475 exposure knee graph, and give this change time to
476 take effect before doing autogain. */
477 if (sd->autogain) {
478 sd->exposure = PAC207_EXPOSURE_DEFAULT;
479 sd->gain = PAC207_GAIN_DEFAULT;
480 if (gspca_dev->streaming) {
481 sd->autogain_ignore_frames =
482 PAC_AUTOGAIN_IGNORE_FRAMES;
483 setexposure(gspca_dev);
484 setgain(gspca_dev);
488 return 0;
491 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
493 struct sd *sd = (struct sd *) gspca_dev;
495 *val = sd->autogain;
496 return 0;
499 /* sub-driver description */
500 static const struct sd_desc sd_desc = {
501 .name = MODULE_NAME,
502 .ctrls = sd_ctrls,
503 .nctrls = ARRAY_SIZE(sd_ctrls),
504 .config = sd_config,
505 .init = sd_init,
506 .start = sd_start,
507 .stopN = sd_stopN,
508 .dq_callback = pac207_do_auto_gain,
509 .pkt_scan = sd_pkt_scan,
512 /* -- module initialisation -- */
513 static const __devinitdata struct usb_device_id device_table[] = {
514 {USB_DEVICE(0x041e, 0x4028)},
515 {USB_DEVICE(0x093a, 0x2460)},
516 {USB_DEVICE(0x093a, 0x2461)},
517 {USB_DEVICE(0x093a, 0x2463)},
518 {USB_DEVICE(0x093a, 0x2464)},
519 {USB_DEVICE(0x093a, 0x2468)},
520 {USB_DEVICE(0x093a, 0x2470)},
521 {USB_DEVICE(0x093a, 0x2471)},
522 {USB_DEVICE(0x093a, 0x2472)},
523 {USB_DEVICE(0x093a, 0x2474)},
524 {USB_DEVICE(0x093a, 0x2476)},
525 {USB_DEVICE(0x145f, 0x013a)},
526 {USB_DEVICE(0x2001, 0xf115)},
529 MODULE_DEVICE_TABLE(usb, device_table);
531 /* -- device connect -- */
532 static int sd_probe(struct usb_interface *intf,
533 const struct usb_device_id *id)
535 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
536 THIS_MODULE);
539 static struct usb_driver sd_driver = {
540 .name = MODULE_NAME,
541 .id_table = device_table,
542 .probe = sd_probe,
543 .disconnect = gspca_disconnect,
544 #ifdef CONFIG_PM
545 .suspend = gspca_suspend,
546 .resume = gspca_resume,
547 #endif
550 /* -- module insert / remove -- */
551 static int __init sd_mod_init(void)
553 int ret;
554 ret = usb_register(&sd_driver);
555 if (ret < 0)
556 return ret;
557 PDEBUG(D_PROBE, "registered");
558 return 0;
560 static void __exit sd_mod_exit(void)
562 usb_deregister(&sd_driver);
563 PDEBUG(D_PROBE, "deregistered");
566 module_init(sd_mod_init);
567 module_exit(sd_mod_exit);