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