xref: /openbmc/linux/sound/pci/emu10k1/timer.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Copyright (c) by Lee Revell <rlrevell@joe-job.com>
41da177e4SLinus Torvalds  *                   Clemens Ladisch <clemens@ladisch.de>
5*6d68d9cbSOswald Buddenhagen  *                   Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
6*6d68d9cbSOswald Buddenhagen  *
71da177e4SLinus Torvalds  *  Routines for control of EMU10K1 chips
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/time.h>
111da177e4SLinus Torvalds #include <sound/core.h>
121da177e4SLinus Torvalds #include <sound/emu10k1.h>
131da177e4SLinus Torvalds 
snd_emu10k1_timer_start(struct snd_timer * timer)14eb4698f3STakashi Iwai static int snd_emu10k1_timer_start(struct snd_timer *timer)
151da177e4SLinus Torvalds {
16eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
171da177e4SLinus Torvalds 	unsigned int delay;
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds 	emu = snd_timer_chip(timer);
201da177e4SLinus Torvalds 	delay = timer->sticks - 1;
211da177e4SLinus Torvalds 	if (delay < 5 ) /* minimum time is 5 ticks */
221da177e4SLinus Torvalds 		delay = 5;
231da177e4SLinus Torvalds 	snd_emu10k1_intr_enable(emu, INTE_INTERVALTIMERENB);
241da177e4SLinus Torvalds 	outw(delay & TIMER_RATE_MASK, emu->port + TIMER);
251da177e4SLinus Torvalds 	return 0;
261da177e4SLinus Torvalds }
271da177e4SLinus Torvalds 
snd_emu10k1_timer_stop(struct snd_timer * timer)28eb4698f3STakashi Iwai static int snd_emu10k1_timer_stop(struct snd_timer *timer)
291da177e4SLinus Torvalds {
30eb4698f3STakashi Iwai 	struct snd_emu10k1 *emu;
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds 	emu = snd_timer_chip(timer);
331da177e4SLinus Torvalds 	snd_emu10k1_intr_disable(emu, INTE_INTERVALTIMERENB);
341da177e4SLinus Torvalds 	return 0;
351da177e4SLinus Torvalds }
361da177e4SLinus Torvalds 
snd_emu10k1_timer_c_resolution(struct snd_timer * timer)37ca533448SOswald Buddenhagen static unsigned long snd_emu10k1_timer_c_resolution(struct snd_timer *timer)
38ca533448SOswald Buddenhagen {
39ca533448SOswald Buddenhagen 	struct snd_emu10k1 *emu = snd_timer_chip(timer);
40ca533448SOswald Buddenhagen 
41ca533448SOswald Buddenhagen 	if (emu->card_capabilities->emu_model &&
42ca533448SOswald Buddenhagen 	    emu->emu1010.word_clock == 44100)
43ca533448SOswald Buddenhagen 		return 22676;  // 1 sample @ 44.1 kHz = 22.675736...us
44ca533448SOswald Buddenhagen 	else
45ca533448SOswald Buddenhagen 		return 20833;  // 1 sample @ 48 kHz = 20.833...us
46ca533448SOswald Buddenhagen }
47ca533448SOswald Buddenhagen 
snd_emu10k1_timer_precise_resolution(struct snd_timer * timer,unsigned long * num,unsigned long * den)48eb4698f3STakashi Iwai static int snd_emu10k1_timer_precise_resolution(struct snd_timer *timer,
491da177e4SLinus Torvalds 					       unsigned long *num, unsigned long *den)
501da177e4SLinus Torvalds {
51ca533448SOswald Buddenhagen 	struct snd_emu10k1 *emu = snd_timer_chip(timer);
52ca533448SOswald Buddenhagen 
531da177e4SLinus Torvalds 	*num = 1;
54ca533448SOswald Buddenhagen 	if (emu->card_capabilities->emu_model)
55ca533448SOswald Buddenhagen 		*den = emu->emu1010.word_clock;
56ca533448SOswald Buddenhagen 	else
571da177e4SLinus Torvalds 		*den = 48000;
581da177e4SLinus Torvalds 	return 0;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
615ff16a3dSTakashi Iwai static const struct snd_timer_hardware snd_emu10k1_timer_hw = {
621da177e4SLinus Torvalds 	.flags = SNDRV_TIMER_HW_AUTO,
631da177e4SLinus Torvalds 	.ticks = 1024,
641da177e4SLinus Torvalds 	.start = snd_emu10k1_timer_start,
651da177e4SLinus Torvalds 	.stop = snd_emu10k1_timer_stop,
66ca533448SOswald Buddenhagen 	.c_resolution = snd_emu10k1_timer_c_resolution,
671da177e4SLinus Torvalds 	.precise_resolution = snd_emu10k1_timer_precise_resolution,
681da177e4SLinus Torvalds };
691da177e4SLinus Torvalds 
snd_emu10k1_timer(struct snd_emu10k1 * emu,int device)70e23e7a14SBill Pemberton int snd_emu10k1_timer(struct snd_emu10k1 *emu, int device)
711da177e4SLinus Torvalds {
72eb4698f3STakashi Iwai 	struct snd_timer *timer = NULL;
73eb4698f3STakashi Iwai 	struct snd_timer_id tid;
741da177e4SLinus Torvalds 	int err;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
771da177e4SLinus Torvalds 	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
781da177e4SLinus Torvalds 	tid.card = emu->card->number;
791da177e4SLinus Torvalds 	tid.device = device;
801da177e4SLinus Torvalds 	tid.subdevice = 0;
8112bda107STakashi Iwai 	err = snd_timer_new(emu->card, "EMU10K1", &tid, &timer);
8212bda107STakashi Iwai 	if (err >= 0) {
831da177e4SLinus Torvalds 		strcpy(timer->name, "EMU10K1 timer");
841da177e4SLinus Torvalds 		timer->private_data = emu;
851da177e4SLinus Torvalds 		timer->hw = snd_emu10k1_timer_hw;
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds 	emu->timer = timer;
881da177e4SLinus Torvalds 	return err;
891da177e4SLinus Torvalds }
90