Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tm6000-core.c
Go to the documentation of this file.
1 /*
2  * tm6000-core.c - driver for TM5600/TM6000/TM6010 USB video capture devices
3  *
4  * Copyright (C) 2006-2007 Mauro Carvalho Chehab <[email protected]>
5  *
6  * Copyright (C) 2007 Michel Ludwig <[email protected]>
7  * - DVB-T support
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation version 2
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22 
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/usb.h>
27 #include <linux/i2c.h>
28 #include "tm6000.h"
29 #include "tm6000-regs.h"
30 #include <media/v4l2-common.h>
31 #include <media/tuner.h>
32 
33 #define USB_TIMEOUT (5 * HZ) /* ms */
34 
35 int tm6000_read_write_usb(struct tm6000_core *dev, u8 req_type, u8 req,
36  u16 value, u16 index, u8 *buf, u16 len)
37 {
38  int ret, i;
39  unsigned int pipe;
40  u8 *data = NULL;
41  int delay = 5000;
42 
43  mutex_lock(&dev->usb_lock);
44 
45  if (len)
46  data = kzalloc(len, GFP_KERNEL);
47 
48  if (req_type & USB_DIR_IN)
49  pipe = usb_rcvctrlpipe(dev->udev, 0);
50  else {
51  pipe = usb_sndctrlpipe(dev->udev, 0);
52  memcpy(data, buf, len);
53  }
54 
56  printk(KERN_DEBUG "(dev %p, pipe %08x): ", dev->udev, pipe);
57 
58  printk(KERN_CONT "%s: %02x %02x %02x %02x %02x %02x %02x %02x ",
59  (req_type & USB_DIR_IN) ? " IN" : "OUT",
60  req_type, req, value&0xff, value>>8, index&0xff,
61  index>>8, len&0xff, len>>8);
62 
63  if (!(req_type & USB_DIR_IN)) {
64  printk(KERN_CONT ">>> ");
65  for (i = 0; i < len; i++)
66  printk(KERN_CONT " %02x", buf[i]);
67  printk(KERN_CONT "\n");
68  }
69  }
70 
71  ret = usb_control_msg(dev->udev, pipe, req, req_type, value, index,
72  data, len, USB_TIMEOUT);
73 
74  if (req_type & USB_DIR_IN)
75  memcpy(buf, data, len);
76 
77  if (tm6000_debug & V4L2_DEBUG_I2C) {
78  if (ret < 0) {
79  if (req_type & USB_DIR_IN)
80  printk(KERN_DEBUG "<<< (len=%d)\n", len);
81 
82  printk(KERN_CONT "%s: Error #%d\n", __func__, ret);
83  } else if (req_type & USB_DIR_IN) {
84  printk(KERN_CONT "<<< ");
85  for (i = 0; i < len; i++)
86  printk(KERN_CONT " %02x", buf[i]);
87  printk(KERN_CONT "\n");
88  }
89  }
90 
91  kfree(data);
92 
94  delay = 0;
95 
96  if (req == REQ_16_SET_GET_I2C_WR1_RDN && !(req_type & USB_DIR_IN)) {
97  unsigned int tsleep;
98  /* Calculate delay time, 14000us for 64 bytes */
99  tsleep = (len * 200) + 200;
100  if (tsleep < delay)
101  tsleep = delay;
102  usleep_range(tsleep, tsleep + 1000);
103  }
104  else if (delay)
105  usleep_range(delay, delay + 1000);
106 
107  mutex_unlock(&dev->usb_lock);
108  return ret;
109 }
110 
112 {
113  return
115  req, value, index, NULL, 0);
116 }
118 
120 {
121  int rc;
122  u8 buf[1];
123 
125  value, index, buf, 1);
126 
127  if (rc < 0)
128  return rc;
129 
130  return *buf;
131 }
133 
135  u16 index, u16 mask)
136 {
137  int rc;
138  u8 buf[1];
139  u8 new_index;
140 
142  value, 0, buf, 1);
143 
144  if (rc < 0)
145  return rc;
146 
147  new_index = (buf[0] & ~mask) | (index & mask);
148 
149  if (new_index == buf[0])
150  return 0;
151 
153  req, value, new_index, NULL, 0);
154 }
156 
158 {
159  int rc;
160  u8 buf[2];
161 
163  value, index, buf, 2);
164 
165  if (rc < 0)
166  return rc;
167 
168  return buf[1]|buf[0]<<8;
169 }
170 
172 {
173  int rc;
174  u8 buf[4];
175 
177  value, index, buf, 4);
178 
179  if (rc < 0)
180  return rc;
181 
182  return buf[3] | buf[2] << 8 | buf[1] << 16 | buf[0] << 24;
183 }
184 
185 int tm6000_i2c_reset(struct tm6000_core *dev, u16 tsleep)
186 {
187  int rc;
188 
190  if (rc < 0)
191  return rc;
192 
193  msleep(tsleep);
194 
196  msleep(tsleep);
197 
198  return rc;
199 }
200 
202 {
203  if (dev->dev_type == TM6010) {
204  int val;
205 
206  val = tm6000_get_reg(dev, TM6010_REQ07_RCC_ACTIVE_IF, 0) & 0xfc;
207  if (dev->fourcc == V4L2_PIX_FMT_UYVY)
209  else
211  } else {
212  if (dev->fourcc == V4L2_PIX_FMT_UYVY)
214  else
216  }
217 }
218 
219 static void tm6000_set_vbi(struct tm6000_core *dev)
220 {
221  /*
222  * FIXME:
223  * VBI lines and start/end are different between 60Hz and 50Hz
224  * So, it is very likely that we need to change the config to
225  * something that takes it into account, doing something different
226  * if (dev->norm & V4L2_STD_525_60)
227  */
228 
229  if (dev->dev_type == TM6010) {
236  tm6000_set_reg(dev,
238  tm6000_set_reg(dev,
240  tm6000_set_reg(dev,
242  tm6000_set_reg(dev,
244  tm6000_set_reg(dev,
246  tm6000_set_reg(dev,
248  tm6000_set_reg(dev,
250  tm6000_set_reg(dev,
252  tm6000_set_reg(dev,
254  tm6000_set_reg(dev,
256  tm6000_set_reg(dev,
258  tm6000_set_reg(dev,
260  tm6000_set_reg(dev,
262  tm6000_set_reg(dev,
264  tm6000_set_reg(dev,
266  tm6000_set_reg(dev,
268  tm6000_set_reg(dev,
270  tm6000_set_reg(dev,
278  }
279 }
280 
282 {
283  struct v4l2_frequency f;
284 
285  if (dev->dev_type == TM6010) {
287 
288  if (!dev->radio)
290 
291  /* Enable video and audio */
293  active, 0x60);
294  /* Disable TS input */
296  0x00, 0x40);
297  } else {
298  /* Enables soft reset */
300 
301  if (dev->scaler)
302  /* Disable Hfilter and Enable TS Drop err */
304  else /* Enable Hfilter and disable TS Drop err */
306 
313 
314  /* AP Software reset */
317 
319 
320  /* Disables soft reset */
322  }
323  msleep(20);
324 
325  /* Tuner firmware can now be loaded */
326 
327  /*
328  * FIXME: This is a hack! xc3028 "sleeps" when no channel is detected
329  * for more than a few seconds. Not sure why, as this behavior does
330  * not happen on other devices with xc3028. So, I suspect that it
331  * is yet another bug at tm6000. After start sleeping, decoding
332  * doesn't start automatically. Instead, it requires some
333  * I2C commands to wake it up. As we want to have image at the
334  * beginning, we needed to add this hack. The better would be to
335  * discover some way to make tm6000 to wake up without this hack.
336  */
337  f.frequency = dev->freq;
338  v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
339 
340  msleep(100);
341  tm6000_set_standard(dev);
342  tm6000_set_vbi(dev);
343  tm6000_set_audio_bitrate(dev, 48000);
344 
345  /* switch dvb led off */
346  if (dev->gpio.dvb_led) {
348  dev->gpio.dvb_led, 0x01);
349  }
350 
351  return 0;
352 }
353 
355 {
356  if (dev->dev_type == TM6010) {
357  /* Disable video and audio */
359  0x00, 0x60);
360  /* Enable TS input */
362  0x40, 0x40);
363  /* all power down, but not the digital data port */
367  } else {
382 
386  msleep(50);
387 
388  tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 0x0020, 0x00);
389  msleep(50);
390  tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 0x0020, 0x01);
391  msleep(50);
392  tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 0x0020, 0x00);
393  msleep(100);
394  }
395 
396  /* switch dvb led on */
397  if (dev->gpio.dvb_led) {
399  dev->gpio.dvb_led, 0x00);
400  }
401 
402  return 0;
403 }
405 
406 struct reg_init {
410 };
411 
412 /* The meaning of those initializations are unknown */
413 static struct reg_init tm6000_init_tab[] = {
414  /* REG VALUE */
416  { TM6010_REQ07_RFF_SOFT_RESET, 0x08 },
417  { TM6010_REQ07_RFF_SOFT_RESET, 0x00 },
418  { TM6010_REQ07_RD5_POWERSAVE, 0x4f },
419  { TM6000_REQ07_RDA_CLK_SEL, 0x23 },
420  { TM6000_REQ07_RDB_OUT_SEL, 0x08 },
425  { TM6000_REQ07_REB_VADC_AADC_MODE, 0x64 }, /* 48000 bits/sample, external input */
427 
428  { TM6010_REQ07_R3F_RESET, 0x01 }, /* Start of soft reset */
475  { TM6010_REQ07_RC1_TRESHOLD, 0xd0 },
476  { TM6010_REQ07_RC3_HSTART1, 0x88 },
477  { TM6010_REQ07_R3F_RESET, 0x00 }, /* End of the soft reset */
478  { TM6010_REQ05_R18_IMASK7, 0x00 },
479 };
480 
481 static struct reg_init tm6010_init_tab[] = {
483  { TM6010_REQ07_RC4_HSTART0, 0xa0 },
484  { TM6010_REQ07_RC6_HEND0, 0x40 },
485  { TM6010_REQ07_RCA_VEND0, 0x31 },
486  { TM6010_REQ07_RCC_ACTIVE_IF, 0xe1 },
488  { TM6010_REQ07_RFE_POWER_DOWN, 0x7f },
489 
499 
500  { TM6010_REQ07_R3F_RESET, 0x01 },
547  { TM6010_REQ07_RC1_TRESHOLD, 0xd0 },
548  { TM6010_REQ07_RC3_HSTART1, 0x88 },
549  { TM6010_REQ07_R3F_RESET, 0x00 },
550 
551  { TM6010_REQ05_R18_IMASK7, 0x00 },
552 
553  { TM6010_REQ07_RDC_IR_LEADER1, 0xaa },
554  { TM6010_REQ07_RDD_IR_LEADER0, 0x30 },
557  { REQ_04_EN_DISABLE_MCU_INT, 0x02, 0x00 },
558  { TM6010_REQ07_RD8_IR, 0x0f },
559 
560  /* set remote wakeup key:any key wakeup */
563 };
564 
565 int tm6000_init(struct tm6000_core *dev)
566 {
567  int board, rc = 0, i, size;
568  struct reg_init *tab;
569 
570  /* Check board revision */
571  board = tm6000_get_reg32(dev, REQ_40_GET_VERSION, 0, 0);
572  if (board >= 0) {
573  switch (board & 0xff) {
574  case 0xf3:
575  printk(KERN_INFO "Found tm6000\n");
576  if (dev->dev_type != TM6000)
577  dev->dev_type = TM6000;
578  break;
579  case 0xf4:
580  printk(KERN_INFO "Found tm6010\n");
581  if (dev->dev_type != TM6010)
582  dev->dev_type = TM6010;
583  break;
584  default:
585  printk(KERN_INFO "Unknown board version = 0x%08x\n", board);
586  }
587  } else
588  printk(KERN_ERR "Error %i while retrieving board version\n", board);
589 
590  if (dev->dev_type == TM6010) {
591  tab = tm6010_init_tab;
592  size = ARRAY_SIZE(tm6010_init_tab);
593  } else {
594  tab = tm6000_init_tab;
595  size = ARRAY_SIZE(tm6000_init_tab);
596  }
597 
598  /* Load board's initialization table */
599  for (i = 0; i < size; i++) {
600  rc = tm6000_set_reg(dev, tab[i].req, tab[i].reg, tab[i].val);
601  if (rc < 0) {
602  printk(KERN_ERR "Error %i while setting req %d, "
603  "reg %d to value %d\n", rc,
604  tab[i].req, tab[i].reg, tab[i].val);
605  return rc;
606  }
607  }
608 
609  msleep(5); /* Just to be conservative */
610 
611  rc = tm6000_cards_setup(dev);
612 
613  return rc;
614 }
615 
616 
618 {
619  int val = 0;
620  u8 areg_f0 = 0x60; /* ADC MCLK = 250 Fs */
621  u8 areg_0a = 0x91; /* SIF 48KHz */
622 
623  switch (bitrate) {
624  case 48000:
625  areg_f0 = 0x60; /* ADC MCLK = 250 Fs */
626  areg_0a = 0x91; /* SIF 48KHz */
627  dev->audio_bitrate = bitrate;
628  break;
629  case 32000:
630  areg_f0 = 0x00; /* ADC MCLK = 375 Fs */
631  areg_0a = 0x90; /* SIF 32KHz */
632  dev->audio_bitrate = bitrate;
633  break;
634  default:
635  return -EINVAL;
636  }
637 
638 
639  /* enable I2S, if we use sif or external I2S device */
640  if (dev->dev_type == TM6010) {
641  val = tm6000_set_reg(dev, TM6010_REQ08_R0A_A_I2S_MOD, areg_0a);
642  if (val < 0)
643  return val;
644 
646  areg_f0, 0xf0);
647  if (val < 0)
648  return val;
649  } else {
651  areg_f0, 0xf0);
652  if (val < 0)
653  return val;
654  }
655  return 0;
656 }
658 
660 {
661  if (dev->dev_type == TM6010) {
662  /* Audio crossbar setting, default SIF1 */
663  u8 areg_f0;
664  u8 areg_07 = 0x10;
665 
666  switch (dev->rinput.amux) {
667  case TM6000_AMUX_SIF1:
668  case TM6000_AMUX_SIF2:
669  areg_f0 = 0x03;
670  areg_07 = 0x30;
671  break;
672  case TM6000_AMUX_ADC1:
673  areg_f0 = 0x00;
674  break;
675  case TM6000_AMUX_ADC2:
676  areg_f0 = 0x08;
677  break;
678  case TM6000_AMUX_I2S:
679  areg_f0 = 0x04;
680  break;
681  default:
682  printk(KERN_INFO "%s: audio input dosn't support\n",
683  dev->name);
684  return 0;
685  break;
686  }
687  /* Set audio input crossbar */
689  areg_f0, 0x0f);
690  /* Mux overflow workaround */
692  areg_07, 0xf0);
693  } else {
694  u8 areg_eb;
695  /* Audio setting, default LINE1 */
696  switch (dev->rinput.amux) {
697  case TM6000_AMUX_ADC1:
698  areg_eb = 0x00;
699  break;
700  case TM6000_AMUX_ADC2:
701  areg_eb = 0x04;
702  break;
703  default:
704  printk(KERN_INFO "%s: audio input dosn't support\n",
705  dev->name);
706  return 0;
707  break;
708  }
709  /* Set audio input */
711  areg_eb, 0x0f);
712  }
713  return 0;
714 }
715 
716 static void tm6010_set_mute_sif(struct tm6000_core *dev, u8 mute)
717 {
718  u8 mute_reg = 0;
719 
720  if (mute)
721  mute_reg = 0x08;
722 
723  tm6000_set_reg_mask(dev, TM6010_REQ08_R0A_A_I2S_MOD, mute_reg, 0x08);
724 }
725 
726 static void tm6010_set_mute_adc(struct tm6000_core *dev, u8 mute)
727 {
728  u8 mute_reg = 0;
729 
730  if (mute)
731  mute_reg = 0x20;
732 
733  if (dev->dev_type == TM6010) {
735  mute_reg, 0x20);
737  mute_reg, 0x20);
738  } else {
740  mute_reg, 0x20);
742  mute_reg, 0x20);
743  }
744 }
745 
746 int tm6000_tvaudio_set_mute(struct tm6000_core *dev, u8 mute)
747 {
748  enum tm6000_mux mux;
749 
750  if (dev->radio)
751  mux = dev->rinput.amux;
752  else
753  mux = dev->vinput[dev->input].amux;
754 
755  switch (mux) {
756  case TM6000_AMUX_SIF1:
757  case TM6000_AMUX_SIF2:
758  if (dev->dev_type == TM6010)
759  tm6010_set_mute_sif(dev, mute);
760  else {
761  printk(KERN_INFO "ERROR: TM5600 and TM6000 don't has"
762  " SIF audio inputs. Please check the %s"
763  " configuration.\n", dev->name);
764  return -EINVAL;
765  }
766  break;
767  case TM6000_AMUX_ADC1:
768  case TM6000_AMUX_ADC2:
769  tm6010_set_mute_adc(dev, mute);
770  break;
771  default:
772  return -EINVAL;
773  break;
774  }
775  return 0;
776 }
777 
778 static void tm6010_set_volume_sif(struct tm6000_core *dev, int vol)
779 {
780  u8 vol_reg;
781 
782  vol_reg = vol & 0x0F;
783 
784  if (vol < 0)
785  vol_reg |= 0x40;
786 
789 }
790 
791 static void tm6010_set_volume_adc(struct tm6000_core *dev, int vol)
792 {
793  u8 vol_reg;
794 
795  vol_reg = (vol + 0x10) & 0x1f;
796 
797  if (dev->dev_type == TM6010) {
800  } else {
803  }
804 }
805 
806 void tm6000_set_volume(struct tm6000_core *dev, int vol)
807 {
808  enum tm6000_mux mux;
809 
810  if (dev->radio) {
811  mux = dev->rinput.amux;
812  vol += 8; /* Offset to 0 dB */
813  } else
814  mux = dev->vinput[dev->input].amux;
815 
816  switch (mux) {
817  case TM6000_AMUX_SIF1:
818  case TM6000_AMUX_SIF2:
819  if (dev->dev_type == TM6010)
820  tm6010_set_volume_sif(dev, vol);
821  else
822  printk(KERN_INFO "ERROR: TM5600 and TM6000 don't has"
823  " SIF audio inputs. Please check the %s"
824  " configuration.\n", dev->name);
825  break;
826  case TM6000_AMUX_ADC1:
827  case TM6000_AMUX_ADC2:
828  tm6010_set_volume_adc(dev, vol);
829  break;
830  default:
831  break;
832  }
833 }
834 
835 static LIST_HEAD(tm6000_devlist);
836 static DEFINE_MUTEX(tm6000_devlist_mutex);
837 
838 /*
839  * tm6000_realease_resource()
840  */
841 
843 {
844  mutex_lock(&tm6000_devlist_mutex);
845  list_del(&dev->devlist);
846  mutex_unlock(&tm6000_devlist_mutex);
847 };
848 
850 {
851  mutex_lock(&tm6000_devlist_mutex);
852  list_add_tail(&dev->devlist, &tm6000_devlist);
853  mutex_unlock(&tm6000_devlist_mutex);
854 };
855 
856 /*
857  * Extension interface
858  */
859 
860 static LIST_HEAD(tm6000_extension_devlist);
861 
863  char *buf, int size)
864 {
865  struct tm6000_ops *ops = NULL;
866 
867  /* FIXME: tm6000_extension_devlist_lock should be a spinlock */
868 
869  if (!list_empty(&tm6000_extension_devlist)) {
870  list_for_each_entry(ops, &tm6000_extension_devlist, next) {
871  if (ops->fillbuf && ops->type == type)
872  ops->fillbuf(dev, buf, size);
873  }
874  }
875 
876  return 0;
877 }
878 
880 {
881  struct tm6000_core *dev = NULL;
882 
883  mutex_lock(&tm6000_devlist_mutex);
884  list_add_tail(&ops->next, &tm6000_extension_devlist);
885  list_for_each_entry(dev, &tm6000_devlist, devlist) {
886  ops->init(dev);
887  printk(KERN_INFO "%s: Initialized (%s) extension\n",
888  dev->name, ops->name);
889  }
890  mutex_unlock(&tm6000_devlist_mutex);
891  return 0;
892 }
894 
896 {
897  struct tm6000_core *dev = NULL;
898 
899  mutex_lock(&tm6000_devlist_mutex);
900  list_for_each_entry(dev, &tm6000_devlist, devlist)
901  ops->fini(dev);
902 
903  printk(KERN_INFO "tm6000: Remove (%s) extension\n", ops->name);
904  list_del(&ops->next);
905  mutex_unlock(&tm6000_devlist_mutex);
906 }
908 
910 {
911  struct tm6000_ops *ops = NULL;
912 
913  mutex_lock(&tm6000_devlist_mutex);
914  if (!list_empty(&tm6000_extension_devlist)) {
915  list_for_each_entry(ops, &tm6000_extension_devlist, next) {
916  if (ops->init)
917  ops->init(dev);
918  }
919  }
920  mutex_unlock(&tm6000_devlist_mutex);
921 }
922 
924 {
925  struct tm6000_ops *ops = NULL;
926 
927  mutex_lock(&tm6000_devlist_mutex);
928  if (!list_empty(&tm6000_extension_devlist)) {
929  list_for_each_entry(ops, &tm6000_extension_devlist, next) {
930  if (ops->fini)
931  ops->fini(dev);
932  }
933  }
934  mutex_unlock(&tm6000_devlist_mutex);
935 }