blob: 1cd149b9ce69e4060c13c1cae1e75115e24c948b [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
6 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +01008 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020016 * TODO:
17 * o Add hw rules to enforce rates, etc.
18 * o More testing with other codecs/machines.
19 * o Add more codecs and platforms to ensure good API coverage.
20 * o Support TDM on PCM and I2S
21 */
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/pm.h>
28#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070029#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020030#include <linux/platform_device.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020031#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/soc.h>
35#include <sound/soc-dapm.h>
36#include <sound/initval.h>
37
Frank Mandarinodb2a4162006-10-06 18:31:09 +020038static DEFINE_MUTEX(pcm_mutex);
39static DEFINE_MUTEX(io_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
41
Mark Brown384c89e2008-12-03 17:34:03 +000042#ifdef CONFIG_DEBUG_FS
43static struct dentry *debugfs_root;
44#endif
45
Mark Brownc5af3a22008-11-28 13:29:45 +000046static DEFINE_MUTEX(client_mutex);
47static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000048static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000049static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000050static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000051
52static int snd_soc_register_card(struct snd_soc_card *card);
53static int snd_soc_unregister_card(struct snd_soc_card *card);
54
Frank Mandarinodb2a4162006-10-06 18:31:09 +020055/*
56 * This is a timeout to do a DAPM powerdown after a stream is closed().
57 * It can be used to eliminate pops between different playback streams, e.g.
58 * between two audio tracks.
59 */
60static int pmdown_time = 5000;
61module_param(pmdown_time, int, 0);
62MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
63
Liam Girdwood965ac422007-01-31 14:14:57 +010064/*
65 * This function forces any delayed work to be queued and run.
66 */
67static int run_delayed_work(struct delayed_work *dwork)
68{
69 int ret;
70
71 /* cancel any work waiting to be queued. */
72 ret = cancel_delayed_work(dwork);
73
74 /* if there was any work waiting then we run it now and
75 * wait for it's completion */
76 if (ret) {
77 schedule_delayed_work(dwork, 0);
78 flush_scheduled_work();
79 }
80 return ret;
81}
82
Frank Mandarinodb2a4162006-10-06 18:31:09 +020083#ifdef CONFIG_SND_SOC_AC97_BUS
84/* unregister ac97 codec */
85static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
86{
87 if (codec->ac97->dev.bus)
88 device_unregister(&codec->ac97->dev);
89 return 0;
90}
91
92/* stop no dev release warning */
93static void soc_ac97_device_release(struct device *dev){}
94
95/* register ac97 codec to bus */
96static int soc_ac97_dev_register(struct snd_soc_codec *codec)
97{
98 int err;
99
100 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100101 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200102 codec->ac97->dev.release = soc_ac97_device_release;
103
Kay Sieversbb072bf2008-11-02 03:50:35 +0100104 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
105 codec->card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200106 err = device_register(&codec->ac97->dev);
107 if (err < 0) {
108 snd_printk(KERN_ERR "Can't register ac97 bus\n");
109 codec->ac97->dev.bus = NULL;
110 return err;
111 }
112 return 0;
113}
114#endif
115
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200116/*
117 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
118 * then initialized and any private data can be allocated. This also calls
119 * startup for the cpu DAI, platform, machine and codec DAI.
120 */
121static int soc_pcm_open(struct snd_pcm_substream *substream)
122{
123 struct snd_soc_pcm_runtime *rtd = substream->private_data;
124 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000125 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200126 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100127 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000128 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100129 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
130 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200131 int ret = 0;
132
133 mutex_lock(&pcm_mutex);
134
135 /* startup the audio subsystem */
Eric Miao6335d052009-03-03 09:41:00 +0800136 if (cpu_dai->ops->startup) {
137 ret = cpu_dai->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200138 if (ret < 0) {
139 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100140 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200141 goto out;
142 }
143 }
144
145 if (platform->pcm_ops->open) {
146 ret = platform->pcm_ops->open(substream);
147 if (ret < 0) {
148 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
149 goto platform_err;
150 }
151 }
152
Eric Miao6335d052009-03-03 09:41:00 +0800153 if (codec_dai->ops->startup) {
154 ret = codec_dai->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100155 if (ret < 0) {
156 printk(KERN_ERR "asoc: can't open codec %s\n",
157 codec_dai->name);
158 goto codec_dai_err;
159 }
160 }
161
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200162 if (machine->ops && machine->ops->startup) {
163 ret = machine->ops->startup(substream);
164 if (ret < 0) {
165 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
166 goto machine_err;
167 }
168 }
169
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200170 /* Check that the codec and cpu DAI's are compatible */
171 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
172 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200173 max(codec_dai->playback.rate_min,
174 cpu_dai->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200175 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200176 min(codec_dai->playback.rate_max,
177 cpu_dai->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200178 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100179 max(codec_dai->playback.channels_min,
180 cpu_dai->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200181 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100182 min(codec_dai->playback.channels_max,
183 cpu_dai->playback.channels_max);
184 runtime->hw.formats =
185 codec_dai->playback.formats & cpu_dai->playback.formats;
186 runtime->hw.rates =
187 codec_dai->playback.rates & cpu_dai->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200188 } else {
189 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200190 max(codec_dai->capture.rate_min,
191 cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200192 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200193 min(codec_dai->capture.rate_max,
194 cpu_dai->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200195 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100196 max(codec_dai->capture.channels_min,
197 cpu_dai->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200198 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100199 min(codec_dai->capture.channels_max,
200 cpu_dai->capture.channels_max);
201 runtime->hw.formats =
202 codec_dai->capture.formats & cpu_dai->capture.formats;
203 runtime->hw.rates =
204 codec_dai->capture.rates & cpu_dai->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200205 }
206
207 snd_pcm_limit_hw_rates(runtime);
208 if (!runtime->hw.rates) {
209 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100210 codec_dai->name, cpu_dai->name);
211 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200212 }
213 if (!runtime->hw.formats) {
214 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100215 codec_dai->name, cpu_dai->name);
216 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200217 }
218 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
219 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100220 codec_dai->name, cpu_dai->name);
221 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200222 }
223
Mark Brownf24368c2008-10-21 21:45:08 +0100224 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
225 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
226 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
227 runtime->hw.channels_max);
228 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
229 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200230
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200231 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100232 cpu_dai->playback.active = codec_dai->playback.active = 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200233 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100234 cpu_dai->capture.active = codec_dai->capture.active = 1;
235 cpu_dai->active = codec_dai->active = 1;
236 cpu_dai->runtime = runtime;
Mark Brown6627a652009-01-23 22:55:23 +0000237 card->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200238 mutex_unlock(&pcm_mutex);
239 return 0;
240
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100241machine_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200242 if (machine->ops && machine->ops->shutdown)
243 machine->ops->shutdown(substream);
244
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100245codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200246 if (platform->pcm_ops->close)
247 platform->pcm_ops->close(substream);
248
249platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800250 if (cpu_dai->ops->shutdown)
251 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200252out:
253 mutex_unlock(&pcm_mutex);
254 return ret;
255}
256
257/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200258 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200259 * This is to ensure there are no pops or clicks in between any music tracks
260 * due to DAPM power cycling.
261 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100262static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200263{
Mark Brown63084192008-12-02 15:08:03 +0000264 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
265 delayed_work.work);
266 struct snd_soc_device *socdev = card->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000267 struct snd_soc_codec *codec = card->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100268 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200269 int i;
270
271 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200272 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200273 codec_dai = &codec->dai[i];
274
Mark Brownf24368c2008-10-21 21:45:08 +0100275 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
276 codec_dai->playback.stream_name,
277 codec_dai->playback.active ? "active" : "inactive",
278 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200279
280 /* are we waiting on this codec DAI stream */
281 if (codec_dai->pop_wait == 1) {
282
Mark Brown0be98982008-05-19 12:31:28 +0200283 /* Reduce power if no longer active */
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100284 if (codec->active == 0) {
Mark Brownf24368c2008-10-21 21:45:08 +0100285 pr_debug("pop wq D1 %s %s\n", codec->name,
286 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200287 snd_soc_dapm_set_bias_level(socdev,
288 SND_SOC_BIAS_PREPARE);
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100289 }
290
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200291 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100292 snd_soc_dapm_stream_event(codec,
293 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200294 SND_SOC_DAPM_STREAM_STOP);
295
Mark Brown0be98982008-05-19 12:31:28 +0200296 /* Fall into standby if no longer active */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200297 if (codec->active == 0) {
Mark Brownf24368c2008-10-21 21:45:08 +0100298 pr_debug("pop wq D3 %s %s\n", codec->name,
299 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200300 snd_soc_dapm_set_bias_level(socdev,
301 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200302 }
303 }
304 }
305 mutex_unlock(&pcm_mutex);
306}
307
308/*
309 * Called by ALSA when a PCM substream is closed. Private data can be
310 * freed here. The cpu DAI, codec DAI, machine and platform are also
311 * shutdown.
312 */
313static int soc_codec_close(struct snd_pcm_substream *substream)
314{
315 struct snd_soc_pcm_runtime *rtd = substream->private_data;
316 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000317 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100318 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000319 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100320 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
321 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000322 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200323
324 mutex_lock(&pcm_mutex);
325
326 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100327 cpu_dai->playback.active = codec_dai->playback.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200328 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100329 cpu_dai->capture.active = codec_dai->capture.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200330
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100331 if (codec_dai->playback.active == 0 &&
332 codec_dai->capture.active == 0) {
333 cpu_dai->active = codec_dai->active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200334 }
335 codec->active--;
336
Mark Brown6010b2d2008-09-06 18:33:24 +0100337 /* Muting the DAC suppresses artifacts caused during digital
338 * shutdown, for example from stopping clocks.
339 */
340 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
341 snd_soc_dai_digital_mute(codec_dai, 1);
342
Eric Miao6335d052009-03-03 09:41:00 +0800343 if (cpu_dai->ops->shutdown)
344 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200345
Eric Miao6335d052009-03-03 09:41:00 +0800346 if (codec_dai->ops->shutdown)
347 codec_dai->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200348
349 if (machine->ops && machine->ops->shutdown)
350 machine->ops->shutdown(substream);
351
352 if (platform->pcm_ops->close)
353 platform->pcm_ops->close(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100354 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200355
356 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
357 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100358 codec_dai->pop_wait = 1;
Mark Brown63084192008-12-02 15:08:03 +0000359 schedule_delayed_work(&card->delayed_work,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200360 msecs_to_jiffies(pmdown_time));
361 } else {
362 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100363 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100364 codec_dai->capture.stream_name,
365 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200366
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100367 if (codec->active == 0 && codec_dai->pop_wait == 0)
Mark Brown0be98982008-05-19 12:31:28 +0200368 snd_soc_dapm_set_bias_level(socdev,
369 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200370 }
371
372 mutex_unlock(&pcm_mutex);
373 return 0;
374}
375
376/*
377 * Called by ALSA when the PCM substream is prepared, can set format, sample
378 * rate, etc. This function is non atomic and can be called multiple times,
379 * it can refer to the runtime info.
380 */
381static int soc_pcm_prepare(struct snd_pcm_substream *substream)
382{
383 struct snd_soc_pcm_runtime *rtd = substream->private_data;
384 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000385 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100386 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000387 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100388 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
389 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000390 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200391 int ret = 0;
392
393 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100394
395 if (machine->ops && machine->ops->prepare) {
396 ret = machine->ops->prepare(substream);
397 if (ret < 0) {
398 printk(KERN_ERR "asoc: machine prepare error\n");
399 goto out;
400 }
401 }
402
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200403 if (platform->pcm_ops->prepare) {
404 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200405 if (ret < 0) {
406 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200407 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200408 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200409 }
410
Eric Miao6335d052009-03-03 09:41:00 +0800411 if (codec_dai->ops->prepare) {
412 ret = codec_dai->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200413 if (ret < 0) {
414 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200415 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200416 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200417 }
418
Eric Miao6335d052009-03-03 09:41:00 +0800419 if (cpu_dai->ops->prepare) {
420 ret = cpu_dai->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100421 if (ret < 0) {
422 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
423 goto out;
424 }
425 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200426
Mark Brownd45f6212008-10-14 13:58:36 +0100427 /* cancel any delayed stream shutdown that is pending */
428 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
429 codec_dai->pop_wait) {
430 codec_dai->pop_wait = 0;
Mark Brown63084192008-12-02 15:08:03 +0000431 cancel_delayed_work(&card->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100432 }
433
434 /* do we need to power up codec */
435 if (codec->bias_level != SND_SOC_BIAS_ON) {
436 snd_soc_dapm_set_bias_level(socdev,
437 SND_SOC_BIAS_PREPARE);
438
439 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
440 snd_soc_dapm_stream_event(codec,
441 codec_dai->playback.stream_name,
442 SND_SOC_DAPM_STREAM_START);
443 else
444 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100445 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200446 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100447
448 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
449 snd_soc_dai_digital_mute(codec_dai, 0);
450
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200451 } else {
Mark Brownd45f6212008-10-14 13:58:36 +0100452 /* codec already powered - power on widgets */
453 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
454 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100455 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200456 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100457 else
458 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100459 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200460 SND_SOC_DAPM_STREAM_START);
461
Mark Brownd45f6212008-10-14 13:58:36 +0100462 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200463 }
464
465out:
466 mutex_unlock(&pcm_mutex);
467 return ret;
468}
469
470/*
471 * Called by ALSA when the hardware params are set by application. This
472 * function can also be called multiple times and can allocate buffers
473 * (using snd_pcm_lib_* ). It's non-atomic.
474 */
475static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
476 struct snd_pcm_hw_params *params)
477{
478 struct snd_soc_pcm_runtime *rtd = substream->private_data;
479 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100480 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000481 struct snd_soc_card *card = socdev->card;
482 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100483 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
484 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200485 int ret = 0;
486
487 mutex_lock(&pcm_mutex);
488
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100489 if (machine->ops && machine->ops->hw_params) {
490 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200491 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100492 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493 goto out;
494 }
495 }
496
Eric Miao6335d052009-03-03 09:41:00 +0800497 if (codec_dai->ops->hw_params) {
498 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100499 if (ret < 0) {
500 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
501 codec_dai->name);
502 goto codec_err;
503 }
504 }
505
Eric Miao6335d052009-03-03 09:41:00 +0800506 if (cpu_dai->ops->hw_params) {
507 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200508 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200509 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100510 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200511 goto interface_err;
512 }
513 }
514
515 if (platform->pcm_ops->hw_params) {
516 ret = platform->pcm_ops->hw_params(substream, params);
517 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200518 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200519 platform->name);
520 goto platform_err;
521 }
522 }
523
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524out:
525 mutex_unlock(&pcm_mutex);
526 return ret;
527
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800529 if (cpu_dai->ops->hw_free)
530 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531
532interface_err:
Eric Miao6335d052009-03-03 09:41:00 +0800533 if (codec_dai->ops->hw_free)
534 codec_dai->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100535
536codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200537 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100538 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200539
540 mutex_unlock(&pcm_mutex);
541 return ret;
542}
543
544/*
545 * Free's resources allocated by hw_params, can be called multiple times
546 */
547static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
548{
549 struct snd_soc_pcm_runtime *rtd = substream->private_data;
550 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100551 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000552 struct snd_soc_card *card = socdev->card;
553 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100554 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
555 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000556 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200557
558 mutex_lock(&pcm_mutex);
559
560 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100561 if (!codec->active)
562 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200563
564 /* free any machine hw params */
565 if (machine->ops && machine->ops->hw_free)
566 machine->ops->hw_free(substream);
567
568 /* free any DMA resources */
569 if (platform->pcm_ops->hw_free)
570 platform->pcm_ops->hw_free(substream);
571
572 /* now free hw params for the DAI's */
Eric Miao6335d052009-03-03 09:41:00 +0800573 if (codec_dai->ops->hw_free)
574 codec_dai->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200575
Eric Miao6335d052009-03-03 09:41:00 +0800576 if (cpu_dai->ops->hw_free)
577 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200578
579 mutex_unlock(&pcm_mutex);
580 return 0;
581}
582
583static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
584{
585 struct snd_soc_pcm_runtime *rtd = substream->private_data;
586 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000587 struct snd_soc_card *card= socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100588 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000589 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100590 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
591 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592 int ret;
593
Eric Miao6335d052009-03-03 09:41:00 +0800594 if (codec_dai->ops->trigger) {
595 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596 if (ret < 0)
597 return ret;
598 }
599
600 if (platform->pcm_ops->trigger) {
601 ret = platform->pcm_ops->trigger(substream, cmd);
602 if (ret < 0)
603 return ret;
604 }
605
Eric Miao6335d052009-03-03 09:41:00 +0800606 if (cpu_dai->ops->trigger) {
607 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200608 if (ret < 0)
609 return ret;
610 }
611 return 0;
612}
613
614/* ASoC PCM operations */
615static struct snd_pcm_ops soc_pcm_ops = {
616 .open = soc_pcm_open,
617 .close = soc_codec_close,
618 .hw_params = soc_pcm_hw_params,
619 .hw_free = soc_pcm_hw_free,
620 .prepare = soc_pcm_prepare,
621 .trigger = soc_pcm_trigger,
622};
623
624#ifdef CONFIG_PM
625/* powers down audio subsystem for suspend */
626static int soc_suspend(struct platform_device *pdev, pm_message_t state)
627{
Mark Brown3ff3f642008-05-19 12:32:25 +0200628 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000629 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000630 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200631 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000632 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200633 int i;
634
Andy Green6ed25972008-06-13 16:24:05 +0100635 /* Due to the resume being scheduled into a workqueue we could
636 * suspend before that's finished - wait for it to complete.
637 */
638 snd_power_lock(codec->card);
639 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
640 snd_power_unlock(codec->card);
641
642 /* we're going to block userspace touching us until resume completes */
643 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
644
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200645 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000646 for (i = 0; i < card->num_links; i++) {
647 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800648 if (dai->ops->digital_mute && dai->playback.active)
649 dai->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200650 }
651
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100652 /* suspend all pcms */
Mark Brown87506542008-11-18 20:50:34 +0000653 for (i = 0; i < card->num_links; i++)
654 snd_pcm_suspend_all(card->dai_link[i].pcm);
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100655
Mark Brown87506542008-11-18 20:50:34 +0000656 if (card->suspend_pre)
657 card->suspend_pre(pdev, state);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200658
Mark Brown87506542008-11-18 20:50:34 +0000659 for (i = 0; i < card->num_links; i++) {
660 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000661 if (cpu_dai->suspend && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000662 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663 if (platform->suspend)
Mark Brown07c84d02008-12-03 18:17:28 +0000664 platform->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200665 }
666
667 /* close any waiting streams and save state */
Mark Brown63084192008-12-02 15:08:03 +0000668 run_delayed_work(&card->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200669 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200670
Mark Brown3ff3f642008-05-19 12:32:25 +0200671 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200672 char *stream = codec->dai[i].playback.stream_name;
673 if (stream != NULL)
674 snd_soc_dapm_stream_event(codec, stream,
675 SND_SOC_DAPM_STREAM_SUSPEND);
676 stream = codec->dai[i].capture.stream_name;
677 if (stream != NULL)
678 snd_soc_dapm_stream_event(codec, stream,
679 SND_SOC_DAPM_STREAM_SUSPEND);
680 }
681
682 if (codec_dev->suspend)
683 codec_dev->suspend(pdev, state);
684
Mark Brown87506542008-11-18 20:50:34 +0000685 for (i = 0; i < card->num_links; i++) {
686 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000687 if (cpu_dai->suspend && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000688 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200689 }
690
Mark Brown87506542008-11-18 20:50:34 +0000691 if (card->suspend_post)
692 card->suspend_post(pdev, state);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200693
694 return 0;
695}
696
Andy Green6ed25972008-06-13 16:24:05 +0100697/* deferred resume work, so resume can complete before we finished
698 * setting our codec back up, which can be very slow on I2C
699 */
700static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200701{
Mark Brown63084192008-12-02 15:08:03 +0000702 struct snd_soc_card *card = container_of(work,
703 struct snd_soc_card,
704 deferred_resume_work);
705 struct snd_soc_device *socdev = card->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000706 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200707 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000708 struct snd_soc_codec *codec = card->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100709 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200710 int i;
711
Andy Green6ed25972008-06-13 16:24:05 +0100712 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
713 * so userspace apps are blocked from touching us
714 */
715
Mark Brownfde22f22008-11-24 18:08:18 +0000716 dev_dbg(socdev->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100717
Mark Brown87506542008-11-18 20:50:34 +0000718 if (card->resume_pre)
719 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200720
Mark Brown87506542008-11-18 20:50:34 +0000721 for (i = 0; i < card->num_links; i++) {
722 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000723 if (cpu_dai->resume && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000724 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200725 }
726
727 if (codec_dev->resume)
728 codec_dev->resume(pdev);
729
Mark Brown3ff3f642008-05-19 12:32:25 +0200730 for (i = 0; i < codec->num_dai; i++) {
731 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200732 if (stream != NULL)
733 snd_soc_dapm_stream_event(codec, stream,
734 SND_SOC_DAPM_STREAM_RESUME);
735 stream = codec->dai[i].capture.stream_name;
736 if (stream != NULL)
737 snd_soc_dapm_stream_event(codec, stream,
738 SND_SOC_DAPM_STREAM_RESUME);
739 }
740
Mark Brown3ff3f642008-05-19 12:32:25 +0200741 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +0000742 for (i = 0; i < card->num_links; i++) {
743 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800744 if (dai->ops->digital_mute && dai->playback.active)
745 dai->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200746 }
747
Mark Brown87506542008-11-18 20:50:34 +0000748 for (i = 0; i < card->num_links; i++) {
749 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000750 if (cpu_dai->resume && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000751 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200752 if (platform->resume)
Mark Brown07c84d02008-12-03 18:17:28 +0000753 platform->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200754 }
755
Mark Brown87506542008-11-18 20:50:34 +0000756 if (card->resume_post)
757 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200758
Mark Brownfde22f22008-11-24 18:08:18 +0000759 dev_dbg(socdev->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100760
761 /* userspace can access us now we are back as we were before */
762 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
763}
764
765/* powers up audio subsystem after a suspend */
766static int soc_resume(struct platform_device *pdev)
767{
768 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown63084192008-12-02 15:08:03 +0000769 struct snd_soc_card *card = socdev->card;
Mark Brown64ab9ba2009-03-31 11:27:03 +0100770 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
Andy Green6ed25972008-06-13 16:24:05 +0100771
Mark Brown64ab9ba2009-03-31 11:27:03 +0100772 /* AC97 devices might have other drivers hanging off them so
773 * need to resume immediately. Other drivers don't have that
774 * problem and may take a substantial amount of time to resume
775 * due to I/O costs and anti-pop so handle them out of line.
776 */
777 if (cpu_dai->ac97_control) {
778 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
779 soc_resume_deferred(&card->deferred_resume_work);
780 } else {
781 dev_dbg(socdev->dev, "Scheduling resume work\n");
782 if (!schedule_work(&card->deferred_resume_work))
783 dev_err(socdev->dev, "resume work item may be lost\n");
784 }
Andy Green6ed25972008-06-13 16:24:05 +0100785
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200786 return 0;
787}
788
789#else
790#define soc_suspend NULL
791#define soc_resume NULL
792#endif
793
Mark Brown435c5e22008-12-04 15:32:53 +0000794static void snd_soc_instantiate_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795{
Mark Brown435c5e22008-12-04 15:32:53 +0000796 struct platform_device *pdev = container_of(card->dev,
797 struct platform_device,
798 dev);
799 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
800 struct snd_soc_platform *platform;
801 struct snd_soc_dai *dai;
Mark Brown6b05eda2008-12-08 19:26:48 +0000802 int i, found, ret, ac97;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200803
Mark Brown435c5e22008-12-04 15:32:53 +0000804 if (card->instantiated)
805 return;
Mark Brown63084192008-12-02 15:08:03 +0000806
Mark Brown435c5e22008-12-04 15:32:53 +0000807 found = 0;
808 list_for_each_entry(platform, &platform_list, list)
809 if (card->platform == platform) {
810 found = 1;
811 break;
812 }
813 if (!found) {
814 dev_dbg(card->dev, "Platform %s not registered\n",
815 card->platform->name);
816 return;
Mark Brownc5af3a22008-11-28 13:29:45 +0000817 }
818
Mark Brown6b05eda2008-12-08 19:26:48 +0000819 ac97 = 0;
Mark Brown435c5e22008-12-04 15:32:53 +0000820 for (i = 0; i < card->num_links; i++) {
821 found = 0;
822 list_for_each_entry(dai, &dai_list, list)
823 if (card->dai_link[i].cpu_dai == dai) {
824 found = 1;
825 break;
826 }
827 if (!found) {
828 dev_dbg(card->dev, "DAI %s not registered\n",
829 card->dai_link[i].cpu_dai->name);
830 return;
831 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000832
833 if (card->dai_link[i].cpu_dai->ac97_control)
834 ac97 = 1;
Mark Brown435c5e22008-12-04 15:32:53 +0000835 }
836
Mark Brown6b05eda2008-12-08 19:26:48 +0000837 /* If we have AC97 in the system then don't wait for the
838 * codec. This will need revisiting if we have to handle
839 * systems with mixed AC97 and non-AC97 parts. Only check for
840 * DAIs currently; we can't do this per link since some AC97
841 * codecs have non-AC97 DAIs.
842 */
843 if (!ac97)
844 for (i = 0; i < card->num_links; i++) {
845 found = 0;
846 list_for_each_entry(dai, &dai_list, list)
847 if (card->dai_link[i].codec_dai == dai) {
848 found = 1;
849 break;
850 }
851 if (!found) {
852 dev_dbg(card->dev, "DAI %s not registered\n",
853 card->dai_link[i].codec_dai->name);
854 return;
855 }
856 }
857
Mark Brown435c5e22008-12-04 15:32:53 +0000858 /* Note that we do not current check for codec components */
859
860 dev_dbg(card->dev, "All components present, instantiating\n");
861
862 /* Found everything, bring it up */
Mark Brown87506542008-11-18 20:50:34 +0000863 if (card->probe) {
864 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200865 if (ret < 0)
Mark Brown435c5e22008-12-04 15:32:53 +0000866 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200867 }
868
Mark Brown87506542008-11-18 20:50:34 +0000869 for (i = 0; i < card->num_links; i++) {
870 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200871 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +0100872 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +0200873 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200874 goto cpu_dai_err;
875 }
876 }
877
878 if (codec_dev->probe) {
879 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200880 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200881 goto cpu_dai_err;
882 }
883
884 if (platform->probe) {
885 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200886 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200887 goto platform_err;
888 }
889
890 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +0000891 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +0100892#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +0100893 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +0000894 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +0100895#endif
Andy Green6ed25972008-06-13 16:24:05 +0100896
Mark Brown435c5e22008-12-04 15:32:53 +0000897 card->instantiated = 1;
898
899 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200900
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200901platform_err:
902 if (codec_dev->remove)
903 codec_dev->remove(pdev);
904
905cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +0100906 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +0000907 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200908 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100909 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200910 }
911
Mark Brown87506542008-11-18 20:50:34 +0000912 if (card->remove)
913 card->remove(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +0000914}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200915
Mark Brown435c5e22008-12-04 15:32:53 +0000916/*
917 * Attempt to initialise any uninitalised cards. Must be called with
918 * client_mutex.
919 */
920static void snd_soc_instantiate_cards(void)
921{
922 struct snd_soc_card *card;
923 list_for_each_entry(card, &card_list, list)
924 snd_soc_instantiate_card(card);
925}
926
927/* probes a new socdev */
928static int soc_probe(struct platform_device *pdev)
929{
930 int ret = 0;
931 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
932 struct snd_soc_card *card = socdev->card;
933
934 /* Bodge while we push things out of socdev */
935 card->socdev = socdev;
936
937 /* Bodge while we unpick instantiation */
938 card->dev = &pdev->dev;
939 ret = snd_soc_register_card(card);
940 if (ret != 0) {
941 dev_err(&pdev->dev, "Failed to register card\n");
942 return ret;
943 }
944
945 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200946}
947
948/* removes a socdev */
949static int soc_remove(struct platform_device *pdev)
950{
951 int i;
952 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000953 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000954 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200955 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
956
Mike Rapoport914dc182009-05-11 13:04:55 +0300957 if (!card->instantiated)
958 return 0;
959
Mark Brown63084192008-12-02 15:08:03 +0000960 run_delayed_work(&card->delayed_work);
Liam Girdwood965ac422007-01-31 14:14:57 +0100961
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200962 if (platform->remove)
963 platform->remove(pdev);
964
965 if (codec_dev->remove)
966 codec_dev->remove(pdev);
967
Mark Brown87506542008-11-18 20:50:34 +0000968 for (i = 0; i < card->num_links; i++) {
969 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200970 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100971 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200972 }
973
Mark Brown87506542008-11-18 20:50:34 +0000974 if (card->remove)
975 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200976
Mark Brownc5af3a22008-11-28 13:29:45 +0000977 snd_soc_unregister_card(card);
978
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200979 return 0;
980}
981
982/* ASoC platform driver */
983static struct platform_driver soc_driver = {
984 .driver = {
985 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +0200986 .owner = THIS_MODULE,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200987 },
988 .probe = soc_probe,
989 .remove = soc_remove,
990 .suspend = soc_suspend,
991 .resume = soc_resume,
992};
993
994/* create a new pcm */
995static int soc_new_pcm(struct snd_soc_device *socdev,
996 struct snd_soc_dai_link *dai_link, int num)
997{
Mark Brown87689d52008-12-02 16:01:14 +0000998 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +0000999 struct snd_soc_codec *codec = card->codec;
Mark Brown87689d52008-12-02 16:01:14 +00001000 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001001 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1002 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001003 struct snd_soc_pcm_runtime *rtd;
1004 struct snd_pcm *pcm;
1005 char new_name[64];
1006 int ret = 0, playback = 0, capture = 0;
1007
1008 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1009 if (rtd == NULL)
1010 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +01001011
1012 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001013 rtd->socdev = socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001014 codec_dai->codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001015
1016 /* check client and interface hw capabilities */
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001017 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
1018 num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001019
1020 if (codec_dai->playback.channels_min)
1021 playback = 1;
1022 if (codec_dai->capture.channels_min)
1023 capture = 1;
1024
1025 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1026 capture, &pcm);
1027 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001028 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1029 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001030 kfree(rtd);
1031 return ret;
1032 }
1033
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001034 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001035 pcm->private_data = rtd;
Mark Brown87689d52008-12-02 16:01:14 +00001036 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1037 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
1038 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1039 soc_pcm_ops.copy = platform->pcm_ops->copy;
1040 soc_pcm_ops.silence = platform->pcm_ops->silence;
1041 soc_pcm_ops.ack = platform->pcm_ops->ack;
1042 soc_pcm_ops.page = platform->pcm_ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001043
1044 if (playback)
1045 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1046
1047 if (capture)
1048 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1049
Mark Brown87689d52008-12-02 16:01:14 +00001050 ret = platform->pcm_new(codec->card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001051 if (ret < 0) {
1052 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1053 kfree(rtd);
1054 return ret;
1055 }
1056
Mark Brown87689d52008-12-02 16:01:14 +00001057 pcm->private_free = platform->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001058 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1059 cpu_dai->name);
1060 return ret;
1061}
1062
1063/* codec register dump */
Mark Brown6627a652009-01-23 22:55:23 +00001064static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001065{
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001066 int i, step = 1, count = 0;
1067
1068 if (!codec->reg_cache_size)
1069 return 0;
1070
1071 if (codec->reg_cache_step)
1072 step = codec->reg_cache_step;
1073
1074 count += sprintf(buf, "%s registers\n", codec->name);
Mark Brown58cd33c2008-07-29 11:42:25 +01001075 for (i = 0; i < codec->reg_cache_size; i += step) {
1076 count += sprintf(buf + count, "%2x: ", i);
1077 if (count >= PAGE_SIZE - 1)
1078 break;
1079
1080 if (codec->display_register)
1081 count += codec->display_register(codec, buf + count,
1082 PAGE_SIZE - count, i);
1083 else
1084 count += snprintf(buf + count, PAGE_SIZE - count,
1085 "%4x", codec->read(codec, i));
1086
1087 if (count >= PAGE_SIZE - 1)
1088 break;
1089
1090 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1091 if (count >= PAGE_SIZE - 1)
1092 break;
1093 }
1094
1095 /* Truncate count; min() would cause a warning */
1096 if (count >= PAGE_SIZE)
1097 count = PAGE_SIZE - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001098
1099 return count;
1100}
Troy Kisky12ef1932008-10-13 17:42:14 -07001101static ssize_t codec_reg_show(struct device *dev,
1102 struct device_attribute *attr, char *buf)
1103{
1104 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001105 return soc_codec_reg_show(devdata->card->codec, buf);
Troy Kisky12ef1932008-10-13 17:42:14 -07001106}
1107
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001108static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1109
Troy Kisky12ef1932008-10-13 17:42:14 -07001110#ifdef CONFIG_DEBUG_FS
1111static int codec_reg_open_file(struct inode *inode, struct file *file)
1112{
1113 file->private_data = inode->i_private;
1114 return 0;
1115}
1116
1117static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1118 size_t count, loff_t *ppos)
1119{
1120 ssize_t ret;
Mark Brown384c89e2008-12-03 17:34:03 +00001121 struct snd_soc_codec *codec = file->private_data;
Troy Kisky12ef1932008-10-13 17:42:14 -07001122 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1123 if (!buf)
1124 return -ENOMEM;
Mark Brown6627a652009-01-23 22:55:23 +00001125 ret = soc_codec_reg_show(codec, buf);
Troy Kisky12ef1932008-10-13 17:42:14 -07001126 if (ret >= 0)
1127 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1128 kfree(buf);
1129 return ret;
1130}
1131
1132static ssize_t codec_reg_write_file(struct file *file,
1133 const char __user *user_buf, size_t count, loff_t *ppos)
1134{
1135 char buf[32];
1136 int buf_size;
1137 char *start = buf;
1138 unsigned long reg, value;
1139 int step = 1;
Mark Brown384c89e2008-12-03 17:34:03 +00001140 struct snd_soc_codec *codec = file->private_data;
Troy Kisky12ef1932008-10-13 17:42:14 -07001141
1142 buf_size = min(count, (sizeof(buf)-1));
1143 if (copy_from_user(buf, user_buf, buf_size))
1144 return -EFAULT;
1145 buf[buf_size] = 0;
1146
1147 if (codec->reg_cache_step)
1148 step = codec->reg_cache_step;
1149
1150 while (*start == ' ')
1151 start++;
1152 reg = simple_strtoul(start, &start, 16);
1153 if ((reg >= codec->reg_cache_size) || (reg % step))
1154 return -EINVAL;
1155 while (*start == ' ')
1156 start++;
1157 if (strict_strtoul(start, 16, &value))
1158 return -EINVAL;
1159 codec->write(codec, reg, value);
1160 return buf_size;
1161}
1162
1163static const struct file_operations codec_reg_fops = {
1164 .open = codec_reg_open_file,
1165 .read = codec_reg_read_file,
1166 .write = codec_reg_write_file,
1167};
1168
Mark Brown384c89e2008-12-03 17:34:03 +00001169static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001170{
Mark Brown384c89e2008-12-03 17:34:03 +00001171 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
1172 debugfs_root, codec,
1173 &codec_reg_fops);
1174 if (!codec->debugfs_reg)
1175 printk(KERN_WARNING
1176 "ASoC: Failed to create codec register debugfs file\n");
Troy Kisky12ef1932008-10-13 17:42:14 -07001177
Mark Brown384c89e2008-12-03 17:34:03 +00001178 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
1179 debugfs_root,
1180 &codec->pop_time);
1181 if (!codec->debugfs_pop_time)
1182 printk(KERN_WARNING
1183 "Failed to create pop time debugfs file\n");
Troy Kisky12ef1932008-10-13 17:42:14 -07001184}
1185
Mark Brown384c89e2008-12-03 17:34:03 +00001186static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001187{
Mark Brown384c89e2008-12-03 17:34:03 +00001188 debugfs_remove(codec->debugfs_pop_time);
1189 debugfs_remove(codec->debugfs_reg);
Troy Kisky12ef1932008-10-13 17:42:14 -07001190}
1191
1192#else
1193
Mark Brown384c89e2008-12-03 17:34:03 +00001194static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001195{
1196}
1197
Mark Brown384c89e2008-12-03 17:34:03 +00001198static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001199{
1200}
1201#endif
1202
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001203/**
1204 * snd_soc_new_ac97_codec - initailise AC97 device
1205 * @codec: audio codec
1206 * @ops: AC97 bus operations
1207 * @num: AC97 codec number
1208 *
1209 * Initialises AC97 codec resources for use by ad-hoc devices only.
1210 */
1211int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1212 struct snd_ac97_bus_ops *ops, int num)
1213{
1214 mutex_lock(&codec->mutex);
1215
1216 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1217 if (codec->ac97 == NULL) {
1218 mutex_unlock(&codec->mutex);
1219 return -ENOMEM;
1220 }
1221
1222 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1223 if (codec->ac97->bus == NULL) {
1224 kfree(codec->ac97);
1225 codec->ac97 = NULL;
1226 mutex_unlock(&codec->mutex);
1227 return -ENOMEM;
1228 }
1229
1230 codec->ac97->bus->ops = ops;
1231 codec->ac97->num = num;
1232 mutex_unlock(&codec->mutex);
1233 return 0;
1234}
1235EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1236
1237/**
1238 * snd_soc_free_ac97_codec - free AC97 codec device
1239 * @codec: audio codec
1240 *
1241 * Frees AC97 codec device resources.
1242 */
1243void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1244{
1245 mutex_lock(&codec->mutex);
1246 kfree(codec->ac97->bus);
1247 kfree(codec->ac97);
1248 codec->ac97 = NULL;
1249 mutex_unlock(&codec->mutex);
1250}
1251EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1252
1253/**
1254 * snd_soc_update_bits - update codec register bits
1255 * @codec: audio codec
1256 * @reg: codec register
1257 * @mask: register mask
1258 * @value: new value
1259 *
1260 * Writes new register value.
1261 *
1262 * Returns 1 for change else 0.
1263 */
1264int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1265 unsigned short mask, unsigned short value)
1266{
1267 int change;
1268 unsigned short old, new;
1269
1270 mutex_lock(&io_mutex);
1271 old = snd_soc_read(codec, reg);
1272 new = (old & ~mask) | value;
1273 change = old != new;
1274 if (change)
1275 snd_soc_write(codec, reg, new);
1276
1277 mutex_unlock(&io_mutex);
1278 return change;
1279}
1280EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1281
1282/**
1283 * snd_soc_test_bits - test register for change
1284 * @codec: audio codec
1285 * @reg: codec register
1286 * @mask: register mask
1287 * @value: new value
1288 *
1289 * Tests a register with a new value and checks if the new value is
1290 * different from the old value.
1291 *
1292 * Returns 1 for change else 0.
1293 */
1294int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1295 unsigned short mask, unsigned short value)
1296{
1297 int change;
1298 unsigned short old, new;
1299
1300 mutex_lock(&io_mutex);
1301 old = snd_soc_read(codec, reg);
1302 new = (old & ~mask) | value;
1303 change = old != new;
1304 mutex_unlock(&io_mutex);
1305
1306 return change;
1307}
1308EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1309
1310/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001311 * snd_soc_new_pcms - create new sound card and pcms
1312 * @socdev: the SoC audio device
Mark Brownac11a2b2009-01-01 12:18:17 +00001313 * @idx: ALSA card index
1314 * @xid: card identification
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001315 *
1316 * Create a new sound card based upon the codec and interface pcms.
1317 *
1318 * Returns 0 for success, else error.
1319 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001320int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001321{
Mark Brown87506542008-11-18 20:50:34 +00001322 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001323 struct snd_soc_codec *codec = card->codec;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001324 int ret, i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001325
1326 mutex_lock(&codec->mutex);
1327
1328 /* register a sound card */
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001329 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1330 if (ret < 0) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001331 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1332 codec->name);
1333 mutex_unlock(&codec->mutex);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001334 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001335 }
1336
1337 codec->card->dev = socdev->dev;
1338 codec->card->private_data = codec;
1339 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1340
1341 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001342 for (i = 0; i < card->num_links; i++) {
1343 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001344 if (ret < 0) {
1345 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001346 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001347 mutex_unlock(&codec->mutex);
1348 return ret;
1349 }
1350 }
1351
1352 mutex_unlock(&codec->mutex);
1353 return ret;
1354}
1355EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1356
1357/**
Mark Brown968a6022008-11-28 11:49:07 +00001358 * snd_soc_init_card - register sound card
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001359 * @socdev: the SoC audio device
1360 *
1361 * Register a SoC sound card. Also registers an AC97 device if the
1362 * codec is AC97 for ad hoc devices.
1363 *
1364 * Returns 0 for success, else error.
1365 */
Mark Brown968a6022008-11-28 11:49:07 +00001366int snd_soc_init_card(struct snd_soc_device *socdev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001367{
Mark Brown87506542008-11-18 20:50:34 +00001368 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001369 struct snd_soc_codec *codec = card->codec;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001370 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001371
Mark Brown87506542008-11-18 20:50:34 +00001372 for (i = 0; i < card->num_links; i++) {
1373 if (card->dai_link[i].init) {
1374 err = card->dai_link[i].init(codec);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001375 if (err < 0) {
1376 printk(KERN_ERR "asoc: failed to init %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001377 card->dai_link[i].stream_name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001378 continue;
1379 }
1380 }
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001381 if (card->dai_link[i].codec_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001382 ac97 = 1;
1383 }
1384 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
Mark Brown87506542008-11-18 20:50:34 +00001385 "%s", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001386 snprintf(codec->card->longname, sizeof(codec->card->longname),
Mark Brown87506542008-11-18 20:50:34 +00001387 "%s (%s)", card->name, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001388
1389 ret = snd_card_register(codec->card);
1390 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001391 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001392 codec->name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001393 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001394 }
1395
Mark Brown08c8efe2008-01-21 14:33:37 +01001396 mutex_lock(&codec->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001397#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown14fa43f2009-02-12 19:33:19 +00001398 /* Only instantiate AC97 if not already done by the adaptor
1399 * for the generic AC97 subsystem.
1400 */
1401 if (ac97 && strcmp(codec->name, "AC97") != 0) {
Liam Girdwood12e74f72006-10-16 21:19:48 +02001402 ret = soc_ac97_dev_register(codec);
1403 if (ret < 0) {
1404 printk(KERN_ERR "asoc: AC97 device register failed\n");
1405 snd_card_free(codec->card);
Mark Brown08c8efe2008-01-21 14:33:37 +01001406 mutex_unlock(&codec->mutex);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001407 goto out;
1408 }
1409 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001410#endif
1411
Liam Girdwood12e74f72006-10-16 21:19:48 +02001412 err = snd_soc_dapm_sys_add(socdev->dev);
1413 if (err < 0)
1414 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1415
1416 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1417 if (err < 0)
Mark Brown3ff3f642008-05-19 12:32:25 +02001418 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
Mark Brown08c8efe2008-01-21 14:33:37 +01001419
Mark Brown6627a652009-01-23 22:55:23 +00001420 soc_init_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001421 mutex_unlock(&codec->mutex);
Mark Brown08c8efe2008-01-21 14:33:37 +01001422
1423out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001424 return ret;
1425}
Mark Brown968a6022008-11-28 11:49:07 +00001426EXPORT_SYMBOL_GPL(snd_soc_init_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001427
1428/**
1429 * snd_soc_free_pcms - free sound card and pcms
1430 * @socdev: the SoC audio device
1431 *
1432 * Frees sound card and pcms associated with the socdev.
1433 * Also unregister the codec if it is an AC97 device.
1434 */
1435void snd_soc_free_pcms(struct snd_soc_device *socdev)
1436{
Mark Brown6627a652009-01-23 22:55:23 +00001437 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001438#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001439 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001440 int i;
1441#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001442
1443 mutex_lock(&codec->mutex);
Mark Brown6627a652009-01-23 22:55:23 +00001444 soc_cleanup_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001445#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001446 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001447 codec_dai = &codec->dai[i];
Atsushi Nemotod2314e02009-03-16 23:26:20 +09001448 if (codec_dai->ac97_control && codec->ac97 &&
1449 strcmp(codec->name, "AC97") != 0) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001450 soc_ac97_dev_unregister(codec);
1451 goto free_card;
1452 }
1453 }
1454free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001455#endif
1456
1457 if (codec->card)
1458 snd_card_free(codec->card);
1459 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1460 mutex_unlock(&codec->mutex);
1461}
1462EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1463
1464/**
1465 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1466 * @substream: the pcm substream
1467 * @hw: the hardware parameters
1468 *
1469 * Sets the substream runtime hardware parameters.
1470 */
1471int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1472 const struct snd_pcm_hardware *hw)
1473{
1474 struct snd_pcm_runtime *runtime = substream->runtime;
1475 runtime->hw.info = hw->info;
1476 runtime->hw.formats = hw->formats;
1477 runtime->hw.period_bytes_min = hw->period_bytes_min;
1478 runtime->hw.period_bytes_max = hw->period_bytes_max;
1479 runtime->hw.periods_min = hw->periods_min;
1480 runtime->hw.periods_max = hw->periods_max;
1481 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1482 runtime->hw.fifo_size = hw->fifo_size;
1483 return 0;
1484}
1485EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1486
1487/**
1488 * snd_soc_cnew - create new control
1489 * @_template: control template
1490 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001491 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001492 *
1493 * Create a new mixer control from a template control.
1494 *
1495 * Returns 0 for success, else error.
1496 */
1497struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1498 void *data, char *long_name)
1499{
1500 struct snd_kcontrol_new template;
1501
1502 memcpy(&template, _template, sizeof(template));
1503 if (long_name)
1504 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001505 template.index = 0;
1506
1507 return snd_ctl_new1(&template, data);
1508}
1509EXPORT_SYMBOL_GPL(snd_soc_cnew);
1510
1511/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001512 * snd_soc_add_controls - add an array of controls to a codec.
1513 * Convienience function to add a list of controls. Many codecs were
1514 * duplicating this code.
1515 *
1516 * @codec: codec to add controls to
1517 * @controls: array of controls to add
1518 * @num_controls: number of elements in the array
1519 *
1520 * Return 0 for success, else error.
1521 */
1522int snd_soc_add_controls(struct snd_soc_codec *codec,
1523 const struct snd_kcontrol_new *controls, int num_controls)
1524{
1525 struct snd_card *card = codec->card;
1526 int err, i;
1527
1528 for (i = 0; i < num_controls; i++) {
1529 const struct snd_kcontrol_new *control = &controls[i];
1530 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1531 if (err < 0) {
1532 dev_err(codec->dev, "%s: Failed to add %s\n",
1533 codec->name, control->name);
1534 return err;
1535 }
1536 }
1537
1538 return 0;
1539}
1540EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1541
1542/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001543 * snd_soc_info_enum_double - enumerated double mixer info callback
1544 * @kcontrol: mixer control
1545 * @uinfo: control element information
1546 *
1547 * Callback to provide information about a double enumerated
1548 * mixer control.
1549 *
1550 * Returns 0 for success.
1551 */
1552int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1553 struct snd_ctl_elem_info *uinfo)
1554{
1555 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1556
1557 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1558 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001559 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001560
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001561 if (uinfo->value.enumerated.item > e->max - 1)
1562 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001563 strcpy(uinfo->value.enumerated.name,
1564 e->texts[uinfo->value.enumerated.item]);
1565 return 0;
1566}
1567EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1568
1569/**
1570 * snd_soc_get_enum_double - enumerated double mixer get callback
1571 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001572 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001573 *
1574 * Callback to get the value of a double enumerated mixer.
1575 *
1576 * Returns 0 for success.
1577 */
1578int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1579 struct snd_ctl_elem_value *ucontrol)
1580{
1581 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1582 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1583 unsigned short val, bitmask;
1584
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001585 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001586 ;
1587 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001588 ucontrol->value.enumerated.item[0]
1589 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001590 if (e->shift_l != e->shift_r)
1591 ucontrol->value.enumerated.item[1] =
1592 (val >> e->shift_r) & (bitmask - 1);
1593
1594 return 0;
1595}
1596EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1597
1598/**
1599 * snd_soc_put_enum_double - enumerated double mixer put callback
1600 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001601 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001602 *
1603 * Callback to set the value of a double enumerated mixer.
1604 *
1605 * Returns 0 for success.
1606 */
1607int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1608 struct snd_ctl_elem_value *ucontrol)
1609{
1610 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1611 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1612 unsigned short val;
1613 unsigned short mask, bitmask;
1614
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001615 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001616 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001617 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001618 return -EINVAL;
1619 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1620 mask = (bitmask - 1) << e->shift_l;
1621 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001622 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001623 return -EINVAL;
1624 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1625 mask |= (bitmask - 1) << e->shift_r;
1626 }
1627
1628 return snd_soc_update_bits(codec, e->reg, mask, val);
1629}
1630EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1631
1632/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001633 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1634 * @kcontrol: mixer control
1635 * @ucontrol: control element information
1636 *
1637 * Callback to get the value of a double semi enumerated mixer.
1638 *
1639 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1640 * used for handling bitfield coded enumeration for example.
1641 *
1642 * Returns 0 for success.
1643 */
1644int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1645 struct snd_ctl_elem_value *ucontrol)
1646{
1647 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001648 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001649 unsigned short reg_val, val, mux;
1650
1651 reg_val = snd_soc_read(codec, e->reg);
1652 val = (reg_val >> e->shift_l) & e->mask;
1653 for (mux = 0; mux < e->max; mux++) {
1654 if (val == e->values[mux])
1655 break;
1656 }
1657 ucontrol->value.enumerated.item[0] = mux;
1658 if (e->shift_l != e->shift_r) {
1659 val = (reg_val >> e->shift_r) & e->mask;
1660 for (mux = 0; mux < e->max; mux++) {
1661 if (val == e->values[mux])
1662 break;
1663 }
1664 ucontrol->value.enumerated.item[1] = mux;
1665 }
1666
1667 return 0;
1668}
1669EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1670
1671/**
1672 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1673 * @kcontrol: mixer control
1674 * @ucontrol: control element information
1675 *
1676 * Callback to set the value of a double semi enumerated mixer.
1677 *
1678 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1679 * used for handling bitfield coded enumeration for example.
1680 *
1681 * Returns 0 for success.
1682 */
1683int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1684 struct snd_ctl_elem_value *ucontrol)
1685{
1686 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001687 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001688 unsigned short val;
1689 unsigned short mask;
1690
1691 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1692 return -EINVAL;
1693 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1694 mask = e->mask << e->shift_l;
1695 if (e->shift_l != e->shift_r) {
1696 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1697 return -EINVAL;
1698 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1699 mask |= e->mask << e->shift_r;
1700 }
1701
1702 return snd_soc_update_bits(codec, e->reg, mask, val);
1703}
1704EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1705
1706/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001707 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1708 * @kcontrol: mixer control
1709 * @uinfo: control element information
1710 *
1711 * Callback to provide information about an external enumerated
1712 * single mixer.
1713 *
1714 * Returns 0 for success.
1715 */
1716int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1717 struct snd_ctl_elem_info *uinfo)
1718{
1719 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1720
1721 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1722 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001723 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001724
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001725 if (uinfo->value.enumerated.item > e->max - 1)
1726 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001727 strcpy(uinfo->value.enumerated.name,
1728 e->texts[uinfo->value.enumerated.item]);
1729 return 0;
1730}
1731EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1732
1733/**
1734 * snd_soc_info_volsw_ext - external single mixer info callback
1735 * @kcontrol: mixer control
1736 * @uinfo: control element information
1737 *
1738 * Callback to provide information about a single external mixer control.
1739 *
1740 * Returns 0 for success.
1741 */
1742int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1743 struct snd_ctl_elem_info *uinfo)
1744{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001745 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001746
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001747 if (max == 1)
1748 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1749 else
1750 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1751
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001752 uinfo->count = 1;
1753 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001754 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001755 return 0;
1756}
1757EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1758
1759/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001760 * snd_soc_info_volsw - single mixer info callback
1761 * @kcontrol: mixer control
1762 * @uinfo: control element information
1763 *
1764 * Callback to provide information about a single mixer control.
1765 *
1766 * Returns 0 for success.
1767 */
1768int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1769 struct snd_ctl_elem_info *uinfo)
1770{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001771 struct soc_mixer_control *mc =
1772 (struct soc_mixer_control *)kcontrol->private_value;
1773 int max = mc->max;
Mark Brown762b8df2008-10-30 12:37:08 +00001774 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001775 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001776
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001777 if (max == 1)
1778 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1779 else
1780 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1781
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001782 uinfo->count = shift == rshift ? 1 : 2;
1783 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001784 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001785 return 0;
1786}
1787EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1788
1789/**
1790 * snd_soc_get_volsw - single mixer get callback
1791 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001792 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001793 *
1794 * Callback to get the value of a single mixer control.
1795 *
1796 * Returns 0 for success.
1797 */
1798int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1799 struct snd_ctl_elem_value *ucontrol)
1800{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001801 struct soc_mixer_control *mc =
1802 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001803 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001804 unsigned int reg = mc->reg;
1805 unsigned int shift = mc->shift;
1806 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001807 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001808 unsigned int mask = (1 << fls(max)) - 1;
1809 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001810
1811 ucontrol->value.integer.value[0] =
1812 (snd_soc_read(codec, reg) >> shift) & mask;
1813 if (shift != rshift)
1814 ucontrol->value.integer.value[1] =
1815 (snd_soc_read(codec, reg) >> rshift) & mask;
1816 if (invert) {
1817 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001818 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001819 if (shift != rshift)
1820 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001821 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001822 }
1823
1824 return 0;
1825}
1826EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1827
1828/**
1829 * snd_soc_put_volsw - single mixer put callback
1830 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001831 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001832 *
1833 * Callback to set the value of a single mixer control.
1834 *
1835 * Returns 0 for success.
1836 */
1837int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1838 struct snd_ctl_elem_value *ucontrol)
1839{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001840 struct soc_mixer_control *mc =
1841 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001842 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001843 unsigned int reg = mc->reg;
1844 unsigned int shift = mc->shift;
1845 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001846 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001847 unsigned int mask = (1 << fls(max)) - 1;
1848 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001849 unsigned short val, val2, val_mask;
1850
1851 val = (ucontrol->value.integer.value[0] & mask);
1852 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001853 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001854 val_mask = mask << shift;
1855 val = val << shift;
1856 if (shift != rshift) {
1857 val2 = (ucontrol->value.integer.value[1] & mask);
1858 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001859 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001860 val_mask |= mask << rshift;
1861 val |= val2 << rshift;
1862 }
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001863 return snd_soc_update_bits(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001864}
1865EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1866
1867/**
1868 * snd_soc_info_volsw_2r - double mixer info callback
1869 * @kcontrol: mixer control
1870 * @uinfo: control element information
1871 *
1872 * Callback to provide information about a double mixer control that
1873 * spans 2 codec registers.
1874 *
1875 * Returns 0 for success.
1876 */
1877int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1878 struct snd_ctl_elem_info *uinfo)
1879{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001880 struct soc_mixer_control *mc =
1881 (struct soc_mixer_control *)kcontrol->private_value;
1882 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001883
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001884 if (max == 1)
1885 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1886 else
1887 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1888
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001889 uinfo->count = 2;
1890 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001891 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001892 return 0;
1893}
1894EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1895
1896/**
1897 * snd_soc_get_volsw_2r - double mixer get callback
1898 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001899 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001900 *
1901 * Callback to get the value of a double mixer control that spans 2 registers.
1902 *
1903 * Returns 0 for success.
1904 */
1905int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1906 struct snd_ctl_elem_value *ucontrol)
1907{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001908 struct soc_mixer_control *mc =
1909 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001910 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001911 unsigned int reg = mc->reg;
1912 unsigned int reg2 = mc->rreg;
1913 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001914 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001915 unsigned int mask = (1<<fls(max))-1;
1916 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001917
1918 ucontrol->value.integer.value[0] =
1919 (snd_soc_read(codec, reg) >> shift) & mask;
1920 ucontrol->value.integer.value[1] =
1921 (snd_soc_read(codec, reg2) >> shift) & mask;
1922 if (invert) {
1923 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001924 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001925 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001926 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001927 }
1928
1929 return 0;
1930}
1931EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1932
1933/**
1934 * snd_soc_put_volsw_2r - double mixer set callback
1935 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001936 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001937 *
1938 * Callback to set the value of a double mixer control that spans 2 registers.
1939 *
1940 * Returns 0 for success.
1941 */
1942int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1943 struct snd_ctl_elem_value *ucontrol)
1944{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001945 struct soc_mixer_control *mc =
1946 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001947 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001948 unsigned int reg = mc->reg;
1949 unsigned int reg2 = mc->rreg;
1950 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001951 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001952 unsigned int mask = (1 << fls(max)) - 1;
1953 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001954 int err;
1955 unsigned short val, val2, val_mask;
1956
1957 val_mask = mask << shift;
1958 val = (ucontrol->value.integer.value[0] & mask);
1959 val2 = (ucontrol->value.integer.value[1] & mask);
1960
1961 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001962 val = max - val;
1963 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001964 }
1965
1966 val = val << shift;
1967 val2 = val2 << shift;
1968
Mark Brown3ff3f642008-05-19 12:32:25 +02001969 err = snd_soc_update_bits(codec, reg, val_mask, val);
1970 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001971 return err;
1972
1973 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1974 return err;
1975}
1976EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1977
Mark Browne13ac2e2008-05-28 17:58:05 +01001978/**
1979 * snd_soc_info_volsw_s8 - signed mixer info callback
1980 * @kcontrol: mixer control
1981 * @uinfo: control element information
1982 *
1983 * Callback to provide information about a signed mixer control.
1984 *
1985 * Returns 0 for success.
1986 */
1987int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1988 struct snd_ctl_elem_info *uinfo)
1989{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001990 struct soc_mixer_control *mc =
1991 (struct soc_mixer_control *)kcontrol->private_value;
1992 int max = mc->max;
1993 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001994
1995 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1996 uinfo->count = 2;
1997 uinfo->value.integer.min = 0;
1998 uinfo->value.integer.max = max-min;
1999 return 0;
2000}
2001EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2002
2003/**
2004 * snd_soc_get_volsw_s8 - signed mixer get callback
2005 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002006 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002007 *
2008 * Callback to get the value of a signed mixer control.
2009 *
2010 * Returns 0 for success.
2011 */
2012int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2013 struct snd_ctl_elem_value *ucontrol)
2014{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002015 struct soc_mixer_control *mc =
2016 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002017 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002018 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002019 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002020 int val = snd_soc_read(codec, reg);
2021
2022 ucontrol->value.integer.value[0] =
2023 ((signed char)(val & 0xff))-min;
2024 ucontrol->value.integer.value[1] =
2025 ((signed char)((val >> 8) & 0xff))-min;
2026 return 0;
2027}
2028EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2029
2030/**
2031 * snd_soc_put_volsw_sgn - signed mixer put callback
2032 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002033 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002034 *
2035 * Callback to set the value of a signed mixer control.
2036 *
2037 * Returns 0 for success.
2038 */
2039int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2040 struct snd_ctl_elem_value *ucontrol)
2041{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002042 struct soc_mixer_control *mc =
2043 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002044 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002045 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002046 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002047 unsigned short val;
2048
2049 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2050 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2051
2052 return snd_soc_update_bits(codec, reg, 0xffff, val);
2053}
2054EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2055
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002056/**
2057 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2058 * @dai: DAI
2059 * @clk_id: DAI specific clock ID
2060 * @freq: new clock frequency in Hz
2061 * @dir: new clock direction - input/output.
2062 *
2063 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2064 */
2065int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2066 unsigned int freq, int dir)
2067{
Eric Miao6335d052009-03-03 09:41:00 +08002068 if (dai->ops->set_sysclk)
2069 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002070 else
2071 return -EINVAL;
2072}
2073EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2074
2075/**
2076 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2077 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002078 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002079 * @div: new clock divisor.
2080 *
2081 * Configures the clock dividers. This is used to derive the best DAI bit and
2082 * frame clocks from the system or master clock. It's best to set the DAI bit
2083 * and frame clocks as low as possible to save system power.
2084 */
2085int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2086 int div_id, int div)
2087{
Eric Miao6335d052009-03-03 09:41:00 +08002088 if (dai->ops->set_clkdiv)
2089 return dai->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002090 else
2091 return -EINVAL;
2092}
2093EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2094
2095/**
2096 * snd_soc_dai_set_pll - configure DAI PLL.
2097 * @dai: DAI
2098 * @pll_id: DAI specific PLL ID
2099 * @freq_in: PLL input clock frequency in Hz
2100 * @freq_out: requested PLL output clock frequency in Hz
2101 *
2102 * Configures and enables PLL to generate output clock based on input clock.
2103 */
2104int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
2105 int pll_id, unsigned int freq_in, unsigned int freq_out)
2106{
Eric Miao6335d052009-03-03 09:41:00 +08002107 if (dai->ops->set_pll)
2108 return dai->ops->set_pll(dai, pll_id, freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002109 else
2110 return -EINVAL;
2111}
2112EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2113
2114/**
2115 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2116 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002117 * @fmt: SND_SOC_DAIFMT_ format value.
2118 *
2119 * Configures the DAI hardware format and clocking.
2120 */
2121int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2122{
Eric Miao6335d052009-03-03 09:41:00 +08002123 if (dai->ops->set_fmt)
2124 return dai->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002125 else
2126 return -EINVAL;
2127}
2128EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2129
2130/**
2131 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2132 * @dai: DAI
2133 * @mask: DAI specific mask representing used slots.
2134 * @slots: Number of slots in use.
2135 *
2136 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2137 * specific.
2138 */
2139int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2140 unsigned int mask, int slots)
2141{
Eric Miao6335d052009-03-03 09:41:00 +08002142 if (dai->ops->set_sysclk)
2143 return dai->ops->set_tdm_slot(dai, mask, slots);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002144 else
2145 return -EINVAL;
2146}
2147EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2148
2149/**
2150 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2151 * @dai: DAI
2152 * @tristate: tristate enable
2153 *
2154 * Tristates the DAI so that others can use it.
2155 */
2156int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2157{
Eric Miao6335d052009-03-03 09:41:00 +08002158 if (dai->ops->set_sysclk)
2159 return dai->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002160 else
2161 return -EINVAL;
2162}
2163EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2164
2165/**
2166 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2167 * @dai: DAI
2168 * @mute: mute enable
2169 *
2170 * Mutes the DAI DAC.
2171 */
2172int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2173{
Eric Miao6335d052009-03-03 09:41:00 +08002174 if (dai->ops->digital_mute)
2175 return dai->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002176 else
2177 return -EINVAL;
2178}
2179EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2180
Mark Brownc5af3a22008-11-28 13:29:45 +00002181/**
2182 * snd_soc_register_card - Register a card with the ASoC core
2183 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002184 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002185 *
2186 * Note that currently this is an internal only function: it will be
2187 * exposed to machine drivers after further backporting of ASoC v2
2188 * registration APIs.
2189 */
2190static int snd_soc_register_card(struct snd_soc_card *card)
2191{
2192 if (!card->name || !card->dev)
2193 return -EINVAL;
2194
2195 INIT_LIST_HEAD(&card->list);
2196 card->instantiated = 0;
2197
2198 mutex_lock(&client_mutex);
2199 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002200 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002201 mutex_unlock(&client_mutex);
2202
2203 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2204
2205 return 0;
2206}
2207
2208/**
2209 * snd_soc_unregister_card - Unregister a card with the ASoC core
2210 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002211 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002212 *
2213 * Note that currently this is an internal only function: it will be
2214 * exposed to machine drivers after further backporting of ASoC v2
2215 * registration APIs.
2216 */
2217static int snd_soc_unregister_card(struct snd_soc_card *card)
2218{
2219 mutex_lock(&client_mutex);
2220 list_del(&card->list);
2221 mutex_unlock(&client_mutex);
2222
2223 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2224
2225 return 0;
2226}
2227
Eric Miao6335d052009-03-03 09:41:00 +08002228static struct snd_soc_dai_ops null_dai_ops = {
2229};
2230
Mark Brown91151712008-11-30 23:31:24 +00002231/**
2232 * snd_soc_register_dai - Register a DAI with the ASoC core
2233 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002234 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002235 */
2236int snd_soc_register_dai(struct snd_soc_dai *dai)
2237{
2238 if (!dai->name)
2239 return -EINVAL;
2240
2241 /* The device should become mandatory over time */
2242 if (!dai->dev)
2243 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2244
Eric Miao6335d052009-03-03 09:41:00 +08002245 if (!dai->ops)
2246 dai->ops = &null_dai_ops;
2247
Mark Brown91151712008-11-30 23:31:24 +00002248 INIT_LIST_HEAD(&dai->list);
2249
2250 mutex_lock(&client_mutex);
2251 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002252 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002253 mutex_unlock(&client_mutex);
2254
2255 pr_debug("Registered DAI '%s'\n", dai->name);
2256
2257 return 0;
2258}
2259EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2260
2261/**
2262 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2263 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002264 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002265 */
2266void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2267{
2268 mutex_lock(&client_mutex);
2269 list_del(&dai->list);
2270 mutex_unlock(&client_mutex);
2271
2272 pr_debug("Unregistered DAI '%s'\n", dai->name);
2273}
2274EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2275
2276/**
2277 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2278 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002279 * @dai: Array of DAIs to register
2280 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002281 */
2282int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2283{
2284 int i, ret;
2285
2286 for (i = 0; i < count; i++) {
2287 ret = snd_soc_register_dai(&dai[i]);
2288 if (ret != 0)
2289 goto err;
2290 }
2291
2292 return 0;
2293
2294err:
2295 for (i--; i >= 0; i--)
2296 snd_soc_unregister_dai(&dai[i]);
2297
2298 return ret;
2299}
2300EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2301
2302/**
2303 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2304 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002305 * @dai: Array of DAIs to unregister
2306 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002307 */
2308void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2309{
2310 int i;
2311
2312 for (i = 0; i < count; i++)
2313 snd_soc_unregister_dai(&dai[i]);
2314}
2315EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2316
Mark Brown12a48a8c2008-12-03 19:40:30 +00002317/**
2318 * snd_soc_register_platform - Register a platform with the ASoC core
2319 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002320 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00002321 */
2322int snd_soc_register_platform(struct snd_soc_platform *platform)
2323{
2324 if (!platform->name)
2325 return -EINVAL;
2326
2327 INIT_LIST_HEAD(&platform->list);
2328
2329 mutex_lock(&client_mutex);
2330 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002331 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00002332 mutex_unlock(&client_mutex);
2333
2334 pr_debug("Registered platform '%s'\n", platform->name);
2335
2336 return 0;
2337}
2338EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2339
2340/**
2341 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2342 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002343 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002344 */
2345void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2346{
2347 mutex_lock(&client_mutex);
2348 list_del(&platform->list);
2349 mutex_unlock(&client_mutex);
2350
2351 pr_debug("Unregistered platform '%s'\n", platform->name);
2352}
2353EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2354
Mark Brown0d0cf002008-12-10 14:32:45 +00002355/**
2356 * snd_soc_register_codec - Register a codec with the ASoC core
2357 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002358 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00002359 */
2360int snd_soc_register_codec(struct snd_soc_codec *codec)
2361{
2362 if (!codec->name)
2363 return -EINVAL;
2364
2365 /* The device should become mandatory over time */
2366 if (!codec->dev)
2367 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2368
2369 INIT_LIST_HEAD(&codec->list);
2370
2371 mutex_lock(&client_mutex);
2372 list_add(&codec->list, &codec_list);
2373 snd_soc_instantiate_cards();
2374 mutex_unlock(&client_mutex);
2375
2376 pr_debug("Registered codec '%s'\n", codec->name);
2377
2378 return 0;
2379}
2380EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2381
2382/**
2383 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2384 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002385 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00002386 */
2387void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2388{
2389 mutex_lock(&client_mutex);
2390 list_del(&codec->list);
2391 mutex_unlock(&client_mutex);
2392
2393 pr_debug("Unregistered codec '%s'\n", codec->name);
2394}
2395EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2396
Takashi Iwaic9b3a402008-12-10 07:47:22 +01002397static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002398{
Mark Brown384c89e2008-12-03 17:34:03 +00002399#ifdef CONFIG_DEBUG_FS
2400 debugfs_root = debugfs_create_dir("asoc", NULL);
2401 if (IS_ERR(debugfs_root) || !debugfs_root) {
2402 printk(KERN_WARNING
2403 "ASoC: Failed to create debugfs directory\n");
2404 debugfs_root = NULL;
2405 }
2406#endif
2407
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002408 return platform_driver_register(&soc_driver);
2409}
2410
Mark Brown7d8c16a2008-11-30 22:11:24 +00002411static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002412{
Mark Brown384c89e2008-12-03 17:34:03 +00002413#ifdef CONFIG_DEBUG_FS
2414 debugfs_remove_recursive(debugfs_root);
2415#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02002416 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002417}
2418
2419module_init(snd_soc_init);
2420module_exit(snd_soc_exit);
2421
2422/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002423MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002424MODULE_DESCRIPTION("ALSA SoC Core");
2425MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02002426MODULE_ALIAS("platform:soc-audio");