blob: 844ae8221a3af65be95dcd848f925b26651a0640 [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{
Mark Brown5164d742010-07-14 16:14:33 +010087 int ret, i, step = 1, count = 0;
Mark Brown2624d5f2009-11-03 21:56:13 +000088
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
Mark Brown5164d742010-07-14 16:14:33 +0100104 if (codec->display_register) {
Mark Brown2624d5f2009-11-03 21:56:13 +0000105 count += codec->display_register(codec, buf + count,
106 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100107 } else {
108 /* If the read fails it's almost certainly due to
109 * the register being volatile and the device being
110 * powered off.
111 */
112 ret = codec->read(codec, i);
113 if (ret >= 0)
114 count += snprintf(buf + count,
115 PAGE_SIZE - count,
116 "%4x", ret);
117 else
118 count += snprintf(buf + count,
119 PAGE_SIZE - count,
120 "<no data: %d>", ret);
121 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000122
123 if (count >= PAGE_SIZE - 1)
124 break;
125
126 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
127 if (count >= PAGE_SIZE - 1)
128 break;
129 }
130
131 /* Truncate count; min() would cause a warning */
132 if (count >= PAGE_SIZE)
133 count = PAGE_SIZE - 1;
134
135 return count;
136}
137static ssize_t codec_reg_show(struct device *dev,
138 struct device_attribute *attr, char *buf)
139{
140 struct snd_soc_device *devdata = dev_get_drvdata(dev);
141 return soc_codec_reg_show(devdata->card->codec, buf);
142}
143
144static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
145
Mark Browndbe21402010-02-12 11:37:24 +0000146static ssize_t pmdown_time_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
148{
149 struct snd_soc_device *socdev = dev_get_drvdata(dev);
150 struct snd_soc_card *card = socdev->card;
151
Mark Brown6c5f1fe2010-02-17 14:30:44 +0000152 return sprintf(buf, "%ld\n", card->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000153}
154
155static ssize_t pmdown_time_set(struct device *dev,
156 struct device_attribute *attr,
157 const char *buf, size_t count)
158{
159 struct snd_soc_device *socdev = dev_get_drvdata(dev);
160 struct snd_soc_card *card = socdev->card;
161
162 strict_strtol(buf, 10, &card->pmdown_time);
163
164 return count;
165}
166
167static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
168
Mark Brown2624d5f2009-11-03 21:56:13 +0000169#ifdef CONFIG_DEBUG_FS
170static int codec_reg_open_file(struct inode *inode, struct file *file)
171{
172 file->private_data = inode->i_private;
173 return 0;
174}
175
176static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
177 size_t count, loff_t *ppos)
178{
179 ssize_t ret;
180 struct snd_soc_codec *codec = file->private_data;
181 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
182 if (!buf)
183 return -ENOMEM;
184 ret = soc_codec_reg_show(codec, buf);
185 if (ret >= 0)
186 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
187 kfree(buf);
188 return ret;
189}
190
191static ssize_t codec_reg_write_file(struct file *file,
192 const char __user *user_buf, size_t count, loff_t *ppos)
193{
194 char buf[32];
195 int buf_size;
196 char *start = buf;
197 unsigned long reg, value;
198 int step = 1;
199 struct snd_soc_codec *codec = file->private_data;
200
201 buf_size = min(count, (sizeof(buf)-1));
202 if (copy_from_user(buf, user_buf, buf_size))
203 return -EFAULT;
204 buf[buf_size] = 0;
205
206 if (codec->reg_cache_step)
207 step = codec->reg_cache_step;
208
209 while (*start == ' ')
210 start++;
211 reg = simple_strtoul(start, &start, 16);
212 if ((reg >= codec->reg_cache_size) || (reg % step))
213 return -EINVAL;
214 while (*start == ' ')
215 start++;
216 if (strict_strtoul(start, 16, &value))
217 return -EINVAL;
218 codec->write(codec, reg, value);
219 return buf_size;
220}
221
222static const struct file_operations codec_reg_fops = {
223 .open = codec_reg_open_file,
224 .read = codec_reg_read_file,
225 .write = codec_reg_write_file,
226};
227
228static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
229{
230 char codec_root[128];
231
232 if (codec->dev)
233 snprintf(codec_root, sizeof(codec_root),
234 "%s.%s", codec->name, dev_name(codec->dev));
235 else
236 snprintf(codec_root, sizeof(codec_root),
237 "%s", codec->name);
238
239 codec->debugfs_codec_root = debugfs_create_dir(codec_root,
240 debugfs_root);
241 if (!codec->debugfs_codec_root) {
242 printk(KERN_WARNING
243 "ASoC: Failed to create codec debugfs directory\n");
244 return;
245 }
246
247 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
248 codec->debugfs_codec_root,
249 codec, &codec_reg_fops);
250 if (!codec->debugfs_reg)
251 printk(KERN_WARNING
252 "ASoC: Failed to create codec register debugfs file\n");
253
254 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
255 codec->debugfs_codec_root,
256 &codec->pop_time);
257 if (!codec->debugfs_pop_time)
258 printk(KERN_WARNING
259 "Failed to create pop time debugfs file\n");
260
261 codec->debugfs_dapm = debugfs_create_dir("dapm",
262 codec->debugfs_codec_root);
263 if (!codec->debugfs_dapm)
264 printk(KERN_WARNING
265 "Failed to create DAPM debugfs directory\n");
266
267 snd_soc_dapm_debugfs_init(codec);
268}
269
270static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
271{
272 debugfs_remove_recursive(codec->debugfs_codec_root);
273}
274
275#else
276
277static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
278{
279}
280
281static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
282{
283}
284#endif
285
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200286#ifdef CONFIG_SND_SOC_AC97_BUS
287/* unregister ac97 codec */
288static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
289{
290 if (codec->ac97->dev.bus)
291 device_unregister(&codec->ac97->dev);
292 return 0;
293}
294
295/* stop no dev release warning */
296static void soc_ac97_device_release(struct device *dev){}
297
298/* register ac97 codec to bus */
299static int soc_ac97_dev_register(struct snd_soc_codec *codec)
300{
301 int err;
302
303 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100304 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200305 codec->ac97->dev.release = soc_ac97_device_release;
306
Kay Sieversbb072bf2008-11-02 03:50:35 +0100307 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
308 codec->card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200309 err = device_register(&codec->ac97->dev);
310 if (err < 0) {
311 snd_printk(KERN_ERR "Can't register ac97 bus\n");
312 codec->ac97->dev.bus = NULL;
313 return err;
314 }
315 return 0;
316}
317#endif
318
Mark Brown06f409d2009-04-07 18:10:13 +0100319static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
320{
321 struct snd_soc_pcm_runtime *rtd = substream->private_data;
322 struct snd_soc_device *socdev = rtd->socdev;
323 struct snd_soc_card *card = socdev->card;
324 struct snd_soc_dai_link *machine = rtd->dai;
325 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
326 struct snd_soc_dai *codec_dai = machine->codec_dai;
327 int ret;
328
329 if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
330 machine->symmetric_rates) {
Peter Ujfalusi50831452010-03-03 15:08:04 +0200331 dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
Mark Brown06f409d2009-04-07 18:10:13 +0100332 machine->rate);
333
334 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
335 SNDRV_PCM_HW_PARAM_RATE,
336 machine->rate,
337 machine->rate);
338 if (ret < 0) {
339 dev_err(card->dev,
340 "Unable to apply rate symmetry constraint: %d\n", ret);
341 return ret;
342 }
343 }
344
345 return 0;
346}
347
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200348/*
349 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
350 * then initialized and any private data can be allocated. This also calls
351 * startup for the cpu DAI, platform, machine and codec DAI.
352 */
353static int soc_pcm_open(struct snd_pcm_substream *substream)
354{
355 struct snd_soc_pcm_runtime *rtd = substream->private_data;
356 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000357 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200358 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100359 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000360 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100361 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
362 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200363 int ret = 0;
364
365 mutex_lock(&pcm_mutex);
366
367 /* startup the audio subsystem */
Eric Miao6335d052009-03-03 09:41:00 +0800368 if (cpu_dai->ops->startup) {
369 ret = cpu_dai->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200370 if (ret < 0) {
371 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100372 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200373 goto out;
374 }
375 }
376
377 if (platform->pcm_ops->open) {
378 ret = platform->pcm_ops->open(substream);
379 if (ret < 0) {
380 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
381 goto platform_err;
382 }
383 }
384
Eric Miao6335d052009-03-03 09:41:00 +0800385 if (codec_dai->ops->startup) {
386 ret = codec_dai->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100387 if (ret < 0) {
388 printk(KERN_ERR "asoc: can't open codec %s\n",
389 codec_dai->name);
390 goto codec_dai_err;
391 }
392 }
393
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200394 if (machine->ops && machine->ops->startup) {
395 ret = machine->ops->startup(substream);
396 if (ret < 0) {
397 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
398 goto machine_err;
399 }
400 }
401
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200402 /* Check that the codec and cpu DAI's are compatible */
403 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
404 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200405 max(codec_dai->playback.rate_min,
406 cpu_dai->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200407 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200408 min(codec_dai->playback.rate_max,
409 cpu_dai->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200410 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100411 max(codec_dai->playback.channels_min,
412 cpu_dai->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200413 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100414 min(codec_dai->playback.channels_max,
415 cpu_dai->playback.channels_max);
416 runtime->hw.formats =
417 codec_dai->playback.formats & cpu_dai->playback.formats;
418 runtime->hw.rates =
419 codec_dai->playback.rates & cpu_dai->playback.rates;
Jassi Brard9ad6292010-03-12 13:38:52 +0900420 if (codec_dai->playback.rates
421 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
422 runtime->hw.rates |= cpu_dai->playback.rates;
423 if (cpu_dai->playback.rates
424 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
425 runtime->hw.rates |= codec_dai->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200426 } else {
427 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200428 max(codec_dai->capture.rate_min,
429 cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200430 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200431 min(codec_dai->capture.rate_max,
432 cpu_dai->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200433 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100434 max(codec_dai->capture.channels_min,
435 cpu_dai->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200436 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100437 min(codec_dai->capture.channels_max,
438 cpu_dai->capture.channels_max);
439 runtime->hw.formats =
440 codec_dai->capture.formats & cpu_dai->capture.formats;
441 runtime->hw.rates =
442 codec_dai->capture.rates & cpu_dai->capture.rates;
Jassi Brard9ad6292010-03-12 13:38:52 +0900443 if (codec_dai->capture.rates
444 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
445 runtime->hw.rates |= cpu_dai->capture.rates;
446 if (cpu_dai->capture.rates
447 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
448 runtime->hw.rates |= codec_dai->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200449 }
450
451 snd_pcm_limit_hw_rates(runtime);
452 if (!runtime->hw.rates) {
453 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100454 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900455 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200456 }
457 if (!runtime->hw.formats) {
458 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100459 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900460 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200461 }
462 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
463 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100464 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900465 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200466 }
467
Mark Brown06f409d2009-04-07 18:10:13 +0100468 /* Symmetry only applies if we've already got an active stream. */
469 if (cpu_dai->active || codec_dai->active) {
470 ret = soc_pcm_apply_symmetry(substream);
471 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900472 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100473 }
474
Mark Brownf24368c2008-10-21 21:45:08 +0100475 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
476 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
477 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
478 runtime->hw.channels_max);
479 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
480 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200481
Jassi Brar14dc5732010-02-26 09:12:32 +0900482 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
483 cpu_dai->playback.active++;
484 codec_dai->playback.active++;
485 } else {
486 cpu_dai->capture.active++;
487 codec_dai->capture.active++;
488 }
489 cpu_dai->active++;
490 codec_dai->active++;
Mark Brown6627a652009-01-23 22:55:23 +0000491 card->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200492 mutex_unlock(&pcm_mutex);
493 return 0;
494
Jassi Brarbb1c0472010-02-25 11:24:53 +0900495config_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496 if (machine->ops && machine->ops->shutdown)
497 machine->ops->shutdown(substream);
498
Jassi Brarbb1c0472010-02-25 11:24:53 +0900499machine_err:
500 if (codec_dai->ops->shutdown)
501 codec_dai->ops->shutdown(substream, codec_dai);
502
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100503codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504 if (platform->pcm_ops->close)
505 platform->pcm_ops->close(substream);
506
507platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800508 if (cpu_dai->ops->shutdown)
509 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510out:
511 mutex_unlock(&pcm_mutex);
512 return ret;
513}
514
515/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200516 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200517 * This is to ensure there are no pops or clicks in between any music tracks
518 * due to DAPM power cycling.
519 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100520static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200521{
Mark Brown63084192008-12-02 15:08:03 +0000522 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
523 delayed_work.work);
Mark Brown6627a652009-01-23 22:55:23 +0000524 struct snd_soc_codec *codec = card->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100525 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200526 int i;
527
528 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200529 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200530 codec_dai = &codec->dai[i];
531
Mark Brownf24368c2008-10-21 21:45:08 +0100532 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
533 codec_dai->playback.stream_name,
534 codec_dai->playback.active ? "active" : "inactive",
535 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536
537 /* are we waiting on this codec DAI stream */
538 if (codec_dai->pop_wait == 1) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200539 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100540 snd_soc_dapm_stream_event(codec,
541 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200542 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200543 }
544 }
545 mutex_unlock(&pcm_mutex);
546}
547
548/*
549 * Called by ALSA when a PCM substream is closed. Private data can be
550 * freed here. The cpu DAI, codec DAI, machine and platform are also
551 * shutdown.
552 */
553static int soc_codec_close(struct snd_pcm_substream *substream)
554{
555 struct snd_soc_pcm_runtime *rtd = substream->private_data;
556 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000557 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100558 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000559 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100560 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
561 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000562 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200563
564 mutex_lock(&pcm_mutex);
565
Jassi Brar14dc5732010-02-26 09:12:32 +0900566 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
567 cpu_dai->playback.active--;
568 codec_dai->playback.active--;
569 } else {
570 cpu_dai->capture.active--;
571 codec_dai->capture.active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200572 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900573
574 cpu_dai->active--;
575 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200576 codec->active--;
577
Mark Brown6010b2d2008-09-06 18:33:24 +0100578 /* Muting the DAC suppresses artifacts caused during digital
579 * shutdown, for example from stopping clocks.
580 */
581 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
582 snd_soc_dai_digital_mute(codec_dai, 1);
583
Eric Miao6335d052009-03-03 09:41:00 +0800584 if (cpu_dai->ops->shutdown)
585 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586
Eric Miao6335d052009-03-03 09:41:00 +0800587 if (codec_dai->ops->shutdown)
588 codec_dai->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200589
590 if (machine->ops && machine->ops->shutdown)
591 machine->ops->shutdown(substream);
592
593 if (platform->pcm_ops->close)
594 platform->pcm_ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200595
596 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
597 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100598 codec_dai->pop_wait = 1;
Mark Brown63084192008-12-02 15:08:03 +0000599 schedule_delayed_work(&card->delayed_work,
Mark Brown96dd3622010-02-12 11:05:44 +0000600 msecs_to_jiffies(card->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200601 } else {
602 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100603 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100604 codec_dai->capture.stream_name,
605 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200606 }
607
608 mutex_unlock(&pcm_mutex);
609 return 0;
610}
611
612/*
613 * Called by ALSA when the PCM substream is prepared, can set format, sample
614 * rate, etc. This function is non atomic and can be called multiple times,
615 * it can refer to the runtime info.
616 */
617static int soc_pcm_prepare(struct snd_pcm_substream *substream)
618{
619 struct snd_soc_pcm_runtime *rtd = substream->private_data;
620 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000621 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100622 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000623 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100624 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
625 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000626 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200627 int ret = 0;
628
629 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100630
631 if (machine->ops && machine->ops->prepare) {
632 ret = machine->ops->prepare(substream);
633 if (ret < 0) {
634 printk(KERN_ERR "asoc: machine prepare error\n");
635 goto out;
636 }
637 }
638
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200639 if (platform->pcm_ops->prepare) {
640 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200641 if (ret < 0) {
642 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200643 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200644 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200645 }
646
Eric Miao6335d052009-03-03 09:41:00 +0800647 if (codec_dai->ops->prepare) {
648 ret = codec_dai->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200649 if (ret < 0) {
650 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200651 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200652 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200653 }
654
Eric Miao6335d052009-03-03 09:41:00 +0800655 if (cpu_dai->ops->prepare) {
656 ret = cpu_dai->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100657 if (ret < 0) {
658 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
659 goto out;
660 }
661 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200662
Mark Brownd45f6212008-10-14 13:58:36 +0100663 /* cancel any delayed stream shutdown that is pending */
664 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
665 codec_dai->pop_wait) {
666 codec_dai->pop_wait = 0;
Mark Brown63084192008-12-02 15:08:03 +0000667 cancel_delayed_work(&card->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100668 }
669
Mark Brown452c5ea2009-05-17 21:41:23 +0100670 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
671 snd_soc_dapm_stream_event(codec,
672 codec_dai->playback.stream_name,
673 SND_SOC_DAPM_STREAM_START);
674 else
675 snd_soc_dapm_stream_event(codec,
676 codec_dai->capture.stream_name,
677 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100678
Mark Brown452c5ea2009-05-17 21:41:23 +0100679 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680
681out:
682 mutex_unlock(&pcm_mutex);
683 return ret;
684}
685
686/*
687 * Called by ALSA when the hardware params are set by application. This
688 * function can also be called multiple times and can allocate buffers
689 * (using snd_pcm_lib_* ). It's non-atomic.
690 */
691static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
692 struct snd_pcm_hw_params *params)
693{
694 struct snd_soc_pcm_runtime *rtd = substream->private_data;
695 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100696 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000697 struct snd_soc_card *card = socdev->card;
698 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100699 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
700 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200701 int ret = 0;
702
703 mutex_lock(&pcm_mutex);
704
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100705 if (machine->ops && machine->ops->hw_params) {
706 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200707 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100708 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200709 goto out;
710 }
711 }
712
Eric Miao6335d052009-03-03 09:41:00 +0800713 if (codec_dai->ops->hw_params) {
714 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100715 if (ret < 0) {
716 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
717 codec_dai->name);
718 goto codec_err;
719 }
720 }
721
Eric Miao6335d052009-03-03 09:41:00 +0800722 if (cpu_dai->ops->hw_params) {
723 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200724 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200725 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100726 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200727 goto interface_err;
728 }
729 }
730
731 if (platform->pcm_ops->hw_params) {
732 ret = platform->pcm_ops->hw_params(substream, params);
733 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200734 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735 platform->name);
736 goto platform_err;
737 }
738 }
739
Mark Brown06f409d2009-04-07 18:10:13 +0100740 machine->rate = params_rate(params);
741
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200742out:
743 mutex_unlock(&pcm_mutex);
744 return ret;
745
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200746platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800747 if (cpu_dai->ops->hw_free)
748 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200749
750interface_err:
Eric Miao6335d052009-03-03 09:41:00 +0800751 if (codec_dai->ops->hw_free)
752 codec_dai->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100753
754codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200755 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100756 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200757
758 mutex_unlock(&pcm_mutex);
759 return ret;
760}
761
762/*
763 * Free's resources allocated by hw_params, can be called multiple times
764 */
765static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
766{
767 struct snd_soc_pcm_runtime *rtd = substream->private_data;
768 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100769 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000770 struct snd_soc_card *card = socdev->card;
771 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100772 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
773 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000774 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200775
776 mutex_lock(&pcm_mutex);
777
778 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100779 if (!codec->active)
780 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200781
782 /* free any machine hw params */
783 if (machine->ops && machine->ops->hw_free)
784 machine->ops->hw_free(substream);
785
786 /* free any DMA resources */
787 if (platform->pcm_ops->hw_free)
788 platform->pcm_ops->hw_free(substream);
789
790 /* now free hw params for the DAI's */
Eric Miao6335d052009-03-03 09:41:00 +0800791 if (codec_dai->ops->hw_free)
792 codec_dai->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200793
Eric Miao6335d052009-03-03 09:41:00 +0800794 if (cpu_dai->ops->hw_free)
795 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200796
797 mutex_unlock(&pcm_mutex);
798 return 0;
799}
800
801static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
802{
803 struct snd_soc_pcm_runtime *rtd = substream->private_data;
804 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000805 struct snd_soc_card *card= socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100806 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000807 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100808 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
809 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200810 int ret;
811
Eric Miao6335d052009-03-03 09:41:00 +0800812 if (codec_dai->ops->trigger) {
813 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200814 if (ret < 0)
815 return ret;
816 }
817
818 if (platform->pcm_ops->trigger) {
819 ret = platform->pcm_ops->trigger(substream, cmd);
820 if (ret < 0)
821 return ret;
822 }
823
Eric Miao6335d052009-03-03 09:41:00 +0800824 if (cpu_dai->ops->trigger) {
825 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200826 if (ret < 0)
827 return ret;
828 }
829 return 0;
830}
831
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200832/*
833 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200834 * If cpu_dai, codec_dai, platform driver has the delay callback, than
835 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200836 */
837static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
838{
839 struct snd_soc_pcm_runtime *rtd = substream->private_data;
840 struct snd_soc_device *socdev = rtd->socdev;
841 struct snd_soc_card *card = socdev->card;
842 struct snd_soc_platform *platform = card->platform;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200843 struct snd_soc_dai_link *machine = rtd->dai;
844 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
845 struct snd_soc_dai *codec_dai = machine->codec_dai;
846 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200847 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200848 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200849
850 if (platform->pcm_ops->pointer)
851 offset = platform->pcm_ops->pointer(substream);
852
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200853 if (cpu_dai->ops->delay)
854 delay += cpu_dai->ops->delay(substream, cpu_dai);
855
856 if (codec_dai->ops->delay)
857 delay += codec_dai->ops->delay(substream, codec_dai);
858
859 if (platform->delay)
860 delay += platform->delay(substream, codec_dai);
861
862 runtime->delay = delay;
863
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200864 return offset;
865}
866
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200867/* ASoC PCM operations */
868static struct snd_pcm_ops soc_pcm_ops = {
869 .open = soc_pcm_open,
870 .close = soc_codec_close,
871 .hw_params = soc_pcm_hw_params,
872 .hw_free = soc_pcm_hw_free,
873 .prepare = soc_pcm_prepare,
874 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200875 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200876};
877
878#ifdef CONFIG_PM
879/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100880static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200881{
Mark Brown416356f2009-06-30 19:05:15 +0100882 struct platform_device *pdev = to_platform_device(dev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200883 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000884 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000885 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200886 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000887 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200888 int i;
889
Daniel Macke3509ff2009-06-03 17:44:49 +0200890 /* If the initialization of this soc device failed, there is no codec
891 * associated with it. Just bail out in this case.
892 */
893 if (!codec)
894 return 0;
895
Andy Green6ed25972008-06-13 16:24:05 +0100896 /* Due to the resume being scheduled into a workqueue we could
897 * suspend before that's finished - wait for it to complete.
898 */
899 snd_power_lock(codec->card);
900 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
901 snd_power_unlock(codec->card);
902
903 /* we're going to block userspace touching us until resume completes */
904 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
905
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200906 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000907 for (i = 0; i < card->num_links; i++) {
908 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100909
910 if (card->dai_link[i].ignore_suspend)
911 continue;
912
Eric Miao6335d052009-03-03 09:41:00 +0800913 if (dai->ops->digital_mute && dai->playback.active)
914 dai->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200915 }
916
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100917 /* suspend all pcms */
Mark Brown3efab7d2010-05-09 13:25:43 +0100918 for (i = 0; i < card->num_links; i++) {
919 if (card->dai_link[i].ignore_suspend)
920 continue;
921
Mark Brown87506542008-11-18 20:50:34 +0000922 snd_pcm_suspend_all(card->dai_link[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100923 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100924
Mark Brown87506542008-11-18 20:50:34 +0000925 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100926 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200927
Mark Brown87506542008-11-18 20:50:34 +0000928 for (i = 0; i < card->num_links; i++) {
929 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100930
931 if (card->dai_link[i].ignore_suspend)
932 continue;
933
Mark Brown3ba9e102008-11-24 18:01:05 +0000934 if (cpu_dai->suspend && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000935 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200936 if (platform->suspend)
jassi brard273ebe2010-02-22 15:58:04 +0900937 platform->suspend(&card->dai_link[i]);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200938 }
939
940 /* close any waiting streams and save state */
Mark Brown63084192008-12-02 15:08:03 +0000941 run_delayed_work(&card->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200942 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200943
Mark Brown3ff3f642008-05-19 12:32:25 +0200944 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200945 char *stream = codec->dai[i].playback.stream_name;
Mark Brown3efab7d2010-05-09 13:25:43 +0100946
947 if (card->dai_link[i].ignore_suspend)
948 continue;
949
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200950 if (stream != NULL)
951 snd_soc_dapm_stream_event(codec, stream,
952 SND_SOC_DAPM_STREAM_SUSPEND);
953 stream = codec->dai[i].capture.stream_name;
954 if (stream != NULL)
955 snd_soc_dapm_stream_event(codec, stream,
956 SND_SOC_DAPM_STREAM_SUSPEND);
957 }
958
Mark Brown1547aba2010-05-07 21:11:40 +0100959 /* If there are paths active then the CODEC will be held with
960 * bias _ON and should not be suspended. */
961 if (codec_dev->suspend) {
962 switch (codec->bias_level) {
963 case SND_SOC_BIAS_STANDBY:
964 case SND_SOC_BIAS_OFF:
965 codec_dev->suspend(pdev, PMSG_SUSPEND);
966 break;
967 default:
968 dev_dbg(socdev->dev, "CODEC is on over suspend\n");
969 break;
970 }
971 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200972
Mark Brown87506542008-11-18 20:50:34 +0000973 for (i = 0; i < card->num_links; i++) {
974 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100975
976 if (card->dai_link[i].ignore_suspend)
977 continue;
978
Mark Brown3ba9e102008-11-24 18:01:05 +0000979 if (cpu_dai->suspend && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000980 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200981 }
982
Mark Brown87506542008-11-18 20:50:34 +0000983 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +0100984 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200985
986 return 0;
987}
988
Andy Green6ed25972008-06-13 16:24:05 +0100989/* deferred resume work, so resume can complete before we finished
990 * setting our codec back up, which can be very slow on I2C
991 */
992static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200993{
Mark Brown63084192008-12-02 15:08:03 +0000994 struct snd_soc_card *card = container_of(work,
995 struct snd_soc_card,
996 deferred_resume_work);
997 struct snd_soc_device *socdev = card->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000998 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200999 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +00001000 struct snd_soc_codec *codec = card->codec;
Andy Green6ed25972008-06-13 16:24:05 +01001001 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001002 int i;
1003
Andy Green6ed25972008-06-13 16:24:05 +01001004 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1005 * so userspace apps are blocked from touching us
1006 */
1007
Mark Brownfde22f22008-11-24 18:08:18 +00001008 dev_dbg(socdev->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001009
Mark Brown99497882010-05-07 20:24:05 +01001010 /* Bring us up into D2 so that DAPM starts enabling things */
1011 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D2);
1012
Mark Brown87506542008-11-18 20:50:34 +00001013 if (card->resume_pre)
1014 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001015
Mark Brown87506542008-11-18 20:50:34 +00001016 for (i = 0; i < card->num_links; i++) {
1017 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001018
1019 if (card->dai_link[i].ignore_suspend)
1020 continue;
1021
Mark Brown3ba9e102008-11-24 18:01:05 +00001022 if (cpu_dai->resume && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +00001023 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001024 }
1025
Mark Brown1547aba2010-05-07 21:11:40 +01001026 /* If the CODEC was idle over suspend then it will have been
1027 * left with bias OFF or STANDBY and suspended so we must now
1028 * resume. Otherwise the suspend was suppressed.
1029 */
1030 if (codec_dev->resume) {
1031 switch (codec->bias_level) {
1032 case SND_SOC_BIAS_STANDBY:
1033 case SND_SOC_BIAS_OFF:
1034 codec_dev->resume(pdev);
1035 break;
1036 default:
1037 dev_dbg(socdev->dev, "CODEC was on over suspend\n");
1038 break;
1039 }
1040 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001041
Mark Brown3ff3f642008-05-19 12:32:25 +02001042 for (i = 0; i < codec->num_dai; i++) {
1043 char *stream = codec->dai[i].playback.stream_name;
Mark Brown3efab7d2010-05-09 13:25:43 +01001044
1045 if (card->dai_link[i].ignore_suspend)
1046 continue;
1047
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001048 if (stream != NULL)
1049 snd_soc_dapm_stream_event(codec, stream,
1050 SND_SOC_DAPM_STREAM_RESUME);
1051 stream = codec->dai[i].capture.stream_name;
1052 if (stream != NULL)
1053 snd_soc_dapm_stream_event(codec, stream,
1054 SND_SOC_DAPM_STREAM_RESUME);
1055 }
1056
Mark Brown3ff3f642008-05-19 12:32:25 +02001057 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +00001058 for (i = 0; i < card->num_links; i++) {
1059 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001060
1061 if (card->dai_link[i].ignore_suspend)
1062 continue;
1063
Eric Miao6335d052009-03-03 09:41:00 +08001064 if (dai->ops->digital_mute && dai->playback.active)
1065 dai->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001066 }
1067
Mark Brown87506542008-11-18 20:50:34 +00001068 for (i = 0; i < card->num_links; i++) {
1069 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001070
1071 if (card->dai_link[i].ignore_suspend)
1072 continue;
1073
Mark Brown3ba9e102008-11-24 18:01:05 +00001074 if (cpu_dai->resume && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +00001075 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001076 if (platform->resume)
jassi brard273ebe2010-02-22 15:58:04 +09001077 platform->resume(&card->dai_link[i]);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001078 }
1079
Mark Brown87506542008-11-18 20:50:34 +00001080 if (card->resume_post)
1081 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001082
Mark Brownfde22f22008-11-24 18:08:18 +00001083 dev_dbg(socdev->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001084
1085 /* userspace can access us now we are back as we were before */
1086 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
1087}
1088
1089/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001090static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001091{
Mark Brown416356f2009-06-30 19:05:15 +01001092 struct platform_device *pdev = to_platform_device(dev);
Andy Green6ed25972008-06-13 16:24:05 +01001093 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown63084192008-12-02 15:08:03 +00001094 struct snd_soc_card *card = socdev->card;
Mark Brown64ab9ba2009-03-31 11:27:03 +01001095 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
Andy Green6ed25972008-06-13 16:24:05 +01001096
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001097 /* If the initialization of this soc device failed, there is no codec
1098 * associated with it. Just bail out in this case.
1099 */
1100 if (!card->codec)
1101 return 0;
1102
Mark Brown64ab9ba2009-03-31 11:27:03 +01001103 /* AC97 devices might have other drivers hanging off them so
1104 * need to resume immediately. Other drivers don't have that
1105 * problem and may take a substantial amount of time to resume
1106 * due to I/O costs and anti-pop so handle them out of line.
1107 */
1108 if (cpu_dai->ac97_control) {
1109 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
1110 soc_resume_deferred(&card->deferred_resume_work);
1111 } else {
1112 dev_dbg(socdev->dev, "Scheduling resume work\n");
1113 if (!schedule_work(&card->deferred_resume_work))
1114 dev_err(socdev->dev, "resume work item may be lost\n");
1115 }
Andy Green6ed25972008-06-13 16:24:05 +01001116
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001117 return 0;
1118}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001119#else
1120#define soc_suspend NULL
1121#define soc_resume NULL
1122#endif
1123
Barry Song02a06d32009-10-16 18:13:38 +08001124static struct snd_soc_dai_ops null_dai_ops = {
1125};
1126
Mark Brown435c5e22008-12-04 15:32:53 +00001127static void snd_soc_instantiate_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001128{
Mark Brown435c5e22008-12-04 15:32:53 +00001129 struct platform_device *pdev = container_of(card->dev,
1130 struct platform_device,
1131 dev);
1132 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001133 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001134 struct snd_soc_platform *platform;
1135 struct snd_soc_dai *dai;
Mark Brown6b05eda2008-12-08 19:26:48 +00001136 int i, found, ret, ac97;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001137
Mark Brown435c5e22008-12-04 15:32:53 +00001138 if (card->instantiated)
1139 return;
Mark Brown63084192008-12-02 15:08:03 +00001140
Mark Brown435c5e22008-12-04 15:32:53 +00001141 found = 0;
1142 list_for_each_entry(platform, &platform_list, list)
1143 if (card->platform == platform) {
1144 found = 1;
1145 break;
1146 }
1147 if (!found) {
1148 dev_dbg(card->dev, "Platform %s not registered\n",
1149 card->platform->name);
1150 return;
Mark Brownc5af3a22008-11-28 13:29:45 +00001151 }
1152
Mark Brown6b05eda2008-12-08 19:26:48 +00001153 ac97 = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001154 for (i = 0; i < card->num_links; i++) {
1155 found = 0;
1156 list_for_each_entry(dai, &dai_list, list)
1157 if (card->dai_link[i].cpu_dai == dai) {
1158 found = 1;
1159 break;
1160 }
1161 if (!found) {
1162 dev_dbg(card->dev, "DAI %s not registered\n",
1163 card->dai_link[i].cpu_dai->name);
1164 return;
1165 }
Mark Brown6b05eda2008-12-08 19:26:48 +00001166
1167 if (card->dai_link[i].cpu_dai->ac97_control)
1168 ac97 = 1;
Mark Brown435c5e22008-12-04 15:32:53 +00001169 }
1170
Barry Song02a06d32009-10-16 18:13:38 +08001171 for (i = 0; i < card->num_links; i++) {
1172 if (!card->dai_link[i].codec_dai->ops)
1173 card->dai_link[i].codec_dai->ops = &null_dai_ops;
1174 }
1175
Mark Brown6b05eda2008-12-08 19:26:48 +00001176 /* If we have AC97 in the system then don't wait for the
1177 * codec. This will need revisiting if we have to handle
1178 * systems with mixed AC97 and non-AC97 parts. Only check for
1179 * DAIs currently; we can't do this per link since some AC97
1180 * codecs have non-AC97 DAIs.
1181 */
1182 if (!ac97)
1183 for (i = 0; i < card->num_links; i++) {
1184 found = 0;
1185 list_for_each_entry(dai, &dai_list, list)
1186 if (card->dai_link[i].codec_dai == dai) {
1187 found = 1;
1188 break;
1189 }
1190 if (!found) {
1191 dev_dbg(card->dev, "DAI %s not registered\n",
1192 card->dai_link[i].codec_dai->name);
1193 return;
1194 }
1195 }
1196
Mark Brown435c5e22008-12-04 15:32:53 +00001197 /* Note that we do not current check for codec components */
1198
1199 dev_dbg(card->dev, "All components present, instantiating\n");
1200
1201 /* Found everything, bring it up */
Mark Brown96dd3622010-02-12 11:05:44 +00001202 card->pmdown_time = pmdown_time;
1203
Mark Brown87506542008-11-18 20:50:34 +00001204 if (card->probe) {
1205 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001206 if (ret < 0)
Mark Brown435c5e22008-12-04 15:32:53 +00001207 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001208 }
1209
Mark Brown87506542008-11-18 20:50:34 +00001210 for (i = 0; i < card->num_links; i++) {
1211 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001212 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +01001213 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +02001214 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001215 goto cpu_dai_err;
1216 }
1217 }
1218
1219 if (codec_dev->probe) {
1220 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001221 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001222 goto cpu_dai_err;
1223 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001224 codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001225
1226 if (platform->probe) {
1227 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001228 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001229 goto platform_err;
1230 }
1231
1232 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +00001233 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +01001234#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001235 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001236 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001237#endif
Andy Green6ed25972008-06-13 16:24:05 +01001238
Mark Brownfe3e78e2009-11-03 22:13:13 +00001239 for (i = 0; i < card->num_links; i++) {
1240 if (card->dai_link[i].init) {
1241 ret = card->dai_link[i].init(codec);
1242 if (ret < 0) {
1243 printk(KERN_ERR "asoc: failed to init %s\n",
1244 card->dai_link[i].stream_name);
1245 continue;
1246 }
1247 }
Barry Songf7732052009-11-12 12:01:47 +08001248 if (card->dai_link[i].codec_dai->ac97_control)
Mark Brownfe3e78e2009-11-03 22:13:13 +00001249 ac97 = 1;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001250 }
1251
1252 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1253 "%s", card->name);
1254 snprintf(codec->card->longname, sizeof(codec->card->longname),
1255 "%s (%s)", card->name, codec->name);
1256
1257 /* Make sure all DAPM widgets are instantiated */
1258 snd_soc_dapm_new_widgets(codec);
1259
1260 ret = snd_card_register(codec->card);
1261 if (ret < 0) {
1262 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1263 codec->name);
1264 goto card_err;
1265 }
1266
1267 mutex_lock(&codec->mutex);
1268#ifdef CONFIG_SND_SOC_AC97_BUS
1269 /* Only instantiate AC97 if not already done by the adaptor
1270 * for the generic AC97 subsystem.
1271 */
1272 if (ac97 && strcmp(codec->name, "AC97") != 0) {
1273 ret = soc_ac97_dev_register(codec);
1274 if (ret < 0) {
1275 printk(KERN_ERR "asoc: AC97 device register failed\n");
1276 snd_card_free(codec->card);
1277 mutex_unlock(&codec->mutex);
1278 goto card_err;
1279 }
1280 }
1281#endif
1282
1283 ret = snd_soc_dapm_sys_add(card->socdev->dev);
1284 if (ret < 0)
1285 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1286
Mark Browndbe21402010-02-12 11:37:24 +00001287 ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1288 if (ret < 0)
1289 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1290
Mark Brownfe3e78e2009-11-03 22:13:13 +00001291 ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1292 if (ret < 0)
1293 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1294
1295 soc_init_codec_debugfs(codec);
1296 mutex_unlock(&codec->mutex);
1297
Mark Brown435c5e22008-12-04 15:32:53 +00001298 card->instantiated = 1;
1299
1300 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001301
Mark Brownfe3e78e2009-11-03 22:13:13 +00001302card_err:
1303 if (platform->remove)
1304 platform->remove(pdev);
1305
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001306platform_err:
1307 if (codec_dev->remove)
1308 codec_dev->remove(pdev);
1309
1310cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +01001311 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +00001312 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001313 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +01001314 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001315 }
1316
Mark Brown87506542008-11-18 20:50:34 +00001317 if (card->remove)
1318 card->remove(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001319}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001320
Mark Brown435c5e22008-12-04 15:32:53 +00001321/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001322 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001323 * client_mutex.
1324 */
1325static void snd_soc_instantiate_cards(void)
1326{
1327 struct snd_soc_card *card;
1328 list_for_each_entry(card, &card_list, list)
1329 snd_soc_instantiate_card(card);
1330}
1331
1332/* probes a new socdev */
1333static int soc_probe(struct platform_device *pdev)
1334{
1335 int ret = 0;
1336 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1337 struct snd_soc_card *card = socdev->card;
1338
1339 /* Bodge while we push things out of socdev */
1340 card->socdev = socdev;
1341
1342 /* Bodge while we unpick instantiation */
1343 card->dev = &pdev->dev;
1344 ret = snd_soc_register_card(card);
1345 if (ret != 0) {
1346 dev_err(&pdev->dev, "Failed to register card\n");
1347 return ret;
1348 }
1349
1350 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001351}
1352
1353/* removes a socdev */
1354static int soc_remove(struct platform_device *pdev)
1355{
1356 int i;
1357 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +00001358 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +00001359 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001360 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1361
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001362 if (card->instantiated) {
1363 run_delayed_work(&card->delayed_work);
Mike Rapoport914dc182009-05-11 13:04:55 +03001364
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001365 if (platform->remove)
1366 platform->remove(pdev);
Liam Girdwood965ac422007-01-31 14:14:57 +01001367
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001368 if (codec_dev->remove)
1369 codec_dev->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001370
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001371 for (i = 0; i < card->num_links; i++) {
1372 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
1373 if (cpu_dai->remove)
1374 cpu_dai->remove(pdev, cpu_dai);
1375 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001376
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001377 if (card->remove)
1378 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001379 }
1380
Mark Brownc5af3a22008-11-28 13:29:45 +00001381 snd_soc_unregister_card(card);
1382
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001383 return 0;
1384}
1385
Mark Brown416356f2009-06-30 19:05:15 +01001386static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001387{
Mark Brown416356f2009-06-30 19:05:15 +01001388 struct platform_device *pdev = to_platform_device(dev);
Mark Brown51737472009-06-22 13:16:51 +01001389 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1390 struct snd_soc_card *card = socdev->card;
1391
1392 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001393 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001394
1395 /* Flush out pmdown_time work - we actually do want to run it
1396 * now, we're shutting down so no imminent restart. */
1397 run_delayed_work(&card->delayed_work);
1398
1399 snd_soc_dapm_shutdown(socdev);
Mark Brown416356f2009-06-30 19:05:15 +01001400
1401 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001402}
1403
Alexey Dobriyan47145212009-12-14 18:00:08 -08001404static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001405 .suspend = soc_suspend,
1406 .resume = soc_resume,
1407 .poweroff = soc_poweroff,
1408};
1409
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001410/* ASoC platform driver */
1411static struct platform_driver soc_driver = {
1412 .driver = {
1413 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001414 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001415 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001416 },
1417 .probe = soc_probe,
1418 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001419};
1420
1421/* create a new pcm */
1422static int soc_new_pcm(struct snd_soc_device *socdev,
1423 struct snd_soc_dai_link *dai_link, int num)
1424{
Mark Brown87689d52008-12-02 16:01:14 +00001425 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001426 struct snd_soc_codec *codec = card->codec;
Mark Brown87689d52008-12-02 16:01:14 +00001427 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001428 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1429 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001430 struct snd_soc_pcm_runtime *rtd;
1431 struct snd_pcm *pcm;
1432 char new_name[64];
1433 int ret = 0, playback = 0, capture = 0;
1434
1435 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1436 if (rtd == NULL)
1437 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +01001438
1439 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001440 rtd->socdev = socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001441 codec_dai->codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001442
1443 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001444 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1445 dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001446
1447 if (codec_dai->playback.channels_min)
1448 playback = 1;
1449 if (codec_dai->capture.channels_min)
1450 capture = 1;
1451
1452 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1453 capture, &pcm);
1454 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001455 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1456 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001457 kfree(rtd);
1458 return ret;
1459 }
1460
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001461 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001462 pcm->private_data = rtd;
Mark Brown87689d52008-12-02 16:01:14 +00001463 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
Mark Brown87689d52008-12-02 16:01:14 +00001464 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1465 soc_pcm_ops.copy = platform->pcm_ops->copy;
1466 soc_pcm_ops.silence = platform->pcm_ops->silence;
1467 soc_pcm_ops.ack = platform->pcm_ops->ack;
1468 soc_pcm_ops.page = platform->pcm_ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001469
1470 if (playback)
1471 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1472
1473 if (capture)
1474 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1475
Mark Brown87689d52008-12-02 16:01:14 +00001476 ret = platform->pcm_new(codec->card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001477 if (ret < 0) {
1478 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1479 kfree(rtd);
1480 return ret;
1481 }
1482
Mark Brown87689d52008-12-02 16:01:14 +00001483 pcm->private_free = platform->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001484 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1485 cpu_dai->name);
1486 return ret;
1487}
1488
Mark Brown096e49d2009-07-05 15:12:22 +01001489/**
1490 * snd_soc_codec_volatile_register: Report if a register is volatile.
1491 *
1492 * @codec: CODEC to query.
1493 * @reg: Register to query.
1494 *
1495 * Boolean function indiciating if a CODEC register is volatile.
1496 */
1497int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1498{
1499 if (codec->volatile_register)
1500 return codec->volatile_register(reg);
1501 else
1502 return 0;
1503}
1504EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1505
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001506/**
1507 * snd_soc_new_ac97_codec - initailise AC97 device
1508 * @codec: audio codec
1509 * @ops: AC97 bus operations
1510 * @num: AC97 codec number
1511 *
1512 * Initialises AC97 codec resources for use by ad-hoc devices only.
1513 */
1514int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1515 struct snd_ac97_bus_ops *ops, int num)
1516{
1517 mutex_lock(&codec->mutex);
1518
1519 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1520 if (codec->ac97 == NULL) {
1521 mutex_unlock(&codec->mutex);
1522 return -ENOMEM;
1523 }
1524
1525 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1526 if (codec->ac97->bus == NULL) {
1527 kfree(codec->ac97);
1528 codec->ac97 = NULL;
1529 mutex_unlock(&codec->mutex);
1530 return -ENOMEM;
1531 }
1532
1533 codec->ac97->bus->ops = ops;
1534 codec->ac97->num = num;
Mark Brown27186252010-01-28 12:36:29 +00001535 codec->dev = &codec->ac97->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001536 mutex_unlock(&codec->mutex);
1537 return 0;
1538}
1539EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1540
1541/**
1542 * snd_soc_free_ac97_codec - free AC97 codec device
1543 * @codec: audio codec
1544 *
1545 * Frees AC97 codec device resources.
1546 */
1547void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1548{
1549 mutex_lock(&codec->mutex);
1550 kfree(codec->ac97->bus);
1551 kfree(codec->ac97);
1552 codec->ac97 = NULL;
1553 mutex_unlock(&codec->mutex);
1554}
1555EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1556
1557/**
1558 * snd_soc_update_bits - update codec register bits
1559 * @codec: audio codec
1560 * @reg: codec register
1561 * @mask: register mask
1562 * @value: new value
1563 *
1564 * Writes new register value.
1565 *
1566 * Returns 1 for change else 0.
1567 */
1568int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001569 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001570{
1571 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001572 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001573
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001574 old = snd_soc_read(codec, reg);
1575 new = (old & ~mask) | value;
1576 change = old != new;
1577 if (change)
1578 snd_soc_write(codec, reg, new);
1579
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001580 return change;
1581}
1582EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1583
1584/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001585 * snd_soc_update_bits_locked - update codec register bits
1586 * @codec: audio codec
1587 * @reg: codec register
1588 * @mask: register mask
1589 * @value: new value
1590 *
1591 * Writes new register value, and takes the codec mutex.
1592 *
1593 * Returns 1 for change else 0.
1594 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001595int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1596 unsigned short reg, unsigned int mask,
1597 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001598{
1599 int change;
1600
1601 mutex_lock(&codec->mutex);
1602 change = snd_soc_update_bits(codec, reg, mask, value);
1603 mutex_unlock(&codec->mutex);
1604
1605 return change;
1606}
Mark Browndd1b3d52009-12-04 14:22:03 +00001607EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001608
1609/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001610 * snd_soc_test_bits - test register for change
1611 * @codec: audio codec
1612 * @reg: codec register
1613 * @mask: register mask
1614 * @value: new value
1615 *
1616 * Tests a register with a new value and checks if the new value is
1617 * different from the old value.
1618 *
1619 * Returns 1 for change else 0.
1620 */
1621int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001622 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001623{
1624 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001625 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001626
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001627 old = snd_soc_read(codec, reg);
1628 new = (old & ~mask) | value;
1629 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001630
1631 return change;
1632}
1633EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1634
1635/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001636 * snd_soc_new_pcms - create new sound card and pcms
1637 * @socdev: the SoC audio device
Mark Brownac11a2b2009-01-01 12:18:17 +00001638 * @idx: ALSA card index
1639 * @xid: card identification
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001640 *
1641 * Create a new sound card based upon the codec and interface pcms.
1642 *
1643 * Returns 0 for success, else error.
1644 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001645int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001646{
Mark Brown87506542008-11-18 20:50:34 +00001647 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001648 struct snd_soc_codec *codec = card->codec;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001649 int ret, i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001650
1651 mutex_lock(&codec->mutex);
1652
1653 /* register a sound card */
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001654 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1655 if (ret < 0) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001656 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1657 codec->name);
1658 mutex_unlock(&codec->mutex);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001659 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001660 }
1661
Mark Brown452c5ea2009-05-17 21:41:23 +01001662 codec->socdev = socdev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001663 codec->card->dev = socdev->dev;
1664 codec->card->private_data = codec;
1665 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1666
1667 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001668 for (i = 0; i < card->num_links; i++) {
1669 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001670 if (ret < 0) {
1671 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001672 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001673 mutex_unlock(&codec->mutex);
1674 return ret;
1675 }
Graham Gowerfb48e3c2010-03-25 10:52:12 +10301676 /* Check for codec->ac97 to handle the ac97.c fun */
1677 if (card->dai_link[i].codec_dai->ac97_control && codec->ac97) {
Barry Songf7732052009-11-12 12:01:47 +08001678 snd_ac97_dev_add_pdata(codec->ac97,
1679 card->dai_link[i].cpu_dai->ac97_pdata);
1680 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001681 }
1682
1683 mutex_unlock(&codec->mutex);
1684 return ret;
1685}
1686EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1687
1688/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001689 * snd_soc_free_pcms - free sound card and pcms
1690 * @socdev: the SoC audio device
1691 *
1692 * Frees sound card and pcms associated with the socdev.
1693 * Also unregister the codec if it is an AC97 device.
1694 */
1695void snd_soc_free_pcms(struct snd_soc_device *socdev)
1696{
Mark Brown6627a652009-01-23 22:55:23 +00001697 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001698#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001699 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001700 int i;
1701#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001702
1703 mutex_lock(&codec->mutex);
Mark Brown6627a652009-01-23 22:55:23 +00001704 soc_cleanup_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001705#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001706 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001707 codec_dai = &codec->dai[i];
Atsushi Nemotod2314e02009-03-16 23:26:20 +09001708 if (codec_dai->ac97_control && codec->ac97 &&
1709 strcmp(codec->name, "AC97") != 0) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001710 soc_ac97_dev_unregister(codec);
1711 goto free_card;
1712 }
1713 }
1714free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001715#endif
1716
1717 if (codec->card)
1718 snd_card_free(codec->card);
1719 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1720 mutex_unlock(&codec->mutex);
1721}
1722EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1723
1724/**
1725 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1726 * @substream: the pcm substream
1727 * @hw: the hardware parameters
1728 *
1729 * Sets the substream runtime hardware parameters.
1730 */
1731int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1732 const struct snd_pcm_hardware *hw)
1733{
1734 struct snd_pcm_runtime *runtime = substream->runtime;
1735 runtime->hw.info = hw->info;
1736 runtime->hw.formats = hw->formats;
1737 runtime->hw.period_bytes_min = hw->period_bytes_min;
1738 runtime->hw.period_bytes_max = hw->period_bytes_max;
1739 runtime->hw.periods_min = hw->periods_min;
1740 runtime->hw.periods_max = hw->periods_max;
1741 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1742 runtime->hw.fifo_size = hw->fifo_size;
1743 return 0;
1744}
1745EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1746
1747/**
1748 * snd_soc_cnew - create new control
1749 * @_template: control template
1750 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001751 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001752 *
1753 * Create a new mixer control from a template control.
1754 *
1755 * Returns 0 for success, else error.
1756 */
1757struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1758 void *data, char *long_name)
1759{
1760 struct snd_kcontrol_new template;
1761
1762 memcpy(&template, _template, sizeof(template));
1763 if (long_name)
1764 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001765 template.index = 0;
1766
1767 return snd_ctl_new1(&template, data);
1768}
1769EXPORT_SYMBOL_GPL(snd_soc_cnew);
1770
1771/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001772 * snd_soc_add_controls - add an array of controls to a codec.
1773 * Convienience function to add a list of controls. Many codecs were
1774 * duplicating this code.
1775 *
1776 * @codec: codec to add controls to
1777 * @controls: array of controls to add
1778 * @num_controls: number of elements in the array
1779 *
1780 * Return 0 for success, else error.
1781 */
1782int snd_soc_add_controls(struct snd_soc_codec *codec,
1783 const struct snd_kcontrol_new *controls, int num_controls)
1784{
1785 struct snd_card *card = codec->card;
1786 int err, i;
1787
1788 for (i = 0; i < num_controls; i++) {
1789 const struct snd_kcontrol_new *control = &controls[i];
1790 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1791 if (err < 0) {
1792 dev_err(codec->dev, "%s: Failed to add %s\n",
1793 codec->name, control->name);
1794 return err;
1795 }
1796 }
1797
1798 return 0;
1799}
1800EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1801
1802/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001803 * snd_soc_info_enum_double - enumerated double mixer info callback
1804 * @kcontrol: mixer control
1805 * @uinfo: control element information
1806 *
1807 * Callback to provide information about a double enumerated
1808 * mixer control.
1809 *
1810 * Returns 0 for success.
1811 */
1812int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1813 struct snd_ctl_elem_info *uinfo)
1814{
1815 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1816
1817 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1818 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001819 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001820
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001821 if (uinfo->value.enumerated.item > e->max - 1)
1822 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001823 strcpy(uinfo->value.enumerated.name,
1824 e->texts[uinfo->value.enumerated.item]);
1825 return 0;
1826}
1827EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1828
1829/**
1830 * snd_soc_get_enum_double - enumerated double mixer get callback
1831 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001832 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001833 *
1834 * Callback to get the value of a double enumerated mixer.
1835 *
1836 * Returns 0 for success.
1837 */
1838int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1839 struct snd_ctl_elem_value *ucontrol)
1840{
1841 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1842 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001843 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001844
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001845 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001846 ;
1847 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001848 ucontrol->value.enumerated.item[0]
1849 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001850 if (e->shift_l != e->shift_r)
1851 ucontrol->value.enumerated.item[1] =
1852 (val >> e->shift_r) & (bitmask - 1);
1853
1854 return 0;
1855}
1856EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1857
1858/**
1859 * snd_soc_put_enum_double - enumerated double mixer put callback
1860 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001861 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001862 *
1863 * Callback to set the value of a double enumerated mixer.
1864 *
1865 * Returns 0 for success.
1866 */
1867int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1868 struct snd_ctl_elem_value *ucontrol)
1869{
1870 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1871 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001872 unsigned int val;
1873 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001874
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001875 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001876 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001877 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001878 return -EINVAL;
1879 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1880 mask = (bitmask - 1) << e->shift_l;
1881 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001882 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001883 return -EINVAL;
1884 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1885 mask |= (bitmask - 1) << e->shift_r;
1886 }
1887
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001888 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001889}
1890EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1891
1892/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001893 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1894 * @kcontrol: mixer control
1895 * @ucontrol: control element information
1896 *
1897 * Callback to get the value of a double semi enumerated mixer.
1898 *
1899 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1900 * used for handling bitfield coded enumeration for example.
1901 *
1902 * Returns 0 for success.
1903 */
1904int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1905 struct snd_ctl_elem_value *ucontrol)
1906{
1907 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001908 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001909 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001910
1911 reg_val = snd_soc_read(codec, e->reg);
1912 val = (reg_val >> e->shift_l) & e->mask;
1913 for (mux = 0; mux < e->max; mux++) {
1914 if (val == e->values[mux])
1915 break;
1916 }
1917 ucontrol->value.enumerated.item[0] = mux;
1918 if (e->shift_l != e->shift_r) {
1919 val = (reg_val >> e->shift_r) & e->mask;
1920 for (mux = 0; mux < e->max; mux++) {
1921 if (val == e->values[mux])
1922 break;
1923 }
1924 ucontrol->value.enumerated.item[1] = mux;
1925 }
1926
1927 return 0;
1928}
1929EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1930
1931/**
1932 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1933 * @kcontrol: mixer control
1934 * @ucontrol: control element information
1935 *
1936 * Callback to set the value of a double semi enumerated mixer.
1937 *
1938 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1939 * used for handling bitfield coded enumeration for example.
1940 *
1941 * Returns 0 for success.
1942 */
1943int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1944 struct snd_ctl_elem_value *ucontrol)
1945{
1946 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001947 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001948 unsigned int val;
1949 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001950
1951 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1952 return -EINVAL;
1953 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1954 mask = e->mask << e->shift_l;
1955 if (e->shift_l != e->shift_r) {
1956 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1957 return -EINVAL;
1958 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1959 mask |= e->mask << e->shift_r;
1960 }
1961
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001962 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001963}
1964EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1965
1966/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001967 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1968 * @kcontrol: mixer control
1969 * @uinfo: control element information
1970 *
1971 * Callback to provide information about an external enumerated
1972 * single mixer.
1973 *
1974 * Returns 0 for success.
1975 */
1976int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1977 struct snd_ctl_elem_info *uinfo)
1978{
1979 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1980
1981 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1982 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001983 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001984
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001985 if (uinfo->value.enumerated.item > e->max - 1)
1986 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001987 strcpy(uinfo->value.enumerated.name,
1988 e->texts[uinfo->value.enumerated.item]);
1989 return 0;
1990}
1991EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1992
1993/**
1994 * snd_soc_info_volsw_ext - external single mixer info callback
1995 * @kcontrol: mixer control
1996 * @uinfo: control element information
1997 *
1998 * Callback to provide information about a single external mixer control.
1999 *
2000 * Returns 0 for success.
2001 */
2002int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_info *uinfo)
2004{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002005 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002006
Mark Brownfd5dfad2009-04-15 21:37:46 +01002007 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002008 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2009 else
2010 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2011
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002012 uinfo->count = 1;
2013 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002014 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002015 return 0;
2016}
2017EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2018
2019/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002020 * snd_soc_info_volsw - single mixer info callback
2021 * @kcontrol: mixer control
2022 * @uinfo: control element information
2023 *
2024 * Callback to provide information about a single mixer control.
2025 *
2026 * Returns 0 for success.
2027 */
2028int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2029 struct snd_ctl_elem_info *uinfo)
2030{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002031 struct soc_mixer_control *mc =
2032 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002033 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002034 unsigned int shift = mc->shift;
Jon Smirl815ecf82008-07-29 10:22:24 -04002035 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002036
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002037 if (!mc->platform_max)
2038 mc->platform_max = mc->max;
2039 platform_max = mc->platform_max;
2040
2041 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002042 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2043 else
2044 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2045
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002046 uinfo->count = shift == rshift ? 1 : 2;
2047 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002048 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002049 return 0;
2050}
2051EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2052
2053/**
2054 * snd_soc_get_volsw - single mixer get callback
2055 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002056 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002057 *
2058 * Callback to get the value of a single mixer control.
2059 *
2060 * Returns 0 for success.
2061 */
2062int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2063 struct snd_ctl_elem_value *ucontrol)
2064{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002065 struct soc_mixer_control *mc =
2066 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002067 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002068 unsigned int reg = mc->reg;
2069 unsigned int shift = mc->shift;
2070 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002071 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002072 unsigned int mask = (1 << fls(max)) - 1;
2073 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002074
2075 ucontrol->value.integer.value[0] =
2076 (snd_soc_read(codec, reg) >> shift) & mask;
2077 if (shift != rshift)
2078 ucontrol->value.integer.value[1] =
2079 (snd_soc_read(codec, reg) >> rshift) & mask;
2080 if (invert) {
2081 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002082 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002083 if (shift != rshift)
2084 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002085 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002086 }
2087
2088 return 0;
2089}
2090EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2091
2092/**
2093 * snd_soc_put_volsw - single mixer put callback
2094 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002095 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002096 *
2097 * Callback to set the value of a single mixer control.
2098 *
2099 * Returns 0 for success.
2100 */
2101int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2102 struct snd_ctl_elem_value *ucontrol)
2103{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002104 struct soc_mixer_control *mc =
2105 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002106 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002107 unsigned int reg = mc->reg;
2108 unsigned int shift = mc->shift;
2109 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002110 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002111 unsigned int mask = (1 << fls(max)) - 1;
2112 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002113 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002114
2115 val = (ucontrol->value.integer.value[0] & mask);
2116 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002117 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002118 val_mask = mask << shift;
2119 val = val << shift;
2120 if (shift != rshift) {
2121 val2 = (ucontrol->value.integer.value[1] & mask);
2122 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002123 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002124 val_mask |= mask << rshift;
2125 val |= val2 << rshift;
2126 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002127 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002128}
2129EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2130
2131/**
2132 * snd_soc_info_volsw_2r - double mixer info callback
2133 * @kcontrol: mixer control
2134 * @uinfo: control element information
2135 *
2136 * Callback to provide information about a double mixer control that
2137 * spans 2 codec registers.
2138 *
2139 * Returns 0 for success.
2140 */
2141int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2142 struct snd_ctl_elem_info *uinfo)
2143{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002144 struct soc_mixer_control *mc =
2145 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002146 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002147
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002148 if (!mc->platform_max)
2149 mc->platform_max = mc->max;
2150 platform_max = mc->platform_max;
2151
2152 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002153 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2154 else
2155 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2156
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002157 uinfo->count = 2;
2158 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002159 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002160 return 0;
2161}
2162EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2163
2164/**
2165 * snd_soc_get_volsw_2r - double mixer get callback
2166 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002167 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002168 *
2169 * Callback to get the value of a double mixer control that spans 2 registers.
2170 *
2171 * Returns 0 for success.
2172 */
2173int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2174 struct snd_ctl_elem_value *ucontrol)
2175{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002176 struct soc_mixer_control *mc =
2177 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002178 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002179 unsigned int reg = mc->reg;
2180 unsigned int reg2 = mc->rreg;
2181 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002182 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002183 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf82008-07-29 10:22:24 -04002184 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002185
2186 ucontrol->value.integer.value[0] =
2187 (snd_soc_read(codec, reg) >> shift) & mask;
2188 ucontrol->value.integer.value[1] =
2189 (snd_soc_read(codec, reg2) >> shift) & mask;
2190 if (invert) {
2191 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002192 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002193 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002194 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002195 }
2196
2197 return 0;
2198}
2199EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2200
2201/**
2202 * snd_soc_put_volsw_2r - double mixer set callback
2203 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002204 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002205 *
2206 * Callback to set the value of a double mixer control that spans 2 registers.
2207 *
2208 * Returns 0 for success.
2209 */
2210int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2211 struct snd_ctl_elem_value *ucontrol)
2212{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002213 struct soc_mixer_control *mc =
2214 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002215 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002216 unsigned int reg = mc->reg;
2217 unsigned int reg2 = mc->rreg;
2218 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002219 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002220 unsigned int mask = (1 << fls(max)) - 1;
2221 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002223 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002224
2225 val_mask = mask << shift;
2226 val = (ucontrol->value.integer.value[0] & mask);
2227 val2 = (ucontrol->value.integer.value[1] & mask);
2228
2229 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002230 val = max - val;
2231 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002232 }
2233
2234 val = val << shift;
2235 val2 = val2 << shift;
2236
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002237 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002238 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002239 return err;
2240
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002241 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002242 return err;
2243}
2244EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2245
Mark Browne13ac2e2008-05-28 17:58:05 +01002246/**
2247 * snd_soc_info_volsw_s8 - signed mixer info callback
2248 * @kcontrol: mixer control
2249 * @uinfo: control element information
2250 *
2251 * Callback to provide information about a signed mixer control.
2252 *
2253 * Returns 0 for success.
2254 */
2255int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2256 struct snd_ctl_elem_info *uinfo)
2257{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002258 struct soc_mixer_control *mc =
2259 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002260 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002261 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002262
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002263 if (!mc->platform_max)
2264 mc->platform_max = mc->max;
2265 platform_max = mc->platform_max;
2266
Mark Browne13ac2e2008-05-28 17:58:05 +01002267 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2268 uinfo->count = 2;
2269 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002270 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002271 return 0;
2272}
2273EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2274
2275/**
2276 * snd_soc_get_volsw_s8 - signed mixer get callback
2277 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002278 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002279 *
2280 * Callback to get the value of a signed mixer control.
2281 *
2282 * Returns 0 for success.
2283 */
2284int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2285 struct snd_ctl_elem_value *ucontrol)
2286{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002287 struct soc_mixer_control *mc =
2288 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002289 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002290 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002291 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002292 int val = snd_soc_read(codec, reg);
2293
2294 ucontrol->value.integer.value[0] =
2295 ((signed char)(val & 0xff))-min;
2296 ucontrol->value.integer.value[1] =
2297 ((signed char)((val >> 8) & 0xff))-min;
2298 return 0;
2299}
2300EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2301
2302/**
2303 * snd_soc_put_volsw_sgn - signed mixer put callback
2304 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002305 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002306 *
2307 * Callback to set the value of a signed mixer control.
2308 *
2309 * Returns 0 for success.
2310 */
2311int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2312 struct snd_ctl_elem_value *ucontrol)
2313{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002314 struct soc_mixer_control *mc =
2315 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002316 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002317 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002318 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002319 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002320
2321 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2322 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2323
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002324 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002325}
2326EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2327
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002328/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002329 * snd_soc_limit_volume - Set new limit to an existing volume control.
2330 *
2331 * @codec: where to look for the control
2332 * @name: Name of the control
2333 * @max: new maximum limit
2334 *
2335 * Return 0 for success, else error.
2336 */
2337int snd_soc_limit_volume(struct snd_soc_codec *codec,
2338 const char *name, int max)
2339{
2340 struct snd_card *card = codec->card;
2341 struct snd_kcontrol *kctl;
2342 struct soc_mixer_control *mc;
2343 int found = 0;
2344 int ret = -EINVAL;
2345
2346 /* Sanity check for name and max */
2347 if (unlikely(!name || max <= 0))
2348 return -EINVAL;
2349
2350 list_for_each_entry(kctl, &card->controls, list) {
2351 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2352 found = 1;
2353 break;
2354 }
2355 }
2356 if (found) {
2357 mc = (struct soc_mixer_control *)kctl->private_value;
2358 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002359 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002360 ret = 0;
2361 }
2362 }
2363 return ret;
2364}
2365EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2366
2367/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002368 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2369 * mixer info callback
2370 * @kcontrol: mixer control
2371 * @uinfo: control element information
2372 *
2373 * Returns 0 for success.
2374 */
2375int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2376 struct snd_ctl_elem_info *uinfo)
2377{
2378 struct soc_mixer_control *mc =
2379 (struct soc_mixer_control *)kcontrol->private_value;
2380 int max = mc->max;
2381 int min = mc->min;
2382
2383 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2384 uinfo->count = 2;
2385 uinfo->value.integer.min = 0;
2386 uinfo->value.integer.max = max-min;
2387
2388 return 0;
2389}
2390EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2391
2392/**
2393 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2394 * mixer get callback
2395 * @kcontrol: mixer control
2396 * @uinfo: control element information
2397 *
2398 * Returns 0 for success.
2399 */
2400int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2401 struct snd_ctl_elem_value *ucontrol)
2402{
2403 struct soc_mixer_control *mc =
2404 (struct soc_mixer_control *)kcontrol->private_value;
2405 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2406 unsigned int mask = (1<<mc->shift)-1;
2407 int min = mc->min;
2408 int val = snd_soc_read(codec, mc->reg) & mask;
2409 int valr = snd_soc_read(codec, mc->rreg) & mask;
2410
Stuart Longland20630c72010-06-18 12:56:10 +10002411 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2412 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002413 return 0;
2414}
2415EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2416
2417/**
2418 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2419 * mixer put callback
2420 * @kcontrol: mixer control
2421 * @uinfo: control element information
2422 *
2423 * Returns 0 for success.
2424 */
2425int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_value *ucontrol)
2427{
2428 struct soc_mixer_control *mc =
2429 (struct soc_mixer_control *)kcontrol->private_value;
2430 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2431 unsigned int mask = (1<<mc->shift)-1;
2432 int min = mc->min;
2433 int ret;
2434 unsigned int val, valr, oval, ovalr;
2435
2436 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2437 val &= mask;
2438 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2439 valr &= mask;
2440
2441 oval = snd_soc_read(codec, mc->reg) & mask;
2442 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2443
2444 ret = 0;
2445 if (oval != val) {
2446 ret = snd_soc_write(codec, mc->reg, val);
2447 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002448 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002449 }
2450 if (ovalr != valr) {
2451 ret = snd_soc_write(codec, mc->rreg, valr);
2452 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002453 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002454 }
2455
2456 return 0;
2457}
2458EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2459
2460/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002461 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2462 * @dai: DAI
2463 * @clk_id: DAI specific clock ID
2464 * @freq: new clock frequency in Hz
2465 * @dir: new clock direction - input/output.
2466 *
2467 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2468 */
2469int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2470 unsigned int freq, int dir)
2471{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002472 if (dai->ops && dai->ops->set_sysclk)
Eric Miao6335d052009-03-03 09:41:00 +08002473 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002474 else
2475 return -EINVAL;
2476}
2477EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2478
2479/**
2480 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2481 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002482 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002483 * @div: new clock divisor.
2484 *
2485 * Configures the clock dividers. This is used to derive the best DAI bit and
2486 * frame clocks from the system or master clock. It's best to set the DAI bit
2487 * and frame clocks as low as possible to save system power.
2488 */
2489int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2490 int div_id, int div)
2491{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002492 if (dai->ops && dai->ops->set_clkdiv)
Eric Miao6335d052009-03-03 09:41:00 +08002493 return dai->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002494 else
2495 return -EINVAL;
2496}
2497EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2498
2499/**
2500 * snd_soc_dai_set_pll - configure DAI PLL.
2501 * @dai: DAI
2502 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002503 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002504 * @freq_in: PLL input clock frequency in Hz
2505 * @freq_out: requested PLL output clock frequency in Hz
2506 *
2507 * Configures and enables PLL to generate output clock based on input clock.
2508 */
Mark Brown85488032009-09-05 18:52:16 +01002509int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2510 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002511{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002512 if (dai->ops && dai->ops->set_pll)
Mark Brown85488032009-09-05 18:52:16 +01002513 return dai->ops->set_pll(dai, pll_id, source,
2514 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002515 else
2516 return -EINVAL;
2517}
2518EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2519
2520/**
2521 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2522 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002523 * @fmt: SND_SOC_DAIFMT_ format value.
2524 *
2525 * Configures the DAI hardware format and clocking.
2526 */
2527int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2528{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002529 if (dai->ops && dai->ops->set_fmt)
Eric Miao6335d052009-03-03 09:41:00 +08002530 return dai->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002531 else
2532 return -EINVAL;
2533}
2534EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2535
2536/**
2537 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2538 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002539 * @tx_mask: bitmask representing active TX slots.
2540 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002541 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002542 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002543 *
2544 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2545 * specific.
2546 */
2547int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002548 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002549{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002550 if (dai->ops && dai->ops->set_tdm_slot)
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002551 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2552 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002553 else
2554 return -EINVAL;
2555}
2556EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2557
2558/**
Barry Song472df3c2009-09-12 01:16:29 +08002559 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2560 * @dai: DAI
2561 * @tx_num: how many TX channels
2562 * @tx_slot: pointer to an array which imply the TX slot number channel
2563 * 0~num-1 uses
2564 * @rx_num: how many RX channels
2565 * @rx_slot: pointer to an array which imply the RX slot number channel
2566 * 0~num-1 uses
2567 *
2568 * configure the relationship between channel number and TDM slot number.
2569 */
2570int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2571 unsigned int tx_num, unsigned int *tx_slot,
2572 unsigned int rx_num, unsigned int *rx_slot)
2573{
2574 if (dai->ops && dai->ops->set_channel_map)
2575 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2576 rx_num, rx_slot);
2577 else
2578 return -EINVAL;
2579}
2580EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2581
2582/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002583 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2584 * @dai: DAI
2585 * @tristate: tristate enable
2586 *
2587 * Tristates the DAI so that others can use it.
2588 */
2589int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2590{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002591 if (dai->ops && dai->ops->set_tristate)
Eric Miao6335d052009-03-03 09:41:00 +08002592 return dai->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002593 else
2594 return -EINVAL;
2595}
2596EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2597
2598/**
2599 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2600 * @dai: DAI
2601 * @mute: mute enable
2602 *
2603 * Mutes the DAI DAC.
2604 */
2605int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2606{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002607 if (dai->ops && dai->ops->digital_mute)
Eric Miao6335d052009-03-03 09:41:00 +08002608 return dai->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002609 else
2610 return -EINVAL;
2611}
2612EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2613
Mark Brownc5af3a22008-11-28 13:29:45 +00002614/**
2615 * snd_soc_register_card - Register a card with the ASoC core
2616 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002617 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002618 *
2619 * Note that currently this is an internal only function: it will be
2620 * exposed to machine drivers after further backporting of ASoC v2
2621 * registration APIs.
2622 */
2623static int snd_soc_register_card(struct snd_soc_card *card)
2624{
2625 if (!card->name || !card->dev)
2626 return -EINVAL;
2627
2628 INIT_LIST_HEAD(&card->list);
2629 card->instantiated = 0;
2630
2631 mutex_lock(&client_mutex);
2632 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002633 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002634 mutex_unlock(&client_mutex);
2635
2636 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2637
2638 return 0;
2639}
2640
2641/**
2642 * snd_soc_unregister_card - Unregister a card with the ASoC core
2643 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002644 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002645 *
2646 * Note that currently this is an internal only function: it will be
2647 * exposed to machine drivers after further backporting of ASoC v2
2648 * registration APIs.
2649 */
2650static int snd_soc_unregister_card(struct snd_soc_card *card)
2651{
2652 mutex_lock(&client_mutex);
2653 list_del(&card->list);
2654 mutex_unlock(&client_mutex);
2655
2656 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2657
2658 return 0;
2659}
2660
Mark Brown91151712008-11-30 23:31:24 +00002661/**
2662 * snd_soc_register_dai - Register a DAI with the ASoC core
2663 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002664 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002665 */
2666int snd_soc_register_dai(struct snd_soc_dai *dai)
2667{
2668 if (!dai->name)
2669 return -EINVAL;
2670
2671 /* The device should become mandatory over time */
2672 if (!dai->dev)
2673 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2674
Eric Miao6335d052009-03-03 09:41:00 +08002675 if (!dai->ops)
2676 dai->ops = &null_dai_ops;
2677
Mark Brown91151712008-11-30 23:31:24 +00002678 INIT_LIST_HEAD(&dai->list);
2679
2680 mutex_lock(&client_mutex);
2681 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002682 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002683 mutex_unlock(&client_mutex);
2684
2685 pr_debug("Registered DAI '%s'\n", dai->name);
2686
2687 return 0;
2688}
2689EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2690
2691/**
2692 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2693 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002694 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002695 */
2696void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2697{
2698 mutex_lock(&client_mutex);
2699 list_del(&dai->list);
2700 mutex_unlock(&client_mutex);
2701
2702 pr_debug("Unregistered DAI '%s'\n", dai->name);
2703}
2704EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2705
2706/**
2707 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2708 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002709 * @dai: Array of DAIs to register
2710 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002711 */
2712int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2713{
2714 int i, ret;
2715
2716 for (i = 0; i < count; i++) {
2717 ret = snd_soc_register_dai(&dai[i]);
2718 if (ret != 0)
2719 goto err;
2720 }
2721
2722 return 0;
2723
2724err:
2725 for (i--; i >= 0; i--)
2726 snd_soc_unregister_dai(&dai[i]);
2727
2728 return ret;
2729}
2730EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2731
2732/**
2733 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2734 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002735 * @dai: Array of DAIs to unregister
2736 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002737 */
2738void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2739{
2740 int i;
2741
2742 for (i = 0; i < count; i++)
2743 snd_soc_unregister_dai(&dai[i]);
2744}
2745EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2746
Mark Brown12a48a8c2008-12-03 19:40:30 +00002747/**
2748 * snd_soc_register_platform - Register a platform with the ASoC core
2749 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002750 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00002751 */
2752int snd_soc_register_platform(struct snd_soc_platform *platform)
2753{
2754 if (!platform->name)
2755 return -EINVAL;
2756
2757 INIT_LIST_HEAD(&platform->list);
2758
2759 mutex_lock(&client_mutex);
2760 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002761 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00002762 mutex_unlock(&client_mutex);
2763
2764 pr_debug("Registered platform '%s'\n", platform->name);
2765
2766 return 0;
2767}
2768EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2769
2770/**
2771 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2772 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002773 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002774 */
2775void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2776{
2777 mutex_lock(&client_mutex);
2778 list_del(&platform->list);
2779 mutex_unlock(&client_mutex);
2780
2781 pr_debug("Unregistered platform '%s'\n", platform->name);
2782}
2783EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2784
Mark Brown151ab222009-05-09 16:22:58 +01002785static u64 codec_format_map[] = {
2786 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2787 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2788 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2789 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2790 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2791 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2792 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2793 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2794 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2795 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2796 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2797 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2798 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2799 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2800 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2801 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2802};
2803
2804/* Fix up the DAI formats for endianness: codecs don't actually see
2805 * the endianness of the data but we're using the CPU format
2806 * definitions which do need to include endianness so we ensure that
2807 * codec DAIs always have both big and little endian variants set.
2808 */
2809static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2810{
2811 int i;
2812
2813 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2814 if (stream->formats & codec_format_map[i])
2815 stream->formats |= codec_format_map[i];
2816}
2817
Mark Brown0d0cf002008-12-10 14:32:45 +00002818/**
2819 * snd_soc_register_codec - Register a codec with the ASoC core
2820 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002821 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00002822 */
2823int snd_soc_register_codec(struct snd_soc_codec *codec)
2824{
Mark Brown151ab222009-05-09 16:22:58 +01002825 int i;
2826
Mark Brown0d0cf002008-12-10 14:32:45 +00002827 if (!codec->name)
2828 return -EINVAL;
2829
2830 /* The device should become mandatory over time */
2831 if (!codec->dev)
2832 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2833
2834 INIT_LIST_HEAD(&codec->list);
2835
Mark Brown151ab222009-05-09 16:22:58 +01002836 for (i = 0; i < codec->num_dai; i++) {
2837 fixup_codec_formats(&codec->dai[i].playback);
2838 fixup_codec_formats(&codec->dai[i].capture);
2839 }
2840
Mark Brown0d0cf002008-12-10 14:32:45 +00002841 mutex_lock(&client_mutex);
2842 list_add(&codec->list, &codec_list);
2843 snd_soc_instantiate_cards();
2844 mutex_unlock(&client_mutex);
2845
2846 pr_debug("Registered codec '%s'\n", codec->name);
2847
2848 return 0;
2849}
2850EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2851
2852/**
2853 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2854 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002855 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00002856 */
2857void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2858{
2859 mutex_lock(&client_mutex);
2860 list_del(&codec->list);
2861 mutex_unlock(&client_mutex);
2862
2863 pr_debug("Unregistered codec '%s'\n", codec->name);
2864}
2865EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2866
Takashi Iwaic9b3a402008-12-10 07:47:22 +01002867static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002868{
Mark Brown384c89e2008-12-03 17:34:03 +00002869#ifdef CONFIG_DEBUG_FS
2870 debugfs_root = debugfs_create_dir("asoc", NULL);
2871 if (IS_ERR(debugfs_root) || !debugfs_root) {
2872 printk(KERN_WARNING
2873 "ASoC: Failed to create debugfs directory\n");
2874 debugfs_root = NULL;
2875 }
2876#endif
2877
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002878 return platform_driver_register(&soc_driver);
2879}
2880
Mark Brown7d8c16a2008-11-30 22:11:24 +00002881static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002882{
Mark Brown384c89e2008-12-03 17:34:03 +00002883#ifdef CONFIG_DEBUG_FS
2884 debugfs_remove_recursive(debugfs_root);
2885#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02002886 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002887}
2888
2889module_init(snd_soc_init);
2890module_exit(snd_soc_exit);
2891
2892/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002893MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002894MODULE_DESCRIPTION("ALSA SoC Core");
2895MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02002896MODULE_ALIAS("platform:soc-audio");