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