Merge remote-tracking branch 'asoc/fix/fsl-sai' into asoc-linus
[cascardo/linux.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 struct dentry *soc_debugfs_create_dir(struct dentry *parent,
274         const char *fmt, ...)
275 {
276         struct dentry *de;
277         va_list ap;
278         char *s;
279
280         va_start(ap, fmt);
281         s = kvasprintf(GFP_KERNEL, fmt, ap);
282         va_end(ap);
283
284         if (!s)
285                 return NULL;
286
287         de = debugfs_create_dir(s, parent);
288         kfree(s);
289
290         return de;
291 }
292
293 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
294 {
295         struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
296
297         codec->debugfs_codec_root = soc_debugfs_create_dir(debugfs_card_root,
298                                                 "codec:%s", codec->name);
299         if (!codec->debugfs_codec_root) {
300                 dev_warn(codec->dev,
301                         "ASoC: Failed to create codec debugfs directory\n");
302                 return;
303         }
304
305         debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
306                             &codec->cache_sync);
307         debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
308                             &codec->cache_only);
309
310         codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
311                                                  codec->debugfs_codec_root,
312                                                  codec, &codec_reg_fops);
313         if (!codec->debugfs_reg)
314                 dev_warn(codec->dev,
315                         "ASoC: Failed to create codec register debugfs file\n");
316
317         snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
318 }
319
320 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
321 {
322         debugfs_remove_recursive(codec->debugfs_codec_root);
323 }
324
325 static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
326 {
327         struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
328
329         platform->debugfs_platform_root = soc_debugfs_create_dir(debugfs_card_root,
330                                                 "platform:%s", platform->name);
331         if (!platform->debugfs_platform_root) {
332                 dev_warn(platform->dev,
333                         "ASoC: Failed to create platform debugfs directory\n");
334                 return;
335         }
336
337         snd_soc_dapm_debugfs_init(&platform->dapm,
338                 platform->debugfs_platform_root);
339 }
340
341 static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
342 {
343         debugfs_remove_recursive(platform->debugfs_platform_root);
344 }
345
346 static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
347                                     size_t count, loff_t *ppos)
348 {
349         char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
350         ssize_t len, ret = 0;
351         struct snd_soc_codec *codec;
352
353         if (!buf)
354                 return -ENOMEM;
355
356         list_for_each_entry(codec, &codec_list, list) {
357                 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
358                                codec->name);
359                 if (len >= 0)
360                         ret += len;
361                 if (ret > PAGE_SIZE) {
362                         ret = PAGE_SIZE;
363                         break;
364                 }
365         }
366
367         if (ret >= 0)
368                 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
369
370         kfree(buf);
371
372         return ret;
373 }
374
375 static const struct file_operations codec_list_fops = {
376         .read = codec_list_read_file,
377         .llseek = default_llseek,/* read accesses f_pos */
378 };
379
380 static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
381                                   size_t count, loff_t *ppos)
382 {
383         char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
384         ssize_t len, ret = 0;
385         struct snd_soc_component *component;
386         struct snd_soc_dai *dai;
387
388         if (!buf)
389                 return -ENOMEM;
390
391         list_for_each_entry(component, &component_list, list) {
392                 list_for_each_entry(dai, &component->dai_list, list) {
393                         len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
394                                 dai->name);
395                         if (len >= 0)
396                                 ret += len;
397                         if (ret > PAGE_SIZE) {
398                                 ret = PAGE_SIZE;
399                                 break;
400                         }
401                 }
402         }
403
404         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
405
406         kfree(buf);
407
408         return ret;
409 }
410
411 static const struct file_operations dai_list_fops = {
412         .read = dai_list_read_file,
413         .llseek = default_llseek,/* read accesses f_pos */
414 };
415
416 static ssize_t platform_list_read_file(struct file *file,
417                                        char __user *user_buf,
418                                        size_t count, loff_t *ppos)
419 {
420         char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
421         ssize_t len, ret = 0;
422         struct snd_soc_platform *platform;
423
424         if (!buf)
425                 return -ENOMEM;
426
427         list_for_each_entry(platform, &platform_list, list) {
428                 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
429                                platform->name);
430                 if (len >= 0)
431                         ret += len;
432                 if (ret > PAGE_SIZE) {
433                         ret = PAGE_SIZE;
434                         break;
435                 }
436         }
437
438         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
439
440         kfree(buf);
441
442         return ret;
443 }
444
445 static const struct file_operations platform_list_fops = {
446         .read = platform_list_read_file,
447         .llseek = default_llseek,/* read accesses f_pos */
448 };
449
450 static void soc_init_card_debugfs(struct snd_soc_card *card)
451 {
452         card->debugfs_card_root = debugfs_create_dir(card->name,
453                                                      snd_soc_debugfs_root);
454         if (!card->debugfs_card_root) {
455                 dev_warn(card->dev,
456                          "ASoC: Failed to create card debugfs directory\n");
457                 return;
458         }
459
460         card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
461                                                     card->debugfs_card_root,
462                                                     &card->pop_time);
463         if (!card->debugfs_pop_time)
464                 dev_warn(card->dev,
465                        "ASoC: Failed to create pop time debugfs file\n");
466 }
467
468 static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
469 {
470         debugfs_remove_recursive(card->debugfs_card_root);
471 }
472
473 #else
474
475 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
476 {
477 }
478
479 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
480 {
481 }
482
483 static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
484 {
485 }
486
487 static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
488 {
489 }
490
491 static inline void soc_init_card_debugfs(struct snd_soc_card *card)
492 {
493 }
494
495 static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
496 {
497 }
498 #endif
499
500 struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
501                 const char *dai_link, int stream)
502 {
503         int i;
504
505         for (i = 0; i < card->num_links; i++) {
506                 if (card->rtd[i].dai_link->no_pcm &&
507                         !strcmp(card->rtd[i].dai_link->name, dai_link))
508                         return card->rtd[i].pcm->streams[stream].substream;
509         }
510         dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
511         return NULL;
512 }
513 EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
514
515 struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
516                 const char *dai_link)
517 {
518         int i;
519
520         for (i = 0; i < card->num_links; i++) {
521                 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
522                         return &card->rtd[i];
523         }
524         dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
525         return NULL;
526 }
527 EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
528
529 #ifdef CONFIG_SND_SOC_AC97_BUS
530 /* unregister ac97 codec */
531 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
532 {
533         if (codec->ac97->dev.bus)
534                 device_unregister(&codec->ac97->dev);
535         return 0;
536 }
537
538 /* stop no dev release warning */
539 static void soc_ac97_device_release(struct device *dev){}
540
541 /* register ac97 codec to bus */
542 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
543 {
544         int err;
545
546         codec->ac97->dev.bus = &ac97_bus_type;
547         codec->ac97->dev.parent = codec->card->dev;
548         codec->ac97->dev.release = soc_ac97_device_release;
549
550         dev_set_name(&codec->ac97->dev, "%d-%d:%s",
551                      codec->card->snd_card->number, 0, codec->name);
552         err = device_register(&codec->ac97->dev);
553         if (err < 0) {
554                 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
555                 codec->ac97->dev.bus = NULL;
556                 return err;
557         }
558         return 0;
559 }
560 #endif
561
562 static void codec2codec_close_delayed_work(struct work_struct *work)
563 {
564         /* Currently nothing to do for c2c links
565          * Since c2c links are internal nodes in the DAPM graph and
566          * don't interface with the outside world or application layer
567          * we don't have to do any special handling on close.
568          */
569 }
570
571 #ifdef CONFIG_PM_SLEEP
572 /* powers down audio subsystem for suspend */
573 int snd_soc_suspend(struct device *dev)
574 {
575         struct snd_soc_card *card = dev_get_drvdata(dev);
576         struct snd_soc_codec *codec;
577         int i;
578
579         /* If the initialization of this soc device failed, there is no codec
580          * associated with it. Just bail out in this case.
581          */
582         if (list_empty(&card->codec_dev_list))
583                 return 0;
584
585         /* Due to the resume being scheduled into a workqueue we could
586         * suspend before that's finished - wait for it to complete.
587          */
588         snd_power_lock(card->snd_card);
589         snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
590         snd_power_unlock(card->snd_card);
591
592         /* we're going to block userspace touching us until resume completes */
593         snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
594
595         /* mute any active DACs */
596         for (i = 0; i < card->num_rtd; i++) {
597                 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
598                 struct snd_soc_dai_driver *drv = dai->driver;
599
600                 if (card->rtd[i].dai_link->ignore_suspend)
601                         continue;
602
603                 if (drv->ops->digital_mute && dai->playback_active)
604                         drv->ops->digital_mute(dai, 1);
605         }
606
607         /* suspend all pcms */
608         for (i = 0; i < card->num_rtd; i++) {
609                 if (card->rtd[i].dai_link->ignore_suspend)
610                         continue;
611
612                 snd_pcm_suspend_all(card->rtd[i].pcm);
613         }
614
615         if (card->suspend_pre)
616                 card->suspend_pre(card);
617
618         for (i = 0; i < card->num_rtd; i++) {
619                 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
620                 struct snd_soc_platform *platform = card->rtd[i].platform;
621
622                 if (card->rtd[i].dai_link->ignore_suspend)
623                         continue;
624
625                 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
626                         cpu_dai->driver->suspend(cpu_dai);
627                 if (platform->driver->suspend && !platform->suspended) {
628                         platform->driver->suspend(cpu_dai);
629                         platform->suspended = 1;
630                 }
631         }
632
633         /* close any waiting streams and save state */
634         for (i = 0; i < card->num_rtd; i++) {
635                 flush_delayed_work(&card->rtd[i].delayed_work);
636                 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
637         }
638
639         for (i = 0; i < card->num_rtd; i++) {
640
641                 if (card->rtd[i].dai_link->ignore_suspend)
642                         continue;
643
644                 snd_soc_dapm_stream_event(&card->rtd[i],
645                                           SNDRV_PCM_STREAM_PLAYBACK,
646                                           SND_SOC_DAPM_STREAM_SUSPEND);
647
648                 snd_soc_dapm_stream_event(&card->rtd[i],
649                                           SNDRV_PCM_STREAM_CAPTURE,
650                                           SND_SOC_DAPM_STREAM_SUSPEND);
651         }
652
653         /* Recheck all analogue paths too */
654         dapm_mark_io_dirty(&card->dapm);
655         snd_soc_dapm_sync(&card->dapm);
656
657         /* suspend all CODECs */
658         list_for_each_entry(codec, &card->codec_dev_list, card_list) {
659                 /* If there are paths active then the CODEC will be held with
660                  * bias _ON and should not be suspended. */
661                 if (!codec->suspended && codec->driver->suspend) {
662                         switch (codec->dapm.bias_level) {
663                         case SND_SOC_BIAS_STANDBY:
664                                 /*
665                                  * If the CODEC is capable of idle
666                                  * bias off then being in STANDBY
667                                  * means it's doing something,
668                                  * otherwise fall through.
669                                  */
670                                 if (codec->dapm.idle_bias_off) {
671                                         dev_dbg(codec->dev,
672                                                 "ASoC: idle_bias_off CODEC on over suspend\n");
673                                         break;
674                                 }
675                         case SND_SOC_BIAS_OFF:
676                                 codec->driver->suspend(codec);
677                                 codec->suspended = 1;
678                                 codec->cache_sync = 1;
679                                 if (codec->component.regmap)
680                                         regcache_mark_dirty(codec->component.regmap);
681                                 /* deactivate pins to sleep state */
682                                 pinctrl_pm_select_sleep_state(codec->dev);
683                                 break;
684                         default:
685                                 dev_dbg(codec->dev,
686                                         "ASoC: CODEC is on over suspend\n");
687                                 break;
688                         }
689                 }
690         }
691
692         for (i = 0; i < card->num_rtd; i++) {
693                 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
694
695                 if (card->rtd[i].dai_link->ignore_suspend)
696                         continue;
697
698                 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
699                         cpu_dai->driver->suspend(cpu_dai);
700
701                 /* deactivate pins to sleep state */
702                 pinctrl_pm_select_sleep_state(cpu_dai->dev);
703         }
704
705         if (card->suspend_post)
706                 card->suspend_post(card);
707
708         return 0;
709 }
710 EXPORT_SYMBOL_GPL(snd_soc_suspend);
711
712 /* deferred resume work, so resume can complete before we finished
713  * setting our codec back up, which can be very slow on I2C
714  */
715 static void soc_resume_deferred(struct work_struct *work)
716 {
717         struct snd_soc_card *card =
718                         container_of(work, struct snd_soc_card, deferred_resume_work);
719         struct snd_soc_codec *codec;
720         int i;
721
722         /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
723          * so userspace apps are blocked from touching us
724          */
725
726         dev_dbg(card->dev, "ASoC: starting resume work\n");
727
728         /* Bring us up into D2 so that DAPM starts enabling things */
729         snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
730
731         if (card->resume_pre)
732                 card->resume_pre(card);
733
734         /* resume AC97 DAIs */
735         for (i = 0; i < card->num_rtd; i++) {
736                 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
737
738                 if (card->rtd[i].dai_link->ignore_suspend)
739                         continue;
740
741                 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
742                         cpu_dai->driver->resume(cpu_dai);
743         }
744
745         list_for_each_entry(codec, &card->codec_dev_list, card_list) {
746                 /* If the CODEC was idle over suspend then it will have been
747                  * left with bias OFF or STANDBY and suspended so we must now
748                  * resume.  Otherwise the suspend was suppressed.
749                  */
750                 if (codec->driver->resume && codec->suspended) {
751                         switch (codec->dapm.bias_level) {
752                         case SND_SOC_BIAS_STANDBY:
753                         case SND_SOC_BIAS_OFF:
754                                 codec->driver->resume(codec);
755                                 codec->suspended = 0;
756                                 break;
757                         default:
758                                 dev_dbg(codec->dev,
759                                         "ASoC: CODEC was on over suspend\n");
760                                 break;
761                         }
762                 }
763         }
764
765         for (i = 0; i < card->num_rtd; i++) {
766
767                 if (card->rtd[i].dai_link->ignore_suspend)
768                         continue;
769
770                 snd_soc_dapm_stream_event(&card->rtd[i],
771                                           SNDRV_PCM_STREAM_PLAYBACK,
772                                           SND_SOC_DAPM_STREAM_RESUME);
773
774                 snd_soc_dapm_stream_event(&card->rtd[i],
775                                           SNDRV_PCM_STREAM_CAPTURE,
776                                           SND_SOC_DAPM_STREAM_RESUME);
777         }
778
779         /* unmute any active DACs */
780         for (i = 0; i < card->num_rtd; i++) {
781                 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
782                 struct snd_soc_dai_driver *drv = dai->driver;
783
784                 if (card->rtd[i].dai_link->ignore_suspend)
785                         continue;
786
787                 if (drv->ops->digital_mute && dai->playback_active)
788                         drv->ops->digital_mute(dai, 0);
789         }
790
791         for (i = 0; i < card->num_rtd; i++) {
792                 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
793                 struct snd_soc_platform *platform = card->rtd[i].platform;
794
795                 if (card->rtd[i].dai_link->ignore_suspend)
796                         continue;
797
798                 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
799                         cpu_dai->driver->resume(cpu_dai);
800                 if (platform->driver->resume && platform->suspended) {
801                         platform->driver->resume(cpu_dai);
802                         platform->suspended = 0;
803                 }
804         }
805
806         if (card->resume_post)
807                 card->resume_post(card);
808
809         dev_dbg(card->dev, "ASoC: resume work completed\n");
810
811         /* userspace can access us now we are back as we were before */
812         snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
813
814         /* Recheck all analogue paths too */
815         dapm_mark_io_dirty(&card->dapm);
816         snd_soc_dapm_sync(&card->dapm);
817 }
818
819 /* powers up audio subsystem after a suspend */
820 int snd_soc_resume(struct device *dev)
821 {
822         struct snd_soc_card *card = dev_get_drvdata(dev);
823         int i, ac97_control = 0;
824
825         /* If the initialization of this soc device failed, there is no codec
826          * associated with it. Just bail out in this case.
827          */
828         if (list_empty(&card->codec_dev_list))
829                 return 0;
830
831         /* activate pins from sleep state */
832         for (i = 0; i < card->num_rtd; i++) {
833                 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
834                 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
835                 if (cpu_dai->active)
836                         pinctrl_pm_select_default_state(cpu_dai->dev);
837                 if (codec_dai->active)
838                         pinctrl_pm_select_default_state(codec_dai->dev);
839         }
840
841         /* AC97 devices might have other drivers hanging off them so
842          * need to resume immediately.  Other drivers don't have that
843          * problem and may take a substantial amount of time to resume
844          * due to I/O costs and anti-pop so handle them out of line.
845          */
846         for (i = 0; i < card->num_rtd; i++) {
847                 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
848                 ac97_control |= cpu_dai->driver->ac97_control;
849         }
850         if (ac97_control) {
851                 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
852                 soc_resume_deferred(&card->deferred_resume_work);
853         } else {
854                 dev_dbg(dev, "ASoC: Scheduling resume work\n");
855                 if (!schedule_work(&card->deferred_resume_work))
856                         dev_err(dev, "ASoC: resume work item may be lost\n");
857         }
858
859         return 0;
860 }
861 EXPORT_SYMBOL_GPL(snd_soc_resume);
862 #else
863 #define snd_soc_suspend NULL
864 #define snd_soc_resume NULL
865 #endif
866
867 static const struct snd_soc_dai_ops null_dai_ops = {
868 };
869
870 static struct snd_soc_codec *soc_find_codec(const struct device_node *codec_of_node,
871                                             const char *codec_name)
872 {
873         struct snd_soc_codec *codec;
874
875         list_for_each_entry(codec, &codec_list, list) {
876                 if (codec_of_node) {
877                         if (codec->dev->of_node != codec_of_node)
878                                 continue;
879                 } else {
880                         if (strcmp(codec->name, codec_name))
881                                 continue;
882                 }
883
884                 return codec;
885         }
886
887         return NULL;
888 }
889
890 static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
891                                               const char *codec_dai_name)
892 {
893         struct snd_soc_dai *codec_dai;
894
895         list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
896                 if (!strcmp(codec_dai->name, codec_dai_name)) {
897                         return codec_dai;
898                 }
899         }
900
901         return NULL;
902 }
903
904 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
905 {
906         struct snd_soc_dai_link *dai_link = &card->dai_link[num];
907         struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
908         struct snd_soc_component *component;
909         struct snd_soc_platform *platform;
910         struct snd_soc_dai *cpu_dai;
911         const char *platform_name;
912
913         dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
914
915         /* Find CPU DAI from registered DAIs*/
916         list_for_each_entry(component, &component_list, list) {
917                 if (dai_link->cpu_of_node &&
918                         component->dev->of_node != dai_link->cpu_of_node)
919                         continue;
920                 if (dai_link->cpu_name &&
921                         strcmp(dev_name(component->dev), dai_link->cpu_name))
922                         continue;
923                 list_for_each_entry(cpu_dai, &component->dai_list, list) {
924                         if (dai_link->cpu_dai_name &&
925                                 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
926                                 continue;
927
928                         rtd->cpu_dai = cpu_dai;
929                 }
930         }
931
932         if (!rtd->cpu_dai) {
933                 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
934                         dai_link->cpu_dai_name);
935                 return -EPROBE_DEFER;
936         }
937
938         /* Find CODEC from registered list */
939         rtd->codec = soc_find_codec(dai_link->codec_of_node,
940                                     dai_link->codec_name);
941         if (!rtd->codec) {
942                 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
943                         dai_link->codec_name);
944                 return -EPROBE_DEFER;
945         }
946
947         /* Find CODEC DAI from registered list */
948         rtd->codec_dai = soc_find_codec_dai(rtd->codec,
949                                             dai_link->codec_dai_name);
950         if (!rtd->codec_dai) {
951                 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
952                         dai_link->codec_dai_name);
953                 return -EPROBE_DEFER;
954         }
955
956         /* if there's no platform we match on the empty platform */
957         platform_name = dai_link->platform_name;
958         if (!platform_name && !dai_link->platform_of_node)
959                 platform_name = "snd-soc-dummy";
960
961         /* find one from the set of registered platforms */
962         list_for_each_entry(platform, &platform_list, list) {
963                 if (dai_link->platform_of_node) {
964                         if (platform->dev->of_node !=
965                             dai_link->platform_of_node)
966                                 continue;
967                 } else {
968                         if (strcmp(platform->name, platform_name))
969                                 continue;
970                 }
971
972                 rtd->platform = platform;
973         }
974         if (!rtd->platform) {
975                 dev_err(card->dev, "ASoC: platform %s not registered\n",
976                         dai_link->platform_name);
977                 return -EPROBE_DEFER;
978         }
979
980         card->num_rtd++;
981
982         return 0;
983 }
984
985 static int soc_remove_platform(struct snd_soc_platform *platform)
986 {
987         int ret;
988
989         if (platform->driver->remove) {
990                 ret = platform->driver->remove(platform);
991                 if (ret < 0)
992                         dev_err(platform->dev, "ASoC: failed to remove %d\n",
993                                 ret);
994         }
995
996         /* Make sure all DAPM widgets are freed */
997         snd_soc_dapm_free(&platform->dapm);
998
999         soc_cleanup_platform_debugfs(platform);
1000         platform->probed = 0;
1001         list_del(&platform->card_list);
1002         module_put(platform->dev->driver->owner);
1003
1004         return 0;
1005 }
1006
1007 static void soc_remove_codec(struct snd_soc_codec *codec)
1008 {
1009         int err;
1010
1011         if (codec->driver->remove) {
1012                 err = codec->driver->remove(codec);
1013                 if (err < 0)
1014                         dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
1015         }
1016
1017         /* Make sure all DAPM widgets are freed */
1018         snd_soc_dapm_free(&codec->dapm);
1019
1020         soc_cleanup_codec_debugfs(codec);
1021         codec->probed = 0;
1022         list_del(&codec->card_list);
1023         module_put(codec->dev->driver->owner);
1024 }
1025
1026 static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1027 {
1028         int err;
1029
1030         if (codec_dai && codec_dai->probed &&
1031                         codec_dai->driver->remove_order == order) {
1032                 if (codec_dai->driver->remove) {
1033                         err = codec_dai->driver->remove(codec_dai);
1034                         if (err < 0)
1035                                 dev_err(codec_dai->dev,
1036                                         "ASoC: failed to remove %s: %d\n",
1037                                         codec_dai->name, err);
1038                 }
1039                 codec_dai->probed = 0;
1040         }
1041 }
1042
1043 static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
1044 {
1045         struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1046         struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1047         int err;
1048
1049         /* unregister the rtd device */
1050         if (rtd->dev_registered) {
1051                 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1052                 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1053                 device_unregister(rtd->dev);
1054                 rtd->dev_registered = 0;
1055         }
1056
1057         /* remove the CODEC DAI */
1058         soc_remove_codec_dai(codec_dai, order);
1059
1060         /* remove the cpu_dai */
1061         if (cpu_dai && cpu_dai->probed &&
1062                         cpu_dai->driver->remove_order == order) {
1063                 if (cpu_dai->driver->remove) {
1064                         err = cpu_dai->driver->remove(cpu_dai);
1065                         if (err < 0)
1066                                 dev_err(cpu_dai->dev,
1067                                         "ASoC: failed to remove %s: %d\n",
1068                                         cpu_dai->name, err);
1069                 }
1070                 cpu_dai->probed = 0;
1071
1072                 if (!cpu_dai->codec) {
1073                         snd_soc_dapm_free(&cpu_dai->dapm);
1074                         module_put(cpu_dai->dev->driver->owner);
1075                 }
1076         }
1077 }
1078
1079 static void soc_remove_link_components(struct snd_soc_card *card, int num,
1080                                        int order)
1081 {
1082         struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1083         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1084         struct snd_soc_dai *codec_dai = rtd->codec_dai;
1085         struct snd_soc_platform *platform = rtd->platform;
1086         struct snd_soc_codec *codec;
1087
1088         /* remove the platform */
1089         if (platform && platform->probed &&
1090             platform->driver->remove_order == order) {
1091                 soc_remove_platform(platform);
1092         }
1093
1094         /* remove the CODEC-side CODEC */
1095         if (codec_dai) {
1096                 codec = codec_dai->codec;
1097                 if (codec && codec->probed &&
1098                     codec->driver->remove_order == order)
1099                         soc_remove_codec(codec);
1100         }
1101
1102         /* remove any CPU-side CODEC */
1103         if (cpu_dai) {
1104                 codec = cpu_dai->codec;
1105                 if (codec && codec->probed &&
1106                     codec->driver->remove_order == order)
1107                         soc_remove_codec(codec);
1108         }
1109 }
1110
1111 static void soc_remove_dai_links(struct snd_soc_card *card)
1112 {
1113         int dai, order;
1114
1115         for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1116                         order++) {
1117                 for (dai = 0; dai < card->num_rtd; dai++)
1118                         soc_remove_link_dais(card, dai, order);
1119         }
1120
1121         for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1122                         order++) {
1123                 for (dai = 0; dai < card->num_rtd; dai++)
1124                         soc_remove_link_components(card, dai, order);
1125         }
1126
1127         card->num_rtd = 0;
1128 }
1129
1130 static void soc_set_name_prefix(struct snd_soc_card *card,
1131                                 struct snd_soc_codec *codec)
1132 {
1133         int i;
1134
1135         if (card->codec_conf == NULL)
1136                 return;
1137
1138         for (i = 0; i < card->num_configs; i++) {
1139                 struct snd_soc_codec_conf *map = &card->codec_conf[i];
1140                 if (map->of_node && codec->dev->of_node != map->of_node)
1141                         continue;
1142                 if (map->dev_name && strcmp(codec->name, map->dev_name))
1143                         continue;
1144                 codec->name_prefix = map->name_prefix;
1145                 break;
1146         }
1147 }
1148
1149 static int soc_probe_codec(struct snd_soc_card *card,
1150                            struct snd_soc_codec *codec)
1151 {
1152         int ret = 0;
1153         const struct snd_soc_codec_driver *driver = codec->driver;
1154         struct snd_soc_dai *dai;
1155
1156         codec->card = card;
1157         codec->dapm.card = card;
1158         soc_set_name_prefix(card, codec);
1159
1160         if (!try_module_get(codec->dev->driver->owner))
1161                 return -ENODEV;
1162
1163         soc_init_codec_debugfs(codec);
1164
1165         if (driver->dapm_widgets) {
1166                 ret = snd_soc_dapm_new_controls(&codec->dapm,
1167                                                 driver->dapm_widgets,
1168                                                 driver->num_dapm_widgets);
1169
1170                 if (ret != 0) {
1171                         dev_err(codec->dev,
1172                                 "Failed to create new controls %d\n", ret);
1173                         goto err_probe;
1174                 }
1175         }
1176
1177         /* Create DAPM widgets for each DAI stream */
1178         list_for_each_entry(dai, &codec->component.dai_list, list) {
1179                 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1180
1181                 if (ret != 0) {
1182                         dev_err(codec->dev,
1183                                 "Failed to create DAI widgets %d\n", ret);
1184                         goto err_probe;
1185                 }
1186         }
1187
1188         codec->dapm.idle_bias_off = driver->idle_bias_off;
1189
1190         if (driver->probe) {
1191                 ret = driver->probe(codec);
1192                 if (ret < 0) {
1193                         dev_err(codec->dev,
1194                                 "ASoC: failed to probe CODEC %d\n", ret);
1195                         goto err_probe;
1196                 }
1197                 WARN(codec->dapm.idle_bias_off &&
1198                         codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1199                         "codec %s can not start from non-off bias with idle_bias_off==1\n",
1200                         codec->name);
1201         }
1202
1203         if (driver->controls)
1204                 snd_soc_add_codec_controls(codec, driver->controls,
1205                                      driver->num_controls);
1206         if (driver->dapm_routes)
1207                 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1208                                         driver->num_dapm_routes);
1209
1210         /* mark codec as probed and add to card codec list */
1211         codec->probed = 1;
1212         list_add(&codec->card_list, &card->codec_dev_list);
1213         list_add(&codec->dapm.list, &card->dapm_list);
1214
1215         return 0;
1216
1217 err_probe:
1218         soc_cleanup_codec_debugfs(codec);
1219         module_put(codec->dev->driver->owner);
1220
1221         return ret;
1222 }
1223
1224 static int soc_probe_platform(struct snd_soc_card *card,
1225                            struct snd_soc_platform *platform)
1226 {
1227         int ret = 0;
1228         const struct snd_soc_platform_driver *driver = platform->driver;
1229         struct snd_soc_component *component;
1230         struct snd_soc_dai *dai;
1231
1232         platform->card = card;
1233         platform->dapm.card = card;
1234
1235         if (!try_module_get(platform->dev->driver->owner))
1236                 return -ENODEV;
1237
1238         soc_init_platform_debugfs(platform);
1239
1240         if (driver->dapm_widgets)
1241                 snd_soc_dapm_new_controls(&platform->dapm,
1242                         driver->dapm_widgets, driver->num_dapm_widgets);
1243
1244         /* Create DAPM widgets for each DAI stream */
1245         list_for_each_entry(component, &component_list, list) {
1246                 if (component->dev != platform->dev)
1247                         continue;
1248                 list_for_each_entry(dai, &component->dai_list, list)
1249                         snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1250         }
1251
1252         platform->dapm.idle_bias_off = 1;
1253
1254         if (driver->probe) {
1255                 ret = driver->probe(platform);
1256                 if (ret < 0) {
1257                         dev_err(platform->dev,
1258                                 "ASoC: failed to probe platform %d\n", ret);
1259                         goto err_probe;
1260                 }
1261         }
1262
1263         if (driver->controls)
1264                 snd_soc_add_platform_controls(platform, driver->controls,
1265                                      driver->num_controls);
1266         if (driver->dapm_routes)
1267                 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1268                                         driver->num_dapm_routes);
1269
1270         /* mark platform as probed and add to card platform list */
1271         platform->probed = 1;
1272         list_add(&platform->card_list, &card->platform_dev_list);
1273         list_add(&platform->dapm.list, &card->dapm_list);
1274
1275         return 0;
1276
1277 err_probe:
1278         soc_cleanup_platform_debugfs(platform);
1279         module_put(platform->dev->driver->owner);
1280
1281         return ret;
1282 }
1283
1284 static void rtd_release(struct device *dev)
1285 {
1286         kfree(dev);
1287 }
1288
1289 static int soc_aux_dev_init(struct snd_soc_card *card,
1290                             struct snd_soc_codec *codec,
1291                             int num)
1292 {
1293         struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1294         struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1295         int ret;
1296
1297         rtd->card = card;
1298
1299         /* do machine specific initialization */
1300         if (aux_dev->init) {
1301                 ret = aux_dev->init(&codec->dapm);
1302                 if (ret < 0)
1303                         return ret;
1304         }
1305
1306         rtd->codec = codec;
1307
1308         return 0;
1309 }
1310
1311 static int soc_dai_link_init(struct snd_soc_card *card,
1312                              struct snd_soc_codec *codec,
1313                              int num)
1314 {
1315         struct snd_soc_dai_link *dai_link =  &card->dai_link[num];
1316         struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1317         int ret;
1318
1319         rtd->card = card;
1320
1321         /* do machine specific initialization */
1322         if (dai_link->init) {
1323                 ret = dai_link->init(rtd);
1324                 if (ret < 0)
1325                         return ret;
1326         }
1327
1328         rtd->codec = codec;
1329
1330         return 0;
1331 }
1332
1333 static int soc_post_component_init(struct snd_soc_card *card,
1334                                    struct snd_soc_codec *codec,
1335                                    int num, int dailess)
1336 {
1337         struct snd_soc_dai_link *dai_link = NULL;
1338         struct snd_soc_aux_dev *aux_dev = NULL;
1339         struct snd_soc_pcm_runtime *rtd;
1340         const char *name;
1341         int ret = 0;
1342
1343         if (!dailess) {
1344                 dai_link = &card->dai_link[num];
1345                 rtd = &card->rtd[num];
1346                 name = dai_link->name;
1347                 ret = soc_dai_link_init(card, codec, num);
1348         } else {
1349                 aux_dev = &card->aux_dev[num];
1350                 rtd = &card->rtd_aux[num];
1351                 name = aux_dev->name;
1352                 ret = soc_aux_dev_init(card, codec, num);
1353         }
1354
1355         if (ret < 0) {
1356                 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1357                 return ret;
1358         }
1359
1360         /* register the rtd device */
1361         rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1362         if (!rtd->dev)
1363                 return -ENOMEM;
1364         device_initialize(rtd->dev);
1365         rtd->dev->parent = card->dev;
1366         rtd->dev->release = rtd_release;
1367         rtd->dev->init_name = name;
1368         dev_set_drvdata(rtd->dev, rtd);
1369         mutex_init(&rtd->pcm_mutex);
1370         INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1371         INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1372         INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1373         INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1374         ret = device_add(rtd->dev);
1375         if (ret < 0) {
1376                 /* calling put_device() here to free the rtd->dev */
1377                 put_device(rtd->dev);
1378                 dev_err(card->dev,
1379                         "ASoC: failed to register runtime device: %d\n", ret);
1380                 return ret;
1381         }
1382         rtd->dev_registered = 1;
1383
1384         /* add DAPM sysfs entries for this codec */
1385         ret = snd_soc_dapm_sys_add(rtd->dev);
1386         if (ret < 0)
1387                 dev_err(codec->dev,
1388                         "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1389
1390         /* add codec sysfs entries */
1391         ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1392         if (ret < 0)
1393                 dev_err(codec->dev,
1394                         "ASoC: failed to add codec sysfs files: %d\n", ret);
1395
1396 #ifdef CONFIG_DEBUG_FS
1397         /* add DPCM sysfs entries */
1398         if (!dailess && !dai_link->dynamic)
1399                 goto out;
1400
1401         ret = soc_dpcm_debugfs_add(rtd);
1402         if (ret < 0)
1403                 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1404
1405 out:
1406 #endif
1407         return 0;
1408 }
1409
1410 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1411                                      int order)
1412 {
1413         struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1414         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1415         struct snd_soc_dai *codec_dai = rtd->codec_dai;
1416         struct snd_soc_platform *platform = rtd->platform;
1417         int ret;
1418
1419         /* probe the CPU-side component, if it is a CODEC */
1420         if (cpu_dai->codec &&
1421             !cpu_dai->codec->probed &&
1422             cpu_dai->codec->driver->probe_order == order) {
1423                 ret = soc_probe_codec(card, cpu_dai->codec);
1424                 if (ret < 0)
1425                         return ret;
1426         }
1427
1428         /* probe the CODEC-side component */
1429         if (!codec_dai->codec->probed &&
1430             codec_dai->codec->driver->probe_order == order) {
1431                 ret = soc_probe_codec(card, codec_dai->codec);
1432                 if (ret < 0)
1433                         return ret;
1434         }
1435
1436         /* probe the platform */
1437         if (!platform->probed &&
1438             platform->driver->probe_order == order) {
1439                 ret = soc_probe_platform(card, platform);
1440                 if (ret < 0)
1441                         return ret;
1442         }
1443
1444         return 0;
1445 }
1446
1447 static int soc_probe_codec_dai(struct snd_soc_card *card,
1448                                struct snd_soc_dai *codec_dai,
1449                                int order)
1450 {
1451         int ret;
1452
1453         if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1454                 if (codec_dai->driver->probe) {
1455                         ret = codec_dai->driver->probe(codec_dai);
1456                         if (ret < 0) {
1457                                 dev_err(codec_dai->dev,
1458                                         "ASoC: failed to probe CODEC DAI %s: %d\n",
1459                                         codec_dai->name, ret);
1460                                 return ret;
1461                         }
1462                 }
1463
1464                 /* mark codec_dai as probed and add to card dai list */
1465                 codec_dai->probed = 1;
1466         }
1467
1468         return 0;
1469 }
1470
1471 static int soc_link_dai_widgets(struct snd_soc_card *card,
1472                                 struct snd_soc_dai_link *dai_link,
1473                                 struct snd_soc_dai *cpu_dai,
1474                                 struct snd_soc_dai *codec_dai)
1475 {
1476         struct snd_soc_dapm_widget *play_w, *capture_w;
1477         int ret;
1478
1479         /* link the DAI widgets */
1480         play_w = codec_dai->playback_widget;
1481         capture_w = cpu_dai->capture_widget;
1482         if (play_w && capture_w) {
1483                 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1484                                            capture_w, play_w);
1485                 if (ret != 0) {
1486                         dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1487                                 play_w->name, capture_w->name, ret);
1488                         return ret;
1489                 }
1490         }
1491
1492         play_w = cpu_dai->playback_widget;
1493         capture_w = codec_dai->capture_widget;
1494         if (play_w && capture_w) {
1495                 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1496                                            capture_w, play_w);
1497                 if (ret != 0) {
1498                         dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1499                                 play_w->name, capture_w->name, ret);
1500                         return ret;
1501                 }
1502         }
1503
1504         return 0;
1505 }
1506
1507 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1508 {
1509         struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1510         struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1511         struct snd_soc_codec *codec = rtd->codec;
1512         struct snd_soc_platform *platform = rtd->platform;
1513         struct snd_soc_dai *codec_dai = rtd->codec_dai;
1514         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1515         int ret;
1516
1517         dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1518                         card->name, num, order);
1519
1520         /* config components */
1521         cpu_dai->platform = platform;
1522         codec_dai->card = card;
1523         cpu_dai->card = card;
1524
1525         /* set default power off timeout */
1526         rtd->pmdown_time = pmdown_time;
1527
1528         /* probe the cpu_dai */
1529         if (!cpu_dai->probed &&
1530                         cpu_dai->driver->probe_order == order) {
1531                 if (!cpu_dai->codec) {
1532                         cpu_dai->dapm.card = card;
1533                         if (!try_module_get(cpu_dai->dev->driver->owner))
1534                                 return -ENODEV;
1535
1536                         list_add(&cpu_dai->dapm.list, &card->dapm_list);
1537                 }
1538
1539                 if (cpu_dai->driver->probe) {
1540                         ret = cpu_dai->driver->probe(cpu_dai);
1541                         if (ret < 0) {
1542                                 dev_err(cpu_dai->dev,
1543                                         "ASoC: failed to probe CPU DAI %s: %d\n",
1544                                         cpu_dai->name, ret);
1545                                 module_put(cpu_dai->dev->driver->owner);
1546                                 return ret;
1547                         }
1548                 }
1549                 cpu_dai->probed = 1;
1550         }
1551
1552         /* probe the CODEC DAI */
1553         ret = soc_probe_codec_dai(card, codec_dai, order);
1554         if (ret)
1555                 return ret;
1556
1557         /* complete DAI probe during last probe */
1558         if (order != SND_SOC_COMP_ORDER_LAST)
1559                 return 0;
1560
1561         ret = soc_post_component_init(card, codec, num, 0);
1562         if (ret)
1563                 return ret;
1564
1565         ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1566         if (ret < 0)
1567                 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1568                         ret);
1569
1570         if (cpu_dai->driver->compress_dai) {
1571                 /*create compress_device"*/
1572                 ret = soc_new_compress(rtd, num);
1573                 if (ret < 0) {
1574                         dev_err(card->dev, "ASoC: can't create compress %s\n",
1575                                          dai_link->stream_name);
1576                         return ret;
1577                 }
1578         } else {
1579
1580                 if (!dai_link->params) {
1581                         /* create the pcm */
1582                         ret = soc_new_pcm(rtd, num);
1583                         if (ret < 0) {
1584                                 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1585                                        dai_link->stream_name, ret);
1586                                 return ret;
1587                         }
1588                 } else {
1589                         INIT_DELAYED_WORK(&rtd->delayed_work,
1590                                                 codec2codec_close_delayed_work);
1591
1592                         /* link the DAI widgets */
1593                         ret = soc_link_dai_widgets(card, dai_link,
1594                                         cpu_dai, codec_dai);
1595                         if (ret)
1596                                 return ret;
1597                 }
1598         }
1599
1600         /* add platform data for AC97 devices */
1601         if (rtd->codec_dai->driver->ac97_control)
1602                 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1603
1604         return 0;
1605 }
1606
1607 #ifdef CONFIG_SND_SOC_AC97_BUS
1608 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1609                                    struct snd_soc_dai *codec_dai)
1610 {
1611         int ret;
1612
1613         /* Only instantiate AC97 if not already done by the adaptor
1614          * for the generic AC97 subsystem.
1615          */
1616         if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
1617                 /*
1618                  * It is possible that the AC97 device is already registered to
1619                  * the device subsystem. This happens when the device is created
1620                  * via snd_ac97_mixer(). Currently only SoC codec that does so
1621                  * is the generic AC97 glue but others migh emerge.
1622                  *
1623                  * In those cases we don't try to register the device again.
1624                  */
1625                 if (!codec->ac97_created)
1626                         return 0;
1627
1628                 ret = soc_ac97_dev_register(codec);
1629                 if (ret < 0) {
1630                         dev_err(codec->dev,
1631                                 "ASoC: AC97 device register failed: %d\n", ret);
1632                         return ret;
1633                 }
1634
1635                 codec->ac97_registered = 1;
1636         }
1637         return 0;
1638 }
1639
1640 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1641 {
1642         return soc_register_ac97_codec(rtd->codec, rtd->codec_dai);
1643 }
1644
1645 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
1646 {
1647         if (codec->ac97_registered) {
1648                 soc_ac97_dev_unregister(codec);
1649                 codec->ac97_registered = 0;
1650         }
1651 }
1652
1653 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1654 {
1655         soc_unregister_ac97_codec(rtd->codec);
1656 }
1657 #endif
1658
1659 static struct snd_soc_codec *soc_find_matching_codec(struct snd_soc_card *card,
1660         int num)
1661 {
1662         struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1663         struct snd_soc_codec *codec;
1664
1665         /* find CODEC from registered CODECs */
1666         list_for_each_entry(codec, &codec_list, list) {
1667                 if (aux_dev->codec_of_node &&
1668                    (codec->dev->of_node != aux_dev->codec_of_node))
1669                         continue;
1670                 if (aux_dev->codec_name && strcmp(codec->name, aux_dev->codec_name))
1671                         continue;
1672                 return codec;
1673         }
1674
1675         return NULL;
1676 }
1677
1678 static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1679 {
1680         struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1681         const char *codecname = aux_dev->codec_name;
1682         struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
1683
1684         if (codec)
1685                 return 0;
1686         if (aux_dev->codec_of_node)
1687                 codecname = of_node_full_name(aux_dev->codec_of_node);
1688
1689         dev_err(card->dev, "ASoC: %s not registered\n", codecname);
1690         return -EPROBE_DEFER;
1691 }
1692
1693 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1694 {
1695         struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1696         const char *codecname = aux_dev->codec_name;
1697         int ret = -ENODEV;
1698         struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
1699
1700         if (!codec) {
1701                 if (aux_dev->codec_of_node)
1702                         codecname = of_node_full_name(aux_dev->codec_of_node);
1703
1704                 /* codec not found */
1705                 dev_err(card->dev, "ASoC: codec %s not found", codecname);
1706                 return -EPROBE_DEFER;
1707         }
1708
1709         if (codec->probed) {
1710                 dev_err(codec->dev, "ASoC: codec already probed");
1711                 return -EBUSY;
1712         }
1713
1714         ret = soc_probe_codec(card, codec);
1715         if (ret < 0)
1716                 return ret;
1717
1718         ret = soc_post_component_init(card, codec, num, 1);
1719
1720         return ret;
1721 }
1722
1723 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1724 {
1725         struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1726         struct snd_soc_codec *codec = rtd->codec;
1727
1728         /* unregister the rtd device */
1729         if (rtd->dev_registered) {
1730                 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1731                 device_unregister(rtd->dev);
1732                 rtd->dev_registered = 0;
1733         }
1734
1735         if (codec && codec->probed)
1736                 soc_remove_codec(codec);
1737 }
1738
1739 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
1740 {
1741         int ret;
1742
1743         if (codec->cache_init)
1744                 return 0;
1745
1746         ret = snd_soc_cache_init(codec);
1747         if (ret < 0) {
1748                 dev_err(codec->dev,
1749                         "ASoC: Failed to set cache compression type: %d\n",
1750                         ret);
1751                 return ret;
1752         }
1753         codec->cache_init = 1;
1754         return 0;
1755 }
1756
1757 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1758 {
1759         struct snd_soc_codec *codec;
1760         struct snd_soc_dai_link *dai_link;
1761         int ret, i, order, dai_fmt;
1762
1763         mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1764
1765         /* bind DAIs */
1766         for (i = 0; i < card->num_links; i++) {
1767                 ret = soc_bind_dai_link(card, i);
1768                 if (ret != 0)
1769                         goto base_error;
1770         }
1771
1772         /* check aux_devs too */
1773         for (i = 0; i < card->num_aux_devs; i++) {
1774                 ret = soc_check_aux_dev(card, i);
1775                 if (ret != 0)
1776                         goto base_error;
1777         }
1778
1779         /* initialize the register cache for each available codec */
1780         list_for_each_entry(codec, &codec_list, list) {
1781                 if (codec->cache_init)
1782                         continue;
1783                 ret = snd_soc_init_codec_cache(codec);
1784                 if (ret < 0)
1785                         goto base_error;
1786         }
1787
1788         /* card bind complete so register a sound card */
1789         ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1790                         card->owner, 0, &card->snd_card);
1791         if (ret < 0) {
1792                 dev_err(card->dev,
1793                         "ASoC: can't create sound card for card %s: %d\n",
1794                         card->name, ret);
1795                 goto base_error;
1796         }
1797
1798         card->dapm.bias_level = SND_SOC_BIAS_OFF;
1799         card->dapm.dev = card->dev;
1800         card->dapm.card = card;
1801         list_add(&card->dapm.list, &card->dapm_list);
1802
1803 #ifdef CONFIG_DEBUG_FS
1804         snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1805 #endif
1806
1807 #ifdef CONFIG_PM_SLEEP
1808         /* deferred resume work */
1809         INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1810 #endif
1811
1812         if (card->dapm_widgets)
1813                 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1814                                           card->num_dapm_widgets);
1815
1816         /* initialise the sound card only once */
1817         if (card->probe) {
1818                 ret = card->probe(card);
1819                 if (ret < 0)
1820                         goto card_probe_error;
1821         }
1822
1823         /* probe all components used by DAI links on this card */
1824         for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1825                         order++) {
1826                 for (i = 0; i < card->num_links; i++) {
1827                         ret = soc_probe_link_components(card, i, order);
1828                         if (ret < 0) {
1829                                 dev_err(card->dev,
1830                                         "ASoC: failed to instantiate card %d\n",
1831                                         ret);
1832                                 goto probe_dai_err;
1833                         }
1834                 }
1835         }
1836
1837         /* probe all DAI links on this card */
1838         for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1839                         order++) {
1840                 for (i = 0; i < card->num_links; i++) {
1841                         ret = soc_probe_link_dais(card, i, order);
1842                         if (ret < 0) {
1843                                 dev_err(card->dev,
1844                                         "ASoC: failed to instantiate card %d\n",
1845                                         ret);
1846                                 goto probe_dai_err;
1847                         }
1848                 }
1849         }
1850
1851         for (i = 0; i < card->num_aux_devs; i++) {
1852                 ret = soc_probe_aux_dev(card, i);
1853                 if (ret < 0) {
1854                         dev_err(card->dev,
1855                                 "ASoC: failed to add auxiliary devices %d\n",
1856                                 ret);
1857                         goto probe_aux_dev_err;
1858                 }
1859         }
1860
1861         snd_soc_dapm_link_dai_widgets(card);
1862         snd_soc_dapm_connect_dai_link_widgets(card);
1863
1864         if (card->controls)
1865                 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1866
1867         if (card->dapm_routes)
1868                 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1869                                         card->num_dapm_routes);
1870
1871         for (i = 0; i < card->num_links; i++) {
1872                 dai_link = &card->dai_link[i];
1873                 dai_fmt = dai_link->dai_fmt;
1874
1875                 if (dai_fmt) {
1876                         ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1877                                                   dai_fmt);
1878                         if (ret != 0 && ret != -ENOTSUPP)
1879                                 dev_warn(card->rtd[i].codec_dai->dev,
1880                                          "ASoC: Failed to set DAI format: %d\n",
1881                                          ret);
1882                 }
1883
1884                 /* If this is a regular CPU link there will be a platform */
1885                 if (dai_fmt &&
1886                     (dai_link->platform_name || dai_link->platform_of_node)) {
1887                         ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1888                                                   dai_fmt);
1889                         if (ret != 0 && ret != -ENOTSUPP)
1890                                 dev_warn(card->rtd[i].cpu_dai->dev,
1891                                          "ASoC: Failed to set DAI format: %d\n",
1892                                          ret);
1893                 } else if (dai_fmt) {
1894                         /* Flip the polarity for the "CPU" end */
1895                         dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1896                         switch (dai_link->dai_fmt &
1897                                 SND_SOC_DAIFMT_MASTER_MASK) {
1898                         case SND_SOC_DAIFMT_CBM_CFM:
1899                                 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1900                                 break;
1901                         case SND_SOC_DAIFMT_CBM_CFS:
1902                                 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1903                                 break;
1904                         case SND_SOC_DAIFMT_CBS_CFM:
1905                                 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1906                                 break;
1907                         case SND_SOC_DAIFMT_CBS_CFS:
1908                                 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1909                                 break;
1910                         }
1911
1912                         ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1913                                                   dai_fmt);
1914                         if (ret != 0 && ret != -ENOTSUPP)
1915                                 dev_warn(card->rtd[i].cpu_dai->dev,
1916                                          "ASoC: Failed to set DAI format: %d\n",
1917                                          ret);
1918                 }
1919         }
1920
1921         snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1922                  "%s", card->name);
1923         snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1924                  "%s", card->long_name ? card->long_name : card->name);
1925         snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1926                  "%s", card->driver_name ? card->driver_name : card->name);
1927         for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1928                 switch (card->snd_card->driver[i]) {
1929                 case '_':
1930                 case '-':
1931                 case '\0':
1932                         break;
1933                 default:
1934                         if (!isalnum(card->snd_card->driver[i]))
1935                                 card->snd_card->driver[i] = '_';
1936                         break;
1937                 }
1938         }
1939
1940         if (card->late_probe) {
1941                 ret = card->late_probe(card);
1942                 if (ret < 0) {
1943                         dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1944                                 card->name, ret);
1945                         goto probe_aux_dev_err;
1946                 }
1947         }
1948
1949         if (card->fully_routed)
1950                 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1951                         snd_soc_dapm_auto_nc_codec_pins(codec);
1952
1953         snd_soc_dapm_new_widgets(card);
1954
1955         ret = snd_card_register(card->snd_card);
1956         if (ret < 0) {
1957                 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1958                                 ret);
1959                 goto probe_aux_dev_err;
1960         }
1961
1962 #ifdef CONFIG_SND_SOC_AC97_BUS
1963         /* register any AC97 codecs */
1964         for (i = 0; i < card->num_rtd; i++) {
1965                 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1966                 if (ret < 0) {
1967                         dev_err(card->dev,
1968                                 "ASoC: failed to register AC97: %d\n", ret);
1969                         while (--i >= 0)
1970                                 soc_unregister_ac97_dai_link(&card->rtd[i]);
1971                         goto probe_aux_dev_err;
1972                 }
1973         }
1974 #endif
1975
1976         card->instantiated = 1;
1977         snd_soc_dapm_sync(&card->dapm);
1978         mutex_unlock(&card->mutex);
1979
1980         return 0;
1981
1982 probe_aux_dev_err:
1983         for (i = 0; i < card->num_aux_devs; i++)
1984                 soc_remove_aux_dev(card, i);
1985
1986 probe_dai_err:
1987         soc_remove_dai_links(card);
1988
1989 card_probe_error:
1990         if (card->remove)
1991                 card->remove(card);
1992
1993         snd_card_free(card->snd_card);
1994
1995 base_error:
1996         mutex_unlock(&card->mutex);
1997
1998         return ret;
1999 }
2000
2001 /* probes a new socdev */
2002 static int soc_probe(struct platform_device *pdev)
2003 {
2004         struct snd_soc_card *card = platform_get_drvdata(pdev);
2005
2006         /*
2007          * no card, so machine driver should be registering card
2008          * we should not be here in that case so ret error
2009          */
2010         if (!card)
2011                 return -EINVAL;
2012
2013         dev_warn(&pdev->dev,
2014                  "ASoC: machine %s should use snd_soc_register_card()\n",
2015                  card->name);
2016
2017         /* Bodge while we unpick instantiation */
2018         card->dev = &pdev->dev;
2019
2020         return snd_soc_register_card(card);
2021 }
2022
2023 static int soc_cleanup_card_resources(struct snd_soc_card *card)
2024 {
2025         int i;
2026
2027         /* make sure any delayed work runs */
2028         for (i = 0; i < card->num_rtd; i++) {
2029                 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2030                 flush_delayed_work(&rtd->delayed_work);
2031         }
2032
2033         /* remove auxiliary devices */
2034         for (i = 0; i < card->num_aux_devs; i++)
2035                 soc_remove_aux_dev(card, i);
2036
2037         /* remove and free each DAI */
2038         soc_remove_dai_links(card);
2039
2040         soc_cleanup_card_debugfs(card);
2041
2042         /* remove the card */
2043         if (card->remove)
2044                 card->remove(card);
2045
2046         snd_soc_dapm_free(&card->dapm);
2047
2048         snd_card_free(card->snd_card);
2049         return 0;
2050
2051 }
2052
2053 /* removes a socdev */
2054 static int soc_remove(struct platform_device *pdev)
2055 {
2056         struct snd_soc_card *card = platform_get_drvdata(pdev);
2057
2058         snd_soc_unregister_card(card);
2059         return 0;
2060 }
2061
2062 int snd_soc_poweroff(struct device *dev)
2063 {
2064         struct snd_soc_card *card = dev_get_drvdata(dev);
2065         int i;
2066
2067         if (!card->instantiated)
2068                 return 0;
2069
2070         /* Flush out pmdown_time work - we actually do want to run it
2071          * now, we're shutting down so no imminent restart. */
2072         for (i = 0; i < card->num_rtd; i++) {
2073                 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2074                 flush_delayed_work(&rtd->delayed_work);
2075         }
2076
2077         snd_soc_dapm_shutdown(card);
2078
2079         /* deactivate pins to sleep state */
2080         for (i = 0; i < card->num_rtd; i++) {
2081                 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
2082                 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
2083                 pinctrl_pm_select_sleep_state(codec_dai->dev);
2084                 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2085         }
2086
2087         return 0;
2088 }
2089 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2090
2091 const struct dev_pm_ops snd_soc_pm_ops = {
2092         .suspend = snd_soc_suspend,
2093         .resume = snd_soc_resume,
2094         .freeze = snd_soc_suspend,
2095         .thaw = snd_soc_resume,
2096         .poweroff = snd_soc_poweroff,
2097         .restore = snd_soc_resume,
2098 };
2099 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2100
2101 /* ASoC platform driver */
2102 static struct platform_driver soc_driver = {
2103         .driver         = {
2104                 .name           = "soc-audio",
2105                 .owner          = THIS_MODULE,
2106                 .pm             = &snd_soc_pm_ops,
2107         },
2108         .probe          = soc_probe,
2109         .remove         = soc_remove,
2110 };
2111
2112 /**
2113  * snd_soc_new_ac97_codec - initailise AC97 device
2114  * @codec: audio codec
2115  * @ops: AC97 bus operations
2116  * @num: AC97 codec number
2117  *
2118  * Initialises AC97 codec resources for use by ad-hoc devices only.
2119  */
2120 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2121         struct snd_ac97_bus_ops *ops, int num)
2122 {
2123         mutex_lock(&codec->mutex);
2124
2125         codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2126         if (codec->ac97 == NULL) {
2127                 mutex_unlock(&codec->mutex);
2128                 return -ENOMEM;
2129         }
2130
2131         codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2132         if (codec->ac97->bus == NULL) {
2133                 kfree(codec->ac97);
2134                 codec->ac97 = NULL;
2135                 mutex_unlock(&codec->mutex);
2136                 return -ENOMEM;
2137         }
2138
2139         codec->ac97->bus->ops = ops;
2140         codec->ac97->num = num;
2141
2142         /*
2143          * Mark the AC97 device to be created by us. This way we ensure that the
2144          * device will be registered with the device subsystem later on.
2145          */
2146         codec->ac97_created = 1;
2147
2148         mutex_unlock(&codec->mutex);
2149         return 0;
2150 }
2151 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2152
2153 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2154
2155 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2156 {
2157         struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2158
2159         pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2160
2161         gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2162
2163         udelay(10);
2164
2165         gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2166
2167         pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2168         msleep(2);
2169 }
2170
2171 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2172 {
2173         struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2174
2175         pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2176
2177         gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2178         gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2179         gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2180
2181         udelay(10);
2182
2183         gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2184
2185         pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2186         msleep(2);
2187 }
2188
2189 static int snd_soc_ac97_parse_pinctl(struct device *dev,
2190                 struct snd_ac97_reset_cfg *cfg)
2191 {
2192         struct pinctrl *p;
2193         struct pinctrl_state *state;
2194         int gpio;
2195         int ret;
2196
2197         p = devm_pinctrl_get(dev);
2198         if (IS_ERR(p)) {
2199                 dev_err(dev, "Failed to get pinctrl\n");
2200                 return PTR_ERR(p);
2201         }
2202         cfg->pctl = p;
2203
2204         state = pinctrl_lookup_state(p, "ac97-reset");
2205         if (IS_ERR(state)) {
2206                 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2207                 return PTR_ERR(state);
2208         }
2209         cfg->pstate_reset = state;
2210
2211         state = pinctrl_lookup_state(p, "ac97-warm-reset");
2212         if (IS_ERR(state)) {
2213                 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2214                 return PTR_ERR(state);
2215         }
2216         cfg->pstate_warm_reset = state;
2217
2218         state = pinctrl_lookup_state(p, "ac97-running");
2219         if (IS_ERR(state)) {
2220                 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2221                 return PTR_ERR(state);
2222         }
2223         cfg->pstate_run = state;
2224
2225         gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2226         if (gpio < 0) {
2227                 dev_err(dev, "Can't find ac97-sync gpio\n");
2228                 return gpio;
2229         }
2230         ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2231         if (ret) {
2232                 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2233                 return ret;
2234         }
2235         cfg->gpio_sync = gpio;
2236
2237         gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2238         if (gpio < 0) {
2239                 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2240                 return gpio;
2241         }
2242         ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2243         if (ret) {
2244                 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2245                 return ret;
2246         }
2247         cfg->gpio_sdata = gpio;
2248
2249         gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2250         if (gpio < 0) {
2251                 dev_err(dev, "Can't find ac97-reset gpio\n");
2252                 return gpio;
2253         }
2254         ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2255         if (ret) {
2256                 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2257                 return ret;
2258         }
2259         cfg->gpio_reset = gpio;
2260
2261         return 0;
2262 }
2263
2264 struct snd_ac97_bus_ops *soc_ac97_ops;
2265 EXPORT_SYMBOL_GPL(soc_ac97_ops);
2266
2267 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2268 {
2269         if (ops == soc_ac97_ops)
2270                 return 0;
2271
2272         if (soc_ac97_ops && ops)
2273                 return -EBUSY;
2274
2275         soc_ac97_ops = ops;
2276
2277         return 0;
2278 }
2279 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2280
2281 /**
2282  * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2283  *
2284  * This function sets the reset and warm_reset properties of ops and parses
2285  * the device node of pdev to get pinctrl states and gpio numbers to use.
2286  */
2287 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2288                 struct platform_device *pdev)
2289 {
2290         struct device *dev = &pdev->dev;
2291         struct snd_ac97_reset_cfg cfg;
2292         int ret;
2293
2294         ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2295         if (ret)
2296                 return ret;
2297
2298         ret = snd_soc_set_ac97_ops(ops);
2299         if (ret)
2300                 return ret;
2301
2302         ops->warm_reset = snd_soc_ac97_warm_reset;
2303         ops->reset = snd_soc_ac97_reset;
2304
2305         snd_ac97_rst_cfg = cfg;
2306         return 0;
2307 }
2308 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2309
2310 /**
2311  * snd_soc_free_ac97_codec - free AC97 codec device
2312  * @codec: audio codec
2313  *
2314  * Frees AC97 codec device resources.
2315  */
2316 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2317 {
2318         mutex_lock(&codec->mutex);
2319 #ifdef CONFIG_SND_SOC_AC97_BUS
2320         soc_unregister_ac97_codec(codec);
2321 #endif
2322         kfree(codec->ac97->bus);
2323         kfree(codec->ac97);
2324         codec->ac97 = NULL;
2325         codec->ac97_created = 0;
2326         mutex_unlock(&codec->mutex);
2327 }
2328 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2329
2330 /**
2331  * snd_soc_cnew - create new control
2332  * @_template: control template
2333  * @data: control private data
2334  * @long_name: control long name
2335  * @prefix: control name prefix
2336  *
2337  * Create a new mixer control from a template control.
2338  *
2339  * Returns 0 for success, else error.
2340  */
2341 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2342                                   void *data, const char *long_name,
2343                                   const char *prefix)
2344 {
2345         struct snd_kcontrol_new template;
2346         struct snd_kcontrol *kcontrol;
2347         char *name = NULL;
2348
2349         memcpy(&template, _template, sizeof(template));
2350         template.index = 0;
2351
2352         if (!long_name)
2353                 long_name = template.name;
2354
2355         if (prefix) {
2356                 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2357                 if (!name)
2358                         return NULL;
2359
2360                 template.name = name;
2361         } else {
2362                 template.name = long_name;
2363         }
2364
2365         kcontrol = snd_ctl_new1(&template, data);
2366
2367         kfree(name);
2368
2369         return kcontrol;
2370 }
2371 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2372
2373 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2374         const struct snd_kcontrol_new *controls, int num_controls,
2375         const char *prefix, void *data)
2376 {
2377         int err, i;
2378
2379         for (i = 0; i < num_controls; i++) {
2380                 const struct snd_kcontrol_new *control = &controls[i];
2381                 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2382                                                      control->name, prefix));
2383                 if (err < 0) {
2384                         dev_err(dev, "ASoC: Failed to add %s: %d\n",
2385                                 control->name, err);
2386                         return err;
2387                 }
2388         }
2389
2390         return 0;
2391 }
2392
2393 struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2394                                                const char *name)
2395 {
2396         struct snd_card *card = soc_card->snd_card;
2397         struct snd_kcontrol *kctl;
2398
2399         if (unlikely(!name))
2400                 return NULL;
2401
2402         list_for_each_entry(kctl, &card->controls, list)
2403                 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2404                         return kctl;
2405         return NULL;
2406 }
2407 EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2408
2409 /**
2410  * snd_soc_add_codec_controls - add an array of controls to a codec.
2411  * Convenience function to add a list of controls. Many codecs were
2412  * duplicating this code.
2413  *
2414  * @codec: codec to add controls to
2415  * @controls: array of controls to add
2416  * @num_controls: number of elements in the array
2417  *
2418  * Return 0 for success, else error.
2419  */
2420 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2421         const struct snd_kcontrol_new *controls, int num_controls)
2422 {
2423         struct snd_card *card = codec->card->snd_card;
2424
2425         return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2426                         codec->name_prefix, &codec->component);
2427 }
2428 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2429
2430 /**
2431  * snd_soc_add_platform_controls - add an array of controls to a platform.
2432  * Convenience function to add a list of controls.
2433  *
2434  * @platform: platform to add controls to
2435  * @controls: array of controls to add
2436  * @num_controls: number of elements in the array
2437  *
2438  * Return 0 for success, else error.
2439  */
2440 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2441         const struct snd_kcontrol_new *controls, int num_controls)
2442 {
2443         struct snd_card *card = platform->card->snd_card;
2444
2445         return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2446                         NULL, &platform->component);
2447 }
2448 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2449
2450 /**
2451  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2452  * Convenience function to add a list of controls.
2453  *
2454  * @soc_card: SoC card to add controls to
2455  * @controls: array of controls to add
2456  * @num_controls: number of elements in the array
2457  *
2458  * Return 0 for success, else error.
2459  */
2460 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2461         const struct snd_kcontrol_new *controls, int num_controls)
2462 {
2463         struct snd_card *card = soc_card->snd_card;
2464
2465         return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2466                         NULL, soc_card);
2467 }
2468 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2469
2470 /**
2471  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2472  * Convienience function to add a list of controls.
2473  *
2474  * @dai: DAI to add controls to
2475  * @controls: array of controls to add
2476  * @num_controls: number of elements in the array
2477  *
2478  * Return 0 for success, else error.
2479  */
2480 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2481         const struct snd_kcontrol_new *controls, int num_controls)
2482 {
2483         struct snd_card *card = dai->card->snd_card;
2484
2485         return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2486                         NULL, dai);
2487 }
2488 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2489
2490 /**
2491  * snd_soc_info_enum_double - enumerated double mixer info callback
2492  * @kcontrol: mixer control
2493  * @uinfo: control element information
2494  *
2495  * Callback to provide information about a double enumerated
2496  * mixer control.
2497  *
2498  * Returns 0 for success.
2499  */
2500 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2501         struct snd_ctl_elem_info *uinfo)
2502 {
2503         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2504
2505         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2506         uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2507         uinfo->value.enumerated.items = e->items;
2508
2509         if (uinfo->value.enumerated.item >= e->items)
2510                 uinfo->value.enumerated.item = e->items - 1;
2511         strlcpy(uinfo->value.enumerated.name,
2512                 e->texts[uinfo->value.enumerated.item],
2513                 sizeof(uinfo->value.enumerated.name));
2514         return 0;
2515 }
2516 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2517
2518 /**
2519  * snd_soc_get_enum_double - enumerated double mixer get callback
2520  * @kcontrol: mixer control
2521  * @ucontrol: control element information
2522  *
2523  * Callback to get the value of a double enumerated mixer.
2524  *
2525  * Returns 0 for success.
2526  */
2527 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2528         struct snd_ctl_elem_value *ucontrol)
2529 {
2530         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2531         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2532         unsigned int val, item;
2533         unsigned int reg_val;
2534         int ret;
2535
2536         ret = snd_soc_component_read(component, e->reg, &reg_val);
2537         if (ret)
2538                 return ret;
2539         val = (reg_val >> e->shift_l) & e->mask;
2540         item = snd_soc_enum_val_to_item(e, val);
2541         ucontrol->value.enumerated.item[0] = item;
2542         if (e->shift_l != e->shift_r) {
2543                 val = (reg_val >> e->shift_l) & e->mask;
2544                 item = snd_soc_enum_val_to_item(e, val);
2545                 ucontrol->value.enumerated.item[1] = item;
2546         }
2547
2548         return 0;
2549 }
2550 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2551
2552 /**
2553  * snd_soc_put_enum_double - enumerated double mixer put callback
2554  * @kcontrol: mixer control
2555  * @ucontrol: control element information
2556  *
2557  * Callback to set the value of a double enumerated mixer.
2558  *
2559  * Returns 0 for success.
2560  */
2561 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2562         struct snd_ctl_elem_value *ucontrol)
2563 {
2564         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2565         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2566         unsigned int *item = ucontrol->value.enumerated.item;
2567         unsigned int val;
2568         unsigned int mask;
2569
2570         if (item[0] >= e->items)
2571                 return -EINVAL;
2572         val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2573         mask = e->mask << e->shift_l;
2574         if (e->shift_l != e->shift_r) {
2575                 if (item[1] >= e->items)
2576                         return -EINVAL;
2577                 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
2578                 mask |= e->mask << e->shift_r;
2579         }
2580
2581         return snd_soc_component_update_bits(component, e->reg, mask, val);
2582 }
2583 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2584
2585 /**
2586  * snd_soc_read_signed - Read a codec register and interprete as signed value
2587  * @component: component
2588  * @reg: Register to read
2589  * @mask: Mask to use after shifting the register value
2590  * @shift: Right shift of register value
2591  * @sign_bit: Bit that describes if a number is negative or not.
2592  * @signed_val: Pointer to where the read value should be stored
2593  *
2594  * This functions reads a codec register. The register value is shifted right
2595  * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2596  * the given registervalue into a signed integer if sign_bit is non-zero.
2597  *
2598  * Returns 0 on sucess, otherwise an error value
2599  */
2600 static int snd_soc_read_signed(struct snd_soc_component *component,
2601         unsigned int reg, unsigned int mask, unsigned int shift,
2602         unsigned int sign_bit, int *signed_val)
2603 {
2604         int ret;
2605         unsigned int val;
2606
2607         ret = snd_soc_component_read(component, reg, &val);
2608         if (ret < 0)
2609                 return ret;
2610
2611         val = (val >> shift) & mask;
2612
2613         if (!sign_bit) {
2614                 *signed_val = val;
2615                 return 0;
2616         }
2617
2618         /* non-negative number */
2619         if (!(val & BIT(sign_bit))) {
2620                 *signed_val = val;
2621                 return 0;
2622         }
2623
2624         ret = val;
2625
2626         /*
2627          * The register most probably does not contain a full-sized int.
2628          * Instead we have an arbitrary number of bits in a signed
2629          * representation which has to be translated into a full-sized int.
2630          * This is done by filling up all bits above the sign-bit.
2631          */
2632         ret |= ~((int)(BIT(sign_bit) - 1));
2633
2634         *signed_val = ret;
2635
2636         return 0;
2637 }
2638
2639 /**
2640  * snd_soc_info_volsw - single mixer info callback
2641  * @kcontrol: mixer control
2642  * @uinfo: control element information
2643  *
2644  * Callback to provide information about a single mixer control, or a double
2645  * mixer control that spans 2 registers.
2646  *
2647  * Returns 0 for success.
2648  */
2649 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2650         struct snd_ctl_elem_info *uinfo)
2651 {
2652         struct soc_mixer_control *mc =
2653                 (struct soc_mixer_control *)kcontrol->private_value;
2654         int platform_max;
2655
2656         if (!mc->platform_max)
2657                 mc->platform_max = mc->max;
2658         platform_max = mc->platform_max;
2659
2660         if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2661                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2662         else
2663                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2664
2665         uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2666         uinfo->value.integer.min = 0;
2667         uinfo->value.integer.max = platform_max - mc->min;
2668         return 0;
2669 }
2670 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2671
2672 /**
2673  * snd_soc_get_volsw - single mixer get callback
2674  * @kcontrol: mixer control
2675  * @ucontrol: control element information
2676  *
2677  * Callback to get the value of a single mixer control, or a double mixer
2678  * control that spans 2 registers.
2679  *
2680  * Returns 0 for success.
2681  */
2682 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2683         struct snd_ctl_elem_value *ucontrol)
2684 {
2685         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2686         struct soc_mixer_control *mc =
2687                 (struct soc_mixer_control *)kcontrol->private_value;
2688         unsigned int reg = mc->reg;
2689         unsigned int reg2 = mc->rreg;
2690         unsigned int shift = mc->shift;
2691         unsigned int rshift = mc->rshift;
2692         int max = mc->max;
2693         int min = mc->min;
2694         int sign_bit = mc->sign_bit;
2695         unsigned int mask = (1 << fls(max)) - 1;
2696         unsigned int invert = mc->invert;
2697         int val;
2698         int ret;
2699
2700         if (sign_bit)
2701                 mask = BIT(sign_bit + 1) - 1;
2702
2703         ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2704         if (ret)
2705                 return ret;
2706
2707         ucontrol->value.integer.value[0] = val - min;
2708         if (invert)
2709                 ucontrol->value.integer.value[0] =
2710                         max - ucontrol->value.integer.value[0];
2711
2712         if (snd_soc_volsw_is_stereo(mc)) {
2713                 if (reg == reg2)
2714                         ret = snd_soc_read_signed(component, reg, mask, rshift,
2715                                 sign_bit, &val);
2716                 else
2717                         ret = snd_soc_read_signed(component, reg2, mask, shift,
2718                                 sign_bit, &val);
2719                 if (ret)
2720                         return ret;
2721
2722                 ucontrol->value.integer.value[1] = val - min;
2723                 if (invert)
2724                         ucontrol->value.integer.value[1] =
2725                                 max - ucontrol->value.integer.value[1];
2726         }
2727
2728         return 0;
2729 }
2730 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2731
2732 /**
2733  * snd_soc_put_volsw - single mixer put callback
2734  * @kcontrol: mixer control
2735  * @ucontrol: control element information
2736  *
2737  * Callback to set the value of a single mixer control, or a double mixer
2738  * control that spans 2 registers.
2739  *
2740  * Returns 0 for success.
2741  */
2742 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2743         struct snd_ctl_elem_value *ucontrol)
2744 {
2745         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2746         struct soc_mixer_control *mc =
2747                 (struct soc_mixer_control *)kcontrol->private_value;
2748         unsigned int reg = mc->reg;
2749         unsigned int reg2 = mc->rreg;
2750         unsigned int shift = mc->shift;
2751         unsigned int rshift = mc->rshift;
2752         int max = mc->max;
2753         int min = mc->min;
2754         unsigned int sign_bit = mc->sign_bit;
2755         unsigned int mask = (1 << fls(max)) - 1;
2756         unsigned int invert = mc->invert;
2757         int err;
2758         bool type_2r = false;
2759         unsigned int val2 = 0;
2760         unsigned int val, val_mask;
2761
2762         if (sign_bit)
2763                 mask = BIT(sign_bit + 1) - 1;
2764
2765         val = ((ucontrol->value.integer.value[0] + min) & mask);
2766         if (invert)
2767                 val = max - val;
2768         val_mask = mask << shift;
2769         val = val << shift;
2770         if (snd_soc_volsw_is_stereo(mc)) {
2771                 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
2772                 if (invert)
2773                         val2 = max - val2;
2774                 if (reg == reg2) {
2775                         val_mask |= mask << rshift;
2776                         val |= val2 << rshift;
2777                 } else {
2778                         val2 = val2 << shift;
2779                         type_2r = true;
2780                 }
2781         }
2782         err = snd_soc_component_update_bits(component, reg, val_mask, val);
2783         if (err < 0)
2784                 return err;
2785
2786         if (type_2r)
2787                 err = snd_soc_component_update_bits(component, reg2, val_mask,
2788                         val2);
2789
2790         return err;
2791 }
2792 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2793
2794 /**
2795  * snd_soc_get_volsw_sx - single mixer get callback
2796  * @kcontrol: mixer control
2797  * @ucontrol: control element information
2798  *
2799  * Callback to get the value of a single mixer control, or a double mixer
2800  * control that spans 2 registers.
2801  *
2802  * Returns 0 for success.
2803  */
2804 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2805                       struct snd_ctl_elem_value *ucontrol)
2806 {
2807         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2808         struct soc_mixer_control *mc =
2809             (struct soc_mixer_control *)kcontrol->private_value;
2810         unsigned int reg = mc->reg;
2811         unsigned int reg2 = mc->rreg;
2812         unsigned int shift = mc->shift;
2813         unsigned int rshift = mc->rshift;
2814         int max = mc->max;
2815         int min = mc->min;
2816         int mask = (1 << (fls(min + max) - 1)) - 1;
2817         unsigned int val;
2818         int ret;
2819
2820         ret = snd_soc_component_read(component, reg, &val);
2821         if (ret < 0)
2822                 return ret;
2823
2824         ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2825
2826         if (snd_soc_volsw_is_stereo(mc)) {
2827                 ret = snd_soc_component_read(component, reg2, &val);
2828                 if (ret < 0)
2829                         return ret;
2830
2831                 val = ((val >> rshift) - min) & mask;
2832                 ucontrol->value.integer.value[1] = val;
2833         }
2834
2835         return 0;
2836 }
2837 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2838
2839 /**
2840  * snd_soc_put_volsw_sx - double mixer set callback
2841  * @kcontrol: mixer control
2842  * @uinfo: control element information
2843  *
2844  * Callback to set the value of a double mixer control that spans 2 registers.
2845  *
2846  * Returns 0 for success.
2847  */
2848 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2849                          struct snd_ctl_elem_value *ucontrol)
2850 {
2851         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2852         struct soc_mixer_control *mc =
2853             (struct soc_mixer_control *)kcontrol->private_value;
2854
2855         unsigned int reg = mc->reg;
2856         unsigned int reg2 = mc->rreg;
2857         unsigned int shift = mc->shift;
2858         unsigned int rshift = mc->rshift;
2859         int max = mc->max;
2860         int min = mc->min;
2861         int mask = (1 << (fls(min + max) - 1)) - 1;
2862         int err = 0;
2863         unsigned int val, val_mask, val2 = 0;
2864
2865         val_mask = mask << shift;
2866         val = (ucontrol->value.integer.value[0] + min) & mask;
2867         val = val << shift;
2868
2869         err = snd_soc_component_update_bits(component, reg, val_mask, val);
2870         if (err < 0)
2871                 return err;
2872
2873         if (snd_soc_volsw_is_stereo(mc)) {
2874                 val_mask = mask << rshift;
2875                 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2876                 val2 = val2 << rshift;
2877
2878                 err = snd_soc_component_update_bits(component, reg2, val_mask,
2879                         val2);
2880         }
2881         return err;
2882 }
2883 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2884
2885 /**
2886  * snd_soc_info_volsw_s8 - signed mixer info callback
2887  * @kcontrol: mixer control
2888  * @uinfo: control element information
2889  *
2890  * Callback to provide information about a signed mixer control.
2891  *
2892  * Returns 0 for success.
2893  */
2894 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2895         struct snd_ctl_elem_info *uinfo)
2896 {
2897         struct soc_mixer_control *mc =
2898                 (struct soc_mixer_control *)kcontrol->private_value;
2899         int platform_max;
2900         int min = mc->min;
2901
2902         if (!mc->platform_max)
2903                 mc->platform_max = mc->max;
2904         platform_max = mc->platform_max;
2905
2906         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2907         uinfo->count = 2;
2908         uinfo->value.integer.min = 0;
2909         uinfo->value.integer.max = platform_max - min;
2910         return 0;
2911 }
2912 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2913
2914 /**
2915  * snd_soc_get_volsw_s8 - signed mixer get callback
2916  * @kcontrol: mixer control
2917  * @ucontrol: control element information
2918  *
2919  * Callback to get the value of a signed mixer control.
2920  *
2921  * Returns 0 for success.
2922  */
2923 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2924         struct snd_ctl_elem_value *ucontrol)
2925 {
2926         struct soc_mixer_control *mc =
2927                 (struct soc_mixer_control *)kcontrol->private_value;
2928         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2929         unsigned int reg = mc->reg;
2930         unsigned int val;
2931         int min = mc->min;
2932         int ret;
2933
2934         ret = snd_soc_component_read(component, reg, &val);
2935         if (ret)
2936                 return ret;
2937
2938         ucontrol->value.integer.value[0] =
2939                 ((signed char)(val & 0xff))-min;
2940         ucontrol->value.integer.value[1] =
2941                 ((signed char)((val >> 8) & 0xff))-min;
2942         return 0;
2943 }
2944 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2945
2946 /**
2947  * snd_soc_put_volsw_sgn - signed mixer put callback
2948  * @kcontrol: mixer control
2949  * @ucontrol: control element information
2950  *
2951  * Callback to set the value of a signed mixer control.
2952  *
2953  * Returns 0 for success.
2954  */
2955 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2956         struct snd_ctl_elem_value *ucontrol)
2957 {
2958         struct soc_mixer_control *mc =
2959                 (struct soc_mixer_control *)kcontrol->private_value;
2960         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
2961         unsigned int reg = mc->reg;
2962         int min = mc->min;
2963         unsigned int val;
2964
2965         val = (ucontrol->value.integer.value[0]+min) & 0xff;
2966         val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2967
2968         return snd_soc_component_update_bits(component, reg, 0xffff, val);
2969 }
2970 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2971
2972 /**
2973  * snd_soc_info_volsw_range - single mixer info callback with range.
2974  * @kcontrol: mixer control
2975  * @uinfo: control element information
2976  *
2977  * Callback to provide information, within a range, about a single
2978  * mixer control.
2979  *
2980  * returns 0 for success.
2981  */
2982 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2983         struct snd_ctl_elem_info *uinfo)
2984 {
2985         struct soc_mixer_control *mc =
2986                 (struct soc_mixer_control *)kcontrol->private_value;
2987         int platform_max;
2988         int min = mc->min;
2989
2990         if (!mc->platform_max)
2991                 mc->platform_max = mc->max;
2992         platform_max = mc->platform_max;
2993
2994         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2995         uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2996         uinfo->value.integer.min = 0;
2997         uinfo->value.integer.max = platform_max - min;
2998
2999         return 0;
3000 }
3001 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
3002
3003 /**
3004  * snd_soc_put_volsw_range - single mixer put value callback with range.
3005  * @kcontrol: mixer control
3006  * @ucontrol: control element information
3007  *
3008  * Callback to set the value, within a range, for a single mixer control.
3009  *
3010  * Returns 0 for success.
3011  */
3012 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
3013         struct snd_ctl_elem_value *ucontrol)
3014 {
3015         struct soc_mixer_control *mc =
3016                 (struct soc_mixer_control *)kcontrol->private_value;
3017         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3018         unsigned int reg = mc->reg;
3019         unsigned int rreg = mc->rreg;
3020         unsigned int shift = mc->shift;
3021         int min = mc->min;
3022         int max = mc->max;
3023         unsigned int mask = (1 << fls(max)) - 1;
3024         unsigned int invert = mc->invert;
3025         unsigned int val, val_mask;
3026         int ret;
3027
3028         val = ((ucontrol->value.integer.value[0] + min) & mask);
3029         if (invert)
3030                 val = max - val;
3031         val_mask = mask << shift;
3032         val = val << shift;
3033
3034         ret = snd_soc_component_update_bits(component, reg, val_mask, val);
3035         if (ret < 0)
3036                 return ret;
3037
3038         if (snd_soc_volsw_is_stereo(mc)) {
3039                 val = ((ucontrol->value.integer.value[1] + min) & mask);
3040                 if (invert)
3041                         val = max - val;
3042                 val_mask = mask << shift;
3043                 val = val << shift;
3044
3045                 ret = snd_soc_component_update_bits(component, rreg, val_mask,
3046                         val);
3047         }
3048
3049         return ret;
3050 }
3051 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3052
3053 /**
3054  * snd_soc_get_volsw_range - single mixer get callback with range
3055  * @kcontrol: mixer control
3056  * @ucontrol: control element information
3057  *
3058  * Callback to get the value, within a range, of a single mixer control.
3059  *
3060  * Returns 0 for success.
3061  */
3062 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3063         struct snd_ctl_elem_value *ucontrol)
3064 {
3065         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3066         struct soc_mixer_control *mc =
3067                 (struct soc_mixer_control *)kcontrol->private_value;
3068         unsigned int reg = mc->reg;
3069         unsigned int rreg = mc->rreg;
3070         unsigned int shift = mc->shift;
3071         int min = mc->min;
3072         int max = mc->max;
3073         unsigned int mask = (1 << fls(max)) - 1;
3074         unsigned int invert = mc->invert;
3075         unsigned int val;
3076         int ret;
3077
3078         ret = snd_soc_component_read(component, reg, &val);
3079         if (ret)
3080                 return ret;
3081
3082         ucontrol->value.integer.value[0] = (val >> shift) & mask;
3083         if (invert)
3084                 ucontrol->value.integer.value[0] =
3085                         max - ucontrol->value.integer.value[0];
3086         ucontrol->value.integer.value[0] =
3087                 ucontrol->value.integer.value[0] - min;
3088
3089         if (snd_soc_volsw_is_stereo(mc)) {
3090                 ret = snd_soc_component_read(component, rreg, &val);
3091                 if (ret)
3092                         return ret;
3093
3094                 ucontrol->value.integer.value[1] = (val >> shift) & mask;
3095                 if (invert)
3096                         ucontrol->value.integer.value[1] =
3097                                 max - ucontrol->value.integer.value[1];
3098                 ucontrol->value.integer.value[1] =
3099                         ucontrol->value.integer.value[1] - min;
3100         }
3101
3102         return 0;
3103 }
3104 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3105
3106 /**
3107  * snd_soc_limit_volume - Set new limit to an existing volume control.
3108  *
3109  * @codec: where to look for the control
3110  * @name: Name of the control
3111  * @max: new maximum limit
3112  *
3113  * Return 0 for success, else error.
3114  */
3115 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3116         const char *name, int max)
3117 {
3118         struct snd_card *card = codec->card->snd_card;
3119         struct snd_kcontrol *kctl;
3120         struct soc_mixer_control *mc;
3121         int found = 0;
3122         int ret = -EINVAL;
3123
3124         /* Sanity check for name and max */
3125         if (unlikely(!name || max <= 0))
3126                 return -EINVAL;
3127
3128         list_for_each_entry(kctl, &card->controls, list) {
3129                 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3130                         found = 1;
3131                         break;
3132                 }
3133         }
3134         if (found) {
3135                 mc = (struct soc_mixer_control *)kctl->private_value;
3136                 if (max <= mc->max) {
3137                         mc->platform_max = max;
3138                         ret = 0;
3139                 }
3140         }
3141         return ret;
3142 }
3143 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3144
3145 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3146                        struct snd_ctl_elem_info *uinfo)
3147 {
3148         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3149         struct soc_bytes *params = (void *)kcontrol->private_value;
3150
3151         uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3152         uinfo->count = params->num_regs * component->val_bytes;
3153
3154         return 0;
3155 }
3156 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3157
3158 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3159                       struct snd_ctl_elem_value *ucontrol)
3160 {
3161         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3162         struct soc_bytes *params = (void *)kcontrol->private_value;
3163         int ret;
3164
3165         if (component->regmap)
3166                 ret = regmap_raw_read(component->regmap, params->base,
3167                                       ucontrol->value.bytes.data,
3168                                       params->num_regs * component->val_bytes);
3169         else
3170                 ret = -EINVAL;
3171
3172         /* Hide any masked bytes to ensure consistent data reporting */
3173         if (ret == 0 && params->mask) {
3174                 switch (component->val_bytes) {
3175                 case 1:
3176                         ucontrol->value.bytes.data[0] &= ~params->mask;
3177                         break;
3178                 case 2:
3179                         ((u16 *)(&ucontrol->value.bytes.data))[0]
3180                                 &= cpu_to_be16(~params->mask);
3181                         break;
3182                 case 4:
3183                         ((u32 *)(&ucontrol->value.bytes.data))[0]
3184                                 &= cpu_to_be32(~params->mask);
3185                         break;
3186                 default:
3187                         return -EINVAL;
3188                 }
3189         }
3190
3191         return ret;
3192 }
3193 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3194
3195 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3196                       struct snd_ctl_elem_value *ucontrol)
3197 {
3198         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3199         struct soc_bytes *params = (void *)kcontrol->private_value;
3200         int ret, len;
3201         unsigned int val, mask;
3202         void *data;
3203
3204         if (!component->regmap)
3205                 return -EINVAL;
3206
3207         len = params->num_regs * component->val_bytes;
3208
3209         data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3210         if (!data)
3211                 return -ENOMEM;
3212
3213         /*
3214          * If we've got a mask then we need to preserve the register
3215          * bits.  We shouldn't modify the incoming data so take a
3216          * copy.
3217          */
3218         if (params->mask) {
3219                 ret = regmap_read(component->regmap, params->base, &val);
3220                 if (ret != 0)
3221                         goto out;
3222
3223                 val &= params->mask;
3224
3225                 switch (component->val_bytes) {
3226                 case 1:
3227                         ((u8 *)data)[0] &= ~params->mask;
3228                         ((u8 *)data)[0] |= val;
3229                         break;
3230                 case 2:
3231                         mask = ~params->mask;
3232                         ret = regmap_parse_val(component->regmap,
3233                                                         &mask, &mask);
3234                         if (ret != 0)
3235                                 goto out;
3236
3237                         ((u16 *)data)[0] &= mask;
3238
3239                         ret = regmap_parse_val(component->regmap,
3240                                                         &val, &val);
3241                         if (ret != 0)
3242                                 goto out;
3243
3244                         ((u16 *)data)[0] |= val;
3245                         break;
3246                 case 4:
3247                         mask = ~params->mask;
3248                         ret = regmap_parse_val(component->regmap,
3249                                                         &mask, &mask);
3250                         if (ret != 0)
3251                                 goto out;
3252
3253                         ((u32 *)data)[0] &= mask;
3254
3255                         ret = regmap_parse_val(component->regmap,
3256                                                         &val, &val);
3257                         if (ret != 0)
3258                                 goto out;
3259
3260                         ((u32 *)data)[0] |= val;
3261                         break;
3262                 default:
3263                         ret = -EINVAL;
3264                         goto out;
3265                 }
3266         }
3267
3268         ret = regmap_raw_write(component->regmap, params->base,
3269                                data, len);
3270
3271 out:
3272         kfree(data);
3273
3274         return ret;
3275 }
3276 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3277
3278 int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3279                         struct snd_ctl_elem_info *ucontrol)
3280 {
3281         struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3282
3283         ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3284         ucontrol->count = params->max;
3285
3286         return 0;
3287 }
3288 EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3289
3290 /**
3291  * snd_soc_info_xr_sx - signed multi register info callback
3292  * @kcontrol: mreg control
3293  * @uinfo: control element information
3294  *
3295  * Callback to provide information of a control that can
3296  * span multiple codec registers which together
3297  * forms a single signed value in a MSB/LSB manner.
3298  *
3299  * Returns 0 for success.
3300  */
3301 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3302         struct snd_ctl_elem_info *uinfo)
3303 {
3304         struct soc_mreg_control *mc =
3305                 (struct soc_mreg_control *)kcontrol->private_value;
3306         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3307         uinfo->count = 1;
3308         uinfo->value.integer.min = mc->min;
3309         uinfo->value.integer.max = mc->max;
3310
3311         return 0;
3312 }
3313 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3314
3315 /**
3316  * snd_soc_get_xr_sx - signed multi register get callback
3317  * @kcontrol: mreg control
3318  * @ucontrol: control element information
3319  *
3320  * Callback to get the value of a control that can span
3321  * multiple codec registers which together forms a single
3322  * signed value in a MSB/LSB manner. The control supports
3323  * specifying total no of bits used to allow for bitfields
3324  * across the multiple codec registers.
3325  *
3326  * Returns 0 for success.
3327  */
3328 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3329         struct snd_ctl_elem_value *ucontrol)
3330 {
3331         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3332         struct soc_mreg_control *mc =
3333                 (struct soc_mreg_control *)kcontrol->private_value;
3334         unsigned int regbase = mc->regbase;
3335         unsigned int regcount = mc->regcount;
3336         unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3337         unsigned int regwmask = (1<<regwshift)-1;
3338         unsigned int invert = mc->invert;
3339         unsigned long mask = (1UL<<mc->nbits)-1;
3340         long min = mc->min;
3341         long max = mc->max;
3342         long val = 0;
3343         unsigned int regval;
3344         unsigned int i;
3345         int ret;
3346
3347         for (i = 0; i < regcount; i++) {
3348                 ret = snd_soc_component_read(component, regbase+i, &regval);
3349                 if (ret)
3350                         return ret;
3351                 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
3352         }
3353         val &= mask;
3354         if (min < 0 && val > max)
3355                 val |= ~mask;
3356         if (invert)
3357                 val = max - val;
3358         ucontrol->value.integer.value[0] = val;
3359
3360         return 0;
3361 }
3362 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3363
3364 /**
3365  * snd_soc_put_xr_sx - signed multi register get callback
3366  * @kcontrol: mreg control
3367  * @ucontrol: control element information
3368  *
3369  * Callback to set the value of a control that can span
3370  * multiple codec registers which together forms a single
3371  * signed value in a MSB/LSB manner. The control supports
3372  * specifying total no of bits used to allow for bitfields
3373  * across the multiple codec registers.
3374  *
3375  * Returns 0 for success.
3376  */
3377 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3378         struct snd_ctl_elem_value *ucontrol)
3379 {
3380         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3381         struct soc_mreg_control *mc =
3382                 (struct soc_mreg_control *)kcontrol->private_value;
3383         unsigned int regbase = mc->regbase;
3384         unsigned int regcount = mc->regcount;
3385         unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
3386         unsigned int regwmask = (1<<regwshift)-1;
3387         unsigned int invert = mc->invert;
3388         unsigned long mask = (1UL<<mc->nbits)-1;
3389         long max = mc->max;
3390         long val = ucontrol->value.integer.value[0];
3391         unsigned int i, regval, regmask;
3392         int err;
3393
3394         if (invert)
3395                 val = max - val;
3396         val &= mask;
3397         for (i = 0; i < regcount; i++) {
3398                 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3399                 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3400                 err = snd_soc_component_update_bits(component, regbase+i,
3401                                 regmask, regval);
3402                 if (err < 0)
3403                         return err;
3404         }
3405
3406         return 0;
3407 }
3408 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3409
3410 /**
3411  * snd_soc_get_strobe - strobe get callback
3412  * @kcontrol: mixer control
3413  * @ucontrol: control element information
3414  *
3415  * Callback get the value of a strobe mixer control.
3416  *
3417  * Returns 0 for success.
3418  */
3419 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3420         struct snd_ctl_elem_value *ucontrol)
3421 {
3422         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3423         struct soc_mixer_control *mc =
3424                 (struct soc_mixer_control *)kcontrol->private_value;
3425         unsigned int reg = mc->reg;
3426         unsigned int shift = mc->shift;
3427         unsigned int mask = 1 << shift;
3428         unsigned int invert = mc->invert != 0;
3429         unsigned int val;
3430         int ret;
3431
3432         ret = snd_soc_component_read(component, reg, &val);
3433         if (ret)
3434                 return ret;
3435
3436         val &= mask;
3437
3438         if (shift != 0 && val != 0)
3439                 val = val >> shift;
3440         ucontrol->value.enumerated.item[0] = val ^ invert;
3441
3442         return 0;
3443 }
3444 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3445
3446 /**
3447  * snd_soc_put_strobe - strobe put callback
3448  * @kcontrol: mixer control
3449  * @ucontrol: control element information
3450  *
3451  * Callback strobe a register bit to high then low (or the inverse)
3452  * in one pass of a single mixer enum control.
3453  *
3454  * Returns 1 for success.
3455  */
3456 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3457         struct snd_ctl_elem_value *ucontrol)
3458 {
3459         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
3460         struct soc_mixer_control *mc =
3461                 (struct soc_mixer_control *)kcontrol->private_value;
3462         unsigned int reg = mc->reg;
3463         unsigned int shift = mc->shift;
3464         unsigned int mask = 1 << shift;
3465         unsigned int invert = mc->invert != 0;
3466         unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3467         unsigned int val1 = (strobe ^ invert) ? mask : 0;
3468         unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3469         int err;
3470
3471         err = snd_soc_component_update_bits(component, reg, mask, val1);
3472         if (err < 0)
3473                 return err;
3474
3475         return snd_soc_component_update_bits(component, reg, mask, val2);
3476 }
3477 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3478
3479 /**
3480  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3481  * @dai: DAI
3482  * @clk_id: DAI specific clock ID
3483  * @freq: new clock frequency in Hz
3484  * @dir: new clock direction - input/output.
3485  *
3486  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3487  */
3488 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3489         unsigned int freq, int dir)
3490 {
3491         if (dai->driver && dai->driver->ops->set_sysclk)
3492                 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3493         else if (dai->codec && dai->codec->driver->set_sysclk)
3494                 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3495                                                       freq, dir);
3496         else
3497                 return -ENOTSUPP;
3498 }
3499 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3500
3501 /**
3502  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3503  * @codec: CODEC
3504  * @clk_id: DAI specific clock ID
3505  * @source: Source for the clock
3506  * @freq: new clock frequency in Hz
3507  * @dir: new clock direction - input/output.
3508  *
3509  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3510  */
3511 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3512                              int source, unsigned int freq, int dir)
3513 {
3514         if (codec->driver->set_sysclk)
3515                 return codec->driver->set_sysclk(codec, clk_id, source,
3516                                                  freq, dir);
3517         else
3518                 return -ENOTSUPP;
3519 }
3520 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3521
3522 /**
3523  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3524  * @dai: DAI
3525  * @div_id: DAI specific clock divider ID
3526  * @div: new clock divisor.
3527  *
3528  * Configures the clock dividers. This is used to derive the best DAI bit and
3529  * frame clocks from the system or master clock. It's best to set the DAI bit
3530  * and frame clocks as low as possible to save system power.
3531  */
3532 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3533         int div_id, int div)
3534 {
3535         if (dai->driver && dai->driver->ops->set_clkdiv)
3536                 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3537         else
3538                 return -EINVAL;
3539 }
3540 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3541
3542 /**
3543  * snd_soc_dai_set_pll - configure DAI PLL.
3544  * @dai: DAI
3545  * @pll_id: DAI specific PLL ID
3546  * @source: DAI specific source for the PLL
3547  * @freq_in: PLL input clock frequency in Hz
3548  * @freq_out: requested PLL output clock frequency in Hz
3549  *
3550  * Configures and enables PLL to generate output clock based on input clock.
3551  */
3552 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3553         unsigned int freq_in, unsigned int freq_out)
3554 {
3555         if (dai->driver && dai->driver->ops->set_pll)
3556                 return dai->driver->ops->set_pll(dai, pll_id, source,
3557                                          freq_in, freq_out);
3558         else if (dai->codec && dai->codec->driver->set_pll)
3559                 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3560                                                    freq_in, freq_out);
3561         else
3562                 return -EINVAL;
3563 }
3564 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3565
3566 /*
3567  * snd_soc_codec_set_pll - configure codec PLL.
3568  * @codec: CODEC
3569  * @pll_id: DAI specific PLL ID
3570  * @source: DAI specific source for the PLL
3571  * @freq_in: PLL input clock frequency in Hz
3572  * @freq_out: requested PLL output clock frequency in Hz
3573  *
3574  * Configures and enables PLL to generate output clock based on input clock.
3575  */
3576 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3577                           unsigned int freq_in, unsigned int freq_out)
3578 {
3579         if (codec->driver->set_pll)
3580                 return codec->driver->set_pll(codec, pll_id, source,
3581                                               freq_in, freq_out);
3582         else
3583                 return -EINVAL;
3584 }
3585 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3586
3587 /**
3588  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3589  * @dai: DAI
3590  * @ratio Ratio of BCLK to Sample rate.
3591  *
3592  * Configures the DAI for a preset BCLK to sample rate ratio.
3593  */
3594 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3595 {
3596         if (dai->driver && dai->driver->ops->set_bclk_ratio)
3597                 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3598         else
3599                 return -EINVAL;
3600 }
3601 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3602
3603 /**
3604  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3605  * @dai: DAI
3606  * @fmt: SND_SOC_DAIFMT_ format value.
3607  *
3608  * Configures the DAI hardware format and clocking.
3609  */
3610 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3611 {
3612         if (dai->driver == NULL)
3613                 return -EINVAL;
3614         if (dai->driver->ops->set_fmt == NULL)
3615                 return -ENOTSUPP;
3616         return dai->driver->ops->set_fmt(dai, fmt);
3617 }
3618 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3619
3620 /**
3621  * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
3622  * @slots: Number of slots in use.
3623  * @tx_mask: bitmask representing active TX slots.
3624  * @rx_mask: bitmask representing active RX slots.
3625  *
3626  * Generates the TDM tx and rx slot default masks for DAI.
3627  */
3628 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
3629                                           unsigned int *tx_mask,
3630                                           unsigned int *rx_mask)
3631 {
3632         if (*tx_mask || *rx_mask)
3633                 return 0;
3634
3635         if (!slots)
3636                 return -EINVAL;
3637
3638         *tx_mask = (1 << slots) - 1;
3639         *rx_mask = (1 << slots) - 1;
3640
3641         return 0;
3642 }
3643
3644 /**
3645  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3646  * @dai: DAI
3647  * @tx_mask: bitmask representing active TX slots.
3648  * @rx_mask: bitmask representing active RX slots.
3649  * @slots: Number of slots in use.
3650  * @slot_width: Width in bits for each slot.
3651  *
3652  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3653  * specific.
3654  */
3655 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3656         unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3657 {
3658         if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3659                 dai->driver->ops->xlate_tdm_slot_mask(slots,
3660                                                 &tx_mask, &rx_mask);
3661         else
3662                 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
3663
3664         if (dai->driver && dai->driver->ops->set_tdm_slot)
3665                 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3666                                 slots, slot_width);
3667         else
3668                 return -ENOTSUPP;
3669 }
3670 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3671
3672 /**
3673  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3674  * @dai: DAI
3675  * @tx_num: how many TX channels
3676  * @tx_slot: pointer to an array which imply the TX slot number channel
3677  *           0~num-1 uses
3678  * @rx_num: how many RX channels
3679  * @rx_slot: pointer to an array which imply the RX slot number channel
3680  *           0~num-1 uses
3681  *
3682  * configure the relationship between channel number and TDM slot number.
3683  */
3684 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3685         unsigned int tx_num, unsigned int *tx_slot,
3686         unsigned int rx_num, unsigned int *rx_slot)
3687 {
3688         if (dai->driver && dai->driver->ops->set_channel_map)
3689                 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3690                         rx_num, rx_slot);
3691         else
3692                 return -EINVAL;
3693 }
3694 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3695
3696 /**
3697  * snd_soc_dai_set_tristate - configure DAI system or master clock.
3698  * @dai: DAI
3699  * @tristate: tristate enable
3700  *
3701  * Tristates the DAI so that others can use it.
3702  */
3703 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3704 {
3705         if (dai->driver && dai->driver->ops->set_tristate)
3706                 return dai->driver->ops->set_tristate(dai, tristate);
3707         else
3708                 return -EINVAL;
3709 }
3710 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3711
3712 /**
3713  * snd_soc_dai_digital_mute - configure DAI system or master clock.
3714  * @dai: DAI
3715  * @mute: mute enable
3716  * @direction: stream to mute
3717  *
3718  * Mutes the DAI DAC.
3719  */
3720 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3721                              int direction)
3722 {
3723         if (!dai->driver)
3724                 return -ENOTSUPP;
3725
3726         if (dai->driver->ops->mute_stream)
3727                 return dai->driver->ops->mute_stream(dai, mute, direction);
3728         else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3729                  dai->driver->ops->digital_mute)
3730                 return dai->driver->ops->digital_mute(dai, mute);
3731         else
3732                 return -ENOTSUPP;
3733 }
3734 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3735
3736 /**
3737  * snd_soc_register_card - Register a card with the ASoC core
3738  *
3739  * @card: Card to register
3740  *
3741  */
3742 int snd_soc_register_card(struct snd_soc_card *card)
3743 {
3744         int i, ret;
3745
3746         if (!card->name || !card->dev)
3747                 return -EINVAL;
3748
3749         for (i = 0; i < card->num_links; i++) {
3750                 struct snd_soc_dai_link *link = &card->dai_link[i];
3751
3752                 /*
3753                  * Codec must be specified by 1 of name or OF node,
3754                  * not both or neither.
3755                  */
3756                 if (!!link->codec_name == !!link->codec_of_node) {
3757                         dev_err(card->dev,
3758                                 "ASoC: Neither/both codec name/of_node are set for %s\n",
3759                                 link->name);
3760                         return -EINVAL;
3761                 }
3762                 /* Codec DAI name must be specified */
3763                 if (!link->codec_dai_name) {
3764                         dev_err(card->dev,
3765                                 "ASoC: codec_dai_name not set for %s\n",
3766                                 link->name);
3767                         return -EINVAL;
3768                 }
3769
3770                 /*
3771                  * Platform may be specified by either name or OF node, but
3772                  * can be left unspecified, and a dummy platform will be used.
3773                  */
3774                 if (link->platform_name && link->platform_of_node) {
3775                         dev_err(card->dev,
3776                                 "ASoC: Both platform name/of_node are set for %s\n",
3777                                 link->name);
3778                         return -EINVAL;
3779                 }
3780
3781                 /*
3782                  * CPU device may be specified by either name or OF node, but
3783                  * can be left unspecified, and will be matched based on DAI
3784                  * name alone..
3785                  */
3786                 if (link->cpu_name && link->cpu_of_node) {
3787                         dev_err(card->dev,
3788                                 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3789                                 link->name);
3790                         return -EINVAL;
3791                 }
3792                 /*
3793                  * At least one of CPU DAI name or CPU device name/node must be
3794                  * specified
3795                  */
3796                 if (!link->cpu_dai_name &&
3797                     !(link->cpu_name || link->cpu_of_node)) {
3798                         dev_err(card->dev,
3799                                 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3800                                 link->name);
3801                         return -EINVAL;
3802                 }
3803         }
3804
3805         dev_set_drvdata(card->dev, card);
3806
3807         snd_soc_initialize_card_lists(card);
3808
3809         soc_init_card_debugfs(card);
3810
3811         card->rtd = devm_kzalloc(card->dev,
3812                                  sizeof(struct snd_soc_pcm_runtime) *
3813                                  (card->num_links + card->num_aux_devs),
3814                                  GFP_KERNEL);
3815         if (card->rtd == NULL)
3816                 return -ENOMEM;
3817         card->num_rtd = 0;
3818         card->rtd_aux = &card->rtd[card->num_links];
3819
3820         for (i = 0; i < card->num_links; i++)
3821                 card->rtd[i].dai_link = &card->dai_link[i];
3822
3823         INIT_LIST_HEAD(&card->dapm_dirty);
3824         card->instantiated = 0;
3825         mutex_init(&card->mutex);
3826         mutex_init(&card->dapm_mutex);
3827
3828         ret = snd_soc_instantiate_card(card);
3829         if (ret != 0)
3830                 soc_cleanup_card_debugfs(card);
3831
3832         /* deactivate pins to sleep state */
3833         for (i = 0; i < card->num_rtd; i++) {
3834                 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3835                 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3836                 if (!codec_dai->active)
3837                         pinctrl_pm_select_sleep_state(codec_dai->dev);
3838                 if (!cpu_dai->active)
3839                         pinctrl_pm_select_sleep_state(cpu_dai->dev);
3840         }
3841
3842         return ret;
3843 }
3844 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3845
3846 /**
3847  * snd_soc_unregister_card - Unregister a card with the ASoC core
3848  *
3849  * @card: Card to unregister
3850  *
3851  */
3852 int snd_soc_unregister_card(struct snd_soc_card *card)
3853 {
3854         if (card->instantiated)
3855                 soc_cleanup_card_resources(card);
3856         dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3857
3858         return 0;
3859 }
3860 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3861
3862 /*
3863  * Simplify DAI link configuration by removing ".-1" from device names
3864  * and sanitizing names.
3865  */
3866 static char *fmt_single_name(struct device *dev, int *id)
3867 {
3868         char *found, name[NAME_SIZE];
3869         int id1, id2;
3870
3871         if (dev_name(dev) == NULL)
3872                 return NULL;
3873
3874         strlcpy(name, dev_name(dev), NAME_SIZE);
3875
3876         /* are we a "%s.%d" name (platform and SPI components) */
3877         found = strstr(name, dev->driver->name);
3878         if (found) {
3879                 /* get ID */
3880                 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3881
3882                         /* discard ID from name if ID == -1 */
3883                         if (*id == -1)
3884                                 found[strlen(dev->driver->name)] = '\0';
3885                 }
3886
3887         } else {
3888                 /* I2C component devices are named "bus-addr"  */
3889                 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3890                         char tmp[NAME_SIZE];
3891
3892                         /* create unique ID number from I2C addr and bus */
3893                         *id = ((id1 & 0xffff) << 16) + id2;
3894
3895                         /* sanitize component name for DAI link creation */
3896                         snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3897                         strlcpy(name, tmp, NAME_SIZE);
3898                 } else
3899                         *id = 0;
3900         }
3901
3902         return kstrdup(name, GFP_KERNEL);
3903 }
3904
3905 /*
3906  * Simplify DAI link naming for single devices with multiple DAIs by removing
3907  * any ".-1" and using the DAI name (instead of device name).
3908  */
3909 static inline char *fmt_multiple_name(struct device *dev,
3910                 struct snd_soc_dai_driver *dai_drv)
3911 {
3912         if (dai_drv->name == NULL) {
3913                 dev_err(dev,
3914                         "ASoC: error - multiple DAI %s registered with no name\n",
3915                         dev_name(dev));
3916                 return NULL;
3917         }
3918
3919         return kstrdup(dai_drv->name, GFP_KERNEL);
3920 }
3921
3922 /**
3923  * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
3924  *
3925  * @component: The component for which the DAIs should be unregistered
3926  */
3927 static void snd_soc_unregister_dais(struct snd_soc_component *component)
3928 {
3929         struct snd_soc_dai *dai, *_dai;
3930
3931         list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
3932                 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3933                         dai->name);
3934                 list_del(&dai->list);
3935                 kfree(dai->name);
3936                 kfree(dai);
3937         }
3938 }
3939
3940 /**
3941  * snd_soc_register_dais - Register a DAI with the ASoC core
3942  *
3943  * @component: The component the DAIs are registered for
3944  * @codec: The CODEC that the DAIs are registered for, NULL if the component is
3945  *         not a CODEC.
3946  * @dai_drv: DAI driver to use for the DAIs
3947  * @count: Number of DAIs
3948  * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3949  *                     parent's name.
3950  */
3951 static int snd_soc_register_dais(struct snd_soc_component *component,
3952         struct snd_soc_codec *codec, struct snd_soc_dai_driver *dai_drv,
3953         size_t count, bool legacy_dai_naming)
3954 {
3955         struct device *dev = component->dev;
3956         struct snd_soc_dai *dai;
3957         unsigned int i;
3958         int ret;
3959
3960         dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3961
3962         for (i = 0; i < count; i++) {
3963
3964                 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3965                 if (dai == NULL) {
3966                         ret = -ENOMEM;
3967                         goto err;
3968                 }
3969
3970                 /*
3971                  * Back in the old days when we still had component-less DAIs,
3972                  * instead of having a static name, component-less DAIs would
3973                  * inherit the name of the parent device so it is possible to
3974                  * register multiple instances of the DAI. We still need to keep
3975                  * the same naming style even though those DAIs are not
3976                  * component-less anymore.
3977                  */
3978                 if (count == 1 && legacy_dai_naming) {
3979                         dai->name = fmt_single_name(dev, &dai->id);
3980                 } else {
3981                         dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3982                         if (dai_drv[i].id)
3983                                 dai->id = dai_drv[i].id;
3984                         else
3985                                 dai->id = i;
3986                 }
3987                 if (dai->name == NULL) {
3988                         kfree(dai);
3989                         ret = -ENOMEM;
3990                         goto err;
3991                 }
3992
3993                 dai->component = component;
3994                 dai->codec = codec;
3995                 dai->dev = dev;
3996                 dai->driver = &dai_drv[i];
3997                 dai->dapm.dev = dev;
3998                 if (!dai->driver->ops)
3999                         dai->driver->ops = &null_dai_ops;
4000
4001                 if (!dai->codec)
4002                         dai->dapm.idle_bias_off = 1;
4003
4004                 list_add(&dai->list, &component->dai_list);
4005
4006                 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
4007         }
4008
4009         return 0;
4010
4011 err:
4012         snd_soc_unregister_dais(component);
4013
4014         return ret;
4015 }
4016
4017 /**
4018  * snd_soc_register_component - Register a component with the ASoC core
4019  *
4020  */
4021 static int
4022 __snd_soc_register_component(struct device *dev,
4023                              struct snd_soc_component *cmpnt,
4024                              const struct snd_soc_component_driver *cmpnt_drv,
4025                              struct snd_soc_codec *codec,
4026                              struct snd_soc_dai_driver *dai_drv,
4027                              int num_dai, bool allow_single_dai)
4028 {
4029         int ret;
4030
4031         dev_dbg(dev, "component register %s\n", dev_name(dev));
4032
4033         if (!cmpnt) {
4034                 dev_err(dev, "ASoC: Failed to connecting component\n");
4035                 return -ENOMEM;
4036         }
4037
4038         mutex_init(&cmpnt->io_mutex);
4039
4040         cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4041         if (!cmpnt->name) {
4042                 dev_err(dev, "ASoC: Failed to simplifying name\n");
4043                 return -ENOMEM;
4044         }
4045
4046         cmpnt->dev      = dev;
4047         cmpnt->driver   = cmpnt_drv;
4048         cmpnt->dai_drv  = dai_drv;
4049         cmpnt->num_dai  = num_dai;
4050         INIT_LIST_HEAD(&cmpnt->dai_list);
4051
4052         ret = snd_soc_register_dais(cmpnt, codec, dai_drv, num_dai,
4053                 allow_single_dai);
4054         if (ret < 0) {
4055                 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4056                 goto error_component_name;
4057         }
4058
4059         mutex_lock(&client_mutex);
4060         list_add(&cmpnt->list, &component_list);
4061         mutex_unlock(&client_mutex);
4062
4063         dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4064
4065         return ret;
4066
4067 error_component_name:
4068         kfree(cmpnt->name);
4069
4070         return ret;
4071 }
4072
4073 int snd_soc_register_component(struct device *dev,
4074                                const struct snd_soc_component_driver *cmpnt_drv,
4075                                struct snd_soc_dai_driver *dai_drv,
4076                                int num_dai)
4077 {
4078         struct snd_soc_component *cmpnt;
4079
4080         cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4081         if (!cmpnt) {
4082                 dev_err(dev, "ASoC: Failed to allocate memory\n");
4083                 return -ENOMEM;
4084         }
4085
4086         cmpnt->ignore_pmdown_time = true;
4087         cmpnt->registered_as_component = true;
4088
4089         return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, NULL,
4090                                             dai_drv, num_dai, true);
4091 }
4092 EXPORT_SYMBOL_GPL(snd_soc_register_component);
4093
4094 static void __snd_soc_unregister_component(struct snd_soc_component *cmpnt)
4095 {
4096         snd_soc_unregister_dais(cmpnt);
4097
4098         mutex_lock(&client_mutex);
4099         list_del(&cmpnt->list);
4100         mutex_unlock(&client_mutex);
4101
4102         dev_dbg(cmpnt->dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4103         kfree(cmpnt->name);
4104 }
4105
4106 /**
4107  * snd_soc_unregister_component - Unregister a component from the ASoC core
4108  *
4109  */
4110 void snd_soc_unregister_component(struct device *dev)
4111 {
4112         struct snd_soc_component *cmpnt;
4113
4114         list_for_each_entry(cmpnt, &component_list, list) {
4115                 if (dev == cmpnt->dev && cmpnt->registered_as_component)
4116                         goto found;
4117         }
4118         return;
4119
4120 found:
4121         __snd_soc_unregister_component(cmpnt);
4122 }
4123 EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4124
4125 static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4126         unsigned int reg, unsigned int val)
4127 {
4128         struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4129
4130         return platform->driver->write(platform, reg, val);
4131 }
4132
4133 static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4134         unsigned int reg, unsigned int *val)
4135 {
4136         struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4137
4138         *val = platform->driver->read(platform, reg);
4139
4140         return 0;
4141 }
4142
4143 /**
4144  * snd_soc_add_platform - Add a platform to the ASoC core
4145  * @dev: The parent device for the platform
4146  * @platform: The platform to add
4147  * @platform_driver: The driver for the platform
4148  */
4149 int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
4150                 const struct snd_soc_platform_driver *platform_drv)
4151 {
4152         int ret;
4153
4154         /* create platform component name */
4155         platform->name = fmt_single_name(dev, &platform->id);
4156         if (platform->name == NULL)
4157                 return -ENOMEM;
4158
4159         platform->dev = dev;
4160         platform->driver = platform_drv;
4161         platform->dapm.dev = dev;
4162         platform->dapm.platform = platform;
4163         platform->dapm.component = &platform->component;
4164         platform->dapm.stream_event = platform_drv->stream_event;
4165         if (platform_drv->write)
4166                 platform->component.write = snd_soc_platform_drv_write;
4167         if (platform_drv->read)
4168                 platform->component.read = snd_soc_platform_drv_read;
4169
4170         /* register component */
4171         ret = __snd_soc_register_component(dev, &platform->component,
4172                                            &platform_drv->component_driver,
4173                                            NULL, NULL, 0, false);
4174         if (ret < 0) {
4175                 dev_err(platform->component.dev,
4176                         "ASoC: Failed to register component: %d\n", ret);
4177                 return ret;
4178         }
4179
4180         mutex_lock(&client_mutex);
4181         list_add(&platform->list, &platform_list);
4182         mutex_unlock(&client_mutex);
4183
4184         dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
4185
4186         return 0;
4187 }
4188 EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4189
4190 /**
4191  * snd_soc_register_platform - Register a platform with the ASoC core
4192  *
4193  * @platform: platform to register
4194  */
4195 int snd_soc_register_platform(struct device *dev,
4196                 const struct snd_soc_platform_driver *platform_drv)
4197 {
4198         struct snd_soc_platform *platform;
4199         int ret;
4200
4201         dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4202
4203         platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4204         if (platform == NULL)
4205                 return -ENOMEM;
4206
4207         ret = snd_soc_add_platform(dev, platform, platform_drv);
4208         if (ret)
4209                 kfree(platform);
4210
4211         return ret;
4212 }
4213 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4214
4215 /**
4216  * snd_soc_remove_platform - Remove a platform from the ASoC core
4217  * @platform: the platform to remove
4218  */
4219 void snd_soc_remove_platform(struct snd_soc_platform *platform)
4220 {
4221         __snd_soc_unregister_component(&platform->component);
4222
4223         mutex_lock(&client_mutex);
4224         list_del(&platform->list);
4225         mutex_unlock(&client_mutex);
4226
4227         dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4228                 platform->name);
4229         kfree(platform->name);
4230 }
4231 EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4232
4233 struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4234 {
4235         struct snd_soc_platform *platform;
4236
4237         list_for_each_entry(platform, &platform_list, list) {
4238                 if (dev == platform->dev)
4239                         return platform;
4240         }
4241
4242         return NULL;
4243 }
4244 EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4245
4246 /**
4247  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4248  *
4249  * @platform: platform to unregister
4250  */
4251 void snd_soc_unregister_platform(struct device *dev)
4252 {
4253         struct snd_soc_platform *platform;
4254
4255         platform = snd_soc_lookup_platform(dev);
4256         if (!platform)
4257                 return;
4258
4259         snd_soc_remove_platform(platform);
4260         kfree(platform);
4261 }
4262 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4263
4264 static u64 codec_format_map[] = {
4265         SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4266         SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4267         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4268         SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4269         SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4270         SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4271         SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4272         SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4273         SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4274         SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4275         SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4276         SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4277         SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4278         SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4279         SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4280         | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4281 };
4282
4283 /* Fix up the DAI formats for endianness: codecs don't actually see
4284  * the endianness of the data but we're using the CPU format
4285  * definitions which do need to include endianness so we ensure that
4286  * codec DAIs always have both big and little endian variants set.
4287  */
4288 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4289 {
4290         int i;
4291
4292         for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4293                 if (stream->formats & codec_format_map[i])
4294                         stream->formats |= codec_format_map[i];
4295 }
4296
4297 static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4298         unsigned int reg, unsigned int val)
4299 {
4300         struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4301
4302         return codec->driver->write(codec, reg, val);
4303 }
4304
4305 static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4306         unsigned int reg, unsigned int *val)
4307 {
4308         struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4309
4310         *val = codec->driver->read(codec, reg);
4311
4312         return 0;
4313 }
4314
4315 /**
4316  * snd_soc_register_codec - Register a codec with the ASoC core
4317  *
4318  * @codec: codec to register
4319  */
4320 int snd_soc_register_codec(struct device *dev,
4321                            const struct snd_soc_codec_driver *codec_drv,
4322                            struct snd_soc_dai_driver *dai_drv,
4323                            int num_dai)
4324 {
4325         struct snd_soc_codec *codec;
4326         struct regmap *regmap;
4327         int ret, i;
4328
4329         dev_dbg(dev, "codec register %s\n", dev_name(dev));
4330
4331         codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4332         if (codec == NULL)
4333                 return -ENOMEM;
4334
4335         /* create CODEC component name */
4336         codec->name = fmt_single_name(dev, &codec->id);
4337         if (codec->name == NULL) {
4338                 ret = -ENOMEM;
4339                 goto fail_codec;
4340         }
4341
4342         if (codec_drv->write)
4343                 codec->component.write = snd_soc_codec_drv_write;
4344         if (codec_drv->read)
4345                 codec->component.read = snd_soc_codec_drv_read;
4346         codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4347         codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4348         codec->dapm.dev = dev;
4349         codec->dapm.codec = codec;
4350         codec->dapm.component = &codec->component;
4351         codec->dapm.seq_notifier = codec_drv->seq_notifier;
4352         codec->dapm.stream_event = codec_drv->stream_event;
4353         codec->dev = dev;
4354         codec->driver = codec_drv;
4355         codec->component.val_bytes = codec_drv->reg_word_size;
4356         mutex_init(&codec->mutex);
4357
4358         if (!codec->component.write) {
4359                 if (codec_drv->get_regmap)
4360                         regmap = codec_drv->get_regmap(dev);
4361                 else
4362                         regmap = dev_get_regmap(dev, NULL);
4363
4364                 if (regmap) {
4365                         ret = snd_soc_component_init_io(&codec->component,
4366                                 regmap);
4367                         if (ret) {
4368                                 dev_err(codec->dev,
4369                                                 "Failed to set cache I/O:%d\n",
4370                                                 ret);
4371                                 return ret;
4372                         }
4373                 }
4374         }
4375
4376         for (i = 0; i < num_dai; i++) {
4377                 fixup_codec_formats(&dai_drv[i].playback);
4378                 fixup_codec_formats(&dai_drv[i].capture);
4379         }
4380
4381         mutex_lock(&client_mutex);
4382         list_add(&codec->list, &codec_list);
4383         mutex_unlock(&client_mutex);
4384
4385         /* register component */
4386         ret = __snd_soc_register_component(dev, &codec->component,
4387                                            &codec_drv->component_driver,
4388                                            codec, dai_drv, num_dai, false);
4389         if (ret < 0) {
4390                 dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
4391                 goto fail_codec_name;
4392         }
4393
4394         dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
4395         return 0;
4396
4397 fail_codec_name:
4398         mutex_lock(&client_mutex);
4399         list_del(&codec->list);
4400         mutex_unlock(&client_mutex);
4401
4402         kfree(codec->name);
4403 fail_codec:
4404         kfree(codec);
4405         return ret;
4406 }
4407 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4408
4409 /**
4410  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4411  *
4412  * @codec: codec to unregister
4413  */
4414 void snd_soc_unregister_codec(struct device *dev)
4415 {
4416         struct snd_soc_codec *codec;
4417
4418         list_for_each_entry(codec, &codec_list, list) {
4419                 if (dev == codec->dev)
4420                         goto found;
4421         }
4422         return;
4423
4424 found:
4425         __snd_soc_unregister_component(&codec->component);
4426
4427         mutex_lock(&client_mutex);
4428         list_del(&codec->list);
4429         mutex_unlock(&client_mutex);
4430
4431         dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4432
4433         snd_soc_cache_exit(codec);
4434         kfree(codec->name);
4435         kfree(codec);
4436 }
4437 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4438
4439 /* Retrieve a card's name from device tree */
4440 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4441                                const char *propname)
4442 {
4443         struct device_node *np = card->dev->of_node;
4444         int ret;
4445
4446         ret = of_property_read_string_index(np, propname, 0, &card->name);
4447         /*
4448          * EINVAL means the property does not exist. This is fine providing
4449          * card->name was previously set, which is checked later in
4450          * snd_soc_register_card.
4451          */
4452         if (ret < 0 && ret != -EINVAL) {
4453                 dev_err(card->dev,
4454                         "ASoC: Property '%s' could not be read: %d\n",
4455                         propname, ret);
4456                 return ret;
4457         }
4458
4459         return 0;
4460 }
4461 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4462
4463 static const struct snd_soc_dapm_widget simple_widgets[] = {
4464         SND_SOC_DAPM_MIC("Microphone", NULL),
4465         SND_SOC_DAPM_LINE("Line", NULL),
4466         SND_SOC_DAPM_HP("Headphone", NULL),
4467         SND_SOC_DAPM_SPK("Speaker", NULL),
4468 };
4469
4470 int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4471                                           const char *propname)
4472 {
4473         struct device_node *np = card->dev->of_node;
4474         struct snd_soc_dapm_widget *widgets;
4475         const char *template, *wname;
4476         int i, j, num_widgets, ret;
4477
4478         num_widgets = of_property_count_strings(np, propname);
4479         if (num_widgets < 0) {
4480                 dev_err(card->dev,
4481                         "ASoC: Property '%s' does not exist\n", propname);
4482                 return -EINVAL;
4483         }
4484         if (num_widgets & 1) {
4485                 dev_err(card->dev,
4486                         "ASoC: Property '%s' length is not even\n", propname);
4487                 return -EINVAL;
4488         }
4489
4490         num_widgets /= 2;
4491         if (!num_widgets) {
4492                 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4493                         propname);
4494                 return -EINVAL;
4495         }
4496
4497         widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4498                                GFP_KERNEL);
4499         if (!widgets) {
4500                 dev_err(card->dev,
4501                         "ASoC: Could not allocate memory for widgets\n");
4502                 return -ENOMEM;
4503         }
4504
4505         for (i = 0; i < num_widgets; i++) {
4506                 ret = of_property_read_string_index(np, propname,
4507                         2 * i, &template);
4508                 if (ret) {
4509                         dev_err(card->dev,
4510                                 "ASoC: Property '%s' index %d read error:%d\n",
4511                                 propname, 2 * i, ret);
4512                         return -EINVAL;
4513                 }
4514
4515                 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4516                         if (!strncmp(template, simple_widgets[j].name,
4517                                      strlen(simple_widgets[j].name))) {
4518                                 widgets[i] = simple_widgets[j];
4519                                 break;
4520                         }
4521                 }
4522
4523                 if (j >= ARRAY_SIZE(simple_widgets)) {
4524                         dev_err(card->dev,
4525                                 "ASoC: DAPM widget '%s' is not supported\n",
4526                                 template);
4527                         return -EINVAL;
4528                 }
4529
4530                 ret = of_property_read_string_index(np, propname,
4531                                                     (2 * i) + 1,
4532                                                     &wname);
4533                 if (ret) {
4534                         dev_err(card->dev,
4535                                 "ASoC: Property '%s' index %d read error:%d\n",
4536                                 propname, (2 * i) + 1, ret);
4537                         return -EINVAL;
4538                 }
4539
4540                 widgets[i].name = wname;
4541         }
4542
4543         card->dapm_widgets = widgets;
4544         card->num_dapm_widgets = num_widgets;
4545
4546         return 0;
4547 }
4548 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4549
4550 int snd_soc_of_parse_tdm_slot(struct device_node *np,
4551                               unsigned int *slots,
4552                               unsigned int *slot_width)
4553 {
4554         u32 val;
4555         int ret;
4556
4557         if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4558                 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4559                 if (ret)
4560                         return ret;
4561
4562                 if (slots)
4563                         *slots = val;
4564         }
4565
4566         if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4567                 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4568                 if (ret)
4569                         return ret;
4570
4571                 if (slot_width)
4572                         *slot_width = val;
4573         }
4574
4575         return 0;
4576 }
4577 EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4578
4579 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4580                                    const char *propname)
4581 {
4582         struct device_node *np = card->dev->of_node;
4583         int num_routes;
4584         struct snd_soc_dapm_route *routes;
4585         int i, ret;
4586
4587         num_routes = of_property_count_strings(np, propname);
4588         if (num_routes < 0 || num_routes & 1) {
4589                 dev_err(card->dev,
4590                         "ASoC: Property '%s' does not exist or its length is not even\n",
4591                         propname);
4592                 return -EINVAL;
4593         }
4594         num_routes /= 2;
4595         if (!num_routes) {
4596                 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4597                         propname);
4598                 return -EINVAL;
4599         }
4600
4601         routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4602                               GFP_KERNEL);
4603         if (!routes) {
4604                 dev_err(card->dev,
4605                         "ASoC: Could not allocate DAPM route table\n");
4606                 return -EINVAL;
4607         }
4608
4609         for (i = 0; i < num_routes; i++) {
4610                 ret = of_property_read_string_index(np, propname,
4611                         2 * i, &routes[i].sink);
4612                 if (ret) {
4613                         dev_err(card->dev,
4614                                 "ASoC: Property '%s' index %d could not be read: %d\n",
4615                                 propname, 2 * i, ret);
4616                         return -EINVAL;
4617                 }
4618                 ret = of_property_read_string_index(np, propname,
4619                         (2 * i) + 1, &routes[i].source);
4620                 if (ret) {
4621                         dev_err(card->dev,
4622                                 "ASoC: Property '%s' index %d could not be read: %d\n",
4623                                 propname, (2 * i) + 1, ret);
4624                         return -EINVAL;
4625                 }
4626         }
4627
4628         card->num_dapm_routes = num_routes;
4629         card->dapm_routes = routes;
4630
4631         return 0;
4632 }
4633 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4634
4635 unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4636                                      const char *prefix,
4637                                      struct device_node **bitclkmaster,
4638                                      struct device_node **framemaster)
4639 {
4640         int ret, i;
4641         char prop[128];
4642         unsigned int format = 0;
4643         int bit, frame;
4644         const char *str;
4645         struct {
4646                 char *name;
4647                 unsigned int val;
4648         } of_fmt_table[] = {
4649                 { "i2s",        SND_SOC_DAIFMT_I2S },
4650                 { "right_j",    SND_SOC_DAIFMT_RIGHT_J },
4651                 { "left_j",     SND_SOC_DAIFMT_LEFT_J },
4652                 { "dsp_a",      SND_SOC_DAIFMT_DSP_A },
4653                 { "dsp_b",      SND_SOC_DAIFMT_DSP_B },
4654                 { "ac97",       SND_SOC_DAIFMT_AC97 },
4655                 { "pdm",        SND_SOC_DAIFMT_PDM},
4656                 { "msb",        SND_SOC_DAIFMT_MSB },
4657                 { "lsb",        SND_SOC_DAIFMT_LSB },
4658         };
4659
4660         if (!prefix)
4661                 prefix = "";
4662
4663         /*
4664          * check "[prefix]format = xxx"
4665          * SND_SOC_DAIFMT_FORMAT_MASK area
4666          */
4667         snprintf(prop, sizeof(prop), "%sformat", prefix);
4668         ret = of_property_read_string(np, prop, &str);
4669         if (ret == 0) {
4670                 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4671                         if (strcmp(str, of_fmt_table[i].name) == 0) {
4672                                 format |= of_fmt_table[i].val;
4673                                 break;
4674                         }
4675                 }
4676         }
4677
4678         /*
4679          * check "[prefix]continuous-clock"
4680          * SND_SOC_DAIFMT_CLOCK_MASK area
4681          */
4682         snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4683         if (of_get_property(np, prop, NULL))
4684                 format |= SND_SOC_DAIFMT_CONT;
4685         else
4686                 format |= SND_SOC_DAIFMT_GATED;
4687
4688         /*
4689          * check "[prefix]bitclock-inversion"
4690          * check "[prefix]frame-inversion"
4691          * SND_SOC_DAIFMT_INV_MASK area
4692          */
4693         snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4694         bit = !!of_get_property(np, prop, NULL);
4695
4696         snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4697         frame = !!of_get_property(np, prop, NULL);
4698
4699         switch ((bit << 4) + frame) {
4700         case 0x11:
4701                 format |= SND_SOC_DAIFMT_IB_IF;
4702                 break;
4703         case 0x10:
4704                 format |= SND_SOC_DAIFMT_IB_NF;
4705                 break;
4706         case 0x01:
4707                 format |= SND_SOC_DAIFMT_NB_IF;
4708                 break;
4709         default:
4710                 /* SND_SOC_DAIFMT_NB_NF is default */
4711                 break;
4712         }
4713
4714         /*
4715          * check "[prefix]bitclock-master"
4716          * check "[prefix]frame-master"
4717          * SND_SOC_DAIFMT_MASTER_MASK area
4718          */
4719         snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4720         bit = !!of_get_property(np, prop, NULL);
4721         if (bit && bitclkmaster)
4722                 *bitclkmaster = of_parse_phandle(np, prop, 0);
4723
4724         snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4725         frame = !!of_get_property(np, prop, NULL);
4726         if (frame && framemaster)
4727                 *framemaster = of_parse_phandle(np, prop, 0);
4728
4729         switch ((bit << 4) + frame) {
4730         case 0x11:
4731                 format |= SND_SOC_DAIFMT_CBM_CFM;
4732                 break;
4733         case 0x10:
4734                 format |= SND_SOC_DAIFMT_CBM_CFS;
4735                 break;
4736         case 0x01:
4737                 format |= SND_SOC_DAIFMT_CBS_CFM;
4738                 break;
4739         default:
4740                 format |= SND_SOC_DAIFMT_CBS_CFS;
4741                 break;
4742         }
4743
4744         return format;
4745 }
4746 EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4747
4748 int snd_soc_of_get_dai_name(struct device_node *of_node,
4749                             const char **dai_name)
4750 {
4751         struct snd_soc_component *pos;
4752         struct of_phandle_args args;
4753         int ret;
4754
4755         ret = of_parse_phandle_with_args(of_node, "sound-dai",
4756                                          "#sound-dai-cells", 0, &args);
4757         if (ret)
4758                 return ret;
4759
4760         ret = -EPROBE_DEFER;
4761
4762         mutex_lock(&client_mutex);
4763         list_for_each_entry(pos, &component_list, list) {
4764                 if (pos->dev->of_node != args.np)
4765                         continue;
4766
4767                 if (pos->driver->of_xlate_dai_name) {
4768                         ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4769                 } else {
4770                         int id = -1;
4771
4772                         switch (args.args_count) {
4773                         case 0:
4774                                 id = 0; /* same as dai_drv[0] */
4775                                 break;
4776                         case 1:
4777                                 id = args.args[0];
4778                                 break;
4779                         default:
4780                                 /* not supported */
4781                                 break;
4782                         }
4783
4784                         if (id < 0 || id >= pos->num_dai) {
4785                                 ret = -EINVAL;
4786                                 continue;
4787                         }
4788
4789                         ret = 0;
4790
4791                         *dai_name = pos->dai_drv[id].name;
4792                         if (!*dai_name)
4793                                 *dai_name = pos->name;
4794                 }
4795
4796                 break;
4797         }
4798         mutex_unlock(&client_mutex);
4799
4800         of_node_put(args.np);
4801
4802         return ret;
4803 }
4804 EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4805
4806 static int __init snd_soc_init(void)
4807 {
4808 #ifdef CONFIG_DEBUG_FS
4809         snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4810         if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4811                 pr_warn("ASoC: Failed to create debugfs directory\n");
4812                 snd_soc_debugfs_root = NULL;
4813         }
4814
4815         if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4816                                  &codec_list_fops))
4817                 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4818
4819         if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4820                                  &dai_list_fops))
4821                 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4822
4823         if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4824                                  &platform_list_fops))
4825                 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4826 #endif
4827
4828         snd_soc_util_init();
4829
4830         return platform_driver_register(&soc_driver);
4831 }
4832 module_init(snd_soc_init);
4833
4834 static void __exit snd_soc_exit(void)
4835 {
4836         snd_soc_util_exit();
4837
4838 #ifdef CONFIG_DEBUG_FS
4839         debugfs_remove_recursive(snd_soc_debugfs_root);
4840 #endif
4841         platform_driver_unregister(&soc_driver);
4842 }
4843 module_exit(snd_soc_exit);
4844
4845 /* Module information */
4846 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4847 MODULE_DESCRIPTION("ALSA SoC Core");
4848 MODULE_LICENSE("GPL");
4849 MODULE_ALIAS("platform:soc-audio");