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