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