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