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