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