ASoC: Consolidate CPU and CODEC DAI probe
[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 endpoints too, their state is affected by suspend */
628         dapm_mark_endpoints_dirty(card);
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 endpoints too, their state is affected by suspend */
790         dapm_mark_endpoints_dirty(card);
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_dai(struct snd_soc_dai *dai, int order)
1245 {
1246         int ret;
1247
1248         if (!dai->probed && dai->driver->probe_order == order) {
1249                 if (dai->driver->probe) {
1250                         ret = dai->driver->probe(dai);
1251                         if (ret < 0) {
1252                                 dev_err(dai->dev,
1253                                         "ASoC: failed to probe DAI %s: %d\n",
1254                                         dai->name, ret);
1255                                 return ret;
1256                         }
1257                 }
1258
1259                 dai->probed = 1;
1260         }
1261
1262         return 0;
1263 }
1264
1265 static int soc_link_dai_widgets(struct snd_soc_card *card,
1266                                 struct snd_soc_dai_link *dai_link,
1267                                 struct snd_soc_pcm_runtime *rtd)
1268 {
1269         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1270         struct snd_soc_dai *codec_dai = rtd->codec_dai;
1271         struct snd_soc_dapm_widget *play_w, *capture_w;
1272         int ret;
1273
1274         if (rtd->num_codecs > 1)
1275                 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1276
1277         /* link the DAI widgets */
1278         play_w = codec_dai->playback_widget;
1279         capture_w = cpu_dai->capture_widget;
1280         if (play_w && capture_w) {
1281                 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1282                                            capture_w, play_w);
1283                 if (ret != 0) {
1284                         dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1285                                 play_w->name, capture_w->name, ret);
1286                         return ret;
1287                 }
1288         }
1289
1290         play_w = cpu_dai->playback_widget;
1291         capture_w = codec_dai->capture_widget;
1292         if (play_w && capture_w) {
1293                 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1294                                            capture_w, play_w);
1295                 if (ret != 0) {
1296                         dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1297                                 play_w->name, capture_w->name, ret);
1298                         return ret;
1299                 }
1300         }
1301
1302         return 0;
1303 }
1304
1305 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1306 {
1307         struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1308         struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1309         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1310         int i, ret;
1311
1312         dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1313                         card->name, num, order);
1314
1315         /* set default power off timeout */
1316         rtd->pmdown_time = pmdown_time;
1317
1318         ret = soc_probe_dai(cpu_dai, order);
1319         if (ret)
1320                 return ret;
1321
1322         /* probe the CODEC DAI */
1323         for (i = 0; i < rtd->num_codecs; i++) {
1324                 ret = soc_probe_dai(rtd->codec_dais[i], order);
1325                 if (ret)
1326                         return ret;
1327         }
1328
1329         /* complete DAI probe during last probe */
1330         if (order != SND_SOC_COMP_ORDER_LAST)
1331                 return 0;
1332
1333         /* do machine specific initialization */
1334         if (dai_link->init) {
1335                 ret = dai_link->init(rtd);
1336                 if (ret < 0) {
1337                         dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1338                                 dai_link->name, ret);
1339                         return ret;
1340                 }
1341         }
1342
1343         ret = soc_post_component_init(rtd, dai_link->name);
1344         if (ret)
1345                 return ret;
1346
1347 #ifdef CONFIG_DEBUG_FS
1348         /* add DPCM sysfs entries */
1349         if (dai_link->dynamic) {
1350                 ret = soc_dpcm_debugfs_add(rtd);
1351                 if (ret < 0) {
1352                         dev_err(rtd->dev,
1353                                 "ASoC: failed to add dpcm sysfs entries: %d\n",
1354                                 ret);
1355                         return ret;
1356                 }
1357         }
1358 #endif
1359
1360         ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1361         if (ret < 0)
1362                 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1363                         ret);
1364
1365         if (cpu_dai->driver->compress_dai) {
1366                 /*create compress_device"*/
1367                 ret = soc_new_compress(rtd, num);
1368                 if (ret < 0) {
1369                         dev_err(card->dev, "ASoC: can't create compress %s\n",
1370                                          dai_link->stream_name);
1371                         return ret;
1372                 }
1373         } else {
1374
1375                 if (!dai_link->params) {
1376                         /* create the pcm */
1377                         ret = soc_new_pcm(rtd, num);
1378                         if (ret < 0) {
1379                                 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1380                                        dai_link->stream_name, ret);
1381                                 return ret;
1382                         }
1383                 } else {
1384                         INIT_DELAYED_WORK(&rtd->delayed_work,
1385                                                 codec2codec_close_delayed_work);
1386
1387                         /* link the DAI widgets */
1388                         ret = soc_link_dai_widgets(card, dai_link, rtd);
1389                         if (ret)
1390                                 return ret;
1391                 }
1392         }
1393
1394         /* add platform data for AC97 devices */
1395         for (i = 0; i < rtd->num_codecs; i++) {
1396                 if (rtd->codec_dais[i]->driver->ac97_control)
1397                         snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1398                                                rtd->cpu_dai->ac97_pdata);
1399         }
1400
1401         return 0;
1402 }
1403
1404 #ifdef CONFIG_SND_SOC_AC97_BUS
1405 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1406                                    struct snd_soc_dai *codec_dai)
1407 {
1408         int ret;
1409
1410         /* Only instantiate AC97 if not already done by the adaptor
1411          * for the generic AC97 subsystem.
1412          */
1413         if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
1414                 /*
1415                  * It is possible that the AC97 device is already registered to
1416                  * the device subsystem. This happens when the device is created
1417                  * via snd_ac97_mixer(). Currently only SoC codec that does so
1418                  * is the generic AC97 glue but others migh emerge.
1419                  *
1420                  * In those cases we don't try to register the device again.
1421                  */
1422                 if (!codec->ac97_created)
1423                         return 0;
1424
1425                 ret = soc_ac97_dev_register(codec);
1426                 if (ret < 0) {
1427                         dev_err(codec->dev,
1428                                 "ASoC: AC97 device register failed: %d\n", ret);
1429                         return ret;
1430                 }
1431
1432                 codec->ac97_registered = 1;
1433         }
1434         return 0;
1435 }
1436
1437 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1438 {
1439         if (codec->ac97_registered) {
1440                 soc_ac97_dev_unregister(codec);
1441                 codec->ac97_registered = 0;
1442         }
1443 }
1444
1445 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1446 {
1447         int i, ret;
1448
1449         for (i = 0; i < rtd->num_codecs; i++) {
1450                 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1451
1452                 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1453                 if (ret) {
1454                         while (--i >= 0)
1455                                 soc_unregister_ac97_codec(codec_dai->codec);
1456                         return ret;
1457                 }
1458         }
1459
1460         return 0;
1461 }
1462
1463 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1464 {
1465         int i;
1466
1467         for (i = 0; i < rtd->num_codecs; i++)
1468                 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
1469 }
1470 #endif
1471
1472 static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
1473 {
1474         struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1475         struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1476         const char *name = aux_dev->codec_name;
1477
1478         rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1479         if (!rtd->component) {
1480                 if (aux_dev->codec_of_node)
1481                         name = of_node_full_name(aux_dev->codec_of_node);
1482
1483                 dev_err(card->dev, "ASoC: %s not registered\n", name);
1484                 return -EPROBE_DEFER;
1485         }
1486
1487         /*
1488          * Some places still reference rtd->codec, so we have to keep that
1489          * initialized if the component is a CODEC. Once all those references
1490          * have been removed, this code can be removed as well.
1491          */
1492          rtd->codec = rtd->component->codec;
1493
1494         return 0;
1495 }
1496
1497 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1498 {
1499         struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1500         struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1501         int ret;
1502
1503         ret = soc_probe_component(card, rtd->component);
1504         if (ret < 0)
1505                 return ret;
1506
1507         /* do machine specific initialization */
1508         if (aux_dev->init) {
1509                 ret = aux_dev->init(rtd->component);
1510                 if (ret < 0) {
1511                         dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1512                                 aux_dev->name, ret);
1513                         return ret;
1514                 }
1515         }
1516
1517         return soc_post_component_init(rtd, aux_dev->name);
1518 }
1519
1520 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1521 {
1522         struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1523         struct snd_soc_component *component = rtd->component;
1524
1525         /* unregister the rtd device */
1526         if (rtd->dev_registered) {
1527                 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1528                 device_unregister(rtd->dev);
1529                 rtd->dev_registered = 0;
1530         }
1531
1532         if (component && component->probed)
1533                 soc_remove_component(component);
1534 }
1535
1536 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1537 {
1538         int ret;
1539
1540         if (codec->cache_init)
1541                 return 0;
1542
1543         ret = snd_soc_cache_init(codec);
1544         if (ret < 0) {
1545                 dev_err(codec->dev,
1546                         "ASoC: Failed to set cache compression type: %d\n",
1547                         ret);
1548                 return ret;
1549         }
1550         codec->cache_init = 1;
1551         return 0;
1552 }
1553
1554 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1555 {
1556         struct snd_soc_codec *codec;
1557         struct snd_soc_dai_link *dai_link;
1558         int ret, i, order, dai_fmt;
1559
1560         mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1561
1562         /* bind DAIs */
1563         for (i = 0; i < card->num_links; i++) {
1564                 ret = soc_bind_dai_link(card, i);
1565                 if (ret != 0)
1566                         goto base_error;
1567         }
1568
1569         /* bind aux_devs too */
1570         for (i = 0; i < card->num_aux_devs; i++) {
1571                 ret = soc_bind_aux_dev(card, i);
1572                 if (ret != 0)
1573                         goto base_error;
1574         }
1575
1576         /* initialize the register cache for each available codec */
1577         list_for_each_entry(codec, &codec_list, list) {
1578                 if (codec->cache_init)
1579                         continue;
1580                 ret = snd_soc_init_codec_cache(codec);
1581                 if (ret < 0)
1582                         goto base_error;
1583         }
1584
1585         /* card bind complete so register a sound card */
1586         ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1587                         card->owner, 0, &card->snd_card);
1588         if (ret < 0) {
1589                 dev_err(card->dev,
1590                         "ASoC: can't create sound card for card %s: %d\n",
1591                         card->name, ret);
1592                 goto base_error;
1593         }
1594
1595         card->dapm.bias_level = SND_SOC_BIAS_OFF;
1596         card->dapm.dev = card->dev;
1597         card->dapm.card = card;
1598         list_add(&card->dapm.list, &card->dapm_list);
1599
1600 #ifdef CONFIG_DEBUG_FS
1601         snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1602 #endif
1603
1604 #ifdef CONFIG_PM_SLEEP
1605         /* deferred resume work */
1606         INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1607 #endif
1608
1609         if (card->dapm_widgets)
1610                 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1611                                           card->num_dapm_widgets);
1612
1613         /* initialise the sound card only once */
1614         if (card->probe) {
1615                 ret = card->probe(card);
1616                 if (ret < 0)
1617                         goto card_probe_error;
1618         }
1619
1620         /* probe all components used by DAI links on this card */
1621         for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1622                         order++) {
1623                 for (i = 0; i < card->num_links; i++) {
1624                         ret = soc_probe_link_components(card, i, order);
1625                         if (ret < 0) {
1626                                 dev_err(card->dev,
1627                                         "ASoC: failed to instantiate card %d\n",
1628                                         ret);
1629                                 goto probe_dai_err;
1630                         }
1631                 }
1632         }
1633
1634         /* probe all DAI links on this card */
1635         for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1636                         order++) {
1637                 for (i = 0; i < card->num_links; i++) {
1638                         ret = soc_probe_link_dais(card, i, order);
1639                         if (ret < 0) {
1640                                 dev_err(card->dev,
1641                                         "ASoC: failed to instantiate card %d\n",
1642                                         ret);
1643                                 goto probe_dai_err;
1644                         }
1645                 }
1646         }
1647
1648         for (i = 0; i < card->num_aux_devs; i++) {
1649                 ret = soc_probe_aux_dev(card, i);
1650                 if (ret < 0) {
1651                         dev_err(card->dev,
1652                                 "ASoC: failed to add auxiliary devices %d\n",
1653                                 ret);
1654                         goto probe_aux_dev_err;
1655                 }
1656         }
1657
1658         snd_soc_dapm_link_dai_widgets(card);
1659         snd_soc_dapm_connect_dai_link_widgets(card);
1660
1661         if (card->controls)
1662                 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1663
1664         if (card->dapm_routes)
1665                 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1666                                         card->num_dapm_routes);
1667
1668         for (i = 0; i < card->num_links; i++) {
1669                 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1670                 dai_link = &card->dai_link[i];
1671                 dai_fmt = dai_link->dai_fmt;
1672
1673                 if (dai_fmt) {
1674                         struct snd_soc_dai **codec_dais = rtd->codec_dais;
1675                         int j;
1676
1677                         for (j = 0; j < rtd->num_codecs; j++) {
1678                                 struct snd_soc_dai *codec_dai = codec_dais[j];
1679
1680                                 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1681                                 if (ret != 0 && ret != -ENOTSUPP)
1682                                         dev_warn(codec_dai->dev,
1683                                                  "ASoC: Failed to set DAI format: %d\n",
1684                                                  ret);
1685                         }
1686                 }
1687
1688                 /* If this is a regular CPU link there will be a platform */
1689                 if (dai_fmt &&
1690                     (dai_link->platform_name || dai_link->platform_of_node)) {
1691                         ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1692                                                   dai_fmt);
1693                         if (ret != 0 && ret != -ENOTSUPP)
1694                                 dev_warn(card->rtd[i].cpu_dai->dev,
1695                                          "ASoC: Failed to set DAI format: %d\n",
1696                                          ret);
1697                 } else if (dai_fmt) {
1698                         /* Flip the polarity for the "CPU" end */
1699                         dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1700                         switch (dai_link->dai_fmt &
1701                                 SND_SOC_DAIFMT_MASTER_MASK) {
1702                         case SND_SOC_DAIFMT_CBM_CFM:
1703                                 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1704                                 break;
1705                         case SND_SOC_DAIFMT_CBM_CFS:
1706                                 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1707                                 break;
1708                         case SND_SOC_DAIFMT_CBS_CFM:
1709                                 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1710                                 break;
1711                         case SND_SOC_DAIFMT_CBS_CFS:
1712                                 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1713                                 break;
1714                         }
1715
1716                         ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1717                                                   dai_fmt);
1718                         if (ret != 0 && ret != -ENOTSUPP)
1719                                 dev_warn(card->rtd[i].cpu_dai->dev,
1720                                          "ASoC: Failed to set DAI format: %d\n",
1721                                          ret);
1722                 }
1723         }
1724
1725         snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1726                  "%s", card->name);
1727         snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1728                  "%s", card->long_name ? card->long_name : card->name);
1729         snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1730                  "%s", card->driver_name ? card->driver_name : card->name);
1731         for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1732                 switch (card->snd_card->driver[i]) {
1733                 case '_':
1734                 case '-':
1735                 case '\0':
1736                         break;
1737                 default:
1738                         if (!isalnum(card->snd_card->driver[i]))
1739                                 card->snd_card->driver[i] = '_';
1740                         break;
1741                 }
1742         }
1743
1744         if (card->late_probe) {
1745                 ret = card->late_probe(card);
1746                 if (ret < 0) {
1747                         dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1748                                 card->name, ret);
1749                         goto probe_aux_dev_err;
1750                 }
1751         }
1752
1753         if (card->fully_routed)
1754                 snd_soc_dapm_auto_nc_pins(card);
1755
1756         snd_soc_dapm_new_widgets(card);
1757
1758         ret = snd_card_register(card->snd_card);
1759         if (ret < 0) {
1760                 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1761                                 ret);
1762                 goto probe_aux_dev_err;
1763         }
1764
1765 #ifdef CONFIG_SND_SOC_AC97_BUS
1766         /* register any AC97 codecs */
1767         for (i = 0; i < card->num_rtd; i++) {
1768                 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1769                 if (ret < 0) {
1770                         dev_err(card->dev,
1771                                 "ASoC: failed to register AC97: %d\n", ret);
1772                         while (--i >= 0)
1773                                 soc_unregister_ac97_dai_link(&card->rtd[i]);
1774                         goto probe_aux_dev_err;
1775                 }
1776         }
1777 #endif
1778
1779         card->instantiated = 1;
1780         snd_soc_dapm_sync(&card->dapm);
1781         mutex_unlock(&card->mutex);
1782
1783         return 0;
1784
1785 probe_aux_dev_err:
1786         for (i = 0; i < card->num_aux_devs; i++)
1787                 soc_remove_aux_dev(card, i);
1788
1789 probe_dai_err:
1790         soc_remove_dai_links(card);
1791
1792 card_probe_error:
1793         if (card->remove)
1794                 card->remove(card);
1795
1796         snd_card_free(card->snd_card);
1797
1798 base_error:
1799         mutex_unlock(&card->mutex);
1800
1801         return ret;
1802 }
1803
1804 /* probes a new socdev */
1805 static int soc_probe(struct platform_device *pdev)
1806 {
1807         struct snd_soc_card *card = platform_get_drvdata(pdev);
1808
1809         /*
1810          * no card, so machine driver should be registering card
1811          * we should not be here in that case so ret error
1812          */
1813         if (!card)
1814                 return -EINVAL;
1815
1816         dev_warn(&pdev->dev,
1817                  "ASoC: machine %s should use snd_soc_register_card()\n",
1818                  card->name);
1819
1820         /* Bodge while we unpick instantiation */
1821         card->dev = &pdev->dev;
1822
1823         return snd_soc_register_card(card);
1824 }
1825
1826 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1827 {
1828         int i;
1829
1830         /* make sure any delayed work runs */
1831         for (i = 0; i < card->num_rtd; i++) {
1832                 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1833                 flush_delayed_work(&rtd->delayed_work);
1834         }
1835
1836         /* remove auxiliary devices */
1837         for (i = 0; i < card->num_aux_devs; i++)
1838                 soc_remove_aux_dev(card, i);
1839
1840         /* remove and free each DAI */
1841         soc_remove_dai_links(card);
1842
1843         soc_cleanup_card_debugfs(card);
1844
1845         /* remove the card */
1846         if (card->remove)
1847                 card->remove(card);
1848
1849         snd_soc_dapm_free(&card->dapm);
1850
1851         snd_card_free(card->snd_card);
1852         return 0;
1853
1854 }
1855
1856 /* removes a socdev */
1857 static int soc_remove(struct platform_device *pdev)
1858 {
1859         struct snd_soc_card *card = platform_get_drvdata(pdev);
1860
1861         snd_soc_unregister_card(card);
1862         return 0;
1863 }
1864
1865 int snd_soc_poweroff(struct device *dev)
1866 {
1867         struct snd_soc_card *card = dev_get_drvdata(dev);
1868         int i;
1869
1870         if (!card->instantiated)
1871                 return 0;
1872
1873         /* Flush out pmdown_time work - we actually do want to run it
1874          * now, we're shutting down so no imminent restart. */
1875         for (i = 0; i < card->num_rtd; i++) {
1876                 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1877                 flush_delayed_work(&rtd->delayed_work);
1878         }
1879
1880         snd_soc_dapm_shutdown(card);
1881
1882         /* deactivate pins to sleep state */
1883         for (i = 0; i < card->num_rtd; i++) {
1884                 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1885                 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1886                 int j;
1887
1888                 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1889                 for (j = 0; j < rtd->num_codecs; j++) {
1890                         struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1891                         pinctrl_pm_select_sleep_state(codec_dai->dev);
1892                 }
1893         }
1894
1895         return 0;
1896 }
1897 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1898
1899 const struct dev_pm_ops snd_soc_pm_ops = {
1900         .suspend = snd_soc_suspend,
1901         .resume = snd_soc_resume,
1902         .freeze = snd_soc_suspend,
1903         .thaw = snd_soc_resume,
1904         .poweroff = snd_soc_poweroff,
1905         .restore = snd_soc_resume,
1906 };
1907 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1908
1909 /* ASoC platform driver */
1910 static struct platform_driver soc_driver = {
1911         .driver         = {
1912                 .name           = "soc-audio",
1913                 .owner          = THIS_MODULE,
1914                 .pm             = &snd_soc_pm_ops,
1915         },
1916         .probe          = soc_probe,
1917         .remove         = soc_remove,
1918 };
1919
1920 /**
1921  * snd_soc_new_ac97_codec - initailise AC97 device
1922  * @codec: audio codec
1923  * @ops: AC97 bus operations
1924  * @num: AC97 codec number
1925  *
1926  * Initialises AC97 codec resources for use by ad-hoc devices only.
1927  */
1928 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1929         struct snd_ac97_bus_ops *ops, int num)
1930 {
1931         codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1932         if (codec->ac97 == NULL)
1933                 return -ENOMEM;
1934
1935         codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1936         if (codec->ac97->bus == NULL) {
1937                 kfree(codec->ac97);
1938                 codec->ac97 = NULL;
1939                 return -ENOMEM;
1940         }
1941
1942         codec->ac97->bus->ops = ops;
1943         codec->ac97->num = num;
1944
1945         /*
1946          * Mark the AC97 device to be created by us. This way we ensure that the
1947          * device will be registered with the device subsystem later on.
1948          */
1949         codec->ac97_created = 1;
1950
1951         return 0;
1952 }
1953 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1954
1955 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
1956
1957 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
1958 {
1959         struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
1960
1961         pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
1962
1963         gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
1964
1965         udelay(10);
1966
1967         gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
1968
1969         pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
1970         msleep(2);
1971 }
1972
1973 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
1974 {
1975         struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
1976
1977         pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
1978
1979         gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
1980         gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
1981         gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
1982
1983         udelay(10);
1984
1985         gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
1986
1987         pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
1988         msleep(2);
1989 }
1990
1991 static int snd_soc_ac97_parse_pinctl(struct device *dev,
1992                 struct snd_ac97_reset_cfg *cfg)
1993 {
1994         struct pinctrl *p;
1995         struct pinctrl_state *state;
1996         int gpio;
1997         int ret;
1998
1999         p = devm_pinctrl_get(dev);
2000         if (IS_ERR(p)) {
2001                 dev_err(dev, "Failed to get pinctrl\n");
2002                 return PTR_ERR(p);
2003         }
2004         cfg->pctl = p;
2005
2006         state = pinctrl_lookup_state(p, "ac97-reset");
2007         if (IS_ERR(state)) {
2008                 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2009                 return PTR_ERR(state);
2010         }
2011         cfg->pstate_reset = state;
2012
2013         state = pinctrl_lookup_state(p, "ac97-warm-reset");
2014         if (IS_ERR(state)) {
2015                 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2016                 return PTR_ERR(state);
2017         }
2018         cfg->pstate_warm_reset = state;
2019
2020         state = pinctrl_lookup_state(p, "ac97-running");
2021         if (IS_ERR(state)) {
2022                 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2023                 return PTR_ERR(state);
2024         }
2025         cfg->pstate_run = state;
2026
2027         gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2028         if (gpio < 0) {
2029                 dev_err(dev, "Can't find ac97-sync gpio\n");
2030                 return gpio;
2031         }
2032         ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2033         if (ret) {
2034                 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2035                 return ret;
2036         }
2037         cfg->gpio_sync = gpio;
2038
2039         gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2040         if (gpio < 0) {
2041                 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2042                 return gpio;
2043         }
2044         ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2045         if (ret) {
2046                 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2047                 return ret;
2048         }
2049         cfg->gpio_sdata = gpio;
2050
2051         gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2052         if (gpio < 0) {
2053                 dev_err(dev, "Can't find ac97-reset gpio\n");
2054                 return gpio;
2055         }
2056         ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2057         if (ret) {
2058                 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2059                 return ret;
2060         }
2061         cfg->gpio_reset = gpio;
2062
2063         return 0;
2064 }
2065
2066 struct snd_ac97_bus_ops *soc_ac97_ops;
2067 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2068
2069 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2070 {
2071         if (ops == soc_ac97_ops)
2072                 return 0;
2073
2074         if (soc_ac97_ops && ops)
2075                 return -EBUSY;
2076
2077         soc_ac97_ops = ops;
2078
2079         return 0;
2080 }
2081 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2082
2083 /**
2084  * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2085  *
2086  * This function sets the reset and warm_reset properties of ops and parses
2087  * the device node of pdev to get pinctrl states and gpio numbers to use.
2088  */
2089 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2090                 struct platform_device *pdev)
2091 {
2092         struct device *dev = &pdev->dev;
2093         struct snd_ac97_reset_cfg cfg;
2094         int ret;
2095
2096         ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2097         if (ret)
2098                 return ret;
2099
2100         ret = snd_soc_set_ac97_ops(ops);
2101         if (ret)
2102                 return ret;
2103
2104         ops->warm_reset = snd_soc_ac97_warm_reset;
2105         ops->reset = snd_soc_ac97_reset;
2106
2107         snd_ac97_rst_cfg = cfg;
2108         return 0;
2109 }
2110 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2111
2112 /**
2113  * snd_soc_free_ac97_codec - free AC97 codec device
2114  * @codec: audio codec
2115  *
2116  * Frees AC97 codec device resources.
2117  */
2118 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2119 {
2120 #ifdef CONFIG_SND_SOC_AC97_BUS
2121         soc_unregister_ac97_codec(codec);
2122 #endif
2123         kfree(codec->ac97->bus);
2124         kfree(codec->ac97);
2125         codec->ac97 = NULL;
2126         codec->ac97_created = 0;
2127 }
2128 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2129
2130 /**
2131  * snd_soc_cnew - create new control
2132  * @_template: control template
2133  * @data: control private data
2134  * @long_name: control long name
2135  * @prefix: control name prefix
2136  *
2137  * Create a new mixer control from a template control.
2138  *
2139  * Returns 0 for success, else error.
2140  */
2141 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2142                                   void *data, const char *long_name,
2143                                   const char *prefix)
2144 {
2145         struct snd_kcontrol_new template;
2146         struct snd_kcontrol *kcontrol;
2147         char *name = NULL;
2148
2149         memcpy(&template, _template, sizeof(template));
2150         template.index = 0;
2151
2152         if (!long_name)
2153                 long_name = template.name;
2154
2155         if (prefix) {
2156                 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2157                 if (!name)
2158                         return NULL;
2159
2160                 template.name = name;
2161         } else {
2162                 template.name = long_name;
2163         }
2164
2165         kcontrol = snd_ctl_new1(&template, data);
2166
2167         kfree(name);
2168
2169         return kcontrol;
2170 }
2171 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2172
2173 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2174         const struct snd_kcontrol_new *controls, int num_controls,
2175         const char *prefix, void *data)
2176 {
2177         int err, i;
2178
2179         for (i = 0; i < num_controls; i++) {
2180                 const struct snd_kcontrol_new *control = &controls[i];
2181                 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2182                                                      control->name, prefix));
2183                 if (err < 0) {
2184                         dev_err(dev, "ASoC: Failed to add %s: %d\n",
2185                                 control->name, err);
2186                         return err;
2187                 }
2188         }
2189
2190         return 0;
2191 }
2192
2193 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2194                                                const char *name)
2195 {
2196         struct snd_card *card = soc_card->snd_card;
2197         struct snd_kcontrol *kctl;
2198
2199         if (unlikely(!name))
2200                 return NULL;
2201
2202         list_for_each_entry(kctl, &card->controls, list)
2203                 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2204                         return kctl;
2205         return NULL;
2206 }
2207 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2208
2209 /**
2210  * snd_soc_add_component_controls - Add an array of controls to a component.
2211  *
2212  * @component: Component to add controls to
2213  * @controls: Array of controls to add
2214  * @num_controls: Number of elements in the array
2215  *
2216  * Return: 0 for success, else error.
2217  */
2218 int snd_soc_add_component_controls(struct snd_soc_component *component,
2219         const struct snd_kcontrol_new *controls, unsigned int num_controls)
2220 {
2221         struct snd_card *card = component->card->snd_card;
2222
2223         return snd_soc_add_controls(card, component->dev, controls,
2224                         num_controls, component->name_prefix, component);
2225 }
2226 EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2227
2228 /**
2229  * snd_soc_add_codec_controls - add an array of controls to a codec.
2230  * Convenience function to add a list of controls. Many codecs were
2231  * duplicating this code.
2232  *
2233  * @codec: codec to add controls to
2234  * @controls: array of controls to add
2235  * @num_controls: number of elements in the array
2236  *
2237  * Return 0 for success, else error.
2238  */
2239 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2240         const struct snd_kcontrol_new *controls, unsigned int num_controls)
2241 {
2242         return snd_soc_add_component_controls(&codec->component, controls,
2243                 num_controls);
2244 }
2245 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2246
2247 /**
2248  * snd_soc_add_platform_controls - add an array of controls to a platform.
2249  * Convenience function to add a list of controls.
2250  *
2251  * @platform: platform to add controls to
2252  * @controls: array of controls to add
2253  * @num_controls: number of elements in the array
2254  *
2255  * Return 0 for success, else error.
2256  */
2257 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2258         const struct snd_kcontrol_new *controls, unsigned int num_controls)
2259 {
2260         return snd_soc_add_component_controls(&platform->component, controls,
2261                 num_controls);
2262 }
2263 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2264
2265 /**
2266  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2267  * Convenience function to add a list of controls.
2268  *
2269  * @soc_card: SoC card to add controls to
2270  * @controls: array of controls to add
2271  * @num_controls: number of elements in the array
2272  *
2273  * Return 0 for success, else error.
2274  */
2275 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2276         const struct snd_kcontrol_new *controls, int num_controls)
2277 {
2278         struct snd_card *card = soc_card->snd_card;
2279
2280         return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2281                         NULL, soc_card);
2282 }
2283 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2284
2285 /**
2286  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2287  * Convienience function to add a list of controls.
2288  *
2289  * @dai: DAI to add controls to
2290  * @controls: array of controls to add
2291  * @num_controls: number of elements in the array
2292  *
2293  * Return 0 for success, else error.
2294  */
2295 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2296         const struct snd_kcontrol_new *controls, int num_controls)
2297 {
2298         struct snd_card *card = dai->component->card->snd_card;
2299
2300         return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2301                         NULL, dai);
2302 }
2303 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2304
2305 /**
2306  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2307  * @dai: DAI
2308  * @clk_id: DAI specific clock ID
2309  * @freq: new clock frequency in Hz
2310  * @dir: new clock direction - input/output.
2311  *
2312  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2313  */
2314 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2315         unsigned int freq, int dir)
2316 {
2317         if (dai->driver && dai->driver->ops->set_sysclk)
2318                 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2319         else if (dai->codec && dai->codec->driver->set_sysclk)
2320                 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2321                                                       freq, dir);
2322         else
2323                 return -ENOTSUPP;
2324 }
2325 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2326
2327 /**
2328  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2329  * @codec: CODEC
2330  * @clk_id: DAI specific clock ID
2331  * @source: Source for the clock
2332  * @freq: new clock frequency in Hz
2333  * @dir: new clock direction - input/output.
2334  *
2335  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2336  */
2337 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2338                              int source, unsigned int freq, int dir)
2339 {
2340         if (codec->driver->set_sysclk)
2341                 return codec->driver->set_sysclk(codec, clk_id, source,
2342                                                  freq, dir);
2343         else
2344                 return -ENOTSUPP;
2345 }
2346 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2347
2348 /**
2349  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2350  * @dai: DAI
2351  * @div_id: DAI specific clock divider ID
2352  * @div: new clock divisor.
2353  *
2354  * Configures the clock dividers. This is used to derive the best DAI bit and
2355  * frame clocks from the system or master clock. It's best to set the DAI bit
2356  * and frame clocks as low as possible to save system power.
2357  */
2358 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2359         int div_id, int div)
2360 {
2361         if (dai->driver && dai->driver->ops->set_clkdiv)
2362                 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2363         else
2364                 return -EINVAL;
2365 }
2366 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2367
2368 /**
2369  * snd_soc_dai_set_pll - configure DAI PLL.
2370  * @dai: DAI
2371  * @pll_id: DAI specific PLL ID
2372  * @source: DAI specific source for the PLL
2373  * @freq_in: PLL input clock frequency in Hz
2374  * @freq_out: requested PLL output clock frequency in Hz
2375  *
2376  * Configures and enables PLL to generate output clock based on input clock.
2377  */
2378 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2379         unsigned int freq_in, unsigned int freq_out)
2380 {
2381         if (dai->driver && dai->driver->ops->set_pll)
2382                 return dai->driver->ops->set_pll(dai, pll_id, source,
2383                                          freq_in, freq_out);
2384         else if (dai->codec && dai->codec->driver->set_pll)
2385                 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2386                                                    freq_in, freq_out);
2387         else
2388                 return -EINVAL;
2389 }
2390 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2391
2392 /*
2393  * snd_soc_codec_set_pll - configure codec PLL.
2394  * @codec: CODEC
2395  * @pll_id: DAI specific PLL ID
2396  * @source: DAI specific source for the PLL
2397  * @freq_in: PLL input clock frequency in Hz
2398  * @freq_out: requested PLL output clock frequency in Hz
2399  *
2400  * Configures and enables PLL to generate output clock based on input clock.
2401  */
2402 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2403                           unsigned int freq_in, unsigned int freq_out)
2404 {
2405         if (codec->driver->set_pll)
2406                 return codec->driver->set_pll(codec, pll_id, source,
2407                                               freq_in, freq_out);
2408         else
2409                 return -EINVAL;
2410 }
2411 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2412
2413 /**
2414  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2415  * @dai: DAI
2416  * @ratio Ratio of BCLK to Sample rate.
2417  *
2418  * Configures the DAI for a preset BCLK to sample rate ratio.
2419  */
2420 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2421 {
2422         if (dai->driver && dai->driver->ops->set_bclk_ratio)
2423                 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2424         else
2425                 return -EINVAL;
2426 }
2427 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2428
2429 /**
2430  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2431  * @dai: DAI
2432  * @fmt: SND_SOC_DAIFMT_ format value.
2433  *
2434  * Configures the DAI hardware format and clocking.
2435  */
2436 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2437 {
2438         if (dai->driver == NULL)
2439                 return -EINVAL;
2440         if (dai->driver->ops->set_fmt == NULL)
2441                 return -ENOTSUPP;
2442         return dai->driver->ops->set_fmt(dai, fmt);
2443 }
2444 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2445
2446 /**
2447  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
2448  * @slots: Number of slots in use.
2449  * @tx_mask: bitmask representing active TX slots.
2450  * @rx_mask: bitmask representing active RX slots.
2451  *
2452  * Generates the TDM tx and rx slot default masks for DAI.
2453  */
2454 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
2455                                           unsigned int *tx_mask,
2456                                           unsigned int *rx_mask)
2457 {
2458         if (*tx_mask || *rx_mask)
2459                 return 0;
2460
2461         if (!slots)
2462                 return -EINVAL;
2463
2464         *tx_mask = (1 << slots) - 1;
2465         *rx_mask = (1 << slots) - 1;
2466
2467         return 0;
2468 }
2469
2470 /**
2471  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2472  * @dai: DAI
2473  * @tx_mask: bitmask representing active TX slots.
2474  * @rx_mask: bitmask representing active RX slots.
2475  * @slots: Number of slots in use.
2476  * @slot_width: Width in bits for each slot.
2477  *
2478  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2479  * specific.
2480  */
2481 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2482         unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2483 {
2484         if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2485                 dai->driver->ops->xlate_tdm_slot_mask(slots,
2486                                                 &tx_mask, &rx_mask);
2487         else
2488                 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
2489
2490         dai->tx_mask = tx_mask;
2491         dai->rx_mask = rx_mask;
2492
2493         if (dai->driver && dai->driver->ops->set_tdm_slot)
2494                 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2495                                 slots, slot_width);
2496         else
2497                 return -ENOTSUPP;
2498 }
2499 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2500
2501 /**
2502  * snd_soc_dai_set_channel_map - configure DAI audio channel map
2503  * @dai: DAI
2504  * @tx_num: how many TX channels
2505  * @tx_slot: pointer to an array which imply the TX slot number channel
2506  *           0~num-1 uses
2507  * @rx_num: how many RX channels
2508  * @rx_slot: pointer to an array which imply the RX slot number channel
2509  *           0~num-1 uses
2510  *
2511  * configure the relationship between channel number and TDM slot number.
2512  */
2513 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2514         unsigned int tx_num, unsigned int *tx_slot,
2515         unsigned int rx_num, unsigned int *rx_slot)
2516 {
2517         if (dai->driver && dai->driver->ops->set_channel_map)
2518                 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2519                         rx_num, rx_slot);
2520         else
2521                 return -EINVAL;
2522 }
2523 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2524
2525 /**
2526  * snd_soc_dai_set_tristate - configure DAI system or master clock.
2527  * @dai: DAI
2528  * @tristate: tristate enable
2529  *
2530  * Tristates the DAI so that others can use it.
2531  */
2532 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2533 {
2534         if (dai->driver && dai->driver->ops->set_tristate)
2535                 return dai->driver->ops->set_tristate(dai, tristate);
2536         else
2537                 return -EINVAL;
2538 }
2539 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2540
2541 /**
2542  * snd_soc_dai_digital_mute - configure DAI system or master clock.
2543  * @dai: DAI
2544  * @mute: mute enable
2545  * @direction: stream to mute
2546  *
2547  * Mutes the DAI DAC.
2548  */
2549 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2550                              int direction)
2551 {
2552         if (!dai->driver)
2553                 return -ENOTSUPP;
2554
2555         if (dai->driver->ops->mute_stream)
2556                 return dai->driver->ops->mute_stream(dai, mute, direction);
2557         else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2558                  dai->driver->ops->digital_mute)
2559                 return dai->driver->ops->digital_mute(dai, mute);
2560         else
2561                 return -ENOTSUPP;
2562 }
2563 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2564
2565 static int snd_soc_init_multicodec(struct snd_soc_card *card,
2566                                    struct snd_soc_dai_link *dai_link)
2567 {
2568         /* Legacy codec/codec_dai link is a single entry in multicodec */
2569         if (dai_link->codec_name || dai_link->codec_of_node ||
2570             dai_link->codec_dai_name) {
2571                 dai_link->num_codecs = 1;
2572
2573                 dai_link->codecs = devm_kzalloc(card->dev,
2574                                 sizeof(struct snd_soc_dai_link_component),
2575                                 GFP_KERNEL);
2576                 if (!dai_link->codecs)
2577                         return -ENOMEM;
2578
2579                 dai_link->codecs[0].name = dai_link->codec_name;
2580                 dai_link->codecs[0].of_node = dai_link->codec_of_node;
2581                 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
2582         }
2583
2584         if (!dai_link->codecs) {
2585                 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
2586                 return -EINVAL;
2587         }
2588
2589         return 0;
2590 }
2591
2592 /**
2593  * snd_soc_register_card - Register a card with the ASoC core
2594  *
2595  * @card: Card to register
2596  *
2597  */
2598 int snd_soc_register_card(struct snd_soc_card *card)
2599 {
2600         int i, j, ret;
2601
2602         if (!card->name || !card->dev)
2603                 return -EINVAL;
2604
2605         for (i = 0; i < card->num_links; i++) {
2606                 struct snd_soc_dai_link *link = &card->dai_link[i];
2607
2608                 ret = snd_soc_init_multicodec(card, link);
2609                 if (ret) {
2610                         dev_err(card->dev, "ASoC: failed to init multicodec\n");
2611                         return ret;
2612                 }
2613
2614                 for (j = 0; j < link->num_codecs; j++) {
2615                         /*
2616                          * Codec must be specified by 1 of name or OF node,
2617                          * not both or neither.
2618                          */
2619                         if (!!link->codecs[j].name ==
2620                             !!link->codecs[j].of_node) {
2621                                 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
2622                                         link->name);
2623                                 return -EINVAL;
2624                         }
2625                         /* Codec DAI name must be specified */
2626                         if (!link->codecs[j].dai_name) {
2627                                 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
2628                                         link->name);
2629                                 return -EINVAL;
2630                         }
2631                 }
2632
2633                 /*
2634                  * Platform may be specified by either name or OF node, but
2635                  * can be left unspecified, and a dummy platform will be used.
2636                  */
2637                 if (link->platform_name && link->platform_of_node) {
2638                         dev_err(card->dev,
2639                                 "ASoC: Both platform name/of_node are set for %s\n",
2640                                 link->name);
2641                         return -EINVAL;
2642                 }
2643
2644                 /*
2645                  * CPU device may be specified by either name or OF node, but
2646                  * can be left unspecified, and will be matched based on DAI
2647                  * name alone..
2648                  */
2649                 if (link->cpu_name && link->cpu_of_node) {
2650                         dev_err(card->dev,
2651                                 "ASoC: Neither/both cpu name/of_node are set for %s\n",
2652                                 link->name);
2653                         return -EINVAL;
2654                 }
2655                 /*
2656                  * At least one of CPU DAI name or CPU device name/node must be
2657                  * specified
2658                  */
2659                 if (!link->cpu_dai_name &&
2660                     !(link->cpu_name || link->cpu_of_node)) {
2661                         dev_err(card->dev,
2662                                 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
2663                                 link->name);
2664                         return -EINVAL;
2665                 }
2666         }
2667
2668         dev_set_drvdata(card->dev, card);
2669
2670         snd_soc_initialize_card_lists(card);
2671
2672         soc_init_card_debugfs(card);
2673
2674         card->rtd = devm_kzalloc(card->dev,
2675                                  sizeof(struct snd_soc_pcm_runtime) *
2676                                  (card->num_links + card->num_aux_devs),
2677                                  GFP_KERNEL);
2678         if (card->rtd == NULL)
2679                 return -ENOMEM;
2680         card->num_rtd = 0;
2681         card->rtd_aux = &card->rtd[card->num_links];
2682
2683         for (i = 0; i < card->num_links; i++) {
2684                 card->rtd[i].card = card;
2685                 card->rtd[i].dai_link = &card->dai_link[i];
2686                 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
2687                                         sizeof(struct snd_soc_dai *) *
2688                                         (card->rtd[i].dai_link->num_codecs),
2689                                         GFP_KERNEL);
2690                 if (card->rtd[i].codec_dais == NULL)
2691                         return -ENOMEM;
2692         }
2693
2694         for (i = 0; i < card->num_aux_devs; i++)
2695                 card->rtd_aux[i].card = card;
2696
2697         INIT_LIST_HEAD(&card->dapm_dirty);
2698         card->instantiated = 0;
2699         mutex_init(&card->mutex);
2700         mutex_init(&card->dapm_mutex);
2701
2702         ret = snd_soc_instantiate_card(card);
2703         if (ret != 0)
2704                 soc_cleanup_card_debugfs(card);
2705
2706         /* deactivate pins to sleep state */
2707         for (i = 0; i < card->num_rtd; i++) {
2708                 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2709                 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2710                 int j;
2711
2712                 for (j = 0; j < rtd->num_codecs; j++) {
2713                         struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2714                         if (!codec_dai->active)
2715                                 pinctrl_pm_select_sleep_state(codec_dai->dev);
2716                 }
2717
2718                 if (!cpu_dai->active)
2719                         pinctrl_pm_select_sleep_state(cpu_dai->dev);
2720         }
2721
2722         return ret;
2723 }
2724 EXPORT_SYMBOL_GPL(snd_soc_register_card);
2725
2726 /**
2727  * snd_soc_unregister_card - Unregister a card with the ASoC core
2728  *
2729  * @card: Card to unregister
2730  *
2731  */
2732 int snd_soc_unregister_card(struct snd_soc_card *card)
2733 {
2734         if (card->instantiated) {
2735                 card->instantiated = false;
2736                 snd_soc_dapm_shutdown(card);
2737                 soc_cleanup_card_resources(card);
2738         }
2739         dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2740
2741         return 0;
2742 }
2743 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2744
2745 /*
2746  * Simplify DAI link configuration by removing ".-1" from device names
2747  * and sanitizing names.
2748  */
2749 static char *fmt_single_name(struct device *dev, int *id)
2750 {
2751         char *found, name[NAME_SIZE];
2752         int id1, id2;
2753
2754         if (dev_name(dev) == NULL)
2755                 return NULL;
2756
2757         strlcpy(name, dev_name(dev), NAME_SIZE);
2758
2759         /* are we a "%s.%d" name (platform and SPI components) */
2760         found = strstr(name, dev->driver->name);
2761         if (found) {
2762                 /* get ID */
2763                 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2764
2765                         /* discard ID from name if ID == -1 */
2766                         if (*id == -1)
2767                                 found[strlen(dev->driver->name)] = '\0';
2768                 }
2769
2770         } else {
2771                 /* I2C component devices are named "bus-addr"  */
2772                 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2773                         char tmp[NAME_SIZE];
2774
2775                         /* create unique ID number from I2C addr and bus */
2776                         *id = ((id1 & 0xffff) << 16) + id2;
2777
2778                         /* sanitize component name for DAI link creation */
2779                         snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2780                         strlcpy(name, tmp, NAME_SIZE);
2781                 } else
2782                         *id = 0;
2783         }
2784
2785         return kstrdup(name, GFP_KERNEL);
2786 }
2787
2788 /*
2789  * Simplify DAI link naming for single devices with multiple DAIs by removing
2790  * any ".-1" and using the DAI name (instead of device name).
2791  */
2792 static inline char *fmt_multiple_name(struct device *dev,
2793                 struct snd_soc_dai_driver *dai_drv)
2794 {
2795         if (dai_drv->name == NULL) {
2796                 dev_err(dev,
2797                         "ASoC: error - multiple DAI %s registered with no name\n",
2798                         dev_name(dev));
2799                 return NULL;
2800         }
2801
2802         return kstrdup(dai_drv->name, GFP_KERNEL);
2803 }
2804
2805 /**
2806  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
2807  *
2808  * @component: The component for which the DAIs should be unregistered
2809  */
2810 static void snd_soc_unregister_dais(struct snd_soc_component *component)
2811 {
2812         struct snd_soc_dai *dai, *_dai;
2813
2814         list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
2815                 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2816                         dai->name);
2817                 list_del(&dai->list);
2818                 kfree(dai->name);
2819                 kfree(dai);
2820         }
2821 }
2822
2823 /**
2824  * snd_soc_register_dais - Register a DAI with the ASoC core
2825  *
2826  * @component: The component the DAIs are registered for
2827  * @dai_drv: DAI driver to use for the DAIs
2828  * @count: Number of DAIs
2829  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
2830  *                     parent's name.
2831  */
2832 static int snd_soc_register_dais(struct snd_soc_component *component,
2833         struct snd_soc_dai_driver *dai_drv, size_t count,
2834         bool legacy_dai_naming)
2835 {
2836         struct device *dev = component->dev;
2837         struct snd_soc_dai *dai;
2838         unsigned int i;
2839         int ret;
2840
2841         dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
2842
2843         component->dai_drv = dai_drv;
2844         component->num_dai = count;
2845
2846         for (i = 0; i < count; i++) {
2847
2848                 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2849                 if (dai == NULL) {
2850                         ret = -ENOMEM;
2851                         goto err;
2852                 }
2853
2854                 /*
2855                  * Back in the old days when we still had component-less DAIs,
2856                  * instead of having a static name, component-less DAIs would
2857                  * inherit the name of the parent device so it is possible to
2858                  * register multiple instances of the DAI. We still need to keep
2859                  * the same naming style even though those DAIs are not
2860                  * component-less anymore.
2861                  */
2862                 if (count == 1 && legacy_dai_naming) {
2863                         dai->name = fmt_single_name(dev, &dai->id);
2864                 } else {
2865                         dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2866                         if (dai_drv[i].id)
2867                                 dai->id = dai_drv[i].id;
2868                         else
2869                                 dai->id = i;
2870                 }
2871                 if (dai->name == NULL) {
2872                         kfree(dai);
2873                         ret = -ENOMEM;
2874                         goto err;
2875                 }
2876
2877                 dai->component = component;
2878                 dai->dev = dev;
2879                 dai->driver = &dai_drv[i];
2880                 if (!dai->driver->ops)
2881                         dai->driver->ops = &null_dai_ops;
2882
2883                 list_add(&dai->list, &component->dai_list);
2884
2885                 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2886         }
2887
2888         return 0;
2889
2890 err:
2891         snd_soc_unregister_dais(component);
2892
2893         return ret;
2894 }
2895
2896 static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
2897         enum snd_soc_dapm_type type, int subseq)
2898 {
2899         struct snd_soc_component *component = dapm->component;
2900
2901         component->driver->seq_notifier(component, type, subseq);
2902 }
2903
2904 static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
2905         int event)
2906 {
2907         struct snd_soc_component *component = dapm->component;
2908
2909         return component->driver->stream_event(component, event);
2910 }
2911
2912 static int snd_soc_component_initialize(struct snd_soc_component *component,
2913         const struct snd_soc_component_driver *driver, struct device *dev)
2914 {
2915         struct snd_soc_dapm_context *dapm;
2916
2917         component->name = fmt_single_name(dev, &component->id);
2918         if (!component->name) {
2919                 dev_err(dev, "ASoC: Failed to allocate name\n");
2920                 return -ENOMEM;
2921         }
2922
2923         component->dev = dev;
2924         component->driver = driver;
2925         component->probe = component->driver->probe;
2926         component->remove = component->driver->remove;
2927
2928         if (!component->dapm_ptr)
2929                 component->dapm_ptr = &component->dapm;
2930
2931         dapm = component->dapm_ptr;
2932         dapm->dev = dev;
2933         dapm->component = component;
2934         dapm->bias_level = SND_SOC_BIAS_OFF;
2935         dapm->idle_bias_off = true;
2936         if (driver->seq_notifier)
2937                 dapm->seq_notifier = snd_soc_component_seq_notifier;
2938         if (driver->stream_event)
2939                 dapm->stream_event = snd_soc_component_stream_event;
2940
2941         component->controls = driver->controls;
2942         component->num_controls = driver->num_controls;
2943         component->dapm_widgets = driver->dapm_widgets;
2944         component->num_dapm_widgets = driver->num_dapm_widgets;
2945         component->dapm_routes = driver->dapm_routes;
2946         component->num_dapm_routes = driver->num_dapm_routes;
2947
2948         INIT_LIST_HEAD(&component->dai_list);
2949         mutex_init(&component->io_mutex);
2950
2951         return 0;
2952 }
2953
2954 static void snd_soc_component_init_regmap(struct snd_soc_component *component)
2955 {
2956         if (!component->regmap)
2957                 component->regmap = dev_get_regmap(component->dev, NULL);
2958         if (component->regmap) {
2959                 int val_bytes = regmap_get_val_bytes(component->regmap);
2960                 /* Errors are legitimate for non-integer byte multiples */
2961                 if (val_bytes > 0)
2962                         component->val_bytes = val_bytes;
2963         }
2964 }
2965
2966 static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
2967 {
2968         if (!component->write && !component->read)
2969                 snd_soc_component_init_regmap(component);
2970
2971         list_add(&component->list, &component_list);
2972 }
2973
2974 static void snd_soc_component_add(struct snd_soc_component *component)
2975 {
2976         mutex_lock(&client_mutex);
2977         snd_soc_component_add_unlocked(component);
2978         mutex_unlock(&client_mutex);
2979 }
2980
2981 static void snd_soc_component_cleanup(struct snd_soc_component *component)
2982 {
2983         snd_soc_unregister_dais(component);
2984         kfree(component->name);
2985 }
2986
2987 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
2988 {
2989         list_del(&component->list);
2990 }
2991
2992 static void snd_soc_component_del(struct snd_soc_component *component)
2993 {
2994         mutex_lock(&client_mutex);
2995         snd_soc_component_del_unlocked(component);
2996         mutex_unlock(&client_mutex);
2997 }
2998
2999 int snd_soc_register_component(struct device *dev,
3000                                const struct snd_soc_component_driver *cmpnt_drv,
3001                                struct snd_soc_dai_driver *dai_drv,
3002                                int num_dai)
3003 {
3004         struct snd_soc_component *cmpnt;
3005         int ret;
3006
3007         cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
3008         if (!cmpnt) {
3009                 dev_err(dev, "ASoC: Failed to allocate memory\n");
3010                 return -ENOMEM;
3011         }
3012
3013         ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3014         if (ret)
3015                 goto err_free;
3016
3017         cmpnt->ignore_pmdown_time = true;
3018         cmpnt->registered_as_component = true;
3019
3020         ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3021         if (ret < 0) {
3022                 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3023                 goto err_cleanup;
3024         }
3025
3026         snd_soc_component_add(cmpnt);
3027
3028         return 0;
3029
3030 err_cleanup:
3031         snd_soc_component_cleanup(cmpnt);
3032 err_free:
3033         kfree(cmpnt);
3034         return ret;
3035 }
3036 EXPORT_SYMBOL_GPL(snd_soc_register_component);
3037
3038 /**
3039  * snd_soc_unregister_component - Unregister a component from the ASoC core
3040  *
3041  */
3042 void snd_soc_unregister_component(struct device *dev)
3043 {
3044         struct snd_soc_component *cmpnt;
3045
3046         list_for_each_entry(cmpnt, &component_list, list) {
3047                 if (dev == cmpnt->dev && cmpnt->registered_as_component)
3048                         goto found;
3049         }
3050         return;
3051
3052 found:
3053         snd_soc_component_del(cmpnt);
3054         snd_soc_component_cleanup(cmpnt);
3055         kfree(cmpnt);
3056 }
3057 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3058
3059 static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
3060 {
3061         struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3062
3063         return platform->driver->probe(platform);
3064 }
3065
3066 static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
3067 {
3068         struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3069
3070         platform->driver->remove(platform);
3071 }
3072
3073 /**
3074  * snd_soc_add_platform - Add a platform to the ASoC core
3075  * @dev: The parent device for the platform
3076  * @platform: The platform to add
3077  * @platform_driver: The driver for the platform
3078  */
3079 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
3080                 const struct snd_soc_platform_driver *platform_drv)
3081 {
3082         int ret;
3083
3084         ret = snd_soc_component_initialize(&platform->component,
3085                         &platform_drv->component_driver, dev);
3086         if (ret)
3087                 return ret;
3088
3089         platform->dev = dev;
3090         platform->driver = platform_drv;
3091
3092         if (platform_drv->probe)
3093                 platform->component.probe = snd_soc_platform_drv_probe;
3094         if (platform_drv->remove)
3095                 platform->component.remove = snd_soc_platform_drv_remove;
3096
3097 #ifdef CONFIG_DEBUG_FS
3098         platform->component.debugfs_prefix = "platform";
3099 #endif
3100
3101         mutex_lock(&client_mutex);
3102         snd_soc_component_add_unlocked(&platform->component);
3103         list_add(&platform->list, &platform_list);
3104         mutex_unlock(&client_mutex);
3105
3106         dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3107                 platform->component.name);
3108
3109         return 0;
3110 }
3111 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3112
3113 /**
3114  * snd_soc_register_platform - Register a platform with the ASoC core
3115  *
3116  * @platform: platform to register
3117  */
3118 int snd_soc_register_platform(struct device *dev,
3119                 const struct snd_soc_platform_driver *platform_drv)
3120 {
3121         struct snd_soc_platform *platform;
3122         int ret;
3123
3124         dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3125
3126         platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3127         if (platform == NULL)
3128                 return -ENOMEM;
3129
3130         ret = snd_soc_add_platform(dev, platform, platform_drv);
3131         if (ret)
3132                 kfree(platform);
3133
3134         return ret;
3135 }
3136 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3137
3138 /**
3139  * snd_soc_remove_platform - Remove a platform from the ASoC core
3140  * @platform: the platform to remove
3141  */
3142 void snd_soc_remove_platform(struct snd_soc_platform *platform)
3143 {
3144
3145         mutex_lock(&client_mutex);
3146         list_del(&platform->list);
3147         snd_soc_component_del_unlocked(&platform->component);
3148         mutex_unlock(&client_mutex);
3149
3150         dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3151                 platform->component.name);
3152
3153         snd_soc_component_cleanup(&platform->component);
3154 }
3155 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3156
3157 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3158 {
3159         struct snd_soc_platform *platform;
3160
3161         list_for_each_entry(platform, &platform_list, list) {
3162                 if (dev == platform->dev)
3163                         return platform;
3164         }
3165
3166         return NULL;
3167 }
3168 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3169
3170 /**
3171  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3172  *
3173  * @platform: platform to unregister
3174  */
3175 void snd_soc_unregister_platform(struct device *dev)
3176 {
3177         struct snd_soc_platform *platform;
3178
3179         platform = snd_soc_lookup_platform(dev);
3180         if (!platform)
3181                 return;
3182
3183         snd_soc_remove_platform(platform);
3184         kfree(platform);
3185 }
3186 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3187
3188 static u64 codec_format_map[] = {
3189         SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3190         SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3191         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3192         SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3193         SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3194         SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3195         SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3196         SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3197         SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3198         SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3199         SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3200         SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3201         SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3202         SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3203         SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3204         | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3205 };
3206
3207 /* Fix up the DAI formats for endianness: codecs don't actually see
3208  * the endianness of the data but we're using the CPU format
3209  * definitions which do need to include endianness so we ensure that
3210  * codec DAIs always have both big and little endian variants set.
3211  */
3212 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3213 {
3214         int i;
3215
3216         for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3217                 if (stream->formats & codec_format_map[i])
3218                         stream->formats |= codec_format_map[i];
3219 }
3220
3221 static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3222 {
3223         struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3224
3225         return codec->driver->probe(codec);
3226 }
3227
3228 static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3229 {
3230         struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3231
3232         codec->driver->remove(codec);
3233 }
3234
3235 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3236         unsigned int reg, unsigned int val)
3237 {
3238         struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3239
3240         return codec->driver->write(codec, reg, val);
3241 }
3242
3243 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3244         unsigned int reg, unsigned int *val)
3245 {
3246         struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3247
3248         *val = codec->driver->read(codec, reg);
3249
3250         return 0;
3251 }
3252
3253 static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3254         enum snd_soc_bias_level level)
3255 {
3256         struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3257
3258         return codec->driver->set_bias_level(codec, level);
3259 }
3260
3261 /**
3262  * snd_soc_register_codec - Register a codec with the ASoC core
3263  *
3264  * @codec: codec to register
3265  */
3266 int snd_soc_register_codec(struct device *dev,
3267                            const struct snd_soc_codec_driver *codec_drv,
3268                            struct snd_soc_dai_driver *dai_drv,
3269                            int num_dai)
3270 {
3271         struct snd_soc_codec *codec;
3272         struct snd_soc_dai *dai;
3273         int ret, i;
3274
3275         dev_dbg(dev, "codec register %s\n", dev_name(dev));
3276
3277         codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3278         if (codec == NULL)
3279                 return -ENOMEM;
3280
3281         codec->component.dapm_ptr = &codec->dapm;
3282         codec->component.codec = codec;
3283
3284         ret = snd_soc_component_initialize(&codec->component,
3285                         &codec_drv->component_driver, dev);
3286         if (ret)
3287                 goto err_free;
3288
3289         if (codec_drv->controls) {
3290                 codec->component.controls = codec_drv->controls;
3291                 codec->component.num_controls = codec_drv->num_controls;
3292         }
3293         if (codec_drv->dapm_widgets) {
3294                 codec->component.dapm_widgets = codec_drv->dapm_widgets;
3295                 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
3296         }
3297         if (codec_drv->dapm_routes) {
3298                 codec->component.dapm_routes = codec_drv->dapm_routes;
3299                 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
3300         }
3301
3302         if (codec_drv->probe)
3303                 codec->component.probe = snd_soc_codec_drv_probe;
3304         if (codec_drv->remove)
3305                 codec->component.remove = snd_soc_codec_drv_remove;
3306         if (codec_drv->write)
3307                 codec->component.write = snd_soc_codec_drv_write;
3308         if (codec_drv->read)
3309                 codec->component.read = snd_soc_codec_drv_read;
3310         codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3311         codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
3312         codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
3313         if (codec_drv->seq_notifier)
3314                 codec->dapm.seq_notifier = codec_drv->seq_notifier;
3315         if (codec_drv->set_bias_level)
3316                 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
3317         codec->dev = dev;
3318         codec->driver = codec_drv;
3319         codec->component.val_bytes = codec_drv->reg_word_size;
3320         mutex_init(&codec->mutex);
3321
3322 #ifdef CONFIG_DEBUG_FS
3323         codec->component.init_debugfs = soc_init_codec_debugfs;
3324         codec->component.debugfs_prefix = "codec";
3325 #endif
3326
3327         if (codec_drv->get_regmap)
3328                 codec->component.regmap = codec_drv->get_regmap(dev);
3329
3330         for (i = 0; i < num_dai; i++) {
3331                 fixup_codec_formats(&dai_drv[i].playback);
3332                 fixup_codec_formats(&dai_drv[i].capture);
3333         }
3334
3335         ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
3336         if (ret < 0) {
3337                 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3338                 goto err_cleanup;
3339         }
3340
3341         list_for_each_entry(dai, &codec->component.dai_list, list)
3342                 dai->codec = codec;
3343
3344         mutex_lock(&client_mutex);
3345         snd_soc_component_add_unlocked(&codec->component);
3346         list_add(&codec->list, &codec_list);
3347         mutex_unlock(&client_mutex);
3348
3349         dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3350                 codec->component.name);
3351         return 0;
3352
3353 err_cleanup:
3354         snd_soc_component_cleanup(&codec->component);
3355 err_free:
3356         kfree(codec);
3357         return ret;
3358 }
3359 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3360
3361 /**
3362  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3363  *
3364  * @codec: codec to unregister
3365  */
3366 void snd_soc_unregister_codec(struct device *dev)
3367 {
3368         struct snd_soc_codec *codec;
3369
3370         list_for_each_entry(codec, &codec_list, list) {
3371                 if (dev == codec->dev)
3372                         goto found;
3373         }
3374         return;
3375
3376 found:
3377
3378         mutex_lock(&client_mutex);
3379         list_del(&codec->list);
3380         snd_soc_component_del_unlocked(&codec->component);
3381         mutex_unlock(&client_mutex);
3382
3383         dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3384                         codec->component.name);
3385
3386         snd_soc_component_cleanup(&codec->component);
3387         snd_soc_cache_exit(codec);
3388         kfree(codec);
3389 }
3390 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3391
3392 /* Retrieve a card's name from device tree */
3393 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3394                                const char *propname)
3395 {
3396         struct device_node *np;
3397         int ret;
3398
3399         if (!card->dev) {
3400                 pr_err("card->dev is not set before calling %s\n", __func__);
3401                 return -EINVAL;
3402         }
3403
3404         np = card->dev->of_node;
3405
3406         ret = of_property_read_string_index(np, propname, 0, &card->name);
3407         /*
3408          * EINVAL means the property does not exist. This is fine providing
3409          * card->name was previously set, which is checked later in
3410          * snd_soc_register_card.
3411          */
3412         if (ret < 0 && ret != -EINVAL) {
3413                 dev_err(card->dev,
3414                         "ASoC: Property '%s' could not be read: %d\n",
3415                         propname, ret);
3416                 return ret;
3417         }
3418
3419         return 0;
3420 }
3421 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3422
3423 static const struct snd_soc_dapm_widget simple_widgets[] = {
3424         SND_SOC_DAPM_MIC("Microphone", NULL),
3425         SND_SOC_DAPM_LINE("Line", NULL),
3426         SND_SOC_DAPM_HP("Headphone", NULL),
3427         SND_SOC_DAPM_SPK("Speaker", NULL),
3428 };
3429
3430 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
3431                                           const char *propname)
3432 {
3433         struct device_node *np = card->dev->of_node;
3434         struct snd_soc_dapm_widget *widgets;
3435         const char *template, *wname;
3436         int i, j, num_widgets, ret;
3437
3438         num_widgets = of_property_count_strings(np, propname);
3439         if (num_widgets < 0) {
3440                 dev_err(card->dev,
3441                         "ASoC: Property '%s' does not exist\n", propname);
3442                 return -EINVAL;
3443         }
3444         if (num_widgets & 1) {
3445                 dev_err(card->dev,
3446                         "ASoC: Property '%s' length is not even\n", propname);
3447                 return -EINVAL;
3448         }
3449
3450         num_widgets /= 2;
3451         if (!num_widgets) {
3452                 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3453                         propname);
3454                 return -EINVAL;
3455         }
3456
3457         widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3458                                GFP_KERNEL);
3459         if (!widgets) {
3460                 dev_err(card->dev,
3461                         "ASoC: Could not allocate memory for widgets\n");
3462                 return -ENOMEM;
3463         }
3464
3465         for (i = 0; i < num_widgets; i++) {
3466                 ret = of_property_read_string_index(np, propname,
3467                         2 * i, &template);
3468                 if (ret) {
3469                         dev_err(card->dev,
3470                                 "ASoC: Property '%s' index %d read error:%d\n",
3471                                 propname, 2 * i, ret);
3472                         return -EINVAL;
3473                 }
3474
3475                 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3476                         if (!strncmp(template, simple_widgets[j].name,
3477                                      strlen(simple_widgets[j].name))) {
3478                                 widgets[i] = simple_widgets[j];
3479                                 break;
3480                         }
3481                 }
3482
3483                 if (j >= ARRAY_SIZE(simple_widgets)) {
3484                         dev_err(card->dev,
3485                                 "ASoC: DAPM widget '%s' is not supported\n",
3486                                 template);
3487                         return -EINVAL;
3488                 }
3489
3490                 ret = of_property_read_string_index(np, propname,
3491                                                     (2 * i) + 1,
3492                                                     &wname);
3493                 if (ret) {
3494                         dev_err(card->dev,
3495                                 "ASoC: Property '%s' index %d read error:%d\n",
3496                                 propname, (2 * i) + 1, ret);
3497                         return -EINVAL;
3498                 }
3499
3500                 widgets[i].name = wname;
3501         }
3502
3503         card->dapm_widgets = widgets;
3504         card->num_dapm_widgets = num_widgets;
3505
3506         return 0;
3507 }
3508 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3509
3510 int snd_soc_of_parse_tdm_slot(struct device_node *np,
3511                               unsigned int *slots,
3512                               unsigned int *slot_width)
3513 {
3514         u32 val;
3515         int ret;
3516
3517         if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3518                 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3519                 if (ret)
3520                         return ret;
3521
3522                 if (slots)
3523                         *slots = val;
3524         }
3525
3526         if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3527                 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3528                 if (ret)
3529                         return ret;
3530
3531                 if (slot_width)
3532                         *slot_width = val;
3533         }
3534
3535         return 0;
3536 }
3537 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3538
3539 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3540                                    const char *propname)
3541 {
3542         struct device_node *np = card->dev->of_node;
3543         int num_routes;
3544         struct snd_soc_dapm_route *routes;
3545         int i, ret;
3546
3547         num_routes = of_property_count_strings(np, propname);
3548         if (num_routes < 0 || num_routes & 1) {
3549                 dev_err(card->dev,
3550                         "ASoC: Property '%s' does not exist or its length is not even\n",
3551                         propname);
3552                 return -EINVAL;
3553         }
3554         num_routes /= 2;
3555         if (!num_routes) {
3556                 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3557                         propname);
3558                 return -EINVAL;
3559         }
3560
3561         routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3562                               GFP_KERNEL);
3563         if (!routes) {
3564                 dev_err(card->dev,
3565                         "ASoC: Could not allocate DAPM route table\n");
3566                 return -EINVAL;
3567         }
3568
3569         for (i = 0; i < num_routes; i++) {
3570                 ret = of_property_read_string_index(np, propname,
3571                         2 * i, &routes[i].sink);
3572                 if (ret) {
3573                         dev_err(card->dev,
3574                                 "ASoC: Property '%s' index %d could not be read: %d\n",
3575                                 propname, 2 * i, ret);
3576                         return -EINVAL;
3577                 }
3578                 ret = of_property_read_string_index(np, propname,
3579                         (2 * i) + 1, &routes[i].source);
3580                 if (ret) {
3581                         dev_err(card->dev,
3582                                 "ASoC: Property '%s' index %d could not be read: %d\n",
3583                                 propname, (2 * i) + 1, ret);
3584                         return -EINVAL;
3585                 }
3586         }
3587
3588         card->num_dapm_routes = num_routes;
3589         card->dapm_routes = routes;
3590
3591         return 0;
3592 }
3593 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3594
3595 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
3596                                      const char *prefix,
3597                                      struct device_node **bitclkmaster,
3598                                      struct device_node **framemaster)
3599 {
3600         int ret, i;
3601         char prop[128];
3602         unsigned int format = 0;
3603         int bit, frame;
3604         const char *str;
3605         struct {
3606                 char *name;
3607                 unsigned int val;
3608         } of_fmt_table[] = {
3609                 { "i2s",        SND_SOC_DAIFMT_I2S },
3610                 { "right_j",    SND_SOC_DAIFMT_RIGHT_J },
3611                 { "left_j",     SND_SOC_DAIFMT_LEFT_J },
3612                 { "dsp_a",      SND_SOC_DAIFMT_DSP_A },
3613                 { "dsp_b",      SND_SOC_DAIFMT_DSP_B },
3614                 { "ac97",       SND_SOC_DAIFMT_AC97 },
3615                 { "pdm",        SND_SOC_DAIFMT_PDM},
3616                 { "msb",        SND_SOC_DAIFMT_MSB },
3617                 { "lsb",        SND_SOC_DAIFMT_LSB },
3618         };
3619
3620         if (!prefix)
3621                 prefix = "";
3622
3623         /*
3624          * check "[prefix]format = xxx"
3625          * SND_SOC_DAIFMT_FORMAT_MASK area
3626          */
3627         snprintf(prop, sizeof(prop), "%sformat", prefix);
3628         ret = of_property_read_string(np, prop, &str);
3629         if (ret == 0) {
3630                 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3631                         if (strcmp(str, of_fmt_table[i].name) == 0) {
3632                                 format |= of_fmt_table[i].val;
3633                                 break;
3634                         }
3635                 }
3636         }
3637
3638         /*
3639          * check "[prefix]continuous-clock"
3640          * SND_SOC_DAIFMT_CLOCK_MASK area
3641          */
3642         snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3643         if (of_get_property(np, prop, NULL))
3644                 format |= SND_SOC_DAIFMT_CONT;
3645         else
3646                 format |= SND_SOC_DAIFMT_GATED;
3647
3648         /*
3649          * check "[prefix]bitclock-inversion"
3650          * check "[prefix]frame-inversion"
3651          * SND_SOC_DAIFMT_INV_MASK area
3652          */
3653         snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3654         bit = !!of_get_property(np, prop, NULL);
3655
3656         snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3657         frame = !!of_get_property(np, prop, NULL);
3658
3659         switch ((bit << 4) + frame) {
3660         case 0x11:
3661                 format |= SND_SOC_DAIFMT_IB_IF;
3662                 break;
3663         case 0x10:
3664                 format |= SND_SOC_DAIFMT_IB_NF;
3665                 break;
3666         case 0x01:
3667                 format |= SND_SOC_DAIFMT_NB_IF;
3668                 break;
3669         default:
3670                 /* SND_SOC_DAIFMT_NB_NF is default */
3671                 break;
3672         }
3673
3674         /*
3675          * check "[prefix]bitclock-master"
3676          * check "[prefix]frame-master"
3677          * SND_SOC_DAIFMT_MASTER_MASK area
3678          */
3679         snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3680         bit = !!of_get_property(np, prop, NULL);
3681         if (bit && bitclkmaster)
3682                 *bitclkmaster = of_parse_phandle(np, prop, 0);
3683
3684         snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3685         frame = !!of_get_property(np, prop, NULL);
3686         if (frame && framemaster)
3687                 *framemaster = of_parse_phandle(np, prop, 0);
3688
3689         switch ((bit << 4) + frame) {
3690         case 0x11:
3691                 format |= SND_SOC_DAIFMT_CBM_CFM;
3692                 break;
3693         case 0x10:
3694                 format |= SND_SOC_DAIFMT_CBM_CFS;
3695                 break;
3696         case 0x01:
3697                 format |= SND_SOC_DAIFMT_CBS_CFM;
3698                 break;
3699         default:
3700                 format |= SND_SOC_DAIFMT_CBS_CFS;
3701                 break;
3702         }
3703
3704         return format;
3705 }
3706 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
3707
3708 int snd_soc_of_get_dai_name(struct device_node *of_node,
3709                             const char **dai_name)
3710 {
3711         struct snd_soc_component *pos;
3712         struct of_phandle_args args;
3713         int ret;
3714
3715         ret = of_parse_phandle_with_args(of_node, "sound-dai",
3716                                          "#sound-dai-cells", 0, &args);
3717         if (ret)
3718                 return ret;
3719
3720         ret = -EPROBE_DEFER;
3721
3722         mutex_lock(&client_mutex);
3723         list_for_each_entry(pos, &component_list, list) {
3724                 if (pos->dev->of_node != args.np)
3725                         continue;
3726
3727                 if (pos->driver->of_xlate_dai_name) {
3728                         ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
3729                 } else {
3730                         int id = -1;
3731
3732                         switch (args.args_count) {
3733                         case 0:
3734                                 id = 0; /* same as dai_drv[0] */
3735                                 break;
3736                         case 1:
3737                                 id = args.args[0];
3738                                 break;
3739                         default:
3740                                 /* not supported */
3741                                 break;
3742                         }
3743
3744                         if (id < 0 || id >= pos->num_dai) {
3745                                 ret = -EINVAL;
3746                                 continue;
3747                         }
3748
3749                         ret = 0;
3750
3751                         *dai_name = pos->dai_drv[id].name;
3752                         if (!*dai_name)
3753                                 *dai_name = pos->name;
3754                 }
3755
3756                 break;
3757         }
3758         mutex_unlock(&client_mutex);
3759
3760         of_node_put(args.np);
3761
3762         return ret;
3763 }
3764 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3765
3766 static int __init snd_soc_init(void)
3767 {
3768 #ifdef CONFIG_DEBUG_FS
3769         snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3770         if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3771                 pr_warn("ASoC: Failed to create debugfs directory\n");
3772                 snd_soc_debugfs_root = NULL;
3773         }
3774
3775         if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3776                                  &codec_list_fops))
3777                 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3778
3779         if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3780                                  &dai_list_fops))
3781                 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3782
3783         if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3784                                  &platform_list_fops))
3785                 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3786 #endif
3787
3788         snd_soc_util_init();
3789
3790         return platform_driver_register(&soc_driver);
3791 }
3792 module_init(snd_soc_init);
3793
3794 static void __exit snd_soc_exit(void)
3795 {
3796         snd_soc_util_exit();
3797
3798 #ifdef CONFIG_DEBUG_FS
3799         debugfs_remove_recursive(snd_soc_debugfs_root);
3800 #endif
3801         platform_driver_unregister(&soc_driver);
3802 }
3803 module_exit(snd_soc_exit);
3804
3805 /* Module information */
3806 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3807 MODULE_DESCRIPTION("ALSA SoC Core");
3808 MODULE_LICENSE("GPL");
3809 MODULE_ALIAS("platform:soc-audio");