blob: 11389f13de73ebadd9e4d95a2ee102c67875073f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/time.h>
Florian Fainellia9605392009-12-21 16:36:10 -080023#include <linux/gcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/timer.h>
27
Colin Ian King7421a162017-05-30 11:39:45 +010028#include "pcm_local.h"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * Timer functions
32 */
33
Takashi Iwai877211f2005-11-17 13:59:38 +010034void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 unsigned long rate, mult, fsize, l, post;
Takashi Iwai877211f2005-11-17 13:59:38 +010037 struct snd_pcm_runtime *runtime = substream->runtime;
Colin Ian King7421a162017-05-30 11:39:45 +010038
39 mult = 1000000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 rate = runtime->rate;
Takashi Iwai7eaa9432008-08-08 17:09:09 +020041 if (snd_BUG_ON(!rate))
42 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 l = gcd(mult, rate);
44 mult /= l;
45 rate /= l;
46 fsize = runtime->period_size;
Takashi Iwai7eaa9432008-08-08 17:09:09 +020047 if (snd_BUG_ON(!fsize))
48 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 l = gcd(rate, fsize);
50 rate /= l;
51 fsize /= l;
52 post = 1;
53 while ((mult * fsize) / fsize != mult) {
54 mult /= 2;
55 post *= 2;
56 }
57 if (rate == 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +010058 pcm_err(substream->pcm,
59 "pcm timer resolution out of range (rate = %u, period_size = %lu)\n",
60 runtime->rate, runtime->period_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 runtime->timer_resolution = -1;
62 return;
63 }
64 runtime->timer_resolution = (mult * fsize / rate) * post;
65}
66
Takashi Iwai877211f2005-11-17 13:59:38 +010067static unsigned long snd_pcm_timer_resolution(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Takashi Iwai877211f2005-11-17 13:59:38 +010069 struct snd_pcm_substream *substream;
Colin Ian King7421a162017-05-30 11:39:45 +010070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 substream = timer->private_data;
72 return substream->runtime ? substream->runtime->timer_resolution : 0;
73}
74
Takashi Iwai877211f2005-11-17 13:59:38 +010075static int snd_pcm_timer_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Takashi Iwai877211f2005-11-17 13:59:38 +010077 struct snd_pcm_substream *substream;
Colin Ian King7421a162017-05-30 11:39:45 +010078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 substream = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 substream->timer_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return 0;
82}
83
Takashi Iwai877211f2005-11-17 13:59:38 +010084static int snd_pcm_timer_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Takashi Iwai877211f2005-11-17 13:59:38 +010086 struct snd_pcm_substream *substream;
Colin Ian King7421a162017-05-30 11:39:45 +010087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 substream = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 substream->timer_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return 0;
91}
92
Takashi Iwai877211f2005-11-17 13:59:38 +010093static struct snd_timer_hardware snd_pcm_timer =
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 .flags = SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_SLAVE,
96 .resolution = 0,
97 .ticks = 1,
98 .c_resolution = snd_pcm_timer_resolution,
99 .start = snd_pcm_timer_start,
100 .stop = snd_pcm_timer_stop,
101};
102
103/*
104 * Init functions
105 */
106
Takashi Iwai877211f2005-11-17 13:59:38 +0100107static void snd_pcm_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Takashi Iwai877211f2005-11-17 13:59:38 +0100109 struct snd_pcm_substream *substream = timer->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 substream->timer = NULL;
111}
112
Takashi Iwai877211f2005-11-17 13:59:38 +0100113void snd_pcm_timer_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Takashi Iwai877211f2005-11-17 13:59:38 +0100115 struct snd_timer_id tid;
116 struct snd_timer *timer;
Colin Ian King7421a162017-05-30 11:39:45 +0100117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
119 tid.dev_class = SNDRV_TIMER_CLASS_PCM;
120 tid.card = substream->pcm->card->number;
121 tid.device = substream->pcm->device;
122 tid.subdevice = (substream->number << 1) | (substream->stream & 1);
123 if (snd_timer_new(substream->pcm->card, "PCM", &tid, &timer) < 0)
124 return;
125 sprintf(timer->name, "PCM %s %i-%i-%i",
126 substream->stream == SNDRV_PCM_STREAM_CAPTURE ?
127 "capture" : "playback",
128 tid.card, tid.device, tid.subdevice);
129 timer->hw = snd_pcm_timer;
130 if (snd_device_register(timer->card, timer) < 0) {
131 snd_device_free(timer->card, timer);
132 return;
133 }
134 timer->private_data = substream;
135 timer->private_free = snd_pcm_timer_free;
136 substream->timer = timer;
137}
138
Takashi Iwai877211f2005-11-17 13:59:38 +0100139void snd_pcm_timer_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 if (substream->timer) {
142 snd_device_free(substream->pcm->card, substream->timer);
143 substream->timer = NULL;
144 }
145}