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