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