ivtv-alsa-mixer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * ALSA mixer controls for the
  3. * ALSA interface to ivtv PCM capture streams
  4. *
  5. * Copyright (C) 2009,2012 Andy Walls <awalls@md.metrocast.net>
  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, or
  10. * (at your option) any later version.
  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
  20. * 02111-1307 USA
  21. */
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/device.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/videodev2.h>
  27. #include <media/v4l2-device.h>
  28. #include <sound/core.h>
  29. #include <sound/control.h>
  30. #include <sound/tlv.h>
  31. #include "ivtv-alsa.h"
  32. #include "ivtv-driver.h"
  33. /*
  34. * Note the cx25840-core volume scale is funny, due to the alignment of the
  35. * scale with another chip's range:
  36. *
  37. * v4l2_control value /512 indicated dB actual dB reg 0x8d4
  38. * 0x0000 - 0x01ff 0 -119 -96 228
  39. * 0x0200 - 0x02ff 1 -118 -96 228
  40. * ...
  41. * 0x2c00 - 0x2dff 22 -97 -96 228
  42. * 0x2e00 - 0x2fff 23 -96 -96 228
  43. * 0x3000 - 0x31ff 24 -95 -95 226
  44. * ...
  45. * 0xee00 - 0xefff 119 0 0 36
  46. * ...
  47. * 0xfe00 - 0xffff 127 +8 +8 20
  48. */
  49. static inline int dB_to_cx25840_vol(int dB)
  50. {
  51. if (dB < -96)
  52. dB = -96;
  53. else if (dB > 8)
  54. dB = 8;
  55. return (dB + 119) << 9;
  56. }
  57. static inline int cx25840_vol_to_dB(int v)
  58. {
  59. if (v < (23 << 9))
  60. v = (23 << 9);
  61. else if (v > (127 << 9))
  62. v = (127 << 9);
  63. return (v >> 9) - 119;
  64. }
  65. static int snd_ivtv_mixer_tv_vol_info(struct snd_kcontrol *kcontrol,
  66. struct snd_ctl_elem_info *uinfo)
  67. {
  68. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  69. uinfo->count = 1;
  70. /* We're already translating values, just keep this control in dB */
  71. uinfo->value.integer.min = -96;
  72. uinfo->value.integer.max = 8;
  73. uinfo->value.integer.step = 1;
  74. return 0;
  75. }
  76. static int snd_ivtv_mixer_tv_vol_get(struct snd_kcontrol *kctl,
  77. struct snd_ctl_elem_value *uctl)
  78. {
  79. struct snd_ivtv_card *itvsc = snd_kcontrol_chip(kctl);
  80. struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
  81. struct v4l2_control vctrl;
  82. int ret;
  83. vctrl.id = V4L2_CID_AUDIO_VOLUME;
  84. vctrl.value = dB_to_cx25840_vol(uctl->value.integer.value[0]);
  85. snd_ivtv_lock(itvsc);
  86. ret = v4l2_subdev_call(itv->sd_audio, core, g_ctrl, &vctrl);
  87. snd_ivtv_unlock(itvsc);
  88. if (!ret)
  89. uctl->value.integer.value[0] = cx25840_vol_to_dB(vctrl.value);
  90. return ret;
  91. }
  92. static int snd_ivtv_mixer_tv_vol_put(struct snd_kcontrol *kctl,
  93. struct snd_ctl_elem_value *uctl)
  94. {
  95. struct snd_ivtv_card *itvsc = snd_kcontrol_chip(kctl);
  96. struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
  97. struct v4l2_control vctrl;
  98. int ret;
  99. vctrl.id = V4L2_CID_AUDIO_VOLUME;
  100. vctrl.value = dB_to_cx25840_vol(uctl->value.integer.value[0]);
  101. snd_ivtv_lock(itvsc);
  102. /* Fetch current state */
  103. ret = v4l2_subdev_call(itv->sd_audio, core, g_ctrl, &vctrl);
  104. if (ret ||
  105. (cx25840_vol_to_dB(vctrl.value) != uctl->value.integer.value[0])) {
  106. /* Set, if needed */
  107. vctrl.value = dB_to_cx25840_vol(uctl->value.integer.value[0]);
  108. ret = v4l2_subdev_call(itv->sd_audio, core, s_ctrl, &vctrl);
  109. if (!ret)
  110. ret = 1; /* Indicate control was changed w/o error */
  111. }
  112. snd_ivtv_unlock(itvsc);
  113. return ret;
  114. }
  115. /* This is a bit of overkill, the slider is already in dB internally */
  116. static DECLARE_TLV_DB_SCALE(snd_ivtv_mixer_tv_vol_db_scale, -9600, 100, 0);
  117. static struct snd_kcontrol_new snd_ivtv_mixer_tv_vol __initdata = {
  118. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  119. .name = "Analog TV Capture Volume",
  120. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  121. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  122. .info = snd_ivtv_mixer_tv_volume_info,
  123. .get = snd_ivtv_mixer_tv_volume_get,
  124. .put = snd_ivtv_mixer_tv_volume_put,
  125. .tlv.p = snd_ivtv_mixer_tv_vol_db_scale
  126. };
  127. /* FIXME - add mute switch and balance, bass, treble sliders:
  128. V4L2_CID_AUDIO_MUTE
  129. V4L2_CID_AUDIO_BALANCE
  130. V4L2_CID_AUDIO_BASS
  131. V4L2_CID_AUDIO_TREBLE
  132. */
  133. /* FIXME - add stereo, lang1, lang2, mono menu */
  134. /* FIXME - add I2S volume */
  135. int __init snd_ivtv_mixer_create(struct snd_ivtv_card *itvsc)
  136. {
  137. struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
  138. struct snd_card *sc = itvsc->sc;
  139. int ret;
  140. strlcpy(sc->mixername, "CX2341[56] Mixer", sizeof(sc->mixername));
  141. ret = snd_ctl_add(sc, snd_ctl_new1(snd_ivtv_mixer_tv_vol, itvsc));
  142. if (ret) {
  143. IVTV_ALSA_WARN("%s: failed to add %s control, err %d\n",
  144. __func__, snd_ivtv_mixer_tv_vol.name, ret);
  145. }
  146. return ret;
  147. }