xref: /linux/sound/pci/emu10k1/timer.c (revision c894ec016c9d0418dd832202225a8c64f450d71e)
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  *  Routines for control of EMU10K1 chips
6  *
7  *  BUGS:
8  *    --
9  *
10  *  TODO:
11  *    --
12  */
13 
14 #include <linux/time.h>
15 #include <sound/core.h>
16 #include <sound/emu10k1.h>
17 
18 static int snd_emu10k1_timer_start(struct snd_timer *timer)
19 {
20 	struct snd_emu10k1 *emu;
21 	unsigned int delay;
22 
23 	emu = snd_timer_chip(timer);
24 	delay = timer->sticks - 1;
25 	if (delay < 5 ) /* minimum time is 5 ticks */
26 		delay = 5;
27 	snd_emu10k1_intr_enable(emu, INTE_INTERVALTIMERENB);
28 	outw(delay & TIMER_RATE_MASK, emu->port + TIMER);
29 	return 0;
30 }
31 
32 static int snd_emu10k1_timer_stop(struct snd_timer *timer)
33 {
34 	struct snd_emu10k1 *emu;
35 
36 	emu = snd_timer_chip(timer);
37 	snd_emu10k1_intr_disable(emu, INTE_INTERVALTIMERENB);
38 	return 0;
39 }
40 
41 static int snd_emu10k1_timer_precise_resolution(struct snd_timer *timer,
42 					       unsigned long *num, unsigned long *den)
43 {
44 	*num = 1;
45 	*den = 48000;
46 	return 0;
47 }
48 
49 static const struct snd_timer_hardware snd_emu10k1_timer_hw = {
50 	.flags = SNDRV_TIMER_HW_AUTO,
51 	.resolution = 20833, /* 1 sample @ 48KHZ = 20.833...us */
52 	.ticks = 1024,
53 	.start = snd_emu10k1_timer_start,
54 	.stop = snd_emu10k1_timer_stop,
55 	.precise_resolution = snd_emu10k1_timer_precise_resolution,
56 };
57 
58 int snd_emu10k1_timer(struct snd_emu10k1 *emu, int device)
59 {
60 	struct snd_timer *timer = NULL;
61 	struct snd_timer_id tid;
62 	int err;
63 
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 }
78