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