gus_timer.c 4.5 KB

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