gus_timer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Routines for Gravis UltraSound soundcards - Timers
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. * GUS have similar timers as AdLib (OPL2/OPL3 chips).
  6. *
  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, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/time.h>
  24. #include <sound/core.h>
  25. #include <sound/gus.h>
  26. /*
  27. * Timer 1 - 80us
  28. */
  29. static int snd_gf1_timer1_start(struct snd_timer * timer)
  30. {
  31. unsigned long flags;
  32. unsigned char tmp;
  33. unsigned int ticks;
  34. struct snd_gus_card *gus;
  35. gus = snd_timer_chip(timer);
  36. spin_lock_irqsave(&gus->reg_lock, flags);
  37. ticks = timer->sticks;
  38. tmp = (gus->gf1.timer_enabled |= 4);
  39. snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_1, 256 - ticks); /* timer 1 count */
  40. snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* enable timer 1 IRQ */
  41. snd_gf1_adlib_write(gus, 0x04, tmp >> 2); /* timer 2 start */
  42. spin_unlock_irqrestore(&gus->reg_lock, flags);
  43. return 0;
  44. }
  45. static int snd_gf1_timer1_stop(struct snd_timer * timer)
  46. {
  47. unsigned long flags;
  48. unsigned char tmp;
  49. struct snd_gus_card *gus;
  50. gus = snd_timer_chip(timer);
  51. spin_lock_irqsave(&gus->reg_lock, flags);
  52. tmp = (gus->gf1.timer_enabled &= ~4);
  53. snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* disable timer #1 */
  54. spin_unlock_irqrestore(&gus->reg_lock, flags);
  55. return 0;
  56. }
  57. /*
  58. * Timer 2 - 320us
  59. */
  60. static int snd_gf1_timer2_start(struct snd_timer * timer)
  61. {
  62. unsigned long flags;
  63. unsigned char tmp;
  64. unsigned int ticks;
  65. struct snd_gus_card *gus;
  66. gus = snd_timer_chip(timer);
  67. spin_lock_irqsave(&gus->reg_lock, flags);
  68. ticks = timer->sticks;
  69. tmp = (gus->gf1.timer_enabled |= 8);
  70. snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_2, 256 - ticks); /* timer 2 count */
  71. snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* enable timer 2 IRQ */
  72. snd_gf1_adlib_write(gus, 0x04, tmp >> 2); /* timer 2 start */
  73. spin_unlock_irqrestore(&gus->reg_lock, flags);
  74. return 0;
  75. }
  76. static int snd_gf1_timer2_stop(struct snd_timer * timer)
  77. {
  78. unsigned long flags;
  79. unsigned char tmp;
  80. struct snd_gus_card *gus;
  81. gus = snd_timer_chip(timer);
  82. spin_lock_irqsave(&gus->reg_lock, flags);
  83. tmp = (gus->gf1.timer_enabled &= ~8);
  84. snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* disable timer #1 */
  85. spin_unlock_irqrestore(&gus->reg_lock, flags);
  86. return 0;
  87. }
  88. /*
  89. */
  90. static void snd_gf1_interrupt_timer1(struct snd_gus_card * gus)
  91. {
  92. struct snd_timer *timer = gus->gf1.timer1;
  93. if (timer == NULL)
  94. return;
  95. snd_timer_interrupt(timer, timer->sticks);
  96. }
  97. static void snd_gf1_interrupt_timer2(struct snd_gus_card * gus)
  98. {
  99. struct snd_timer *timer = gus->gf1.timer2;
  100. if (timer == NULL)
  101. return;
  102. snd_timer_interrupt(timer, timer->sticks);
  103. }
  104. /*
  105. */
  106. static struct snd_timer_hardware snd_gf1_timer1 =
  107. {
  108. .flags = SNDRV_TIMER_HW_STOP,
  109. .resolution = 80000,
  110. .ticks = 256,
  111. .start = snd_gf1_timer1_start,
  112. .stop = snd_gf1_timer1_stop,
  113. };
  114. static struct snd_timer_hardware snd_gf1_timer2 =
  115. {
  116. .flags = SNDRV_TIMER_HW_STOP,
  117. .resolution = 320000,
  118. .ticks = 256,
  119. .start = snd_gf1_timer2_start,
  120. .stop = snd_gf1_timer2_stop,
  121. };
  122. static void snd_gf1_timer1_free(struct snd_timer *timer)
  123. {
  124. struct snd_gus_card *gus = timer->private_data;
  125. gus->gf1.timer1 = NULL;
  126. }
  127. static void snd_gf1_timer2_free(struct snd_timer *timer)
  128. {
  129. struct snd_gus_card *gus = timer->private_data;
  130. gus->gf1.timer2 = NULL;
  131. }
  132. void snd_gf1_timers_init(struct snd_gus_card * gus)
  133. {
  134. struct snd_timer *timer;
  135. struct snd_timer_id tid;
  136. if (gus->gf1.timer1 != NULL || gus->gf1.timer2 != NULL)
  137. return;
  138. gus->gf1.interrupt_handler_timer1 = snd_gf1_interrupt_timer1;
  139. gus->gf1.interrupt_handler_timer2 = snd_gf1_interrupt_timer2;
  140. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  141. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  142. tid.card = gus->card->number;
  143. tid.device = gus->timer_dev;
  144. tid.subdevice = 0;
  145. if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
  146. strcpy(timer->name, "GF1 timer #1");
  147. timer->private_data = gus;
  148. timer->private_free = snd_gf1_timer1_free;
  149. timer->hw = snd_gf1_timer1;
  150. }
  151. gus->gf1.timer1 = timer;
  152. tid.device++;
  153. if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
  154. strcpy(timer->name, "GF1 timer #2");
  155. timer->private_data = gus;
  156. timer->private_free = snd_gf1_timer2_free;
  157. timer->hw = snd_gf1_timer2;
  158. }
  159. gus->gf1.timer2 = timer;
  160. }
  161. void snd_gf1_timers_done(struct snd_gus_card * gus)
  162. {
  163. snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_TIMER1 | SNDRV_GF1_HANDLER_TIMER2);
  164. if (gus->gf1.timer1) {
  165. snd_device_free(gus->card, gus->gf1.timer1);
  166. gus->gf1.timer1 = NULL;
  167. }
  168. if (gus->gf1.timer2) {
  169. snd_device_free(gus->card, gus->gf1.timer2);
  170. gus->gf1.timer2 = NULL;
  171. }
  172. }