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