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/mutex.h>
31 #include <linux/uaccess.h>
32 #include <linux/slab.h>
33 #include <linux/videodev2.h>
34 #include <media/tuner.h>
35 #include <media/v4l2-common.h>
37 #include "go7007-priv.h"
40 * Wait for an interrupt to be delivered from the GO7007SB and return
41 * the associated value and data.
43 * Must be called with the hw_lock held.
45 int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data)
47 go->interrupt_available = 0;
48 go->hpi_ops->read_interrupt(go);
49 if (wait_event_timeout(go->interrupt_waitq,
50 go->interrupt_available, 5*HZ) < 0) {
51 v4l2_err(&go->v4l2_dev, "timeout waiting for read interrupt\n");
54 if (!go->interrupt_available)
56 go->interrupt_available = 0;
57 *value = go->interrupt_value & 0xfffe;
58 *data = go->interrupt_data;
61 EXPORT_SYMBOL(go7007_read_interrupt);
64 * Read a register/address on the GO7007SB.
66 * Must be called with the hw_lock held.
68 int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data)
73 if (go7007_write_interrupt(go, 0x0010, addr) < 0)
76 if (go7007_read_interrupt(go, &value, data) == 0 &&
82 EXPORT_SYMBOL(go7007_read_addr);
85 * Send the boot firmware to the encoder, which just wakes it up and lets
86 * us talk to the GPIO pins and on-board I2C adapter.
88 * Must be called with the hw_lock held.
90 static int go7007_load_encoder(struct go7007 *go)
92 const struct firmware *fw_entry;
93 char fw_name[] = "go7007/go7007fw.bin";
96 u16 intr_val, intr_data;
98 if (go->boot_fw == NULL) {
99 if (request_firmware(&fw_entry, fw_name, go->dev)) {
100 v4l2_err(go, "unable to load firmware from file \"%s\"\n", fw_name);
103 if (fw_entry->size < 16 || memcmp(fw_entry->data, "WISGO7007FW", 11)) {
104 v4l2_err(go, "file \"%s\" does not appear to be go7007 firmware\n", fw_name);
105 release_firmware(fw_entry);
108 fw_len = fw_entry->size - 16;
109 bounce = kmemdup(fw_entry->data + 16, fw_len, GFP_KERNEL);
110 if (bounce == NULL) {
111 v4l2_err(go, "unable to allocate %d bytes for firmware transfer\n", fw_len);
112 release_firmware(fw_entry);
115 release_firmware(fw_entry);
116 go->boot_fw_len = fw_len;
117 go->boot_fw = bounce;
119 if (go7007_interface_reset(go) < 0 ||
120 go7007_send_firmware(go, go->boot_fw, go->boot_fw_len) < 0 ||
121 go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
122 (intr_val & ~0x1) != 0x5a5a) {
123 v4l2_err(go, "error transferring firmware\n");
129 MODULE_FIRMWARE("go7007/go7007fw.bin");
132 * Boot the encoder and register the I2C adapter if requested. Do the
133 * minimum initialization necessary, since the board-specific code may
134 * still need to probe the board ID.
136 * Must NOT be called with the hw_lock held.
138 int go7007_boot_encoder(struct go7007 *go, int init_i2c)
142 mutex_lock(&go->hw_lock);
143 ret = go7007_load_encoder(go);
144 mutex_unlock(&go->hw_lock);
149 if (go7007_i2c_init(go) < 0)
151 go->i2c_adapter_online = 1;
154 EXPORT_SYMBOL(go7007_boot_encoder);
157 * Configure any hardware-related registers in the GO7007, such as GPIO
158 * pins and bus parameters, which are board-specific. This assumes
159 * the boot firmware has already been downloaded.
161 * Must be called with the hw_lock held.
163 static int go7007_init_encoder(struct go7007 *go)
165 if (go->board_info->audio_flags & GO7007_AUDIO_I2S_MASTER) {
166 go7007_write_addr(go, 0x1000, 0x0811);
167 go7007_write_addr(go, 0x1000, 0x0c11);
169 switch (go->board_id) {
170 case 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);
175 case GO7007_BOARDID_ADLINK_MPG24:
176 /* set GPIO5 to be an output, currently low */
177 go7007_write_addr(go, 0x3c82, 0x0000);
178 go7007_write_addr(go, 0x3c80, 0x00df);
180 case GO7007_BOARDID_ADS_USBAV_709:
181 /* GPIO pin 0: audio clock control */
182 /* pin 2: TW9906 reset */
183 /* pin 3: capture LED */
184 go7007_write_addr(go, 0x3c82, 0x000d);
185 go7007_write_addr(go, 0x3c80, 0x00f2);
192 * Send the boot firmware to the GO7007 and configure the registers. This
193 * is the only way to stop the encoder once it has started streaming video.
195 * Must be called with the hw_lock held.
197 int go7007_reset_encoder(struct go7007 *go)
199 if (go7007_load_encoder(go) < 0)
201 return go7007_init_encoder(go);
205 * Attempt to instantiate an I2C client by ID, probably loading a module.
207 static int init_i2c_module(struct i2c_adapter *adapter, const struct go_i2c *const i2c)
209 struct go7007 *go = i2c_get_adapdata(adapter);
210 struct v4l2_device *v4l2_dev = &go->v4l2_dev;
211 struct v4l2_subdev *sd;
212 struct i2c_board_info info;
214 memset(&info, 0, sizeof(info));
215 strlcpy(info.type, i2c->type, sizeof(info.type));
216 info.addr = i2c->addr;
217 info.flags = i2c->flags;
219 sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, NULL);
228 printk(KERN_INFO "go7007: probing for module i2c:%s failed\n", i2c->type);
233 * Detach and unregister the encoder. The go7007 struct won't be freed
234 * until v4l2 finishes releasing its resources and all associated fds are
235 * closed by applications.
237 static void go7007_remove(struct v4l2_device *v4l2_dev)
239 struct go7007 *go = container_of(v4l2_dev, struct go7007, v4l2_dev);
241 v4l2_device_unregister(v4l2_dev);
242 if (go->hpi_ops->release)
243 go->hpi_ops->release(go);
244 if (go->i2c_adapter_online) {
245 i2c_del_adapter(&go->i2c_adapter);
246 go->i2c_adapter_online = 0;
250 go7007_v4l2_remove(go);
255 * Finalize the GO7007 hardware setup, register the on-board I2C adapter
256 * (if used on this board), load the I2C client driver for the sensor
257 * (SAA7115 or whatever) and other devices, and register the ALSA and V4L2
260 * Must NOT be called with the hw_lock held.
262 int go7007_register_encoder(struct go7007 *go, unsigned num_i2c_devs)
266 dev_info(go->dev, "go7007: registering new %s\n", go->name);
268 go->v4l2_dev.release = go7007_remove;
269 ret = v4l2_device_register(go->dev, &go->v4l2_dev);
273 mutex_lock(&go->hw_lock);
274 ret = go7007_init_encoder(go);
275 mutex_unlock(&go->hw_lock);
279 ret = go7007_v4l2_ctrl_init(go);
283 if (!go->i2c_adapter_online &&
284 go->board_info->flags & GO7007_BOARD_USE_ONBOARD_I2C) {
285 ret = go7007_i2c_init(go);
288 go->i2c_adapter_online = 1;
290 if (go->i2c_adapter_online) {
291 if (go->board_id == GO7007_BOARDID_ADS_USBAV_709) {
292 /* Reset the TW9906 */
293 go7007_write_addr(go, 0x3c82, 0x0009);
295 go7007_write_addr(go, 0x3c82, 0x000d);
297 for (i = 0; i < num_i2c_devs; ++i)
298 init_i2c_module(&go->i2c_adapter, &go->board_info->i2c_devs[i]);
300 if (go->tuner_type >= 0) {
301 struct tuner_setup setup = {
303 .type = go->tuner_type,
304 .mode_mask = T_ANALOG_TV,
307 v4l2_device_call_all(&go->v4l2_dev, 0, tuner,
308 s_type_addr, &setup);
310 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24)
311 v4l2_subdev_call(go->sd_video, video, s_routing,
312 0, 0, go->channel_number + 1);
315 ret = go7007_v4l2_init(go);
319 if (go->board_info->flags & GO7007_BOARD_HAS_AUDIO) {
320 go->audio_enabled = 1;
325 EXPORT_SYMBOL(go7007_register_encoder);
328 * Send the encode firmware to the encoder, which will cause it
329 * to immediately start delivering the video and audio streams.
331 * Must be called with the hw_lock held.
333 int go7007_start_encoder(struct go7007 *go)
336 int fw_len, rv = 0, i;
337 u16 intr_val, intr_data;
339 go->modet_enable = 0;
341 for (i = 0; i < 4; ++i) {
342 if (go->modet[i].enable) {
343 go->modet_enable = 1;
346 go->modet[i].pixel_threshold = 32767;
347 go->modet[i].motion_threshold = 32767;
348 go->modet[i].mb_threshold = 32767;
351 if (go7007_construct_fw_image(go, &fw, &fw_len) < 0)
354 if (go7007_send_firmware(go, fw, fw_len) < 0 ||
355 go7007_read_interrupt(go, &intr_val, &intr_data) < 0) {
356 v4l2_err(&go->v4l2_dev, "error transferring firmware\n");
361 go->state = STATE_DATA;
362 go->parse_length = 0;
364 if (go7007_stream_start(go) < 0) {
365 v4l2_err(&go->v4l2_dev, "error starting stream transfer\n");
376 * Store a byte in the current video buffer, if there is one.
378 static inline void store_byte(struct go7007_buffer *vb, u8 byte)
380 if (vb && vb->vb.v4l2_planes[0].bytesused < GO7007_BUF_SIZE) {
381 u8 *ptr = vb2_plane_vaddr(&vb->vb, 0);
383 ptr[vb->vb.v4l2_planes[0].bytesused++] = byte;
388 * Deliver the last video buffer and get a new one to start writing to.
390 static struct go7007_buffer *frame_boundary(struct go7007 *go, struct go7007_buffer *vb)
392 struct go7007_buffer *vb_tmp = NULL;
393 u32 *bytesused = &vb->vb.v4l2_planes[0].bytesused;
397 if (vb->modet_active) {
398 if (*bytesused + 216 < GO7007_BUF_SIZE) {
399 for (i = 0; i < 216; ++i)
400 store_byte(vb, go->active_map[i]);
403 vb->modet_active = 0;
405 vb->vb.v4l2_buf.sequence = go->next_seq++;
406 v4l2_get_timestamp(&vb->vb.v4l2_buf.timestamp);
408 spin_lock(&go->spinlock);
410 if (list_empty(&go->vidq_active))
413 vb = list_first_entry(&go->vidq_active, struct go7007_buffer, list);
415 spin_unlock(&go->spinlock);
416 vb2_buffer_done(&vb_tmp->vb, VB2_BUF_STATE_DONE);
419 spin_lock(&go->spinlock);
420 if (!list_empty(&go->vidq_active))
421 vb = go->active_buf =
422 list_first_entry(&go->vidq_active, struct go7007_buffer, list);
423 spin_unlock(&go->spinlock);
428 static void write_bitmap_word(struct go7007 *go)
430 int x, y, i, stride = ((go->width >> 4) + 7) >> 3;
432 for (i = 0; i < 16; ++i) {
433 y = (((go->parse_length - 1) << 3) + i) / (go->width >> 4);
434 x = (((go->parse_length - 1) << 3) + i) % (go->width >> 4);
435 if (stride * y + (x >> 3) < sizeof(go->active_map))
436 go->active_map[stride * y + (x >> 3)] |=
437 (go->modet_word & 1) << (x & 0x7);
438 go->modet_word >>= 1;
443 * Parse a chunk of the video stream into frames. The frames are not
444 * delimited by the hardware, so we have to parse the frame boundaries
445 * based on the type of video stream we're receiving.
447 void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length)
449 struct go7007_buffer *vb = go->active_buf;
450 int i, seq_start_code = -1, gop_start_code = -1, frame_start_code = -1;
452 switch (go->format) {
453 case V4L2_PIX_FMT_MPEG4:
454 seq_start_code = 0xB0;
455 gop_start_code = 0xB3;
456 frame_start_code = 0xB6;
458 case V4L2_PIX_FMT_MPEG1:
459 case V4L2_PIX_FMT_MPEG2:
460 seq_start_code = 0xB3;
461 gop_start_code = 0xB8;
462 frame_start_code = 0x00;
466 for (i = 0; i < length; ++i) {
467 if (vb && vb->vb.v4l2_planes[0].bytesused >= GO7007_BUF_SIZE - 3) {
468 v4l2_info(&go->v4l2_dev, "dropping oversized frame\n");
469 vb->vb.v4l2_planes[0].bytesused = 0;
470 vb->frame_offset = 0;
471 vb->modet_active = 0;
472 vb = go->active_buf = NULL;
479 go->state = STATE_00;
482 go->state = STATE_FF;
485 store_byte(vb, buf[i]);
492 go->state = STATE_00_00;
495 store_byte(vb, 0x00);
496 go->state = STATE_FF;
499 store_byte(vb, 0x00);
500 store_byte(vb, buf[i]);
501 go->state = STATE_DATA;
508 store_byte(vb, 0x00);
509 /* go->state remains STATE_00_00 */
512 go->state = STATE_00_00_01;
515 store_byte(vb, 0x00);
516 store_byte(vb, 0x00);
517 go->state = STATE_FF;
520 store_byte(vb, 0x00);
521 store_byte(vb, 0x00);
522 store_byte(vb, buf[i]);
523 go->state = STATE_DATA;
528 if (buf[i] == 0xF8 && go->modet_enable == 0) {
529 /* MODET start code, but MODET not enabled */
530 store_byte(vb, 0x00);
531 store_byte(vb, 0x00);
532 store_byte(vb, 0x01);
533 store_byte(vb, 0xF8);
534 go->state = STATE_DATA;
537 /* If this is the start of a new MPEG frame,
538 * get a new buffer */
539 if ((go->format == V4L2_PIX_FMT_MPEG1 ||
540 go->format == V4L2_PIX_FMT_MPEG2 ||
541 go->format == V4L2_PIX_FMT_MPEG4) &&
542 (buf[i] == seq_start_code ||
543 buf[i] == gop_start_code ||
544 buf[i] == frame_start_code)) {
545 if (vb == NULL || go->seen_frame)
546 vb = frame_boundary(go, vb);
547 go->seen_frame = buf[i] == frame_start_code;
548 if (vb && go->seen_frame)
549 vb->frame_offset = vb->vb.v4l2_planes[0].bytesused;
551 /* Handle any special chunk types, or just write the
552 * start code to the (potentially new) buffer */
554 case 0xF5: /* timestamp */
555 go->parse_length = 12;
556 go->state = STATE_UNPARSED;
559 go->state = STATE_VBI_LEN_A;
561 case 0xF8: /* MD map */
562 go->parse_length = 0;
563 memset(go->active_map, 0,
564 sizeof(go->active_map));
565 go->state = STATE_MODET_MAP;
567 case 0xFF: /* Potential JPEG start code */
568 store_byte(vb, 0x00);
569 store_byte(vb, 0x00);
570 store_byte(vb, 0x01);
571 go->state = STATE_FF;
574 store_byte(vb, 0x00);
575 store_byte(vb, 0x00);
576 store_byte(vb, 0x01);
577 store_byte(vb, buf[i]);
578 go->state = STATE_DATA;
585 store_byte(vb, 0xFF);
586 go->state = STATE_00;
589 store_byte(vb, 0xFF);
590 /* go->state remains STATE_FF */
593 if (go->format == V4L2_PIX_FMT_MJPEG)
594 vb = frame_boundary(go, vb);
597 store_byte(vb, 0xFF);
598 store_byte(vb, buf[i]);
599 go->state = STATE_DATA;
603 case STATE_VBI_LEN_A:
604 go->parse_length = buf[i] << 8;
605 go->state = STATE_VBI_LEN_B;
607 case STATE_VBI_LEN_B:
608 go->parse_length |= buf[i];
609 if (go->parse_length > 0)
610 go->state = STATE_UNPARSED;
612 go->state = STATE_DATA;
614 case STATE_MODET_MAP:
615 if (go->parse_length < 204) {
616 if (go->parse_length & 1) {
617 go->modet_word |= buf[i];
618 write_bitmap_word(go);
620 go->modet_word = buf[i] << 8;
621 } else if (go->parse_length == 207 && vb) {
622 vb->modet_active = buf[i];
624 if (++go->parse_length == 208)
625 go->state = STATE_DATA;
628 if (--go->parse_length == 0)
629 go->state = STATE_DATA;
634 EXPORT_SYMBOL(go7007_parse_video_stream);
637 * Allocate a new go7007 struct. Used by the hardware-specific probe.
639 struct go7007 *go7007_alloc(const struct go7007_board_info *board,
645 go = kzalloc(sizeof(struct go7007), GFP_KERNEL);
649 go->board_info = board;
652 go->channel_number = 0;
654 mutex_init(&go->hw_lock);
655 init_waitqueue_head(&go->frame_waitq);
656 spin_lock_init(&go->spinlock);
657 go->status = STATUS_INIT;
658 memset(&go->i2c_adapter, 0, sizeof(go->i2c_adapter));
659 go->i2c_adapter_online = 0;
660 go->interrupt_available = 0;
661 init_waitqueue_head(&go->interrupt_waitq);
663 go7007_update_board(go);
664 go->encoder_h_halve = 0;
665 go->encoder_v_halve = 0;
666 go->encoder_subsample = 0;
667 go->format = V4L2_PIX_FMT_MJPEG;
668 go->bitrate = 1500000;
671 go->aspect_ratio = GO7007_RATIO_1_1;
675 go->repeat_seqhead = 0;
676 go->seq_header_enable = 0;
677 go->gop_header_enable = 0;
679 go->interlace_coding = 0;
680 for (i = 0; i < 4; ++i)
681 go->modet[i].enable = 0;
682 for (i = 0; i < 1624; ++i)
683 go->modet_map[i] = 0;
684 go->audio_deliver = NULL;
685 go->audio_enabled = 0;
689 EXPORT_SYMBOL(go7007_alloc);
691 void go7007_update_board(struct go7007 *go)
693 const struct go7007_board_info *board = go->board_info;
695 if (board->sensor_flags & GO7007_SENSOR_TV) {
696 go->standard = GO7007_STD_NTSC;
697 go->std = V4L2_STD_NTSC_M;
700 go->sensor_framerate = 30000;
702 go->standard = GO7007_STD_OTHER;
703 go->width = board->sensor_width;
704 go->height = board->sensor_height;
705 go->sensor_framerate = board->sensor_framerate;
707 go->encoder_v_offset = board->sensor_v_offset;
708 go->encoder_h_offset = board->sensor_h_offset;
710 EXPORT_SYMBOL(go7007_update_board);
712 MODULE_LICENSE("GPL v2");