blob: e048e0910099f07723359a85e29dac1d3a6169c2 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
6 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +01008 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020010 *
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 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020016 * 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>
Troy Kisky12ef1932008-10-13 17:42:14 -070029#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020030#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Marek Vasut474828a2009-07-22 13:01:03 +020032#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020033#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
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040static DEFINE_MUTEX(pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
42
Mark Brown384c89e2008-12-03 17:34:03 +000043#ifdef CONFIG_DEBUG_FS
44static struct dentry *debugfs_root;
45#endif
46
Mark Brownc5af3a22008-11-28 13:29:45 +000047static DEFINE_MUTEX(client_mutex);
48static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000049static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000050static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000051static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000052
53static int snd_soc_register_card(struct snd_soc_card *card);
54static int snd_soc_unregister_card(struct snd_soc_card *card);
55
Frank Mandarinodb2a4162006-10-06 18:31:09 +020056/*
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
Liam Girdwood965ac422007-01-31 14:14:57 +010065/*
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
Mark Brown2624d5f2009-11-03 21:56:13 +000084/* 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
Mark Browndbe21402010-02-12 11:37:24 +0000134static 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
Mark Brown6c5f1fe2010-02-17 14:30:44 +0000140 return sprintf(buf, "%ld\n", card->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000141}
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
Mark Brown2624d5f2009-11-03 21:56:13 +0000157#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
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200274#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;
Mark Brown4ac5c612009-04-01 19:35:01 +0100292 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200293 codec->ac97->dev.release = soc_ac97_device_release;
294
Kay Sieversbb072bf2008-11-02 03:50:35 +0100295 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
296 codec->card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200297 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
Mark Brown06f409d2009-04-07 18:10:13 +0100307static 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) {
Peter Ujfalusi50831452010-03-03 15:08:04 +0200319 dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
Mark Brown06f409d2009-04-07 18:10:13 +0100320 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
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200336/*
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;
Mark Brown87689d52008-12-02 16:01:14 +0000345 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200346 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100347 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000348 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100349 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
350 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200351 int ret = 0;
352
353 mutex_lock(&pcm_mutex);
354
355 /* startup the audio subsystem */
Eric Miao6335d052009-03-03 09:41:00 +0800356 if (cpu_dai->ops->startup) {
357 ret = cpu_dai->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200358 if (ret < 0) {
359 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100360 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200361 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
Eric Miao6335d052009-03-03 09:41:00 +0800373 if (codec_dai->ops->startup) {
374 ret = codec_dai->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100375 if (ret < 0) {
376 printk(KERN_ERR "asoc: can't open codec %s\n",
377 codec_dai->name);
378 goto codec_dai_err;
379 }
380 }
381
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200382 if (machine->ops && machine->ops->startup) {
383 ret = machine->ops->startup(substream);
384 if (ret < 0) {
385 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
386 goto machine_err;
387 }
388 }
389
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200390 /* Check that the codec and cpu DAI's are compatible */
391 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
392 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200393 max(codec_dai->playback.rate_min,
394 cpu_dai->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200395 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200396 min(codec_dai->playback.rate_max,
397 cpu_dai->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200398 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100399 max(codec_dai->playback.channels_min,
400 cpu_dai->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200401 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100402 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;
Jassi Brard9ad6292010-03-12 13:38:52 +0900408 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;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200414 } else {
415 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200416 max(codec_dai->capture.rate_min,
417 cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200418 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200419 min(codec_dai->capture.rate_max,
420 cpu_dai->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200421 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100422 max(codec_dai->capture.channels_min,
423 cpu_dai->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200424 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100425 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;
Jassi Brard9ad6292010-03-12 13:38:52 +0900431 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;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200437 }
438
439 snd_pcm_limit_hw_rates(runtime);
440 if (!runtime->hw.rates) {
441 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100442 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900443 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200444 }
445 if (!runtime->hw.formats) {
446 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100447 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900448 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200449 }
450 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
451 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100452 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900453 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200454 }
455
Mark Brown06f409d2009-04-07 18:10:13 +0100456 /* 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)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900460 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100461 }
462
Mark Brownf24368c2008-10-21 21:45:08 +0100463 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);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200469
Jassi Brar14dc5732010-02-26 09:12:32 +0900470 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++;
Mark Brown6627a652009-01-23 22:55:23 +0000479 card->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200480 mutex_unlock(&pcm_mutex);
481 return 0;
482
Jassi Brarbb1c0472010-02-25 11:24:53 +0900483config_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200484 if (machine->ops && machine->ops->shutdown)
485 machine->ops->shutdown(substream);
486
Jassi Brarbb1c0472010-02-25 11:24:53 +0900487machine_err:
488 if (codec_dai->ops->shutdown)
489 codec_dai->ops->shutdown(substream, codec_dai);
490
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100491codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200492 if (platform->pcm_ops->close)
493 platform->pcm_ops->close(substream);
494
495platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800496 if (cpu_dai->ops->shutdown)
497 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200498out:
499 mutex_unlock(&pcm_mutex);
500 return ret;
501}
502
503/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200504 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200505 * This is to ensure there are no pops or clicks in between any music tracks
506 * due to DAPM power cycling.
507 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100508static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200509{
Mark Brown63084192008-12-02 15:08:03 +0000510 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
511 delayed_work.work);
Mark Brown6627a652009-01-23 22:55:23 +0000512 struct snd_soc_codec *codec = card->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100513 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200514 int i;
515
516 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200517 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200518 codec_dai = &codec->dai[i];
519
Mark Brownf24368c2008-10-21 21:45:08 +0100520 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");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524
525 /* are we waiting on this codec DAI stream */
526 if (codec_dai->pop_wait == 1) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200527 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100528 snd_soc_dapm_stream_event(codec,
529 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200530 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531 }
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;
Mark Brown63084192008-12-02 15:08:03 +0000545 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100546 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000547 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100548 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
549 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000550 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200551
552 mutex_lock(&pcm_mutex);
553
Jassi Brar14dc5732010-02-26 09:12:32 +0900554 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--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200560 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900561
562 cpu_dai->active--;
563 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564 codec->active--;
565
Mark Brown6010b2d2008-09-06 18:33:24 +0100566 /* 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
Eric Miao6335d052009-03-03 09:41:00 +0800572 if (cpu_dai->ops->shutdown)
573 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200574
Eric Miao6335d052009-03-03 09:41:00 +0800575 if (codec_dai->ops->shutdown)
576 codec_dai->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200577
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);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200583
584 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
585 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100586 codec_dai->pop_wait = 1;
Mark Brown63084192008-12-02 15:08:03 +0000587 schedule_delayed_work(&card->delayed_work,
Mark Brown96dd3622010-02-12 11:05:44 +0000588 msecs_to_jiffies(card->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200589 } else {
590 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100591 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100592 codec_dai->capture.stream_name,
593 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200594 }
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;
Mark Brown63084192008-12-02 15:08:03 +0000609 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100610 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000611 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100612 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
613 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000614 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615 int ret = 0;
616
617 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100618
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
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200627 if (platform->pcm_ops->prepare) {
628 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200629 if (ret < 0) {
630 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200631 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200632 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200633 }
634
Eric Miao6335d052009-03-03 09:41:00 +0800635 if (codec_dai->ops->prepare) {
636 ret = codec_dai->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200637 if (ret < 0) {
638 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200639 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200640 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200641 }
642
Eric Miao6335d052009-03-03 09:41:00 +0800643 if (cpu_dai->ops->prepare) {
644 ret = cpu_dai->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100645 if (ret < 0) {
646 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
647 goto out;
648 }
649 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200650
Mark Brownd45f6212008-10-14 13:58:36 +0100651 /* 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;
Mark Brown63084192008-12-02 15:08:03 +0000655 cancel_delayed_work(&card->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100656 }
657
Mark Brown452c5ea2009-05-17 21:41:23 +0100658 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);
Mark Brownd45f6212008-10-14 13:58:36 +0100666
Mark Brown452c5ea2009-05-17 21:41:23 +0100667 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200668
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;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100684 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000685 struct snd_soc_card *card = socdev->card;
686 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100687 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
688 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200689 int ret = 0;
690
691 mutex_lock(&pcm_mutex);
692
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100693 if (machine->ops && machine->ops->hw_params) {
694 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200695 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100696 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200697 goto out;
698 }
699 }
700
Eric Miao6335d052009-03-03 09:41:00 +0800701 if (codec_dai->ops->hw_params) {
702 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100703 if (ret < 0) {
704 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
705 codec_dai->name);
706 goto codec_err;
707 }
708 }
709
Eric Miao6335d052009-03-03 09:41:00 +0800710 if (cpu_dai->ops->hw_params) {
711 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200713 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100714 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715 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) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200722 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723 platform->name);
724 goto platform_err;
725 }
726 }
727
Mark Brown06f409d2009-04-07 18:10:13 +0100728 machine->rate = params_rate(params);
729
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730out:
731 mutex_unlock(&pcm_mutex);
732 return ret;
733
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200734platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800735 if (cpu_dai->ops->hw_free)
736 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200737
738interface_err:
Eric Miao6335d052009-03-03 09:41:00 +0800739 if (codec_dai->ops->hw_free)
740 codec_dai->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100741
742codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200743 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100744 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200745
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;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100757 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000758 struct snd_soc_card *card = socdev->card;
759 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100760 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
761 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000762 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200763
764 mutex_lock(&pcm_mutex);
765
766 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100767 if (!codec->active)
768 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200769
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 */
Eric Miao6335d052009-03-03 09:41:00 +0800779 if (codec_dai->ops->hw_free)
780 codec_dai->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200781
Eric Miao6335d052009-03-03 09:41:00 +0800782 if (cpu_dai->ops->hw_free)
783 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200784
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;
Mark Brown87689d52008-12-02 16:01:14 +0000793 struct snd_soc_card *card= socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100794 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000795 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100796 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
797 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200798 int ret;
799
Eric Miao6335d052009-03-03 09:41:00 +0800800 if (codec_dai->ops->trigger) {
801 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200802 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
Eric Miao6335d052009-03-03 09:41:00 +0800812 if (cpu_dai->ops->trigger) {
813 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200814 if (ret < 0)
815 return ret;
816 }
817 return 0;
818}
819
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200820/*
821 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200822 * If cpu_dai, codec_dai, platform driver has the delay callback, than
823 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200824 */
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;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200831 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;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200835 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200836 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200837
838 if (platform->pcm_ops->pointer)
839 offset = platform->pcm_ops->pointer(substream);
840
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200841 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
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200852 return offset;
853}
854
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200855/* 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,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200863 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200864};
865
866#ifdef CONFIG_PM
867/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100868static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200869{
Mark Brown416356f2009-06-30 19:05:15 +0100870 struct platform_device *pdev = to_platform_device(dev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200871 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000872 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000873 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200874 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000875 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200876 int i;
877
Daniel Macke3509ff2009-06-03 17:44:49 +0200878 /* 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
Andy Green6ed25972008-06-13 16:24:05 +0100884 /* 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
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200894 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000895 for (i = 0; i < card->num_links; i++) {
896 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100897
898 if (card->dai_link[i].ignore_suspend)
899 continue;
900
Eric Miao6335d052009-03-03 09:41:00 +0800901 if (dai->ops->digital_mute && dai->playback.active)
902 dai->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200903 }
904
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100905 /* suspend all pcms */
Mark Brown3efab7d2010-05-09 13:25:43 +0100906 for (i = 0; i < card->num_links; i++) {
907 if (card->dai_link[i].ignore_suspend)
908 continue;
909
Mark Brown87506542008-11-18 20:50:34 +0000910 snd_pcm_suspend_all(card->dai_link[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100911 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100912
Mark Brown87506542008-11-18 20:50:34 +0000913 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100914 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200915
Mark Brown87506542008-11-18 20:50:34 +0000916 for (i = 0; i < card->num_links; i++) {
917 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100918
919 if (card->dai_link[i].ignore_suspend)
920 continue;
921
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000922 if (cpu_dai->suspend && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000923 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200924 if (platform->suspend)
jassi brard273ebe2010-02-22 15:58:04 +0900925 platform->suspend(&card->dai_link[i]);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200926 }
927
928 /* close any waiting streams and save state */
Mark Brown63084192008-12-02 15:08:03 +0000929 run_delayed_work(&card->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200930 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200931
Mark Brown3ff3f642008-05-19 12:32:25 +0200932 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200933 char *stream = codec->dai[i].playback.stream_name;
Mark Brown3efab7d2010-05-09 13:25:43 +0100934
935 if (card->dai_link[i].ignore_suspend)
936 continue;
937
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200938 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
Mark Brown1547aba2010-05-07 21:11:40 +0100947 /* 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 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200960
Mark Brown87506542008-11-18 20:50:34 +0000961 for (i = 0; i < card->num_links; i++) {
962 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100963
964 if (card->dai_link[i].ignore_suspend)
965 continue;
966
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000967 if (cpu_dai->suspend && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000968 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200969 }
970
Mark Brown87506542008-11-18 20:50:34 +0000971 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +0100972 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200973
974 return 0;
975}
976
Andy Green6ed25972008-06-13 16:24:05 +0100977/* 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)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200981{
Mark Brown63084192008-12-02 15:08:03 +0000982 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;
Mark Brown87689d52008-12-02 16:01:14 +0000986 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200987 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000988 struct snd_soc_codec *codec = card->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100989 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200990 int i;
991
Andy Green6ed25972008-06-13 16:24:05 +0100992 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
993 * so userspace apps are blocked from touching us
994 */
995
Mark Brownfde22f22008-11-24 18:08:18 +0000996 dev_dbg(socdev->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100997
Mark Brown9949788b2010-05-07 20:24:05 +0100998 /* Bring us up into D2 so that DAPM starts enabling things */
999 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D2);
1000
Mark Brown87506542008-11-18 20:50:34 +00001001 if (card->resume_pre)
1002 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001003
Mark Brown87506542008-11-18 20:50:34 +00001004 for (i = 0; i < card->num_links; i++) {
1005 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001006
1007 if (card->dai_link[i].ignore_suspend)
1008 continue;
1009
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001010 if (cpu_dai->resume && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +00001011 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001012 }
1013
Mark Brown1547aba2010-05-07 21:11:40 +01001014 /* 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 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001029
Mark Brown3ff3f642008-05-19 12:32:25 +02001030 for (i = 0; i < codec->num_dai; i++) {
1031 char *stream = codec->dai[i].playback.stream_name;
Mark Brown3efab7d2010-05-09 13:25:43 +01001032
1033 if (card->dai_link[i].ignore_suspend)
1034 continue;
1035
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001036 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
Mark Brown3ff3f642008-05-19 12:32:25 +02001045 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +00001046 for (i = 0; i < card->num_links; i++) {
1047 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001048
1049 if (card->dai_link[i].ignore_suspend)
1050 continue;
1051
Eric Miao6335d052009-03-03 09:41:00 +08001052 if (dai->ops->digital_mute && dai->playback.active)
1053 dai->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001054 }
1055
Mark Brown87506542008-11-18 20:50:34 +00001056 for (i = 0; i < card->num_links; i++) {
1057 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001058
1059 if (card->dai_link[i].ignore_suspend)
1060 continue;
1061
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001062 if (cpu_dai->resume && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +00001063 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001064 if (platform->resume)
jassi brard273ebe2010-02-22 15:58:04 +09001065 platform->resume(&card->dai_link[i]);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001066 }
1067
Mark Brown87506542008-11-18 20:50:34 +00001068 if (card->resume_post)
1069 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001070
Mark Brownfde22f22008-11-24 18:08:18 +00001071 dev_dbg(socdev->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001072
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 */
Mark Brown416356f2009-06-30 19:05:15 +01001078static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001079{
Mark Brown416356f2009-06-30 19:05:15 +01001080 struct platform_device *pdev = to_platform_device(dev);
Andy Green6ed25972008-06-13 16:24:05 +01001081 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown63084192008-12-02 15:08:03 +00001082 struct snd_soc_card *card = socdev->card;
Mark Brown64ab9ba2009-03-31 11:27:03 +01001083 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
Andy Green6ed25972008-06-13 16:24:05 +01001084
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001085 /* 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
Mark Brown64ab9ba2009-03-31 11:27:03 +01001091 /* 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 }
Andy Green6ed25972008-06-13 16:24:05 +01001104
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001105 return 0;
1106}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001107#else
1108#define soc_suspend NULL
1109#define soc_resume NULL
1110#endif
1111
Barry Song02a06d32009-10-16 18:13:38 +08001112static struct snd_soc_dai_ops null_dai_ops = {
1113};
1114
Mark Brown435c5e22008-12-04 15:32:53 +00001115static void snd_soc_instantiate_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001116{
Mark Brown435c5e22008-12-04 15:32:53 +00001117 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;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001121 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001122 struct snd_soc_platform *platform;
1123 struct snd_soc_dai *dai;
Mark Brown6b05eda2008-12-08 19:26:48 +00001124 int i, found, ret, ac97;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001125
Mark Brown435c5e22008-12-04 15:32:53 +00001126 if (card->instantiated)
1127 return;
Mark Brown63084192008-12-02 15:08:03 +00001128
Mark Brown435c5e22008-12-04 15:32:53 +00001129 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;
Mark Brownc5af3a22008-11-28 13:29:45 +00001139 }
1140
Mark Brown6b05eda2008-12-08 19:26:48 +00001141 ac97 = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001142 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 }
Mark Brown6b05eda2008-12-08 19:26:48 +00001154
1155 if (card->dai_link[i].cpu_dai->ac97_control)
1156 ac97 = 1;
Mark Brown435c5e22008-12-04 15:32:53 +00001157 }
1158
Barry Song02a06d32009-10-16 18:13:38 +08001159 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
Mark Brown6b05eda2008-12-08 19:26:48 +00001164 /* 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
Mark Brown435c5e22008-12-04 15:32:53 +00001185 /* 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 */
Mark Brown96dd3622010-02-12 11:05:44 +00001190 card->pmdown_time = pmdown_time;
1191
Mark Brown87506542008-11-18 20:50:34 +00001192 if (card->probe) {
1193 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001194 if (ret < 0)
Mark Brown435c5e22008-12-04 15:32:53 +00001195 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001196 }
1197
Mark Brown87506542008-11-18 20:50:34 +00001198 for (i = 0; i < card->num_links; i++) {
1199 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001200 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +01001201 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +02001202 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001203 goto cpu_dai_err;
1204 }
1205 }
1206
1207 if (codec_dev->probe) {
1208 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001209 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001210 goto cpu_dai_err;
1211 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001212 codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001213
1214 if (platform->probe) {
1215 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001216 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001217 goto platform_err;
1218 }
1219
1220 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +00001221 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +01001222#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001223 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001224 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001225#endif
Andy Green6ed25972008-06-13 16:24:05 +01001226
Mark Brownfe3e78e2009-11-03 22:13:13 +00001227 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 }
Barry Songf7732052009-11-12 12:01:47 +08001236 if (card->dai_link[i].codec_dai->ac97_control)
Mark Brownfe3e78e2009-11-03 22:13:13 +00001237 ac97 = 1;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001238 }
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
Mark Browndbe21402010-02-12 11:37:24 +00001275 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
Mark Brownfe3e78e2009-11-03 22:13:13 +00001279 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
Mark Brown435c5e22008-12-04 15:32:53 +00001286 card->instantiated = 1;
1287
1288 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001289
Mark Brownfe3e78e2009-11-03 22:13:13 +00001290card_err:
1291 if (platform->remove)
1292 platform->remove(pdev);
1293
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001294platform_err:
1295 if (codec_dev->remove)
1296 codec_dev->remove(pdev);
1297
1298cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +01001299 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +00001300 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001301 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +01001302 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001303 }
1304
Mark Brown87506542008-11-18 20:50:34 +00001305 if (card->remove)
1306 card->remove(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001307}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001308
Mark Brown435c5e22008-12-04 15:32:53 +00001309/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001310 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001311 * 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;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001339}
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);
Mark Brown87506542008-11-18 20:50:34 +00001346 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +00001347 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001348 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1349
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001350 if (card->instantiated) {
1351 run_delayed_work(&card->delayed_work);
Mike Rapoport914dc182009-05-11 13:04:55 +03001352
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001353 if (platform->remove)
1354 platform->remove(pdev);
Liam Girdwood965ac422007-01-31 14:14:57 +01001355
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001356 if (codec_dev->remove)
1357 codec_dev->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001358
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001359 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 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001364
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001365 if (card->remove)
1366 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001367 }
1368
Mark Brownc5af3a22008-11-28 13:29:45 +00001369 snd_soc_unregister_card(card);
1370
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001371 return 0;
1372}
1373
Mark Brown416356f2009-06-30 19:05:15 +01001374static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001375{
Mark Brown416356f2009-06-30 19:05:15 +01001376 struct platform_device *pdev = to_platform_device(dev);
Mark Brown51737472009-06-22 13:16:51 +01001377 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1378 struct snd_soc_card *card = socdev->card;
1379
1380 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001381 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001382
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);
Mark Brown416356f2009-06-30 19:05:15 +01001388
1389 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001390}
1391
Alexey Dobriyan47145212009-12-14 18:00:08 -08001392static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001393 .suspend = soc_suspend,
1394 .resume = soc_resume,
1395 .poweroff = soc_poweroff,
1396};
1397
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001398/* ASoC platform driver */
1399static struct platform_driver soc_driver = {
1400 .driver = {
1401 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001402 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001403 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001404 },
1405 .probe = soc_probe,
1406 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001407};
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{
Mark Brown87689d52008-12-02 16:01:14 +00001413 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001414 struct snd_soc_codec *codec = card->codec;
Mark Brown87689d52008-12-02 16:01:14 +00001415 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001416 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1417 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001418 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;
Liam Girdwoodcb666e52007-02-02 17:13:49 +01001426
1427 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001428 rtd->socdev = socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001429 codec_dai->codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001430
1431 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001432 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1433 dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001434
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) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001443 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1444 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001445 kfree(rtd);
1446 return ret;
1447 }
1448
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001449 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001450 pcm->private_data = rtd;
Mark Brown87689d52008-12-02 16:01:14 +00001451 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
Mark Brown87689d52008-12-02 16:01:14 +00001452 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;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001457
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
Mark Brown87689d52008-12-02 16:01:14 +00001464 ret = platform->pcm_new(codec->card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001465 if (ret < 0) {
1466 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1467 kfree(rtd);
1468 return ret;
1469 }
1470
Mark Brown87689d52008-12-02 16:01:14 +00001471 pcm->private_free = platform->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001472 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1473 cpu_dai->name);
1474 return ret;
1475}
1476
Mark Brown096e49d2009-07-05 15:12:22 +01001477/**
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
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001494/**
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;
Mark Brown27186252010-01-28 12:36:29 +00001523 codec->dev = &codec->ac97->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001524 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,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001557 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001558{
1559 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001560 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001561
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001562 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
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001568 return change;
1569}
1570EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1571
1572/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001573 * 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 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001583int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1584 unsigned short reg, unsigned int mask,
1585 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001586{
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}
Mark Browndd1b3d52009-12-04 14:22:03 +00001595EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001596
1597/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001598 * 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,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001610 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001611{
1612 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001613 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001614
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001615 old = snd_soc_read(codec, reg);
1616 new = (old & ~mask) | value;
1617 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001618
1619 return change;
1620}
1621EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1622
1623/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001624 * snd_soc_new_pcms - create new sound card and pcms
1625 * @socdev: the SoC audio device
Mark Brownac11a2b2009-01-01 12:18:17 +00001626 * @idx: ALSA card index
1627 * @xid: card identification
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001628 *
1629 * Create a new sound card based upon the codec and interface pcms.
1630 *
1631 * Returns 0 for success, else error.
1632 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001633int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001634{
Mark Brown87506542008-11-18 20:50:34 +00001635 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001636 struct snd_soc_codec *codec = card->codec;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001637 int ret, i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001638
1639 mutex_lock(&codec->mutex);
1640
1641 /* register a sound card */
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001642 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1643 if (ret < 0) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001644 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1645 codec->name);
1646 mutex_unlock(&codec->mutex);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001647 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001648 }
1649
Mark Brown452c5ea2009-05-17 21:41:23 +01001650 codec->socdev = socdev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001651 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 */
Mark Brown87506542008-11-18 20:50:34 +00001656 for (i = 0; i < card->num_links; i++) {
1657 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001658 if (ret < 0) {
1659 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001660 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001661 mutex_unlock(&codec->mutex);
1662 return ret;
1663 }
Graham Gowerfb48e3c2010-03-25 10:52:12 +10301664 /* Check for codec->ac97 to handle the ac97.c fun */
1665 if (card->dai_link[i].codec_dai->ac97_control && codec->ac97) {
Barry Songf7732052009-11-12 12:01:47 +08001666 snd_ac97_dev_add_pdata(codec->ac97,
1667 card->dai_link[i].cpu_dai->ac97_pdata);
1668 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001669 }
1670
1671 mutex_unlock(&codec->mutex);
1672 return ret;
1673}
1674EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1675
1676/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001677 * 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{
Mark Brown6627a652009-01-23 22:55:23 +00001685 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001686#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001687 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001688 int i;
1689#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001690
1691 mutex_lock(&codec->mutex);
Mark Brown6627a652009-01-23 22:55:23 +00001692 soc_cleanup_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001693#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001694 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001695 codec_dai = &codec->dai[i];
Atsushi Nemotod2314e02009-03-16 23:26:20 +09001696 if (codec_dai->ac97_control && codec->ac97 &&
1697 strcmp(codec->name, "AC97") != 0) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001698 soc_ac97_dev_unregister(codec);
1699 goto free_card;
1700 }
1701 }
1702free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001703#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
Mark Brownac11a2b2009-01-01 12:18:17 +00001739 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001740 *
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;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001753 template.index = 0;
1754
1755 return snd_ctl_new1(&template, data);
1756}
1757EXPORT_SYMBOL_GPL(snd_soc_cnew);
1758
1759/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001760 * 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
1790/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001791 * 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;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001807 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001808
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001809 if (uinfo->value.enumerated.item > e->max - 1)
1810 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001811 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
Mark Brownac11a2b2009-01-01 12:18:17 +00001820 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001821 *
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;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001831 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001832
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001833 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001834 ;
1835 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001836 ucontrol->value.enumerated.item[0]
1837 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001838 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
Mark Brownac11a2b2009-01-01 12:18:17 +00001849 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001850 *
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;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001860 unsigned int val;
1861 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001862
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001863 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001864 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001865 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001866 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) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001870 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001871 return -EINVAL;
1872 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1873 mask |= (bitmask - 1) << e->shift_r;
1874 }
1875
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001876 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001877}
1878EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1879
1880/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001881 * 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);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001896 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001897 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001898
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);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001935 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001936 unsigned int val;
1937 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001938
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
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001950 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001951}
1952EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1953
1954/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001955 * 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;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001971 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001972
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001973 if (uinfo->value.enumerated.item > e->max - 1)
1974 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001975 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{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001993 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001994
Mark Brownfd5dfad2009-04-15 21:37:46 +01001995 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001996 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1997 else
1998 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1999
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002000 uinfo->count = 1;
2001 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002002 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002003 return 0;
2004}
2005EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2006
2007/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002008 * 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{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002019 struct soc_mixer_control *mc =
2020 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002021 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002022 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002023 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002024
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002025 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"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002030 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2031 else
2032 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2033
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002034 uinfo->count = shift == rshift ? 1 : 2;
2035 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002036 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002037 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
Mark Brownac11a2b2009-01-01 12:18:17 +00002044 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002045 *
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{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002053 struct soc_mixer_control *mc =
2054 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002055 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002056 unsigned int reg = mc->reg;
2057 unsigned int shift = mc->shift;
2058 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002059 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002060 unsigned int mask = (1 << fls(max)) - 1;
2061 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002062
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] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002070 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002071 if (shift != rshift)
2072 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002073 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002074 }
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
Mark Brownac11a2b2009-01-01 12:18:17 +00002083 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002084 *
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{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002092 struct soc_mixer_control *mc =
2093 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002094 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002095 unsigned int reg = mc->reg;
2096 unsigned int shift = mc->shift;
2097 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002098 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002099 unsigned int mask = (1 << fls(max)) - 1;
2100 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002101 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002102
2103 val = (ucontrol->value.integer.value[0] & mask);
2104 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002105 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002106 val_mask = mask << shift;
2107 val = val << shift;
2108 if (shift != rshift) {
2109 val2 = (ucontrol->value.integer.value[1] & mask);
2110 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002111 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002112 val_mask |= mask << rshift;
2113 val |= val2 << rshift;
2114 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002115 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002116}
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{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002132 struct soc_mixer_control *mc =
2133 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002134 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002135
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002136 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"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002141 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2142 else
2143 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2144
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002145 uinfo->count = 2;
2146 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002147 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002148 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
Mark Brownac11a2b2009-01-01 12:18:17 +00002155 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002156 *
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{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002164 struct soc_mixer_control *mc =
2165 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002166 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002167 unsigned int reg = mc->reg;
2168 unsigned int reg2 = mc->rreg;
2169 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002170 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002171 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002172 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002173
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] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002180 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002181 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002182 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002183 }
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
Mark Brownac11a2b2009-01-01 12:18:17 +00002192 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002193 *
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{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002201 struct soc_mixer_control *mc =
2202 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002203 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002204 unsigned int reg = mc->reg;
2205 unsigned int reg2 = mc->rreg;
2206 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002207 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002208 unsigned int mask = (1 << fls(max)) - 1;
2209 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002210 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002211 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002212
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) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002218 val = max - val;
2219 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002220 }
2221
2222 val = val << shift;
2223 val2 = val2 << shift;
2224
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002225 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002226 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002227 return err;
2228
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002229 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002230 return err;
2231}
2232EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2233
Mark Browne13ac2e2008-05-28 17:58:05 +01002234/**
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{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002246 struct soc_mixer_control *mc =
2247 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002248 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002249 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002250
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002251 if (!mc->platform_max)
2252 mc->platform_max = mc->max;
2253 platform_max = mc->platform_max;
2254
Mark Browne13ac2e2008-05-28 17:58:05 +01002255 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2256 uinfo->count = 2;
2257 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002258 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002259 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
Mark Brownac11a2b2009-01-01 12:18:17 +00002266 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002267 *
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{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002275 struct soc_mixer_control *mc =
2276 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002277 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002278 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002279 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002280 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
Mark Brownac11a2b2009-01-01 12:18:17 +00002293 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002294 *
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{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002302 struct soc_mixer_control *mc =
2303 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002304 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002305 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002306 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002307 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002308
2309 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2310 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2311
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002312 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002313}
2314EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2315
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002316/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002317 * 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) {
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002347 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002348 ret = 0;
2349 }
2350 }
2351 return ret;
2352}
2353EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2354
2355/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002356 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2357 * @dai: DAI
2358 * @clk_id: DAI specific clock ID
2359 * @freq: new clock frequency in Hz
2360 * @dir: new clock direction - input/output.
2361 *
2362 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2363 */
2364int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2365 unsigned int freq, int dir)
2366{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002367 if (dai->ops && dai->ops->set_sysclk)
Eric Miao6335d052009-03-03 09:41:00 +08002368 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002369 else
2370 return -EINVAL;
2371}
2372EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2373
2374/**
2375 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2376 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002377 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002378 * @div: new clock divisor.
2379 *
2380 * Configures the clock dividers. This is used to derive the best DAI bit and
2381 * frame clocks from the system or master clock. It's best to set the DAI bit
2382 * and frame clocks as low as possible to save system power.
2383 */
2384int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2385 int div_id, int div)
2386{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002387 if (dai->ops && dai->ops->set_clkdiv)
Eric Miao6335d052009-03-03 09:41:00 +08002388 return dai->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002389 else
2390 return -EINVAL;
2391}
2392EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2393
2394/**
2395 * snd_soc_dai_set_pll - configure DAI PLL.
2396 * @dai: DAI
2397 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002398 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002399 * @freq_in: PLL input clock frequency in Hz
2400 * @freq_out: requested PLL output clock frequency in Hz
2401 *
2402 * Configures and enables PLL to generate output clock based on input clock.
2403 */
Mark Brown85488032009-09-05 18:52:16 +01002404int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2405 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002406{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002407 if (dai->ops && dai->ops->set_pll)
Mark Brown85488032009-09-05 18:52:16 +01002408 return dai->ops->set_pll(dai, pll_id, source,
2409 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002410 else
2411 return -EINVAL;
2412}
2413EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2414
2415/**
2416 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2417 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002418 * @fmt: SND_SOC_DAIFMT_ format value.
2419 *
2420 * Configures the DAI hardware format and clocking.
2421 */
2422int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2423{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002424 if (dai->ops && dai->ops->set_fmt)
Eric Miao6335d052009-03-03 09:41:00 +08002425 return dai->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002426 else
2427 return -EINVAL;
2428}
2429EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2430
2431/**
2432 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2433 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002434 * @tx_mask: bitmask representing active TX slots.
2435 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002436 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002437 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002438 *
2439 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2440 * specific.
2441 */
2442int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002443 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002444{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002445 if (dai->ops && dai->ops->set_tdm_slot)
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002446 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2447 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002448 else
2449 return -EINVAL;
2450}
2451EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2452
2453/**
Barry Song472df3c2009-09-12 01:16:29 +08002454 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2455 * @dai: DAI
2456 * @tx_num: how many TX channels
2457 * @tx_slot: pointer to an array which imply the TX slot number channel
2458 * 0~num-1 uses
2459 * @rx_num: how many RX channels
2460 * @rx_slot: pointer to an array which imply the RX slot number channel
2461 * 0~num-1 uses
2462 *
2463 * configure the relationship between channel number and TDM slot number.
2464 */
2465int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2466 unsigned int tx_num, unsigned int *tx_slot,
2467 unsigned int rx_num, unsigned int *rx_slot)
2468{
2469 if (dai->ops && dai->ops->set_channel_map)
2470 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2471 rx_num, rx_slot);
2472 else
2473 return -EINVAL;
2474}
2475EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2476
2477/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002478 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2479 * @dai: DAI
2480 * @tristate: tristate enable
2481 *
2482 * Tristates the DAI so that others can use it.
2483 */
2484int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2485{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002486 if (dai->ops && dai->ops->set_tristate)
Eric Miao6335d052009-03-03 09:41:00 +08002487 return dai->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002488 else
2489 return -EINVAL;
2490}
2491EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2492
2493/**
2494 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2495 * @dai: DAI
2496 * @mute: mute enable
2497 *
2498 * Mutes the DAI DAC.
2499 */
2500int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2501{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002502 if (dai->ops && dai->ops->digital_mute)
Eric Miao6335d052009-03-03 09:41:00 +08002503 return dai->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002504 else
2505 return -EINVAL;
2506}
2507EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2508
Mark Brownc5af3a22008-11-28 13:29:45 +00002509/**
2510 * snd_soc_register_card - Register a card with the ASoC core
2511 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002512 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002513 *
2514 * Note that currently this is an internal only function: it will be
2515 * exposed to machine drivers after further backporting of ASoC v2
2516 * registration APIs.
2517 */
2518static int snd_soc_register_card(struct snd_soc_card *card)
2519{
2520 if (!card->name || !card->dev)
2521 return -EINVAL;
2522
2523 INIT_LIST_HEAD(&card->list);
2524 card->instantiated = 0;
2525
2526 mutex_lock(&client_mutex);
2527 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002528 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002529 mutex_unlock(&client_mutex);
2530
2531 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2532
2533 return 0;
2534}
2535
2536/**
2537 * snd_soc_unregister_card - Unregister a card with the ASoC core
2538 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002539 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002540 *
2541 * Note that currently this is an internal only function: it will be
2542 * exposed to machine drivers after further backporting of ASoC v2
2543 * registration APIs.
2544 */
2545static int snd_soc_unregister_card(struct snd_soc_card *card)
2546{
2547 mutex_lock(&client_mutex);
2548 list_del(&card->list);
2549 mutex_unlock(&client_mutex);
2550
2551 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2552
2553 return 0;
2554}
2555
Mark Brown91151712008-11-30 23:31:24 +00002556/**
2557 * snd_soc_register_dai - Register a DAI with the ASoC core
2558 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002559 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002560 */
2561int snd_soc_register_dai(struct snd_soc_dai *dai)
2562{
2563 if (!dai->name)
2564 return -EINVAL;
2565
2566 /* The device should become mandatory over time */
2567 if (!dai->dev)
2568 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2569
Eric Miao6335d052009-03-03 09:41:00 +08002570 if (!dai->ops)
2571 dai->ops = &null_dai_ops;
2572
Mark Brown91151712008-11-30 23:31:24 +00002573 INIT_LIST_HEAD(&dai->list);
2574
2575 mutex_lock(&client_mutex);
2576 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002577 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002578 mutex_unlock(&client_mutex);
2579
2580 pr_debug("Registered DAI '%s'\n", dai->name);
2581
2582 return 0;
2583}
2584EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2585
2586/**
2587 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2588 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002589 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002590 */
2591void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2592{
2593 mutex_lock(&client_mutex);
2594 list_del(&dai->list);
2595 mutex_unlock(&client_mutex);
2596
2597 pr_debug("Unregistered DAI '%s'\n", dai->name);
2598}
2599EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2600
2601/**
2602 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2603 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002604 * @dai: Array of DAIs to register
2605 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002606 */
2607int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2608{
2609 int i, ret;
2610
2611 for (i = 0; i < count; i++) {
2612 ret = snd_soc_register_dai(&dai[i]);
2613 if (ret != 0)
2614 goto err;
2615 }
2616
2617 return 0;
2618
2619err:
2620 for (i--; i >= 0; i--)
2621 snd_soc_unregister_dai(&dai[i]);
2622
2623 return ret;
2624}
2625EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2626
2627/**
2628 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2629 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002630 * @dai: Array of DAIs to unregister
2631 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002632 */
2633void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2634{
2635 int i;
2636
2637 for (i = 0; i < count; i++)
2638 snd_soc_unregister_dai(&dai[i]);
2639}
2640EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2641
Mark Brown12a48a8c2008-12-03 19:40:30 +00002642/**
2643 * snd_soc_register_platform - Register a platform with the ASoC core
2644 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002645 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00002646 */
2647int snd_soc_register_platform(struct snd_soc_platform *platform)
2648{
2649 if (!platform->name)
2650 return -EINVAL;
2651
2652 INIT_LIST_HEAD(&platform->list);
2653
2654 mutex_lock(&client_mutex);
2655 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002656 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00002657 mutex_unlock(&client_mutex);
2658
2659 pr_debug("Registered platform '%s'\n", platform->name);
2660
2661 return 0;
2662}
2663EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2664
2665/**
2666 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2667 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002668 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002669 */
2670void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2671{
2672 mutex_lock(&client_mutex);
2673 list_del(&platform->list);
2674 mutex_unlock(&client_mutex);
2675
2676 pr_debug("Unregistered platform '%s'\n", platform->name);
2677}
2678EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2679
Mark Brown151ab222009-05-09 16:22:58 +01002680static u64 codec_format_map[] = {
2681 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2682 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2683 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2684 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2685 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2686 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2687 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2688 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2689 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2690 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2691 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2692 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2693 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2694 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2695 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2696 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2697};
2698
2699/* Fix up the DAI formats for endianness: codecs don't actually see
2700 * the endianness of the data but we're using the CPU format
2701 * definitions which do need to include endianness so we ensure that
2702 * codec DAIs always have both big and little endian variants set.
2703 */
2704static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2705{
2706 int i;
2707
2708 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2709 if (stream->formats & codec_format_map[i])
2710 stream->formats |= codec_format_map[i];
2711}
2712
Mark Brown0d0cf002008-12-10 14:32:45 +00002713/**
2714 * snd_soc_register_codec - Register a codec with the ASoC core
2715 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002716 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00002717 */
2718int snd_soc_register_codec(struct snd_soc_codec *codec)
2719{
Mark Brown151ab222009-05-09 16:22:58 +01002720 int i;
2721
Mark Brown0d0cf002008-12-10 14:32:45 +00002722 if (!codec->name)
2723 return -EINVAL;
2724
2725 /* The device should become mandatory over time */
2726 if (!codec->dev)
2727 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2728
2729 INIT_LIST_HEAD(&codec->list);
2730
Mark Brown151ab222009-05-09 16:22:58 +01002731 for (i = 0; i < codec->num_dai; i++) {
2732 fixup_codec_formats(&codec->dai[i].playback);
2733 fixup_codec_formats(&codec->dai[i].capture);
2734 }
2735
Mark Brown0d0cf002008-12-10 14:32:45 +00002736 mutex_lock(&client_mutex);
2737 list_add(&codec->list, &codec_list);
2738 snd_soc_instantiate_cards();
2739 mutex_unlock(&client_mutex);
2740
2741 pr_debug("Registered codec '%s'\n", codec->name);
2742
2743 return 0;
2744}
2745EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2746
2747/**
2748 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2749 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002750 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00002751 */
2752void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2753{
2754 mutex_lock(&client_mutex);
2755 list_del(&codec->list);
2756 mutex_unlock(&client_mutex);
2757
2758 pr_debug("Unregistered codec '%s'\n", codec->name);
2759}
2760EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2761
Takashi Iwaic9b3a402008-12-10 07:47:22 +01002762static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002763{
Mark Brown384c89e2008-12-03 17:34:03 +00002764#ifdef CONFIG_DEBUG_FS
2765 debugfs_root = debugfs_create_dir("asoc", NULL);
2766 if (IS_ERR(debugfs_root) || !debugfs_root) {
2767 printk(KERN_WARNING
2768 "ASoC: Failed to create debugfs directory\n");
2769 debugfs_root = NULL;
2770 }
2771#endif
2772
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002773 return platform_driver_register(&soc_driver);
2774}
2775
Mark Brown7d8c16a2008-11-30 22:11:24 +00002776static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002777{
Mark Brown384c89e2008-12-03 17:34:03 +00002778#ifdef CONFIG_DEBUG_FS
2779 debugfs_remove_recursive(debugfs_root);
2780#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02002781 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002782}
2783
2784module_init(snd_soc_init);
2785module_exit(snd_soc_exit);
2786
2787/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002788MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002789MODULE_DESCRIPTION("ALSA SoC Core");
2790MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02002791MODULE_ALIAS("platform:soc-audio");