pvrusb2-audio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. *
  3. * $Id$
  4. *
  5. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  6. * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include "pvrusb2-audio.h"
  23. #include "pvrusb2-hdw-internal.h"
  24. #include "pvrusb2-debug.h"
  25. #include <linux/videodev2.h>
  26. #include <media/msp3400.h>
  27. #include <media/v4l2-common.h>
  28. struct pvr2_msp3400_handler {
  29. struct pvr2_hdw *hdw;
  30. struct pvr2_i2c_client *client;
  31. struct pvr2_i2c_handler i2c_handler;
  32. struct pvr2_audio_stat astat;
  33. unsigned long stale_mask;
  34. };
  35. /* This function selects the correct audio input source */
  36. static void set_stereo(struct pvr2_msp3400_handler *ctxt)
  37. {
  38. struct pvr2_hdw *hdw = ctxt->hdw;
  39. struct v4l2_routing route;
  40. pvr2_trace(PVR2_TRACE_CHIPS,"i2c msp3400 v4l2 set_stereo");
  41. if (hdw->input_val == PVR2_CVAL_INPUT_TV) {
  42. struct v4l2_tuner vt;
  43. memset(&vt,0,sizeof(vt));
  44. vt.audmode = hdw->audiomode_val;
  45. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_S_TUNER,&vt);
  46. }
  47. route.input = MSP_INPUT_DEFAULT;
  48. route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
  49. switch (hdw->input_val) {
  50. case PVR2_CVAL_INPUT_TV:
  51. break;
  52. case PVR2_CVAL_INPUT_RADIO:
  53. /* Assume that msp34xx also handle FM decoding, in which case
  54. we're still using the tuner. */
  55. /* HV: actually it is more likely to be the SCART2 input if
  56. the ivtv experience is any indication. */
  57. route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
  58. MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
  59. break;
  60. case PVR2_CVAL_INPUT_SVIDEO:
  61. case PVR2_CVAL_INPUT_COMPOSITE:
  62. /* SCART 1 input */
  63. route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
  64. MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
  65. break;
  66. }
  67. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_AUDIO_ROUTING,&route);
  68. }
  69. static int check_stereo(struct pvr2_msp3400_handler *ctxt)
  70. {
  71. struct pvr2_hdw *hdw = ctxt->hdw;
  72. return (hdw->input_dirty ||
  73. hdw->audiomode_dirty);
  74. }
  75. struct pvr2_msp3400_ops {
  76. void (*update)(struct pvr2_msp3400_handler *);
  77. int (*check)(struct pvr2_msp3400_handler *);
  78. };
  79. static const struct pvr2_msp3400_ops msp3400_ops[] = {
  80. { .update = set_stereo, .check = check_stereo},
  81. };
  82. static int msp3400_check(struct pvr2_msp3400_handler *ctxt)
  83. {
  84. unsigned long msk;
  85. unsigned int idx;
  86. for (idx = 0; idx < sizeof(msp3400_ops)/sizeof(msp3400_ops[0]);
  87. idx++) {
  88. msk = 1 << idx;
  89. if (ctxt->stale_mask & msk) continue;
  90. if (msp3400_ops[idx].check(ctxt)) {
  91. ctxt->stale_mask |= msk;
  92. }
  93. }
  94. return ctxt->stale_mask != 0;
  95. }
  96. static void msp3400_update(struct pvr2_msp3400_handler *ctxt)
  97. {
  98. unsigned long msk;
  99. unsigned int idx;
  100. for (idx = 0; idx < sizeof(msp3400_ops)/sizeof(msp3400_ops[0]);
  101. idx++) {
  102. msk = 1 << idx;
  103. if (!(ctxt->stale_mask & msk)) continue;
  104. ctxt->stale_mask &= ~msk;
  105. msp3400_ops[idx].update(ctxt);
  106. }
  107. }
  108. /* This reads back the current signal type */
  109. static int get_audio_status(struct pvr2_msp3400_handler *ctxt)
  110. {
  111. struct v4l2_tuner vt;
  112. int stat;
  113. memset(&vt,0,sizeof(vt));
  114. stat = pvr2_i2c_client_cmd(ctxt->client,VIDIOC_G_TUNER,&vt);
  115. if (stat < 0) return stat;
  116. ctxt->hdw->flag_stereo = (vt.audmode & V4L2_TUNER_MODE_STEREO) != 0;
  117. ctxt->hdw->flag_bilingual =
  118. (vt.audmode & V4L2_TUNER_MODE_LANG2) != 0;
  119. return 0;
  120. }
  121. static void pvr2_msp3400_detach(struct pvr2_msp3400_handler *ctxt)
  122. {
  123. ctxt->client->handler = NULL;
  124. ctxt->hdw->audio_stat = NULL;
  125. kfree(ctxt);
  126. }
  127. static unsigned int pvr2_msp3400_describe(struct pvr2_msp3400_handler *ctxt,
  128. char *buf,unsigned int cnt)
  129. {
  130. return scnprintf(buf,cnt,"handler: pvrusb2-audio v4l2");
  131. }
  132. const static struct pvr2_i2c_handler_functions msp3400_funcs = {
  133. .detach = (void (*)(void *))pvr2_msp3400_detach,
  134. .check = (int (*)(void *))msp3400_check,
  135. .update = (void (*)(void *))msp3400_update,
  136. .describe = (unsigned int (*)(void *,char *,unsigned int))pvr2_msp3400_describe,
  137. };
  138. int pvr2_i2c_msp3400_setup(struct pvr2_hdw *hdw,struct pvr2_i2c_client *cp)
  139. {
  140. struct pvr2_msp3400_handler *ctxt;
  141. if (hdw->audio_stat) return 0;
  142. if (cp->handler) return 0;
  143. ctxt = kmalloc(sizeof(*ctxt),GFP_KERNEL);
  144. if (!ctxt) return 0;
  145. memset(ctxt,0,sizeof(*ctxt));
  146. ctxt->i2c_handler.func_data = ctxt;
  147. ctxt->i2c_handler.func_table = &msp3400_funcs;
  148. ctxt->client = cp;
  149. ctxt->hdw = hdw;
  150. ctxt->astat.ctxt = ctxt;
  151. ctxt->astat.status = (int (*)(void *))get_audio_status;
  152. ctxt->astat.detach = (void (*)(void *))pvr2_msp3400_detach;
  153. ctxt->stale_mask = (1 << (sizeof(msp3400_ops)/
  154. sizeof(msp3400_ops[0]))) - 1;
  155. cp->handler = &ctxt->i2c_handler;
  156. hdw->audio_stat = &ctxt->astat;
  157. pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x msp3400 V4L2 handler set up",
  158. cp->client->addr);
  159. return !0;
  160. }
  161. /*
  162. Stuff for Emacs to see, in order to encourage consistent editing style:
  163. *** Local Variables: ***
  164. *** mode: c ***
  165. *** fill-column: 70 ***
  166. *** tab-width: 8 ***
  167. *** c-basic-offset: 8 ***
  168. *** End: ***
  169. */