1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#define W9968CF_I2C_BUS_DELAY 4
23
24#define Y_QUANTABLE (&sd->jpeg_hdr[JPEG_QT0_OFFSET])
25#define UV_QUANTABLE (&sd->jpeg_hdr[JPEG_QT1_OFFSET])
26
27static const struct v4l2_pix_format w9968cf_vga_mode[] = {
28 {160, 120, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
29 .bytesperline = 160 * 2,
30 .sizeimage = 160 * 120 * 2,
31 .colorspace = V4L2_COLORSPACE_JPEG},
32 {176, 144, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
33 .bytesperline = 176 * 2,
34 .sizeimage = 176 * 144 * 2,
35 .colorspace = V4L2_COLORSPACE_JPEG},
36 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
37 .bytesperline = 320 * 2,
38 .sizeimage = 320 * 240 * 2,
39 .colorspace = V4L2_COLORSPACE_JPEG},
40 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
41 .bytesperline = 352 * 2,
42 .sizeimage = 352 * 288 * 2,
43 .colorspace = V4L2_COLORSPACE_JPEG},
44 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
45 .bytesperline = 640 * 2,
46 .sizeimage = 640 * 480 * 2,
47 .colorspace = V4L2_COLORSPACE_JPEG},
48};
49
50static void reg_w(struct sd *sd, u16 index, u16 value);
51
52
53
54
55
56static void w9968cf_write_fsb(struct sd *sd, u16* data)
57{
58 struct usb_device *udev = sd->gspca_dev.dev;
59 u16 value;
60 int ret;
61
62 if (sd->gspca_dev.usb_err < 0)
63 return;
64
65 value = *data++;
66 memcpy(sd->gspca_dev.usb_buf, data, 6);
67
68
69 udelay(150);
70 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
71 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
72 value, 0x06, sd->gspca_dev.usb_buf, 6, 500);
73 if (ret < 0) {
74 pr_err("Write FSB registers failed (%d)\n", ret);
75 sd->gspca_dev.usb_err = ret;
76 }
77}
78
79
80
81
82
83static void w9968cf_write_sb(struct sd *sd, u16 value)
84{
85 int ret;
86
87 if (sd->gspca_dev.usb_err < 0)
88 return;
89
90
91 udelay(150);
92
93
94
95 ret = usb_control_msg(sd->gspca_dev.dev,
96 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
97 0,
98 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
99 value, 0x01, NULL, 0, 500);
100
101 udelay(W9968CF_I2C_BUS_DELAY);
102
103 if (ret < 0) {
104 pr_err("Write SB reg [01] %04x failed\n", value);
105 sd->gspca_dev.usb_err = ret;
106 }
107}
108
109
110
111
112
113static int w9968cf_read_sb(struct sd *sd)
114{
115 int ret;
116
117 if (sd->gspca_dev.usb_err < 0)
118 return -1;
119
120
121 udelay(150);
122
123
124
125 ret = usb_control_msg(sd->gspca_dev.dev,
126 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
127 1,
128 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
129 0, 0x01, sd->gspca_dev.usb_buf, 2, 500);
130 if (ret >= 0) {
131 ret = sd->gspca_dev.usb_buf[0] |
132 (sd->gspca_dev.usb_buf[1] << 8);
133 } else {
134 pr_err("Read SB reg [01] failed\n");
135 sd->gspca_dev.usb_err = ret;
136 }
137
138 udelay(W9968CF_I2C_BUS_DELAY);
139
140 return ret;
141}
142
143
144
145
146
147
148static void w9968cf_upload_quantizationtables(struct sd *sd)
149{
150 u16 a, b;
151 int i, j;
152
153 reg_w(sd, 0x39, 0x0010);
154
155 for (i = 0, j = 0; i < 32; i++, j += 2) {
156 a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j + 1]) << 8);
157 b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j + 1]) << 8);
158 reg_w(sd, 0x40 + i, a);
159 reg_w(sd, 0x60 + i, b);
160 }
161 reg_w(sd, 0x39, 0x0012);
162}
163
164
165
166
167
168
169
170
171
172static void w9968cf_smbus_start(struct sd *sd)
173{
174 w9968cf_write_sb(sd, 0x0011);
175 w9968cf_write_sb(sd, 0x0010);
176}
177
178static void w9968cf_smbus_stop(struct sd *sd)
179{
180 w9968cf_write_sb(sd, 0x0010);
181 w9968cf_write_sb(sd, 0x0011);
182 w9968cf_write_sb(sd, 0x0013);
183}
184
185static void w9968cf_smbus_write_byte(struct sd *sd, u8 v)
186{
187 u8 bit;
188 int sda;
189
190 for (bit = 0 ; bit < 8 ; bit++) {
191 sda = (v & 0x80) ? 2 : 0;
192 v <<= 1;
193
194 w9968cf_write_sb(sd, 0x10 | sda);
195
196 w9968cf_write_sb(sd, 0x11 | sda);
197
198 w9968cf_write_sb(sd, 0x10 | sda);
199 }
200}
201
202static void w9968cf_smbus_read_byte(struct sd *sd, u8 *v)
203{
204 u8 bit;
205
206
207
208 *v = 0;
209 for (bit = 0 ; bit < 8 ; bit++) {
210 *v <<= 1;
211
212 w9968cf_write_sb(sd, 0x0013);
213 *v |= (w9968cf_read_sb(sd) & 0x0008) ? 1 : 0;
214
215 w9968cf_write_sb(sd, 0x0012);
216 }
217}
218
219static void w9968cf_smbus_write_nack(struct sd *sd)
220{
221
222
223 w9968cf_write_sb(sd, 0x0013);
224 w9968cf_write_sb(sd, 0x0012);
225}
226
227static void w9968cf_smbus_read_ack(struct sd *sd)
228{
229 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
230 int sda;
231
232
233 w9968cf_write_sb(sd, 0x0012);
234 w9968cf_write_sb(sd, 0x0013);
235 sda = w9968cf_read_sb(sd);
236 w9968cf_write_sb(sd, 0x0012);
237 if (sda >= 0 && (sda & 0x08)) {
238 gspca_dbg(gspca_dev, D_USBI, "Did not receive i2c ACK\n");
239 sd->gspca_dev.usb_err = -EIO;
240 }
241}
242
243
244static void w9968cf_i2c_w(struct sd *sd, u8 reg, u8 value)
245{
246 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
247 u16* data = (u16 *)sd->gspca_dev.usb_buf;
248
249 data[0] = 0x082f | ((sd->sensor_addr & 0x80) ? 0x1500 : 0x0);
250 data[0] |= (sd->sensor_addr & 0x40) ? 0x4000 : 0x0;
251 data[1] = 0x2082 | ((sd->sensor_addr & 0x40) ? 0x0005 : 0x0);
252 data[1] |= (sd->sensor_addr & 0x20) ? 0x0150 : 0x0;
253 data[1] |= (sd->sensor_addr & 0x10) ? 0x5400 : 0x0;
254 data[2] = 0x8208 | ((sd->sensor_addr & 0x08) ? 0x0015 : 0x0);
255 data[2] |= (sd->sensor_addr & 0x04) ? 0x0540 : 0x0;
256 data[2] |= (sd->sensor_addr & 0x02) ? 0x5000 : 0x0;
257 data[3] = 0x1d20 | ((sd->sensor_addr & 0x02) ? 0x0001 : 0x0);
258 data[3] |= (sd->sensor_addr & 0x01) ? 0x0054 : 0x0;
259
260 w9968cf_write_fsb(sd, data);
261
262 data[0] = 0x8208 | ((reg & 0x80) ? 0x0015 : 0x0);
263 data[0] |= (reg & 0x40) ? 0x0540 : 0x0;
264 data[0] |= (reg & 0x20) ? 0x5000 : 0x0;
265 data[1] = 0x0820 | ((reg & 0x20) ? 0x0001 : 0x0);
266 data[1] |= (reg & 0x10) ? 0x0054 : 0x0;
267 data[1] |= (reg & 0x08) ? 0x1500 : 0x0;
268 data[1] |= (reg & 0x04) ? 0x4000 : 0x0;
269 data[2] = 0x2082 | ((reg & 0x04) ? 0x0005 : 0x0);
270 data[2] |= (reg & 0x02) ? 0x0150 : 0x0;
271 data[2] |= (reg & 0x01) ? 0x5400 : 0x0;
272 data[3] = 0x001d;
273
274 w9968cf_write_fsb(sd, data);
275
276 data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
277 data[0] |= (value & 0x40) ? 0x0540 : 0x0;
278 data[0] |= (value & 0x20) ? 0x5000 : 0x0;
279 data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
280 data[1] |= (value & 0x10) ? 0x0054 : 0x0;
281 data[1] |= (value & 0x08) ? 0x1500 : 0x0;
282 data[1] |= (value & 0x04) ? 0x4000 : 0x0;
283 data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
284 data[2] |= (value & 0x02) ? 0x0150 : 0x0;
285 data[2] |= (value & 0x01) ? 0x5400 : 0x0;
286 data[3] = 0xfe1d;
287
288 w9968cf_write_fsb(sd, data);
289
290 gspca_dbg(gspca_dev, D_USBO, "i2c 0x%02x -> [0x%02x]\n", value, reg);
291}
292
293
294static int w9968cf_i2c_r(struct sd *sd, u8 reg)
295{
296 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
297 int ret = 0;
298 u8 value;
299
300
301 w9968cf_write_sb(sd, 0x0013);
302
303 w9968cf_smbus_start(sd);
304 w9968cf_smbus_write_byte(sd, sd->sensor_addr);
305 w9968cf_smbus_read_ack(sd);
306 w9968cf_smbus_write_byte(sd, reg);
307 w9968cf_smbus_read_ack(sd);
308 w9968cf_smbus_stop(sd);
309 w9968cf_smbus_start(sd);
310 w9968cf_smbus_write_byte(sd, sd->sensor_addr + 1);
311 w9968cf_smbus_read_ack(sd);
312 w9968cf_smbus_read_byte(sd, &value);
313
314
315
316 w9968cf_smbus_write_nack(sd);
317 w9968cf_smbus_stop(sd);
318
319
320 w9968cf_write_sb(sd, 0x0030);
321
322 if (sd->gspca_dev.usb_err >= 0) {
323 ret = value;
324 gspca_dbg(gspca_dev, D_USBI, "i2c [0x%02X] -> 0x%02X\n",
325 reg, value);
326 } else
327 gspca_err(gspca_dev, "i2c read [0x%02x] failed\n", reg);
328
329 return ret;
330}
331
332
333
334
335
336static void w9968cf_configure(struct sd *sd)
337{
338 reg_w(sd, 0x00, 0xff00);
339 reg_w(sd, 0x00, 0xbf17);
340 reg_w(sd, 0x00, 0xbf10);
341 reg_w(sd, 0x01, 0x0010);
342 reg_w(sd, 0x01, 0x0000);
343 reg_w(sd, 0x01, 0x0010);
344 reg_w(sd, 0x01, 0x0030);
345
346 sd->stopped = 1;
347}
348
349static void w9968cf_init(struct sd *sd)
350{
351 unsigned long hw_bufsize = sd->sif ? (352 * 288 * 2) : (640 * 480 * 2),
352 y0 = 0x0000,
353 u0 = y0 + hw_bufsize / 2,
354 v0 = u0 + hw_bufsize / 4,
355 y1 = v0 + hw_bufsize / 4,
356 u1 = y1 + hw_bufsize / 2,
357 v1 = u1 + hw_bufsize / 4;
358
359 reg_w(sd, 0x00, 0xff00);
360 reg_w(sd, 0x00, 0xbf10);
361
362 reg_w(sd, 0x03, 0x405d);
363 reg_w(sd, 0x04, 0x0030);
364
365 reg_w(sd, 0x20, y0 & 0xffff);
366 reg_w(sd, 0x21, y0 >> 16);
367 reg_w(sd, 0x24, u0 & 0xffff);
368 reg_w(sd, 0x25, u0 >> 16);
369 reg_w(sd, 0x28, v0 & 0xffff);
370 reg_w(sd, 0x29, v0 >> 16);
371
372 reg_w(sd, 0x22, y1 & 0xffff);
373 reg_w(sd, 0x23, y1 >> 16);
374 reg_w(sd, 0x26, u1 & 0xffff);
375 reg_w(sd, 0x27, u1 >> 16);
376 reg_w(sd, 0x2a, v1 & 0xffff);
377 reg_w(sd, 0x2b, v1 >> 16);
378
379 reg_w(sd, 0x32, y1 & 0xffff);
380 reg_w(sd, 0x33, y1 >> 16);
381
382 reg_w(sd, 0x34, y1 & 0xffff);
383 reg_w(sd, 0x35, y1 >> 16);
384
385 reg_w(sd, 0x36, 0x0000);
386 reg_w(sd, 0x37, 0x0804);
387 reg_w(sd, 0x38, 0x0000);
388 reg_w(sd, 0x3f, 0x0000);
389}
390
391static void w9968cf_set_crop_window(struct sd *sd)
392{
393 int start_cropx, start_cropy, x, y, fw, fh, cw, ch,
394 max_width, max_height;
395
396 if (sd->sif) {
397 max_width = 352;
398 max_height = 288;
399 } else {
400 max_width = 640;
401 max_height = 480;
402 }
403
404 if (sd->sensor == SEN_OV7620) {
405
406
407
408
409
410
411
412 if (sd->freq->val == 1) {
413 start_cropx = 277;
414 start_cropy = 37;
415 } else {
416 start_cropx = 105;
417 start_cropy = 37;
418 }
419 } else {
420 start_cropx = 320;
421 start_cropy = 35;
422 }
423
424
425 #define SC(x) ((x) << 10)
426
427
428 fw = SC(sd->gspca_dev.pixfmt.width) / max_width;
429 fh = SC(sd->gspca_dev.pixfmt.height) / max_height;
430
431 cw = (fw >= fh) ? max_width : SC(sd->gspca_dev.pixfmt.width) / fh;
432 ch = (fw >= fh) ? SC(sd->gspca_dev.pixfmt.height) / fw : max_height;
433
434 sd->sensor_width = max_width;
435 sd->sensor_height = max_height;
436
437 x = (max_width - cw) / 2;
438 y = (max_height - ch) / 2;
439
440 reg_w(sd, 0x10, start_cropx + x);
441 reg_w(sd, 0x11, start_cropy + y);
442 reg_w(sd, 0x12, start_cropx + x + cw);
443 reg_w(sd, 0x13, start_cropy + y + ch);
444}
445
446static void w9968cf_mode_init_regs(struct sd *sd)
447{
448 int val, vs_polarity, hs_polarity;
449
450 w9968cf_set_crop_window(sd);
451
452 reg_w(sd, 0x14, sd->gspca_dev.pixfmt.width);
453 reg_w(sd, 0x15, sd->gspca_dev.pixfmt.height);
454
455
456 reg_w(sd, 0x30, sd->gspca_dev.pixfmt.width);
457 reg_w(sd, 0x31, sd->gspca_dev.pixfmt.height);
458
459
460 if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==
461 V4L2_PIX_FMT_JPEG) {
462 reg_w(sd, 0x2c, sd->gspca_dev.pixfmt.width / 2);
463 reg_w(sd, 0x2d, sd->gspca_dev.pixfmt.width / 4);
464 } else
465 reg_w(sd, 0x2c, sd->gspca_dev.pixfmt.width);
466
467 reg_w(sd, 0x00, 0xbf17);
468 reg_w(sd, 0x00, 0xbf10);
469
470
471 val = sd->gspca_dev.pixfmt.width * sd->gspca_dev.pixfmt.height;
472 reg_w(sd, 0x3d, val & 0xffff);
473 reg_w(sd, 0x3e, val >> 16);
474
475 if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==
476 V4L2_PIX_FMT_JPEG) {
477
478 jpeg_define(sd->jpeg_hdr, sd->gspca_dev.pixfmt.height,
479 sd->gspca_dev.pixfmt.width, 0x22);
480 jpeg_set_qual(sd->jpeg_hdr, v4l2_ctrl_g_ctrl(sd->jpegqual));
481 w9968cf_upload_quantizationtables(sd);
482 v4l2_ctrl_grab(sd->jpegqual, true);
483 }
484
485
486 if (sd->sensor == SEN_OV7620) {
487
488 vs_polarity = 1;
489 hs_polarity = 1;
490 } else {
491 vs_polarity = 1;
492 hs_polarity = 0;
493 }
494
495 val = (vs_polarity << 12) | (hs_polarity << 11);
496
497
498
499
500
501 if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==
502 V4L2_PIX_FMT_JPEG) {
503
504 val |= 0x0003;
505 } else
506 val |= 0x0080;
507
508
509
510
511
512 val |= 0x8000;
513
514 reg_w(sd, 0x16, val);
515
516 sd->gspca_dev.empty_packet = 0;
517}
518
519static void w9968cf_stop0(struct sd *sd)
520{
521 v4l2_ctrl_grab(sd->jpegqual, false);
522 reg_w(sd, 0x39, 0x0000);
523 reg_w(sd, 0x16, 0x0000);
524}
525
526
527
528
529
530
531
532
533
534static void w9968cf_pkt_scan(struct gspca_dev *gspca_dev,
535 u8 *data,
536 int len)
537{
538 struct sd *sd = (struct sd *) gspca_dev;
539
540 if (w9968cf_vga_mode[gspca_dev->curr_mode].pixelformat ==
541 V4L2_PIX_FMT_JPEG) {
542 if (len >= 2 &&
543 data[0] == 0xff &&
544 data[1] == 0xd8) {
545 gspca_frame_add(gspca_dev, LAST_PACKET,
546 NULL, 0);
547 gspca_frame_add(gspca_dev, FIRST_PACKET,
548 sd->jpeg_hdr, JPEG_HDR_SZ);
549
550
551 len -= 2;
552 data += 2;
553 }
554 } else {
555
556 if (gspca_dev->empty_packet) {
557 gspca_frame_add(gspca_dev, LAST_PACKET,
558 NULL, 0);
559 gspca_frame_add(gspca_dev, FIRST_PACKET,
560 NULL, 0);
561 gspca_dev->empty_packet = 0;
562 }
563 }
564 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
565}
566