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/delay.h>
20 #include <linux/sched.h>
21 #include <linux/spinlock.h>
22 #include <linux/unistd.h>
23 #include <linux/time.h>
25 #include <linux/vmalloc.h>
26 #include <linux/device.h>
27 #include <linux/i2c.h>
28 #include <linux/firmware.h>
29 #include <linux/mutex.h>
30 #include <linux/uaccess.h>
31 #include <linux/slab.h>
32 #include <linux/videodev2.h>
33 #include <media/tuner.h>
34 #include <media/v4l2-common.h>
36 #include "go7007-priv.h"
39 * Wait for an interrupt to be delivered from the GO7007SB and return
40 * the associated value and data.
42 * Must be called with the hw_lock held.
44 int go7007_read_interrupt(struct go7007
*go
, u16
*value
, u16
*data
)
46 go
->interrupt_available
= 0;
47 go
->hpi_ops
->read_interrupt(go
);
48 if (wait_event_timeout(go
->interrupt_waitq
,
49 go
->interrupt_available
, 5*HZ
) < 0) {
50 v4l2_err(&go
->v4l2_dev
, "timeout waiting for read interrupt\n");
53 if (!go
->interrupt_available
)
55 go
->interrupt_available
= 0;
56 *value
= go
->interrupt_value
& 0xfffe;
57 *data
= go
->interrupt_data
;
60 EXPORT_SYMBOL(go7007_read_interrupt
);
63 * Read a register/address on the GO7007SB.
65 * Must be called with the hw_lock held.
67 int go7007_read_addr(struct go7007
*go
, u16 addr
, u16
*data
)
72 if (go7007_write_interrupt(go
, 0x0010, addr
) < 0)
75 if (go7007_read_interrupt(go
, &value
, data
) == 0 &&
81 EXPORT_SYMBOL(go7007_read_addr
);
84 * Send the boot firmware to the encoder, which just wakes it up and lets
85 * us talk to the GPIO pins and on-board I2C adapter.
87 * Must be called with the hw_lock held.
89 static int go7007_load_encoder(struct go7007
*go
)
91 const struct firmware
*fw_entry
;
92 char fw_name
[] = "go7007/go7007fw.bin";
95 u16 intr_val
, intr_data
;
97 if (go
->boot_fw
== NULL
) {
98 if (request_firmware(&fw_entry
, fw_name
, go
->dev
)) {
99 v4l2_err(go
, "unable to load firmware from file \"%s\"\n", fw_name
);
102 if (fw_entry
->size
< 16 || memcmp(fw_entry
->data
, "WISGO7007FW", 11)) {
103 v4l2_err(go
, "file \"%s\" does not appear to be go7007 firmware\n", fw_name
);
104 release_firmware(fw_entry
);
107 fw_len
= fw_entry
->size
- 16;
108 bounce
= kmemdup(fw_entry
->data
+ 16, fw_len
, GFP_KERNEL
);
109 if (bounce
== NULL
) {
110 v4l2_err(go
, "unable to allocate %d bytes for firmware transfer\n", fw_len
);
111 release_firmware(fw_entry
);
114 release_firmware(fw_entry
);
115 go
->boot_fw_len
= fw_len
;
116 go
->boot_fw
= bounce
;
118 if (go7007_interface_reset(go
) < 0 ||
119 go7007_send_firmware(go
, go
->boot_fw
, go
->boot_fw_len
) < 0 ||
120 go7007_read_interrupt(go
, &intr_val
, &intr_data
) < 0 ||
121 (intr_val
& ~0x1) != 0x5a5a) {
122 v4l2_err(go
, "error transferring firmware\n");
128 MODULE_FIRMWARE("go7007/go7007fw.bin");
131 * Boot the encoder and register the I2C adapter if requested. Do the
132 * minimum initialization necessary, since the board-specific code may
133 * still need to probe the board ID.
135 * Must NOT be called with the hw_lock held.
137 int go7007_boot_encoder(struct go7007
*go
, int init_i2c
)
141 mutex_lock(&go
->hw_lock
);
142 ret
= go7007_load_encoder(go
);
143 mutex_unlock(&go
->hw_lock
);
148 if (go7007_i2c_init(go
) < 0)
150 go
->i2c_adapter_online
= 1;
153 EXPORT_SYMBOL(go7007_boot_encoder
);
156 * Configure any hardware-related registers in the GO7007, such as GPIO
157 * pins and bus parameters, which are board-specific. This assumes
158 * the boot firmware has already been downloaded.
160 * Must be called with the hw_lock held.
162 static int go7007_init_encoder(struct go7007
*go
)
164 if (go
->board_info
->audio_flags
& GO7007_AUDIO_I2S_MASTER
) {
165 go7007_write_addr(go
, 0x1000, 0x0811);
166 go7007_write_addr(go
, 0x1000, 0x0c11);
168 switch (go
->board_id
) {
169 case GO7007_BOARDID_MATRIX_REV
:
170 /* Set GPIO pin 0 to be an output (audio clock control) */
171 go7007_write_addr(go
, 0x3c82, 0x0001);
172 go7007_write_addr(go
, 0x3c80, 0x00fe);
174 case GO7007_BOARDID_ADLINK_MPG24
:
175 /* set GPIO5 to be an output, currently low */
176 go7007_write_addr(go
, 0x3c82, 0x0000);
177 go7007_write_addr(go
, 0x3c80, 0x00df);
179 case GO7007_BOARDID_ADS_USBAV_709
:
180 /* GPIO pin 0: audio clock control */
181 /* pin 2: TW9906 reset */
182 /* pin 3: capture LED */
183 go7007_write_addr(go
, 0x3c82, 0x000d);
184 go7007_write_addr(go
, 0x3c80, 0x00f2);
191 * Send the boot firmware to the GO7007 and configure the registers. This
192 * is the only way to stop the encoder once it has started streaming video.
194 * Must be called with the hw_lock held.
196 int go7007_reset_encoder(struct go7007
*go
)
198 if (go7007_load_encoder(go
) < 0)
200 return go7007_init_encoder(go
);
204 * Attempt to instantiate an I2C client by ID, probably loading a module.
206 static int init_i2c_module(struct i2c_adapter
*adapter
, const struct go_i2c
*const i2c
)
208 struct go7007
*go
= i2c_get_adapdata(adapter
);
209 struct v4l2_device
*v4l2_dev
= &go
->v4l2_dev
;
210 struct v4l2_subdev
*sd
;
211 struct i2c_board_info info
;
213 memset(&info
, 0, sizeof(info
));
214 strlcpy(info
.type
, i2c
->type
, sizeof(info
.type
));
215 info
.addr
= i2c
->addr
;
216 info
.flags
= i2c
->flags
;
218 sd
= v4l2_i2c_new_subdev_board(v4l2_dev
, adapter
, &info
, NULL
);
227 printk(KERN_INFO
"go7007: probing for module i2c:%s failed\n", i2c
->type
);
232 * Detach and unregister the encoder. The go7007 struct won't be freed
233 * until v4l2 finishes releasing its resources and all associated fds are
234 * closed by applications.
236 static void go7007_remove(struct v4l2_device
*v4l2_dev
)
238 struct go7007
*go
= container_of(v4l2_dev
, struct go7007
, v4l2_dev
);
240 v4l2_device_unregister(v4l2_dev
);
241 if (go
->hpi_ops
->release
)
242 go
->hpi_ops
->release(go
);
243 if (go
->i2c_adapter_online
) {
244 i2c_del_adapter(&go
->i2c_adapter
);
245 go
->i2c_adapter_online
= 0;
249 go7007_v4l2_remove(go
);
254 * Finalize the GO7007 hardware setup, register the on-board I2C adapter
255 * (if used on this board), load the I2C client driver for the sensor
256 * (SAA7115 or whatever) and other devices, and register the ALSA and V4L2
259 * Must NOT be called with the hw_lock held.
261 int go7007_register_encoder(struct go7007
*go
, unsigned num_i2c_devs
)
265 dev_info(go
->dev
, "go7007: registering new %s\n", go
->name
);
267 go
->v4l2_dev
.release
= go7007_remove
;
268 ret
= v4l2_device_register(go
->dev
, &go
->v4l2_dev
);
272 mutex_lock(&go
->hw_lock
);
273 ret
= go7007_init_encoder(go
);
274 mutex_unlock(&go
->hw_lock
);
278 ret
= go7007_v4l2_ctrl_init(go
);
282 if (!go
->i2c_adapter_online
&&
283 go
->board_info
->flags
& GO7007_BOARD_USE_ONBOARD_I2C
) {
284 ret
= go7007_i2c_init(go
);
287 go
->i2c_adapter_online
= 1;
289 if (go
->i2c_adapter_online
) {
290 if (go
->board_id
== GO7007_BOARDID_ADS_USBAV_709
) {
291 /* Reset the TW9906 */
292 go7007_write_addr(go
, 0x3c82, 0x0009);
294 go7007_write_addr(go
, 0x3c82, 0x000d);
296 for (i
= 0; i
< num_i2c_devs
; ++i
)
297 init_i2c_module(&go
->i2c_adapter
, &go
->board_info
->i2c_devs
[i
]);
299 if (go
->tuner_type
>= 0) {
300 struct tuner_setup setup
= {
302 .type
= go
->tuner_type
,
303 .mode_mask
= T_ANALOG_TV
,
306 v4l2_device_call_all(&go
->v4l2_dev
, 0, tuner
,
307 s_type_addr
, &setup
);
309 if (go
->board_id
== GO7007_BOARDID_ADLINK_MPG24
)
310 v4l2_subdev_call(go
->sd_video
, video
, s_routing
,
311 0, 0, go
->channel_number
+ 1);
314 ret
= go7007_v4l2_init(go
);
318 if (go
->board_info
->flags
& GO7007_BOARD_HAS_AUDIO
) {
319 go
->audio_enabled
= 1;
324 EXPORT_SYMBOL(go7007_register_encoder
);
327 * Send the encode firmware to the encoder, which will cause it
328 * to immediately start delivering the video and audio streams.
330 * Must be called with the hw_lock held.
332 int go7007_start_encoder(struct go7007
*go
)
335 int fw_len
, rv
= 0, i
;
336 u16 intr_val
, intr_data
;
338 go
->modet_enable
= 0;
340 for (i
= 0; i
< 4; ++i
) {
341 if (go
->modet
[i
].enable
) {
342 go
->modet_enable
= 1;
345 go
->modet
[i
].pixel_threshold
= 32767;
346 go
->modet
[i
].motion_threshold
= 32767;
347 go
->modet
[i
].mb_threshold
= 32767;
350 if (go7007_construct_fw_image(go
, &fw
, &fw_len
) < 0)
353 if (go7007_send_firmware(go
, fw
, fw_len
) < 0 ||
354 go7007_read_interrupt(go
, &intr_val
, &intr_data
) < 0) {
355 v4l2_err(&go
->v4l2_dev
, "error transferring firmware\n");
360 go
->state
= STATE_DATA
;
361 go
->parse_length
= 0;
363 if (go7007_stream_start(go
) < 0) {
364 v4l2_err(&go
->v4l2_dev
, "error starting stream transfer\n");
375 * Store a byte in the current video buffer, if there is one.
377 static inline void store_byte(struct go7007_buffer
*vb
, u8 byte
)
379 if (vb
&& vb
->vb
.v4l2_planes
[0].bytesused
< GO7007_BUF_SIZE
) {
380 u8
*ptr
= vb2_plane_vaddr(&vb
->vb
, 0);
382 ptr
[vb
->vb
.v4l2_planes
[0].bytesused
++] = byte
;
387 * Deliver the last video buffer and get a new one to start writing to.
389 static struct go7007_buffer
*frame_boundary(struct go7007
*go
, struct go7007_buffer
*vb
)
391 struct go7007_buffer
*vb_tmp
= NULL
;
392 u32
*bytesused
= &vb
->vb
.v4l2_planes
[0].bytesused
;
396 if (vb
->modet_active
) {
397 if (*bytesused
+ 216 < GO7007_BUF_SIZE
) {
398 for (i
= 0; i
< 216; ++i
)
399 store_byte(vb
, go
->active_map
[i
]);
402 vb
->modet_active
= 0;
404 vb
->vb
.v4l2_buf
.sequence
= go
->next_seq
++;
405 v4l2_get_timestamp(&vb
->vb
.v4l2_buf
.timestamp
);
407 spin_lock(&go
->spinlock
);
409 if (list_empty(&go
->vidq_active
))
412 vb
= list_first_entry(&go
->vidq_active
, struct go7007_buffer
, list
);
414 spin_unlock(&go
->spinlock
);
415 vb2_buffer_done(&vb_tmp
->vb
, VB2_BUF_STATE_DONE
);
418 spin_lock(&go
->spinlock
);
419 if (!list_empty(&go
->vidq_active
))
420 vb
= go
->active_buf
=
421 list_first_entry(&go
->vidq_active
, struct go7007_buffer
, list
);
422 spin_unlock(&go
->spinlock
);
427 static void write_bitmap_word(struct go7007
*go
)
429 int x
, y
, i
, stride
= ((go
->width
>> 4) + 7) >> 3;
431 for (i
= 0; i
< 16; ++i
) {
432 y
= (((go
->parse_length
- 1) << 3) + i
) / (go
->width
>> 4);
433 x
= (((go
->parse_length
- 1) << 3) + i
) % (go
->width
>> 4);
434 if (stride
* y
+ (x
>> 3) < sizeof(go
->active_map
))
435 go
->active_map
[stride
* y
+ (x
>> 3)] |=
436 (go
->modet_word
& 1) << (x
& 0x7);
437 go
->modet_word
>>= 1;
442 * Parse a chunk of the video stream into frames. The frames are not
443 * delimited by the hardware, so we have to parse the frame boundaries
444 * based on the type of video stream we're receiving.
446 void go7007_parse_video_stream(struct go7007
*go
, u8
*buf
, int length
)
448 struct go7007_buffer
*vb
= go
->active_buf
;
449 int i
, seq_start_code
= -1, gop_start_code
= -1, frame_start_code
= -1;
451 switch (go
->format
) {
452 case V4L2_PIX_FMT_MPEG4
:
453 seq_start_code
= 0xB0;
454 gop_start_code
= 0xB3;
455 frame_start_code
= 0xB6;
457 case V4L2_PIX_FMT_MPEG1
:
458 case V4L2_PIX_FMT_MPEG2
:
459 seq_start_code
= 0xB3;
460 gop_start_code
= 0xB8;
461 frame_start_code
= 0x00;
465 for (i
= 0; i
< length
; ++i
) {
466 if (vb
&& vb
->vb
.v4l2_planes
[0].bytesused
>= GO7007_BUF_SIZE
- 3) {
467 v4l2_info(&go
->v4l2_dev
, "dropping oversized frame\n");
468 vb
->vb
.v4l2_planes
[0].bytesused
= 0;
469 vb
->frame_offset
= 0;
470 vb
->modet_active
= 0;
471 vb
= go
->active_buf
= NULL
;
478 go
->state
= STATE_00
;
481 go
->state
= STATE_FF
;
484 store_byte(vb
, buf
[i
]);
491 go
->state
= STATE_00_00
;
494 store_byte(vb
, 0x00);
495 go
->state
= STATE_FF
;
498 store_byte(vb
, 0x00);
499 store_byte(vb
, buf
[i
]);
500 go
->state
= STATE_DATA
;
507 store_byte(vb
, 0x00);
508 /* go->state remains STATE_00_00 */
511 go
->state
= STATE_00_00_01
;
514 store_byte(vb
, 0x00);
515 store_byte(vb
, 0x00);
516 go
->state
= STATE_FF
;
519 store_byte(vb
, 0x00);
520 store_byte(vb
, 0x00);
521 store_byte(vb
, buf
[i
]);
522 go
->state
= STATE_DATA
;
527 if (buf
[i
] == 0xF8 && go
->modet_enable
== 0) {
528 /* MODET start code, but MODET not enabled */
529 store_byte(vb
, 0x00);
530 store_byte(vb
, 0x00);
531 store_byte(vb
, 0x01);
532 store_byte(vb
, 0xF8);
533 go
->state
= STATE_DATA
;
536 /* If this is the start of a new MPEG frame,
537 * get a new buffer */
538 if ((go
->format
== V4L2_PIX_FMT_MPEG1
||
539 go
->format
== V4L2_PIX_FMT_MPEG2
||
540 go
->format
== V4L2_PIX_FMT_MPEG4
) &&
541 (buf
[i
] == seq_start_code
||
542 buf
[i
] == gop_start_code
||
543 buf
[i
] == frame_start_code
)) {
544 if (vb
== NULL
|| go
->seen_frame
)
545 vb
= frame_boundary(go
, vb
);
546 go
->seen_frame
= buf
[i
] == frame_start_code
;
547 if (vb
&& go
->seen_frame
)
548 vb
->frame_offset
= vb
->vb
.v4l2_planes
[0].bytesused
;
550 /* Handle any special chunk types, or just write the
551 * start code to the (potentially new) buffer */
553 case 0xF5: /* timestamp */
554 go
->parse_length
= 12;
555 go
->state
= STATE_UNPARSED
;
558 go
->state
= STATE_VBI_LEN_A
;
560 case 0xF8: /* MD map */
561 go
->parse_length
= 0;
562 memset(go
->active_map
, 0,
563 sizeof(go
->active_map
));
564 go
->state
= STATE_MODET_MAP
;
566 case 0xFF: /* Potential JPEG start code */
567 store_byte(vb
, 0x00);
568 store_byte(vb
, 0x00);
569 store_byte(vb
, 0x01);
570 go
->state
= STATE_FF
;
573 store_byte(vb
, 0x00);
574 store_byte(vb
, 0x00);
575 store_byte(vb
, 0x01);
576 store_byte(vb
, buf
[i
]);
577 go
->state
= STATE_DATA
;
584 store_byte(vb
, 0xFF);
585 go
->state
= STATE_00
;
588 store_byte(vb
, 0xFF);
589 /* go->state remains STATE_FF */
592 if (go
->format
== V4L2_PIX_FMT_MJPEG
)
593 vb
= frame_boundary(go
, vb
);
596 store_byte(vb
, 0xFF);
597 store_byte(vb
, buf
[i
]);
598 go
->state
= STATE_DATA
;
602 case STATE_VBI_LEN_A
:
603 go
->parse_length
= buf
[i
] << 8;
604 go
->state
= STATE_VBI_LEN_B
;
606 case STATE_VBI_LEN_B
:
607 go
->parse_length
|= buf
[i
];
608 if (go
->parse_length
> 0)
609 go
->state
= STATE_UNPARSED
;
611 go
->state
= STATE_DATA
;
613 case STATE_MODET_MAP
:
614 if (go
->parse_length
< 204) {
615 if (go
->parse_length
& 1) {
616 go
->modet_word
|= buf
[i
];
617 write_bitmap_word(go
);
619 go
->modet_word
= buf
[i
] << 8;
620 } else if (go
->parse_length
== 207 && vb
) {
621 vb
->modet_active
= buf
[i
];
623 if (++go
->parse_length
== 208)
624 go
->state
= STATE_DATA
;
627 if (--go
->parse_length
== 0)
628 go
->state
= STATE_DATA
;
633 EXPORT_SYMBOL(go7007_parse_video_stream
);
636 * Allocate a new go7007 struct. Used by the hardware-specific probe.
638 struct go7007
*go7007_alloc(const struct go7007_board_info
*board
,
644 go
= kzalloc(sizeof(struct go7007
), GFP_KERNEL
);
648 go
->board_info
= board
;
651 go
->channel_number
= 0;
653 mutex_init(&go
->hw_lock
);
654 init_waitqueue_head(&go
->frame_waitq
);
655 spin_lock_init(&go
->spinlock
);
656 go
->status
= STATUS_INIT
;
657 memset(&go
->i2c_adapter
, 0, sizeof(go
->i2c_adapter
));
658 go
->i2c_adapter_online
= 0;
659 go
->interrupt_available
= 0;
660 init_waitqueue_head(&go
->interrupt_waitq
);
662 go7007_update_board(go
);
663 go
->encoder_h_halve
= 0;
664 go
->encoder_v_halve
= 0;
665 go
->encoder_subsample
= 0;
666 go
->format
= V4L2_PIX_FMT_MJPEG
;
667 go
->bitrate
= 1500000;
670 go
->aspect_ratio
= GO7007_RATIO_1_1
;
674 go
->repeat_seqhead
= 0;
675 go
->seq_header_enable
= 0;
676 go
->gop_header_enable
= 0;
678 go
->interlace_coding
= 0;
679 for (i
= 0; i
< 4; ++i
)
680 go
->modet
[i
].enable
= 0;
681 for (i
= 0; i
< 1624; ++i
)
682 go
->modet_map
[i
] = 0;
683 go
->audio_deliver
= NULL
;
684 go
->audio_enabled
= 0;
688 EXPORT_SYMBOL(go7007_alloc
);
690 void go7007_update_board(struct go7007
*go
)
692 const struct go7007_board_info
*board
= go
->board_info
;
694 if (board
->sensor_flags
& GO7007_SENSOR_TV
) {
695 go
->standard
= GO7007_STD_NTSC
;
696 go
->std
= V4L2_STD_NTSC_M
;
699 go
->sensor_framerate
= 30000;
701 go
->standard
= GO7007_STD_OTHER
;
702 go
->width
= board
->sensor_width
;
703 go
->height
= board
->sensor_height
;
704 go
->sensor_framerate
= board
->sensor_framerate
;
706 go
->encoder_v_offset
= board
->sensor_v_offset
;
707 go
->encoder_h_offset
= board
->sensor_h_offset
;
709 EXPORT_SYMBOL(go7007_update_board
);
711 MODULE_LICENSE("GPL v2");