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