]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - sound/soc/soc-core.c
llseek: automatically add .llseek fop
[mirror_ubuntu-artful-kernel.git] / sound / soc / soc-core.c
CommitLineData
db2a4165
FM
1/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
0664d888
LG
5 * Copyright 2005 Openedhand Ltd.
6 *
d331124d 7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
0664d888
LG
8 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
db2a4165
FM
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
db2a4165
FM
16 * TODO:
17 * o Add hw rules to enforce rates, etc.
18 * o More testing with other codecs/machines.
19 * o Add more codecs and platforms to ensure good API coverage.
20 * o Support TDM on PCM and I2S
21 */
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/pm.h>
28#include <linux/bitops.h>
12ef193d 29#include <linux/debugfs.h>
db2a4165 30#include <linux/platform_device.h>
5a0e3ad6 31#include <linux/slab.h>
474828a4 32#include <sound/ac97_codec.h>
db2a4165
FM
33#include <sound/core.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/soc.h>
37#include <sound/soc-dapm.h>
38#include <sound/initval.h>
39
db2a4165 40static DEFINE_MUTEX(pcm_mutex);
db2a4165
FM
41static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
42
384c89e2
MB
43#ifdef CONFIG_DEBUG_FS
44static struct dentry *debugfs_root;
45#endif
46
c5af3a2e
MB
47static DEFINE_MUTEX(client_mutex);
48static LIST_HEAD(card_list);
9115171a 49static LIST_HEAD(dai_list);
12a48a8c 50static LIST_HEAD(platform_list);
0d0cf00a 51static LIST_HEAD(codec_list);
c5af3a2e
MB
52
53static int snd_soc_register_card(struct snd_soc_card *card);
54static int snd_soc_unregister_card(struct snd_soc_card *card);
55
db2a4165
FM
56/*
57 * This is a timeout to do a DAPM powerdown after a stream is closed().
58 * It can be used to eliminate pops between different playback streams, e.g.
59 * between two audio tracks.
60 */
61static int pmdown_time = 5000;
62module_param(pmdown_time, int, 0);
63MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
64
965ac42c
LG
65/*
66 * This function forces any delayed work to be queued and run.
67 */
68static int run_delayed_work(struct delayed_work *dwork)
69{
70 int ret;
71
72 /* cancel any work waiting to be queued. */
73 ret = cancel_delayed_work(dwork);
74
75 /* if there was any work waiting then we run it now and
76 * wait for it's completion */
77 if (ret) {
78 schedule_delayed_work(dwork, 0);
79 flush_scheduled_work();
80 }
81 return ret;
82}
83
2624d5fa
MB
84/* codec register dump */
85static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
86{
5164d74d 87 int ret, i, step = 1, count = 0;
2624d5fa
MB
88
89 if (!codec->reg_cache_size)
90 return 0;
91
92 if (codec->reg_cache_step)
93 step = codec->reg_cache_step;
94
95 count += sprintf(buf, "%s registers\n", codec->name);
96 for (i = 0; i < codec->reg_cache_size; i += step) {
97 if (codec->readable_register && !codec->readable_register(i))
98 continue;
99
100 count += sprintf(buf + count, "%2x: ", i);
101 if (count >= PAGE_SIZE - 1)
102 break;
103
5164d74d 104 if (codec->display_register) {
2624d5fa
MB
105 count += codec->display_register(codec, buf + count,
106 PAGE_SIZE - count, i);
5164d74d
MB
107 } else {
108 /* If the read fails it's almost certainly due to
109 * the register being volatile and the device being
110 * powered off.
111 */
112 ret = codec->read(codec, i);
113 if (ret >= 0)
114 count += snprintf(buf + count,
115 PAGE_SIZE - count,
116 "%4x", ret);
117 else
118 count += snprintf(buf + count,
119 PAGE_SIZE - count,
120 "<no data: %d>", ret);
121 }
2624d5fa
MB
122
123 if (count >= PAGE_SIZE - 1)
124 break;
125
126 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
127 if (count >= PAGE_SIZE - 1)
128 break;
129 }
130
131 /* Truncate count; min() would cause a warning */
132 if (count >= PAGE_SIZE)
133 count = PAGE_SIZE - 1;
134
135 return count;
136}
137static ssize_t codec_reg_show(struct device *dev,
138 struct device_attribute *attr, char *buf)
139{
140 struct snd_soc_device *devdata = dev_get_drvdata(dev);
141 return soc_codec_reg_show(devdata->card->codec, buf);
142}
143
144static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
145
dbe21408
MB
146static ssize_t pmdown_time_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
148{
149 struct snd_soc_device *socdev = dev_get_drvdata(dev);
150 struct snd_soc_card *card = socdev->card;
151
6c5f1fed 152 return sprintf(buf, "%ld\n", card->pmdown_time);
dbe21408
MB
153}
154
155static ssize_t pmdown_time_set(struct device *dev,
156 struct device_attribute *attr,
157 const char *buf, size_t count)
158{
159 struct snd_soc_device *socdev = dev_get_drvdata(dev);
160 struct snd_soc_card *card = socdev->card;
161
162 strict_strtol(buf, 10, &card->pmdown_time);
163
164 return count;
165}
166
167static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
168
2624d5fa
MB
169#ifdef CONFIG_DEBUG_FS
170static int codec_reg_open_file(struct inode *inode, struct file *file)
171{
172 file->private_data = inode->i_private;
173 return 0;
174}
175
176static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
177 size_t count, loff_t *ppos)
178{
179 ssize_t ret;
180 struct snd_soc_codec *codec = file->private_data;
181 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
182 if (!buf)
183 return -ENOMEM;
184 ret = soc_codec_reg_show(codec, buf);
185 if (ret >= 0)
186 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
187 kfree(buf);
188 return ret;
189}
190
191static ssize_t codec_reg_write_file(struct file *file,
192 const char __user *user_buf, size_t count, loff_t *ppos)
193{
194 char buf[32];
195 int buf_size;
196 char *start = buf;
197 unsigned long reg, value;
198 int step = 1;
199 struct snd_soc_codec *codec = file->private_data;
200
201 buf_size = min(count, (sizeof(buf)-1));
202 if (copy_from_user(buf, user_buf, buf_size))
203 return -EFAULT;
204 buf[buf_size] = 0;
205
206 if (codec->reg_cache_step)
207 step = codec->reg_cache_step;
208
209 while (*start == ' ')
210 start++;
211 reg = simple_strtoul(start, &start, 16);
212 if ((reg >= codec->reg_cache_size) || (reg % step))
213 return -EINVAL;
214 while (*start == ' ')
215 start++;
216 if (strict_strtoul(start, 16, &value))
217 return -EINVAL;
218 codec->write(codec, reg, value);
219 return buf_size;
220}
221
222static const struct file_operations codec_reg_fops = {
223 .open = codec_reg_open_file,
224 .read = codec_reg_read_file,
225 .write = codec_reg_write_file,
6038f373 226 .llseek = default_llseek,
2624d5fa
MB
227};
228
229static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
230{
231 char codec_root[128];
232
233 if (codec->dev)
234 snprintf(codec_root, sizeof(codec_root),
235 "%s.%s", codec->name, dev_name(codec->dev));
236 else
237 snprintf(codec_root, sizeof(codec_root),
238 "%s", codec->name);
239
240 codec->debugfs_codec_root = debugfs_create_dir(codec_root,
241 debugfs_root);
242 if (!codec->debugfs_codec_root) {
243 printk(KERN_WARNING
244 "ASoC: Failed to create codec debugfs directory\n");
245 return;
246 }
247
248 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
249 codec->debugfs_codec_root,
250 codec, &codec_reg_fops);
251 if (!codec->debugfs_reg)
252 printk(KERN_WARNING
253 "ASoC: Failed to create codec register debugfs file\n");
254
708fafb3 255 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
2624d5fa
MB
256 codec->debugfs_codec_root,
257 &codec->pop_time);
258 if (!codec->debugfs_pop_time)
259 printk(KERN_WARNING
260 "Failed to create pop time debugfs file\n");
261
262 codec->debugfs_dapm = debugfs_create_dir("dapm",
263 codec->debugfs_codec_root);
264 if (!codec->debugfs_dapm)
265 printk(KERN_WARNING
266 "Failed to create DAPM debugfs directory\n");
267
268 snd_soc_dapm_debugfs_init(codec);
269}
270
271static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
272{
273 debugfs_remove_recursive(codec->debugfs_codec_root);
274}
275
276#else
277
278static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
279{
280}
281
282static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
283{
284}
285#endif
286
db2a4165
FM
287#ifdef CONFIG_SND_SOC_AC97_BUS
288/* unregister ac97 codec */
289static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
290{
291 if (codec->ac97->dev.bus)
292 device_unregister(&codec->ac97->dev);
293 return 0;
294}
295
296/* stop no dev release warning */
297static void soc_ac97_device_release(struct device *dev){}
298
299/* register ac97 codec to bus */
300static int soc_ac97_dev_register(struct snd_soc_codec *codec)
301{
302 int err;
303
304 codec->ac97->dev.bus = &ac97_bus_type;
4ac5c61f 305 codec->ac97->dev.parent = codec->card->dev;
db2a4165
FM
306 codec->ac97->dev.release = soc_ac97_device_release;
307
bb072bf0
KS
308 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
309 codec->card->number, 0, codec->name);
db2a4165
FM
310 err = device_register(&codec->ac97->dev);
311 if (err < 0) {
312 snd_printk(KERN_ERR "Can't register ac97 bus\n");
313 codec->ac97->dev.bus = NULL;
314 return err;
315 }
316 return 0;
317}
318#endif
319
06f409d7
MB
320static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
321{
322 struct snd_soc_pcm_runtime *rtd = substream->private_data;
323 struct snd_soc_device *socdev = rtd->socdev;
324 struct snd_soc_card *card = socdev->card;
325 struct snd_soc_dai_link *machine = rtd->dai;
326 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
327 struct snd_soc_dai *codec_dai = machine->codec_dai;
328 int ret;
329
330 if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
331 machine->symmetric_rates) {
50831450 332 dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
06f409d7
MB
333 machine->rate);
334
335 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
336 SNDRV_PCM_HW_PARAM_RATE,
337 machine->rate,
338 machine->rate);
339 if (ret < 0) {
340 dev_err(card->dev,
341 "Unable to apply rate symmetry constraint: %d\n", ret);
342 return ret;
343 }
344 }
345
346 return 0;
347}
348
db2a4165
FM
349/*
350 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
351 * then initialized and any private data can be allocated. This also calls
352 * startup for the cpu DAI, platform, machine and codec DAI.
353 */
354static int soc_pcm_open(struct snd_pcm_substream *substream)
355{
356 struct snd_soc_pcm_runtime *rtd = substream->private_data;
357 struct snd_soc_device *socdev = rtd->socdev;
87689d56 358 struct snd_soc_card *card = socdev->card;
db2a4165 359 struct snd_pcm_runtime *runtime = substream->runtime;
cb666e5b 360 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 361 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
362 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
363 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
364 int ret = 0;
365
366 mutex_lock(&pcm_mutex);
367
368 /* startup the audio subsystem */
6335d055
EM
369 if (cpu_dai->ops->startup) {
370 ret = cpu_dai->ops->startup(substream, cpu_dai);
db2a4165
FM
371 if (ret < 0) {
372 printk(KERN_ERR "asoc: can't open interface %s\n",
cb666e5b 373 cpu_dai->name);
db2a4165
FM
374 goto out;
375 }
376 }
377
378 if (platform->pcm_ops->open) {
379 ret = platform->pcm_ops->open(substream);
380 if (ret < 0) {
381 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
382 goto platform_err;
383 }
384 }
385
6335d055
EM
386 if (codec_dai->ops->startup) {
387 ret = codec_dai->ops->startup(substream, codec_dai);
db2a4165 388 if (ret < 0) {
cb666e5b
LG
389 printk(KERN_ERR "asoc: can't open codec %s\n",
390 codec_dai->name);
391 goto codec_dai_err;
db2a4165
FM
392 }
393 }
394
cb666e5b
LG
395 if (machine->ops && machine->ops->startup) {
396 ret = machine->ops->startup(substream);
db2a4165 397 if (ret < 0) {
cb666e5b
LG
398 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
399 goto machine_err;
db2a4165
FM
400 }
401 }
402
db2a4165
FM
403 /* Check that the codec and cpu DAI's are compatible */
404 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
405 runtime->hw.rate_min =
3ff3f64b
MB
406 max(codec_dai->playback.rate_min,
407 cpu_dai->playback.rate_min);
db2a4165 408 runtime->hw.rate_max =
3ff3f64b
MB
409 min(codec_dai->playback.rate_max,
410 cpu_dai->playback.rate_max);
db2a4165 411 runtime->hw.channels_min =
cb666e5b
LG
412 max(codec_dai->playback.channels_min,
413 cpu_dai->playback.channels_min);
db2a4165 414 runtime->hw.channels_max =
cb666e5b
LG
415 min(codec_dai->playback.channels_max,
416 cpu_dai->playback.channels_max);
417 runtime->hw.formats =
418 codec_dai->playback.formats & cpu_dai->playback.formats;
419 runtime->hw.rates =
420 codec_dai->playback.rates & cpu_dai->playback.rates;
d9ad6296
JB
421 if (codec_dai->playback.rates
422 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
423 runtime->hw.rates |= cpu_dai->playback.rates;
424 if (cpu_dai->playback.rates
425 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
426 runtime->hw.rates |= codec_dai->playback.rates;
db2a4165
FM
427 } else {
428 runtime->hw.rate_min =
3ff3f64b
MB
429 max(codec_dai->capture.rate_min,
430 cpu_dai->capture.rate_min);
db2a4165 431 runtime->hw.rate_max =
3ff3f64b
MB
432 min(codec_dai->capture.rate_max,
433 cpu_dai->capture.rate_max);
db2a4165 434 runtime->hw.channels_min =
cb666e5b
LG
435 max(codec_dai->capture.channels_min,
436 cpu_dai->capture.channels_min);
db2a4165 437 runtime->hw.channels_max =
cb666e5b
LG
438 min(codec_dai->capture.channels_max,
439 cpu_dai->capture.channels_max);
440 runtime->hw.formats =
441 codec_dai->capture.formats & cpu_dai->capture.formats;
442 runtime->hw.rates =
443 codec_dai->capture.rates & cpu_dai->capture.rates;
d9ad6296
JB
444 if (codec_dai->capture.rates
445 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
446 runtime->hw.rates |= cpu_dai->capture.rates;
447 if (cpu_dai->capture.rates
448 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
449 runtime->hw.rates |= codec_dai->capture.rates;
db2a4165
FM
450 }
451
452 snd_pcm_limit_hw_rates(runtime);
453 if (!runtime->hw.rates) {
454 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
cb666e5b 455 codec_dai->name, cpu_dai->name);
bb1c0478 456 goto config_err;
db2a4165
FM
457 }
458 if (!runtime->hw.formats) {
459 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
cb666e5b 460 codec_dai->name, cpu_dai->name);
bb1c0478 461 goto config_err;
db2a4165
FM
462 }
463 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
464 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
cb666e5b 465 codec_dai->name, cpu_dai->name);
bb1c0478 466 goto config_err;
db2a4165
FM
467 }
468
06f409d7
MB
469 /* Symmetry only applies if we've already got an active stream. */
470 if (cpu_dai->active || codec_dai->active) {
471 ret = soc_pcm_apply_symmetry(substream);
472 if (ret != 0)
bb1c0478 473 goto config_err;
06f409d7
MB
474 }
475
f24368c2
MB
476 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
477 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
478 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
479 runtime->hw.channels_max);
480 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
481 runtime->hw.rate_max);
db2a4165 482
14dc5734
JB
483 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
484 cpu_dai->playback.active++;
485 codec_dai->playback.active++;
486 } else {
487 cpu_dai->capture.active++;
488 codec_dai->capture.active++;
489 }
490 cpu_dai->active++;
491 codec_dai->active++;
6627a653 492 card->codec->active++;
db2a4165
FM
493 mutex_unlock(&pcm_mutex);
494 return 0;
495
bb1c0478 496config_err:
db2a4165
FM
497 if (machine->ops && machine->ops->shutdown)
498 machine->ops->shutdown(substream);
499
bb1c0478
JB
500machine_err:
501 if (codec_dai->ops->shutdown)
502 codec_dai->ops->shutdown(substream, codec_dai);
503
cb666e5b 504codec_dai_err:
db2a4165
FM
505 if (platform->pcm_ops->close)
506 platform->pcm_ops->close(substream);
507
508platform_err:
6335d055
EM
509 if (cpu_dai->ops->shutdown)
510 cpu_dai->ops->shutdown(substream, cpu_dai);
db2a4165
FM
511out:
512 mutex_unlock(&pcm_mutex);
513 return ret;
514}
515
516/*
3a4fa0a2 517 * Power down the audio subsystem pmdown_time msecs after close is called.
db2a4165
FM
518 * This is to ensure there are no pops or clicks in between any music tracks
519 * due to DAPM power cycling.
520 */
4484bb2e 521static void close_delayed_work(struct work_struct *work)
db2a4165 522{
6308419a
MB
523 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
524 delayed_work.work);
6627a653 525 struct snd_soc_codec *codec = card->codec;
3c4b266f 526 struct snd_soc_dai *codec_dai;
db2a4165
FM
527 int i;
528
529 mutex_lock(&pcm_mutex);
3ff3f64b 530 for (i = 0; i < codec->num_dai; i++) {
db2a4165
FM
531 codec_dai = &codec->dai[i];
532
f24368c2
MB
533 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
534 codec_dai->playback.stream_name,
535 codec_dai->playback.active ? "active" : "inactive",
536 codec_dai->pop_wait ? "yes" : "no");
db2a4165
FM
537
538 /* are we waiting on this codec DAI stream */
539 if (codec_dai->pop_wait == 1) {
db2a4165 540 codec_dai->pop_wait = 0;
0b4d221b
LG
541 snd_soc_dapm_stream_event(codec,
542 codec_dai->playback.stream_name,
db2a4165 543 SND_SOC_DAPM_STREAM_STOP);
db2a4165
FM
544 }
545 }
546 mutex_unlock(&pcm_mutex);
547}
548
549/*
550 * Called by ALSA when a PCM substream is closed. Private data can be
551 * freed here. The cpu DAI, codec DAI, machine and platform are also
552 * shutdown.
553 */
554static int soc_codec_close(struct snd_pcm_substream *substream)
555{
556 struct snd_soc_pcm_runtime *rtd = substream->private_data;
557 struct snd_soc_device *socdev = rtd->socdev;
6308419a 558 struct snd_soc_card *card = socdev->card;
cb666e5b 559 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 560 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
561 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
562 struct snd_soc_dai *codec_dai = machine->codec_dai;
6627a653 563 struct snd_soc_codec *codec = card->codec;
db2a4165
FM
564
565 mutex_lock(&pcm_mutex);
566
14dc5734
JB
567 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
568 cpu_dai->playback.active--;
569 codec_dai->playback.active--;
570 } else {
571 cpu_dai->capture.active--;
572 codec_dai->capture.active--;
db2a4165 573 }
14dc5734
JB
574
575 cpu_dai->active--;
576 codec_dai->active--;
db2a4165
FM
577 codec->active--;
578
6010b2da
MB
579 /* Muting the DAC suppresses artifacts caused during digital
580 * shutdown, for example from stopping clocks.
581 */
582 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
583 snd_soc_dai_digital_mute(codec_dai, 1);
584
6335d055
EM
585 if (cpu_dai->ops->shutdown)
586 cpu_dai->ops->shutdown(substream, cpu_dai);
db2a4165 587
6335d055
EM
588 if (codec_dai->ops->shutdown)
589 codec_dai->ops->shutdown(substream, codec_dai);
db2a4165
FM
590
591 if (machine->ops && machine->ops->shutdown)
592 machine->ops->shutdown(substream);
593
594 if (platform->pcm_ops->close)
595 platform->pcm_ops->close(substream);
db2a4165
FM
596
597 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
598 /* start delayed pop wq here for playback streams */
cb666e5b 599 codec_dai->pop_wait = 1;
6308419a 600 schedule_delayed_work(&card->delayed_work,
96dd3622 601 msecs_to_jiffies(card->pmdown_time));
db2a4165
FM
602 } else {
603 /* capture streams can be powered down now */
cb666e5b 604 snd_soc_dapm_stream_event(codec,
0b4d221b
LG
605 codec_dai->capture.stream_name,
606 SND_SOC_DAPM_STREAM_STOP);
db2a4165
FM
607 }
608
609 mutex_unlock(&pcm_mutex);
610 return 0;
611}
612
613/*
614 * Called by ALSA when the PCM substream is prepared, can set format, sample
615 * rate, etc. This function is non atomic and can be called multiple times,
616 * it can refer to the runtime info.
617 */
618static int soc_pcm_prepare(struct snd_pcm_substream *substream)
619{
620 struct snd_soc_pcm_runtime *rtd = substream->private_data;
621 struct snd_soc_device *socdev = rtd->socdev;
6308419a 622 struct snd_soc_card *card = socdev->card;
cb666e5b 623 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 624 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
625 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
626 struct snd_soc_dai *codec_dai = machine->codec_dai;
6627a653 627 struct snd_soc_codec *codec = card->codec;
db2a4165
FM
628 int ret = 0;
629
630 mutex_lock(&pcm_mutex);
cb666e5b
LG
631
632 if (machine->ops && machine->ops->prepare) {
633 ret = machine->ops->prepare(substream);
634 if (ret < 0) {
635 printk(KERN_ERR "asoc: machine prepare error\n");
636 goto out;
637 }
638 }
639
db2a4165
FM
640 if (platform->pcm_ops->prepare) {
641 ret = platform->pcm_ops->prepare(substream);
a71a468a
LG
642 if (ret < 0) {
643 printk(KERN_ERR "asoc: platform prepare error\n");
db2a4165 644 goto out;
a71a468a 645 }
db2a4165
FM
646 }
647
6335d055
EM
648 if (codec_dai->ops->prepare) {
649 ret = codec_dai->ops->prepare(substream, codec_dai);
a71a468a
LG
650 if (ret < 0) {
651 printk(KERN_ERR "asoc: codec DAI prepare error\n");
db2a4165 652 goto out;
a71a468a 653 }
db2a4165
FM
654 }
655
6335d055
EM
656 if (cpu_dai->ops->prepare) {
657 ret = cpu_dai->ops->prepare(substream, cpu_dai);
cb666e5b
LG
658 if (ret < 0) {
659 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
660 goto out;
661 }
662 }
db2a4165 663
d45f6219
MB
664 /* cancel any delayed stream shutdown that is pending */
665 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
666 codec_dai->pop_wait) {
667 codec_dai->pop_wait = 0;
6308419a 668 cancel_delayed_work(&card->delayed_work);
d45f6219 669 }
db2a4165 670
452c5eaa
MB
671 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
672 snd_soc_dapm_stream_event(codec,
673 codec_dai->playback.stream_name,
674 SND_SOC_DAPM_STREAM_START);
675 else
676 snd_soc_dapm_stream_event(codec,
677 codec_dai->capture.stream_name,
678 SND_SOC_DAPM_STREAM_START);
8c6529db 679
452c5eaa 680 snd_soc_dai_digital_mute(codec_dai, 0);
db2a4165
FM
681
682out:
683 mutex_unlock(&pcm_mutex);
684 return ret;
685}
686
687/*
688 * Called by ALSA when the hardware params are set by application. This
689 * function can also be called multiple times and can allocate buffers
690 * (using snd_pcm_lib_* ). It's non-atomic.
691 */
692static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
693 struct snd_pcm_hw_params *params)
694{
695 struct snd_soc_pcm_runtime *rtd = substream->private_data;
696 struct snd_soc_device *socdev = rtd->socdev;
cb666e5b 697 struct snd_soc_dai_link *machine = rtd->dai;
87689d56
MB
698 struct snd_soc_card *card = socdev->card;
699 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
700 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
701 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
702 int ret = 0;
703
704 mutex_lock(&pcm_mutex);
705
cb666e5b
LG
706 if (machine->ops && machine->ops->hw_params) {
707 ret = machine->ops->hw_params(substream, params);
708 if (ret < 0) {
709 printk(KERN_ERR "asoc: machine hw_params failed\n");
db2a4165 710 goto out;
cb666e5b 711 }
db2a4165
FM
712 }
713
6335d055
EM
714 if (codec_dai->ops->hw_params) {
715 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
db2a4165
FM
716 if (ret < 0) {
717 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
cb666e5b
LG
718 codec_dai->name);
719 goto codec_err;
db2a4165
FM
720 }
721 }
722
6335d055
EM
723 if (cpu_dai->ops->hw_params) {
724 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
db2a4165 725 if (ret < 0) {
3ff3f64b 726 printk(KERN_ERR "asoc: interface %s hw params failed\n",
cb666e5b 727 cpu_dai->name);
db2a4165
FM
728 goto interface_err;
729 }
730 }
731
732 if (platform->pcm_ops->hw_params) {
733 ret = platform->pcm_ops->hw_params(substream, params);
734 if (ret < 0) {
3ff3f64b 735 printk(KERN_ERR "asoc: platform %s hw params failed\n",
db2a4165
FM
736 platform->name);
737 goto platform_err;
738 }
739 }
740
06f409d7
MB
741 machine->rate = params_rate(params);
742
db2a4165
FM
743out:
744 mutex_unlock(&pcm_mutex);
745 return ret;
746
db2a4165 747platform_err:
6335d055
EM
748 if (cpu_dai->ops->hw_free)
749 cpu_dai->ops->hw_free(substream, cpu_dai);
db2a4165
FM
750
751interface_err:
6335d055
EM
752 if (codec_dai->ops->hw_free)
753 codec_dai->ops->hw_free(substream, codec_dai);
cb666e5b
LG
754
755codec_err:
3ff3f64b 756 if (machine->ops && machine->ops->hw_free)
cb666e5b 757 machine->ops->hw_free(substream);
db2a4165
FM
758
759 mutex_unlock(&pcm_mutex);
760 return ret;
761}
762
763/*
764 * Free's resources allocated by hw_params, can be called multiple times
765 */
766static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
767{
768 struct snd_soc_pcm_runtime *rtd = substream->private_data;
769 struct snd_soc_device *socdev = rtd->socdev;
cb666e5b 770 struct snd_soc_dai_link *machine = rtd->dai;
87689d56
MB
771 struct snd_soc_card *card = socdev->card;
772 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
773 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
774 struct snd_soc_dai *codec_dai = machine->codec_dai;
6627a653 775 struct snd_soc_codec *codec = card->codec;
db2a4165
FM
776
777 mutex_lock(&pcm_mutex);
778
779 /* apply codec digital mute */
8c6529db
LG
780 if (!codec->active)
781 snd_soc_dai_digital_mute(codec_dai, 1);
db2a4165
FM
782
783 /* free any machine hw params */
784 if (machine->ops && machine->ops->hw_free)
785 machine->ops->hw_free(substream);
786
787 /* free any DMA resources */
788 if (platform->pcm_ops->hw_free)
789 platform->pcm_ops->hw_free(substream);
790
791 /* now free hw params for the DAI's */
6335d055
EM
792 if (codec_dai->ops->hw_free)
793 codec_dai->ops->hw_free(substream, codec_dai);
db2a4165 794
6335d055
EM
795 if (cpu_dai->ops->hw_free)
796 cpu_dai->ops->hw_free(substream, cpu_dai);
db2a4165
FM
797
798 mutex_unlock(&pcm_mutex);
799 return 0;
800}
801
802static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
803{
804 struct snd_soc_pcm_runtime *rtd = substream->private_data;
805 struct snd_soc_device *socdev = rtd->socdev;
87689d56 806 struct snd_soc_card *card= socdev->card;
cb666e5b 807 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 808 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
809 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
810 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
811 int ret;
812
6335d055
EM
813 if (codec_dai->ops->trigger) {
814 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
db2a4165
FM
815 if (ret < 0)
816 return ret;
817 }
818
819 if (platform->pcm_ops->trigger) {
820 ret = platform->pcm_ops->trigger(substream, cmd);
821 if (ret < 0)
822 return ret;
823 }
824
6335d055
EM
825 if (cpu_dai->ops->trigger) {
826 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
db2a4165
FM
827 if (ret < 0)
828 return ret;
829 }
830 return 0;
831}
832
377b6f62
PU
833/*
834 * soc level wrapper for pointer callback
258020d0
PU
835 * If cpu_dai, codec_dai, platform driver has the delay callback, than
836 * the runtime->delay will be updated accordingly.
377b6f62
PU
837 */
838static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
839{
840 struct snd_soc_pcm_runtime *rtd = substream->private_data;
841 struct snd_soc_device *socdev = rtd->socdev;
842 struct snd_soc_card *card = socdev->card;
843 struct snd_soc_platform *platform = card->platform;
258020d0
PU
844 struct snd_soc_dai_link *machine = rtd->dai;
845 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
846 struct snd_soc_dai *codec_dai = machine->codec_dai;
847 struct snd_pcm_runtime *runtime = substream->runtime;
377b6f62 848 snd_pcm_uframes_t offset = 0;
258020d0 849 snd_pcm_sframes_t delay = 0;
377b6f62
PU
850
851 if (platform->pcm_ops->pointer)
852 offset = platform->pcm_ops->pointer(substream);
853
258020d0
PU
854 if (cpu_dai->ops->delay)
855 delay += cpu_dai->ops->delay(substream, cpu_dai);
856
857 if (codec_dai->ops->delay)
858 delay += codec_dai->ops->delay(substream, codec_dai);
859
860 if (platform->delay)
861 delay += platform->delay(substream, codec_dai);
862
863 runtime->delay = delay;
864
377b6f62
PU
865 return offset;
866}
867
db2a4165
FM
868/* ASoC PCM operations */
869static struct snd_pcm_ops soc_pcm_ops = {
870 .open = soc_pcm_open,
871 .close = soc_codec_close,
872 .hw_params = soc_pcm_hw_params,
873 .hw_free = soc_pcm_hw_free,
874 .prepare = soc_pcm_prepare,
875 .trigger = soc_pcm_trigger,
377b6f62 876 .pointer = soc_pcm_pointer,
db2a4165
FM
877};
878
879#ifdef CONFIG_PM
880/* powers down audio subsystem for suspend */
416356fc 881static int soc_suspend(struct device *dev)
db2a4165 882{
416356fc 883 struct platform_device *pdev = to_platform_device(dev);
3ff3f64b 884 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
87506549 885 struct snd_soc_card *card = socdev->card;
87689d56 886 struct snd_soc_platform *platform = card->platform;
3ff3f64b 887 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
6627a653 888 struct snd_soc_codec *codec = card->codec;
db2a4165
FM
889 int i;
890
e3509ff0
DM
891 /* If the initialization of this soc device failed, there is no codec
892 * associated with it. Just bail out in this case.
893 */
894 if (!codec)
895 return 0;
896
6ed25978
AG
897 /* Due to the resume being scheduled into a workqueue we could
898 * suspend before that's finished - wait for it to complete.
899 */
900 snd_power_lock(codec->card);
901 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
902 snd_power_unlock(codec->card);
903
904 /* we're going to block userspace touching us until resume completes */
905 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
906
db2a4165 907 /* mute any active DAC's */
dee89c4d
MB
908 for (i = 0; i < card->num_links; i++) {
909 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
3efab7dc
MB
910
911 if (card->dai_link[i].ignore_suspend)
912 continue;
913
6335d055
EM
914 if (dai->ops->digital_mute && dai->playback.active)
915 dai->ops->digital_mute(dai, 1);
db2a4165
FM
916 }
917
4ccab3e7 918 /* suspend all pcms */
3efab7dc
MB
919 for (i = 0; i < card->num_links; i++) {
920 if (card->dai_link[i].ignore_suspend)
921 continue;
922
87506549 923 snd_pcm_suspend_all(card->dai_link[i].pcm);
3efab7dc 924 }
4ccab3e7 925
87506549 926 if (card->suspend_pre)
416356fc 927 card->suspend_pre(pdev, PMSG_SUSPEND);
db2a4165 928
87506549
MB
929 for (i = 0; i < card->num_links; i++) {
930 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3efab7dc
MB
931
932 if (card->dai_link[i].ignore_suspend)
933 continue;
934
3ba9e10a 935 if (cpu_dai->suspend && !cpu_dai->ac97_control)
dc7d7b83 936 cpu_dai->suspend(cpu_dai);
db2a4165 937 if (platform->suspend)
d273ebe7 938 platform->suspend(&card->dai_link[i]);
db2a4165
FM
939 }
940
941 /* close any waiting streams and save state */
6308419a 942 run_delayed_work(&card->delayed_work);
0be9898a 943 codec->suspend_bias_level = codec->bias_level;
db2a4165 944
3ff3f64b 945 for (i = 0; i < codec->num_dai; i++) {
db2a4165 946 char *stream = codec->dai[i].playback.stream_name;
3efab7dc
MB
947
948 if (card->dai_link[i].ignore_suspend)
949 continue;
950
db2a4165
FM
951 if (stream != NULL)
952 snd_soc_dapm_stream_event(codec, stream,
953 SND_SOC_DAPM_STREAM_SUSPEND);
954 stream = codec->dai[i].capture.stream_name;
955 if (stream != NULL)
956 snd_soc_dapm_stream_event(codec, stream,
957 SND_SOC_DAPM_STREAM_SUSPEND);
958 }
959
1547aba9
MB
960 /* If there are paths active then the CODEC will be held with
961 * bias _ON and should not be suspended. */
962 if (codec_dev->suspend) {
963 switch (codec->bias_level) {
964 case SND_SOC_BIAS_STANDBY:
965 case SND_SOC_BIAS_OFF:
966 codec_dev->suspend(pdev, PMSG_SUSPEND);
967 break;
968 default:
969 dev_dbg(socdev->dev, "CODEC is on over suspend\n");
970 break;
971 }
972 }
db2a4165 973
87506549
MB
974 for (i = 0; i < card->num_links; i++) {
975 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3efab7dc
MB
976
977 if (card->dai_link[i].ignore_suspend)
978 continue;
979
3ba9e10a 980 if (cpu_dai->suspend && cpu_dai->ac97_control)
dc7d7b83 981 cpu_dai->suspend(cpu_dai);
db2a4165
FM
982 }
983
87506549 984 if (card->suspend_post)
416356fc 985 card->suspend_post(pdev, PMSG_SUSPEND);
db2a4165
FM
986
987 return 0;
988}
989
6ed25978
AG
990/* deferred resume work, so resume can complete before we finished
991 * setting our codec back up, which can be very slow on I2C
992 */
993static void soc_resume_deferred(struct work_struct *work)
db2a4165 994{
6308419a
MB
995 struct snd_soc_card *card = container_of(work,
996 struct snd_soc_card,
997 deferred_resume_work);
998 struct snd_soc_device *socdev = card->socdev;
87689d56 999 struct snd_soc_platform *platform = card->platform;
3ff3f64b 1000 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
6627a653 1001 struct snd_soc_codec *codec = card->codec;
6ed25978 1002 struct platform_device *pdev = to_platform_device(socdev->dev);
db2a4165
FM
1003 int i;
1004
6ed25978
AG
1005 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1006 * so userspace apps are blocked from touching us
1007 */
1008
fde22f27 1009 dev_dbg(socdev->dev, "starting resume work\n");
6ed25978 1010
9949788b
MB
1011 /* Bring us up into D2 so that DAPM starts enabling things */
1012 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D2);
1013
87506549
MB
1014 if (card->resume_pre)
1015 card->resume_pre(pdev);
db2a4165 1016
87506549
MB
1017 for (i = 0; i < card->num_links; i++) {
1018 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3efab7dc
MB
1019
1020 if (card->dai_link[i].ignore_suspend)
1021 continue;
1022
3ba9e10a 1023 if (cpu_dai->resume && cpu_dai->ac97_control)
dc7d7b83 1024 cpu_dai->resume(cpu_dai);
db2a4165
FM
1025 }
1026
1547aba9
MB
1027 /* If the CODEC was idle over suspend then it will have been
1028 * left with bias OFF or STANDBY and suspended so we must now
1029 * resume. Otherwise the suspend was suppressed.
1030 */
1031 if (codec_dev->resume) {
1032 switch (codec->bias_level) {
1033 case SND_SOC_BIAS_STANDBY:
1034 case SND_SOC_BIAS_OFF:
1035 codec_dev->resume(pdev);
1036 break;
1037 default:
1038 dev_dbg(socdev->dev, "CODEC was on over suspend\n");
1039 break;
1040 }
1041 }
db2a4165 1042
3ff3f64b
MB
1043 for (i = 0; i < codec->num_dai; i++) {
1044 char *stream = codec->dai[i].playback.stream_name;
3efab7dc
MB
1045
1046 if (card->dai_link[i].ignore_suspend)
1047 continue;
1048
db2a4165
FM
1049 if (stream != NULL)
1050 snd_soc_dapm_stream_event(codec, stream,
1051 SND_SOC_DAPM_STREAM_RESUME);
1052 stream = codec->dai[i].capture.stream_name;
1053 if (stream != NULL)
1054 snd_soc_dapm_stream_event(codec, stream,
1055 SND_SOC_DAPM_STREAM_RESUME);
1056 }
1057
3ff3f64b 1058 /* unmute any active DACs */
dee89c4d
MB
1059 for (i = 0; i < card->num_links; i++) {
1060 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
3efab7dc
MB
1061
1062 if (card->dai_link[i].ignore_suspend)
1063 continue;
1064
6335d055
EM
1065 if (dai->ops->digital_mute && dai->playback.active)
1066 dai->ops->digital_mute(dai, 0);
db2a4165
FM
1067 }
1068
87506549
MB
1069 for (i = 0; i < card->num_links; i++) {
1070 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3efab7dc
MB
1071
1072 if (card->dai_link[i].ignore_suspend)
1073 continue;
1074
3ba9e10a 1075 if (cpu_dai->resume && !cpu_dai->ac97_control)
dc7d7b83 1076 cpu_dai->resume(cpu_dai);
db2a4165 1077 if (platform->resume)
d273ebe7 1078 platform->resume(&card->dai_link[i]);
db2a4165
FM
1079 }
1080
87506549
MB
1081 if (card->resume_post)
1082 card->resume_post(pdev);
db2a4165 1083
fde22f27 1084 dev_dbg(socdev->dev, "resume work completed\n");
6ed25978
AG
1085
1086 /* userspace can access us now we are back as we were before */
1087 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
1088}
1089
1090/* powers up audio subsystem after a suspend */
416356fc 1091static int soc_resume(struct device *dev)
6ed25978 1092{
416356fc 1093 struct platform_device *pdev = to_platform_device(dev);
6ed25978 1094 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
6308419a 1095 struct snd_soc_card *card = socdev->card;
64ab9baa 1096 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
6ed25978 1097
b9dd94a8
PU
1098 /* If the initialization of this soc device failed, there is no codec
1099 * associated with it. Just bail out in this case.
1100 */
1101 if (!card->codec)
1102 return 0;
1103
64ab9baa
MB
1104 /* AC97 devices might have other drivers hanging off them so
1105 * need to resume immediately. Other drivers don't have that
1106 * problem and may take a substantial amount of time to resume
1107 * due to I/O costs and anti-pop so handle them out of line.
1108 */
1109 if (cpu_dai->ac97_control) {
1110 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
1111 soc_resume_deferred(&card->deferred_resume_work);
1112 } else {
1113 dev_dbg(socdev->dev, "Scheduling resume work\n");
1114 if (!schedule_work(&card->deferred_resume_work))
1115 dev_err(socdev->dev, "resume work item may be lost\n");
1116 }
6ed25978 1117
db2a4165
FM
1118 return 0;
1119}
db2a4165
FM
1120#else
1121#define soc_suspend NULL
1122#define soc_resume NULL
1123#endif
1124
02a06d30
BS
1125static struct snd_soc_dai_ops null_dai_ops = {
1126};
1127
435c5e25 1128static void snd_soc_instantiate_card(struct snd_soc_card *card)
db2a4165 1129{
435c5e25
MB
1130 struct platform_device *pdev = container_of(card->dev,
1131 struct platform_device,
1132 dev);
1133 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
fe3e78e0 1134 struct snd_soc_codec *codec;
435c5e25
MB
1135 struct snd_soc_platform *platform;
1136 struct snd_soc_dai *dai;
6b05eda6 1137 int i, found, ret, ac97;
435c5e25
MB
1138
1139 if (card->instantiated)
1140 return;
1141
1142 found = 0;
1143 list_for_each_entry(platform, &platform_list, list)
1144 if (card->platform == platform) {
1145 found = 1;
1146 break;
1147 }
1148 if (!found) {
1149 dev_dbg(card->dev, "Platform %s not registered\n",
1150 card->platform->name);
1151 return;
1152 }
6308419a 1153
6b05eda6 1154 ac97 = 0;
435c5e25
MB
1155 for (i = 0; i < card->num_links; i++) {
1156 found = 0;
1157 list_for_each_entry(dai, &dai_list, list)
1158 if (card->dai_link[i].cpu_dai == dai) {
1159 found = 1;
1160 break;
1161 }
1162 if (!found) {
1163 dev_dbg(card->dev, "DAI %s not registered\n",
1164 card->dai_link[i].cpu_dai->name);
1165 return;
1166 }
6b05eda6
MB
1167
1168 if (card->dai_link[i].cpu_dai->ac97_control)
1169 ac97 = 1;
c5af3a2e
MB
1170 }
1171
02a06d30
BS
1172 for (i = 0; i < card->num_links; i++) {
1173 if (!card->dai_link[i].codec_dai->ops)
1174 card->dai_link[i].codec_dai->ops = &null_dai_ops;
1175 }
1176
6b05eda6
MB
1177 /* If we have AC97 in the system then don't wait for the
1178 * codec. This will need revisiting if we have to handle
1179 * systems with mixed AC97 and non-AC97 parts. Only check for
1180 * DAIs currently; we can't do this per link since some AC97
1181 * codecs have non-AC97 DAIs.
1182 */
1183 if (!ac97)
1184 for (i = 0; i < card->num_links; i++) {
1185 found = 0;
1186 list_for_each_entry(dai, &dai_list, list)
1187 if (card->dai_link[i].codec_dai == dai) {
1188 found = 1;
1189 break;
1190 }
1191 if (!found) {
1192 dev_dbg(card->dev, "DAI %s not registered\n",
1193 card->dai_link[i].codec_dai->name);
1194 return;
1195 }
1196 }
1197
435c5e25
MB
1198 /* Note that we do not current check for codec components */
1199
1200 dev_dbg(card->dev, "All components present, instantiating\n");
1201
1202 /* Found everything, bring it up */
96dd3622
MB
1203 card->pmdown_time = pmdown_time;
1204
87506549
MB
1205 if (card->probe) {
1206 ret = card->probe(pdev);
3ff3f64b 1207 if (ret < 0)
435c5e25 1208 return;
db2a4165
FM
1209 }
1210
87506549
MB
1211 for (i = 0; i < card->num_links; i++) {
1212 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 1213 if (cpu_dai->probe) {
bdb92876 1214 ret = cpu_dai->probe(pdev, cpu_dai);
3ff3f64b 1215 if (ret < 0)
db2a4165
FM
1216 goto cpu_dai_err;
1217 }
1218 }
1219
1220 if (codec_dev->probe) {
1221 ret = codec_dev->probe(pdev);
3ff3f64b 1222 if (ret < 0)
db2a4165
FM
1223 goto cpu_dai_err;
1224 }
fe3e78e0 1225 codec = card->codec;
db2a4165
FM
1226
1227 if (platform->probe) {
1228 ret = platform->probe(pdev);
3ff3f64b 1229 if (ret < 0)
db2a4165
FM
1230 goto platform_err;
1231 }
1232
1233 /* DAPM stream work */
6308419a 1234 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
1301a964 1235#ifdef CONFIG_PM
6ed25978 1236 /* deferred resume work */
6308419a 1237 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1301a964 1238#endif
6ed25978 1239
fe3e78e0
MB
1240 for (i = 0; i < card->num_links; i++) {
1241 if (card->dai_link[i].init) {
1242 ret = card->dai_link[i].init(codec);
1243 if (ret < 0) {
1244 printk(KERN_ERR "asoc: failed to init %s\n",
1245 card->dai_link[i].stream_name);
1246 continue;
1247 }
1248 }
f7732053 1249 if (card->dai_link[i].codec_dai->ac97_control)
fe3e78e0 1250 ac97 = 1;
fe3e78e0
MB
1251 }
1252
1253 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1254 "%s", card->name);
1255 snprintf(codec->card->longname, sizeof(codec->card->longname),
1256 "%s (%s)", card->name, codec->name);
1257
1258 /* Make sure all DAPM widgets are instantiated */
1259 snd_soc_dapm_new_widgets(codec);
1260
1261 ret = snd_card_register(codec->card);
1262 if (ret < 0) {
1263 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1264 codec->name);
1265 goto card_err;
1266 }
1267
1268 mutex_lock(&codec->mutex);
1269#ifdef CONFIG_SND_SOC_AC97_BUS
1270 /* Only instantiate AC97 if not already done by the adaptor
1271 * for the generic AC97 subsystem.
1272 */
1273 if (ac97 && strcmp(codec->name, "AC97") != 0) {
1274 ret = soc_ac97_dev_register(codec);
1275 if (ret < 0) {
1276 printk(KERN_ERR "asoc: AC97 device register failed\n");
1277 snd_card_free(codec->card);
1278 mutex_unlock(&codec->mutex);
1279 goto card_err;
1280 }
1281 }
1282#endif
1283
1284 ret = snd_soc_dapm_sys_add(card->socdev->dev);
1285 if (ret < 0)
1286 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1287
dbe21408
MB
1288 ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1289 if (ret < 0)
1290 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1291
fe3e78e0
MB
1292 ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1293 if (ret < 0)
1294 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1295
1296 soc_init_codec_debugfs(codec);
1297 mutex_unlock(&codec->mutex);
1298
435c5e25
MB
1299 card->instantiated = 1;
1300
1301 return;
db2a4165 1302
fe3e78e0
MB
1303card_err:
1304 if (platform->remove)
1305 platform->remove(pdev);
1306
db2a4165
FM
1307platform_err:
1308 if (codec_dev->remove)
1309 codec_dev->remove(pdev);
1310
1311cpu_dai_err:
18b9b3d9 1312 for (i--; i >= 0; i--) {
87506549 1313 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 1314 if (cpu_dai->remove)
bdb92876 1315 cpu_dai->remove(pdev, cpu_dai);
db2a4165
FM
1316 }
1317
87506549
MB
1318 if (card->remove)
1319 card->remove(pdev);
435c5e25 1320}
db2a4165 1321
435c5e25 1322/*
421f91d2 1323 * Attempt to initialise any uninitialised cards. Must be called with
435c5e25
MB
1324 * client_mutex.
1325 */
1326static void snd_soc_instantiate_cards(void)
1327{
1328 struct snd_soc_card *card;
1329 list_for_each_entry(card, &card_list, list)
1330 snd_soc_instantiate_card(card);
1331}
1332
1333/* probes a new socdev */
1334static int soc_probe(struct platform_device *pdev)
1335{
1336 int ret = 0;
1337 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1338 struct snd_soc_card *card = socdev->card;
1339
1340 /* Bodge while we push things out of socdev */
1341 card->socdev = socdev;
1342
1343 /* Bodge while we unpick instantiation */
1344 card->dev = &pdev->dev;
1345 ret = snd_soc_register_card(card);
1346 if (ret != 0) {
1347 dev_err(&pdev->dev, "Failed to register card\n");
1348 return ret;
1349 }
1350
1351 return 0;
db2a4165
FM
1352}
1353
1354/* removes a socdev */
1355static int soc_remove(struct platform_device *pdev)
1356{
1357 int i;
1358 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
87506549 1359 struct snd_soc_card *card = socdev->card;
87689d56 1360 struct snd_soc_platform *platform = card->platform;
db2a4165
FM
1361 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1362
b2dfa62c
GL
1363 if (card->instantiated) {
1364 run_delayed_work(&card->delayed_work);
914dc182 1365
b2dfa62c
GL
1366 if (platform->remove)
1367 platform->remove(pdev);
965ac42c 1368
b2dfa62c
GL
1369 if (codec_dev->remove)
1370 codec_dev->remove(pdev);
db2a4165 1371
b2dfa62c
GL
1372 for (i = 0; i < card->num_links; i++) {
1373 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1374 if (cpu_dai->remove)
1375 cpu_dai->remove(pdev, cpu_dai);
1376 }
db2a4165 1377
b2dfa62c
GL
1378 if (card->remove)
1379 card->remove(pdev);
db2a4165
FM
1380 }
1381
c5af3a2e
MB
1382 snd_soc_unregister_card(card);
1383
db2a4165
FM
1384 return 0;
1385}
1386
416356fc 1387static int soc_poweroff(struct device *dev)
51737470 1388{
416356fc 1389 struct platform_device *pdev = to_platform_device(dev);
51737470
MB
1390 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1391 struct snd_soc_card *card = socdev->card;
1392
1393 if (!card->instantiated)
416356fc 1394 return 0;
51737470
MB
1395
1396 /* Flush out pmdown_time work - we actually do want to run it
1397 * now, we're shutting down so no imminent restart. */
1398 run_delayed_work(&card->delayed_work);
1399
1400 snd_soc_dapm_shutdown(socdev);
416356fc
MB
1401
1402 return 0;
51737470
MB
1403}
1404
47145210 1405static const struct dev_pm_ops soc_pm_ops = {
416356fc
MB
1406 .suspend = soc_suspend,
1407 .resume = soc_resume,
1408 .poweroff = soc_poweroff,
1409};
1410
db2a4165
FM
1411/* ASoC platform driver */
1412static struct platform_driver soc_driver = {
1413 .driver = {
1414 .name = "soc-audio",
8b45a209 1415 .owner = THIS_MODULE,
416356fc 1416 .pm = &soc_pm_ops,
db2a4165
FM
1417 },
1418 .probe = soc_probe,
1419 .remove = soc_remove,
db2a4165
FM
1420};
1421
1422/* create a new pcm */
1423static int soc_new_pcm(struct snd_soc_device *socdev,
1424 struct snd_soc_dai_link *dai_link, int num)
1425{
87689d56 1426 struct snd_soc_card *card = socdev->card;
6627a653 1427 struct snd_soc_codec *codec = card->codec;
87689d56 1428 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
1429 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1430 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
db2a4165
FM
1431 struct snd_soc_pcm_runtime *rtd;
1432 struct snd_pcm *pcm;
1433 char new_name[64];
1434 int ret = 0, playback = 0, capture = 0;
1435
1436 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1437 if (rtd == NULL)
1438 return -ENOMEM;
cb666e5b
LG
1439
1440 rtd->dai = dai_link;
db2a4165 1441 rtd->socdev = socdev;
6627a653 1442 codec_dai->codec = card->codec;
db2a4165
FM
1443
1444 /* check client and interface hw capabilities */
40ca1142
MB
1445 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1446 dai_link->stream_name, codec_dai->name, num);
db2a4165
FM
1447
1448 if (codec_dai->playback.channels_min)
1449 playback = 1;
1450 if (codec_dai->capture.channels_min)
1451 capture = 1;
1452
1453 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1454 capture, &pcm);
1455 if (ret < 0) {
3ff3f64b
MB
1456 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1457 codec->name);
db2a4165
FM
1458 kfree(rtd);
1459 return ret;
1460 }
1461
4ccab3e7 1462 dai_link->pcm = pcm;
db2a4165 1463 pcm->private_data = rtd;
87689d56 1464 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
87689d56
MB
1465 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1466 soc_pcm_ops.copy = platform->pcm_ops->copy;
1467 soc_pcm_ops.silence = platform->pcm_ops->silence;
1468 soc_pcm_ops.ack = platform->pcm_ops->ack;
1469 soc_pcm_ops.page = platform->pcm_ops->page;
db2a4165
FM
1470
1471 if (playback)
1472 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1473
1474 if (capture)
1475 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1476
87689d56 1477 ret = platform->pcm_new(codec->card, codec_dai, pcm);
db2a4165
FM
1478 if (ret < 0) {
1479 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1480 kfree(rtd);
1481 return ret;
1482 }
1483
87689d56 1484 pcm->private_free = platform->pcm_free;
db2a4165
FM
1485 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1486 cpu_dai->name);
1487 return ret;
1488}
1489
096e49d5
MB
1490/**
1491 * snd_soc_codec_volatile_register: Report if a register is volatile.
1492 *
1493 * @codec: CODEC to query.
1494 * @reg: Register to query.
1495 *
1496 * Boolean function indiciating if a CODEC register is volatile.
1497 */
1498int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1499{
1500 if (codec->volatile_register)
1501 return codec->volatile_register(reg);
1502 else
1503 return 0;
1504}
1505EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1506
db2a4165
FM
1507/**
1508 * snd_soc_new_ac97_codec - initailise AC97 device
1509 * @codec: audio codec
1510 * @ops: AC97 bus operations
1511 * @num: AC97 codec number
1512 *
1513 * Initialises AC97 codec resources for use by ad-hoc devices only.
1514 */
1515int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1516 struct snd_ac97_bus_ops *ops, int num)
1517{
1518 mutex_lock(&codec->mutex);
1519
1520 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1521 if (codec->ac97 == NULL) {
1522 mutex_unlock(&codec->mutex);
1523 return -ENOMEM;
1524 }
1525
1526 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1527 if (codec->ac97->bus == NULL) {
1528 kfree(codec->ac97);
1529 codec->ac97 = NULL;
1530 mutex_unlock(&codec->mutex);
1531 return -ENOMEM;
1532 }
1533
1534 codec->ac97->bus->ops = ops;
1535 codec->ac97->num = num;
2718625f 1536 codec->dev = &codec->ac97->dev;
db2a4165
FM
1537 mutex_unlock(&codec->mutex);
1538 return 0;
1539}
1540EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1541
1542/**
1543 * snd_soc_free_ac97_codec - free AC97 codec device
1544 * @codec: audio codec
1545 *
1546 * Frees AC97 codec device resources.
1547 */
1548void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1549{
1550 mutex_lock(&codec->mutex);
1551 kfree(codec->ac97->bus);
1552 kfree(codec->ac97);
1553 codec->ac97 = NULL;
1554 mutex_unlock(&codec->mutex);
1555}
1556EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1557
1558/**
1559 * snd_soc_update_bits - update codec register bits
1560 * @codec: audio codec
1561 * @reg: codec register
1562 * @mask: register mask
1563 * @value: new value
1564 *
1565 * Writes new register value.
1566 *
1567 * Returns 1 for change else 0.
1568 */
1569int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
46f5822f 1570 unsigned int mask, unsigned int value)
db2a4165
FM
1571{
1572 int change;
46f5822f 1573 unsigned int old, new;
db2a4165 1574
db2a4165
FM
1575 old = snd_soc_read(codec, reg);
1576 new = (old & ~mask) | value;
1577 change = old != new;
1578 if (change)
1579 snd_soc_write(codec, reg, new);
1580
db2a4165
FM
1581 return change;
1582}
1583EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1584
6c508c62
EN
1585/**
1586 * snd_soc_update_bits_locked - update codec register bits
1587 * @codec: audio codec
1588 * @reg: codec register
1589 * @mask: register mask
1590 * @value: new value
1591 *
1592 * Writes new register value, and takes the codec mutex.
1593 *
1594 * Returns 1 for change else 0.
1595 */
dd1b3d53
MB
1596int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1597 unsigned short reg, unsigned int mask,
1598 unsigned int value)
6c508c62
EN
1599{
1600 int change;
1601
1602 mutex_lock(&codec->mutex);
1603 change = snd_soc_update_bits(codec, reg, mask, value);
1604 mutex_unlock(&codec->mutex);
1605
1606 return change;
1607}
dd1b3d53 1608EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
6c508c62 1609
db2a4165
FM
1610/**
1611 * snd_soc_test_bits - test register for change
1612 * @codec: audio codec
1613 * @reg: codec register
1614 * @mask: register mask
1615 * @value: new value
1616 *
1617 * Tests a register with a new value and checks if the new value is
1618 * different from the old value.
1619 *
1620 * Returns 1 for change else 0.
1621 */
1622int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
46f5822f 1623 unsigned int mask, unsigned int value)
db2a4165
FM
1624{
1625 int change;
46f5822f 1626 unsigned int old, new;
db2a4165 1627
db2a4165
FM
1628 old = snd_soc_read(codec, reg);
1629 new = (old & ~mask) | value;
1630 change = old != new;
db2a4165
FM
1631
1632 return change;
1633}
1634EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1635
db2a4165
FM
1636/**
1637 * snd_soc_new_pcms - create new sound card and pcms
1638 * @socdev: the SoC audio device
ac11a2b3
MB
1639 * @idx: ALSA card index
1640 * @xid: card identification
db2a4165
FM
1641 *
1642 * Create a new sound card based upon the codec and interface pcms.
1643 *
1644 * Returns 0 for success, else error.
1645 */
bc7320c5 1646int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
db2a4165 1647{
87506549 1648 struct snd_soc_card *card = socdev->card;
6627a653 1649 struct snd_soc_codec *codec = card->codec;
bd7dd77c 1650 int ret, i;
db2a4165
FM
1651
1652 mutex_lock(&codec->mutex);
1653
1654 /* register a sound card */
bd7dd77c
TI
1655 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1656 if (ret < 0) {
db2a4165
FM
1657 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1658 codec->name);
1659 mutex_unlock(&codec->mutex);
bd7dd77c 1660 return ret;
db2a4165
FM
1661 }
1662
452c5eaa 1663 codec->socdev = socdev;
db2a4165
FM
1664 codec->card->dev = socdev->dev;
1665 codec->card->private_data = codec;
1666 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1667
1668 /* create the pcms */
87506549
MB
1669 for (i = 0; i < card->num_links; i++) {
1670 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
db2a4165
FM
1671 if (ret < 0) {
1672 printk(KERN_ERR "asoc: can't create pcm %s\n",
87506549 1673 card->dai_link[i].stream_name);
db2a4165
FM
1674 mutex_unlock(&codec->mutex);
1675 return ret;
1676 }
fb48e3c6
GG
1677 /* Check for codec->ac97 to handle the ac97.c fun */
1678 if (card->dai_link[i].codec_dai->ac97_control && codec->ac97) {
f7732053
BS
1679 snd_ac97_dev_add_pdata(codec->ac97,
1680 card->dai_link[i].cpu_dai->ac97_pdata);
1681 }
db2a4165
FM
1682 }
1683
1684 mutex_unlock(&codec->mutex);
1685 return ret;
1686}
1687EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1688
db2a4165
FM
1689/**
1690 * snd_soc_free_pcms - free sound card and pcms
1691 * @socdev: the SoC audio device
1692 *
1693 * Frees sound card and pcms associated with the socdev.
1694 * Also unregister the codec if it is an AC97 device.
1695 */
1696void snd_soc_free_pcms(struct snd_soc_device *socdev)
1697{
6627a653 1698 struct snd_soc_codec *codec = socdev->card->codec;
a68660e0 1699#ifdef CONFIG_SND_SOC_AC97_BUS
3c4b266f 1700 struct snd_soc_dai *codec_dai;
a68660e0
LG
1701 int i;
1702#endif
db2a4165
FM
1703
1704 mutex_lock(&codec->mutex);
6627a653 1705 soc_cleanup_codec_debugfs(codec);
db2a4165 1706#ifdef CONFIG_SND_SOC_AC97_BUS
3ff3f64b 1707 for (i = 0; i < codec->num_dai; i++) {
a68660e0 1708 codec_dai = &codec->dai[i];
d2314e0e
AN
1709 if (codec_dai->ac97_control && codec->ac97 &&
1710 strcmp(codec->name, "AC97") != 0) {
a68660e0
LG
1711 soc_ac97_dev_unregister(codec);
1712 goto free_card;
1713 }
1714 }
1715free_card:
db2a4165
FM
1716#endif
1717
1718 if (codec->card)
1719 snd_card_free(codec->card);
1720 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1721 mutex_unlock(&codec->mutex);
1722}
1723EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1724
1725/**
1726 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1727 * @substream: the pcm substream
1728 * @hw: the hardware parameters
1729 *
1730 * Sets the substream runtime hardware parameters.
1731 */
1732int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1733 const struct snd_pcm_hardware *hw)
1734{
1735 struct snd_pcm_runtime *runtime = substream->runtime;
1736 runtime->hw.info = hw->info;
1737 runtime->hw.formats = hw->formats;
1738 runtime->hw.period_bytes_min = hw->period_bytes_min;
1739 runtime->hw.period_bytes_max = hw->period_bytes_max;
1740 runtime->hw.periods_min = hw->periods_min;
1741 runtime->hw.periods_max = hw->periods_max;
1742 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1743 runtime->hw.fifo_size = hw->fifo_size;
1744 return 0;
1745}
1746EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1747
1748/**
1749 * snd_soc_cnew - create new control
1750 * @_template: control template
1751 * @data: control private data
ac11a2b3 1752 * @long_name: control long name
db2a4165
FM
1753 *
1754 * Create a new mixer control from a template control.
1755 *
1756 * Returns 0 for success, else error.
1757 */
1758struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1759 void *data, char *long_name)
1760{
1761 struct snd_kcontrol_new template;
1762
1763 memcpy(&template, _template, sizeof(template));
1764 if (long_name)
1765 template.name = long_name;
db2a4165
FM
1766 template.index = 0;
1767
1768 return snd_ctl_new1(&template, data);
1769}
1770EXPORT_SYMBOL_GPL(snd_soc_cnew);
1771
3e8e1952
IM
1772/**
1773 * snd_soc_add_controls - add an array of controls to a codec.
1774 * Convienience function to add a list of controls. Many codecs were
1775 * duplicating this code.
1776 *
1777 * @codec: codec to add controls to
1778 * @controls: array of controls to add
1779 * @num_controls: number of elements in the array
1780 *
1781 * Return 0 for success, else error.
1782 */
1783int snd_soc_add_controls(struct snd_soc_codec *codec,
1784 const struct snd_kcontrol_new *controls, int num_controls)
1785{
1786 struct snd_card *card = codec->card;
1787 int err, i;
1788
1789 for (i = 0; i < num_controls; i++) {
1790 const struct snd_kcontrol_new *control = &controls[i];
1791 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1792 if (err < 0) {
1793 dev_err(codec->dev, "%s: Failed to add %s\n",
1794 codec->name, control->name);
1795 return err;
1796 }
1797 }
1798
1799 return 0;
1800}
1801EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1802
db2a4165
FM
1803/**
1804 * snd_soc_info_enum_double - enumerated double mixer info callback
1805 * @kcontrol: mixer control
1806 * @uinfo: control element information
1807 *
1808 * Callback to provide information about a double enumerated
1809 * mixer control.
1810 *
1811 * Returns 0 for success.
1812 */
1813int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1814 struct snd_ctl_elem_info *uinfo)
1815{
1816 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1817
1818 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1819 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
f8ba0b7b 1820 uinfo->value.enumerated.items = e->max;
db2a4165 1821
f8ba0b7b
JS
1822 if (uinfo->value.enumerated.item > e->max - 1)
1823 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
1824 strcpy(uinfo->value.enumerated.name,
1825 e->texts[uinfo->value.enumerated.item]);
1826 return 0;
1827}
1828EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1829
1830/**
1831 * snd_soc_get_enum_double - enumerated double mixer get callback
1832 * @kcontrol: mixer control
ac11a2b3 1833 * @ucontrol: control element information
db2a4165
FM
1834 *
1835 * Callback to get the value of a double enumerated mixer.
1836 *
1837 * Returns 0 for success.
1838 */
1839int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1840 struct snd_ctl_elem_value *ucontrol)
1841{
1842 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1843 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 1844 unsigned int val, bitmask;
db2a4165 1845
f8ba0b7b 1846 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165
FM
1847 ;
1848 val = snd_soc_read(codec, e->reg);
3ff3f64b
MB
1849 ucontrol->value.enumerated.item[0]
1850 = (val >> e->shift_l) & (bitmask - 1);
db2a4165
FM
1851 if (e->shift_l != e->shift_r)
1852 ucontrol->value.enumerated.item[1] =
1853 (val >> e->shift_r) & (bitmask - 1);
1854
1855 return 0;
1856}
1857EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1858
1859/**
1860 * snd_soc_put_enum_double - enumerated double mixer put callback
1861 * @kcontrol: mixer control
ac11a2b3 1862 * @ucontrol: control element information
db2a4165
FM
1863 *
1864 * Callback to set the value of a double enumerated mixer.
1865 *
1866 * Returns 0 for success.
1867 */
1868int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1869 struct snd_ctl_elem_value *ucontrol)
1870{
1871 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1872 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f
DR
1873 unsigned int val;
1874 unsigned int mask, bitmask;
db2a4165 1875
f8ba0b7b 1876 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165 1877 ;
f8ba0b7b 1878 if (ucontrol->value.enumerated.item[0] > e->max - 1)
db2a4165
FM
1879 return -EINVAL;
1880 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1881 mask = (bitmask - 1) << e->shift_l;
1882 if (e->shift_l != e->shift_r) {
f8ba0b7b 1883 if (ucontrol->value.enumerated.item[1] > e->max - 1)
db2a4165
FM
1884 return -EINVAL;
1885 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1886 mask |= (bitmask - 1) << e->shift_r;
1887 }
1888
6c508c62 1889 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
db2a4165
FM
1890}
1891EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1892
2e72f8e3
PU
1893/**
1894 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1895 * @kcontrol: mixer control
1896 * @ucontrol: control element information
1897 *
1898 * Callback to get the value of a double semi enumerated mixer.
1899 *
1900 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1901 * used for handling bitfield coded enumeration for example.
1902 *
1903 * Returns 0 for success.
1904 */
1905int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1906 struct snd_ctl_elem_value *ucontrol)
1907{
1908 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
74155556 1909 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 1910 unsigned int reg_val, val, mux;
2e72f8e3
PU
1911
1912 reg_val = snd_soc_read(codec, e->reg);
1913 val = (reg_val >> e->shift_l) & e->mask;
1914 for (mux = 0; mux < e->max; mux++) {
1915 if (val == e->values[mux])
1916 break;
1917 }
1918 ucontrol->value.enumerated.item[0] = mux;
1919 if (e->shift_l != e->shift_r) {
1920 val = (reg_val >> e->shift_r) & e->mask;
1921 for (mux = 0; mux < e->max; mux++) {
1922 if (val == e->values[mux])
1923 break;
1924 }
1925 ucontrol->value.enumerated.item[1] = mux;
1926 }
1927
1928 return 0;
1929}
1930EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1931
1932/**
1933 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1934 * @kcontrol: mixer control
1935 * @ucontrol: control element information
1936 *
1937 * Callback to set the value of a double semi enumerated mixer.
1938 *
1939 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1940 * used for handling bitfield coded enumeration for example.
1941 *
1942 * Returns 0 for success.
1943 */
1944int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1945 struct snd_ctl_elem_value *ucontrol)
1946{
1947 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
74155556 1948 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f
DR
1949 unsigned int val;
1950 unsigned int mask;
2e72f8e3
PU
1951
1952 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1953 return -EINVAL;
1954 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1955 mask = e->mask << e->shift_l;
1956 if (e->shift_l != e->shift_r) {
1957 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1958 return -EINVAL;
1959 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1960 mask |= e->mask << e->shift_r;
1961 }
1962
6c508c62 1963 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2e72f8e3
PU
1964}
1965EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1966
db2a4165
FM
1967/**
1968 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1969 * @kcontrol: mixer control
1970 * @uinfo: control element information
1971 *
1972 * Callback to provide information about an external enumerated
1973 * single mixer.
1974 *
1975 * Returns 0 for success.
1976 */
1977int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1978 struct snd_ctl_elem_info *uinfo)
1979{
1980 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1981
1982 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1983 uinfo->count = 1;
f8ba0b7b 1984 uinfo->value.enumerated.items = e->max;
db2a4165 1985
f8ba0b7b
JS
1986 if (uinfo->value.enumerated.item > e->max - 1)
1987 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
1988 strcpy(uinfo->value.enumerated.name,
1989 e->texts[uinfo->value.enumerated.item]);
1990 return 0;
1991}
1992EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1993
1994/**
1995 * snd_soc_info_volsw_ext - external single mixer info callback
1996 * @kcontrol: mixer control
1997 * @uinfo: control element information
1998 *
1999 * Callback to provide information about a single external mixer control.
2000 *
2001 * Returns 0 for success.
2002 */
2003int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2004 struct snd_ctl_elem_info *uinfo)
2005{
a7a4ac86
PZ
2006 int max = kcontrol->private_value;
2007
fd5dfad9 2008 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
a7a4ac86
PZ
2009 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2010 else
2011 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 2012
db2a4165
FM
2013 uinfo->count = 1;
2014 uinfo->value.integer.min = 0;
a7a4ac86 2015 uinfo->value.integer.max = max;
db2a4165
FM
2016 return 0;
2017}
2018EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2019
db2a4165
FM
2020/**
2021 * snd_soc_info_volsw - single mixer info callback
2022 * @kcontrol: mixer control
2023 * @uinfo: control element information
2024 *
2025 * Callback to provide information about a single mixer control.
2026 *
2027 * Returns 0 for success.
2028 */
2029int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2030 struct snd_ctl_elem_info *uinfo)
2031{
4eaa9819
JS
2032 struct soc_mixer_control *mc =
2033 (struct soc_mixer_control *)kcontrol->private_value;
d11bb4a9 2034 int platform_max;
762b8df7 2035 unsigned int shift = mc->shift;
815ecf8d 2036 unsigned int rshift = mc->rshift;
db2a4165 2037
d11bb4a9
PU
2038 if (!mc->platform_max)
2039 mc->platform_max = mc->max;
2040 platform_max = mc->platform_max;
2041
2042 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
a7a4ac86
PZ
2043 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2044 else
2045 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2046
db2a4165
FM
2047 uinfo->count = shift == rshift ? 1 : 2;
2048 uinfo->value.integer.min = 0;
d11bb4a9 2049 uinfo->value.integer.max = platform_max;
db2a4165
FM
2050 return 0;
2051}
2052EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2053
2054/**
2055 * snd_soc_get_volsw - single mixer get callback
2056 * @kcontrol: mixer control
ac11a2b3 2057 * @ucontrol: control element information
db2a4165
FM
2058 *
2059 * Callback to get the value of a single mixer control.
2060 *
2061 * Returns 0 for success.
2062 */
2063int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_value *ucontrol)
2065{
4eaa9819
JS
2066 struct soc_mixer_control *mc =
2067 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2068 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
2069 unsigned int reg = mc->reg;
2070 unsigned int shift = mc->shift;
2071 unsigned int rshift = mc->rshift;
4eaa9819 2072 int max = mc->max;
815ecf8d
JS
2073 unsigned int mask = (1 << fls(max)) - 1;
2074 unsigned int invert = mc->invert;
db2a4165
FM
2075
2076 ucontrol->value.integer.value[0] =
2077 (snd_soc_read(codec, reg) >> shift) & mask;
2078 if (shift != rshift)
2079 ucontrol->value.integer.value[1] =
2080 (snd_soc_read(codec, reg) >> rshift) & mask;
2081 if (invert) {
2082 ucontrol->value.integer.value[0] =
a7a4ac86 2083 max - ucontrol->value.integer.value[0];
db2a4165
FM
2084 if (shift != rshift)
2085 ucontrol->value.integer.value[1] =
a7a4ac86 2086 max - ucontrol->value.integer.value[1];
db2a4165
FM
2087 }
2088
2089 return 0;
2090}
2091EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2092
2093/**
2094 * snd_soc_put_volsw - single mixer put callback
2095 * @kcontrol: mixer control
ac11a2b3 2096 * @ucontrol: control element information
db2a4165
FM
2097 *
2098 * Callback to set the value of a single mixer control.
2099 *
2100 * Returns 0 for success.
2101 */
2102int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2103 struct snd_ctl_elem_value *ucontrol)
2104{
4eaa9819
JS
2105 struct soc_mixer_control *mc =
2106 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2107 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
2108 unsigned int reg = mc->reg;
2109 unsigned int shift = mc->shift;
2110 unsigned int rshift = mc->rshift;
4eaa9819 2111 int max = mc->max;
815ecf8d
JS
2112 unsigned int mask = (1 << fls(max)) - 1;
2113 unsigned int invert = mc->invert;
46f5822f 2114 unsigned int val, val2, val_mask;
db2a4165
FM
2115
2116 val = (ucontrol->value.integer.value[0] & mask);
2117 if (invert)
a7a4ac86 2118 val = max - val;
db2a4165
FM
2119 val_mask = mask << shift;
2120 val = val << shift;
2121 if (shift != rshift) {
2122 val2 = (ucontrol->value.integer.value[1] & mask);
2123 if (invert)
a7a4ac86 2124 val2 = max - val2;
db2a4165
FM
2125 val_mask |= mask << rshift;
2126 val |= val2 << rshift;
2127 }
6c508c62 2128 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
db2a4165
FM
2129}
2130EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2131
2132/**
2133 * snd_soc_info_volsw_2r - double mixer info callback
2134 * @kcontrol: mixer control
2135 * @uinfo: control element information
2136 *
2137 * Callback to provide information about a double mixer control that
2138 * spans 2 codec registers.
2139 *
2140 * Returns 0 for success.
2141 */
2142int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2143 struct snd_ctl_elem_info *uinfo)
2144{
4eaa9819
JS
2145 struct soc_mixer_control *mc =
2146 (struct soc_mixer_control *)kcontrol->private_value;
d11bb4a9 2147 int platform_max;
a7a4ac86 2148
d11bb4a9
PU
2149 if (!mc->platform_max)
2150 mc->platform_max = mc->max;
2151 platform_max = mc->platform_max;
2152
2153 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
a7a4ac86
PZ
2154 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2155 else
2156 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 2157
db2a4165
FM
2158 uinfo->count = 2;
2159 uinfo->value.integer.min = 0;
d11bb4a9 2160 uinfo->value.integer.max = platform_max;
db2a4165
FM
2161 return 0;
2162}
2163EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2164
2165/**
2166 * snd_soc_get_volsw_2r - double mixer get callback
2167 * @kcontrol: mixer control
ac11a2b3 2168 * @ucontrol: control element information
db2a4165
FM
2169 *
2170 * Callback to get the value of a double mixer control that spans 2 registers.
2171 *
2172 * Returns 0 for success.
2173 */
2174int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2175 struct snd_ctl_elem_value *ucontrol)
2176{
4eaa9819
JS
2177 struct soc_mixer_control *mc =
2178 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2179 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
2180 unsigned int reg = mc->reg;
2181 unsigned int reg2 = mc->rreg;
2182 unsigned int shift = mc->shift;
4eaa9819 2183 int max = mc->max;
46f5822f 2184 unsigned int mask = (1 << fls(max)) - 1;
815ecf8d 2185 unsigned int invert = mc->invert;
db2a4165
FM
2186
2187 ucontrol->value.integer.value[0] =
2188 (snd_soc_read(codec, reg) >> shift) & mask;
2189 ucontrol->value.integer.value[1] =
2190 (snd_soc_read(codec, reg2) >> shift) & mask;
2191 if (invert) {
2192 ucontrol->value.integer.value[0] =
a7a4ac86 2193 max - ucontrol->value.integer.value[0];
db2a4165 2194 ucontrol->value.integer.value[1] =
a7a4ac86 2195 max - ucontrol->value.integer.value[1];
db2a4165
FM
2196 }
2197
2198 return 0;
2199}
2200EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2201
2202/**
2203 * snd_soc_put_volsw_2r - double mixer set callback
2204 * @kcontrol: mixer control
ac11a2b3 2205 * @ucontrol: control element information
db2a4165
FM
2206 *
2207 * Callback to set the value of a double mixer control that spans 2 registers.
2208 *
2209 * Returns 0 for success.
2210 */
2211int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2212 struct snd_ctl_elem_value *ucontrol)
2213{
4eaa9819
JS
2214 struct soc_mixer_control *mc =
2215 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2216 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
2217 unsigned int reg = mc->reg;
2218 unsigned int reg2 = mc->rreg;
2219 unsigned int shift = mc->shift;
4eaa9819 2220 int max = mc->max;
815ecf8d
JS
2221 unsigned int mask = (1 << fls(max)) - 1;
2222 unsigned int invert = mc->invert;
db2a4165 2223 int err;
46f5822f 2224 unsigned int val, val2, val_mask;
db2a4165
FM
2225
2226 val_mask = mask << shift;
2227 val = (ucontrol->value.integer.value[0] & mask);
2228 val2 = (ucontrol->value.integer.value[1] & mask);
2229
2230 if (invert) {
a7a4ac86
PZ
2231 val = max - val;
2232 val2 = max - val2;
db2a4165
FM
2233 }
2234
2235 val = val << shift;
2236 val2 = val2 << shift;
2237
6c508c62 2238 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
3ff3f64b 2239 if (err < 0)
db2a4165
FM
2240 return err;
2241
6c508c62 2242 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
db2a4165
FM
2243 return err;
2244}
2245EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2246
e13ac2e9
MB
2247/**
2248 * snd_soc_info_volsw_s8 - signed mixer info callback
2249 * @kcontrol: mixer control
2250 * @uinfo: control element information
2251 *
2252 * Callback to provide information about a signed mixer control.
2253 *
2254 * Returns 0 for success.
2255 */
2256int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2257 struct snd_ctl_elem_info *uinfo)
2258{
4eaa9819
JS
2259 struct soc_mixer_control *mc =
2260 (struct soc_mixer_control *)kcontrol->private_value;
d11bb4a9 2261 int platform_max;
4eaa9819 2262 int min = mc->min;
e13ac2e9 2263
d11bb4a9
PU
2264 if (!mc->platform_max)
2265 mc->platform_max = mc->max;
2266 platform_max = mc->platform_max;
2267
e13ac2e9
MB
2268 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2269 uinfo->count = 2;
2270 uinfo->value.integer.min = 0;
d11bb4a9 2271 uinfo->value.integer.max = platform_max - min;
e13ac2e9
MB
2272 return 0;
2273}
2274EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2275
2276/**
2277 * snd_soc_get_volsw_s8 - signed mixer get callback
2278 * @kcontrol: mixer control
ac11a2b3 2279 * @ucontrol: control element information
e13ac2e9
MB
2280 *
2281 * Callback to get the value of a signed mixer control.
2282 *
2283 * Returns 0 for success.
2284 */
2285int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2286 struct snd_ctl_elem_value *ucontrol)
2287{
4eaa9819
JS
2288 struct soc_mixer_control *mc =
2289 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 2290 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 2291 unsigned int reg = mc->reg;
4eaa9819 2292 int min = mc->min;
e13ac2e9
MB
2293 int val = snd_soc_read(codec, reg);
2294
2295 ucontrol->value.integer.value[0] =
2296 ((signed char)(val & 0xff))-min;
2297 ucontrol->value.integer.value[1] =
2298 ((signed char)((val >> 8) & 0xff))-min;
2299 return 0;
2300}
2301EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2302
2303/**
2304 * snd_soc_put_volsw_sgn - signed mixer put callback
2305 * @kcontrol: mixer control
ac11a2b3 2306 * @ucontrol: control element information
e13ac2e9
MB
2307 *
2308 * Callback to set the value of a signed mixer control.
2309 *
2310 * Returns 0 for success.
2311 */
2312int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2313 struct snd_ctl_elem_value *ucontrol)
2314{
4eaa9819
JS
2315 struct soc_mixer_control *mc =
2316 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 2317 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 2318 unsigned int reg = mc->reg;
4eaa9819 2319 int min = mc->min;
46f5822f 2320 unsigned int val;
e13ac2e9
MB
2321
2322 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2323 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2324
6c508c62 2325 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
e13ac2e9
MB
2326}
2327EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2328
637d3847
PU
2329/**
2330 * snd_soc_limit_volume - Set new limit to an existing volume control.
2331 *
2332 * @codec: where to look for the control
2333 * @name: Name of the control
2334 * @max: new maximum limit
2335 *
2336 * Return 0 for success, else error.
2337 */
2338int snd_soc_limit_volume(struct snd_soc_codec *codec,
2339 const char *name, int max)
2340{
2341 struct snd_card *card = codec->card;
2342 struct snd_kcontrol *kctl;
2343 struct soc_mixer_control *mc;
2344 int found = 0;
2345 int ret = -EINVAL;
2346
2347 /* Sanity check for name and max */
2348 if (unlikely(!name || max <= 0))
2349 return -EINVAL;
2350
2351 list_for_each_entry(kctl, &card->controls, list) {
2352 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2353 found = 1;
2354 break;
2355 }
2356 }
2357 if (found) {
2358 mc = (struct soc_mixer_control *)kctl->private_value;
2359 if (max <= mc->max) {
d11bb4a9 2360 mc->platform_max = max;
637d3847
PU
2361 ret = 0;
2362 }
2363 }
2364 return ret;
2365}
2366EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2367
b6f4bb38 2368/**
2369 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2370 * mixer info callback
2371 * @kcontrol: mixer control
2372 * @uinfo: control element information
2373 *
2374 * Returns 0 for success.
2375 */
2376int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2377 struct snd_ctl_elem_info *uinfo)
2378{
2379 struct soc_mixer_control *mc =
2380 (struct soc_mixer_control *)kcontrol->private_value;
2381 int max = mc->max;
2382 int min = mc->min;
2383
2384 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2385 uinfo->count = 2;
2386 uinfo->value.integer.min = 0;
2387 uinfo->value.integer.max = max-min;
2388
2389 return 0;
2390}
2391EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2392
2393/**
2394 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2395 * mixer get callback
2396 * @kcontrol: mixer control
2397 * @uinfo: control element information
2398 *
2399 * Returns 0 for success.
2400 */
2401int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_value *ucontrol)
2403{
2404 struct soc_mixer_control *mc =
2405 (struct soc_mixer_control *)kcontrol->private_value;
2406 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2407 unsigned int mask = (1<<mc->shift)-1;
2408 int min = mc->min;
2409 int val = snd_soc_read(codec, mc->reg) & mask;
2410 int valr = snd_soc_read(codec, mc->rreg) & mask;
2411
20630c7f
SL
2412 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2413 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
b6f4bb38 2414 return 0;
2415}
2416EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2417
2418/**
2419 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2420 * mixer put callback
2421 * @kcontrol: mixer control
2422 * @uinfo: control element information
2423 *
2424 * Returns 0 for success.
2425 */
2426int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2427 struct snd_ctl_elem_value *ucontrol)
2428{
2429 struct soc_mixer_control *mc =
2430 (struct soc_mixer_control *)kcontrol->private_value;
2431 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2432 unsigned int mask = (1<<mc->shift)-1;
2433 int min = mc->min;
2434 int ret;
2435 unsigned int val, valr, oval, ovalr;
2436
2437 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2438 val &= mask;
2439 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2440 valr &= mask;
2441
2442 oval = snd_soc_read(codec, mc->reg) & mask;
2443 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2444
2445 ret = 0;
2446 if (oval != val) {
2447 ret = snd_soc_write(codec, mc->reg, val);
2448 if (ret < 0)
f1df5aec 2449 return ret;
b6f4bb38 2450 }
2451 if (ovalr != valr) {
2452 ret = snd_soc_write(codec, mc->rreg, valr);
2453 if (ret < 0)
f1df5aec 2454 return ret;
b6f4bb38 2455 }
2456
2457 return 0;
2458}
2459EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2460
8c6529db
LG
2461/**
2462 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2463 * @dai: DAI
2464 * @clk_id: DAI specific clock ID
2465 * @freq: new clock frequency in Hz
2466 * @dir: new clock direction - input/output.
2467 *
2468 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2469 */
2470int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2471 unsigned int freq, int dir)
2472{
3f1a4d82 2473 if (dai->ops && dai->ops->set_sysclk)
6335d055 2474 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
8c6529db
LG
2475 else
2476 return -EINVAL;
2477}
2478EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2479
2480/**
2481 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2482 * @dai: DAI
ac11a2b3 2483 * @div_id: DAI specific clock divider ID
8c6529db
LG
2484 * @div: new clock divisor.
2485 *
2486 * Configures the clock dividers. This is used to derive the best DAI bit and
2487 * frame clocks from the system or master clock. It's best to set the DAI bit
2488 * and frame clocks as low as possible to save system power.
2489 */
2490int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2491 int div_id, int div)
2492{
3f1a4d82 2493 if (dai->ops && dai->ops->set_clkdiv)
6335d055 2494 return dai->ops->set_clkdiv(dai, div_id, div);
8c6529db
LG
2495 else
2496 return -EINVAL;
2497}
2498EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2499
2500/**
2501 * snd_soc_dai_set_pll - configure DAI PLL.
2502 * @dai: DAI
2503 * @pll_id: DAI specific PLL ID
85488037 2504 * @source: DAI specific source for the PLL
8c6529db
LG
2505 * @freq_in: PLL input clock frequency in Hz
2506 * @freq_out: requested PLL output clock frequency in Hz
2507 *
2508 * Configures and enables PLL to generate output clock based on input clock.
2509 */
85488037
MB
2510int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2511 unsigned int freq_in, unsigned int freq_out)
8c6529db 2512{
3f1a4d82 2513 if (dai->ops && dai->ops->set_pll)
85488037
MB
2514 return dai->ops->set_pll(dai, pll_id, source,
2515 freq_in, freq_out);
8c6529db
LG
2516 else
2517 return -EINVAL;
2518}
2519EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2520
2521/**
2522 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2523 * @dai: DAI
8c6529db
LG
2524 * @fmt: SND_SOC_DAIFMT_ format value.
2525 *
2526 * Configures the DAI hardware format and clocking.
2527 */
2528int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2529{
3f1a4d82 2530 if (dai->ops && dai->ops->set_fmt)
6335d055 2531 return dai->ops->set_fmt(dai, fmt);
8c6529db
LG
2532 else
2533 return -EINVAL;
2534}
2535EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2536
2537/**
2538 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2539 * @dai: DAI
a5479e38
DR
2540 * @tx_mask: bitmask representing active TX slots.
2541 * @rx_mask: bitmask representing active RX slots.
8c6529db 2542 * @slots: Number of slots in use.
a5479e38 2543 * @slot_width: Width in bits for each slot.
8c6529db
LG
2544 *
2545 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2546 * specific.
2547 */
2548int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
a5479e38 2549 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
8c6529db 2550{
3f1a4d82 2551 if (dai->ops && dai->ops->set_tdm_slot)
a5479e38
DR
2552 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2553 slots, slot_width);
8c6529db
LG
2554 else
2555 return -EINVAL;
2556}
2557EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2558
472df3cb
BS
2559/**
2560 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2561 * @dai: DAI
2562 * @tx_num: how many TX channels
2563 * @tx_slot: pointer to an array which imply the TX slot number channel
2564 * 0~num-1 uses
2565 * @rx_num: how many RX channels
2566 * @rx_slot: pointer to an array which imply the RX slot number channel
2567 * 0~num-1 uses
2568 *
2569 * configure the relationship between channel number and TDM slot number.
2570 */
2571int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2572 unsigned int tx_num, unsigned int *tx_slot,
2573 unsigned int rx_num, unsigned int *rx_slot)
2574{
2575 if (dai->ops && dai->ops->set_channel_map)
2576 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2577 rx_num, rx_slot);
2578 else
2579 return -EINVAL;
2580}
2581EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2582
8c6529db
LG
2583/**
2584 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2585 * @dai: DAI
2586 * @tristate: tristate enable
2587 *
2588 * Tristates the DAI so that others can use it.
2589 */
2590int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2591{
3f1a4d82 2592 if (dai->ops && dai->ops->set_tristate)
6335d055 2593 return dai->ops->set_tristate(dai, tristate);
8c6529db
LG
2594 else
2595 return -EINVAL;
2596}
2597EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2598
2599/**
2600 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2601 * @dai: DAI
2602 * @mute: mute enable
2603 *
2604 * Mutes the DAI DAC.
2605 */
2606int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2607{
3f1a4d82 2608 if (dai->ops && dai->ops->digital_mute)
6335d055 2609 return dai->ops->digital_mute(dai, mute);
8c6529db
LG
2610 else
2611 return -EINVAL;
2612}
2613EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2614
c5af3a2e
MB
2615/**
2616 * snd_soc_register_card - Register a card with the ASoC core
2617 *
ac11a2b3 2618 * @card: Card to register
c5af3a2e
MB
2619 *
2620 * Note that currently this is an internal only function: it will be
2621 * exposed to machine drivers after further backporting of ASoC v2
2622 * registration APIs.
2623 */
2624static int snd_soc_register_card(struct snd_soc_card *card)
2625{
2626 if (!card->name || !card->dev)
2627 return -EINVAL;
2628
2629 INIT_LIST_HEAD(&card->list);
2630 card->instantiated = 0;
2631
2632 mutex_lock(&client_mutex);
2633 list_add(&card->list, &card_list);
435c5e25 2634 snd_soc_instantiate_cards();
c5af3a2e
MB
2635 mutex_unlock(&client_mutex);
2636
2637 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2638
2639 return 0;
2640}
2641
2642/**
2643 * snd_soc_unregister_card - Unregister a card with the ASoC core
2644 *
ac11a2b3 2645 * @card: Card to unregister
c5af3a2e
MB
2646 *
2647 * Note that currently this is an internal only function: it will be
2648 * exposed to machine drivers after further backporting of ASoC v2
2649 * registration APIs.
2650 */
2651static int snd_soc_unregister_card(struct snd_soc_card *card)
2652{
2653 mutex_lock(&client_mutex);
2654 list_del(&card->list);
2655 mutex_unlock(&client_mutex);
2656
2657 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2658
2659 return 0;
2660}
2661
9115171a
MB
2662/**
2663 * snd_soc_register_dai - Register a DAI with the ASoC core
2664 *
ac11a2b3 2665 * @dai: DAI to register
9115171a
MB
2666 */
2667int snd_soc_register_dai(struct snd_soc_dai *dai)
2668{
2669 if (!dai->name)
2670 return -EINVAL;
2671
2672 /* The device should become mandatory over time */
2673 if (!dai->dev)
2674 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2675
6335d055
EM
2676 if (!dai->ops)
2677 dai->ops = &null_dai_ops;
2678
9115171a
MB
2679 INIT_LIST_HEAD(&dai->list);
2680
2681 mutex_lock(&client_mutex);
2682 list_add(&dai->list, &dai_list);
435c5e25 2683 snd_soc_instantiate_cards();
9115171a
MB
2684 mutex_unlock(&client_mutex);
2685
2686 pr_debug("Registered DAI '%s'\n", dai->name);
2687
2688 return 0;
2689}
2690EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2691
2692/**
2693 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2694 *
ac11a2b3 2695 * @dai: DAI to unregister
9115171a
MB
2696 */
2697void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2698{
2699 mutex_lock(&client_mutex);
2700 list_del(&dai->list);
2701 mutex_unlock(&client_mutex);
2702
2703 pr_debug("Unregistered DAI '%s'\n", dai->name);
2704}
2705EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2706
2707/**
2708 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2709 *
ac11a2b3
MB
2710 * @dai: Array of DAIs to register
2711 * @count: Number of DAIs
9115171a
MB
2712 */
2713int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2714{
2715 int i, ret;
2716
2717 for (i = 0; i < count; i++) {
2718 ret = snd_soc_register_dai(&dai[i]);
2719 if (ret != 0)
2720 goto err;
2721 }
2722
2723 return 0;
2724
2725err:
2726 for (i--; i >= 0; i--)
2727 snd_soc_unregister_dai(&dai[i]);
2728
2729 return ret;
2730}
2731EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2732
2733/**
2734 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2735 *
ac11a2b3
MB
2736 * @dai: Array of DAIs to unregister
2737 * @count: Number of DAIs
9115171a
MB
2738 */
2739void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2740{
2741 int i;
2742
2743 for (i = 0; i < count; i++)
2744 snd_soc_unregister_dai(&dai[i]);
2745}
2746EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2747
12a48a8c
MB
2748/**
2749 * snd_soc_register_platform - Register a platform with the ASoC core
2750 *
ac11a2b3 2751 * @platform: platform to register
12a48a8c
MB
2752 */
2753int snd_soc_register_platform(struct snd_soc_platform *platform)
2754{
2755 if (!platform->name)
2756 return -EINVAL;
2757
2758 INIT_LIST_HEAD(&platform->list);
2759
2760 mutex_lock(&client_mutex);
2761 list_add(&platform->list, &platform_list);
435c5e25 2762 snd_soc_instantiate_cards();
12a48a8c
MB
2763 mutex_unlock(&client_mutex);
2764
2765 pr_debug("Registered platform '%s'\n", platform->name);
2766
2767 return 0;
2768}
2769EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2770
2771/**
2772 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2773 *
ac11a2b3 2774 * @platform: platform to unregister
12a48a8c
MB
2775 */
2776void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2777{
2778 mutex_lock(&client_mutex);
2779 list_del(&platform->list);
2780 mutex_unlock(&client_mutex);
2781
2782 pr_debug("Unregistered platform '%s'\n", platform->name);
2783}
2784EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2785
151ab22c
MB
2786static u64 codec_format_map[] = {
2787 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2788 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2789 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2790 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2791 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2792 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2793 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2794 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2795 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2796 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2797 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2798 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2799 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2800 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2801 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2802 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2803};
2804
2805/* Fix up the DAI formats for endianness: codecs don't actually see
2806 * the endianness of the data but we're using the CPU format
2807 * definitions which do need to include endianness so we ensure that
2808 * codec DAIs always have both big and little endian variants set.
2809 */
2810static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2811{
2812 int i;
2813
2814 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2815 if (stream->formats & codec_format_map[i])
2816 stream->formats |= codec_format_map[i];
2817}
2818
0d0cf00a
MB
2819/**
2820 * snd_soc_register_codec - Register a codec with the ASoC core
2821 *
ac11a2b3 2822 * @codec: codec to register
0d0cf00a
MB
2823 */
2824int snd_soc_register_codec(struct snd_soc_codec *codec)
2825{
151ab22c
MB
2826 int i;
2827
0d0cf00a
MB
2828 if (!codec->name)
2829 return -EINVAL;
2830
2831 /* The device should become mandatory over time */
2832 if (!codec->dev)
2833 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2834
2835 INIT_LIST_HEAD(&codec->list);
2836
151ab22c
MB
2837 for (i = 0; i < codec->num_dai; i++) {
2838 fixup_codec_formats(&codec->dai[i].playback);
2839 fixup_codec_formats(&codec->dai[i].capture);
2840 }
2841
0d0cf00a
MB
2842 mutex_lock(&client_mutex);
2843 list_add(&codec->list, &codec_list);
2844 snd_soc_instantiate_cards();
2845 mutex_unlock(&client_mutex);
2846
2847 pr_debug("Registered codec '%s'\n", codec->name);
2848
2849 return 0;
2850}
2851EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2852
2853/**
2854 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2855 *
ac11a2b3 2856 * @codec: codec to unregister
0d0cf00a
MB
2857 */
2858void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2859{
2860 mutex_lock(&client_mutex);
2861 list_del(&codec->list);
2862 mutex_unlock(&client_mutex);
2863
2864 pr_debug("Unregistered codec '%s'\n", codec->name);
2865}
2866EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2867
c9b3a40f 2868static int __init snd_soc_init(void)
db2a4165 2869{
384c89e2
MB
2870#ifdef CONFIG_DEBUG_FS
2871 debugfs_root = debugfs_create_dir("asoc", NULL);
2872 if (IS_ERR(debugfs_root) || !debugfs_root) {
2873 printk(KERN_WARNING
2874 "ASoC: Failed to create debugfs directory\n");
2875 debugfs_root = NULL;
2876 }
2877#endif
2878
db2a4165
FM
2879 return platform_driver_register(&soc_driver);
2880}
2881
7d8c16a6 2882static void __exit snd_soc_exit(void)
db2a4165 2883{
384c89e2
MB
2884#ifdef CONFIG_DEBUG_FS
2885 debugfs_remove_recursive(debugfs_root);
2886#endif
3ff3f64b 2887 platform_driver_unregister(&soc_driver);
db2a4165
FM
2888}
2889
2890module_init(snd_soc_init);
2891module_exit(snd_soc_exit);
2892
2893/* Module information */
d331124d 2894MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
db2a4165
FM
2895MODULE_DESCRIPTION("ALSA SoC Core");
2896MODULE_LICENSE("GPL");
8b45a209 2897MODULE_ALIAS("platform:soc-audio");