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