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