blob: 1215a0ec2df0bfcec2d3c5b1ae9a6bc373393ec0 [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>
Marek Vasut474828a2009-07-22 13:01:03 +020031#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <sound/core.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/soc.h>
36#include <sound/soc-dapm.h>
37#include <sound/initval.h>
38
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039static DEFINE_MUTEX(pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
41
Mark Brown384c89e2008-12-03 17:34:03 +000042#ifdef CONFIG_DEBUG_FS
43static struct dentry *debugfs_root;
44#endif
45
Mark Brownc5af3a22008-11-28 13:29:45 +000046static DEFINE_MUTEX(client_mutex);
47static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000048static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000049static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000050static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000051
52static int snd_soc_register_card(struct snd_soc_card *card);
53static int snd_soc_unregister_card(struct snd_soc_card *card);
54
Frank Mandarinodb2a4162006-10-06 18:31:09 +020055/*
56 * This is a timeout to do a DAPM powerdown after a stream is closed().
57 * It can be used to eliminate pops between different playback streams, e.g.
58 * between two audio tracks.
59 */
60static int pmdown_time = 5000;
61module_param(pmdown_time, int, 0);
62MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
63
Liam Girdwood965ac422007-01-31 14:14:57 +010064/*
65 * This function forces any delayed work to be queued and run.
66 */
67static int run_delayed_work(struct delayed_work *dwork)
68{
69 int ret;
70
71 /* cancel any work waiting to be queued. */
72 ret = cancel_delayed_work(dwork);
73
74 /* if there was any work waiting then we run it now and
75 * wait for it's completion */
76 if (ret) {
77 schedule_delayed_work(dwork, 0);
78 flush_scheduled_work();
79 }
80 return ret;
81}
82
Mark Brown2624d5f2009-11-03 21:56:13 +000083/* codec register dump */
84static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
85{
86 int i, step = 1, count = 0;
87
88 if (!codec->reg_cache_size)
89 return 0;
90
91 if (codec->reg_cache_step)
92 step = codec->reg_cache_step;
93
94 count += sprintf(buf, "%s registers\n", codec->name);
95 for (i = 0; i < codec->reg_cache_size; i += step) {
96 if (codec->readable_register && !codec->readable_register(i))
97 continue;
98
99 count += sprintf(buf + count, "%2x: ", i);
100 if (count >= PAGE_SIZE - 1)
101 break;
102
103 if (codec->display_register)
104 count += codec->display_register(codec, buf + count,
105 PAGE_SIZE - count, i);
106 else
107 count += snprintf(buf + count, PAGE_SIZE - count,
108 "%4x", codec->read(codec, i));
109
110 if (count >= PAGE_SIZE - 1)
111 break;
112
113 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
114 if (count >= PAGE_SIZE - 1)
115 break;
116 }
117
118 /* Truncate count; min() would cause a warning */
119 if (count >= PAGE_SIZE)
120 count = PAGE_SIZE - 1;
121
122 return count;
123}
124static ssize_t codec_reg_show(struct device *dev,
125 struct device_attribute *attr, char *buf)
126{
127 struct snd_soc_device *devdata = dev_get_drvdata(dev);
128 return soc_codec_reg_show(devdata->card->codec, buf);
129}
130
131static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
132
Mark Browndbe21402010-02-12 11:37:24 +0000133static ssize_t pmdown_time_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
135{
136 struct snd_soc_device *socdev = dev_get_drvdata(dev);
137 struct snd_soc_card *card = socdev->card;
138
Mark Brown6c5f1fe2010-02-17 14:30:44 +0000139 return sprintf(buf, "%ld\n", card->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000140}
141
142static ssize_t pmdown_time_set(struct device *dev,
143 struct device_attribute *attr,
144 const char *buf, size_t count)
145{
146 struct snd_soc_device *socdev = dev_get_drvdata(dev);
147 struct snd_soc_card *card = socdev->card;
148
149 strict_strtol(buf, 10, &card->pmdown_time);
150
151 return count;
152}
153
154static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
155
Mark Brown2624d5f2009-11-03 21:56:13 +0000156#ifdef CONFIG_DEBUG_FS
157static int codec_reg_open_file(struct inode *inode, struct file *file)
158{
159 file->private_data = inode->i_private;
160 return 0;
161}
162
163static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
164 size_t count, loff_t *ppos)
165{
166 ssize_t ret;
167 struct snd_soc_codec *codec = file->private_data;
168 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
169 if (!buf)
170 return -ENOMEM;
171 ret = soc_codec_reg_show(codec, buf);
172 if (ret >= 0)
173 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
174 kfree(buf);
175 return ret;
176}
177
178static ssize_t codec_reg_write_file(struct file *file,
179 const char __user *user_buf, size_t count, loff_t *ppos)
180{
181 char buf[32];
182 int buf_size;
183 char *start = buf;
184 unsigned long reg, value;
185 int step = 1;
186 struct snd_soc_codec *codec = file->private_data;
187
188 buf_size = min(count, (sizeof(buf)-1));
189 if (copy_from_user(buf, user_buf, buf_size))
190 return -EFAULT;
191 buf[buf_size] = 0;
192
193 if (codec->reg_cache_step)
194 step = codec->reg_cache_step;
195
196 while (*start == ' ')
197 start++;
198 reg = simple_strtoul(start, &start, 16);
199 if ((reg >= codec->reg_cache_size) || (reg % step))
200 return -EINVAL;
201 while (*start == ' ')
202 start++;
203 if (strict_strtoul(start, 16, &value))
204 return -EINVAL;
205 codec->write(codec, reg, value);
206 return buf_size;
207}
208
209static const struct file_operations codec_reg_fops = {
210 .open = codec_reg_open_file,
211 .read = codec_reg_read_file,
212 .write = codec_reg_write_file,
213};
214
215static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
216{
217 char codec_root[128];
218
219 if (codec->dev)
220 snprintf(codec_root, sizeof(codec_root),
221 "%s.%s", codec->name, dev_name(codec->dev));
222 else
223 snprintf(codec_root, sizeof(codec_root),
224 "%s", codec->name);
225
226 codec->debugfs_codec_root = debugfs_create_dir(codec_root,
227 debugfs_root);
228 if (!codec->debugfs_codec_root) {
229 printk(KERN_WARNING
230 "ASoC: Failed to create codec debugfs directory\n");
231 return;
232 }
233
234 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
235 codec->debugfs_codec_root,
236 codec, &codec_reg_fops);
237 if (!codec->debugfs_reg)
238 printk(KERN_WARNING
239 "ASoC: Failed to create codec register debugfs file\n");
240
241 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
242 codec->debugfs_codec_root,
243 &codec->pop_time);
244 if (!codec->debugfs_pop_time)
245 printk(KERN_WARNING
246 "Failed to create pop time debugfs file\n");
247
248 codec->debugfs_dapm = debugfs_create_dir("dapm",
249 codec->debugfs_codec_root);
250 if (!codec->debugfs_dapm)
251 printk(KERN_WARNING
252 "Failed to create DAPM debugfs directory\n");
253
254 snd_soc_dapm_debugfs_init(codec);
255}
256
257static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
258{
259 debugfs_remove_recursive(codec->debugfs_codec_root);
260}
261
262#else
263
264static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
265{
266}
267
268static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
269{
270}
271#endif
272
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200273#ifdef CONFIG_SND_SOC_AC97_BUS
274/* unregister ac97 codec */
275static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
276{
277 if (codec->ac97->dev.bus)
278 device_unregister(&codec->ac97->dev);
279 return 0;
280}
281
282/* stop no dev release warning */
283static void soc_ac97_device_release(struct device *dev){}
284
285/* register ac97 codec to bus */
286static int soc_ac97_dev_register(struct snd_soc_codec *codec)
287{
288 int err;
289
290 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100291 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200292 codec->ac97->dev.release = soc_ac97_device_release;
293
Kay Sieversbb072bf2008-11-02 03:50:35 +0100294 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
295 codec->card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200296 err = device_register(&codec->ac97->dev);
297 if (err < 0) {
298 snd_printk(KERN_ERR "Can't register ac97 bus\n");
299 codec->ac97->dev.bus = NULL;
300 return err;
301 }
302 return 0;
303}
304#endif
305
Mark Brown06f409d2009-04-07 18:10:13 +0100306static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
307{
308 struct snd_soc_pcm_runtime *rtd = substream->private_data;
309 struct snd_soc_device *socdev = rtd->socdev;
310 struct snd_soc_card *card = socdev->card;
311 struct snd_soc_dai_link *machine = rtd->dai;
312 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
313 struct snd_soc_dai *codec_dai = machine->codec_dai;
314 int ret;
315
316 if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
317 machine->symmetric_rates) {
318 dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
319 machine->rate);
320
321 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
322 SNDRV_PCM_HW_PARAM_RATE,
323 machine->rate,
324 machine->rate);
325 if (ret < 0) {
326 dev_err(card->dev,
327 "Unable to apply rate symmetry constraint: %d\n", ret);
328 return ret;
329 }
330 }
331
332 return 0;
333}
334
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200335/*
336 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
337 * then initialized and any private data can be allocated. This also calls
338 * startup for the cpu DAI, platform, machine and codec DAI.
339 */
340static int soc_pcm_open(struct snd_pcm_substream *substream)
341{
342 struct snd_soc_pcm_runtime *rtd = substream->private_data;
343 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000344 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200345 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100346 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000347 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100348 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
349 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200350 int ret = 0;
351
352 mutex_lock(&pcm_mutex);
353
354 /* startup the audio subsystem */
Eric Miao6335d052009-03-03 09:41:00 +0800355 if (cpu_dai->ops->startup) {
356 ret = cpu_dai->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200357 if (ret < 0) {
358 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100359 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200360 goto out;
361 }
362 }
363
364 if (platform->pcm_ops->open) {
365 ret = platform->pcm_ops->open(substream);
366 if (ret < 0) {
367 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
368 goto platform_err;
369 }
370 }
371
Eric Miao6335d052009-03-03 09:41:00 +0800372 if (codec_dai->ops->startup) {
373 ret = codec_dai->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100374 if (ret < 0) {
375 printk(KERN_ERR "asoc: can't open codec %s\n",
376 codec_dai->name);
377 goto codec_dai_err;
378 }
379 }
380
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200381 if (machine->ops && machine->ops->startup) {
382 ret = machine->ops->startup(substream);
383 if (ret < 0) {
384 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
385 goto machine_err;
386 }
387 }
388
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200389 /* Check that the codec and cpu DAI's are compatible */
390 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
391 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200392 max(codec_dai->playback.rate_min,
393 cpu_dai->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200394 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200395 min(codec_dai->playback.rate_max,
396 cpu_dai->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200397 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100398 max(codec_dai->playback.channels_min,
399 cpu_dai->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200400 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100401 min(codec_dai->playback.channels_max,
402 cpu_dai->playback.channels_max);
403 runtime->hw.formats =
404 codec_dai->playback.formats & cpu_dai->playback.formats;
405 runtime->hw.rates =
406 codec_dai->playback.rates & cpu_dai->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200407 } else {
408 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200409 max(codec_dai->capture.rate_min,
410 cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200411 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200412 min(codec_dai->capture.rate_max,
413 cpu_dai->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200414 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100415 max(codec_dai->capture.channels_min,
416 cpu_dai->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200417 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100418 min(codec_dai->capture.channels_max,
419 cpu_dai->capture.channels_max);
420 runtime->hw.formats =
421 codec_dai->capture.formats & cpu_dai->capture.formats;
422 runtime->hw.rates =
423 codec_dai->capture.rates & cpu_dai->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200424 }
425
426 snd_pcm_limit_hw_rates(runtime);
427 if (!runtime->hw.rates) {
428 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100429 codec_dai->name, cpu_dai->name);
430 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200431 }
432 if (!runtime->hw.formats) {
433 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100434 codec_dai->name, cpu_dai->name);
435 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200436 }
437 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
438 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100439 codec_dai->name, cpu_dai->name);
440 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200441 }
442
Mark Brown06f409d2009-04-07 18:10:13 +0100443 /* Symmetry only applies if we've already got an active stream. */
444 if (cpu_dai->active || codec_dai->active) {
445 ret = soc_pcm_apply_symmetry(substream);
446 if (ret != 0)
447 goto machine_err;
448 }
449
Mark Brownf24368c2008-10-21 21:45:08 +0100450 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
451 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
452 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
453 runtime->hw.channels_max);
454 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
455 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200456
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200457 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100458 cpu_dai->playback.active = codec_dai->playback.active = 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200459 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100460 cpu_dai->capture.active = codec_dai->capture.active = 1;
461 cpu_dai->active = codec_dai->active = 1;
Mark Brown6627a652009-01-23 22:55:23 +0000462 card->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200463 mutex_unlock(&pcm_mutex);
464 return 0;
465
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100466machine_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200467 if (machine->ops && machine->ops->shutdown)
468 machine->ops->shutdown(substream);
469
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100470codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200471 if (platform->pcm_ops->close)
472 platform->pcm_ops->close(substream);
473
474platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800475 if (cpu_dai->ops->shutdown)
476 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200477out:
478 mutex_unlock(&pcm_mutex);
479 return ret;
480}
481
482/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200483 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200484 * This is to ensure there are no pops or clicks in between any music tracks
485 * due to DAPM power cycling.
486 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100487static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200488{
Mark Brown63084192008-12-02 15:08:03 +0000489 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
490 delayed_work.work);
Mark Brown6627a652009-01-23 22:55:23 +0000491 struct snd_soc_codec *codec = card->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100492 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493 int i;
494
495 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200496 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200497 codec_dai = &codec->dai[i];
498
Mark Brownf24368c2008-10-21 21:45:08 +0100499 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
500 codec_dai->playback.stream_name,
501 codec_dai->playback.active ? "active" : "inactive",
502 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200503
504 /* are we waiting on this codec DAI stream */
505 if (codec_dai->pop_wait == 1) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200506 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100507 snd_soc_dapm_stream_event(codec,
508 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200509 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510 }
511 }
512 mutex_unlock(&pcm_mutex);
513}
514
515/*
516 * Called by ALSA when a PCM substream is closed. Private data can be
517 * freed here. The cpu DAI, codec DAI, machine and platform are also
518 * shutdown.
519 */
520static int soc_codec_close(struct snd_pcm_substream *substream)
521{
522 struct snd_soc_pcm_runtime *rtd = substream->private_data;
523 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000524 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100525 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000526 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100527 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
528 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000529 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200530
531 mutex_lock(&pcm_mutex);
532
533 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100534 cpu_dai->playback.active = codec_dai->playback.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200535 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100536 cpu_dai->capture.active = codec_dai->capture.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200537
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100538 if (codec_dai->playback.active == 0 &&
539 codec_dai->capture.active == 0) {
540 cpu_dai->active = codec_dai->active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200541 }
542 codec->active--;
543
Mark Brown6010b2d2008-09-06 18:33:24 +0100544 /* Muting the DAC suppresses artifacts caused during digital
545 * shutdown, for example from stopping clocks.
546 */
547 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
548 snd_soc_dai_digital_mute(codec_dai, 1);
549
Eric Miao6335d052009-03-03 09:41:00 +0800550 if (cpu_dai->ops->shutdown)
551 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552
Eric Miao6335d052009-03-03 09:41:00 +0800553 if (codec_dai->ops->shutdown)
554 codec_dai->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200555
556 if (machine->ops && machine->ops->shutdown)
557 machine->ops->shutdown(substream);
558
559 if (platform->pcm_ops->close)
560 platform->pcm_ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561
562 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
563 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100564 codec_dai->pop_wait = 1;
Mark Brown63084192008-12-02 15:08:03 +0000565 schedule_delayed_work(&card->delayed_work,
Mark Brown96dd3622010-02-12 11:05:44 +0000566 msecs_to_jiffies(card->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200567 } else {
568 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100569 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100570 codec_dai->capture.stream_name,
571 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200572 }
573
574 mutex_unlock(&pcm_mutex);
575 return 0;
576}
577
578/*
579 * Called by ALSA when the PCM substream is prepared, can set format, sample
580 * rate, etc. This function is non atomic and can be called multiple times,
581 * it can refer to the runtime info.
582 */
583static int soc_pcm_prepare(struct snd_pcm_substream *substream)
584{
585 struct snd_soc_pcm_runtime *rtd = substream->private_data;
586 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000587 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100588 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000589 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100590 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
591 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000592 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200593 int ret = 0;
594
595 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100596
597 if (machine->ops && machine->ops->prepare) {
598 ret = machine->ops->prepare(substream);
599 if (ret < 0) {
600 printk(KERN_ERR "asoc: machine prepare error\n");
601 goto out;
602 }
603 }
604
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200605 if (platform->pcm_ops->prepare) {
606 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200607 if (ret < 0) {
608 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200609 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200610 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611 }
612
Eric Miao6335d052009-03-03 09:41:00 +0800613 if (codec_dai->ops->prepare) {
614 ret = codec_dai->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200615 if (ret < 0) {
616 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200617 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200618 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619 }
620
Eric Miao6335d052009-03-03 09:41:00 +0800621 if (cpu_dai->ops->prepare) {
622 ret = cpu_dai->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100623 if (ret < 0) {
624 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
625 goto out;
626 }
627 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200628
Mark Brownd45f6212008-10-14 13:58:36 +0100629 /* cancel any delayed stream shutdown that is pending */
630 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
631 codec_dai->pop_wait) {
632 codec_dai->pop_wait = 0;
Mark Brown63084192008-12-02 15:08:03 +0000633 cancel_delayed_work(&card->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100634 }
635
Mark Brown452c5ea2009-05-17 21:41:23 +0100636 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
637 snd_soc_dapm_stream_event(codec,
638 codec_dai->playback.stream_name,
639 SND_SOC_DAPM_STREAM_START);
640 else
641 snd_soc_dapm_stream_event(codec,
642 codec_dai->capture.stream_name,
643 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100644
Mark Brown452c5ea2009-05-17 21:41:23 +0100645 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200646
647out:
648 mutex_unlock(&pcm_mutex);
649 return ret;
650}
651
652/*
653 * Called by ALSA when the hardware params are set by application. This
654 * function can also be called multiple times and can allocate buffers
655 * (using snd_pcm_lib_* ). It's non-atomic.
656 */
657static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
658 struct snd_pcm_hw_params *params)
659{
660 struct snd_soc_pcm_runtime *rtd = substream->private_data;
661 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100662 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000663 struct snd_soc_card *card = socdev->card;
664 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100665 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
666 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667 int ret = 0;
668
669 mutex_lock(&pcm_mutex);
670
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100671 if (machine->ops && machine->ops->hw_params) {
672 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200673 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100674 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200675 goto out;
676 }
677 }
678
Eric Miao6335d052009-03-03 09:41:00 +0800679 if (codec_dai->ops->hw_params) {
680 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100681 if (ret < 0) {
682 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
683 codec_dai->name);
684 goto codec_err;
685 }
686 }
687
Eric Miao6335d052009-03-03 09:41:00 +0800688 if (cpu_dai->ops->hw_params) {
689 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200691 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100692 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200693 goto interface_err;
694 }
695 }
696
697 if (platform->pcm_ops->hw_params) {
698 ret = platform->pcm_ops->hw_params(substream, params);
699 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200700 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200701 platform->name);
702 goto platform_err;
703 }
704 }
705
Mark Brown06f409d2009-04-07 18:10:13 +0100706 machine->rate = params_rate(params);
707
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200708out:
709 mutex_unlock(&pcm_mutex);
710 return ret;
711
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800713 if (cpu_dai->ops->hw_free)
714 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715
716interface_err:
Eric Miao6335d052009-03-03 09:41:00 +0800717 if (codec_dai->ops->hw_free)
718 codec_dai->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100719
720codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200721 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100722 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723
724 mutex_unlock(&pcm_mutex);
725 return ret;
726}
727
728/*
729 * Free's resources allocated by hw_params, can be called multiple times
730 */
731static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
732{
733 struct snd_soc_pcm_runtime *rtd = substream->private_data;
734 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100735 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000736 struct snd_soc_card *card = socdev->card;
737 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100738 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
739 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000740 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200741
742 mutex_lock(&pcm_mutex);
743
744 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100745 if (!codec->active)
746 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200747
748 /* free any machine hw params */
749 if (machine->ops && machine->ops->hw_free)
750 machine->ops->hw_free(substream);
751
752 /* free any DMA resources */
753 if (platform->pcm_ops->hw_free)
754 platform->pcm_ops->hw_free(substream);
755
756 /* now free hw params for the DAI's */
Eric Miao6335d052009-03-03 09:41:00 +0800757 if (codec_dai->ops->hw_free)
758 codec_dai->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200759
Eric Miao6335d052009-03-03 09:41:00 +0800760 if (cpu_dai->ops->hw_free)
761 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200762
763 mutex_unlock(&pcm_mutex);
764 return 0;
765}
766
767static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
768{
769 struct snd_soc_pcm_runtime *rtd = substream->private_data;
770 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000771 struct snd_soc_card *card= socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100772 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000773 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100774 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
775 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200776 int ret;
777
Eric Miao6335d052009-03-03 09:41:00 +0800778 if (codec_dai->ops->trigger) {
779 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200780 if (ret < 0)
781 return ret;
782 }
783
784 if (platform->pcm_ops->trigger) {
785 ret = platform->pcm_ops->trigger(substream, cmd);
786 if (ret < 0)
787 return ret;
788 }
789
Eric Miao6335d052009-03-03 09:41:00 +0800790 if (cpu_dai->ops->trigger) {
791 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200792 if (ret < 0)
793 return ret;
794 }
795 return 0;
796}
797
798/* ASoC PCM operations */
799static struct snd_pcm_ops soc_pcm_ops = {
800 .open = soc_pcm_open,
801 .close = soc_codec_close,
802 .hw_params = soc_pcm_hw_params,
803 .hw_free = soc_pcm_hw_free,
804 .prepare = soc_pcm_prepare,
805 .trigger = soc_pcm_trigger,
806};
807
808#ifdef CONFIG_PM
809/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100810static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200811{
Mark Brown416356f2009-06-30 19:05:15 +0100812 struct platform_device *pdev = to_platform_device(dev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200813 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000814 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000815 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200816 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000817 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200818 int i;
819
Daniel Macke3509ff2009-06-03 17:44:49 +0200820 /* If the initialization of this soc device failed, there is no codec
821 * associated with it. Just bail out in this case.
822 */
823 if (!codec)
824 return 0;
825
Andy Green6ed25972008-06-13 16:24:05 +0100826 /* Due to the resume being scheduled into a workqueue we could
827 * suspend before that's finished - wait for it to complete.
828 */
829 snd_power_lock(codec->card);
830 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
831 snd_power_unlock(codec->card);
832
833 /* we're going to block userspace touching us until resume completes */
834 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
835
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200836 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000837 for (i = 0; i < card->num_links; i++) {
838 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800839 if (dai->ops->digital_mute && dai->playback.active)
840 dai->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200841 }
842
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100843 /* suspend all pcms */
Mark Brown87506542008-11-18 20:50:34 +0000844 for (i = 0; i < card->num_links; i++)
845 snd_pcm_suspend_all(card->dai_link[i].pcm);
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100846
Mark Brown87506542008-11-18 20:50:34 +0000847 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100848 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849
Mark Brown87506542008-11-18 20:50:34 +0000850 for (i = 0; i < card->num_links; i++) {
851 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000852 if (cpu_dai->suspend && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000853 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200854 if (platform->suspend)
jassi brard273ebe2010-02-22 15:58:04 +0900855 platform->suspend(&card->dai_link[i]);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200856 }
857
858 /* close any waiting streams and save state */
Mark Brown63084192008-12-02 15:08:03 +0000859 run_delayed_work(&card->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200860 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200861
Mark Brown3ff3f642008-05-19 12:32:25 +0200862 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863 char *stream = codec->dai[i].playback.stream_name;
864 if (stream != NULL)
865 snd_soc_dapm_stream_event(codec, stream,
866 SND_SOC_DAPM_STREAM_SUSPEND);
867 stream = codec->dai[i].capture.stream_name;
868 if (stream != NULL)
869 snd_soc_dapm_stream_event(codec, stream,
870 SND_SOC_DAPM_STREAM_SUSPEND);
871 }
872
873 if (codec_dev->suspend)
Mark Brown416356f2009-06-30 19:05:15 +0100874 codec_dev->suspend(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200875
Mark Brown87506542008-11-18 20:50:34 +0000876 for (i = 0; i < card->num_links; i++) {
877 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000878 if (cpu_dai->suspend && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000879 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200880 }
881
Mark Brown87506542008-11-18 20:50:34 +0000882 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +0100883 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200884
885 return 0;
886}
887
Andy Green6ed25972008-06-13 16:24:05 +0100888/* deferred resume work, so resume can complete before we finished
889 * setting our codec back up, which can be very slow on I2C
890 */
891static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200892{
Mark Brown63084192008-12-02 15:08:03 +0000893 struct snd_soc_card *card = container_of(work,
894 struct snd_soc_card,
895 deferred_resume_work);
896 struct snd_soc_device *socdev = card->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000897 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200898 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000899 struct snd_soc_codec *codec = card->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100900 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200901 int i;
902
Andy Green6ed25972008-06-13 16:24:05 +0100903 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
904 * so userspace apps are blocked from touching us
905 */
906
Mark Brownfde22f22008-11-24 18:08:18 +0000907 dev_dbg(socdev->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100908
Mark Brown87506542008-11-18 20:50:34 +0000909 if (card->resume_pre)
910 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200911
Mark Brown87506542008-11-18 20:50:34 +0000912 for (i = 0; i < card->num_links; i++) {
913 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000914 if (cpu_dai->resume && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000915 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200916 }
917
918 if (codec_dev->resume)
919 codec_dev->resume(pdev);
920
Mark Brown3ff3f642008-05-19 12:32:25 +0200921 for (i = 0; i < codec->num_dai; i++) {
922 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200923 if (stream != NULL)
924 snd_soc_dapm_stream_event(codec, stream,
925 SND_SOC_DAPM_STREAM_RESUME);
926 stream = codec->dai[i].capture.stream_name;
927 if (stream != NULL)
928 snd_soc_dapm_stream_event(codec, stream,
929 SND_SOC_DAPM_STREAM_RESUME);
930 }
931
Mark Brown3ff3f642008-05-19 12:32:25 +0200932 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +0000933 for (i = 0; i < card->num_links; i++) {
934 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800935 if (dai->ops->digital_mute && dai->playback.active)
936 dai->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200937 }
938
Mark Brown87506542008-11-18 20:50:34 +0000939 for (i = 0; i < card->num_links; i++) {
940 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000941 if (cpu_dai->resume && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000942 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200943 if (platform->resume)
jassi brard273ebe2010-02-22 15:58:04 +0900944 platform->resume(&card->dai_link[i]);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200945 }
946
Mark Brown87506542008-11-18 20:50:34 +0000947 if (card->resume_post)
948 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200949
Mark Brownfde22f22008-11-24 18:08:18 +0000950 dev_dbg(socdev->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100951
952 /* userspace can access us now we are back as we were before */
953 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
954}
955
956/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100957static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100958{
Mark Brown416356f2009-06-30 19:05:15 +0100959 struct platform_device *pdev = to_platform_device(dev);
Andy Green6ed25972008-06-13 16:24:05 +0100960 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown63084192008-12-02 15:08:03 +0000961 struct snd_soc_card *card = socdev->card;
Mark Brown64ab9ba2009-03-31 11:27:03 +0100962 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
Andy Green6ed25972008-06-13 16:24:05 +0100963
Mark Brown64ab9ba2009-03-31 11:27:03 +0100964 /* AC97 devices might have other drivers hanging off them so
965 * need to resume immediately. Other drivers don't have that
966 * problem and may take a substantial amount of time to resume
967 * due to I/O costs and anti-pop so handle them out of line.
968 */
969 if (cpu_dai->ac97_control) {
970 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
971 soc_resume_deferred(&card->deferred_resume_work);
972 } else {
973 dev_dbg(socdev->dev, "Scheduling resume work\n");
974 if (!schedule_work(&card->deferred_resume_work))
975 dev_err(socdev->dev, "resume work item may be lost\n");
976 }
Andy Green6ed25972008-06-13 16:24:05 +0100977
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200978 return 0;
979}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200980#else
981#define soc_suspend NULL
982#define soc_resume NULL
983#endif
984
Barry Song02a06d32009-10-16 18:13:38 +0800985static struct snd_soc_dai_ops null_dai_ops = {
986};
987
Mark Brown435c5e22008-12-04 15:32:53 +0000988static void snd_soc_instantiate_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200989{
Mark Brown435c5e22008-12-04 15:32:53 +0000990 struct platform_device *pdev = container_of(card->dev,
991 struct platform_device,
992 dev);
993 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
Mark Brownfe3e78e2009-11-03 22:13:13 +0000994 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000995 struct snd_soc_platform *platform;
996 struct snd_soc_dai *dai;
Mark Brown6b05eda2008-12-08 19:26:48 +0000997 int i, found, ret, ac97;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200998
Mark Brown435c5e22008-12-04 15:32:53 +0000999 if (card->instantiated)
1000 return;
Mark Brown63084192008-12-02 15:08:03 +00001001
Mark Brown435c5e22008-12-04 15:32:53 +00001002 found = 0;
1003 list_for_each_entry(platform, &platform_list, list)
1004 if (card->platform == platform) {
1005 found = 1;
1006 break;
1007 }
1008 if (!found) {
1009 dev_dbg(card->dev, "Platform %s not registered\n",
1010 card->platform->name);
1011 return;
Mark Brownc5af3a22008-11-28 13:29:45 +00001012 }
1013
Mark Brown6b05eda2008-12-08 19:26:48 +00001014 ac97 = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001015 for (i = 0; i < card->num_links; i++) {
1016 found = 0;
1017 list_for_each_entry(dai, &dai_list, list)
1018 if (card->dai_link[i].cpu_dai == dai) {
1019 found = 1;
1020 break;
1021 }
1022 if (!found) {
1023 dev_dbg(card->dev, "DAI %s not registered\n",
1024 card->dai_link[i].cpu_dai->name);
1025 return;
1026 }
Mark Brown6b05eda2008-12-08 19:26:48 +00001027
1028 if (card->dai_link[i].cpu_dai->ac97_control)
1029 ac97 = 1;
Mark Brown435c5e22008-12-04 15:32:53 +00001030 }
1031
Barry Song02a06d32009-10-16 18:13:38 +08001032 for (i = 0; i < card->num_links; i++) {
1033 if (!card->dai_link[i].codec_dai->ops)
1034 card->dai_link[i].codec_dai->ops = &null_dai_ops;
1035 }
1036
Mark Brown6b05eda2008-12-08 19:26:48 +00001037 /* If we have AC97 in the system then don't wait for the
1038 * codec. This will need revisiting if we have to handle
1039 * systems with mixed AC97 and non-AC97 parts. Only check for
1040 * DAIs currently; we can't do this per link since some AC97
1041 * codecs have non-AC97 DAIs.
1042 */
1043 if (!ac97)
1044 for (i = 0; i < card->num_links; i++) {
1045 found = 0;
1046 list_for_each_entry(dai, &dai_list, list)
1047 if (card->dai_link[i].codec_dai == dai) {
1048 found = 1;
1049 break;
1050 }
1051 if (!found) {
1052 dev_dbg(card->dev, "DAI %s not registered\n",
1053 card->dai_link[i].codec_dai->name);
1054 return;
1055 }
1056 }
1057
Mark Brown435c5e22008-12-04 15:32:53 +00001058 /* Note that we do not current check for codec components */
1059
1060 dev_dbg(card->dev, "All components present, instantiating\n");
1061
1062 /* Found everything, bring it up */
Mark Brown96dd3622010-02-12 11:05:44 +00001063 card->pmdown_time = pmdown_time;
1064
Mark Brown87506542008-11-18 20:50:34 +00001065 if (card->probe) {
1066 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001067 if (ret < 0)
Mark Brown435c5e22008-12-04 15:32:53 +00001068 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001069 }
1070
Mark Brown87506542008-11-18 20:50:34 +00001071 for (i = 0; i < card->num_links; i++) {
1072 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001073 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +01001074 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +02001075 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001076 goto cpu_dai_err;
1077 }
1078 }
1079
1080 if (codec_dev->probe) {
1081 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001082 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001083 goto cpu_dai_err;
1084 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001085 codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001086
1087 if (platform->probe) {
1088 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +02001089 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001090 goto platform_err;
1091 }
1092
1093 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +00001094 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +01001095#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001096 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001097 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001098#endif
Andy Green6ed25972008-06-13 16:24:05 +01001099
Mark Brownfe3e78e2009-11-03 22:13:13 +00001100 for (i = 0; i < card->num_links; i++) {
1101 if (card->dai_link[i].init) {
1102 ret = card->dai_link[i].init(codec);
1103 if (ret < 0) {
1104 printk(KERN_ERR "asoc: failed to init %s\n",
1105 card->dai_link[i].stream_name);
1106 continue;
1107 }
1108 }
Barry Songf7732052009-11-12 12:01:47 +08001109 if (card->dai_link[i].codec_dai->ac97_control)
Mark Brownfe3e78e2009-11-03 22:13:13 +00001110 ac97 = 1;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001111 }
1112
1113 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1114 "%s", card->name);
1115 snprintf(codec->card->longname, sizeof(codec->card->longname),
1116 "%s (%s)", card->name, codec->name);
1117
1118 /* Make sure all DAPM widgets are instantiated */
1119 snd_soc_dapm_new_widgets(codec);
1120
1121 ret = snd_card_register(codec->card);
1122 if (ret < 0) {
1123 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1124 codec->name);
1125 goto card_err;
1126 }
1127
1128 mutex_lock(&codec->mutex);
1129#ifdef CONFIG_SND_SOC_AC97_BUS
1130 /* Only instantiate AC97 if not already done by the adaptor
1131 * for the generic AC97 subsystem.
1132 */
1133 if (ac97 && strcmp(codec->name, "AC97") != 0) {
1134 ret = soc_ac97_dev_register(codec);
1135 if (ret < 0) {
1136 printk(KERN_ERR "asoc: AC97 device register failed\n");
1137 snd_card_free(codec->card);
1138 mutex_unlock(&codec->mutex);
1139 goto card_err;
1140 }
1141 }
1142#endif
1143
1144 ret = snd_soc_dapm_sys_add(card->socdev->dev);
1145 if (ret < 0)
1146 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1147
Mark Browndbe21402010-02-12 11:37:24 +00001148 ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time);
1149 if (ret < 0)
1150 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1151
Mark Brownfe3e78e2009-11-03 22:13:13 +00001152 ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg);
1153 if (ret < 0)
1154 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1155
1156 soc_init_codec_debugfs(codec);
1157 mutex_unlock(&codec->mutex);
1158
Mark Brown435c5e22008-12-04 15:32:53 +00001159 card->instantiated = 1;
1160
1161 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001162
Mark Brownfe3e78e2009-11-03 22:13:13 +00001163card_err:
1164 if (platform->remove)
1165 platform->remove(pdev);
1166
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001167platform_err:
1168 if (codec_dev->remove)
1169 codec_dev->remove(pdev);
1170
1171cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +01001172 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +00001173 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001174 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +01001175 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001176 }
1177
Mark Brown87506542008-11-18 20:50:34 +00001178 if (card->remove)
1179 card->remove(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001180}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001181
Mark Brown435c5e22008-12-04 15:32:53 +00001182/*
1183 * Attempt to initialise any uninitalised cards. Must be called with
1184 * client_mutex.
1185 */
1186static void snd_soc_instantiate_cards(void)
1187{
1188 struct snd_soc_card *card;
1189 list_for_each_entry(card, &card_list, list)
1190 snd_soc_instantiate_card(card);
1191}
1192
1193/* probes a new socdev */
1194static int soc_probe(struct platform_device *pdev)
1195{
1196 int ret = 0;
1197 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1198 struct snd_soc_card *card = socdev->card;
1199
1200 /* Bodge while we push things out of socdev */
1201 card->socdev = socdev;
1202
1203 /* Bodge while we unpick instantiation */
1204 card->dev = &pdev->dev;
1205 ret = snd_soc_register_card(card);
1206 if (ret != 0) {
1207 dev_err(&pdev->dev, "Failed to register card\n");
1208 return ret;
1209 }
1210
1211 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001212}
1213
1214/* removes a socdev */
1215static int soc_remove(struct platform_device *pdev)
1216{
1217 int i;
1218 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +00001219 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +00001220 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001221 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1222
Mike Rapoport914dc182009-05-11 13:04:55 +03001223 if (!card->instantiated)
1224 return 0;
1225
Mark Brown63084192008-12-02 15:08:03 +00001226 run_delayed_work(&card->delayed_work);
Liam Girdwood965ac422007-01-31 14:14:57 +01001227
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001228 if (platform->remove)
1229 platform->remove(pdev);
1230
1231 if (codec_dev->remove)
1232 codec_dev->remove(pdev);
1233
Mark Brown87506542008-11-18 20:50:34 +00001234 for (i = 0; i < card->num_links; i++) {
1235 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001236 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +01001237 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001238 }
1239
Mark Brown87506542008-11-18 20:50:34 +00001240 if (card->remove)
1241 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001242
Mark Brownc5af3a22008-11-28 13:29:45 +00001243 snd_soc_unregister_card(card);
1244
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001245 return 0;
1246}
1247
Mark Brown416356f2009-06-30 19:05:15 +01001248static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001249{
Mark Brown416356f2009-06-30 19:05:15 +01001250 struct platform_device *pdev = to_platform_device(dev);
Mark Brown51737472009-06-22 13:16:51 +01001251 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1252 struct snd_soc_card *card = socdev->card;
1253
1254 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001255 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001256
1257 /* Flush out pmdown_time work - we actually do want to run it
1258 * now, we're shutting down so no imminent restart. */
1259 run_delayed_work(&card->delayed_work);
1260
1261 snd_soc_dapm_shutdown(socdev);
Mark Brown416356f2009-06-30 19:05:15 +01001262
1263 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001264}
1265
Alexey Dobriyan47145212009-12-14 18:00:08 -08001266static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001267 .suspend = soc_suspend,
1268 .resume = soc_resume,
1269 .poweroff = soc_poweroff,
1270};
1271
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001272/* ASoC platform driver */
1273static struct platform_driver soc_driver = {
1274 .driver = {
1275 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001276 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001277 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001278 },
1279 .probe = soc_probe,
1280 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001281};
1282
1283/* create a new pcm */
1284static int soc_new_pcm(struct snd_soc_device *socdev,
1285 struct snd_soc_dai_link *dai_link, int num)
1286{
Mark Brown87689d52008-12-02 16:01:14 +00001287 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001288 struct snd_soc_codec *codec = card->codec;
Mark Brown87689d52008-12-02 16:01:14 +00001289 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001290 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
1291 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001292 struct snd_soc_pcm_runtime *rtd;
1293 struct snd_pcm *pcm;
1294 char new_name[64];
1295 int ret = 0, playback = 0, capture = 0;
1296
1297 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1298 if (rtd == NULL)
1299 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +01001300
1301 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001302 rtd->socdev = socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001303 codec_dai->codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001304
1305 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001306 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1307 dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001308
1309 if (codec_dai->playback.channels_min)
1310 playback = 1;
1311 if (codec_dai->capture.channels_min)
1312 capture = 1;
1313
1314 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1315 capture, &pcm);
1316 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001317 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1318 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001319 kfree(rtd);
1320 return ret;
1321 }
1322
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001323 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001324 pcm->private_data = rtd;
Mark Brown87689d52008-12-02 16:01:14 +00001325 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1326 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
1327 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1328 soc_pcm_ops.copy = platform->pcm_ops->copy;
1329 soc_pcm_ops.silence = platform->pcm_ops->silence;
1330 soc_pcm_ops.ack = platform->pcm_ops->ack;
1331 soc_pcm_ops.page = platform->pcm_ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001332
1333 if (playback)
1334 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1335
1336 if (capture)
1337 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1338
Mark Brown87689d52008-12-02 16:01:14 +00001339 ret = platform->pcm_new(codec->card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001340 if (ret < 0) {
1341 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1342 kfree(rtd);
1343 return ret;
1344 }
1345
Mark Brown87689d52008-12-02 16:01:14 +00001346 pcm->private_free = platform->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001347 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1348 cpu_dai->name);
1349 return ret;
1350}
1351
Mark Brown096e49d2009-07-05 15:12:22 +01001352/**
1353 * snd_soc_codec_volatile_register: Report if a register is volatile.
1354 *
1355 * @codec: CODEC to query.
1356 * @reg: Register to query.
1357 *
1358 * Boolean function indiciating if a CODEC register is volatile.
1359 */
1360int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1361{
1362 if (codec->volatile_register)
1363 return codec->volatile_register(reg);
1364 else
1365 return 0;
1366}
1367EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1368
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001369/**
1370 * snd_soc_new_ac97_codec - initailise AC97 device
1371 * @codec: audio codec
1372 * @ops: AC97 bus operations
1373 * @num: AC97 codec number
1374 *
1375 * Initialises AC97 codec resources for use by ad-hoc devices only.
1376 */
1377int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1378 struct snd_ac97_bus_ops *ops, int num)
1379{
1380 mutex_lock(&codec->mutex);
1381
1382 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1383 if (codec->ac97 == NULL) {
1384 mutex_unlock(&codec->mutex);
1385 return -ENOMEM;
1386 }
1387
1388 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1389 if (codec->ac97->bus == NULL) {
1390 kfree(codec->ac97);
1391 codec->ac97 = NULL;
1392 mutex_unlock(&codec->mutex);
1393 return -ENOMEM;
1394 }
1395
1396 codec->ac97->bus->ops = ops;
1397 codec->ac97->num = num;
Mark Brown27186252010-01-28 12:36:29 +00001398 codec->dev = &codec->ac97->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001399 mutex_unlock(&codec->mutex);
1400 return 0;
1401}
1402EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1403
1404/**
1405 * snd_soc_free_ac97_codec - free AC97 codec device
1406 * @codec: audio codec
1407 *
1408 * Frees AC97 codec device resources.
1409 */
1410void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1411{
1412 mutex_lock(&codec->mutex);
1413 kfree(codec->ac97->bus);
1414 kfree(codec->ac97);
1415 codec->ac97 = NULL;
1416 mutex_unlock(&codec->mutex);
1417}
1418EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1419
1420/**
1421 * snd_soc_update_bits - update codec register bits
1422 * @codec: audio codec
1423 * @reg: codec register
1424 * @mask: register mask
1425 * @value: new value
1426 *
1427 * Writes new register value.
1428 *
1429 * Returns 1 for change else 0.
1430 */
1431int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001432 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001433{
1434 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001435 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001436
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001437 old = snd_soc_read(codec, reg);
1438 new = (old & ~mask) | value;
1439 change = old != new;
1440 if (change)
1441 snd_soc_write(codec, reg, new);
1442
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001443 return change;
1444}
1445EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1446
1447/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001448 * snd_soc_update_bits_locked - update codec register bits
1449 * @codec: audio codec
1450 * @reg: codec register
1451 * @mask: register mask
1452 * @value: new value
1453 *
1454 * Writes new register value, and takes the codec mutex.
1455 *
1456 * Returns 1 for change else 0.
1457 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001458int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1459 unsigned short reg, unsigned int mask,
1460 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001461{
1462 int change;
1463
1464 mutex_lock(&codec->mutex);
1465 change = snd_soc_update_bits(codec, reg, mask, value);
1466 mutex_unlock(&codec->mutex);
1467
1468 return change;
1469}
Mark Browndd1b3d52009-12-04 14:22:03 +00001470EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001471
1472/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001473 * snd_soc_test_bits - test register for change
1474 * @codec: audio codec
1475 * @reg: codec register
1476 * @mask: register mask
1477 * @value: new value
1478 *
1479 * Tests a register with a new value and checks if the new value is
1480 * different from the old value.
1481 *
1482 * Returns 1 for change else 0.
1483 */
1484int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001485 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001486{
1487 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001488 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001489
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001490 old = snd_soc_read(codec, reg);
1491 new = (old & ~mask) | value;
1492 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001493
1494 return change;
1495}
1496EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1497
1498/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001499 * snd_soc_new_pcms - create new sound card and pcms
1500 * @socdev: the SoC audio device
Mark Brownac11a2b2009-01-01 12:18:17 +00001501 * @idx: ALSA card index
1502 * @xid: card identification
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001503 *
1504 * Create a new sound card based upon the codec and interface pcms.
1505 *
1506 * Returns 0 for success, else error.
1507 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001508int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001509{
Mark Brown87506542008-11-18 20:50:34 +00001510 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001511 struct snd_soc_codec *codec = card->codec;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001512 int ret, i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001513
1514 mutex_lock(&codec->mutex);
1515
1516 /* register a sound card */
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001517 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1518 if (ret < 0) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001519 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1520 codec->name);
1521 mutex_unlock(&codec->mutex);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001522 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001523 }
1524
Mark Brown452c5ea2009-05-17 21:41:23 +01001525 codec->socdev = socdev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001526 codec->card->dev = socdev->dev;
1527 codec->card->private_data = codec;
1528 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1529
1530 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001531 for (i = 0; i < card->num_links; i++) {
1532 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001533 if (ret < 0) {
1534 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001535 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001536 mutex_unlock(&codec->mutex);
1537 return ret;
1538 }
Barry Songf7732052009-11-12 12:01:47 +08001539 if (card->dai_link[i].codec_dai->ac97_control) {
1540 snd_ac97_dev_add_pdata(codec->ac97,
1541 card->dai_link[i].cpu_dai->ac97_pdata);
1542 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001543 }
1544
1545 mutex_unlock(&codec->mutex);
1546 return ret;
1547}
1548EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1549
1550/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001551 * snd_soc_free_pcms - free sound card and pcms
1552 * @socdev: the SoC audio device
1553 *
1554 * Frees sound card and pcms associated with the socdev.
1555 * Also unregister the codec if it is an AC97 device.
1556 */
1557void snd_soc_free_pcms(struct snd_soc_device *socdev)
1558{
Mark Brown6627a652009-01-23 22:55:23 +00001559 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001560#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001561 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001562 int i;
1563#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001564
1565 mutex_lock(&codec->mutex);
Mark Brown6627a652009-01-23 22:55:23 +00001566 soc_cleanup_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001567#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001568 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001569 codec_dai = &codec->dai[i];
Atsushi Nemotod2314e02009-03-16 23:26:20 +09001570 if (codec_dai->ac97_control && codec->ac97 &&
1571 strcmp(codec->name, "AC97") != 0) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001572 soc_ac97_dev_unregister(codec);
1573 goto free_card;
1574 }
1575 }
1576free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001577#endif
1578
1579 if (codec->card)
1580 snd_card_free(codec->card);
1581 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1582 mutex_unlock(&codec->mutex);
1583}
1584EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1585
1586/**
1587 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1588 * @substream: the pcm substream
1589 * @hw: the hardware parameters
1590 *
1591 * Sets the substream runtime hardware parameters.
1592 */
1593int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1594 const struct snd_pcm_hardware *hw)
1595{
1596 struct snd_pcm_runtime *runtime = substream->runtime;
1597 runtime->hw.info = hw->info;
1598 runtime->hw.formats = hw->formats;
1599 runtime->hw.period_bytes_min = hw->period_bytes_min;
1600 runtime->hw.period_bytes_max = hw->period_bytes_max;
1601 runtime->hw.periods_min = hw->periods_min;
1602 runtime->hw.periods_max = hw->periods_max;
1603 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1604 runtime->hw.fifo_size = hw->fifo_size;
1605 return 0;
1606}
1607EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1608
1609/**
1610 * snd_soc_cnew - create new control
1611 * @_template: control template
1612 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001613 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001614 *
1615 * Create a new mixer control from a template control.
1616 *
1617 * Returns 0 for success, else error.
1618 */
1619struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1620 void *data, char *long_name)
1621{
1622 struct snd_kcontrol_new template;
1623
1624 memcpy(&template, _template, sizeof(template));
1625 if (long_name)
1626 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001627 template.index = 0;
1628
1629 return snd_ctl_new1(&template, data);
1630}
1631EXPORT_SYMBOL_GPL(snd_soc_cnew);
1632
1633/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001634 * snd_soc_add_controls - add an array of controls to a codec.
1635 * Convienience function to add a list of controls. Many codecs were
1636 * duplicating this code.
1637 *
1638 * @codec: codec to add controls to
1639 * @controls: array of controls to add
1640 * @num_controls: number of elements in the array
1641 *
1642 * Return 0 for success, else error.
1643 */
1644int snd_soc_add_controls(struct snd_soc_codec *codec,
1645 const struct snd_kcontrol_new *controls, int num_controls)
1646{
1647 struct snd_card *card = codec->card;
1648 int err, i;
1649
1650 for (i = 0; i < num_controls; i++) {
1651 const struct snd_kcontrol_new *control = &controls[i];
1652 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1653 if (err < 0) {
1654 dev_err(codec->dev, "%s: Failed to add %s\n",
1655 codec->name, control->name);
1656 return err;
1657 }
1658 }
1659
1660 return 0;
1661}
1662EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1663
1664/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001665 * snd_soc_info_enum_double - enumerated double mixer info callback
1666 * @kcontrol: mixer control
1667 * @uinfo: control element information
1668 *
1669 * Callback to provide information about a double enumerated
1670 * mixer control.
1671 *
1672 * Returns 0 for success.
1673 */
1674int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1675 struct snd_ctl_elem_info *uinfo)
1676{
1677 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1678
1679 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1680 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001681 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001682
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001683 if (uinfo->value.enumerated.item > e->max - 1)
1684 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001685 strcpy(uinfo->value.enumerated.name,
1686 e->texts[uinfo->value.enumerated.item]);
1687 return 0;
1688}
1689EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1690
1691/**
1692 * snd_soc_get_enum_double - enumerated double mixer get callback
1693 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001694 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001695 *
1696 * Callback to get the value of a double enumerated mixer.
1697 *
1698 * Returns 0 for success.
1699 */
1700int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1701 struct snd_ctl_elem_value *ucontrol)
1702{
1703 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1704 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001705 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001706
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001707 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001708 ;
1709 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001710 ucontrol->value.enumerated.item[0]
1711 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001712 if (e->shift_l != e->shift_r)
1713 ucontrol->value.enumerated.item[1] =
1714 (val >> e->shift_r) & (bitmask - 1);
1715
1716 return 0;
1717}
1718EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1719
1720/**
1721 * snd_soc_put_enum_double - enumerated double mixer put callback
1722 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001723 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001724 *
1725 * Callback to set the value of a double enumerated mixer.
1726 *
1727 * Returns 0 for success.
1728 */
1729int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1730 struct snd_ctl_elem_value *ucontrol)
1731{
1732 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1733 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001734 unsigned int val;
1735 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001736
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001737 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001738 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001739 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001740 return -EINVAL;
1741 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1742 mask = (bitmask - 1) << e->shift_l;
1743 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001744 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001745 return -EINVAL;
1746 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1747 mask |= (bitmask - 1) << e->shift_r;
1748 }
1749
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001750 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001751}
1752EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1753
1754/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001755 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1756 * @kcontrol: mixer control
1757 * @ucontrol: control element information
1758 *
1759 * Callback to get the value of a double semi enumerated mixer.
1760 *
1761 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1762 * used for handling bitfield coded enumeration for example.
1763 *
1764 * Returns 0 for success.
1765 */
1766int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1767 struct snd_ctl_elem_value *ucontrol)
1768{
1769 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001770 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001771 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001772
1773 reg_val = snd_soc_read(codec, e->reg);
1774 val = (reg_val >> e->shift_l) & e->mask;
1775 for (mux = 0; mux < e->max; mux++) {
1776 if (val == e->values[mux])
1777 break;
1778 }
1779 ucontrol->value.enumerated.item[0] = mux;
1780 if (e->shift_l != e->shift_r) {
1781 val = (reg_val >> e->shift_r) & e->mask;
1782 for (mux = 0; mux < e->max; mux++) {
1783 if (val == e->values[mux])
1784 break;
1785 }
1786 ucontrol->value.enumerated.item[1] = mux;
1787 }
1788
1789 return 0;
1790}
1791EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1792
1793/**
1794 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1795 * @kcontrol: mixer control
1796 * @ucontrol: control element information
1797 *
1798 * Callback to set the value of a double semi enumerated mixer.
1799 *
1800 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1801 * used for handling bitfield coded enumeration for example.
1802 *
1803 * Returns 0 for success.
1804 */
1805int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1806 struct snd_ctl_elem_value *ucontrol)
1807{
1808 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001809 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001810 unsigned int val;
1811 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001812
1813 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1814 return -EINVAL;
1815 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1816 mask = e->mask << e->shift_l;
1817 if (e->shift_l != e->shift_r) {
1818 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1819 return -EINVAL;
1820 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1821 mask |= e->mask << e->shift_r;
1822 }
1823
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001824 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001825}
1826EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1827
1828/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001829 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1830 * @kcontrol: mixer control
1831 * @uinfo: control element information
1832 *
1833 * Callback to provide information about an external enumerated
1834 * single mixer.
1835 *
1836 * Returns 0 for success.
1837 */
1838int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1839 struct snd_ctl_elem_info *uinfo)
1840{
1841 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1842
1843 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1844 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001845 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001846
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001847 if (uinfo->value.enumerated.item > e->max - 1)
1848 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001849 strcpy(uinfo->value.enumerated.name,
1850 e->texts[uinfo->value.enumerated.item]);
1851 return 0;
1852}
1853EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1854
1855/**
1856 * snd_soc_info_volsw_ext - external single mixer info callback
1857 * @kcontrol: mixer control
1858 * @uinfo: control element information
1859 *
1860 * Callback to provide information about a single external mixer control.
1861 *
1862 * Returns 0 for success.
1863 */
1864int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1865 struct snd_ctl_elem_info *uinfo)
1866{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001867 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001868
Mark Brownfd5dfad2009-04-15 21:37:46 +01001869 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001870 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1871 else
1872 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1873
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001874 uinfo->count = 1;
1875 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001876 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001877 return 0;
1878}
1879EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1880
1881/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001882 * snd_soc_info_volsw - single mixer info callback
1883 * @kcontrol: mixer control
1884 * @uinfo: control element information
1885 *
1886 * Callback to provide information about a single mixer control.
1887 *
1888 * Returns 0 for success.
1889 */
1890int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1891 struct snd_ctl_elem_info *uinfo)
1892{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001893 struct soc_mixer_control *mc =
1894 (struct soc_mixer_control *)kcontrol->private_value;
1895 int max = mc->max;
Mark Brown762b8df2008-10-30 12:37:08 +00001896 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001897 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001898
Mark Brownfd5dfad2009-04-15 21:37:46 +01001899 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001900 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1901 else
1902 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1903
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001904 uinfo->count = shift == rshift ? 1 : 2;
1905 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001906 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001907 return 0;
1908}
1909EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1910
1911/**
1912 * snd_soc_get_volsw - single mixer get callback
1913 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001914 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001915 *
1916 * Callback to get the value of a single mixer control.
1917 *
1918 * Returns 0 for success.
1919 */
1920int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1921 struct snd_ctl_elem_value *ucontrol)
1922{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001923 struct soc_mixer_control *mc =
1924 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001925 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001926 unsigned int reg = mc->reg;
1927 unsigned int shift = mc->shift;
1928 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001929 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001930 unsigned int mask = (1 << fls(max)) - 1;
1931 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001932
1933 ucontrol->value.integer.value[0] =
1934 (snd_soc_read(codec, reg) >> shift) & mask;
1935 if (shift != rshift)
1936 ucontrol->value.integer.value[1] =
1937 (snd_soc_read(codec, reg) >> rshift) & mask;
1938 if (invert) {
1939 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001940 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001941 if (shift != rshift)
1942 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001943 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001944 }
1945
1946 return 0;
1947}
1948EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1949
1950/**
1951 * snd_soc_put_volsw - single mixer put callback
1952 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001953 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001954 *
1955 * Callback to set the value of a single mixer control.
1956 *
1957 * Returns 0 for success.
1958 */
1959int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1960 struct snd_ctl_elem_value *ucontrol)
1961{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001962 struct soc_mixer_control *mc =
1963 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001964 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001965 unsigned int reg = mc->reg;
1966 unsigned int shift = mc->shift;
1967 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001968 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001969 unsigned int mask = (1 << fls(max)) - 1;
1970 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001971 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001972
1973 val = (ucontrol->value.integer.value[0] & mask);
1974 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001975 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001976 val_mask = mask << shift;
1977 val = val << shift;
1978 if (shift != rshift) {
1979 val2 = (ucontrol->value.integer.value[1] & mask);
1980 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001981 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001982 val_mask |= mask << rshift;
1983 val |= val2 << rshift;
1984 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001985 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001986}
1987EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1988
1989/**
1990 * snd_soc_info_volsw_2r - double mixer info callback
1991 * @kcontrol: mixer control
1992 * @uinfo: control element information
1993 *
1994 * Callback to provide information about a double mixer control that
1995 * spans 2 codec registers.
1996 *
1997 * Returns 0 for success.
1998 */
1999int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2000 struct snd_ctl_elem_info *uinfo)
2001{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002002 struct soc_mixer_control *mc =
2003 (struct soc_mixer_control *)kcontrol->private_value;
2004 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002005
Mark Brownfd5dfad2009-04-15 21:37:46 +01002006 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002007 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2008 else
2009 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2010
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002011 uinfo->count = 2;
2012 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002013 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002014 return 0;
2015}
2016EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2017
2018/**
2019 * snd_soc_get_volsw_2r - double mixer get callback
2020 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002021 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002022 *
2023 * Callback to get the value of a double mixer control that spans 2 registers.
2024 *
2025 * Returns 0 for success.
2026 */
2027int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2028 struct snd_ctl_elem_value *ucontrol)
2029{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002030 struct soc_mixer_control *mc =
2031 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002032 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002033 unsigned int reg = mc->reg;
2034 unsigned int reg2 = mc->rreg;
2035 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002036 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002037 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002038 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002039
2040 ucontrol->value.integer.value[0] =
2041 (snd_soc_read(codec, reg) >> shift) & mask;
2042 ucontrol->value.integer.value[1] =
2043 (snd_soc_read(codec, reg2) >> shift) & mask;
2044 if (invert) {
2045 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002046 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002047 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002048 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002049 }
2050
2051 return 0;
2052}
2053EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2054
2055/**
2056 * snd_soc_put_volsw_2r - double mixer set callback
2057 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002058 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002059 *
2060 * Callback to set the value of a double mixer control that spans 2 registers.
2061 *
2062 * Returns 0 for success.
2063 */
2064int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2065 struct snd_ctl_elem_value *ucontrol)
2066{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002067 struct soc_mixer_control *mc =
2068 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002069 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002070 unsigned int reg = mc->reg;
2071 unsigned int reg2 = mc->rreg;
2072 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002073 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002074 unsigned int mask = (1 << fls(max)) - 1;
2075 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002076 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002077 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002078
2079 val_mask = mask << shift;
2080 val = (ucontrol->value.integer.value[0] & mask);
2081 val2 = (ucontrol->value.integer.value[1] & mask);
2082
2083 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002084 val = max - val;
2085 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002086 }
2087
2088 val = val << shift;
2089 val2 = val2 << shift;
2090
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002091 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002092 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002093 return err;
2094
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002095 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002096 return err;
2097}
2098EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2099
Mark Browne13ac2e2008-05-28 17:58:05 +01002100/**
2101 * snd_soc_info_volsw_s8 - signed mixer info callback
2102 * @kcontrol: mixer control
2103 * @uinfo: control element information
2104 *
2105 * Callback to provide information about a signed mixer control.
2106 *
2107 * Returns 0 for success.
2108 */
2109int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2110 struct snd_ctl_elem_info *uinfo)
2111{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002112 struct soc_mixer_control *mc =
2113 (struct soc_mixer_control *)kcontrol->private_value;
2114 int max = mc->max;
2115 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002116
2117 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2118 uinfo->count = 2;
2119 uinfo->value.integer.min = 0;
2120 uinfo->value.integer.max = max-min;
2121 return 0;
2122}
2123EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2124
2125/**
2126 * snd_soc_get_volsw_s8 - signed mixer get callback
2127 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002128 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002129 *
2130 * Callback to get the value of a signed mixer control.
2131 *
2132 * Returns 0 for success.
2133 */
2134int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2135 struct snd_ctl_elem_value *ucontrol)
2136{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002137 struct soc_mixer_control *mc =
2138 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002139 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002140 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002141 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002142 int val = snd_soc_read(codec, reg);
2143
2144 ucontrol->value.integer.value[0] =
2145 ((signed char)(val & 0xff))-min;
2146 ucontrol->value.integer.value[1] =
2147 ((signed char)((val >> 8) & 0xff))-min;
2148 return 0;
2149}
2150EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2151
2152/**
2153 * snd_soc_put_volsw_sgn - signed mixer put callback
2154 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002155 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002156 *
2157 * Callback to set the value of a signed mixer control.
2158 *
2159 * Returns 0 for success.
2160 */
2161int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2162 struct snd_ctl_elem_value *ucontrol)
2163{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002164 struct soc_mixer_control *mc =
2165 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002166 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002167 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002168 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002169 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002170
2171 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2172 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2173
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002174 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002175}
2176EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2177
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002178/**
2179 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2180 * @dai: DAI
2181 * @clk_id: DAI specific clock ID
2182 * @freq: new clock frequency in Hz
2183 * @dir: new clock direction - input/output.
2184 *
2185 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2186 */
2187int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2188 unsigned int freq, int dir)
2189{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002190 if (dai->ops && dai->ops->set_sysclk)
Eric Miao6335d052009-03-03 09:41:00 +08002191 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002192 else
2193 return -EINVAL;
2194}
2195EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2196
2197/**
2198 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2199 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002200 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002201 * @div: new clock divisor.
2202 *
2203 * Configures the clock dividers. This is used to derive the best DAI bit and
2204 * frame clocks from the system or master clock. It's best to set the DAI bit
2205 * and frame clocks as low as possible to save system power.
2206 */
2207int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2208 int div_id, int div)
2209{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002210 if (dai->ops && dai->ops->set_clkdiv)
Eric Miao6335d052009-03-03 09:41:00 +08002211 return dai->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002212 else
2213 return -EINVAL;
2214}
2215EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2216
2217/**
2218 * snd_soc_dai_set_pll - configure DAI PLL.
2219 * @dai: DAI
2220 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002221 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002222 * @freq_in: PLL input clock frequency in Hz
2223 * @freq_out: requested PLL output clock frequency in Hz
2224 *
2225 * Configures and enables PLL to generate output clock based on input clock.
2226 */
Mark Brown85488032009-09-05 18:52:16 +01002227int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2228 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002229{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002230 if (dai->ops && dai->ops->set_pll)
Mark Brown85488032009-09-05 18:52:16 +01002231 return dai->ops->set_pll(dai, pll_id, source,
2232 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002233 else
2234 return -EINVAL;
2235}
2236EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2237
2238/**
2239 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2240 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002241 * @fmt: SND_SOC_DAIFMT_ format value.
2242 *
2243 * Configures the DAI hardware format and clocking.
2244 */
2245int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2246{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002247 if (dai->ops && dai->ops->set_fmt)
Eric Miao6335d052009-03-03 09:41:00 +08002248 return dai->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002249 else
2250 return -EINVAL;
2251}
2252EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2253
2254/**
2255 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2256 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002257 * @tx_mask: bitmask representing active TX slots.
2258 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002259 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002260 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002261 *
2262 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2263 * specific.
2264 */
2265int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002266 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002267{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002268 if (dai->ops && dai->ops->set_tdm_slot)
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002269 return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2270 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002271 else
2272 return -EINVAL;
2273}
2274EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2275
2276/**
Barry Song472df3c2009-09-12 01:16:29 +08002277 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2278 * @dai: DAI
2279 * @tx_num: how many TX channels
2280 * @tx_slot: pointer to an array which imply the TX slot number channel
2281 * 0~num-1 uses
2282 * @rx_num: how many RX channels
2283 * @rx_slot: pointer to an array which imply the RX slot number channel
2284 * 0~num-1 uses
2285 *
2286 * configure the relationship between channel number and TDM slot number.
2287 */
2288int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2289 unsigned int tx_num, unsigned int *tx_slot,
2290 unsigned int rx_num, unsigned int *rx_slot)
2291{
2292 if (dai->ops && dai->ops->set_channel_map)
2293 return dai->ops->set_channel_map(dai, tx_num, tx_slot,
2294 rx_num, rx_slot);
2295 else
2296 return -EINVAL;
2297}
2298EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2299
2300/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002301 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2302 * @dai: DAI
2303 * @tristate: tristate enable
2304 *
2305 * Tristates the DAI so that others can use it.
2306 */
2307int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2308{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002309 if (dai->ops && dai->ops->set_tristate)
Eric Miao6335d052009-03-03 09:41:00 +08002310 return dai->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002311 else
2312 return -EINVAL;
2313}
2314EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2315
2316/**
2317 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2318 * @dai: DAI
2319 * @mute: mute enable
2320 *
2321 * Mutes the DAI DAC.
2322 */
2323int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2324{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002325 if (dai->ops && dai->ops->digital_mute)
Eric Miao6335d052009-03-03 09:41:00 +08002326 return dai->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002327 else
2328 return -EINVAL;
2329}
2330EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2331
Mark Brownc5af3a22008-11-28 13:29:45 +00002332/**
2333 * snd_soc_register_card - Register a card with the ASoC core
2334 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002335 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002336 *
2337 * Note that currently this is an internal only function: it will be
2338 * exposed to machine drivers after further backporting of ASoC v2
2339 * registration APIs.
2340 */
2341static int snd_soc_register_card(struct snd_soc_card *card)
2342{
2343 if (!card->name || !card->dev)
2344 return -EINVAL;
2345
2346 INIT_LIST_HEAD(&card->list);
2347 card->instantiated = 0;
2348
2349 mutex_lock(&client_mutex);
2350 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002351 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002352 mutex_unlock(&client_mutex);
2353
2354 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2355
2356 return 0;
2357}
2358
2359/**
2360 * snd_soc_unregister_card - Unregister a card with the ASoC core
2361 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002362 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002363 *
2364 * Note that currently this is an internal only function: it will be
2365 * exposed to machine drivers after further backporting of ASoC v2
2366 * registration APIs.
2367 */
2368static int snd_soc_unregister_card(struct snd_soc_card *card)
2369{
2370 mutex_lock(&client_mutex);
2371 list_del(&card->list);
2372 mutex_unlock(&client_mutex);
2373
2374 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2375
2376 return 0;
2377}
2378
Mark Brown91151712008-11-30 23:31:24 +00002379/**
2380 * snd_soc_register_dai - Register a DAI with the ASoC core
2381 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002382 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002383 */
2384int snd_soc_register_dai(struct snd_soc_dai *dai)
2385{
2386 if (!dai->name)
2387 return -EINVAL;
2388
2389 /* The device should become mandatory over time */
2390 if (!dai->dev)
2391 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2392
Eric Miao6335d052009-03-03 09:41:00 +08002393 if (!dai->ops)
2394 dai->ops = &null_dai_ops;
2395
Mark Brown91151712008-11-30 23:31:24 +00002396 INIT_LIST_HEAD(&dai->list);
2397
2398 mutex_lock(&client_mutex);
2399 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002400 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002401 mutex_unlock(&client_mutex);
2402
2403 pr_debug("Registered DAI '%s'\n", dai->name);
2404
2405 return 0;
2406}
2407EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2408
2409/**
2410 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2411 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002412 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002413 */
2414void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2415{
2416 mutex_lock(&client_mutex);
2417 list_del(&dai->list);
2418 mutex_unlock(&client_mutex);
2419
2420 pr_debug("Unregistered DAI '%s'\n", dai->name);
2421}
2422EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2423
2424/**
2425 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2426 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002427 * @dai: Array of DAIs to register
2428 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002429 */
2430int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2431{
2432 int i, ret;
2433
2434 for (i = 0; i < count; i++) {
2435 ret = snd_soc_register_dai(&dai[i]);
2436 if (ret != 0)
2437 goto err;
2438 }
2439
2440 return 0;
2441
2442err:
2443 for (i--; i >= 0; i--)
2444 snd_soc_unregister_dai(&dai[i]);
2445
2446 return ret;
2447}
2448EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2449
2450/**
2451 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2452 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002453 * @dai: Array of DAIs to unregister
2454 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002455 */
2456void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2457{
2458 int i;
2459
2460 for (i = 0; i < count; i++)
2461 snd_soc_unregister_dai(&dai[i]);
2462}
2463EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2464
Mark Brown12a48a8c2008-12-03 19:40:30 +00002465/**
2466 * snd_soc_register_platform - Register a platform with the ASoC core
2467 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002468 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00002469 */
2470int snd_soc_register_platform(struct snd_soc_platform *platform)
2471{
2472 if (!platform->name)
2473 return -EINVAL;
2474
2475 INIT_LIST_HEAD(&platform->list);
2476
2477 mutex_lock(&client_mutex);
2478 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002479 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00002480 mutex_unlock(&client_mutex);
2481
2482 pr_debug("Registered platform '%s'\n", platform->name);
2483
2484 return 0;
2485}
2486EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2487
2488/**
2489 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2490 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002491 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002492 */
2493void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2494{
2495 mutex_lock(&client_mutex);
2496 list_del(&platform->list);
2497 mutex_unlock(&client_mutex);
2498
2499 pr_debug("Unregistered platform '%s'\n", platform->name);
2500}
2501EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2502
Mark Brown151ab222009-05-09 16:22:58 +01002503static u64 codec_format_map[] = {
2504 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2505 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2506 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2507 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2508 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2509 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2510 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2511 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2512 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2513 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2514 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2515 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2516 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2517 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2518 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2519 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2520};
2521
2522/* Fix up the DAI formats for endianness: codecs don't actually see
2523 * the endianness of the data but we're using the CPU format
2524 * definitions which do need to include endianness so we ensure that
2525 * codec DAIs always have both big and little endian variants set.
2526 */
2527static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2528{
2529 int i;
2530
2531 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2532 if (stream->formats & codec_format_map[i])
2533 stream->formats |= codec_format_map[i];
2534}
2535
Mark Brown0d0cf002008-12-10 14:32:45 +00002536/**
2537 * snd_soc_register_codec - Register a codec with the ASoC core
2538 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002539 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00002540 */
2541int snd_soc_register_codec(struct snd_soc_codec *codec)
2542{
Mark Brown151ab222009-05-09 16:22:58 +01002543 int i;
2544
Mark Brown0d0cf002008-12-10 14:32:45 +00002545 if (!codec->name)
2546 return -EINVAL;
2547
2548 /* The device should become mandatory over time */
2549 if (!codec->dev)
2550 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2551
2552 INIT_LIST_HEAD(&codec->list);
2553
Mark Brown151ab222009-05-09 16:22:58 +01002554 for (i = 0; i < codec->num_dai; i++) {
2555 fixup_codec_formats(&codec->dai[i].playback);
2556 fixup_codec_formats(&codec->dai[i].capture);
2557 }
2558
Mark Brown0d0cf002008-12-10 14:32:45 +00002559 mutex_lock(&client_mutex);
2560 list_add(&codec->list, &codec_list);
2561 snd_soc_instantiate_cards();
2562 mutex_unlock(&client_mutex);
2563
2564 pr_debug("Registered codec '%s'\n", codec->name);
2565
2566 return 0;
2567}
2568EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2569
2570/**
2571 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2572 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002573 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00002574 */
2575void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2576{
2577 mutex_lock(&client_mutex);
2578 list_del(&codec->list);
2579 mutex_unlock(&client_mutex);
2580
2581 pr_debug("Unregistered codec '%s'\n", codec->name);
2582}
2583EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2584
Takashi Iwaic9b3a402008-12-10 07:47:22 +01002585static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002586{
Mark Brown384c89e2008-12-03 17:34:03 +00002587#ifdef CONFIG_DEBUG_FS
2588 debugfs_root = debugfs_create_dir("asoc", NULL);
2589 if (IS_ERR(debugfs_root) || !debugfs_root) {
2590 printk(KERN_WARNING
2591 "ASoC: Failed to create debugfs directory\n");
2592 debugfs_root = NULL;
2593 }
2594#endif
2595
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002596 return platform_driver_register(&soc_driver);
2597}
2598
Mark Brown7d8c16a2008-11-30 22:11:24 +00002599static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002600{
Mark Brown384c89e2008-12-03 17:34:03 +00002601#ifdef CONFIG_DEBUG_FS
2602 debugfs_remove_recursive(debugfs_root);
2603#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02002604 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002605}
2606
2607module_init(snd_soc_init);
2608module_exit(snd_soc_exit);
2609
2610/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002611MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002612MODULE_DESCRIPTION("ALSA SoC Core");
2613MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02002614MODULE_ALIAS("platform:soc-audio");