timer.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) by Lee Revell <rlrevell@joe-job.com>
  4. * Clemens Ladisch <clemens@ladisch.de>
  5. * Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
  6. *
  7. * Routines for control of EMU10K1 chips
  8. */
  9. #include <linux/time.h>
  10. #include <sound/core.h>
  11. #include <sound/emu10k1.h>
  12. static int snd_emu10k1_timer_start(struct snd_timer *timer)
  13. {
  14. struct snd_emu10k1 *emu;
  15. unsigned int delay;
  16. emu = snd_timer_chip(timer);
  17. delay = timer->sticks - 1;
  18. if (delay < 5 ) /* minimum time is 5 ticks */
  19. delay = 5;
  20. snd_emu10k1_intr_enable(emu, INTE_INTERVALTIMERENB);
  21. outw(delay & TIMER_RATE_MASK, emu->port + TIMER);
  22. return 0;
  23. }
  24. static int snd_emu10k1_timer_stop(struct snd_timer *timer)
  25. {
  26. struct snd_emu10k1 *emu;
  27. emu = snd_timer_chip(timer);
  28. snd_emu10k1_intr_disable(emu, INTE_INTERVALTIMERENB);
  29. return 0;
  30. }
  31. static unsigned long snd_emu10k1_timer_c_resolution(struct snd_timer *timer)
  32. {
  33. struct snd_emu10k1 *emu = snd_timer_chip(timer);
  34. if (emu->card_capabilities->emu_model &&
  35. emu->emu1010.word_clock == 44100)
  36. return 22676; // 1 sample @ 44.1 kHz = 22.675736...us
  37. else
  38. return 20833; // 1 sample @ 48 kHz = 20.833...us
  39. }
  40. static int snd_emu10k1_timer_precise_resolution(struct snd_timer *timer,
  41. unsigned long *num, unsigned long *den)
  42. {
  43. struct snd_emu10k1 *emu = snd_timer_chip(timer);
  44. *num = 1;
  45. if (emu->card_capabilities->emu_model)
  46. *den = emu->emu1010.word_clock;
  47. else
  48. *den = 48000;
  49. return 0;
  50. }
  51. static const struct snd_timer_hardware snd_emu10k1_timer_hw = {
  52. .flags = SNDRV_TIMER_HW_AUTO,
  53. .ticks = 1024,
  54. .start = snd_emu10k1_timer_start,
  55. .stop = snd_emu10k1_timer_stop,
  56. .c_resolution = snd_emu10k1_timer_c_resolution,
  57. .precise_resolution = snd_emu10k1_timer_precise_resolution,
  58. };
  59. int snd_emu10k1_timer(struct snd_emu10k1 *emu, int device)
  60. {
  61. struct snd_timer *timer = NULL;
  62. struct snd_timer_id tid;
  63. int err;
  64. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  65. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  66. tid.card = emu->card->number;
  67. tid.device = device;
  68. tid.subdevice = 0;
  69. err = snd_timer_new(emu->card, "EMU10K1", &tid, &timer);
  70. if (err >= 0) {
  71. strcpy(timer->name, "EMU10K1 timer");
  72. timer->private_data = emu;
  73. timer->hw = snd_emu10k1_timer_hw;
  74. }
  75. emu->timer = timer;
  76. return err;
  77. }