gus_timer.c 4.3 KB

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