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