added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / staging / go7007 / go7007-driver.c
blob58bfc8d81b3b3f6a25203c0767bcdad8907ee639
1 /*
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>
25 #include <linux/mm.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"
38 #include "wis-i2c.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");
53 return -1;
55 if (!go->interrupt_available)
56 return -1;
57 go->interrupt_available = 0;
58 *value = go->interrupt_value & 0xfffe;
59 *data = go->interrupt_data;
60 return 0;
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)
71 int count = 100;
72 u16 value;
74 if (go7007_write_interrupt(go, 0x0010, addr) < 0)
75 return -EIO;
76 while (count-- > 0) {
77 if (go7007_read_interrupt(go, &value, data) == 0 &&
78 value == 0xa000)
79 return 0;
81 return -EIO;
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";
95 void *bounce;
96 int fw_len, rv = 0;
97 u16 intr_val, intr_data;
99 if (request_firmware(&fw_entry, fw_name, go->dev)) {
100 printk(KERN_ERR
101 "go7007: unable to load firmware from file \"%s\"\n",
102 fw_name);
103 return -1;
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);
109 return -1;
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);
117 return -1;
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");
126 rv = -1;
128 kfree(bounce);
129 return rv;
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)
141 int ret;
143 down(&go->hw_lock);
144 ret = go7007_load_encoder(go);
145 up(&go->hw_lock);
146 if (ret < 0)
147 return -1;
148 if (!init_i2c)
149 return 0;
150 if (go7007_i2c_init(go) < 0)
151 return -1;
152 go->i2c_adapter_online = 1;
153 return 0;
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);
175 return 0;
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)
187 return -1;
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, int id, int addr)
196 char *modname;
198 switch (id) {
199 case I2C_DRIVERID_WIS_SAA7115:
200 modname = "wis-saa7115";
201 break;
202 case I2C_DRIVERID_WIS_SAA7113:
203 modname = "wis-saa7113";
204 break;
205 case I2C_DRIVERID_WIS_UDA1342:
206 modname = "wis-uda1342";
207 break;
208 case I2C_DRIVERID_WIS_SONY_TUNER:
209 modname = "wis-sony-tuner";
210 break;
211 case I2C_DRIVERID_WIS_TW9903:
212 modname = "wis-tw9903";
213 break;
214 case I2C_DRIVERID_WIS_TW2804:
215 modname = "wis-tw2804";
216 break;
217 case I2C_DRIVERID_WIS_OV7640:
218 modname = "wis-ov7640";
219 break;
220 case I2C_DRIVERID_S2250:
221 modname = "s2250-board";
222 break;
223 default:
224 modname = NULL;
225 break;
227 if (modname != NULL)
228 request_module(modname);
229 if (wis_i2c_probe_device(adapter, id, addr) == 1)
230 return 0;
231 if (modname != NULL)
232 printk(KERN_INFO
233 "go7007: probing for module %s failed\n", modname);
234 else
235 printk(KERN_INFO
236 "go7007: sensor %u seems to be unsupported!\n", id);
237 return -1;
241 * Finalize the GO7007 hardware setup, register the on-board I2C adapter
242 * (if used on this board), load the I2C client driver for the sensor
243 * (SAA7115 or whatever) and other devices, and register the ALSA and V4L2
244 * interfaces.
246 * Must NOT be called with the hw_lock held.
248 int go7007_register_encoder(struct go7007 *go)
250 int i, ret;
252 printk(KERN_INFO "go7007: registering new %s\n", go->name);
254 down(&go->hw_lock);
255 ret = go7007_init_encoder(go);
256 up(&go->hw_lock);
257 if (ret < 0)
258 return -1;
260 if (!go->i2c_adapter_online &&
261 go->board_info->flags & GO7007_BOARD_USE_ONBOARD_I2C) {
262 if (go7007_i2c_init(go) < 0)
263 return -1;
264 go->i2c_adapter_online = 1;
266 if (go->i2c_adapter_online) {
267 for (i = 0; i < go->board_info->num_i2c_devs; ++i)
268 init_i2c_module(&go->i2c_adapter,
269 go->board_info->i2c_devs[i].id,
270 go->board_info->i2c_devs[i].addr);
271 #ifdef TUNER_SET_TYPE_ADDR
272 if (go->tuner_type >= 0) {
273 struct tuner_setup tun_setup = {
274 .mode_mask = T_ANALOG_TV,
275 .addr = ADDR_UNSET,
276 .type = go->tuner_type
278 i2c_clients_command(&go->i2c_adapter,
279 TUNER_SET_TYPE_ADDR, &tun_setup);
281 #else
282 if (go->tuner_type >= 0)
283 i2c_clients_command(&go->i2c_adapter,
284 TUNER_SET_TYPE, &go->tuner_type);
285 #endif
286 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24)
287 i2c_clients_command(&go->i2c_adapter,
288 DECODER_SET_CHANNEL, &go->channel_number);
290 if (go->board_info->flags & GO7007_BOARD_HAS_AUDIO) {
291 go->audio_enabled = 1;
292 go7007_snd_init(go);
294 return go7007_v4l2_init(go);
296 EXPORT_SYMBOL(go7007_register_encoder);
299 * Send the encode firmware to the encoder, which will cause it
300 * to immediately start delivering the video and audio streams.
302 * Must be called with the hw_lock held.
304 int go7007_start_encoder(struct go7007 *go)
306 u8 *fw;
307 int fw_len, rv = 0, i;
308 u16 intr_val, intr_data;
310 go->modet_enable = 0;
311 if (!go->dvd_mode)
312 for (i = 0; i < 4; ++i) {
313 if (go->modet[i].enable) {
314 go->modet_enable = 1;
315 continue;
317 go->modet[i].pixel_threshold = 32767;
318 go->modet[i].motion_threshold = 32767;
319 go->modet[i].mb_threshold = 32767;
322 if (go7007_construct_fw_image(go, &fw, &fw_len) < 0)
323 return -1;
325 if (go7007_send_firmware(go, fw, fw_len) < 0 ||
326 go7007_read_interrupt(go, &intr_val, &intr_data) < 0) {
327 printk(KERN_ERR "go7007: error transferring firmware\n");
328 rv = -1;
329 goto start_error;
332 go->state = STATE_DATA;
333 go->parse_length = 0;
334 go->seen_frame = 0;
335 if (go7007_stream_start(go) < 0) {
336 printk(KERN_ERR "go7007: error starting stream transfer\n");
337 rv = -1;
338 goto start_error;
341 start_error:
342 kfree(fw);
343 return rv;
347 * Store a byte in the current video buffer, if there is one.
349 static inline void store_byte(struct go7007_buffer *gobuf, u8 byte)
351 if (gobuf != NULL && gobuf->bytesused < GO7007_BUF_SIZE) {
352 unsigned int pgidx = gobuf->offset >> PAGE_SHIFT;
353 unsigned int pgoff = gobuf->offset & ~PAGE_MASK;
355 *((u8 *)page_address(gobuf->pages[pgidx]) + pgoff) = byte;
356 ++gobuf->offset;
357 ++gobuf->bytesused;
362 * Deliver the last video buffer and get a new one to start writing to.
364 static void frame_boundary(struct go7007 *go)
366 struct go7007_buffer *gobuf;
367 int i;
369 if (go->active_buf) {
370 if (go->active_buf->modet_active) {
371 if (go->active_buf->bytesused + 216 < GO7007_BUF_SIZE) {
372 for (i = 0; i < 216; ++i)
373 store_byte(go->active_buf,
374 go->active_map[i]);
375 go->active_buf->bytesused -= 216;
376 } else
377 go->active_buf->modet_active = 0;
379 go->active_buf->state = BUF_STATE_DONE;
380 wake_up_interruptible(&go->frame_waitq);
381 go->active_buf = NULL;
383 list_for_each_entry(gobuf, &go->stream, stream)
384 if (gobuf->state == BUF_STATE_QUEUED) {
385 gobuf->seq = go->next_seq;
386 do_gettimeofday(&gobuf->timestamp);
387 go->active_buf = gobuf;
388 break;
390 ++go->next_seq;
393 static void write_bitmap_word(struct go7007 *go)
395 int x, y, i, stride = ((go->width >> 4) + 7) >> 3;
397 for (i = 0; i < 16; ++i) {
398 y = (((go->parse_length - 1) << 3) + i) / (go->width >> 4);
399 x = (((go->parse_length - 1) << 3) + i) % (go->width >> 4);
400 go->active_map[stride * y + (x >> 3)] |=
401 (go->modet_word & 1) << (x & 0x7);
402 go->modet_word >>= 1;
407 * Parse a chunk of the video stream into frames. The frames are not
408 * delimited by the hardware, so we have to parse the frame boundaries
409 * based on the type of video stream we're receiving.
411 void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length)
413 int i, seq_start_code = -1, frame_start_code = -1;
415 spin_lock(&go->spinlock);
417 switch (go->format) {
418 case GO7007_FORMAT_MPEG4:
419 seq_start_code = 0xB0;
420 frame_start_code = 0xB6;
421 break;
422 case GO7007_FORMAT_MPEG1:
423 case GO7007_FORMAT_MPEG2:
424 seq_start_code = 0xB3;
425 frame_start_code = 0x00;
426 break;
429 for (i = 0; i < length; ++i) {
430 if (go->active_buf != NULL &&
431 go->active_buf->bytesused >= GO7007_BUF_SIZE - 3) {
432 printk(KERN_DEBUG "go7007: dropping oversized frame\n");
433 go->active_buf->offset -= go->active_buf->bytesused;
434 go->active_buf->bytesused = 0;
435 go->active_buf->modet_active = 0;
436 go->active_buf = NULL;
439 switch (go->state) {
440 case STATE_DATA:
441 switch (buf[i]) {
442 case 0x00:
443 go->state = STATE_00;
444 break;
445 case 0xFF:
446 go->state = STATE_FF;
447 break;
448 default:
449 store_byte(go->active_buf, buf[i]);
450 break;
452 break;
453 case STATE_00:
454 switch (buf[i]) {
455 case 0x00:
456 go->state = STATE_00_00;
457 break;
458 case 0xFF:
459 store_byte(go->active_buf, 0x00);
460 go->state = STATE_FF;
461 break;
462 default:
463 store_byte(go->active_buf, 0x00);
464 store_byte(go->active_buf, buf[i]);
465 go->state = STATE_DATA;
466 break;
468 break;
469 case STATE_00_00:
470 switch (buf[i]) {
471 case 0x00:
472 store_byte(go->active_buf, 0x00);
473 /* go->state remains STATE_00_00 */
474 break;
475 case 0x01:
476 go->state = STATE_00_00_01;
477 break;
478 case 0xFF:
479 store_byte(go->active_buf, 0x00);
480 store_byte(go->active_buf, 0x00);
481 go->state = STATE_FF;
482 break;
483 default:
484 store_byte(go->active_buf, 0x00);
485 store_byte(go->active_buf, 0x00);
486 store_byte(go->active_buf, buf[i]);
487 go->state = STATE_DATA;
488 break;
490 break;
491 case STATE_00_00_01:
492 /* If this is the start of a new MPEG frame,
493 * get a new buffer */
494 if ((go->format == GO7007_FORMAT_MPEG1 ||
495 go->format == GO7007_FORMAT_MPEG2 ||
496 go->format == GO7007_FORMAT_MPEG4) &&
497 (buf[i] == seq_start_code ||
498 buf[i] == 0xB8 || /* GOP code */
499 buf[i] == frame_start_code)) {
500 if (go->active_buf == NULL || go->seen_frame)
501 frame_boundary(go);
502 if (buf[i] == frame_start_code) {
503 if (go->active_buf != NULL)
504 go->active_buf->frame_offset =
505 go->active_buf->offset;
506 go->seen_frame = 1;
507 } else {
508 go->seen_frame = 0;
511 /* Handle any special chunk types, or just write the
512 * start code to the (potentially new) buffer */
513 switch (buf[i]) {
514 case 0xF5: /* timestamp */
515 go->parse_length = 12;
516 go->state = STATE_UNPARSED;
517 break;
518 case 0xF6: /* vbi */
519 go->state = STATE_VBI_LEN_A;
520 break;
521 case 0xF8: /* MD map */
522 go->parse_length = 0;
523 memset(go->active_map, 0,
524 sizeof(go->active_map));
525 go->state = STATE_MODET_MAP;
526 break;
527 case 0xFF: /* Potential JPEG start code */
528 store_byte(go->active_buf, 0x00);
529 store_byte(go->active_buf, 0x00);
530 store_byte(go->active_buf, 0x01);
531 go->state = STATE_FF;
532 break;
533 default:
534 store_byte(go->active_buf, 0x00);
535 store_byte(go->active_buf, 0x00);
536 store_byte(go->active_buf, 0x01);
537 store_byte(go->active_buf, buf[i]);
538 go->state = STATE_DATA;
539 break;
541 break;
542 case STATE_FF:
543 switch (buf[i]) {
544 case 0x00:
545 store_byte(go->active_buf, 0xFF);
546 go->state = STATE_00;
547 break;
548 case 0xFF:
549 store_byte(go->active_buf, 0xFF);
550 /* go->state remains STATE_FF */
551 break;
552 case 0xD8:
553 if (go->format == GO7007_FORMAT_MJPEG)
554 frame_boundary(go);
555 /* fall through */
556 default:
557 store_byte(go->active_buf, 0xFF);
558 store_byte(go->active_buf, buf[i]);
559 go->state = STATE_DATA;
560 break;
562 break;
563 case STATE_VBI_LEN_A:
564 go->parse_length = buf[i] << 8;
565 go->state = STATE_VBI_LEN_B;
566 break;
567 case STATE_VBI_LEN_B:
568 go->parse_length |= buf[i];
569 if (go->parse_length > 0)
570 go->state = STATE_UNPARSED;
571 else
572 go->state = STATE_DATA;
573 break;
574 case STATE_MODET_MAP:
575 if (go->parse_length < 204) {
576 if (go->parse_length & 1) {
577 go->modet_word |= buf[i];
578 write_bitmap_word(go);
579 } else
580 go->modet_word = buf[i] << 8;
581 } else if (go->parse_length == 207 && go->active_buf) {
582 go->active_buf->modet_active = buf[i];
584 if (++go->parse_length == 208)
585 go->state = STATE_DATA;
586 break;
587 case STATE_UNPARSED:
588 if (--go->parse_length == 0)
589 go->state = STATE_DATA;
590 break;
594 spin_unlock(&go->spinlock);
596 EXPORT_SYMBOL(go7007_parse_video_stream);
599 * Allocate a new go7007 struct. Used by the hardware-specific probe.
601 struct go7007 *go7007_alloc(struct go7007_board_info *board, struct device *dev)
603 struct go7007 *go;
604 int i;
606 go = kmalloc(sizeof(struct go7007), GFP_KERNEL);
607 if (go == NULL)
608 return NULL;
609 go->dev = dev;
610 go->board_info = board;
611 go->board_id = 0;
612 go->tuner_type = -1;
613 go->channel_number = 0;
614 go->name[0] = 0;
615 init_MUTEX(&go->hw_lock);
616 init_waitqueue_head(&go->frame_waitq);
617 spin_lock_init(&go->spinlock);
618 go->video_dev = NULL;
619 go->ref_count = 0;
620 go->status = STATUS_INIT;
621 memset(&go->i2c_adapter, 0, sizeof(go->i2c_adapter));
622 go->i2c_adapter_online = 0;
623 go->interrupt_available = 0;
624 init_waitqueue_head(&go->interrupt_waitq);
625 go->in_use = 0;
626 go->input = 0;
627 if (board->sensor_flags & GO7007_SENSOR_TV) {
628 go->standard = GO7007_STD_NTSC;
629 go->width = 720;
630 go->height = 480;
631 go->sensor_framerate = 30000;
632 } else {
633 go->standard = GO7007_STD_OTHER;
634 go->width = board->sensor_width;
635 go->height = board->sensor_height;
636 go->sensor_framerate = board->sensor_framerate;
638 go->encoder_v_offset = board->sensor_v_offset;
639 go->encoder_h_offset = board->sensor_h_offset;
640 go->encoder_h_halve = 0;
641 go->encoder_v_halve = 0;
642 go->encoder_subsample = 0;
643 go->streaming = 0;
644 go->format = GO7007_FORMAT_MJPEG;
645 go->bitrate = 1500000;
646 go->fps_scale = 1;
647 go->pali = 0;
648 go->aspect_ratio = GO7007_RATIO_1_1;
649 go->gop_size = 0;
650 go->ipb = 0;
651 go->closed_gop = 0;
652 go->repeat_seqhead = 0;
653 go->seq_header_enable = 0;
654 go->gop_header_enable = 0;
655 go->dvd_mode = 0;
656 go->interlace_coding = 0;
657 for (i = 0; i < 4; ++i)
658 go->modet[i].enable = 0;;
659 for (i = 0; i < 1624; ++i)
660 go->modet_map[i] = 0;
661 go->audio_deliver = NULL;
662 go->audio_enabled = 0;
663 INIT_LIST_HEAD(&go->stream);
665 return go;
667 EXPORT_SYMBOL(go7007_alloc);
670 * Detach and unregister the encoder. The go7007 struct won't be freed
671 * until v4l2 finishes releasing its resources and all associated fds are
672 * closed by applications.
674 void go7007_remove(struct go7007 *go)
676 if (go->i2c_adapter_online) {
677 if (i2c_del_adapter(&go->i2c_adapter) == 0)
678 go->i2c_adapter_online = 0;
679 else
680 printk(KERN_ERR
681 "go7007: error removing I2C adapter!\n");
684 if (go->audio_enabled)
685 go7007_snd_remove(go);
686 go7007_v4l2_remove(go);
688 EXPORT_SYMBOL(go7007_remove);
690 MODULE_LICENSE("GPL v2");