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