gus_timer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Routines for Gravis UltraSound soundcards - Timers
  3. * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
  24. #include <linux/time.h>
  25. #include <sound/core.h>
  26. #include <sound/gus.h>
  27. /*
  28. * Timer 1 - 80us
  29. */
  30. static int snd_gf1_timer1_start(struct snd_timer * timer)
  31. {
  32. unsigned long flags;
  33. unsigned char tmp;
  34. unsigned int ticks;
  35. struct snd_gus_card *gus;
  36. gus = snd_timer_chip(timer);
  37. spin_lock_irqsave(&gus->reg_lock, flags);
  38. ticks = timer->sticks;
  39. tmp = (gus->gf1.timer_enabled |= 4);
  40. snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_1, 256 - ticks); /* timer 1 count */
  41. snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* enable timer 1 IRQ */
  42. snd_gf1_adlib_write(gus, 0x04, tmp >> 2); /* timer 2 start */
  43. spin_unlock_irqrestore(&gus->reg_lock, flags);
  44. return 0;
  45. }
  46. static int snd_gf1_timer1_stop(struct snd_timer * timer)
  47. {
  48. unsigned long flags;
  49. unsigned char tmp;
  50. struct snd_gus_card *gus;
  51. gus = snd_timer_chip(timer);
  52. spin_lock_irqsave(&gus->reg_lock, flags);
  53. tmp = (gus->gf1.timer_enabled &= ~4);
  54. snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* disable timer #1 */
  55. spin_unlock_irqrestore(&gus->reg_lock, flags);
  56. return 0;
  57. }
  58. /*
  59. * Timer 2 - 320us
  60. */
  61. static int snd_gf1_timer2_start(struct snd_timer * timer)
  62. {
  63. unsigned long flags;
  64. unsigned char tmp;
  65. unsigned int ticks;
  66. struct snd_gus_card *gus;
  67. gus = snd_timer_chip(timer);
  68. spin_lock_irqsave(&gus->reg_lock, flags);
  69. ticks = timer->sticks;
  70. tmp = (gus->gf1.timer_enabled |= 8);
  71. snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_2, 256 - ticks); /* timer 2 count */
  72. snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* enable timer 2 IRQ */
  73. snd_gf1_adlib_write(gus, 0x04, tmp >> 2); /* timer 2 start */
  74. spin_unlock_irqrestore(&gus->reg_lock, flags);
  75. return 0;
  76. }
  77. static int snd_gf1_timer2_stop(struct snd_timer * timer)
  78. {
  79. unsigned long flags;
  80. unsigned char tmp;
  81. struct snd_gus_card *gus;
  82. gus = snd_timer_chip(timer);
  83. spin_lock_irqsave(&gus->reg_lock, flags);
  84. tmp = (gus->gf1.timer_enabled &= ~8);
  85. snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* disable timer #1 */
  86. spin_unlock_irqrestore(&gus->reg_lock, flags);
  87. return 0;
  88. }
  89. /*
  90. */
  91. static void snd_gf1_interrupt_timer1(struct snd_gus_card * gus)
  92. {
  93. struct snd_timer *timer = gus->gf1.timer1;
  94. if (timer == NULL)
  95. return;
  96. snd_timer_interrupt(timer, timer->sticks);
  97. }
  98. static void snd_gf1_interrupt_timer2(struct snd_gus_card * gus)
  99. {
  100. struct snd_timer *timer = gus->gf1.timer2;
  101. if (timer == NULL)
  102. return;
  103. snd_timer_interrupt(timer, timer->sticks);
  104. }
  105. /*
  106. */
  107. static struct snd_timer_hardware snd_gf1_timer1 =
  108. {
  109. .flags = SNDRV_TIMER_HW_STOP,
  110. .resolution = 80000,
  111. .ticks = 256,
  112. .start = snd_gf1_timer1_start,
  113. .stop = snd_gf1_timer1_stop,
  114. };
  115. static struct snd_timer_hardware snd_gf1_timer2 =
  116. {
  117. .flags = SNDRV_TIMER_HW_STOP,
  118. .resolution = 320000,
  119. .ticks = 256,
  120. .start = snd_gf1_timer2_start,
  121. .stop = snd_gf1_timer2_stop,
  122. };
  123. static void snd_gf1_timer1_free(struct snd_timer *timer)
  124. {
  125. struct snd_gus_card *gus = timer->private_data;
  126. gus->gf1.timer1 = NULL;
  127. }
  128. static void snd_gf1_timer2_free(struct snd_timer *timer)
  129. {
  130. struct snd_gus_card *gus = timer->private_data;
  131. gus->gf1.timer2 = NULL;
  132. }
  133. void snd_gf1_timers_init(struct snd_gus_card * gus)
  134. {
  135. struct snd_timer *timer;
  136. struct snd_timer_id tid;
  137. if (gus->gf1.timer1 != NULL || gus->gf1.timer2 != NULL)
  138. return;
  139. gus->gf1.interrupt_handler_timer1 = snd_gf1_interrupt_timer1;
  140. gus->gf1.interrupt_handler_timer2 = snd_gf1_interrupt_timer2;
  141. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  142. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  143. tid.card = gus->card->number;
  144. tid.device = gus->timer_dev;
  145. tid.subdevice = 0;
  146. if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
  147. strcpy(timer->name, "GF1 timer #1");
  148. timer->private_data = gus;
  149. timer->private_free = snd_gf1_timer1_free;
  150. timer->hw = snd_gf1_timer1;
  151. }
  152. gus->gf1.timer1 = timer;
  153. tid.device++;
  154. if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) {
  155. strcpy(timer->name, "GF1 timer #2");
  156. timer->private_data = gus;
  157. timer->private_free = snd_gf1_timer2_free;
  158. timer->hw = snd_gf1_timer2;
  159. }
  160. gus->gf1.timer2 = timer;
  161. }
  162. void snd_gf1_timers_done(struct snd_gus_card * gus)
  163. {
  164. snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_TIMER1 | SNDRV_GF1_HANDLER_TIMER2);
  165. if (gus->gf1.timer1) {
  166. snd_device_free(gus->card, gus->gf1.timer1);
  167. gus->gf1.timer1 = NULL;
  168. }
  169. if (gus->gf1.timer2) {
  170. snd_device_free(gus->card, gus->gf1.timer2);
  171. gus->gf1.timer2 = NULL;
  172. }
  173. }