ics2101.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* $OpenBSD: ics2101.c,v 1.9 2014/09/14 14:17:25 jsg Exp $ */
  2. /* $NetBSD: ics2101.c,v 1.6 1997/10/09 07:57:23 jtc Exp $ */
  3. /*-
  4. * Copyright (c) 1996 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Ken Hornstein and John Kohl.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  21. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  23. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <sys/param.h>
  32. #include <sys/systm.h>
  33. #include <sys/errno.h>
  34. #include <sys/ioctl.h>
  35. #include <sys/syslog.h>
  36. #include <sys/device.h>
  37. #include <sys/buf.h>
  38. #include <machine/cpu.h>
  39. #include <sys/audioio.h>
  40. #include <dev/audio_if.h>
  41. #include <dev/isa/isavar.h>
  42. #include <dev/isa/isadmavar.h>
  43. #include <dev/ic/ics2101reg.h>
  44. #include <dev/isa/ics2101var.h>
  45. #define ICS_VALUE 0x01
  46. #define ICS_MUTE 0x02
  47. #define ICS_MUTE_MUTED 0x04
  48. /* convert from [AUDIO_MIN_GAIN,AUDIO_MAX_GAIN] (0,255) to
  49. [ICSMIX_MAX_ATTN,ICSMIX_MIN_ATTN] (0,127) */
  50. #define cvt_value(val) ((val) >> 1)
  51. static void ics2101_mix_doit(struct ics2101_softc *, u_int, u_int, u_int,
  52. u_int);
  53. /*
  54. * Program one channel of the ICS mixer
  55. */
  56. static void
  57. ics2101_mix_doit(sc, chan, side, value, flags)
  58. struct ics2101_softc *sc;
  59. u_int chan, side, value, flags;
  60. {
  61. bus_space_tag_t iot = sc->sc_iot;
  62. unsigned char flip_left[6] = {0x01, 0x01, 0x01, 0x02, 0x01, 0x02};
  63. unsigned char flip_right[6] = {0x02, 0x02, 0x02, 0x01, 0x02, 0x01};
  64. register unsigned char ctrl_addr;
  65. register unsigned char attn_addr;
  66. register unsigned char normal;
  67. if (chan < ICSMIX_CHAN_0 || chan > ICSMIX_CHAN_5)
  68. return;
  69. if (side != ICSMIX_LEFT && side != ICSMIX_RIGHT)
  70. return;
  71. if (flags & ICS_MUTE) {
  72. value = cvt_value(sc->sc_setting[chan][side]);
  73. sc->sc_mute[chan][side] = flags & ICS_MUTE_MUTED;
  74. } else if (flags & ICS_VALUE) {
  75. sc->sc_setting[chan][side] = value;
  76. value = cvt_value(value);
  77. if (value > ICSMIX_MIN_ATTN)
  78. value = ICSMIX_MIN_ATTN;
  79. } else
  80. return;
  81. ctrl_addr = chan << 3;
  82. attn_addr = chan << 3;
  83. if (side == ICSMIX_LEFT) {
  84. ctrl_addr |= ICSMIX_CTRL_LEFT;
  85. attn_addr |= ICSMIX_ATTN_LEFT;
  86. if (sc->sc_mute[chan][side])
  87. normal = 0x0;
  88. else if (sc->sc_flags & ICS_FLIP)
  89. normal = flip_left[chan];
  90. else
  91. normal = 0x01;
  92. } else {
  93. ctrl_addr |= ICSMIX_CTRL_RIGHT;
  94. attn_addr |= ICSMIX_ATTN_RIGHT;
  95. if (sc->sc_mute[chan][side])
  96. normal = 0x0;
  97. else if (sc->sc_flags & ICS_FLIP)
  98. normal = flip_right[chan];
  99. else
  100. normal = 0x02;
  101. }
  102. mtx_enter(&audio_lock);
  103. bus_space_write_1(iot, sc->sc_selio_ioh, sc->sc_selio, ctrl_addr);
  104. bus_space_write_1(iot, sc->sc_dataio_ioh, sc->sc_dataio, normal);
  105. bus_space_write_1(iot, sc->sc_selio_ioh, sc->sc_selio, attn_addr);
  106. bus_space_write_1(iot, sc->sc_dataio_ioh, sc->sc_dataio, (unsigned char) value);
  107. mtx_leave(&audio_lock);
  108. }
  109. void
  110. ics2101_mix_mute(sc, chan, side, domute)
  111. struct ics2101_softc *sc;
  112. unsigned int chan, side, domute;
  113. {
  114. ics2101_mix_doit(sc, chan, side, 0,
  115. domute ? ICS_MUTE|ICS_MUTE_MUTED : ICS_MUTE);
  116. }
  117. void
  118. ics2101_mix_attenuate(sc, chan, side, value)
  119. struct ics2101_softc *sc;
  120. unsigned int chan, side, value;
  121. {
  122. ics2101_mix_doit(sc, chan, side, value, ICS_VALUE);
  123. }