pvrusb2-wm8775.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. *
  3. *
  4. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  5. * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. /*
  22. This source file is specifically designed to interface with the
  23. wm8775.
  24. */
  25. #include "pvrusb2-wm8775.h"
  26. #include "pvrusb2-i2c-cmd-v4l2.h"
  27. #include "pvrusb2-hdw-internal.h"
  28. #include "pvrusb2-debug.h"
  29. #include <linux/videodev2.h>
  30. #include <media/v4l2-common.h>
  31. #include <linux/errno.h>
  32. #include <linux/slab.h>
  33. struct pvr2_v4l_wm8775 {
  34. struct pvr2_i2c_handler handler;
  35. struct pvr2_i2c_client *client;
  36. struct pvr2_hdw *hdw;
  37. unsigned long stale_mask;
  38. };
  39. static void set_input(struct pvr2_v4l_wm8775 *ctxt)
  40. {
  41. struct v4l2_routing route;
  42. struct pvr2_hdw *hdw = ctxt->hdw;
  43. memset(&route,0,sizeof(route));
  44. switch(hdw->input_val) {
  45. case PVR2_CVAL_INPUT_RADIO:
  46. route.input = 1;
  47. break;
  48. default:
  49. /* All other cases just use the second input */
  50. route.input = 2;
  51. break;
  52. }
  53. pvr2_trace(PVR2_TRACE_CHIPS,"i2c wm8775 set_input(val=%d route=0x%x)",
  54. hdw->input_val,route.input);
  55. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_AUDIO_ROUTING,&route);
  56. }
  57. static int check_input(struct pvr2_v4l_wm8775 *ctxt)
  58. {
  59. struct pvr2_hdw *hdw = ctxt->hdw;
  60. return hdw->input_dirty != 0;
  61. }
  62. struct pvr2_v4l_wm8775_ops {
  63. void (*update)(struct pvr2_v4l_wm8775 *);
  64. int (*check)(struct pvr2_v4l_wm8775 *);
  65. };
  66. static const struct pvr2_v4l_wm8775_ops wm8775_ops[] = {
  67. { .update = set_input, .check = check_input},
  68. };
  69. static unsigned int wm8775_describe(struct pvr2_v4l_wm8775 *ctxt,
  70. char *buf,unsigned int cnt)
  71. {
  72. return scnprintf(buf,cnt,"handler: pvrusb2-wm8775");
  73. }
  74. static void wm8775_detach(struct pvr2_v4l_wm8775 *ctxt)
  75. {
  76. ctxt->client->handler = NULL;
  77. kfree(ctxt);
  78. }
  79. static int wm8775_check(struct pvr2_v4l_wm8775 *ctxt)
  80. {
  81. unsigned long msk;
  82. unsigned int idx;
  83. for (idx = 0; idx < ARRAY_SIZE(wm8775_ops); idx++) {
  84. msk = 1 << idx;
  85. if (ctxt->stale_mask & msk) continue;
  86. if (wm8775_ops[idx].check(ctxt)) {
  87. ctxt->stale_mask |= msk;
  88. }
  89. }
  90. return ctxt->stale_mask != 0;
  91. }
  92. static void wm8775_update(struct pvr2_v4l_wm8775 *ctxt)
  93. {
  94. unsigned long msk;
  95. unsigned int idx;
  96. for (idx = 0; idx < ARRAY_SIZE(wm8775_ops); idx++) {
  97. msk = 1 << idx;
  98. if (!(ctxt->stale_mask & msk)) continue;
  99. ctxt->stale_mask &= ~msk;
  100. wm8775_ops[idx].update(ctxt);
  101. }
  102. }
  103. static const struct pvr2_i2c_handler_functions hfuncs = {
  104. .detach = (void (*)(void *))wm8775_detach,
  105. .check = (int (*)(void *))wm8775_check,
  106. .update = (void (*)(void *))wm8775_update,
  107. .describe = (unsigned int (*)(void *,char *,unsigned int))wm8775_describe,
  108. };
  109. int pvr2_i2c_wm8775_setup(struct pvr2_hdw *hdw,struct pvr2_i2c_client *cp)
  110. {
  111. struct pvr2_v4l_wm8775 *ctxt;
  112. if (cp->handler) return 0;
  113. ctxt = kzalloc(sizeof(*ctxt),GFP_KERNEL);
  114. if (!ctxt) return 0;
  115. ctxt->handler.func_data = ctxt;
  116. ctxt->handler.func_table = &hfuncs;
  117. ctxt->client = cp;
  118. ctxt->hdw = hdw;
  119. ctxt->stale_mask = (1 << ARRAY_SIZE(wm8775_ops)) - 1;
  120. cp->handler = &ctxt->handler;
  121. pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x wm8775 V4L2 handler set up",
  122. cp->client->addr);
  123. return !0;
  124. }
  125. void pvr2_wm8775_update(struct pvr2_hdw *hdw, struct v4l2_subdev *sd)
  126. {
  127. if (hdw->input_dirty) {
  128. struct v4l2_routing route;
  129. memset(&route, 0, sizeof(route));
  130. switch (hdw->input_val) {
  131. case PVR2_CVAL_INPUT_RADIO:
  132. route.input = 1;
  133. break;
  134. default:
  135. /* All other cases just use the second input */
  136. route.input = 2;
  137. break;
  138. }
  139. pvr2_trace(PVR2_TRACE_CHIPS, "subdev wm8775"
  140. " set_input(val=%d route=0x%x)",
  141. hdw->input_val, route.input);
  142. sd->ops->audio->s_routing(sd, &route);
  143. }
  144. }
  145. /*
  146. Stuff for Emacs to see, in order to encourage consistent editing style:
  147. *** Local Variables: ***
  148. *** mode: c ***
  149. *** fill-column: 70 ***
  150. *** tab-width: 8 ***
  151. *** c-basic-offset: 8 ***
  152. *** End: ***
  153. */