ASoC: Update PM ifdefs for exported suspend/resume
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / soc / soc-core.c
1 /*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
18 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
34 #include <sound/ac97_codec.h>
35 #include <sound/core.h>
36 #include <sound/jack.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include <sound/soc.h>
40 #include <sound/initval.h>
41
42 #define CREATE_TRACE_POINTS
43 #include <trace/events/asoc.h>
44
45 #define NAME_SIZE 32
46
47 static DEFINE_MUTEX(pcm_mutex);
48 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
49
50 #ifdef CONFIG_DEBUG_FS
51 struct dentry *snd_soc_debugfs_root;
52 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
53 #endif
54
55 static DEFINE_MUTEX(client_mutex);
56 static LIST_HEAD(card_list);
57 static LIST_HEAD(dai_list);
58 static LIST_HEAD(platform_list);
59 static LIST_HEAD(codec_list);
60
61 static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
62
63 /*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68 static int pmdown_time = 5000;
69 module_param(pmdown_time, int, 0);
70 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
72 /* returns the minimum number of bytes needed to represent
73 * a particular given value */
74 static int min_bytes_needed(unsigned long val)
75 {
76 int c = 0;
77 int i;
78
79 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
80 if (val & (1UL << i))
81 break;
82 c = (sizeof val * 8) - c;
83 if (!c || (c % 8))
84 c = (c + 8) / 8;
85 else
86 c /= 8;
87 return c;
88 }
89
90 /* codec register dump */
91 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
92 {
93 int ret, i, step = 1, count = 0;
94 int wordsize, regsize;
95
96 wordsize = codec->driver->reg_word_size * 2;
97 regsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
98
99 if (!codec->driver->reg_cache_size)
100 return 0;
101
102 if (codec->driver->reg_cache_step)
103 step = codec->driver->reg_cache_step;
104
105 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
106 if (codec->readable_register && !codec->readable_register(codec, i))
107 continue;
108
109 count += sprintf(buf + count, "%.*x: ", regsize, i);
110 if (count >= PAGE_SIZE - 1)
111 break;
112
113 if (codec->driver->display_register) {
114 count += codec->driver->display_register(codec, buf + count,
115 PAGE_SIZE - count, i);
116 } else {
117 /* If the read fails it's almost certainly due to
118 * the register being volatile and the device being
119 * powered off.
120 */
121 ret = snd_soc_read(codec, i);
122 if (ret >= 0)
123 count += snprintf(buf + count,
124 PAGE_SIZE - count,
125 "%.*x", wordsize, ret);
126 else
127 count += snprintf(buf + count,
128 PAGE_SIZE - count,
129 "<no data: %d>", ret);
130 }
131
132 if (count >= PAGE_SIZE - 1)
133 break;
134
135 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
136 if (count >= PAGE_SIZE - 1)
137 break;
138 }
139
140 /* Truncate count; min() would cause a warning */
141 if (count >= PAGE_SIZE)
142 count = PAGE_SIZE - 1;
143
144 return count;
145 }
146 static ssize_t codec_reg_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
148 {
149 struct snd_soc_pcm_runtime *rtd =
150 container_of(dev, struct snd_soc_pcm_runtime, dev);
151
152 return soc_codec_reg_show(rtd->codec, buf);
153 }
154
155 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
156
157 static ssize_t pmdown_time_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
159 {
160 struct snd_soc_pcm_runtime *rtd =
161 container_of(dev, struct snd_soc_pcm_runtime, dev);
162
163 return sprintf(buf, "%ld\n", rtd->pmdown_time);
164 }
165
166 static ssize_t pmdown_time_set(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf, size_t count)
169 {
170 struct snd_soc_pcm_runtime *rtd =
171 container_of(dev, struct snd_soc_pcm_runtime, dev);
172 int ret;
173
174 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
175 if (ret)
176 return ret;
177
178 return count;
179 }
180
181 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
182
183 #ifdef CONFIG_DEBUG_FS
184 static int codec_reg_open_file(struct inode *inode, struct file *file)
185 {
186 file->private_data = inode->i_private;
187 return 0;
188 }
189
190 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
191 size_t count, loff_t *ppos)
192 {
193 ssize_t ret;
194 struct snd_soc_codec *codec = file->private_data;
195 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
196 if (!buf)
197 return -ENOMEM;
198 ret = soc_codec_reg_show(codec, buf);
199 if (ret >= 0)
200 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
201 kfree(buf);
202 return ret;
203 }
204
205 static ssize_t codec_reg_write_file(struct file *file,
206 const char __user *user_buf, size_t count, loff_t *ppos)
207 {
208 char buf[32];
209 int buf_size;
210 char *start = buf;
211 unsigned long reg, value;
212 int step = 1;
213 struct snd_soc_codec *codec = file->private_data;
214
215 buf_size = min(count, (sizeof(buf)-1));
216 if (copy_from_user(buf, user_buf, buf_size))
217 return -EFAULT;
218 buf[buf_size] = 0;
219
220 if (codec->driver->reg_cache_step)
221 step = codec->driver->reg_cache_step;
222
223 while (*start == ' ')
224 start++;
225 reg = simple_strtoul(start, &start, 16);
226 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
227 return -EINVAL;
228 while (*start == ' ')
229 start++;
230 if (strict_strtoul(start, 16, &value))
231 return -EINVAL;
232
233 /* Userspace has been fiddling around behind the kernel's back */
234 add_taint(TAINT_USER);
235
236 snd_soc_write(codec, reg, value);
237 return buf_size;
238 }
239
240 static const struct file_operations codec_reg_fops = {
241 .open = codec_reg_open_file,
242 .read = codec_reg_read_file,
243 .write = codec_reg_write_file,
244 .llseek = default_llseek,
245 };
246
247 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
248 {
249 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
250
251 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
252 debugfs_card_root);
253 if (!codec->debugfs_codec_root) {
254 printk(KERN_WARNING
255 "ASoC: Failed to create codec debugfs directory\n");
256 return;
257 }
258
259 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
260 &codec->cache_sync);
261 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
262 &codec->cache_only);
263
264 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
265 codec->debugfs_codec_root,
266 codec, &codec_reg_fops);
267 if (!codec->debugfs_reg)
268 printk(KERN_WARNING
269 "ASoC: Failed to create codec register debugfs file\n");
270
271 codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
272 codec->debugfs_codec_root);
273 if (!codec->dapm.debugfs_dapm)
274 printk(KERN_WARNING
275 "Failed to create DAPM debugfs directory\n");
276
277 snd_soc_dapm_debugfs_init(&codec->dapm);
278 }
279
280 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
281 {
282 debugfs_remove_recursive(codec->debugfs_codec_root);
283 }
284
285 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
286 size_t count, loff_t *ppos)
287 {
288 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
289 ssize_t len, ret = 0;
290 struct snd_soc_codec *codec;
291
292 if (!buf)
293 return -ENOMEM;
294
295 list_for_each_entry(codec, &codec_list, list) {
296 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
297 codec->name);
298 if (len >= 0)
299 ret += len;
300 if (ret > PAGE_SIZE) {
301 ret = PAGE_SIZE;
302 break;
303 }
304 }
305
306 if (ret >= 0)
307 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
308
309 kfree(buf);
310
311 return ret;
312 }
313
314 static const struct file_operations codec_list_fops = {
315 .read = codec_list_read_file,
316 .llseek = default_llseek,/* read accesses f_pos */
317 };
318
319 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
320 size_t count, loff_t *ppos)
321 {
322 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
323 ssize_t len, ret = 0;
324 struct snd_soc_dai *dai;
325
326 if (!buf)
327 return -ENOMEM;
328
329 list_for_each_entry(dai, &dai_list, list) {
330 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
331 if (len >= 0)
332 ret += len;
333 if (ret > PAGE_SIZE) {
334 ret = PAGE_SIZE;
335 break;
336 }
337 }
338
339 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
340
341 kfree(buf);
342
343 return ret;
344 }
345
346 static const struct file_operations dai_list_fops = {
347 .read = dai_list_read_file,
348 .llseek = default_llseek,/* read accesses f_pos */
349 };
350
351 static ssize_t platform_list_read_file(struct file *file,
352 char __user *user_buf,
353 size_t count, loff_t *ppos)
354 {
355 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
356 ssize_t len, ret = 0;
357 struct snd_soc_platform *platform;
358
359 if (!buf)
360 return -ENOMEM;
361
362 list_for_each_entry(platform, &platform_list, list) {
363 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
364 platform->name);
365 if (len >= 0)
366 ret += len;
367 if (ret > PAGE_SIZE) {
368 ret = PAGE_SIZE;
369 break;
370 }
371 }
372
373 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
374
375 kfree(buf);
376
377 return ret;
378 }
379
380 static const struct file_operations platform_list_fops = {
381 .read = platform_list_read_file,
382 .llseek = default_llseek,/* read accesses f_pos */
383 };
384
385 static void soc_init_card_debugfs(struct snd_soc_card *card)
386 {
387 card->debugfs_card_root = debugfs_create_dir(card->name,
388 snd_soc_debugfs_root);
389 if (!card->debugfs_card_root) {
390 dev_warn(card->dev,
391 "ASoC: Failed to create codec debugfs directory\n");
392 return;
393 }
394
395 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
396 card->debugfs_card_root,
397 &card->pop_time);
398 if (!card->debugfs_pop_time)
399 dev_warn(card->dev,
400 "Failed to create pop time debugfs file\n");
401 }
402
403 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
404 {
405 debugfs_remove_recursive(card->debugfs_card_root);
406 }
407
408 #else
409
410 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
411 {
412 }
413
414 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
415 {
416 }
417
418 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
419 {
420 }
421
422 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
423 {
424 }
425 #endif
426
427 #ifdef CONFIG_SND_SOC_AC97_BUS
428 /* unregister ac97 codec */
429 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
430 {
431 if (codec->ac97->dev.bus)
432 device_unregister(&codec->ac97->dev);
433 return 0;
434 }
435
436 /* stop no dev release warning */
437 static void soc_ac97_device_release(struct device *dev){}
438
439 /* register ac97 codec to bus */
440 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
441 {
442 int err;
443
444 codec->ac97->dev.bus = &ac97_bus_type;
445 codec->ac97->dev.parent = codec->card->dev;
446 codec->ac97->dev.release = soc_ac97_device_release;
447
448 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
449 codec->card->snd_card->number, 0, codec->name);
450 err = device_register(&codec->ac97->dev);
451 if (err < 0) {
452 snd_printk(KERN_ERR "Can't register ac97 bus\n");
453 codec->ac97->dev.bus = NULL;
454 return err;
455 }
456 return 0;
457 }
458 #endif
459
460 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
461 {
462 struct snd_soc_pcm_runtime *rtd = substream->private_data;
463 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
464 struct snd_soc_dai *codec_dai = rtd->codec_dai;
465 int ret;
466
467 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
468 rtd->dai_link->symmetric_rates) {
469 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
470 rtd->rate);
471
472 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
473 SNDRV_PCM_HW_PARAM_RATE,
474 rtd->rate,
475 rtd->rate);
476 if (ret < 0) {
477 dev_err(&rtd->dev,
478 "Unable to apply rate symmetry constraint: %d\n", ret);
479 return ret;
480 }
481 }
482
483 return 0;
484 }
485
486 /*
487 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
488 * then initialized and any private data can be allocated. This also calls
489 * startup for the cpu DAI, platform, machine and codec DAI.
490 */
491 static int soc_pcm_open(struct snd_pcm_substream *substream)
492 {
493 struct snd_soc_pcm_runtime *rtd = substream->private_data;
494 struct snd_pcm_runtime *runtime = substream->runtime;
495 struct snd_soc_platform *platform = rtd->platform;
496 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
497 struct snd_soc_dai *codec_dai = rtd->codec_dai;
498 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
499 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
500 int ret = 0;
501
502 mutex_lock(&pcm_mutex);
503
504 /* startup the audio subsystem */
505 if (cpu_dai->driver->ops->startup) {
506 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
507 if (ret < 0) {
508 printk(KERN_ERR "asoc: can't open interface %s\n",
509 cpu_dai->name);
510 goto out;
511 }
512 }
513
514 if (platform->driver->ops->open) {
515 ret = platform->driver->ops->open(substream);
516 if (ret < 0) {
517 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
518 goto platform_err;
519 }
520 }
521
522 if (codec_dai->driver->ops->startup) {
523 ret = codec_dai->driver->ops->startup(substream, codec_dai);
524 if (ret < 0) {
525 printk(KERN_ERR "asoc: can't open codec %s\n",
526 codec_dai->name);
527 goto codec_dai_err;
528 }
529 }
530
531 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
532 ret = rtd->dai_link->ops->startup(substream);
533 if (ret < 0) {
534 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
535 goto machine_err;
536 }
537 }
538
539 /* Check that the codec and cpu DAIs are compatible */
540 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
541 runtime->hw.rate_min =
542 max(codec_dai_drv->playback.rate_min,
543 cpu_dai_drv->playback.rate_min);
544 runtime->hw.rate_max =
545 min(codec_dai_drv->playback.rate_max,
546 cpu_dai_drv->playback.rate_max);
547 runtime->hw.channels_min =
548 max(codec_dai_drv->playback.channels_min,
549 cpu_dai_drv->playback.channels_min);
550 runtime->hw.channels_max =
551 min(codec_dai_drv->playback.channels_max,
552 cpu_dai_drv->playback.channels_max);
553 runtime->hw.formats =
554 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
555 runtime->hw.rates =
556 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
557 if (codec_dai_drv->playback.rates
558 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
559 runtime->hw.rates |= cpu_dai_drv->playback.rates;
560 if (cpu_dai_drv->playback.rates
561 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
562 runtime->hw.rates |= codec_dai_drv->playback.rates;
563 } else {
564 runtime->hw.rate_min =
565 max(codec_dai_drv->capture.rate_min,
566 cpu_dai_drv->capture.rate_min);
567 runtime->hw.rate_max =
568 min(codec_dai_drv->capture.rate_max,
569 cpu_dai_drv->capture.rate_max);
570 runtime->hw.channels_min =
571 max(codec_dai_drv->capture.channels_min,
572 cpu_dai_drv->capture.channels_min);
573 runtime->hw.channels_max =
574 min(codec_dai_drv->capture.channels_max,
575 cpu_dai_drv->capture.channels_max);
576 runtime->hw.formats =
577 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
578 runtime->hw.rates =
579 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
580 if (codec_dai_drv->capture.rates
581 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
582 runtime->hw.rates |= cpu_dai_drv->capture.rates;
583 if (cpu_dai_drv->capture.rates
584 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
585 runtime->hw.rates |= codec_dai_drv->capture.rates;
586 }
587
588 snd_pcm_limit_hw_rates(runtime);
589 if (!runtime->hw.rates) {
590 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
591 codec_dai->name, cpu_dai->name);
592 goto config_err;
593 }
594 if (!runtime->hw.formats) {
595 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
596 codec_dai->name, cpu_dai->name);
597 goto config_err;
598 }
599 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
600 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
601 codec_dai->name, cpu_dai->name);
602 goto config_err;
603 }
604
605 /* Symmetry only applies if we've already got an active stream. */
606 if (cpu_dai->active || codec_dai->active) {
607 ret = soc_pcm_apply_symmetry(substream);
608 if (ret != 0)
609 goto config_err;
610 }
611
612 pr_debug("asoc: %s <-> %s info:\n",
613 codec_dai->name, cpu_dai->name);
614 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
615 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
616 runtime->hw.channels_max);
617 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
618 runtime->hw.rate_max);
619
620 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
621 cpu_dai->playback_active++;
622 codec_dai->playback_active++;
623 } else {
624 cpu_dai->capture_active++;
625 codec_dai->capture_active++;
626 }
627 cpu_dai->active++;
628 codec_dai->active++;
629 rtd->codec->active++;
630 mutex_unlock(&pcm_mutex);
631 return 0;
632
633 config_err:
634 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
635 rtd->dai_link->ops->shutdown(substream);
636
637 machine_err:
638 if (codec_dai->driver->ops->shutdown)
639 codec_dai->driver->ops->shutdown(substream, codec_dai);
640
641 codec_dai_err:
642 if (platform->driver->ops->close)
643 platform->driver->ops->close(substream);
644
645 platform_err:
646 if (cpu_dai->driver->ops->shutdown)
647 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
648 out:
649 mutex_unlock(&pcm_mutex);
650 return ret;
651 }
652
653 /*
654 * Power down the audio subsystem pmdown_time msecs after close is called.
655 * This is to ensure there are no pops or clicks in between any music tracks
656 * due to DAPM power cycling.
657 */
658 static void close_delayed_work(struct work_struct *work)
659 {
660 struct snd_soc_pcm_runtime *rtd =
661 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
662 struct snd_soc_dai *codec_dai = rtd->codec_dai;
663
664 mutex_lock(&pcm_mutex);
665
666 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
667 codec_dai->driver->playback.stream_name,
668 codec_dai->playback_active ? "active" : "inactive",
669 codec_dai->pop_wait ? "yes" : "no");
670
671 /* are we waiting on this codec DAI stream */
672 if (codec_dai->pop_wait == 1) {
673 codec_dai->pop_wait = 0;
674 snd_soc_dapm_stream_event(rtd,
675 codec_dai->driver->playback.stream_name,
676 SND_SOC_DAPM_STREAM_STOP);
677 }
678
679 mutex_unlock(&pcm_mutex);
680 }
681
682 /*
683 * Called by ALSA when a PCM substream is closed. Private data can be
684 * freed here. The cpu DAI, codec DAI, machine and platform are also
685 * shutdown.
686 */
687 static int soc_codec_close(struct snd_pcm_substream *substream)
688 {
689 struct snd_soc_pcm_runtime *rtd = substream->private_data;
690 struct snd_soc_platform *platform = rtd->platform;
691 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
692 struct snd_soc_dai *codec_dai = rtd->codec_dai;
693 struct snd_soc_codec *codec = rtd->codec;
694
695 mutex_lock(&pcm_mutex);
696
697 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
698 cpu_dai->playback_active--;
699 codec_dai->playback_active--;
700 } else {
701 cpu_dai->capture_active--;
702 codec_dai->capture_active--;
703 }
704
705 cpu_dai->active--;
706 codec_dai->active--;
707 codec->active--;
708
709 /* Muting the DAC suppresses artifacts caused during digital
710 * shutdown, for example from stopping clocks.
711 */
712 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
713 snd_soc_dai_digital_mute(codec_dai, 1);
714
715 if (cpu_dai->driver->ops->shutdown)
716 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
717
718 if (codec_dai->driver->ops->shutdown)
719 codec_dai->driver->ops->shutdown(substream, codec_dai);
720
721 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
722 rtd->dai_link->ops->shutdown(substream);
723
724 if (platform->driver->ops->close)
725 platform->driver->ops->close(substream);
726 cpu_dai->runtime = NULL;
727
728 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
729 /* start delayed pop wq here for playback streams */
730 codec_dai->pop_wait = 1;
731 schedule_delayed_work(&rtd->delayed_work,
732 msecs_to_jiffies(rtd->pmdown_time));
733 } else {
734 /* capture streams can be powered down now */
735 snd_soc_dapm_stream_event(rtd,
736 codec_dai->driver->capture.stream_name,
737 SND_SOC_DAPM_STREAM_STOP);
738 }
739
740 mutex_unlock(&pcm_mutex);
741 return 0;
742 }
743
744 /*
745 * Called by ALSA when the PCM substream is prepared, can set format, sample
746 * rate, etc. This function is non atomic and can be called multiple times,
747 * it can refer to the runtime info.
748 */
749 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
750 {
751 struct snd_soc_pcm_runtime *rtd = substream->private_data;
752 struct snd_soc_platform *platform = rtd->platform;
753 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
754 struct snd_soc_dai *codec_dai = rtd->codec_dai;
755 int ret = 0;
756
757 mutex_lock(&pcm_mutex);
758
759 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
760 ret = rtd->dai_link->ops->prepare(substream);
761 if (ret < 0) {
762 printk(KERN_ERR "asoc: machine prepare error\n");
763 goto out;
764 }
765 }
766
767 if (platform->driver->ops->prepare) {
768 ret = platform->driver->ops->prepare(substream);
769 if (ret < 0) {
770 printk(KERN_ERR "asoc: platform prepare error\n");
771 goto out;
772 }
773 }
774
775 if (codec_dai->driver->ops->prepare) {
776 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
777 if (ret < 0) {
778 printk(KERN_ERR "asoc: codec DAI prepare error\n");
779 goto out;
780 }
781 }
782
783 if (cpu_dai->driver->ops->prepare) {
784 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
785 if (ret < 0) {
786 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
787 goto out;
788 }
789 }
790
791 /* cancel any delayed stream shutdown that is pending */
792 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
793 codec_dai->pop_wait) {
794 codec_dai->pop_wait = 0;
795 cancel_delayed_work(&rtd->delayed_work);
796 }
797
798 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
799 snd_soc_dapm_stream_event(rtd,
800 codec_dai->driver->playback.stream_name,
801 SND_SOC_DAPM_STREAM_START);
802 else
803 snd_soc_dapm_stream_event(rtd,
804 codec_dai->driver->capture.stream_name,
805 SND_SOC_DAPM_STREAM_START);
806
807 snd_soc_dai_digital_mute(codec_dai, 0);
808
809 out:
810 mutex_unlock(&pcm_mutex);
811 return ret;
812 }
813
814 /*
815 * Called by ALSA when the hardware params are set by application. This
816 * function can also be called multiple times and can allocate buffers
817 * (using snd_pcm_lib_* ). It's non-atomic.
818 */
819 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
820 struct snd_pcm_hw_params *params)
821 {
822 struct snd_soc_pcm_runtime *rtd = substream->private_data;
823 struct snd_soc_platform *platform = rtd->platform;
824 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
825 struct snd_soc_dai *codec_dai = rtd->codec_dai;
826 int ret = 0;
827
828 mutex_lock(&pcm_mutex);
829
830 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
831 ret = rtd->dai_link->ops->hw_params(substream, params);
832 if (ret < 0) {
833 printk(KERN_ERR "asoc: machine hw_params failed\n");
834 goto out;
835 }
836 }
837
838 if (codec_dai->driver->ops->hw_params) {
839 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
840 if (ret < 0) {
841 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
842 codec_dai->name);
843 goto codec_err;
844 }
845 }
846
847 if (cpu_dai->driver->ops->hw_params) {
848 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
849 if (ret < 0) {
850 printk(KERN_ERR "asoc: interface %s hw params failed\n",
851 cpu_dai->name);
852 goto interface_err;
853 }
854 }
855
856 if (platform->driver->ops->hw_params) {
857 ret = platform->driver->ops->hw_params(substream, params);
858 if (ret < 0) {
859 printk(KERN_ERR "asoc: platform %s hw params failed\n",
860 platform->name);
861 goto platform_err;
862 }
863 }
864
865 rtd->rate = params_rate(params);
866
867 out:
868 mutex_unlock(&pcm_mutex);
869 return ret;
870
871 platform_err:
872 if (cpu_dai->driver->ops->hw_free)
873 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
874
875 interface_err:
876 if (codec_dai->driver->ops->hw_free)
877 codec_dai->driver->ops->hw_free(substream, codec_dai);
878
879 codec_err:
880 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
881 rtd->dai_link->ops->hw_free(substream);
882
883 mutex_unlock(&pcm_mutex);
884 return ret;
885 }
886
887 /*
888 * Frees resources allocated by hw_params, can be called multiple times
889 */
890 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
891 {
892 struct snd_soc_pcm_runtime *rtd = substream->private_data;
893 struct snd_soc_platform *platform = rtd->platform;
894 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
895 struct snd_soc_dai *codec_dai = rtd->codec_dai;
896 struct snd_soc_codec *codec = rtd->codec;
897
898 mutex_lock(&pcm_mutex);
899
900 /* apply codec digital mute */
901 if (!codec->active)
902 snd_soc_dai_digital_mute(codec_dai, 1);
903
904 /* free any machine hw params */
905 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
906 rtd->dai_link->ops->hw_free(substream);
907
908 /* free any DMA resources */
909 if (platform->driver->ops->hw_free)
910 platform->driver->ops->hw_free(substream);
911
912 /* now free hw params for the DAIs */
913 if (codec_dai->driver->ops->hw_free)
914 codec_dai->driver->ops->hw_free(substream, codec_dai);
915
916 if (cpu_dai->driver->ops->hw_free)
917 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
918
919 mutex_unlock(&pcm_mutex);
920 return 0;
921 }
922
923 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
924 {
925 struct snd_soc_pcm_runtime *rtd = substream->private_data;
926 struct snd_soc_platform *platform = rtd->platform;
927 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
928 struct snd_soc_dai *codec_dai = rtd->codec_dai;
929 int ret;
930
931 if (codec_dai->driver->ops->trigger) {
932 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
933 if (ret < 0)
934 return ret;
935 }
936
937 if (platform->driver->ops->trigger) {
938 ret = platform->driver->ops->trigger(substream, cmd);
939 if (ret < 0)
940 return ret;
941 }
942
943 if (cpu_dai->driver->ops->trigger) {
944 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
945 if (ret < 0)
946 return ret;
947 }
948 return 0;
949 }
950
951 /*
952 * soc level wrapper for pointer callback
953 * If cpu_dai, codec_dai, platform driver has the delay callback, than
954 * the runtime->delay will be updated accordingly.
955 */
956 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
957 {
958 struct snd_soc_pcm_runtime *rtd = substream->private_data;
959 struct snd_soc_platform *platform = rtd->platform;
960 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
961 struct snd_soc_dai *codec_dai = rtd->codec_dai;
962 struct snd_pcm_runtime *runtime = substream->runtime;
963 snd_pcm_uframes_t offset = 0;
964 snd_pcm_sframes_t delay = 0;
965
966 if (platform->driver->ops->pointer)
967 offset = platform->driver->ops->pointer(substream);
968
969 if (cpu_dai->driver->ops->delay)
970 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
971
972 if (codec_dai->driver->ops->delay)
973 delay += codec_dai->driver->ops->delay(substream, codec_dai);
974
975 if (platform->driver->delay)
976 delay += platform->driver->delay(substream, codec_dai);
977
978 runtime->delay = delay;
979
980 return offset;
981 }
982
983 /* ASoC PCM operations */
984 static struct snd_pcm_ops soc_pcm_ops = {
985 .open = soc_pcm_open,
986 .close = soc_codec_close,
987 .hw_params = soc_pcm_hw_params,
988 .hw_free = soc_pcm_hw_free,
989 .prepare = soc_pcm_prepare,
990 .trigger = soc_pcm_trigger,
991 .pointer = soc_pcm_pointer,
992 };
993
994 #ifdef CONFIG_PM_SLEEP
995 /* powers down audio subsystem for suspend */
996 int snd_soc_suspend(struct device *dev)
997 {
998 struct snd_soc_card *card = dev_get_drvdata(dev);
999 struct snd_soc_codec *codec;
1000 int i;
1001
1002 /* If the initialization of this soc device failed, there is no codec
1003 * associated with it. Just bail out in this case.
1004 */
1005 if (list_empty(&card->codec_dev_list))
1006 return 0;
1007
1008 /* Due to the resume being scheduled into a workqueue we could
1009 * suspend before that's finished - wait for it to complete.
1010 */
1011 snd_power_lock(card->snd_card);
1012 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
1013 snd_power_unlock(card->snd_card);
1014
1015 /* we're going to block userspace touching us until resume completes */
1016 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
1017
1018 /* mute any active DACs */
1019 for (i = 0; i < card->num_rtd; i++) {
1020 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1021 struct snd_soc_dai_driver *drv = dai->driver;
1022
1023 if (card->rtd[i].dai_link->ignore_suspend)
1024 continue;
1025
1026 if (drv->ops->digital_mute && dai->playback_active)
1027 drv->ops->digital_mute(dai, 1);
1028 }
1029
1030 /* suspend all pcms */
1031 for (i = 0; i < card->num_rtd; i++) {
1032 if (card->rtd[i].dai_link->ignore_suspend)
1033 continue;
1034
1035 snd_pcm_suspend_all(card->rtd[i].pcm);
1036 }
1037
1038 if (card->suspend_pre)
1039 card->suspend_pre(card);
1040
1041 for (i = 0; i < card->num_rtd; i++) {
1042 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1043 struct snd_soc_platform *platform = card->rtd[i].platform;
1044
1045 if (card->rtd[i].dai_link->ignore_suspend)
1046 continue;
1047
1048 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1049 cpu_dai->driver->suspend(cpu_dai);
1050 if (platform->driver->suspend && !platform->suspended) {
1051 platform->driver->suspend(cpu_dai);
1052 platform->suspended = 1;
1053 }
1054 }
1055
1056 /* close any waiting streams and save state */
1057 for (i = 0; i < card->num_rtd; i++) {
1058 flush_delayed_work_sync(&card->rtd[i].delayed_work);
1059 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
1060 }
1061
1062 for (i = 0; i < card->num_rtd; i++) {
1063 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1064
1065 if (card->rtd[i].dai_link->ignore_suspend)
1066 continue;
1067
1068 if (driver->playback.stream_name != NULL)
1069 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1070 SND_SOC_DAPM_STREAM_SUSPEND);
1071
1072 if (driver->capture.stream_name != NULL)
1073 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1074 SND_SOC_DAPM_STREAM_SUSPEND);
1075 }
1076
1077 /* suspend all CODECs */
1078 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
1079 /* If there are paths active then the CODEC will be held with
1080 * bias _ON and should not be suspended. */
1081 if (!codec->suspended && codec->driver->suspend) {
1082 switch (codec->dapm.bias_level) {
1083 case SND_SOC_BIAS_STANDBY:
1084 case SND_SOC_BIAS_OFF:
1085 codec->driver->suspend(codec, PMSG_SUSPEND);
1086 codec->suspended = 1;
1087 break;
1088 default:
1089 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1090 break;
1091 }
1092 }
1093 }
1094
1095 for (i = 0; i < card->num_rtd; i++) {
1096 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1097
1098 if (card->rtd[i].dai_link->ignore_suspend)
1099 continue;
1100
1101 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1102 cpu_dai->driver->suspend(cpu_dai);
1103 }
1104
1105 if (card->suspend_post)
1106 card->suspend_post(card);
1107
1108 return 0;
1109 }
1110 EXPORT_SYMBOL_GPL(snd_soc_suspend);
1111
1112 /* deferred resume work, so resume can complete before we finished
1113 * setting our codec back up, which can be very slow on I2C
1114 */
1115 static void soc_resume_deferred(struct work_struct *work)
1116 {
1117 struct snd_soc_card *card =
1118 container_of(work, struct snd_soc_card, deferred_resume_work);
1119 struct snd_soc_codec *codec;
1120 int i;
1121
1122 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1123 * so userspace apps are blocked from touching us
1124 */
1125
1126 dev_dbg(card->dev, "starting resume work\n");
1127
1128 /* Bring us up into D2 so that DAPM starts enabling things */
1129 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
1130
1131 if (card->resume_pre)
1132 card->resume_pre(card);
1133
1134 /* resume AC97 DAIs */
1135 for (i = 0; i < card->num_rtd; i++) {
1136 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1137
1138 if (card->rtd[i].dai_link->ignore_suspend)
1139 continue;
1140
1141 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1142 cpu_dai->driver->resume(cpu_dai);
1143 }
1144
1145 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
1146 /* If the CODEC was idle over suspend then it will have been
1147 * left with bias OFF or STANDBY and suspended so we must now
1148 * resume. Otherwise the suspend was suppressed.
1149 */
1150 if (codec->driver->resume && codec->suspended) {
1151 switch (codec->dapm.bias_level) {
1152 case SND_SOC_BIAS_STANDBY:
1153 case SND_SOC_BIAS_OFF:
1154 codec->driver->resume(codec);
1155 codec->suspended = 0;
1156 break;
1157 default:
1158 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1159 break;
1160 }
1161 }
1162 }
1163
1164 for (i = 0; i < card->num_rtd; i++) {
1165 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1166
1167 if (card->rtd[i].dai_link->ignore_suspend)
1168 continue;
1169
1170 if (driver->playback.stream_name != NULL)
1171 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1172 SND_SOC_DAPM_STREAM_RESUME);
1173
1174 if (driver->capture.stream_name != NULL)
1175 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1176 SND_SOC_DAPM_STREAM_RESUME);
1177 }
1178
1179 /* unmute any active DACs */
1180 for (i = 0; i < card->num_rtd; i++) {
1181 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1182 struct snd_soc_dai_driver *drv = dai->driver;
1183
1184 if (card->rtd[i].dai_link->ignore_suspend)
1185 continue;
1186
1187 if (drv->ops->digital_mute && dai->playback_active)
1188 drv->ops->digital_mute(dai, 0);
1189 }
1190
1191 for (i = 0; i < card->num_rtd; i++) {
1192 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1193 struct snd_soc_platform *platform = card->rtd[i].platform;
1194
1195 if (card->rtd[i].dai_link->ignore_suspend)
1196 continue;
1197
1198 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1199 cpu_dai->driver->resume(cpu_dai);
1200 if (platform->driver->resume && platform->suspended) {
1201 platform->driver->resume(cpu_dai);
1202 platform->suspended = 0;
1203 }
1204 }
1205
1206 if (card->resume_post)
1207 card->resume_post(card);
1208
1209 dev_dbg(card->dev, "resume work completed\n");
1210
1211 /* userspace can access us now we are back as we were before */
1212 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
1213 }
1214
1215 /* powers up audio subsystem after a suspend */
1216 int snd_soc_resume(struct device *dev)
1217 {
1218 struct snd_soc_card *card = dev_get_drvdata(dev);
1219 int i;
1220
1221 /* AC97 devices might have other drivers hanging off them so
1222 * need to resume immediately. Other drivers don't have that
1223 * problem and may take a substantial amount of time to resume
1224 * due to I/O costs and anti-pop so handle them out of line.
1225 */
1226 for (i = 0; i < card->num_rtd; i++) {
1227 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1228 if (cpu_dai->driver->ac97_control) {
1229 dev_dbg(dev, "Resuming AC97 immediately\n");
1230 soc_resume_deferred(&card->deferred_resume_work);
1231 } else {
1232 dev_dbg(dev, "Scheduling resume work\n");
1233 if (!schedule_work(&card->deferred_resume_work))
1234 dev_err(dev, "resume work item may be lost\n");
1235 }
1236 }
1237
1238 return 0;
1239 }
1240 EXPORT_SYMBOL_GPL(snd_soc_resume);
1241 #else
1242 #define snd_soc_suspend NULL
1243 #define snd_soc_resume NULL
1244 #endif
1245
1246 static struct snd_soc_dai_ops null_dai_ops = {
1247 };
1248
1249 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
1250 {
1251 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1252 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1253 struct snd_soc_codec *codec;
1254 struct snd_soc_platform *platform;
1255 struct snd_soc_dai *codec_dai, *cpu_dai;
1256
1257 if (rtd->complete)
1258 return 1;
1259 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
1260
1261 /* do we already have the CPU DAI for this link ? */
1262 if (rtd->cpu_dai) {
1263 goto find_codec;
1264 }
1265 /* no, then find CPU DAI from registered DAIs*/
1266 list_for_each_entry(cpu_dai, &dai_list, list) {
1267 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1268
1269 if (!try_module_get(cpu_dai->dev->driver->owner))
1270 return -ENODEV;
1271
1272 rtd->cpu_dai = cpu_dai;
1273 goto find_codec;
1274 }
1275 }
1276 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1277 dai_link->cpu_dai_name);
1278
1279 find_codec:
1280 /* do we already have the CODEC for this link ? */
1281 if (rtd->codec) {
1282 goto find_platform;
1283 }
1284
1285 /* no, then find CODEC from registered CODECs*/
1286 list_for_each_entry(codec, &codec_list, list) {
1287 if (!strcmp(codec->name, dai_link->codec_name)) {
1288 rtd->codec = codec;
1289
1290 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1291 list_for_each_entry(codec_dai, &dai_list, list) {
1292 if (codec->dev == codec_dai->dev &&
1293 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1294 rtd->codec_dai = codec_dai;
1295 goto find_platform;
1296 }
1297 }
1298 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1299 dai_link->codec_dai_name);
1300
1301 goto find_platform;
1302 }
1303 }
1304 dev_dbg(card->dev, "CODEC %s not registered\n",
1305 dai_link->codec_name);
1306
1307 find_platform:
1308 /* do we already have the CODEC DAI for this link ? */
1309 if (rtd->platform) {
1310 goto out;
1311 }
1312 /* no, then find CPU DAI from registered DAIs*/
1313 list_for_each_entry(platform, &platform_list, list) {
1314 if (!strcmp(platform->name, dai_link->platform_name)) {
1315 rtd->platform = platform;
1316 goto out;
1317 }
1318 }
1319
1320 dev_dbg(card->dev, "platform %s not registered\n",
1321 dai_link->platform_name);
1322 return 0;
1323
1324 out:
1325 /* mark rtd as complete if we found all 4 of our client devices */
1326 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1327 rtd->complete = 1;
1328 card->num_rtd++;
1329 }
1330 return 1;
1331 }
1332
1333 static void soc_remove_codec(struct snd_soc_codec *codec)
1334 {
1335 int err;
1336
1337 if (codec->driver->remove) {
1338 err = codec->driver->remove(codec);
1339 if (err < 0)
1340 dev_err(codec->dev,
1341 "asoc: failed to remove %s: %d\n",
1342 codec->name, err);
1343 }
1344
1345 /* Make sure all DAPM widgets are freed */
1346 snd_soc_dapm_free(&codec->dapm);
1347
1348 soc_cleanup_codec_debugfs(codec);
1349 codec->probed = 0;
1350 list_del(&codec->card_list);
1351 module_put(codec->dev->driver->owner);
1352 }
1353
1354 static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1355 {
1356 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1357 struct snd_soc_codec *codec = rtd->codec;
1358 struct snd_soc_platform *platform = rtd->platform;
1359 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1360 int err;
1361
1362 /* unregister the rtd device */
1363 if (rtd->dev_registered) {
1364 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1365 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1366 device_unregister(&rtd->dev);
1367 rtd->dev_registered = 0;
1368 }
1369
1370 /* remove the CODEC DAI */
1371 if (codec_dai && codec_dai->probed) {
1372 if (codec_dai->driver->remove) {
1373 err = codec_dai->driver->remove(codec_dai);
1374 if (err < 0)
1375 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1376 }
1377 codec_dai->probed = 0;
1378 list_del(&codec_dai->card_list);
1379 }
1380
1381 /* remove the platform */
1382 if (platform && platform->probed) {
1383 if (platform->driver->remove) {
1384 err = platform->driver->remove(platform);
1385 if (err < 0)
1386 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1387 }
1388 platform->probed = 0;
1389 list_del(&platform->card_list);
1390 module_put(platform->dev->driver->owner);
1391 }
1392
1393 /* remove the CODEC */
1394 if (codec && codec->probed)
1395 soc_remove_codec(codec);
1396
1397 /* remove the cpu_dai */
1398 if (cpu_dai && cpu_dai->probed) {
1399 if (cpu_dai->driver->remove) {
1400 err = cpu_dai->driver->remove(cpu_dai);
1401 if (err < 0)
1402 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1403 }
1404 cpu_dai->probed = 0;
1405 list_del(&cpu_dai->card_list);
1406 module_put(cpu_dai->dev->driver->owner);
1407 }
1408 }
1409
1410 static void soc_set_name_prefix(struct snd_soc_card *card,
1411 struct snd_soc_codec *codec)
1412 {
1413 int i;
1414
1415 if (card->codec_conf == NULL)
1416 return;
1417
1418 for (i = 0; i < card->num_configs; i++) {
1419 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1420 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1421 codec->name_prefix = map->name_prefix;
1422 break;
1423 }
1424 }
1425 }
1426
1427 static int soc_probe_codec(struct snd_soc_card *card,
1428 struct snd_soc_codec *codec)
1429 {
1430 int ret = 0;
1431
1432 codec->card = card;
1433 codec->dapm.card = card;
1434 soc_set_name_prefix(card, codec);
1435
1436 if (!try_module_get(codec->dev->driver->owner))
1437 return -ENODEV;
1438
1439 if (codec->driver->probe) {
1440 ret = codec->driver->probe(codec);
1441 if (ret < 0) {
1442 dev_err(codec->dev,
1443 "asoc: failed to probe CODEC %s: %d\n",
1444 codec->name, ret);
1445 goto err_probe;
1446 }
1447 }
1448
1449 soc_init_codec_debugfs(codec);
1450
1451 /* mark codec as probed and add to card codec list */
1452 codec->probed = 1;
1453 list_add(&codec->card_list, &card->codec_dev_list);
1454 list_add(&codec->dapm.list, &card->dapm_list);
1455
1456 return 0;
1457
1458 err_probe:
1459 module_put(codec->dev->driver->owner);
1460
1461 return ret;
1462 }
1463
1464 static void rtd_release(struct device *dev) {}
1465
1466 static int soc_post_component_init(struct snd_soc_card *card,
1467 struct snd_soc_codec *codec,
1468 int num, int dailess)
1469 {
1470 struct snd_soc_dai_link *dai_link = NULL;
1471 struct snd_soc_aux_dev *aux_dev = NULL;
1472 struct snd_soc_pcm_runtime *rtd;
1473 const char *temp, *name;
1474 int ret = 0;
1475
1476 if (!dailess) {
1477 dai_link = &card->dai_link[num];
1478 rtd = &card->rtd[num];
1479 name = dai_link->name;
1480 } else {
1481 aux_dev = &card->aux_dev[num];
1482 rtd = &card->rtd_aux[num];
1483 name = aux_dev->name;
1484 }
1485
1486 /* machine controls, routes and widgets are not prefixed */
1487 temp = codec->name_prefix;
1488 codec->name_prefix = NULL;
1489
1490 /* do machine specific initialization */
1491 if (!dailess && dai_link->init)
1492 ret = dai_link->init(rtd);
1493 else if (dailess && aux_dev->init)
1494 ret = aux_dev->init(&codec->dapm);
1495 if (ret < 0) {
1496 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1497 return ret;
1498 }
1499 codec->name_prefix = temp;
1500
1501 /* Make sure all DAPM widgets are instantiated */
1502 snd_soc_dapm_new_widgets(&codec->dapm);
1503
1504 /* register the rtd device */
1505 rtd->codec = codec;
1506 rtd->card = card;
1507 rtd->dev.parent = card->dev;
1508 rtd->dev.release = rtd_release;
1509 rtd->dev.init_name = name;
1510 ret = device_register(&rtd->dev);
1511 if (ret < 0) {
1512 dev_err(card->dev,
1513 "asoc: failed to register runtime device: %d\n", ret);
1514 return ret;
1515 }
1516 rtd->dev_registered = 1;
1517
1518 /* add DAPM sysfs entries for this codec */
1519 ret = snd_soc_dapm_sys_add(&rtd->dev);
1520 if (ret < 0)
1521 dev_err(codec->dev,
1522 "asoc: failed to add codec dapm sysfs entries: %d\n",
1523 ret);
1524
1525 /* add codec sysfs entries */
1526 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1527 if (ret < 0)
1528 dev_err(codec->dev,
1529 "asoc: failed to add codec sysfs files: %d\n", ret);
1530
1531 return 0;
1532 }
1533
1534 static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1535 {
1536 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1537 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1538 struct snd_soc_codec *codec = rtd->codec;
1539 struct snd_soc_platform *platform = rtd->platform;
1540 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1541 int ret;
1542
1543 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1544
1545 /* config components */
1546 codec_dai->codec = codec;
1547 cpu_dai->platform = platform;
1548 codec_dai->card = card;
1549 cpu_dai->card = card;
1550
1551 /* set default power off timeout */
1552 rtd->pmdown_time = pmdown_time;
1553
1554 /* probe the cpu_dai */
1555 if (!cpu_dai->probed) {
1556 if (cpu_dai->driver->probe) {
1557 ret = cpu_dai->driver->probe(cpu_dai);
1558 if (ret < 0) {
1559 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1560 cpu_dai->name);
1561 return ret;
1562 }
1563 }
1564 cpu_dai->probed = 1;
1565 /* mark cpu_dai as probed and add to card cpu_dai list */
1566 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1567 }
1568
1569 /* probe the CODEC */
1570 if (!codec->probed) {
1571 ret = soc_probe_codec(card, codec);
1572 if (ret < 0)
1573 return ret;
1574 }
1575
1576 /* probe the platform */
1577 if (!platform->probed) {
1578 if (!try_module_get(platform->dev->driver->owner))
1579 return -ENODEV;
1580
1581 if (platform->driver->probe) {
1582 ret = platform->driver->probe(platform);
1583 if (ret < 0) {
1584 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1585 platform->name);
1586 module_put(platform->dev->driver->owner);
1587 return ret;
1588 }
1589 }
1590 /* mark platform as probed and add to card platform list */
1591 platform->probed = 1;
1592 list_add(&platform->card_list, &card->platform_dev_list);
1593 }
1594
1595 /* probe the CODEC DAI */
1596 if (!codec_dai->probed) {
1597 if (codec_dai->driver->probe) {
1598 ret = codec_dai->driver->probe(codec_dai);
1599 if (ret < 0) {
1600 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1601 codec_dai->name);
1602 return ret;
1603 }
1604 }
1605
1606 /* mark cpu_dai as probed and add to card cpu_dai list */
1607 codec_dai->probed = 1;
1608 list_add(&codec_dai->card_list, &card->dai_dev_list);
1609 }
1610
1611 /* DAPM dai link stream work */
1612 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1613
1614 ret = soc_post_component_init(card, codec, num, 0);
1615 if (ret)
1616 return ret;
1617
1618 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1619 if (ret < 0)
1620 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1621
1622 /* create the pcm */
1623 ret = soc_new_pcm(rtd, num);
1624 if (ret < 0) {
1625 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1626 return ret;
1627 }
1628
1629 /* add platform data for AC97 devices */
1630 if (rtd->codec_dai->driver->ac97_control)
1631 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1632
1633 return 0;
1634 }
1635
1636 #ifdef CONFIG_SND_SOC_AC97_BUS
1637 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1638 {
1639 int ret;
1640
1641 /* Only instantiate AC97 if not already done by the adaptor
1642 * for the generic AC97 subsystem.
1643 */
1644 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1645 /*
1646 * It is possible that the AC97 device is already registered to
1647 * the device subsystem. This happens when the device is created
1648 * via snd_ac97_mixer(). Currently only SoC codec that does so
1649 * is the generic AC97 glue but others migh emerge.
1650 *
1651 * In those cases we don't try to register the device again.
1652 */
1653 if (!rtd->codec->ac97_created)
1654 return 0;
1655
1656 ret = soc_ac97_dev_register(rtd->codec);
1657 if (ret < 0) {
1658 printk(KERN_ERR "asoc: AC97 device register failed\n");
1659 return ret;
1660 }
1661
1662 rtd->codec->ac97_registered = 1;
1663 }
1664 return 0;
1665 }
1666
1667 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1668 {
1669 if (codec->ac97_registered) {
1670 soc_ac97_dev_unregister(codec);
1671 codec->ac97_registered = 0;
1672 }
1673 }
1674 #endif
1675
1676 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1677 {
1678 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1679 struct snd_soc_codec *codec;
1680 int ret = -ENODEV;
1681
1682 /* find CODEC from registered CODECs*/
1683 list_for_each_entry(codec, &codec_list, list) {
1684 if (!strcmp(codec->name, aux_dev->codec_name)) {
1685 if (codec->probed) {
1686 dev_err(codec->dev,
1687 "asoc: codec already probed");
1688 ret = -EBUSY;
1689 goto out;
1690 }
1691 goto found;
1692 }
1693 }
1694 /* codec not found */
1695 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1696 goto out;
1697
1698 found:
1699 ret = soc_probe_codec(card, codec);
1700 if (ret < 0)
1701 return ret;
1702
1703 ret = soc_post_component_init(card, codec, num, 1);
1704
1705 out:
1706 return ret;
1707 }
1708
1709 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1710 {
1711 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1712 struct snd_soc_codec *codec = rtd->codec;
1713
1714 /* unregister the rtd device */
1715 if (rtd->dev_registered) {
1716 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1717 device_unregister(&rtd->dev);
1718 rtd->dev_registered = 0;
1719 }
1720
1721 if (codec && codec->probed)
1722 soc_remove_codec(codec);
1723 }
1724
1725 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1726 enum snd_soc_compress_type compress_type)
1727 {
1728 int ret;
1729
1730 if (codec->cache_init)
1731 return 0;
1732
1733 /* override the compress_type if necessary */
1734 if (compress_type && codec->compress_type != compress_type)
1735 codec->compress_type = compress_type;
1736 ret = snd_soc_cache_init(codec);
1737 if (ret < 0) {
1738 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1739 ret);
1740 return ret;
1741 }
1742 codec->cache_init = 1;
1743 return 0;
1744 }
1745
1746 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1747 {
1748 struct snd_soc_codec *codec;
1749 struct snd_soc_codec_conf *codec_conf;
1750 enum snd_soc_compress_type compress_type;
1751 int ret, i;
1752
1753 mutex_lock(&card->mutex);
1754
1755 if (card->instantiated) {
1756 mutex_unlock(&card->mutex);
1757 return;
1758 }
1759
1760 /* bind DAIs */
1761 for (i = 0; i < card->num_links; i++)
1762 soc_bind_dai_link(card, i);
1763
1764 /* bind completed ? */
1765 if (card->num_rtd != card->num_links) {
1766 mutex_unlock(&card->mutex);
1767 return;
1768 }
1769
1770 /* initialize the register cache for each available codec */
1771 list_for_each_entry(codec, &codec_list, list) {
1772 if (codec->cache_init)
1773 continue;
1774 /* by default we don't override the compress_type */
1775 compress_type = 0;
1776 /* check to see if we need to override the compress_type */
1777 for (i = 0; i < card->num_configs; ++i) {
1778 codec_conf = &card->codec_conf[i];
1779 if (!strcmp(codec->name, codec_conf->dev_name)) {
1780 compress_type = codec_conf->compress_type;
1781 if (compress_type && compress_type
1782 != codec->compress_type)
1783 break;
1784 }
1785 }
1786 ret = snd_soc_init_codec_cache(codec, compress_type);
1787 if (ret < 0) {
1788 mutex_unlock(&card->mutex);
1789 return;
1790 }
1791 }
1792
1793 /* card bind complete so register a sound card */
1794 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1795 card->owner, 0, &card->snd_card);
1796 if (ret < 0) {
1797 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1798 card->name);
1799 mutex_unlock(&card->mutex);
1800 return;
1801 }
1802 card->snd_card->dev = card->dev;
1803
1804 #ifdef CONFIG_PM_SLEEP
1805 /* deferred resume work */
1806 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1807 #endif
1808
1809 /* initialise the sound card only once */
1810 if (card->probe) {
1811 ret = card->probe(card);
1812 if (ret < 0)
1813 goto card_probe_error;
1814 }
1815
1816 for (i = 0; i < card->num_links; i++) {
1817 ret = soc_probe_dai_link(card, i);
1818 if (ret < 0) {
1819 pr_err("asoc: failed to instantiate card %s: %d\n",
1820 card->name, ret);
1821 goto probe_dai_err;
1822 }
1823 }
1824
1825 for (i = 0; i < card->num_aux_devs; i++) {
1826 ret = soc_probe_aux_dev(card, i);
1827 if (ret < 0) {
1828 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1829 card->name, ret);
1830 goto probe_aux_dev_err;
1831 }
1832 }
1833
1834 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1835 "%s", card->name);
1836 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1837 "%s", card->name);
1838
1839 ret = snd_card_register(card->snd_card);
1840 if (ret < 0) {
1841 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1842 goto probe_aux_dev_err;
1843 }
1844
1845 #ifdef CONFIG_SND_SOC_AC97_BUS
1846 /* register any AC97 codecs */
1847 for (i = 0; i < card->num_rtd; i++) {
1848 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1849 if (ret < 0) {
1850 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1851 while (--i >= 0)
1852 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1853 goto probe_aux_dev_err;
1854 }
1855 }
1856 #endif
1857
1858 card->instantiated = 1;
1859 mutex_unlock(&card->mutex);
1860 return;
1861
1862 probe_aux_dev_err:
1863 for (i = 0; i < card->num_aux_devs; i++)
1864 soc_remove_aux_dev(card, i);
1865
1866 probe_dai_err:
1867 for (i = 0; i < card->num_links; i++)
1868 soc_remove_dai_link(card, i);
1869
1870 card_probe_error:
1871 if (card->remove)
1872 card->remove(card);
1873
1874 snd_card_free(card->snd_card);
1875
1876 mutex_unlock(&card->mutex);
1877 }
1878
1879 /*
1880 * Attempt to initialise any uninitialised cards. Must be called with
1881 * client_mutex.
1882 */
1883 static void snd_soc_instantiate_cards(void)
1884 {
1885 struct snd_soc_card *card;
1886 list_for_each_entry(card, &card_list, list)
1887 snd_soc_instantiate_card(card);
1888 }
1889
1890 /* probes a new socdev */
1891 static int soc_probe(struct platform_device *pdev)
1892 {
1893 struct snd_soc_card *card = platform_get_drvdata(pdev);
1894 int ret = 0;
1895
1896 /*
1897 * no card, so machine driver should be registering card
1898 * we should not be here in that case so ret error
1899 */
1900 if (!card)
1901 return -EINVAL;
1902
1903 /* Bodge while we unpick instantiation */
1904 card->dev = &pdev->dev;
1905
1906 ret = snd_soc_register_card(card);
1907 if (ret != 0) {
1908 dev_err(&pdev->dev, "Failed to register card\n");
1909 return ret;
1910 }
1911
1912 return 0;
1913 }
1914
1915 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1916 {
1917 int i;
1918
1919 /* make sure any delayed work runs */
1920 for (i = 0; i < card->num_rtd; i++) {
1921 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1922 flush_delayed_work_sync(&rtd->delayed_work);
1923 }
1924
1925 /* remove auxiliary devices */
1926 for (i = 0; i < card->num_aux_devs; i++)
1927 soc_remove_aux_dev(card, i);
1928
1929 /* remove and free each DAI */
1930 for (i = 0; i < card->num_rtd; i++)
1931 soc_remove_dai_link(card, i);
1932
1933 soc_cleanup_card_debugfs(card);
1934
1935 /* remove the card */
1936 if (card->remove)
1937 card->remove(card);
1938
1939 kfree(card->rtd);
1940 snd_card_free(card->snd_card);
1941 return 0;
1942
1943 }
1944
1945 /* removes a socdev */
1946 static int soc_remove(struct platform_device *pdev)
1947 {
1948 struct snd_soc_card *card = platform_get_drvdata(pdev);
1949
1950 snd_soc_unregister_card(card);
1951 return 0;
1952 }
1953
1954 int snd_soc_poweroff(struct device *dev)
1955 {
1956 struct snd_soc_card *card = dev_get_drvdata(dev);
1957 int i;
1958
1959 if (!card->instantiated)
1960 return 0;
1961
1962 /* Flush out pmdown_time work - we actually do want to run it
1963 * now, we're shutting down so no imminent restart. */
1964 for (i = 0; i < card->num_rtd; i++) {
1965 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1966 flush_delayed_work_sync(&rtd->delayed_work);
1967 }
1968
1969 snd_soc_dapm_shutdown(card);
1970
1971 return 0;
1972 }
1973 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1974
1975 const struct dev_pm_ops snd_soc_pm_ops = {
1976 .suspend = snd_soc_suspend,
1977 .resume = snd_soc_resume,
1978 .poweroff = snd_soc_poweroff,
1979 };
1980
1981 /* ASoC platform driver */
1982 static struct platform_driver soc_driver = {
1983 .driver = {
1984 .name = "soc-audio",
1985 .owner = THIS_MODULE,
1986 .pm = &snd_soc_pm_ops,
1987 },
1988 .probe = soc_probe,
1989 .remove = soc_remove,
1990 };
1991
1992 /* create a new pcm */
1993 static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1994 {
1995 struct snd_soc_codec *codec = rtd->codec;
1996 struct snd_soc_platform *platform = rtd->platform;
1997 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1998 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1999 struct snd_pcm *pcm;
2000 char new_name[64];
2001 int ret = 0, playback = 0, capture = 0;
2002
2003 /* check client and interface hw capabilities */
2004 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2005 rtd->dai_link->stream_name, codec_dai->name, num);
2006
2007 if (codec_dai->driver->playback.channels_min)
2008 playback = 1;
2009 if (codec_dai->driver->capture.channels_min)
2010 capture = 1;
2011
2012 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
2013 ret = snd_pcm_new(rtd->card->snd_card, new_name,
2014 num, playback, capture, &pcm);
2015 if (ret < 0) {
2016 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
2017 return ret;
2018 }
2019
2020 rtd->pcm = pcm;
2021 pcm->private_data = rtd;
2022 soc_pcm_ops.mmap = platform->driver->ops->mmap;
2023 soc_pcm_ops.pointer = platform->driver->ops->pointer;
2024 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
2025 soc_pcm_ops.copy = platform->driver->ops->copy;
2026 soc_pcm_ops.silence = platform->driver->ops->silence;
2027 soc_pcm_ops.ack = platform->driver->ops->ack;
2028 soc_pcm_ops.page = platform->driver->ops->page;
2029
2030 if (playback)
2031 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
2032
2033 if (capture)
2034 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
2035
2036 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
2037 if (ret < 0) {
2038 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
2039 return ret;
2040 }
2041
2042 pcm->private_free = platform->driver->pcm_free;
2043 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2044 cpu_dai->name);
2045 return ret;
2046 }
2047
2048 /**
2049 * snd_soc_codec_volatile_register: Report if a register is volatile.
2050 *
2051 * @codec: CODEC to query.
2052 * @reg: Register to query.
2053 *
2054 * Boolean function indiciating if a CODEC register is volatile.
2055 */
2056 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
2057 unsigned int reg)
2058 {
2059 if (codec->volatile_register)
2060 return codec->volatile_register(codec, reg);
2061 else
2062 return 0;
2063 }
2064 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2065
2066 /**
2067 * snd_soc_new_ac97_codec - initailise AC97 device
2068 * @codec: audio codec
2069 * @ops: AC97 bus operations
2070 * @num: AC97 codec number
2071 *
2072 * Initialises AC97 codec resources for use by ad-hoc devices only.
2073 */
2074 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2075 struct snd_ac97_bus_ops *ops, int num)
2076 {
2077 mutex_lock(&codec->mutex);
2078
2079 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2080 if (codec->ac97 == NULL) {
2081 mutex_unlock(&codec->mutex);
2082 return -ENOMEM;
2083 }
2084
2085 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2086 if (codec->ac97->bus == NULL) {
2087 kfree(codec->ac97);
2088 codec->ac97 = NULL;
2089 mutex_unlock(&codec->mutex);
2090 return -ENOMEM;
2091 }
2092
2093 codec->ac97->bus->ops = ops;
2094 codec->ac97->num = num;
2095
2096 /*
2097 * Mark the AC97 device to be created by us. This way we ensure that the
2098 * device will be registered with the device subsystem later on.
2099 */
2100 codec->ac97_created = 1;
2101
2102 mutex_unlock(&codec->mutex);
2103 return 0;
2104 }
2105 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2106
2107 /**
2108 * snd_soc_free_ac97_codec - free AC97 codec device
2109 * @codec: audio codec
2110 *
2111 * Frees AC97 codec device resources.
2112 */
2113 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2114 {
2115 mutex_lock(&codec->mutex);
2116 #ifdef CONFIG_SND_SOC_AC97_BUS
2117 soc_unregister_ac97_dai_link(codec);
2118 #endif
2119 kfree(codec->ac97->bus);
2120 kfree(codec->ac97);
2121 codec->ac97 = NULL;
2122 codec->ac97_created = 0;
2123 mutex_unlock(&codec->mutex);
2124 }
2125 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2126
2127 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2128 {
2129 unsigned int ret;
2130
2131 ret = codec->read(codec, reg);
2132 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2133 trace_snd_soc_reg_read(codec, reg, ret);
2134
2135 return ret;
2136 }
2137 EXPORT_SYMBOL_GPL(snd_soc_read);
2138
2139 unsigned int snd_soc_write(struct snd_soc_codec *codec,
2140 unsigned int reg, unsigned int val)
2141 {
2142 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2143 trace_snd_soc_reg_write(codec, reg, val);
2144 return codec->write(codec, reg, val);
2145 }
2146 EXPORT_SYMBOL_GPL(snd_soc_write);
2147
2148 /**
2149 * snd_soc_update_bits - update codec register bits
2150 * @codec: audio codec
2151 * @reg: codec register
2152 * @mask: register mask
2153 * @value: new value
2154 *
2155 * Writes new register value.
2156 *
2157 * Returns 1 for change, 0 for no change, or negative error code.
2158 */
2159 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
2160 unsigned int mask, unsigned int value)
2161 {
2162 int change;
2163 unsigned int old, new;
2164 int ret;
2165
2166 ret = snd_soc_read(codec, reg);
2167 if (ret < 0)
2168 return ret;
2169
2170 old = ret;
2171 new = (old & ~mask) | value;
2172 change = old != new;
2173 if (change) {
2174 ret = snd_soc_write(codec, reg, new);
2175 if (ret < 0)
2176 return ret;
2177 }
2178
2179 return change;
2180 }
2181 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2182
2183 /**
2184 * snd_soc_update_bits_locked - update codec register bits
2185 * @codec: audio codec
2186 * @reg: codec register
2187 * @mask: register mask
2188 * @value: new value
2189 *
2190 * Writes new register value, and takes the codec mutex.
2191 *
2192 * Returns 1 for change else 0.
2193 */
2194 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2195 unsigned short reg, unsigned int mask,
2196 unsigned int value)
2197 {
2198 int change;
2199
2200 mutex_lock(&codec->mutex);
2201 change = snd_soc_update_bits(codec, reg, mask, value);
2202 mutex_unlock(&codec->mutex);
2203
2204 return change;
2205 }
2206 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
2207
2208 /**
2209 * snd_soc_test_bits - test register for change
2210 * @codec: audio codec
2211 * @reg: codec register
2212 * @mask: register mask
2213 * @value: new value
2214 *
2215 * Tests a register with a new value and checks if the new value is
2216 * different from the old value.
2217 *
2218 * Returns 1 for change else 0.
2219 */
2220 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
2221 unsigned int mask, unsigned int value)
2222 {
2223 int change;
2224 unsigned int old, new;
2225
2226 old = snd_soc_read(codec, reg);
2227 new = (old & ~mask) | value;
2228 change = old != new;
2229
2230 return change;
2231 }
2232 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2233
2234 /**
2235 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2236 * @substream: the pcm substream
2237 * @hw: the hardware parameters
2238 *
2239 * Sets the substream runtime hardware parameters.
2240 */
2241 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2242 const struct snd_pcm_hardware *hw)
2243 {
2244 struct snd_pcm_runtime *runtime = substream->runtime;
2245 runtime->hw.info = hw->info;
2246 runtime->hw.formats = hw->formats;
2247 runtime->hw.period_bytes_min = hw->period_bytes_min;
2248 runtime->hw.period_bytes_max = hw->period_bytes_max;
2249 runtime->hw.periods_min = hw->periods_min;
2250 runtime->hw.periods_max = hw->periods_max;
2251 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2252 runtime->hw.fifo_size = hw->fifo_size;
2253 return 0;
2254 }
2255 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2256
2257 /**
2258 * snd_soc_cnew - create new control
2259 * @_template: control template
2260 * @data: control private data
2261 * @long_name: control long name
2262 *
2263 * Create a new mixer control from a template control.
2264 *
2265 * Returns 0 for success, else error.
2266 */
2267 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2268 void *data, char *long_name)
2269 {
2270 struct snd_kcontrol_new template;
2271
2272 memcpy(&template, _template, sizeof(template));
2273 if (long_name)
2274 template.name = long_name;
2275 template.index = 0;
2276
2277 return snd_ctl_new1(&template, data);
2278 }
2279 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2280
2281 /**
2282 * snd_soc_add_controls - add an array of controls to a codec.
2283 * Convienience function to add a list of controls. Many codecs were
2284 * duplicating this code.
2285 *
2286 * @codec: codec to add controls to
2287 * @controls: array of controls to add
2288 * @num_controls: number of elements in the array
2289 *
2290 * Return 0 for success, else error.
2291 */
2292 int snd_soc_add_controls(struct snd_soc_codec *codec,
2293 const struct snd_kcontrol_new *controls, int num_controls)
2294 {
2295 struct snd_card *card = codec->card->snd_card;
2296 char prefixed_name[44], *name;
2297 int err, i;
2298
2299 for (i = 0; i < num_controls; i++) {
2300 const struct snd_kcontrol_new *control = &controls[i];
2301 if (codec->name_prefix) {
2302 snprintf(prefixed_name, sizeof(prefixed_name), "%s %s",
2303 codec->name_prefix, control->name);
2304 name = prefixed_name;
2305 } else {
2306 name = control->name;
2307 }
2308 err = snd_ctl_add(card, snd_soc_cnew(control, codec, name));
2309 if (err < 0) {
2310 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2311 codec->name, name, err);
2312 return err;
2313 }
2314 }
2315
2316 return 0;
2317 }
2318 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2319
2320 /**
2321 * snd_soc_info_enum_double - enumerated double mixer info callback
2322 * @kcontrol: mixer control
2323 * @uinfo: control element information
2324 *
2325 * Callback to provide information about a double enumerated
2326 * mixer control.
2327 *
2328 * Returns 0 for success.
2329 */
2330 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_info *uinfo)
2332 {
2333 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2334
2335 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2336 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2337 uinfo->value.enumerated.items = e->max;
2338
2339 if (uinfo->value.enumerated.item > e->max - 1)
2340 uinfo->value.enumerated.item = e->max - 1;
2341 strcpy(uinfo->value.enumerated.name,
2342 e->texts[uinfo->value.enumerated.item]);
2343 return 0;
2344 }
2345 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2346
2347 /**
2348 * snd_soc_get_enum_double - enumerated double mixer get callback
2349 * @kcontrol: mixer control
2350 * @ucontrol: control element information
2351 *
2352 * Callback to get the value of a double enumerated mixer.
2353 *
2354 * Returns 0 for success.
2355 */
2356 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_value *ucontrol)
2358 {
2359 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2360 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2361 unsigned int val, bitmask;
2362
2363 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2364 ;
2365 val = snd_soc_read(codec, e->reg);
2366 ucontrol->value.enumerated.item[0]
2367 = (val >> e->shift_l) & (bitmask - 1);
2368 if (e->shift_l != e->shift_r)
2369 ucontrol->value.enumerated.item[1] =
2370 (val >> e->shift_r) & (bitmask - 1);
2371
2372 return 0;
2373 }
2374 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2375
2376 /**
2377 * snd_soc_put_enum_double - enumerated double mixer put callback
2378 * @kcontrol: mixer control
2379 * @ucontrol: control element information
2380 *
2381 * Callback to set the value of a double enumerated mixer.
2382 *
2383 * Returns 0 for success.
2384 */
2385 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_value *ucontrol)
2387 {
2388 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2389 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2390 unsigned int val;
2391 unsigned int mask, bitmask;
2392
2393 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2394 ;
2395 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2396 return -EINVAL;
2397 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2398 mask = (bitmask - 1) << e->shift_l;
2399 if (e->shift_l != e->shift_r) {
2400 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2401 return -EINVAL;
2402 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2403 mask |= (bitmask - 1) << e->shift_r;
2404 }
2405
2406 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2407 }
2408 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2409
2410 /**
2411 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2412 * @kcontrol: mixer control
2413 * @ucontrol: control element information
2414 *
2415 * Callback to get the value of a double semi enumerated mixer.
2416 *
2417 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2418 * used for handling bitfield coded enumeration for example.
2419 *
2420 * Returns 0 for success.
2421 */
2422 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2423 struct snd_ctl_elem_value *ucontrol)
2424 {
2425 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2426 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2427 unsigned int reg_val, val, mux;
2428
2429 reg_val = snd_soc_read(codec, e->reg);
2430 val = (reg_val >> e->shift_l) & e->mask;
2431 for (mux = 0; mux < e->max; mux++) {
2432 if (val == e->values[mux])
2433 break;
2434 }
2435 ucontrol->value.enumerated.item[0] = mux;
2436 if (e->shift_l != e->shift_r) {
2437 val = (reg_val >> e->shift_r) & e->mask;
2438 for (mux = 0; mux < e->max; mux++) {
2439 if (val == e->values[mux])
2440 break;
2441 }
2442 ucontrol->value.enumerated.item[1] = mux;
2443 }
2444
2445 return 0;
2446 }
2447 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2448
2449 /**
2450 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2451 * @kcontrol: mixer control
2452 * @ucontrol: control element information
2453 *
2454 * Callback to set the value of a double semi enumerated mixer.
2455 *
2456 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2457 * used for handling bitfield coded enumeration for example.
2458 *
2459 * Returns 0 for success.
2460 */
2461 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2462 struct snd_ctl_elem_value *ucontrol)
2463 {
2464 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2465 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2466 unsigned int val;
2467 unsigned int mask;
2468
2469 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2470 return -EINVAL;
2471 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2472 mask = e->mask << e->shift_l;
2473 if (e->shift_l != e->shift_r) {
2474 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2475 return -EINVAL;
2476 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2477 mask |= e->mask << e->shift_r;
2478 }
2479
2480 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2481 }
2482 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2483
2484 /**
2485 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2486 * @kcontrol: mixer control
2487 * @uinfo: control element information
2488 *
2489 * Callback to provide information about an external enumerated
2490 * single mixer.
2491 *
2492 * Returns 0 for success.
2493 */
2494 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2495 struct snd_ctl_elem_info *uinfo)
2496 {
2497 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2498
2499 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2500 uinfo->count = 1;
2501 uinfo->value.enumerated.items = e->max;
2502
2503 if (uinfo->value.enumerated.item > e->max - 1)
2504 uinfo->value.enumerated.item = e->max - 1;
2505 strcpy(uinfo->value.enumerated.name,
2506 e->texts[uinfo->value.enumerated.item]);
2507 return 0;
2508 }
2509 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2510
2511 /**
2512 * snd_soc_info_volsw_ext - external single mixer info callback
2513 * @kcontrol: mixer control
2514 * @uinfo: control element information
2515 *
2516 * Callback to provide information about a single external mixer control.
2517 *
2518 * Returns 0 for success.
2519 */
2520 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2521 struct snd_ctl_elem_info *uinfo)
2522 {
2523 int max = kcontrol->private_value;
2524
2525 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2526 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2527 else
2528 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2529
2530 uinfo->count = 1;
2531 uinfo->value.integer.min = 0;
2532 uinfo->value.integer.max = max;
2533 return 0;
2534 }
2535 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2536
2537 /**
2538 * snd_soc_info_volsw - single mixer info callback
2539 * @kcontrol: mixer control
2540 * @uinfo: control element information
2541 *
2542 * Callback to provide information about a single mixer control.
2543 *
2544 * Returns 0 for success.
2545 */
2546 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2547 struct snd_ctl_elem_info *uinfo)
2548 {
2549 struct soc_mixer_control *mc =
2550 (struct soc_mixer_control *)kcontrol->private_value;
2551 int platform_max;
2552 unsigned int shift = mc->shift;
2553 unsigned int rshift = mc->rshift;
2554
2555 if (!mc->platform_max)
2556 mc->platform_max = mc->max;
2557 platform_max = mc->platform_max;
2558
2559 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2560 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2561 else
2562 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2563
2564 uinfo->count = shift == rshift ? 1 : 2;
2565 uinfo->value.integer.min = 0;
2566 uinfo->value.integer.max = platform_max;
2567 return 0;
2568 }
2569 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2570
2571 /**
2572 * snd_soc_get_volsw - single mixer get callback
2573 * @kcontrol: mixer control
2574 * @ucontrol: control element information
2575 *
2576 * Callback to get the value of a single mixer control.
2577 *
2578 * Returns 0 for success.
2579 */
2580 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_value *ucontrol)
2582 {
2583 struct soc_mixer_control *mc =
2584 (struct soc_mixer_control *)kcontrol->private_value;
2585 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2586 unsigned int reg = mc->reg;
2587 unsigned int shift = mc->shift;
2588 unsigned int rshift = mc->rshift;
2589 int max = mc->max;
2590 unsigned int mask = (1 << fls(max)) - 1;
2591 unsigned int invert = mc->invert;
2592
2593 ucontrol->value.integer.value[0] =
2594 (snd_soc_read(codec, reg) >> shift) & mask;
2595 if (shift != rshift)
2596 ucontrol->value.integer.value[1] =
2597 (snd_soc_read(codec, reg) >> rshift) & mask;
2598 if (invert) {
2599 ucontrol->value.integer.value[0] =
2600 max - ucontrol->value.integer.value[0];
2601 if (shift != rshift)
2602 ucontrol->value.integer.value[1] =
2603 max - ucontrol->value.integer.value[1];
2604 }
2605
2606 return 0;
2607 }
2608 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2609
2610 /**
2611 * snd_soc_put_volsw - single mixer put callback
2612 * @kcontrol: mixer control
2613 * @ucontrol: control element information
2614 *
2615 * Callback to set the value of a single mixer control.
2616 *
2617 * Returns 0 for success.
2618 */
2619 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_value *ucontrol)
2621 {
2622 struct soc_mixer_control *mc =
2623 (struct soc_mixer_control *)kcontrol->private_value;
2624 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2625 unsigned int reg = mc->reg;
2626 unsigned int shift = mc->shift;
2627 unsigned int rshift = mc->rshift;
2628 int max = mc->max;
2629 unsigned int mask = (1 << fls(max)) - 1;
2630 unsigned int invert = mc->invert;
2631 unsigned int val, val2, val_mask;
2632
2633 val = (ucontrol->value.integer.value[0] & mask);
2634 if (invert)
2635 val = max - val;
2636 val_mask = mask << shift;
2637 val = val << shift;
2638 if (shift != rshift) {
2639 val2 = (ucontrol->value.integer.value[1] & mask);
2640 if (invert)
2641 val2 = max - val2;
2642 val_mask |= mask << rshift;
2643 val |= val2 << rshift;
2644 }
2645 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2646 }
2647 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2648
2649 /**
2650 * snd_soc_info_volsw_2r - double mixer info callback
2651 * @kcontrol: mixer control
2652 * @uinfo: control element information
2653 *
2654 * Callback to provide information about a double mixer control that
2655 * spans 2 codec registers.
2656 *
2657 * Returns 0 for success.
2658 */
2659 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_info *uinfo)
2661 {
2662 struct soc_mixer_control *mc =
2663 (struct soc_mixer_control *)kcontrol->private_value;
2664 int platform_max;
2665
2666 if (!mc->platform_max)
2667 mc->platform_max = mc->max;
2668 platform_max = mc->platform_max;
2669
2670 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2671 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2672 else
2673 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2674
2675 uinfo->count = 2;
2676 uinfo->value.integer.min = 0;
2677 uinfo->value.integer.max = platform_max;
2678 return 0;
2679 }
2680 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2681
2682 /**
2683 * snd_soc_get_volsw_2r - double mixer get callback
2684 * @kcontrol: mixer control
2685 * @ucontrol: control element information
2686 *
2687 * Callback to get the value of a double mixer control that spans 2 registers.
2688 *
2689 * Returns 0 for success.
2690 */
2691 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
2693 {
2694 struct soc_mixer_control *mc =
2695 (struct soc_mixer_control *)kcontrol->private_value;
2696 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2697 unsigned int reg = mc->reg;
2698 unsigned int reg2 = mc->rreg;
2699 unsigned int shift = mc->shift;
2700 int max = mc->max;
2701 unsigned int mask = (1 << fls(max)) - 1;
2702 unsigned int invert = mc->invert;
2703
2704 ucontrol->value.integer.value[0] =
2705 (snd_soc_read(codec, reg) >> shift) & mask;
2706 ucontrol->value.integer.value[1] =
2707 (snd_soc_read(codec, reg2) >> shift) & mask;
2708 if (invert) {
2709 ucontrol->value.integer.value[0] =
2710 max - ucontrol->value.integer.value[0];
2711 ucontrol->value.integer.value[1] =
2712 max - ucontrol->value.integer.value[1];
2713 }
2714
2715 return 0;
2716 }
2717 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2718
2719 /**
2720 * snd_soc_put_volsw_2r - double mixer set callback
2721 * @kcontrol: mixer control
2722 * @ucontrol: control element information
2723 *
2724 * Callback to set the value of a double mixer control that spans 2 registers.
2725 *
2726 * Returns 0 for success.
2727 */
2728 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_value *ucontrol)
2730 {
2731 struct soc_mixer_control *mc =
2732 (struct soc_mixer_control *)kcontrol->private_value;
2733 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2734 unsigned int reg = mc->reg;
2735 unsigned int reg2 = mc->rreg;
2736 unsigned int shift = mc->shift;
2737 int max = mc->max;
2738 unsigned int mask = (1 << fls(max)) - 1;
2739 unsigned int invert = mc->invert;
2740 int err;
2741 unsigned int val, val2, val_mask;
2742
2743 val_mask = mask << shift;
2744 val = (ucontrol->value.integer.value[0] & mask);
2745 val2 = (ucontrol->value.integer.value[1] & mask);
2746
2747 if (invert) {
2748 val = max - val;
2749 val2 = max - val2;
2750 }
2751
2752 val = val << shift;
2753 val2 = val2 << shift;
2754
2755 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2756 if (err < 0)
2757 return err;
2758
2759 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2760 return err;
2761 }
2762 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2763
2764 /**
2765 * snd_soc_info_volsw_s8 - signed mixer info callback
2766 * @kcontrol: mixer control
2767 * @uinfo: control element information
2768 *
2769 * Callback to provide information about a signed mixer control.
2770 *
2771 * Returns 0 for success.
2772 */
2773 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2774 struct snd_ctl_elem_info *uinfo)
2775 {
2776 struct soc_mixer_control *mc =
2777 (struct soc_mixer_control *)kcontrol->private_value;
2778 int platform_max;
2779 int min = mc->min;
2780
2781 if (!mc->platform_max)
2782 mc->platform_max = mc->max;
2783 platform_max = mc->platform_max;
2784
2785 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2786 uinfo->count = 2;
2787 uinfo->value.integer.min = 0;
2788 uinfo->value.integer.max = platform_max - min;
2789 return 0;
2790 }
2791 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2792
2793 /**
2794 * snd_soc_get_volsw_s8 - signed mixer get callback
2795 * @kcontrol: mixer control
2796 * @ucontrol: control element information
2797 *
2798 * Callback to get the value of a signed mixer control.
2799 *
2800 * Returns 0 for success.
2801 */
2802 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2803 struct snd_ctl_elem_value *ucontrol)
2804 {
2805 struct soc_mixer_control *mc =
2806 (struct soc_mixer_control *)kcontrol->private_value;
2807 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2808 unsigned int reg = mc->reg;
2809 int min = mc->min;
2810 int val = snd_soc_read(codec, reg);
2811
2812 ucontrol->value.integer.value[0] =
2813 ((signed char)(val & 0xff))-min;
2814 ucontrol->value.integer.value[1] =
2815 ((signed char)((val >> 8) & 0xff))-min;
2816 return 0;
2817 }
2818 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2819
2820 /**
2821 * snd_soc_put_volsw_sgn - signed mixer put callback
2822 * @kcontrol: mixer control
2823 * @ucontrol: control element information
2824 *
2825 * Callback to set the value of a signed mixer control.
2826 *
2827 * Returns 0 for success.
2828 */
2829 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_value *ucontrol)
2831 {
2832 struct soc_mixer_control *mc =
2833 (struct soc_mixer_control *)kcontrol->private_value;
2834 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2835 unsigned int reg = mc->reg;
2836 int min = mc->min;
2837 unsigned int val;
2838
2839 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2840 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2841
2842 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2843 }
2844 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2845
2846 /**
2847 * snd_soc_limit_volume - Set new limit to an existing volume control.
2848 *
2849 * @codec: where to look for the control
2850 * @name: Name of the control
2851 * @max: new maximum limit
2852 *
2853 * Return 0 for success, else error.
2854 */
2855 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2856 const char *name, int max)
2857 {
2858 struct snd_card *card = codec->card->snd_card;
2859 struct snd_kcontrol *kctl;
2860 struct soc_mixer_control *mc;
2861 int found = 0;
2862 int ret = -EINVAL;
2863
2864 /* Sanity check for name and max */
2865 if (unlikely(!name || max <= 0))
2866 return -EINVAL;
2867
2868 list_for_each_entry(kctl, &card->controls, list) {
2869 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2870 found = 1;
2871 break;
2872 }
2873 }
2874 if (found) {
2875 mc = (struct soc_mixer_control *)kctl->private_value;
2876 if (max <= mc->max) {
2877 mc->platform_max = max;
2878 ret = 0;
2879 }
2880 }
2881 return ret;
2882 }
2883 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2884
2885 /**
2886 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2887 * mixer info callback
2888 * @kcontrol: mixer control
2889 * @uinfo: control element information
2890 *
2891 * Returns 0 for success.
2892 */
2893 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2894 struct snd_ctl_elem_info *uinfo)
2895 {
2896 struct soc_mixer_control *mc =
2897 (struct soc_mixer_control *)kcontrol->private_value;
2898 int max = mc->max;
2899 int min = mc->min;
2900
2901 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2902 uinfo->count = 2;
2903 uinfo->value.integer.min = 0;
2904 uinfo->value.integer.max = max-min;
2905
2906 return 0;
2907 }
2908 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2909
2910 /**
2911 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2912 * mixer get callback
2913 * @kcontrol: mixer control
2914 * @uinfo: control element information
2915 *
2916 * Returns 0 for success.
2917 */
2918 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_value *ucontrol)
2920 {
2921 struct soc_mixer_control *mc =
2922 (struct soc_mixer_control *)kcontrol->private_value;
2923 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2924 unsigned int mask = (1<<mc->shift)-1;
2925 int min = mc->min;
2926 int val = snd_soc_read(codec, mc->reg) & mask;
2927 int valr = snd_soc_read(codec, mc->rreg) & mask;
2928
2929 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2930 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2931 return 0;
2932 }
2933 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2934
2935 /**
2936 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2937 * mixer put callback
2938 * @kcontrol: mixer control
2939 * @uinfo: control element information
2940 *
2941 * Returns 0 for success.
2942 */
2943 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2944 struct snd_ctl_elem_value *ucontrol)
2945 {
2946 struct soc_mixer_control *mc =
2947 (struct soc_mixer_control *)kcontrol->private_value;
2948 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2949 unsigned int mask = (1<<mc->shift)-1;
2950 int min = mc->min;
2951 int ret;
2952 unsigned int val, valr, oval, ovalr;
2953
2954 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2955 val &= mask;
2956 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2957 valr &= mask;
2958
2959 oval = snd_soc_read(codec, mc->reg) & mask;
2960 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2961
2962 ret = 0;
2963 if (oval != val) {
2964 ret = snd_soc_write(codec, mc->reg, val);
2965 if (ret < 0)
2966 return ret;
2967 }
2968 if (ovalr != valr) {
2969 ret = snd_soc_write(codec, mc->rreg, valr);
2970 if (ret < 0)
2971 return ret;
2972 }
2973
2974 return 0;
2975 }
2976 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2977
2978 /**
2979 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2980 * @dai: DAI
2981 * @clk_id: DAI specific clock ID
2982 * @freq: new clock frequency in Hz
2983 * @dir: new clock direction - input/output.
2984 *
2985 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2986 */
2987 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2988 unsigned int freq, int dir)
2989 {
2990 if (dai->driver && dai->driver->ops->set_sysclk)
2991 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2992 else
2993 return -EINVAL;
2994 }
2995 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2996
2997 /**
2998 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2999 * @dai: DAI
3000 * @div_id: DAI specific clock divider ID
3001 * @div: new clock divisor.
3002 *
3003 * Configures the clock dividers. This is used to derive the best DAI bit and
3004 * frame clocks from the system or master clock. It's best to set the DAI bit
3005 * and frame clocks as low as possible to save system power.
3006 */
3007 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3008 int div_id, int div)
3009 {
3010 if (dai->driver && dai->driver->ops->set_clkdiv)
3011 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3012 else
3013 return -EINVAL;
3014 }
3015 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3016
3017 /**
3018 * snd_soc_dai_set_pll - configure DAI PLL.
3019 * @dai: DAI
3020 * @pll_id: DAI specific PLL ID
3021 * @source: DAI specific source for the PLL
3022 * @freq_in: PLL input clock frequency in Hz
3023 * @freq_out: requested PLL output clock frequency in Hz
3024 *
3025 * Configures and enables PLL to generate output clock based on input clock.
3026 */
3027 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3028 unsigned int freq_in, unsigned int freq_out)
3029 {
3030 if (dai->driver && dai->driver->ops->set_pll)
3031 return dai->driver->ops->set_pll(dai, pll_id, source,
3032 freq_in, freq_out);
3033 else
3034 return -EINVAL;
3035 }
3036 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3037
3038 /**
3039 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3040 * @dai: DAI
3041 * @fmt: SND_SOC_DAIFMT_ format value.
3042 *
3043 * Configures the DAI hardware format and clocking.
3044 */
3045 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3046 {
3047 if (dai->driver && dai->driver->ops->set_fmt)
3048 return dai->driver->ops->set_fmt(dai, fmt);
3049 else
3050 return -EINVAL;
3051 }
3052 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3053
3054 /**
3055 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3056 * @dai: DAI
3057 * @tx_mask: bitmask representing active TX slots.
3058 * @rx_mask: bitmask representing active RX slots.
3059 * @slots: Number of slots in use.
3060 * @slot_width: Width in bits for each slot.
3061 *
3062 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3063 * specific.
3064 */
3065 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3066 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3067 {
3068 if (dai->driver && dai->driver->ops->set_tdm_slot)
3069 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3070 slots, slot_width);
3071 else
3072 return -EINVAL;
3073 }
3074 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3075
3076 /**
3077 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3078 * @dai: DAI
3079 * @tx_num: how many TX channels
3080 * @tx_slot: pointer to an array which imply the TX slot number channel
3081 * 0~num-1 uses
3082 * @rx_num: how many RX channels
3083 * @rx_slot: pointer to an array which imply the RX slot number channel
3084 * 0~num-1 uses
3085 *
3086 * configure the relationship between channel number and TDM slot number.
3087 */
3088 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3089 unsigned int tx_num, unsigned int *tx_slot,
3090 unsigned int rx_num, unsigned int *rx_slot)
3091 {
3092 if (dai->driver && dai->driver->ops->set_channel_map)
3093 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3094 rx_num, rx_slot);
3095 else
3096 return -EINVAL;
3097 }
3098 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3099
3100 /**
3101 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3102 * @dai: DAI
3103 * @tristate: tristate enable
3104 *
3105 * Tristates the DAI so that others can use it.
3106 */
3107 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3108 {
3109 if (dai->driver && dai->driver->ops->set_tristate)
3110 return dai->driver->ops->set_tristate(dai, tristate);
3111 else
3112 return -EINVAL;
3113 }
3114 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3115
3116 /**
3117 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3118 * @dai: DAI
3119 * @mute: mute enable
3120 *
3121 * Mutes the DAI DAC.
3122 */
3123 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3124 {
3125 if (dai->driver && dai->driver->ops->digital_mute)
3126 return dai->driver->ops->digital_mute(dai, mute);
3127 else
3128 return -EINVAL;
3129 }
3130 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3131
3132 /**
3133 * snd_soc_register_card - Register a card with the ASoC core
3134 *
3135 * @card: Card to register
3136 *
3137 */
3138 int snd_soc_register_card(struct snd_soc_card *card)
3139 {
3140 int i;
3141
3142 if (!card->name || !card->dev)
3143 return -EINVAL;
3144
3145 snd_soc_initialize_card_lists(card);
3146
3147 soc_init_card_debugfs(card);
3148
3149 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3150 (card->num_links + card->num_aux_devs),
3151 GFP_KERNEL);
3152 if (card->rtd == NULL)
3153 return -ENOMEM;
3154 card->rtd_aux = &card->rtd[card->num_links];
3155
3156 for (i = 0; i < card->num_links; i++)
3157 card->rtd[i].dai_link = &card->dai_link[i];
3158
3159 INIT_LIST_HEAD(&card->list);
3160 card->instantiated = 0;
3161 mutex_init(&card->mutex);
3162
3163 mutex_lock(&client_mutex);
3164 list_add(&card->list, &card_list);
3165 snd_soc_instantiate_cards();
3166 mutex_unlock(&client_mutex);
3167
3168 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3169
3170 return 0;
3171 }
3172 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3173
3174 /**
3175 * snd_soc_unregister_card - Unregister a card with the ASoC core
3176 *
3177 * @card: Card to unregister
3178 *
3179 */
3180 int snd_soc_unregister_card(struct snd_soc_card *card)
3181 {
3182 if (card->instantiated)
3183 soc_cleanup_card_resources(card);
3184 mutex_lock(&client_mutex);
3185 list_del(&card->list);
3186 mutex_unlock(&client_mutex);
3187 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3188
3189 return 0;
3190 }
3191 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3192
3193 /*
3194 * Simplify DAI link configuration by removing ".-1" from device names
3195 * and sanitizing names.
3196 */
3197 static char *fmt_single_name(struct device *dev, int *id)
3198 {
3199 char *found, name[NAME_SIZE];
3200 int id1, id2;
3201
3202 if (dev_name(dev) == NULL)
3203 return NULL;
3204
3205 strlcpy(name, dev_name(dev), NAME_SIZE);
3206
3207 /* are we a "%s.%d" name (platform and SPI components) */
3208 found = strstr(name, dev->driver->name);
3209 if (found) {
3210 /* get ID */
3211 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3212
3213 /* discard ID from name if ID == -1 */
3214 if (*id == -1)
3215 found[strlen(dev->driver->name)] = '\0';
3216 }
3217
3218 } else {
3219 /* I2C component devices are named "bus-addr" */
3220 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3221 char tmp[NAME_SIZE];
3222
3223 /* create unique ID number from I2C addr and bus */
3224 *id = ((id1 & 0xffff) << 16) + id2;
3225
3226 /* sanitize component name for DAI link creation */
3227 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3228 strlcpy(name, tmp, NAME_SIZE);
3229 } else
3230 *id = 0;
3231 }
3232
3233 return kstrdup(name, GFP_KERNEL);
3234 }
3235
3236 /*
3237 * Simplify DAI link naming for single devices with multiple DAIs by removing
3238 * any ".-1" and using the DAI name (instead of device name).
3239 */
3240 static inline char *fmt_multiple_name(struct device *dev,
3241 struct snd_soc_dai_driver *dai_drv)
3242 {
3243 if (dai_drv->name == NULL) {
3244 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3245 dev_name(dev));
3246 return NULL;
3247 }
3248
3249 return kstrdup(dai_drv->name, GFP_KERNEL);
3250 }
3251
3252 /**
3253 * snd_soc_register_dai - Register a DAI with the ASoC core
3254 *
3255 * @dai: DAI to register
3256 */
3257 int snd_soc_register_dai(struct device *dev,
3258 struct snd_soc_dai_driver *dai_drv)
3259 {
3260 struct snd_soc_dai *dai;
3261
3262 dev_dbg(dev, "dai register %s\n", dev_name(dev));
3263
3264 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3265 if (dai == NULL)
3266 return -ENOMEM;
3267
3268 /* create DAI component name */
3269 dai->name = fmt_single_name(dev, &dai->id);
3270 if (dai->name == NULL) {
3271 kfree(dai);
3272 return -ENOMEM;
3273 }
3274
3275 dai->dev = dev;
3276 dai->driver = dai_drv;
3277 if (!dai->driver->ops)
3278 dai->driver->ops = &null_dai_ops;
3279
3280 mutex_lock(&client_mutex);
3281 list_add(&dai->list, &dai_list);
3282 snd_soc_instantiate_cards();
3283 mutex_unlock(&client_mutex);
3284
3285 pr_debug("Registered DAI '%s'\n", dai->name);
3286
3287 return 0;
3288 }
3289 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3290
3291 /**
3292 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3293 *
3294 * @dai: DAI to unregister
3295 */
3296 void snd_soc_unregister_dai(struct device *dev)
3297 {
3298 struct snd_soc_dai *dai;
3299
3300 list_for_each_entry(dai, &dai_list, list) {
3301 if (dev == dai->dev)
3302 goto found;
3303 }
3304 return;
3305
3306 found:
3307 mutex_lock(&client_mutex);
3308 list_del(&dai->list);
3309 mutex_unlock(&client_mutex);
3310
3311 pr_debug("Unregistered DAI '%s'\n", dai->name);
3312 kfree(dai->name);
3313 kfree(dai);
3314 }
3315 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3316
3317 /**
3318 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3319 *
3320 * @dai: Array of DAIs to register
3321 * @count: Number of DAIs
3322 */
3323 int snd_soc_register_dais(struct device *dev,
3324 struct snd_soc_dai_driver *dai_drv, size_t count)
3325 {
3326 struct snd_soc_dai *dai;
3327 int i, ret = 0;
3328
3329 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3330
3331 for (i = 0; i < count; i++) {
3332
3333 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3334 if (dai == NULL) {
3335 ret = -ENOMEM;
3336 goto err;
3337 }
3338
3339 /* create DAI component name */
3340 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3341 if (dai->name == NULL) {
3342 kfree(dai);
3343 ret = -EINVAL;
3344 goto err;
3345 }
3346
3347 dai->dev = dev;
3348 dai->driver = &dai_drv[i];
3349 if (dai->driver->id)
3350 dai->id = dai->driver->id;
3351 else
3352 dai->id = i;
3353 if (!dai->driver->ops)
3354 dai->driver->ops = &null_dai_ops;
3355
3356 mutex_lock(&client_mutex);
3357 list_add(&dai->list, &dai_list);
3358 mutex_unlock(&client_mutex);
3359
3360 pr_debug("Registered DAI '%s'\n", dai->name);
3361 }
3362
3363 mutex_lock(&client_mutex);
3364 snd_soc_instantiate_cards();
3365 mutex_unlock(&client_mutex);
3366 return 0;
3367
3368 err:
3369 for (i--; i >= 0; i--)
3370 snd_soc_unregister_dai(dev);
3371
3372 return ret;
3373 }
3374 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3375
3376 /**
3377 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3378 *
3379 * @dai: Array of DAIs to unregister
3380 * @count: Number of DAIs
3381 */
3382 void snd_soc_unregister_dais(struct device *dev, size_t count)
3383 {
3384 int i;
3385
3386 for (i = 0; i < count; i++)
3387 snd_soc_unregister_dai(dev);
3388 }
3389 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3390
3391 /**
3392 * snd_soc_register_platform - Register a platform with the ASoC core
3393 *
3394 * @platform: platform to register
3395 */
3396 int snd_soc_register_platform(struct device *dev,
3397 struct snd_soc_platform_driver *platform_drv)
3398 {
3399 struct snd_soc_platform *platform;
3400
3401 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3402
3403 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3404 if (platform == NULL)
3405 return -ENOMEM;
3406
3407 /* create platform component name */
3408 platform->name = fmt_single_name(dev, &platform->id);
3409 if (platform->name == NULL) {
3410 kfree(platform);
3411 return -ENOMEM;
3412 }
3413
3414 platform->dev = dev;
3415 platform->driver = platform_drv;
3416
3417 mutex_lock(&client_mutex);
3418 list_add(&platform->list, &platform_list);
3419 snd_soc_instantiate_cards();
3420 mutex_unlock(&client_mutex);
3421
3422 pr_debug("Registered platform '%s'\n", platform->name);
3423
3424 return 0;
3425 }
3426 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3427
3428 /**
3429 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3430 *
3431 * @platform: platform to unregister
3432 */
3433 void snd_soc_unregister_platform(struct device *dev)
3434 {
3435 struct snd_soc_platform *platform;
3436
3437 list_for_each_entry(platform, &platform_list, list) {
3438 if (dev == platform->dev)
3439 goto found;
3440 }
3441 return;
3442
3443 found:
3444 mutex_lock(&client_mutex);
3445 list_del(&platform->list);
3446 mutex_unlock(&client_mutex);
3447
3448 pr_debug("Unregistered platform '%s'\n", platform->name);
3449 kfree(platform->name);
3450 kfree(platform);
3451 }
3452 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3453
3454 static u64 codec_format_map[] = {
3455 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3456 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3457 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3458 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3459 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3460 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3461 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3462 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3463 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3464 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3465 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3466 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3467 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3468 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3469 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3470 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3471 };
3472
3473 /* Fix up the DAI formats for endianness: codecs don't actually see
3474 * the endianness of the data but we're using the CPU format
3475 * definitions which do need to include endianness so we ensure that
3476 * codec DAIs always have both big and little endian variants set.
3477 */
3478 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3479 {
3480 int i;
3481
3482 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3483 if (stream->formats & codec_format_map[i])
3484 stream->formats |= codec_format_map[i];
3485 }
3486
3487 /**
3488 * snd_soc_register_codec - Register a codec with the ASoC core
3489 *
3490 * @codec: codec to register
3491 */
3492 int snd_soc_register_codec(struct device *dev,
3493 const struct snd_soc_codec_driver *codec_drv,
3494 struct snd_soc_dai_driver *dai_drv,
3495 int num_dai)
3496 {
3497 size_t reg_size;
3498 struct snd_soc_codec *codec;
3499 int ret, i;
3500
3501 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3502
3503 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3504 if (codec == NULL)
3505 return -ENOMEM;
3506
3507 /* create CODEC component name */
3508 codec->name = fmt_single_name(dev, &codec->id);
3509 if (codec->name == NULL) {
3510 kfree(codec);
3511 return -ENOMEM;
3512 }
3513
3514 if (codec_drv->compress_type)
3515 codec->compress_type = codec_drv->compress_type;
3516 else
3517 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3518
3519 codec->write = codec_drv->write;
3520 codec->read = codec_drv->read;
3521 codec->volatile_register = codec_drv->volatile_register;
3522 codec->readable_register = codec_drv->readable_register;
3523 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3524 codec->dapm.dev = dev;
3525 codec->dapm.codec = codec;
3526 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3527 codec->dev = dev;
3528 codec->driver = codec_drv;
3529 codec->num_dai = num_dai;
3530 mutex_init(&codec->mutex);
3531
3532 /* allocate CODEC register cache */
3533 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3534 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3535 codec->reg_size = reg_size;
3536 /* it is necessary to make a copy of the default register cache
3537 * because in the case of using a compression type that requires
3538 * the default register cache to be marked as __devinitconst the
3539 * kernel might have freed the array by the time we initialize
3540 * the cache.
3541 */
3542 if (codec_drv->reg_cache_default) {
3543 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3544 reg_size, GFP_KERNEL);
3545 if (!codec->reg_def_copy) {
3546 ret = -ENOMEM;
3547 goto fail;
3548 }
3549 }
3550 }
3551
3552 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3553 if (!codec->volatile_register)
3554 codec->volatile_register = snd_soc_default_volatile_register;
3555 if (!codec->readable_register)
3556 codec->readable_register = snd_soc_default_readable_register;
3557 }
3558
3559 for (i = 0; i < num_dai; i++) {
3560 fixup_codec_formats(&dai_drv[i].playback);
3561 fixup_codec_formats(&dai_drv[i].capture);
3562 }
3563
3564 /* register any DAIs */
3565 if (num_dai) {
3566 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3567 if (ret < 0)
3568 goto fail;
3569 }
3570
3571 mutex_lock(&client_mutex);
3572 list_add(&codec->list, &codec_list);
3573 snd_soc_instantiate_cards();
3574 mutex_unlock(&client_mutex);
3575
3576 pr_debug("Registered codec '%s'\n", codec->name);
3577 return 0;
3578
3579 fail:
3580 kfree(codec->reg_def_copy);
3581 codec->reg_def_copy = NULL;
3582 kfree(codec->name);
3583 kfree(codec);
3584 return ret;
3585 }
3586 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3587
3588 /**
3589 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3590 *
3591 * @codec: codec to unregister
3592 */
3593 void snd_soc_unregister_codec(struct device *dev)
3594 {
3595 struct snd_soc_codec *codec;
3596 int i;
3597
3598 list_for_each_entry(codec, &codec_list, list) {
3599 if (dev == codec->dev)
3600 goto found;
3601 }
3602 return;
3603
3604 found:
3605 if (codec->num_dai)
3606 for (i = 0; i < codec->num_dai; i++)
3607 snd_soc_unregister_dai(dev);
3608
3609 mutex_lock(&client_mutex);
3610 list_del(&codec->list);
3611 mutex_unlock(&client_mutex);
3612
3613 pr_debug("Unregistered codec '%s'\n", codec->name);
3614
3615 snd_soc_cache_exit(codec);
3616 kfree(codec->reg_def_copy);
3617 kfree(codec->name);
3618 kfree(codec);
3619 }
3620 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3621
3622 static int __init snd_soc_init(void)
3623 {
3624 #ifdef CONFIG_DEBUG_FS
3625 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3626 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3627 printk(KERN_WARNING
3628 "ASoC: Failed to create debugfs directory\n");
3629 snd_soc_debugfs_root = NULL;
3630 }
3631
3632 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3633 &codec_list_fops))
3634 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3635
3636 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3637 &dai_list_fops))
3638 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3639
3640 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3641 &platform_list_fops))
3642 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3643 #endif
3644
3645 return platform_driver_register(&soc_driver);
3646 }
3647 module_init(snd_soc_init);
3648
3649 static void __exit snd_soc_exit(void)
3650 {
3651 #ifdef CONFIG_DEBUG_FS
3652 debugfs_remove_recursive(snd_soc_debugfs_root);
3653 #endif
3654 platform_driver_unregister(&soc_driver);
3655 }
3656 module_exit(snd_soc_exit);
3657
3658 /* Module information */
3659 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3660 MODULE_DESCRIPTION("ALSA SoC Core");
3661 MODULE_LICENSE("GPL");
3662 MODULE_ALIAS("platform:soc-audio");