OSDN Git Service

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