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