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