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