]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/go7007/go7007-driver.c
Staging: add the go7007 video driver
[net-next-2.6.git] / drivers / staging / go7007 / go7007-driver.c
CommitLineData
866b8695
GKH
1/*
2 * Copyright (C) 2005-2006 Micronas USA Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
16 */
17
18#include <linux/module.h>
19#include <linux/version.h>
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/sched.h>
23#include <linux/spinlock.h>
24#include <linux/unistd.h>
25#include <linux/time.h>
26#include <linux/mm.h>
27#include <linux/vmalloc.h>
28#include <linux/device.h>
29#include <linux/i2c.h>
30#include <linux/firmware.h>
31#include <linux/semaphore.h>
32#include <linux/uaccess.h>
33#include <asm/system.h>
34#include <linux/videodev.h>
35#include <media/tuner.h>
36#include <media/v4l2-common.h>
37
38#include "go7007-priv.h"
39#include "wis-i2c.h"
40
41/*
42 * Wait for an interrupt to be delivered from the GO7007SB and return
43 * the associated value and data.
44 *
45 * Must be called with the hw_lock held.
46 */
47int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data)
48{
49 go->interrupt_available = 0;
50 go->hpi_ops->read_interrupt(go);
51 if (wait_event_timeout(go->interrupt_waitq,
52 go->interrupt_available, 5*HZ) < 0) {
53 printk(KERN_ERR "go7007: timeout waiting for read interrupt\n");
54 return -1;
55 }
56 if (!go->interrupt_available)
57 return -1;
58 go->interrupt_available = 0;
59 *value = go->interrupt_value & 0xfffe;
60 *data = go->interrupt_data;
61 return 0;
62}
63EXPORT_SYMBOL(go7007_read_interrupt);
64
65/*
66 * Read a register/address on the GO7007SB.
67 *
68 * Must be called with the hw_lock held.
69 */
70int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data)
71{
72 int count = 100;
73 u16 value;
74
75 if (go7007_write_interrupt(go, 0x0010, addr) < 0)
76 return -EIO;
77 while (count-- > 0) {
78 if (go7007_read_interrupt(go, &value, data) == 0 &&
79 value == 0xa000)
80 return 0;
81 }
82 return -EIO;
83}
84EXPORT_SYMBOL(go7007_read_addr);
85
86/*
87 * Send the boot firmware to the encoder, which just wakes it up and lets
88 * us talk to the GPIO pins and on-board I2C adapter.
89 *
90 * Must be called with the hw_lock held.
91 */
92static int go7007_load_encoder(struct go7007 *go)
93{
94 const struct firmware *fw_entry;
95 char fw_name[] = "go7007fw.bin";
96 void *bounce;
97 int fw_len, rv = 0;
98 u16 intr_val, intr_data;
99
100 if (request_firmware(&fw_entry, fw_name, go->dev)) {
101 printk(KERN_ERR
102 "go7007: unable to load firmware from file \"%s\"\n",
103 fw_name);
104 return -1;
105 }
106 if (fw_entry->size < 16 || memcmp(fw_entry->data, "WISGO7007FW", 11)) {
107 printk(KERN_ERR "go7007: file \"%s\" does not appear to be "
108 "go7007 firmware\n", fw_name);
109 release_firmware(fw_entry);
110 return -1;
111 }
112 fw_len = fw_entry->size - 16;
113 bounce = kmalloc(fw_len, GFP_KERNEL);
114 if (bounce == NULL) {
115 printk(KERN_ERR "go7007: unable to allocate %d bytes for "
116 "firmware transfer\n", fw_len);
117 release_firmware(fw_entry);
118 return -1;
119 }
120 memcpy(bounce, fw_entry->data + 16, fw_len);
121 release_firmware(fw_entry);
122 if (go7007_interface_reset(go) < 0 ||
123 go7007_send_firmware(go, bounce, fw_len) < 0 ||
124 go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
125 (intr_val & ~0x1) != 0x5a5a) {
126 printk(KERN_ERR "go7007: error transferring firmware\n");
127 rv = -1;
128 }
129 kfree(bounce);
130 return rv;
131}
132
133/*
134 * Boot the encoder and register the I2C adapter if requested. Do the
135 * minimum initialization necessary, since the board-specific code may
136 * still need to probe the board ID.
137 *
138 * Must NOT be called with the hw_lock held.
139 */
140int go7007_boot_encoder(struct go7007 *go, int init_i2c)
141{
142 int ret;
143
144 down(&go->hw_lock);
145 ret = go7007_load_encoder(go);
146 up(&go->hw_lock);
147 if (ret < 0)
148 return -1;
149 if (!init_i2c)
150 return 0;
151 if (go7007_i2c_init(go) < 0)
152 return -1;
153 go->i2c_adapter_online = 1;
154 return 0;
155}
156EXPORT_SYMBOL(go7007_boot_encoder);
157
158/*
159 * Configure any hardware-related registers in the GO7007, such as GPIO
160 * pins and bus parameters, which are board-specific. This assumes
161 * the boot firmware has already been downloaded.
162 *
163 * Must be called with the hw_lock held.
164 */
165static int go7007_init_encoder(struct go7007 *go)
166{
167 if (go->board_info->audio_flags & GO7007_AUDIO_I2S_MASTER) {
168 go7007_write_addr(go, 0x1000, 0x0811);
169 go7007_write_addr(go, 0x1000, 0x0c11);
170 }
171 if (go->board_id == GO7007_BOARDID_MATRIX_REV) {
172 /* Set GPIO pin 0 to be an output (audio clock control) */
173 go7007_write_addr(go, 0x3c82, 0x0001);
174 go7007_write_addr(go, 0x3c80, 0x00fe);
175 }
176 return 0;
177}
178
179/*
180 * Send the boot firmware to the GO7007 and configure the registers. This
181 * is the only way to stop the encoder once it has started streaming video.
182 *
183 * Must be called with the hw_lock held.
184 */
185int go7007_reset_encoder(struct go7007 *go)
186{
187 if (go7007_load_encoder(go) < 0)
188 return -1;
189 return go7007_init_encoder(go);
190}
191
192/*
193 * Attempt to instantiate an I2C client by ID, probably loading a module.
194 */
195static int init_i2c_module(struct i2c_adapter *adapter, int id, int addr)
196{
197 char *modname;
198
199 switch (id) {
200 case I2C_DRIVERID_WIS_SAA7115:
201 modname = "wis-saa7115";
202 break;
203 case I2C_DRIVERID_WIS_SAA7113:
204 modname = "wis-saa7113";
205 break;
206 case I2C_DRIVERID_WIS_UDA1342:
207 modname = "wis-uda1342";
208 break;
209 case I2C_DRIVERID_WIS_SONY_TUNER:
210 modname = "wis-sony-tuner";
211 break;
212 case I2C_DRIVERID_WIS_TW9903:
213 modname = "wis-tw9903";
214 break;
215 case I2C_DRIVERID_WIS_TW2804:
216 modname = "wis-tw2804";
217 break;
218 case I2C_DRIVERID_WIS_OV7640:
219 modname = "wis-ov7640";
220 break;
221 default:
222 modname = NULL;
223 break;
224 }
225 if (modname != NULL)
226 request_module(modname);
227 if (wis_i2c_probe_device(adapter, id, addr) == 1)
228 return 0;
229 if (modname != NULL)
230 printk(KERN_INFO
231 "go7007: probing for module %s failed", modname);
232 else
233 printk(KERN_INFO
234 "go7007: sensor %u seems to be unsupported!\n", id);
235 return -1;
236}
237
238/*
239 * Finalize the GO7007 hardware setup, register the on-board I2C adapter
240 * (if used on this board), load the I2C client driver for the sensor
241 * (SAA7115 or whatever) and other devices, and register the ALSA and V4L2
242 * interfaces.
243 *
244 * Must NOT be called with the hw_lock held.
245 */
246int go7007_register_encoder(struct go7007 *go)
247{
248 int i, ret;
249
250 printk(KERN_INFO "go7007: registering new %s\n", go->name);
251
252 down(&go->hw_lock);
253 ret = go7007_init_encoder(go);
254 up(&go->hw_lock);
255 if (ret < 0)
256 return -1;
257
258 if (!go->i2c_adapter_online &&
259 go->board_info->flags & GO7007_BOARD_USE_ONBOARD_I2C) {
260 if (go7007_i2c_init(go) < 0)
261 return -1;
262 go->i2c_adapter_online = 1;
263 }
264 if (go->i2c_adapter_online) {
265 for (i = 0; i < go->board_info->num_i2c_devs; ++i)
266 init_i2c_module(&go->i2c_adapter,
267 go->board_info->i2c_devs[i].id,
268 go->board_info->i2c_devs[i].addr);
269#ifdef TUNER_SET_TYPE_ADDR
270 if (go->tuner_type >= 0) {
271 struct tuner_setup tun_setup = {
272 .mode_mask = T_ANALOG_TV,
273 .addr = ADDR_UNSET,
274 .type = go->tuner_type
275 };
276 i2c_clients_command(&go->i2c_adapter,
277 TUNER_SET_TYPE_ADDR, &tun_setup);
278 }
279#else
280 if (go->tuner_type >= 0)
281 i2c_clients_command(&go->i2c_adapter,
282 TUNER_SET_TYPE, &go->tuner_type);
283#endif
284 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24)
285 i2c_clients_command(&go->i2c_adapter,
286 DECODER_SET_CHANNEL, &go->channel_number);
287 }
288 if (go->board_info->flags & GO7007_BOARD_HAS_AUDIO) {
289 go->audio_enabled = 1;
290 go7007_snd_init(go);
291 }
292 return go7007_v4l2_init(go);
293}
294EXPORT_SYMBOL(go7007_register_encoder);
295
296/*
297 * Send the encode firmware to the encoder, which will cause it
298 * to immediately start delivering the video and audio streams.
299 *
300 * Must be called with the hw_lock held.
301 */
302int go7007_start_encoder(struct go7007 *go)
303{
304 u8 *fw;
305 int fw_len, rv = 0, i;
306 u16 intr_val, intr_data;
307
308 go->modet_enable = 0;
309 if (!go->dvd_mode)
310 for (i = 0; i < 4; ++i) {
311 if (go->modet[i].enable) {
312 go->modet_enable = 1;
313 continue;
314 }
315 go->modet[i].pixel_threshold = 32767;
316 go->modet[i].motion_threshold = 32767;
317 go->modet[i].mb_threshold = 32767;
318 }
319
320 if (go7007_construct_fw_image(go, &fw, &fw_len) < 0)
321 return -1;
322
323 if (go7007_send_firmware(go, fw, fw_len) < 0 ||
324 go7007_read_interrupt(go, &intr_val, &intr_data) < 0) {
325 printk(KERN_ERR "go7007: error transferring firmware\n");
326 rv = -1;
327 goto start_error;
328 }
329
330 go->state = STATE_DATA;
331 go->parse_length = 0;
332 go->seen_frame = 0;
333 if (go7007_stream_start(go) < 0) {
334 printk(KERN_ERR "go7007: error starting stream transfer\n");
335 rv = -1;
336 goto start_error;
337 }
338
339start_error:
340 kfree(fw);
341 return rv;
342}
343
344/*
345 * Store a byte in the current video buffer, if there is one.
346 */
347static inline void store_byte(struct go7007_buffer *gobuf, u8 byte)
348{
349 if (gobuf != NULL && gobuf->bytesused < GO7007_BUF_SIZE) {
350 unsigned int pgidx = gobuf->offset >> PAGE_SHIFT;
351 unsigned int pgoff = gobuf->offset & ~PAGE_MASK;
352
353 *((u8 *)page_address(gobuf->pages[pgidx]) + pgoff) = byte;
354 ++gobuf->offset;
355 ++gobuf->bytesused;
356 }
357}
358
359/*
360 * Deliver the last video buffer and get a new one to start writing to.
361 */
362static void frame_boundary(struct go7007 *go)
363{
364 struct go7007_buffer *gobuf;
365 int i;
366
367 if (go->active_buf) {
368 if (go->active_buf->modet_active) {
369 if (go->active_buf->bytesused + 216 < GO7007_BUF_SIZE) {
370 for (i = 0; i < 216; ++i)
371 store_byte(go->active_buf,
372 go->active_map[i]);
373 go->active_buf->bytesused -= 216;
374 } else
375 go->active_buf->modet_active = 0;
376 }
377 go->active_buf->state = BUF_STATE_DONE;
378 wake_up_interruptible(&go->frame_waitq);
379 go->active_buf = NULL;
380 }
381 list_for_each_entry(gobuf, &go->stream, stream)
382 if (gobuf->state == BUF_STATE_QUEUED) {
383 gobuf->seq = go->next_seq;
384 do_gettimeofday(&gobuf->timestamp);
385 go->active_buf = gobuf;
386 break;
387 }
388 ++go->next_seq;
389}
390
391static void write_bitmap_word(struct go7007 *go)
392{
393 int x, y, i, stride = ((go->width >> 4) + 7) >> 3;
394
395 for (i = 0; i < 16; ++i) {
396 y = (((go->parse_length - 1) << 3) + i) / (go->width >> 4);
397 x = (((go->parse_length - 1) << 3) + i) % (go->width >> 4);
398 go->active_map[stride * y + (x >> 3)] |=
399 (go->modet_word & 1) << (x & 0x7);
400 go->modet_word >>= 1;
401 }
402}
403
404/*
405 * Parse a chunk of the video stream into frames. The frames are not
406 * delimited by the hardware, so we have to parse the frame boundaries
407 * based on the type of video stream we're receiving.
408 */
409void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length)
410{
411 int i, seq_start_code = -1, frame_start_code = -1;
412
413 spin_lock(&go->spinlock);
414
415 switch (go->format) {
416 case GO7007_FORMAT_MPEG4:
417 seq_start_code = 0xB0;
418 frame_start_code = 0xB6;
419 break;
420 case GO7007_FORMAT_MPEG1:
421 case GO7007_FORMAT_MPEG2:
422 seq_start_code = 0xB3;
423 frame_start_code = 0x00;
424 break;
425 }
426
427 for (i = 0; i < length; ++i) {
428 if (go->active_buf != NULL &&
429 go->active_buf->bytesused >= GO7007_BUF_SIZE - 3) {
430 printk(KERN_DEBUG "go7007: dropping oversized frame\n");
431 go->active_buf->offset -= go->active_buf->bytesused;
432 go->active_buf->bytesused = 0;
433 go->active_buf->modet_active = 0;
434 go->active_buf = NULL;
435 }
436
437 switch (go->state) {
438 case STATE_DATA:
439 switch (buf[i]) {
440 case 0x00:
441 go->state = STATE_00;
442 break;
443 case 0xFF:
444 go->state = STATE_FF;
445 break;
446 default:
447 store_byte(go->active_buf, buf[i]);
448 break;
449 }
450 break;
451 case STATE_00:
452 switch (buf[i]) {
453 case 0x00:
454 go->state = STATE_00_00;
455 break;
456 case 0xFF:
457 store_byte(go->active_buf, 0x00);
458 go->state = STATE_FF;
459 break;
460 default:
461 store_byte(go->active_buf, 0x00);
462 store_byte(go->active_buf, buf[i]);
463 go->state = STATE_DATA;
464 break;
465 }
466 break;
467 case STATE_00_00:
468 switch (buf[i]) {
469 case 0x00:
470 store_byte(go->active_buf, 0x00);
471 /* go->state remains STATE_00_00 */
472 break;
473 case 0x01:
474 go->state = STATE_00_00_01;
475 break;
476 case 0xFF:
477 store_byte(go->active_buf, 0x00);
478 store_byte(go->active_buf, 0x00);
479 go->state = STATE_FF;
480 break;
481 default:
482 store_byte(go->active_buf, 0x00);
483 store_byte(go->active_buf, 0x00);
484 store_byte(go->active_buf, buf[i]);
485 go->state = STATE_DATA;
486 break;
487 }
488 break;
489 case STATE_00_00_01:
490 /* If this is the start of a new MPEG frame,
491 * get a new buffer */
492 if ((go->format == GO7007_FORMAT_MPEG1 ||
493 go->format == GO7007_FORMAT_MPEG2 ||
494 go->format == GO7007_FORMAT_MPEG4) &&
495 (buf[i] == seq_start_code ||
496 buf[i] == 0xB8 || /* GOP code */
497 buf[i] == frame_start_code)) {
498 if (go->active_buf == NULL || go->seen_frame)
499 frame_boundary(go);
500 if (buf[i] == frame_start_code) {
501 if (go->active_buf != NULL)
502 go->active_buf->frame_offset =
503 go->active_buf->offset;
504 go->seen_frame = 1;
505 } else {
506 go->seen_frame = 0;
507 }
508 }
509 /* Handle any special chunk types, or just write the
510 * start code to the (potentially new) buffer */
511 switch (buf[i]) {
512 case 0xF5: /* timestamp */
513 go->parse_length = 12;
514 go->state = STATE_UNPARSED;
515 break;
516 case 0xF6: /* vbi */
517 go->state = STATE_VBI_LEN_A;
518 break;
519 case 0xF8: /* MD map */
520 go->parse_length = 0;
521 memset(go->active_map, 0,
522 sizeof(go->active_map));
523 go->state = STATE_MODET_MAP;
524 break;
525 case 0xFF: /* Potential JPEG start code */
526 store_byte(go->active_buf, 0x00);
527 store_byte(go->active_buf, 0x00);
528 store_byte(go->active_buf, 0x01);
529 go->state = STATE_FF;
530 break;
531 default:
532 store_byte(go->active_buf, 0x00);
533 store_byte(go->active_buf, 0x00);
534 store_byte(go->active_buf, 0x01);
535 store_byte(go->active_buf, buf[i]);
536 go->state = STATE_DATA;
537 break;
538 }
539 break;
540 case STATE_FF:
541 switch (buf[i]) {
542 case 0x00:
543 store_byte(go->active_buf, 0xFF);
544 go->state = STATE_00;
545 break;
546 case 0xFF:
547 store_byte(go->active_buf, 0xFF);
548 /* go->state remains STATE_FF */
549 break;
550 case 0xD8:
551 if (go->format == GO7007_FORMAT_MJPEG)
552 frame_boundary(go);
553 /* fall through */
554 default:
555 store_byte(go->active_buf, 0xFF);
556 store_byte(go->active_buf, buf[i]);
557 go->state = STATE_DATA;
558 break;
559 }
560 break;
561 case STATE_VBI_LEN_A:
562 go->parse_length = buf[i] << 8;
563 go->state = STATE_VBI_LEN_B;
564 break;
565 case STATE_VBI_LEN_B:
566 go->parse_length |= buf[i];
567 if (go->parse_length > 0)
568 go->state = STATE_UNPARSED;
569 else
570 go->state = STATE_DATA;
571 break;
572 case STATE_MODET_MAP:
573 if (go->parse_length < 204) {
574 if (go->parse_length & 1) {
575 go->modet_word |= buf[i];
576 write_bitmap_word(go);
577 } else
578 go->modet_word = buf[i] << 8;
579 } else if (go->parse_length == 207 && go->active_buf) {
580 go->active_buf->modet_active = buf[i];
581 }
582 if (++go->parse_length == 208)
583 go->state = STATE_DATA;
584 break;
585 case STATE_UNPARSED:
586 if (--go->parse_length == 0)
587 go->state = STATE_DATA;
588 break;
589 }
590 }
591
592 spin_unlock(&go->spinlock);
593}
594EXPORT_SYMBOL(go7007_parse_video_stream);
595
596/*
597 * Allocate a new go7007 struct. Used by the hardware-specific probe.
598 */
599struct go7007 *go7007_alloc(struct go7007_board_info *board, struct device *dev)
600{
601 struct go7007 *go;
602 int i;
603
604 go = kmalloc(sizeof(struct go7007), GFP_KERNEL);
605 if (go == NULL)
606 return NULL;
607 go->dev = dev;
608 go->board_info = board;
609 go->board_id = 0;
610 go->tuner_type = -1;
611 go->channel_number = 0;
612 go->name[0] = 0;
613 init_MUTEX(&go->hw_lock);
614 init_waitqueue_head(&go->frame_waitq);
615 spin_lock_init(&go->spinlock);
616 go->video_dev = NULL;
617 go->ref_count = 0;
618 go->status = STATUS_INIT;
619 memset(&go->i2c_adapter, 0, sizeof(go->i2c_adapter));
620 go->i2c_adapter_online = 0;
621 go->interrupt_available = 0;
622 init_waitqueue_head(&go->interrupt_waitq);
623 go->in_use = 0;
624 go->input = 0;
625 if (board->sensor_flags & GO7007_SENSOR_TV) {
626 go->standard = GO7007_STD_NTSC;
627 go->width = 720;
628 go->height = 480;
629 go->sensor_framerate = 30000;
630 } else {
631 go->standard = GO7007_STD_OTHER;
632 go->width = board->sensor_width;
633 go->height = board->sensor_height;
634 go->sensor_framerate = board->sensor_framerate;
635 }
636 go->encoder_v_offset = board->sensor_v_offset;
637 go->encoder_h_offset = board->sensor_h_offset;
638 go->encoder_h_halve = 0;
639 go->encoder_v_halve = 0;
640 go->encoder_subsample = 0;
641 go->streaming = 0;
642 go->format = GO7007_FORMAT_MJPEG;
643 go->bitrate = 1500000;
644 go->fps_scale = 1;
645 go->pali = 0;
646 go->aspect_ratio = GO7007_RATIO_1_1;
647 go->gop_size = 0;
648 go->ipb = 0;
649 go->closed_gop = 0;
650 go->repeat_seqhead = 0;
651 go->seq_header_enable = 0;
652 go->gop_header_enable = 0;
653 go->dvd_mode = 0;
654 go->interlace_coding = 0;
655 for (i = 0; i < 4; ++i)
656 go->modet[i].enable = 0;;
657 for (i = 0; i < 1624; ++i)
658 go->modet_map[i] = 0;
659 go->audio_deliver = NULL;
660 go->audio_enabled = 0;
661 INIT_LIST_HEAD(&go->stream);
662
663 return go;
664}
665EXPORT_SYMBOL(go7007_alloc);
666
667/*
668 * Detach and unregister the encoder. The go7007 struct won't be freed
669 * until v4l2 finishes releasing its resources and all associated fds are
670 * closed by applications.
671 */
672void go7007_remove(struct go7007 *go)
673{
674 if (go->i2c_adapter_online) {
675 if (i2c_del_adapter(&go->i2c_adapter) == 0)
676 go->i2c_adapter_online = 0;
677 else
678 printk(KERN_ERR
679 "go7007: error removing I2C adapter!\n");
680 }
681
682 if (go->audio_enabled)
683 go7007_snd_remove(go);
684 go7007_v4l2_remove(go);
685}
686EXPORT_SYMBOL(go7007_remove);
687
688MODULE_LICENSE("GPL v2");