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