]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/soc/soc-core.c
ASoC: Display return code when failing to add a DAPM kcontrol
[net-next-2.6.git] / sound / soc / soc-core.c
CommitLineData
db2a4165
FM
1/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
0664d888
LG
5 * Copyright 2005 Openedhand Ltd.
6 *
d331124d 7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
0664d888
LG
8 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
db2a4165
FM
10 *
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 *
db2a4165
FM
16 * 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>
12ef193d 29#include <linux/debugfs.h>
db2a4165 30#include <linux/platform_device.h>
db2a4165
FM
31#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
db2a4165
FM
38static DEFINE_MUTEX(pcm_mutex);
39static DEFINE_MUTEX(io_mutex);
db2a4165
FM
40static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
41
384c89e2
MB
42#ifdef CONFIG_DEBUG_FS
43static struct dentry *debugfs_root;
44#endif
45
c5af3a2e
MB
46static DEFINE_MUTEX(client_mutex);
47static LIST_HEAD(card_list);
9115171a 48static LIST_HEAD(dai_list);
12a48a8c 49static LIST_HEAD(platform_list);
0d0cf00a 50static LIST_HEAD(codec_list);
c5af3a2e
MB
51
52static int snd_soc_register_card(struct snd_soc_card *card);
53static int snd_soc_unregister_card(struct snd_soc_card *card);
54
db2a4165
FM
55/*
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
965ac42c
LG
64/*
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
db2a4165
FM
83#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;
4ac5c61f 101 codec->ac97->dev.parent = codec->card->dev;
db2a4165
FM
102 codec->ac97->dev.release = soc_ac97_device_release;
103
bb072bf0
KS
104 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
105 codec->card->number, 0, codec->name);
db2a4165
FM
106 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
db2a4165
FM
116/*
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;
87689d56 125 struct snd_soc_card *card = socdev->card;
db2a4165 126 struct snd_pcm_runtime *runtime = substream->runtime;
cb666e5b 127 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 128 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
129 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
130 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
131 int ret = 0;
132
133 mutex_lock(&pcm_mutex);
134
135 /* startup the audio subsystem */
6335d055
EM
136 if (cpu_dai->ops->startup) {
137 ret = cpu_dai->ops->startup(substream, cpu_dai);
db2a4165
FM
138 if (ret < 0) {
139 printk(KERN_ERR "asoc: can't open interface %s\n",
cb666e5b 140 cpu_dai->name);
db2a4165
FM
141 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
6335d055
EM
153 if (codec_dai->ops->startup) {
154 ret = codec_dai->ops->startup(substream, codec_dai);
db2a4165 155 if (ret < 0) {
cb666e5b
LG
156 printk(KERN_ERR "asoc: can't open codec %s\n",
157 codec_dai->name);
158 goto codec_dai_err;
db2a4165
FM
159 }
160 }
161
cb666e5b
LG
162 if (machine->ops && machine->ops->startup) {
163 ret = machine->ops->startup(substream);
db2a4165 164 if (ret < 0) {
cb666e5b
LG
165 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
166 goto machine_err;
db2a4165
FM
167 }
168 }
169
db2a4165
FM
170 /* Check that the codec and cpu DAI's are compatible */
171 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
172 runtime->hw.rate_min =
3ff3f64b
MB
173 max(codec_dai->playback.rate_min,
174 cpu_dai->playback.rate_min);
db2a4165 175 runtime->hw.rate_max =
3ff3f64b
MB
176 min(codec_dai->playback.rate_max,
177 cpu_dai->playback.rate_max);
db2a4165 178 runtime->hw.channels_min =
cb666e5b
LG
179 max(codec_dai->playback.channels_min,
180 cpu_dai->playback.channels_min);
db2a4165 181 runtime->hw.channels_max =
cb666e5b
LG
182 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;
db2a4165
FM
188 } else {
189 runtime->hw.rate_min =
3ff3f64b
MB
190 max(codec_dai->capture.rate_min,
191 cpu_dai->capture.rate_min);
db2a4165 192 runtime->hw.rate_max =
3ff3f64b
MB
193 min(codec_dai->capture.rate_max,
194 cpu_dai->capture.rate_max);
db2a4165 195 runtime->hw.channels_min =
cb666e5b
LG
196 max(codec_dai->capture.channels_min,
197 cpu_dai->capture.channels_min);
db2a4165 198 runtime->hw.channels_max =
cb666e5b
LG
199 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;
db2a4165
FM
205 }
206
207 snd_pcm_limit_hw_rates(runtime);
208 if (!runtime->hw.rates) {
209 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
cb666e5b
LG
210 codec_dai->name, cpu_dai->name);
211 goto machine_err;
db2a4165
FM
212 }
213 if (!runtime->hw.formats) {
214 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
cb666e5b
LG
215 codec_dai->name, cpu_dai->name);
216 goto machine_err;
db2a4165
FM
217 }
218 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
219 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
cb666e5b
LG
220 codec_dai->name, cpu_dai->name);
221 goto machine_err;
db2a4165
FM
222 }
223
f24368c2
MB
224 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);
db2a4165 230
db2a4165 231 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
cb666e5b 232 cpu_dai->playback.active = codec_dai->playback.active = 1;
db2a4165 233 else
cb666e5b
LG
234 cpu_dai->capture.active = codec_dai->capture.active = 1;
235 cpu_dai->active = codec_dai->active = 1;
236 cpu_dai->runtime = runtime;
6627a653 237 card->codec->active++;
db2a4165
FM
238 mutex_unlock(&pcm_mutex);
239 return 0;
240
cb666e5b 241machine_err:
db2a4165
FM
242 if (machine->ops && machine->ops->shutdown)
243 machine->ops->shutdown(substream);
244
cb666e5b 245codec_dai_err:
db2a4165
FM
246 if (platform->pcm_ops->close)
247 platform->pcm_ops->close(substream);
248
249platform_err:
6335d055
EM
250 if (cpu_dai->ops->shutdown)
251 cpu_dai->ops->shutdown(substream, cpu_dai);
db2a4165
FM
252out:
253 mutex_unlock(&pcm_mutex);
254 return ret;
255}
256
257/*
3a4fa0a2 258 * Power down the audio subsystem pmdown_time msecs after close is called.
db2a4165
FM
259 * This is to ensure there are no pops or clicks in between any music tracks
260 * due to DAPM power cycling.
261 */
4484bb2e 262static void close_delayed_work(struct work_struct *work)
db2a4165 263{
6308419a
MB
264 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
265 delayed_work.work);
266 struct snd_soc_device *socdev = card->socdev;
6627a653 267 struct snd_soc_codec *codec = card->codec;
3c4b266f 268 struct snd_soc_dai *codec_dai;
db2a4165
FM
269 int i;
270
271 mutex_lock(&pcm_mutex);
3ff3f64b 272 for (i = 0; i < codec->num_dai; i++) {
db2a4165
FM
273 codec_dai = &codec->dai[i];
274
f24368c2
MB
275 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");
db2a4165
FM
279
280 /* are we waiting on this codec DAI stream */
281 if (codec_dai->pop_wait == 1) {
282
0be9898a 283 /* Reduce power if no longer active */
3c1c47e0 284 if (codec->active == 0) {
f24368c2
MB
285 pr_debug("pop wq D1 %s %s\n", codec->name,
286 codec_dai->playback.stream_name);
0be9898a
MB
287 snd_soc_dapm_set_bias_level(socdev,
288 SND_SOC_BIAS_PREPARE);
3c1c47e0
LG
289 }
290
db2a4165 291 codec_dai->pop_wait = 0;
0b4d221b
LG
292 snd_soc_dapm_stream_event(codec,
293 codec_dai->playback.stream_name,
db2a4165
FM
294 SND_SOC_DAPM_STREAM_STOP);
295
0be9898a 296 /* Fall into standby if no longer active */
db2a4165 297 if (codec->active == 0) {
f24368c2
MB
298 pr_debug("pop wq D3 %s %s\n", codec->name,
299 codec_dai->playback.stream_name);
0be9898a
MB
300 snd_soc_dapm_set_bias_level(socdev,
301 SND_SOC_BIAS_STANDBY);
db2a4165
FM
302 }
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;
6308419a 317 struct snd_soc_card *card = socdev->card;
cb666e5b 318 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 319 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
320 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
321 struct snd_soc_dai *codec_dai = machine->codec_dai;
6627a653 322 struct snd_soc_codec *codec = card->codec;
db2a4165
FM
323
324 mutex_lock(&pcm_mutex);
325
326 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
cb666e5b 327 cpu_dai->playback.active = codec_dai->playback.active = 0;
db2a4165 328 else
cb666e5b 329 cpu_dai->capture.active = codec_dai->capture.active = 0;
db2a4165 330
cb666e5b
LG
331 if (codec_dai->playback.active == 0 &&
332 codec_dai->capture.active == 0) {
333 cpu_dai->active = codec_dai->active = 0;
db2a4165
FM
334 }
335 codec->active--;
336
6010b2da
MB
337 /* 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
6335d055
EM
343 if (cpu_dai->ops->shutdown)
344 cpu_dai->ops->shutdown(substream, cpu_dai);
db2a4165 345
6335d055
EM
346 if (codec_dai->ops->shutdown)
347 codec_dai->ops->shutdown(substream, codec_dai);
db2a4165
FM
348
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);
cb666e5b 354 cpu_dai->runtime = NULL;
db2a4165
FM
355
356 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
357 /* start delayed pop wq here for playback streams */
cb666e5b 358 codec_dai->pop_wait = 1;
6308419a 359 schedule_delayed_work(&card->delayed_work,
db2a4165
FM
360 msecs_to_jiffies(pmdown_time));
361 } else {
362 /* capture streams can be powered down now */
cb666e5b 363 snd_soc_dapm_stream_event(codec,
0b4d221b
LG
364 codec_dai->capture.stream_name,
365 SND_SOC_DAPM_STREAM_STOP);
db2a4165 366
0b4d221b 367 if (codec->active == 0 && codec_dai->pop_wait == 0)
0be9898a
MB
368 snd_soc_dapm_set_bias_level(socdev,
369 SND_SOC_BIAS_STANDBY);
db2a4165
FM
370 }
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;
6308419a 385 struct snd_soc_card *card = socdev->card;
cb666e5b 386 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 387 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
388 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
389 struct snd_soc_dai *codec_dai = machine->codec_dai;
6627a653 390 struct snd_soc_codec *codec = card->codec;
db2a4165
FM
391 int ret = 0;
392
393 mutex_lock(&pcm_mutex);
cb666e5b
LG
394
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
db2a4165
FM
403 if (platform->pcm_ops->prepare) {
404 ret = platform->pcm_ops->prepare(substream);
a71a468a
LG
405 if (ret < 0) {
406 printk(KERN_ERR "asoc: platform prepare error\n");
db2a4165 407 goto out;
a71a468a 408 }
db2a4165
FM
409 }
410
6335d055
EM
411 if (codec_dai->ops->prepare) {
412 ret = codec_dai->ops->prepare(substream, codec_dai);
a71a468a
LG
413 if (ret < 0) {
414 printk(KERN_ERR "asoc: codec DAI prepare error\n");
db2a4165 415 goto out;
a71a468a 416 }
db2a4165
FM
417 }
418
6335d055
EM
419 if (cpu_dai->ops->prepare) {
420 ret = cpu_dai->ops->prepare(substream, cpu_dai);
cb666e5b
LG
421 if (ret < 0) {
422 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
423 goto out;
424 }
425 }
db2a4165 426
d45f6219
MB
427 /* 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;
6308419a 431 cancel_delayed_work(&card->delayed_work);
d45f6219 432 }
db2a4165 433
d45f6219
MB
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);
db2a4165 438
d45f6219
MB
439 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
440 snd_soc_dapm_stream_event(codec,
cb666e5b 441 codec_dai->playback.stream_name,
db2a4165 442 SND_SOC_DAPM_STREAM_START);
d45f6219
MB
443 else
444 snd_soc_dapm_stream_event(codec,
cb666e5b 445 codec_dai->capture.stream_name,
db2a4165
FM
446 SND_SOC_DAPM_STREAM_START);
447
d45f6219
MB
448 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
449 snd_soc_dai_digital_mute(codec_dai, 0);
db2a4165 450
d45f6219
MB
451 } else {
452 /* codec already powered - power on widgets */
453 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
454 snd_soc_dapm_stream_event(codec,
cb666e5b 455 codec_dai->playback.stream_name,
db2a4165 456 SND_SOC_DAPM_STREAM_START);
d45f6219
MB
457 else
458 snd_soc_dapm_stream_event(codec,
cb666e5b 459 codec_dai->capture.stream_name,
db2a4165 460 SND_SOC_DAPM_STREAM_START);
8c6529db 461
d45f6219 462 snd_soc_dai_digital_mute(codec_dai, 0);
db2a4165
FM
463 }
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;
cb666e5b 480 struct snd_soc_dai_link *machine = rtd->dai;
87689d56
MB
481 struct snd_soc_card *card = socdev->card;
482 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
483 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
484 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
485 int ret = 0;
486
487 mutex_lock(&pcm_mutex);
488
cb666e5b
LG
489 if (machine->ops && machine->ops->hw_params) {
490 ret = machine->ops->hw_params(substream, params);
491 if (ret < 0) {
492 printk(KERN_ERR "asoc: machine hw_params failed\n");
db2a4165 493 goto out;
cb666e5b 494 }
db2a4165
FM
495 }
496
6335d055
EM
497 if (codec_dai->ops->hw_params) {
498 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
db2a4165
FM
499 if (ret < 0) {
500 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
cb666e5b
LG
501 codec_dai->name);
502 goto codec_err;
db2a4165
FM
503 }
504 }
505
6335d055
EM
506 if (cpu_dai->ops->hw_params) {
507 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
db2a4165 508 if (ret < 0) {
3ff3f64b 509 printk(KERN_ERR "asoc: interface %s hw params failed\n",
cb666e5b 510 cpu_dai->name);
db2a4165
FM
511 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) {
3ff3f64b 518 printk(KERN_ERR "asoc: platform %s hw params failed\n",
db2a4165
FM
519 platform->name);
520 goto platform_err;
521 }
522 }
523
db2a4165
FM
524out:
525 mutex_unlock(&pcm_mutex);
526 return ret;
527
db2a4165 528platform_err:
6335d055
EM
529 if (cpu_dai->ops->hw_free)
530 cpu_dai->ops->hw_free(substream, cpu_dai);
db2a4165
FM
531
532interface_err:
6335d055
EM
533 if (codec_dai->ops->hw_free)
534 codec_dai->ops->hw_free(substream, codec_dai);
cb666e5b
LG
535
536codec_err:
3ff3f64b 537 if (machine->ops && machine->ops->hw_free)
cb666e5b 538 machine->ops->hw_free(substream);
db2a4165
FM
539
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;
cb666e5b 551 struct snd_soc_dai_link *machine = rtd->dai;
87689d56
MB
552 struct snd_soc_card *card = socdev->card;
553 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
554 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
555 struct snd_soc_dai *codec_dai = machine->codec_dai;
6627a653 556 struct snd_soc_codec *codec = card->codec;
db2a4165
FM
557
558 mutex_lock(&pcm_mutex);
559
560 /* apply codec digital mute */
8c6529db
LG
561 if (!codec->active)
562 snd_soc_dai_digital_mute(codec_dai, 1);
db2a4165
FM
563
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 */
6335d055
EM
573 if (codec_dai->ops->hw_free)
574 codec_dai->ops->hw_free(substream, codec_dai);
db2a4165 575
6335d055
EM
576 if (cpu_dai->ops->hw_free)
577 cpu_dai->ops->hw_free(substream, cpu_dai);
db2a4165
FM
578
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;
87689d56 587 struct snd_soc_card *card= socdev->card;
cb666e5b 588 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 589 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
590 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
591 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
592 int ret;
593
6335d055
EM
594 if (codec_dai->ops->trigger) {
595 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
db2a4165
FM
596 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
6335d055
EM
606 if (cpu_dai->ops->trigger) {
607 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
db2a4165
FM
608 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{
3ff3f64b 628 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
87506549 629 struct snd_soc_card *card = socdev->card;
87689d56 630 struct snd_soc_platform *platform = card->platform;
3ff3f64b 631 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
6627a653 632 struct snd_soc_codec *codec = card->codec;
db2a4165
FM
633 int i;
634
6ed25978
AG
635 /* 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
db2a4165 645 /* mute any active DAC's */
dee89c4d
MB
646 for (i = 0; i < card->num_links; i++) {
647 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
6335d055
EM
648 if (dai->ops->digital_mute && dai->playback.active)
649 dai->ops->digital_mute(dai, 1);
db2a4165
FM
650 }
651
4ccab3e7 652 /* suspend all pcms */
87506549
MB
653 for (i = 0; i < card->num_links; i++)
654 snd_pcm_suspend_all(card->dai_link[i].pcm);
4ccab3e7 655
87506549
MB
656 if (card->suspend_pre)
657 card->suspend_pre(pdev, state);
db2a4165 658
87506549
MB
659 for (i = 0; i < card->num_links; i++) {
660 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 661 if (cpu_dai->suspend && !cpu_dai->ac97_control)
dc7d7b83 662 cpu_dai->suspend(cpu_dai);
db2a4165 663 if (platform->suspend)
07c84d04 664 platform->suspend(cpu_dai);
db2a4165
FM
665 }
666
667 /* close any waiting streams and save state */
6308419a 668 run_delayed_work(&card->delayed_work);
0be9898a 669 codec->suspend_bias_level = codec->bias_level;
db2a4165 670
3ff3f64b 671 for (i = 0; i < codec->num_dai; i++) {
db2a4165
FM
672 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
87506549
MB
685 for (i = 0; i < card->num_links; i++) {
686 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 687 if (cpu_dai->suspend && cpu_dai->ac97_control)
dc7d7b83 688 cpu_dai->suspend(cpu_dai);
db2a4165
FM
689 }
690
87506549
MB
691 if (card->suspend_post)
692 card->suspend_post(pdev, state);
db2a4165
FM
693
694 return 0;
695}
696
6ed25978
AG
697/* 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)
db2a4165 701{
6308419a
MB
702 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;
87689d56 706 struct snd_soc_platform *platform = card->platform;
3ff3f64b 707 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
6627a653 708 struct snd_soc_codec *codec = card->codec;
6ed25978 709 struct platform_device *pdev = to_platform_device(socdev->dev);
db2a4165
FM
710 int i;
711
6ed25978
AG
712 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
713 * so userspace apps are blocked from touching us
714 */
715
fde22f27 716 dev_dbg(socdev->dev, "starting resume work\n");
6ed25978 717
87506549
MB
718 if (card->resume_pre)
719 card->resume_pre(pdev);
db2a4165 720
87506549
MB
721 for (i = 0; i < card->num_links; i++) {
722 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 723 if (cpu_dai->resume && cpu_dai->ac97_control)
dc7d7b83 724 cpu_dai->resume(cpu_dai);
db2a4165
FM
725 }
726
727 if (codec_dev->resume)
728 codec_dev->resume(pdev);
729
3ff3f64b
MB
730 for (i = 0; i < codec->num_dai; i++) {
731 char *stream = codec->dai[i].playback.stream_name;
db2a4165
FM
732 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
3ff3f64b 741 /* unmute any active DACs */
dee89c4d
MB
742 for (i = 0; i < card->num_links; i++) {
743 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
6335d055
EM
744 if (dai->ops->digital_mute && dai->playback.active)
745 dai->ops->digital_mute(dai, 0);
db2a4165
FM
746 }
747
87506549
MB
748 for (i = 0; i < card->num_links; i++) {
749 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 750 if (cpu_dai->resume && !cpu_dai->ac97_control)
dc7d7b83 751 cpu_dai->resume(cpu_dai);
db2a4165 752 if (platform->resume)
07c84d04 753 platform->resume(cpu_dai);
db2a4165
FM
754 }
755
87506549
MB
756 if (card->resume_post)
757 card->resume_post(pdev);
db2a4165 758
fde22f27 759 dev_dbg(socdev->dev, "resume work completed\n");
6ed25978
AG
760
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);
6308419a 769 struct snd_soc_card *card = socdev->card;
64ab9baa 770 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
6ed25978 771
64ab9baa
MB
772 /* AC97 devices might have other drivers hanging off them so
773 * need to resume immediately. Other drivers don't have that
774 * problem and may take a substantial amount of time to resume
775 * due to I/O costs and anti-pop so handle them out of line.
776 */
777 if (cpu_dai->ac97_control) {
778 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
779 soc_resume_deferred(&card->deferred_resume_work);
780 } else {
781 dev_dbg(socdev->dev, "Scheduling resume work\n");
782 if (!schedule_work(&card->deferred_resume_work))
783 dev_err(socdev->dev, "resume work item may be lost\n");
784 }
6ed25978 785
db2a4165
FM
786 return 0;
787}
788
789#else
790#define soc_suspend NULL
791#define soc_resume NULL
792#endif
793
435c5e25 794static void snd_soc_instantiate_card(struct snd_soc_card *card)
db2a4165 795{
435c5e25
MB
796 struct platform_device *pdev = container_of(card->dev,
797 struct platform_device,
798 dev);
799 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
800 struct snd_soc_platform *platform;
801 struct snd_soc_dai *dai;
6b05eda6 802 int i, found, ret, ac97;
435c5e25
MB
803
804 if (card->instantiated)
805 return;
806
807 found = 0;
808 list_for_each_entry(platform, &platform_list, list)
809 if (card->platform == platform) {
810 found = 1;
811 break;
812 }
813 if (!found) {
814 dev_dbg(card->dev, "Platform %s not registered\n",
815 card->platform->name);
816 return;
817 }
6308419a 818
6b05eda6 819 ac97 = 0;
435c5e25
MB
820 for (i = 0; i < card->num_links; i++) {
821 found = 0;
822 list_for_each_entry(dai, &dai_list, list)
823 if (card->dai_link[i].cpu_dai == dai) {
824 found = 1;
825 break;
826 }
827 if (!found) {
828 dev_dbg(card->dev, "DAI %s not registered\n",
829 card->dai_link[i].cpu_dai->name);
830 return;
831 }
6b05eda6
MB
832
833 if (card->dai_link[i].cpu_dai->ac97_control)
834 ac97 = 1;
c5af3a2e
MB
835 }
836
6b05eda6
MB
837 /* If we have AC97 in the system then don't wait for the
838 * codec. This will need revisiting if we have to handle
839 * systems with mixed AC97 and non-AC97 parts. Only check for
840 * DAIs currently; we can't do this per link since some AC97
841 * codecs have non-AC97 DAIs.
842 */
843 if (!ac97)
844 for (i = 0; i < card->num_links; i++) {
845 found = 0;
846 list_for_each_entry(dai, &dai_list, list)
847 if (card->dai_link[i].codec_dai == dai) {
848 found = 1;
849 break;
850 }
851 if (!found) {
852 dev_dbg(card->dev, "DAI %s not registered\n",
853 card->dai_link[i].codec_dai->name);
854 return;
855 }
856 }
857
435c5e25
MB
858 /* Note that we do not current check for codec components */
859
860 dev_dbg(card->dev, "All components present, instantiating\n");
861
862 /* Found everything, bring it up */
87506549
MB
863 if (card->probe) {
864 ret = card->probe(pdev);
3ff3f64b 865 if (ret < 0)
435c5e25 866 return;
db2a4165
FM
867 }
868
87506549
MB
869 for (i = 0; i < card->num_links; i++) {
870 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 871 if (cpu_dai->probe) {
bdb92876 872 ret = cpu_dai->probe(pdev, cpu_dai);
3ff3f64b 873 if (ret < 0)
db2a4165
FM
874 goto cpu_dai_err;
875 }
876 }
877
878 if (codec_dev->probe) {
879 ret = codec_dev->probe(pdev);
3ff3f64b 880 if (ret < 0)
db2a4165
FM
881 goto cpu_dai_err;
882 }
883
884 if (platform->probe) {
885 ret = platform->probe(pdev);
3ff3f64b 886 if (ret < 0)
db2a4165
FM
887 goto platform_err;
888 }
889
890 /* DAPM stream work */
6308419a 891 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
1301a964 892#ifdef CONFIG_PM
6ed25978 893 /* deferred resume work */
6308419a 894 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1301a964 895#endif
6ed25978 896
435c5e25
MB
897 card->instantiated = 1;
898
899 return;
db2a4165 900
db2a4165
FM
901platform_err:
902 if (codec_dev->remove)
903 codec_dev->remove(pdev);
904
905cpu_dai_err:
18b9b3d9 906 for (i--; i >= 0; i--) {
87506549 907 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 908 if (cpu_dai->remove)
bdb92876 909 cpu_dai->remove(pdev, cpu_dai);
db2a4165
FM
910 }
911
87506549
MB
912 if (card->remove)
913 card->remove(pdev);
435c5e25 914}
db2a4165 915
435c5e25
MB
916/*
917 * Attempt to initialise any uninitalised cards. Must be called with
918 * client_mutex.
919 */
920static void snd_soc_instantiate_cards(void)
921{
922 struct snd_soc_card *card;
923 list_for_each_entry(card, &card_list, list)
924 snd_soc_instantiate_card(card);
925}
926
927/* probes a new socdev */
928static int soc_probe(struct platform_device *pdev)
929{
930 int ret = 0;
931 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
932 struct snd_soc_card *card = socdev->card;
933
934 /* Bodge while we push things out of socdev */
935 card->socdev = socdev;
936
937 /* Bodge while we unpick instantiation */
938 card->dev = &pdev->dev;
939 ret = snd_soc_register_card(card);
940 if (ret != 0) {
941 dev_err(&pdev->dev, "Failed to register card\n");
942 return ret;
943 }
944
945 return 0;
db2a4165
FM
946}
947
948/* removes a socdev */
949static int soc_remove(struct platform_device *pdev)
950{
951 int i;
952 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
87506549 953 struct snd_soc_card *card = socdev->card;
87689d56 954 struct snd_soc_platform *platform = card->platform;
db2a4165
FM
955 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
956
6308419a 957 run_delayed_work(&card->delayed_work);
965ac42c 958
db2a4165
FM
959 if (platform->remove)
960 platform->remove(pdev);
961
962 if (codec_dev->remove)
963 codec_dev->remove(pdev);
964
87506549
MB
965 for (i = 0; i < card->num_links; i++) {
966 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 967 if (cpu_dai->remove)
bdb92876 968 cpu_dai->remove(pdev, cpu_dai);
db2a4165
FM
969 }
970
87506549
MB
971 if (card->remove)
972 card->remove(pdev);
db2a4165 973
c5af3a2e
MB
974 snd_soc_unregister_card(card);
975
db2a4165
FM
976 return 0;
977}
978
979/* ASoC platform driver */
980static struct platform_driver soc_driver = {
981 .driver = {
982 .name = "soc-audio",
8b45a209 983 .owner = THIS_MODULE,
db2a4165
FM
984 },
985 .probe = soc_probe,
986 .remove = soc_remove,
987 .suspend = soc_suspend,
988 .resume = soc_resume,
989};
990
991/* create a new pcm */
992static int soc_new_pcm(struct snd_soc_device *socdev,
993 struct snd_soc_dai_link *dai_link, int num)
994{
87689d56 995 struct snd_soc_card *card = socdev->card;
6627a653 996 struct snd_soc_codec *codec = card->codec;
87689d56 997 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
998 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
999 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
db2a4165
FM
1000 struct snd_soc_pcm_runtime *rtd;
1001 struct snd_pcm *pcm;
1002 char new_name[64];
1003 int ret = 0, playback = 0, capture = 0;
1004
1005 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1006 if (rtd == NULL)
1007 return -ENOMEM;
cb666e5b
LG
1008
1009 rtd->dai = dai_link;
db2a4165 1010 rtd->socdev = socdev;
6627a653 1011 codec_dai->codec = card->codec;
db2a4165
FM
1012
1013 /* check client and interface hw capabilities */
3ba9e10a
MB
1014 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
1015 num);
db2a4165
FM
1016
1017 if (codec_dai->playback.channels_min)
1018 playback = 1;
1019 if (codec_dai->capture.channels_min)
1020 capture = 1;
1021
1022 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1023 capture, &pcm);
1024 if (ret < 0) {
3ff3f64b
MB
1025 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1026 codec->name);
db2a4165
FM
1027 kfree(rtd);
1028 return ret;
1029 }
1030
4ccab3e7 1031 dai_link->pcm = pcm;
db2a4165 1032 pcm->private_data = rtd;
87689d56
MB
1033 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1034 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
1035 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1036 soc_pcm_ops.copy = platform->pcm_ops->copy;
1037 soc_pcm_ops.silence = platform->pcm_ops->silence;
1038 soc_pcm_ops.ack = platform->pcm_ops->ack;
1039 soc_pcm_ops.page = platform->pcm_ops->page;
db2a4165
FM
1040
1041 if (playback)
1042 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1043
1044 if (capture)
1045 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1046
87689d56 1047 ret = platform->pcm_new(codec->card, codec_dai, pcm);
db2a4165
FM
1048 if (ret < 0) {
1049 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1050 kfree(rtd);
1051 return ret;
1052 }
1053
87689d56 1054 pcm->private_free = platform->pcm_free;
db2a4165
FM
1055 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1056 cpu_dai->name);
1057 return ret;
1058}
1059
1060/* codec register dump */
6627a653 1061static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
db2a4165 1062{
db2a4165
FM
1063 int i, step = 1, count = 0;
1064
1065 if (!codec->reg_cache_size)
1066 return 0;
1067
1068 if (codec->reg_cache_step)
1069 step = codec->reg_cache_step;
1070
1071 count += sprintf(buf, "%s registers\n", codec->name);
58cd33c0
MB
1072 for (i = 0; i < codec->reg_cache_size; i += step) {
1073 count += sprintf(buf + count, "%2x: ", i);
1074 if (count >= PAGE_SIZE - 1)
1075 break;
1076
1077 if (codec->display_register)
1078 count += codec->display_register(codec, buf + count,
1079 PAGE_SIZE - count, i);
1080 else
1081 count += snprintf(buf + count, PAGE_SIZE - count,
1082 "%4x", codec->read(codec, i));
1083
1084 if (count >= PAGE_SIZE - 1)
1085 break;
1086
1087 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1088 if (count >= PAGE_SIZE - 1)
1089 break;
1090 }
1091
1092 /* Truncate count; min() would cause a warning */
1093 if (count >= PAGE_SIZE)
1094 count = PAGE_SIZE - 1;
db2a4165
FM
1095
1096 return count;
1097}
12ef193d
TK
1098static ssize_t codec_reg_show(struct device *dev,
1099 struct device_attribute *attr, char *buf)
1100{
1101 struct snd_soc_device *devdata = dev_get_drvdata(dev);
6627a653 1102 return soc_codec_reg_show(devdata->card->codec, buf);
12ef193d
TK
1103}
1104
db2a4165
FM
1105static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1106
12ef193d
TK
1107#ifdef CONFIG_DEBUG_FS
1108static int codec_reg_open_file(struct inode *inode, struct file *file)
1109{
1110 file->private_data = inode->i_private;
1111 return 0;
1112}
1113
1114static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1115 size_t count, loff_t *ppos)
1116{
1117 ssize_t ret;
384c89e2 1118 struct snd_soc_codec *codec = file->private_data;
12ef193d
TK
1119 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1120 if (!buf)
1121 return -ENOMEM;
6627a653 1122 ret = soc_codec_reg_show(codec, buf);
12ef193d
TK
1123 if (ret >= 0)
1124 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1125 kfree(buf);
1126 return ret;
1127}
1128
1129static ssize_t codec_reg_write_file(struct file *file,
1130 const char __user *user_buf, size_t count, loff_t *ppos)
1131{
1132 char buf[32];
1133 int buf_size;
1134 char *start = buf;
1135 unsigned long reg, value;
1136 int step = 1;
384c89e2 1137 struct snd_soc_codec *codec = file->private_data;
12ef193d
TK
1138
1139 buf_size = min(count, (sizeof(buf)-1));
1140 if (copy_from_user(buf, user_buf, buf_size))
1141 return -EFAULT;
1142 buf[buf_size] = 0;
1143
1144 if (codec->reg_cache_step)
1145 step = codec->reg_cache_step;
1146
1147 while (*start == ' ')
1148 start++;
1149 reg = simple_strtoul(start, &start, 16);
1150 if ((reg >= codec->reg_cache_size) || (reg % step))
1151 return -EINVAL;
1152 while (*start == ' ')
1153 start++;
1154 if (strict_strtoul(start, 16, &value))
1155 return -EINVAL;
1156 codec->write(codec, reg, value);
1157 return buf_size;
1158}
1159
1160static const struct file_operations codec_reg_fops = {
1161 .open = codec_reg_open_file,
1162 .read = codec_reg_read_file,
1163 .write = codec_reg_write_file,
1164};
1165
384c89e2 1166static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
12ef193d 1167{
384c89e2
MB
1168 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
1169 debugfs_root, codec,
1170 &codec_reg_fops);
1171 if (!codec->debugfs_reg)
1172 printk(KERN_WARNING
1173 "ASoC: Failed to create codec register debugfs file\n");
1174
1175 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
1176 debugfs_root,
1177 &codec->pop_time);
1178 if (!codec->debugfs_pop_time)
1179 printk(KERN_WARNING
1180 "Failed to create pop time debugfs file\n");
12ef193d
TK
1181}
1182
384c89e2 1183static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
12ef193d 1184{
384c89e2
MB
1185 debugfs_remove(codec->debugfs_pop_time);
1186 debugfs_remove(codec->debugfs_reg);
12ef193d
TK
1187}
1188
1189#else
1190
384c89e2 1191static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
12ef193d
TK
1192{
1193}
1194
384c89e2 1195static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
12ef193d
TK
1196{
1197}
1198#endif
1199
db2a4165
FM
1200/**
1201 * snd_soc_new_ac97_codec - initailise AC97 device
1202 * @codec: audio codec
1203 * @ops: AC97 bus operations
1204 * @num: AC97 codec number
1205 *
1206 * Initialises AC97 codec resources for use by ad-hoc devices only.
1207 */
1208int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1209 struct snd_ac97_bus_ops *ops, int num)
1210{
1211 mutex_lock(&codec->mutex);
1212
1213 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1214 if (codec->ac97 == NULL) {
1215 mutex_unlock(&codec->mutex);
1216 return -ENOMEM;
1217 }
1218
1219 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1220 if (codec->ac97->bus == NULL) {
1221 kfree(codec->ac97);
1222 codec->ac97 = NULL;
1223 mutex_unlock(&codec->mutex);
1224 return -ENOMEM;
1225 }
1226
1227 codec->ac97->bus->ops = ops;
1228 codec->ac97->num = num;
1229 mutex_unlock(&codec->mutex);
1230 return 0;
1231}
1232EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1233
1234/**
1235 * snd_soc_free_ac97_codec - free AC97 codec device
1236 * @codec: audio codec
1237 *
1238 * Frees AC97 codec device resources.
1239 */
1240void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1241{
1242 mutex_lock(&codec->mutex);
1243 kfree(codec->ac97->bus);
1244 kfree(codec->ac97);
1245 codec->ac97 = NULL;
1246 mutex_unlock(&codec->mutex);
1247}
1248EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1249
1250/**
1251 * snd_soc_update_bits - update codec register bits
1252 * @codec: audio codec
1253 * @reg: codec register
1254 * @mask: register mask
1255 * @value: new value
1256 *
1257 * Writes new register value.
1258 *
1259 * Returns 1 for change else 0.
1260 */
1261int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1262 unsigned short mask, unsigned short value)
1263{
1264 int change;
1265 unsigned short old, new;
1266
1267 mutex_lock(&io_mutex);
1268 old = snd_soc_read(codec, reg);
1269 new = (old & ~mask) | value;
1270 change = old != new;
1271 if (change)
1272 snd_soc_write(codec, reg, new);
1273
1274 mutex_unlock(&io_mutex);
1275 return change;
1276}
1277EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1278
1279/**
1280 * snd_soc_test_bits - test register for change
1281 * @codec: audio codec
1282 * @reg: codec register
1283 * @mask: register mask
1284 * @value: new value
1285 *
1286 * Tests a register with a new value and checks if the new value is
1287 * different from the old value.
1288 *
1289 * Returns 1 for change else 0.
1290 */
1291int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1292 unsigned short mask, unsigned short value)
1293{
1294 int change;
1295 unsigned short old, new;
1296
1297 mutex_lock(&io_mutex);
1298 old = snd_soc_read(codec, reg);
1299 new = (old & ~mask) | value;
1300 change = old != new;
1301 mutex_unlock(&io_mutex);
1302
1303 return change;
1304}
1305EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1306
db2a4165
FM
1307/**
1308 * snd_soc_new_pcms - create new sound card and pcms
1309 * @socdev: the SoC audio device
ac11a2b3
MB
1310 * @idx: ALSA card index
1311 * @xid: card identification
db2a4165
FM
1312 *
1313 * Create a new sound card based upon the codec and interface pcms.
1314 *
1315 * Returns 0 for success, else error.
1316 */
bc7320c5 1317int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
db2a4165 1318{
87506549 1319 struct snd_soc_card *card = socdev->card;
6627a653 1320 struct snd_soc_codec *codec = card->codec;
bd7dd77c 1321 int ret, i;
db2a4165
FM
1322
1323 mutex_lock(&codec->mutex);
1324
1325 /* register a sound card */
bd7dd77c
TI
1326 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1327 if (ret < 0) {
db2a4165
FM
1328 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1329 codec->name);
1330 mutex_unlock(&codec->mutex);
bd7dd77c 1331 return ret;
db2a4165
FM
1332 }
1333
1334 codec->card->dev = socdev->dev;
1335 codec->card->private_data = codec;
1336 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1337
1338 /* create the pcms */
87506549
MB
1339 for (i = 0; i < card->num_links; i++) {
1340 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
db2a4165
FM
1341 if (ret < 0) {
1342 printk(KERN_ERR "asoc: can't create pcm %s\n",
87506549 1343 card->dai_link[i].stream_name);
db2a4165
FM
1344 mutex_unlock(&codec->mutex);
1345 return ret;
1346 }
1347 }
1348
1349 mutex_unlock(&codec->mutex);
1350 return ret;
1351}
1352EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1353
1354/**
968a6025 1355 * snd_soc_init_card - register sound card
db2a4165
FM
1356 * @socdev: the SoC audio device
1357 *
1358 * Register a SoC sound card. Also registers an AC97 device if the
1359 * codec is AC97 for ad hoc devices.
1360 *
1361 * Returns 0 for success, else error.
1362 */
968a6025 1363int snd_soc_init_card(struct snd_soc_device *socdev)
db2a4165 1364{
87506549 1365 struct snd_soc_card *card = socdev->card;
6627a653 1366 struct snd_soc_codec *codec = card->codec;
12e74f7d 1367 int ret = 0, i, ac97 = 0, err = 0;
db2a4165 1368
87506549
MB
1369 for (i = 0; i < card->num_links; i++) {
1370 if (card->dai_link[i].init) {
1371 err = card->dai_link[i].init(codec);
12e74f7d
LG
1372 if (err < 0) {
1373 printk(KERN_ERR "asoc: failed to init %s\n",
87506549 1374 card->dai_link[i].stream_name);
12e74f7d
LG
1375 continue;
1376 }
1377 }
3ba9e10a 1378 if (card->dai_link[i].codec_dai->ac97_control)
db2a4165
FM
1379 ac97 = 1;
1380 }
1381 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
87506549 1382 "%s", card->name);
db2a4165 1383 snprintf(codec->card->longname, sizeof(codec->card->longname),
87506549 1384 "%s (%s)", card->name, codec->name);
db2a4165
FM
1385
1386 ret = snd_card_register(codec->card);
1387 if (ret < 0) {
3ff3f64b 1388 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
db2a4165 1389 codec->name);
12e74f7d 1390 goto out;
db2a4165
FM
1391 }
1392
08c8efe6 1393 mutex_lock(&codec->mutex);
db2a4165 1394#ifdef CONFIG_SND_SOC_AC97_BUS
14fa43f5
MB
1395 /* Only instantiate AC97 if not already done by the adaptor
1396 * for the generic AC97 subsystem.
1397 */
1398 if (ac97 && strcmp(codec->name, "AC97") != 0) {
12e74f7d
LG
1399 ret = soc_ac97_dev_register(codec);
1400 if (ret < 0) {
1401 printk(KERN_ERR "asoc: AC97 device register failed\n");
1402 snd_card_free(codec->card);
08c8efe6 1403 mutex_unlock(&codec->mutex);
12e74f7d
LG
1404 goto out;
1405 }
1406 }
db2a4165
FM
1407#endif
1408
12e74f7d
LG
1409 err = snd_soc_dapm_sys_add(socdev->dev);
1410 if (err < 0)
1411 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1412
1413 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1414 if (err < 0)
3ff3f64b 1415 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
08c8efe6 1416
6627a653 1417 soc_init_codec_debugfs(codec);
db2a4165 1418 mutex_unlock(&codec->mutex);
08c8efe6
MB
1419
1420out:
db2a4165
FM
1421 return ret;
1422}
968a6025 1423EXPORT_SYMBOL_GPL(snd_soc_init_card);
db2a4165
FM
1424
1425/**
1426 * snd_soc_free_pcms - free sound card and pcms
1427 * @socdev: the SoC audio device
1428 *
1429 * Frees sound card and pcms associated with the socdev.
1430 * Also unregister the codec if it is an AC97 device.
1431 */
1432void snd_soc_free_pcms(struct snd_soc_device *socdev)
1433{
6627a653 1434 struct snd_soc_codec *codec = socdev->card->codec;
a68660e0 1435#ifdef CONFIG_SND_SOC_AC97_BUS
3c4b266f 1436 struct snd_soc_dai *codec_dai;
a68660e0
LG
1437 int i;
1438#endif
db2a4165
FM
1439
1440 mutex_lock(&codec->mutex);
6627a653 1441 soc_cleanup_codec_debugfs(codec);
db2a4165 1442#ifdef CONFIG_SND_SOC_AC97_BUS
3ff3f64b 1443 for (i = 0; i < codec->num_dai; i++) {
a68660e0 1444 codec_dai = &codec->dai[i];
d2314e0e
AN
1445 if (codec_dai->ac97_control && codec->ac97 &&
1446 strcmp(codec->name, "AC97") != 0) {
a68660e0
LG
1447 soc_ac97_dev_unregister(codec);
1448 goto free_card;
1449 }
1450 }
1451free_card:
db2a4165
FM
1452#endif
1453
1454 if (codec->card)
1455 snd_card_free(codec->card);
1456 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1457 mutex_unlock(&codec->mutex);
1458}
1459EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1460
1461/**
1462 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1463 * @substream: the pcm substream
1464 * @hw: the hardware parameters
1465 *
1466 * Sets the substream runtime hardware parameters.
1467 */
1468int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1469 const struct snd_pcm_hardware *hw)
1470{
1471 struct snd_pcm_runtime *runtime = substream->runtime;
1472 runtime->hw.info = hw->info;
1473 runtime->hw.formats = hw->formats;
1474 runtime->hw.period_bytes_min = hw->period_bytes_min;
1475 runtime->hw.period_bytes_max = hw->period_bytes_max;
1476 runtime->hw.periods_min = hw->periods_min;
1477 runtime->hw.periods_max = hw->periods_max;
1478 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1479 runtime->hw.fifo_size = hw->fifo_size;
1480 return 0;
1481}
1482EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1483
1484/**
1485 * snd_soc_cnew - create new control
1486 * @_template: control template
1487 * @data: control private data
ac11a2b3 1488 * @long_name: control long name
db2a4165
FM
1489 *
1490 * Create a new mixer control from a template control.
1491 *
1492 * Returns 0 for success, else error.
1493 */
1494struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1495 void *data, char *long_name)
1496{
1497 struct snd_kcontrol_new template;
1498
1499 memcpy(&template, _template, sizeof(template));
1500 if (long_name)
1501 template.name = long_name;
db2a4165
FM
1502 template.index = 0;
1503
1504 return snd_ctl_new1(&template, data);
1505}
1506EXPORT_SYMBOL_GPL(snd_soc_cnew);
1507
3e8e1952
IM
1508/**
1509 * snd_soc_add_controls - add an array of controls to a codec.
1510 * Convienience function to add a list of controls. Many codecs were
1511 * duplicating this code.
1512 *
1513 * @codec: codec to add controls to
1514 * @controls: array of controls to add
1515 * @num_controls: number of elements in the array
1516 *
1517 * Return 0 for success, else error.
1518 */
1519int snd_soc_add_controls(struct snd_soc_codec *codec,
1520 const struct snd_kcontrol_new *controls, int num_controls)
1521{
1522 struct snd_card *card = codec->card;
1523 int err, i;
1524
1525 for (i = 0; i < num_controls; i++) {
1526 const struct snd_kcontrol_new *control = &controls[i];
1527 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1528 if (err < 0) {
1529 dev_err(codec->dev, "%s: Failed to add %s\n",
1530 codec->name, control->name);
1531 return err;
1532 }
1533 }
1534
1535 return 0;
1536}
1537EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1538
db2a4165
FM
1539/**
1540 * snd_soc_info_enum_double - enumerated double mixer info callback
1541 * @kcontrol: mixer control
1542 * @uinfo: control element information
1543 *
1544 * Callback to provide information about a double enumerated
1545 * mixer control.
1546 *
1547 * Returns 0 for success.
1548 */
1549int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1550 struct snd_ctl_elem_info *uinfo)
1551{
1552 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1553
1554 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1555 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
f8ba0b7b 1556 uinfo->value.enumerated.items = e->max;
db2a4165 1557
f8ba0b7b
JS
1558 if (uinfo->value.enumerated.item > e->max - 1)
1559 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
1560 strcpy(uinfo->value.enumerated.name,
1561 e->texts[uinfo->value.enumerated.item]);
1562 return 0;
1563}
1564EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1565
1566/**
1567 * snd_soc_get_enum_double - enumerated double mixer get callback
1568 * @kcontrol: mixer control
ac11a2b3 1569 * @ucontrol: control element information
db2a4165
FM
1570 *
1571 * Callback to get the value of a double enumerated mixer.
1572 *
1573 * Returns 0 for success.
1574 */
1575int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1576 struct snd_ctl_elem_value *ucontrol)
1577{
1578 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1579 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1580 unsigned short val, bitmask;
1581
f8ba0b7b 1582 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165
FM
1583 ;
1584 val = snd_soc_read(codec, e->reg);
3ff3f64b
MB
1585 ucontrol->value.enumerated.item[0]
1586 = (val >> e->shift_l) & (bitmask - 1);
db2a4165
FM
1587 if (e->shift_l != e->shift_r)
1588 ucontrol->value.enumerated.item[1] =
1589 (val >> e->shift_r) & (bitmask - 1);
1590
1591 return 0;
1592}
1593EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1594
1595/**
1596 * snd_soc_put_enum_double - enumerated double mixer put callback
1597 * @kcontrol: mixer control
ac11a2b3 1598 * @ucontrol: control element information
db2a4165
FM
1599 *
1600 * Callback to set the value of a double enumerated mixer.
1601 *
1602 * Returns 0 for success.
1603 */
1604int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1605 struct snd_ctl_elem_value *ucontrol)
1606{
1607 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1608 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1609 unsigned short val;
1610 unsigned short mask, bitmask;
1611
f8ba0b7b 1612 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165 1613 ;
f8ba0b7b 1614 if (ucontrol->value.enumerated.item[0] > e->max - 1)
db2a4165
FM
1615 return -EINVAL;
1616 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1617 mask = (bitmask - 1) << e->shift_l;
1618 if (e->shift_l != e->shift_r) {
f8ba0b7b 1619 if (ucontrol->value.enumerated.item[1] > e->max - 1)
db2a4165
FM
1620 return -EINVAL;
1621 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1622 mask |= (bitmask - 1) << e->shift_r;
1623 }
1624
1625 return snd_soc_update_bits(codec, e->reg, mask, val);
1626}
1627EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1628
2e72f8e3
PU
1629/**
1630 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1631 * @kcontrol: mixer control
1632 * @ucontrol: control element information
1633 *
1634 * Callback to get the value of a double semi enumerated mixer.
1635 *
1636 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1637 * used for handling bitfield coded enumeration for example.
1638 *
1639 * Returns 0 for success.
1640 */
1641int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1642 struct snd_ctl_elem_value *ucontrol)
1643{
1644 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
74155556 1645 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2e72f8e3
PU
1646 unsigned short reg_val, val, mux;
1647
1648 reg_val = snd_soc_read(codec, e->reg);
1649 val = (reg_val >> e->shift_l) & e->mask;
1650 for (mux = 0; mux < e->max; mux++) {
1651 if (val == e->values[mux])
1652 break;
1653 }
1654 ucontrol->value.enumerated.item[0] = mux;
1655 if (e->shift_l != e->shift_r) {
1656 val = (reg_val >> e->shift_r) & e->mask;
1657 for (mux = 0; mux < e->max; mux++) {
1658 if (val == e->values[mux])
1659 break;
1660 }
1661 ucontrol->value.enumerated.item[1] = mux;
1662 }
1663
1664 return 0;
1665}
1666EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1667
1668/**
1669 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1670 * @kcontrol: mixer control
1671 * @ucontrol: control element information
1672 *
1673 * Callback to set the value of a double semi enumerated mixer.
1674 *
1675 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1676 * used for handling bitfield coded enumeration for example.
1677 *
1678 * Returns 0 for success.
1679 */
1680int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1681 struct snd_ctl_elem_value *ucontrol)
1682{
1683 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
74155556 1684 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2e72f8e3
PU
1685 unsigned short val;
1686 unsigned short mask;
1687
1688 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1689 return -EINVAL;
1690 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1691 mask = e->mask << e->shift_l;
1692 if (e->shift_l != e->shift_r) {
1693 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1694 return -EINVAL;
1695 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1696 mask |= e->mask << e->shift_r;
1697 }
1698
1699 return snd_soc_update_bits(codec, e->reg, mask, val);
1700}
1701EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1702
db2a4165
FM
1703/**
1704 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1705 * @kcontrol: mixer control
1706 * @uinfo: control element information
1707 *
1708 * Callback to provide information about an external enumerated
1709 * single mixer.
1710 *
1711 * Returns 0 for success.
1712 */
1713int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1714 struct snd_ctl_elem_info *uinfo)
1715{
1716 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1717
1718 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1719 uinfo->count = 1;
f8ba0b7b 1720 uinfo->value.enumerated.items = e->max;
db2a4165 1721
f8ba0b7b
JS
1722 if (uinfo->value.enumerated.item > e->max - 1)
1723 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
1724 strcpy(uinfo->value.enumerated.name,
1725 e->texts[uinfo->value.enumerated.item]);
1726 return 0;
1727}
1728EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1729
1730/**
1731 * snd_soc_info_volsw_ext - external single mixer info callback
1732 * @kcontrol: mixer control
1733 * @uinfo: control element information
1734 *
1735 * Callback to provide information about a single external mixer control.
1736 *
1737 * Returns 0 for success.
1738 */
1739int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1740 struct snd_ctl_elem_info *uinfo)
1741{
a7a4ac86
PZ
1742 int max = kcontrol->private_value;
1743
1744 if (max == 1)
1745 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1746 else
1747 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 1748
db2a4165
FM
1749 uinfo->count = 1;
1750 uinfo->value.integer.min = 0;
a7a4ac86 1751 uinfo->value.integer.max = max;
db2a4165
FM
1752 return 0;
1753}
1754EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1755
db2a4165
FM
1756/**
1757 * snd_soc_info_volsw - single mixer info callback
1758 * @kcontrol: mixer control
1759 * @uinfo: control element information
1760 *
1761 * Callback to provide information about a single mixer control.
1762 *
1763 * Returns 0 for success.
1764 */
1765int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_info *uinfo)
1767{
4eaa9819
JS
1768 struct soc_mixer_control *mc =
1769 (struct soc_mixer_control *)kcontrol->private_value;
1770 int max = mc->max;
762b8df7 1771 unsigned int shift = mc->shift;
815ecf8d 1772 unsigned int rshift = mc->rshift;
db2a4165 1773
a7a4ac86
PZ
1774 if (max == 1)
1775 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1776 else
1777 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1778
db2a4165
FM
1779 uinfo->count = shift == rshift ? 1 : 2;
1780 uinfo->value.integer.min = 0;
a7a4ac86 1781 uinfo->value.integer.max = max;
db2a4165
FM
1782 return 0;
1783}
1784EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1785
1786/**
1787 * snd_soc_get_volsw - single mixer get callback
1788 * @kcontrol: mixer control
ac11a2b3 1789 * @ucontrol: control element information
db2a4165
FM
1790 *
1791 * Callback to get the value of a single mixer control.
1792 *
1793 * Returns 0 for success.
1794 */
1795int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1796 struct snd_ctl_elem_value *ucontrol)
1797{
4eaa9819
JS
1798 struct soc_mixer_control *mc =
1799 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1800 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1801 unsigned int reg = mc->reg;
1802 unsigned int shift = mc->shift;
1803 unsigned int rshift = mc->rshift;
4eaa9819 1804 int max = mc->max;
815ecf8d
JS
1805 unsigned int mask = (1 << fls(max)) - 1;
1806 unsigned int invert = mc->invert;
db2a4165
FM
1807
1808 ucontrol->value.integer.value[0] =
1809 (snd_soc_read(codec, reg) >> shift) & mask;
1810 if (shift != rshift)
1811 ucontrol->value.integer.value[1] =
1812 (snd_soc_read(codec, reg) >> rshift) & mask;
1813 if (invert) {
1814 ucontrol->value.integer.value[0] =
a7a4ac86 1815 max - ucontrol->value.integer.value[0];
db2a4165
FM
1816 if (shift != rshift)
1817 ucontrol->value.integer.value[1] =
a7a4ac86 1818 max - ucontrol->value.integer.value[1];
db2a4165
FM
1819 }
1820
1821 return 0;
1822}
1823EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1824
1825/**
1826 * snd_soc_put_volsw - single mixer put callback
1827 * @kcontrol: mixer control
ac11a2b3 1828 * @ucontrol: control element information
db2a4165
FM
1829 *
1830 * Callback to set the value of a single mixer control.
1831 *
1832 * Returns 0 for success.
1833 */
1834int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1835 struct snd_ctl_elem_value *ucontrol)
1836{
4eaa9819
JS
1837 struct soc_mixer_control *mc =
1838 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1839 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1840 unsigned int reg = mc->reg;
1841 unsigned int shift = mc->shift;
1842 unsigned int rshift = mc->rshift;
4eaa9819 1843 int max = mc->max;
815ecf8d
JS
1844 unsigned int mask = (1 << fls(max)) - 1;
1845 unsigned int invert = mc->invert;
db2a4165
FM
1846 unsigned short val, val2, val_mask;
1847
1848 val = (ucontrol->value.integer.value[0] & mask);
1849 if (invert)
a7a4ac86 1850 val = max - val;
db2a4165
FM
1851 val_mask = mask << shift;
1852 val = val << shift;
1853 if (shift != rshift) {
1854 val2 = (ucontrol->value.integer.value[1] & mask);
1855 if (invert)
a7a4ac86 1856 val2 = max - val2;
db2a4165
FM
1857 val_mask |= mask << rshift;
1858 val |= val2 << rshift;
1859 }
a7a4ac86 1860 return snd_soc_update_bits(codec, reg, val_mask, val);
db2a4165
FM
1861}
1862EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1863
1864/**
1865 * snd_soc_info_volsw_2r - double mixer info callback
1866 * @kcontrol: mixer control
1867 * @uinfo: control element information
1868 *
1869 * Callback to provide information about a double mixer control that
1870 * spans 2 codec registers.
1871 *
1872 * Returns 0 for success.
1873 */
1874int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1875 struct snd_ctl_elem_info *uinfo)
1876{
4eaa9819
JS
1877 struct soc_mixer_control *mc =
1878 (struct soc_mixer_control *)kcontrol->private_value;
1879 int max = mc->max;
a7a4ac86
PZ
1880
1881 if (max == 1)
1882 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1883 else
1884 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 1885
db2a4165
FM
1886 uinfo->count = 2;
1887 uinfo->value.integer.min = 0;
a7a4ac86 1888 uinfo->value.integer.max = max;
db2a4165
FM
1889 return 0;
1890}
1891EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1892
1893/**
1894 * snd_soc_get_volsw_2r - double mixer get callback
1895 * @kcontrol: mixer control
ac11a2b3 1896 * @ucontrol: control element information
db2a4165
FM
1897 *
1898 * Callback to get the value of a double mixer control that spans 2 registers.
1899 *
1900 * Returns 0 for success.
1901 */
1902int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1903 struct snd_ctl_elem_value *ucontrol)
1904{
4eaa9819
JS
1905 struct soc_mixer_control *mc =
1906 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1907 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1908 unsigned int reg = mc->reg;
1909 unsigned int reg2 = mc->rreg;
1910 unsigned int shift = mc->shift;
4eaa9819 1911 int max = mc->max;
815ecf8d
JS
1912 unsigned int mask = (1<<fls(max))-1;
1913 unsigned int invert = mc->invert;
db2a4165
FM
1914
1915 ucontrol->value.integer.value[0] =
1916 (snd_soc_read(codec, reg) >> shift) & mask;
1917 ucontrol->value.integer.value[1] =
1918 (snd_soc_read(codec, reg2) >> shift) & mask;
1919 if (invert) {
1920 ucontrol->value.integer.value[0] =
a7a4ac86 1921 max - ucontrol->value.integer.value[0];
db2a4165 1922 ucontrol->value.integer.value[1] =
a7a4ac86 1923 max - ucontrol->value.integer.value[1];
db2a4165
FM
1924 }
1925
1926 return 0;
1927}
1928EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1929
1930/**
1931 * snd_soc_put_volsw_2r - double mixer set callback
1932 * @kcontrol: mixer control
ac11a2b3 1933 * @ucontrol: control element information
db2a4165
FM
1934 *
1935 * Callback to set the value of a double mixer control that spans 2 registers.
1936 *
1937 * Returns 0 for success.
1938 */
1939int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1940 struct snd_ctl_elem_value *ucontrol)
1941{
4eaa9819
JS
1942 struct soc_mixer_control *mc =
1943 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1944 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1945 unsigned int reg = mc->reg;
1946 unsigned int reg2 = mc->rreg;
1947 unsigned int shift = mc->shift;
4eaa9819 1948 int max = mc->max;
815ecf8d
JS
1949 unsigned int mask = (1 << fls(max)) - 1;
1950 unsigned int invert = mc->invert;
db2a4165
FM
1951 int err;
1952 unsigned short val, val2, val_mask;
1953
1954 val_mask = mask << shift;
1955 val = (ucontrol->value.integer.value[0] & mask);
1956 val2 = (ucontrol->value.integer.value[1] & mask);
1957
1958 if (invert) {
a7a4ac86
PZ
1959 val = max - val;
1960 val2 = max - val2;
db2a4165
FM
1961 }
1962
1963 val = val << shift;
1964 val2 = val2 << shift;
1965
3ff3f64b
MB
1966 err = snd_soc_update_bits(codec, reg, val_mask, val);
1967 if (err < 0)
db2a4165
FM
1968 return err;
1969
1970 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1971 return err;
1972}
1973EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1974
e13ac2e9
MB
1975/**
1976 * snd_soc_info_volsw_s8 - signed mixer info callback
1977 * @kcontrol: mixer control
1978 * @uinfo: control element information
1979 *
1980 * Callback to provide information about a signed mixer control.
1981 *
1982 * Returns 0 for success.
1983 */
1984int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1985 struct snd_ctl_elem_info *uinfo)
1986{
4eaa9819
JS
1987 struct soc_mixer_control *mc =
1988 (struct soc_mixer_control *)kcontrol->private_value;
1989 int max = mc->max;
1990 int min = mc->min;
e13ac2e9
MB
1991
1992 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1993 uinfo->count = 2;
1994 uinfo->value.integer.min = 0;
1995 uinfo->value.integer.max = max-min;
1996 return 0;
1997}
1998EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1999
2000/**
2001 * snd_soc_get_volsw_s8 - signed mixer get callback
2002 * @kcontrol: mixer control
ac11a2b3 2003 * @ucontrol: control element information
e13ac2e9
MB
2004 *
2005 * Callback to get the value of a signed mixer control.
2006 *
2007 * Returns 0 for success.
2008 */
2009int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2010 struct snd_ctl_elem_value *ucontrol)
2011{
4eaa9819
JS
2012 struct soc_mixer_control *mc =
2013 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 2014 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 2015 unsigned int reg = mc->reg;
4eaa9819 2016 int min = mc->min;
e13ac2e9
MB
2017 int val = snd_soc_read(codec, reg);
2018
2019 ucontrol->value.integer.value[0] =
2020 ((signed char)(val & 0xff))-min;
2021 ucontrol->value.integer.value[1] =
2022 ((signed char)((val >> 8) & 0xff))-min;
2023 return 0;
2024}
2025EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2026
2027/**
2028 * snd_soc_put_volsw_sgn - signed mixer put callback
2029 * @kcontrol: mixer control
ac11a2b3 2030 * @ucontrol: control element information
e13ac2e9
MB
2031 *
2032 * Callback to set the value of a signed mixer control.
2033 *
2034 * Returns 0 for success.
2035 */
2036int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2037 struct snd_ctl_elem_value *ucontrol)
2038{
4eaa9819
JS
2039 struct soc_mixer_control *mc =
2040 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 2041 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 2042 unsigned int reg = mc->reg;
4eaa9819 2043 int min = mc->min;
e13ac2e9
MB
2044 unsigned short val;
2045
2046 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2047 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2048
2049 return snd_soc_update_bits(codec, reg, 0xffff, val);
2050}
2051EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2052
8c6529db
LG
2053/**
2054 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2055 * @dai: DAI
2056 * @clk_id: DAI specific clock ID
2057 * @freq: new clock frequency in Hz
2058 * @dir: new clock direction - input/output.
2059 *
2060 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2061 */
2062int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2063 unsigned int freq, int dir)
2064{
6335d055
EM
2065 if (dai->ops->set_sysclk)
2066 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
8c6529db
LG
2067 else
2068 return -EINVAL;
2069}
2070EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2071
2072/**
2073 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2074 * @dai: DAI
ac11a2b3 2075 * @div_id: DAI specific clock divider ID
8c6529db
LG
2076 * @div: new clock divisor.
2077 *
2078 * Configures the clock dividers. This is used to derive the best DAI bit and
2079 * frame clocks from the system or master clock. It's best to set the DAI bit
2080 * and frame clocks as low as possible to save system power.
2081 */
2082int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2083 int div_id, int div)
2084{
6335d055
EM
2085 if (dai->ops->set_clkdiv)
2086 return dai->ops->set_clkdiv(dai, div_id, div);
8c6529db
LG
2087 else
2088 return -EINVAL;
2089}
2090EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2091
2092/**
2093 * snd_soc_dai_set_pll - configure DAI PLL.
2094 * @dai: DAI
2095 * @pll_id: DAI specific PLL ID
2096 * @freq_in: PLL input clock frequency in Hz
2097 * @freq_out: requested PLL output clock frequency in Hz
2098 *
2099 * Configures and enables PLL to generate output clock based on input clock.
2100 */
2101int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
2102 int pll_id, unsigned int freq_in, unsigned int freq_out)
2103{
6335d055
EM
2104 if (dai->ops->set_pll)
2105 return dai->ops->set_pll(dai, pll_id, freq_in, freq_out);
8c6529db
LG
2106 else
2107 return -EINVAL;
2108}
2109EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2110
2111/**
2112 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2113 * @dai: DAI
8c6529db
LG
2114 * @fmt: SND_SOC_DAIFMT_ format value.
2115 *
2116 * Configures the DAI hardware format and clocking.
2117 */
2118int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2119{
6335d055
EM
2120 if (dai->ops->set_fmt)
2121 return dai->ops->set_fmt(dai, fmt);
8c6529db
LG
2122 else
2123 return -EINVAL;
2124}
2125EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2126
2127/**
2128 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2129 * @dai: DAI
2130 * @mask: DAI specific mask representing used slots.
2131 * @slots: Number of slots in use.
2132 *
2133 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2134 * specific.
2135 */
2136int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2137 unsigned int mask, int slots)
2138{
6335d055
EM
2139 if (dai->ops->set_sysclk)
2140 return dai->ops->set_tdm_slot(dai, mask, slots);
8c6529db
LG
2141 else
2142 return -EINVAL;
2143}
2144EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2145
2146/**
2147 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2148 * @dai: DAI
2149 * @tristate: tristate enable
2150 *
2151 * Tristates the DAI so that others can use it.
2152 */
2153int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2154{
6335d055
EM
2155 if (dai->ops->set_sysclk)
2156 return dai->ops->set_tristate(dai, tristate);
8c6529db
LG
2157 else
2158 return -EINVAL;
2159}
2160EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2161
2162/**
2163 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2164 * @dai: DAI
2165 * @mute: mute enable
2166 *
2167 * Mutes the DAI DAC.
2168 */
2169int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2170{
6335d055
EM
2171 if (dai->ops->digital_mute)
2172 return dai->ops->digital_mute(dai, mute);
8c6529db
LG
2173 else
2174 return -EINVAL;
2175}
2176EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2177
c5af3a2e
MB
2178/**
2179 * snd_soc_register_card - Register a card with the ASoC core
2180 *
ac11a2b3 2181 * @card: Card to register
c5af3a2e
MB
2182 *
2183 * Note that currently this is an internal only function: it will be
2184 * exposed to machine drivers after further backporting of ASoC v2
2185 * registration APIs.
2186 */
2187static int snd_soc_register_card(struct snd_soc_card *card)
2188{
2189 if (!card->name || !card->dev)
2190 return -EINVAL;
2191
2192 INIT_LIST_HEAD(&card->list);
2193 card->instantiated = 0;
2194
2195 mutex_lock(&client_mutex);
2196 list_add(&card->list, &card_list);
435c5e25 2197 snd_soc_instantiate_cards();
c5af3a2e
MB
2198 mutex_unlock(&client_mutex);
2199
2200 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2201
2202 return 0;
2203}
2204
2205/**
2206 * snd_soc_unregister_card - Unregister a card with the ASoC core
2207 *
ac11a2b3 2208 * @card: Card to unregister
c5af3a2e
MB
2209 *
2210 * Note that currently this is an internal only function: it will be
2211 * exposed to machine drivers after further backporting of ASoC v2
2212 * registration APIs.
2213 */
2214static int snd_soc_unregister_card(struct snd_soc_card *card)
2215{
2216 mutex_lock(&client_mutex);
2217 list_del(&card->list);
2218 mutex_unlock(&client_mutex);
2219
2220 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2221
2222 return 0;
2223}
2224
6335d055
EM
2225static struct snd_soc_dai_ops null_dai_ops = {
2226};
2227
9115171a
MB
2228/**
2229 * snd_soc_register_dai - Register a DAI with the ASoC core
2230 *
ac11a2b3 2231 * @dai: DAI to register
9115171a
MB
2232 */
2233int snd_soc_register_dai(struct snd_soc_dai *dai)
2234{
2235 if (!dai->name)
2236 return -EINVAL;
2237
2238 /* The device should become mandatory over time */
2239 if (!dai->dev)
2240 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2241
6335d055
EM
2242 if (!dai->ops)
2243 dai->ops = &null_dai_ops;
2244
9115171a
MB
2245 INIT_LIST_HEAD(&dai->list);
2246
2247 mutex_lock(&client_mutex);
2248 list_add(&dai->list, &dai_list);
435c5e25 2249 snd_soc_instantiate_cards();
9115171a
MB
2250 mutex_unlock(&client_mutex);
2251
2252 pr_debug("Registered DAI '%s'\n", dai->name);
2253
2254 return 0;
2255}
2256EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2257
2258/**
2259 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2260 *
ac11a2b3 2261 * @dai: DAI to unregister
9115171a
MB
2262 */
2263void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2264{
2265 mutex_lock(&client_mutex);
2266 list_del(&dai->list);
2267 mutex_unlock(&client_mutex);
2268
2269 pr_debug("Unregistered DAI '%s'\n", dai->name);
2270}
2271EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2272
2273/**
2274 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2275 *
ac11a2b3
MB
2276 * @dai: Array of DAIs to register
2277 * @count: Number of DAIs
9115171a
MB
2278 */
2279int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2280{
2281 int i, ret;
2282
2283 for (i = 0; i < count; i++) {
2284 ret = snd_soc_register_dai(&dai[i]);
2285 if (ret != 0)
2286 goto err;
2287 }
2288
2289 return 0;
2290
2291err:
2292 for (i--; i >= 0; i--)
2293 snd_soc_unregister_dai(&dai[i]);
2294
2295 return ret;
2296}
2297EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2298
2299/**
2300 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2301 *
ac11a2b3
MB
2302 * @dai: Array of DAIs to unregister
2303 * @count: Number of DAIs
9115171a
MB
2304 */
2305void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2306{
2307 int i;
2308
2309 for (i = 0; i < count; i++)
2310 snd_soc_unregister_dai(&dai[i]);
2311}
2312EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2313
12a48a8c
MB
2314/**
2315 * snd_soc_register_platform - Register a platform with the ASoC core
2316 *
ac11a2b3 2317 * @platform: platform to register
12a48a8c
MB
2318 */
2319int snd_soc_register_platform(struct snd_soc_platform *platform)
2320{
2321 if (!platform->name)
2322 return -EINVAL;
2323
2324 INIT_LIST_HEAD(&platform->list);
2325
2326 mutex_lock(&client_mutex);
2327 list_add(&platform->list, &platform_list);
435c5e25 2328 snd_soc_instantiate_cards();
12a48a8c
MB
2329 mutex_unlock(&client_mutex);
2330
2331 pr_debug("Registered platform '%s'\n", platform->name);
2332
2333 return 0;
2334}
2335EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2336
2337/**
2338 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2339 *
ac11a2b3 2340 * @platform: platform to unregister
12a48a8c
MB
2341 */
2342void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2343{
2344 mutex_lock(&client_mutex);
2345 list_del(&platform->list);
2346 mutex_unlock(&client_mutex);
2347
2348 pr_debug("Unregistered platform '%s'\n", platform->name);
2349}
2350EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2351
0d0cf00a
MB
2352/**
2353 * snd_soc_register_codec - Register a codec with the ASoC core
2354 *
ac11a2b3 2355 * @codec: codec to register
0d0cf00a
MB
2356 */
2357int snd_soc_register_codec(struct snd_soc_codec *codec)
2358{
2359 if (!codec->name)
2360 return -EINVAL;
2361
2362 /* The device should become mandatory over time */
2363 if (!codec->dev)
2364 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2365
2366 INIT_LIST_HEAD(&codec->list);
2367
2368 mutex_lock(&client_mutex);
2369 list_add(&codec->list, &codec_list);
2370 snd_soc_instantiate_cards();
2371 mutex_unlock(&client_mutex);
2372
2373 pr_debug("Registered codec '%s'\n", codec->name);
2374
2375 return 0;
2376}
2377EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2378
2379/**
2380 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2381 *
ac11a2b3 2382 * @codec: codec to unregister
0d0cf00a
MB
2383 */
2384void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2385{
2386 mutex_lock(&client_mutex);
2387 list_del(&codec->list);
2388 mutex_unlock(&client_mutex);
2389
2390 pr_debug("Unregistered codec '%s'\n", codec->name);
2391}
2392EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2393
c9b3a40f 2394static int __init snd_soc_init(void)
db2a4165 2395{
384c89e2
MB
2396#ifdef CONFIG_DEBUG_FS
2397 debugfs_root = debugfs_create_dir("asoc", NULL);
2398 if (IS_ERR(debugfs_root) || !debugfs_root) {
2399 printk(KERN_WARNING
2400 "ASoC: Failed to create debugfs directory\n");
2401 debugfs_root = NULL;
2402 }
2403#endif
2404
db2a4165
FM
2405 return platform_driver_register(&soc_driver);
2406}
2407
7d8c16a6 2408static void __exit snd_soc_exit(void)
db2a4165 2409{
384c89e2
MB
2410#ifdef CONFIG_DEBUG_FS
2411 debugfs_remove_recursive(debugfs_root);
2412#endif
3ff3f64b 2413 platform_driver_unregister(&soc_driver);
db2a4165
FM
2414}
2415
2416module_init(snd_soc_init);
2417module_exit(snd_soc_exit);
2418
2419/* Module information */
d331124d 2420MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
db2a4165
FM
2421MODULE_DESCRIPTION("ALSA SoC Core");
2422MODULE_LICENSE("GPL");
8b45a209 2423MODULE_ALIAS("platform:soc-audio");