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