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