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