ASoC: Add card registration API
[GitHub/mt8127/android_kernel_alcatel_ttab.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);
48
49static int snd_soc_register_card(struct snd_soc_card *card);
50static int snd_soc_unregister_card(struct snd_soc_card *card);
51
db2a4165
FM
52/*
53 * This is a timeout to do a DAPM powerdown after a stream is closed().
54 * It can be used to eliminate pops between different playback streams, e.g.
55 * between two audio tracks.
56 */
57static int pmdown_time = 5000;
58module_param(pmdown_time, int, 0);
59MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
60
965ac42c
LG
61/*
62 * This function forces any delayed work to be queued and run.
63 */
64static int run_delayed_work(struct delayed_work *dwork)
65{
66 int ret;
67
68 /* cancel any work waiting to be queued. */
69 ret = cancel_delayed_work(dwork);
70
71 /* if there was any work waiting then we run it now and
72 * wait for it's completion */
73 if (ret) {
74 schedule_delayed_work(dwork, 0);
75 flush_scheduled_work();
76 }
77 return ret;
78}
79
db2a4165
FM
80#ifdef CONFIG_SND_SOC_AC97_BUS
81/* unregister ac97 codec */
82static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
83{
84 if (codec->ac97->dev.bus)
85 device_unregister(&codec->ac97->dev);
86 return 0;
87}
88
89/* stop no dev release warning */
90static void soc_ac97_device_release(struct device *dev){}
91
92/* register ac97 codec to bus */
93static int soc_ac97_dev_register(struct snd_soc_codec *codec)
94{
95 int err;
96
97 codec->ac97->dev.bus = &ac97_bus_type;
98 codec->ac97->dev.parent = NULL;
99 codec->ac97->dev.release = soc_ac97_device_release;
100
bb072bf0
KS
101 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
102 codec->card->number, 0, codec->name);
db2a4165
FM
103 err = device_register(&codec->ac97->dev);
104 if (err < 0) {
105 snd_printk(KERN_ERR "Can't register ac97 bus\n");
106 codec->ac97->dev.bus = NULL;
107 return err;
108 }
109 return 0;
110}
111#endif
112
db2a4165
FM
113/*
114 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
115 * then initialized and any private data can be allocated. This also calls
116 * startup for the cpu DAI, platform, machine and codec DAI.
117 */
118static int soc_pcm_open(struct snd_pcm_substream *substream)
119{
120 struct snd_soc_pcm_runtime *rtd = substream->private_data;
121 struct snd_soc_device *socdev = rtd->socdev;
87689d56 122 struct snd_soc_card *card = socdev->card;
db2a4165 123 struct snd_pcm_runtime *runtime = substream->runtime;
cb666e5b 124 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 125 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
126 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
127 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
128 int ret = 0;
129
130 mutex_lock(&pcm_mutex);
131
132 /* startup the audio subsystem */
cb666e5b 133 if (cpu_dai->ops.startup) {
dee89c4d 134 ret = cpu_dai->ops.startup(substream, cpu_dai);
db2a4165
FM
135 if (ret < 0) {
136 printk(KERN_ERR "asoc: can't open interface %s\n",
cb666e5b 137 cpu_dai->name);
db2a4165
FM
138 goto out;
139 }
140 }
141
142 if (platform->pcm_ops->open) {
143 ret = platform->pcm_ops->open(substream);
144 if (ret < 0) {
145 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
146 goto platform_err;
147 }
148 }
149
cb666e5b 150 if (codec_dai->ops.startup) {
dee89c4d 151 ret = codec_dai->ops.startup(substream, codec_dai);
db2a4165 152 if (ret < 0) {
cb666e5b
LG
153 printk(KERN_ERR "asoc: can't open codec %s\n",
154 codec_dai->name);
155 goto codec_dai_err;
db2a4165
FM
156 }
157 }
158
cb666e5b
LG
159 if (machine->ops && machine->ops->startup) {
160 ret = machine->ops->startup(substream);
db2a4165 161 if (ret < 0) {
cb666e5b
LG
162 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
163 goto machine_err;
db2a4165
FM
164 }
165 }
166
db2a4165
FM
167 /* Check that the codec and cpu DAI's are compatible */
168 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
169 runtime->hw.rate_min =
3ff3f64b
MB
170 max(codec_dai->playback.rate_min,
171 cpu_dai->playback.rate_min);
db2a4165 172 runtime->hw.rate_max =
3ff3f64b
MB
173 min(codec_dai->playback.rate_max,
174 cpu_dai->playback.rate_max);
db2a4165 175 runtime->hw.channels_min =
cb666e5b
LG
176 max(codec_dai->playback.channels_min,
177 cpu_dai->playback.channels_min);
db2a4165 178 runtime->hw.channels_max =
cb666e5b
LG
179 min(codec_dai->playback.channels_max,
180 cpu_dai->playback.channels_max);
181 runtime->hw.formats =
182 codec_dai->playback.formats & cpu_dai->playback.formats;
183 runtime->hw.rates =
184 codec_dai->playback.rates & cpu_dai->playback.rates;
db2a4165
FM
185 } else {
186 runtime->hw.rate_min =
3ff3f64b
MB
187 max(codec_dai->capture.rate_min,
188 cpu_dai->capture.rate_min);
db2a4165 189 runtime->hw.rate_max =
3ff3f64b
MB
190 min(codec_dai->capture.rate_max,
191 cpu_dai->capture.rate_max);
db2a4165 192 runtime->hw.channels_min =
cb666e5b
LG
193 max(codec_dai->capture.channels_min,
194 cpu_dai->capture.channels_min);
db2a4165 195 runtime->hw.channels_max =
cb666e5b
LG
196 min(codec_dai->capture.channels_max,
197 cpu_dai->capture.channels_max);
198 runtime->hw.formats =
199 codec_dai->capture.formats & cpu_dai->capture.formats;
200 runtime->hw.rates =
201 codec_dai->capture.rates & cpu_dai->capture.rates;
db2a4165
FM
202 }
203
204 snd_pcm_limit_hw_rates(runtime);
205 if (!runtime->hw.rates) {
206 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
cb666e5b
LG
207 codec_dai->name, cpu_dai->name);
208 goto machine_err;
db2a4165
FM
209 }
210 if (!runtime->hw.formats) {
211 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
cb666e5b
LG
212 codec_dai->name, cpu_dai->name);
213 goto machine_err;
db2a4165
FM
214 }
215 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
216 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
cb666e5b
LG
217 codec_dai->name, cpu_dai->name);
218 goto machine_err;
db2a4165
FM
219 }
220
f24368c2
MB
221 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
222 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
223 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
224 runtime->hw.channels_max);
225 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
226 runtime->hw.rate_max);
db2a4165 227
db2a4165 228 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
cb666e5b 229 cpu_dai->playback.active = codec_dai->playback.active = 1;
db2a4165 230 else
cb666e5b
LG
231 cpu_dai->capture.active = codec_dai->capture.active = 1;
232 cpu_dai->active = codec_dai->active = 1;
233 cpu_dai->runtime = runtime;
db2a4165
FM
234 socdev->codec->active++;
235 mutex_unlock(&pcm_mutex);
236 return 0;
237
cb666e5b 238machine_err:
db2a4165
FM
239 if (machine->ops && machine->ops->shutdown)
240 machine->ops->shutdown(substream);
241
cb666e5b 242codec_dai_err:
db2a4165
FM
243 if (platform->pcm_ops->close)
244 platform->pcm_ops->close(substream);
245
246platform_err:
cb666e5b 247 if (cpu_dai->ops.shutdown)
dee89c4d 248 cpu_dai->ops.shutdown(substream, cpu_dai);
db2a4165
FM
249out:
250 mutex_unlock(&pcm_mutex);
251 return ret;
252}
253
254/*
3a4fa0a2 255 * Power down the audio subsystem pmdown_time msecs after close is called.
db2a4165
FM
256 * This is to ensure there are no pops or clicks in between any music tracks
257 * due to DAPM power cycling.
258 */
4484bb2e 259static void close_delayed_work(struct work_struct *work)
db2a4165 260{
6308419a
MB
261 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
262 delayed_work.work);
263 struct snd_soc_device *socdev = card->socdev;
db2a4165 264 struct snd_soc_codec *codec = socdev->codec;
3c4b266f 265 struct snd_soc_dai *codec_dai;
db2a4165
FM
266 int i;
267
268 mutex_lock(&pcm_mutex);
3ff3f64b 269 for (i = 0; i < codec->num_dai; i++) {
db2a4165
FM
270 codec_dai = &codec->dai[i];
271
f24368c2
MB
272 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
273 codec_dai->playback.stream_name,
274 codec_dai->playback.active ? "active" : "inactive",
275 codec_dai->pop_wait ? "yes" : "no");
db2a4165
FM
276
277 /* are we waiting on this codec DAI stream */
278 if (codec_dai->pop_wait == 1) {
279
0be9898a 280 /* Reduce power if no longer active */
3c1c47e0 281 if (codec->active == 0) {
f24368c2
MB
282 pr_debug("pop wq D1 %s %s\n", codec->name,
283 codec_dai->playback.stream_name);
0be9898a
MB
284 snd_soc_dapm_set_bias_level(socdev,
285 SND_SOC_BIAS_PREPARE);
3c1c47e0
LG
286 }
287
db2a4165 288 codec_dai->pop_wait = 0;
0b4d221b
LG
289 snd_soc_dapm_stream_event(codec,
290 codec_dai->playback.stream_name,
db2a4165
FM
291 SND_SOC_DAPM_STREAM_STOP);
292
0be9898a 293 /* Fall into standby if no longer active */
db2a4165 294 if (codec->active == 0) {
f24368c2
MB
295 pr_debug("pop wq D3 %s %s\n", codec->name,
296 codec_dai->playback.stream_name);
0be9898a
MB
297 snd_soc_dapm_set_bias_level(socdev,
298 SND_SOC_BIAS_STANDBY);
db2a4165
FM
299 }
300 }
301 }
302 mutex_unlock(&pcm_mutex);
303}
304
305/*
306 * Called by ALSA when a PCM substream is closed. Private data can be
307 * freed here. The cpu DAI, codec DAI, machine and platform are also
308 * shutdown.
309 */
310static int soc_codec_close(struct snd_pcm_substream *substream)
311{
312 struct snd_soc_pcm_runtime *rtd = substream->private_data;
313 struct snd_soc_device *socdev = rtd->socdev;
6308419a 314 struct snd_soc_card *card = socdev->card;
cb666e5b 315 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 316 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
317 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
318 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
319 struct snd_soc_codec *codec = socdev->codec;
320
321 mutex_lock(&pcm_mutex);
322
323 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
cb666e5b 324 cpu_dai->playback.active = codec_dai->playback.active = 0;
db2a4165 325 else
cb666e5b 326 cpu_dai->capture.active = codec_dai->capture.active = 0;
db2a4165 327
cb666e5b
LG
328 if (codec_dai->playback.active == 0 &&
329 codec_dai->capture.active == 0) {
330 cpu_dai->active = codec_dai->active = 0;
db2a4165
FM
331 }
332 codec->active--;
333
6010b2da
MB
334 /* Muting the DAC suppresses artifacts caused during digital
335 * shutdown, for example from stopping clocks.
336 */
337 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
338 snd_soc_dai_digital_mute(codec_dai, 1);
339
cb666e5b 340 if (cpu_dai->ops.shutdown)
dee89c4d 341 cpu_dai->ops.shutdown(substream, cpu_dai);
db2a4165 342
cb666e5b 343 if (codec_dai->ops.shutdown)
dee89c4d 344 codec_dai->ops.shutdown(substream, codec_dai);
db2a4165
FM
345
346 if (machine->ops && machine->ops->shutdown)
347 machine->ops->shutdown(substream);
348
349 if (platform->pcm_ops->close)
350 platform->pcm_ops->close(substream);
cb666e5b 351 cpu_dai->runtime = NULL;
db2a4165
FM
352
353 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
354 /* start delayed pop wq here for playback streams */
cb666e5b 355 codec_dai->pop_wait = 1;
6308419a 356 schedule_delayed_work(&card->delayed_work,
db2a4165
FM
357 msecs_to_jiffies(pmdown_time));
358 } else {
359 /* capture streams can be powered down now */
cb666e5b 360 snd_soc_dapm_stream_event(codec,
0b4d221b
LG
361 codec_dai->capture.stream_name,
362 SND_SOC_DAPM_STREAM_STOP);
db2a4165 363
0b4d221b 364 if (codec->active == 0 && codec_dai->pop_wait == 0)
0be9898a
MB
365 snd_soc_dapm_set_bias_level(socdev,
366 SND_SOC_BIAS_STANDBY);
db2a4165
FM
367 }
368
369 mutex_unlock(&pcm_mutex);
370 return 0;
371}
372
373/*
374 * Called by ALSA when the PCM substream is prepared, can set format, sample
375 * rate, etc. This function is non atomic and can be called multiple times,
376 * it can refer to the runtime info.
377 */
378static int soc_pcm_prepare(struct snd_pcm_substream *substream)
379{
380 struct snd_soc_pcm_runtime *rtd = substream->private_data;
381 struct snd_soc_device *socdev = rtd->socdev;
6308419a 382 struct snd_soc_card *card = socdev->card;
cb666e5b 383 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 384 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
385 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
386 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
387 struct snd_soc_codec *codec = socdev->codec;
388 int ret = 0;
389
390 mutex_lock(&pcm_mutex);
cb666e5b
LG
391
392 if (machine->ops && machine->ops->prepare) {
393 ret = machine->ops->prepare(substream);
394 if (ret < 0) {
395 printk(KERN_ERR "asoc: machine prepare error\n");
396 goto out;
397 }
398 }
399
db2a4165
FM
400 if (platform->pcm_ops->prepare) {
401 ret = platform->pcm_ops->prepare(substream);
a71a468a
LG
402 if (ret < 0) {
403 printk(KERN_ERR "asoc: platform prepare error\n");
db2a4165 404 goto out;
a71a468a 405 }
db2a4165
FM
406 }
407
cb666e5b 408 if (codec_dai->ops.prepare) {
dee89c4d 409 ret = codec_dai->ops.prepare(substream, codec_dai);
a71a468a
LG
410 if (ret < 0) {
411 printk(KERN_ERR "asoc: codec DAI prepare error\n");
db2a4165 412 goto out;
a71a468a 413 }
db2a4165
FM
414 }
415
cb666e5b 416 if (cpu_dai->ops.prepare) {
dee89c4d 417 ret = cpu_dai->ops.prepare(substream, cpu_dai);
cb666e5b
LG
418 if (ret < 0) {
419 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
420 goto out;
421 }
422 }
db2a4165 423
d45f6219
MB
424 /* cancel any delayed stream shutdown that is pending */
425 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
426 codec_dai->pop_wait) {
427 codec_dai->pop_wait = 0;
6308419a 428 cancel_delayed_work(&card->delayed_work);
d45f6219 429 }
db2a4165 430
d45f6219
MB
431 /* do we need to power up codec */
432 if (codec->bias_level != SND_SOC_BIAS_ON) {
433 snd_soc_dapm_set_bias_level(socdev,
434 SND_SOC_BIAS_PREPARE);
db2a4165 435
d45f6219
MB
436 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
437 snd_soc_dapm_stream_event(codec,
cb666e5b 438 codec_dai->playback.stream_name,
db2a4165 439 SND_SOC_DAPM_STREAM_START);
d45f6219
MB
440 else
441 snd_soc_dapm_stream_event(codec,
cb666e5b 442 codec_dai->capture.stream_name,
db2a4165
FM
443 SND_SOC_DAPM_STREAM_START);
444
d45f6219
MB
445 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
446 snd_soc_dai_digital_mute(codec_dai, 0);
db2a4165 447
d45f6219
MB
448 } else {
449 /* codec already powered - power on widgets */
450 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
451 snd_soc_dapm_stream_event(codec,
cb666e5b 452 codec_dai->playback.stream_name,
db2a4165 453 SND_SOC_DAPM_STREAM_START);
d45f6219
MB
454 else
455 snd_soc_dapm_stream_event(codec,
cb666e5b 456 codec_dai->capture.stream_name,
db2a4165 457 SND_SOC_DAPM_STREAM_START);
8c6529db 458
d45f6219 459 snd_soc_dai_digital_mute(codec_dai, 0);
db2a4165
FM
460 }
461
462out:
463 mutex_unlock(&pcm_mutex);
464 return ret;
465}
466
467/*
468 * Called by ALSA when the hardware params are set by application. This
469 * function can also be called multiple times and can allocate buffers
470 * (using snd_pcm_lib_* ). It's non-atomic.
471 */
472static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
473 struct snd_pcm_hw_params *params)
474{
475 struct snd_soc_pcm_runtime *rtd = substream->private_data;
476 struct snd_soc_device *socdev = rtd->socdev;
cb666e5b 477 struct snd_soc_dai_link *machine = rtd->dai;
87689d56
MB
478 struct snd_soc_card *card = socdev->card;
479 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
480 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
481 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
482 int ret = 0;
483
484 mutex_lock(&pcm_mutex);
485
cb666e5b
LG
486 if (machine->ops && machine->ops->hw_params) {
487 ret = machine->ops->hw_params(substream, params);
488 if (ret < 0) {
489 printk(KERN_ERR "asoc: machine hw_params failed\n");
db2a4165 490 goto out;
cb666e5b 491 }
db2a4165
FM
492 }
493
cb666e5b 494 if (codec_dai->ops.hw_params) {
dee89c4d 495 ret = codec_dai->ops.hw_params(substream, params, codec_dai);
db2a4165
FM
496 if (ret < 0) {
497 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
cb666e5b
LG
498 codec_dai->name);
499 goto codec_err;
db2a4165
FM
500 }
501 }
502
cb666e5b 503 if (cpu_dai->ops.hw_params) {
dee89c4d 504 ret = cpu_dai->ops.hw_params(substream, params, cpu_dai);
db2a4165 505 if (ret < 0) {
3ff3f64b 506 printk(KERN_ERR "asoc: interface %s hw params failed\n",
cb666e5b 507 cpu_dai->name);
db2a4165
FM
508 goto interface_err;
509 }
510 }
511
512 if (platform->pcm_ops->hw_params) {
513 ret = platform->pcm_ops->hw_params(substream, params);
514 if (ret < 0) {
3ff3f64b 515 printk(KERN_ERR "asoc: platform %s hw params failed\n",
db2a4165
FM
516 platform->name);
517 goto platform_err;
518 }
519 }
520
db2a4165
FM
521out:
522 mutex_unlock(&pcm_mutex);
523 return ret;
524
db2a4165 525platform_err:
cb666e5b 526 if (cpu_dai->ops.hw_free)
dee89c4d 527 cpu_dai->ops.hw_free(substream, cpu_dai);
db2a4165
FM
528
529interface_err:
cb666e5b 530 if (codec_dai->ops.hw_free)
dee89c4d 531 codec_dai->ops.hw_free(substream, codec_dai);
cb666e5b
LG
532
533codec_err:
3ff3f64b 534 if (machine->ops && machine->ops->hw_free)
cb666e5b 535 machine->ops->hw_free(substream);
db2a4165
FM
536
537 mutex_unlock(&pcm_mutex);
538 return ret;
539}
540
541/*
542 * Free's resources allocated by hw_params, can be called multiple times
543 */
544static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
545{
546 struct snd_soc_pcm_runtime *rtd = substream->private_data;
547 struct snd_soc_device *socdev = rtd->socdev;
cb666e5b 548 struct snd_soc_dai_link *machine = rtd->dai;
87689d56
MB
549 struct snd_soc_card *card = socdev->card;
550 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
551 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
552 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165 553 struct snd_soc_codec *codec = socdev->codec;
db2a4165
FM
554
555 mutex_lock(&pcm_mutex);
556
557 /* apply codec digital mute */
8c6529db
LG
558 if (!codec->active)
559 snd_soc_dai_digital_mute(codec_dai, 1);
db2a4165
FM
560
561 /* free any machine hw params */
562 if (machine->ops && machine->ops->hw_free)
563 machine->ops->hw_free(substream);
564
565 /* free any DMA resources */
566 if (platform->pcm_ops->hw_free)
567 platform->pcm_ops->hw_free(substream);
568
569 /* now free hw params for the DAI's */
cb666e5b 570 if (codec_dai->ops.hw_free)
dee89c4d 571 codec_dai->ops.hw_free(substream, codec_dai);
db2a4165 572
cb666e5b 573 if (cpu_dai->ops.hw_free)
dee89c4d 574 cpu_dai->ops.hw_free(substream, cpu_dai);
db2a4165
FM
575
576 mutex_unlock(&pcm_mutex);
577 return 0;
578}
579
580static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
581{
582 struct snd_soc_pcm_runtime *rtd = substream->private_data;
583 struct snd_soc_device *socdev = rtd->socdev;
87689d56 584 struct snd_soc_card *card= socdev->card;
cb666e5b 585 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 586 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
587 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
588 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
589 int ret;
590
cb666e5b 591 if (codec_dai->ops.trigger) {
dee89c4d 592 ret = codec_dai->ops.trigger(substream, cmd, codec_dai);
db2a4165
FM
593 if (ret < 0)
594 return ret;
595 }
596
597 if (platform->pcm_ops->trigger) {
598 ret = platform->pcm_ops->trigger(substream, cmd);
599 if (ret < 0)
600 return ret;
601 }
602
cb666e5b 603 if (cpu_dai->ops.trigger) {
dee89c4d 604 ret = cpu_dai->ops.trigger(substream, cmd, cpu_dai);
db2a4165
FM
605 if (ret < 0)
606 return ret;
607 }
608 return 0;
609}
610
611/* ASoC PCM operations */
612static struct snd_pcm_ops soc_pcm_ops = {
613 .open = soc_pcm_open,
614 .close = soc_codec_close,
615 .hw_params = soc_pcm_hw_params,
616 .hw_free = soc_pcm_hw_free,
617 .prepare = soc_pcm_prepare,
618 .trigger = soc_pcm_trigger,
619};
620
621#ifdef CONFIG_PM
622/* powers down audio subsystem for suspend */
623static int soc_suspend(struct platform_device *pdev, pm_message_t state)
624{
3ff3f64b 625 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
87506549 626 struct snd_soc_card *card = socdev->card;
87689d56 627 struct snd_soc_platform *platform = card->platform;
3ff3f64b 628 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
db2a4165
FM
629 struct snd_soc_codec *codec = socdev->codec;
630 int i;
631
6ed25978
AG
632 /* Due to the resume being scheduled into a workqueue we could
633 * suspend before that's finished - wait for it to complete.
634 */
635 snd_power_lock(codec->card);
636 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
637 snd_power_unlock(codec->card);
638
639 /* we're going to block userspace touching us until resume completes */
640 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
641
db2a4165 642 /* mute any active DAC's */
dee89c4d
MB
643 for (i = 0; i < card->num_links; i++) {
644 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
645 if (dai->ops.digital_mute && dai->playback.active)
646 dai->ops.digital_mute(dai, 1);
db2a4165
FM
647 }
648
4ccab3e7 649 /* suspend all pcms */
87506549
MB
650 for (i = 0; i < card->num_links; i++)
651 snd_pcm_suspend_all(card->dai_link[i].pcm);
4ccab3e7 652
87506549
MB
653 if (card->suspend_pre)
654 card->suspend_pre(pdev, state);
db2a4165 655
87506549
MB
656 for (i = 0; i < card->num_links; i++) {
657 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 658 if (cpu_dai->suspend && !cpu_dai->ac97_control)
dc7d7b83 659 cpu_dai->suspend(cpu_dai);
db2a4165 660 if (platform->suspend)
07c84d04 661 platform->suspend(cpu_dai);
db2a4165
FM
662 }
663
664 /* close any waiting streams and save state */
6308419a 665 run_delayed_work(&card->delayed_work);
0be9898a 666 codec->suspend_bias_level = codec->bias_level;
db2a4165 667
3ff3f64b 668 for (i = 0; i < codec->num_dai; i++) {
db2a4165
FM
669 char *stream = codec->dai[i].playback.stream_name;
670 if (stream != NULL)
671 snd_soc_dapm_stream_event(codec, stream,
672 SND_SOC_DAPM_STREAM_SUSPEND);
673 stream = codec->dai[i].capture.stream_name;
674 if (stream != NULL)
675 snd_soc_dapm_stream_event(codec, stream,
676 SND_SOC_DAPM_STREAM_SUSPEND);
677 }
678
679 if (codec_dev->suspend)
680 codec_dev->suspend(pdev, state);
681
87506549
MB
682 for (i = 0; i < card->num_links; i++) {
683 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 684 if (cpu_dai->suspend && cpu_dai->ac97_control)
dc7d7b83 685 cpu_dai->suspend(cpu_dai);
db2a4165
FM
686 }
687
87506549
MB
688 if (card->suspend_post)
689 card->suspend_post(pdev, state);
db2a4165
FM
690
691 return 0;
692}
693
6ed25978
AG
694/* deferred resume work, so resume can complete before we finished
695 * setting our codec back up, which can be very slow on I2C
696 */
697static void soc_resume_deferred(struct work_struct *work)
db2a4165 698{
6308419a
MB
699 struct snd_soc_card *card = container_of(work,
700 struct snd_soc_card,
701 deferred_resume_work);
702 struct snd_soc_device *socdev = card->socdev;
87689d56 703 struct snd_soc_platform *platform = card->platform;
3ff3f64b 704 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
db2a4165 705 struct snd_soc_codec *codec = socdev->codec;
6ed25978 706 struct platform_device *pdev = to_platform_device(socdev->dev);
db2a4165
FM
707 int i;
708
6ed25978
AG
709 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
710 * so userspace apps are blocked from touching us
711 */
712
fde22f27 713 dev_dbg(socdev->dev, "starting resume work\n");
6ed25978 714
87506549
MB
715 if (card->resume_pre)
716 card->resume_pre(pdev);
db2a4165 717
87506549
MB
718 for (i = 0; i < card->num_links; i++) {
719 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 720 if (cpu_dai->resume && cpu_dai->ac97_control)
dc7d7b83 721 cpu_dai->resume(cpu_dai);
db2a4165
FM
722 }
723
724 if (codec_dev->resume)
725 codec_dev->resume(pdev);
726
3ff3f64b
MB
727 for (i = 0; i < codec->num_dai; i++) {
728 char *stream = codec->dai[i].playback.stream_name;
db2a4165
FM
729 if (stream != NULL)
730 snd_soc_dapm_stream_event(codec, stream,
731 SND_SOC_DAPM_STREAM_RESUME);
732 stream = codec->dai[i].capture.stream_name;
733 if (stream != NULL)
734 snd_soc_dapm_stream_event(codec, stream,
735 SND_SOC_DAPM_STREAM_RESUME);
736 }
737
3ff3f64b 738 /* unmute any active DACs */
dee89c4d
MB
739 for (i = 0; i < card->num_links; i++) {
740 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
741 if (dai->ops.digital_mute && dai->playback.active)
742 dai->ops.digital_mute(dai, 0);
db2a4165
FM
743 }
744
87506549
MB
745 for (i = 0; i < card->num_links; i++) {
746 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 747 if (cpu_dai->resume && !cpu_dai->ac97_control)
dc7d7b83 748 cpu_dai->resume(cpu_dai);
db2a4165 749 if (platform->resume)
07c84d04 750 platform->resume(cpu_dai);
db2a4165
FM
751 }
752
87506549
MB
753 if (card->resume_post)
754 card->resume_post(pdev);
db2a4165 755
fde22f27 756 dev_dbg(socdev->dev, "resume work completed\n");
6ed25978
AG
757
758 /* userspace can access us now we are back as we were before */
759 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
760}
761
762/* powers up audio subsystem after a suspend */
763static int soc_resume(struct platform_device *pdev)
764{
765 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
6308419a 766 struct snd_soc_card *card = socdev->card;
6ed25978 767
fde22f27 768 dev_dbg(socdev->dev, "scheduling resume work\n");
6ed25978 769
6308419a 770 if (!schedule_work(&card->deferred_resume_work))
fde22f27 771 dev_err(socdev->dev, "resume work item may be lost\n");
6ed25978 772
db2a4165
FM
773 return 0;
774}
775
776#else
777#define soc_suspend NULL
778#define soc_resume NULL
779#endif
780
781/* probes a new socdev */
782static int soc_probe(struct platform_device *pdev)
783{
784 int ret = 0, i;
785 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
87506549 786 struct snd_soc_card *card = socdev->card;
87689d56 787 struct snd_soc_platform *platform = card->platform;
db2a4165
FM
788 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
789
6308419a
MB
790 /* Bodge while we push things out of socdev */
791 card->socdev = socdev;
792
c5af3a2e
MB
793 /* Bodge while we unpick instantiation */
794 card->dev = &pdev->dev;
795 ret = snd_soc_register_card(card);
796 if (ret != 0) {
797 dev_err(&pdev->dev, "Failed to register card\n");
798 return ret;
799 }
800
87506549
MB
801 if (card->probe) {
802 ret = card->probe(pdev);
3ff3f64b 803 if (ret < 0)
db2a4165
FM
804 return ret;
805 }
806
87506549
MB
807 for (i = 0; i < card->num_links; i++) {
808 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 809 if (cpu_dai->probe) {
bdb92876 810 ret = cpu_dai->probe(pdev, cpu_dai);
3ff3f64b 811 if (ret < 0)
db2a4165
FM
812 goto cpu_dai_err;
813 }
814 }
815
816 if (codec_dev->probe) {
817 ret = codec_dev->probe(pdev);
3ff3f64b 818 if (ret < 0)
db2a4165
FM
819 goto cpu_dai_err;
820 }
821
822 if (platform->probe) {
823 ret = platform->probe(pdev);
3ff3f64b 824 if (ret < 0)
db2a4165
FM
825 goto platform_err;
826 }
827
828 /* DAPM stream work */
6308419a 829 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
1301a964 830#ifdef CONFIG_PM
6ed25978 831 /* deferred resume work */
6308419a 832 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1301a964 833#endif
6ed25978 834
db2a4165
FM
835 return 0;
836
db2a4165
FM
837platform_err:
838 if (codec_dev->remove)
839 codec_dev->remove(pdev);
840
841cpu_dai_err:
18b9b3d9 842 for (i--; i >= 0; i--) {
87506549 843 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 844 if (cpu_dai->remove)
bdb92876 845 cpu_dai->remove(pdev, cpu_dai);
db2a4165
FM
846 }
847
87506549
MB
848 if (card->remove)
849 card->remove(pdev);
db2a4165
FM
850
851 return ret;
852}
853
854/* removes a socdev */
855static int soc_remove(struct platform_device *pdev)
856{
857 int i;
858 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
87506549 859 struct snd_soc_card *card = socdev->card;
87689d56 860 struct snd_soc_platform *platform = card->platform;
db2a4165
FM
861 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
862
6308419a 863 run_delayed_work(&card->delayed_work);
965ac42c 864
db2a4165
FM
865 if (platform->remove)
866 platform->remove(pdev);
867
868 if (codec_dev->remove)
869 codec_dev->remove(pdev);
870
87506549
MB
871 for (i = 0; i < card->num_links; i++) {
872 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 873 if (cpu_dai->remove)
bdb92876 874 cpu_dai->remove(pdev, cpu_dai);
db2a4165
FM
875 }
876
87506549
MB
877 if (card->remove)
878 card->remove(pdev);
db2a4165 879
c5af3a2e
MB
880 snd_soc_unregister_card(card);
881
db2a4165
FM
882 return 0;
883}
884
885/* ASoC platform driver */
886static struct platform_driver soc_driver = {
887 .driver = {
888 .name = "soc-audio",
8b45a209 889 .owner = THIS_MODULE,
db2a4165
FM
890 },
891 .probe = soc_probe,
892 .remove = soc_remove,
893 .suspend = soc_suspend,
894 .resume = soc_resume,
895};
896
897/* create a new pcm */
898static int soc_new_pcm(struct snd_soc_device *socdev,
899 struct snd_soc_dai_link *dai_link, int num)
900{
901 struct snd_soc_codec *codec = socdev->codec;
87689d56
MB
902 struct snd_soc_card *card = socdev->card;
903 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
904 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
905 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
db2a4165
FM
906 struct snd_soc_pcm_runtime *rtd;
907 struct snd_pcm *pcm;
908 char new_name[64];
909 int ret = 0, playback = 0, capture = 0;
910
911 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
912 if (rtd == NULL)
913 return -ENOMEM;
cb666e5b
LG
914
915 rtd->dai = dai_link;
db2a4165 916 rtd->socdev = socdev;
cb666e5b 917 codec_dai->codec = socdev->codec;
db2a4165
FM
918
919 /* check client and interface hw capabilities */
3ba9e10a
MB
920 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
921 num);
db2a4165
FM
922
923 if (codec_dai->playback.channels_min)
924 playback = 1;
925 if (codec_dai->capture.channels_min)
926 capture = 1;
927
928 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
929 capture, &pcm);
930 if (ret < 0) {
3ff3f64b
MB
931 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
932 codec->name);
db2a4165
FM
933 kfree(rtd);
934 return ret;
935 }
936
4ccab3e7 937 dai_link->pcm = pcm;
db2a4165 938 pcm->private_data = rtd;
87689d56
MB
939 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
940 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
941 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
942 soc_pcm_ops.copy = platform->pcm_ops->copy;
943 soc_pcm_ops.silence = platform->pcm_ops->silence;
944 soc_pcm_ops.ack = platform->pcm_ops->ack;
945 soc_pcm_ops.page = platform->pcm_ops->page;
db2a4165
FM
946
947 if (playback)
948 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
949
950 if (capture)
951 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
952
87689d56 953 ret = platform->pcm_new(codec->card, codec_dai, pcm);
db2a4165
FM
954 if (ret < 0) {
955 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
956 kfree(rtd);
957 return ret;
958 }
959
87689d56 960 pcm->private_free = platform->pcm_free;
db2a4165
FM
961 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
962 cpu_dai->name);
963 return ret;
964}
965
966/* codec register dump */
12ef193d 967static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
db2a4165 968{
db2a4165
FM
969 struct snd_soc_codec *codec = devdata->codec;
970 int i, step = 1, count = 0;
971
972 if (!codec->reg_cache_size)
973 return 0;
974
975 if (codec->reg_cache_step)
976 step = codec->reg_cache_step;
977
978 count += sprintf(buf, "%s registers\n", codec->name);
58cd33c0
MB
979 for (i = 0; i < codec->reg_cache_size; i += step) {
980 count += sprintf(buf + count, "%2x: ", i);
981 if (count >= PAGE_SIZE - 1)
982 break;
983
984 if (codec->display_register)
985 count += codec->display_register(codec, buf + count,
986 PAGE_SIZE - count, i);
987 else
988 count += snprintf(buf + count, PAGE_SIZE - count,
989 "%4x", codec->read(codec, i));
990
991 if (count >= PAGE_SIZE - 1)
992 break;
993
994 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
995 if (count >= PAGE_SIZE - 1)
996 break;
997 }
998
999 /* Truncate count; min() would cause a warning */
1000 if (count >= PAGE_SIZE)
1001 count = PAGE_SIZE - 1;
db2a4165
FM
1002
1003 return count;
1004}
12ef193d
TK
1005static ssize_t codec_reg_show(struct device *dev,
1006 struct device_attribute *attr, char *buf)
1007{
1008 struct snd_soc_device *devdata = dev_get_drvdata(dev);
1009 return soc_codec_reg_show(devdata, buf);
1010}
1011
db2a4165
FM
1012static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1013
12ef193d
TK
1014#ifdef CONFIG_DEBUG_FS
1015static int codec_reg_open_file(struct inode *inode, struct file *file)
1016{
1017 file->private_data = inode->i_private;
1018 return 0;
1019}
1020
1021static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1022 size_t count, loff_t *ppos)
1023{
1024 ssize_t ret;
384c89e2
MB
1025 struct snd_soc_codec *codec = file->private_data;
1026 struct device *card_dev = codec->card->dev;
1027 struct snd_soc_device *devdata = card_dev->driver_data;
12ef193d
TK
1028 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1029 if (!buf)
1030 return -ENOMEM;
1031 ret = soc_codec_reg_show(devdata, buf);
1032 if (ret >= 0)
1033 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1034 kfree(buf);
1035 return ret;
1036}
1037
1038static ssize_t codec_reg_write_file(struct file *file,
1039 const char __user *user_buf, size_t count, loff_t *ppos)
1040{
1041 char buf[32];
1042 int buf_size;
1043 char *start = buf;
1044 unsigned long reg, value;
1045 int step = 1;
384c89e2 1046 struct snd_soc_codec *codec = file->private_data;
12ef193d
TK
1047
1048 buf_size = min(count, (sizeof(buf)-1));
1049 if (copy_from_user(buf, user_buf, buf_size))
1050 return -EFAULT;
1051 buf[buf_size] = 0;
1052
1053 if (codec->reg_cache_step)
1054 step = codec->reg_cache_step;
1055
1056 while (*start == ' ')
1057 start++;
1058 reg = simple_strtoul(start, &start, 16);
1059 if ((reg >= codec->reg_cache_size) || (reg % step))
1060 return -EINVAL;
1061 while (*start == ' ')
1062 start++;
1063 if (strict_strtoul(start, 16, &value))
1064 return -EINVAL;
1065 codec->write(codec, reg, value);
1066 return buf_size;
1067}
1068
1069static const struct file_operations codec_reg_fops = {
1070 .open = codec_reg_open_file,
1071 .read = codec_reg_read_file,
1072 .write = codec_reg_write_file,
1073};
1074
384c89e2 1075static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
12ef193d 1076{
384c89e2
MB
1077 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
1078 debugfs_root, codec,
1079 &codec_reg_fops);
1080 if (!codec->debugfs_reg)
1081 printk(KERN_WARNING
1082 "ASoC: Failed to create codec register debugfs file\n");
1083
1084 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
1085 debugfs_root,
1086 &codec->pop_time);
1087 if (!codec->debugfs_pop_time)
1088 printk(KERN_WARNING
1089 "Failed to create pop time debugfs file\n");
12ef193d
TK
1090}
1091
384c89e2 1092static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
12ef193d 1093{
384c89e2
MB
1094 debugfs_remove(codec->debugfs_pop_time);
1095 debugfs_remove(codec->debugfs_reg);
12ef193d
TK
1096}
1097
1098#else
1099
384c89e2 1100static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
12ef193d
TK
1101{
1102}
1103
384c89e2 1104static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
12ef193d
TK
1105{
1106}
1107#endif
1108
db2a4165
FM
1109/**
1110 * snd_soc_new_ac97_codec - initailise AC97 device
1111 * @codec: audio codec
1112 * @ops: AC97 bus operations
1113 * @num: AC97 codec number
1114 *
1115 * Initialises AC97 codec resources for use by ad-hoc devices only.
1116 */
1117int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1118 struct snd_ac97_bus_ops *ops, int num)
1119{
1120 mutex_lock(&codec->mutex);
1121
1122 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1123 if (codec->ac97 == NULL) {
1124 mutex_unlock(&codec->mutex);
1125 return -ENOMEM;
1126 }
1127
1128 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1129 if (codec->ac97->bus == NULL) {
1130 kfree(codec->ac97);
1131 codec->ac97 = NULL;
1132 mutex_unlock(&codec->mutex);
1133 return -ENOMEM;
1134 }
1135
1136 codec->ac97->bus->ops = ops;
1137 codec->ac97->num = num;
1138 mutex_unlock(&codec->mutex);
1139 return 0;
1140}
1141EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1142
1143/**
1144 * snd_soc_free_ac97_codec - free AC97 codec device
1145 * @codec: audio codec
1146 *
1147 * Frees AC97 codec device resources.
1148 */
1149void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1150{
1151 mutex_lock(&codec->mutex);
1152 kfree(codec->ac97->bus);
1153 kfree(codec->ac97);
1154 codec->ac97 = NULL;
1155 mutex_unlock(&codec->mutex);
1156}
1157EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1158
1159/**
1160 * snd_soc_update_bits - update codec register bits
1161 * @codec: audio codec
1162 * @reg: codec register
1163 * @mask: register mask
1164 * @value: new value
1165 *
1166 * Writes new register value.
1167 *
1168 * Returns 1 for change else 0.
1169 */
1170int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1171 unsigned short mask, unsigned short value)
1172{
1173 int change;
1174 unsigned short old, new;
1175
1176 mutex_lock(&io_mutex);
1177 old = snd_soc_read(codec, reg);
1178 new = (old & ~mask) | value;
1179 change = old != new;
1180 if (change)
1181 snd_soc_write(codec, reg, new);
1182
1183 mutex_unlock(&io_mutex);
1184 return change;
1185}
1186EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1187
1188/**
1189 * snd_soc_test_bits - test register for change
1190 * @codec: audio codec
1191 * @reg: codec register
1192 * @mask: register mask
1193 * @value: new value
1194 *
1195 * Tests a register with a new value and checks if the new value is
1196 * different from the old value.
1197 *
1198 * Returns 1 for change else 0.
1199 */
1200int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1201 unsigned short mask, unsigned short value)
1202{
1203 int change;
1204 unsigned short old, new;
1205
1206 mutex_lock(&io_mutex);
1207 old = snd_soc_read(codec, reg);
1208 new = (old & ~mask) | value;
1209 change = old != new;
1210 mutex_unlock(&io_mutex);
1211
1212 return change;
1213}
1214EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1215
db2a4165
FM
1216/**
1217 * snd_soc_new_pcms - create new sound card and pcms
1218 * @socdev: the SoC audio device
1219 *
1220 * Create a new sound card based upon the codec and interface pcms.
1221 *
1222 * Returns 0 for success, else error.
1223 */
bc7320c5 1224int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
db2a4165
FM
1225{
1226 struct snd_soc_codec *codec = socdev->codec;
87506549 1227 struct snd_soc_card *card = socdev->card;
db2a4165
FM
1228 int ret = 0, i;
1229
1230 mutex_lock(&codec->mutex);
1231
1232 /* register a sound card */
1233 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1234 if (!codec->card) {
1235 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1236 codec->name);
1237 mutex_unlock(&codec->mutex);
1238 return -ENODEV;
1239 }
1240
1241 codec->card->dev = socdev->dev;
1242 codec->card->private_data = codec;
1243 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1244
1245 /* create the pcms */
87506549
MB
1246 for (i = 0; i < card->num_links; i++) {
1247 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
db2a4165
FM
1248 if (ret < 0) {
1249 printk(KERN_ERR "asoc: can't create pcm %s\n",
87506549 1250 card->dai_link[i].stream_name);
db2a4165
FM
1251 mutex_unlock(&codec->mutex);
1252 return ret;
1253 }
1254 }
1255
1256 mutex_unlock(&codec->mutex);
1257 return ret;
1258}
1259EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1260
1261/**
968a6025 1262 * snd_soc_init_card - register sound card
db2a4165
FM
1263 * @socdev: the SoC audio device
1264 *
1265 * Register a SoC sound card. Also registers an AC97 device if the
1266 * codec is AC97 for ad hoc devices.
1267 *
1268 * Returns 0 for success, else error.
1269 */
968a6025 1270int snd_soc_init_card(struct snd_soc_device *socdev)
db2a4165
FM
1271{
1272 struct snd_soc_codec *codec = socdev->codec;
87506549 1273 struct snd_soc_card *card = socdev->card;
12e74f7d 1274 int ret = 0, i, ac97 = 0, err = 0;
db2a4165 1275
87506549
MB
1276 for (i = 0; i < card->num_links; i++) {
1277 if (card->dai_link[i].init) {
1278 err = card->dai_link[i].init(codec);
12e74f7d
LG
1279 if (err < 0) {
1280 printk(KERN_ERR "asoc: failed to init %s\n",
87506549 1281 card->dai_link[i].stream_name);
12e74f7d
LG
1282 continue;
1283 }
1284 }
3ba9e10a 1285 if (card->dai_link[i].codec_dai->ac97_control)
db2a4165
FM
1286 ac97 = 1;
1287 }
1288 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
87506549 1289 "%s", card->name);
db2a4165 1290 snprintf(codec->card->longname, sizeof(codec->card->longname),
87506549 1291 "%s (%s)", card->name, codec->name);
db2a4165
FM
1292
1293 ret = snd_card_register(codec->card);
1294 if (ret < 0) {
3ff3f64b 1295 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
db2a4165 1296 codec->name);
12e74f7d 1297 goto out;
db2a4165
FM
1298 }
1299
08c8efe6 1300 mutex_lock(&codec->mutex);
db2a4165 1301#ifdef CONFIG_SND_SOC_AC97_BUS
12e74f7d
LG
1302 if (ac97) {
1303 ret = soc_ac97_dev_register(codec);
1304 if (ret < 0) {
1305 printk(KERN_ERR "asoc: AC97 device register failed\n");
1306 snd_card_free(codec->card);
08c8efe6 1307 mutex_unlock(&codec->mutex);
12e74f7d
LG
1308 goto out;
1309 }
1310 }
db2a4165
FM
1311#endif
1312
12e74f7d
LG
1313 err = snd_soc_dapm_sys_add(socdev->dev);
1314 if (err < 0)
1315 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1316
1317 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1318 if (err < 0)
3ff3f64b 1319 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
08c8efe6 1320
384c89e2 1321 soc_init_codec_debugfs(socdev->codec);
db2a4165 1322 mutex_unlock(&codec->mutex);
08c8efe6
MB
1323
1324out:
db2a4165
FM
1325 return ret;
1326}
968a6025 1327EXPORT_SYMBOL_GPL(snd_soc_init_card);
db2a4165
FM
1328
1329/**
1330 * snd_soc_free_pcms - free sound card and pcms
1331 * @socdev: the SoC audio device
1332 *
1333 * Frees sound card and pcms associated with the socdev.
1334 * Also unregister the codec if it is an AC97 device.
1335 */
1336void snd_soc_free_pcms(struct snd_soc_device *socdev)
1337{
1338 struct snd_soc_codec *codec = socdev->codec;
a68660e0 1339#ifdef CONFIG_SND_SOC_AC97_BUS
3c4b266f 1340 struct snd_soc_dai *codec_dai;
a68660e0
LG
1341 int i;
1342#endif
db2a4165
FM
1343
1344 mutex_lock(&codec->mutex);
384c89e2 1345 soc_cleanup_codec_debugfs(socdev->codec);
db2a4165 1346#ifdef CONFIG_SND_SOC_AC97_BUS
3ff3f64b 1347 for (i = 0; i < codec->num_dai; i++) {
a68660e0 1348 codec_dai = &codec->dai[i];
3ba9e10a 1349 if (codec_dai->ac97_control && codec->ac97) {
a68660e0
LG
1350 soc_ac97_dev_unregister(codec);
1351 goto free_card;
1352 }
1353 }
1354free_card:
db2a4165
FM
1355#endif
1356
1357 if (codec->card)
1358 snd_card_free(codec->card);
1359 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1360 mutex_unlock(&codec->mutex);
1361}
1362EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1363
1364/**
1365 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1366 * @substream: the pcm substream
1367 * @hw: the hardware parameters
1368 *
1369 * Sets the substream runtime hardware parameters.
1370 */
1371int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1372 const struct snd_pcm_hardware *hw)
1373{
1374 struct snd_pcm_runtime *runtime = substream->runtime;
1375 runtime->hw.info = hw->info;
1376 runtime->hw.formats = hw->formats;
1377 runtime->hw.period_bytes_min = hw->period_bytes_min;
1378 runtime->hw.period_bytes_max = hw->period_bytes_max;
1379 runtime->hw.periods_min = hw->periods_min;
1380 runtime->hw.periods_max = hw->periods_max;
1381 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1382 runtime->hw.fifo_size = hw->fifo_size;
1383 return 0;
1384}
1385EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1386
1387/**
1388 * snd_soc_cnew - create new control
1389 * @_template: control template
1390 * @data: control private data
1391 * @lnng_name: control long name
1392 *
1393 * Create a new mixer control from a template control.
1394 *
1395 * Returns 0 for success, else error.
1396 */
1397struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1398 void *data, char *long_name)
1399{
1400 struct snd_kcontrol_new template;
1401
1402 memcpy(&template, _template, sizeof(template));
1403 if (long_name)
1404 template.name = long_name;
db2a4165
FM
1405 template.index = 0;
1406
1407 return snd_ctl_new1(&template, data);
1408}
1409EXPORT_SYMBOL_GPL(snd_soc_cnew);
1410
1411/**
1412 * snd_soc_info_enum_double - enumerated double mixer info callback
1413 * @kcontrol: mixer control
1414 * @uinfo: control element information
1415 *
1416 * Callback to provide information about a double enumerated
1417 * mixer control.
1418 *
1419 * Returns 0 for success.
1420 */
1421int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1422 struct snd_ctl_elem_info *uinfo)
1423{
1424 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1425
1426 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1427 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
f8ba0b7b 1428 uinfo->value.enumerated.items = e->max;
db2a4165 1429
f8ba0b7b
JS
1430 if (uinfo->value.enumerated.item > e->max - 1)
1431 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
1432 strcpy(uinfo->value.enumerated.name,
1433 e->texts[uinfo->value.enumerated.item]);
1434 return 0;
1435}
1436EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1437
1438/**
1439 * snd_soc_get_enum_double - enumerated double mixer get callback
1440 * @kcontrol: mixer control
1441 * @uinfo: control element information
1442 *
1443 * Callback to get the value of a double enumerated mixer.
1444 *
1445 * Returns 0 for success.
1446 */
1447int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1448 struct snd_ctl_elem_value *ucontrol)
1449{
1450 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1451 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1452 unsigned short val, bitmask;
1453
f8ba0b7b 1454 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165
FM
1455 ;
1456 val = snd_soc_read(codec, e->reg);
3ff3f64b
MB
1457 ucontrol->value.enumerated.item[0]
1458 = (val >> e->shift_l) & (bitmask - 1);
db2a4165
FM
1459 if (e->shift_l != e->shift_r)
1460 ucontrol->value.enumerated.item[1] =
1461 (val >> e->shift_r) & (bitmask - 1);
1462
1463 return 0;
1464}
1465EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1466
1467/**
1468 * snd_soc_put_enum_double - enumerated double mixer put callback
1469 * @kcontrol: mixer control
1470 * @uinfo: control element information
1471 *
1472 * Callback to set the value of a double enumerated mixer.
1473 *
1474 * Returns 0 for success.
1475 */
1476int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1477 struct snd_ctl_elem_value *ucontrol)
1478{
1479 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1480 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1481 unsigned short val;
1482 unsigned short mask, bitmask;
1483
f8ba0b7b 1484 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165 1485 ;
f8ba0b7b 1486 if (ucontrol->value.enumerated.item[0] > e->max - 1)
db2a4165
FM
1487 return -EINVAL;
1488 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1489 mask = (bitmask - 1) << e->shift_l;
1490 if (e->shift_l != e->shift_r) {
f8ba0b7b 1491 if (ucontrol->value.enumerated.item[1] > e->max - 1)
db2a4165
FM
1492 return -EINVAL;
1493 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1494 mask |= (bitmask - 1) << e->shift_r;
1495 }
1496
1497 return snd_soc_update_bits(codec, e->reg, mask, val);
1498}
1499EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1500
1501/**
1502 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1503 * @kcontrol: mixer control
1504 * @uinfo: control element information
1505 *
1506 * Callback to provide information about an external enumerated
1507 * single mixer.
1508 *
1509 * Returns 0 for success.
1510 */
1511int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1512 struct snd_ctl_elem_info *uinfo)
1513{
1514 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1515
1516 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1517 uinfo->count = 1;
f8ba0b7b 1518 uinfo->value.enumerated.items = e->max;
db2a4165 1519
f8ba0b7b
JS
1520 if (uinfo->value.enumerated.item > e->max - 1)
1521 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
1522 strcpy(uinfo->value.enumerated.name,
1523 e->texts[uinfo->value.enumerated.item]);
1524 return 0;
1525}
1526EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1527
1528/**
1529 * snd_soc_info_volsw_ext - external single mixer info callback
1530 * @kcontrol: mixer control
1531 * @uinfo: control element information
1532 *
1533 * Callback to provide information about a single external mixer control.
1534 *
1535 * Returns 0 for success.
1536 */
1537int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1538 struct snd_ctl_elem_info *uinfo)
1539{
a7a4ac86
PZ
1540 int max = kcontrol->private_value;
1541
1542 if (max == 1)
1543 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1544 else
1545 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 1546
db2a4165
FM
1547 uinfo->count = 1;
1548 uinfo->value.integer.min = 0;
a7a4ac86 1549 uinfo->value.integer.max = max;
db2a4165
FM
1550 return 0;
1551}
1552EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1553
db2a4165
FM
1554/**
1555 * snd_soc_info_volsw - single mixer info callback
1556 * @kcontrol: mixer control
1557 * @uinfo: control element information
1558 *
1559 * Callback to provide information about a single mixer control.
1560 *
1561 * Returns 0 for success.
1562 */
1563int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1564 struct snd_ctl_elem_info *uinfo)
1565{
4eaa9819
JS
1566 struct soc_mixer_control *mc =
1567 (struct soc_mixer_control *)kcontrol->private_value;
1568 int max = mc->max;
762b8df7 1569 unsigned int shift = mc->shift;
815ecf8d 1570 unsigned int rshift = mc->rshift;
db2a4165 1571
a7a4ac86
PZ
1572 if (max == 1)
1573 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1574 else
1575 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1576
db2a4165
FM
1577 uinfo->count = shift == rshift ? 1 : 2;
1578 uinfo->value.integer.min = 0;
a7a4ac86 1579 uinfo->value.integer.max = max;
db2a4165
FM
1580 return 0;
1581}
1582EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1583
1584/**
1585 * snd_soc_get_volsw - single mixer get callback
1586 * @kcontrol: mixer control
1587 * @uinfo: control element information
1588 *
1589 * Callback to get the value of a single mixer control.
1590 *
1591 * Returns 0 for success.
1592 */
1593int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1594 struct snd_ctl_elem_value *ucontrol)
1595{
4eaa9819
JS
1596 struct soc_mixer_control *mc =
1597 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1598 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1599 unsigned int reg = mc->reg;
1600 unsigned int shift = mc->shift;
1601 unsigned int rshift = mc->rshift;
4eaa9819 1602 int max = mc->max;
815ecf8d
JS
1603 unsigned int mask = (1 << fls(max)) - 1;
1604 unsigned int invert = mc->invert;
db2a4165
FM
1605
1606 ucontrol->value.integer.value[0] =
1607 (snd_soc_read(codec, reg) >> shift) & mask;
1608 if (shift != rshift)
1609 ucontrol->value.integer.value[1] =
1610 (snd_soc_read(codec, reg) >> rshift) & mask;
1611 if (invert) {
1612 ucontrol->value.integer.value[0] =
a7a4ac86 1613 max - ucontrol->value.integer.value[0];
db2a4165
FM
1614 if (shift != rshift)
1615 ucontrol->value.integer.value[1] =
a7a4ac86 1616 max - ucontrol->value.integer.value[1];
db2a4165
FM
1617 }
1618
1619 return 0;
1620}
1621EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1622
1623/**
1624 * snd_soc_put_volsw - single mixer put callback
1625 * @kcontrol: mixer control
1626 * @uinfo: control element information
1627 *
1628 * Callback to set the value of a single mixer control.
1629 *
1630 * Returns 0 for success.
1631 */
1632int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1633 struct snd_ctl_elem_value *ucontrol)
1634{
4eaa9819
JS
1635 struct soc_mixer_control *mc =
1636 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1637 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1638 unsigned int reg = mc->reg;
1639 unsigned int shift = mc->shift;
1640 unsigned int rshift = mc->rshift;
4eaa9819 1641 int max = mc->max;
815ecf8d
JS
1642 unsigned int mask = (1 << fls(max)) - 1;
1643 unsigned int invert = mc->invert;
db2a4165
FM
1644 unsigned short val, val2, val_mask;
1645
1646 val = (ucontrol->value.integer.value[0] & mask);
1647 if (invert)
a7a4ac86 1648 val = max - val;
db2a4165
FM
1649 val_mask = mask << shift;
1650 val = val << shift;
1651 if (shift != rshift) {
1652 val2 = (ucontrol->value.integer.value[1] & mask);
1653 if (invert)
a7a4ac86 1654 val2 = max - val2;
db2a4165
FM
1655 val_mask |= mask << rshift;
1656 val |= val2 << rshift;
1657 }
a7a4ac86 1658 return snd_soc_update_bits(codec, reg, val_mask, val);
db2a4165
FM
1659}
1660EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1661
1662/**
1663 * snd_soc_info_volsw_2r - double mixer info callback
1664 * @kcontrol: mixer control
1665 * @uinfo: control element information
1666 *
1667 * Callback to provide information about a double mixer control that
1668 * spans 2 codec registers.
1669 *
1670 * Returns 0 for success.
1671 */
1672int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1673 struct snd_ctl_elem_info *uinfo)
1674{
4eaa9819
JS
1675 struct soc_mixer_control *mc =
1676 (struct soc_mixer_control *)kcontrol->private_value;
1677 int max = mc->max;
a7a4ac86
PZ
1678
1679 if (max == 1)
1680 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1681 else
1682 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 1683
db2a4165
FM
1684 uinfo->count = 2;
1685 uinfo->value.integer.min = 0;
a7a4ac86 1686 uinfo->value.integer.max = max;
db2a4165
FM
1687 return 0;
1688}
1689EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1690
1691/**
1692 * snd_soc_get_volsw_2r - double mixer get callback
1693 * @kcontrol: mixer control
1694 * @uinfo: control element information
1695 *
1696 * Callback to get the value of a double mixer control that spans 2 registers.
1697 *
1698 * Returns 0 for success.
1699 */
1700int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1701 struct snd_ctl_elem_value *ucontrol)
1702{
4eaa9819
JS
1703 struct soc_mixer_control *mc =
1704 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1705 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1706 unsigned int reg = mc->reg;
1707 unsigned int reg2 = mc->rreg;
1708 unsigned int shift = mc->shift;
4eaa9819 1709 int max = mc->max;
815ecf8d
JS
1710 unsigned int mask = (1<<fls(max))-1;
1711 unsigned int invert = mc->invert;
db2a4165
FM
1712
1713 ucontrol->value.integer.value[0] =
1714 (snd_soc_read(codec, reg) >> shift) & mask;
1715 ucontrol->value.integer.value[1] =
1716 (snd_soc_read(codec, reg2) >> shift) & mask;
1717 if (invert) {
1718 ucontrol->value.integer.value[0] =
a7a4ac86 1719 max - ucontrol->value.integer.value[0];
db2a4165 1720 ucontrol->value.integer.value[1] =
a7a4ac86 1721 max - ucontrol->value.integer.value[1];
db2a4165
FM
1722 }
1723
1724 return 0;
1725}
1726EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1727
1728/**
1729 * snd_soc_put_volsw_2r - double mixer set callback
1730 * @kcontrol: mixer control
1731 * @uinfo: control element information
1732 *
1733 * Callback to set the value of a double mixer control that spans 2 registers.
1734 *
1735 * Returns 0 for success.
1736 */
1737int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1738 struct snd_ctl_elem_value *ucontrol)
1739{
4eaa9819
JS
1740 struct soc_mixer_control *mc =
1741 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1742 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1743 unsigned int reg = mc->reg;
1744 unsigned int reg2 = mc->rreg;
1745 unsigned int shift = mc->shift;
4eaa9819 1746 int max = mc->max;
815ecf8d
JS
1747 unsigned int mask = (1 << fls(max)) - 1;
1748 unsigned int invert = mc->invert;
db2a4165
FM
1749 int err;
1750 unsigned short val, val2, val_mask;
1751
1752 val_mask = mask << shift;
1753 val = (ucontrol->value.integer.value[0] & mask);
1754 val2 = (ucontrol->value.integer.value[1] & mask);
1755
1756 if (invert) {
a7a4ac86
PZ
1757 val = max - val;
1758 val2 = max - val2;
db2a4165
FM
1759 }
1760
1761 val = val << shift;
1762 val2 = val2 << shift;
1763
3ff3f64b
MB
1764 err = snd_soc_update_bits(codec, reg, val_mask, val);
1765 if (err < 0)
db2a4165
FM
1766 return err;
1767
1768 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1769 return err;
1770}
1771EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1772
e13ac2e9
MB
1773/**
1774 * snd_soc_info_volsw_s8 - signed mixer info callback
1775 * @kcontrol: mixer control
1776 * @uinfo: control element information
1777 *
1778 * Callback to provide information about a signed mixer control.
1779 *
1780 * Returns 0 for success.
1781 */
1782int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1783 struct snd_ctl_elem_info *uinfo)
1784{
4eaa9819
JS
1785 struct soc_mixer_control *mc =
1786 (struct soc_mixer_control *)kcontrol->private_value;
1787 int max = mc->max;
1788 int min = mc->min;
e13ac2e9
MB
1789
1790 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1791 uinfo->count = 2;
1792 uinfo->value.integer.min = 0;
1793 uinfo->value.integer.max = max-min;
1794 return 0;
1795}
1796EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1797
1798/**
1799 * snd_soc_get_volsw_s8 - signed mixer get callback
1800 * @kcontrol: mixer control
1801 * @uinfo: control element information
1802 *
1803 * Callback to get the value of a signed mixer control.
1804 *
1805 * Returns 0 for success.
1806 */
1807int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1808 struct snd_ctl_elem_value *ucontrol)
1809{
4eaa9819
JS
1810 struct soc_mixer_control *mc =
1811 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 1812 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 1813 unsigned int reg = mc->reg;
4eaa9819 1814 int min = mc->min;
e13ac2e9
MB
1815 int val = snd_soc_read(codec, reg);
1816
1817 ucontrol->value.integer.value[0] =
1818 ((signed char)(val & 0xff))-min;
1819 ucontrol->value.integer.value[1] =
1820 ((signed char)((val >> 8) & 0xff))-min;
1821 return 0;
1822}
1823EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1824
1825/**
1826 * snd_soc_put_volsw_sgn - signed mixer put callback
1827 * @kcontrol: mixer control
1828 * @uinfo: control element information
1829 *
1830 * Callback to set the value of a signed mixer control.
1831 *
1832 * Returns 0 for success.
1833 */
1834int snd_soc_put_volsw_s8(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;
e13ac2e9 1839 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 1840 unsigned int reg = mc->reg;
4eaa9819 1841 int min = mc->min;
e13ac2e9
MB
1842 unsigned short val;
1843
1844 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1845 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1846
1847 return snd_soc_update_bits(codec, reg, 0xffff, val);
1848}
1849EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1850
8c6529db
LG
1851/**
1852 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1853 * @dai: DAI
1854 * @clk_id: DAI specific clock ID
1855 * @freq: new clock frequency in Hz
1856 * @dir: new clock direction - input/output.
1857 *
1858 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1859 */
1860int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1861 unsigned int freq, int dir)
1862{
dee89c4d
MB
1863 if (dai->ops.set_sysclk)
1864 return dai->ops.set_sysclk(dai, clk_id, freq, dir);
8c6529db
LG
1865 else
1866 return -EINVAL;
1867}
1868EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1869
1870/**
1871 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1872 * @dai: DAI
1873 * @clk_id: DAI specific clock divider ID
1874 * @div: new clock divisor.
1875 *
1876 * Configures the clock dividers. This is used to derive the best DAI bit and
1877 * frame clocks from the system or master clock. It's best to set the DAI bit
1878 * and frame clocks as low as possible to save system power.
1879 */
1880int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1881 int div_id, int div)
1882{
dee89c4d
MB
1883 if (dai->ops.set_clkdiv)
1884 return dai->ops.set_clkdiv(dai, div_id, div);
8c6529db
LG
1885 else
1886 return -EINVAL;
1887}
1888EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1889
1890/**
1891 * snd_soc_dai_set_pll - configure DAI PLL.
1892 * @dai: DAI
1893 * @pll_id: DAI specific PLL ID
1894 * @freq_in: PLL input clock frequency in Hz
1895 * @freq_out: requested PLL output clock frequency in Hz
1896 *
1897 * Configures and enables PLL to generate output clock based on input clock.
1898 */
1899int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1900 int pll_id, unsigned int freq_in, unsigned int freq_out)
1901{
dee89c4d
MB
1902 if (dai->ops.set_pll)
1903 return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
8c6529db
LG
1904 else
1905 return -EINVAL;
1906}
1907EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1908
1909/**
1910 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1911 * @dai: DAI
8c6529db
LG
1912 * @fmt: SND_SOC_DAIFMT_ format value.
1913 *
1914 * Configures the DAI hardware format and clocking.
1915 */
1916int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1917{
dee89c4d
MB
1918 if (dai->ops.set_fmt)
1919 return dai->ops.set_fmt(dai, fmt);
8c6529db
LG
1920 else
1921 return -EINVAL;
1922}
1923EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
1924
1925/**
1926 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1927 * @dai: DAI
1928 * @mask: DAI specific mask representing used slots.
1929 * @slots: Number of slots in use.
1930 *
1931 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1932 * specific.
1933 */
1934int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
1935 unsigned int mask, int slots)
1936{
dee89c4d
MB
1937 if (dai->ops.set_sysclk)
1938 return dai->ops.set_tdm_slot(dai, mask, slots);
8c6529db
LG
1939 else
1940 return -EINVAL;
1941}
1942EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
1943
1944/**
1945 * snd_soc_dai_set_tristate - configure DAI system or master clock.
1946 * @dai: DAI
1947 * @tristate: tristate enable
1948 *
1949 * Tristates the DAI so that others can use it.
1950 */
1951int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
1952{
dee89c4d
MB
1953 if (dai->ops.set_sysclk)
1954 return dai->ops.set_tristate(dai, tristate);
8c6529db
LG
1955 else
1956 return -EINVAL;
1957}
1958EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
1959
1960/**
1961 * snd_soc_dai_digital_mute - configure DAI system or master clock.
1962 * @dai: DAI
1963 * @mute: mute enable
1964 *
1965 * Mutes the DAI DAC.
1966 */
1967int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
1968{
dee89c4d
MB
1969 if (dai->ops.digital_mute)
1970 return dai->ops.digital_mute(dai, mute);
8c6529db
LG
1971 else
1972 return -EINVAL;
1973}
1974EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
1975
c5af3a2e
MB
1976/**
1977 * snd_soc_register_card - Register a card with the ASoC core
1978 *
1979 * @param card Card to register
1980 *
1981 * Note that currently this is an internal only function: it will be
1982 * exposed to machine drivers after further backporting of ASoC v2
1983 * registration APIs.
1984 */
1985static int snd_soc_register_card(struct snd_soc_card *card)
1986{
1987 if (!card->name || !card->dev)
1988 return -EINVAL;
1989
1990 INIT_LIST_HEAD(&card->list);
1991 card->instantiated = 0;
1992
1993 mutex_lock(&client_mutex);
1994 list_add(&card->list, &card_list);
1995 mutex_unlock(&client_mutex);
1996
1997 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
1998
1999 return 0;
2000}
2001
2002/**
2003 * snd_soc_unregister_card - Unregister a card with the ASoC core
2004 *
2005 * @param card Card to unregister
2006 *
2007 * Note that currently this is an internal only function: it will be
2008 * exposed to machine drivers after further backporting of ASoC v2
2009 * registration APIs.
2010 */
2011static int snd_soc_unregister_card(struct snd_soc_card *card)
2012{
2013 mutex_lock(&client_mutex);
2014 list_del(&card->list);
2015 mutex_unlock(&client_mutex);
2016
2017 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2018
2019 return 0;
2020}
2021
db2a4165
FM
2022static int __devinit snd_soc_init(void)
2023{
384c89e2
MB
2024#ifdef CONFIG_DEBUG_FS
2025 debugfs_root = debugfs_create_dir("asoc", NULL);
2026 if (IS_ERR(debugfs_root) || !debugfs_root) {
2027 printk(KERN_WARNING
2028 "ASoC: Failed to create debugfs directory\n");
2029 debugfs_root = NULL;
2030 }
2031#endif
2032
db2a4165
FM
2033 return platform_driver_register(&soc_driver);
2034}
2035
7d8c16a6 2036static void __exit snd_soc_exit(void)
db2a4165 2037{
384c89e2
MB
2038#ifdef CONFIG_DEBUG_FS
2039 debugfs_remove_recursive(debugfs_root);
2040#endif
3ff3f64b 2041 platform_driver_unregister(&soc_driver);
db2a4165
FM
2042}
2043
2044module_init(snd_soc_init);
2045module_exit(snd_soc_exit);
2046
2047/* Module information */
d331124d 2048MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
db2a4165
FM
2049MODULE_DESCRIPTION("ALSA SoC Core");
2050MODULE_LICENSE("GPL");
8b45a209 2051MODULE_ALIAS("platform:soc-audio");