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