cx18-alsa-mixer.c 4.5 KB

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