Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Digital Audio (PCM) abstract layer |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/time.h> |
Florian Fainelli | a960539 | 2009-12-21 16:36:10 -0800 | [diff] [blame] | 23 | #include <linux/gcd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <sound/core.h> |
| 25 | #include <sound/pcm.h> |
| 26 | #include <sound/timer.h> |
| 27 | |
| 28 | /* |
| 29 | * Timer functions |
| 30 | */ |
| 31 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 32 | void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
| 34 | unsigned long rate, mult, fsize, l, post; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 35 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | mult = 1000000000; |
| 38 | rate = runtime->rate; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 39 | if (snd_BUG_ON(!rate)) |
| 40 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | l = gcd(mult, rate); |
| 42 | mult /= l; |
| 43 | rate /= l; |
| 44 | fsize = runtime->period_size; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 45 | if (snd_BUG_ON(!fsize)) |
| 46 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | l = gcd(rate, fsize); |
| 48 | rate /= l; |
| 49 | fsize /= l; |
| 50 | post = 1; |
| 51 | while ((mult * fsize) / fsize != mult) { |
| 52 | mult /= 2; |
| 53 | post *= 2; |
| 54 | } |
| 55 | if (rate == 0) { |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 56 | pcm_err(substream->pcm, |
| 57 | "pcm timer resolution out of range (rate = %u, period_size = %lu)\n", |
| 58 | runtime->rate, runtime->period_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | runtime->timer_resolution = -1; |
| 60 | return; |
| 61 | } |
| 62 | runtime->timer_resolution = (mult * fsize / rate) * post; |
| 63 | } |
| 64 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 65 | static unsigned long snd_pcm_timer_resolution(struct snd_timer * timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 67 | struct snd_pcm_substream *substream; |
Karthikeyan Mani | 9a67a57 | 2017-09-28 11:06:55 -0700 | [diff] [blame^] | 68 | unsigned long ret = 0, flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | substream = timer->private_data; |
Karthikeyan Mani | 9a67a57 | 2017-09-28 11:06:55 -0700 | [diff] [blame^] | 71 | spin_lock_irqsave(&substream->runtime_lock, flags); |
| 72 | if (substream->runtime) |
| 73 | ret = substream->runtime->timer_resolution; |
| 74 | else |
| 75 | ret = 0; |
| 76 | spin_unlock_irqrestore(&substream->runtime_lock, flags); |
| 77 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 80 | static int snd_pcm_timer_start(struct snd_timer * timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 82 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | substream = snd_timer_chip(timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | substream->timer_running = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 89 | static int snd_pcm_timer_stop(struct snd_timer * timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 91 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | substream = snd_timer_chip(timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | substream->timer_running = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 98 | static struct snd_timer_hardware snd_pcm_timer = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
| 100 | .flags = SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_SLAVE, |
| 101 | .resolution = 0, |
| 102 | .ticks = 1, |
| 103 | .c_resolution = snd_pcm_timer_resolution, |
| 104 | .start = snd_pcm_timer_start, |
| 105 | .stop = snd_pcm_timer_stop, |
| 106 | }; |
| 107 | |
| 108 | /* |
| 109 | * Init functions |
| 110 | */ |
| 111 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 112 | static void snd_pcm_timer_free(struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 114 | struct snd_pcm_substream *substream = timer->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | substream->timer = NULL; |
| 116 | } |
| 117 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 118 | void snd_pcm_timer_init(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 120 | struct snd_timer_id tid; |
| 121 | struct snd_timer *timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; |
| 124 | tid.dev_class = SNDRV_TIMER_CLASS_PCM; |
| 125 | tid.card = substream->pcm->card->number; |
| 126 | tid.device = substream->pcm->device; |
| 127 | tid.subdevice = (substream->number << 1) | (substream->stream & 1); |
| 128 | if (snd_timer_new(substream->pcm->card, "PCM", &tid, &timer) < 0) |
| 129 | return; |
| 130 | sprintf(timer->name, "PCM %s %i-%i-%i", |
| 131 | substream->stream == SNDRV_PCM_STREAM_CAPTURE ? |
| 132 | "capture" : "playback", |
| 133 | tid.card, tid.device, tid.subdevice); |
| 134 | timer->hw = snd_pcm_timer; |
| 135 | if (snd_device_register(timer->card, timer) < 0) { |
| 136 | snd_device_free(timer->card, timer); |
| 137 | return; |
| 138 | } |
| 139 | timer->private_data = substream; |
| 140 | timer->private_free = snd_pcm_timer_free; |
| 141 | substream->timer = timer; |
| 142 | } |
| 143 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 144 | void snd_pcm_timer_done(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
| 146 | if (substream->timer) { |
| 147 | snd_device_free(substream->pcm->card, substream->timer); |
| 148 | substream->timer = NULL; |
| 149 | } |
| 150 | } |