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