]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/soc/soc-core.c
ASoC: move dma_data from snd_soc_dai to snd_soc_pcm_stream
[net-next-2.6.git] / sound / soc / soc-core.c
CommitLineData
db2a4165
FM
1/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
0664d888
LG
5 * Copyright 2005 Openedhand Ltd.
6 *
d331124d 7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
0664d888
LG
8 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
db2a4165
FM
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
db2a4165
FM
16 * TODO:
17 * o Add hw rules to enforce rates, etc.
18 * o More testing with other codecs/machines.
19 * o Add more codecs and platforms to ensure good API coverage.
20 * o Support TDM on PCM and I2S
21 */
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/pm.h>
28#include <linux/bitops.h>
12ef193d 29#include <linux/debugfs.h>
db2a4165 30#include <linux/platform_device.h>
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
87506549
MB
966 if (card->resume_pre)
967 card->resume_pre(pdev);
db2a4165 968
87506549
MB
969 for (i = 0; i < card->num_links; i++) {
970 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 971 if (cpu_dai->resume && cpu_dai->ac97_control)
dc7d7b83 972 cpu_dai->resume(cpu_dai);
db2a4165
FM
973 }
974
975 if (codec_dev->resume)
976 codec_dev->resume(pdev);
977
3ff3f64b
MB
978 for (i = 0; i < codec->num_dai; i++) {
979 char *stream = codec->dai[i].playback.stream_name;
db2a4165
FM
980 if (stream != NULL)
981 snd_soc_dapm_stream_event(codec, stream,
982 SND_SOC_DAPM_STREAM_RESUME);
983 stream = codec->dai[i].capture.stream_name;
984 if (stream != NULL)
985 snd_soc_dapm_stream_event(codec, stream,
986 SND_SOC_DAPM_STREAM_RESUME);
987 }
988
3ff3f64b 989 /* unmute any active DACs */
dee89c4d
MB
990 for (i = 0; i < card->num_links; i++) {
991 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
6335d055
EM
992 if (dai->ops->digital_mute && dai->playback.active)
993 dai->ops->digital_mute(dai, 0);
db2a4165
FM
994 }
995
87506549
MB
996 for (i = 0; i < card->num_links; i++) {
997 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 998 if (cpu_dai->resume && !cpu_dai->ac97_control)
dc7d7b83 999 cpu_dai->resume(cpu_dai);
db2a4165 1000 if (platform->resume)
d273ebe7 1001 platform->resume(&card->dai_link[i]);
db2a4165
FM
1002 }
1003
87506549
MB
1004 if (card->resume_post)
1005 card->resume_post(pdev);
db2a4165 1006
fde22f27 1007 dev_dbg(socdev->dev, "resume work completed\n");
6ed25978
AG
1008
1009 /* userspace can access us now we are back as we were before */
1010 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
1011}
1012
1013/* powers up audio subsystem after a suspend */
416356fc 1014static int soc_resume(struct device *dev)
6ed25978 1015{
416356fc 1016 struct platform_device *pdev = to_platform_device(dev);
6ed25978 1017 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
6308419a 1018 struct snd_soc_card *card = socdev->card;
64ab9baa 1019 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
6ed25978 1020
b9dd94a8
PU
1021 /* If the initialization of this soc device failed, there is no codec
1022 * associated with it. Just bail out in this case.
1023 */
1024 if (!card->codec)
1025 return 0;
1026
64ab9baa
MB
1027 /* AC97 devices might have other drivers hanging off them so
1028 * need to resume immediately. Other drivers don't have that
1029 * problem and may take a substantial amount of time to resume
1030 * due to I/O costs and anti-pop so handle them out of line.
1031 */
1032 if (cpu_dai->ac97_control) {
1033 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
1034 soc_resume_deferred(&card->deferred_resume_work);
1035 } else {
1036 dev_dbg(socdev->dev, "Scheduling resume work\n");
1037 if (!schedule_work(&card->deferred_resume_work))
1038 dev_err(socdev->dev, "resume work item may be lost\n");
1039 }
6ed25978 1040
db2a4165
FM
1041 return 0;
1042}
db2a4165
FM
1043#else
1044#define soc_suspend NULL
1045#define soc_resume NULL
1046#endif
1047
02a06d30
BS
1048static struct snd_soc_dai_ops null_dai_ops = {
1049};
1050
435c5e25 1051static void snd_soc_instantiate_card(struct snd_soc_card *card)
db2a4165 1052{
435c5e25
MB
1053 struct platform_device *pdev = container_of(card->dev,
1054 struct platform_device,
1055 dev);
1056 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
fe3e78e0 1057 struct snd_soc_codec *codec;
435c5e25
MB
1058 struct snd_soc_platform *platform;
1059 struct snd_soc_dai *dai;
6b05eda6 1060 int i, found, ret, ac97;
435c5e25
MB
1061
1062 if (card->instantiated)
1063 return;
1064
1065 found = 0;
1066 list_for_each_entry(platform, &platform_list, list)
1067 if (card->platform == platform) {
1068 found = 1;
1069 break;
1070 }
1071 if (!found) {
1072 dev_dbg(card->dev, "Platform %s not registered\n",
1073 card->platform->name);
1074 return;
1075 }
6308419a 1076
6b05eda6 1077 ac97 = 0;
435c5e25
MB
1078 for (i = 0; i < card->num_links; i++) {
1079 found = 0;
1080 list_for_each_entry(dai, &dai_list, list)
1081 if (card->dai_link[i].cpu_dai == dai) {
1082 found = 1;
1083 break;
1084 }
1085 if (!found) {
1086 dev_dbg(card->dev, "DAI %s not registered\n",
1087 card->dai_link[i].cpu_dai->name);
1088 return;
1089 }
6b05eda6
MB
1090
1091 if (card->dai_link[i].cpu_dai->ac97_control)
1092 ac97 = 1;
c5af3a2e
MB
1093 }
1094
02a06d30
BS
1095 for (i = 0; i < card->num_links; i++) {
1096 if (!card->dai_link[i].codec_dai->ops)
1097 card->dai_link[i].codec_dai->ops = &null_dai_ops;
1098 }
1099
6b05eda6
MB
1100 /* If we have AC97 in the system then don't wait for the
1101 * codec. This will need revisiting if we have to handle
1102 * systems with mixed AC97 and non-AC97 parts. Only check for
1103 * DAIs currently; we can't do this per link since some AC97
1104 * codecs have non-AC97 DAIs.
1105 */
1106 if (!ac97)
1107 for (i = 0; i < card->num_links; i++) {
1108 found = 0;
1109 list_for_each_entry(dai, &dai_list, list)
1110 if (card->dai_link[i].codec_dai == dai) {
1111 found = 1;
1112 break;
1113 }
1114 if (!found) {
1115 dev_dbg(card->dev, "DAI %s not registered\n",
1116 card->dai_link[i].codec_dai->name);
1117 return;
1118 }
1119 }
1120
435c5e25
MB
1121 /* Note that we do not current check for codec components */
1122
1123 dev_dbg(card->dev, "All components present, instantiating\n");
1124
1125 /* Found everything, bring it up */
96dd3622
MB
1126 card->pmdown_time = pmdown_time;
1127
87506549
MB
1128 if (card->probe) {
1129 ret = card->probe(pdev);
3ff3f64b 1130 if (ret < 0)
435c5e25 1131 return;
db2a4165
FM
1132 }
1133
87506549
MB
1134 for (i = 0; i < card->num_links; i++) {
1135 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 1136 if (cpu_dai->probe) {
bdb92876 1137 ret = cpu_dai->probe(pdev, cpu_dai);
3ff3f64b 1138 if (ret < 0)
db2a4165
FM
1139 goto cpu_dai_err;
1140 }
1141 }
1142
1143 if (codec_dev->probe) {
1144 ret = codec_dev->probe(pdev);
3ff3f64b 1145 if (ret < 0)
db2a4165
FM
1146 goto cpu_dai_err;
1147 }
fe3e78e0 1148 codec = card->codec;
db2a4165
FM
1149
1150 if (platform->probe) {
1151 ret = platform->probe(pdev);
3ff3f64b 1152 if (ret < 0)
db2a4165
FM
1153 goto platform_err;
1154 }
1155
1156 /* DAPM stream work */
6308419a 1157 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
1301a964 1158#ifdef CONFIG_PM
6ed25978 1159 /* deferred resume work */
6308419a 1160 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1301a964 1161#endif
6ed25978 1162
fe3e78e0
MB
1163 for (i = 0; i < card->num_links; i++) {
1164 if (card->dai_link[i].init) {
1165 ret = card->dai_link[i].init(codec);
1166 if (ret < 0) {
1167 printk(KERN_ERR "asoc: failed to init %s\n",
1168 card->dai_link[i].stream_name);
1169 continue;
1170 }
1171 }
f7732053 1172 if (card->dai_link[i].codec_dai->ac97_control)
fe3e78e0 1173 ac97 = 1;
fe3e78e0
MB
1174 }
1175
1176 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1177 "%s", card->name);
1178 snprintf(codec->card->longname, sizeof(codec->card->longname),
1179 "%s (%s)", card->name, codec->name);
1180
1181 /* Make sure all DAPM widgets are instantiated */
1182 snd_soc_dapm_new_widgets(codec);
1183
1184 ret = snd_card_register(codec->card);
1185 if (ret < 0) {
1186 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1187 codec->name);
1188 goto card_err;
1189 }
1190
1191 mutex_lock(&codec->mutex);
1192#ifdef CONFIG_SND_SOC_AC97_BUS
1193 /* Only instantiate AC97 if not already done by the adaptor
1194 * for the generic AC97 subsystem.
1195 */
1196 if (ac97 && strcmp(codec->name, "AC97") != 0) {
1197 ret = soc_ac97_dev_register(codec);
1198 if (ret < 0) {
1199 printk(KERN_ERR "asoc: AC97 device register failed\n");
1200 snd_card_free(codec->card);
1201 mutex_unlock(&codec->mutex);
1202 goto card_err;
1203 }
1204 }
1205#endif
1206
1207 ret = snd_soc_dapm_sys_add(card->socdev->dev);
1208 if (ret < 0)
1209 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1210
dbe21408
MB
1211 ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1212 if (ret < 0)
1213 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1214
fe3e78e0
MB
1215 ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1216 if (ret < 0)
1217 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1218
1219 soc_init_codec_debugfs(codec);
1220 mutex_unlock(&codec->mutex);
1221
435c5e25
MB
1222 card->instantiated = 1;
1223
1224 return;
db2a4165 1225
fe3e78e0
MB
1226card_err:
1227 if (platform->remove)
1228 platform->remove(pdev);
1229
db2a4165
FM
1230platform_err:
1231 if (codec_dev->remove)
1232 codec_dev->remove(pdev);
1233
1234cpu_dai_err:
18b9b3d9 1235 for (i--; i >= 0; i--) {
87506549 1236 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 1237 if (cpu_dai->remove)
bdb92876 1238 cpu_dai->remove(pdev, cpu_dai);
db2a4165
FM
1239 }
1240
87506549
MB
1241 if (card->remove)
1242 card->remove(pdev);
435c5e25 1243}
db2a4165 1244
435c5e25
MB
1245/*
1246 * Attempt to initialise any uninitalised cards. Must be called with
1247 * client_mutex.
1248 */
1249static void snd_soc_instantiate_cards(void)
1250{
1251 struct snd_soc_card *card;
1252 list_for_each_entry(card, &card_list, list)
1253 snd_soc_instantiate_card(card);
1254}
1255
1256/* probes a new socdev */
1257static int soc_probe(struct platform_device *pdev)
1258{
1259 int ret = 0;
1260 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1261 struct snd_soc_card *card = socdev->card;
1262
1263 /* Bodge while we push things out of socdev */
1264 card->socdev = socdev;
1265
1266 /* Bodge while we unpick instantiation */
1267 card->dev = &pdev->dev;
1268 ret = snd_soc_register_card(card);
1269 if (ret != 0) {
1270 dev_err(&pdev->dev, "Failed to register card\n");
1271 return ret;
1272 }
1273
1274 return 0;
db2a4165
FM
1275}
1276
1277/* removes a socdev */
1278static int soc_remove(struct platform_device *pdev)
1279{
1280 int i;
1281 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
87506549 1282 struct snd_soc_card *card = socdev->card;
87689d56 1283 struct snd_soc_platform *platform = card->platform;
db2a4165
FM
1284 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1285
914dc182
MR
1286 if (!card->instantiated)
1287 return 0;
1288
6308419a 1289 run_delayed_work(&card->delayed_work);
965ac42c 1290
db2a4165
FM
1291 if (platform->remove)
1292 platform->remove(pdev);
1293
1294 if (codec_dev->remove)
1295 codec_dev->remove(pdev);
1296
87506549
MB
1297 for (i = 0; i < card->num_links; i++) {
1298 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 1299 if (cpu_dai->remove)
bdb92876 1300 cpu_dai->remove(pdev, cpu_dai);
db2a4165
FM
1301 }
1302
87506549
MB
1303 if (card->remove)
1304 card->remove(pdev);
db2a4165 1305
c5af3a2e
MB
1306 snd_soc_unregister_card(card);
1307
db2a4165
FM
1308 return 0;
1309}
1310
416356fc 1311static int soc_poweroff(struct device *dev)
51737470 1312{
416356fc 1313 struct platform_device *pdev = to_platform_device(dev);
51737470
MB
1314 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1315 struct snd_soc_card *card = socdev->card;
1316
1317 if (!card->instantiated)
416356fc 1318 return 0;
51737470
MB
1319
1320 /* Flush out pmdown_time work - we actually do want to run it
1321 * now, we're shutting down so no imminent restart. */
1322 run_delayed_work(&card->delayed_work);
1323
1324 snd_soc_dapm_shutdown(socdev);
416356fc
MB
1325
1326 return 0;
51737470
MB
1327}
1328
47145210 1329static const struct dev_pm_ops soc_pm_ops = {
416356fc
MB
1330 .suspend = soc_suspend,
1331 .resume = soc_resume,
1332 .poweroff = soc_poweroff,
1333};
1334
db2a4165
FM
1335/* ASoC platform driver */
1336static struct platform_driver soc_driver = {
1337 .driver = {
1338 .name = "soc-audio",
8b45a209 1339 .owner = THIS_MODULE,
416356fc 1340 .pm = &soc_pm_ops,
db2a4165
FM
1341 },
1342 .probe = soc_probe,
1343 .remove = soc_remove,
db2a4165
FM
1344};
1345
1346/* create a new pcm */
1347static int soc_new_pcm(struct snd_soc_device *socdev,
1348 struct snd_soc_dai_link *dai_link, int num)
1349{
87689d56 1350 struct snd_soc_card *card = socdev->card;
6627a653 1351 struct snd_soc_codec *codec = card->codec;
87689d56 1352 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
1353 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1354 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
db2a4165
FM
1355 struct snd_soc_pcm_runtime *rtd;
1356 struct snd_pcm *pcm;
1357 char new_name[64];
1358 int ret = 0, playback = 0, capture = 0;
1359
1360 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1361 if (rtd == NULL)
1362 return -ENOMEM;
cb666e5b
LG
1363
1364 rtd->dai = dai_link;
db2a4165 1365 rtd->socdev = socdev;
6627a653 1366 codec_dai->codec = card->codec;
db2a4165
FM
1367
1368 /* check client and interface hw capabilities */
40ca1142
MB
1369 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1370 dai_link->stream_name, codec_dai->name, num);
db2a4165
FM
1371
1372 if (codec_dai->playback.channels_min)
1373 playback = 1;
1374 if (codec_dai->capture.channels_min)
1375 capture = 1;
1376
1377 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1378 capture, &pcm);
1379 if (ret < 0) {
3ff3f64b
MB
1380 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1381 codec->name);
db2a4165
FM
1382 kfree(rtd);
1383 return ret;
1384 }
1385
4ccab3e7 1386 dai_link->pcm = pcm;
db2a4165 1387 pcm->private_data = rtd;
87689d56 1388 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
87689d56
MB
1389 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1390 soc_pcm_ops.copy = platform->pcm_ops->copy;
1391 soc_pcm_ops.silence = platform->pcm_ops->silence;
1392 soc_pcm_ops.ack = platform->pcm_ops->ack;
1393 soc_pcm_ops.page = platform->pcm_ops->page;
db2a4165
FM
1394
1395 if (playback)
1396 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1397
1398 if (capture)
1399 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1400
87689d56 1401 ret = platform->pcm_new(codec->card, codec_dai, pcm);
db2a4165
FM
1402 if (ret < 0) {
1403 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1404 kfree(rtd);
1405 return ret;
1406 }
1407
87689d56 1408 pcm->private_free = platform->pcm_free;
db2a4165
FM
1409 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1410 cpu_dai->name);
1411 return ret;
1412}
1413
096e49d5
MB
1414/**
1415 * snd_soc_codec_volatile_register: Report if a register is volatile.
1416 *
1417 * @codec: CODEC to query.
1418 * @reg: Register to query.
1419 *
1420 * Boolean function indiciating if a CODEC register is volatile.
1421 */
1422int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1423{
1424 if (codec->volatile_register)
1425 return codec->volatile_register(reg);
1426 else
1427 return 0;
1428}
1429EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1430
db2a4165
FM
1431/**
1432 * snd_soc_new_ac97_codec - initailise AC97 device
1433 * @codec: audio codec
1434 * @ops: AC97 bus operations
1435 * @num: AC97 codec number
1436 *
1437 * Initialises AC97 codec resources for use by ad-hoc devices only.
1438 */
1439int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1440 struct snd_ac97_bus_ops *ops, int num)
1441{
1442 mutex_lock(&codec->mutex);
1443
1444 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1445 if (codec->ac97 == NULL) {
1446 mutex_unlock(&codec->mutex);
1447 return -ENOMEM;
1448 }
1449
1450 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1451 if (codec->ac97->bus == NULL) {
1452 kfree(codec->ac97);
1453 codec->ac97 = NULL;
1454 mutex_unlock(&codec->mutex);
1455 return -ENOMEM;
1456 }
1457
1458 codec->ac97->bus->ops = ops;
1459 codec->ac97->num = num;
2718625f 1460 codec->dev = &codec->ac97->dev;
db2a4165
FM
1461 mutex_unlock(&codec->mutex);
1462 return 0;
1463}
1464EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1465
1466/**
1467 * snd_soc_free_ac97_codec - free AC97 codec device
1468 * @codec: audio codec
1469 *
1470 * Frees AC97 codec device resources.
1471 */
1472void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1473{
1474 mutex_lock(&codec->mutex);
1475 kfree(codec->ac97->bus);
1476 kfree(codec->ac97);
1477 codec->ac97 = NULL;
1478 mutex_unlock(&codec->mutex);
1479}
1480EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1481
1482/**
1483 * snd_soc_update_bits - update codec register bits
1484 * @codec: audio codec
1485 * @reg: codec register
1486 * @mask: register mask
1487 * @value: new value
1488 *
1489 * Writes new register value.
1490 *
1491 * Returns 1 for change else 0.
1492 */
1493int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
46f5822f 1494 unsigned int mask, unsigned int value)
db2a4165
FM
1495{
1496 int change;
46f5822f 1497 unsigned int old, new;
db2a4165 1498
db2a4165
FM
1499 old = snd_soc_read(codec, reg);
1500 new = (old & ~mask) | value;
1501 change = old != new;
1502 if (change)
1503 snd_soc_write(codec, reg, new);
1504
db2a4165
FM
1505 return change;
1506}
1507EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1508
6c508c62
EN
1509/**
1510 * snd_soc_update_bits_locked - update codec register bits
1511 * @codec: audio codec
1512 * @reg: codec register
1513 * @mask: register mask
1514 * @value: new value
1515 *
1516 * Writes new register value, and takes the codec mutex.
1517 *
1518 * Returns 1 for change else 0.
1519 */
dd1b3d53
MB
1520int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1521 unsigned short reg, unsigned int mask,
1522 unsigned int value)
6c508c62
EN
1523{
1524 int change;
1525
1526 mutex_lock(&codec->mutex);
1527 change = snd_soc_update_bits(codec, reg, mask, value);
1528 mutex_unlock(&codec->mutex);
1529
1530 return change;
1531}
dd1b3d53 1532EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
6c508c62 1533
db2a4165
FM
1534/**
1535 * snd_soc_test_bits - test register for change
1536 * @codec: audio codec
1537 * @reg: codec register
1538 * @mask: register mask
1539 * @value: new value
1540 *
1541 * Tests a register with a new value and checks if the new value is
1542 * different from the old value.
1543 *
1544 * Returns 1 for change else 0.
1545 */
1546int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
46f5822f 1547 unsigned int mask, unsigned int value)
db2a4165
FM
1548{
1549 int change;
46f5822f 1550 unsigned int old, new;
db2a4165 1551
db2a4165
FM
1552 old = snd_soc_read(codec, reg);
1553 new = (old & ~mask) | value;
1554 change = old != new;
db2a4165
FM
1555
1556 return change;
1557}
1558EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1559
db2a4165
FM
1560/**
1561 * snd_soc_new_pcms - create new sound card and pcms
1562 * @socdev: the SoC audio device
ac11a2b3
MB
1563 * @idx: ALSA card index
1564 * @xid: card identification
db2a4165
FM
1565 *
1566 * Create a new sound card based upon the codec and interface pcms.
1567 *
1568 * Returns 0 for success, else error.
1569 */
bc7320c5 1570int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
db2a4165 1571{
87506549 1572 struct snd_soc_card *card = socdev->card;
6627a653 1573 struct snd_soc_codec *codec = card->codec;
bd7dd77c 1574 int ret, i;
db2a4165
FM
1575
1576 mutex_lock(&codec->mutex);
1577
1578 /* register a sound card */
bd7dd77c
TI
1579 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1580 if (ret < 0) {
db2a4165
FM
1581 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1582 codec->name);
1583 mutex_unlock(&codec->mutex);
bd7dd77c 1584 return ret;
db2a4165
FM
1585 }
1586
452c5eaa 1587 codec->socdev = socdev;
db2a4165
FM
1588 codec->card->dev = socdev->dev;
1589 codec->card->private_data = codec;
1590 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1591
1592 /* create the pcms */
87506549
MB
1593 for (i = 0; i < card->num_links; i++) {
1594 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
db2a4165
FM
1595 if (ret < 0) {
1596 printk(KERN_ERR "asoc: can't create pcm %s\n",
87506549 1597 card->dai_link[i].stream_name);
db2a4165
FM
1598 mutex_unlock(&codec->mutex);
1599 return ret;
1600 }
f7732053
BS
1601 if (card->dai_link[i].codec_dai->ac97_control) {
1602 snd_ac97_dev_add_pdata(codec->ac97,
1603 card->dai_link[i].cpu_dai->ac97_pdata);
1604 }
db2a4165
FM
1605 }
1606
1607 mutex_unlock(&codec->mutex);
1608 return ret;
1609}
1610EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1611
db2a4165
FM
1612/**
1613 * snd_soc_free_pcms - free sound card and pcms
1614 * @socdev: the SoC audio device
1615 *
1616 * Frees sound card and pcms associated with the socdev.
1617 * Also unregister the codec if it is an AC97 device.
1618 */
1619void snd_soc_free_pcms(struct snd_soc_device *socdev)
1620{
6627a653 1621 struct snd_soc_codec *codec = socdev->card->codec;
a68660e0 1622#ifdef CONFIG_SND_SOC_AC97_BUS
3c4b266f 1623 struct snd_soc_dai *codec_dai;
a68660e0
LG
1624 int i;
1625#endif
db2a4165
FM
1626
1627 mutex_lock(&codec->mutex);
6627a653 1628 soc_cleanup_codec_debugfs(codec);
db2a4165 1629#ifdef CONFIG_SND_SOC_AC97_BUS
3ff3f64b 1630 for (i = 0; i < codec->num_dai; i++) {
a68660e0 1631 codec_dai = &codec->dai[i];
d2314e0e
AN
1632 if (codec_dai->ac97_control && codec->ac97 &&
1633 strcmp(codec->name, "AC97") != 0) {
a68660e0
LG
1634 soc_ac97_dev_unregister(codec);
1635 goto free_card;
1636 }
1637 }
1638free_card:
db2a4165
FM
1639#endif
1640
1641 if (codec->card)
1642 snd_card_free(codec->card);
1643 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1644 mutex_unlock(&codec->mutex);
1645}
1646EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1647
1648/**
1649 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1650 * @substream: the pcm substream
1651 * @hw: the hardware parameters
1652 *
1653 * Sets the substream runtime hardware parameters.
1654 */
1655int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1656 const struct snd_pcm_hardware *hw)
1657{
1658 struct snd_pcm_runtime *runtime = substream->runtime;
1659 runtime->hw.info = hw->info;
1660 runtime->hw.formats = hw->formats;
1661 runtime->hw.period_bytes_min = hw->period_bytes_min;
1662 runtime->hw.period_bytes_max = hw->period_bytes_max;
1663 runtime->hw.periods_min = hw->periods_min;
1664 runtime->hw.periods_max = hw->periods_max;
1665 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1666 runtime->hw.fifo_size = hw->fifo_size;
1667 return 0;
1668}
1669EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1670
1671/**
1672 * snd_soc_cnew - create new control
1673 * @_template: control template
1674 * @data: control private data
ac11a2b3 1675 * @long_name: control long name
db2a4165
FM
1676 *
1677 * Create a new mixer control from a template control.
1678 *
1679 * Returns 0 for success, else error.
1680 */
1681struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1682 void *data, char *long_name)
1683{
1684 struct snd_kcontrol_new template;
1685
1686 memcpy(&template, _template, sizeof(template));
1687 if (long_name)
1688 template.name = long_name;
db2a4165
FM
1689 template.index = 0;
1690
1691 return snd_ctl_new1(&template, data);
1692}
1693EXPORT_SYMBOL_GPL(snd_soc_cnew);
1694
3e8e1952
IM
1695/**
1696 * snd_soc_add_controls - add an array of controls to a codec.
1697 * Convienience function to add a list of controls. Many codecs were
1698 * duplicating this code.
1699 *
1700 * @codec: codec to add controls to
1701 * @controls: array of controls to add
1702 * @num_controls: number of elements in the array
1703 *
1704 * Return 0 for success, else error.
1705 */
1706int snd_soc_add_controls(struct snd_soc_codec *codec,
1707 const struct snd_kcontrol_new *controls, int num_controls)
1708{
1709 struct snd_card *card = codec->card;
1710 int err, i;
1711
1712 for (i = 0; i < num_controls; i++) {
1713 const struct snd_kcontrol_new *control = &controls[i];
1714 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1715 if (err < 0) {
1716 dev_err(codec->dev, "%s: Failed to add %s\n",
1717 codec->name, control->name);
1718 return err;
1719 }
1720 }
1721
1722 return 0;
1723}
1724EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1725
db2a4165
FM
1726/**
1727 * snd_soc_info_enum_double - enumerated double mixer info callback
1728 * @kcontrol: mixer control
1729 * @uinfo: control element information
1730 *
1731 * Callback to provide information about a double enumerated
1732 * mixer control.
1733 *
1734 * Returns 0 for success.
1735 */
1736int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1737 struct snd_ctl_elem_info *uinfo)
1738{
1739 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1740
1741 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1742 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
f8ba0b7b 1743 uinfo->value.enumerated.items = e->max;
db2a4165 1744
f8ba0b7b
JS
1745 if (uinfo->value.enumerated.item > e->max - 1)
1746 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
1747 strcpy(uinfo->value.enumerated.name,
1748 e->texts[uinfo->value.enumerated.item]);
1749 return 0;
1750}
1751EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1752
1753/**
1754 * snd_soc_get_enum_double - enumerated double mixer get callback
1755 * @kcontrol: mixer control
ac11a2b3 1756 * @ucontrol: control element information
db2a4165
FM
1757 *
1758 * Callback to get the value of a double enumerated mixer.
1759 *
1760 * Returns 0 for success.
1761 */
1762int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1763 struct snd_ctl_elem_value *ucontrol)
1764{
1765 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1766 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 1767 unsigned int val, bitmask;
db2a4165 1768
f8ba0b7b 1769 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165
FM
1770 ;
1771 val = snd_soc_read(codec, e->reg);
3ff3f64b
MB
1772 ucontrol->value.enumerated.item[0]
1773 = (val >> e->shift_l) & (bitmask - 1);
db2a4165
FM
1774 if (e->shift_l != e->shift_r)
1775 ucontrol->value.enumerated.item[1] =
1776 (val >> e->shift_r) & (bitmask - 1);
1777
1778 return 0;
1779}
1780EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1781
1782/**
1783 * snd_soc_put_enum_double - enumerated double mixer put callback
1784 * @kcontrol: mixer control
ac11a2b3 1785 * @ucontrol: control element information
db2a4165
FM
1786 *
1787 * Callback to set the value of a double enumerated mixer.
1788 *
1789 * Returns 0 for success.
1790 */
1791int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1792 struct snd_ctl_elem_value *ucontrol)
1793{
1794 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1795 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f
DR
1796 unsigned int val;
1797 unsigned int mask, bitmask;
db2a4165 1798
f8ba0b7b 1799 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165 1800 ;
f8ba0b7b 1801 if (ucontrol->value.enumerated.item[0] > e->max - 1)
db2a4165
FM
1802 return -EINVAL;
1803 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1804 mask = (bitmask - 1) << e->shift_l;
1805 if (e->shift_l != e->shift_r) {
f8ba0b7b 1806 if (ucontrol->value.enumerated.item[1] > e->max - 1)
db2a4165
FM
1807 return -EINVAL;
1808 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1809 mask |= (bitmask - 1) << e->shift_r;
1810 }
1811
6c508c62 1812 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
db2a4165
FM
1813}
1814EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1815
2e72f8e3
PU
1816/**
1817 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1818 * @kcontrol: mixer control
1819 * @ucontrol: control element information
1820 *
1821 * Callback to get the value of a double semi enumerated mixer.
1822 *
1823 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1824 * used for handling bitfield coded enumeration for example.
1825 *
1826 * Returns 0 for success.
1827 */
1828int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1829 struct snd_ctl_elem_value *ucontrol)
1830{
1831 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
74155556 1832 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 1833 unsigned int reg_val, val, mux;
2e72f8e3
PU
1834
1835 reg_val = snd_soc_read(codec, e->reg);
1836 val = (reg_val >> e->shift_l) & e->mask;
1837 for (mux = 0; mux < e->max; mux++) {
1838 if (val == e->values[mux])
1839 break;
1840 }
1841 ucontrol->value.enumerated.item[0] = mux;
1842 if (e->shift_l != e->shift_r) {
1843 val = (reg_val >> e->shift_r) & e->mask;
1844 for (mux = 0; mux < e->max; mux++) {
1845 if (val == e->values[mux])
1846 break;
1847 }
1848 ucontrol->value.enumerated.item[1] = mux;
1849 }
1850
1851 return 0;
1852}
1853EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1854
1855/**
1856 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1857 * @kcontrol: mixer control
1858 * @ucontrol: control element information
1859 *
1860 * Callback to set the value of a double semi enumerated mixer.
1861 *
1862 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1863 * used for handling bitfield coded enumeration for example.
1864 *
1865 * Returns 0 for success.
1866 */
1867int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1868 struct snd_ctl_elem_value *ucontrol)
1869{
1870 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
74155556 1871 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f
DR
1872 unsigned int val;
1873 unsigned int mask;
2e72f8e3
PU
1874
1875 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1876 return -EINVAL;
1877 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1878 mask = e->mask << e->shift_l;
1879 if (e->shift_l != e->shift_r) {
1880 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1881 return -EINVAL;
1882 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1883 mask |= e->mask << e->shift_r;
1884 }
1885
6c508c62 1886 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2e72f8e3
PU
1887}
1888EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1889
db2a4165
FM
1890/**
1891 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1892 * @kcontrol: mixer control
1893 * @uinfo: control element information
1894 *
1895 * Callback to provide information about an external enumerated
1896 * single mixer.
1897 *
1898 * Returns 0 for success.
1899 */
1900int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_info *uinfo)
1902{
1903 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1904
1905 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1906 uinfo->count = 1;
f8ba0b7b 1907 uinfo->value.enumerated.items = e->max;
db2a4165 1908
f8ba0b7b
JS
1909 if (uinfo->value.enumerated.item > e->max - 1)
1910 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
1911 strcpy(uinfo->value.enumerated.name,
1912 e->texts[uinfo->value.enumerated.item]);
1913 return 0;
1914}
1915EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1916
1917/**
1918 * snd_soc_info_volsw_ext - external single mixer info callback
1919 * @kcontrol: mixer control
1920 * @uinfo: control element information
1921 *
1922 * Callback to provide information about a single external mixer control.
1923 *
1924 * Returns 0 for success.
1925 */
1926int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1927 struct snd_ctl_elem_info *uinfo)
1928{
a7a4ac86
PZ
1929 int max = kcontrol->private_value;
1930
fd5dfad9 1931 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
a7a4ac86
PZ
1932 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1933 else
1934 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 1935
db2a4165
FM
1936 uinfo->count = 1;
1937 uinfo->value.integer.min = 0;
a7a4ac86 1938 uinfo->value.integer.max = max;
db2a4165
FM
1939 return 0;
1940}
1941EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1942
db2a4165
FM
1943/**
1944 * snd_soc_info_volsw - single mixer info callback
1945 * @kcontrol: mixer control
1946 * @uinfo: control element information
1947 *
1948 * Callback to provide information about a single mixer control.
1949 *
1950 * Returns 0 for success.
1951 */
1952int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1953 struct snd_ctl_elem_info *uinfo)
1954{
4eaa9819
JS
1955 struct soc_mixer_control *mc =
1956 (struct soc_mixer_control *)kcontrol->private_value;
1957 int max = mc->max;
762b8df7 1958 unsigned int shift = mc->shift;
815ecf8d 1959 unsigned int rshift = mc->rshift;
db2a4165 1960
fd5dfad9 1961 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
a7a4ac86
PZ
1962 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1963 else
1964 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1965
db2a4165
FM
1966 uinfo->count = shift == rshift ? 1 : 2;
1967 uinfo->value.integer.min = 0;
a7a4ac86 1968 uinfo->value.integer.max = max;
db2a4165
FM
1969 return 0;
1970}
1971EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1972
1973/**
1974 * snd_soc_get_volsw - single mixer get callback
1975 * @kcontrol: mixer control
ac11a2b3 1976 * @ucontrol: control element information
db2a4165
FM
1977 *
1978 * Callback to get the value of a single mixer control.
1979 *
1980 * Returns 0 for success.
1981 */
1982int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1983 struct snd_ctl_elem_value *ucontrol)
1984{
4eaa9819
JS
1985 struct soc_mixer_control *mc =
1986 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1987 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1988 unsigned int reg = mc->reg;
1989 unsigned int shift = mc->shift;
1990 unsigned int rshift = mc->rshift;
4eaa9819 1991 int max = mc->max;
815ecf8d
JS
1992 unsigned int mask = (1 << fls(max)) - 1;
1993 unsigned int invert = mc->invert;
db2a4165
FM
1994
1995 ucontrol->value.integer.value[0] =
1996 (snd_soc_read(codec, reg) >> shift) & mask;
1997 if (shift != rshift)
1998 ucontrol->value.integer.value[1] =
1999 (snd_soc_read(codec, reg) >> rshift) & mask;
2000 if (invert) {
2001 ucontrol->value.integer.value[0] =
a7a4ac86 2002 max - ucontrol->value.integer.value[0];
db2a4165
FM
2003 if (shift != rshift)
2004 ucontrol->value.integer.value[1] =
a7a4ac86 2005 max - ucontrol->value.integer.value[1];
db2a4165
FM
2006 }
2007
2008 return 0;
2009}
2010EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2011
2012/**
2013 * snd_soc_put_volsw - single mixer put callback
2014 * @kcontrol: mixer control
ac11a2b3 2015 * @ucontrol: control element information
db2a4165
FM
2016 *
2017 * Callback to set the value of a single mixer control.
2018 *
2019 * Returns 0 for success.
2020 */
2021int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2022 struct snd_ctl_elem_value *ucontrol)
2023{
4eaa9819
JS
2024 struct soc_mixer_control *mc =
2025 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2026 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
2027 unsigned int reg = mc->reg;
2028 unsigned int shift = mc->shift;
2029 unsigned int rshift = mc->rshift;
4eaa9819 2030 int max = mc->max;
815ecf8d
JS
2031 unsigned int mask = (1 << fls(max)) - 1;
2032 unsigned int invert = mc->invert;
46f5822f 2033 unsigned int val, val2, val_mask;
db2a4165
FM
2034
2035 val = (ucontrol->value.integer.value[0] & mask);
2036 if (invert)
a7a4ac86 2037 val = max - val;
db2a4165
FM
2038 val_mask = mask << shift;
2039 val = val << shift;
2040 if (shift != rshift) {
2041 val2 = (ucontrol->value.integer.value[1] & mask);
2042 if (invert)
a7a4ac86 2043 val2 = max - val2;
db2a4165
FM
2044 val_mask |= mask << rshift;
2045 val |= val2 << rshift;
2046 }
6c508c62 2047 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
db2a4165
FM
2048}
2049EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2050
2051/**
2052 * snd_soc_info_volsw_2r - double mixer info callback
2053 * @kcontrol: mixer control
2054 * @uinfo: control element information
2055 *
2056 * Callback to provide information about a double mixer control that
2057 * spans 2 codec registers.
2058 *
2059 * Returns 0 for success.
2060 */
2061int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2062 struct snd_ctl_elem_info *uinfo)
2063{
4eaa9819
JS
2064 struct soc_mixer_control *mc =
2065 (struct soc_mixer_control *)kcontrol->private_value;
2066 int max = mc->max;
a7a4ac86 2067
fd5dfad9 2068 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
a7a4ac86
PZ
2069 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2070 else
2071 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 2072
db2a4165
FM
2073 uinfo->count = 2;
2074 uinfo->value.integer.min = 0;
a7a4ac86 2075 uinfo->value.integer.max = max;
db2a4165
FM
2076 return 0;
2077}
2078EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2079
2080/**
2081 * snd_soc_get_volsw_2r - double mixer get callback
2082 * @kcontrol: mixer control
ac11a2b3 2083 * @ucontrol: control element information
db2a4165
FM
2084 *
2085 * Callback to get the value of a double mixer control that spans 2 registers.
2086 *
2087 * Returns 0 for success.
2088 */
2089int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2090 struct snd_ctl_elem_value *ucontrol)
2091{
4eaa9819
JS
2092 struct soc_mixer_control *mc =
2093 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2094 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
2095 unsigned int reg = mc->reg;
2096 unsigned int reg2 = mc->rreg;
2097 unsigned int shift = mc->shift;
4eaa9819 2098 int max = mc->max;
46f5822f 2099 unsigned int mask = (1 << fls(max)) - 1;
815ecf8d 2100 unsigned int invert = mc->invert;
db2a4165
FM
2101
2102 ucontrol->value.integer.value[0] =
2103 (snd_soc_read(codec, reg) >> shift) & mask;
2104 ucontrol->value.integer.value[1] =
2105 (snd_soc_read(codec, reg2) >> shift) & mask;
2106 if (invert) {
2107 ucontrol->value.integer.value[0] =
a7a4ac86 2108 max - ucontrol->value.integer.value[0];
db2a4165 2109 ucontrol->value.integer.value[1] =
a7a4ac86 2110 max - ucontrol->value.integer.value[1];
db2a4165
FM
2111 }
2112
2113 return 0;
2114}
2115EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2116
2117/**
2118 * snd_soc_put_volsw_2r - double mixer set callback
2119 * @kcontrol: mixer control
ac11a2b3 2120 * @ucontrol: control element information
db2a4165
FM
2121 *
2122 * Callback to set the value of a double mixer control that spans 2 registers.
2123 *
2124 * Returns 0 for success.
2125 */
2126int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_value *ucontrol)
2128{
4eaa9819
JS
2129 struct soc_mixer_control *mc =
2130 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2131 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
2132 unsigned int reg = mc->reg;
2133 unsigned int reg2 = mc->rreg;
2134 unsigned int shift = mc->shift;
4eaa9819 2135 int max = mc->max;
815ecf8d
JS
2136 unsigned int mask = (1 << fls(max)) - 1;
2137 unsigned int invert = mc->invert;
db2a4165 2138 int err;
46f5822f 2139 unsigned int val, val2, val_mask;
db2a4165
FM
2140
2141 val_mask = mask << shift;
2142 val = (ucontrol->value.integer.value[0] & mask);
2143 val2 = (ucontrol->value.integer.value[1] & mask);
2144
2145 if (invert) {
a7a4ac86
PZ
2146 val = max - val;
2147 val2 = max - val2;
db2a4165
FM
2148 }
2149
2150 val = val << shift;
2151 val2 = val2 << shift;
2152
6c508c62 2153 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
3ff3f64b 2154 if (err < 0)
db2a4165
FM
2155 return err;
2156
6c508c62 2157 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
db2a4165
FM
2158 return err;
2159}
2160EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2161
e13ac2e9
MB
2162/**
2163 * snd_soc_info_volsw_s8 - signed mixer info callback
2164 * @kcontrol: mixer control
2165 * @uinfo: control element information
2166 *
2167 * Callback to provide information about a signed mixer control.
2168 *
2169 * Returns 0 for success.
2170 */
2171int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2172 struct snd_ctl_elem_info *uinfo)
2173{
4eaa9819
JS
2174 struct soc_mixer_control *mc =
2175 (struct soc_mixer_control *)kcontrol->private_value;
2176 int max = mc->max;
2177 int min = mc->min;
e13ac2e9
MB
2178
2179 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2180 uinfo->count = 2;
2181 uinfo->value.integer.min = 0;
2182 uinfo->value.integer.max = max-min;
2183 return 0;
2184}
2185EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2186
2187/**
2188 * snd_soc_get_volsw_s8 - signed mixer get callback
2189 * @kcontrol: mixer control
ac11a2b3 2190 * @ucontrol: control element information
e13ac2e9
MB
2191 *
2192 * Callback to get the value of a signed mixer control.
2193 *
2194 * Returns 0 for success.
2195 */
2196int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2197 struct snd_ctl_elem_value *ucontrol)
2198{
4eaa9819
JS
2199 struct soc_mixer_control *mc =
2200 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 2201 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 2202 unsigned int reg = mc->reg;
4eaa9819 2203 int min = mc->min;
e13ac2e9
MB
2204 int val = snd_soc_read(codec, reg);
2205
2206 ucontrol->value.integer.value[0] =
2207 ((signed char)(val & 0xff))-min;
2208 ucontrol->value.integer.value[1] =
2209 ((signed char)((val >> 8) & 0xff))-min;
2210 return 0;
2211}
2212EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2213
2214/**
2215 * snd_soc_put_volsw_sgn - signed mixer put callback
2216 * @kcontrol: mixer control
ac11a2b3 2217 * @ucontrol: control element information
e13ac2e9
MB
2218 *
2219 * Callback to set the value of a signed mixer control.
2220 *
2221 * Returns 0 for success.
2222 */
2223int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2224 struct snd_ctl_elem_value *ucontrol)
2225{
4eaa9819
JS
2226 struct soc_mixer_control *mc =
2227 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 2228 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 2229 unsigned int reg = mc->reg;
4eaa9819 2230 int min = mc->min;
46f5822f 2231 unsigned int val;
e13ac2e9
MB
2232
2233 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2234 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2235
6c508c62 2236 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
e13ac2e9
MB
2237}
2238EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2239
8c6529db
LG
2240/**
2241 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2242 * @dai: DAI
2243 * @clk_id: DAI specific clock ID
2244 * @freq: new clock frequency in Hz
2245 * @dir: new clock direction - input/output.
2246 *
2247 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2248 */
2249int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2250 unsigned int freq, int dir)
2251{
3f1a4d82 2252 if (dai->ops && dai->ops->set_sysclk)
6335d055 2253 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
8c6529db
LG
2254 else
2255 return -EINVAL;
2256}
2257EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2258
2259/**
2260 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2261 * @dai: DAI
ac11a2b3 2262 * @div_id: DAI specific clock divider ID
8c6529db
LG
2263 * @div: new clock divisor.
2264 *
2265 * Configures the clock dividers. This is used to derive the best DAI bit and
2266 * frame clocks from the system or master clock. It's best to set the DAI bit
2267 * and frame clocks as low as possible to save system power.
2268 */
2269int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2270 int div_id, int div)
2271{
3f1a4d82 2272 if (dai->ops && dai->ops->set_clkdiv)
6335d055 2273 return dai->ops->set_clkdiv(dai, div_id, div);
8c6529db
LG
2274 else
2275 return -EINVAL;
2276}
2277EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2278
2279/**
2280 * snd_soc_dai_set_pll - configure DAI PLL.
2281 * @dai: DAI
2282 * @pll_id: DAI specific PLL ID
85488037 2283 * @source: DAI specific source for the PLL
8c6529db
LG
2284 * @freq_in: PLL input clock frequency in Hz
2285 * @freq_out: requested PLL output clock frequency in Hz
2286 *
2287 * Configures and enables PLL to generate output clock based on input clock.
2288 */
85488037
MB
2289int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2290 unsigned int freq_in, unsigned int freq_out)
8c6529db 2291{
3f1a4d82 2292 if (dai->ops && dai->ops->set_pll)
85488037
MB
2293 return dai->ops->set_pll(dai, pll_id, source,
2294 freq_in, freq_out);
8c6529db
LG
2295 else
2296 return -EINVAL;
2297}
2298EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2299
2300/**
2301 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2302 * @dai: DAI
8c6529db
LG
2303 * @fmt: SND_SOC_DAIFMT_ format value.
2304 *
2305 * Configures the DAI hardware format and clocking.
2306 */
2307int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2308{
3f1a4d82 2309 if (dai->ops && dai->ops->set_fmt)
6335d055 2310 return dai->ops->set_fmt(dai, fmt);
8c6529db
LG
2311 else
2312 return -EINVAL;
2313}
2314EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2315
2316/**
2317 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2318 * @dai: DAI
a5479e38
DR
2319 * @tx_mask: bitmask representing active TX slots.
2320 * @rx_mask: bitmask representing active RX slots.
8c6529db 2321 * @slots: Number of slots in use.
a5479e38 2322 * @slot_width: Width in bits for each slot.
8c6529db
LG
2323 *
2324 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2325 * specific.
2326 */
2327int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
a5479e38 2328 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
8c6529db 2329{
3f1a4d82 2330 if (dai->ops && dai->ops->set_tdm_slot)
a5479e38
DR
2331 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2332 slots, slot_width);
8c6529db
LG
2333 else
2334 return -EINVAL;
2335}
2336EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2337
472df3cb
BS
2338/**
2339 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2340 * @dai: DAI
2341 * @tx_num: how many TX channels
2342 * @tx_slot: pointer to an array which imply the TX slot number channel
2343 * 0~num-1 uses
2344 * @rx_num: how many RX channels
2345 * @rx_slot: pointer to an array which imply the RX slot number channel
2346 * 0~num-1 uses
2347 *
2348 * configure the relationship between channel number and TDM slot number.
2349 */
2350int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2351 unsigned int tx_num, unsigned int *tx_slot,
2352 unsigned int rx_num, unsigned int *rx_slot)
2353{
2354 if (dai->ops && dai->ops->set_channel_map)
2355 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2356 rx_num, rx_slot);
2357 else
2358 return -EINVAL;
2359}
2360EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2361
8c6529db
LG
2362/**
2363 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2364 * @dai: DAI
2365 * @tristate: tristate enable
2366 *
2367 * Tristates the DAI so that others can use it.
2368 */
2369int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2370{
3f1a4d82 2371 if (dai->ops && dai->ops->set_tristate)
6335d055 2372 return dai->ops->set_tristate(dai, tristate);
8c6529db
LG
2373 else
2374 return -EINVAL;
2375}
2376EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2377
2378/**
2379 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2380 * @dai: DAI
2381 * @mute: mute enable
2382 *
2383 * Mutes the DAI DAC.
2384 */
2385int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2386{
3f1a4d82 2387 if (dai->ops && dai->ops->digital_mute)
6335d055 2388 return dai->ops->digital_mute(dai, mute);
8c6529db
LG
2389 else
2390 return -EINVAL;
2391}
2392EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2393
c5af3a2e
MB
2394/**
2395 * snd_soc_register_card - Register a card with the ASoC core
2396 *
ac11a2b3 2397 * @card: Card to register
c5af3a2e
MB
2398 *
2399 * Note that currently this is an internal only function: it will be
2400 * exposed to machine drivers after further backporting of ASoC v2
2401 * registration APIs.
2402 */
2403static int snd_soc_register_card(struct snd_soc_card *card)
2404{
2405 if (!card->name || !card->dev)
2406 return -EINVAL;
2407
2408 INIT_LIST_HEAD(&card->list);
2409 card->instantiated = 0;
2410
2411 mutex_lock(&client_mutex);
2412 list_add(&card->list, &card_list);
435c5e25 2413 snd_soc_instantiate_cards();
c5af3a2e
MB
2414 mutex_unlock(&client_mutex);
2415
2416 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2417
2418 return 0;
2419}
2420
2421/**
2422 * snd_soc_unregister_card - Unregister a card with the ASoC core
2423 *
ac11a2b3 2424 * @card: Card to unregister
c5af3a2e
MB
2425 *
2426 * Note that currently this is an internal only function: it will be
2427 * exposed to machine drivers after further backporting of ASoC v2
2428 * registration APIs.
2429 */
2430static int snd_soc_unregister_card(struct snd_soc_card *card)
2431{
2432 mutex_lock(&client_mutex);
2433 list_del(&card->list);
2434 mutex_unlock(&client_mutex);
2435
2436 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2437
2438 return 0;
2439}
2440
9115171a
MB
2441/**
2442 * snd_soc_register_dai - Register a DAI with the ASoC core
2443 *
ac11a2b3 2444 * @dai: DAI to register
9115171a
MB
2445 */
2446int snd_soc_register_dai(struct snd_soc_dai *dai)
2447{
2448 if (!dai->name)
2449 return -EINVAL;
2450
2451 /* The device should become mandatory over time */
2452 if (!dai->dev)
2453 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2454
6335d055
EM
2455 if (!dai->ops)
2456 dai->ops = &null_dai_ops;
2457
9115171a
MB
2458 INIT_LIST_HEAD(&dai->list);
2459
2460 mutex_lock(&client_mutex);
2461 list_add(&dai->list, &dai_list);
435c5e25 2462 snd_soc_instantiate_cards();
9115171a
MB
2463 mutex_unlock(&client_mutex);
2464
2465 pr_debug("Registered DAI '%s'\n", dai->name);
2466
2467 return 0;
2468}
2469EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2470
2471/**
2472 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2473 *
ac11a2b3 2474 * @dai: DAI to unregister
9115171a
MB
2475 */
2476void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2477{
2478 mutex_lock(&client_mutex);
2479 list_del(&dai->list);
2480 mutex_unlock(&client_mutex);
2481
2482 pr_debug("Unregistered DAI '%s'\n", dai->name);
2483}
2484EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2485
2486/**
2487 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2488 *
ac11a2b3
MB
2489 * @dai: Array of DAIs to register
2490 * @count: Number of DAIs
9115171a
MB
2491 */
2492int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2493{
2494 int i, ret;
2495
2496 for (i = 0; i < count; i++) {
2497 ret = snd_soc_register_dai(&dai[i]);
2498 if (ret != 0)
2499 goto err;
2500 }
2501
2502 return 0;
2503
2504err:
2505 for (i--; i >= 0; i--)
2506 snd_soc_unregister_dai(&dai[i]);
2507
2508 return ret;
2509}
2510EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2511
2512/**
2513 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2514 *
ac11a2b3
MB
2515 * @dai: Array of DAIs to unregister
2516 * @count: Number of DAIs
9115171a
MB
2517 */
2518void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2519{
2520 int i;
2521
2522 for (i = 0; i < count; i++)
2523 snd_soc_unregister_dai(&dai[i]);
2524}
2525EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2526
12a48a8c
MB
2527/**
2528 * snd_soc_register_platform - Register a platform with the ASoC core
2529 *
ac11a2b3 2530 * @platform: platform to register
12a48a8c
MB
2531 */
2532int snd_soc_register_platform(struct snd_soc_platform *platform)
2533{
2534 if (!platform->name)
2535 return -EINVAL;
2536
2537 INIT_LIST_HEAD(&platform->list);
2538
2539 mutex_lock(&client_mutex);
2540 list_add(&platform->list, &platform_list);
435c5e25 2541 snd_soc_instantiate_cards();
12a48a8c
MB
2542 mutex_unlock(&client_mutex);
2543
2544 pr_debug("Registered platform '%s'\n", platform->name);
2545
2546 return 0;
2547}
2548EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2549
2550/**
2551 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2552 *
ac11a2b3 2553 * @platform: platform to unregister
12a48a8c
MB
2554 */
2555void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2556{
2557 mutex_lock(&client_mutex);
2558 list_del(&platform->list);
2559 mutex_unlock(&client_mutex);
2560
2561 pr_debug("Unregistered platform '%s'\n", platform->name);
2562}
2563EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2564
151ab22c
MB
2565static u64 codec_format_map[] = {
2566 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2567 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2568 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2569 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2570 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2571 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2572 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2573 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2574 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2575 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2576 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2577 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2578 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2579 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2580 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2581 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2582};
2583
2584/* Fix up the DAI formats for endianness: codecs don't actually see
2585 * the endianness of the data but we're using the CPU format
2586 * definitions which do need to include endianness so we ensure that
2587 * codec DAIs always have both big and little endian variants set.
2588 */
2589static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2590{
2591 int i;
2592
2593 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2594 if (stream->formats & codec_format_map[i])
2595 stream->formats |= codec_format_map[i];
2596}
2597
0d0cf00a
MB
2598/**
2599 * snd_soc_register_codec - Register a codec with the ASoC core
2600 *
ac11a2b3 2601 * @codec: codec to register
0d0cf00a
MB
2602 */
2603int snd_soc_register_codec(struct snd_soc_codec *codec)
2604{
151ab22c
MB
2605 int i;
2606
0d0cf00a
MB
2607 if (!codec->name)
2608 return -EINVAL;
2609
2610 /* The device should become mandatory over time */
2611 if (!codec->dev)
2612 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2613
2614 INIT_LIST_HEAD(&codec->list);
2615
151ab22c
MB
2616 for (i = 0; i < codec->num_dai; i++) {
2617 fixup_codec_formats(&codec->dai[i].playback);
2618 fixup_codec_formats(&codec->dai[i].capture);
2619 }
2620
0d0cf00a
MB
2621 mutex_lock(&client_mutex);
2622 list_add(&codec->list, &codec_list);
2623 snd_soc_instantiate_cards();
2624 mutex_unlock(&client_mutex);
2625
2626 pr_debug("Registered codec '%s'\n", codec->name);
2627
2628 return 0;
2629}
2630EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2631
2632/**
2633 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2634 *
ac11a2b3 2635 * @codec: codec to unregister
0d0cf00a
MB
2636 */
2637void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2638{
2639 mutex_lock(&client_mutex);
2640 list_del(&codec->list);
2641 mutex_unlock(&client_mutex);
2642
2643 pr_debug("Unregistered codec '%s'\n", codec->name);
2644}
2645EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2646
c9b3a40f 2647static int __init snd_soc_init(void)
db2a4165 2648{
384c89e2
MB
2649#ifdef CONFIG_DEBUG_FS
2650 debugfs_root = debugfs_create_dir("asoc", NULL);
2651 if (IS_ERR(debugfs_root) || !debugfs_root) {
2652 printk(KERN_WARNING
2653 "ASoC: Failed to create debugfs directory\n");
2654 debugfs_root = NULL;
2655 }
2656#endif
2657
db2a4165
FM
2658 return platform_driver_register(&soc_driver);
2659}
2660
7d8c16a6 2661static void __exit snd_soc_exit(void)
db2a4165 2662{
384c89e2
MB
2663#ifdef CONFIG_DEBUG_FS
2664 debugfs_remove_recursive(debugfs_root);
2665#endif
3ff3f64b 2666 platform_driver_unregister(&soc_driver);
db2a4165
FM
2667}
2668
2669module_init(snd_soc_init);
2670module_exit(snd_soc_exit);
2671
2672/* Module information */
d331124d 2673MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
db2a4165
FM
2674MODULE_DESCRIPTION("ALSA SoC Core");
2675MODULE_LICENSE("GPL");
8b45a209 2676MODULE_ALIAS("platform:soc-audio");