Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[deliverable/linux.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>
866b8695
GKH
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>
fd9a40da 30#include <linux/mutex.h>
866b8695
GKH
31#include <linux/uaccess.h>
32#include <asm/system.h>
df20d69e 33#include <linux/videodev2.h>
866b8695
GKH
34#include <media/tuner.h>
35#include <media/v4l2-common.h>
36
37#include "go7007-priv.h"
38#include "wis-i2c.h"
39
40/*
41 * Wait for an interrupt to be delivered from the GO7007SB and return
42 * the associated value and data.
43 *
44 * Must be called with the hw_lock held.
45 */
46int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data)
47{
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) {
62474076 52 v4l2_err(go->video_dev, "timeout waiting for read interrupt\n");
866b8695
GKH
53 return -1;
54 }
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;
61}
62EXPORT_SYMBOL(go7007_read_interrupt);
63
64/*
65 * Read a register/address on the GO7007SB.
66 *
67 * Must be called with the hw_lock held.
68 */
69int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data)
70{
71 int count = 100;
72 u16 value;
73
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;
80 }
81 return -EIO;
82}
83EXPORT_SYMBOL(go7007_read_addr);
84
85/*
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.
88 *
89 * Must be called with the hw_lock held.
90 */
91static int go7007_load_encoder(struct go7007 *go)
92{
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;
98
99 if (request_firmware(&fw_entry, fw_name, go->dev)) {
62474076
PE
100 v4l2_err(go, "unable to load firmware from file "
101 "\"%s\"\n", fw_name);
866b8695
GKH
102 return -1;
103 }
104 if (fw_entry->size < 16 || memcmp(fw_entry->data, "WISGO7007FW", 11)) {
62474076 105 v4l2_err(go, "file \"%s\" does not appear to be "
866b8695
GKH
106 "go7007 firmware\n", fw_name);
107 release_firmware(fw_entry);
108 return -1;
109 }
110 fw_len = fw_entry->size - 16;
111 bounce = kmalloc(fw_len, GFP_KERNEL);
112 if (bounce == NULL) {
62474076 113 v4l2_err(go, "unable to allocate %d bytes for "
866b8695
GKH
114 "firmware transfer\n", fw_len);
115 release_firmware(fw_entry);
116 return -1;
117 }
118 memcpy(bounce, fw_entry->data + 16, fw_len);
119 release_firmware(fw_entry);
120 if (go7007_interface_reset(go) < 0 ||
121 go7007_send_firmware(go, bounce, fw_len) < 0 ||
122 go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
123 (intr_val & ~0x1) != 0x5a5a) {
62474076 124 v4l2_err(go, "error transferring firmware\n");
866b8695
GKH
125 rv = -1;
126 }
127 kfree(bounce);
128 return rv;
129}
130
131/*
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.
135 *
136 * Must NOT be called with the hw_lock held.
137 */
138int go7007_boot_encoder(struct go7007 *go, int init_i2c)
139{
140 int ret;
141
fd9a40da 142 mutex_lock(&go->hw_lock);
866b8695 143 ret = go7007_load_encoder(go);
fd9a40da 144 mutex_unlock(&go->hw_lock);
866b8695
GKH
145 if (ret < 0)
146 return -1;
147 if (!init_i2c)
148 return 0;
149 if (go7007_i2c_init(go) < 0)
150 return -1;
151 go->i2c_adapter_online = 1;
152 return 0;
153}
154EXPORT_SYMBOL(go7007_boot_encoder);
155
156/*
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.
160 *
161 * Must be called with the hw_lock held.
162 */
163static int go7007_init_encoder(struct go7007 *go)
164{
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);
168 }
169 if (go->board_id == 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);
173 }
174 return 0;
175}
176
177/*
178 * Send the boot firmware to the GO7007 and configure the registers. This
179 * is the only way to stop the encoder once it has started streaming video.
180 *
181 * Must be called with the hw_lock held.
182 */
183int go7007_reset_encoder(struct go7007 *go)
184{
185 if (go7007_load_encoder(go) < 0)
186 return -1;
187 return go7007_init_encoder(go);
188}
189
190/*
191 * Attempt to instantiate an I2C client by ID, probably loading a module.
192 */
7400516a
JD
193static int init_i2c_module(struct i2c_adapter *adapter, const char *type,
194 int id, int addr)
866b8695 195{
7400516a 196 struct i2c_board_info info;
866b8695
GKH
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;
b11869db
PE
221 case I2C_DRIVERID_S2250:
222 modname = "s2250-board";
223 break;
866b8695
GKH
224 default:
225 modname = NULL;
226 break;
227 }
228 if (modname != NULL)
229 request_module(modname);
7400516a
JD
230
231 memset(&info, 0, sizeof(struct i2c_board_info));
232 info.addr = addr;
233 strlcpy(info.type, type, I2C_NAME_SIZE);
234 if (!i2c_new_device(adapter, &info))
866b8695
GKH
235 return 0;
236 if (modname != NULL)
237 printk(KERN_INFO
d73f822c 238 "go7007: probing for module %s failed\n", modname);
866b8695
GKH
239 else
240 printk(KERN_INFO
241 "go7007: sensor %u seems to be unsupported!\n", id);
242 return -1;
243}
244
245/*
246 * Finalize the GO7007 hardware setup, register the on-board I2C adapter
247 * (if used on this board), load the I2C client driver for the sensor
248 * (SAA7115 or whatever) and other devices, and register the ALSA and V4L2
249 * interfaces.
250 *
251 * Must NOT be called with the hw_lock held.
252 */
253int go7007_register_encoder(struct go7007 *go)
254{
255 int i, ret;
256
257 printk(KERN_INFO "go7007: registering new %s\n", go->name);
258
fd9a40da 259 mutex_lock(&go->hw_lock);
866b8695 260 ret = go7007_init_encoder(go);
fd9a40da 261 mutex_unlock(&go->hw_lock);
866b8695
GKH
262 if (ret < 0)
263 return -1;
264
265 if (!go->i2c_adapter_online &&
266 go->board_info->flags & GO7007_BOARD_USE_ONBOARD_I2C) {
267 if (go7007_i2c_init(go) < 0)
268 return -1;
269 go->i2c_adapter_online = 1;
270 }
271 if (go->i2c_adapter_online) {
272 for (i = 0; i < go->board_info->num_i2c_devs; ++i)
273 init_i2c_module(&go->i2c_adapter,
7400516a 274 go->board_info->i2c_devs[i].type,
866b8695
GKH
275 go->board_info->i2c_devs[i].id,
276 go->board_info->i2c_devs[i].addr);
866b8695
GKH
277 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24)
278 i2c_clients_command(&go->i2c_adapter,
279 DECODER_SET_CHANNEL, &go->channel_number);
280 }
281 if (go->board_info->flags & GO7007_BOARD_HAS_AUDIO) {
282 go->audio_enabled = 1;
283 go7007_snd_init(go);
284 }
285 return go7007_v4l2_init(go);
286}
287EXPORT_SYMBOL(go7007_register_encoder);
288
289/*
290 * Send the encode firmware to the encoder, which will cause it
291 * to immediately start delivering the video and audio streams.
292 *
293 * Must be called with the hw_lock held.
294 */
295int go7007_start_encoder(struct go7007 *go)
296{
297 u8 *fw;
298 int fw_len, rv = 0, i;
299 u16 intr_val, intr_data;
300
301 go->modet_enable = 0;
302 if (!go->dvd_mode)
303 for (i = 0; i < 4; ++i) {
304 if (go->modet[i].enable) {
305 go->modet_enable = 1;
306 continue;
307 }
308 go->modet[i].pixel_threshold = 32767;
309 go->modet[i].motion_threshold = 32767;
310 go->modet[i].mb_threshold = 32767;
311 }
312
313 if (go7007_construct_fw_image(go, &fw, &fw_len) < 0)
314 return -1;
315
316 if (go7007_send_firmware(go, fw, fw_len) < 0 ||
317 go7007_read_interrupt(go, &intr_val, &intr_data) < 0) {
62474076 318 v4l2_err(go->video_dev, "error transferring firmware\n");
866b8695
GKH
319 rv = -1;
320 goto start_error;
321 }
322
323 go->state = STATE_DATA;
324 go->parse_length = 0;
325 go->seen_frame = 0;
326 if (go7007_stream_start(go) < 0) {
62474076 327 v4l2_err(go->video_dev, "error starting stream transfer\n");
866b8695
GKH
328 rv = -1;
329 goto start_error;
330 }
331
332start_error:
333 kfree(fw);
334 return rv;
335}
336
337/*
338 * Store a byte in the current video buffer, if there is one.
339 */
340static inline void store_byte(struct go7007_buffer *gobuf, u8 byte)
341{
342 if (gobuf != NULL && gobuf->bytesused < GO7007_BUF_SIZE) {
343 unsigned int pgidx = gobuf->offset >> PAGE_SHIFT;
344 unsigned int pgoff = gobuf->offset & ~PAGE_MASK;
345
346 *((u8 *)page_address(gobuf->pages[pgidx]) + pgoff) = byte;
347 ++gobuf->offset;
348 ++gobuf->bytesused;
349 }
350}
351
352/*
353 * Deliver the last video buffer and get a new one to start writing to.
354 */
355static void frame_boundary(struct go7007 *go)
356{
357 struct go7007_buffer *gobuf;
358 int i;
359
360 if (go->active_buf) {
361 if (go->active_buf->modet_active) {
362 if (go->active_buf->bytesused + 216 < GO7007_BUF_SIZE) {
363 for (i = 0; i < 216; ++i)
364 store_byte(go->active_buf,
365 go->active_map[i]);
366 go->active_buf->bytesused -= 216;
367 } else
368 go->active_buf->modet_active = 0;
369 }
370 go->active_buf->state = BUF_STATE_DONE;
371 wake_up_interruptible(&go->frame_waitq);
372 go->active_buf = NULL;
373 }
374 list_for_each_entry(gobuf, &go->stream, stream)
375 if (gobuf->state == BUF_STATE_QUEUED) {
376 gobuf->seq = go->next_seq;
377 do_gettimeofday(&gobuf->timestamp);
378 go->active_buf = gobuf;
379 break;
380 }
381 ++go->next_seq;
382}
383
384static void write_bitmap_word(struct go7007 *go)
385{
386 int x, y, i, stride = ((go->width >> 4) + 7) >> 3;
387
388 for (i = 0; i < 16; ++i) {
389 y = (((go->parse_length - 1) << 3) + i) / (go->width >> 4);
390 x = (((go->parse_length - 1) << 3) + i) % (go->width >> 4);
391 go->active_map[stride * y + (x >> 3)] |=
392 (go->modet_word & 1) << (x & 0x7);
393 go->modet_word >>= 1;
394 }
395}
396
397/*
398 * Parse a chunk of the video stream into frames. The frames are not
399 * delimited by the hardware, so we have to parse the frame boundaries
400 * based on the type of video stream we're receiving.
401 */
402void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length)
403{
404 int i, seq_start_code = -1, frame_start_code = -1;
405
406 spin_lock(&go->spinlock);
407
408 switch (go->format) {
409 case GO7007_FORMAT_MPEG4:
410 seq_start_code = 0xB0;
411 frame_start_code = 0xB6;
412 break;
413 case GO7007_FORMAT_MPEG1:
414 case GO7007_FORMAT_MPEG2:
415 seq_start_code = 0xB3;
416 frame_start_code = 0x00;
417 break;
418 }
419
420 for (i = 0; i < length; ++i) {
421 if (go->active_buf != NULL &&
422 go->active_buf->bytesused >= GO7007_BUF_SIZE - 3) {
62474076 423 v4l2_info(go->video_dev, "dropping oversized frame\n");
866b8695
GKH
424 go->active_buf->offset -= go->active_buf->bytesused;
425 go->active_buf->bytesused = 0;
426 go->active_buf->modet_active = 0;
427 go->active_buf = NULL;
428 }
429
430 switch (go->state) {
431 case STATE_DATA:
432 switch (buf[i]) {
433 case 0x00:
434 go->state = STATE_00;
435 break;
436 case 0xFF:
437 go->state = STATE_FF;
438 break;
439 default:
440 store_byte(go->active_buf, buf[i]);
441 break;
442 }
443 break;
444 case STATE_00:
445 switch (buf[i]) {
446 case 0x00:
447 go->state = STATE_00_00;
448 break;
449 case 0xFF:
450 store_byte(go->active_buf, 0x00);
451 go->state = STATE_FF;
452 break;
453 default:
454 store_byte(go->active_buf, 0x00);
455 store_byte(go->active_buf, buf[i]);
456 go->state = STATE_DATA;
457 break;
458 }
459 break;
460 case STATE_00_00:
461 switch (buf[i]) {
462 case 0x00:
463 store_byte(go->active_buf, 0x00);
464 /* go->state remains STATE_00_00 */
465 break;
466 case 0x01:
467 go->state = STATE_00_00_01;
468 break;
469 case 0xFF:
470 store_byte(go->active_buf, 0x00);
471 store_byte(go->active_buf, 0x00);
472 go->state = STATE_FF;
473 break;
474 default:
475 store_byte(go->active_buf, 0x00);
476 store_byte(go->active_buf, 0x00);
477 store_byte(go->active_buf, buf[i]);
478 go->state = STATE_DATA;
479 break;
480 }
481 break;
482 case STATE_00_00_01:
483 /* If this is the start of a new MPEG frame,
484 * get a new buffer */
485 if ((go->format == GO7007_FORMAT_MPEG1 ||
486 go->format == GO7007_FORMAT_MPEG2 ||
487 go->format == GO7007_FORMAT_MPEG4) &&
488 (buf[i] == seq_start_code ||
489 buf[i] == 0xB8 || /* GOP code */
490 buf[i] == frame_start_code)) {
491 if (go->active_buf == NULL || go->seen_frame)
492 frame_boundary(go);
493 if (buf[i] == frame_start_code) {
494 if (go->active_buf != NULL)
495 go->active_buf->frame_offset =
496 go->active_buf->offset;
497 go->seen_frame = 1;
498 } else {
499 go->seen_frame = 0;
500 }
501 }
502 /* Handle any special chunk types, or just write the
503 * start code to the (potentially new) buffer */
504 switch (buf[i]) {
505 case 0xF5: /* timestamp */
506 go->parse_length = 12;
507 go->state = STATE_UNPARSED;
508 break;
509 case 0xF6: /* vbi */
510 go->state = STATE_VBI_LEN_A;
511 break;
512 case 0xF8: /* MD map */
513 go->parse_length = 0;
514 memset(go->active_map, 0,
515 sizeof(go->active_map));
516 go->state = STATE_MODET_MAP;
517 break;
518 case 0xFF: /* Potential JPEG start code */
519 store_byte(go->active_buf, 0x00);
520 store_byte(go->active_buf, 0x00);
521 store_byte(go->active_buf, 0x01);
522 go->state = STATE_FF;
523 break;
524 default:
525 store_byte(go->active_buf, 0x00);
526 store_byte(go->active_buf, 0x00);
527 store_byte(go->active_buf, 0x01);
528 store_byte(go->active_buf, buf[i]);
529 go->state = STATE_DATA;
530 break;
531 }
532 break;
533 case STATE_FF:
534 switch (buf[i]) {
535 case 0x00:
536 store_byte(go->active_buf, 0xFF);
537 go->state = STATE_00;
538 break;
539 case 0xFF:
540 store_byte(go->active_buf, 0xFF);
541 /* go->state remains STATE_FF */
542 break;
543 case 0xD8:
544 if (go->format == GO7007_FORMAT_MJPEG)
545 frame_boundary(go);
546 /* fall through */
547 default:
548 store_byte(go->active_buf, 0xFF);
549 store_byte(go->active_buf, buf[i]);
550 go->state = STATE_DATA;
551 break;
552 }
553 break;
554 case STATE_VBI_LEN_A:
555 go->parse_length = buf[i] << 8;
556 go->state = STATE_VBI_LEN_B;
557 break;
558 case STATE_VBI_LEN_B:
559 go->parse_length |= buf[i];
560 if (go->parse_length > 0)
561 go->state = STATE_UNPARSED;
562 else
563 go->state = STATE_DATA;
564 break;
565 case STATE_MODET_MAP:
566 if (go->parse_length < 204) {
567 if (go->parse_length & 1) {
568 go->modet_word |= buf[i];
569 write_bitmap_word(go);
570 } else
571 go->modet_word = buf[i] << 8;
572 } else if (go->parse_length == 207 && go->active_buf) {
573 go->active_buf->modet_active = buf[i];
574 }
575 if (++go->parse_length == 208)
576 go->state = STATE_DATA;
577 break;
578 case STATE_UNPARSED:
579 if (--go->parse_length == 0)
580 go->state = STATE_DATA;
581 break;
582 }
583 }
584
585 spin_unlock(&go->spinlock);
586}
587EXPORT_SYMBOL(go7007_parse_video_stream);
588
589/*
590 * Allocate a new go7007 struct. Used by the hardware-specific probe.
591 */
592struct go7007 *go7007_alloc(struct go7007_board_info *board, struct device *dev)
593{
594 struct go7007 *go;
595 int i;
596
597 go = kmalloc(sizeof(struct go7007), GFP_KERNEL);
598 if (go == NULL)
599 return NULL;
600 go->dev = dev;
601 go->board_info = board;
602 go->board_id = 0;
603 go->tuner_type = -1;
604 go->channel_number = 0;
605 go->name[0] = 0;
fd9a40da 606 mutex_init(&go->hw_lock);
866b8695
GKH
607 init_waitqueue_head(&go->frame_waitq);
608 spin_lock_init(&go->spinlock);
609 go->video_dev = NULL;
610 go->ref_count = 0;
611 go->status = STATUS_INIT;
612 memset(&go->i2c_adapter, 0, sizeof(go->i2c_adapter));
613 go->i2c_adapter_online = 0;
614 go->interrupt_available = 0;
615 init_waitqueue_head(&go->interrupt_waitq);
616 go->in_use = 0;
617 go->input = 0;
618 if (board->sensor_flags & GO7007_SENSOR_TV) {
619 go->standard = GO7007_STD_NTSC;
620 go->width = 720;
621 go->height = 480;
622 go->sensor_framerate = 30000;
623 } else {
624 go->standard = GO7007_STD_OTHER;
625 go->width = board->sensor_width;
626 go->height = board->sensor_height;
627 go->sensor_framerate = board->sensor_framerate;
628 }
629 go->encoder_v_offset = board->sensor_v_offset;
630 go->encoder_h_offset = board->sensor_h_offset;
631 go->encoder_h_halve = 0;
632 go->encoder_v_halve = 0;
633 go->encoder_subsample = 0;
634 go->streaming = 0;
635 go->format = GO7007_FORMAT_MJPEG;
636 go->bitrate = 1500000;
637 go->fps_scale = 1;
638 go->pali = 0;
639 go->aspect_ratio = GO7007_RATIO_1_1;
640 go->gop_size = 0;
641 go->ipb = 0;
642 go->closed_gop = 0;
643 go->repeat_seqhead = 0;
644 go->seq_header_enable = 0;
645 go->gop_header_enable = 0;
646 go->dvd_mode = 0;
647 go->interlace_coding = 0;
648 for (i = 0; i < 4; ++i)
649 go->modet[i].enable = 0;;
650 for (i = 0; i < 1624; ++i)
651 go->modet_map[i] = 0;
652 go->audio_deliver = NULL;
653 go->audio_enabled = 0;
654 INIT_LIST_HEAD(&go->stream);
655
656 return go;
657}
658EXPORT_SYMBOL(go7007_alloc);
659
660/*
661 * Detach and unregister the encoder. The go7007 struct won't be freed
662 * until v4l2 finishes releasing its resources and all associated fds are
663 * closed by applications.
664 */
665void go7007_remove(struct go7007 *go)
666{
667 if (go->i2c_adapter_online) {
668 if (i2c_del_adapter(&go->i2c_adapter) == 0)
669 go->i2c_adapter_online = 0;
670 else
62474076
PE
671 v4l2_err(go->video_dev,
672 "error removing I2C adapter!\n");
866b8695
GKH
673 }
674
675 if (go->audio_enabled)
676 go7007_snd_remove(go);
677 go7007_v4l2_remove(go);
678}
679EXPORT_SYMBOL(go7007_remove);
680
681MODULE_LICENSE("GPL v2");
This page took 0.174232 seconds and 5 git commands to generate.