2 * Copyright (C) 2005-2006 Micronas USA Inc.
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.
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.
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.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/sched.h>
22 #include <linux/spinlock.h>
23 #include <linux/unistd.h>
24 #include <linux/time.h>
26 #include <linux/vmalloc.h>
27 #include <linux/device.h>
28 #include <linux/i2c.h>
29 #include <linux/firmware.h>
30 #include <linux/semaphore.h>
31 #include <linux/uaccess.h>
32 #include <asm/system.h>
33 #include <linux/videodev2.h>
34 #include <media/tuner.h>
35 #include <media/v4l2-common.h>
37 #include "go7007-priv.h"
41 * Wait for an interrupt to be delivered from the GO7007SB and return
42 * the associated value and data.
44 * Must be called with the hw_lock held.
46 int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data)
48 go->interrupt_available = 0;
49 go->hpi_ops->read_interrupt(go);
50 if (wait_event_timeout(go->interrupt_waitq,
51 go->interrupt_available, 5*HZ) < 0) {
52 printk(KERN_ERR "go7007: timeout waiting for read interrupt\n");
55 if (!go->interrupt_available)
57 go->interrupt_available = 0;
58 *value = go->interrupt_value & 0xfffe;
59 *data = go->interrupt_data;
62 EXPORT_SYMBOL(go7007_read_interrupt);
65 * Read a register/address on the GO7007SB.
67 * Must be called with the hw_lock held.
69 int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data)
74 if (go7007_write_interrupt(go, 0x0010, addr) < 0)
77 if (go7007_read_interrupt(go, &value, data) == 0 &&
83 EXPORT_SYMBOL(go7007_read_addr);
86 * Send the boot firmware to the encoder, which just wakes it up and lets
87 * us talk to the GPIO pins and on-board I2C adapter.
89 * Must be called with the hw_lock held.
91 static int go7007_load_encoder(struct go7007 *go)
93 const struct firmware *fw_entry;
94 char fw_name[] = "go7007fw.bin";
97 u16 intr_val, intr_data;
99 if (request_firmware(&fw_entry, fw_name, go->dev)) {
101 "go7007: unable to load firmware from file \"%s\"\n",
105 if (fw_entry->size < 16 || memcmp(fw_entry->data, "WISGO7007FW", 11)) {
106 printk(KERN_ERR "go7007: file \"%s\" does not appear to be "
107 "go7007 firmware\n", fw_name);
108 release_firmware(fw_entry);
111 fw_len = fw_entry->size - 16;
112 bounce = kmalloc(fw_len, GFP_KERNEL);
113 if (bounce == NULL) {
114 printk(KERN_ERR "go7007: unable to allocate %d bytes for "
115 "firmware transfer\n", fw_len);
116 release_firmware(fw_entry);
119 memcpy(bounce, fw_entry->data + 16, fw_len);
120 release_firmware(fw_entry);
121 if (go7007_interface_reset(go) < 0 ||
122 go7007_send_firmware(go, bounce, fw_len) < 0 ||
123 go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
124 (intr_val & ~0x1) != 0x5a5a) {
125 printk(KERN_ERR "go7007: error transferring firmware\n");
133 * Boot the encoder and register the I2C adapter if requested. Do the
134 * minimum initialization necessary, since the board-specific code may
135 * still need to probe the board ID.
137 * Must NOT be called with the hw_lock held.
139 int go7007_boot_encoder(struct go7007 *go, int init_i2c)
144 ret = go7007_load_encoder(go);
150 if (go7007_i2c_init(go) < 0)
152 go->i2c_adapter_online = 1;
155 EXPORT_SYMBOL(go7007_boot_encoder);
158 * Configure any hardware-related registers in the GO7007, such as GPIO
159 * pins and bus parameters, which are board-specific. This assumes
160 * the boot firmware has already been downloaded.
162 * Must be called with the hw_lock held.
164 static int go7007_init_encoder(struct go7007 *go)
166 if (go->board_info->audio_flags & GO7007_AUDIO_I2S_MASTER) {
167 go7007_write_addr(go, 0x1000, 0x0811);
168 go7007_write_addr(go, 0x1000, 0x0c11);
170 if (go->board_id == GO7007_BOARDID_MATRIX_REV) {
171 /* Set GPIO pin 0 to be an output (audio clock control) */
172 go7007_write_addr(go, 0x3c82, 0x0001);
173 go7007_write_addr(go, 0x3c80, 0x00fe);
179 * Send the boot firmware to the GO7007 and configure the registers. This
180 * is the only way to stop the encoder once it has started streaming video.
182 * Must be called with the hw_lock held.
184 int go7007_reset_encoder(struct go7007 *go)
186 if (go7007_load_encoder(go) < 0)
188 return go7007_init_encoder(go);
192 * Attempt to instantiate an I2C client by ID, probably loading a module.
194 static int init_i2c_module(struct i2c_adapter *adapter, const char *type,
197 struct i2c_board_info info;
201 case I2C_DRIVERID_WIS_SAA7115:
202 modname = "wis-saa7115";
204 case I2C_DRIVERID_WIS_SAA7113:
205 modname = "wis-saa7113";
207 case I2C_DRIVERID_WIS_UDA1342:
208 modname = "wis-uda1342";
210 case I2C_DRIVERID_WIS_SONY_TUNER:
211 modname = "wis-sony-tuner";
213 case I2C_DRIVERID_WIS_TW9903:
214 modname = "wis-tw9903";
216 case I2C_DRIVERID_WIS_TW2804:
217 modname = "wis-tw2804";
219 case I2C_DRIVERID_WIS_OV7640:
220 modname = "wis-ov7640";
222 case I2C_DRIVERID_S2250:
223 modname = "s2250-board";
230 request_module(modname);
232 memset(&info, 0, sizeof(struct i2c_board_info));
234 strlcpy(info.type, type, I2C_NAME_SIZE);
235 if (!i2c_new_device(adapter, &info))
239 "go7007: probing for module %s failed\n", modname);
242 "go7007: sensor %u seems to be unsupported!\n", id);
247 * Finalize the GO7007 hardware setup, register the on-board I2C adapter
248 * (if used on this board), load the I2C client driver for the sensor
249 * (SAA7115 or whatever) and other devices, and register the ALSA and V4L2
252 * Must NOT be called with the hw_lock held.
254 int go7007_register_encoder(struct go7007 *go)
258 printk(KERN_INFO "go7007: registering new %s\n", go->name);
261 ret = go7007_init_encoder(go);
266 if (!go->i2c_adapter_online &&
267 go->board_info->flags & GO7007_BOARD_USE_ONBOARD_I2C) {
268 if (go7007_i2c_init(go) < 0)
270 go->i2c_adapter_online = 1;
272 if (go->i2c_adapter_online) {
273 for (i = 0; i < go->board_info->num_i2c_devs; ++i)
274 init_i2c_module(&go->i2c_adapter,
275 go->board_info->i2c_devs[i].type,
276 go->board_info->i2c_devs[i].id,
277 go->board_info->i2c_devs[i].addr);
278 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24)
279 i2c_clients_command(&go->i2c_adapter,
280 DECODER_SET_CHANNEL, &go->channel_number);
282 if (go->board_info->flags & GO7007_BOARD_HAS_AUDIO) {
283 go->audio_enabled = 1;
286 return go7007_v4l2_init(go);
288 EXPORT_SYMBOL(go7007_register_encoder);
291 * Send the encode firmware to the encoder, which will cause it
292 * to immediately start delivering the video and audio streams.
294 * Must be called with the hw_lock held.
296 int go7007_start_encoder(struct go7007 *go)
299 int fw_len, rv = 0, i;
300 u16 intr_val, intr_data;
302 go->modet_enable = 0;
304 for (i = 0; i < 4; ++i) {
305 if (go->modet[i].enable) {
306 go->modet_enable = 1;
309 go->modet[i].pixel_threshold = 32767;
310 go->modet[i].motion_threshold = 32767;
311 go->modet[i].mb_threshold = 32767;
314 if (go7007_construct_fw_image(go, &fw, &fw_len) < 0)
317 if (go7007_send_firmware(go, fw, fw_len) < 0 ||
318 go7007_read_interrupt(go, &intr_val, &intr_data) < 0) {
319 printk(KERN_ERR "go7007: error transferring firmware\n");
324 go->state = STATE_DATA;
325 go->parse_length = 0;
327 if (go7007_stream_start(go) < 0) {
328 printk(KERN_ERR "go7007: error starting stream transfer\n");
339 * Store a byte in the current video buffer, if there is one.
341 static inline void store_byte(struct go7007_buffer *gobuf, u8 byte)
343 if (gobuf != NULL && gobuf->bytesused < GO7007_BUF_SIZE) {
344 unsigned int pgidx = gobuf->offset >> PAGE_SHIFT;
345 unsigned int pgoff = gobuf->offset & ~PAGE_MASK;
347 *((u8 *)page_address(gobuf->pages[pgidx]) + pgoff) = byte;
354 * Deliver the last video buffer and get a new one to start writing to.
356 static void frame_boundary(struct go7007 *go)
358 struct go7007_buffer *gobuf;
361 if (go->active_buf) {
362 if (go->active_buf->modet_active) {
363 if (go->active_buf->bytesused + 216 < GO7007_BUF_SIZE) {
364 for (i = 0; i < 216; ++i)
365 store_byte(go->active_buf,
367 go->active_buf->bytesused -= 216;
369 go->active_buf->modet_active = 0;
371 go->active_buf->state = BUF_STATE_DONE;
372 wake_up_interruptible(&go->frame_waitq);
373 go->active_buf = NULL;
375 list_for_each_entry(gobuf, &go->stream, stream)
376 if (gobuf->state == BUF_STATE_QUEUED) {
377 gobuf->seq = go->next_seq;
378 do_gettimeofday(&gobuf->timestamp);
379 go->active_buf = gobuf;
385 static void write_bitmap_word(struct go7007 *go)
387 int x, y, i, stride = ((go->width >> 4) + 7) >> 3;
389 for (i = 0; i < 16; ++i) {
390 y = (((go->parse_length - 1) << 3) + i) / (go->width >> 4);
391 x = (((go->parse_length - 1) << 3) + i) % (go->width >> 4);
392 go->active_map[stride * y + (x >> 3)] |=
393 (go->modet_word & 1) << (x & 0x7);
394 go->modet_word >>= 1;
399 * Parse a chunk of the video stream into frames. The frames are not
400 * delimited by the hardware, so we have to parse the frame boundaries
401 * based on the type of video stream we're receiving.
403 void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length)
405 int i, seq_start_code = -1, frame_start_code = -1;
407 spin_lock(&go->spinlock);
409 switch (go->format) {
410 case GO7007_FORMAT_MPEG4:
411 seq_start_code = 0xB0;
412 frame_start_code = 0xB6;
414 case GO7007_FORMAT_MPEG1:
415 case GO7007_FORMAT_MPEG2:
416 seq_start_code = 0xB3;
417 frame_start_code = 0x00;
421 for (i = 0; i < length; ++i) {
422 if (go->active_buf != NULL &&
423 go->active_buf->bytesused >= GO7007_BUF_SIZE - 3) {
424 printk(KERN_DEBUG "go7007: dropping oversized frame\n");
425 go->active_buf->offset -= go->active_buf->bytesused;
426 go->active_buf->bytesused = 0;
427 go->active_buf->modet_active = 0;
428 go->active_buf = NULL;
435 go->state = STATE_00;
438 go->state = STATE_FF;
441 store_byte(go->active_buf, buf[i]);
448 go->state = STATE_00_00;
451 store_byte(go->active_buf, 0x00);
452 go->state = STATE_FF;
455 store_byte(go->active_buf, 0x00);
456 store_byte(go->active_buf, buf[i]);
457 go->state = STATE_DATA;
464 store_byte(go->active_buf, 0x00);
465 /* go->state remains STATE_00_00 */
468 go->state = STATE_00_00_01;
471 store_byte(go->active_buf, 0x00);
472 store_byte(go->active_buf, 0x00);
473 go->state = STATE_FF;
476 store_byte(go->active_buf, 0x00);
477 store_byte(go->active_buf, 0x00);
478 store_byte(go->active_buf, buf[i]);
479 go->state = STATE_DATA;
484 /* If this is the start of a new MPEG frame,
485 * get a new buffer */
486 if ((go->format == GO7007_FORMAT_MPEG1 ||
487 go->format == GO7007_FORMAT_MPEG2 ||
488 go->format == GO7007_FORMAT_MPEG4) &&
489 (buf[i] == seq_start_code ||
490 buf[i] == 0xB8 || /* GOP code */
491 buf[i] == frame_start_code)) {
492 if (go->active_buf == NULL || go->seen_frame)
494 if (buf[i] == frame_start_code) {
495 if (go->active_buf != NULL)
496 go->active_buf->frame_offset =
497 go->active_buf->offset;
503 /* Handle any special chunk types, or just write the
504 * start code to the (potentially new) buffer */
506 case 0xF5: /* timestamp */
507 go->parse_length = 12;
508 go->state = STATE_UNPARSED;
511 go->state = STATE_VBI_LEN_A;
513 case 0xF8: /* MD map */
514 go->parse_length = 0;
515 memset(go->active_map, 0,
516 sizeof(go->active_map));
517 go->state = STATE_MODET_MAP;
519 case 0xFF: /* Potential JPEG start code */
520 store_byte(go->active_buf, 0x00);
521 store_byte(go->active_buf, 0x00);
522 store_byte(go->active_buf, 0x01);
523 go->state = STATE_FF;
526 store_byte(go->active_buf, 0x00);
527 store_byte(go->active_buf, 0x00);
528 store_byte(go->active_buf, 0x01);
529 store_byte(go->active_buf, buf[i]);
530 go->state = STATE_DATA;
537 store_byte(go->active_buf, 0xFF);
538 go->state = STATE_00;
541 store_byte(go->active_buf, 0xFF);
542 /* go->state remains STATE_FF */
545 if (go->format == GO7007_FORMAT_MJPEG)
549 store_byte(go->active_buf, 0xFF);
550 store_byte(go->active_buf, buf[i]);
551 go->state = STATE_DATA;
555 case STATE_VBI_LEN_A:
556 go->parse_length = buf[i] << 8;
557 go->state = STATE_VBI_LEN_B;
559 case STATE_VBI_LEN_B:
560 go->parse_length |= buf[i];
561 if (go->parse_length > 0)
562 go->state = STATE_UNPARSED;
564 go->state = STATE_DATA;
566 case STATE_MODET_MAP:
567 if (go->parse_length < 204) {
568 if (go->parse_length & 1) {
569 go->modet_word |= buf[i];
570 write_bitmap_word(go);
572 go->modet_word = buf[i] << 8;
573 } else if (go->parse_length == 207 && go->active_buf) {
574 go->active_buf->modet_active = buf[i];
576 if (++go->parse_length == 208)
577 go->state = STATE_DATA;
580 if (--go->parse_length == 0)
581 go->state = STATE_DATA;
586 spin_unlock(&go->spinlock);
588 EXPORT_SYMBOL(go7007_parse_video_stream);
591 * Allocate a new go7007 struct. Used by the hardware-specific probe.
593 struct go7007 *go7007_alloc(struct go7007_board_info *board, struct device *dev)
598 go = kmalloc(sizeof(struct go7007), GFP_KERNEL);
602 go->board_info = board;
605 go->channel_number = 0;
607 init_MUTEX(&go->hw_lock);
608 init_waitqueue_head(&go->frame_waitq);
609 spin_lock_init(&go->spinlock);
610 go->video_dev = NULL;
612 go->status = STATUS_INIT;
613 memset(&go->i2c_adapter, 0, sizeof(go->i2c_adapter));
614 go->i2c_adapter_online = 0;
615 go->interrupt_available = 0;
616 init_waitqueue_head(&go->interrupt_waitq);
619 if (board->sensor_flags & GO7007_SENSOR_TV) {
620 go->standard = GO7007_STD_NTSC;
623 go->sensor_framerate = 30000;
625 go->standard = GO7007_STD_OTHER;
626 go->width = board->sensor_width;
627 go->height = board->sensor_height;
628 go->sensor_framerate = board->sensor_framerate;
630 go->encoder_v_offset = board->sensor_v_offset;
631 go->encoder_h_offset = board->sensor_h_offset;
632 go->encoder_h_halve = 0;
633 go->encoder_v_halve = 0;
634 go->encoder_subsample = 0;
636 go->format = GO7007_FORMAT_MJPEG;
637 go->bitrate = 1500000;
640 go->aspect_ratio = GO7007_RATIO_1_1;
644 go->repeat_seqhead = 0;
645 go->seq_header_enable = 0;
646 go->gop_header_enable = 0;
648 go->interlace_coding = 0;
649 for (i = 0; i < 4; ++i)
650 go->modet[i].enable = 0;;
651 for (i = 0; i < 1624; ++i)
652 go->modet_map[i] = 0;
653 go->audio_deliver = NULL;
654 go->audio_enabled = 0;
655 INIT_LIST_HEAD(&go->stream);
659 EXPORT_SYMBOL(go7007_alloc);
662 * Detach and unregister the encoder. The go7007 struct won't be freed
663 * until v4l2 finishes releasing its resources and all associated fds are
664 * closed by applications.
666 void go7007_remove(struct go7007 *go)
668 if (go->i2c_adapter_online) {
669 if (i2c_del_adapter(&go->i2c_adapter) == 0)
670 go->i2c_adapter_online = 0;
673 "go7007: error removing I2C adapter!\n");
676 if (go->audio_enabled)
677 go7007_snd_remove(go);
678 go7007_v4l2_remove(go);
680 EXPORT_SYMBOL(go7007_remove);
682 MODULE_LICENSE("GPL v2");