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