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