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