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