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