blob: d4b90d82a098cdf98d939f1b409d7b1b2ace087c [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;
101 codec->ac97->dev.parent = NULL;
102 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 */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100136 if (cpu_dai->ops.startup) {
Mark Browndee89c42008-11-18 22:11:38 +0000137 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
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100153 if (codec_dai->ops.startup) {
Mark Browndee89c42008-11-18 22:11:38 +0000154 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:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100250 if (cpu_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000251 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
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100343 if (cpu_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000344 cpu_dai->ops.shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200345
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100346 if (codec_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000347 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
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100411 if (codec_dai->ops.prepare) {
Mark Browndee89c42008-11-18 22:11:38 +0000412 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
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100419 if (cpu_dai->ops.prepare) {
Mark Browndee89c42008-11-18 22:11:38 +0000420 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
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100497 if (codec_dai->ops.hw_params) {
Mark Browndee89c42008-11-18 22:11:38 +0000498 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
506 if (cpu_dai->ops.hw_params) {
Mark Browndee89c42008-11-18 22:11:38 +0000507 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:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100529 if (cpu_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000530 cpu_dai->ops.hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531
532interface_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100533 if (codec_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000534 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 */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100573 if (codec_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000574 codec_dai->ops.hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200575
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100576 if (cpu_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000577 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
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100594 if (codec_dai->ops.trigger) {
Mark Browndee89c42008-11-18 22:11:38 +0000595 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
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100606 if (cpu_dai->ops.trigger) {
Mark Browndee89c42008-11-18 22:11:38 +0000607 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;
648 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;
744 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;
Andy Green6ed25972008-06-13 16:24:05 +0100770
Mark Brownfde22f22008-11-24 18:08:18 +0000771 dev_dbg(socdev->dev, "scheduling resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100772
Mark Brown63084192008-12-02 15:08:03 +0000773 if (!schedule_work(&card->deferred_resume_work))
Mark Brownfde22f22008-11-24 18:08:18 +0000774 dev_err(socdev->dev, "resume work item may be lost\n");
Andy Green6ed25972008-06-13 16:24:05 +0100775
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200776 return 0;
777}
778
779#else
780#define soc_suspend NULL
781#define soc_resume NULL
782#endif
783
Mark Brown435c5e22008-12-04 15:32:53 +0000784static void snd_soc_instantiate_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200785{
Mark Brown435c5e22008-12-04 15:32:53 +0000786 struct platform_device *pdev = container_of(card->dev,
787 struct platform_device,
788 dev);
789 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
790 struct snd_soc_platform *platform;
791 struct snd_soc_dai *dai;
Mark Brown6b05eda2008-12-08 19:26:48 +0000792 int i, found, ret, ac97;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200793
Mark Brown435c5e22008-12-04 15:32:53 +0000794 if (card->instantiated)
795 return;
Mark Brown63084192008-12-02 15:08:03 +0000796
Mark Brown435c5e22008-12-04 15:32:53 +0000797 found = 0;
798 list_for_each_entry(platform, &platform_list, list)
799 if (card->platform == platform) {
800 found = 1;
801 break;
802 }
803 if (!found) {
804 dev_dbg(card->dev, "Platform %s not registered\n",
805 card->platform->name);
806 return;
Mark Brownc5af3a22008-11-28 13:29:45 +0000807 }
808
Mark Brown6b05eda2008-12-08 19:26:48 +0000809 ac97 = 0;
Mark Brown435c5e22008-12-04 15:32:53 +0000810 for (i = 0; i < card->num_links; i++) {
811 found = 0;
812 list_for_each_entry(dai, &dai_list, list)
813 if (card->dai_link[i].cpu_dai == dai) {
814 found = 1;
815 break;
816 }
817 if (!found) {
818 dev_dbg(card->dev, "DAI %s not registered\n",
819 card->dai_link[i].cpu_dai->name);
820 return;
821 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000822
823 if (card->dai_link[i].cpu_dai->ac97_control)
824 ac97 = 1;
Mark Brown435c5e22008-12-04 15:32:53 +0000825 }
826
Mark Brown6b05eda2008-12-08 19:26:48 +0000827 /* If we have AC97 in the system then don't wait for the
828 * codec. This will need revisiting if we have to handle
829 * systems with mixed AC97 and non-AC97 parts. Only check for
830 * DAIs currently; we can't do this per link since some AC97
831 * codecs have non-AC97 DAIs.
832 */
833 if (!ac97)
834 for (i = 0; i < card->num_links; i++) {
835 found = 0;
836 list_for_each_entry(dai, &dai_list, list)
837 if (card->dai_link[i].codec_dai == dai) {
838 found = 1;
839 break;
840 }
841 if (!found) {
842 dev_dbg(card->dev, "DAI %s not registered\n",
843 card->dai_link[i].codec_dai->name);
844 return;
845 }
846 }
847
Mark Brown435c5e22008-12-04 15:32:53 +0000848 /* Note that we do not current check for codec components */
849
850 dev_dbg(card->dev, "All components present, instantiating\n");
851
852 /* Found everything, bring it up */
Mark Brown87506542008-11-18 20:50:34 +0000853 if (card->probe) {
854 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200855 if (ret < 0)
Mark Brown435c5e22008-12-04 15:32:53 +0000856 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200857 }
858
Mark Brown87506542008-11-18 20:50:34 +0000859 for (i = 0; i < card->num_links; i++) {
860 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200861 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +0100862 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +0200863 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200864 goto cpu_dai_err;
865 }
866 }
867
868 if (codec_dev->probe) {
869 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200870 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200871 goto cpu_dai_err;
872 }
873
874 if (platform->probe) {
875 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200876 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200877 goto platform_err;
878 }
879
880 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +0000881 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +0100882#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +0100883 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +0000884 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +0100885#endif
Andy Green6ed25972008-06-13 16:24:05 +0100886
Mark Brown435c5e22008-12-04 15:32:53 +0000887 card->instantiated = 1;
888
889 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200891platform_err:
892 if (codec_dev->remove)
893 codec_dev->remove(pdev);
894
895cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +0100896 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +0000897 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200898 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100899 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200900 }
901
Mark Brown87506542008-11-18 20:50:34 +0000902 if (card->remove)
903 card->remove(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +0000904}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200905
Mark Brown435c5e22008-12-04 15:32:53 +0000906/*
907 * Attempt to initialise any uninitalised cards. Must be called with
908 * client_mutex.
909 */
910static void snd_soc_instantiate_cards(void)
911{
912 struct snd_soc_card *card;
913 list_for_each_entry(card, &card_list, list)
914 snd_soc_instantiate_card(card);
915}
916
917/* probes a new socdev */
918static int soc_probe(struct platform_device *pdev)
919{
920 int ret = 0;
921 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
922 struct snd_soc_card *card = socdev->card;
923
924 /* Bodge while we push things out of socdev */
925 card->socdev = socdev;
926
927 /* Bodge while we unpick instantiation */
928 card->dev = &pdev->dev;
929 ret = snd_soc_register_card(card);
930 if (ret != 0) {
931 dev_err(&pdev->dev, "Failed to register card\n");
932 return ret;
933 }
934
935 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200936}
937
938/* removes a socdev */
939static int soc_remove(struct platform_device *pdev)
940{
941 int i;
942 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000943 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000944 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200945 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
946
Mark Brown63084192008-12-02 15:08:03 +0000947 run_delayed_work(&card->delayed_work);
Liam Girdwood965ac422007-01-31 14:14:57 +0100948
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200949 if (platform->remove)
950 platform->remove(pdev);
951
952 if (codec_dev->remove)
953 codec_dev->remove(pdev);
954
Mark Brown87506542008-11-18 20:50:34 +0000955 for (i = 0; i < card->num_links; i++) {
956 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200957 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100958 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200959 }
960
Mark Brown87506542008-11-18 20:50:34 +0000961 if (card->remove)
962 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200963
Mark Brownc5af3a22008-11-28 13:29:45 +0000964 snd_soc_unregister_card(card);
965
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200966 return 0;
967}
968
969/* ASoC platform driver */
970static struct platform_driver soc_driver = {
971 .driver = {
972 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +0200973 .owner = THIS_MODULE,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200974 },
975 .probe = soc_probe,
976 .remove = soc_remove,
977 .suspend = soc_suspend,
978 .resume = soc_resume,
979};
980
981/* create a new pcm */
982static int soc_new_pcm(struct snd_soc_device *socdev,
983 struct snd_soc_dai_link *dai_link, int num)
984{
Mark Brown87689d52008-12-02 16:01:14 +0000985 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +0000986 struct snd_soc_codec *codec = card->codec;
Mark Brown87689d52008-12-02 16:01:14 +0000987 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100988 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
989 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200990 struct snd_soc_pcm_runtime *rtd;
991 struct snd_pcm *pcm;
992 char new_name[64];
993 int ret = 0, playback = 0, capture = 0;
994
995 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
996 if (rtd == NULL)
997 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100998
999 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001000 rtd->socdev = socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001001 codec_dai->codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001002
1003 /* check client and interface hw capabilities */
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001004 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
1005 num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001006
1007 if (codec_dai->playback.channels_min)
1008 playback = 1;
1009 if (codec_dai->capture.channels_min)
1010 capture = 1;
1011
1012 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1013 capture, &pcm);
1014 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001015 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1016 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001017 kfree(rtd);
1018 return ret;
1019 }
1020
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001021 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001022 pcm->private_data = rtd;
Mark Brown87689d52008-12-02 16:01:14 +00001023 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1024 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
1025 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1026 soc_pcm_ops.copy = platform->pcm_ops->copy;
1027 soc_pcm_ops.silence = platform->pcm_ops->silence;
1028 soc_pcm_ops.ack = platform->pcm_ops->ack;
1029 soc_pcm_ops.page = platform->pcm_ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001030
1031 if (playback)
1032 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1033
1034 if (capture)
1035 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1036
Mark Brown87689d52008-12-02 16:01:14 +00001037 ret = platform->pcm_new(codec->card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001038 if (ret < 0) {
1039 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1040 kfree(rtd);
1041 return ret;
1042 }
1043
Mark Brown87689d52008-12-02 16:01:14 +00001044 pcm->private_free = platform->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001045 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1046 cpu_dai->name);
1047 return ret;
1048}
1049
1050/* codec register dump */
Mark Brown6627a652009-01-23 22:55:23 +00001051static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001052{
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001053 int i, step = 1, count = 0;
1054
1055 if (!codec->reg_cache_size)
1056 return 0;
1057
1058 if (codec->reg_cache_step)
1059 step = codec->reg_cache_step;
1060
1061 count += sprintf(buf, "%s registers\n", codec->name);
Mark Brown58cd33c2008-07-29 11:42:25 +01001062 for (i = 0; i < codec->reg_cache_size; i += step) {
1063 count += sprintf(buf + count, "%2x: ", i);
1064 if (count >= PAGE_SIZE - 1)
1065 break;
1066
1067 if (codec->display_register)
1068 count += codec->display_register(codec, buf + count,
1069 PAGE_SIZE - count, i);
1070 else
1071 count += snprintf(buf + count, PAGE_SIZE - count,
1072 "%4x", codec->read(codec, i));
1073
1074 if (count >= PAGE_SIZE - 1)
1075 break;
1076
1077 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1078 if (count >= PAGE_SIZE - 1)
1079 break;
1080 }
1081
1082 /* Truncate count; min() would cause a warning */
1083 if (count >= PAGE_SIZE)
1084 count = PAGE_SIZE - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001085
1086 return count;
1087}
Troy Kisky12ef1932008-10-13 17:42:14 -07001088static ssize_t codec_reg_show(struct device *dev,
1089 struct device_attribute *attr, char *buf)
1090{
1091 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001092 return soc_codec_reg_show(devdata->card->codec, buf);
Troy Kisky12ef1932008-10-13 17:42:14 -07001093}
1094
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001095static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1096
Troy Kisky12ef1932008-10-13 17:42:14 -07001097#ifdef CONFIG_DEBUG_FS
1098static int codec_reg_open_file(struct inode *inode, struct file *file)
1099{
1100 file->private_data = inode->i_private;
1101 return 0;
1102}
1103
1104static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1105 size_t count, loff_t *ppos)
1106{
1107 ssize_t ret;
Mark Brown384c89e2008-12-03 17:34:03 +00001108 struct snd_soc_codec *codec = file->private_data;
Troy Kisky12ef1932008-10-13 17:42:14 -07001109 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1110 if (!buf)
1111 return -ENOMEM;
Mark Brown6627a652009-01-23 22:55:23 +00001112 ret = soc_codec_reg_show(codec, buf);
Troy Kisky12ef1932008-10-13 17:42:14 -07001113 if (ret >= 0)
1114 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1115 kfree(buf);
1116 return ret;
1117}
1118
1119static ssize_t codec_reg_write_file(struct file *file,
1120 const char __user *user_buf, size_t count, loff_t *ppos)
1121{
1122 char buf[32];
1123 int buf_size;
1124 char *start = buf;
1125 unsigned long reg, value;
1126 int step = 1;
Mark Brown384c89e2008-12-03 17:34:03 +00001127 struct snd_soc_codec *codec = file->private_data;
Troy Kisky12ef1932008-10-13 17:42:14 -07001128
1129 buf_size = min(count, (sizeof(buf)-1));
1130 if (copy_from_user(buf, user_buf, buf_size))
1131 return -EFAULT;
1132 buf[buf_size] = 0;
1133
1134 if (codec->reg_cache_step)
1135 step = codec->reg_cache_step;
1136
1137 while (*start == ' ')
1138 start++;
1139 reg = simple_strtoul(start, &start, 16);
1140 if ((reg >= codec->reg_cache_size) || (reg % step))
1141 return -EINVAL;
1142 while (*start == ' ')
1143 start++;
1144 if (strict_strtoul(start, 16, &value))
1145 return -EINVAL;
1146 codec->write(codec, reg, value);
1147 return buf_size;
1148}
1149
1150static const struct file_operations codec_reg_fops = {
1151 .open = codec_reg_open_file,
1152 .read = codec_reg_read_file,
1153 .write = codec_reg_write_file,
1154};
1155
Mark Brown384c89e2008-12-03 17:34:03 +00001156static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001157{
Mark Brown384c89e2008-12-03 17:34:03 +00001158 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
1159 debugfs_root, codec,
1160 &codec_reg_fops);
1161 if (!codec->debugfs_reg)
1162 printk(KERN_WARNING
1163 "ASoC: Failed to create codec register debugfs file\n");
Troy Kisky12ef1932008-10-13 17:42:14 -07001164
Mark Brown384c89e2008-12-03 17:34:03 +00001165 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
1166 debugfs_root,
1167 &codec->pop_time);
1168 if (!codec->debugfs_pop_time)
1169 printk(KERN_WARNING
1170 "Failed to create pop time debugfs file\n");
Troy Kisky12ef1932008-10-13 17:42:14 -07001171}
1172
Mark Brown384c89e2008-12-03 17:34:03 +00001173static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001174{
Mark Brown384c89e2008-12-03 17:34:03 +00001175 debugfs_remove(codec->debugfs_pop_time);
1176 debugfs_remove(codec->debugfs_reg);
Troy Kisky12ef1932008-10-13 17:42:14 -07001177}
1178
1179#else
1180
Mark Brown384c89e2008-12-03 17:34:03 +00001181static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001182{
1183}
1184
Mark Brown384c89e2008-12-03 17:34:03 +00001185static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001186{
1187}
1188#endif
1189
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001190/**
1191 * snd_soc_new_ac97_codec - initailise AC97 device
1192 * @codec: audio codec
1193 * @ops: AC97 bus operations
1194 * @num: AC97 codec number
1195 *
1196 * Initialises AC97 codec resources for use by ad-hoc devices only.
1197 */
1198int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1199 struct snd_ac97_bus_ops *ops, int num)
1200{
1201 mutex_lock(&codec->mutex);
1202
1203 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1204 if (codec->ac97 == NULL) {
1205 mutex_unlock(&codec->mutex);
1206 return -ENOMEM;
1207 }
1208
1209 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1210 if (codec->ac97->bus == NULL) {
1211 kfree(codec->ac97);
1212 codec->ac97 = NULL;
1213 mutex_unlock(&codec->mutex);
1214 return -ENOMEM;
1215 }
1216
1217 codec->ac97->bus->ops = ops;
1218 codec->ac97->num = num;
1219 mutex_unlock(&codec->mutex);
1220 return 0;
1221}
1222EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1223
1224/**
1225 * snd_soc_free_ac97_codec - free AC97 codec device
1226 * @codec: audio codec
1227 *
1228 * Frees AC97 codec device resources.
1229 */
1230void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1231{
1232 mutex_lock(&codec->mutex);
1233 kfree(codec->ac97->bus);
1234 kfree(codec->ac97);
1235 codec->ac97 = NULL;
1236 mutex_unlock(&codec->mutex);
1237}
1238EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1239
1240/**
1241 * snd_soc_update_bits - update codec register bits
1242 * @codec: audio codec
1243 * @reg: codec register
1244 * @mask: register mask
1245 * @value: new value
1246 *
1247 * Writes new register value.
1248 *
1249 * Returns 1 for change else 0.
1250 */
1251int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1252 unsigned short mask, unsigned short value)
1253{
1254 int change;
1255 unsigned short old, new;
1256
1257 mutex_lock(&io_mutex);
1258 old = snd_soc_read(codec, reg);
1259 new = (old & ~mask) | value;
1260 change = old != new;
1261 if (change)
1262 snd_soc_write(codec, reg, new);
1263
1264 mutex_unlock(&io_mutex);
1265 return change;
1266}
1267EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1268
1269/**
1270 * snd_soc_test_bits - test register for change
1271 * @codec: audio codec
1272 * @reg: codec register
1273 * @mask: register mask
1274 * @value: new value
1275 *
1276 * Tests a register with a new value and checks if the new value is
1277 * different from the old value.
1278 *
1279 * Returns 1 for change else 0.
1280 */
1281int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1282 unsigned short mask, unsigned short value)
1283{
1284 int change;
1285 unsigned short old, new;
1286
1287 mutex_lock(&io_mutex);
1288 old = snd_soc_read(codec, reg);
1289 new = (old & ~mask) | value;
1290 change = old != new;
1291 mutex_unlock(&io_mutex);
1292
1293 return change;
1294}
1295EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1296
1297/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001298 * snd_soc_new_pcms - create new sound card and pcms
1299 * @socdev: the SoC audio device
Mark Brownac11a2b2009-01-01 12:18:17 +00001300 * @idx: ALSA card index
1301 * @xid: card identification
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001302 *
1303 * Create a new sound card based upon the codec and interface pcms.
1304 *
1305 * Returns 0 for success, else error.
1306 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001307int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001308{
Mark Brown87506542008-11-18 20:50:34 +00001309 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001310 struct snd_soc_codec *codec = card->codec;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001311 int ret, i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001312
1313 mutex_lock(&codec->mutex);
1314
1315 /* register a sound card */
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001316 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1317 if (ret < 0) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001318 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1319 codec->name);
1320 mutex_unlock(&codec->mutex);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001321 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001322 }
1323
1324 codec->card->dev = socdev->dev;
1325 codec->card->private_data = codec;
1326 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1327
1328 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001329 for (i = 0; i < card->num_links; i++) {
1330 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001331 if (ret < 0) {
1332 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001333 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001334 mutex_unlock(&codec->mutex);
1335 return ret;
1336 }
1337 }
1338
1339 mutex_unlock(&codec->mutex);
1340 return ret;
1341}
1342EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1343
1344/**
Mark Brown968a6022008-11-28 11:49:07 +00001345 * snd_soc_init_card - register sound card
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001346 * @socdev: the SoC audio device
1347 *
1348 * Register a SoC sound card. Also registers an AC97 device if the
1349 * codec is AC97 for ad hoc devices.
1350 *
1351 * Returns 0 for success, else error.
1352 */
Mark Brown968a6022008-11-28 11:49:07 +00001353int snd_soc_init_card(struct snd_soc_device *socdev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001354{
Mark Brown87506542008-11-18 20:50:34 +00001355 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001356 struct snd_soc_codec *codec = card->codec;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001357 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001358
Mark Brown87506542008-11-18 20:50:34 +00001359 for (i = 0; i < card->num_links; i++) {
1360 if (card->dai_link[i].init) {
1361 err = card->dai_link[i].init(codec);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001362 if (err < 0) {
1363 printk(KERN_ERR "asoc: failed to init %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001364 card->dai_link[i].stream_name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001365 continue;
1366 }
1367 }
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001368 if (card->dai_link[i].codec_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001369 ac97 = 1;
1370 }
1371 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
Mark Brown87506542008-11-18 20:50:34 +00001372 "%s", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001373 snprintf(codec->card->longname, sizeof(codec->card->longname),
Mark Brown87506542008-11-18 20:50:34 +00001374 "%s (%s)", card->name, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001375
1376 ret = snd_card_register(codec->card);
1377 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001378 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001379 codec->name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001380 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001381 }
1382
Mark Brown08c8efe2008-01-21 14:33:37 +01001383 mutex_lock(&codec->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001384#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown14fa43f2009-02-12 19:33:19 +00001385 /* Only instantiate AC97 if not already done by the adaptor
1386 * for the generic AC97 subsystem.
1387 */
1388 if (ac97 && strcmp(codec->name, "AC97") != 0) {
Liam Girdwood12e74f72006-10-16 21:19:48 +02001389 ret = soc_ac97_dev_register(codec);
1390 if (ret < 0) {
1391 printk(KERN_ERR "asoc: AC97 device register failed\n");
1392 snd_card_free(codec->card);
Mark Brown08c8efe2008-01-21 14:33:37 +01001393 mutex_unlock(&codec->mutex);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001394 goto out;
1395 }
1396 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001397#endif
1398
Liam Girdwood12e74f72006-10-16 21:19:48 +02001399 err = snd_soc_dapm_sys_add(socdev->dev);
1400 if (err < 0)
1401 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1402
1403 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1404 if (err < 0)
Mark Brown3ff3f642008-05-19 12:32:25 +02001405 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
Mark Brown08c8efe2008-01-21 14:33:37 +01001406
Mark Brown6627a652009-01-23 22:55:23 +00001407 soc_init_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001408 mutex_unlock(&codec->mutex);
Mark Brown08c8efe2008-01-21 14:33:37 +01001409
1410out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001411 return ret;
1412}
Mark Brown968a6022008-11-28 11:49:07 +00001413EXPORT_SYMBOL_GPL(snd_soc_init_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001414
1415/**
1416 * snd_soc_free_pcms - free sound card and pcms
1417 * @socdev: the SoC audio device
1418 *
1419 * Frees sound card and pcms associated with the socdev.
1420 * Also unregister the codec if it is an AC97 device.
1421 */
1422void snd_soc_free_pcms(struct snd_soc_device *socdev)
1423{
Mark Brown6627a652009-01-23 22:55:23 +00001424 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001425#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001426 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001427 int i;
1428#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001429
1430 mutex_lock(&codec->mutex);
Mark Brown6627a652009-01-23 22:55:23 +00001431 soc_cleanup_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001432#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001433 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001434 codec_dai = &codec->dai[i];
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001435 if (codec_dai->ac97_control && codec->ac97) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001436 soc_ac97_dev_unregister(codec);
1437 goto free_card;
1438 }
1439 }
1440free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001441#endif
1442
1443 if (codec->card)
1444 snd_card_free(codec->card);
1445 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1446 mutex_unlock(&codec->mutex);
1447}
1448EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1449
1450/**
1451 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1452 * @substream: the pcm substream
1453 * @hw: the hardware parameters
1454 *
1455 * Sets the substream runtime hardware parameters.
1456 */
1457int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1458 const struct snd_pcm_hardware *hw)
1459{
1460 struct snd_pcm_runtime *runtime = substream->runtime;
1461 runtime->hw.info = hw->info;
1462 runtime->hw.formats = hw->formats;
1463 runtime->hw.period_bytes_min = hw->period_bytes_min;
1464 runtime->hw.period_bytes_max = hw->period_bytes_max;
1465 runtime->hw.periods_min = hw->periods_min;
1466 runtime->hw.periods_max = hw->periods_max;
1467 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1468 runtime->hw.fifo_size = hw->fifo_size;
1469 return 0;
1470}
1471EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1472
1473/**
1474 * snd_soc_cnew - create new control
1475 * @_template: control template
1476 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001477 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001478 *
1479 * Create a new mixer control from a template control.
1480 *
1481 * Returns 0 for success, else error.
1482 */
1483struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1484 void *data, char *long_name)
1485{
1486 struct snd_kcontrol_new template;
1487
1488 memcpy(&template, _template, sizeof(template));
1489 if (long_name)
1490 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001491 template.index = 0;
1492
1493 return snd_ctl_new1(&template, data);
1494}
1495EXPORT_SYMBOL_GPL(snd_soc_cnew);
1496
1497/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001498 * snd_soc_add_controls - add an array of controls to a codec.
1499 * Convienience function to add a list of controls. Many codecs were
1500 * duplicating this code.
1501 *
1502 * @codec: codec to add controls to
1503 * @controls: array of controls to add
1504 * @num_controls: number of elements in the array
1505 *
1506 * Return 0 for success, else error.
1507 */
1508int snd_soc_add_controls(struct snd_soc_codec *codec,
1509 const struct snd_kcontrol_new *controls, int num_controls)
1510{
1511 struct snd_card *card = codec->card;
1512 int err, i;
1513
1514 for (i = 0; i < num_controls; i++) {
1515 const struct snd_kcontrol_new *control = &controls[i];
1516 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1517 if (err < 0) {
1518 dev_err(codec->dev, "%s: Failed to add %s\n",
1519 codec->name, control->name);
1520 return err;
1521 }
1522 }
1523
1524 return 0;
1525}
1526EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1527
1528/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001529 * snd_soc_info_enum_double - enumerated double mixer info callback
1530 * @kcontrol: mixer control
1531 * @uinfo: control element information
1532 *
1533 * Callback to provide information about a double enumerated
1534 * mixer control.
1535 *
1536 * Returns 0 for success.
1537 */
1538int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1539 struct snd_ctl_elem_info *uinfo)
1540{
1541 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1542
1543 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1544 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001545 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001546
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001547 if (uinfo->value.enumerated.item > e->max - 1)
1548 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001549 strcpy(uinfo->value.enumerated.name,
1550 e->texts[uinfo->value.enumerated.item]);
1551 return 0;
1552}
1553EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1554
1555/**
1556 * snd_soc_get_enum_double - enumerated double mixer get callback
1557 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001558 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001559 *
1560 * Callback to get the value of a double enumerated mixer.
1561 *
1562 * Returns 0 for success.
1563 */
1564int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1565 struct snd_ctl_elem_value *ucontrol)
1566{
1567 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1568 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1569 unsigned short val, bitmask;
1570
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001571 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001572 ;
1573 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001574 ucontrol->value.enumerated.item[0]
1575 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001576 if (e->shift_l != e->shift_r)
1577 ucontrol->value.enumerated.item[1] =
1578 (val >> e->shift_r) & (bitmask - 1);
1579
1580 return 0;
1581}
1582EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1583
1584/**
1585 * snd_soc_put_enum_double - enumerated double mixer put callback
1586 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001587 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001588 *
1589 * Callback to set the value of a double enumerated mixer.
1590 *
1591 * Returns 0 for success.
1592 */
1593int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1594 struct snd_ctl_elem_value *ucontrol)
1595{
1596 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1597 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1598 unsigned short val;
1599 unsigned short mask, bitmask;
1600
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001601 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001602 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001603 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001604 return -EINVAL;
1605 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1606 mask = (bitmask - 1) << e->shift_l;
1607 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001608 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001609 return -EINVAL;
1610 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1611 mask |= (bitmask - 1) << e->shift_r;
1612 }
1613
1614 return snd_soc_update_bits(codec, e->reg, mask, val);
1615}
1616EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1617
1618/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001619 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1620 * @kcontrol: mixer control
1621 * @ucontrol: control element information
1622 *
1623 * Callback to get the value of a double semi enumerated mixer.
1624 *
1625 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1626 * used for handling bitfield coded enumeration for example.
1627 *
1628 * Returns 0 for success.
1629 */
1630int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1631 struct snd_ctl_elem_value *ucontrol)
1632{
1633 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001634 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001635 unsigned short reg_val, val, mux;
1636
1637 reg_val = snd_soc_read(codec, e->reg);
1638 val = (reg_val >> e->shift_l) & e->mask;
1639 for (mux = 0; mux < e->max; mux++) {
1640 if (val == e->values[mux])
1641 break;
1642 }
1643 ucontrol->value.enumerated.item[0] = mux;
1644 if (e->shift_l != e->shift_r) {
1645 val = (reg_val >> e->shift_r) & e->mask;
1646 for (mux = 0; mux < e->max; mux++) {
1647 if (val == e->values[mux])
1648 break;
1649 }
1650 ucontrol->value.enumerated.item[1] = mux;
1651 }
1652
1653 return 0;
1654}
1655EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1656
1657/**
1658 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1659 * @kcontrol: mixer control
1660 * @ucontrol: control element information
1661 *
1662 * Callback to set the value of a double semi enumerated mixer.
1663 *
1664 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1665 * used for handling bitfield coded enumeration for example.
1666 *
1667 * Returns 0 for success.
1668 */
1669int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1670 struct snd_ctl_elem_value *ucontrol)
1671{
1672 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001673 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001674 unsigned short val;
1675 unsigned short mask;
1676
1677 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1678 return -EINVAL;
1679 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1680 mask = e->mask << e->shift_l;
1681 if (e->shift_l != e->shift_r) {
1682 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1683 return -EINVAL;
1684 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1685 mask |= e->mask << e->shift_r;
1686 }
1687
1688 return snd_soc_update_bits(codec, e->reg, mask, val);
1689}
1690EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1691
1692/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001693 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1694 * @kcontrol: mixer control
1695 * @uinfo: control element information
1696 *
1697 * Callback to provide information about an external enumerated
1698 * single mixer.
1699 *
1700 * Returns 0 for success.
1701 */
1702int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1703 struct snd_ctl_elem_info *uinfo)
1704{
1705 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1706
1707 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1708 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001709 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001710
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001711 if (uinfo->value.enumerated.item > e->max - 1)
1712 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001713 strcpy(uinfo->value.enumerated.name,
1714 e->texts[uinfo->value.enumerated.item]);
1715 return 0;
1716}
1717EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1718
1719/**
1720 * snd_soc_info_volsw_ext - external single mixer info callback
1721 * @kcontrol: mixer control
1722 * @uinfo: control element information
1723 *
1724 * Callback to provide information about a single external mixer control.
1725 *
1726 * Returns 0 for success.
1727 */
1728int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1729 struct snd_ctl_elem_info *uinfo)
1730{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001731 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001732
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001733 if (max == 1)
1734 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1735 else
1736 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1737
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001738 uinfo->count = 1;
1739 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001740 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001741 return 0;
1742}
1743EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1744
1745/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001746 * snd_soc_info_volsw - single mixer info callback
1747 * @kcontrol: mixer control
1748 * @uinfo: control element information
1749 *
1750 * Callback to provide information about a single mixer control.
1751 *
1752 * Returns 0 for success.
1753 */
1754int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1755 struct snd_ctl_elem_info *uinfo)
1756{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001757 struct soc_mixer_control *mc =
1758 (struct soc_mixer_control *)kcontrol->private_value;
1759 int max = mc->max;
Mark Brown762b8df2008-10-30 12:37:08 +00001760 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001761 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001762
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001763 if (max == 1)
1764 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1765 else
1766 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1767
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001768 uinfo->count = shift == rshift ? 1 : 2;
1769 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001770 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001771 return 0;
1772}
1773EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1774
1775/**
1776 * snd_soc_get_volsw - single mixer get callback
1777 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001778 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001779 *
1780 * Callback to get the value of a single mixer control.
1781 *
1782 * Returns 0 for success.
1783 */
1784int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1785 struct snd_ctl_elem_value *ucontrol)
1786{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001787 struct soc_mixer_control *mc =
1788 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001789 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001790 unsigned int reg = mc->reg;
1791 unsigned int shift = mc->shift;
1792 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001793 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001794 unsigned int mask = (1 << fls(max)) - 1;
1795 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001796
1797 ucontrol->value.integer.value[0] =
1798 (snd_soc_read(codec, reg) >> shift) & mask;
1799 if (shift != rshift)
1800 ucontrol->value.integer.value[1] =
1801 (snd_soc_read(codec, reg) >> rshift) & mask;
1802 if (invert) {
1803 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001804 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001805 if (shift != rshift)
1806 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001807 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001808 }
1809
1810 return 0;
1811}
1812EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1813
1814/**
1815 * snd_soc_put_volsw - single mixer put callback
1816 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001817 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001818 *
1819 * Callback to set the value of a single mixer control.
1820 *
1821 * Returns 0 for success.
1822 */
1823int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1824 struct snd_ctl_elem_value *ucontrol)
1825{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001826 struct soc_mixer_control *mc =
1827 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001828 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001829 unsigned int reg = mc->reg;
1830 unsigned int shift = mc->shift;
1831 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001832 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001833 unsigned int mask = (1 << fls(max)) - 1;
1834 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001835 unsigned short val, val2, val_mask;
1836
1837 val = (ucontrol->value.integer.value[0] & mask);
1838 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001839 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001840 val_mask = mask << shift;
1841 val = val << shift;
1842 if (shift != rshift) {
1843 val2 = (ucontrol->value.integer.value[1] & mask);
1844 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001845 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001846 val_mask |= mask << rshift;
1847 val |= val2 << rshift;
1848 }
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001849 return snd_soc_update_bits(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001850}
1851EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1852
1853/**
1854 * snd_soc_info_volsw_2r - double mixer info callback
1855 * @kcontrol: mixer control
1856 * @uinfo: control element information
1857 *
1858 * Callback to provide information about a double mixer control that
1859 * spans 2 codec registers.
1860 *
1861 * Returns 0 for success.
1862 */
1863int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1864 struct snd_ctl_elem_info *uinfo)
1865{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001866 struct soc_mixer_control *mc =
1867 (struct soc_mixer_control *)kcontrol->private_value;
1868 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001869
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001870 if (max == 1)
1871 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1872 else
1873 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1874
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001875 uinfo->count = 2;
1876 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001877 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001878 return 0;
1879}
1880EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1881
1882/**
1883 * snd_soc_get_volsw_2r - double mixer get callback
1884 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001885 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001886 *
1887 * Callback to get the value of a double mixer control that spans 2 registers.
1888 *
1889 * Returns 0 for success.
1890 */
1891int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1892 struct snd_ctl_elem_value *ucontrol)
1893{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001894 struct soc_mixer_control *mc =
1895 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001896 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001897 unsigned int reg = mc->reg;
1898 unsigned int reg2 = mc->rreg;
1899 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001900 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001901 unsigned int mask = (1<<fls(max))-1;
1902 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001903
1904 ucontrol->value.integer.value[0] =
1905 (snd_soc_read(codec, reg) >> shift) & mask;
1906 ucontrol->value.integer.value[1] =
1907 (snd_soc_read(codec, reg2) >> shift) & mask;
1908 if (invert) {
1909 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001910 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001911 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001912 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001913 }
1914
1915 return 0;
1916}
1917EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1918
1919/**
1920 * snd_soc_put_volsw_2r - double mixer set callback
1921 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001922 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001923 *
1924 * Callback to set the value of a double mixer control that spans 2 registers.
1925 *
1926 * Returns 0 for success.
1927 */
1928int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1929 struct snd_ctl_elem_value *ucontrol)
1930{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001931 struct soc_mixer_control *mc =
1932 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001933 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001934 unsigned int reg = mc->reg;
1935 unsigned int reg2 = mc->rreg;
1936 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001937 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001938 unsigned int mask = (1 << fls(max)) - 1;
1939 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001940 int err;
1941 unsigned short val, val2, val_mask;
1942
1943 val_mask = mask << shift;
1944 val = (ucontrol->value.integer.value[0] & mask);
1945 val2 = (ucontrol->value.integer.value[1] & mask);
1946
1947 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001948 val = max - val;
1949 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001950 }
1951
1952 val = val << shift;
1953 val2 = val2 << shift;
1954
Mark Brown3ff3f642008-05-19 12:32:25 +02001955 err = snd_soc_update_bits(codec, reg, val_mask, val);
1956 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001957 return err;
1958
1959 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1960 return err;
1961}
1962EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1963
Mark Browne13ac2e2008-05-28 17:58:05 +01001964/**
1965 * snd_soc_info_volsw_s8 - signed mixer info callback
1966 * @kcontrol: mixer control
1967 * @uinfo: control element information
1968 *
1969 * Callback to provide information about a signed mixer control.
1970 *
1971 * Returns 0 for success.
1972 */
1973int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1974 struct snd_ctl_elem_info *uinfo)
1975{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001976 struct soc_mixer_control *mc =
1977 (struct soc_mixer_control *)kcontrol->private_value;
1978 int max = mc->max;
1979 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001980
1981 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1982 uinfo->count = 2;
1983 uinfo->value.integer.min = 0;
1984 uinfo->value.integer.max = max-min;
1985 return 0;
1986}
1987EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1988
1989/**
1990 * snd_soc_get_volsw_s8 - signed mixer get callback
1991 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001992 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01001993 *
1994 * Callback to get the value of a signed mixer control.
1995 *
1996 * Returns 0 for success.
1997 */
1998int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1999 struct snd_ctl_elem_value *ucontrol)
2000{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002001 struct soc_mixer_control *mc =
2002 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002003 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002004 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002005 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002006 int val = snd_soc_read(codec, reg);
2007
2008 ucontrol->value.integer.value[0] =
2009 ((signed char)(val & 0xff))-min;
2010 ucontrol->value.integer.value[1] =
2011 ((signed char)((val >> 8) & 0xff))-min;
2012 return 0;
2013}
2014EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2015
2016/**
2017 * snd_soc_put_volsw_sgn - signed mixer put callback
2018 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002019 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002020 *
2021 * Callback to set the value of a signed mixer control.
2022 *
2023 * Returns 0 for success.
2024 */
2025int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2026 struct snd_ctl_elem_value *ucontrol)
2027{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002028 struct soc_mixer_control *mc =
2029 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002030 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002031 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002032 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002033 unsigned short val;
2034
2035 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2036 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2037
2038 return snd_soc_update_bits(codec, reg, 0xffff, val);
2039}
2040EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2041
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002042/**
2043 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2044 * @dai: DAI
2045 * @clk_id: DAI specific clock ID
2046 * @freq: new clock frequency in Hz
2047 * @dir: new clock direction - input/output.
2048 *
2049 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2050 */
2051int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2052 unsigned int freq, int dir)
2053{
Mark Browndee89c42008-11-18 22:11:38 +00002054 if (dai->ops.set_sysclk)
2055 return dai->ops.set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002056 else
2057 return -EINVAL;
2058}
2059EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2060
2061/**
2062 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2063 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002064 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002065 * @div: new clock divisor.
2066 *
2067 * Configures the clock dividers. This is used to derive the best DAI bit and
2068 * frame clocks from the system or master clock. It's best to set the DAI bit
2069 * and frame clocks as low as possible to save system power.
2070 */
2071int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2072 int div_id, int div)
2073{
Mark Browndee89c42008-11-18 22:11:38 +00002074 if (dai->ops.set_clkdiv)
2075 return dai->ops.set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002076 else
2077 return -EINVAL;
2078}
2079EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2080
2081/**
2082 * snd_soc_dai_set_pll - configure DAI PLL.
2083 * @dai: DAI
2084 * @pll_id: DAI specific PLL ID
2085 * @freq_in: PLL input clock frequency in Hz
2086 * @freq_out: requested PLL output clock frequency in Hz
2087 *
2088 * Configures and enables PLL to generate output clock based on input clock.
2089 */
2090int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
2091 int pll_id, unsigned int freq_in, unsigned int freq_out)
2092{
Mark Browndee89c42008-11-18 22:11:38 +00002093 if (dai->ops.set_pll)
2094 return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002095 else
2096 return -EINVAL;
2097}
2098EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2099
2100/**
2101 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2102 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002103 * @fmt: SND_SOC_DAIFMT_ format value.
2104 *
2105 * Configures the DAI hardware format and clocking.
2106 */
2107int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2108{
Mark Browndee89c42008-11-18 22:11:38 +00002109 if (dai->ops.set_fmt)
2110 return dai->ops.set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002111 else
2112 return -EINVAL;
2113}
2114EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2115
2116/**
2117 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2118 * @dai: DAI
2119 * @mask: DAI specific mask representing used slots.
2120 * @slots: Number of slots in use.
2121 *
2122 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2123 * specific.
2124 */
2125int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2126 unsigned int mask, int slots)
2127{
Mark Browndee89c42008-11-18 22:11:38 +00002128 if (dai->ops.set_sysclk)
2129 return dai->ops.set_tdm_slot(dai, mask, slots);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002130 else
2131 return -EINVAL;
2132}
2133EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2134
2135/**
2136 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2137 * @dai: DAI
2138 * @tristate: tristate enable
2139 *
2140 * Tristates the DAI so that others can use it.
2141 */
2142int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2143{
Mark Browndee89c42008-11-18 22:11:38 +00002144 if (dai->ops.set_sysclk)
2145 return dai->ops.set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002146 else
2147 return -EINVAL;
2148}
2149EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2150
2151/**
2152 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2153 * @dai: DAI
2154 * @mute: mute enable
2155 *
2156 * Mutes the DAI DAC.
2157 */
2158int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2159{
Mark Browndee89c42008-11-18 22:11:38 +00002160 if (dai->ops.digital_mute)
2161 return dai->ops.digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002162 else
2163 return -EINVAL;
2164}
2165EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2166
Mark Brownc5af3a22008-11-28 13:29:45 +00002167/**
2168 * snd_soc_register_card - Register a card with the ASoC core
2169 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002170 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002171 *
2172 * Note that currently this is an internal only function: it will be
2173 * exposed to machine drivers after further backporting of ASoC v2
2174 * registration APIs.
2175 */
2176static int snd_soc_register_card(struct snd_soc_card *card)
2177{
2178 if (!card->name || !card->dev)
2179 return -EINVAL;
2180
2181 INIT_LIST_HEAD(&card->list);
2182 card->instantiated = 0;
2183
2184 mutex_lock(&client_mutex);
2185 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002186 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002187 mutex_unlock(&client_mutex);
2188
2189 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2190
2191 return 0;
2192}
2193
2194/**
2195 * snd_soc_unregister_card - Unregister a card with the ASoC core
2196 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002197 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002198 *
2199 * Note that currently this is an internal only function: it will be
2200 * exposed to machine drivers after further backporting of ASoC v2
2201 * registration APIs.
2202 */
2203static int snd_soc_unregister_card(struct snd_soc_card *card)
2204{
2205 mutex_lock(&client_mutex);
2206 list_del(&card->list);
2207 mutex_unlock(&client_mutex);
2208
2209 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2210
2211 return 0;
2212}
2213
Mark Brown91151712008-11-30 23:31:24 +00002214/**
2215 * snd_soc_register_dai - Register a DAI with the ASoC core
2216 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002217 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002218 */
2219int snd_soc_register_dai(struct snd_soc_dai *dai)
2220{
2221 if (!dai->name)
2222 return -EINVAL;
2223
2224 /* The device should become mandatory over time */
2225 if (!dai->dev)
2226 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2227
2228 INIT_LIST_HEAD(&dai->list);
2229
2230 mutex_lock(&client_mutex);
2231 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002232 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002233 mutex_unlock(&client_mutex);
2234
2235 pr_debug("Registered DAI '%s'\n", dai->name);
2236
2237 return 0;
2238}
2239EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2240
2241/**
2242 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2243 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002244 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002245 */
2246void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2247{
2248 mutex_lock(&client_mutex);
2249 list_del(&dai->list);
2250 mutex_unlock(&client_mutex);
2251
2252 pr_debug("Unregistered DAI '%s'\n", dai->name);
2253}
2254EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2255
2256/**
2257 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2258 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002259 * @dai: Array of DAIs to register
2260 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002261 */
2262int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2263{
2264 int i, ret;
2265
2266 for (i = 0; i < count; i++) {
2267 ret = snd_soc_register_dai(&dai[i]);
2268 if (ret != 0)
2269 goto err;
2270 }
2271
2272 return 0;
2273
2274err:
2275 for (i--; i >= 0; i--)
2276 snd_soc_unregister_dai(&dai[i]);
2277
2278 return ret;
2279}
2280EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2281
2282/**
2283 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2284 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002285 * @dai: Array of DAIs to unregister
2286 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002287 */
2288void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2289{
2290 int i;
2291
2292 for (i = 0; i < count; i++)
2293 snd_soc_unregister_dai(&dai[i]);
2294}
2295EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2296
Mark Brown12a48a8c2008-12-03 19:40:30 +00002297/**
2298 * snd_soc_register_platform - Register a platform with the ASoC core
2299 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002300 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00002301 */
2302int snd_soc_register_platform(struct snd_soc_platform *platform)
2303{
2304 if (!platform->name)
2305 return -EINVAL;
2306
2307 INIT_LIST_HEAD(&platform->list);
2308
2309 mutex_lock(&client_mutex);
2310 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002311 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00002312 mutex_unlock(&client_mutex);
2313
2314 pr_debug("Registered platform '%s'\n", platform->name);
2315
2316 return 0;
2317}
2318EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2319
2320/**
2321 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2322 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002323 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002324 */
2325void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2326{
2327 mutex_lock(&client_mutex);
2328 list_del(&platform->list);
2329 mutex_unlock(&client_mutex);
2330
2331 pr_debug("Unregistered platform '%s'\n", platform->name);
2332}
2333EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2334
Mark Brown0d0cf002008-12-10 14:32:45 +00002335/**
2336 * snd_soc_register_codec - Register a codec with the ASoC core
2337 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002338 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00002339 */
2340int snd_soc_register_codec(struct snd_soc_codec *codec)
2341{
2342 if (!codec->name)
2343 return -EINVAL;
2344
2345 /* The device should become mandatory over time */
2346 if (!codec->dev)
2347 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2348
2349 INIT_LIST_HEAD(&codec->list);
2350
2351 mutex_lock(&client_mutex);
2352 list_add(&codec->list, &codec_list);
2353 snd_soc_instantiate_cards();
2354 mutex_unlock(&client_mutex);
2355
2356 pr_debug("Registered codec '%s'\n", codec->name);
2357
2358 return 0;
2359}
2360EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2361
2362/**
2363 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2364 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002365 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00002366 */
2367void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2368{
2369 mutex_lock(&client_mutex);
2370 list_del(&codec->list);
2371 mutex_unlock(&client_mutex);
2372
2373 pr_debug("Unregistered codec '%s'\n", codec->name);
2374}
2375EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2376
Takashi Iwaic9b3a402008-12-10 07:47:22 +01002377static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002378{
Mark Brown384c89e2008-12-03 17:34:03 +00002379#ifdef CONFIG_DEBUG_FS
2380 debugfs_root = debugfs_create_dir("asoc", NULL);
2381 if (IS_ERR(debugfs_root) || !debugfs_root) {
2382 printk(KERN_WARNING
2383 "ASoC: Failed to create debugfs directory\n");
2384 debugfs_root = NULL;
2385 }
2386#endif
2387
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002388 return platform_driver_register(&soc_driver);
2389}
2390
Mark Brown7d8c16a2008-11-30 22:11:24 +00002391static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002392{
Mark Brown384c89e2008-12-03 17:34:03 +00002393#ifdef CONFIG_DEBUG_FS
2394 debugfs_remove_recursive(debugfs_root);
2395#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02002396 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002397}
2398
2399module_init(snd_soc_init);
2400module_exit(snd_soc_exit);
2401
2402/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002403MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002404MODULE_DESCRIPTION("ALSA SoC Core");
2405MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02002406MODULE_ALIAS("platform:soc-audio");