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